The Battle for Wesnoth  1.15.0-dev
ai.cpp
Go to the documentation of this file.
1 
2 /*
3  Copyright (C) 2009 - 2018 by Yurii Chernyi <terraninfo@terraninfo.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * Composite AI with turn sequence which is a vector of stages
18  * @file
19  */
20 
21 #include "ai/composite/ai.hpp"
22 #include "ai/composite/aspect.hpp"
23 #include "ai/composite/engine.hpp"
24 #include "ai/composite/goal.hpp"
26 #include "ai/composite/stage.hpp"
27 #include "ai/configuration.hpp"
28 #include "ai/manager.hpp"
29 #include "actions/attack.hpp"
30 #include "log.hpp"
31 
32 #include "utils/functional.hpp"
33 
34 namespace ai {
35 
36 static lg::log_domain log_ai_composite("ai/composite");
37 #define DBG_AI_COMPOSITE LOG_STREAM(debug, log_ai_composite)
38 #define LOG_AI_COMPOSITE LOG_STREAM(info, log_ai_composite)
39 #define ERR_AI_COMPOSITE LOG_STREAM(err, log_ai_composite)
40 
41 // =======================================================================
42 // COMPOSITE AI
43 // =======================================================================
44 std::string ai_composite::describe_self() const
45 {
46  return "[composite_ai]";
47 }
48 
51 {
53 }
54 
56 {
57  LOG_AI_COMPOSITE << "side "<< get_side() << " : "<<" created AI with id=["<<
58  cfg_["id"]<<"]"<<std::endl;
59 
60  // init the composite ai stages
61  for (const config &cfg_element : cfg_.child_range("stage")) {
62  add_stage(cfg_element);
63  }
64 
65  config cfg;
66  cfg["engine"] = "fai";
67  engine_ptr e_ptr = get_engine_by_cfg(cfg);
68  if (e_ptr) {
69  e_ptr->set_ai_context(this);
70  }
71 
72  std::function<void(std::vector<engine_ptr>&, const config&)> factory_engines =
73  std::bind(&ai::ai_composite::create_engine,*this,_1,_2);
74 
75  std::function<void(std::vector<goal_ptr>&, const config&)> factory_goals =
76  std::bind(&ai::ai_composite::create_goal,*this,_1,_2);
77 
78  std::function<void(std::vector<stage_ptr>&, const config&)> factory_stages =
79  std::bind(&ai::ai_composite::create_stage,*this,_1,_2);
80 
81  std::function<void(std::map<std::string,aspect_ptr>&, const config&, std::string)> factory_aspects =
82  std::bind(&ai::ai_composite::replace_aspect,*this,_1,_2,_3);
83 
84  register_vector_property(property_handlers(),"engine",get_engines(), factory_engines);
85  register_vector_property(property_handlers(),"goal",get_goals(), factory_goals);
86  register_vector_property(property_handlers(),"stage",stages_, factory_stages);
87  register_aspect_property(property_handlers(),"aspect",get_aspects(), factory_aspects);
88 
89 }
90 
91 
92 void ai_composite::create_stage(std::vector<stage_ptr> &stages, const config &cfg)
93 {
94  engine::parse_stage_from_config(*this,cfg,std::back_inserter(stages));
95 }
96 
97 
98 void ai_composite::create_goal(std::vector<goal_ptr> &goals, const config &cfg)
99 {
100  engine::parse_goal_from_config(*this,cfg,std::back_inserter(goals));
101 }
102 
103 
104 void ai_composite::create_engine(std::vector<engine_ptr> &engines, const config &cfg)
105 {
106  engine::parse_engine_from_config(*this,cfg,std::back_inserter(engines));
107 }
108 
109 
110 void ai_composite::replace_aspect(std::map<std::string,aspect_ptr> &aspects, const config &cfg, std::string id)
111 {
112  std::vector<aspect_ptr> temp_aspects;
113  engine::parse_aspect_from_config(*this,cfg,id,std::back_inserter(temp_aspects));
114  aspects[id] = temp_aspects.back();
115 }
116 
118 {
119 }
120 
121 
123 {
124  std::vector< stage_ptr > stages;
125  create_stage(stages,cfg);
126  int j=0;
127  for (stage_ptr b : stages) {
128  stages_.push_back(b);
129  j++;
130  }
131  return (j>0);
132 }
133 
134 
136 {
137  std::vector< goal_ptr > goals;
138  create_goal(goals,cfg);
139  int j=0;
140  for (goal_ptr b : goals) {
141  get_goals().push_back(b);
142  j++;
143  }
144  return (j>0);
145 }
146 
147 
149  for (stage_ptr &s : stages_) {
150  s->play_stage();
151  }
152 }
153 
154 
155 std::string ai_composite::get_id() const
156 {
157  return cfg_["id"];
158 }
159 
160 
161 
162 std::string ai_composite::get_name() const
163 {
164  return cfg_["name"];
165 }
166 
167 
168 std::string ai_composite::get_engine() const
169 {
170  return cfg_["engine"];
171 }
172 
173 
174 std::string ai_composite::evaluate(const std::string& str)
175 {
176  config cfg;
177  cfg["engine"] = "fai";
178  engine_ptr e_ptr = get_engine_by_cfg(cfg);
179  if (!e_ptr) {
180  // This should be unreachable, but not entirely sure...
181  return "engine not found for evaluate command";
182  }
183  return e_ptr->evaluate(str);
184 }
185 
186 
188 {
193  unit_stats_cache().clear();
194 }
195 
196 
198 {
199  return recursion_counter_.get_count();
200 }
201 
203 {
204  set_side(side);
205 }
206 
208 {
209  return *this;
210 }
211 
212 
214 {
215  config cfg;
216 
217  //serialize the composite ai stages
218  for (const stage_ptr &s : stages_) {
219  cfg.add_child("stage",s->to_config());
220  }
221 
222  return cfg;
223 }
224 
226 {
227  config temp_cfg, parsed_cfg;
228  temp_cfg.add_child("ai", cfg);
229  configuration::parse_side_config(ctx.get_side(), temp_cfg, parsed_cfg);
230  return parsed_cfg;
231 }
232 
233 } //end of namespace ai
static void parse_aspect_from_config(readonly_context &context, const config &cfg, const std::string &id, std::back_insert_iterator< std::vector< aspect_ptr >> b)
Definition: engine.cpp:51
virtual std::string evaluate(const std::string &str)
Evaluate command (using fai)
Definition: ai.cpp:174
std::shared_ptr< stage > stage_ptr
Definition: game_info.hpp:101
void on_create()
Definition: ai.cpp:55
#define LOG_AI_COMPOSITE
Definition: ai.cpp:38
void create_engine(std::vector< engine_ptr > &engines, const config &cfg)
Definition: ai.cpp:104
Various functions that implement attacks and attack calculations.
std::vector< stage_ptr > stages_
Stages of the composite AI.
Definition: ai.hpp:118
void create_stage(std::vector< stage_ptr > &stages, const config &cfg)
Definition: ai.cpp:92
property_handler_map & property_handlers()
Definition: component.cpp:132
child_itors child_range(config_key_type key)
Definition: config.cpp:366
virtual bool add_stage(const config &cfg)
Definition: ai.cpp:122
virtual std::string get_name() const
Definition: ai.cpp:162
virtual std::string get_engine() const
Definition: ai.cpp:168
static void parse_engine_from_config(readonly_context &context, const config &cfg, std::back_insert_iterator< std::vector< engine_ptr >> b)
Definition: engine.cpp:69
Composite AI stages.
AI Support engine - creating specific ai components from config.
const config & cfg_
Config of the AI.
Definition: ai.hpp:112
virtual std::string get_id() const
Definition: ai.cpp:155
static void parse_goal_from_config(readonly_context &context, const config &cfg, std::back_insert_iterator< std::vector< goal_ptr >> b)
Definition: engine.cpp:79
virtual bool add_goal(const config &cfg)
Definition: ai.cpp:135
static lg::log_domain log_ai_composite("ai/composite")
static void register_vector_property(property_handler_map &property_handlers, const std::string &property, std::vector< std::shared_ptr< X >> &values, std::function< void(std::vector< std::shared_ptr< X >> &, const config &)> construction_factory)
virtual config to_config() const
serialize
Definition: ai.cpp:213
virtual unit_stats_cache_t & unit_stats_cache() const override
Definition: contexts.hpp:968
#define b
void replace_aspect(std::map< std::string, aspect_ptr > &aspects, const config &cfg, std::string id)
Definition: ai.cpp:110
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:58
static bool parse_side_config(side_number side, const config &original_cfg, config &cfg)
recursion_counter recursion_counter_
Recursion counter.
Definition: ai.hpp:124
std::shared_ptr< goal > goal_ptr
Definition: game_info.hpp:99
virtual void clear_additional_targets() const
Definition: contexts.hpp:207
virtual void new_turn()
On new turn.
Definition: ai.cpp:187
virtual const aspect_map & get_aspects() const override
Definition: contexts.hpp:632
ai_composite(default_ai_context &context, const config &cfg)
Constructor.
Definition: ai.cpp:49
virtual const std::vector< goal_ptr > & get_goals() const override
Definition: contexts.hpp:730
virtual void recalculate_move_maps() const override
Definition: contexts.hpp:925
Composite AI with turn sequence which is a vector of stages.
virtual ai_context & get_ai_context()
unwrap
Definition: ai.cpp:207
void switch_side(side_number side)
Definition: ai.cpp:202
Managing the AIs lifecycle - headers TODO: Refactor history handling and internal commands...
void create_goal(std::vector< goal_ptr > &goals, const config &cfg)
Definition: ai.cpp:98
void init_default_ai_context_proxy(default_ai_context &target)
Definition: contexts.cpp:58
virtual void set_side(side_number side) override
Set the side number.
Definition: contexts.hpp:470
virtual engine_ptr get_engine_by_cfg(const config &cfg) override
get engine by cfg, creating it if it is not created yet but known
Definition: contexts.hpp:706
static map_location::DIRECTION s
virtual side_number get_side() const override
Get the side number.
Definition: contexts.hpp:465
static config preparse_cfg(ai_context &ctx, const config &cfg)
Definition: ai.cpp:225
config & add_child(config_key_type key)
Definition: config.cpp:480
virtual ~ai_composite()
Destructor.
Definition: ai.cpp:117
Composite AI component.
void play_turn()
Play the turn.
Definition: ai.cpp:148
Managing the AIs configuration - headers.
static void parse_stage_from_config(ai_context &context, const config &cfg, std::back_insert_iterator< std::vector< stage_ptr >> b)
Definition: engine.cpp:89
Standard logging facilities (interface).
virtual side_number get_side() const =0
Get the side number.
int get_count() const
Get the current value of the recursion counter.
Definition: contexts.hpp:70
virtual void invalidate_keeps_cache() const override
Definition: contexts.hpp:901
int side_number
Definition: game_info.hpp:39
virtual void invalidate_defensive_position_cache() const override
Definition: contexts.hpp:889
std::shared_ptr< engine > engine_ptr
Definition: game_info.hpp:98
std::string describe_self() const
Definition: ai.cpp:44
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
virtual const std::vector< engine_ptr > & get_engines() const override
Definition: contexts.hpp:712
int get_recursion_count() const
Get the value of the recursion counter.
Definition: ai.cpp:197
static void register_aspect_property(property_handler_map &property_handlers, const std::string &property, std::map< std::string, std::shared_ptr< X >> &aspects, std::function< void(std::map< std::string, std::shared_ptr< X >> &, const config &, std::string)> construction_factory)