The Battle for Wesnoth  1.19.19+dev
type_data.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2025
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 #include <memory>
22 
23 class game_config_view;
24 
25 /**
26  * Contains the database of all known terrain types, both those defined
27  * explicitly by WML [terrain_type]s and those made by combining pairs of
28  * (base, overlay).
29  *
30  * A [terrain_type] isn't limited to (only a base) or (only an overlay). For example,
31  * the various impassible mountains Mm^Xm, Ms^Xm, etc are defined by [terrain_type]s
32  * for those specific (base, overlay) pairs.
33  *
34  * Implementation note: the ones defined by WML [terrain_type]'s are loaded
35  * during the first call to lazy_initialization(). Any terrains made by
36  * combining pairs of (base, overlay) are lazy-created during a later call to
37  * find_or_create().
38  *
39  * The is_known() method will trigger creation of the terrain if needed.
40  */
42 private:
44  using tcodeToTerrain_t = std::map<t_translation::terrain_code, terrain_type>;
46  mutable bool initialized_;
48 
49 public:
52 
53  /**
54  * @returns A pointer to the current terrain database instance.
55  */
56  static terrain_type_data* get();
57 
58  /**
59  * Clears the database and queues reinitialization on the next call to
60  * @ref lazy_initialization.
61  */
62  void reset() const;
63 
64  /**
65  * On the first call to this function, parse all of the [terrain_type]s
66  * that are defined in WML. This is separated from the constructor so that
67  * game_config_manager can create an instance while on the title screen,
68  * without the delay of loading the data (and it's likely that a different
69  * config will be loaded before entering the game).
70  */
71  void lazy_initialization() const;
72 
73  const t_translation::ter_list & list() const;
74  const std::map<t_translation::terrain_code, terrain_type> & map() const;
75 
76  /**
77  * Get the corresponding terrain_type information object for a given type
78  * of terrain.
79  *
80  * If the given terrain is not known, and can not be constructed from the
81  * known terrains, returns a default-constructed instance.
82  */
83  const terrain_type& get_terrain_info(const t_translation::terrain_code & terrain) const;
84 
85  /**
86  * Get a formatted terrain name -- terrain (underlying terrains)
87  */
91 
92  bool is_village(const t_translation::terrain_code & terrain) const
93  { return get_terrain_info(terrain).is_village(); }
94  int gives_healing(const t_translation::terrain_code & terrain) const
95  { return get_terrain_info(terrain).gives_healing(); }
96  bool is_castle(const t_translation::terrain_code & terrain) const
97  { return get_terrain_info(terrain).is_castle(); }
98  bool is_keep(const t_translation::terrain_code & terrain) const
99  { return get_terrain_info(terrain).is_keep(); }
100 
101  enum merge_mode {
104  BOTH
105  };
106 
107  /**
108  * Tries to find a new terrain which is the combination of old and new
109  * terrain using the merge_settings. Here "merge" means to find the
110  * best-fitting terrain code, it does not change any already-created
111  * instance of terrain_data. Think of using the editor's
112  * paint-with-one-layer functionality for the purpose of this.
113  *
114  * Relevant parameters are "layer" and "replace_conflicting"
115  * "layer" specifies the layer that should be replaced (base or overlay, default is both).
116  * If "replace_conflicting" is true the new terrain will replace the old one if merging failed
117  * (using the default base if new terrain is an overlay terrain)
118  * Will return the resulting terrain or NONE_TERRAIN if merging failed
119  */
120  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;
121 
122  /**
123  * Returns true if get_terrain_info(terrain) would succeed, or false if
124  * get_terrain_info(terrain) would return a default-constructed instance.
125  *
126  * This has no connection to prefs::get().encountered_terrains().
127  *
128  * Implementation note: if necessary, will trigger the lazy-creation and
129  * add the resulting terrain to the terrain list.
130  */
131  bool is_known(const t_translation::terrain_code & terrain) const;
132 
133 private:
134  tcodeToTerrain_t::const_iterator find_or_create(t_translation::terrain_code) const;
135 
136  static inline terrain_type_data* singleton_{nullptr};
137 };
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:41
void reset() const
Clears the database and queues reinitialization on the next call to lazy_initialization.
Definition: type_data.cpp:50
t_translation::ter_list terrainList_
Definition: type_data.hpp:43
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:57
t_string get_underlying_terrain_string(const t_translation::terrain_code &terrain) const
Definition: type_data.cpp:162
t_string get_terrain_string(const t_translation::terrain_code &terrain) const
Get a formatted terrain name – terrain (underlying terrains)
Definition: type_data.cpp:135
static terrain_type_data * singleton_
Definition: type_data.hpp:136
tcodeToTerrain_t tcodeToTerrain_
Definition: type_data.hpp:45
const game_config_view & game_config_
Definition: type_data.hpp:47
int gives_healing(const t_translation::terrain_code &terrain) const
Definition: type_data.hpp:94
bool is_keep(const t_translation::terrain_code &terrain) const
Definition: type_data.hpp:98
bool is_village(const t_translation::terrain_code &terrain) const
Definition: type_data.hpp:92
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:209
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:217
std::map< t_translation::terrain_code, terrain_type > tcodeToTerrain_t
Definition: type_data.hpp:44
static terrain_type_data * get()
Definition: type_data.cpp:28
bool is_castle(const t_translation::terrain_code &terrain) const
Definition: type_data.hpp:96
const std::map< t_translation::terrain_code, terrain_type > & map() const
Definition: type_data.cpp:117
tcodeToTerrain_t::const_iterator find_or_create(t_translation::terrain_code) const
Definition: type_data.cpp:182
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:123
t_string get_terrain_editor_string(const t_translation::terrain_code &terrain) const
Definition: type_data.cpp:145
terrain_type_data(const game_config_view &game_config)
Definition: type_data.cpp:34
const t_translation::ter_list & list() const
Definition: type_data.cpp:110
bool is_keep() const
Definition: terrain.hpp:153
bool is_castle() const
Definition: terrain.hpp:152
bool is_village() const
Definition: terrain.hpp:151
int gives_healing() const
Definition: terrain.hpp:150
Game configuration data as global variables.
Definition: build_info.cpp:61
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