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