The Battle for Wesnoth  1.19.0-dev
map.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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 <optional>
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  /**
77  * Clobbers over the terrain at location 'loc', with the given terrain.
78  * Uses mode and replace_if_failed like merge_terrains().
79  */
80  virtual void 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;
81 
82  /**
83  * Looks up terrain at a particular location.
84  *
85  * Hexes off the map may be looked up, and their 'emulated' terrain will
86  * also be returned. This allows proper drawing of the edges of the map.
87  */
88  terrain_code get_terrain(const map_location& loc) const;
89 
92  const std::vector<map_location> starting_positions() const;
93 
94  void set_special_location(const std::string& id, const map_location& loc);
95  map_location special_location(const std::string& id) const;
96 
97  /** Manipulate starting positions of the different sides. */
98  void set_starting_position(int side, const map_location& loc);
99  map_location starting_position(int side) const;
100 
101  /** Counts the number of sides that have valid starting positions on this map */
102  int num_valid_starting_positions() const;
103  /** returns the side number of the side starting at position loc, 0 if no such side exists. */
104  int is_starting_position(const map_location& loc) const;
105  /** returns the name of the special location at position loc, null if no such location exists. */
106  const std::string* is_special_location(const map_location& loc) const;
107 
108  /** Parses ranges of locations into a vector of locations, using this map's dimensions as bounds. */
109  std::vector<map_location> parse_location_range(const std::string& xvals, const std::string &yvals, bool with_border = false) const;
110 
112  {
116  std::optional<t_translation::terrain_code> terrain_;
117  bool use_old_;
119 
121  : old_()
122  , new_()
123  , mode_(terrain_type_data::BOTH)
124  , terrain_()
125  , use_old_(false)
126  , replace_if_failed_(false)
127  {
128 
129  }
130  };
131 
132  /** Overlays another map onto this one at the given position. */
133  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);
134 
135  template<typename F>
136  void for_each_loc(const F& f) const
137  {
138  for(int x = 0; x < total_width(); ++x) {
139  for(int y = 0; y < total_height(); ++y) {
140  f(map_location{x, y , wml_loc()});
141  }
142  }
143  }
144  //Doesn't include border.
145  template<typename F>
146  void for_each_walkable_loc(const F& f) const
147  {
148  for(int x = 0; x < w(); ++x) {
149  for(int y = 0; y < h(); ++y) {
150  f(map_location{x, y});
151  }
152  }
153  }
154 protected:
155  gamemap_base() = default;
156  gamemap_base(int w, int h, terrain_code default_ter = terrain_code());
157  terrain_map& tiles() {return tiles_;}
158  const terrain_map& tiles() const {return tiles_;}
159 private:
162 };
163 
164 /**
165  * Encapsulates the map of the game.
166  *
167  * Although the game is hexagonal, the map is stored as a grid.
168  * Each type of terrain is represented by a multiletter terrain code.
169  * @todo Update for new map-format.
170  */
171 class gamemap : public gamemap_base
172 {
173 public:
174 
175  /* Get info from the terrain_type_data object about the terrain at a location */
179  std::string get_terrain_string(const map_location& loc) const;
180  std::string get_terrain_editor_string(const map_location& loc) const;
181 
182  bool is_village(const map_location& loc) const;
183  int gives_healing(const map_location& loc) const;
184  bool is_castle(const map_location& loc) const;
185  bool is_keep(const map_location& loc) const;
186 
187  /* The above wrappers, but which takes a terrain. This is the old syntax, preserved for brevity in certain cases. */
191  std::string get_terrain_string(const t_translation::terrain_code& terrain) const;
192  std::string get_terrain_editor_string(const t_translation::terrain_code& terrain) const;
193  std::string get_underlying_terrain_string(const t_translation::terrain_code& terrain) const;
194 
195  bool is_village(const t_translation::terrain_code & terrain) const;
196  int gives_healing(const t_translation::terrain_code & terrain) const;
197  bool is_castle(const t_translation::terrain_code & terrain) const;
198  bool is_keep(const t_translation::terrain_code & terrain) const;
199 
200  // Also expose this for the same reason:
201  const terrain_type& get_terrain_info(const t_translation::terrain_code & terrain) const;
202 
203  /* Get the underlying terrain_type_data object. */
204  const std::shared_ptr<terrain_type_data>& tdata() const { return tdata_; }
205 
206  /**
207  * Loads a map.
208  *
209  * Data should be a series of lines, with each character representing one
210  * hex on the map. Starting locations are represented by numbers.
211  *
212  * @param data the map data to load.
213  */
214  gamemap(const std::string& data); // throw(incorrect_map_format_error)
215 
216  void read(const std::string& data, const bool allow_invalid = true);
217 
218  std::string write() const;
219 
221  {
222  return tiles().get(loc.x + border_size(), loc.y + border_size());
223  }
224 private:
225  //private method, use set_terrain instead which also updates villages_.
227  {
228  return tiles().get(loc.x + border_size(), loc.y + border_size());
229  }
230 public:
231  void 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;
232 
233  /** Writes the terrain at loc to cfg. */
234  void write_terrain(const map_location &loc, config& cfg) const;
235 
236  /** Return a list of the locations of villages on the map. */
237  const std::vector<map_location>& villages() const { return villages_; }
238 
239  /** Shortcut to get_terrain_info(get_terrain(loc)). */
240  const terrain_type& get_terrain_info(const map_location &loc) const;
241 
242  /** Gets the list of terrains. */
244 
245 private:
246 
247  /**
248  * Reads the header of a map which is saved in the deprecated map_data format.
249  *
250  * @param data The mapdata to load.
251  */
252  int read_header(const std::string& data);
253 
254  std::shared_ptr<terrain_type_data> tdata_;
255 
256 protected:
257  std::vector<map_location> villages_;
258 };
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
void for_each_walkable_loc(const F &f) const
Definition: map.hpp:146
virtual ~gamemap_base()
Definition: map.cpp:128
terrain_code get_terrain(const map_location &loc) const
Looks up terrain at a particular location.
Definition: map.cpp:301
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:379
t_translation::starting_positions location_map
Definition: map.hpp:33
terrain_map & tiles()
Definition: map.hpp:157
void set_special_location(const std::string &id, const map_location &loc)
Definition: map.cpp:362
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:214
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:311
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:423
map_location starting_position(int side) const
Definition: map.cpp:323
virtual void 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.
t_translation::terrain_code terrain_code
Definition: map.hpp:31
void for_each_loc(const F &f) const
Definition: map.hpp:136
int total_width() const
Real width of the map, including borders.
Definition: map.hpp:59
const location_map & special_locations() const
Definition: map.hpp:91
const terrain_map & tiles() const
Definition: map.hpp:158
std::string to_string() const
Definition: map.cpp:469
int num_valid_starting_positions() const
Counts the number of sides that have valid starting positions on this map.
Definition: map.cpp:334
bool on_board_with_border(const map_location &loc) const
Definition: map.cpp:389
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:346
bool empty() const
Tell if the map is of 0 size.
Definition: map.hpp:65
location_map starting_positions_
Definition: map.hpp:161
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:356
int border_size() const
Size of the map border.
Definition: map.hpp:56
const std::vector< map_location > starting_positions() const
Definition: map.cpp:474
bool on_board(const map_location &loc) const
Tell if a location is on the map.
Definition: map.cpp:384
location_map & special_locations()
Definition: map.hpp:90
terrain_map tiles_
Definition: map.hpp:160
Encapsulates the map of the game.
Definition: map.hpp:172
const t_translation::ter_list & underlying_union_terrain(const map_location &loc) const
Definition: map.cpp:58
bool is_village(const map_location &loc) const
Definition: map.cpp:65
t_translation::terrain_code & operator[](const map_location &loc)
Definition: map.hpp:226
std::string get_underlying_terrain_string(const t_translation::terrain_code &terrain) const
Definition: map.cpp:86
bool is_castle(const map_location &loc) const
Definition: map.cpp:69
void read(const std::string &data, const bool allow_invalid=true)
Definition: map.cpp:132
const t_translation::terrain_code operator[](const map_location &loc) const
Definition: map.hpp:220
const t_translation::ter_list & underlying_mvt_terrain(const map_location &loc) const
Definition: map.cpp:54
const t_translation::ter_list & underlying_def_terrain(const map_location &loc) const
Definition: map.cpp:56
const t_translation::ter_list & get_terrain_list() const
Gets the list of terrains.
Definition: map.cpp:43
std::string write() const
Definition: map.cpp:209
std::vector< map_location > villages_
Definition: map.hpp:257
std::string get_terrain_editor_string(const map_location &loc) const
Definition: map.cpp:62
void write_terrain(const map_location &loc, config &cfg) const
Writes the terrain at loc to cfg.
Definition: map.cpp:100
gamemap(const std::string &data)
Loads a map.
Definition: map.cpp:105
const std::vector< map_location > & villages() const
Return a list of the locations of villages on the map.
Definition: map.hpp:237
bool is_keep(const map_location &loc) const
Definition: map.cpp:71
std::string get_terrain_string(const map_location &loc) const
Definition: map.cpp:60
std::shared_ptr< terrain_type_data > tdata_
Definition: map.hpp:254
const terrain_type & get_terrain_info(const t_translation::terrain_code &terrain) const
Definition: map.cpp:97
void 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:396
int gives_healing(const map_location &loc) const
Definition: map.cpp:67
const std::shared_ptr< terrain_type_data > & tdata() const
Definition: map.hpp:204
int read_header(const std::string &data)
Reads the header of a map which is saved in the deprecated map_data format.
Definition: map.cpp:182
Contains the database of all known terrain types, both those defined explicitly by WML [terrain_type]...
Definition: type_data.hpp:40
constexpr bool is_odd(T num)
Definition: math.hpp:36
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:194
std::optional< t_translation::terrain_code > terrain_
Definition: map.hpp:116
terrain_type_data::merge_mode mode_
Definition: map.hpp:115
t_translation::ter_list new_
Definition: map.hpp:114
t_translation::ter_list old_
Definition: map.hpp:113
Encapsulates the map of the game.
Definition: location.hpp:38
terrain_code & get(int x, int y)
Definition: translation.hpp:89
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