The Battle for Wesnoth  1.13.10+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 - 2017 by Yurii Chernyi <terraninfo@terraninfo.net>
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 #pragma once
21 
22 #include "ai/contexts.hpp"
23 #include "formula/callable.hpp"
24 #include "utils/make_enum.hpp"
25 
26 #ifdef _MSC_VER
27 #pragma warning(push)
28 //silence "inherits via dominance" warnings
29 #pragma warning(disable:4250)
30 #endif
31 
32 //============================================================================
33 namespace ai {
34 
35 
36 struct target {
38  (VILLAGE, "village")
39  (LEADER, "leader")
40  (EXPLICIT, "explicit")
41  (THREAT, "threat")
42  (BATTLE_AID, "battle aid")
43  (MASS, "mass")
44  (SUPPORT, "support")
45  )
46 
47  target(const map_location& pos, double val, TYPE target_type=TYPE::VILLAGE) : loc(pos), value(val), type(target_type)
48  {}
49  map_location loc;
50  double value;
51 
53  ///@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.
54 };
55 
56 
58 {
59 public:
61  wfl::action_callable(),
62  target(),
63  movements(),
64  target_value(0.0),
65  avg_losses(0.0),
66  chance_to_kill(0.0),
69  avg_damage_taken(0.0),
70  resources_used(0.0),
71  terrain_quality(0.0),
73  vulnerability(0.0),
74  support(0.0),
75  leader_threat(false),
76  uses_leader(false),
77  is_surrounded(false)
78  {
79  }
80 
81  void analyze(const gamemap& map, unit_map& units,
82  const readonly_context& ai_obj,
83  const move_map& dstsrc, const move_map& srcdst,
84  const move_map& enemy_dstsrc, double aggression);
85 
86  double rating(double aggression, const readonly_context& ai_obj) const;
87  wfl::variant get_value(const std::string& key) const override;
88  void get_inputs(wfl::formula_input_vector& inputs) const override;
89 
90  bool attack_close(const map_location& loc) const;
91 
93  std::vector<std::pair<map_location,map_location> > movements;
94 
95  /** The value of the unit being targeted. */
96  double target_value;
97 
98  /** The value on average, of units lost in the combat. */
99  double avg_losses;
100 
101  /** Estimated % chance to kill the unit. */
103 
104  /** The average hitpoints damage inflicted. */
106 
108 
109  /** The average hitpoints damage taken. */
111 
112  /** The sum of the values of units used in the attack. */
114 
115  /** The weighted average of the % chance to hit each attacking unit. */
117 
118  /**
119  * The weighted average of the % defense of the best possible terrain
120  * that the attacking units could reach this turn, without attacking
121  * (good for comparison to see just how good/bad 'terrain_quality' is).
122  */
124 
125  /**
126  * The vulnerability is the power projection of enemy units onto the hex
127  * we're standing on. support is the power projection of friendly units.
128  */
130 
131  /** Is true if the unit is a threat to our leader. */
133 
134  /** Is true if this attack sequence makes use of the leader. */
136 
137  /** Is true if the units involved in this attack sequence are surrounded. */
139 
140  wfl::variant execute_self(wfl::variant ctxt) override;
141 };
142 
143 
144 class default_ai_context;
145 class default_ai_context : public virtual readwrite_context{
146 public:
147 
148  virtual int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes) = 0;
149 
150 
151  /** Constructor */
153 
154 
155  /** Destructor */
156  virtual ~default_ai_context();
157 
158 
159  virtual const std::vector<target>& additional_targets() const = 0;
160 
161 
162  virtual void add_target(const target& t) const = 0;
163 
164 
165  virtual void clear_additional_targets() const = 0;
166 
167 
169 
170 
171  virtual std::vector<target> find_targets(const move_map& enemy_dstsrc) = 0;
172 
173 
174  virtual int rate_terrain(const unit& u, const map_location& loc) const = 0;
175 
176 
177  virtual config to_default_ai_context_config() const = 0;
178 
179 
180 };
181 
182 
183 // proxies
185 public:
186 
187  int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes)
188  {
189  return target_->count_free_hexes_in_castle(loc, checked_hexes);
190  }
191 
192 
194  : target_(nullptr)
195  {
196  }
197 
198 
199  virtual ~default_ai_context_proxy();
200 
201 
202  virtual const std::vector<target>& additional_targets() const
203  {
204  return target_->additional_targets();
205  }
206 
207 
208  virtual void add_target(const target& t) const
209  {
210  target_->add_target(t);
211  }
212 
213 
214  virtual void clear_additional_targets() const
215  {
217  }
218 
219 
221  {
223  }
224 
225 
226  virtual std::vector<target> find_targets(const move_map& enemy_dstsrc)
227  {
228  return target_->find_targets(enemy_dstsrc);
229  }
230 
231 
233 
234 
235  virtual int rate_terrain(const unit& u, const map_location& loc) const
236  {
237  return target_->rate_terrain(u,loc);
238  }
239 
240 
242  {
244  }
245 
246 private:
248 };
249 
250 
252 public:
253 
254  int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes);
255 
256 
259  {
261  }
262 
263 
264  virtual ~default_ai_context_impl();
265 
266 
268 
269 
270  virtual const std::vector<target>& additional_targets() const;
271 
272 
273  virtual void add_target(const target& t) const;
274 
275 
276  virtual void clear_additional_targets() const;
277 
278 
280  {
281  return recursion_counter_.get_count();
282  }
283 
284 
285  virtual std::vector<target> find_targets(const move_map& enemy_dstsrc);
286 
287 
288  virtual int rate_terrain(const unit& u, const map_location& loc) const;
289 
290 
291  virtual config to_default_ai_context_config() const;
292 
293 private:
295  mutable std::vector<target> additional_targets_;///@todo 1.9 refactor this
296 
297 
298 };
299 
300 } //end of namespace ai
301 
302 #ifdef _MSC_VER
303 #pragma warning(pop)
304 #endif
bool leader_threat
Is true if the unit is a threat to our leader.
Definition: contexts.hpp:132
virtual default_ai_context & get_default_ai_context()=0
virtual void add_target(const target &t) const
Definition: contexts.cpp:287
TYPE
UNSCALED : image will be drawn "as is" without changing size, even in case of redraw SCALED_TO_ZOOM :...
Definition: image.hpp:189
double avg_damage_taken
The average hitpoints damage taken.
Definition: contexts.hpp:110
virtual int rate_terrain(const unit &u, const map_location &loc) const
Definition: contexts.cpp:100
virtual void add_target(const target &t) const
Definition: contexts.hpp:208
std::vector< char_t > string
virtual const std::vector< target > & additional_targets() const =0
recursion_counter recursion_counter_
Definition: contexts.hpp:294
This class represents a single unit of a specific type.
Definition: unit.hpp:100
double value
Definition: contexts.hpp:50
default_ai_context_impl(readwrite_context &context, const config &)
Definition: contexts.hpp:257
double avg_damage_inflicted
The average hitpoints damage inflicted.
Definition: contexts.hpp:105
MAKE_ENUM(TYPE,(VILLAGE,"village")(LEADER,"leader")(EXPLICIT,"explicit")(THREAT,"threat")(BATTLE_AID,"battle aid")(MASS,"mass")(SUPPORT,"support")) target(const map_location &pos
map_location target
Definition: contexts.hpp:92
double vulnerability
The vulnerability is the power projection of enemy units onto the hex we're standing on...
Definition: contexts.hpp:129
std::vector< formula_input > formula_input_vector
formula_input_vector inputs() const
Definition: callable.hpp:62
int count_free_hexes_in_castle(const map_location &loc, std::set< map_location > &checked_hexes)
Definition: contexts.hpp:187
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:113
double chance_to_kill
Estimated % chance to kill the unit.
Definition: contexts.hpp:102
virtual std::vector< target > find_targets(const move_map &enemy_dstsrc)
Definition: contexts.cpp:131
double terrain_quality
The weighted average of the % chance to hit each attacking unit.
Definition: contexts.hpp:116
double target_value
The value of the unit being targeted.
Definition: contexts.hpp:96
std::multimap< map_location, map_location > move_map
The standard way in which a map of possible moves is recorded.
Definition: game_info.hpp:39
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:58
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:45
virtual std::vector< target > find_targets(const move_map &enemy_dstsrc)
Definition: contexts.hpp:226
virtual const std::vector< target > & additional_targets() const
Definition: contexts.hpp:202
int get_recursion_count() const
Get the value of the recursion counter.
Definition: contexts.hpp:279
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:123
TYPE type
Definition: contexts.hpp:52
bool attack_close(const map_location &loc) const
Definition: attack.cpp:263
virtual default_ai_context & get_default_ai_context()
Definition: contexts.hpp:220
virtual config to_default_ai_context_config() const
Definition: contexts.hpp:241
Encapsulates the map of the game.
Definition: map.hpp:34
void init_default_ai_context_proxy(default_ai_context &target)
Definition: contexts.cpp:58
wfl::variant get_value(const std::string &key) const override
Definition: attack.cpp:341
Encapsulates the map of the game.
Definition: location.hpp:40
bool uses_leader
Is true if this attack sequence makes use of the leader.
Definition: contexts.hpp:135
double TYPE target_type
Definition: contexts.hpp:47
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:76
double val
Definition: contexts.hpp:47
virtual ~default_ai_context()
Destructor.
Definition: contexts.cpp:48
std::vector< std::pair< map_location, map_location > > movements
Definition: contexts.hpp:93
void get_inputs(wfl::formula_input_vector &inputs) const override
Definition: attack.cpp:396
virtual int rate_terrain(const unit &u, const map_location &loc) const
Definition: contexts.hpp:235
double avg_losses
The value on average, of units lost in the combat.
Definition: contexts.hpp:99
virtual int rate_terrain(const unit &u, const map_location &loc) const =0
virtual config to_default_ai_context_config() const
Definition: contexts.cpp:298
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
double t
Definition: astarsearch.cpp:64
Definition: contexts.hpp:42
virtual void clear_additional_targets() const =0
default_ai_context * target_
Definition: contexts.hpp:247
virtual void clear_additional_targets() const
Definition: contexts.cpp:293
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:138
void init_readwrite_context_proxy(readwrite_context &target)
Definition: contexts.hpp:1011
virtual void clear_additional_targets() const
Definition: contexts.hpp:214
std::vector< target > additional_targets_
Definition: contexts.hpp:295
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
virtual config to_default_ai_context_config() const =0
virtual ~default_ai_context_impl()
Definition: contexts.cpp:64
Defines the MAKE_ENUM macro.
virtual std::vector< target > find_targets(const move_map &enemy_dstsrc)=0
unit_map * units
Definition: resources.cpp:34
virtual const std::vector< target > & additional_targets() const
Definition: contexts.cpp:281
virtual void add_target(const target &t) const =0
double rating(double aggression, const readonly_context &ai_obj) const
Definition: attack.cpp:276
wfl::variant execute_self(wfl::variant ctxt) override
Definition: attack.cpp:417