The Battle for Wesnoth  1.19.18+dev
map.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2025
3  by David White <dave@whitevine.net>
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 class config;
19 
20 #include "map/location.hpp"
21 #include "terrain/translation.hpp"
22 #include "terrain/type_data.hpp"
23 #include "utils/optional_fwd.hpp"
24 
25 //class terrain_type_data; Can't forward declare because of enum
26 
27 // This could be moved to a separate file pair...
29 {
30 public:
34  virtual ~gamemap_base();
35 
36  /** The default border style for a map. */
37  static const int default_border = 1;
38 
39  /**
40  * Maximum number of players supported.
41  *
42  * Warning: when you increase this, you need to add
43  * more definitions to the team_colors.cfg file.
44  */
45  static const int MAX_PLAYERS = 9;
46 
47  std::string to_string() const;
48 
49  /** Effective map width. */
50  int w() const { return total_width() - 2 * border_size(); }
51 
52  /** Effective map height. */
53  int h() const { return total_height() - 2 * border_size(); }
54 
55  /** Size of the map border. */
56  int border_size() const { return default_border; }
57 
58  /** Real width of the map, including borders. */
59  int total_width() const { return tiles_.w; }
60 
61  /** Real height of the map, including borders */
62  int total_height() const { return tiles_.h; }
63 
64  /** Tell if the map is of 0 size. */
65  bool empty() const
66  {
67  return w() <= 0 || h() <= 0;
68  }
69 
70  /**
71  * Tell if a location is on the map.
72  */
73  bool on_board(const map_location& loc) const;
74  bool on_board_with_border(const map_location& loc) const;
75 
76  /** What happens to a village hex when its terrain is changed. */
78 
80  {
83  };
84 
85  /**
86  * Clobbers over the terrain at location 'loc', with the given terrain.
87  * Uses mode and replace_if_failed like merge_terrains().
88  */
90  const terrain_code& terrain,
92  bool replace_if_failed = false) = 0;
93 
94  /**
95  * Looks up terrain at a particular location.
96  *
97  * Hexes off the map may be looked up, and their 'emulated' terrain will
98  * also be returned. This allows proper drawing of the edges of the map.
99  */
101 
104  const std::vector<map_location> starting_positions() const;
105 
106  void set_special_location(const std::string& id, const map_location& loc);
107  map_location special_location(const std::string& id) const;
108 
109  /** Manipulate starting positions of the different sides. */
110  void set_starting_position(int side, const map_location& loc);
111  map_location starting_position(int side) const;
112 
113  /** Counts the number of sides that have valid starting positions on this map */
114  int num_valid_starting_positions() const;
115  /** returns the side number of the side starting at position loc, 0 if no such side exists. */
116  int is_starting_position(const map_location& loc) const;
117  /** returns the name of the special location at position loc, null if no such location exists. */
118  const std::string* is_special_location(const map_location& loc) const;
119 
120  /** Parses ranges of locations into a vector of locations, using this map's dimensions as bounds. */
121  std::vector<map_location> parse_location_range(const std::string& xvals, const std::string &yvals, bool with_border = false) const;
122 
124  {
128  utils::optional<t_translation::terrain_code> terrain_;
129  bool use_old_{false};
130  bool replace_if_failed_{false};
131  };
132 
133  /** Overlays another map onto this one at the given position. */
134  void overlay(const gamemap_base& m, map_location loc, const std::vector<overlay_rule>& rules = std::vector<overlay_rule>(), bool is_odd = false, bool ignore_special_locations = false);
135 
136  template<typename F>
137  void for_each_loc(const F& f) const
138  {
139  for(int x = 0; x < total_width(); ++x) {
140  for(int y = 0; y < total_height(); ++y) {
141  f(map_location{x, y , wml_loc()});
142  }
143  }
144  }
145  //Doesn't include border.
146  template<typename F>
147  void for_each_walkable_loc(const F& f) const
148  {
149  for(int x = 0; x < w(); ++x) {
150  for(int y = 0; y < h(); ++y) {
151  f(map_location{x, y});
152  }
153  }
154  }
155 protected:
156  gamemap_base() = default;
157  gamemap_base(int w, int h, const terrain_code& default_ter);
158  terrain_map& tiles() {return tiles_;}
159  const terrain_map& tiles() const {return tiles_;}
160 private:
163 };
164 
165 /**
166  * Encapsulates the map of the game.
167  *
168  * Although the game is hexagonal, the map is stored as a grid.
169  * Each type of terrain is represented by a multiletter terrain code.
170  * @todo Update for new map-format.
171  */
172 class gamemap : public gamemap_base
173 {
174 public:
175 
176  /* Get info from the terrain_type_data object about the terrain at a location */
180  std::string get_terrain_string(const map_location& loc) const;
181  std::string get_terrain_editor_string(const map_location& loc) const;
182  std::string get_underlying_terrain_string(const map_location& loc) const;
183 
184  bool is_village(const map_location& loc) const;
185  int gives_healing(const map_location& loc) const;
186  bool is_castle(const map_location& loc) const;
187  bool is_keep(const map_location& loc) const;
188 
189  /* The above wrappers, but which takes a terrain. This is the old syntax, preserved for brevity in certain cases. */
190  std::string get_terrain_string(const t_translation::terrain_code& terrain) const;
191  std::string get_terrain_editor_string(const t_translation::terrain_code& terrain) const;
192  std::string get_underlying_terrain_string(const t_translation::terrain_code& terrain) const;
193 
194  // Also expose this for the same reason:
195  const terrain_type& get_terrain_info(const t_translation::terrain_code & terrain) const;
196 
197  /* Get the underlying terrain_type_data object. */
198  const std::shared_ptr<terrain_type_data>& tdata() const { return tdata_; }
199 
200  /**
201  * Loads a map.
202  *
203  * Data should be a series of lines, with each character representing one
204  * hex on the map. Starting locations are represented by numbers.
205  *
206  * @param data the map data to load.
207  */
208  gamemap(std::string_view data); // throw(incorrect_map_format_error)
209  gamemap(int w, int h, const terrain_code& default_ter);
210 
211  void read(std::string_view data, const bool allow_invalid = true);
212 
213  std::string write() const;
214 
216  {
217  return tiles().get(loc.x + border_size(), loc.y + border_size());
218  }
219 private:
220  //private method, use set_terrain instead which also updates villages_.
222  {
223  return tiles().get(loc.x + border_size(), loc.y + border_size());
224  }
225 public:
227  const terrain_code& terrain,
229  bool replace_if_failed = false) override;
230 
231  /** Writes the terrain at loc to cfg. */
232  void write_terrain(const map_location &loc, config& cfg) const;
233 
234  /** Return a list of the locations of villages on the map. */
235  const std::vector<map_location>& villages() const { return villages_; }
236 
237  /** Shortcut to get_terrain_info(get_terrain(loc)). */
238  const terrain_type& get_terrain_info(const map_location &loc) const;
239 
240  /** Gets the list of terrains. */
242 
243 private:
244 
245  /**
246  * Returns a subview of @a data which excludes any legacy headers.
247  *
248  * @param data The mapdata to load.
249  */
250  std::string_view strip_legacy_header(std::string_view data) const;
251 
252  std::shared_ptr<terrain_type_data> tdata_;
253 
254 protected:
255  std::vector<map_location> villages_;
256 };
map_location loc
Definition: move.cpp:172
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:157
void for_each_walkable_loc(const F &f) const
Definition: map.hpp:147
virtual ~gamemap_base()
Definition: map.cpp:112
terrain_code get_terrain(const map_location &loc) const
Looks up terrain at a particular location.
Definition: map.cpp:271
int w() const
Effective map width.
Definition: map.hpp:50
void set_starting_position(int side, const map_location &loc)
Manipulate starting positions of the different sides.
Definition: map.cpp:348
t_translation::starting_positions location_map
Definition: map.hpp:33
terrain_map & tiles()
Definition: map.hpp:158
void set_special_location(const std::string &id, const map_location &loc)
Definition: map.cpp:331
int h() const
Effective map height.
Definition: map.hpp:53
void overlay(const gamemap_base &m, map_location loc, const std::vector< overlay_rule > &rules=std::vector< overlay_rule >(), bool is_odd=false, bool ignore_special_locations=false)
Overlays another map onto this one at the given position.
Definition: map.cpp:183
static const int MAX_PLAYERS
Maximum number of players supported.
Definition: map.hpp:45
map_location special_location(const std::string &id) const
Definition: map.cpp:280
gamemap_base()=default
std::vector< map_location > parse_location_range(const std::string &xvals, const std::string &yvals, bool with_border=false) const
Parses ranges of locations into a vector of locations, using this map's dimensions as bounds.
Definition: map.cpp:402
map_location starting_position(int side) const
Definition: map.cpp:292
t_translation::terrain_code terrain_code
Definition: map.hpp:31
void for_each_loc(const F &f) const
Definition: map.hpp:137
int total_width() const
Real width of the map, including borders.
Definition: map.hpp:59
const location_map & special_locations() const
Definition: map.hpp:103
const terrain_map & tiles() const
Definition: map.hpp:159
std::string to_string() const
Definition: map.cpp:448
int num_valid_starting_positions() const
Counts the number of sides that have valid starting positions on this map.
Definition: map.cpp:303
bool on_board_with_border(const map_location &loc) const
Definition: map.cpp:358
static const int default_border
The default border style for a map.
Definition: map.hpp:37
int total_height() const
Real height of the map, including borders.
Definition: map.hpp:62
int is_starting_position(const map_location &loc) const
returns the side number of the side starting at position loc, 0 if no such side exists.
Definition: map.cpp:315
bool empty() const
Tell if the map is of 0 size.
Definition: map.hpp:65
virtual set_terrain_result set_terrain(const map_location &loc, const terrain_code &terrain, const terrain_type_data::merge_mode mode=terrain_type_data::BOTH, bool replace_if_failed=false)=0
Clobbers over the terrain at location 'loc', with the given terrain.
location_map starting_positions_
Definition: map.hpp:162
village_state
What happens to a village hex when its terrain is changed.
Definition: map.hpp:77
const std::string * is_special_location(const map_location &loc) const
returns the name of the special location at position loc, null if no such location exists.
Definition: map.cpp:325
int border_size() const
Size of the map border.
Definition: map.hpp:56
const std::vector< map_location > starting_positions() const
Definition: map.cpp:453
bool on_board(const map_location &loc) const
Tell if a location is on the map.
Definition: map.cpp:353
location_map & special_locations()
Definition: map.hpp:102
terrain_map tiles_
Definition: map.hpp:161
Encapsulates the map of the game.
Definition: map.hpp:173
const t_translation::ter_list & underlying_union_terrain(const map_location &loc) const
Definition: map.cpp:57
bool is_village(const map_location &loc) const
Definition: map.cpp:66
t_translation::terrain_code & operator[](const map_location &loc)
Definition: map.hpp:221
bool is_castle(const map_location &loc) const
Definition: map.cpp:70
std::string get_underlying_terrain_string(const map_location &loc) const
Definition: map.cpp:63
const t_translation::ter_list & underlying_mvt_terrain(const map_location &loc) const
Definition: map.cpp:53
const t_translation::ter_list & underlying_def_terrain(const map_location &loc) const
Definition: map.cpp:55
const t_translation::ter_list & get_terrain_list() const
Gets the list of terrains.
Definition: map.cpp:42
std::string write() const
Definition: map.cpp:178
const t_translation::terrain_code & operator[](const map_location &loc) const
Definition: map.hpp:215
std::vector< map_location > villages_
Definition: map.hpp:255
std::string get_terrain_editor_string(const map_location &loc) const
Definition: map.cpp:61
void write_terrain(const map_location &loc, config &cfg) const
Writes the terrain at loc to cfg.
Definition: map.cpp:87
std::string_view strip_legacy_header(std::string_view data) const
Returns a subview of data which excludes any legacy headers.
Definition: map.cpp:156
const std::vector< map_location > & villages() const
Return a list of the locations of villages on the map.
Definition: map.hpp:235
bool is_keep(const map_location &loc) const
Definition: map.cpp:72
std::string get_terrain_string(const map_location &loc) const
Definition: map.cpp:59
std::shared_ptr< terrain_type_data > tdata_
Definition: map.hpp:252
gamemap_base::set_terrain_result set_terrain(const map_location &loc, const terrain_code &terrain, const terrain_type_data::merge_mode mode=terrain_type_data::BOTH, bool replace_if_failed=false) override
Clobbers over the terrain at location 'loc', with the given terrain.
Definition: map.cpp:365
const terrain_type & get_terrain_info(const t_translation::terrain_code &terrain) const
Definition: map.cpp:84
gamemap(std::string_view data)
Loads a map.
Definition: map.cpp:92
void read(std::string_view data, const bool allow_invalid=true)
Definition: map.cpp:116
int gives_healing(const map_location &loc) const
Definition: map.cpp:68
const std::shared_ptr< terrain_type_data > & tdata() const
Definition: map.hpp:198
const config * cfg
constexpr bool is_odd(T num)
Definition: math.hpp:34
constexpr terrain_code NONE_TERRAIN
Definition: translation.hpp:58
std::vector< terrain_code > ter_list
Definition: translation.hpp:77
boost::bimaps::bimap< boost::bimaps::set_of< std::string >, boost::bimaps::multiset_of< coordinate > > starting_positions
std::string_view data
Definition: picture.cpp:188
terrain_type_data::merge_mode mode_
Definition: map.hpp:127
t_translation::ter_list new_
Definition: map.hpp:126
utils::optional< t_translation::terrain_code > terrain_
Definition: map.hpp:128
t_translation::ter_list old_
Definition: map.hpp:125
village_state village_change
Definition: map.hpp:82
Encapsulates the map of the game.
Definition: location.hpp:46
terrain_code & get(int x, int y)
Definition: translation.hpp:83
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
#define f