ai/formula/candidates.hpp

Go to the documentation of this file.
00001 /*
00002    Copyright (C) 2009 - 2012 by Bartosz Waresiak <dragonking@o2.pl>
00003    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 2 of the License, or
00008    (at your option) any later version.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 
00015 /**
00016  * @file
00017  * Defines formula ai candidate actions - headers
00018  * */
00019 
00020 #ifndef AI_FORMULA_CANDIDATES_HPP_INCLUDED
00021 #define AI_FORMULA_CANDIDATES_HPP_INCLUDED
00022 
00023 #include "formula_function.hpp"
00024 
00025 #include <set>
00026 
00027 class unit_map;
00028 class config;
00029 
00030 namespace ai {
00031 class formula_ai;
00032 }
00033 
00034 namespace game_logic {
00035 
00036 class base_candidate_action;
00037 
00038 typedef std::map< std::string, game_logic::const_formula_ptr > candidate_action_filters;
00039 typedef boost::shared_ptr<game_logic::base_candidate_action> candidate_action_ptr;
00040 
00041 //every new candidate action type should be derived from this class
00042 //and should complete evaluate and update_callable_map methods
00043 class base_candidate_action {
00044 public:
00045     base_candidate_action(const std::string& name, const std::string& type,const config& cfg, function_symbol_table* function_table);
00046 
00047     virtual ~base_candidate_action() {}
00048 
00049     //evaluate candidate action using eval_ formula
00050     virtual void evaluate(ai::formula_ai* /*ai*/, unit_map& /*units*/) {}
00051 
00052     //adds needed callable objects to callable map
00053     virtual void update_callable_map(game_logic::map_formula_callable& /*callable*/) {}
00054 
00055 
00056     //return score of last evaluation
00057     int get_score() const {return score_;}
00058 
00059     const_formula_ptr& get_action() {return action_;}
00060 
00061     const std::string& get_name() const { return name_;}
00062     const std::string& get_type() const { return type_;}
00063 
00064 protected:
00065     int execute_formula(const const_formula_ptr& formula,
00066             const game_logic::formula_callable& callable, const ai::formula_ai* ai);
00067 
00068     std::string name_;
00069     std::string type_;
00070     const_formula_ptr eval_;
00071     const_formula_ptr action_;
00072     int score_;
00073 };
00074 
00075 struct candidate_action_compare {
00076     bool operator() (const candidate_action_ptr laction,
00077             const candidate_action_ptr raction) const
00078     {
00079         return laction->get_score() > raction->get_score();
00080     }
00081 };
00082 
00083 typedef std::set<game_logic::candidate_action_ptr, game_logic::candidate_action_compare> candidate_action_set;
00084 
00085 //this class is responsible for managing candidate actions
00086 class candidate_action_manager {
00087 public:
00088     candidate_action_manager()
00089         : evaluated_candidate_actions_()
00090         , candidate_actions_()
00091     {}
00092 
00093     //register candidate actions from config
00094     void load_config(const config& cfg, ai::formula_ai* ai, function_symbol_table* function_table);
00095 
00096     //register a single candidate action from config
00097     candidate_action_ptr load_candidate_action_from_config(const config& cfg, ai::formula_ai* ai, function_symbol_table* function_table);
00098 
00099     //evaluate candidate action, return true if we have candidate action that have score > 0
00100     bool evaluate_candidate_actions(ai::formula_ai* ai, unit_map& units);
00101 
00102     const_formula_ptr get_best_action_formula() const {
00103         if( evaluated_candidate_actions_.empty() )
00104             return game_logic::formula_ptr();
00105         return (*evaluated_candidate_actions_.begin())->get_action();
00106     }
00107 
00108     //calls same method from best candidate action
00109     void update_callable_map(game_logic::map_formula_callable& callable){
00110         if( evaluated_candidate_actions_.empty() )
00111             return;
00112         (*evaluated_candidate_actions_.begin())->update_callable_map(callable);
00113     }
00114 
00115     void register_candidate_action(candidate_action_ptr& candidate_action){
00116         candidate_actions_.push_back(candidate_action);
00117     }
00118 
00119     bool has_candidate_actions() const { return !candidate_actions_.empty(); }
00120 
00121     void clear() {
00122         candidate_actions_.clear();
00123         evaluated_candidate_actions_.clear();
00124     }
00125 
00126 private:
00127     game_logic::candidate_action_set evaluated_candidate_actions_;
00128     std::vector<candidate_action_ptr> candidate_actions_;
00129 };
00130 
00131 
00132 class candidate_action_with_filters : public base_candidate_action {
00133 public:
00134     candidate_action_with_filters(const std::string& name, const std::string& type,const config& cfg, function_symbol_table* function_table);
00135 protected:
00136         variant do_filtering(ai::formula_ai* ai, variant& input, game_logic::const_formula_ptr formula);
00137 
00138     game_logic::candidate_action_filters filter_map_;
00139 };
00140 
00141 class move_candidate_action : public candidate_action_with_filters {
00142 public:
00143     move_candidate_action(const std::string& name, const std::string& type,const config& cfg, function_symbol_table* function_table);
00144 
00145     virtual void evaluate(ai::formula_ai* ai, unit_map& units);
00146 
00147     virtual void update_callable_map(game_logic::map_formula_callable& callable);
00148 
00149 protected:
00150     variant my_unit_;
00151 };
00152 
00153 class attack_candidate_action : public candidate_action_with_filters {
00154 public:
00155     attack_candidate_action(const std::string& name, const std::string& type,const config& cfg, function_symbol_table* function_table);
00156 
00157     virtual void evaluate(ai::formula_ai* ai, unit_map& units);
00158 
00159     virtual void update_callable_map(game_logic::map_formula_callable& callable);
00160 protected:
00161     variant my_unit_;
00162     variant enemy_unit_;
00163 };
00164 
00165 }
00166 
00167 #endif  /* AI_FORMULA_CANDIDATES_HPP_INCLUDED */
00168 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Wed May 23 2012 01:02:31 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs