The Battle for Wesnoth  1.15.1+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 };
48 
49 
51 {
52 public:
54  wfl::action_callable(),
55  target(),
56  movements(),
57  target_value(0.0),
58  avg_losses(0.0),
59  chance_to_kill(0.0),
60  avg_damage_inflicted(0.0),
61  target_starting_damage(0),
62  avg_damage_taken(0.0),
63  resources_used(0.0),
64  terrain_quality(0.0),
65  alternative_terrain_quality(0.0),
66  vulnerability(0.0),
67  support(0.0),
68  leader_threat(false),
69  uses_leader(false),
70  is_surrounded(false)
71  {
72  }
73 
74  void analyze(const gamemap& map, unit_map& units,
75  const readonly_context& ai_obj,
76  const move_map& dstsrc, const move_map& srcdst,
77  const move_map& enemy_dstsrc, double aggression);
78 
79  double rating(double aggression, const readonly_context& ai_obj) const;
80  wfl::variant get_value(const std::string& key) const override;
81  void get_inputs(wfl::formula_input_vector& inputs) const override;
82 
83  bool attack_close(const map_location& loc) const;
84 
86  std::vector<std::pair<map_location,map_location>> movements;
87 
88  /** The value of the unit being targeted. */
89  double target_value;
90 
91  /** The value on average, of units lost in the combat. */
92  double avg_losses;
93 
94  /** Estimated % chance to kill the unit. */
96 
97  /** The average hitpoints damage inflicted. */
99 
101 
102  /** The average hitpoints damage taken. */
104 
105  /** The sum of the values of units used in the attack. */
107 
108  /** The weighted average of the % chance to hit each attacking unit. */
110 
111  /**
112  * The weighted average of the % defense of the best possible terrain
113  * that the attacking units could reach this turn, without attacking
114  * (good for comparison to see just how good/bad 'terrain_quality' is).
115  */
117 
118  /**
119  * The vulnerability is the power projection of enemy units onto the hex
120  * we're standing on. support is the power projection of friendly units.
121  */
122  double vulnerability, support;
123 
124  /** Is true if the unit is a threat to our leader. */
126 
127  /** Is true if this attack sequence makes use of the leader. */
129 
130  /** Is true if the units involved in this attack sequence are surrounded. */
132 
133  wfl::variant execute_self(wfl::variant ctxt) override;
134 };
135 
136 
137 class default_ai_context;
138 class default_ai_context : public virtual readwrite_context{
139 public:
140 
141  virtual int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes) = 0;
142 
143 
144  /** Constructor */
146 
147 
148  /** Destructor */
149  virtual ~default_ai_context();
150 
151 
152  virtual const std::vector<target>& additional_targets() const = 0;
153 
154 
155  virtual void add_target(const target& t) const = 0;
156 
157 
158  virtual void clear_additional_targets() const = 0;
159 
160 
161  virtual default_ai_context& get_default_ai_context() = 0;
162 
163 
164  virtual std::vector<target> find_targets(const move_map& enemy_dstsrc) = 0;
165 
166 
167  virtual int rate_terrain(const unit& u, const map_location& loc) const = 0;
168 
169 
170  virtual config to_default_ai_context_config() const = 0;
171 
172 
173 };
174 
175 
176 // proxies
178 public:
179 
180  int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes)
181  {
182  return target_->count_free_hexes_in_castle(loc, checked_hexes);
183  }
184 
185 
187  : target_(nullptr)
188  {
189  }
190 
191 
192  virtual ~default_ai_context_proxy();
193 
194 
195  virtual const std::vector<target>& additional_targets() const
196  {
197  return target_->additional_targets();
198  }
199 
200 
201  virtual void add_target(const target& t) const
202  {
203  target_->add_target(t);
204  }
205 
206 
207  virtual void clear_additional_targets() const
208  {
209  target_->clear_additional_targets();
210  }
211 
212 
214  {
215  return target_->get_default_ai_context();
216  }
217 
218 
219  virtual std::vector<target> find_targets(const move_map& enemy_dstsrc)
220  {
221  return target_->find_targets(enemy_dstsrc);
222  }
223 
224 
225  void init_default_ai_context_proxy(default_ai_context &target);
226 
227 
228  virtual int rate_terrain(const unit& u, const map_location& loc) const
229  {
230  return target_->rate_terrain(u,loc);
231  }
232 
233 
235  {
236  return target_->to_default_ai_context_config();
237  }
238 
239 private:
241 };
242 
243 
245 public:
246 
247  int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes);
248 
249 
251  : recursion_counter_(context.get_recursion_count()),additional_targets_()
252  {
253  init_readwrite_context_proxy(context);
254  }
255 
256 
257  virtual ~default_ai_context_impl();
258 
259 
260  virtual default_ai_context& get_default_ai_context();
261 
262 
263  virtual const std::vector<target>& additional_targets() const;
264 
265 
266  virtual void add_target(const target& t) const;
267 
268 
269  virtual void clear_additional_targets() const;
270 
271 
273  {
274  return recursion_counter_.get_count();
275  }
276 
277 
278  virtual std::vector<target> find_targets(const move_map& enemy_dstsrc);
279 
280 
281  virtual int rate_terrain(const unit& u, const map_location& loc) const;
282 
283 
284  virtual config to_default_ai_context_config() const;
285 
286 private:
288  mutable std::vector<target> additional_targets_;// TODO: refactor this (remove mutable)
289 
290 
291 };
292 
293 } //end of namespace ai
bool leader_threat
Is true if the unit is a threat to our leader.
Definition: contexts.hpp:125
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: picture.hpp:185
double avg_damage_taken
The average hitpoints damage taken.
Definition: contexts.hpp:103
recursion_counter recursion_counter_
Definition: contexts.hpp:287
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:250
double avg_damage_inflicted
The average hitpoints damage inflicted.
Definition: contexts.hpp:98
map_location target
Definition: contexts.hpp:85
double vulnerability
The vulnerability is the power projection of enemy units onto the hex we&#39;re standing on...
Definition: contexts.hpp:122
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:180
double resources_used
The sum of the values of units used in the attack.
Definition: contexts.hpp:106
double chance_to_kill
Estimated % chance to kill the unit.
Definition: contexts.hpp:95
virtual void add_target(const target &t) const
Definition: contexts.hpp:201
double terrain_quality
The weighted average of the % chance to hit each attacking unit.
Definition: contexts.hpp:109
double target_value
The value of the unit being targeted.
Definition: contexts.hpp:89
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:207
virtual std::vector< target > find_targets(const move_map &enemy_dstsrc)
Definition: contexts.hpp:219
double alternative_terrain_quality
The weighted average of the % defense of the best possible terrain that the attacking units could rea...
Definition: contexts.hpp:116
virtual config to_default_ai_context_config() const
Definition: contexts.hpp:234
TYPE type
Definition: contexts.hpp:46
int get_recursion_count() const
Get the value of the recursion counter.
Definition: contexts.hpp:272
virtual default_ai_context & get_default_ai_context()
Definition: contexts.hpp:213
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:128
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:86
double avg_losses
The value on average, of units lost in the combat.
Definition: contexts.hpp:92
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:195
double t
Definition: astarsearch.cpp:64
Definition: contexts.hpp:42
default_ai_context * target_
Definition: contexts.hpp:240
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:131
std::vector< target > additional_targets_
Definition: contexts.hpp:288
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
virtual int rate_terrain(const unit &u, const map_location &loc) const
Definition: contexts.hpp:228
Defines the MAKE_ENUM macro.