ai/composite/engine_default.cpp

Go to the documentation of this file.
00001 /* $Id: engine_default.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2009 - 2012 by Yurii Chernyi <terraninfo@terraninfo.net>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 /**
00017  * CPP AI Support engine - creating specific ai components from config
00018  * @file
00019  */
00020 
00021 #include "ai.hpp"
00022 #include "aspect.hpp"
00023 #include "goal.hpp"
00024 #include "engine_default.hpp"
00025 #include "rca.hpp"
00026 #include "stage.hpp"
00027 #include "../../foreach.hpp"
00028 #include "../../log.hpp"
00029 
00030 namespace ai {
00031 
00032 static lg::log_domain log_ai_engine_cpp("ai/engine/cpp");
00033 #define DBG_AI_ENGINE_CPP LOG_STREAM(debug, log_ai_engine_cpp)
00034 #define LOG_AI_ENGINE_CPP LOG_STREAM(info, log_ai_engine_cpp)
00035 #define ERR_AI_ENGINE_CPP LOG_STREAM(err, log_ai_engine_cpp)
00036 
00037 engine_cpp::engine_cpp( readonly_context &context, const config &cfg )
00038     : engine(context,cfg)
00039 {
00040     name_ = "cpp";
00041 }
00042 
00043 engine_cpp::~engine_cpp()
00044 {
00045 }
00046 
00047 
00048 void engine_cpp::do_parse_aspect_from_config( const config &cfg, const std::string &id, std::back_insert_iterator<std::vector< aspect_ptr > > b )
00049 {
00050     const std::string aspect_factory_key = id+"*"+cfg["name"];//@note: hack which combines aspect id and name to get the std::string key of the aspect factory
00051     aspect_factory::factory_map::iterator f = aspect_factory::get_list().find(aspect_factory_key);
00052     if (f == aspect_factory::get_list().end()){
00053         ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNKNOWN aspect["<<aspect_factory_key<<"]" << std::endl;
00054         DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
00055         return;
00056     }
00057     aspect_ptr new_aspect = f->second->get_new_instance(ai_,cfg,id);
00058     if (!new_aspect) {
00059         ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNABLE TO CREATE aspect, key=["<<aspect_factory_key<<"]"<< std::endl;
00060         DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
00061         return;
00062     }
00063     *b = new_aspect;
00064 }
00065 
00066 
00067 void engine_cpp::do_parse_candidate_action_from_config( rca_context &context, const config &cfg, std::back_insert_iterator<std::vector< candidate_action_ptr > > b ){
00068     candidate_action_factory::factory_map::iterator f = candidate_action_factory::get_list().find(cfg["name"]);
00069     if (f == candidate_action_factory::get_list().end()){
00070         ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNKNOWN candidate_action["<<cfg["name"]<<"]"<< std::endl;
00071         DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
00072         return;
00073     }
00074     candidate_action_ptr new_candidate_action = f->second->get_new_instance(context,cfg);
00075     if (!new_candidate_action) {
00076         ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNABLE TO CREATE candidate_action["<<cfg["name"]<<"]"<< std::endl;
00077         DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
00078         return;
00079     }
00080     *b = new_candidate_action;
00081 
00082 }
00083 
00084 void engine_cpp::do_parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr > > b )
00085 {
00086     stage_factory::factory_map::iterator f = stage_factory::get_list().find(cfg["name"]);
00087     if (f == stage_factory::get_list().end()){
00088         ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNKNOWN stage["<<cfg["name"]<<"]"<< std::endl;
00089         DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
00090         return;
00091     }
00092     stage_ptr new_stage = f->second->get_new_instance(context,cfg);
00093     if (!new_stage) {
00094         ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNABLE TO CREATE stage["<<cfg["name"]<<"]"<< std::endl;
00095         DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
00096         return;
00097     }
00098     *b = new_stage;
00099 }
00100 
00101 
00102 void engine_cpp::do_parse_goal_from_config(const config &cfg, std::back_insert_iterator<std::vector< goal_ptr > > b )
00103 {
00104     goal_factory::factory_map::iterator f = goal_factory::get_list().find(cfg["name"]);
00105     if (f == goal_factory::get_list().end()){
00106         ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNKNOWN goal["<<cfg["name"]<<"]"<< std::endl;
00107         DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
00108         return;
00109     }
00110     goal_ptr new_goal = f->second->get_new_instance(ai_,cfg);
00111     if (!new_goal) {
00112         ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNABLE TO CREATE goal["<<cfg["name"]<<"]"<< std::endl;
00113         DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
00114         return;
00115     }
00116     *b = new_goal;
00117 }
00118 
00119 
00120 void engine_cpp::do_parse_engine_from_config(const config &cfg, std::back_insert_iterator<std::vector< engine_ptr > > b )
00121 {
00122     engine_factory::factory_map::iterator f = engine_factory::get_list().find(cfg["name"]);
00123     if (f == engine_factory::get_list().end()){
00124         ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNKNOWN engine["<<cfg["name"]<<"]"<< std::endl;
00125         DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
00126         return;
00127     }
00128     engine_ptr new_engine = f->second->get_new_instance(ai_,cfg);
00129     if (!new_engine) {
00130         ERR_AI_ENGINE_CPP << "side "<<ai_.get_side()<< " : UNABLE TO CREATE engine["<<cfg["name"]<<"]"<< std::endl;
00131         DBG_AI_ENGINE_CPP << "config snippet contains: " << std::endl << cfg << std::endl;
00132         return;
00133     }
00134     *b = new_engine;
00135 }
00136 
00137 
00138 } //end of namespace ai
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Thu May 24 2012 01:02:28 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs