-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
map.hpp
266 lines (210 loc) · 8.55 KB
/
map.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/** @file */
#pragma once
class config;
#include "map/location.hpp"
#include "terrain/translation.hpp"
#include "terrain/type_data.hpp"
#include <boost/optional.hpp>
//class terrain_type_data; Can't forward declare because of enum
/**
* Encapsulates the map of the game.
*
* Although the game is hexagonal, the map is stored as a grid.
* Each type of terrain is represented by a multiletter terrain code.
* @todo Update for new map-format.
*/
class gamemap
{
public:
/* Get info from the terrain_type_data object about the terrain at a location */
const t_translation::ter_list& underlying_mvt_terrain(const map_location& loc) const;
const t_translation::ter_list& underlying_def_terrain(const map_location& loc) const;
const t_translation::ter_list& underlying_union_terrain(const map_location& loc) const;
std::string get_terrain_string(const map_location& loc) const;
std::string get_terrain_editor_string(const map_location& loc) const;
bool is_village(const map_location& loc) const;
int gives_healing(const map_location& loc) const;
bool is_castle(const map_location& loc) const;
bool is_keep(const map_location& loc) const;
/* The above wrappers, but which takes a terrain. This is the old syntax, preserved for brevity in certain cases. */
const t_translation::ter_list& underlying_mvt_terrain(const t_translation::terrain_code & terrain) const;
const t_translation::ter_list& underlying_def_terrain(const t_translation::terrain_code & terrain) const;
const t_translation::ter_list& underlying_union_terrain(const t_translation::terrain_code & terrain) const;
std::string get_terrain_string(const t_translation::terrain_code& terrain) const;
std::string get_terrain_editor_string(const t_translation::terrain_code& terrain) const;
std::string get_underlying_terrain_string(const t_translation::terrain_code& terrain) const;
bool is_village(const t_translation::terrain_code & terrain) const;
int gives_healing(const t_translation::terrain_code & terrain) const;
bool is_castle(const t_translation::terrain_code & terrain) const;
bool is_keep(const t_translation::terrain_code & terrain) const;
// Also expose this for the same reason:
const terrain_type& get_terrain_info(const t_translation::terrain_code & terrain) const;
/* Get the underlying terrain_type_data object. */
const ter_data_cache & tdata() const { return tdata_; }
/**
* Loads a map, with the given terrain configuration.
*
* Data should be a series of lines, with each character representing one
* hex on the map. Starting locations are represented by numbers.
*
* @param tdata the terrain data
* @param data the map data to load.
*/
gamemap(const ter_data_cache &tdata, const std::string &data); //throw(incorrect_map_format_error)
virtual ~gamemap();
void read(const std::string& data, const bool allow_invalid = true);
std::string write() const;
struct overlay_rule
{
t_translation::ter_list old_;
t_translation::ter_list new_;
terrain_type_data::merge_mode mode_;
boost::optional<t_translation::terrain_code> terrain_;
bool use_old_;
bool replace_if_failed_;
overlay_rule()
: old_()
, new_()
, mode_(terrain_type_data::BOTH)
, terrain_()
, use_old_(false)
, replace_if_failed_(false)
{
}
};
/** Overlays another map onto this one at the given position. */
void overlay(const gamemap& m, map_location loc, const std::vector<overlay_rule>& rules = std::vector<overlay_rule>(), bool is_odd = false, bool ignore_special_locations = false);
static void overlay_impl(
// const but changed via set_terrain
const t_translation::ter_map& m1,
t_translation::starting_positions& m1_st,
const t_translation::ter_map& m2,
const t_translation::starting_positions& m2_st,
std::function<void (const map_location&, const t_translation::terrain_code&, terrain_type_data::merge_mode, bool)> set_terrain,
map_location loc,
const std::vector<overlay_rule>& rules,
bool is_odd,
bool ignore_special_locations);
/** Effective map width. */
int w() const { return w_; }
/** Effective map height. */
int h() const { return h_; }
/** Size of the map border. */
int border_size() const { return default_border; }
/** Real width of the map, including borders. */
int total_width() const { return tiles_.w; }
/** Real height of the map, including borders */
int total_height() const { return tiles_.h; }
const t_translation::terrain_code operator[](const map_location& loc) const
{
return tiles_.get(loc.x + border_size(), loc.y + border_size());
}
private:
//private method, use set_terrain instead which also updates villages_.
t_translation::terrain_code& operator[](const map_location& loc)
{
return tiles_.get(loc.x + border_size(), loc.y + border_size());
}
public:
/**
* Looks up terrain at a particular location.
*
* Hexes off the map may be looked up, and their 'emulated' terrain will
* also be returned. This allows proper drawing of the edges of the map.
*/
t_translation::terrain_code get_terrain(const map_location& loc) const;
/** Writes the terrain at loc to cfg. */
void write_terrain(const map_location &loc, config& cfg) const;
/** Manipulate starting positions of the different sides. */
void set_starting_position(int side, const map_location& loc);
map_location starting_position(int side) const;
void set_special_location(const std::string& id, const map_location& loc);
map_location special_location(const std::string& id) const;
/// returns the side number of the side starting at position loc, 0 if no such side exists.
const std::string* is_starting_position(const map_location& loc) const;
int num_valid_starting_positions() const;
/**
* Tell if a location is on the map.
*
* Should be called before indexing using [].
* @todo inline for performance? -- Ilor
*/
bool on_board(const map_location& loc) const;
bool on_board_with_border(const map_location& loc) const;
/** Tell if the map is of 0 size. */
bool empty() const
{
return w_ == 0 || h_ == 0;
}
/** Return a list of the locations of villages on the map. */
const std::vector<map_location>& villages() const { return villages_; }
/** Shortcut to get_terrain_info(get_terrain(loc)). */
const terrain_type& get_terrain_info(const map_location &loc) const;
/** Gets the list of terrains. */
const t_translation::ter_list& get_terrain_list() const;
/**
* Clobbers over the terrain at location 'loc', with the given terrain.
* Uses mode and replace_if_failed like merge_terrains().
*/
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);
/**
* Maximum number of players supported.
*
* Warning: when you increase this, you need to add
* more definitions to the team_colors.cfg file.
*/
enum { MAX_PLAYERS = 9 };
/** The default border style for a map. */
static const int default_border = 1;
/** Parses ranges of locations into a vector of locations, using this map's dimensions as bounds. */
std::vector<map_location> parse_location_range(const std::string& xvals,
const std::string &yvals, bool with_border = false) const;
using starting_positions = t_translation::starting_positions;
const starting_positions& special_locations() const { return starting_positions_; }
template<typename F>
void for_each_loc(const F& f) const
{
for (int x = -border_size(); x < w() + border_size(); ++x) {
for (int y = -border_size(); y < h() + border_size(); ++y) {
f({ x, y });
}
}
}
//Doesn't include border.
template<typename F>
void for_each_walkable_loc(const F& f) const
{
for (int x = 0; x < w(); ++x) {
for (int y = 0; y < h(); ++y) {
f({ x, y });
}
}
}
protected:
t_translation::ter_map tiles_;
starting_positions starting_positions_;
private:
/**
* Reads the header of a map which is saved in the deprecated map_data format.
*
* @param data The mapdata to load.
*/
int read_header(const std::string& data);
ter_data_cache tdata_;
protected:
std::vector<map_location> villages_;
/** Sizes of the map area. */
int w_;
int h_;
};