The Battle for Wesnoth  1.19.2+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  * Returns true if this is a dummy attack_type, for example the placeholder that the unit_attack dialog
134  * uses when a defender has no weapon for a given range.
135  */
136  bool attack_empty() const {return (id().empty() && name().empty() && type().empty() && range().empty());}
137 
138  // In unit_types.cpp:
139 
140  bool matches_filter(const config& filter, const std::string& tag_name = "") const;
141  bool apply_modification(const config& cfg);
142  bool describe_modification(const config& cfg,std::string* description);
143 
144  int movement_used() const { return movement_used_; }
145  void set_movement_used(int value) { movement_used_ = value; }
146  int attacks_used() const { return attacks_used_; }
147  void set_attacks_used(int value) { attacks_used_ = value; }
148 
149  void write(config& cfg) const;
150  inline config to_config() const { config c; write(c); return c; }
151 
153 private:
154  // In unit_abilities.cpp:
155 
156  // Configured as a bit field, in case that is useful.
158  /**
159  * Filter a list of abilities or weapon specials, removing any entries that don't own
160  * the overwrite_specials attributes.
161  *
162  * @param overwriters list that may have overwrite_specials attributes.
163  * @param tag_name type of abilitie/special checked.
164  */
165  unit_ability_list overwrite_special_overwriter(unit_ability_list overwriters, const std::string& tag_name) const;
166  /**
167  * Check whether @a cfg would be overwritten by any element of @a overwriters.
168  *
169  * @return True if element checked is overwritable.
170  * @param overwriters list used for check if element is overwritable.
171  * @param cfg element checked.
172  * @param tag_name type of abilitie/special checked.
173  */
174  bool overwrite_special_checking(unit_ability_list& overwriters, const config& cfg, const std::string& tag_name) const;
175  /** check_self_abilities : return an boolean value for checking of activities of abilities used like weapon
176  * @return True if the special @a special is active.
177  * @param cfg the config to one special ability checked.
178  * @param special The special ability type who is being checked.
179  */
180  bool check_self_abilities(const config& cfg, const std::string& special) const;
181  /** check_adj_abilities : return an boolean value for checking of activities of abilities used like weapon
182  * @return True if the special @a special is active.
183  * @param cfg the config to one special ability checked.
184  * @param special The special ability type who is being checked.
185  * @param dir direction to research a unit adjacent to self_.
186  * @param from unit adjacent to self_ is checked.
187  */
188  bool check_adj_abilities(const config& cfg, const std::string& special, int dir, const unit& from) const;
189  bool special_active(const config& special, AFFECTS whom, const std::string& tag_name,
190  const std::string& filter_self ="filter_self") const;
191 
192 /** weapon_specials_impl_self and weapon_specials_impl_adj : check if special name can be added.
193  * @param[in,out] temp_string the string modified and returned
194  * @param[in] self the unit checked.
195  * @param[in] self_attack the attack used by unit checked in this function.
196  * @param[in] other_attack the attack used by opponent to unit checked.
197  * @param[in] self_loc location of the unit checked.
198  * @param[in] whom determine if unit affected or not by special ability.
199  * @param[in,out] checking_name the reference for checking if a name is already added
200  * @param[in] checking_tags the reference for checking if special ability type can be used
201  * @param[in] leader_bool If true, [leadership] abilities are checked.
202  */
203  static void weapon_specials_impl_self(
204  std::string& temp_string,
205  unit_const_ptr self,
206  const_attack_ptr self_attack,
207  const_attack_ptr other_attack,
208  const map_location& self_loc,
209  AFFECTS whom,
210  std::set<std::string>& checking_name,
211  const std::set<std::string>& checking_tags={},
212  bool leader_bool=false
213  );
214 
215  static void weapon_specials_impl_adj(
216  std::string& temp_string,
217  unit_const_ptr self,
218  const_attack_ptr self_attack,
219  const_attack_ptr other_attack,
220  const map_location& self_loc,
221  AFFECTS whom,
222  std::set<std::string>& checking_name,
223  const std::set<std::string>& checking_tags={},
224  const std::string& affect_adjacents="",
225  bool leader_bool=false
226  );
227  /** check_self_abilities_impl : return an boolean value for checking of activities of abilities used like weapon
228  * @return True if the special @a tag_name is active.
229  * @param self_attack the attack used by unit checked in this function.
230  * @param other_attack the attack used by opponent to unit checked.
231  * @param special the config to one special ability checked.
232  * @param u the unit checked.
233  * @param loc location of the unit checked.
234  * @param whom determine if unit affected or not by special ability.
235  * @param tag_name The special ability type who is being checked.
236  * @param leader_bool If true, [leadership] abilities are checked.
237  */
238  static bool check_self_abilities_impl(
239  const_attack_ptr self_attack,
240  const_attack_ptr other_attack,
241  const config& special,
242  unit_const_ptr u,
243  const map_location& loc,
244  AFFECTS whom,
245  const std::string& tag_name,
246  bool leader_bool=false
247  );
248 
249 
250  /** check_adj_abilities_impl : return an boolean value for checking of activities of abilities used like weapon in unit adjacent to fighter
251  * @return True if the special @a tag_name is active.
252  * @param self_attack the attack used by unit who fight.
253  * @param other_attack the attack used by opponent.
254  * @param special the config to one special ability checked.
255  * @param u the unit who is or not affected by an abilities owned by @a from.
256  * @param from unit adjacent to @a u is checked.
257  * @param dir direction to research a unit adjacent to @a u.
258  * @param loc location of the unit checked.
259  * @param whom determine if unit affected or not by special ability.
260  * @param tag_name The special ability type who is being checked.
261  * @param leader_bool If true, [leadership] abilities are checked.
262  */
263  static bool check_adj_abilities_impl(
264  const_attack_ptr self_attack,
265  const_attack_ptr other_attack,
266  const config& special,
267  unit_const_ptr u,
268  const unit& from,
269  int dir,
270  const map_location& loc,
271  AFFECTS whom,
272  const std::string& tag_name,
273  bool leader_bool=false
274  );
275 
276  static bool special_active_impl(
277  const_attack_ptr self_attack,
278  const_attack_ptr other_attack,
279  const config& special,
280  AFFECTS whom,
281  const std::string& tag_name,
282  const std::string& filter_self ="filter_self"
283  );
284 
285  // Used via specials_context() to control which specials are
286  // considered active.
287  friend class specials_context_t;
291  mutable bool is_attacker_;
293  mutable bool is_for_listing_ = false;
294 public:
296  std::shared_ptr<const attack_type> parent;
297  friend class attack_type;
298  /** Initialize weapon specials context for listing */
299  explicit specials_context_t(const attack_type& weapon, bool attacking);
300  /** Initialize weapon specials context for a unit type */
301  specials_context_t(const attack_type& weapon, const unit_type& self_type, const map_location& loc, bool attacking = true);
302  /** Initialize weapon specials context for a single unit */
303  specials_context_t(const attack_type& weapon, const_attack_ptr other_weapon,
304  unit_const_ptr self, unit_const_ptr other,
305  const map_location& self_loc, const map_location& other_loc,
306  bool attacking);
307  /** Initialize weapon specials context for a pair of units */
308  specials_context_t(const attack_type& weapon, unit_const_ptr self, const map_location& loc, bool attacking);
310  bool was_moved = false;
311  public:
312  // Destructor at least needs to be public for all this to work.
315  };
316  // Set up a specials context.
317  // Usage: auto ctx = weapon.specials_context(...);
319  const map_location& unit_loc, const map_location& other_loc,
320  bool attacking, const_attack_ptr other_attack) const {
321  return specials_context_t(*this, other_attack, self, other, unit_loc, other_loc, attacking);
322  }
323  specials_context_t specials_context(unit_const_ptr self, const map_location& loc, bool attacking = true) const {
324  return specials_context_t(*this, self, loc, attacking);
325  }
326  specials_context_t specials_context(const unit_type& self_type, const map_location& loc, bool attacking = true) const {
327  return specials_context_t(*this, self_type, loc, attacking);
328  }
329  specials_context_t specials_context_for_listing(bool attacking = true) const {
330  return specials_context_t(*this, attacking);
331  }
332  void set_changed(bool value)
333  {
334  changed_ = value;
335  }
336  bool get_changed() const
337  {
338  return changed_;
339  }
340 private:
341 
343  std::string id_;
344  std::string type_;
345  std::string icon_;
346  std::string range_;
348  int damage_;
352 
356  int parry_;
358  bool changed_;
359 };
360 
361 using attack_list = std::vector<attack_ptr>;
362 using attack_itors = boost::iterator_range<boost::indirect_iterator<attack_list::iterator>>;
363 using const_attack_itors = boost::iterator_range<boost::indirect_iterator<attack_list::const_iterator>>;
364 
366  return boost::make_iterator_range(boost::make_indirect_iterator(atks.begin()), boost::make_indirect_iterator(atks.end()));
367 }
368 
370  return boost::make_iterator_range(boost::make_indirect_iterator(atks.begin()), boost::make_indirect_iterator(atks.end()));
371 }
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:1588
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:1762
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:1442
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:1406
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:1425
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:1489
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:1608
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:1453
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:1613
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:1265
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
bool attack_empty() const
Returns true if this is a dummy attack_type, for example the placeholder that the unit_attack dialog ...
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:1593
double defense_weight_
std::string id_
int modified_damage() const
Returns the damage per attack of this weapon, considering specials.
Definition: abilities.cpp:1286
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:1746
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:1734
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:1633
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