The Battle for Wesnoth  1.19.13+dev
ai.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2025
3  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 "actions/attack.hpp"
29 #include "log.hpp"
30 
31 #include <functional>
32 
33 namespace ai {
34 
35 static lg::log_domain log_ai_composite("ai/composite");
36 #define DBG_AI_COMPOSITE LOG_STREAM(debug, log_ai_composite)
37 #define LOG_AI_COMPOSITE LOG_STREAM(info, log_ai_composite)
38 #define ERR_AI_COMPOSITE LOG_STREAM(err, log_ai_composite)
39 
40 // =======================================================================
41 // COMPOSITE AI
42 // =======================================================================
43 std::string ai_composite::describe_self() const
44 {
45  return "[composite_ai]";
46 }
47 
49  : cfg_(cfg),stages_(),recursion_counter_(context.get_recursion_count())
50 {
52 }
53 
55 {
56  LOG_AI_COMPOSITE << "side "<< get_side() << " : "<<" created AI with id=["<<
57  cfg_["id"]<<"]";
58 
59  // init the composite ai stages
60  for (const config &cfg_element : cfg_.child_range("stage")) {
61  add_stage(cfg_element);
62  }
63 
64  config cfg;
65  cfg["engine"] = "fai";
66  engine_ptr e_ptr = get_engine_by_cfg(cfg);
67  if (e_ptr) {
68  e_ptr->set_ai_context(this);
69  }
70 
72  [this](auto&&... args) { create_engine(args...); });
73 
75  [this](auto&&... args) { create_goal(args...); });
76 
78  [this](auto&&... args) { create_stage(args...); });
79 
81  [this](auto&&... args) { replace_aspect(args...); });
82 }
83 
84 void ai_composite::create_stage(std::vector<stage_ptr> &stages, const config &cfg)
85 {
86  engine::parse_stage_from_config(*this,cfg,std::back_inserter(stages));
87 }
88 
89 void ai_composite::create_goal(std::vector<goal_ptr> &goals, const config &cfg)
90 {
91  engine::parse_goal_from_config(*this,cfg,std::back_inserter(goals));
92 }
93 
94 void ai_composite::create_engine(std::vector<engine_ptr> &engines, const config &cfg)
95 {
96  engine::parse_engine_from_config(*this,cfg,std::back_inserter(engines));
97 }
98 
99 void ai_composite::replace_aspect(std::map<std::string,aspect_ptr> &aspects, const config &cfg, const std::string& id)
100 {
101  std::vector<aspect_ptr> temp_aspects;
102  engine::parse_aspect_from_config(*this,cfg,id,std::back_inserter(temp_aspects));
103  aspects[id] = temp_aspects.back();
104 }
105 
107 {
108 }
109 
111 {
112  std::vector< stage_ptr > stages;
113  create_stage(stages,cfg);
114  int j=0;
115  for (stage_ptr b : stages) {
116  stages_.push_back(b);
117  j++;
118  }
119  return (j>0);
120 }
121 
123 {
124  std::vector< goal_ptr > goals;
125  create_goal(goals,cfg);
126  int j=0;
127  for (goal_ptr b : goals) {
128  get_goals().push_back(b);
129  j++;
130  }
131  return (j>0);
132 }
133 
135  for (stage_ptr &s : stages_) {
136  s->play_stage();
137  }
138 }
139 
140 std::string ai_composite::get_id() const
141 {
142  return cfg_["id"];
143 }
144 
145 
146 std::string ai_composite::get_name() const
147 {
148  return cfg_["name"];
149 }
150 
151 std::string ai_composite::get_engine() const
152 {
153  return cfg_["engine"];
154 }
155 
156 std::string ai_composite::evaluate(const std::string& str)
157 {
158  config cfg;
159  cfg["engine"] = "fai";
160  engine_ptr e_ptr = get_engine_by_cfg(cfg);
161  if (!e_ptr) {
162  // This should be unreachable, but not entirely sure...
163  return "engine not found for evaluate command";
164  }
165  return e_ptr->evaluate(str);
166 }
167 
169 {
174  unit_stats_cache().clear();
175 }
176 
178 {
179  return recursion_counter_.get_count();
180 }
181 
183 {
184  set_side(side);
185 }
186 
188 {
189  return *this;
190 }
191 
193 {
194  config cfg;
195 
196  //serialize the composite ai stages
197  for (const stage_ptr &s : stages_) {
198  cfg.add_child("stage",s->to_config());
199  }
200 
201  return cfg;
202 }
203 
205 {
206  config temp_cfg, parsed_cfg;
207  temp_cfg.add_child("ai", cfg);
208  configuration::parse_side_config(ctx.get_side(), temp_cfg, parsed_cfg);
209  return parsed_cfg;
210 }
211 
212 } //end of namespace ai
Various functions that implement attacks and attack calculations.
virtual void new_turn()
On new turn.
Definition: ai.cpp:168
void replace_aspect(std::map< std::string, aspect_ptr > &aspects, const config &cfg, const std::string &id)
Definition: ai.cpp:99
void create_goal(std::vector< goal_ptr > &goals, const config &cfg)
Definition: ai.cpp:89
virtual bool add_goal(const config &cfg)
Definition: ai.cpp:122
int get_recursion_count() const
Get the value of the recursion counter.
Definition: ai.cpp:177
void create_stage(std::vector< stage_ptr > &stages, const config &cfg)
Definition: ai.cpp:84
void switch_side(side_number side)
Definition: ai.cpp:182
virtual std::string get_id() const
Definition: ai.cpp:140
std::vector< stage_ptr > stages_
Stages of the composite AI.
Definition: ai.hpp:103
static config preparse_cfg(ai_context &ctx, const config &cfg)
Definition: ai.cpp:204
virtual std::string get_name() const
Definition: ai.cpp:146
virtual ai_context & get_ai_context()
unwrap
Definition: ai.cpp:187
recursion_counter recursion_counter_
Recursion counter.
Definition: ai.hpp:108
virtual std::string evaluate(const std::string &str)
Evaluate command (using fai)
Definition: ai.cpp:156
const config & cfg_
Config of the AI.
Definition: ai.hpp:98
void on_create()
Definition: ai.cpp:54
virtual ~ai_composite()
Destructor.
Definition: ai.cpp:106
void create_engine(std::vector< engine_ptr > &engines, const config &cfg)
Definition: ai.cpp:94
virtual bool add_stage(const config &cfg)
Definition: ai.cpp:110
ai_composite(default_ai_context &context, const config &cfg)
Constructor.
Definition: ai.cpp:48
virtual config to_config() const
serialize
Definition: ai.cpp:192
void play_turn()
Play the turn.
Definition: ai.cpp:134
std::string describe_self() const
Definition: ai.cpp:43
virtual std::string get_engine() const
Definition: ai.cpp:151
property_handler_map & property_handlers()
Definition: component.cpp:116
static bool parse_side_config(side_number side, const config &original_cfg, config &cfg)
virtual void clear_additional_targets() const
Definition: contexts.hpp:177
void init_default_ai_context_proxy(default_ai_context &target)
Definition: contexts.cpp:57
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
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:52
static void parse_stage_from_config(ai_context &context, const config &cfg, std::back_insert_iterator< std::vector< stage_ptr >> b)
Definition: engine.cpp:88
static void parse_engine_from_config(readonly_context &context, const config &cfg, std::back_insert_iterator< std::vector< engine_ptr >> b)
Definition: engine.cpp:70
virtual void invalidate_keeps_cache() const override
Definition: contexts.hpp:806
virtual const aspect_map & get_aspects() const override
Definition: contexts.hpp:556
virtual void recalculate_move_maps() const override
Definition: contexts.hpp:826
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:616
virtual const std::vector< goal_ptr > & get_goals() const override
Definition: contexts.hpp:636
virtual void invalidate_defensive_position_cache() const override
Definition: contexts.hpp:796
virtual const std::vector< engine_ptr > & get_engines() const override
Definition: contexts.hpp:621
virtual unit_stats_cache_t & unit_stats_cache() const override
Definition: contexts.hpp:866
int get_count() const
Get the current value of the recursion counter.
Definition: contexts.hpp:66
virtual void set_side(side_number side) override
Set the side number.
Definition: contexts.hpp:401
virtual side_number get_side() const override
Get the side number.
Definition: contexts.hpp:396
virtual side_number get_side() const =0
Get the side number.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
child_itors child_range(config_key_type key)
Definition: config.cpp:268
config & add_child(config_key_type key)
Definition: config.cpp:436
#define LOG_AI_COMPOSITE
Definition: ai.cpp:37
Composite AI with turn sequence which is a vector of stages.
Managing the AIs configuration - headers.
AI Support engine - creating specific ai components from config.
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:199
Standard logging facilities (interface).
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:59
std::shared_ptr< engine > engine_ptr
Definition: game_info.hpp:99
static lg::log_domain log_ai_composite("ai/composite")
std::shared_ptr< goal > goal_ptr
Definition: game_info.hpp:100
std::shared_ptr< stage > stage_ptr
Definition: game_info.hpp:102
static void register_vector_property(property_handler_map &property_handlers, const std::string &property, std::vector< std::shared_ptr< X >> &values, Factory &&construction_factory)
int side_number
Definition: game_info.hpp:40
static void register_aspect_property(property_handler_map &property_handlers, const std::string &property, std::map< std::string, std::shared_ptr< X >> &aspects, Factory &&construction_factory)
Composite AI component.
Composite AI stages.
static map_location::direction s
#define b