The Battle for Wesnoth  1.19.7+dev
contexts.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2024
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  * @file
18  * Default AI contexts
19  */
20 
21 #pragma once
22 
23 #include "ai/ai_target.hpp"
24 #include "ai/contexts.hpp"
25 #include "formula/callable.hpp"
26 
27 //============================================================================
28 namespace ai {
29 
30 struct target {
31  target(const map_location& pos, double val, ai_target::type target_type = ai_target::type::village) : loc(pos), value(val), type(target_type)
32  {}
34  double value;
35 
37 };
38 
40 {
41 public:
43  wfl::action_callable(),
44  target(),
45  movements(),
46  target_value(0.0),
47  avg_losses(0.0),
48  chance_to_kill(0.0),
51  avg_damage_taken(0.0),
52  resources_used(0.0),
53  terrain_quality(0.0),
55  vulnerability(0.0),
56  support(0.0),
57  leader_threat(false),
58  uses_leader(false),
59  is_surrounded(false)
60  {
61  }
62 
63  void analyze(const gamemap& map, unit_map& units,
64  const readonly_context& ai_obj,
65  const move_map& dstsrc, const move_map& srcdst,
66  const move_map& enemy_dstsrc, double aggression);
67 
68  double rating(double aggression, const readonly_context& ai_obj) const;
69  wfl::variant get_value(const std::string& key) const override;
70  void get_inputs(wfl::formula_input_vector& inputs) const override;
71 
72  bool attack_close(const map_location& loc) const;
73 
75  std::vector<std::pair<map_location,map_location>> movements;
76 
77  /** The value of the unit being targeted. */
78  double target_value;
79 
80  /** The value on average, of units lost in the combat. */
81  double avg_losses;
82 
83  /** Estimated % chance to kill the unit. */
85 
86  /** The average hitpoints damage inflicted. */
88 
90 
91  /** The average hitpoints damage taken. */
93 
94  /** The sum of the values of units used in the attack. */
96 
97  /** The weighted average of the % chance to hit each attacking unit. */
99 
100  /**
101  * The weighted average of the % defense of the best possible terrain
102  * that the attacking units could reach this turn, without attacking
103  * (good for comparison to see just how good/bad 'terrain_quality' is).
104  */
106 
107  /**
108  * The vulnerability is the power projection of enemy units onto the hex
109  * we're standing on. support is the power projection of friendly units.
110  */
112 
113  /** Is true if the unit is a threat to our leader. */
115 
116  /** Is true if this attack sequence makes use of the leader. */
118 
119  /** Is true if the units involved in this attack sequence are surrounded. */
121 
122  wfl::variant execute_self(wfl::variant ctxt) override;
123 };
124 
125 class default_ai_context : public virtual readwrite_context{
126 public:
127 
128  virtual int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes) = 0;
129 
130  /** Constructor */
132 
133  /** Destructor */
134  virtual ~default_ai_context();
135 
136  virtual const std::vector<target>& additional_targets() const = 0;
137 
138  virtual void add_target(const target& t) const = 0;
139 
140  virtual void clear_additional_targets() const = 0;
141 
143 
144  virtual std::vector<target> find_targets(const move_map& enemy_dstsrc) = 0;
145 
146  virtual int rate_terrain(const unit& u, const map_location& loc) const = 0;
147 
149 };
150 
151 // proxies
153 public:
154 
155  int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes)
156  {
157  return target_->count_free_hexes_in_castle(loc, checked_hexes);
158  }
159 
161  : target_(nullptr)
162  {
163  }
164 
165  virtual ~default_ai_context_proxy();
166 
167  virtual const std::vector<target>& additional_targets() const
168  {
169  return target_->additional_targets();
170  }
171 
172  virtual void add_target(const target& t) const
173  {
174  target_->add_target(t);
175  }
176 
177  virtual void clear_additional_targets() const
178  {
180  }
181 
183  {
185  }
186 
187  virtual std::vector<target> find_targets(const move_map& enemy_dstsrc)
188  {
189  return target_->find_targets(enemy_dstsrc);
190  }
191 
193 
194  virtual int rate_terrain(const unit& u, const map_location& loc) const
195  {
196  return target_->rate_terrain(u,loc);
197  }
198 
200  {
202  }
203 
204 private:
206 };
207 
209 public:
210 
211  int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes);
212 
215  {
217  }
218 
219  virtual ~default_ai_context_impl();
220 
222 
223  virtual const std::vector<target>& additional_targets() const;
224 
225  virtual void add_target(const target& t) const;
226 
227  virtual void clear_additional_targets() const;
228 
230  {
231  return recursion_counter_.get_count();
232  }
233 
234  virtual std::vector<target> find_targets(const move_map& enemy_dstsrc);
235 
236  virtual int rate_terrain(const unit& u, const map_location& loc) const;
237 
238  virtual config to_default_ai_context_config() const;
239 
240 private:
242  mutable std::vector<target> additional_targets_;// TODO: refactor this (remove mutable)
243 
244 };
245 
246 } //end of namespace ai
map_location loc
Definition: move.cpp:172
double t
Definition: astarsearch.cpp:63
std::vector< std::pair< map_location, map_location > > movements
Definition: contexts.hpp:75
void analyze(const gamemap &map, unit_map &units, const readonly_context &ai_obj, const move_map &dstsrc, const move_map &srcdst, const move_map &enemy_dstsrc, double aggression)
Definition: attack.cpp:45
bool uses_leader
Is true if this attack sequence makes use of the leader.
Definition: contexts.hpp:117
wfl::variant get_value(const std::string &key) const override
Definition: attack.cpp:334
void get_inputs(wfl::formula_input_vector &inputs) const override
Definition: attack.cpp:389
map_location target
Definition: contexts.hpp:74
double target_value
The value of the unit being targeted.
Definition: contexts.hpp:78
double avg_damage_inflicted
The average hitpoints damage inflicted.
Definition: contexts.hpp:87
double chance_to_kill
Estimated % chance to kill the unit.
Definition: contexts.hpp:84
bool attack_close(const map_location &loc) const
Definition: attack.cpp:256
wfl::variant execute_self(wfl::variant ctxt) override
Definition: attack.cpp:410
double terrain_quality
The weighted average of the % chance to hit each attacking unit.
Definition: contexts.hpp:98
double avg_damage_taken
The average hitpoints damage taken.
Definition: contexts.hpp:92
double alternative_terrain_quality
The weighted average of the % defense of the best possible terrain that the attacking units could rea...
Definition: contexts.hpp:105
bool leader_threat
Is true if the unit is a threat to our leader.
Definition: contexts.hpp:114
double avg_losses
The value on average, of units lost in the combat.
Definition: contexts.hpp:81
double vulnerability
The vulnerability is the power projection of enemy units onto the hex we're standing on.
Definition: contexts.hpp:111
double resources_used
The sum of the values of units used in the attack.
Definition: contexts.hpp:95
double rating(double aggression, const readonly_context &ai_obj) const
Definition: attack.cpp:269
bool is_surrounded
Is true if the units involved in this attack sequence are surrounded.
Definition: contexts.hpp:120
virtual default_ai_context & get_default_ai_context()
Definition: contexts.cpp:90
virtual void clear_additional_targets() const
Definition: contexts.cpp:278
recursion_counter recursion_counter_
Definition: contexts.hpp:241
int get_recursion_count() const
Get the value of the recursion counter.
Definition: contexts.hpp:229
virtual const std::vector< target > & additional_targets() const
Definition: contexts.cpp:268
virtual int rate_terrain(const unit &u, const map_location &loc) const
Definition: contexts.cpp:94
virtual std::vector< target > find_targets(const move_map &enemy_dstsrc)
Definition: contexts.cpp:125
default_ai_context_impl(readwrite_context &context, const config &)
Definition: contexts.hpp:213
virtual ~default_ai_context_impl()
Definition: contexts.cpp:63
virtual config to_default_ai_context_config() const
Definition: contexts.cpp:283
int count_free_hexes_in_castle(const map_location &loc, std::set< map_location > &checked_hexes)
Definition: contexts.cpp:67
virtual void add_target(const target &t) const
Definition: contexts.cpp:273
std::vector< target > additional_targets_
Definition: contexts.hpp:242
virtual ~default_ai_context_proxy()
Definition: contexts.cpp:53
virtual int rate_terrain(const unit &u, const map_location &loc) const
Definition: contexts.hpp:194
virtual void clear_additional_targets() const
Definition: contexts.hpp:177
int count_free_hexes_in_castle(const map_location &loc, std::set< map_location > &checked_hexes)
Definition: contexts.hpp:155
virtual std::vector< target > find_targets(const move_map &enemy_dstsrc)
Definition: contexts.hpp:187
virtual const std::vector< target > & additional_targets() const
Definition: contexts.hpp:167
default_ai_context * target_
Definition: contexts.hpp:205
virtual default_ai_context & get_default_ai_context()
Definition: contexts.hpp:182
virtual config to_default_ai_context_config() const
Definition: contexts.hpp:199
virtual void add_target(const target &t) const
Definition: contexts.hpp:172
void init_default_ai_context_proxy(default_ai_context &target)
Definition: contexts.cpp:57
virtual std::vector< target > find_targets(const move_map &enemy_dstsrc)=0
default_ai_context()
Constructor.
Definition: contexts.cpp:43
virtual const std::vector< target > & additional_targets() const =0
virtual default_ai_context & get_default_ai_context()=0
virtual int count_free_hexes_in_castle(const map_location &loc, std::set< map_location > &checked_hexes)=0
virtual ~default_ai_context()
Destructor.
Definition: contexts.cpp:47
virtual void clear_additional_targets() const =0
virtual int rate_terrain(const unit &u, const map_location &loc) const =0
virtual void add_target(const target &t) const =0
virtual config to_default_ai_context_config() const =0
void init_readwrite_context_proxy(readwrite_context &target)
Definition: contexts.hpp:882
int get_count() const
Get the current value of the recursion counter.
Definition: contexts.hpp:66
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
Encapsulates the map of the game.
Definition: map.hpp:172
Container associating units to locations.
Definition: map.hpp:98
This class represents a single unit of a specific type.
Definition: unit.hpp:133
formula_input_vector inputs() const
Definition: callable.hpp:63
Helper functions for the object which operates in the context of AI for specific side this is part of...
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:59
std::multimap< map_location, map_location > move_map
The standard way in which a map of possible moves is recorded.
Definition: game_info.hpp:43
Definition: contexts.hpp:43
std::vector< formula_input > formula_input_vector
map_location loc
Definition: contexts.hpp:33
target(const map_location &pos, double val, ai_target::type target_type=ai_target::type::village)
Definition: contexts.hpp:31
ai_target::type type
Definition: contexts.hpp:36
double value
Definition: contexts.hpp:34
Encapsulates the map of the game.
Definition: location.hpp:45