Revert recent map code changes related to map mask handling

They break some unit tests and we can't seem to decide whether the tests
are truly at fault (that is beyond the fact that they _should_ certainly
use [terrain] instead of [terrain_mask]) or whether the issue that the
author was trying to fix at the beginning was an issue in the first
place.

This reverts the following commits:

 * commit 6b78073b49
 * commit 2f650419da
 * commit a436c46f13
 * commit fcf8b72efd
 * commit 079692d424
 * commit 730d837296
This commit is contained in:
Ignacio R. Morelle 2015-08-16 00:38:31 -03:00
parent 7fa589c447
commit 987f0157c2
6 changed files with 101 additions and 17 deletions

View file

@ -36,10 +36,10 @@
{VARIABLE Killer_start_hp $Killer.hitpoints}
[terrain]
[terrain_mask]
x,y=$victim_x|,$victim_y|
terrain="Xv"
[/terrain]
mask="Xv"
[/terrain_mask]
{UNIT $victim_side| "Elvish Archer" $victim_x| $victim_y| (hitpoints=1)}
[do_command]
[attack]

View file

@ -44,7 +44,7 @@ editor_map_load_exception wrap_exc(const char* type, const std::string& e_msg, c
}
editor_map::editor_map(const config& terrain_cfg)
: gamemap(boost::make_shared<terrain_type_data>(terrain_cfg), "")
: gamemap(boost::make_shared<terrain_type_data>(terrain_cfg), gamemap::default_map_header)
, selection_()
{
}
@ -70,7 +70,7 @@ editor_map editor_map::from_string(const config& terrain_cfg, const std::string&
}
editor_map::editor_map(const config& terrain_cfg, size_t width, size_t height, const t_translation::t_terrain & filler)
: gamemap(boost::make_shared<terrain_type_data>(terrain_cfg), t_translation::write_game_map(
: gamemap(boost::make_shared<terrain_type_data>(terrain_cfg), gamemap::default_map_header + t_translation::write_game_map(
t_translation::t_map(width + 2, t_translation::t_list(height + 2, filler))))
, selection_()
{

View file

@ -983,7 +983,7 @@ WML_HANDLER_FUNCTION(replace_map, /*event_info*/, cfg)
try {
if (cfg["map"].empty()) {
const vconfig& map_cfg = cfg.child("map");
map.read(map_cfg["data"], false);
map.read(map_cfg["data"], false, map_cfg["border_size"].to_int(), map_cfg["usage"].str());
}
else map.read(cfg["map"], false);
} catch(incorrect_map_format_error&) {
@ -1561,12 +1561,17 @@ WML_HANDLER_FUNCTION(terrain_mask, /*event_info*/, cfg)
//config level;
std::string mask = cfg["mask"];
std::string usage = "mask";
int border_size = 0;
if (mask.empty()) {
usage = cfg["usage"].str();
border_size = cfg["border_size"];
mask = cfg["data"].str();
}
try {
mask_map.read(mask, false);
mask_map.read(mask, false, border_size, usage);
} catch(incorrect_map_format_error&) {
ERR_NG << "terrain mask is in the incorrect format, and couldn't be applied" << std::endl;
return;
@ -1574,8 +1579,7 @@ WML_HANDLER_FUNCTION(terrain_mask, /*event_info*/, cfg)
e.show(*resources::screen);
return;
}
const bool border = cfg["border"].to_bool(false);
bool border = cfg["border"].to_bool();
resources::gameboard->overlay_map(mask_map, cfg.get_parsed_config(), loc, border);
resources::screen->needs_rebuild(true);
}

View file

@ -370,7 +370,7 @@ static std::string output_map(const terrain_map& terrain,
itor->second.y -= begin_y;
}
return t_translation::write_game_map(map, starting_positions);
return gamemap::default_map_header + t_translation::write_game_map(map, starting_positions);
}
namespace {

View file

@ -40,7 +40,8 @@ static lg::log_domain log_config("config");
#define LOG_G LOG_STREAM(info, lg::general)
#define DBG_G LOG_STREAM(debug, lg::general)
const int gamemap::default_border = 1;
const std::string gamemap::default_map_header = "usage=map\nborder_size=1\n\n";
const gamemap::tborder gamemap::default_border = gamemap::SINGLE_TILE_BORDER;
/** Gets the list of terrains. */
const t_translation::t_list& gamemap::get_terrain_list() const
@ -115,7 +116,8 @@ gamemap::gamemap(const tdata_cache& tdata, const std::string& data):
h_(-1),
total_width_(0),
total_height_(0),
border_size_(gamemap::default_border)
border_size_(gamemap::SINGLE_TILE_BORDER),
usage_(IS_MAP)
{
DBG_G << "loading map: '" << data << "'\n";
@ -132,7 +134,8 @@ gamemap::gamemap(const tdata_cache& tdata, const config& level):
h_(-1),
total_width_(0),
total_height_(0),
border_size_(gamemap::default_border)
border_size_(gamemap::SINGLE_TILE_BORDER),
usage_(IS_MAP)
{
DBG_G << "loading map: '" << level.debug() << "'\n";
@ -149,7 +152,7 @@ gamemap::gamemap(const tdata_cache& tdata, const config& level):
total_height_ = 0;
}
} else {
read(map_child["data"], true);
read(map_child["data"], true, map_child["border_size"], map_child["usage"]);
}
}
@ -157,8 +160,11 @@ gamemap::~gamemap()
{
}
void gamemap::read(const std::string& data, const bool allow_invalid) {
void gamemap::read(const std::string& data, const bool allow_invalid, int border_size, std::string usage) {
// Initial stuff
border_size_ = border_size;
set_usage(usage);
tiles_.clear();
villages_.clear();
std::fill(startingPositions_, startingPositions_ +
@ -242,6 +248,31 @@ void gamemap::read(const std::string& data, const bool allow_invalid) {
}
}
void gamemap::set_usage(const std::string& 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_error("Map has a header but no usage");
} else {
std::string msg = "Map has a header but an unknown usage:" + usage;
throw incorrect_map_format_error(msg);
}
}
int gamemap::read_header(const std::string& data)
{
// Test whether there is a header section
@ -266,6 +297,7 @@ int gamemap::read_header(const std::string& data)
::read(header, header_str);
border_size_ = header["border_size"];
set_usage(header["usage"]);
return header_offset + 2;
}
@ -291,6 +323,29 @@ std::string gamemap::write() const
return s.str();
}
/*
void gamemap::write(config& cfg) const
{
// Convert the starting positions to a map
std::map<int, t_translation::coordinate> starting_positions;
for (int i = 0; i < MAX_PLAYERS + 1; ++i)
{
if (!on_board(startingPositions_[i])) continue;
t_translation::coordinate position(
startingPositions_[i].x + border_size_
, startingPositions_[i].y + border_size_);
starting_positions[i] = position;
}
cfg["border_size"] = border_size_;
cfg["usage"] = (usage_ == IS_MAP ? "map" : "mask");
std::ostringstream s;
s << t_translation::write_game_map(tiles_, starting_positions);
cfg["data"] = s.str();
}
*/
void gamemap::overlay(const gamemap& m, const config& rules_cfg, int xpos, int ypos, bool border)
{
const config::const_child_itors &rules = rules_cfg.child_range("rule");

View file

@ -69,6 +69,16 @@ public:
/* Get the underlying terrain_type_data object. */
const tdata_cache & tdata() const { return tdata_; }
enum tborder {
NO_BORDER = 0,
SINGLE_TILE_BORDER
};
enum tusage {
IS_MAP,
IS_MASK
};
/**
* Loads a map, with the given terrain configuration.
*
@ -94,7 +104,7 @@ public:
virtual ~gamemap();
void read(const std::string& data, const bool allow_invalid = true);
void read(const std::string& data, const bool allow_invalid = true, const int border_size = 1, const std::string usage = "map");
std::string write() const;
@ -130,6 +140,7 @@ public:
/** Writes the terrain at loc to cfg. */
void write_terrain(const map_location &loc, config& cfg) const;
/** Manipulate starting positions of the different sides. */
const map_location& starting_position(int side) const;
int is_starting_position(const map_location& loc) const;
@ -190,8 +201,17 @@ public:
*/
enum { MAX_PLAYERS = 9 };
/** Returns the usage of the map. */
tusage get_usage() const { return usage_; }
/**
* The default map header, needed for maps created with
* terrain_translation::write_game_map().
*/
static const std::string default_map_header;
/** The default border style for a map. */
static const int default_border;
static const tborder default_border;
/** Parses ranges of locations into a vector of locations, using this map's dimensions as bounds. */
std::vector<map_location> parse_location_range(const std::string& xvals,
@ -214,6 +234,8 @@ protected:
private:
void set_usage(const std::string& usage);
/**
* Reads the header of a map which is saved in the deprecated map_data format.
*
@ -247,6 +269,9 @@ protected:
private:
/** The size of the border around the map. */
int border_size_;
/** The kind of map is being loaded. */
tusage usage_;
};
#endif