00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "ai.hpp"
00023 #include "aspect.hpp"
00024 #include "engine.hpp"
00025 #include "goal.hpp"
00026 #include "property_handler.hpp"
00027 #include "stage.hpp"
00028 #include "../manager.hpp"
00029 #include "../../actions.hpp"
00030 #include "../../foreach.hpp"
00031 #include "../../log.hpp"
00032
00033 #include <boost/bind.hpp>
00034 #include <boost/function.hpp>
00035
00036 namespace ai {
00037
00038 static lg::log_domain log_ai_composite("ai/composite");
00039 #define DBG_AI_COMPOSITE LOG_STREAM(debug, log_ai_composite)
00040 #define LOG_AI_COMPOSITE LOG_STREAM(info, log_ai_composite)
00041 #define ERR_AI_COMPOSITE LOG_STREAM(err, log_ai_composite)
00042
00043
00044
00045
00046 std::string ai_composite::describe_self() const
00047 {
00048 return "[composite_ai]";
00049 }
00050
00051 ai_composite::ai_composite( default_ai_context &context, const config &cfg)
00052 : cfg_(cfg),stages_(),recursion_counter_(context.get_recursion_count())
00053 {
00054 init_default_ai_context_proxy(context);
00055 }
00056
00057 void ai_composite::on_create()
00058 {
00059 LOG_AI_COMPOSITE << "side "<< get_side() << " : "<<" created AI with id=["<<
00060 cfg_["id"]<<"]"<<std::endl;
00061
00062
00063 foreach(const config &cfg_element, cfg_.child_range("stage")){
00064 add_stage(cfg_element);
00065 }
00066
00067 config cfg;
00068 cfg["engine"] = "fai";
00069 engine_ptr e_ptr = get_engine_by_cfg(cfg);
00070 if (e_ptr) {
00071 e_ptr->set_ai_context(this);
00072 }
00073
00074 boost::function2<void, std::vector<engine_ptr>&, const config&> factory_engines =
00075 boost::bind(&ai::ai_composite::create_engine,*this,_1,_2);
00076
00077 boost::function2<void, std::vector<goal_ptr>&, const config&> factory_goals =
00078 boost::bind(&ai::ai_composite::create_goal,*this,_1,_2);
00079
00080 boost::function2<void, std::vector<stage_ptr>&, const config&> factory_stages =
00081 boost::bind(&ai::ai_composite::create_stage,*this,_1,_2);
00082
00083 register_vector_property(property_handlers(),"engine",get_engines(), factory_engines);
00084 register_vector_property(property_handlers(),"goal",get_goals(), factory_goals);
00085 register_vector_property(property_handlers(),"stage",stages_, factory_stages);
00086 register_aspect_property(property_handlers(),"aspect",get_aspects());
00087
00088 }
00089
00090
00091 void ai_composite::create_stage(std::vector<stage_ptr> &stages, const config &cfg)
00092 {
00093 engine::parse_stage_from_config(*this,cfg,std::back_inserter(stages));
00094 }
00095
00096
00097 void ai_composite::create_goal(std::vector<goal_ptr> &goals, const config &cfg)
00098 {
00099 engine::parse_goal_from_config(*this,cfg,std::back_inserter(goals));
00100 }
00101
00102
00103 void ai_composite::create_engine(std::vector<engine_ptr> &engines, const config &cfg)
00104 {
00105 engine::parse_engine_from_config(*this,cfg,std::back_inserter(engines));
00106 }
00107
00108 ai_composite::~ai_composite()
00109 {
00110 }
00111
00112
00113 bool ai_composite::add_stage(const config &cfg)
00114 {
00115 std::vector< stage_ptr > stages;
00116 create_stage(stages,cfg);
00117 int j=0;
00118 foreach (stage_ptr b, stages ){
00119 stages_.push_back(b);
00120 j++;
00121 }
00122 return (j>0);
00123 }
00124
00125
00126 bool ai_composite::add_goal(const config &cfg)
00127 {
00128 std::vector< goal_ptr > goals;
00129 create_goal(goals,cfg);
00130 int j=0;
00131 foreach (goal_ptr b, goals ){
00132 get_goals().push_back(b);
00133 j++;
00134 }
00135 return (j>0);
00136 }
00137
00138
00139 void ai_composite::play_turn(){
00140 foreach(stage_ptr &s, stages_){
00141 s->play_stage();
00142 }
00143 }
00144
00145
00146 std::string ai_composite::get_id() const
00147 {
00148 return cfg_["id"];
00149 }
00150
00151
00152
00153 std::string ai_composite::get_name() const
00154 {
00155 return cfg_["name"];
00156 }
00157
00158
00159 std::string ai_composite::get_engine() const
00160 {
00161 return cfg_["engine"];
00162 }
00163
00164
00165 std::string ai_composite::evaluate(const std::string& str)
00166 {
00167 config cfg;
00168 cfg["engine"] = "fai";
00169 engine_ptr e_ptr = get_engine_by_cfg(cfg);
00170 if (!e_ptr) {
00171 return interface::evaluate(str);
00172 }
00173 return e_ptr->evaluate(str);
00174 }
00175
00176
00177 void ai_composite::new_turn()
00178 {
00179
00180 recalculate_move_maps();
00181 invalidate_defensive_position_cache();
00182 invalidate_keeps_cache();
00183 clear_additional_targets();
00184 unit_stats_cache().clear();
00185 }
00186
00187
00188 int ai_composite::get_recursion_count() const
00189 {
00190 return recursion_counter_.get_count();
00191 }
00192
00193 void ai_composite::switch_side(side_number side)
00194 {
00195 set_side(side);
00196 }
00197
00198 ai_context& ai_composite::get_ai_context()
00199 {
00200 return *this;
00201 }
00202
00203
00204 config ai_composite::to_config() const
00205 {
00206 config cfg;
00207
00208
00209 foreach(const stage_ptr &s, stages_){
00210 cfg.add_child("stage",s->to_config());
00211 }
00212
00213 return cfg;
00214 }
00215
00216 }