The Battle for Wesnoth  1.15.2+dev
attack_type.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 https://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 #pragma once
16 
17 #include "map/location.hpp"
18 #include "tstring.hpp"
19 #include "config.hpp"
20 #include <string>
21 #include <vector>
22 #include <cassert>
23 
24 #include <boost/iterator/indirect_iterator.hpp>
25 #include <boost/range/iterator_range.hpp>
26 #include <boost/dynamic_bitset_fwd.hpp>
27 
28 #include "units/ptr.hpp" // for attack_ptr
29 
30 class unit_ability_list;
31 class unit_type;
32 
33 //the 'attack type' is the type of attack, how many times it strikes,
34 //and how much damage it does.
35 class attack_type : public std::enable_shared_from_this<attack_type>
36 {
37 public:
38 
39  explicit attack_type(const config& cfg);
40  const t_string& name() const { return description_; }
41  const std::string& id() const { return id_; }
42  const std::string& type() const { return type_; }
43  const std::string& icon() const { return icon_; }
44  const std::string& range() const { return range_; }
45  int min_range() const { return min_range_; }
46  int max_range() const { return max_range_; }
47  std::string accuracy_parry_description() const;
48  int accuracy() const { return accuracy_; }
49  int parry() const { return parry_; }
50  int damage() const { return damage_; }
51  int num_attacks() const { return num_attacks_; }
52  double attack_weight() const { return attack_weight_; }
53  double defense_weight() const { return defense_weight_; }
54  const config &specials() const { return specials_; }
55 
56  void set_name(const t_string& value) { description_ = value; set_changed(true); }
57  void set_id(const std::string& value) { id_ = value; set_changed(true); }
58  void set_type(const std::string& value) { type_ = value; set_changed(true); }
59  void set_icon(const std::string& value) { icon_ = value; set_changed(true); }
60  void set_range(const std::string& value) { range_ = value; set_changed(true); }
61  void set_accuracy(int value) { accuracy_ = value; set_changed(true); }
62  void set_parry(int value) { parry_ = value; set_changed(true); }
63  void set_damage(int value) { damage_ = value; set_changed(true); }
64  void set_num_attacks(int value) { num_attacks_ = value; set_changed(true); }
65  void set_attack_weight(double value) { attack_weight_ = value; set_changed(true); }
66  void set_defense_weight(double value) { defense_weight_ = value; set_changed(true); }
67  void set_specials(config value) { specials_ = value; set_changed(true); }
68 
69  // In action/attack.cpp
70  std::pair<int, bool> combat_ability(const std::string& ability, int abil_value = 0, bool backstab_pos = false) const;
71  bool bool_ability(const std::string& ability) const;
72 
73 
74  // In unit_abilities.cpp:
75 
76  /// @return True iff the special @a special is active.
77  /// @param simple_check If true, check whether the unit has the special. Else, check whether the special is currently active.
78  /// @param special_id If true, match @a special against the @c id of special tags.
79  /// @param special_tags If true, match @a special against the tag name of special tags.
80  bool get_special_bool(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(bool only_active=false, bool is_backstab=false) const;
84 
85  /// Calculates the number of attacks this weapon has, considering specials.
86  void modified_attacks(bool is_backstab, unsigned & min_attacks,
87  unsigned & max_attacks) const;
88  /// Returns the damage per attack of this weapon, considering specials.
89  int modified_damage(bool is_backstab) const;
90 
91  // In unit_types.cpp:
92 
93  bool matches_filter(const config& filter) const;
94  bool apply_modification(const config& cfg);
95  bool describe_modification(const config& cfg,std::string* description);
96 
97  int movement_used() const { return movement_used_; }
98  void set_movement_used(int value) { movement_used_ = value; }
99 
100  void write(config& cfg) const;
101  inline config to_config() const { config c; write(c); return c; }
102 
103 private:
104  // In unit_abilities.cpp:
105 
106  // Configured as a bit field, in case that is useful.
108  bool special_active(const config& special, AFFECTS whom, const std::string& tag_name,
109  bool include_backstab=true) const;
110 
111  // Used via specials_context() to control which specials are
112  // considered active.
113  friend class specials_context_t;
117  mutable bool is_attacker_;
119  mutable bool is_for_listing_ = false;
120 public:
122  std::shared_ptr<const attack_type> parent;
123  friend class attack_type;
124  /// Initialize weapon specials context for listing
125  explicit specials_context_t(const attack_type& weapon, bool attacking);
126  /// Initialize weapon specials context for a unit type
127  specials_context_t(const attack_type& weapon, const unit_type& self_type, const map_location& loc, bool attacking = true);
128  /// Initialize weapon specials context for a single unit
129  specials_context_t(const attack_type& weapon, const_attack_ptr other_weapon,
130  unit_const_ptr self, unit_const_ptr other,
131  const map_location& self_loc, const map_location& other_loc,
132  bool attacking);
133  /// Initialize weapon specials context for a pair of units
134  specials_context_t(const attack_type& weapon, unit_const_ptr self, const map_location& loc, bool attacking);
135  specials_context_t(const specials_context_t&) = delete;
136  bool was_moved = false;
137  public:
138  // Destructor at least needs to be public for all this to work.
141  };
142  // Set up a specials context.
143  // Usage: auto ctx = weapon.specials_context(...);
145  const map_location& unit_loc, const map_location& other_loc,
146  bool attacking, const_attack_ptr other_attack) const {
147  return specials_context_t(*this, other_attack, self, other, unit_loc, other_loc, attacking);
148  }
149  specials_context_t specials_context(unit_const_ptr self, const map_location& loc, bool attacking = true) const {
150  return specials_context_t(*this, self, loc, attacking);
151  }
152  specials_context_t specials_context(const unit_type& self_type, const map_location& loc, bool attacking = true) const {
153  return specials_context_t(*this, self_type, loc, attacking);
154  }
155  specials_context_t specials_context_for_listing(bool attacking = true) const {
156  return specials_context_t(*this, attacking);
157  }
158  void set_changed(bool value)
159  {
160  changed_ = value;
161  }
162  bool get_changed() const
163  {
164  return changed_;
165  }
166 private:
167 
169  std::string id_;
170  std::string type_;
171  std::string icon_;
172  std::string range_;
174  int damage_;
178 
181  int parry_;
183  bool changed_;
184 };
185 
186 using attack_list = std::vector<attack_ptr>;
187 using attack_itors = boost::iterator_range<boost::indirect_iterator<attack_list::iterator>>;
188 using const_attack_itors = boost::iterator_range<boost::indirect_iterator<attack_list::const_iterator>>;
189 
191  return boost::make_iterator_range(boost::make_indirect_iterator(atks.begin()), boost::make_indirect_iterator(atks.end()));
192 }
193 
195  return boost::make_iterator_range(boost::make_indirect_iterator(atks.begin()), boost::make_indirect_iterator(atks.end()));
196 }
std::vector< attack_ptr > attack_list
boost::intrusive_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:30
bool apply_modification(const config &cfg)
Modifies *this using the specifications in cfg, but only if *this matches cfg viewed as a filter...
std::string weapon_specials(bool only_active=false, bool is_backstab=false) const
Returns a comma-separated string of active names for the specials of *this.
Definition: abilities.cpp:814
std::string icon_
const std::string & id() const
Definition: attack_type.hpp:41
std::string type_
attack_type(const config &cfg)
Definition: attack_type.cpp:46
boost::iterator_range< boost::indirect_iterator< attack_list::iterator > > attack_itors
int max_range() const
Definition: attack_type.hpp:46
map_location other_loc_
void set_num_attacks(int value)
Definition: attack_type.hpp:64
int parry() const
Definition: attack_type.hpp:49
void modified_attacks(bool is_backstab, unsigned &min_attacks, unsigned &max_attacks) const
Calculates the number of attacks this weapon has, considering specials.
Definition: abilities.cpp:937
unit_const_ptr other_
const std::string & type() const
Definition: attack_type.hpp:42
attack_itors make_attack_itors(attack_list &atks)
Definitions for the interface to Wesnoth Markup Language (WML).
std::pair< int, bool > combat_ability(const std::string &ability, int abil_value=0, bool backstab_pos=false) const
Definition: attack.cpp:1748
void set_changed(bool value)
int num_attacks() const
Definition: attack_type.hpp:51
A single unit type that the player may recruit.
Definition: types.hpp:42
double defense_weight_
void set_name(const t_string &value)
Definition: attack_type.hpp:56
int min_range() const
Definition: attack_type.hpp:45
bool bool_ability(const std::string &ability) const
Definition: attack.cpp:1732
int movement_used() const
Definition: attack_type.hpp:97
specials_context_t(const attack_type &weapon, bool attacking)
Initialize weapon specials context for listing.
Definition: abilities.cpp:905
double attack_weight_
bool matches_filter(const config &filter) const
Returns whether or not *this matches the given filter.
const t_string & name() const
Definition: attack_type.hpp:40
std::string range_
specials_context_t specials_context(const unit_type &self_type, const map_location &loc, bool attacking=true) const
const std::string & range() const
Definition: attack_type.hpp:44
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:778
const std::string & icon() const
Definition: attack_type.hpp:43
bool get_changed() const
config specials_
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:746
void set_defense_weight(double value)
Definition: attack_type.hpp:66
void set_specials(config value)
Definition: attack_type.hpp:67
t_string description_
Encapsulates the map of the game.
Definition: location.hpp:42
specials_context_t specials_context(unit_const_ptr self, const map_location &loc, bool attacking=true) const
bool special_active(const config &special, AFFECTS whom, const std::string &tag_name, bool include_backstab=true) const
Returns whether or not the given special is active for the specified unit, based on the current conte...
Definition: abilities.cpp:1095
void set_damage(int value)
Definition: attack_type.hpp:63
bool is_for_listing_
int damage() const
Definition: attack_type.hpp:50
int accuracy() const
Definition: attack_type.hpp:48
const config & specials() const
Definition: attack_type.hpp:54
double attack_weight() const
Definition: attack_type.hpp:52
void write(config &cfg) const
std::string accuracy_parry_description() const
Definition: attack_type.cpp:79
unit_const_ptr self_
int modified_damage(bool is_backstab) const
Returns the damage per attack of this weapon, considering specials.
Definition: abilities.cpp:967
double defense_weight() const
Definition: attack_type.hpp:53
bool describe_modification(const config &cfg, std::string *description)
Trimmed down version of apply_modification(), with no modifications actually made.
void set_range(const std::string &value)
Definition: attack_type.hpp:60
void set_parry(int value)
Definition: attack_type.hpp:62
std::string id_
const_attack_ptr other_attack_
void set_icon(const std::string &value)
Definition: attack_type.hpp:59
void set_id(const std::string &value)
Definition: attack_type.hpp:57
specials_context_t specials_context_for_listing(bool attacking=true) const
bool get_special_bool(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:674
config to_config() const
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
boost::iterator_range< boost::indirect_iterator< attack_list::const_iterator > > const_attack_itors
void set_movement_used(int value)
Definition: attack_type.hpp:98
std::shared_ptr< const attack_type > parent
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
mock_char c
std::shared_ptr< const attack_type > const_attack_ptr
Definition: ptr.hpp:37
void set_accuracy(int value)
Definition: attack_type.hpp:61
map_location self_loc_
void set_attack_weight(double value)
Definition: attack_type.hpp:65
void set_type(const std::string &value)
Definition: attack_type.hpp:58