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