The Battle for Wesnoth  1.15.0-dev
engine_fai.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2018 by Yurii Chernyi <terraninfo@terraninfo.net>
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 
38 public:
40  : candidate_action(context,cfg),fai_ca_(fai_ca),formula_ai_(_formula_ai),cfg_(cfg)///@todo 1.7: implement fai_ca->to_config()
41  {
42 
43 }
44 
46 
47 
48  virtual double evaluate()
49  {
51  return fai_ca_->get_score();
52  }
53 
54 
55  virtual void execute()
56  {
58  }
59 
60  virtual config to_config() const
61  {
62  return cfg_;
63  }
64 private:
67  const config cfg_;
68 };
69 
71  : engine(context,cfg), formula_ai_(new formula_ai(context,cfg.child_or_empty("formula_ai")))
72 {
73  name_ = "fai";
74  formula_ai_->on_create();
75 }
76 
77 
79 {
80 }
81 
82 
83 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 ){
84  wfl::candidate_action_ptr fai_ca = formula_ai_->load_candidate_action_from_config(cfg);
85  if (!fai_ca) {
86  ERR_AI_ENGINE_FAI << "side "<<ai_.get_side()<< " : ERROR creating candidate_action["<<cfg["name"]<<"]"<< std::endl;
87  DBG_AI_ENGINE_FAI << "config snippet contains: " << std::endl << cfg << std::endl;
88  return;
89  }
91  *b = ca;
92 
93 }
94 
95 void engine_fai::do_parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr >> b )
96 {
97  if (!cfg) {
98  return;
99  }
100  const std::string &name = cfg["name"];
101  stage_ptr st_ptr;
102 
103  //dropped from 1.8, as it's not ready
104  //if (name=="rca_formulas") {
105  // st_ptr = stage_ptr(new stage_rca_formulas(context,cfg,formula_ai_));
106 
107  if (name=="side_formulas") {
108  st_ptr = std::make_shared<stage_side_formulas>(context, cfg, *formula_ai_);
109  } else if (name=="unit_formulas") {
110  st_ptr = std::make_shared<stage_unit_formulas>(context, cfg, *formula_ai_);
111  } else {
112  ERR_AI_ENGINE_FAI << "unknown type of formula_ai stage: ["<< name <<"]"<<std::endl;
113  }
114  if (st_ptr) {
115  st_ptr->on_create();
116  *b = st_ptr;
117  }
118 }
119 
120 std::string engine_fai::evaluate(const std::string &str)
121 {
122  return formula_ai_->evaluate(str);
123 }
124 
125 
127 {
128  if (context!=nullptr) {
129  DBG_AI_ENGINE_FAI << "fai engine: ai_context is set" << std::endl;
130  } else {
131  DBG_AI_ENGINE_FAI << "fai engine: ai_context is cleared" << std::endl;
132  }
133  formula_ai_->set_ai_context(context);
134 }
135 
136 
138 {
139  config cfg = engine::to_config();
140  cfg.add_child("formula_ai",formula_ai_->to_config());
141  return cfg;
142 }
143 
144 } //end of namespace ai
Defines formula ai.
std::shared_ptr< candidate_action > candidate_action_ptr
Definition: rca.hpp:141
std::shared_ptr< stage > stage_ptr
Definition: game_info.hpp:101
Defines formula ai candidate actions - headers.
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:83
virtual void execute()
Execute the candidate action.
Definition: engine_fai.cpp:55
bool execute_candidate_action(wfl::candidate_action_ptr fai_ca)
Execute the fai candidate action.
Definition: ai.cpp:685
#define ERR_AI_ENGINE_FAI
Definition: engine_fai.cpp:35
FAI AI Support engine - creating specific ai components from config.
virtual config to_config() const
serialize
Definition: engine.cpp:138
Stage which executes unit formulas.
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:95
virtual config to_config() const
serialize
Definition: engine_fai.cpp:60
wfl::candidate_action_ptr fai_ca_
Definition: engine_fai.cpp:65
#define b
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:58
std::shared_ptr< formula_ai > formula_ai_
Definition: engine_fai.hpp:46
virtual std::string evaluate(const std::string &str)
Definition: engine_fai.cpp:120
virtual ~engine_fai()
Definition: engine_fai.cpp:78
void evaluate_candidate_action(wfl::candidate_action_ptr fai_ca)
Evaluate the fai candidate action.
Definition: ai.cpp:679
#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:48
engine_fai(readonly_context &context, const config &cfg)
Definition: engine_fai.cpp:70
config & add_child(config_key_type key)
Definition: config.cpp:479
Stage which executes side formulas.
static lg::log_domain log_ai_engine_fai("ai/engine/fai")
std::string name_
Definition: engine.hpp:111
Standard logging facilities (interface).
virtual side_number get_side() const =0
Get the side number.
fai_candidate_action_wrapper(rca_context &context, const config &cfg, wfl::candidate_action_ptr fai_ca, formula_ai &_formula_ai)
Definition: engine_fai.cpp:39
virtual config to_config() const
serialize
Definition: engine_fai.cpp:137
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
readonly_context & ai_
Definition: engine.hpp:105
virtual void set_ai_context(ai_context *context)
Definition: engine_fai.cpp:126
candidate action framework