The Battle for Wesnoth  1.15.0-dev
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 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 #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  auto i = find_or_merge(terrain);
54 
55  if(i != tcodeToTerrain_.end()) {
56  return i->second;
57  } else {
58  static const terrain_type default_terrain;
59  return default_terrain;
60  }
61 }
62 
64 {
65  auto i = find_or_merge(terrain);
66 
67  if(i == tcodeToTerrain_.end()) {
68  // TODO: At least in some cases (for example when this is called from lua) it
69  // seems to make more sense to throw an exception here, same goes for get_terrain_info
70  // and underlying_def_terrain
71  static t_translation::ter_list result(1);
72  result[0] = terrain;
73  return result;
74  } else {
75  return i->second.mvt_type();
76  }
77 }
78 
80 {
81  auto i = find_or_merge(terrain);
82 
83  if(i == tcodeToTerrain_.end()) {
84  static t_translation::ter_list result(1);
85  result[0] = terrain;
86  return result;
87  } else {
88  return i->second.def_type();
89  }
90 }
91 
93 {
94  const std::map<t_translation::terrain_code,terrain_type>::const_iterator i =
95  tcodeToTerrain_.find(terrain);
96 
97  if(i == tcodeToTerrain_.end()) {
98  static t_translation::ter_list result(1);
99  result[0] = terrain;
100  return result;
101  } else {
102  return i->second.union_type();
103  }
104 }
105 
106 
107 
109 {
110  std::string str =
111  get_terrain_info(terrain).description();
112 
113  str += get_underlying_terrain_string(terrain);
114 
115  return str;
116 }
117 
119 {
120  std::string str =
121  get_terrain_info(terrain).editor_name();
122  const std::string desc =
123  get_terrain_info(terrain).description();
124 
125  if(str != desc) {
126  str += "/";
127  str += desc;
128  }
129 
130  str += get_underlying_terrain_string(terrain);
131 
132  return str;
133 }
134 
136 {
137  std::string str;
138 
139  const t_translation::ter_list& underlying = underlying_union_terrain(terrain);
140  assert(!underlying.empty());
141 
142  if(underlying.size() > 1 || underlying[0] != terrain) {
143  str += " (";
144  t_translation::ter_list::const_iterator i = underlying.begin();
145  str += get_terrain_info(*i).name();
146  while (++i != underlying.end()) {
147  str += ", " + get_terrain_info(*i).name();
148  }
149  str += ")";
150  }
151 
152  return str;
153 }
154 
155 terrain_type_data::tcodeToTerrain_t::const_iterator terrain_type_data::find_or_merge(t_translation::terrain_code terrain) const
156 {
157  auto i = tcodeToTerrain_.find(terrain);
158  if (i != tcodeToTerrain_.end()) {
159  return i;
160  }
161  else {
164 
165  if(base_iter == tcodeToTerrain_.end() || overlay_iter == tcodeToTerrain_.end()) {
166  return tcodeToTerrain_.end();
167  }
168 
169  terrain_type new_terrain(base_iter->second, overlay_iter->second);
170  terrainList_.push_back(new_terrain.number());
171  return tcodeToTerrain_.emplace(new_terrain.number(), std::move(new_terrain)).first;
172  }
173 }
174 
176 {
177  return find_or_merge(terrain) != tcodeToTerrain_.end();
178 }
179 
182 
183  if(mode == OVERLAY) {
185  if (try_merge_terrains(t)) {
186  result = t;
187  }
188  }
189  else if(mode == BASE) {
191  if (try_merge_terrains(t)) {
192  result = t;
193  }
194  }
195  else if(mode == BOTH && new_t.base != t_translation::NO_LAYER) {
196  // We need to merge here, too, because the dest terrain might be a combined one.
197  if (try_merge_terrains(new_t)) {
198  result = new_t;
199  }
200  }
201 
202  // if merging of overlay and base failed, and replace_if_failed is set,
203  // replace the terrain with the complete new terrain (if given)
204  // or with (default base)^(new overlay)
205  if(result == t_translation::NONE_TERRAIN && replace_if_failed && tcodeToTerrain_.count(new_t) > 0) {
206  if(new_t.base != t_translation::NO_LAYER) {
207  // Same as above
208  if (try_merge_terrains(new_t)) {
209  result = new_t;
210  }
211  }
214  }
215  }
216  return result;
217 }
tcodeToTerrain_t::const_iterator find_or_merge(t_translation::terrain_code) const
Definition: type_data.cpp:155
const terrain_code NONE_TERRAIN
Definition: translation.hpp:59
const t_string & description() const
Definition: terrain.hpp:35
t_translation::ter_list terrainList_
Definition: type_data.hpp:23
child_itors child_range(config_key_type key)
Definition: config.cpp:366
std::string get_terrain_editor_string(const t_translation::terrain_code &terrain) const
Definition: type_data.cpp:118
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
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 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
const ter_layer NO_LAYER
Definition: translation.hpp:41
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:180
const std::map< t_translation::terrain_code, terrain_type > & map() const
Definition: type_data.cpp:40
const t_translation::ter_list & underlying_mvt_terrain(const t_translation::terrain_code &terrain) const
Definition: type_data.cpp:63
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 & list() const
Definition: type_data.cpp:29
const t_translation::ter_list & underlying_def_terrain(const t_translation::terrain_code &terrain) const
Definition: type_data.cpp:79
std::string get_underlying_terrain_string(const t_translation::terrain_code &terrain) const
Definition: type_data.cpp:135
std::string get_terrain_string(const t_translation::terrain_code &terrain) const
Get a formatted terrain name – terrain (underlying, terrains)
Definition: type_data.cpp:108
std::string default_terrain
Definition: game_config.cpp:69
std::size_t i
Definition: function.cpp:933
Game configuration data as global variables.
Definition: build_info.cpp:46
t_translation::terrain_code terrain_with_default_base() const
Definition: terrain.cpp:285
t_translation::terrain_code number() const
Definition: terrain.hpp:44
const t_translation::ter_list & underlying_union_terrain(const t_translation::terrain_code &terrain) const
Definition: type_data.cpp:92
const t_string & name() const
Definition: terrain.hpp:33
double t
Definition: astarsearch.cpp:63
std::vector< terrain_code > ter_list
Definition: translation.hpp:78
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:68
tcodeToTerrain_t tcodeToTerrain_
Definition: type_data.hpp:25
const config & game_config_
Definition: type_data.hpp:28
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:175