The Battle for Wesnoth  1.17.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 <functional>
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, std::placeholders::_1, std::placeholders::_2);
74 
75  std::function<void(std::vector<goal_ptr>&, const config&)> factory_goals =
76  std::bind(&ai::ai_composite::create_goal, *this, std::placeholders::_1, std::placeholders::_2);
77 
78  std::function<void(std::vector<stage_ptr>&, const config&)> factory_stages =
79  std::bind(&ai::ai_composite::create_stage, *this, std::placeholders::_1, std::placeholders::_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, std::placeholders::_1, std::placeholders::_2, std::placeholders::_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 void ai_composite::create_stage(std::vector<stage_ptr> &stages, const config &cfg)
92 {
93  engine::parse_stage_from_config(*this,cfg,std::back_inserter(stages));
94 }
95 
96 void ai_composite::create_goal(std::vector<goal_ptr> &goals, const config &cfg)
97 {
98  engine::parse_goal_from_config(*this,cfg,std::back_inserter(goals));
99 }
100 
101 void ai_composite::create_engine(std::vector<engine_ptr> &engines, const config &cfg)
102 {
103  engine::parse_engine_from_config(*this,cfg,std::back_inserter(engines));
104 }
105 
106 void ai_composite::replace_aspect(std::map<std::string,aspect_ptr> &aspects, const config &cfg, std::string id)
107 {
108  std::vector<aspect_ptr> temp_aspects;
109  engine::parse_aspect_from_config(*this,cfg,id,std::back_inserter(temp_aspects));
110  aspects[id] = temp_aspects.back();
111 }
112 
114 {
115 }
116 
118 {
119  std::vector< stage_ptr > stages;
120  create_stage(stages,cfg);
121  int j=0;
122  for (stage_ptr b : stages) {
123  stages_.push_back(b);
124  j++;
125  }
126  return (j>0);
127 }
128 
130 {
131  std::vector< goal_ptr > goals;
132  create_goal(goals,cfg);
133  int j=0;
134  for (goal_ptr b : goals) {
135  get_goals().push_back(b);
136  j++;
137  }
138  return (j>0);
139 }
140 
142  for (stage_ptr &s : stages_) {
143  s->play_stage();
144  }
145 }
146 
147 std::string ai_composite::get_id() const
148 {
149  return cfg_["id"];
150 }
151 
152 
153 std::string ai_composite::get_name() const
154 {
155  return cfg_["name"];
156 }
157 
158 std::string ai_composite::get_engine() const
159 {
160  return cfg_["engine"];
161 }
162 
163 std::string ai_composite::evaluate(const std::string& str)
164 {
165  config cfg;
166  cfg["engine"] = "fai";
167  engine_ptr e_ptr = get_engine_by_cfg(cfg);
168  if (!e_ptr) {
169  // This should be unreachable, but not entirely sure...
170  return "engine not found for evaluate command";
171  }
172  return e_ptr->evaluate(str);
173 }
174 
176 {
181  unit_stats_cache().clear();
182 }
183 
185 {
186  return recursion_counter_.get_count();
187 }
188 
190 {
191  set_side(side);
192 }
193 
195 {
196  return *this;
197 }
198 
200 {
201  config cfg;
202 
203  //serialize the composite ai stages
204  for (const stage_ptr &s : stages_) {
205  cfg.add_child("stage",s->to_config());
206  }
207 
208  return cfg;
209 }
210 
212 {
213  config temp_cfg, parsed_cfg;
214  temp_cfg.add_child("ai", cfg);
215  configuration::parse_side_config(ctx.get_side(), temp_cfg, parsed_cfg);
216  return parsed_cfg;
217 }
218 
219 } //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:163
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:101
Various functions that implement attacks and attack calculations.
std::vector< stage_ptr > stages_
Stages of the composite AI.
Definition: ai.hpp:102
void create_stage(std::vector< stage_ptr > &stages, const config &cfg)
Definition: ai.cpp:91
property_handler_map & property_handlers()
Definition: component.cpp:124
child_itors child_range(config_key_type key)
Definition: config.cpp:356
virtual bool add_stage(const config &cfg)
Definition: ai.cpp:117
virtual std::string get_name() const
Definition: ai.cpp:153
virtual std::string get_engine() const
Definition: ai.cpp:158
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:97
virtual std::string get_id() const
Definition: ai.cpp:147
static void parse_goal_from_config(readonly_context &context, const config &cfg, std::back_insert_iterator< std::vector< goal_ptr >> b)
Definition: engine.cpp:78
virtual bool add_goal(const config &cfg)
Definition: ai.cpp:129
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:199
virtual unit_stats_cache_t & unit_stats_cache() const override
Definition: contexts.hpp:865
#define b
void replace_aspect(std::map< std::string, aspect_ptr > &aspects, const config &cfg, std::string id)
Definition: ai.cpp:106
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:60
static bool parse_side_config(side_number side, const config &original_cfg, config &cfg)
recursion_counter recursion_counter_
Recursion counter.
Definition: ai.hpp:107
std::shared_ptr< goal > goal_ptr
Definition: game_info.hpp:99
virtual void clear_additional_targets() const
Definition: contexts.hpp:187
virtual void new_turn()
On new turn.
Definition: ai.cpp:175
virtual const aspect_map & get_aspects() const override
Definition: contexts.hpp:555
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:635
virtual void recalculate_move_maps() const override
Definition: contexts.hpp:825
Composite AI with turn sequence which is a vector of stages.
virtual ai_context & get_ai_context()
unwrap
Definition: ai.cpp:194
void switch_side(side_number side)
Definition: ai.cpp:189
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:96
void init_default_ai_context_proxy(default_ai_context &target)
Definition: contexts.cpp:56
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:214
virtual void set_side(side_number side) override
Set the side number.
Definition: contexts.hpp:405
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:615
static map_location::DIRECTION s
virtual side_number get_side() const override
Get the side number.
Definition: contexts.hpp:400
static config preparse_cfg(ai_context &ctx, const config &cfg)
Definition: ai.cpp:211
config & add_child(config_key_type key)
Definition: config.cpp:500
virtual ~ai_composite()
Destructor.
Definition: ai.cpp:113
Composite AI component.
void play_turn()
Play the turn.
Definition: ai.cpp:141
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:87
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:69
virtual void invalidate_keeps_cache() const override
Definition: contexts.hpp:805
int side_number
Definition: game_info.hpp:39
virtual void invalidate_defensive_position_cache() const override
Definition: contexts.hpp:795
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:59
virtual const std::vector< engine_ptr > & get_engines() const override
Definition: contexts.hpp:620
int get_recursion_count() const
Get the value of the recursion counter.
Definition: ai.cpp:184
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)