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