The Battle for Wesnoth  1.19.18+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. */
69  unsigned int experience, max_experience;
70  unsigned int level;
71 
72  unsigned int rounds; /**< Berserk special can force us to fight more than one round. */
73  unsigned int hp; /**< Hitpoints of the unit at the beginning of the battle. */
74  unsigned int max_hp; /**< Maximum hitpoints of the unit. */
75  unsigned int chance_to_hit; /**< Effective chance to hit as a percentage (all factors accounted for). */
76  int damage; /**< Effective damage of the weapon (all factors accounted for). */
77  int slow_damage; /**< Effective damage if unit becomes slowed (== damage, if already slowed) */
78  int drain_percent; /**< Percentage of damage recovered as health */
79  int drain_constant; /**< Base HP drained regardless of damage dealt */
80  unsigned int num_blows; /**< Effective number of blows, takes swarm into account. */
81  unsigned int swarm_min; /**< Minimum number of blows with swarm (equal to num_blows if swarm isn't used). */
82  unsigned int swarm_max; /**< Maximum number of blows with swarm (equal to num_blows if swarm isn't used). */
83 
84  std::string plague_type; /**< The plague type used by the attack, if any. */
85 
87  const map_location& u_loc,
88  int u_attack_num,
89  bool attacking,
91  const map_location& opp_loc,
92  const const_attack_ptr& opp_weapon,
93  utils::optional<int> opp_terrain_defense = {},
94  utils::optional<int> lawful_bonus = {}
95  );
96 
98  {
99  }
100 
101  /** Calculates the number of blows we would have if we had @a new_hp instead of the recorded hp. */
102  unsigned int calc_blows(unsigned new_hp) const
103  {
104  return swarm_blows(swarm_min, swarm_max, new_hp, max_hp);
105  }
106 
107  /**
108  * Special constructor for the stand-alone version of attack_prediction.cpp and the statistis dialog.
109  * (This hardcodes some standard abilities for testing purposes.)
110  */
112  int blows,
113  int hitpoints,
114  int maximum_hp,
115  int hit_chance,
116  bool drain = false,
117  bool slows = false,
118  bool slowed = false,
119  bool berserk = false,
120  bool first = false,
121  bool do_swarm = false)
122  : weapon(nullptr) // Not used in attack prediction.
123  , attack_num(0) // Not used in attack prediction.
124  , is_attacker(true) // Not used in attack prediction.
125  , is_poisoned(false)
126  , is_slowed(slowed)
127  , slows(slows)
128  , drains(drain)
129  , petrifies(false)
130  , plagues(false)
131  , poisons(false)
132  , swarm(do_swarm)
133  , firststrike(first)
134  , disable(false)
135  , leadership_bonus(0)
136  , experience(0) // No units should advance in the attack prediction tests.
137  , max_experience(1000000) // No units should advance in the attack prediction tests.
138  , level(1) // No units should advance in the attack prediction tests.
139  , rounds(berserk ? 30 : 1)
140  , hp(std::max<int>(0, hitpoints))
141  , max_hp(std::max<int>(1, maximum_hp))
142  , chance_to_hit(hit_chance)
143  , damage(std::max(0, dmg))
145  , drain_percent(drain ? 50 : 0)
146  , drain_constant(0)
147  , num_blows(do_swarm ? blows * hp / max_hp : blows)
148  , swarm_min(do_swarm ? 0 : blows)
149  , swarm_max(blows)
150  , plague_type()
151  {
152  if(slowed) {
154  }
155 
156  if(hp > max_hp) {
157  hp = max_hp; // Keeps the prob_matrix from going out of bounds.
158  }
159  }
160 };
161 
162 /** Computes the statistics of a battle between an attacker and a defender unit. */
164 {
165 public:
166  /**
167  * If no attacker_weapon is given, we select the best one,
168  * based on harm_weight (1.0 means 1 hp lost counters 1 hp damage,
169  * 0.0 means we ignore harm weight).
170  * prev_def is for predicting multiple attacks against a defender.
171  */
172  battle_context(const unit_map& units,
173  const map_location& attacker_loc,
174  const map_location& defender_loc,
175  int attacker_weapon = -1,
176  int defender_weapon = -1,
177  double aggression = 0.0,
178  const combatant* prev_def = nullptr,
179  unit_const_ptr attacker_ptr = unit_const_ptr(),
180  unit_const_ptr defender_ptr = unit_const_ptr());
181 
182  /** Used by the AI which caches battle_context_unit_stats */
184 
185  battle_context(battle_context&& other) = default;
186 
188 
189  /** This method returns the statistics of the attacker. */
191  {
192  return *attacker_stats_;
193  }
194 
195  /** This method returns the statistics of the defender. */
197  {
198  return *defender_stats_;
199  }
200 
201  /** Get the simulation results. */
202  const combatant& get_attacker_combatant(const combatant* prev_def = nullptr);
203  const combatant& get_defender_combatant(const combatant* prev_def = nullptr);
204 
205  /** Given this harm_weight, is this attack better than that? */
206  bool better_attack(class battle_context& that, double harm_weight);
207  /** Given this harm_weight, is this attack better than that? */
208  bool better_defense(class battle_context& that, double harm_weight);
209 
210  static bool better_combat(const combatant& us_a,
211  const combatant& them_a,
212  const combatant& us_b,
213  const combatant& them_b,
214  double harm_weight);
215 
216  void simulate(const combatant* prev_def);
217 private:
219  nonempty_unit_const_ptr attacker,
220  const map_location& attacker_loc,
221  int attacker_weapon,
222  nonempty_unit_const_ptr defender,
223  const map_location& defender_loc,
224  int defender_weapon);
225 
227  const nonempty_unit_const_ptr& defender,
228  const map_location& attacker_loc,
229  const map_location& defender_loc,
230  double harm_weight,
231  const combatant* prev_def);
232 
234  nonempty_unit_const_ptr defender,
235  unsigned attacker_weapon,
236  const map_location& attacker_loc,
237  const map_location& defender_loc,
238  const combatant* prev_def);
239 
240  /** Statistics of the units. */
241  std::unique_ptr<battle_context_unit_stats> attacker_stats_;
242  std::unique_ptr<battle_context_unit_stats> defender_stats_;
243 
244  /** Outcome of simulated fight. */
245  std::unique_ptr<combatant> attacker_combatant_;
246  std::unique_ptr<combatant> defender_combatant_;
247 };
248 
249 /** Performs an attack. */
250 void attack_unit(const map_location& attacker,
251  const map_location& defender,
252  int attack_with,
253  int defend_with,
254  bool update_display = true);
255 
256 /** Performs an attack, and advanced the units afterwards */
257 void attack_unit_and_advance(const map_location& attacker,
258  const map_location& defender,
259  int attack_with,
260  int defend_with,
261  bool update_display = true);
262 
263 /**
264  * Tests if the unit at loc is currently affected by leadership.
265  * (i.e. has a higher-level unit with the 'leadership' ability next to it).
266  *
267  * Returns the bonus percentage (possibly 0 if there's no leader adjacent).
268  */
269 int under_leadership(const unit &u, const map_location& loc, const_attack_ptr weapon = nullptr, const_attack_ptr opp_weapon = nullptr);
270 
271 /**
272  * Returns the amount that a unit's damage should be multiplied by
273  * due to the current time of day.
274  */
275 int combat_modifier(const unit_map& units,
276  const gamemap& map,
277  const map_location& loc,
278  unit_alignments::type alignment,
279  bool is_fearless);
280 
281 /**
282  * Returns the amount that a unit's damage should be multiplied by
283  * due to the current time of day.
284  */
285 int combat_modifier(const time_of_day& effective_tod,
286  unit_alignments::type alignment,
287  bool is_fearless);
288 
289 /**
290  * Returns the amount that a unit's damage should be multiplied by
291  * due to a given lawful_bonus.
292  */
293 int generic_combat_modifier(int lawful_bonus, unit_alignments::type alignment, bool is_fearless, int max_liminal_bonus);
294 /**
295  * Function to check if an attack will satisfy the requirements for backstab.
296  * Input:
297  * - the location from which the attack will occur,
298  * - the defending unit location,
299  * - the list of units on the map and
300  * - the list of teams.
301  * The defender and opposite units should be in place already.
302  * The attacking unit doesn't need to be, but if it isn't,
303  * an external check should be made to make sure the opposite unit
304  * isn't also the attacker.
305  */
306 bool backstab_check(const map_location& attacker_loc,
307  const map_location& defender_loc,
308  const unit_map& units,
309  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:1462
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:1518
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:1546
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:1498
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:1491
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:1472
map_location loc
Definition: move.cpp:172
Computes the statistics of a battle between an attacker and a defender unit.
Definition: attack.hpp:164
std::unique_ptr< battle_context_unit_stats > defender_stats_
Definition: attack.hpp:242
std::unique_ptr< combatant > defender_combatant_
Definition: attack.hpp:246
std::unique_ptr< battle_context_unit_stats > attacker_stats_
Statistics of the units.
Definition: attack.hpp:241
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:419
std::unique_ptr< combatant > attacker_combatant_
Outcome of simulated fight.
Definition: attack.hpp:245
void simulate(const combatant *prev_def)
Definition: attack.cpp:265
const battle_context_unit_stats & get_defender_stats() const
This method returns the statistics of the defender.
Definition: attack.hpp:196
const combatant & get_attacker_combatant(const combatant *prev_def=nullptr)
Get the simulation results.
Definition: attack.cpp:333
const battle_context_unit_stats & get_attacker_stats() const
This method returns the statistics of the attacker.
Definition: attack.hpp:190
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:340
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:372
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:472
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:278
bool better_defense(class battle_context &that, double harm_weight)
Given this harm_weight, is this attack better than that?
Definition: attack.cpp:360
bool better_attack(class battle_context &that, double harm_weight)
Given this harm_weight, is this attack better than that?
Definition: attack.cpp:348
Encapsulates the map of the game.
Definition: map.hpp:176
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:39
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:80
std::string plague_type
The plague type used by the attack, if any.
Definition: attack.hpp:84
bool petrifies
Attack petrifies opponent when it hits.
Definition: attack.hpp:62
int drain_percent
Percentage of damage recovered as health.
Definition: attack.hpp:78
unsigned int hp
Hitpoints of the unit at the beginning of the battle.
Definition: attack.hpp:73
unsigned int level
Definition: attack.hpp:70
int slow_damage
Effective damage if unit becomes slowed (== damage, if already slowed)
Definition: attack.hpp:77
unsigned int max_experience
Definition: attack.hpp:69
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:81
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:111
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:72
unsigned int swarm_max
Maximum number of blows with swarm (equal to num_blows if swarm isn't used).
Definition: attack.hpp:82
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:102
unsigned int max_hp
Maximum hitpoints of the unit.
Definition: attack.hpp:74
int damage
Effective damage of the weapon (all factors accounted for).
Definition: attack.hpp:76
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:75
int drain_constant
Base HP drained regardless of damage dealt.
Definition: attack.hpp:79
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:69
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