The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
type_data.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Chris Beck <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #include "terrain/type_data.hpp"
16 
17 #include "global.hpp"
18 
19 #include "terrain/terrain.hpp"
20 
21 #include <map>
22 
24  : terrainList_()
25  , tcodeToTerrain_()
26  , initialized_(false)
27  , game_config_(game_config)
28 {
29 }
30 
32 {
33  if (!initialized_) {
35  initialized_ = true;
36  }
37 
38  return terrainList_;
39 }
40 
41 
42 const std::map<t_translation::t_terrain, terrain_type> & terrain_type_data::map() const
43 {
44  if (!initialized_) {
46  initialized_ = true;
47  }
48 
49  return tcodeToTerrain_;
50 }
51 
52 
54 {
55  static const terrain_type default_terrain;
56  const std::map<t_translation::t_terrain,terrain_type>::const_iterator i =
57  tcodeToTerrain_.find(terrain);
58 
59  if(i != tcodeToTerrain_.end())
60  return i->second;
61  else
62  return default_terrain;
63 }
64 
66 {
67  const std::map<t_translation::t_terrain,terrain_type>::const_iterator i =
68  tcodeToTerrain_.find(terrain);
69 
70  if(i == tcodeToTerrain_.end()) {
71  static t_translation::t_list result(1);
72  result[0] = terrain;
73  return result;
74  } else {
75  return i->second.mvt_type();
76  }
77 }
78 
80 {
81  const std::map<t_translation::t_terrain, terrain_type>::const_iterator i =
82  tcodeToTerrain_.find(terrain);
83 
84  if(i == tcodeToTerrain_.end()) {
85  static t_translation::t_list result(1);
86  result[0] = terrain;
87  return result;
88  } else {
89  return i->second.def_type();
90  }
91 }
92 
94 {
95  const std::map<t_translation::t_terrain,terrain_type>::const_iterator i =
96  tcodeToTerrain_.find(terrain);
97 
98  if(i == tcodeToTerrain_.end()) {
99  static t_translation::t_list result(1);
100  result[0] = terrain;
101  return result;
102  } else {
103  return i->second.union_type();
104  }
105 }
106 
107 
108 
110 {
111  std::string str =
112  get_terrain_info(terrain).description();
113 
114  str += get_underlying_terrain_string(terrain);
115 
116  return str;
117 }
118 
120 {
121  std::string str =
122  get_terrain_info(terrain).editor_name();
123  const std::string desc =
124  get_terrain_info(terrain).description();
125 
126  if(str != desc) {
127  str += "/";
128  str += desc;
129  }
130 
131  str += get_underlying_terrain_string(terrain);
132 
133  return str;
134 }
135 
137 {
138  std::string str;
139 
140  const t_translation::t_list& underlying = underlying_union_terrain(terrain);
141  assert(!underlying.empty());
142 
143  if(underlying.size() > 1 || underlying[0] != terrain) {
144  str += " (";
145  t_translation::t_list::const_iterator i = underlying.begin();
146  str += get_terrain_info(*i).name();
147  while (++i != underlying.end()) {
148  str += ", " + get_terrain_info(*i).name();
149  }
150  str += ")";
151  }
152 
153  return str;
154 }
155 
157 
158  if(tcodeToTerrain_.count(terrain) == 0) {
159  const std::map<t_translation::t_terrain, terrain_type>::const_iterator base_iter =
161  const std::map<t_translation::t_terrain, terrain_type>::const_iterator overlay_iter =
163 
164  if(base_iter == tcodeToTerrain_.end() || overlay_iter == tcodeToTerrain_.end()) {
165  return false;
166  }
167 
168  terrain_type new_terrain(base_iter->second, overlay_iter->second);
169  terrainList_.push_back(new_terrain.number());
170  tcodeToTerrain_.insert(std::pair<t_translation::t_terrain, terrain_type>(
171  new_terrain.number(), new_terrain));
172  return true;
173  }
174  return true; // Terrain already exists, nothing to do
175 }
176 
179 
180  if(mode == OVERLAY) {
182  if (try_merge_terrains(t)) {
183  result = t;
184  }
185  }
186  else if(mode == BASE) {
188  if (try_merge_terrains(t)) {
189  result = t;
190  }
191  }
192  else if(mode == BOTH && new_t.base != t_translation::NO_LAYER) {
193  // We need to merge here, too, because the dest terrain might be a combined one.
194  if (try_merge_terrains(new_t)) {
195  result = new_t;
196  }
197  }
198 
199  // if merging of overlay and base failed, and replace_if_failed is set,
200  // replace the terrain with the complete new terrain (if given)
201  // or with (default base)^(new overlay)
202  if(result == t_translation::NONE_TERRAIN && replace_if_failed && tcodeToTerrain_.count(new_t) > 0) {
203  if(new_t.base != t_translation::NO_LAYER) {
204  // Same as above
205  if (try_merge_terrains(new_t)) {
206  result = new_t;
207  }
208  }
211  }
212  }
213  return result;
214 }
child_itors child_range(const std::string &key)
Definition: config.cpp:613
const t_translation::t_list & underlying_union_terrain(const t_translation::t_terrain &terrain) const
Definition: type_data.cpp:93
bool initialized_
Definition: font.cpp:609
const t_terrain NONE_TERRAIN
Definition: translation.hpp:56
t_translation::t_terrain merge_terrains(const t_translation::t_terrain &old_t, const t_translation::t_terrain &new_t, const tmerge_mode mode, bool replace_if_failed=false)
Tries to merge old and new terrain using the merge_settings config Relevant parameters are "layer" an...
Definition: type_data.cpp:177
const t_string & name() const
Definition: terrain.hpp:33
GLenum mode
Definition: glew.h:2390
GLdouble GLdouble t
Definition: glew.h:1366
const t_string & editor_name() const
Definition: terrain.hpp:34
GLuint64EXT * result
Definition: glew.h:10727
t_translation::t_terrain number() const
Definition: terrain.hpp:43
const terrain_type & get_terrain_info(const t_translation::t_terrain &terrain) const
Get the corresponding terrain_type information object for a given type of terrain.
Definition: type_data.cpp:53
bool try_merge_terrains(const t_translation::t_terrain &terrain)
Tries to find out if "terrain" can be created by combining two existing terrains Will add the resulti...
Definition: type_data.cpp:156
t_translation::t_terrain default_base() const
Definition: terrain.hpp:79
std::string default_terrain
Definition: game_config.cpp:49
std::string get_terrain_editor_string(const t_translation::t_terrain &terrain) const
Definition: type_data.cpp:119
static const ::config * terrain
The terrain used to create the cache.
Definition: minimap.cpp:135
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:47
const std::map< t_translation::t_terrain, terrain_type > & map() const
Definition: type_data.cpp:42
const t_translation::t_list & underlying_def_terrain(const t_translation::t_terrain &terrain) const
Definition: type_data.cpp:79
const t_translation::t_list & list() const
Definition: type_data.cpp:31
Game configuration data as global variables.
Definition: build_info.cpp:38
std::string get_terrain_string(const t_translation::t_terrain &terrain) const
Get a formatted terrain name – terrain (underlying, terrains)
Definition: type_data.cpp:109
size_t i
Definition: function.cpp:1057
t_translation::t_terrain terrain_with_default_base() const
Definition: terrain.cpp:285
const t_layer NO_LAYER
Definition: translation.hpp:38
std::string get_underlying_terrain_string(const t_translation::t_terrain &terrain) const
Definition: type_data.cpp:136
t_translation::t_list terrainList_
Definition: type_data.hpp:25
void create_terrain_maps(const config::const_child_itors &cfgs, t_translation::t_list &terrain_list, std::map< t_translation::t_terrain, terrain_type > &letter_to_terrain)
Definition: terrain.cpp:316
terrain_type_data(const config &game_config)
Definition: type_data.cpp:23
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
const t_string & description() const
Definition: terrain.hpp:35
std::map< t_translation::t_terrain, terrain_type > tcodeToTerrain_
Definition: type_data.hpp:26
const config & game_config_
Definition: type_data.hpp:29
GLsizei const GLcharARB ** string
Definition: glew.h:4503
std::vector< t_terrain > t_list
Definition: translation.hpp:75
const t_translation::t_list & underlying_mvt_terrain(const t_translation::t_terrain &terrain) const
Definition: type_data.cpp:65