38 #include <boost/optional.hpp>
41 #define ERR_CF LOG_STREAM(err, log_config)
42 #define LOG_G LOG_STREAM(info, lg::general())
43 #define DBG_G LOG_STREAM(debug, lg::general())
80 {
return tdata_->underlying_mvt_terrain(terrain); }
82 {
return tdata_->underlying_def_terrain(terrain); }
84 {
return tdata_->underlying_union_terrain(terrain); }
86 {
return tdata_->get_terrain_string(terrain); }
88 {
return tdata_->get_terrain_editor_string(terrain); }
90 {
return tdata_->get_underlying_terrain_string(terrain); }
92 {
return tdata_->get_terrain_info(terrain).is_village(); }
94 {
return tdata_->get_terrain_info(terrain).gives_healing(); }
96 {
return tdata_->get_terrain_info(terrain).is_castle(); }
98 {
return tdata_->get_terrain_info(terrain).is_keep(); }
101 {
return tdata_->get_terrain_info(terrain); }
113 terrainFrequencyCache_(),
118 border_size_(default_border)
120 DBG_G <<
"loading map: '" << data <<
"'\n";
130 terrainFrequencyCache_(),
135 border_size_(default_border)
137 DBG_G <<
"loading map: '" << level.
debug() <<
"'\n";
140 if (!map_data.empty()) {
167 if(allow_invalid)
return;
199 std::stringstream ss;
203 ss <<
"The map cannot be loaded.";
209 if(
x >= border_size_ && y >= border_size_
210 &&
x < total_width_-border_size_ && y < total_height_-border_size_
221 size_t header_offset = data.find(
"\n\n");
222 if(header_offset == std::string::npos) {
227 header_offset = data.find(
"\r\n\r\n");
229 const size_t comma_offset = data.find(
",");
234 if (!(!(header_offset == std::string::npos || comma_offset < header_offset)))
239 ::read(header, header_str);
243 return header_offset + 2;
258 boost::optional<t_translation::t_terrain> terrain_;
260 bool replace_if_failed_;
268 , replace_if_failed_(false)
277 std::vector<overlay_rule> rules(rules_cfg.
child_count(
"rule"));
278 for(
size_t i = 0;
i <rules.size(); ++
i)
285 if(!terrain.empty()) {
286 rules[
i].terrain_ = terrain[0];
288 rules[
i].use_old_ = cfg[
"use_old"].to_bool();
289 rules[
i].replace_if_failed_ = cfg[
"replace_if_failed"].to_bool();
294 const int xstart = std::max<int>(-actual_border, -xpos - actual_border);
295 const int ystart = std::max<int>(-actual_border, -ypos - actual_border - ((xpos & 1) ? 1 : 0));
296 const int xend = std::min<int>(m.
w() + actual_border,
w() + actual_border - xpos);
297 const int yend = std::min<int>(m.
h() + actual_border,
h() + actual_border - ypos);
299 for(
int x1 = xstart; x1 < xend; ++x1) {
300 for(
int y1 = ystart; y1 < yend; ++y1) {
301 const int x2 = x1 + xpos;
302 const int y2 = y1 + ypos + ((xpos & 1) && (x1 & 1) ? 1 : 0);
312 const overlay_rule* rule =
nullptr;
313 for(
const overlay_rule& current_rule : rules)
321 rule = ¤t_rule;
325 if (rule && !rule->use_old_) {
326 set_terrain(
map_location(x2, y2), rule->terrain_ ? *rule->terrain_ : t , rule->mode_, rule->replace_if_failed_);
352 const std::map<map_location, t_translation::t_terrain>::const_iterator
itor =
borderCache_.find(loc);
358 int number_of_items = 0;
362 for(
int n = 0;
n != 6; ++
n) {
376 std::map<map_location, t_translation::t_terrain>::const_iterator itor =
383 items[number_of_items] = itor->second;
393 int terrain_count = 0;
394 for(
int i = 0;
i != number_of_items; ++
i) {
397 if(c > terrain_count) {
404 borderCache_.insert(std::pair<map_location, t_translation::t_terrain>(loc,used_terrain));
413 auto& coordinate = it->second;
431 bool is_number = std::find_if(
id.begin(),
id.
end(), [](
char c) {
return !std::isdigit(c); }) ==
id.
end();
433 res = std::max(res, std::stoi(
id));
493 const bool new_village =
tdata_->is_village(new_terrain);
495 if(old_village && !new_village) {
497 }
else if(!old_village && new_village) {
508 for(
int n = 0;
n < 6; ++
n) {
523 const size_t weight_at_edge = 100;
524 const size_t additional_weight_at_center = 200;
526 for(
size_t i = 0;
i != size_t(
w()); ++
i) {
527 for(
size_t j = 0; j != size_t(
h()); ++j) {
530 (furthest_distance-distance)*additional_weight_at_center;
538 bool with_border)
const
540 std::vector<map_location>
res;
543 int xmin = 1, xmax =
w(), ymin = 1, ymax =
h();
552 for (
unsigned i = 0;
i < xvals.size() ||
i < yvals.size(); ++
i)
554 std::pair<int,int> xrange, yrange;
556 if (
i < xvals.size()) {
558 if (xrange.first < xmin) xrange.first = xmin;
559 if (xrange.second > xmax) xrange.second = xmax;
562 xrange.second = xmax;
565 if (
i < yvals.size()) {
567 if (yrange.first < ymin) yrange.first = ymin;
568 if (yrange.second > ymax) yrange.second = ymax;
571 yrange.second = ymax;
574 for(
int x = xrange.first; x <= xrange.second; ++x) {
575 for(
int y = yrange.first; y <= yrange.second; ++y) {
bool on_board_with_border(const map_location &loc) const
Contains an x and y coordinate used for starting positions in maps.
std::vector< map_location > villages_
t_list read_list(const std::string &str, const t_layer filler)
Reads a list of terrains from a string, when reading the.
std::string get_underlying_terrain_string(const t_translation::t_terrain &terrain) const
std::string get_terrain_editor_string(const map_location &loc) const
std::string write_game_map(const t_map &map, const tstarting_positions &starting_positions, coordinate border_offset)
Write a gamemap in to a vector string.
const t_translation::t_list & get_terrain_list() const
Gets the list of terrains.
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
const t_terrain NONE_TERRAIN
int border_size_
The size of the border around the map.
void get_adjacent_tiles(const map_location &a, map_location *res)
Function which, given a location, will place all adjacent locations in res.
bool is_village(const map_location &loc) const
tdata_cache tdata_
Allows lookup of terrain at a particular location.
static lg::log_domain log_config("config")
static const map_location & ZERO()
Old implementation:
t_map read_game_map(const std::string &str, tstarting_positions &starting_positions, coordinate border_offset)
Reads a gamemap string into a 2D vector.
std::map< map_location, t_translation::t_terrain > borderCache_
GLint GLint GLint GLint GLint GLint y
const std::vector< std::string > items
std::string debug() const
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
std::map< t_translation::t_terrain, size_t > terrainFrequencyCache_
const t_translation::t_list & underlying_union_terrain(const map_location &loc) const
Definitions for the interface to Wesnoth Markup Language (WML).
std::string get_terrain_string(const map_location &loc) const
gamemap(const tdata_cache &tdata, const std::string &data)
Loads a map, with the given terrain configuration.
map_location starting_position(int side) const
size_t distance_between(const map_location &a, const map_location &b)
Function which gives the number of hexes between two tiles (i.e.
int w() const
Effective map width.
Encapsulates the map of the game.
void set_special_location(const std::string &id, const map_location &loc)
int border_size() const
Size of the map border.
int w_
Sizes of the map area.
static const map_location & null_location()
static const ::config * terrain
The terrain used to create the cache.
void read(const std::string &data, const bool allow_invalid=true, const int border_size=1)
GLuint GLuint GLsizei count
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...
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Encapsulates the map of the game.
const terrain_type & get_terrain_info(const t_translation::t_terrain &terrain) const
std::pair< int, int > parse_range(std::string const &str)
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...
std::string write() const
map_location special_location(const std::string &id) const
std::map< std::string, tfilter >::iterator itor
int h() const
Effective map height.
bool terrain_matches(const t_terrain &src, const t_terrain &dest)
Tests whether a specific terrain matches an expression, for matching rules see above.
GLint GLint GLint GLint GLint x
bool is_castle(const map_location &loc) const
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.
void write_terrain(const map_location &loc, config &cfg) const
Writes the terrain at loc to cfg.
std::string write_terrain_code(const t_terrain &tcode)
Writes a single terrain code to a string.
unsigned child_count(const std::string &key) const
t_translation::t_terrain get_terrain(const map_location &loc) const
Looks up terrain at a particular location.
GLint GLint GLsizei GLsizei GLsizei GLint border
bool on_board(const map_location &loc) const
Tell if a location is on the map.
const t_terrain VOID_TERRAIN
tstarting_positions starting_positions_
The size of the starting positions array is MAX_PLAYERS + 1, because the positions themselves are num...
int read_header(const std::string &data)
Reads the header of a map which is saved in the deprecated map_data format.
void remove_from_border_cache(const map_location &loc)
Remove the cached border terrain at loc.
config & child(const std::string &key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
int num_valid_starting_positions() const
int total_width_
Sizes of the map including the borders.
Standard logging facilities (interface).
void set_starting_position(int side, const map_location &loc)
Manipulate starting positions of the different sides.
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.
const t_translation::t_list & underlying_def_terrain(const map_location &loc) const
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.
const std::string remove
remove directive
std::vector< std::string > split(std::string const &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
t_translation::t_map tiles_
A config object defines a single node in a WML file, with access to child nodes.
const t_translation::t_list & underlying_mvt_terrain(const map_location &loc) const
GLsizei const GLcharARB ** string
bool is_keep(const map_location &loc) const
std::vector< t_terrain > t_list
int gives_healing(const map_location &loc) const
const std::string valid
Little parts of regex templates used to parse Wml annoations.