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 http://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 class unit;
33 class unit_map;
34 class gamemap;
35 
36 /** Calculates the number of blows resulting from swarm. */
37 inline unsigned swarm_blows(unsigned min_blows, unsigned max_blows, unsigned hp, unsigned max_hp)
38 {
39  return hp >= max_hp
40  ? max_blows
41  : max_blows < min_blows
42  ? min_blows - (min_blows - max_blows) * hp / max_hp
43  : min_blows + (max_blows - min_blows) * hp / max_hp;
44 }
45 
46 /** Structure describing the statistics of a unit involved in the battle. */
48 {
49  const_attack_ptr weapon; /**< The weapon used by the unit to attack the opponent, or nullptr if there is none. */
50  int attack_num; /**< Index into unit->attacks() or -1 for none. */
51  bool is_attacker; /**< True if the unit is the attacker. */
52  bool is_poisoned; /**< True if the unit is poisoned at the beginning of the battle. */
53  bool is_slowed; /**< True if the unit is slowed at the beginning of the battle. */
54  bool slows; /**< Attack slows opponent when it hits. */
55  bool drains; /**< Attack drains opponent when it hits. */
56  bool petrifies; /**< Attack petrifies opponent when it hits. */
57  bool plagues; /**< Attack turns opponent into a zombie when fatal. */
58  bool poisons; /**< Attack poisons opponent when it hits. */
59  bool backstab_pos; /**<
60  * True if the attacker is in *position* to backstab the defender (this is used to
61  * determine whether to apply the backstab bonus in case the attacker has backstab).
62  */
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,
87  const unit& opp,
88  const map_location& opp_loc,
89  const_attack_ptr opp_weapon,
90  const unit_map& units);
91 
92  /** Used by AI for combat analysis */
94  const_attack_ptr att_weapon,
95  bool attacking,
96  const unit_type* opp_type,
97  const_attack_ptr opp_weapon,
98  unsigned int opp_terrain_defense,
99  int lawful_bonus = 0);
100 
102  {
103  }
104 
105  /// Calculates the number of blows we would have if we had @a new_hp
106  // instead of the recorded hp.
107  unsigned int calc_blows(unsigned new_hp) const
108  {
109  return swarm_blows(swarm_min, swarm_max, new_hp, max_hp);
110  }
111 
112 #if defined(BENCHMARK) || defined(CHECK)
113  /**
114  * Special constructor for the stand-alone version of attack_prediction.cpp.
115  * (This hardcodes some standard abilities for testing purposes.)
116  */
118  int blows,
119  int hitpoints,
120  int maximum_hp,
121  int hit_chance,
122  bool drain,
123  bool slows,
124  bool slowed,
125  bool berserk,
126  bool first,
127  bool do_swarm)
128  : weapon(nullptr) // Not used in attack prediction.
129  , attack_num(0) // Not used in attack prediction.
130  , is_attacker(true) // Not used in attack prediction.
131  , is_poisoned(false)
132  , is_slowed(slowed)
133  , slows(slows)
134  , drains(drain)
135  , petrifies(false)
136  , plagues(false)
137  , poisons(false)
138  , backstab_pos(false)
139  , swarm(do_swarm)
140  , firststrike(first)
141  , disable(false)
142  , experience(0) // No units should advance in the attack prediction tests.
143  , max_experience(50) // No units should advance in the attack prediction tests.
144  , level(1) // No units should advance in the attack prediction tests.
145  , rounds(berserk ? 30 : 1)
146  , hp(std::max<int>(0, hitpoints))
147  , max_hp(std::max<int>(1, maximum_hp))
148  , chance_to_hit(hit_chance)
149  , damage(std::max(0, dmg))
150  , slow_damage(round_damage(damage, 1, 2))
151  , drain_percent(drain ? 50 : 0)
152  , drain_constant(0)
153  , num_blows(do_swarm ? blows * hp / max_hp : blows)
154  , swarm_min(do_swarm ? 0 : blows)
155  , swarm_max(blows)
156  , plague_type()
157  {
158  if(slowed) {
159  damage = slow_damage;
160  }
161 
162  if(hp > max_hp) {
163  hp = max_hp; // Keeps the prob_matrix from going out of bounds.
164  }
165  }
166 #endif
167 };
168 
169 /** Computes the statistics of a battle between an attacker and a defender unit. */
171 {
172 public:
173  /**
174  * If no attacker_weapon is given, we select the best one,
175  * based on harm_weight (1.0 means 1 hp lost counters 1 hp damage,
176  * 0.0 means we ignore harm weight).
177  * prev_def is for predicting multiple attacks against a defender.
178  */
179  battle_context(const unit_map& units,
180  const map_location& attacker_loc,
181  const map_location& defender_loc,
182  int attacker_weapon = -1,
183  int defender_weapon = -1,
184  double aggression = 0.0,
185  const combatant* prev_def = nullptr,
186  const unit* attacker_ptr = nullptr);
187 
188  /** Used by the AI which caches battle_context_unit_stats */
190 
191  battle_context(const battle_context& other);
192  battle_context(battle_context&& other) = default;
193 
194  battle_context& operator=(const battle_context& other);
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 
216  static bool better_combat(const combatant& us_a,
217  const combatant& them_a,
218  const combatant& us_b,
219  const combatant& them_b,
220  double harm_weight);
221 
222 private:
223  int choose_attacker_weapon(const unit& attacker,
224  const unit& defender,
225  const unit_map& units,
226  const map_location& attacker_loc,
227  const map_location& defender_loc,
228  double harm_weight,
229  int* defender_weapon,
230  const combatant* prev_def);
231 
232  int choose_defender_weapon(const unit& attacker,
233  const unit& defender,
234  unsigned attacker_weapon,
235  const unit_map& units,
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,
263 
264 /**
265  * Tests if the unit at loc is currently affected by leadership.
266  * (i.e. has a higher-level unit with the 'leadership' ability next to it).
267  *
268  * Returns a pair of bonus percentage and the leader's location if the unit is affected,
269  * or 0 and map_location::null_location() otherwise.
270  */
271 std::pair<int, map_location> under_leadership(const unit_map& units, const map_location& loc, const_attack_ptr weapon, const_attack_ptr opp_weapon = nullptr);
272 
273 /**
274  * Returns the amount that a unit's damage should be multiplied by
275  * due to the current time of day.
276  */
277 int combat_modifier(const unit_map& units,
278  const gamemap& map,
279  const map_location& loc,
280  unit_type::ALIGNMENT alignment,
281  bool is_fearless);
282 
283 /**
284  * Returns the amount that a unit's damage should be multiplied by
285  * due to a given lawful_bonus.
286  */
287 int generic_combat_modifier(int lawful_bonus, unit_type::ALIGNMENT alignment, bool is_fearless);
288 /**
289  * Function to check if an attack will satisfy the requirements for backstab.
290  * Input:
291  * - the location from which the attack will occur,
292  * - the defending unit location,
293  * - the list of units on the map and
294  * - the list of teams.
295  * The defender and opposite units should be in place already.
296  * The attacking unit doesn't need to be, but if it isn't,
297  * an external check should be made to make sure the opposite unit
298  * isn't also the attacker.
299  */
300 bool backstab_check(const map_location& attacker_loc,
301  const map_location& defender_loc,
302  const unit_map& units,
303  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:49
std::unique_ptr< combatant > attacker_combatant_
Outcome of simulated fight.
Definition: attack.hpp:245
std::pair< int, map_location > under_leadership(const unit_map &units, 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:1630
unsigned int calc_blows(unsigned new_hp) const
Calculates the number of blows we would have if we had new_hp.
Definition: attack.hpp:107
std::string plague_type
The plague type used by the attack, if any.
Definition: attack.hpp:81
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:70
bool is_slowed
True if the unit is slowed at the beginning of the battle.
Definition: attack.hpp:53
bool slows
Attack slows opponent when it hits.
Definition: attack.hpp:54
int drain_constant
Base HP drained regardless of damage dealt.
Definition: attack.hpp:76
unsigned int chance_to_hit
Effective chance to hit as a percentage (all factors accounted for).
Definition: attack.hpp:72
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:1600
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:1610
bool poisons
Attack poisons opponent when it hits.
Definition: attack.hpp:58
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:59
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:73
unsigned int level
Definition: attack.hpp:67
unsigned int rounds
Berserk special can force us to fight more than one round.
Definition: attack.hpp:69
unsigned int swarm_min
Minimum number of blows with swarm (equal to num_blows if swarm isn&#39;t used).
Definition: attack.hpp:78
std::unique_ptr< battle_context_unit_stats > defender_stats_
Definition: attack.hpp:242
bool plagues
Attack turns opponent into a zombie when fatal.
Definition: attack.hpp:57
Encapsulates the map of the game.
Definition: map.hpp:34
Computes the statistics of a battle between an attacker and a defender unit.
Definition: attack.hpp:170
int attack_num
Index into unit->attacks() or -1 for none.
Definition: attack.hpp:50
Structure describing the statistics of a unit involved in the battle.
Definition: attack.hpp:47
std::unique_ptr< battle_context_unit_stats > attacker_stats_
Statistics of the units.
Definition: attack.hpp:241
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:1680
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:37
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:1641
bool swarm
Attack has swarm special.
Definition: attack.hpp:63
int slow_damage
Effective damage if unit becomes slowed (== damage, if already slowed)
Definition: attack.hpp:74
std::unique_ptr< combatant > defender_combatant_
Definition: attack.hpp:246
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:1652
unsigned int experience
Definition: attack.hpp:66
bool disable
Attack has disable special.
Definition: attack.hpp:65
bool firststrike
Attack has firststrike special.
Definition: attack.hpp:64
bool is_poisoned
True if the unit is poisoned at the beginning of the battle.
Definition: attack.hpp:52
int drain_percent
Percentage of damage recovered as health.
Definition: attack.hpp:75
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:65
Container associating units to locations.
Definition: map.hpp:99
unsigned int num_blows
Effective number of blows, takes swarm into account.
Definition: attack.hpp:77
unsigned int max_hp
Maximum hitpoints of the unit.
Definition: attack.hpp:71
bool is_attacker
True if the unit is the attacker.
Definition: attack.hpp:51
unsigned int max_experience
Definition: attack.hpp:66
bool petrifies
Attack petrifies opponent when it hits.
Definition: attack.hpp:56
std::shared_ptr< const attack_type > const_attack_ptr
Definition: ptr.hpp:37
bool drains
Attack drains opponent when it hits.
Definition: attack.hpp:55
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:79