The Battle for Wesnoth  1.13.4+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 - 2016 by David White <[email protected]>
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 #ifndef MAP_H_INCLUDED
18 #define MAP_H_INCLUDED
19 
20 class config;
21 
22 #include "map/location.hpp"
23 #include "terrain/translation.hpp"
24 #include "terrain/type_data.hpp"
25 
26 #include <boost/shared_ptr.hpp>
27 
28 //class terrain_type_data; Can't forward declare because of enum
29 
30 /**
31  * Encapsulates the map of the game.
32  *
33  * Although the game is hexagonal, the map is stored as a grid.
34  * Each type of terrain is represented by a multiletter terrain code.
35  * @todo Update for new map-format.
36  */
37 class gamemap
38 {
39 public:
40 
41  /* Get info from the terrain_type_data object about the terrain at a location */
47 
48  bool is_village(const map_location& loc) const;
49  int gives_healing(const map_location& loc) const;
50  bool is_castle(const map_location& loc) const;
51  bool is_keep(const map_location& loc) const;
52 
53  /* The above wrappers, but which takes a terrain. This is the old syntax, preserved for brevity in certain cases. */
60 
61  bool is_village(const t_translation::t_terrain & terrain) const;
62  int gives_healing(const t_translation::t_terrain & terrain) const;
63  bool is_castle(const t_translation::t_terrain & terrain) const;
64  bool is_keep(const t_translation::t_terrain & terrain) const;
65 
66  // Also expose this for the same reason:
67  const terrain_type& get_terrain_info(const t_translation::t_terrain & terrain) const;
68 
69  /* Get the underlying terrain_type_data object. */
70  const tdata_cache & tdata() const { return tdata_; }
71 
72  /**
73  * Loads a map, with the given terrain configuration.
74  *
75  * Data should be a series of lines, with each character representing one
76  * hex on the map. Starting locations are represented by numbers.
77  *
78  * @param tdata the terrain data
79  * @param data the map data to load.
80  */
81  gamemap(const tdata_cache &tdata, const std::string &data); //throw(incorrect_map_format_error)
82 
83 
84  /**
85  * Loads a map, from the [map] wml config in @a level.
86  *
87  * Data should be a series of lines, with each character representing one
88  * hex on the map. Starting locations are represented by numbers
89  *
90  * @param tdata the terrain data
91  * @param level the scenario config to load from.
92  */
93  gamemap(const tdata_cache &tdata, const config &level); //throw(incorrect_map_format_error)
94 
95  virtual ~gamemap();
96 
97  void read(const std::string& data, const bool allow_invalid = true, const int border_size = 1);
98 
99  std::string write() const;
100 
101  /** Overlays another map onto this one at the given position. */
102  void overlay(const gamemap& m, const config& rules, int x=0, int y=0, bool border=false);
103 
104  /** Effective map width. */
105  int w() const { return w_; }
106 
107  /** Effective map height. */
108  int h() const { return h_; }
109 
110  /** Size of the map border. */
111  int border_size() const { return border_size_; }
112 
113  /** Real width of the map, including borders. */
114  int total_width() const { return total_width_; }
115 
116  /** Real height of the map, including borders */
117  int total_height() const { return total_height_; }
118 
120  { return tiles_[loc.x + border_size_][loc.y + border_size_]; }
121 
122  /**
123  * Looks up terrain at a particular location.
124  *
125  * Hexes off the map may be looked up, and their 'emulated' terrain will
126  * also be returned. This allows proper drawing of the edges of the map.
127  */
129 
130  /** Writes the terrain at loc to cfg. */
131  void write_terrain(const map_location &loc, config& cfg) const;
132 
133 
134  /** Manipulate starting positions of the different sides. */
135  void set_starting_position(int side, const map_location& loc);
136  map_location starting_position(int side) const;
137 
138  void set_special_location(const std::string& id, const map_location& loc);
139  map_location special_location(const std::string& id) const;
140 
141 
142  /// returns the side number of the side starting at position loc, 0 if no such side exists.
143  const std::string* is_starting_position(const map_location& loc) const;
144  int num_valid_starting_positions() const;
145 
146 
147  /**
148  * Tell if a location is on the map.
149  *
150  * Should be called before indexing using [].
151  * @todo inline for performance? -- Ilor
152  */
153  bool on_board(const map_location& loc) const;
154  bool on_board_with_border(const map_location& loc) const;
155 
156  /** Tell if the map is of 0 size. */
157  bool empty() const
158  {
159  return w_ == 0 || h_ == 0;
160  }
161 
162  /** Return a list of the locations of villages on the map. */
163  const std::vector<map_location>& villages() const { return villages_; }
164 
165  /** Shortcut to get_terrain_info(get_terrain(loc)). */
166  const terrain_type& get_terrain_info(const map_location &loc) const;
167 
168  /** Gets the list of terrains. */
170 
171  /**
172  * Clobbers over the terrain at location 'loc', with the given terrain.
173  * Uses mode and replace_if_failed like merge_terrains().
174  */
175  void set_terrain(const map_location& loc, const t_translation::t_terrain & terrain, const terrain_type_data::tmerge_mode mode=terrain_type_data::BOTH, bool replace_if_failed = false);
176 
177  /**
178  * Returns a list of the frequencies of different terrain types on the map,
179  * with terrain nearer the center getting weighted higher.
180  */
181  const std::map<t_translation::t_terrain, size_t>& get_weighted_terrain_frequencies() const;
182 
183  /**
184  * Remove the cached border terrain at loc.
185  *
186  * Needed by the editor to make tiles at the border update correctly when
187  * drawing other tiles.
188  */
190  { borderCache_.erase(loc); }
191 
192  /**
193  * Maximum number of players supported.
194  *
195  * Warning: when you increase this, you need to add
196  * more definitions to the team_colors.cfg file.
197  */
198  enum { MAX_PLAYERS = 9 };
199 
200  /** The default border style for a map. */
201  static const int default_border = 1;
202 
203  /** Parses ranges of locations into a vector of locations, using this map's dimensions as bounds. */
204  std::vector<map_location> parse_location_range(const std::string& xvals,
205  const std::string &yvals, bool with_border = false) const;
206 
209 protected:
211 
212  /**
213  * The size of the starting positions array is MAX_PLAYERS + 1,
214  * because the positions themselves are numbered from 1.
215  */
217 
218  /**
219  * Clears the border cache, needed for the editor
220  */
221  void clear_border_cache() { borderCache_.clear(); }
222 
223 private:
224 
225  /**
226  * Reads the header of a map which is saved in the deprecated map_data format.
227  *
228  * @param data The mapdata to load.
229  */
230  int read_header(const std::string& data);
231 
232  /** Allows lookup of terrain at a particular location. */
233  //const t_translation::t_list operator[](int index) const
234  // { return tiles_[index + border_size_]; }
235 
236 
238  std::vector<map_location> villages_;
239 
240  mutable std::map<map_location, t_translation::t_terrain> borderCache_;
241  mutable std::map<t_translation::t_terrain, size_t> terrainFrequencyCache_;
242 
243 protected:
244  /** Sizes of the map area. */
245  int w_;
246  int h_;
247 
248  /** Sizes of the map including the borders. */
251 
252 private:
253  /** The size of the border around the map. */
255 };
256 
257 #endif
int total_width() const
Real width of the map, including borders.
Definition: map.hpp:114
bool on_board_with_border(const map_location &loc) const
Definition: map.cpp:472
const tstarting_positions & special_locations() const
Definition: map.hpp:208
boost::bimaps::bimap< boost::bimaps::set_of< std::string >, boost::bimaps::multiset_of< coordinate >> tstarting_positions
std::vector< map_location > villages_
Definition: map.hpp:238
GLint level
Definition: glew.h:1220
std::string get_underlying_terrain_string(const t_translation::t_terrain &terrain) const
Definition: map.cpp:89
std::string get_terrain_editor_string(const map_location &loc) const
Definition: map.cpp:65
const t_translation::t_list & get_terrain_list() const
Gets the list of terrains.
Definition: map.cpp:46
int border_size_
The size of the border around the map.
Definition: map.hpp:254
bool is_village(const map_location &loc) const
Definition: map.cpp:68
tdata_cache tdata_
Allows lookup of terrain at a particular location.
Definition: map.hpp:237
static const int default_border
The default border style for a map.
Definition: map.hpp:201
std::map< map_location, t_translation::t_terrain > borderCache_
Definition: map.hpp:240
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
std::map< t_translation::t_terrain, size_t > terrainFrequencyCache_
Definition: map.hpp:241
GLenum mode
Definition: glew.h:2390
const t_translation::t_list & underlying_union_terrain(const map_location &loc) const
Definition: map.cpp:61
std::string get_terrain_string(const map_location &loc) const
Definition: map.cpp:63
const tdata_cache & tdata() const
Definition: map.hpp:70
int total_height() const
Real height of the map, including borders.
Definition: map.hpp:117
std::vector< std::vector< t_terrain > > t_map
Definition: translation.hpp:76
const t_translation::t_terrain operator[](const map_location &loc) const
Definition: map.hpp:119
gamemap(const tdata_cache &tdata, const std::string &data)
Loads a map, with the given terrain configuration.
Definition: map.cpp:108
map_location starting_position(int side) const
Definition: map.cpp:421
int w() const
Effective map width.
Definition: map.hpp:105
t_translation::tstarting_positions tstarting_positions
Definition: map.hpp:207
Encapsulates the map of the game.
Definition: map.hpp:37
void set_special_location(const std::string &id, const map_location &loc)
Definition: map.cpp:445
bool empty() const
Tell if the map is of 0 size.
Definition: map.hpp:157
int border_size() const
Size of the map border.
Definition: map.hpp:111
int w_
Sizes of the map area.
Definition: map.hpp:245
static const ::config * terrain
The terrain used to create the cache.
Definition: minimap.cpp:135
void read(const std::string &data, const bool allow_invalid=true, const int border_size=1)
Definition: map.cpp:154
const std::map< t_translation::t_terrain, size_t > & get_weighted_terrain_frequencies() const
Returns a list of the frequencies of different terrain types on the map, with terrain nearer the cent...
Definition: map.cpp:513
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:47
Encapsulates the map of the game.
Definition: location.hpp:38
const terrain_type & get_terrain_info(const t_translation::t_terrain &terrain) const
Definition: map.cpp:100
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:537
std::string write() const
Definition: map.cpp:247
map_location special_location(const std::string &id) const
Definition: map.cpp:409
int h() const
Effective map height.
Definition: map.hpp:108
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
bool is_castle(const map_location &loc) const
Definition: map.cpp:72
void set_terrain(const map_location &loc, const t_translation::t_terrain &terrain, const terrain_type_data::tmerge_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:479
void write_terrain(const map_location &loc, config &cfg) const
Writes the terrain at loc to cfg.
Definition: map.cpp:103
t_translation::t_terrain get_terrain(const map_location &loc) const
Looks up terrain at a particular location.
Definition: map.cpp:341
GLint GLint GLsizei GLsizei GLsizei GLint border
Definition: glew.h:1222
bool on_board(const map_location &loc) const
Tell if a location is on the map.
Definition: map.cpp:467
tstarting_positions starting_positions_
The size of the starting positions array is MAX_PLAYERS + 1, because the positions themselves are num...
Definition: map.hpp:216
const GLdouble * m
Definition: glew.h:6968
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:218
void remove_from_border_cache(const map_location &loc)
Remove the cached border terrain at loc.
Definition: map.hpp:189
int num_valid_starting_positions() const
Definition: map.cpp:426
int total_width_
Sizes of the map including the borders.
Definition: map.hpp:249
void set_starting_position(int side, const map_location &loc)
Manipulate starting positions of the different sides.
Definition: map.cpp:462
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:439
const t_translation::t_list & underlying_def_terrain(const map_location &loc) const
Definition: map.cpp:59
void overlay(const gamemap &m, const config &rules, int x=0, int y=0, bool border=false)
Overlays another map onto this one at the given position.
Definition: map.cpp:274
t_translation::t_map tiles_
Definition: map.hpp:210
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
const t_translation::t_list & underlying_mvt_terrain(const map_location &loc) const
Definition: map.cpp:57
const std::vector< map_location > & villages() const
Return a list of the locations of villages on the map.
Definition: map.hpp:163
int h_
Definition: map.hpp:246
virtual ~gamemap()
Definition: map.cpp:150
GLsizei const GLcharARB ** string
Definition: glew.h:4503
void clear_border_cache()
Clears the border cache, needed for the editor.
Definition: map.hpp:221
int total_height_
Definition: map.hpp:250
bool is_keep(const map_location &loc) const
Definition: map.cpp:74
std::vector< t_terrain > t_list
Definition: translation.hpp:75
int gives_healing(const map_location &loc) const
Definition: map.cpp:70