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