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