int gamemap::read_header(const std::string& data) { // Test whether there is a header section size_t header_offset = data.find("\n\n"); if(header_offset == std::string::npos) { // For some reason Windows will fail to load a file with \r\n // lineending properly no problems on Linux with those files. // This workaround fixes the problem the copy later will copy // the second \r\n to the map, but that's no problem. header_offset = data.find("\r\n\r\n"); } const size_t comma_offset = data.find(","); // The header shouldn't contain commas, so if the comma is found // before the header, we hit a \n\n inside or after a map. // This is no header, so don't parse it as it would be. if (!(!(header_offset == std::string::npos || comma_offset < header_offset))) return 0; std::string header_str(std::string(data, 0, header_offset + 1)); config header; ::read(header, header_str); border_size_ = header["border_size"]; return header_offset + 2; }
void gamemap::read(const std::string& data) { // Initial stuff tiles_.clear(); villages_.clear(); std::fill(startingPositions_, startingPositions_ + sizeof(startingPositions_) / sizeof(*startingPositions_), map_location()); std::map<int, t_translation::coordinate> starting_positions; if(data.empty()) { w_ = 0; h_ = 0; return; } // Test whether there is a header section size_t header_offset = data.find("\n\n"); if(header_offset == std::string::npos) { // For some reason Windows will fail to load a file with \r\n // lineending properly no problems on Linux with those files. // This workaround fixes the problem the copy later will copy // the second \r\n to the map, but that's no problem. header_offset = data.find("\r\n\r\n"); } const size_t comma_offset = data.find(","); // The header shouldn't contain commas, so if the comma is found // before the header, we hit a \n\n inside or after a map. // This is no header, so don't parse it as it would be. VALIDATE( !(header_offset == std::string::npos || comma_offset < header_offset), _("A map without a header is not supported")); std::string header_str(std::string(data, 0, header_offset + 1)); config header; ::read(header, header_str); border_size_ = lexical_cast_default<int>(header["border_size"], 0); const std::string usage = header["usage"]; utils::string_map symbols; symbols["border_size_key"] = "border_size"; symbols["usage_key"] = "usage"; symbols["usage_val"] = usage; const std::string msg = "'$border_size_key|' should be " "'$border_size_val|' when '$usage_key| = $usage_val|'"; if(usage == "map") { usage_ = IS_MAP; symbols["border_size_val"] = "1"; VALIDATE(border_size_ == 1, vgettext(msg.c_str(), symbols)); } else if(usage == "mask") { usage_ = IS_MASK; symbols["border_size_val"] = "0"; VALIDATE(border_size_ == 0, vgettext(msg.c_str(), symbols)); } else if(usage == "") { throw incorrect_map_format_exception("Map has a header but no usage"); } else { std::string msg = "Map has a header but an unknown usage:" + usage; throw incorrect_map_format_exception(msg.c_str()); } /* The third parameter is required for MSVC++ 6.0 */ const std::string& map = std::string(data, header_offset + 2, std::string::npos); try { tiles_ = t_translation::read_game_map(map, starting_positions); } catch(t_translation::error& e) { // We re-throw the error but as map error. // Since all codepaths test for this, it's the least work. throw incorrect_map_format_exception(e.message); } // Convert the starting positions to the array std::map<int, t_translation::coordinate>::const_iterator itor = starting_positions.begin(); for(; itor != starting_positions.end(); ++itor) { // Check for valid position, // the first valid position is 1, // so the offset 0 in the array is never used. if(itor->first < 1 || itor->first >= MAX_PLAYERS+1) { std::stringstream ss; ss << "Starting position " << itor->first << " out of range\n"; ERR_CF << ss.str(); ss << "The map cannot be loaded."; throw incorrect_map_format_exception(ss.str().c_str()); } // Add to the starting position array startingPositions_[itor->first] = map_location(itor->second.x - 1, itor->second.y - 1); } // Post processing on the map total_width_ = tiles_.size(); total_height_ = total_width_ > 0 ? tiles_[0].size() : 0; w_ = total_width_ - 2 * border_size_; h_ = total_height_ - 2 * border_size_; for(int x = 0; x < total_width_; ++x) { for(int y = 0; y < total_height_; ++y) { // Is the terrain valid? if(tcodeToTerrain_.count(tiles_[x][y]) == 0) { if(!try_merge_terrains(tiles_[x][y])) { std::stringstream ss; ss << "Illegal character in map: (" << t_translation::write_terrain_code(tiles_[x][y]) << ") '" << tiles_[x][y] << "'\n"; ERR_CF << ss.str(); ss << "The map cannot be loaded."; throw incorrect_map_format_exception(ss.str().c_str()); } } // Is it a village? if(x >= border_size_ && y >= border_size_ && x < total_width_-border_size_ && y < total_height_-border_size_ && is_village(tiles_[x][y])) { villages_.push_back(map_location(x-border_size_, y-border_size_)); } } } }