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  /** return a modified damage type and/or add a secondary_type for hybrid use if special is active. */
91  std::pair<std::string, std::string> damage_type() const;
92 
93  /** Returns the damage per attack of this weapon, considering specials. */
94  int modified_damage() const;
95 
96  /** Return the special weapon value, considering specials.
97  * @param abil_list The list of special checked.
98  * @param base_value The value modified or not by function.
99  */
100  int composite_value(const unit_ability_list& abil_list, int base_value) const;
101  /** Returns list for weapon like abilities for each ability type. */
102  unit_ability_list get_weapon_ability(const std::string& ability) const;
103  /**
104  * @param special the tag name to check for
105  * @return list which contains get_weapon_ability and get_specials list for each ability type, with overwritten items removed
106  */
107  unit_ability_list get_specials_and_abilities(const std::string& special) const;
108  /** used for abilities used like weapon
109  * @return True if the ability @a special is active.
110  * @param special The special being checked.
111  * @param special_id If true, match @a special against the @c id of special tags.
112  * @param special_tags If true, match @a special against the tag name of special tags.
113  */
114  bool has_weapon_ability(const std::string& special, bool special_id=true, bool special_tags=true) const;
115  /** used for abilities used like weapon and true specials
116  * @return True if the ability @a special is active.
117  * @param special The special being checked.
118  * @param special_id If true, match @a special against the @c id of special tags.
119  * @param special_tags If true, match @a special against the tag name of special tags.
120  */
121  bool has_special_or_ability(const std::string& special, bool special_id=true, bool special_tags=true) const;
122 
123  // In unit_types.cpp:
124 
125  bool matches_filter(const config& filter, const std::string& tag_name = "") const;
126  bool apply_modification(const config& cfg);
127  bool describe_modification(const config& cfg,std::string* description);
128 
129  int movement_used() const { return movement_used_; }
130  void set_movement_used(int value) { movement_used_ = value; }
131  int attacks_used() const { return attacks_used_; }
132  void set_attacks_used(int value) { attacks_used_ = value; }
133 
134  void write(config& cfg) const;
135  inline config to_config() const { config c; write(c); return c; }
136 
138 private:
139  // In unit_abilities.cpp:
140 
141  // Configured as a bit field, in case that is useful.
143  /**
144  * Filter a list of abilities or weapon specials, removing any entries that don't own
145  * the overwrite_specials attributes.
146  *
147  * @param overwriters list that may have overwrite_specials attributes.
148  * @param tag_name type of abilitie/special checked.
149  */
150  unit_ability_list overwrite_special_overwriter(unit_ability_list overwriters, const std::string& tag_name) const;
151  /**
152  * Check whether @a cfg would be overwritten by any element of @a overwriters.
153  *
154  * @return True if element checked is overwritable.
155  * @param overwriters list used for check if element is overwritable.
156  * @param cfg element checked.
157  * @param tag_name type of abilitie/special checked.
158  */
159  bool overwrite_special_checking(unit_ability_list& overwriters, const config& cfg, const std::string& tag_name) const;
160  /** check_self_abilities : return an boolean value for checking of activities of abilities used like weapon
161  * @return True if the special @a special is active.
162  * @param cfg the config to one special ability checked.
163  * @param special The special ability type who is being checked.
164  */
165  bool check_self_abilities(const config& cfg, const std::string& special) const;
166  /** check_adj_abilities : return an boolean value for checking of activities of abilities used like weapon
167  * @return True if the special @a special is active.
168  * @param cfg the config to one special ability checked.
169  * @param special The special ability type who is being checked.
170  * @param dir direction to research a unit adjacent to self_.
171  * @param from unit adjacent to self_ is checked.
172  */
173  bool check_adj_abilities(const config& cfg, const std::string& special, int dir, const unit& from) const;
174  bool special_active(const config& special, AFFECTS whom, const std::string& tag_name,
175  const std::string& filter_self ="filter_self") const;
176 
177 /** weapon_specials_impl_self and weapon_specials_impl_adj : check if special name can be added.
178  * @param[in,out] temp_string the string modified and returned
179  * @param[in] self the unit checked.
180  * @param[in] self_attack the attack used by unit checked in this function.
181  * @param[in] other_attack the attack used by opponent to unit checked.
182  * @param[in] self_loc location of the unit checked.
183  * @param[in] whom determine if unit affected or not by special ability.
184  * @param[in,out] checking_name the reference for checking if a name is already added
185  * @param[in] checking_tags the reference for checking if special ability type can be used
186  * @param[in] leader_bool If true, [leadership] abilities are checked.
187  */
188  static void weapon_specials_impl_self(
189  std::string& temp_string,
190  unit_const_ptr self,
191  const_attack_ptr self_attack,
192  const_attack_ptr other_attack,
193  const map_location& self_loc,
194  AFFECTS whom,
195  std::set<std::string>& checking_name,
196  const std::set<std::string>& checking_tags={},
197  bool leader_bool=false
198  );
199 
200  static void weapon_specials_impl_adj(
201  std::string& temp_string,
202  unit_const_ptr self,
203  const_attack_ptr self_attack,
204  const_attack_ptr other_attack,
205  const map_location& self_loc,
206  AFFECTS whom,
207  std::set<std::string>& checking_name,
208  const std::set<std::string>& checking_tags={},
209  const std::string& affect_adjacents="",
210  bool leader_bool=false
211  );
212  /** check_self_abilities_impl : return an boolean value for checking of activities of abilities used like weapon
213  * @return True if the special @a tag_name is active.
214  * @param self_attack the attack used by unit checked in this function.
215  * @param other_attack the attack used by opponent to unit checked.
216  * @param special the config to one special ability checked.
217  * @param u the unit checked.
218  * @param loc location of the unit checked.
219  * @param whom determine if unit affected or not by special ability.
220  * @param tag_name The special ability type who is being checked.
221  * @param leader_bool If true, [leadership] abilities are checked.
222  */
223  static bool check_self_abilities_impl(
224  const_attack_ptr self_attack,
225  const_attack_ptr other_attack,
226  const config& special,
227  unit_const_ptr u,
228  const map_location& loc,
229  AFFECTS whom,
230  const std::string& tag_name,
231  bool leader_bool=false
232  );
233 
234 
235  /** check_adj_abilities_impl : return an boolean value for checking of activities of abilities used like weapon in unit adjacent to fighter
236  * @return True if the special @a tag_name is active.
237  * @param self_attack the attack used by unit who fight.
238  * @param other_attack the attack used by opponent.
239  * @param special the config to one special ability checked.
240  * @param u the unit who is or not affected by an abilities owned by @a from.
241  * @param from unit adjacent to @a u is checked.
242  * @param dir direction to research a unit adjacent to @a u.
243  * @param loc location of the unit checked.
244  * @param whom determine if unit affected or not by special ability.
245  * @param tag_name The special ability type who is being checked.
246  * @param leader_bool If true, [leadership] abilities are checked.
247  */
248  static bool check_adj_abilities_impl(
249  const_attack_ptr self_attack,
250  const_attack_ptr other_attack,
251  const config& special,
252  unit_const_ptr u,
253  const unit& from,
254  int dir,
255  const map_location& loc,
256  AFFECTS whom,
257  const std::string& tag_name,
258  bool leader_bool=false
259  );
260 
261  static bool special_active_impl(
262  const_attack_ptr self_attack,
263  const_attack_ptr other_attack,
264  const config& special,
265  AFFECTS whom,
266  const std::string& tag_name,
267  const std::string& filter_self ="filter_self"
268  );
269 
270  // Used via specials_context() to control which specials are
271  // considered active.
272  friend class specials_context_t;
276  mutable bool is_attacker_;
278  mutable bool is_for_listing_ = false;
279 public:
281  std::shared_ptr<const attack_type> parent;
282  friend class attack_type;
283  /** Initialize weapon specials context for listing */
284  explicit specials_context_t(const attack_type& weapon, bool attacking);
285  /** Initialize weapon specials context for a unit type */
286  specials_context_t(const attack_type& weapon, const unit_type& self_type, const map_location& loc, bool attacking = true);
287  /** Initialize weapon specials context for a single unit */
288  specials_context_t(const attack_type& weapon, const_attack_ptr other_weapon,
289  unit_const_ptr self, unit_const_ptr other,
290  const map_location& self_loc, const map_location& other_loc,
291  bool attacking);
292  /** Initialize weapon specials context for a pair of units */
293  specials_context_t(const attack_type& weapon, unit_const_ptr self, const map_location& loc, bool attacking);
295  bool was_moved = false;
296  public:
297  // Destructor at least needs to be public for all this to work.
300  };
301  // Set up a specials context.
302  // Usage: auto ctx = weapon.specials_context(...);
304  const map_location& unit_loc, const map_location& other_loc,
305  bool attacking, const_attack_ptr other_attack) const {
306  return specials_context_t(*this, other_attack, self, other, unit_loc, other_loc, attacking);
307  }
308  specials_context_t specials_context(unit_const_ptr self, const map_location& loc, bool attacking = true) const {
309  return specials_context_t(*this, self, loc, attacking);
310  }
311  specials_context_t specials_context(const unit_type& self_type, const map_location& loc, bool attacking = true) const {
312  return specials_context_t(*this, self_type, loc, attacking);
313  }
314  specials_context_t specials_context_for_listing(bool attacking = true) const {
315  return specials_context_t(*this, attacking);
316  }
317  void set_changed(bool value)
318  {
319  changed_ = value;
320  }
321  bool get_changed() const
322  {
323  return changed_;
324  }
325 private:
326 
328  std::string id_;
329  std::string type_;
330  std::string icon_;
331  std::string range_;
333  int damage_;
337 
341  int parry_;
343  bool changed_;
344 };
345 
346 using attack_list = std::vector<attack_ptr>;
347 using attack_itors = boost::iterator_range<boost::indirect_iterator<attack_list::iterator>>;
348 using const_attack_itors = boost::iterator_range<boost::indirect_iterator<attack_list::const_iterator>>;
349 
351  return boost::make_iterator_range(boost::make_indirect_iterator(atks.begin()), boost::make_indirect_iterator(atks.end()));
352 }
353 
355  return boost::make_iterator_range(boost::make_indirect_iterator(atks.begin()), boost::make_indirect_iterator(atks.end()));
356 }
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:1522
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:1696
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:1376
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:1340
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:1359
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:1423
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:1542
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:1387
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:1547
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
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:1527
double defense_weight_
std::string id_
int modified_damage() const
Returns the damage per attack of this weapon, considering specials.
Definition: abilities.cpp:1220
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:1680
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
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:1668
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:1567
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:1198
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