make the variables config a private member of game_state

(this is a first step towards lazy evaluation of auto-stored units)

fix error in [store_starting_locations]
This commit is contained in:
Patrick Parker 2007-02-24 05:02:16 +00:00
parent 402c8e4c93
commit b1e102291a
6 changed files with 45 additions and 26 deletions

View file

@ -1679,10 +1679,8 @@ bool event_handler::handle_event_command(const queued_event& event_info,
const std::string& variable = cfg["variable"];
const std::string& mode = cfg["mode"];
bool cleared = false;
config& vars = state_of_game->variables;
if(mode != "replace" && mode != "append") {
vars.clear_children(variable);
state_of_game->clear_variable_cfg(variable);
}
const bool kill_units = utils::string_bool(cfg["kill"]);
@ -1694,10 +1692,10 @@ bool event_handler::handle_event_command(const queued_event& event_info,
}
if(mode == "replace" && !cleared) {
vars.clear_children(variable);
state_of_game->clear_variable_cfg(variable);
cleared = true;
}
config& data = vars.add_child(variable);
config& data = state_of_game->add_variable_cfg(variable);
i->first.write(data);
i->second.write(data);
@ -1723,10 +1721,10 @@ bool event_handler::handle_event_command(const queued_event& event_info,
}
if(mode == "replace" && !cleared) {
vars.clear_children(variable);
state_of_game->clear_variable_cfg(variable);
cleared = true;
}
config& data = vars.add_child(variable);
config& data = state_of_game->add_variable_cfg(variable);
j->write(data);
data["x"] = "recall";
data["y"] = "recall";
@ -1819,7 +1817,7 @@ bool event_handler::handle_event_command(const queued_event& event_info,
static const t_string default_store = "location";
const t_string& store = variable.empty() ? default_store : variable;
wassert(state_of_game != NULL);
config &loc_store = state_of_game->get_variable_cfg(store);
config &loc_store = state_of_game->add_variable_cfg(store);
loc.write(loc_store);
game_map->write_terrain(loc, loc_store);
}
@ -1845,7 +1843,7 @@ bool event_handler::handle_event_command(const queued_event& event_info,
radius_str = utils::interpolate_variables_into_string(radius_str, *state_of_game);
const vconfig unit_filter = cfg.child("filter");
state_of_game->variables.clear_children(variable);
state_of_game->clear_variable_cfg(variable);
std::vector<gamemap::location> locs = parse_location_range(x,y);
if(locs.size() > MaxLoop) {
@ -1868,7 +1866,7 @@ bool event_handler::handle_event_command(const queued_event& event_info,
if (u == units->end() || !game_events::unit_matches_filter(u, unit_filter))
continue;
}
config &loc_store = state_of_game->variables.add_child(variable);
config &loc_store = state_of_game->add_variable_cfg(variable);
j->write(loc_store);
game_map->write_terrain(*j, loc_store);
++added;
@ -2369,16 +2367,16 @@ bool pump()
if(i.first != i.second && state_of_game != NULL) {
char buf[50];
snprintf(buf,sizeof(buf),"%d",ev.loc1.x+1);
state_of_game->variables["x1"] = buf;
state_of_game->set_variable("x1", buf);
snprintf(buf,sizeof(buf),"%d",ev.loc1.y+1);
state_of_game->variables["y1"] = buf;
state_of_game->set_variable("y1", buf);
snprintf(buf,sizeof(buf),"%d",ev.loc2.x+1);
state_of_game->variables["x2"] = buf;
state_of_game->set_variable("x2", buf);
snprintf(buf,sizeof(buf),"%d",ev.loc2.y+1);
state_of_game->variables["y2"] = buf;
state_of_game->set_variable("y2", buf);
}
while(i.first != i.second) {

View file

@ -475,7 +475,7 @@ game_state read_game(const game_data& data, const config* cfg)
const config* const vars = cfg->child("variables");
if(vars != NULL) {
res.variables = *vars;
res.set_variables(*vars);
}
const config* const replay = cfg->child("replay");
@ -581,7 +581,7 @@ void write_game(const game_state& gamestate, config& cfg, WRITE_GAME_MODE mode)
cfg["campaign_define"] = gamestate.campaign_define;
cfg.add_child("variables",gamestate.variables);
cfg.add_child("variables",gamestate.get_variables());
for(std::map<std::string, player_info>::const_iterator i=gamestate.players.begin();
i!=gamestate.players.end(); ++i) {
@ -613,7 +613,7 @@ void write_game(config_writer &out, const game_state& gamestate, WRITE_GAME_MODE
out.write_key_val("campaign_type", gamestate.campaign_type);
out.write_key_val("difficulty", gamestate.difficulty);
out.write_key_val("campaign_define", gamestate.campaign_define);
out.write_child("variables", gamestate.variables);
out.write_child("variables", gamestate.get_variables());
for(std::map<std::string, player_info>::const_iterator i=gamestate.players.begin();
i!=gamestate.players.end(); ++i) {
@ -1160,6 +1160,16 @@ void game_state::set_variable(const std::string& key, const t_string& value)
variables[key] = value;
}
config& game_state::add_variable_cfg(const std::string& key, const config& value)
{
return variables.add_child(key, value);
}
void game_state::clear_variable_cfg(const std::string& varname)
{
variables.clear_children(varname);
}
void game_state::clear_variable(const std::string& varname)
{
config* vars = &variables;

View file

@ -80,16 +80,23 @@ public:
// Return the Nth player, or NULL if no such player exists
player_info* get_player(const std::string& id);
private:
config variables; //variables that have been set
public:
const config& get_variables() const { return variables; }
void set_variables(const config& vars) { variables = vars; }
//Variable access
t_string& get_variable(const std::string& varname);
virtual const t_string& get_variable_const(const std::string& varname)const ;
config& get_variable_cfg(const std::string& varname);
void set_variable(const std::string& varname, const t_string& value);
config& add_variable_cfg(const std::string& varname, const config& value=config());
void clear_variable(const std::string& varname);
void clear_variable_cfg(const std::string& varname); //clears only the config children
std::string difficulty; //the difficulty level the game is being played on.

View file

@ -107,11 +107,11 @@ void level_to_gamestate(config& level, game_state& state, bool saved_game)
const config* const vars = level.child("variables");
if(vars != NULL) {
state.variables = *vars;
state.set_variables(*vars);
}
else{
if (state.snapshot.child("variables") != NULL){
state.variables = *state.snapshot.child("variables");
state.set_variables(*state.snapshot.child("variables"));
}
}
}

View file

@ -150,7 +150,7 @@ LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_
// when starting wesnoth --multiplayer there might be
// no variables which leads to a segfault
if(gamestate.snapshot.child("variables") != NULL) {
gamestate.variables = *gamestate.snapshot.child("variables");
gamestate.set_variables(*gamestate.snapshot.child("variables"));
}
//get the current gold values of players so they don't start with the amount
//they had at the start of the scenario

View file

@ -113,15 +113,19 @@ scoped_wml_variable::scoped_wml_variable(const std::string var_name,const config
var_name_(var_name)
{
previous_val_= repos->variables;
repos->variables.clear_children(var_name);
repos->variables.add_child(var_name,var_value);
//previous_val_;
const config::child_list& children = repos->get_variables().get_children(var_name);
for(config::child_list::const_iterator i = children.begin(); i != children.end(); ++i) {
previous_val_.append(**i);
}
repos->clear_variable_cfg(var_name);
repos->add_variable_cfg(var_name, var_value);
}
scoped_wml_variable::~scoped_wml_variable()
{
repos->variables.clear_children(var_name_);
repos->clear_variable_cfg(var_name_);
config::child_list old_val =previous_val_.get_children(var_name_);
for(config::child_list::iterator j=old_val.begin(); j != old_val.end() ; j++){
repos->variables.add_child(var_name_,**j);
repos->add_variable_cfg(var_name_,**j);
}
}