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