The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
movetype.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by David White <[email protected]>
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 #ifndef MOVETYPE_H_INCLUDED
15 #define MOVETYPE_H_INCLUDED
16 
17 #include "config.hpp"
19 
20 #include <boost/shared_ptr.hpp>
21 
22 class attack_type;
23 namespace t_translation { struct t_terrain; }
24 
25 
26 /// The basic "size" of the unit - flying, small land, large land, etc.
27 /// This encompasses terrain costs, defenses, and resistances.
28 class movetype
29 {
30  /// Stores a set of data based on terrain.
32  {
33  /// The terrain-based data.
34  class data;
35  // The data class is not defined here to keep the header file cleaner.
36 
37  public:
38  /// The parameters used when calculating a terrain-based value.
39  struct parameters;
40 
41  explicit terrain_info(const parameters & params,
42  const terrain_info * fallback=nullptr,
43  const terrain_info * cascade=nullptr);
44  terrain_info(const config & cfg, const parameters & params,
45  const terrain_info * fallback=nullptr,
46  const terrain_info * cascade=nullptr);
47  terrain_info(const terrain_info & that,
48  const terrain_info * fallback=nullptr,
49  const terrain_info * cascade=nullptr);
50  ~terrain_info();
51 
52  terrain_info & operator=(const terrain_info & that);
53 
54 
55  /// Clears the cache of values.
56  void clear_cache() const;
57  /// Returns whether or not our data is empty.
58  bool empty() const;
59  /// Merges the given config over the existing values.
60  void merge(const config & new_values, bool overwrite);
61  /// Returns the value associated with the given terrain.
62  int value(const t_translation::t_terrain & terrain) const;
63  /// Writes our data to a config.
64  void write(config & cfg, const std::string & child_name="", bool merged=true) const;
65 
66  private:
67  // Returns a pointer to data the incorporates our fallback.
68  const boost::shared_ptr<data> & get_merged() const;
69  // Ensures our data is not shared, and propagates to our cascade.
70  void make_unique_cascade() const;
71  // Ensures our data is not shared, and propagates to our fallback.
72  void make_unique_fallback() const;
73 
74  private:
75  boost::shared_ptr<data> data_; /// Never nullptr
76  mutable boost::shared_ptr<data> merged_data_; /// Created as needed.
77  const terrain_info * const fallback_;
78  const terrain_info * const cascade_;
79  };
80 
81 
82 public:
83  /// Magic value that signifies a hex is unreachable.
84  /// The UNREACHABLE macro in the data tree should match this value.
85  static const int UNREACHABLE = 99;
86 
87  /// Stores a set of terrain costs (for movement, vision, or "jamming").
88  class terrain_costs : public terrain_info
89  {
90  static const parameters params_;
91  public:
92  explicit terrain_costs(const terrain_costs * fallback=nullptr,
93  const terrain_costs * cascade=nullptr) :
94  terrain_info(params_, fallback, cascade)
95  {}
96  explicit terrain_costs(const config & cfg,
97  const terrain_costs * fallback=nullptr,
98  const terrain_costs * cascade=nullptr) :
99  terrain_info(cfg, params_, fallback, cascade)
100  {}
102  const terrain_costs * fallback=nullptr,
103  const terrain_costs * cascade=nullptr) :
104  terrain_info(that, fallback, cascade)
105  {}
106 
107  /// Returns the cost associated with the given terrain.
108  /// Costs are doubled when @a slowed is true.
109  int cost(const t_translation::t_terrain & terrain, bool slowed=false) const
110  { int result = value(terrain);
111  return slowed && result != movetype::UNREACHABLE ? 2 * result : result; }
112 
113  // Inherited from terrain_info:
114  //void merge(const config & new_values, bool overwrite);
115  //void write(config & cfg, const std::string & child_name="", bool merged=true) const;
116  };
117 
118  /// Stores a set of defense levels.
120  {
123 
124  public:
125  terrain_defense() : min_(params_min_), max_(params_max_) {}
126  explicit terrain_defense(const config & cfg) :
127  min_(cfg, params_min_), max_(cfg, params_max_)
128  {}
129 
130  /// Returns the defense associated with the given terrain.
132  { return std::max(min_.value(terrain), max_.value(terrain)); }
133  /// Returns whether there is a defense cap associated to this terrain.
135  { return min_.value(terrain) != 0; }
136  /// Merges the given config over the existing costs.
137  /// (Not overwriting implies adding.)
138  void merge(const config & new_data, bool overwrite)
139  { min_.merge(new_data, overwrite); max_.merge(new_data, overwrite); }
140  /// Writes our data to a config, as a child if @a child_name is specified.
141  /// (No child is created if there is no data.)
142  void write(config & cfg, const std::string & child_name="") const
143  { max_.write(cfg, child_name, false); }
144 
145  private:
146  // There will be duplication of the config here, but it is a small
147  // config, and the duplication allows greater code sharing.
150  };
151 
152  /// Stores a set of resistances.
154  {
155  public:
156  resistances() : cfg_() {}
157  explicit resistances(const config & cfg) : cfg_(cfg) {}
158 
159  /// Returns a map from attack types to resistances.
161  /// Returns the resistance against the indicated attack.
162  int resistance_against(const attack_type & attack) const;
163  /// Returns the resistance against the indicated damage type.
164  int resistance_against(const std::string & damage_type) const;
165  /// Merges the given config over the existing costs.
166  void merge(const config & new_data, bool overwrite);
167  /// Writes our data to a config, as a child if @a child_name is specified.
168  void write(config & out_cfg, const std::string & child_name="") const;
169 
170  private:
172  };
173 
174 public:
175  movetype();
176  explicit movetype(const config & cfg);
177  movetype(const movetype & that);
178 
179  // This class is basically just a holder for its various pieces, so
180  // provide access to those pieces on demand.
186  // And const access:
187  const terrain_costs & get_movement() const { return movement_; }
188  const terrain_costs & get_vision() const { return vision_; }
189  const terrain_costs & get_jamming() const { return jamming_; }
190  const terrain_defense & get_defense() const { return defense_; }
191  const resistances & get_resistances() const { return resist_; }
192 
193  /// Returns whether or not *this is flagged as a flying movement type.
194  bool is_flying() const { return flying_; }
195  /// Sets whether or not *this is flagged as a flying movement type.
196  void set_flying(bool flies=true) { flying_ = flies; }
197 
198  /// Returns the cost to move through the indicated terrain.
199  int movement_cost(const t_translation::t_terrain & terrain, bool slowed=false) const
200  { return movement_.cost(terrain, slowed); }
201  /// Returns the cost to see through the indicated terrain.
202  int vision_cost(const t_translation::t_terrain & terrain, bool slowed=false) const
203  { return vision_.cost(terrain, slowed); }
204  /// Returns the cost to "jam" through the indicated terrain.
205  int jamming_cost(const t_translation::t_terrain & terrain, bool slowed=false) const
206  { return jamming_.cost(terrain, slowed); }
207 
208  /// Returns the defensive value of the indicated terrain.
210  { return defense_.defense(terrain); }
211 
212  /// Returns the resistance against the indicated attack.
213  int resistance_against(const attack_type & attack) const
214  { return resist_.resistance_against(attack); }
215  /// Returns the resistance against the indicated damage type.
216  int resistance_against(const std::string & damage_type) const
217  { return resist_.resistance_against(damage_type); }
218  /// Returns a map from attack types to resistances.
220  { return resist_.damage_table(); }
221 
222  /// Returns whether or not there are any terrain caps with respect to a set of terrains.
223  bool has_terrain_defense_caps(const std::set<t_translation::t_terrain> & ts) const;
224  /// Returns whether or not there are any vision-specific costs.
225  bool has_vision_data() const { return !vision_.empty(); }
226  /// Returns whether or not there are any jamming-specific costs.
227  bool has_jamming_data() const { return !jamming_.empty(); }
228 
229  /// Merges the given config over the existing data.
230  void merge(const config & new_cfg, bool overwrite=true);
231 
232  /// The set of applicable effects for movement types
233  static const std::set<std::string> effects;
234 
235  /// Writes the movement type data to the provided config.
236  void write(config & cfg) const;
237 
238 private:
244 
245  bool flying_;
246 };
247 
248 
249 #endif // MOVETYPE_H_INCLUDED
250 
Stores a set of terrain costs (for movement, vision, or "jamming").
Definition: movetype.hpp:88
int resistance_against(const std::string &damage_type) const
Returns the resistance against the indicated damage type.
Definition: movetype.hpp:216
resistances & get_resistances()
Definition: movetype.hpp:185
void clear_cache() const
Clears the cache of values.
Definition: movetype.cpp:489
const terrain_info *const fallback_
Created as needed.
Definition: movetype.hpp:77
terrain_defense(const config &cfg)
Definition: movetype.hpp:126
const boost::shared_ptr< data > & get_merged() const
Returns a pointer to data the incorporates our fallback.
Definition: movetype.cpp:572
int vision_cost(const t_translation::t_terrain &terrain, bool slowed=false) const
Returns the cost to see through the indicated terrain.
Definition: movetype.hpp:202
int defense(const t_translation::t_terrain &terrain) const
Returns the defense associated with the given terrain.
Definition: movetype.hpp:131
const terrain_costs & get_movement() const
Definition: movetype.hpp:187
void make_unique_fallback() const
Ensures our data is not shared, and propagates to our fallback.
Definition: movetype.cpp:613
void write(config &cfg, const std::string &child_name="", bool merged=true) const
Writes our data to a config.
Definition: movetype.cpp:558
static const int UNREACHABLE
Magic value that signifies a hex is unreachable.
Definition: movetype.hpp:85
boost::shared_ptr< data > merged_data_
Never nullptr.
Definition: movetype.hpp:76
movetype()
Default constructor.
Definition: movetype.cpp:701
static const parameters params_
Definition: movetype.hpp:90
int resistance_against(const attack_type &attack) const
Returns the resistance against the indicated attack.
Definition: movetype.cpp:645
~terrain_info()
Destructor.
Definition: movetype.cpp:461
boost::shared_ptr< data > data_
Definition: movetype.hpp:75
void merge(const config &new_data, bool overwrite)
Merges the given config over the existing costs.
Definition: movetype.cpp:664
const resistances & get_resistances() const
Definition: movetype.hpp:191
void write(config &cfg) const
Writes the movement type data to the provided config.
Definition: movetype.cpp:792
const GLfloat * params
Definition: glew.h:1499
void merge(const config &new_cfg, bool overwrite=true)
Merges the given config over the existing data.
Definition: movetype.cpp:754
The basic "size" of the unit - flying, small land, large land, etc.
Definition: movetype.hpp:28
Definitions for the interface to Wesnoth Markup Language (WML).
bool capped(const t_translation::t_terrain &terrain) const
Returns whether there is a defense cap associated to this terrain.
Definition: movetype.hpp:134
utils::string_map damage_table() const
Returns a map from attack types to resistances.
Definition: movetype.cpp:630
resistances(const config &cfg)
Definition: movetype.hpp:157
int movement_cost(const t_translation::t_terrain &terrain, bool slowed=false) const
Returns the cost to move through the indicated terrain.
Definition: movetype.hpp:199
const terrain_defense & get_defense() const
Definition: movetype.hpp:190
terrain_costs & get_jamming()
Definition: movetype.hpp:183
bool has_vision_data() const
Returns whether or not there are any vision-specific costs.
Definition: movetype.hpp:225
GLuint64EXT * result
Definition: glew.h:10727
std::map< std::string, t_string > string_map
terrain_costs(const config &cfg, const terrain_costs *fallback=nullptr, const terrain_costs *cascade=nullptr)
Definition: movetype.hpp:96
const terrain_info *const cascade_
Definition: movetype.hpp:78
terrain_defense & get_defense()
Definition: movetype.hpp:184
terrain_costs & get_vision()
Definition: movetype.hpp:182
static const std::set< std::string > effects
The set of applicable effects for movement types.
Definition: movetype.hpp:233
terrain_costs(const terrain_costs *fallback=nullptr, const terrain_costs *cascade=nullptr)
Definition: movetype.hpp:92
Stores a set of resistances.
Definition: movetype.hpp:153
const terrain_costs & get_vision() const
Definition: movetype.hpp:188
Stores a set of data based on terrain.
Definition: movetype.hpp:31
terrain_costs jamming_
Definition: movetype.hpp:241
bool is_flying() const
Returns whether or not *this is flagged as a flying movement type.
Definition: movetype.hpp:194
static const ::config * terrain
The terrain used to create the cache.
Definition: minimap.cpp:135
void merge(const config &new_data, bool overwrite)
Merges the given config over the existing costs.
Definition: movetype.hpp:138
terrain_costs & get_movement()
Definition: movetype.hpp:181
utils::string_map damage_table() const
Returns a map from attack types to resistances.
Definition: movetype.hpp:219
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:47
terrain_costs movement_
Definition: movetype.hpp:239
void write(config &cfg, const std::string &child_name="") const
Writes our data to a config, as a child if child_name is specified.
Definition: movetype.hpp:142
terrain_info & operator=(const terrain_info &that)
Assignment operator.
Definition: movetype.cpp:472
int value(const t_translation::t_terrain &terrain) const
Returns the value associated with the given terrain.
Definition: movetype.cpp:545
int resistance_against(const attack_type &attack) const
Returns the resistance against the indicated attack.
Definition: movetype.hpp:213
bool flying_
Definition: movetype.hpp:245
static const terrain_info::parameters params_max_
Definition: movetype.hpp:122
terrain_costs vision_
Definition: movetype.hpp:240
const terrain_costs & get_jamming() const
Definition: movetype.hpp:189
int cost(const t_translation::t_terrain &terrain, bool slowed=false) const
Returns the cost associated with the given terrain.
Definition: movetype.hpp:109
void write(config &out_cfg, const std::string &child_name="") const
Writes our data to a config, as a child if child_name is specified.
Definition: movetype.cpp:683
bool empty() const
Returns whether or not our data is empty.
Definition: movetype.cpp:499
resistances resist_
Definition: movetype.hpp:243
bool has_terrain_defense_caps(const std::set< t_translation::t_terrain > &ts) const
Returns whether or not there are any terrain caps with respect to a set of terrains.
Definition: movetype.cpp:742
terrain_defense defense_
Definition: movetype.hpp:242
bool has_jamming_data() const
Returns whether or not there are any jamming-specific costs.
Definition: movetype.hpp:227
The parameters used when calculating a terrain-based value.
Definition: movetype.cpp:59
void make_unique_cascade() const
Ensures our data is not shared, and propagates to our cascade.
Definition: movetype.cpp:599
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
void set_flying(bool flies=true)
Sets whether or not *this is flagged as a flying movement type.
Definition: movetype.hpp:196
Stores a set of defense levels.
Definition: movetype.hpp:119
terrain_info(const parameters &params, const terrain_info *fallback=nullptr, const terrain_info *cascade=nullptr)
Constructor.
Definition: movetype.cpp:405
GLsizei const GLcharARB ** string
Definition: glew.h:4503
int jamming_cost(const t_translation::t_terrain &terrain, bool slowed=false) const
Returns the cost to "jam" through the indicated terrain.
Definition: movetype.hpp:205
static const terrain_info::parameters params_min_
Definition: movetype.hpp:121
void merge(const config &new_values, bool overwrite)
Merges the given config over the existing values.
Definition: movetype.cpp:511
int defense_modifier(const t_translation::t_terrain &terrain) const
Returns the defensive value of the indicated terrain.
Definition: movetype.hpp:209
terrain_costs(const terrain_costs &that, const terrain_costs *fallback=nullptr, const terrain_costs *cascade=nullptr)
Definition: movetype.hpp:101