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