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);
91 static terrain_code
string_to_number_(std::string_view str, std::vector<std::string>& start_positions,
const ter_layer filler);
105 static std::string
number_to_string_(terrain_code terrain,
const std::vector<std::string>& start_position = std::vector<std::string>());
179 is_empty(terrain.empty())
185 for(std::size_t
i = 0;
i <
terrain.size();
i++) {
196 is_empty(terrain.empty())
201 for(std::size_t
i = 0;
i <
terrain.size();
i++) {
226 std::size_t offset = 0;
227 while(offset < str.length()) {
230 const std::string separators =
",";
231 const size_t pos_separator = str.find_first_of(separators, offset);
232 std::string_view terrain = str.substr(offset, pos_separator - offset);
238 result.push_back(tile);
241 if(pos_separator == std::string_view::npos) {
242 offset = str.length();
244 offset = pos_separator + 1;
253 std::stringstream result;
255 ter_list::const_iterator itor = list.begin();
256 for( ; itor != list.end(); ++itor) {
257 if(itor == list.begin()) {
267 static std::pair<int, int>
get_map_size(
const char* begin,
const char* end)
271 for (
const char* it = begin; it != end;) {
276 for (;it != end && (*it !=
'\n' && *it !=
'\r'); ++it) {
281 w = std::max(
w, cur_w);
283 while (it != end && (*it ==
'\n' || *it ==
'\r')) {
293 std::size_t offset = 0;
294 int x = 0, y = 0, width = 0;
298 str.remove_prefix(1);
302 if(str.length() <= 1) {
306 auto map_size =
get_map_size(&str[0], &str[0] + str.size());
307 ter_map result(map_size.first, map_size.second);
309 while(offset < str.length()) {
312 const std::string separators =
",\n\r";
313 const std::size_t pos_separator = str.find_first_of(separators, offset);
314 std::string_view terrain = str.substr(offset, pos_separator - offset);
317 std::vector<std::string>
sp;
322 for(
const auto& starting_position :
sp) {
324 WRN_G <<
"Starting position " << starting_position <<
" is redefined.";
329 if(result.
w <= x || result.
h <= y) {
330 throw error(
"Map not a rectangle.");
334 result.
get(x, y) = tile;
337 if(pos_separator == std::string::npos ||
utils::isnewline(str[pos_separator])) {
343 if((x + 1) != width ) {
344 ERR_G <<
"Map not a rectangle error occurred at line offset " << y <<
" position offset " << x;
345 throw error(
"Map not a rectangle.");
349 throw error(
"Map height limit exceeded.");
358 if(pos_separator == std::string::npos) {
359 offset = str.length();
363 offset = pos_separator + 1;
372 offset = pos_separator + 1;
375 throw error(
"Map width limit exceeded.");
381 if(x != 0 && (x + 1) != width) {
382 ERR_G <<
"Map not a rectangle error occurred at the end";
383 throw error(
"Map not a rectangle.");
391 std::stringstream str;
393 for(
int y = 0; y < map.
h; ++y) {
394 for(
int x = 0; x < map.
w; ++x) {
400 std::vector<std::string>
sp;
403 sp.push_back(pair.second);
436 PLAIN_LOG << std::hex <<
"src = " <<
src.base <<
"^" <<
src.overlay <<
"\t"
437 << src_mask.base <<
"^" << src_mask.overlay <<
"\t"
438 << masked_src.base <<
"^" << masked_src.overlay <<
"\t"
439 << src_has_wildcard <<
"\n";
443 ter_list::const_iterator itor = dest.begin();
446 for(; itor != dest.end(); ++itor) {
470 << itor->base <<
"^" << itor->overlay <<
"\t"
471 << dest_mask.
base <<
"^" << dest_mask.
overlay <<
"\t"
472 << masked_dest.
base <<
"^" << masked_dest.
overlay <<
"\t"
473 << dest_has_wildcard <<
"\n";
475 if(dest_has_wildcard &&
521 ter_list::const_iterator end = dest.
terrain.end();
522 for(ter_list::const_iterator terrain_itor = dest.
terrain.begin();
524 ++
i, ++terrain_itor) {
527 if(*terrain_itor ==
STAR) {
532 if(*terrain_itor ==
NOT) {
538 if(*terrain_itor ==
src) {
588 ter_list::const_iterator itor = list.begin();
589 for(; itor != list.end(); ++itor) {
601 std::size_t offset = 0;
607 if((offset + 1) >= str.length()) {
611 auto map_size =
get_map_size(&str[offset], str.c_str() + str.size());
615 while(offset < str.length()) {
618 const std::string separators =
",\n\r";
619 const std::size_t pos_separator = str.find_first_of(separators, offset);
620 std::string terrain =
"";
623 if(pos_separator != offset) {
624 terrain = str.substr(offset, pos_separator - offset);
631 if (result.
h <= x || result.
w <= y) {
632 throw error(
"Map not a rectangle.");
636 result.
get(y, x) = tile;
639 if(pos_separator == std::string::npos) {
645 offset = str.length();
651 offset = pos_separator + 1;
659 offset = pos_separator + 1;
678 if((terrain & 0xFF000000) == 0x2A000000)
return 0x00000000;
679 if((terrain & 0x00FF0000) == 0x002A0000)
return 0xFF000000;
680 if((terrain & 0x0000FF00) == 0x00002A00)
return 0xFFFF0000;
681 if((terrain & 0x000000FF) == 0x0000002A)
return 0xFFFFFF00;
711 throw error(
"A terrain with a string with more "
712 "than 4 characters has been found, the affected terrain is: " + std::string(str));
720 for(std::size_t
i = 0;
i < 4; ++
i) {
721 const unsigned char c = (
i < str.size()) ? str[
i] : 0;
735 std::vector<std::string>
dummy;
751 std::size_t offset = str.find(
' ', 0);
752 while(offset != std::string::npos) {
753 start_positions.push_back(std::string(str.substr(0, offset)));
754 str.remove_prefix(offset + 1);
755 offset = str.find(
' ', 0);
758 offset = str.find(
'^', 0);
759 if(offset != std::string::npos) {
777 std::string result =
"";
780 for (
const std::string& str : start_positions) {
781 result = str +
" " + result;
789 unsigned char tcode[9] {0};
791 tcode[0] = ((terrain.
base & 0xFF000000) >> 24);
792 tcode[1] = ((terrain.
base & 0x00FF0000) >> 16);
793 tcode[2] = ((terrain.
base & 0x0000FF00) >> 8);
794 tcode[3] = (terrain.
base & 0x000000FF);
798 tcode[5] = ((terrain.
overlay & 0xFF000000) >> 24);
799 tcode[6] = ((terrain.
overlay & 0x00FF0000) >> 16);
800 tcode[7] = ((terrain.
overlay & 0x0000FF00) >> 8);
801 tcode[8] = (terrain.
overlay & 0x000000FF);
808 for(
int i = 0;
i < 9; ++
i) {
809 if(tcode[
i] != 0 && tcode[
i] != 0xFF) {
812 if(
i == 4 && tcode[
i] == 0) {
824 const std::string& whitespace =
" \t";
825 str.erase(0, str.find_first_not_of(whitespace));
827 str.erase(str.find_last_not_of(whitespace) + 1);
852 int main(
int argc,
char** argv)
856 if(std::string(argv[1]) ==
"match" && argc == 4) {
862 std::cout <<
"Match\n" ;
864 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...