The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
engine_fai.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2016 by Yurii Chernyi <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * FAI AI Support engine - creating specific ai components from config
17  * @file
18  */
19 
20 #include "ai/formula/ai.hpp"
22 #include "ai/composite/rca.hpp"
23 #include "ai/formula/ai.hpp"
27 #include "log.hpp"
28 #include "units/unit.hpp"
29 
30 namespace ai {
31 
32 static lg::log_domain log_ai_engine_fai("ai/engine/fai");
33 #define DBG_AI_ENGINE_FAI LOG_STREAM(debug, log_ai_engine_fai)
34 #define LOG_AI_ENGINE_FAI LOG_STREAM(info, log_ai_engine_fai)
35 #define ERR_AI_ENGINE_FAI LOG_STREAM(err, log_ai_engine_fai)
36 
37 #ifdef _MSC_VER
38 #pragma warning(push)
39 //silence "inherits via dominance" warnings
40 #pragma warning(disable:4250)
41 #endif
42 
44 public:
46  : candidate_action(context,cfg),fai_ca_(fai_ca),formula_ai_(_formula_ai),cfg_(cfg)///@todo 1.7: implement fai_ca->to_config()
47  {
48 
49 }
50 
52 
53 
54  virtual double evaluate()
55  {
57  return fai_ca_->get_score();
58  }
59 
60 
61  virtual void execute()
62  {
64  }
65 
66  virtual config to_config() const
67  {
68  return cfg_;
69  }
70 private:
73  const config cfg_;
74 };
75 
77  : engine(context,cfg), formula_ai_(new formula_ai(context,cfg.child_or_empty("formula_ai")))
78 {
79  name_ = "fai";
80  formula_ai_->on_create();
81 }
82 
83 
85 {
86 }
87 
88 
89 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 ){
90  game_logic::candidate_action_ptr fai_ca = formula_ai_->load_candidate_action_from_config(cfg);
91  if (!fai_ca) {
92  ERR_AI_ENGINE_FAI << "side "<<ai_.get_side()<< " : ERROR creating candidate_action["<<cfg["name"]<<"]"<< std::endl;
93  DBG_AI_ENGINE_FAI << "config snippet contains: " << std::endl << cfg << std::endl;
94  return;
95  }
97  *b = ca;
98 
99 }
100 
101 void engine_fai::do_parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr > > b )
102 {
103  if (!cfg) {
104  return;
105  }
106  const std::string &name = cfg["name"];
107  stage_ptr st_ptr;
108 
109  //dropped from 1.8, as it's not ready
110  //if (name=="rca_formulas") {
111  // st_ptr = stage_ptr(new stage_rca_formulas(context,cfg,formula_ai_));
112 
113  if (name=="side_formulas") {
114  st_ptr = stage_ptr(new stage_side_formulas(context,cfg,*formula_ai_));
115  } else if (name=="unit_formulas") {
116  st_ptr = stage_ptr(new stage_unit_formulas(context,cfg,*formula_ai_));
117  } else {
118  ERR_AI_ENGINE_FAI << "unknown type of formula_ai stage: ["<< name <<"]"<<std::endl;
119  }
120  if (st_ptr) {
121  st_ptr->on_create();
122  *b = st_ptr;
123  }
124 }
125 
127 {
128  return formula_ai_->evaluate(str);
129 }
130 
131 
133 {
134  if (context!=nullptr) {
135  DBG_AI_ENGINE_FAI << "fai engine: ai_context is set" << std::endl;
136  } else {
137  DBG_AI_ENGINE_FAI << "fai engine: ai_context is cleared" << std::endl;
138  }
139  formula_ai_->set_ai_context(context);
140 }
141 
142 
144 {
145  config cfg = engine::to_config();
146  cfg.add_child("formula_ai",formula_ai_->to_config());
147  return cfg;
148 }
149 
150 #ifdef _MSC_VER
151 #pragma warning(pop)
152 #endif
153 
154 } //end of namespace ai
Defines formula ai.
bool execute_candidate_action(game_logic::candidate_action_ptr fai_ca)
Execute the fai candidate action.
Definition: ai.cpp:980
boost::shared_ptr< stage > stage_ptr
Definition: game_info.hpp:112
Defines formula ai candidate actions - headers.
fai_candidate_action_wrapper(rca_context &context, const config &cfg, game_logic::candidate_action_ptr fai_ca, formula_ai &_formula_ai)
Definition: engine_fai.cpp:45
virtual void execute()
Execute the candidate action.
Definition: engine_fai.cpp:61
#define ERR_AI_ENGINE_FAI
Definition: engine_fai.cpp:35
FAI AI Support engine - creating specific ai components from config.
Stage which executes unit formulas.
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:57
virtual std::string evaluate(const std::string &str)
Definition: engine_fai.cpp:126
virtual ~engine_fai()
Definition: engine_fai.cpp:84
void evaluate_candidate_action(game_logic::candidate_action_ptr fai_ca)
Evaluate the fai candidate action.
Definition: ai.cpp:974
virtual config to_config() const
serialize
Definition: engine_fai.cpp:143
virtual config to_config() const
serialize
Definition: engine.cpp:138
game_logic::candidate_action_ptr fai_ca_
Definition: engine_fai.cpp:71
config & add_child(const std::string &key)
Definition: config.cpp:743
#define DBG_AI_ENGINE_FAI
Definition: engine_fai.cpp:33
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: engine_fai.cpp:54
engine_fai(readonly_context &context, const config &cfg)
Definition: engine_fai.cpp:76
boost::shared_ptr< candidate_action > candidate_action_ptr
Definition: rca.hpp:148
Stage which executes side formulas.
GLuint const GLchar * name
Definition: glew.h:1782
static lg::log_domain log_ai_engine_fai("ai/engine/fai")
virtual config to_config() const
serialize
Definition: engine_fai.cpp:66
std::string name_
Definition: engine.hpp:112
Standard logging facilities (interface).
virtual side_number get_side() const =0
Get the side number.
boost::shared_ptr< formula_ai > formula_ai_
Definition: engine_fai.hpp:47
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
virtual void do_parse_candidate_action_from_config(rca_context &context, const config &cfg, std::back_insert_iterator< std::vector< candidate_action_ptr > > b)
Definition: engine_fai.cpp:89
GLsizei const GLcharARB ** string
Definition: glew.h:4503
readonly_context & ai_
Definition: engine.hpp:106
virtual void set_ai_context(ai_context *context)
Definition: engine_fai.cpp:132
candidate action framework
virtual void do_parse_stage_from_config(ai_context &context, const config &cfg, std::back_insert_iterator< std::vector< stage_ptr > > b)
Definition: engine_fai.cpp:101