The Battle for Wesnoth  1.13.11+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
map.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
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 /** @file */
16 
17 #pragma once
18 
19 class config;
20 
21 #include "map/location.hpp"
22 #include "terrain/translation.hpp"
23 #include "terrain/type_data.hpp"
24 
25 //class terrain_type_data; Can't forward declare because of enum
26 
27 /**
28  * Encapsulates the map of the game.
29  *
30  * Although the game is hexagonal, the map is stored as a grid.
31  * Each type of terrain is represented by a multiletter terrain code.
32  * @todo Update for new map-format.
33  */
34 class gamemap
35 {
36 public:
37 
38  /* Get info from the terrain_type_data object about the terrain at a location */
44 
45  bool is_village(const map_location& loc) const;
46  int gives_healing(const map_location& loc) const;
47  bool is_castle(const map_location& loc) const;
48  bool is_keep(const map_location& loc) const;
49 
50  /* The above wrappers, but which takes a terrain. This is the old syntax, preserved for brevity in certain cases. */
57 
58  bool is_village(const t_translation::terrain_code & terrain) const;
59  int gives_healing(const t_translation::terrain_code & terrain) const;
60  bool is_castle(const t_translation::terrain_code & terrain) const;
61  bool is_keep(const t_translation::terrain_code & terrain) const;
62 
63  // Also expose this for the same reason:
64  const terrain_type& get_terrain_info(const t_translation::terrain_code & terrain) const;
65 
66  /* Get the underlying terrain_type_data object. */
67  const ter_data_cache & tdata() const { return tdata_; }
68 
69  /**
70  * Loads a map, with the given terrain configuration.
71  *
72  * Data should be a series of lines, with each character representing one
73  * hex on the map. Starting locations are represented by numbers.
74  *
75  * @param tdata the terrain data
76  * @param data the map data to load.
77  */
78  gamemap(const ter_data_cache &tdata, const std::string &data); //throw(incorrect_map_format_error)
79 
80  virtual ~gamemap();
81 
82  void read(const std::string& data, const bool allow_invalid = true);
83 
84  std::string write() const;
85 
86  /** Overlays another map onto this one at the given position. */
87  void overlay(const gamemap& m, const config& rules, map_location loc);
88 
89  /** Effective map width. */
90  int w() const { return w_; }
91 
92  /** Effective map height. */
93  int h() const { return h_; }
94 
95  /** Size of the map border. */
96  int border_size() const { return default_border; }
97 
98  /** Real width of the map, including borders. */
99  int total_width() const { return tiles_.w; }
100 
101  /** Real height of the map, including borders */
102  int total_height() const { return tiles_.h; }
103 
105  {
106  return tiles_.get(loc.x + border_size(), loc.y + border_size());
107  }
108 private:
109  //private method, use set_terrain instead which also updates villages_.
111  {
112  return tiles_.get(loc.x + border_size(), loc.y + border_size());
113  }
114 public:
115 
116  /**
117  * Looks up terrain at a particular location.
118  *
119  * Hexes off the map may be looked up, and their 'emulated' terrain will
120  * also be returned. This allows proper drawing of the edges of the map.
121  */
123 
124  /** Writes the terrain at loc to cfg. */
125  void write_terrain(const map_location &loc, config& cfg) const;
126 
127 
128  /** Manipulate starting positions of the different sides. */
129  void set_starting_position(int side, const map_location& loc);
130  map_location starting_position(int side) const;
131 
132  void set_special_location(const std::string& id, const map_location& loc);
133  map_location special_location(const std::string& id) const;
134 
135 
136  /// returns the side number of the side starting at position loc, 0 if no such side exists.
137  const std::string* is_starting_position(const map_location& loc) const;
138  int num_valid_starting_positions() const;
139 
140 
141  /**
142  * Tell if a location is on the map.
143  *
144  * Should be called before indexing using [].
145  * @todo inline for performance? -- Ilor
146  */
147  bool on_board(const map_location& loc) const;
148  bool on_board_with_border(const map_location& loc) const;
149 
150  /** Tell if the map is of 0 size. */
151  bool empty() const
152  {
153  return w_ == 0 || h_ == 0;
154  }
155 
156  /** Return a list of the locations of villages on the map. */
157  const std::vector<map_location>& villages() const { return villages_; }
158 
159  /** Shortcut to get_terrain_info(get_terrain(loc)). */
160  const terrain_type& get_terrain_info(const map_location &loc) const;
161 
162  /** Gets the list of terrains. */
164 
165  /**
166  * Clobbers over the terrain at location 'loc', with the given terrain.
167  * Uses mode and replace_if_failed like merge_terrains().
168  */
169  void set_terrain(const map_location& loc, const t_translation::terrain_code & terrain, const terrain_type_data::merge_mode mode=terrain_type_data::BOTH, bool replace_if_failed = false);
170 
171  /**
172  * Maximum number of players supported.
173  *
174  * Warning: when you increase this, you need to add
175  * more definitions to the team_colors.cfg file.
176  */
177  enum { MAX_PLAYERS = 9 };
178 
179  /** The default border style for a map. */
180  static const int default_border = 1;
181 
182  /** Parses ranges of locations into a vector of locations, using this map's dimensions as bounds. */
183  std::vector<map_location> parse_location_range(const std::string& xvals,
184  const std::string &yvals, bool with_border = false) const;
185 
188 
189  template<typename F>
190  void for_each_loc(const F& f) const
191  {
192  for (int x = -border_size(); x < w() + border_size(); ++x) {
193  for (int y = -border_size(); y < h() + border_size(); ++y) {
194  f({ x, y });
195  }
196  }
197  }
198  //Doesn't include border.
199  template<typename F>
200  void for_each_walkable_loc(const F& f) const
201  {
202  for (int x = 0; x < w(); ++x) {
203  for (int y = 0; y < h(); ++y) {
204  f({ x, y });
205  }
206  }
207  }
208  void add_fog_border();
209 protected:
211 
213 
214 private:
215 
216  /**
217  * Reads the header of a map which is saved in the deprecated map_data format.
218  *
219  * @param data The mapdata to load.
220  */
221  int read_header(const std::string& data);
222 
224  std::vector<map_location> villages_;
225 
226 protected:
227  /** Sizes of the map area. */
228  int w_;
229  int h_;
230 };
int total_width() const
Real width of the map, including borders.
Definition: map.hpp:99
const t_translation::ter_list & get_terrain_list() const
Gets the list of terrains.
Definition: map.cpp:44
bool on_board_with_border(const map_location &loc) const
Definition: map.cpp:374
std::vector< char_t > string
std::vector< map_location > villages_
Definition: map.hpp:224
std::string get_terrain_editor_string(const map_location &loc) const
Definition: map.cpp:63
const t_translation::ter_list & underlying_union_terrain(const map_location &loc) const
Definition: map.cpp:59
bool is_village(const map_location &loc) const
Definition: map.cpp:66
const t_translation::terrain_code operator[](const map_location &loc) const
Definition: map.hpp:104
const t_translation::ter_list & underlying_mvt_terrain(const map_location &loc) const
Definition: map.cpp:55
static const int default_border
The default border style for a map.
Definition: map.hpp:180
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 for_each_walkable_loc(const F &f) const
Definition: map.hpp:200
t_translation::starting_positions starting_positions
Definition: map.hpp:186
std::string get_terrain_string(const map_location &loc) const
Definition: map.cpp:61
void for_each_loc(const F &f) const
Definition: map.hpp:190
void add_fog_border()
Definition: map.cpp:453
int total_height() const
Real height of the map, including borders.
Definition: map.hpp:102
void set_terrain(const map_location &loc, const t_translation::terrain_code &terrain, const terrain_type_data::merge_mode mode=terrain_type_data::BOTH, bool replace_if_failed=false)
Clobbers over the terrain at location 'loc', with the given terrain.
Definition: map.cpp:381
t_translation::terrain_code get_terrain(const map_location &loc) const
Looks up terrain at a particular location.
Definition: map.cpp:301
const t_translation::ter_list & underlying_def_terrain(const map_location &loc) const
Definition: map.cpp:57
map_location starting_position(int side) const
Definition: map.cpp:323
std::string get_underlying_terrain_string(const t_translation::terrain_code &terrain) const
Definition: map.cpp:87
int w() const
Effective map width.
Definition: map.hpp:90
Encapsulates the map of the game.
Definition: map.hpp:34
void set_special_location(const std::string &id, const map_location &loc)
Definition: map.cpp:347
bool empty() const
Tell if the map is of 0 size.
Definition: map.hpp:151
int border_size() const
Size of the map border.
Definition: map.hpp:96
const starting_positions & special_locations() const
Definition: map.hpp:187
int w_
Sizes of the map area.
Definition: map.hpp:228
void read(const std::string &data, const bool allow_invalid=true)
Definition: map.cpp:122
const terrain_type & get_terrain_info(const t_translation::terrain_code &terrain) const
Definition: map.cpp:98
t_translation::terrain_code & operator[](const map_location &loc)
Definition: map.hpp:110
static const ::config * terrain
The terrain used to create the cache.
Definition: minimap.cpp:130
ter_data_cache tdata_
Definition: map.hpp:223
Encapsulates the map of the game.
Definition: location.hpp:42
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:407
std::string write() const
Definition: map.cpp:206
map_location special_location(const std::string &id) const
Definition: map.cpp:311
t_translation::ter_map tiles_
Definition: map.hpp:210
int h() const
Effective map height.
Definition: map.hpp:93
gamemap(const ter_data_cache &tdata, const std::string &data)
Loads a map, with the given terrain configuration.
Definition: map.cpp:106
bool is_castle(const map_location &loc) const
Definition: map.cpp:70
void write_terrain(const map_location &loc, config &cfg) const
Writes the terrain at loc to cfg.
Definition: map.cpp:101
terrain_code & get(int x, int y)
Definition: translation.hpp:98
starting_positions starting_positions_
Definition: map.hpp:212
bool on_board(const map_location &loc) const
Tell if a location is on the map.
Definition: map.cpp:369
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:179
#define f
int num_valid_starting_positions() const
Definition: map.cpp:328
const ter_data_cache & tdata() const
Definition: map.hpp:67
std::vector< terrain_code > ter_list
Definition: translation.hpp:83
void set_starting_position(int side, const map_location &loc)
Manipulate starting positions of the different sides.
Definition: map.cpp:364
const std::string * 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:341
void overlay(const gamemap &m, const config &rules, map_location loc)
Overlays another map onto this one at the given position.
Definition: map.cpp:233
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
const std::vector< map_location > & villages() const
Return a list of the locations of villages on the map.
Definition: map.hpp:157
int h_
Definition: map.hpp:229
virtual ~gamemap()
Definition: map.cpp:118
bool is_keep(const map_location &loc) const
Definition: map.cpp:72
int gives_healing(const map_location &loc) const
Definition: map.cpp:68
boost::bimaps::bimap< boost::bimaps::set_of< std::string >, boost::bimaps::multiset_of< coordinate >> starting_positions
std::shared_ptr< terrain_type_data > ter_data_cache