The Battle for Wesnoth  1.15.0-dev
contexts.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2018 by Yurii Chernyi <terraninfo@terraninfo.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Default AI contexts
18  */
19 
20 #pragma once
21 
22 #include "ai/contexts.hpp"
23 #include "formula/callable.hpp"
24 #include "utils/make_enum.hpp"
25 
26 //============================================================================
27 namespace ai {
28 
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  ///@todo 1.7: ai goals: this is a 'target' marker class which should be expanded with additional information which is generic enough to apply to all targets.
48 };
49 
50 
52 {
53 public:
55  wfl::action_callable(),
56  target(),
57  movements(),
58  target_value(0.0),
59  avg_losses(0.0),
60  chance_to_kill(0.0),
61  avg_damage_inflicted(0.0),
62  target_starting_damage(0),
63  avg_damage_taken(0.0),
64  resources_used(0.0),
65  terrain_quality(0.0),
66  alternative_terrain_quality(0.0),
67  vulnerability(0.0),
68  support(0.0),
69  leader_threat(false),
70  uses_leader(false),
71  is_surrounded(false)
72  {
73  }
74 
75  void analyze(const gamemap& map, unit_map& units,
76  const readonly_context& ai_obj,
77  const move_map& dstsrc, const move_map& srcdst,
78  const move_map& enemy_dstsrc, double aggression);
79 
80  double rating(double aggression, const readonly_context& ai_obj) const;
81  wfl::variant get_value(const std::string& key) const override;
82  void get_inputs(wfl::formula_input_vector& inputs) const override;
83 
84  bool attack_close(const map_location& loc) const;
85 
87  std::vector<std::pair<map_location,map_location>> movements;
88 
89  /** The value of the unit being targeted. */
90  double target_value;
91 
92  /** The value on average, of units lost in the combat. */
93  double avg_losses;
94 
95  /** Estimated % chance to kill the unit. */
97 
98  /** The average hitpoints damage inflicted. */
100 
102 
103  /** The average hitpoints damage taken. */
105 
106  /** The sum of the values of units used in the attack. */
108 
109  /** The weighted average of the % chance to hit each attacking unit. */
111 
112  /**
113  * The weighted average of the % defense of the best possible terrain
114  * that the attacking units could reach this turn, without attacking
115  * (good for comparison to see just how good/bad 'terrain_quality' is).
116  */
118 
119  /**
120  * The vulnerability is the power projection of enemy units onto the hex
121  * we're standing on. support is the power projection of friendly units.
122  */
123  double vulnerability, support;
124 
125  /** Is true if the unit is a threat to our leader. */
127 
128  /** Is true if this attack sequence makes use of the leader. */
130 
131  /** Is true if the units involved in this attack sequence are surrounded. */
133 
134  wfl::variant execute_self(wfl::variant ctxt) override;
135 };
136 
137 
138 class default_ai_context;
139 class default_ai_context : public virtual readwrite_context{
140 public:
141 
142  virtual int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes) = 0;
143 
144 
145  /** Constructor */
147 
148 
149  /** Destructor */
150  virtual ~default_ai_context();
151 
152 
153  virtual const std::vector<target>& additional_targets() const = 0;
154 
155 
156  virtual void add_target(const target& t) const = 0;
157 
158 
159  virtual void clear_additional_targets() const = 0;
160 
161 
162  virtual default_ai_context& get_default_ai_context() = 0;
163 
164 
165  virtual std::vector<target> find_targets(const move_map& enemy_dstsrc) = 0;
166 
167 
168  virtual int rate_terrain(const unit& u, const map_location& loc) const = 0;
169 
170 
171  virtual config to_default_ai_context_config() const = 0;
172 
173 
174 };
175 
176 
177 // proxies
179 public:
180 
181  int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes)
182  {
183  return target_->count_free_hexes_in_castle(loc, checked_hexes);
184  }
185 
186 
188  : target_(nullptr)
189  {
190  }
191 
192 
193  virtual ~default_ai_context_proxy();
194 
195 
196  virtual const std::vector<target>& additional_targets() const
197  {
198  return target_->additional_targets();
199  }
200 
201 
202  virtual void add_target(const target& t) const
203  {
204  target_->add_target(t);
205  }
206 
207 
208  virtual void clear_additional_targets() const
209  {
210  target_->clear_additional_targets();
211  }
212 
213 
215  {
216  return target_->get_default_ai_context();
217  }
218 
219 
220  virtual std::vector<target> find_targets(const move_map& enemy_dstsrc)
221  {
222  return target_->find_targets(enemy_dstsrc);
223  }
224 
225 
226  void init_default_ai_context_proxy(default_ai_context &target);
227 
228 
229  virtual int rate_terrain(const unit& u, const map_location& loc) const
230  {
231  return target_->rate_terrain(u,loc);
232  }
233 
234 
236  {
237  return target_->to_default_ai_context_config();
238  }
239 
240 private:
242 };
243 
244 
246 public:
247 
248  int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes);
249 
250 
252  : recursion_counter_(context.get_recursion_count()),additional_targets_()
253  {
254  init_readwrite_context_proxy(context);
255  }
256 
257 
258  virtual ~default_ai_context_impl();
259 
260 
261  virtual default_ai_context& get_default_ai_context();
262 
263 
264  virtual const std::vector<target>& additional_targets() const;
265 
266 
267  virtual void add_target(const target& t) const;
268 
269 
270  virtual void clear_additional_targets() const;
271 
272 
274  {
275  return recursion_counter_.get_count();
276  }
277 
278 
279  virtual std::vector<target> find_targets(const move_map& enemy_dstsrc);
280 
281 
282  virtual int rate_terrain(const unit& u, const map_location& loc) const;
283 
284 
285  virtual config to_default_ai_context_config() const;
286 
287 private:
289  mutable std::vector<target> additional_targets_;///@todo 1.9 refactor this
290 
291 
292 };
293 
294 } //end of namespace ai
bool leader_threat
Is true if the unit is a threat to our leader.
Definition: contexts.hpp:126
virtual default_ai_context & get_default_ai_context()=0
TYPE
UNSCALED : image will be drawn "as is" without changing size, even in case of redraw SCALED_TO_ZOOM :...
Definition: image.hpp:181
double avg_damage_taken
The average hitpoints damage taken.
Definition: contexts.hpp:104
recursion_counter recursion_counter_
Definition: contexts.hpp:288
This class represents a single unit of a specific type.
Definition: unit.hpp:99
double value
Definition: contexts.hpp:44
default_ai_context_impl(readwrite_context &context, const config &)
Definition: contexts.hpp:251
double avg_damage_inflicted
The average hitpoints damage inflicted.
Definition: contexts.hpp:99
map_location target
Definition: contexts.hpp:86
double vulnerability
The vulnerability is the power projection of enemy units onto the hex we&#39;re standing on...
Definition: contexts.hpp:123
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:181
double resources_used
The sum of the values of units used in the attack.
Definition: contexts.hpp:107
double chance_to_kill
Estimated % chance to kill the unit.
Definition: contexts.hpp:96
virtual void add_target(const target &t) const
Definition: contexts.hpp:202
double terrain_quality
The weighted average of the % chance to hit each attacking unit.
Definition: contexts.hpp:110
double target_value
The value of the unit being targeted.
Definition: contexts.hpp:90
std::multimap< map_location, map_location > move_map
The standard way in which a map of possible moves is recorded.
Definition: game_info.hpp:42
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:58
virtual void clear_additional_targets() const
Definition: contexts.hpp:208
virtual std::vector< target > find_targets(const move_map &enemy_dstsrc)
Definition: contexts.hpp:220
double alternative_terrain_quality
The weighted average of the % defense of the best possible terrain that the attacking units could rea...
Definition: contexts.hpp:117
virtual config to_default_ai_context_config() const
Definition: contexts.hpp:235
TYPE type
Definition: contexts.hpp:46
int get_recursion_count() const
Get the value of the recursion counter.
Definition: contexts.hpp:273
virtual default_ai_context & get_default_ai_context()
Definition: contexts.hpp:214
Encapsulates the map of the game.
Definition: map.hpp:36
Encapsulates the map of the game.
Definition: location.hpp:42
bool uses_leader
Is true if this attack sequence makes use of the leader.
Definition: contexts.hpp:129
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:87
double avg_losses
The value on average, of units lost in the combat.
Definition: contexts.hpp:93
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:196
double t
Definition: astarsearch.cpp:63
Definition: contexts.hpp:43
default_ai_context * target_
Definition: contexts.hpp:241
Container associating units to locations.
Definition: map.hpp:99
bool is_surrounded
Is true if the units involved in this attack sequence are surrounded.
Definition: contexts.hpp:132
std::vector< target > additional_targets_
Definition: contexts.hpp:289
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
virtual int rate_terrain(const unit &u, const map_location &loc) const
Definition: contexts.hpp:229
Defines the MAKE_ENUM macro.