21 #define GETTEXT_DOMAIN "wesnoth-lib"
30 #define ERR_G LOG_STREAM(err, lg::general())
31 #define WRN_G LOG_STREAM(warn, lg::general())
62 static terrain_code
get_mask_(
const terrain_code& terrain);
90 static terrain_code
string_to_number_(std::string_view str, std::vector<std::string>& start_positions,
const ter_layer filler);
104 static std::string
number_to_string_(terrain_code terrain,
const std::vector<std::string>& start_position = std::vector<std::string>());
178 is_empty(terrain.empty())
184 for(std::size_t
i = 0;
i <
terrain.size();
i++) {
195 is_empty(terrain.empty())
200 for(std::size_t
i = 0;
i <
terrain.size();
i++) {
225 std::size_t offset = 0;
226 while(offset < str.length()) {
229 const std::string separators =
",";
230 const size_t pos_separator = str.find_first_of(separators, offset);
231 std::string_view terrain = str.substr(offset, pos_separator - offset);
237 result.push_back(tile);
240 if(pos_separator == std::string_view::npos) {
241 offset = str.length();
243 offset = pos_separator + 1;
252 std::stringstream result;
254 ter_list::const_iterator itor = list.begin();
255 for( ; itor != list.end(); ++itor) {
256 if(itor == list.begin()) {
266 static std::pair<int, int>
get_map_size(
const char* begin,
const char* end)
270 for (
const char* it = begin; it != end;) {
275 for (;it != end && (*it !=
'\n' && *it !=
'\r'); ++it) {
280 w = std::max(
w, cur_w);
282 while (it != end && (*it ==
'\n' || *it ==
'\r')) {
292 std::size_t offset = 0;
293 int x = 0, y = 0, width = 0;
297 str.remove_prefix(1);
301 if(str.length() <= 1) {
305 auto map_size =
get_map_size(&str[0], &str[0] + str.size());
306 ter_map result(map_size.first, map_size.second);
308 while(offset < str.length()) {
311 const std::string separators =
",\n\r";
312 const std::size_t pos_separator = str.find_first_of(separators, offset);
313 std::string_view terrain = str.substr(offset, pos_separator - offset);
316 std::vector<std::string>
sp;
321 for(
const auto& starting_position :
sp) {
323 WRN_G <<
"Starting position " << starting_position <<
" is redefined.";
328 if(result.
w <= x || result.
h <= y) {
329 throw error(
"Map not a rectangle.");
333 result.
get(x, y) = tile;
336 if(pos_separator == std::string::npos ||
utils::isnewline(str[pos_separator])) {
342 if((x + 1) != width ) {
343 ERR_G <<
"Map not a rectangle error occurred at line offset " << y <<
" position offset " << x;
344 throw error(
"Map not a rectangle.");
348 throw error(
"Map height limit exceeded.");
357 if(pos_separator == std::string::npos) {
358 offset = str.length();
362 offset = pos_separator + 1;
371 offset = pos_separator + 1;
374 throw error(
"Map width limit exceeded.");
380 if(x != 0 && (x + 1) != width) {
381 ERR_G <<
"Map not a rectangle error occurred at the end";
382 throw error(
"Map not a rectangle.");
390 std::stringstream str;
392 for(
int y = 0; y < map.
h; ++y) {
393 for(
int x = 0; x < map.
w; ++x) {
399 std::vector<std::string>
sp;
402 sp.push_back(pair.second);
435 PLAIN_LOG << std::hex <<
"src = " <<
src.base <<
"^" <<
src.overlay <<
"\t"
436 << src_mask.base <<
"^" << src_mask.overlay <<
"\t"
437 << masked_src.base <<
"^" << masked_src.overlay <<
"\t"
438 << src_has_wildcard <<
"\n";
442 ter_list::const_iterator itor = dest.begin();
445 for(; itor != dest.end(); ++itor) {
469 << itor->base <<
"^" << itor->overlay <<
"\t"
470 << dest_mask.
base <<
"^" << dest_mask.
overlay <<
"\t"
471 << masked_dest.
base <<
"^" << masked_dest.
overlay <<
"\t"
472 << dest_has_wildcard <<
"\n";
474 if(dest_has_wildcard &&
520 ter_list::const_iterator end = dest.
terrain.end();
521 for(ter_list::const_iterator terrain_itor = dest.
terrain.begin();
523 ++
i, ++terrain_itor) {
526 if(*terrain_itor ==
STAR) {
531 if(terrain_itor->base ==
NOT.
base) {
537 if(*terrain_itor ==
src) {
587 ter_list::const_iterator itor = list.begin();
588 for(; itor != list.end(); ++itor) {
600 std::size_t offset = 0;
606 if((offset + 1) >= str.length()) {
610 auto map_size =
get_map_size(&str[offset], str.c_str() + str.size());
614 while(offset < str.length()) {
617 const std::string separators =
",\n\r";
618 const std::size_t pos_separator = str.find_first_of(separators, offset);
619 std::string terrain =
"";
622 if(pos_separator != offset) {
623 terrain = str.substr(offset, pos_separator - offset);
630 if (result.
h <= x || result.
w <= y) {
631 throw error(
"Map not a rectangle.");
635 result.
get(y, x) = tile;
638 if(pos_separator == std::string::npos) {
644 offset = str.length();
650 offset = pos_separator + 1;
658 offset = pos_separator + 1;
677 if((terrain & 0xFF000000) == 0x2A000000)
return 0x00000000;
678 if((terrain & 0x00FF0000) == 0x002A0000)
return 0xFF000000;
679 if((terrain & 0x0000FF00) == 0x00002A00)
return 0xFFFF0000;
680 if((terrain & 0x000000FF) == 0x0000002A)
return 0xFFFFFF00;
710 throw error(
"A terrain with a string with more "
711 "than 4 characters has been found, the affected terrain is: " + std::string(str));
719 for(std::size_t
i = 0;
i < 4; ++
i) {
720 const unsigned char c = (
i < str.size()) ? str[
i] : 0;
734 std::vector<std::string>
dummy;
750 std::size_t offset = str.find(
' ', 0);
751 while(offset != std::string::npos) {
752 start_positions.push_back(std::string(str.substr(0, offset)));
753 str.remove_prefix(offset + 1);
754 offset = str.find(
' ', 0);
757 offset = str.find(
'^', 0);
758 if(offset != std::string::npos) {
769 std::string result =
"";
772 for (
const std::string& str : start_positions) {
773 result = str +
" " + result;
781 unsigned char tcode[9] {0};
783 tcode[0] = ((terrain.
base & 0xFF000000) >> 24);
784 tcode[1] = ((terrain.
base & 0x00FF0000) >> 16);
785 tcode[2] = ((terrain.
base & 0x0000FF00) >> 8);
786 tcode[3] = (terrain.
base & 0x000000FF);
790 tcode[5] = ((terrain.
overlay & 0xFF000000) >> 24);
791 tcode[6] = ((terrain.
overlay & 0x00FF0000) >> 16);
792 tcode[7] = ((terrain.
overlay & 0x0000FF00) >> 8);
793 tcode[8] = (terrain.
overlay & 0x000000FF);
800 for(
int i = 0;
i < 9; ++
i) {
801 if(tcode[
i] != 0 && tcode[
i] != 0xFF) {
804 if(
i == 4 && tcode[
i] == 0) {
816 const std::string& whitespace =
" \t";
817 str.erase(0, str.find_first_not_of(whitespace));
819 str.erase(str.find_last_not_of(whitespace) + 1);
844 int main(
int argc,
char** argv)
848 if(std::string(argv[1]) ==
"match" && argc == 4) {
854 std::cout <<
"Match\n" ;
856 std::cout <<
"No match\n";
New lexcical_cast header.
To lexical_cast_default(From value, To fallback=To())
Lexical cast converts one type to another with a fallback.
Standard logging facilities (interface).
static std::pair< int, int > get_map_size(const char *begin, const char *end)
terrain_code read_terrain_code(std::string_view str, const ter_layer filler)
Reads a single terrain from a string.
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 CAVE_WALL
const ter_match ALL_FORESTS
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 VOID_TERRAIN
VOID_TERRAIN is used for shrouded hexes.
const terrain_code SHALLOW_WATER
const terrain_code DWARVEN_KEEP
static ter_layer string_to_layer_(std::string_view str)
const terrain_code UNDERGROUND_VILLAGE
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.
const terrain_code HUMAN_CASTLE
int max_map_size()
Return the maximum allowed map size (in either dimension), the maximum map area is,...
bool has_wildcard(const terrain_code &tcode)
Tests whether a terrain code contains a wildcard.
bool terrain_matches(const terrain_code &src, const terrain_code &dest)
Tests whether a specific terrain matches an expression, for matching rules see above.
std::vector< terrain_code > ter_list
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.
const terrain_code GRASS_LAND
const ter_match ALL_OFF_MAP
static terrain_code string_to_builder_number_(std::string str)
Converts a terrain string to a number for the builder.
const terrain_code DEEP_WATER
boost::bimaps::bimap< boost::bimaps::set_of< std::string >, boost::bimaps::multiset_of< coordinate > > starting_positions
const ter_match ALL_HILLS("!,*^V*,!,H*")
ter_map read_builder_map(const std::string &str)
Reads a builder map.
const terrain_code FOREST
ter_list read_list(std::string_view str, const ter_layer filler)
Reads a list of terrains from a string, when reading the.
static ter_layer get_layer_mask_(ter_layer terrain)
Get the mask for a single layer.
const ter_match ALL_MOUNTAINS("!,*^V*,!,M*")
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
const terrain_code MOUNTAIN
const terrain_code DWARVEN_CASTLE
std::string write_terrain_code(const terrain_code &tcode)
Writes a single terrain code to a string.
const terrain_code FOGGED
static terrain_code get_mask_(const terrain_code &terrain)
Gets a mask for a terrain, this mask is used for wildcard matching.
const terrain_code OFF_MAP_USER
std::string write_list(const ter_list &list)
Writes a list of terrains to a string, only writes the new format.
const terrain_code HUMAN_KEEP
void trim(std::string_view &s)
bool isnewline(const char c)
rect src
Non-transparent portion of the surface to compose.
Encapsulates the map of the game.
terrain_code & get(int x, int y)
This structure can be used for matching terrain strings.
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...