ai/composite/engine_fai.cpp

Go to the documentation of this file.
00001 /* $Id: engine_fai.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  * FAI AI Support engine - creating specific ai components from config
00018  * @file
00019  */
00020 
00021 #include "ai.hpp"
00022 #include "engine_fai.hpp"
00023 #include "rca.hpp"
00024 #include "../formula/ai.hpp"
00025 #include "../formula/candidates.hpp"
00026 #include "../formula/stage_side_formulas.hpp"
00027 #include "../formula/stage_unit_formulas.hpp"
00028 #include "../../foreach.hpp"
00029 #include "../../log.hpp"
00030 
00031 namespace ai {
00032 
00033 static lg::log_domain log_ai_engine_fai("ai/engine/fai");
00034 #define DBG_AI_ENGINE_FAI LOG_STREAM(debug, log_ai_engine_fai)
00035 #define LOG_AI_ENGINE_FAI LOG_STREAM(info, log_ai_engine_fai)
00036 #define ERR_AI_ENGINE_FAI LOG_STREAM(err, log_ai_engine_fai)
00037 
00038 #ifdef _MSC_VER
00039 #pragma warning(push)
00040 //silence "inherits via dominance" warnings
00041 #pragma warning(disable:4250)
00042 #endif
00043 
00044 class fai_candidate_action_wrapper : public candidate_action {
00045 public:
00046     fai_candidate_action_wrapper( rca_context &context, const config &cfg, game_logic::candidate_action_ptr fai_ca, formula_ai &_formula_ai )
00047         : candidate_action(context,cfg),fai_ca_(fai_ca),formula_ai_(_formula_ai),cfg_(cfg)///@todo 1.7: implement fai_ca->to_config()
00048     {
00049 
00050 }
00051 
00052     virtual ~fai_candidate_action_wrapper() {}
00053 
00054 
00055     virtual double evaluate()
00056     {
00057         formula_ai_.evaluate_candidate_action(fai_ca_);
00058         return fai_ca_->get_score();
00059     }
00060 
00061 
00062     virtual void execute()
00063     {
00064         formula_ai_.execute_candidate_action(fai_ca_);
00065     }
00066 
00067     virtual config to_config() const
00068     {
00069         return cfg_;
00070     }
00071 private:
00072     game_logic::candidate_action_ptr fai_ca_;
00073     formula_ai &formula_ai_;
00074     const config cfg_;
00075 };
00076 
00077 engine_fai::engine_fai( readonly_context &context, const config &cfg )
00078     : engine(context,cfg), formula_ai_(new formula_ai(context,cfg.child_or_empty("formula_ai")))
00079 {
00080     name_ = "fai";
00081     formula_ai_->on_create();
00082 }
00083 
00084 
00085 engine_fai::~engine_fai()
00086 {
00087 }
00088 
00089 
00090 void engine_fai::do_parse_candidate_action_from_config( rca_context &context, const config &cfg, std::back_insert_iterator<std::vector< candidate_action_ptr > > b ){
00091     game_logic::candidate_action_ptr fai_ca = formula_ai_->load_candidate_action_from_config(cfg);
00092     if (!fai_ca) {
00093         ERR_AI_ENGINE_FAI << "side "<<ai_.get_side()<< " : ERROR creating candidate_action["<<cfg["name"]<<"]"<< std::endl;
00094         DBG_AI_ENGINE_FAI << "config snippet contains: " << std::endl << cfg << std::endl;
00095         return;
00096     }
00097     candidate_action_ptr ca = candidate_action_ptr(new fai_candidate_action_wrapper(context,cfg,fai_ca,*formula_ai_));
00098     *b = ca;
00099 
00100 }
00101 
00102 void engine_fai::do_parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr > > b )
00103 {
00104     if (!cfg) {
00105         return;
00106     }
00107     const std::string &name = cfg["name"];
00108     stage_ptr st_ptr;
00109 
00110     //dropped from 1.8, as it's not ready
00111     //if (name=="rca_formulas") {
00112     //  st_ptr = stage_ptr(new stage_rca_formulas(context,cfg,formula_ai_));
00113 
00114     if (name=="side_formulas") {
00115         st_ptr = stage_ptr(new stage_side_formulas(context,cfg,*formula_ai_));
00116     } else if (name=="unit_formulas") {
00117         st_ptr = stage_ptr(new stage_unit_formulas(context,cfg,*formula_ai_));
00118     } else {
00119         ERR_AI_ENGINE_FAI << "unknown type of formula_ai stage: ["<< name <<"]"<<std::endl;
00120     }
00121     if (st_ptr) {
00122         st_ptr->on_create();
00123         *b = st_ptr;
00124     }
00125 }
00126 
00127 std::string engine_fai::evaluate(const std::string &str)
00128 {
00129     return formula_ai_->evaluate(str);
00130 }
00131 
00132 
00133 void engine_fai::set_ai_context(ai_context *context)
00134 {
00135     if (context!=NULL) {
00136         DBG_AI_ENGINE_FAI << "fai engine: ai_context is set" << std::endl;
00137     } else {
00138         DBG_AI_ENGINE_FAI << "fai engine: ai_context is cleared" << std::endl;
00139     }
00140     formula_ai_->set_ai_context(context);
00141 }
00142 
00143 
00144 config engine_fai::to_config() const
00145 {
00146     config cfg = engine::to_config();
00147     cfg.add_child("formula_ai",formula_ai_->to_config());
00148     return cfg;
00149 }
00150 
00151 #ifdef _MSC_VER
00152 #pragma warning(pop)
00153 #endif
00154 
00155 } //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