The Battle for Wesnoth  1.15.0-dev
attack.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.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  * Various functions that implement attacks and attack calculations.
18  * Unit advancements are also included, as they usually occur as a
19  * result of combat.
20  */
21 
22 #pragma once
23 
25 #include "attack_prediction.hpp"
26 #include "units/types.hpp"
27 
28 #include <vector>
29 
30 struct map_location;
31 class team;
32 struct time_of_day;
33 class unit;
34 class unit_map;
35 class gamemap;
36 
37 /** Calculates the number of blows resulting from swarm. */
38 inline unsigned swarm_blows(unsigned min_blows, unsigned max_blows, unsigned hp, unsigned max_hp)
39 {
40  return hp >= max_hp
41  ? max_blows
42  : max_blows < min_blows
43  ? min_blows - (min_blows - max_blows) * hp / max_hp
44  : min_blows + (max_blows - min_blows) * hp / max_hp;
45 }
46 
47 /** Structure describing the statistics of a unit involved in the battle. */
49 {
50  const_attack_ptr weapon; /**< The weapon used by the unit to attack the opponent, or nullptr if there is none. */
51  int attack_num; /**< Index into unit->attacks() or -1 for none. */
52  bool is_attacker; /**< True if the unit is the attacker. */
53  bool is_poisoned; /**< True if the unit is poisoned at the beginning of the battle. */
54  bool is_slowed; /**< True if the unit is slowed at the beginning of the battle. */
55  bool slows; /**< Attack slows opponent when it hits. */
56  bool drains; /**< Attack drains opponent when it hits. */
57  bool petrifies; /**< Attack petrifies opponent when it hits. */
58  bool plagues; /**< Attack turns opponent into a zombie when fatal. */
59  bool poisons; /**< Attack poisons opponent when it hits. */
60  bool backstab_pos; /**<
61  * True if the attacker is in *position* to backstab the defender (this is used to
62  * determine whether to apply the backstab bonus in case the attacker has backstab).
63  */
64  bool swarm; /**< Attack has swarm special. */
65  bool firststrike; /**< Attack has firststrike special. */
66  bool disable; /**< Attack has disable special. */
67  unsigned int experience, max_experience;
68  unsigned int level;
69 
70  unsigned int rounds; /**< Berserk special can force us to fight more than one round. */
71  unsigned int hp; /**< Hitpoints of the unit at the beginning of the battle. */
72  unsigned int max_hp; /**< Maximum hitpoints of the unit. */
73  unsigned int chance_to_hit; /**< Effective chance to hit as a percentage (all factors accounted for). */
74  int damage; /**< Effective damage of the weapon (all factors accounted for). */
75  int slow_damage; /**< Effective damage if unit becomes slowed (== damage, if already slowed) */
76  int drain_percent; /**< Percentage of damage recovered as health */
77  int drain_constant; /**< Base HP drained regardless of damage dealt */
78  unsigned int num_blows; /**< Effective number of blows, takes swarm into account. */
79  unsigned int swarm_min; /**< Minimum number of blows with swarm (equal to num_blows if swarm isn't used). */
80  unsigned int swarm_max; /**< Maximum number of blows with swarm (equal to num_blows if swarm isn't used). */
81 
82  std::string plague_type; /**< The plague type used by the attack, if any. */
83 
85  const map_location& u_loc,
86  int u_attack_num,
87  bool attacking,
88  const unit& opp,
89  const map_location& opp_loc,
90  const_attack_ptr opp_weapon,
91  const unit_map& units);
92 
93  /** Used by AI for combat analysis */
95  const_attack_ptr att_weapon,
96  bool attacking,
97  const unit_type* opp_type,
98  const_attack_ptr opp_weapon,
99  unsigned int opp_terrain_defense,
100  int lawful_bonus = 0);
101 
103  {
104  }
105 
106  /// Calculates the number of blows we would have if we had @a new_hp
107  // instead of the recorded hp.
108  unsigned int calc_blows(unsigned new_hp) const
109  {
110  return swarm_blows(swarm_min, swarm_max, new_hp, max_hp);
111  }
112 
113 #if defined(BENCHMARK) || defined(CHECK)
114  /**
115  * Special constructor for the stand-alone version of attack_prediction.cpp.
116  * (This hardcodes some standard abilities for testing purposes.)
117  */
119  int blows,
120  int hitpoints,
121  int maximum_hp,
122  int hit_chance,
123  bool drain,
124  bool slows,
125  bool slowed,
126  bool berserk,
127  bool first,
128  bool do_swarm)
129  : weapon(nullptr) // Not used in attack prediction.
130  , attack_num(0) // Not used in attack prediction.
131  , is_attacker(true) // Not used in attack prediction.
132  , is_poisoned(false)
133  , is_slowed(slowed)
134  , slows(slows)
135  , drains(drain)
136  , petrifies(false)
137  , plagues(false)
138  , poisons(false)
139  , backstab_pos(false)
140  , swarm(do_swarm)
141  , firststrike(first)
142  , disable(false)
143  , experience(0) // No units should advance in the attack prediction tests.
144  , max_experience(50) // No units should advance in the attack prediction tests.
145  , level(1) // No units should advance in the attack prediction tests.
146  , rounds(berserk ? 30 : 1)
147  , hp(std::max<int>(0, hitpoints))
148  , max_hp(std::max<int>(1, maximum_hp))
149  , chance_to_hit(hit_chance)
150  , damage(std::max(0, dmg))
151  , slow_damage(round_damage(damage, 1, 2))
152  , drain_percent(drain ? 50 : 0)
153  , drain_constant(0)
154  , num_blows(do_swarm ? blows * hp / max_hp : blows)
155  , swarm_min(do_swarm ? 0 : blows)
156  , swarm_max(blows)
157  , plague_type()
158  {
159  if(slowed) {
160  damage = slow_damage;
161  }
162 
163  if(hp > max_hp) {
164  hp = max_hp; // Keeps the prob_matrix from going out of bounds.
165  }
166  }
167 #endif
168 };
169 
170 /** Computes the statistics of a battle between an attacker and a defender unit. */
172 {
173 public:
174  /**
175  * If no attacker_weapon is given, we select the best one,
176  * based on harm_weight (1.0 means 1 hp lost counters 1 hp damage,
177  * 0.0 means we ignore harm weight).
178  * prev_def is for predicting multiple attacks against a defender.
179  */
180  battle_context(const unit_map& units,
181  const map_location& attacker_loc,
182  const map_location& defender_loc,
183  int attacker_weapon = -1,
184  int defender_weapon = -1,
185  double aggression = 0.0,
186  const combatant* prev_def = nullptr,
187  const unit* attacker_ptr = nullptr,
188  const unit* defender_ptr = nullptr);
189 
190  /** Used by the AI which caches battle_context_unit_stats */
192 
193  battle_context(battle_context&& other) = default;
194 
195  battle_context& operator=(battle_context&& other) = default;
196 
197  /** This method returns the statistics of the attacker. */
199  {
200  return *attacker_stats_;
201  }
202 
203  /** This method returns the statistics of the defender. */
205  {
206  return *defender_stats_;
207  }
208 
209  /** Get the simulation results. */
210  const combatant& get_attacker_combatant(const combatant* prev_def = nullptr);
211  const combatant& get_defender_combatant(const combatant* prev_def = nullptr);
212 
213  /** Given this harm_weight, is this attack better than that? */
214  bool better_attack(class battle_context& that, double harm_weight);
215  /** Given this harm_weight, is this attack better than that? */
216  bool better_defense(class battle_context& that, double harm_weight);
217 
218  static bool better_combat(const combatant& us_a,
219  const combatant& them_a,
220  const combatant& us_b,
221  const combatant& them_b,
222  double harm_weight);
223 
224  void simulate(const combatant* prev_def);
225 private:
227  const unit& attacker,
228  const map_location& attacker_loc,
229  int attacker_weapon,
230  const unit& defender,
231  const map_location& defender_loc,
232  int defender_weapon,
233  const unit_map& units);
234 
235  static battle_context choose_attacker_weapon(const unit& attacker,
236  const unit& defender,
237  const unit_map& units,
238  const map_location& attacker_loc,
239  const map_location& defender_loc,
240  double harm_weight,
241  const combatant* prev_def);
242 
243  static battle_context choose_defender_weapon(const unit& attacker,
244  const unit& defender,
245  unsigned attacker_weapon,
246  const unit_map& units,
247  const map_location& attacker_loc,
248  const map_location& defender_loc,
249  const combatant* prev_def);
250 
251  /** Statistics of the units. */
252  std::unique_ptr<battle_context_unit_stats> attacker_stats_;
253  std::unique_ptr<battle_context_unit_stats> defender_stats_;
254 
255  /** Outcome of simulated fight. */
256  std::unique_ptr<combatant> attacker_combatant_;
257  std::unique_ptr<combatant> defender_combatant_;
258 };
259 
260 /** Performs an attack. */
261 void attack_unit(const map_location& attacker,
262  const map_location& defender,
263  int attack_with,
264  int defend_with,
265  bool update_display = true);
266 
267 /** Performs an attack, and advanced the units afterwards */
268 void attack_unit_and_advance(const map_location& attacker,
269  const map_location& defender,
270  int attack_with,
271  int defend_with,
272  bool update_display = true,
274 
275 /**
276  * Tests if the unit at loc is currently affected by leadership.
277  * (i.e. has a higher-level unit with the 'leadership' ability next to it).
278  *
279  * Returns the bonus percentage (possibly 0 if there's no leader adjacent).
280  */
281 int under_leadership(const unit &u, const map_location& loc, const_attack_ptr weapon, const_attack_ptr opp_weapon = nullptr);
282 bool leadership_affects_self(const std::string& ability,const unit_map& units, const map_location& loc, const_attack_ptr weapon=nullptr,const_attack_ptr opp_weapon=nullptr);
283 bool leadership_affects_opponent(const std::string& ability,const unit_map& units, const map_location& loc, const_attack_ptr weapon=nullptr,const_attack_ptr opp_weapon=nullptr);
284 std::pair<int, bool> ability_leadership(const std::string& ability, const unit_map& units, const map_location& loc, const map_location& opp_loc, bool attacker=true, int abil_value=0, bool backstab_pos=false, const_attack_ptr weapon=nullptr, const_attack_ptr opp_weapon=nullptr);
285 bool bool_leadership(const std::string& ability,const unit_map& units, const map_location& loc, const map_location& opp_loc, bool attacker=true, const_attack_ptr weapon=nullptr, const_attack_ptr opp_weapon=nullptr);
286 
287 /**
288  * Returns the amount that a unit's damage should be multiplied by
289  * due to the current time of day.
290  */
291 int combat_modifier(const unit_map& units,
292  const gamemap& map,
293  const map_location& loc,
294  unit_type::ALIGNMENT alignment,
295  bool is_fearless);
296 
297 /**
298  * Returns the amount that a unit's damage should be multiplied by
299  * due to the current time of day.
300  */
301 int combat_modifier(const time_of_day& effective_tod,
302  unit_type::ALIGNMENT alignment,
303  bool is_fearless);
304 
305 /**
306  * Returns the amount that a unit's damage should be multiplied by
307  * due to a given lawful_bonus.
308  */
309 int generic_combat_modifier(int lawful_bonus, unit_type::ALIGNMENT alignment, bool is_fearless);
310 /**
311  * Function to check if an attack will satisfy the requirements for backstab.
312  * Input:
313  * - the location from which the attack will occur,
314  * - the defending unit location,
315  * - the list of units on the map and
316  * - the list of teams.
317  * The defender and opposite units should be in place already.
318  * The attacking unit doesn't need to be, but if it isn't,
319  * an external check should be made to make sure the opposite unit
320  * isn't also the attacker.
321  */
322 bool backstab_check(const map_location& attacker_loc,
323  const map_location& defender_loc,
324  const unit_map& units,
325  const std::vector<team>& teams);
const_attack_ptr weapon
The weapon used by the unit to attack the opponent, or nullptr if there is none.
Definition: attack.hpp:50
std::unique_ptr< combatant > attacker_combatant_
Outcome of simulated fight.
Definition: attack.hpp:256
unsigned int calc_blows(unsigned new_hp) const
Calculates the number of blows we would have if we had new_hp.
Definition: attack.hpp:108
std::string plague_type
The plague type used by the attack, if any.
Definition: attack.hpp:82
This class represents a single unit of a specific type.
Definition: unit.hpp:99
const battle_context_unit_stats & get_defender_stats() const
This method returns the statistics of the defender.
Definition: attack.hpp:204
unsigned int hp
Hitpoints of the unit at the beginning of the battle.
Definition: attack.hpp:71
bool is_slowed
True if the unit is slowed at the beginning of the battle.
Definition: attack.hpp:54
bool slows
Attack slows opponent when it hits.
Definition: attack.hpp:55
int drain_constant
Base HP drained regardless of damage dealt.
Definition: attack.hpp:77
unsigned int chance_to_hit
Effective chance to hit as a percentage (all factors accounted for).
Definition: attack.hpp:73
A single unit type that the player may recruit.
Definition: types.hpp:42
void attack_unit(const map_location &attacker, const map_location &defender, int attack_with, int defend_with, bool update_display=true)
Performs an attack.
Definition: attack.cpp:1537
void attack_unit_and_advance(const map_location &attacker, const map_location &defender, int attack_with, int defend_with, bool update_display=true, const ai::unit_advancements_aspect &ai_advancement=ai::unit_advancements_aspect())
Performs an attack, and advanced the units afterwards.
Definition: attack.cpp:1547
bool poisons
Attack poisons opponent when it hits.
Definition: attack.hpp:59
Object which defines a time of day with associated bonuses, image, sounds etc.
Definition: time_of_day.hpp:57
bool backstab_pos
True if the attacker is in position to backstab the defender (this is used to determine whether to ap...
Definition: attack.hpp:60
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:44
int damage
Effective damage of the weapon (all factors accounted for).
Definition: attack.hpp:74
unsigned int level
Definition: attack.hpp:68
unsigned int rounds
Berserk special can force us to fight more than one round.
Definition: attack.hpp:70
unsigned int swarm_min
Minimum number of blows with swarm (equal to num_blows if swarm isn&#39;t used).
Definition: attack.hpp:79
std::unique_ptr< battle_context_unit_stats > defender_stats_
Definition: attack.hpp:253
bool plagues
Attack turns opponent into a zombie when fatal.
Definition: attack.hpp:58
Encapsulates the map of the game.
Definition: map.hpp:36
Computes the statistics of a battle between an attacker and a defender unit.
Definition: attack.hpp:171
int attack_num
Index into unit->attacks() or -1 for none.
Definition: attack.hpp:51
Structure describing the statistics of a unit involved in the battle.
Definition: attack.hpp:48
std::unique_ptr< battle_context_unit_stats > attacker_stats_
Statistics of the units.
Definition: attack.hpp:252
bool backstab_check(const map_location &attacker_loc, const map_location &defender_loc, const unit_map &units, const std::vector< team > &teams)
Function to check if an attack will satisfy the requirements for backstab.
Definition: attack.cpp:1773
Encapsulates the map of the game.
Definition: location.hpp:42
int round_damage(int base_damage, int bonus, int divisor)
round (base_damage * bonus / divisor) to the closest integer, but up or down towards base_damage ...
Definition: math.hpp:59
unsigned swarm_blows(unsigned min_blows, unsigned max_blows, unsigned hp, unsigned max_hp)
Calculates the number of blows resulting from swarm.
Definition: attack.hpp:38
All combat-related info.
int combat_modifier(const unit_map &units, const gamemap &map, const map_location &loc, unit_type::ALIGNMENT alignment, bool is_fearless)
Returns the amount that a unit&#39;s damage should be multiplied by due to the current time of day...
Definition: attack.cpp:1726
bool swarm
Attack has swarm special.
Definition: attack.hpp:64
int slow_damage
Effective damage if unit becomes slowed (== damage, if already slowed)
Definition: attack.hpp:75
std::unique_ptr< combatant > defender_combatant_
Definition: attack.hpp:257
int generic_combat_modifier(int lawful_bonus, unit_type::ALIGNMENT alignment, bool is_fearless)
Returns the amount that a unit&#39;s damage should be multiplied by due to a given lawful_bonus.
Definition: attack.cpp:1745
unsigned int experience
Definition: attack.hpp:67
bool disable
Attack has disable special.
Definition: attack.hpp:66
bool firststrike
Attack has firststrike special.
Definition: attack.hpp:65
bool is_poisoned
True if the unit is poisoned at the beginning of the battle.
Definition: attack.hpp:53
std::pair< int, bool > ability_leadership(const std::string &ability, const unit_map &units, const map_location &loc, const map_location &opp_loc, bool attacker=true, int abil_value=0, bool backstab_pos=false, const_attack_ptr weapon=nullptr, const_attack_ptr opp_weapon=nullptr)
Definition: attack.cpp:1636
int drain_percent
Percentage of damage recovered as health.
Definition: attack.hpp:76
int under_leadership(const unit &u, const map_location &loc, const_attack_ptr weapon, const_attack_ptr opp_weapon=nullptr)
Tests if the unit at loc is currently affected by leadership.
Definition: attack.cpp:1567
battle_context_unit_stats(const unit &u, const map_location &u_loc, int u_attack_num, bool attacking, const unit &opp, const map_location &opp_loc, const_attack_ptr opp_weapon, const unit_map &units)
Definition: attack.cpp:72
Container associating units to locations.
Definition: map.hpp:99
unsigned int num_blows
Effective number of blows, takes swarm into account.
Definition: attack.hpp:78
unsigned int max_hp
Maximum hitpoints of the unit.
Definition: attack.hpp:72
bool is_attacker
True if the unit is the attacker.
Definition: attack.hpp:52
unsigned int max_experience
Definition: attack.hpp:67
bool petrifies
Attack petrifies opponent when it hits.
Definition: attack.hpp:57
std::shared_ptr< const attack_type > const_attack_ptr
Definition: ptr.hpp:37
bool bool_leadership(const std::string &ability, const unit_map &units, const map_location &loc, const map_location &opp_loc, bool attacker=true, const_attack_ptr weapon=nullptr, const_attack_ptr opp_weapon=nullptr)
Definition: attack.cpp:1671
bool leadership_affects_opponent(const std::string &ability, const unit_map &units, const map_location &loc, const_attack_ptr weapon=nullptr, const_attack_ptr opp_weapon=nullptr)
Definition: attack.cpp:1617
bool drains
Attack drains opponent when it hits.
Definition: attack.hpp:56
const battle_context_unit_stats & get_attacker_stats() const
This method returns the statistics of the attacker.
Definition: attack.hpp:198
unsigned int swarm_max
Maximum number of blows with swarm (equal to num_blows if swarm isn&#39;t used).
Definition: attack.hpp:80
bool leadership_affects_self(const std::string &ability, const unit_map &units, const map_location &loc, const_attack_ptr weapon=nullptr, const_attack_ptr opp_weapon=nullptr)
Definition: attack.cpp:1599