The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ai.cpp
Go to the documentation of this file.
1 
2 /*
3  Copyright (C) 2009 - 2017 by Yurii Chernyi <terraninfo@terraninfo.net>
4  Part of the Battle for Wesnoth Project http://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 // =======================================================================
45 {
46  return "[composite_ai]";
47 }
48 
50  : cfg_(cfg),stages_(),recursion_counter_(context.get_recursion_count())
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 
156 {
157  return cfg_["id"];
158 }
159 
160 
161 
163 {
164  return cfg_["name"];
165 }
166 
167 
169 {
170  return cfg_["engine"];
171 }
172 
173 
175 {
176  config cfg;
177  cfg["engine"] = "fai";///@todo 1.9 : consider allowing other engines to evaluate
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 {
189  ///@todo 1.9 replace with event system
194  unit_stats_cache().clear();
195 }
196 
197 
199 {
200  return recursion_counter_.get_count();
201 }
202 
204 {
205  set_side(side);
206 }
207 
209 {
210  return *this;
211 }
212 
213 
215 {
216  config cfg;
217 
218  //serialize the composite ai stages
219  for (const stage_ptr &s : stages_) {
220  cfg.add_child("stage",s->to_config());
221  }
222 
223  return cfg;
224 }
225 
227 {
228  config temp_cfg, parsed_cfg;
229  temp_cfg.add_child("ai", cfg);
230  configuration::parse_side_config(ctx.get_side(), temp_cfg, parsed_cfg);
231  return parsed_cfg;
232 }
233 
234 } //end of namespace ai
virtual std::string evaluate(const std::string &str)
Evaluate command (using fai)
Definition: ai.cpp:174
std::vector< char_t > string
void on_create()
Definition: ai.cpp:55
#define LOG_AI_COMPOSITE
Definition: ai.cpp:38
virtual std::string get_id() const
Definition: ai.cpp:155
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:124
void create_stage(std::vector< stage_ptr > &stages, const config &cfg)
Definition: ai.cpp:92
property_handler_map & property_handlers()
Definition: component.cpp:132
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
std::string describe_self() const
Definition: ai.cpp:44
child_itors child_range(config_key_type key)
Definition: config.cpp:343
virtual bool add_stage(const config &cfg)
Definition: ai.cpp:122
Composite AI stages.
AI Support engine - creating specific ai components from config.
const config & cfg_
Config of the AI.
Definition: ai.hpp:118
std::shared_ptr< engine > engine_ptr
Definition: game_info.hpp:101
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 unit_stats_cache_t & unit_stats_cache() const override
Definition: contexts.hpp:992
#define b
int get_recursion_count() const
Get the value of the recursion counter.
Definition: ai.cpp:198
virtual config to_config() const
serialize
Definition: ai.cpp:214
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'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:130
virtual void new_turn()
On new turn.
Definition: ai.cpp:187
std::shared_ptr< stage > stage_ptr
Definition: game_info.hpp:104
virtual const aspect_map & get_aspects() const override
Definition: contexts.hpp:650
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:748
virtual void recalculate_move_maps() const override
Definition: contexts.hpp:949
Composite AI with turn sequence which is a vector of stages.
virtual ai_context & get_ai_context()
unwrap
Definition: ai.cpp:208
void switch_side(side_number side)
Definition: ai.cpp:203
std::shared_ptr< goal > goal_ptr
Definition: game_info.hpp:102
Managing the AIs lifecycle - headers.
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:482
int get_count() const
Get the current value of the recursion counter.
Definition: contexts.hpp:76
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:724
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
static map_location::DIRECTION s
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 side_number get_side() const override
Get the side number.
Definition: contexts.hpp:477
static config preparse_cfg(ai_context &ctx, const config &cfg)
Definition: ai.cpp:226
config & add_child(config_key_type key)
Definition: config.cpp:456
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.
Standard logging facilities (interface).
virtual side_number get_side() const =0
Get the side number.
virtual void clear_additional_targets() const
Definition: contexts.hpp:214
virtual void invalidate_keeps_cache() const override
Definition: contexts.hpp:925
int side_number
Definition: game_info.hpp:36
virtual void invalidate_defensive_position_cache() const override
Definition: contexts.hpp:913
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
virtual std::string get_engine() const
Definition: ai.cpp:168
virtual const std::vector< engine_ptr > & get_engines() const override
Definition: contexts.hpp:730
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 std::string get_name() const
Definition: ai.cpp:162
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)