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