Use the existence of the variable_set base type...

...to move read_location back  into the map class as a method.
This commit is contained in:
Eric S. Raymond 2007-07-04 12:32:57 +00:00
parent f62da2becf
commit 301373feaa
9 changed files with 45 additions and 49 deletions

View File

@ -783,7 +783,7 @@ void ai::find_threats()
items.push_back(protected_item(
lexical_cast_default<double>((**i)["value"], 1.0),
lexical_cast_default<int>((**i)["radius"], 20),
read_location(**i)));
gamemap::location(**i, &get_info().game_state_)));
}
//look for directions to protect a unit
@ -1895,7 +1895,7 @@ void ai::move_leader_to_goals( const move_map& enemy_dstsrc)
return;
}
const gamemap::location dst = read_location(*goal);
const gamemap::location dst(*goal, &get_info().game_state_);
if (!dst.valid()) {
ERR_AI << "Invalid goal\n";
return;

View File

@ -107,7 +107,7 @@ void get_player_info(const config& cfg, game_state& gamestate, std::string save_
//see if the side specifies its location. Otherwise start it at the map-given
//starting position
gamemap::location start_pos = read_location(cfg);
gamemap::location start_pos(cfg, &gamestate);
if(map.empty()) {
throw game::load_game_failed("Map not found");
@ -163,7 +163,7 @@ void get_player_info(const config& cfg, game_state& gamestate, std::string save_
const std::string& x = (**su)["x"];
const std::string& y = (**su)["y"];
gamemap::location loc = read_location(**su);
gamemap::location loc(**su, &gamestate);
if(x.empty() && y.empty()) {
if(player) {
player->available_units.push_back(new_unit);

View File

@ -50,27 +50,6 @@
#define LOG_DP LOG_STREAM(info, display)
#define ERR_CF LOG_STREAM(err, config)
// This is here to avoid creating a cross-dependency with the map class.
gamemap::location read_location(const config& cfg)
{
std::string xs = cfg["x"], ys = cfg["y"];
if (game_events::get_state_of_game())
{
xs = utils::interpolate_variables_into_string( xs, *game_events::get_state_of_game());
ys = utils::interpolate_variables_into_string( ys, *game_events::get_state_of_game());
}
//the co-ordinates in config files will be 1-based, while we
//want them as 0-based
int x = -1, y = -1;
if(xs.empty() == false)
x = atoi(xs.c_str()) - 1;
if(ys.empty() == false)
y = atoi(ys.c_str()) - 1;
return gamemap::location::location(x, y);
};
namespace {
game_display* screen = NULL;
@ -1881,7 +1860,7 @@ bool event_handler::handle_event_command(const queued_event& event_info,
const unit u(game_data_ptr,units,game_map,status_ptr,teams,var);
preferences::encountered_units().insert(u.id());
gamemap::location loc = read_location(var);
gamemap::location loc(var, game_events::get_state_of_game());
if(loc.valid()) {
if(utils::string_bool(cfg["find_vacant"])) {
loc = find_vacant_tile(*game_map,*units,loc);

View File

@ -124,6 +124,23 @@ std::string gamemap::location::write_direction(gamemap::location::DIRECTION dir)
}
}
gamemap::location::location(const config& cfg, const variable_set *variables)
{
std::string xs = cfg["x"], ys = cfg["y"];
if (variables)
{
xs = utils::interpolate_variables_into_string( xs, *variables);
ys = utils::interpolate_variables_into_string( ys, *variables);
}
//the co-ordinates in config files will be 1-based, while we
//want them as 0-based
if(xs.empty() == false)
x = atoi(xs.c_str()) - 1;
if(ys.empty() == false)
y = atoi(ys.c_str()) - 1;
}
void gamemap::location::write(config& cfg) const
{
char buf[50];

View File

@ -22,6 +22,8 @@ class unit_map;
#include "terrain.hpp"
#include "serialization/string_utils.hpp"
#include <iostream>
#include <map>
#include <string>
@ -61,6 +63,7 @@ public:
location() : x(-1), y(-1) {}
location(int x, int y) : x(x), y(y) {}
location(const config& cfg, const variable_set *variables);
void write(config& cfg) const;
@ -234,9 +237,6 @@ std::vector<gamemap::location> parse_location_range(const std::string& xvals,
const std::string& yvals,
const gamemap *const map=NULL);
// parse a WML location with variable interpolation
gamemap::location read_location(const config& cfg);
//dump a position on a stream for debug purposes
std::ostream &operator<<(std::ostream &s, gamemap::location const &l);

View File

@ -80,7 +80,7 @@ void map_labels::read(const config& cfg, const variable_set *variables)
const config::child_list& items = cfg.get_children("label");
for(config::child_list::const_iterator i = items.begin(); i != items.end(); ++i) {
const gamemap::location loc = read_location(**i);
const gamemap::location loc(**i, variables);
terrain_label* label = new terrain_label(*this, **i, variables);
add_label(loc, label);
}
@ -349,7 +349,7 @@ terrain_label::~terrain_label()
void terrain_label::read(const config& cfg, const variable_set *variables)
{
loc_ = read_location(cfg);
loc_ = gamemap::location(cfg, variables);
SDL_Color colour = font::LABEL_COLOUR;
std::string tmp_colour = cfg["colour"];

View File

@ -151,7 +151,7 @@ LEVEL_RESULT playsingle_controller::play_scenario(const std::vector<config*>& st
//find a list of 'items' (i.e. overlays) on the level, and add them
const config::child_list& overlays = level_.get_children("item");
for(config::child_list::const_iterator overlay = overlays.begin(); overlay != overlays.end(); ++overlay) {
gui_->add_overlay(read_location(**overlay),(**overlay)["image"], (**overlay)["halo"]);
gui_->add_overlay(gamemap::location(**overlay,game_events::get_state_of_game()),(**overlay)["image"], (**overlay)["halo"]);
}
victory_conditions::set_victory_when_enemies_defeated(

View File

@ -60,7 +60,7 @@ static void verify(const unit_map& units, const config& cfg)
std::set<gamemap::location> locs;
const config::child_list& items = cfg.get_children("unit");
for(config::child_list::const_iterator i = items.begin(); i != items.end(); ++i) {
const gamemap::location loc = read_location(**i);
const gamemap::location loc(**i, game_events::get_state_of_game());
locs.insert(loc);
if(units.count(loc) == 0) {
@ -81,7 +81,7 @@ static void verify(const unit_map& units, const config& cfg)
const config::child_list& items = cfg.get_children("unit");
for(config::child_list::const_iterator i = items.begin(); i != items.end(); ++i) {
const gamemap::location loc = read_location(**i);
const gamemap::location loc(**i, game_events::get_state_of_game());
const unit_map::const_iterator u = units.find(loc);
if(u == units.end()) {
errbuf << "SYNC VERIFICATION FAILED: data source says there is a '"
@ -456,14 +456,14 @@ void replay::undo()
{
// A unit's move is being undone.
// Repair unsynced cmds whose locations depend on that unit's location.
gamemap::location dst = read_location(*(child->child("destination")));
gamemap::location src = read_location(*(child->child("source")));
gamemap::location dst(*(child->child("destination")), game_events::get_state_of_game());
gamemap::location src(*(child->child("source")), game_events::get_state_of_game());
for (std::vector<config::child_iterator>::iterator async_cmd = async_cmds.begin(); async_cmd != async_cmds.end(); async_cmd++)
{
config* async_child;
if ((async_child = (***async_cmd).child("rename")) != NULL)
{
gamemap::location aloc = read_location(*async_child);
gamemap::location aloc(*async_child, game_events::get_state_of_game());
if (dst == aloc)
{
src.write(*async_child);
@ -475,13 +475,13 @@ void replay::undo()
{
// A unit is being un-recruited or un-recalled.
// Remove unsynced commands that would act on that unit.
gamemap::location src = read_location(*child);
gamemap::location src(*child, game_events::get_state_of_game());
for (std::vector<config::child_iterator>::iterator async_cmd = async_cmds.begin(); async_cmd != async_cmds.end(); async_cmd++)
{
config* async_child;
if ((async_child = (***async_cmd).child("rename")) != NULL)
{
gamemap::location aloc = read_location(*async_child);
gamemap::location aloc(*async_child, game_events::get_state_of_game());
if (src == aloc)
{
cfg_.remove_child("command", *async_cmd - cmd.first);
@ -621,7 +621,7 @@ static void check_checksums(game_display& disp,const unit_map& units,const confi
return;
}
for(config::child_list::const_iterator ci = cfg.get_children("checksum").begin(); ci != cfg.get_children("checksum").end(); ++ci) {
gamemap::location loc = read_location(**ci);
gamemap::location loc(**ci, game_events::get_state_of_game());
unit_map::const_iterator u = units.find(loc);
if(u == units.end()) {
std::stringstream message;
@ -749,7 +749,7 @@ bool do_replay(game_display& disp, const gamemap& map, const game_data& gameinfo
}
else if((child = cfg->child("rename")) != NULL) {
const gamemap::location loc = read_location(*child);
const gamemap::location loc(*child, game_events::get_state_of_game());
const std::string& name = (*child)["name"];
unit_map::iterator u = units.find(loc);
@ -782,7 +782,7 @@ bool do_replay(game_display& disp, const gamemap& map, const game_data& gameinfo
const std::string& recruit_num = (*child)["value"];
const int val = atoi(recruit_num.c_str());
gamemap::location loc = read_location(*child);
gamemap::location loc(*child, game_events::get_state_of_game());
const std::set<std::string>& recruits = current_team.recruits();
@ -840,7 +840,7 @@ bool do_replay(game_display& disp, const gamemap& map, const game_data& gameinfo
const std::string& recall_num = (*child)["value"];
const int val = atoi(recall_num.c_str());
gamemap::location loc = read_location(*child);
gamemap::location loc(*child, game_events::get_state_of_game());
if(val >= 0 && val < int(player->available_units.size())) {
statistics::recall_unit(player->available_units[val]);
@ -893,8 +893,8 @@ bool do_replay(game_display& disp, const gamemap& map, const game_data& gameinfo
replay::throw_error("no destination/source found in movement\n");
}
const gamemap::location src = read_location(*source);
const gamemap::location dst = read_location(*destination);
const gamemap::location src(*source, game_events::get_state_of_game());
const gamemap::location dst(*destination, game_events::get_state_of_game());
unit_map::iterator u = units.find(dst);
if(u != units.end()) {
@ -994,8 +994,8 @@ bool do_replay(game_display& disp, const gamemap& map, const game_data& gameinfo
replay::throw_error("no destination/source found in attack\n");
}
const gamemap::location src = read_location(*source);
const gamemap::location dst = read_location(*destination);
const gamemap::location src(*source, game_events::get_state_of_game());
const gamemap::location dst(*destination, game_events::get_state_of_game());
const std::string& weapon = (*child)["weapon"];
const int weapon_num = atoi(weapon.c_str());
@ -1059,7 +1059,7 @@ bool do_replay(game_display& disp, const gamemap& map, const game_data& gameinfo
if ((event != "side turn") && (event != "turn 1") && (event != "new_turn")){
const config* const source = child->child("source");
if(source != NULL) {
game_events::fire(event, read_location(*source));
game_events::fire(event, gamemap::location(*source, game_events::get_state_of_game()));
} else {
game_events::fire(event);
}

View File

@ -356,7 +356,7 @@ team::team(const config& cfg, int gold) : gold_(gold), auto_shroud_updates_(true
//load in the villages the side controls at the start
const config::child_list& villages = cfg.get_children("village");
for(config::child_list::const_iterator v = villages.begin(); v != villages.end(); ++v) {
villages_.insert(read_location(**v));
villages_.insert(gamemap::location(**v,game_events::get_state_of_game()));
}
countdown_time_=lexical_cast_default<int>(cfg["countdown_time"],0);