The Battle for Wesnoth  1.17.23+dev
contexts.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2023
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;
126 class default_ai_context : public virtual readwrite_context{
127 public:
128 
129  virtual int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes) = 0;
130 
131  /** Constructor */
133 
134  /** Destructor */
135  virtual ~default_ai_context();
136 
137  virtual const std::vector<target>& additional_targets() const = 0;
138 
139  virtual void add_target(const target& t) const = 0;
140 
141  virtual void clear_additional_targets() const = 0;
142 
144 
145  virtual std::vector<target> find_targets(const move_map& enemy_dstsrc) = 0;
146 
147  virtual int rate_terrain(const unit& u, const map_location& loc) const = 0;
148 
150 };
151 
152 // proxies
154 public:
155 
156  int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes)
157  {
158  return target_->count_free_hexes_in_castle(loc, checked_hexes);
159  }
160 
162  : target_(nullptr)
163  {
164  }
165 
166  virtual ~default_ai_context_proxy();
167 
168  virtual const std::vector<target>& additional_targets() const
169  {
170  return target_->additional_targets();
171  }
172 
173  virtual void add_target(const target& t) const
174  {
175  target_->add_target(t);
176  }
177 
178  virtual void clear_additional_targets() const
179  {
181  }
182 
184  {
186  }
187 
188  virtual std::vector<target> find_targets(const move_map& enemy_dstsrc)
189  {
190  return target_->find_targets(enemy_dstsrc);
191  }
192 
194 
195  virtual int rate_terrain(const unit& u, const map_location& loc) const
196  {
197  return target_->rate_terrain(u,loc);
198  }
199 
201  {
203  }
204 
205 private:
207 };
208 
210 public:
211 
212  int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes);
213 
216  {
218  }
219 
220  virtual ~default_ai_context_impl();
221 
223 
224  virtual const std::vector<target>& additional_targets() const;
225 
226  virtual void add_target(const target& t) const;
227 
228  virtual void clear_additional_targets() const;
229 
231  {
232  return recursion_counter_.get_count();
233  }
234 
235  virtual std::vector<target> find_targets(const move_map& enemy_dstsrc);
236 
237  virtual int rate_terrain(const unit& u, const map_location& loc) const;
238 
239  virtual config to_default_ai_context_config() const;
240 
241 private:
243  mutable std::vector<target> additional_targets_;// TODO: refactor this (remove mutable)
244 
245 };
246 
247 } //end of namespace ai
double t
Definition: astarsearch.cpp:65
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:46
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:335
void get_inputs(wfl::formula_input_vector &inputs) const override
Definition: attack.cpp:390
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:257
wfl::variant execute_self(wfl::variant ctxt) override
Definition: attack.cpp:411
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:270
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:282
recursion_counter recursion_counter_
Definition: contexts.hpp:242
int get_recursion_count() const
Get the value of the recursion counter.
Definition: contexts.hpp:230
virtual const std::vector< target > & additional_targets() const
Definition: contexts.cpp:272
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:214
virtual ~default_ai_context_impl()
Definition: contexts.cpp:63
virtual config to_default_ai_context_config() const
Definition: contexts.cpp:287
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:277
std::vector< target > additional_targets_
Definition: contexts.hpp:243
virtual ~default_ai_context_proxy()
Definition: contexts.cpp:53
virtual int rate_terrain(const unit &u, const map_location &loc) const
Definition: contexts.hpp:195
virtual void clear_additional_targets() const
Definition: contexts.hpp:178
int count_free_hexes_in_castle(const map_location &loc, std::set< map_location > &checked_hexes)
Definition: contexts.hpp:156
virtual std::vector< target > find_targets(const move_map &enemy_dstsrc)
Definition: contexts.hpp:188
virtual const std::vector< target > & additional_targets() const
Definition: contexts.hpp:168
default_ai_context * target_
Definition: contexts.hpp:206
virtual default_ai_context & get_default_ai_context()
Definition: contexts.hpp:183
virtual config to_default_ai_context_config() const
Definition: contexts.hpp:200
virtual void add_target(const target &t) const
Definition: contexts.hpp:173
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:889
int get_count() const
Get the current value of the recursion counter.
Definition: contexts.hpp:70
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
Encapsulates the map of the game.
Definition: map.hpp:172
Container associating units to locations.
Definition: map.hpp:99
This class represents a single unit of a specific type.
Definition: unit.hpp:135
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:61
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:44
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:38