The Battle for Wesnoth  1.19.0-dev
attack_type.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 #pragma once
17 
18 #include "map/location.hpp"
19 #include "tstring.hpp"
20 #include "config.hpp"
21 #include <string>
22 #include <vector>
23 
24 #include <boost/iterator/indirect_iterator.hpp>
25 #include <boost/dynamic_bitset_fwd.hpp>
26 
27 #include "units/ptr.hpp" // for attack_ptr
28 
29 class unit_ability_list;
30 class unit_type;
31 namespace wfl {
32  class map_formula_callable;
33 }
34 //the 'attack type' is the type of attack, how many times it strikes,
35 //and how much damage it does.
36 class attack_type : public std::enable_shared_from_this<attack_type>
37 {
38 public:
39 
40  explicit attack_type(const config& cfg);
41  const t_string& name() const { return description_; }
42  const std::string& id() const { return id_; }
43  const std::string& type() const { return type_; }
44  const std::string& icon() const { return icon_; }
45  const std::string& range() const { return range_; }
46  int min_range() const { return min_range_; }
47  int max_range() const { return max_range_; }
48  std::string accuracy_parry_description() const;
49  int accuracy() const { return accuracy_; }
50  int parry() const { return parry_; }
51  int damage() const { return damage_; }
52  int num_attacks() const { return num_attacks_; }
53  double attack_weight() const { return attack_weight_; }
54  double defense_weight() const { return defense_weight_; }
55  const config &specials() const { return specials_; }
56 
57  void set_name(const t_string& value) { description_ = value; set_changed(true); }
58  void set_id(const std::string& value) { id_ = value; set_changed(true); }
59  void set_type(const std::string& value) { type_ = value; set_changed(true); }
60  void set_icon(const std::string& value) { icon_ = value; set_changed(true); }
61  void set_range(const std::string& value) { range_ = value; set_changed(true); }
62  void set_accuracy(int value) { accuracy_ = value; set_changed(true); }
63  void set_parry(int value) { parry_ = value; set_changed(true); }
64  void set_damage(int value) { damage_ = value; set_changed(true); }
65  void set_num_attacks(int value) { num_attacks_ = value; set_changed(true); }
66  void set_attack_weight(double value) { attack_weight_ = value; set_changed(true); }
67  void set_defense_weight(double value) { defense_weight_ = value; set_changed(true); }
68  void set_specials(config value) { specials_ = value; set_changed(true); }
69 
70 
71  // In unit_abilities.cpp:
72 
73  /**
74  * @return True iff the special @a special is active.
75  * @param special The special being checked.
76  * @param simple_check If true, check whether the unit has the special. Else, check whether the special is currently active.
77  * @param special_id If true, match @a special against the @c id of special tags.
78  * @param special_tags If true, match @a special against the tag name of special tags.
79  */
80  bool has_special(const std::string& special, bool simple_check=false, bool special_id=true, bool special_tags=true) const;
81  unit_ability_list get_specials(const std::string& special) const;
82  std::vector<std::pair<t_string, t_string>> special_tooltips(boost::dynamic_bitset<>* active_list = nullptr) const;
83  std::string weapon_specials() const;
84  std::string weapon_specials_value(const std::set<std::string> checking_tags) const;
85 
86  /** Calculates the number of attacks this weapon has, considering specials. */
87  void modified_attacks(unsigned & min_attacks,
88  unsigned & max_attacks) const;
89 
90  /**
91  * Select best damage type based on frequency count for replacement_type and based on highest damage for alternative_type.
92  *
93  * @param damage_type_list list of [damage_type] to check.
94  * @param key_name name of attribute checked 'alternative_type' or 'replacement_type'.
95  * @param resistance_list list of "resistance" abilities to check for each type of damage checked.
96  */
97  std::string select_damage_type(const unit_ability_list& damage_type_list, const std::string& key_name, unit_ability_list resistance_list) const;
98  /** return a modified damage type and/or add a secondary_type for hybrid use if special is active. */
99  std::pair<std::string, std::string> damage_type() const;
100  /** @return A list of alternative_type damage types. */
101  std::set<std::string> alternative_damage_types() const;
102 
103  /** Returns the damage per attack of this weapon, considering specials. */
104  int modified_damage() const;
105 
106  /** Return the special weapon value, considering specials.
107  * @param abil_list The list of special checked.
108  * @param base_value The value modified or not by function.
109  */
110  int composite_value(const unit_ability_list& abil_list, int base_value) const;
111  /** Returns list for weapon like abilities for each ability type. */
112  unit_ability_list get_weapon_ability(const std::string& ability) const;
113  /**
114  * @param special the tag name to check for
115  * @return list which contains get_weapon_ability and get_specials list for each ability type, with overwritten items removed
116  */
117  unit_ability_list get_specials_and_abilities(const std::string& special) const;
118  /** used for abilities used like weapon
119  * @return True if the ability @a special is active.
120  * @param special The special being checked.
121  * @param special_id If true, match @a special against the @c id of special tags.
122  * @param special_tags If true, match @a special against the tag name of special tags.
123  */
124  bool has_weapon_ability(const std::string& special, bool special_id=true, bool special_tags=true) const;
125  /** used for abilities used like weapon and true specials
126  * @return True if the ability @a special is active.
127  * @param special The special being checked.
128  * @param special_id If true, match @a special against the @c id of special tags.
129  * @param special_tags If true, match @a special against the tag name of special tags.
130  */
131  bool has_special_or_ability(const std::string& special, bool special_id=true, bool special_tags=true) const;
132 
133  // In unit_types.cpp:
134 
135  bool matches_filter(const config& filter, const std::string& tag_name = "") const;
136  bool apply_modification(const config& cfg);
137  bool describe_modification(const config& cfg,std::string* description);
138 
139  int movement_used() const { return movement_used_; }
140  void set_movement_used(int value) { movement_used_ = value; }
141  int attacks_used() const { return attacks_used_; }
142  void set_attacks_used(int value) { attacks_used_ = value; }
143 
144  void write(config& cfg) const;
145  inline config to_config() const { config c; write(c); return c; }
146 
148 private:
149  // In unit_abilities.cpp:
150 
151  // Configured as a bit field, in case that is useful.
153  /**
154  * Filter a list of abilities or weapon specials, removing any entries that don't own
155  * the overwrite_specials attributes.
156  *
157  * @param overwriters list that may have overwrite_specials attributes.
158  * @param tag_name type of abilitie/special checked.
159  */
160  unit_ability_list overwrite_special_overwriter(unit_ability_list overwriters, const std::string& tag_name) const;
161  /**
162  * Check whether @a cfg would be overwritten by any element of @a overwriters.
163  *
164  * @return True if element checked is overwritable.
165  * @param overwriters list used for check if element is overwritable.
166  * @param cfg element checked.
167  * @param tag_name type of abilitie/special checked.
168  */
169  bool overwrite_special_checking(unit_ability_list& overwriters, const config& cfg, const std::string& tag_name) const;
170  /** check_self_abilities : return an boolean value for checking of activities of abilities used like weapon
171  * @return True if the special @a special is active.
172  * @param cfg the config to one special ability checked.
173  * @param special The special ability type who is being checked.
174  */
175  bool check_self_abilities(const config& cfg, const std::string& special) const;
176  /** check_adj_abilities : return an boolean value for checking of activities of abilities used like weapon
177  * @return True if the special @a special is active.
178  * @param cfg the config to one special ability checked.
179  * @param special The special ability type who is being checked.
180  * @param dir direction to research a unit adjacent to self_.
181  * @param from unit adjacent to self_ is checked.
182  */
183  bool check_adj_abilities(const config& cfg, const std::string& special, int dir, const unit& from) const;
184  bool special_active(const config& special, AFFECTS whom, const std::string& tag_name,
185  const std::string& filter_self ="filter_self") const;
186 
187 /** weapon_specials_impl_self and weapon_specials_impl_adj : check if special name can be added.
188  * @param[in,out] temp_string the string modified and returned
189  * @param[in] self the unit checked.
190  * @param[in] self_attack the attack used by unit checked in this function.
191  * @param[in] other_attack the attack used by opponent to unit checked.
192  * @param[in] self_loc location of the unit checked.
193  * @param[in] whom determine if unit affected or not by special ability.
194  * @param[in,out] checking_name the reference for checking if a name is already added
195  * @param[in] checking_tags the reference for checking if special ability type can be used
196  * @param[in] leader_bool If true, [leadership] abilities are checked.
197  */
198  static void weapon_specials_impl_self(
199  std::string& temp_string,
200  unit_const_ptr self,
201  const_attack_ptr self_attack,
202  const_attack_ptr other_attack,
203  const map_location& self_loc,
204  AFFECTS whom,
205  std::set<std::string>& checking_name,
206  const std::set<std::string>& checking_tags={},
207  bool leader_bool=false
208  );
209 
210  static void weapon_specials_impl_adj(
211  std::string& temp_string,
212  unit_const_ptr self,
213  const_attack_ptr self_attack,
214  const_attack_ptr other_attack,
215  const map_location& self_loc,
216  AFFECTS whom,
217  std::set<std::string>& checking_name,
218  const std::set<std::string>& checking_tags={},
219  const std::string& affect_adjacents="",
220  bool leader_bool=false
221  );
222  /** check_self_abilities_impl : return an boolean value for checking of activities of abilities used like weapon
223  * @return True if the special @a tag_name is active.
224  * @param self_attack the attack used by unit checked in this function.
225  * @param other_attack the attack used by opponent to unit checked.
226  * @param special the config to one special ability checked.
227  * @param u the unit checked.
228  * @param loc location of the unit checked.
229  * @param whom determine if unit affected or not by special ability.
230  * @param tag_name The special ability type who is being checked.
231  * @param leader_bool If true, [leadership] abilities are checked.
232  */
233  static bool check_self_abilities_impl(
234  const_attack_ptr self_attack,
235  const_attack_ptr other_attack,
236  const config& special,
237  unit_const_ptr u,
238  const map_location& loc,
239  AFFECTS whom,
240  const std::string& tag_name,
241  bool leader_bool=false
242  );
243 
244 
245  /** check_adj_abilities_impl : return an boolean value for checking of activities of abilities used like weapon in unit adjacent to fighter
246  * @return True if the special @a tag_name is active.
247  * @param self_attack the attack used by unit who fight.
248  * @param other_attack the attack used by opponent.
249  * @param special the config to one special ability checked.
250  * @param u the unit who is or not affected by an abilities owned by @a from.
251  * @param from unit adjacent to @a u is checked.
252  * @param dir direction to research a unit adjacent to @a u.
253  * @param loc location of the unit checked.
254  * @param whom determine if unit affected or not by special ability.
255  * @param tag_name The special ability type who is being checked.
256  * @param leader_bool If true, [leadership] abilities are checked.
257  */
258  static bool check_adj_abilities_impl(
259  const_attack_ptr self_attack,
260  const_attack_ptr other_attack,
261  const config& special,
262  unit_const_ptr u,
263  const unit& from,
264  int dir,
265  const map_location& loc,
266  AFFECTS whom,
267  const std::string& tag_name,
268  bool leader_bool=false
269  );
270 
271  static bool special_active_impl(
272  const_attack_ptr self_attack,
273  const_attack_ptr other_attack,
274  const config& special,
275  AFFECTS whom,
276  const std::string& tag_name,
277  const std::string& filter_self ="filter_self"
278  );
279 
280  // Used via specials_context() to control which specials are
281  // considered active.
282  friend class specials_context_t;
286  mutable bool is_attacker_;
288  mutable bool is_for_listing_ = false;
289 public:
291  std::shared_ptr<const attack_type> parent;
292  friend class attack_type;
293  /** Initialize weapon specials context for listing */
294  explicit specials_context_t(const attack_type& weapon, bool attacking);
295  /** Initialize weapon specials context for a unit type */
296  specials_context_t(const attack_type& weapon, const unit_type& self_type, const map_location& loc, bool attacking = true);
297  /** Initialize weapon specials context for a single unit */
298  specials_context_t(const attack_type& weapon, const_attack_ptr other_weapon,
299  unit_const_ptr self, unit_const_ptr other,
300  const map_location& self_loc, const map_location& other_loc,
301  bool attacking);
302  /** Initialize weapon specials context for a pair of units */
303  specials_context_t(const attack_type& weapon, unit_const_ptr self, const map_location& loc, bool attacking);
305  bool was_moved = false;
306  public:
307  // Destructor at least needs to be public for all this to work.
310  };
311  // Set up a specials context.
312  // Usage: auto ctx = weapon.specials_context(...);
314  const map_location& unit_loc, const map_location& other_loc,
315  bool attacking, const_attack_ptr other_attack) const {
316  return specials_context_t(*this, other_attack, self, other, unit_loc, other_loc, attacking);
317  }
318  specials_context_t specials_context(unit_const_ptr self, const map_location& loc, bool attacking = true) const {
319  return specials_context_t(*this, self, loc, attacking);
320  }
321  specials_context_t specials_context(const unit_type& self_type, const map_location& loc, bool attacking = true) const {
322  return specials_context_t(*this, self_type, loc, attacking);
323  }
324  specials_context_t specials_context_for_listing(bool attacking = true) const {
325  return specials_context_t(*this, attacking);
326  }
327  void set_changed(bool value)
328  {
329  changed_ = value;
330  }
331  bool get_changed() const
332  {
333  return changed_;
334  }
335 private:
336 
338  std::string id_;
339  std::string type_;
340  std::string icon_;
341  std::string range_;
343  int damage_;
347 
351  int parry_;
353  bool changed_;
354 };
355 
356 using attack_list = std::vector<attack_ptr>;
357 using attack_itors = boost::iterator_range<boost::indirect_iterator<attack_list::iterator>>;
358 using const_attack_itors = boost::iterator_range<boost::indirect_iterator<attack_list::const_iterator>>;
359 
361  return boost::make_iterator_range(boost::make_indirect_iterator(atks.begin()), boost::make_indirect_iterator(atks.end()));
362 }
363 
365  return boost::make_iterator_range(boost::make_indirect_iterator(atks.begin()), boost::make_indirect_iterator(atks.end()));
366 }
boost::iterator_range< boost::indirect_iterator< attack_list::iterator > > attack_itors
std::vector< attack_ptr > attack_list
boost::iterator_range< boost::indirect_iterator< attack_list::const_iterator > > const_attack_itors
attack_itors make_attack_itors(attack_list &atks)
specials_context_t(const specials_context_t &)=delete
specials_context_t(const attack_type &weapon, bool attacking)
Initialize weapon specials context for listing.
Definition: abilities.cpp:1118
std::shared_ptr< const attack_type > parent
const config & specials() const
Definition: attack_type.hpp:55
map_location other_loc_
std::string weapon_specials() const
Returns a comma-separated string of active names for the specials of *this.
Definition: abilities.cpp:911
double defense_weight() const
Definition: attack_type.hpp:54
void set_num_attacks(int value)
Definition: attack_type.hpp:65
specials_context_t specials_context(unit_const_ptr self, const map_location &loc, bool attacking=true) const
bool check_self_abilities(const config &cfg, const std::string &special) const
check_self_abilities : return an boolean value for checking of activities of abilities used like weap...
Definition: abilities.cpp:1583
bool has_special(const std::string &special, bool simple_check=false, bool special_id=true, bool special_tags=true) const
Returns whether or not *this has a special with a tag or id equal to special.
Definition: abilities.cpp:752
static bool special_active_impl(const_attack_ptr self_attack, const_attack_ptr other_attack, const config &special, AFFECTS whom, const std::string &tag_name, const std::string &filter_self="filter_self")
Returns whether or not the given special is active for the specified unit, based on the current conte...
Definition: abilities.cpp:1757
int min_range() const
Definition: attack_type.hpp:46
int composite_value(const unit_ability_list &abil_list, int base_value) const
Return the special weapon value, considering specials.
Definition: abilities.cpp:1437
double attack_weight() const
Definition: attack_type.hpp:53
const_attack_ptr other_attack_
void add_formula_context(wfl::map_formula_callable &) const
Definition: abilities.cpp:527
const std::string & range() const
Definition: attack_type.hpp:45
void set_attacks_used(int value)
map_location self_loc_
int movement_used() const
specials_context_t specials_context_for_listing(bool attacking=true) const
void set_accuracy(int value)
Definition: attack_type.hpp:62
bool get_changed() const
const std::string & type() const
Definition: attack_type.hpp:43
void set_movement_used(int value)
unit_ability_list get_weapon_ability(const std::string &ability) const
Returns list for weapon like abilities for each ability type.
Definition: abilities.cpp:1401
int parry() const
Definition: attack_type.hpp:50
std::string weapon_specials_value(const std::set< std::string > checking_tags) const
Definition: abilities.cpp:954
std::string accuracy_parry_description() const
Definition: attack_type.cpp:83
unit_ability_list get_specials_and_abilities(const std::string &special) const
Definition: abilities.cpp:1420
bool apply_modification(const config &cfg)
Modifies *this using the specifications in cfg, but only if *this matches cfg viewed as a filter.
bool overwrite_special_checking(unit_ability_list &overwriters, const config &cfg, const std::string &tag_name) const
Check whether cfg would be overwritten by any element of overwriters.
Definition: abilities.cpp:1484
void set_specials(config value)
Definition: attack_type.hpp:68
unit_const_ptr self_
bool check_adj_abilities(const config &cfg, const std::string &special, int dir, const unit &from) const
check_adj_abilities : return an boolean value for checking of activities of abilities used like weapo...
Definition: abilities.cpp:1603
friend class specials_context_t
config specials_
void set_defense_weight(double value)
Definition: attack_type.hpp:67
int num_attacks() const
Definition: attack_type.hpp:52
void set_changed(bool value)
unit_ability_list overwrite_special_overwriter(unit_ability_list overwriters, const std::string &tag_name) const
Filter a list of abilities or weapon specials, removing any entries that don't own the overwrite_spec...
Definition: abilities.cpp:1448
static bool check_adj_abilities_impl(const_attack_ptr self_attack, const_attack_ptr other_attack, const config &special, unit_const_ptr u, const unit &from, int dir, const map_location &loc, AFFECTS whom, const std::string &tag_name, bool leader_bool=false)
check_adj_abilities_impl : return an boolean value for checking of activities of abilities used like ...
Definition: abilities.cpp:1608
static void weapon_specials_impl_adj(std::string &temp_string, unit_const_ptr self, const_attack_ptr self_attack, const_attack_ptr other_attack, const map_location &self_loc, AFFECTS whom, std::set< std::string > &checking_name, const std::set< std::string > &checking_tags={}, const std::string &affect_adjacents="", bool leader_bool=false)
Definition: abilities.cpp:1014
std::vector< std::pair< t_string, t_string > > special_tooltips(boost::dynamic_bitset<> *active_list=nullptr) const
Returns a vector of names and descriptions for the specials of *this.
Definition: abilities.cpp:857
std::string type_
std::string icon_
void set_parry(int value)
Definition: attack_type.hpp:63
void set_attack_weight(double value)
Definition: attack_type.hpp:66
void set_damage(int value)
Definition: attack_type.hpp:64
static void weapon_specials_impl_self(std::string &temp_string, unit_const_ptr self, const_attack_ptr self_attack, const_attack_ptr other_attack, const map_location &self_loc, AFFECTS whom, std::set< std::string > &checking_name, const std::set< std::string > &checking_tags={}, bool leader_bool=false)
weapon_specials_impl_self and weapon_specials_impl_adj : check if special name can be added.
Definition: abilities.cpp:994
const t_string & name() const
Definition: attack_type.hpp:41
std::set< std::string > alternative_damage_types() const
Definition: abilities.cpp:1262
bool describe_modification(const config &cfg, std::string *description)
Trimmed down version of apply_modification(), with no modifications actually made.
int attacks_used() const
const std::string & id() const
Definition: attack_type.hpp:42
void set_icon(const std::string &value)
Definition: attack_type.hpp:60
static bool check_self_abilities_impl(const_attack_ptr self_attack, const_attack_ptr other_attack, const config &special, unit_const_ptr u, const map_location &loc, AFFECTS whom, const std::string &tag_name, bool leader_bool=false)
check_self_abilities_impl : return an boolean value for checking of activities of abilities used like...
Definition: abilities.cpp:1588
double defense_weight_
std::string id_
int modified_damage() const
Returns the damage per attack of this weapon, considering specials.
Definition: abilities.cpp:1281
double attack_weight_
config to_config() const
void modified_attacks(unsigned &min_attacks, unsigned &max_attacks) const
Calculates the number of attacks this weapon has, considering specials.
Definition: abilities.cpp:1150
std::string range_
void set_id(const std::string &value)
Definition: attack_type.hpp:58
specials_context_t specials_context(unit_const_ptr self, unit_const_ptr other, const map_location &unit_loc, const map_location &other_loc, bool attacking, const_attack_ptr other_attack) const
attack_type(const config &cfg)
Definition: attack_type.cpp:49
void set_type(const std::string &value)
Definition: attack_type.hpp:59
void write(config &cfg) const
bool matches_filter(const config &filter, const std::string &tag_name="") const
Returns whether or not *this matches the given filter.
int accuracy() const
Definition: attack_type.hpp:49
unit_const_ptr other_
int max_range() const
Definition: attack_type.hpp:47
void set_range(const std::string &value)
Definition: attack_type.hpp:61
bool special_active(const config &special, AFFECTS whom, const std::string &tag_name, const std::string &filter_self="filter_self") const
Definition: abilities.cpp:1741
unit_ability_list get_specials(const std::string &special) const
Returns the currently active specials as an ability list, given the current context (see set_specials...
Definition: abilities.cpp:824
std::string select_damage_type(const unit_ability_list &damage_type_list, const std::string &key_name, unit_ability_list resistance_list) const
Select best damage type based on frequency count for replacement_type and based on highest damage for...
Definition: abilities.cpp:1228
const std::string & icon() const
Definition: attack_type.hpp:44
specials_context_t specials_context(const unit_type &self_type, const map_location &loc, bool attacking=true) const
int damage() const
Definition: attack_type.hpp:51
bool has_special_or_ability(const std::string &special, bool special_id=true, bool special_tags=true) const
used for abilities used like weapon and true specials
Definition: abilities.cpp:1729
t_string description_
bool has_weapon_ability(const std::string &special, bool special_id=true, bool special_tags=true) const
used for abilities used like weapon
Definition: abilities.cpp:1628
void set_name(const t_string &value)
Definition: attack_type.hpp:57
std::pair< std::string, std::string > damage_type() const
return a modified damage type and/or add a secondary_type for hybrid use if special is active.
Definition: abilities.cpp:1242
bool is_for_listing_
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
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
Definition: contexts.hpp:43
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
Encapsulates the map of the game.
Definition: location.hpp:38
mock_char c