The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
contexts.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2016 by Yurii Chernyi <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #ifndef AI_DEFAULT_CONTEXTS_HPP_INCLUDED
21 #define AI_DEFAULT_CONTEXTS_HPP_INCLUDED
22 
23 #include "ai/contexts.hpp"
24 #include "formula/callable.hpp"
25 #include "utils/make_enum.hpp"
26 
27 #ifdef _MSC_VER
28 #if _MSC_VER < 1600
29 // SDL2 uses [dfi]vec.h which used to define EXPLICIT
30 #undef EXPLICIT
31 #endif
32 #pragma warning(push)
33 //silence "inherits via dominance" warnings
34 #pragma warning(disable:4250)
35 #endif
36 
37 //============================================================================
38 namespace ai {
39 
40 
41 struct target {
43  (VILLAGE, "village")
44  (LEADER, "leader")
45  (EXPLICIT, "explicit")
46  (THREAT, "threat")
47  (BATTLE_AID, "battle aid")
48  (MASS, "mass")
49  (SUPPORT, "support")
50  );
51 
52  target(const map_location& pos, double val, TYPE target_type=TYPE::VILLAGE) : loc(pos), value(val), type(target_type)
53  {}
55  double value;
56 
58  ///@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.
59 };
60 
61 
63 {
64 public:
67  target(),
68  movements(),
69  target_value(0.0),
70  avg_losses(0.0),
71  chance_to_kill(0.0),
74  avg_damage_taken(0.0),
75  resources_used(0.0),
76  terrain_quality(0.0),
78  vulnerability(0.0),
79  support(0.0),
80  leader_threat(false),
81  uses_leader(false),
82  is_surrounded(false)
83  {
84  }
85 
86  void analyze(const gamemap& map, unit_map& units,
87  const readonly_context& ai_obj,
88  const move_map& dstsrc, const move_map& srcdst,
89  const move_map& enemy_dstsrc, double aggression);
90 
91  double rating(double aggression, const readonly_context& ai_obj) const;
92  variant get_value(const std::string& key) const;
93  void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
94 
95  bool attack_close(const map_location& loc) const;
96 
98  std::vector<std::pair<map_location,map_location> > movements;
99 
100  /** The value of the unit being targeted. */
101  double target_value;
102 
103  /** The value on average, of units lost in the combat. */
104  double avg_losses;
105 
106  /** Estimated % chance to kill the unit. */
108 
109  /** The average hitpoints damage inflicted. */
111 
113 
114  /** The average hitpoints damage taken. */
116 
117  /** The sum of the values of units used in the attack. */
119 
120  /** The weighted average of the % chance to hit each attacking unit. */
122 
123  /**
124  * The weighted average of the % defense of the best possible terrain
125  * that the attacking units could reach this turn, without attacking
126  * (good for comparison to see just how good/bad 'terrain_quality' is).
127  */
129 
130  /**
131  * The vulnerability is the power projection of enemy units onto the hex
132  * we're standing on. support is the power projection of friendly units.
133  */
135 
136  /** Is true if the unit is a threat to our leader. */
138 
139  /** Is true if this attack sequence makes use of the leader. */
141 
142  /** Is true if the units involved in this attack sequence are surrounded. */
144 
145 };
146 
147 
148 class default_ai_context;
149 class default_ai_context : public virtual readwrite_context{
150 public:
151 
152  virtual int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes) = 0;
153 
154 
155  /** Constructor */
157 
158 
159  /** Destructor */
160  virtual ~default_ai_context();
161 
162 
163  virtual const std::vector<target>& additional_targets() const = 0;
164 
165 
166  virtual void add_target(const target& t) const = 0;
167 
168 
169  virtual void clear_additional_targets() const = 0;
170 
171 
173 
174 
175  virtual std::vector<target> find_targets(const move_map& enemy_dstsrc) = 0;
176 
177 
178  virtual int rate_terrain(const unit& u, const map_location& loc) const = 0;
179 
180 
181  virtual config to_default_ai_context_config() const = 0;
182 
183 
184 };
185 
186 
187 // proxies
189 public:
190 
191  int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes)
192  {
193  return target_->count_free_hexes_in_castle(loc, checked_hexes);
194  }
195 
196 
198  : target_(nullptr)
199  {
200  }
201 
202 
203  virtual ~default_ai_context_proxy();
204 
205 
206  virtual const std::vector<target>& additional_targets() const
207  {
208  return target_->additional_targets();
209  }
210 
211 
212  virtual void add_target(const target& t) const
213  {
214  target_->add_target(t);
215  }
216 
217 
218  virtual void clear_additional_targets() const
219  {
221  }
222 
223 
225  {
227  }
228 
229 
230  virtual std::vector<target> find_targets(const move_map& enemy_dstsrc)
231  {
232  return target_->find_targets(enemy_dstsrc);
233  }
234 
235 
237 
238 
239  virtual int rate_terrain(const unit& u, const map_location& loc) const
240  {
241  return target_->rate_terrain(u,loc);
242  }
243 
244 
246  {
248  }
249 
250 private:
252 };
253 
254 
256 public:
257 
258  int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes);
259 
260 
263  {
265  }
266 
267 
268  virtual ~default_ai_context_impl();
269 
270 
272 
273 
274  virtual const std::vector<target>& additional_targets() const;
275 
276 
277  virtual void add_target(const target& t) const;
278 
279 
280  virtual void clear_additional_targets() const;
281 
282 
284  {
285  return recursion_counter_.get_count();
286  }
287 
288 
289  virtual std::vector<target> find_targets(const move_map& enemy_dstsrc);
290 
291 
292  virtual int rate_terrain(const unit& u, const map_location& loc) const;
293 
294 
295  virtual config to_default_ai_context_config() const;
296 
297 private:
299  mutable std::vector<target> additional_targets_;///@todo 1.9 refactor this
300 
301 
302 };
303 
304 } //end of namespace ai
305 
306 #ifdef _MSC_VER
307 #pragma warning(pop)
308 #endif
309 
310 
311 #endif
bool leader_threat
Is true if the unit is a threat to our leader.
Definition: contexts.hpp:137
virtual default_ai_context & get_default_ai_context()=0
virtual void add_target(const target &t) const
Definition: contexts.cpp:292
TYPE
UNSCALED : image will be drawn "as is" without changing size, even in case of redraw SCALED_TO_ZOOM :...
Definition: image.hpp:208
double avg_damage_taken
The average hitpoints damage taken.
Definition: contexts.hpp:115
virtual int rate_terrain(const unit &u, const map_location &loc) const
Definition: contexts.cpp:100
map_location loc
Definition: contexts.hpp:54
virtual void add_target(const target &t) const
Definition: contexts.hpp:212
variant get_value(const std::string &key) const
Definition: attack.cpp:335
virtual const std::vector< target > & additional_targets() const =0
recursion_counter recursion_counter_
Definition: contexts.hpp:298
Definition: unit.hpp:95
MAKE_ENUM(TYPE,(VILLAGE,"village")(LEADER,"leader")(EXPLICIT,"explicit")(THREAT,"threat")(BATTLE_AID,"battle aid")(MASS,"mass")(SUPPORT,"support"))
double value
Definition: contexts.hpp:55
int pos
Definition: formula.cpp:800
default_ai_context_impl(readwrite_context &context, const config &)
Definition: contexts.hpp:261
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
double avg_damage_inflicted
The average hitpoints damage inflicted.
Definition: contexts.hpp:110
map_location target
Definition: contexts.hpp:97
double vulnerability
The vulnerability is the power projection of enemy units onto the hex we're standing on...
Definition: contexts.hpp:134
GLuint const GLfloat * val
Definition: glew.h:2614
int count_free_hexes_in_castle(const map_location &loc, std::set< map_location > &checked_hexes)
Definition: contexts.hpp:191
int count_free_hexes_in_castle(const map_location &loc, std::set< map_location > &checked_hexes)
Definition: contexts.cpp:69
double resources_used
The sum of the values of units used in the attack.
Definition: contexts.hpp:118
double chance_to_kill
Estimated % chance to kill the unit.
Definition: contexts.hpp:107
virtual std::vector< target > find_targets(const move_map &enemy_dstsrc)
Definition: contexts.cpp:131
GLdouble GLdouble t
Definition: glew.h:1366
double terrain_quality
The weighted average of the % chance to hit each attacking unit.
Definition: contexts.hpp:121
double target_value
The value of the unit being targeted.
Definition: contexts.hpp:101
std::multimap< map_location, map_location > move_map
The standard way in which a map of possible moves is recorded.
Definition: game_info.hpp:47
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:57
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:40
virtual std::vector< target > find_targets(const move_map &enemy_dstsrc)
Definition: contexts.hpp:230
virtual const std::vector< target > & additional_targets() const
Definition: contexts.hpp:206
int get_recursion_count() const
Get the value of the recursion counter.
Definition: contexts.hpp:283
default_ai_context()
Constructor.
Definition: contexts.cpp:43
virtual default_ai_context & get_default_ai_context()
Definition: contexts.cpp:95
double alternative_terrain_quality
The weighted average of the % defense of the best possible terrain that the attacking units could rea...
Definition: contexts.hpp:128
void get_inputs(std::vector< game_logic::formula_input > *inputs) const
Definition: attack.cpp:390
TYPE type
Definition: contexts.hpp:57
GLsizei const GLfloat * value
Definition: glew.h:1817
bool attack_close(const map_location &loc) const
Definition: attack.cpp:251
virtual default_ai_context & get_default_ai_context()
Definition: contexts.hpp:224
virtual config to_default_ai_context_config() const
Definition: contexts.hpp:245
Encapsulates the map of the game.
Definition: map.hpp:37
target(const map_location &pos, double val, TYPE target_type=TYPE::VILLAGE)
Definition: contexts.hpp:52
formula_callable(bool has_self=true)
Definition: callable.hpp:36
std::vector< formula_input > inputs() const
Definition: callable.hpp:50
void init_default_ai_context_proxy(default_ai_context &target)
Definition: contexts.cpp:58
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:140
virtual int count_free_hexes_in_castle(const map_location &loc, std::set< map_location > &checked_hexes)=0
int get_count() const
Get the current value of the recursion counter.
Definition: contexts.hpp:79
virtual ~default_ai_context()
Destructor.
Definition: contexts.cpp:48
std::vector< std::pair< map_location, map_location > > movements
Definition: contexts.hpp:98
virtual int rate_terrain(const unit &u, const map_location &loc) const
Definition: contexts.hpp:239
double avg_losses
The value on average, of units lost in the combat.
Definition: contexts.hpp:104
virtual int rate_terrain(const unit &u, const map_location &loc) const =0
virtual config to_default_ai_context_config() const
Definition: contexts.cpp:303
Helper functions for the object which operates in the context of AI for specific side this is part of...
virtual ~default_ai_context_proxy()
Definition: contexts.cpp:54
virtual void clear_additional_targets() const =0
default_ai_context * target_
Definition: contexts.hpp:251
virtual void clear_additional_targets() const
Definition: contexts.cpp:298
Container associating units to locations.
Definition: map.hpp:90
bool is_surrounded
Is true if the units involved in this attack sequence are surrounded.
Definition: contexts.hpp:143
void init_readwrite_context_proxy(readwrite_context &target)
Definition: contexts.hpp:1014
virtual void clear_additional_targets() const
Definition: contexts.hpp:218
std::vector< target > additional_targets_
Definition: contexts.hpp:299
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
virtual config to_default_ai_context_config() const =0
virtual ~default_ai_context_impl()
Definition: contexts.cpp:64
Defines the MAKE_ENUM macro.
GLsizei const GLcharARB ** string
Definition: glew.h:4503
virtual std::vector< target > find_targets(const move_map &enemy_dstsrc)=0
unit_map * units
Definition: resources.cpp:35
virtual const std::vector< target > & additional_targets() const
Definition: contexts.cpp:286
virtual void add_target(const target &t) const =0
double rating(double aggression, const readonly_context &ai_obj) const
Definition: attack.cpp:264