21 #define GETTEXT_DOMAIN "wesnoth-lib" 32 #define ERR_G LOG_STREAM(err, lg::general()) 33 #define WRN_G LOG_STREAM(warn, lg::general()) 161 base(string_to_layer_(b)),
overlay(o)
165 base(string_to_layer_(b)),
overlay(string_to_layer_(o))
184 mask.resize(terrain.size());
187 for(std::size_t
i = 0;
i < terrain.size();
i++) {
200 mask.resize(terrain.size());
203 for(std::size_t
i = 0;
i < terrain.size();
i++) {
228 std::size_t offset = 0;
229 while(offset < str.length()) {
232 const std::string separators =
",";
233 const size_t pos_separator = str.find_first_of(separators, offset);
234 std::string_view terrain = str.substr(offset, pos_separator - offset);
240 result.push_back(tile);
243 if(pos_separator == std::string_view::npos) {
244 offset = str.length();
246 offset = pos_separator + 1;
255 std::stringstream result;
257 ter_list::const_iterator itor = list.begin();
258 for( ; itor != list.end(); ++itor) {
259 if(itor == list.begin()) {
269 static std::pair<int, int>
get_map_size(
const char* begin,
const char* end)
273 for (
const char* it = begin; it != end;) {
278 for (;it != end && (*it !=
'\n' && *it !=
'\r'); ++it) {
283 w = std::max(w, cur_w);
285 while (it != end && (*it ==
'\n' || *it ==
'\r')) {
295 std::size_t offset = 0;
296 int x = 0, y = 0, width = 0;
300 str.remove_prefix(1);
304 if(str.length() <= 1) {
308 auto map_size =
get_map_size(&str[0], &str[0] + str.size());
309 ter_map result(map_size.first, map_size.second);
311 while(offset < str.length()) {
314 const std::string separators =
",\n\r";
315 const std::size_t pos_separator = str.find_first_of(separators, offset);
316 std::string_view terrain = str.substr(offset, pos_separator - offset);
319 std::vector<std::string>
sp;
324 for(
const auto& starting_position : sp) {
325 if (starting_positions.left.find(starting_position) != starting_positions.left.end()) {
326 WRN_G <<
"Starting position " << starting_position <<
" is redefined.";
328 starting_positions.insert(starting_positions::value_type(starting_position,
coordinate(x - border_offset.
x, y - border_offset.
y)));
331 if(result.w <= x || result.h <= y) {
332 throw error(
"Map not a rectangle.");
336 result.get(x, y) = tile;
339 if(pos_separator == std::string::npos ||
utils::isnewline(str[pos_separator])) {
345 if((x + 1) != width ) {
346 ERR_G <<
"Map not a rectangle error occurred at line offset " << y <<
" position offset " << x;
347 throw error(
"Map not a rectangle.");
351 throw error(
"Map height limit exceeded.");
360 if(pos_separator == std::string::npos) {
361 offset = str.length();
365 offset = pos_separator + 1;
374 offset = pos_separator + 1;
377 throw error(
"Map width limit exceeded.");
383 if(x != 0 && (x + 1) != width) {
384 ERR_G <<
"Map not a rectangle error occurred at the end";
385 throw error(
"Map not a rectangle.");
393 std::stringstream str;
395 for(
int y = 0; y < map.
h; ++y) {
396 for(
int x = 0; x < map.
w; ++x) {
402 std::vector<std::string>
sp;
404 for(
const auto& pair : starting_positions.right.equal_range(
coordinate(x - border_offset.
x, y - border_offset.
y))) {
405 sp.push_back(pair.second);
439 << src_mask.base <<
"^" << src_mask.overlay <<
"\t" 440 << masked_src.base <<
"^" << masked_src.overlay <<
"\t" 441 << src_has_wildcard <<
"\n";
445 ter_list::const_iterator itor = dest.begin();
448 for(; itor != dest.end(); ++itor) {
472 << itor->base <<
"^" << itor->overlay <<
"\t" 473 << dest_mask.
base <<
"^" << dest_mask.
overlay <<
"\t" 474 << masked_dest.
base <<
"^" << masked_dest.
overlay <<
"\t" 475 << dest_has_wildcard <<
"\n";
477 if(dest_has_wildcard &&
523 ter_list::const_iterator end = dest.
terrain.end();
524 for(ter_list::const_iterator terrain_itor = dest.
terrain.begin();
526 ++
i, ++terrain_itor) {
529 if(*terrain_itor == STAR) {
534 if(*terrain_itor == NOT) {
540 if(*terrain_itor == src) {
590 ter_list::const_iterator itor = list.begin();
591 for(; itor != list.end(); ++itor) {
603 std::size_t offset = 0;
609 if((offset + 1) >= str.length()) {
613 auto map_size =
get_map_size(&str[offset], str.c_str() + str.size());
617 while(offset < str.length()) {
620 const std::string separators =
",\n\r";
621 const std::size_t pos_separator = str.find_first_of(separators, offset);
622 std::string terrain =
"";
625 if(pos_separator != offset) {
626 terrain = str.substr(offset, pos_separator - offset);
633 if (result.h <= x || result.w <= y) {
634 throw error(
"Map not a rectangle.");
638 result.get(y, x) = tile;
641 if(pos_separator == std::string::npos) {
647 offset = str.length();
653 offset = pos_separator + 1;
661 offset = pos_separator + 1;
680 if((terrain & 0xFF000000) == 0x2A000000)
return 0x00000000;
681 if((terrain & 0x00FF0000) == 0x002A0000)
return 0xFF000000;
682 if((terrain & 0x0000FF00) == 0x00002A00)
return 0xFFFF0000;
683 if((terrain & 0x000000FF) == 0x0000002A)
return 0xFFFFFF00;
713 throw error(
"A terrain with a string with more " 714 "than 4 characters has been found, the affected terrain is: " + std::string(str));
722 for(std::size_t
i = 0;
i < 4; ++
i) {
723 const unsigned char c = (
i < str.size()) ? str[
i] : 0;
737 std::vector<std::string> dummy;
753 std::size_t offset = str.find(
' ', 0);
754 while(offset != std::string::npos) {
755 start_positions.push_back(std::string(str.substr(0, offset)));
756 str.remove_prefix(offset + 1);
757 offset = str.find(
' ', 0);
760 offset = str.find(
'^', 0);
761 if(offset != std::string::npos) {
779 std::string result =
"";
782 for (
const std::string& str : start_positions) {
783 result = str +
" " + result;
791 unsigned char tcode[9] {0};
793 tcode[0] = ((terrain.
base & 0xFF000000) >> 24);
794 tcode[1] = ((terrain.
base & 0x00FF0000) >> 16);
795 tcode[2] = ((terrain.
base & 0x0000FF00) >> 8);
796 tcode[3] = (terrain.
base & 0x000000FF);
800 tcode[5] = ((terrain.
overlay & 0xFF000000) >> 24);
801 tcode[6] = ((terrain.
overlay & 0x00FF0000) >> 16);
802 tcode[7] = ((terrain.
overlay & 0x0000FF00) >> 8);
803 tcode[8] = (terrain.
overlay & 0x000000FF);
810 for(
int i = 0;
i < 9; ++
i) {
811 if(tcode[
i] != 0 && tcode[
i] != 0xFF) {
814 if(
i == 4 && tcode[
i] == 0) {
826 const std::string& whitespace =
" \t";
827 str.erase(0, str.find_first_not_of(whitespace));
829 str.erase(str.find_last_not_of(whitespace) + 1);
854 int main(
int argc,
char** argv)
858 if(std::string(argv[1]) ==
"match" && argc == 4) {
864 std::cout <<
"Match\n" ;
866 std::cout <<
"No match\n";
const terrain_code FOREST
boost::bimaps::bimap< boost::bimaps::set_of< std::string >, boost::bimaps::multiset_of< coordinate > > starting_positions
const terrain_code DWARVEN_KEEP
int main(int, char **argv)
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
bool terrain_matches(const terrain_code &src, const terrain_code &dest)
Tests whether a specific terrain matches an expression, for matching rules see above.
New lexcical_cast header.
To lexical_cast_default(From value, To fallback=To())
Lexical cast converts one type to another with a fallback.
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
ter_map read_game_map(std::string_view str, starting_positions &starting_positions, coordinate border_offset)
Reads a gamemap string into a 2D vector.
const terrain_code HUMAN_KEEP
const ter_match ALL_MOUNTAINS("!,*^V*,!,M*")
terrain_code read_terrain_code(std::string_view str, const ter_layer filler)
Reads a single terrain from a string.
const terrain_code VOID_TERRAIN
VOID_TERRAIN is used for shrouded hexes.
static std::string number_to_string_(terrain_code terrain, const std::vector< std::string > &start_position=std::vector< std::string >())
Converts a terrain number to a string.
int max_map_size()
Return the maximum allowed map size (in either dimension), the maximum map area is, therefore, this value squared.
const terrain_code MOUNTAIN
const terrain_code FOGGED
const terrain_code DWARVEN_CASTLE
const ter_match ALL_HILLS("!,*^V*,!,H*")
const terrain_code OFF_MAP_USER
static terrain_code string_to_builder_number_(std::string str)
Converts a terrain string to a number for the builder.
std::string write_terrain_code(const terrain_code &tcode)
Writes a single terrain code to a string.
const terrain_code HUMAN_CASTLE
static ter_layer string_to_layer_(std::string_view str)
Encapsulates the map of the game.
static ter_layer get_layer_mask_(ter_layer terrain)
Get the mask for a single layer.
const ter_match ALL_SWAMPS("!,*^V*,*^B*,!,S*")
static terrain_code string_to_number_(std::string_view str, std::vector< std::string > &start_positions, const ter_layer filler)
Converts a terrain string to a number.
ter_map read_builder_map(const std::string &str)
Reads a builder map.
std::string write_list(const ter_list &list)
Writes a list of terrains to a string, only writes the new format.
static std::pair< int, int > get_map_size(const char *begin, const char *end)
const terrain_code CAVE_WALL
bool has_wildcard(const terrain_code &tcode)
Tests whether a terrain code contains a wildcard.
bool isnewline(const char c)
std::string write_game_map(const ter_map &map, const starting_positions &starting_positions, coordinate border_offset)
Write a gamemap in to a vector string.
const terrain_code DEEP_WATER
const ter_match ALL_OFF_MAP
const terrain_code UNDERGROUND_VILLAGE
Standard logging facilities (interface).
std::vector< terrain_code > ter_list
static terrain_code get_mask_(const terrain_code &terrain)
Gets a mask for a terrain, this mask is used for wildcard matching.
void trim(std::string_view &s)
const terrain_code SHALLOW_WATER
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
This structure can be used for matching terrain strings.
const terrain_code GRASS_LAND
ter_list read_list(std::string_view str, const ter_layer filler)
Reads a list of terrains from a string, when reading the.
const ter_match ALL_FORESTS