The Battle for Wesnoth  1.19.0-dev
type_data.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2024
3  by Chris Beck <render787@gmail.com>
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 "terrain/terrain.hpp"
19 
20 #include <map>
21 
22 class game_config_view;
23 
24 /**
25  * Contains the database of all known terrain types, both those defined
26  * explicitly by WML [terrain_type]s and those made by combining pairs of
27  * (base, overlay).
28  *
29  * A [terrain_type] isn't limited to (only a base) or (only an overlay). For example,
30  * the various impassible mountains Mm^Xm, Ms^Xm, etc are defined by [terrain_type]s
31  * for those specific (base, overlay) pairs.
32  *
33  * Implementation note: the ones defined by WML [terrain_type]'s are loaded
34  * during the first call to lazy_initialization(). Any terrains made by
35  * combining pairs of (base, overlay) are lazy-created during a later call to
36  * find_or_create().
37  *
38  * The is_known() method will trigger creation of the terrain if needed.
39  */
41 private:
43  using tcodeToTerrain_t = std::map<t_translation::terrain_code, terrain_type>;
45  mutable bool initialized_;
47 
48 public:
50 
51  /**
52  * On the first call to this function, parse all of the [terrain_type]s
53  * that are defined in WML. This is separated from the constructor so that
54  * game_config_manager can create an instance while on the title screen,
55  * without the delay of loading the data (and it's likely that a different
56  * config will be loaded before entering the game).
57  */
58  void lazy_initialization() const;
59 
60  const t_translation::ter_list & list() const;
61  const std::map<t_translation::terrain_code, terrain_type> & map() const;
62 
63  /**
64  * Get the corresponding terrain_type information object for a given type
65  * of terrain.
66  *
67  * If the given terrain is not known, and can not be constructed from the
68  * known terrains, returns a default-constructed instance.
69  */
70  const terrain_type& get_terrain_info(const t_translation::terrain_code & terrain) const;
71 
72  /**
73  * The underlying movement type of the terrain.
74  *
75  * The underlying terrain is the name of the terrain for game-logic purposes.
76  * I.e. if the terrain is simply an alias, the underlying terrain name
77  * is the name of the terrain(s) that it's aliased to.
78  *
79  * Whether "underlying" means "only the types used in [movetype]" is determined
80  * by the terrain.cfg file, rather than the .cpp code - in 1.14, the terrain.cfg
81  * file uses only the [movetype] terrains in its alias lists.
82  *
83  * This may start with a t_translation::PLUS or t_translation::MINUS to
84  * indicate whether the movement should be calculated as a best-of or
85  * worst-of combination. These may also occur later in the list, however if
86  * both PLUS and MINUS appear in the list then the values calculated are
87  * implementation defined behavior.
88  */
90  /**
91  * The underlying defense type of the terrain. See the notes for underlying_mvt_terrain.
92  */
94  /**
95  * Unordered set of all terrains used in either underlying_mvt_terrain or
96  * underlying_def_terrain. This does not include any PLUSes or MINUSes.
97  *
98  * May also include the aliasof and vision_alias terrains, however
99  * vision_alias is deprecated and aliasof should probably match the
100  * movement and defense terrains.
101  */
103  /**
104  * Get a formatted terrain name -- terrain (underlying terrains)
105  */
109 
110  bool is_village(const t_translation::terrain_code & terrain) const
111  { return get_terrain_info(terrain).is_village(); }
112  int gives_healing(const t_translation::terrain_code & terrain) const
113  { return get_terrain_info(terrain).gives_healing(); }
114  bool is_castle(const t_translation::terrain_code & terrain) const
115  { return get_terrain_info(terrain).is_castle(); }
116  bool is_keep(const t_translation::terrain_code & terrain) const
117  { return get_terrain_info(terrain).is_keep(); }
118 
119  enum merge_mode {
122  BOTH
123  };
124 
125  /**
126  * Tries to find a new terrain which is the combination of old and new
127  * terrain using the merge_settings. Here "merge" means to find the
128  * best-fitting terrain code, it does not change any already-created
129  * instance of terrain_data. Think of using the editor's
130  * paint-with-one-layer functionality for the purpose of this.
131  *
132  * Relevant parameters are "layer" and "replace_conflicting"
133  * "layer" specifies the layer that should be replaced (base or overlay, default is both).
134  * If "replace_conflicting" is true the new terrain will replace the old one if merging failed
135  * (using the default base if new terrain is an overlay terrain)
136  * Will return the resulting terrain or NONE_TERRAIN if merging failed
137  */
138  t_translation::terrain_code merge_terrains(const t_translation::terrain_code & old_t, const t_translation::terrain_code & new_t, const merge_mode mode, bool replace_if_failed = false) const;
139 
140  /**
141  * Returns true if get_terrain_info(terrain) would succeed, or false if
142  * get_terrain_info(terrain) would return a default-constructed instance.
143  *
144  * This has no connection to preferences::encountered_terrains().
145  *
146  * Implementation note: if necessary, will trigger the lazy-creation and
147  * add the resulting terrain to the terrain list.
148  */
149  bool is_known(const t_translation::terrain_code & terrain) const;
150 
151 private:
152  tcodeToTerrain_t::const_iterator find_or_create(t_translation::terrain_code) const;
153 };
A class grating read only view to a vector of config objects, viewed as one config with all children ...
Contains the database of all known terrain types, both those defined explicitly by WML [terrain_type]...
Definition: type_data.hpp:40
t_translation::ter_list terrainList_
Definition: type_data.hpp:42
void lazy_initialization() const
On the first call to this function, parse all of the [terrain_type]s that are defined in WML.
Definition: type_data.cpp:36
t_string get_underlying_terrain_string(const t_translation::terrain_code &terrain) const
Definition: type_data.cpp:185
const t_translation::ter_list & underlying_def_terrain(const t_translation::terrain_code &terrain) const
The underlying defense type of the terrain.
Definition: type_data.cpp:130
t_string get_terrain_string(const t_translation::terrain_code &terrain) const
Get a formatted terrain name – terrain (underlying terrains)
Definition: type_data.cpp:158
tcodeToTerrain_t tcodeToTerrain_
Definition: type_data.hpp:44
const game_config_view & game_config_
Definition: type_data.hpp:46
int gives_healing(const t_translation::terrain_code &terrain) const
Definition: type_data.hpp:112
bool is_keep(const t_translation::terrain_code &terrain) const
Definition: type_data.hpp:116
bool is_village(const t_translation::terrain_code &terrain) const
Definition: type_data.hpp:110
bool is_known(const t_translation::terrain_code &terrain) const
Returns true if get_terrain_info(terrain) would succeed, or false if get_terrain_info(terrain) would ...
Definition: type_data.cpp:233
const t_translation::ter_list & underlying_mvt_terrain(const t_translation::terrain_code &terrain) const
The underlying movement type of the terrain.
Definition: type_data.cpp:114
t_translation::terrain_code merge_terrains(const t_translation::terrain_code &old_t, const t_translation::terrain_code &new_t, const merge_mode mode, bool replace_if_failed=false) const
Tries to find a new terrain which is the combination of old and new terrain using the merge_settings.
Definition: type_data.cpp:241
std::map< t_translation::terrain_code, terrain_type > tcodeToTerrain_t
Definition: type_data.hpp:43
bool is_castle(const t_translation::terrain_code &terrain) const
Definition: type_data.hpp:114
const std::map< t_translation::terrain_code, terrain_type > & map() const
Definition: type_data.cpp:96
tcodeToTerrain_t::const_iterator find_or_create(t_translation::terrain_code) const
Definition: type_data.cpp:206
const terrain_type & get_terrain_info(const t_translation::terrain_code &terrain) const
Get the corresponding terrain_type information object for a given type of terrain.
Definition: type_data.cpp:102
const t_translation::ter_list & underlying_union_terrain(const t_translation::terrain_code &terrain) const
Unordered set of all terrains used in either underlying_mvt_terrain or underlying_def_terrain.
Definition: type_data.cpp:143
t_string get_terrain_editor_string(const t_translation::terrain_code &terrain) const
Definition: type_data.cpp:168
terrain_type_data(const game_config_view &game_config)
Definition: type_data.cpp:28
const t_translation::ter_list & list() const
Definition: type_data.cpp:89
bool is_keep() const
Definition: terrain.hpp:143
bool is_castle() const
Definition: terrain.hpp:142
bool is_village() const
Definition: terrain.hpp:141
int gives_healing() const
Definition: terrain.hpp:140
Game configuration data as global variables.
Definition: build_info.cpp:60
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