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);
85 static terrain_code
string_to_number_(std::string_view str, std::vector<std::string>& start_positions,
const ter_layer filler);
99 static std::string
number_to_string_(terrain_code terrain,
const std::vector<std::string>& start_position = std::vector<std::string>());
169 , is_empty(terrain.empty())
174 for(std::size_t
i = 0;
i <
terrain.size();
i++) {
185 , is_empty(terrain.empty())
190 for(std::size_t
i = 0;
i <
terrain.size();
i++) {
215 std::size_t offset = 0;
216 while(offset < str.length()) {
219 const std::string separators =
",";
220 const std::size_t pos_separator = str.find_first_of(separators, offset);
221 std::string_view terrain = str.substr(offset, pos_separator - offset);
227 result.push_back(tile);
230 if(pos_separator == std::string_view::npos) {
231 offset = str.length();
233 offset = pos_separator + 1;
242 std::stringstream result;
244 ter_list::const_iterator itor = list.begin();
245 for( ; itor != list.end(); ++itor) {
246 if(itor == list.begin()) {
256 static std::pair<int, int>
get_map_size(
const char* begin,
const char* end)
260 for (
const char* it = begin; it != end;) {
265 for (;it != end && (*it !=
'\n' && *it !=
'\r'); ++it) {
270 w = std::max(
w, cur_w);
272 while (it != end && (*it ==
'\n' || *it ==
'\r')) {
282 std::size_t offset = 0;
283 int x = 0, y = 0, width = 0;
287 str.remove_prefix(1);
291 if(str.length() <= 1) {
295 auto map_size =
get_map_size(&str[0], &str[0] + str.size());
296 ter_map result(map_size.first, map_size.second);
298 while(offset < str.length()) {
301 const std::string separators =
",\n\r";
302 const std::size_t pos_separator = str.find_first_of(separators, offset);
303 std::string_view terrain = str.substr(offset, pos_separator - offset);
306 std::vector<std::string>
sp;
311 for(
const auto& starting_position :
sp) {
313 WRN_G <<
"Starting position " << starting_position <<
" is redefined.";
318 if(result.
w <= x || result.
h <= y) {
319 throw error(
"Map not a rectangle.");
323 result.
get(x, y) = tile;
326 if(pos_separator == std::string::npos ||
utils::isnewline(str[pos_separator])) {
332 if((x + 1) != width ) {
333 ERR_G <<
"Map not a rectangle error occurred at line offset " << y <<
" position offset " << x;
334 throw error(
"Map not a rectangle.");
338 throw error(
"Map height limit exceeded.");
347 if(pos_separator == std::string::npos) {
348 offset = str.length();
352 offset = pos_separator + 1;
361 offset = pos_separator + 1;
364 throw error(
"Map width limit exceeded.");
370 if(x != 0 && (x + 1) != width) {
371 ERR_G <<
"Map not a rectangle error occurred at the end";
372 throw error(
"Map not a rectangle.");
380 std::stringstream str;
382 for(
int y = 0; y < map.
h; ++y) {
383 for(
int x = 0; x < map.
w; ++x) {
389 std::vector<std::string>
sp;
392 sp.push_back(pair.second);
425 PLAIN_LOG << std::hex <<
"src = " <<
src.base <<
"^" <<
src.overlay <<
"\t"
426 << src_mask.base <<
"^" << src_mask.overlay <<
"\t"
427 << masked_src.base <<
"^" << masked_src.overlay <<
"\t"
428 << src_has_wildcard <<
"\n";
432 ter_list::const_iterator itor = dest.begin();
435 for(; itor != dest.end(); ++itor) {
459 << itor->base <<
"^" << itor->overlay <<
"\t"
460 << dest_mask.
base <<
"^" << dest_mask.
overlay <<
"\t"
461 << masked_dest.
base <<
"^" << masked_dest.
overlay <<
"\t"
462 << dest_has_wildcard <<
"\n";
464 if(dest_has_wildcard &&
510 ter_list::const_iterator end = dest.
terrain.end();
511 for(ter_list::const_iterator terrain_itor = dest.
terrain.begin();
513 ++
i, ++terrain_itor) {
516 if(*terrain_itor ==
STAR) {
521 if(terrain_itor->base ==
NOT.
base) {
527 if(*terrain_itor ==
src) {
577 ter_list::const_iterator itor = list.begin();
578 for(; itor != list.end(); ++itor) {
590 std::size_t offset = 0;
596 if((offset + 1) >= str.length()) {
600 auto map_size =
get_map_size(&str[offset], str.c_str() + str.size());
604 while(offset < str.length()) {
607 const std::string separators =
",\n\r";
608 const std::size_t pos_separator = str.find_first_of(separators, offset);
609 std::string terrain =
"";
612 if(pos_separator != offset) {
613 terrain = str.substr(offset, pos_separator - offset);
620 if (result.
h <= x || result.
w <= y) {
621 throw error(
"Map not a rectangle.");
625 result.
get(y, x) = tile;
628 if(pos_separator == std::string::npos) {
634 offset = str.length();
640 offset = pos_separator + 1;
648 offset = pos_separator + 1;
667 if((terrain & 0xFF000000) == 0x2A000000)
return 0x00000000;
668 if((terrain & 0x00FF0000) == 0x002A0000)
return 0xFF000000;
669 if((terrain & 0x0000FF00) == 0x00002A00)
return 0xFFFF0000;
670 if((terrain & 0x000000FF) == 0x0000002A)
return 0xFFFFFF00;
700 throw error(
"A terrain with a string with more "
701 "than 4 characters has been found, the affected terrain is: " + std::string(str));
709 for(std::size_t
i = 0;
i < 4; ++
i) {
710 const unsigned char c = (
i < str.size()) ? str[
i] : 0;
724 std::vector<std::string>
dummy;
738 std::size_t offset = str.find(
' ', 0);
739 while(offset != std::string::npos) {
740 start_positions.emplace_back(str.substr(0, offset));
741 str.remove_prefix(offset + 1);
742 offset = str.find(
' ', 0);
745 offset = str.find(
'^', 0);
746 if(offset != std::string::npos) {
755 std::string result =
"";
758 for (
const std::string& str : start_positions) {
759 result = str +
" " + result;
767 unsigned char tcode[9] {0};
769 tcode[0] = ((terrain.
base & 0xFF000000) >> 24);
770 tcode[1] = ((terrain.
base & 0x00FF0000) >> 16);
771 tcode[2] = ((terrain.
base & 0x0000FF00) >> 8);
772 tcode[3] = (terrain.
base & 0x000000FF);
776 tcode[5] = ((terrain.
overlay & 0xFF000000) >> 24);
777 tcode[6] = ((terrain.
overlay & 0x00FF0000) >> 16);
778 tcode[7] = ((terrain.
overlay & 0x0000FF00) >> 8);
779 tcode[8] = (terrain.
overlay & 0x000000FF);
786 for(
int i = 0;
i < 9; ++
i) {
787 if(tcode[
i] != 0 && tcode[
i] != 0xFF) {
790 if(
i == 4 && tcode[
i] == 0) {
802 const std::string& whitespace =
" \t";
803 str.erase(0, str.find_first_not_of(whitespace));
805 str.erase(str.find_last_not_of(whitespace) + 1);
830 int main(
int argc,
char** argv)
834 if(std::string(argv[1]) ==
"match" && argc == 4) {
840 std::cout <<
"Match\n" ;
842 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
constexpr terrain_code NONE_TERRAIN
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)
Converts a string to a layer.
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
constexpr ter_layer NO_LAYER
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...
constexpr terrain_code()=default