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