The Battle for Wesnoth  1.19.19+dev
terrain.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2025
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 "config.hpp"
19 #include "terrain/translation.hpp"
20 #include "utils/math.hpp"
21 
23 {
24 public:
25 
26  /**
27  * Creates an instance for which is_nonnull() returns false. Used for the
28  * sentinel value when terrain_type_data::get_terrain_info() is called for
29  * an unknown terrain code.
30  */
31  explicit terrain_type();
32 
33  /**
34  * Constructor for terrains defined by a WML [terrain_type] tag.
35  */
36  explicit terrain_type(const config& cfg);
37 
38  /**
39  * Used when a terrain code which hasn't been defined with a [terrain_type]
40  * tag is used in a map, to build the terrain from already-defined terrains.
41  */
42  explicit terrain_type(const terrain_type& base, const terrain_type& overlay);
43 
44  const std::string& icon_image() const { return icon_image_; }
45  const std::string& minimap_image() const { return minimap_image_; }
46  const std::string& minimap_image_overlay() const { return minimap_image_overlay_; }
47  const std::string& editor_image() const { return editor_image_; }
48  const t_string& name() const { return name_; }
49  const t_string& editor_name() const { return editor_name_.empty() ? description() : editor_name_; }
50  const t_string& description() const { return description_.empty() ? name_ : description_; }
51  const t_string& help_topic_text() const { return help_topic_text_; }
52  const std::string& id() const { return id_; }
53 
54  /**
55  * For instances created from a [terrain_type] tag, the value in the tag
56  * (with default false).
57  *
58  * Always true for instances generated by the terrain_type(base, overlay)
59  * constructor.
60  */
61  bool hide_help() const { return hide_help_; }
62  bool hide_in_editor() const { return hide_in_editor_; }
63  bool hide_if_impassable() const { return hide_if_impassable_; }
64 
65  //the character representing this terrain
67 
68  /**
69  * The underlying movement type of the terrain.
70  *
71  * The underlying terrain is the name of the terrain for game-logic purposes.
72  * I.e. if the terrain is simply an alias, the underlying terrain name
73  * is the name of the terrain(s) that it's aliased to.
74  *
75  * Whether "underlying" means "only the types used in [movetype]" is determined
76  * by the terrain.cfg file, rather than the .cpp code - in 1.14, the terrain.cfg
77  * file uses only the [movetype] terrains in its alias lists.
78  *
79  * This may start with a t_translation::PLUS or t_translation::MINUS to
80  * indicate whether the movement should be calculated as a best-of or
81  * worst-of combination. These may also occur later in the list, however if
82  * both PLUS and MINUS appear in the list then the values calculated are
83  * implementation defined behavior.
84  */
85  const t_translation::ter_list& mvt_type() const { return mvt_type_; }
86  const t_translation::ter_list& def_type() const { return def_type_; }
87  const t_translation::ter_list& union_type() const { return union_type_; }
88 
89  /**
90  * Returns true if a terrain has no underlying types other than itself,
91  * in respect of either union, movement or defense.
92  *
93  * If this returns false, then @a underlying must be non-empty.
94  *
95  * This function is to encapsulate the logic of whether such a terrain
96  * is represented by a empty list, or whether it's represented by a list
97  * including only itself; so that a refactor can switch between these two
98  * possible implementations.
99  *
100  * This is not related to whether the terrain has an overlay. For example,
101  * Gg^Uf (flat with old mushrooms) is indivisible (it's only Tt), although
102  * Gg^Tf (flat with new mushrooms) can be divided (in to Gt and Tt).
103  *
104  * @param id the terrain
105  * @param underlying the corresponding mvt_type(), def_type() or union_type()
106  */
108  return (underlying.empty()
109  || (underlying.size() == 1 && underlying.front() == id));
110  }
111 
112  /**
113  * Returns true if this terrain has no underlying types other than itself.
114  *
115  * \todo what about a terrain where is_mvt_indivisible() != is_def_indivisible()?
116  */
117  bool is_indivisible() const {
118  return (union_type_.empty()
119  || (union_type_.size() == 1 && union_type_.front() == number_));
120  }
121  bool is_mvt_indivisible() const {
122  return (mvt_type_.empty()
123  || (mvt_type_.size() == 1 && mvt_type_.front() == number_));
124  }
125 
126  /**
127  * True if this object represents some sentinel values.
128  *
129  * \todo number_ should never be NONE_TERRAIN
130  * \todo there's two different VOID_TERRAINS - see the comment attached to
131  * the definition of VOID_TERRAIN.
132  *
133  * \todo unclear what this should mean, so replace it with a clearly-named
134  * successor.
135  */
136  bool is_nonnull() const {
139  }
140 
141  /** Returns the light (lawful) bonus for this terrain when the time of day gives a @a base bonus. */
142  int light_bonus(int base) const
143  {
145  }
146 
147  int unit_height_adjust() const { return height_adjust_; }
148  double unit_submerge() const { return submerge_; }
149 
150  int gives_healing() const { return heals_; }
151  bool is_village() const { return village_; }
152  bool is_castle() const { return castle_; }
153  bool is_keep() const { return keep_; }
154 
155  //these descriptions are shown for the terrain in the mouse over
156  //depending on the owner or the village
161 
162  const std::string& editor_group() const { return editor_group_; }
163  void set_editor_group(const std::string& str) { editor_group_ = str; }
164 
165  bool is_overlay() const { return overlay_; }
166 
167  /**
168  * True for instances created by the terrain_code(base, overlay) constructor.
169  *
170  * False for instances created by the terrain_type(config) constructor,
171  * implying that they were created directly from a WML [terrain_type] tag.
172  *
173  * Note that this returns false for terrains such as Dd^Dc (crater) and
174  * Mm^Xm (regular impassible mountains), because there are [terrain_type]
175  * tags for those specific combinations of base^overlay.
176  */
177  bool is_combined() const { return combined_; }
178 
179  /**
180  * Overlay terrains defined by a [terrain_type] can declare a fallback base
181  * terrain, for use when the overlay is selected in the editor, or when the
182  * overlay is placed on the map using [terrain]replace_if_failed=true.
183  *
184  * If there's no default, returns a sentinel value; see has_default_base().
185  */
188 
189  /**
190  * Return the overlay part of this terrain, on the default_base(). Might
191  * return an unknown terrain, if there's a typo in the default base.
192  *
193  * If this terrain has no overlay, it returns the terrain itself, ignoring
194  * the default_base() even if the terrain has a default_base().
195  *
196  * This is intended for the editor's single-layer placement, or for
197  * replacing terrains via ActionWML, where the user or WML author intends
198  * to only use one layer of the current terrain.
199  */
201 
202  /**
203  * Returns true if most of the data matches. The ones that don't need to match:
204  * - editor_group_
205  * - icon_image_
206  * - description_
207  * - help_topic_text_
208  * - income_description_ , ~ally_ , ~enemy_, ~own_
209  * - hide_if_impassable_
210  *
211  * The intention seems to be to allow additional [terrain_type] tags to add
212  * compatible definitions to multiple addon-specific editor groups. For
213  * this purpose the description strings aren't too important, and
214  * hide_if_impassable_ seems trivial.
215  */
216  bool operator==(const terrain_type& other) const;
217 private:
218 
219  /** The image used as symbol icon */
220  std::string icon_image_;
221 
222  /** The image used in the minimap */
223  std::string minimap_image_;
225 
226  /**
227  * The image used in the editor palette if not defined in WML it will be
228  * initialized with the value of minimap_image_
229  */
230  std::string editor_image_;
231  std::string id_;
236 
237  //the 'number' is the number that represents this
238  //terrain type. The 'type' is a list of the 'underlying types'
239  //of the terrain. This may simply be the same as the number.
240  //This is the internal number used, WML still uses character strings.
245 
246  double submerge_;
248 
251 
255  int heals_;
256 
261 
262  std::string editor_group_;
264 
265  bool village_;
266  bool castle_;
267  bool keep_;
268  bool overlay_;
269  bool combined_;
273 };
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:157
bool empty() const
Definition: tstring.hpp:199
const std::string & editor_group() const
Definition: terrain.hpp:162
bool has_default_base() const
Definition: terrain.hpp:187
bool castle_
Definition: terrain.hpp:266
bool submerge_set_
Definition: terrain.hpp:249
int light_modification_
Definition: terrain.hpp:252
bool hide_help() const
For instances created from a [terrain_type] tag, the value in the tag (with default false).
Definition: terrain.hpp:61
const t_string & income_description_enemy() const
Definition: terrain.hpp:159
std::string id_
Definition: terrain.hpp:231
const std::string & minimap_image() const
Definition: terrain.hpp:45
const std::string & icon_image() const
Definition: terrain.hpp:44
t_string income_description_
Definition: terrain.hpp:257
const t_string & income_description() const
Definition: terrain.hpp:157
const t_string & name() const
Definition: terrain.hpp:48
bool overlay_
Definition: terrain.hpp:268
std::string editor_image_
The image used in the editor palette if not defined in WML it will be initialized with the value of m...
Definition: terrain.hpp:230
t_translation::terrain_code editor_default_base_
Definition: terrain.hpp:263
bool is_combined() const
True for instances created by the terrain_code(base, overlay) constructor.
Definition: terrain.hpp:177
int unit_height_adjust() const
Definition: terrain.hpp:147
t_translation::terrain_code terrain_with_default_base() const
Return the overlay part of this terrain, on the default_base().
Definition: terrain.cpp:269
int height_adjust_
Definition: terrain.hpp:247
const std::string & editor_image() const
Definition: terrain.hpp:47
t_translation::ter_list mvt_type_
Definition: terrain.hpp:242
bool is_nonnull() const
True if this object represents some sentinel values.
Definition: terrain.hpp:136
bool is_keep() const
Definition: terrain.hpp:153
t_string editor_name_
Definition: terrain.hpp:233
bool is_castle() const
Definition: terrain.hpp:152
std::string minimap_image_overlay_
Definition: terrain.hpp:224
int max_light_
Definition: terrain.hpp:253
const t_string & income_description_ally() const
Definition: terrain.hpp:158
t_string help_topic_text_
Definition: terrain.hpp:235
t_translation::terrain_code number_
Definition: terrain.hpp:241
t_string income_description_ally_
Definition: terrain.hpp:258
std::string icon_image_
The image used as symbol icon.
Definition: terrain.hpp:220
const std::string & id() const
Definition: terrain.hpp:52
bool is_mvt_indivisible() const
Definition: terrain.hpp:121
t_translation::ter_list union_type_
Definition: terrain.hpp:244
bool combined_
Definition: terrain.hpp:269
bool hide_help_
Definition: terrain.hpp:270
t_translation::ter_list def_type_
Definition: terrain.hpp:243
t_string description_
Definition: terrain.hpp:234
void set_editor_group(const std::string &str)
Definition: terrain.hpp:163
const t_string & description() const
Definition: terrain.hpp:50
std::string minimap_image_
The image used in the minimap.
Definition: terrain.hpp:223
terrain_type()
Creates an instance for which is_nonnull() returns false.
Definition: terrain.cpp:34
const t_string & help_topic_text() const
Definition: terrain.hpp:51
std::string editor_group_
Definition: terrain.hpp:262
bool is_village() const
Definition: terrain.hpp:151
const t_translation::ter_list & def_type() const
Definition: terrain.hpp:86
const t_translation::ter_list & mvt_type() const
The underlying movement type of the terrain.
Definition: terrain.hpp:85
double submerge_
Definition: terrain.hpp:246
double unit_submerge() const
Definition: terrain.hpp:148
int light_bonus(int base) const
Returns the light (lawful) bonus for this terrain when the time of day gives a base bonus.
Definition: terrain.hpp:142
t_string income_description_own_
Definition: terrain.hpp:260
bool hide_if_impassable() const
Definition: terrain.hpp:63
const t_translation::ter_list & union_type() const
Definition: terrain.hpp:87
int min_light_
Definition: terrain.hpp:254
bool is_overlay() const
Definition: terrain.hpp:165
bool hide_if_impassable_
Definition: terrain.hpp:272
bool village_
Definition: terrain.hpp:265
bool hide_in_editor_
Definition: terrain.hpp:271
static bool is_indivisible(t_translation::terrain_code id, const t_translation::ter_list &underlying)
Returns true if a terrain has no underlying types other than itself, in respect of either union,...
Definition: terrain.hpp:107
const t_string & editor_name() const
Definition: terrain.hpp:49
int gives_healing() const
Definition: terrain.hpp:150
t_translation::terrain_code default_base() const
Overlay terrains defined by a [terrain_type] can declare a fallback base terrain, for use when the ov...
Definition: terrain.hpp:186
t_string name_
Definition: terrain.hpp:232
const t_string & income_description_own() const
Definition: terrain.hpp:160
bool height_adjust_set_
Definition: terrain.hpp:250
bool hide_in_editor() const
Definition: terrain.hpp:62
t_string income_description_enemy_
Definition: terrain.hpp:259
const std::string & minimap_image_overlay() const
Definition: terrain.hpp:46
bool is_indivisible() const
Returns true if this terrain has no underlying types other than itself.
Definition: terrain.hpp:117
t_translation::terrain_code number() const
Definition: terrain.hpp:66
bool operator==(const terrain_type &other) const
Returns true if most of the data matches.
Definition: terrain.cpp:276
Definitions for the interface to Wesnoth Markup Language (WML).
const config * cfg
General math utility functions.
constexpr int bounded_add(int base, int increment, int max_sum, int min_sum=0)
Returns base + increment, but will not increase base above max_sum, nor decrease it below min_sum.
Definition: math.hpp:46
constexpr terrain_code NONE_TERRAIN
Definition: translation.hpp:58
const terrain_code VOID_TERRAIN
VOID_TERRAIN is used for shrouded hexes.
std::vector< terrain_code > ter_list
Definition: translation.hpp:77
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:49