renaming of 'state' to 'gamestate'...

...to get a more consistent variable naming for it (not complete yet,
just a first step)
This commit is contained in:
Jörg Hinrichs 2007-01-04 19:14:15 +00:00
parent 24138ab340
commit d84b2a94be
3 changed files with 118 additions and 119 deletions

View file

@ -565,26 +565,26 @@ void write_player(config_writer &out, const player_info& player)
// Deprecated, use other write_game below.
void write_game(const game_state& game, config& cfg/*, WRITE_GAME_MODE mode*/)
void write_game(const game_state& gamestate, config& cfg/*, WRITE_GAME_MODE mode*/)
{
log_scope("write_game");
cfg["label"] = game.label;
cfg["label"] = gamestate.label;
cfg["version"] = game_config::version;
cfg["scenario"] = game.scenario;
cfg["scenario"] = gamestate.scenario;
cfg["campaign"] = game.campaign;
cfg["campaign"] = gamestate.campaign;
cfg["campaign_type"] = game.campaign_type;
cfg["campaign_type"] = gamestate.campaign_type;
cfg["difficulty"] = game.difficulty;
cfg["difficulty"] = gamestate.difficulty;
cfg["campaign_define"] = game.campaign_define;
cfg["campaign_define"] = gamestate.campaign_define;
cfg.add_child("variables",game.variables);
cfg.add_child("variables",gamestate.variables);
for(std::map<std::string, player_info>::const_iterator i=game.players.begin();
i!=game.players.end(); ++i) {
for(std::map<std::string, player_info>::const_iterator i=gamestate.players.begin();
i!=gamestate.players.end(); ++i) {
config new_cfg;
write_player(i->second, new_cfg);
new_cfg["save_id"]=i->first;
@ -592,43 +592,43 @@ void write_game(const game_state& game, config& cfg/*, WRITE_GAME_MODE mode*/)
}
// if(mode == WRITE_FULL_GAME) {
if(game.replay_data.child("replay") == NULL) {
cfg.add_child("replay",game.replay_data);
if(gamestate.replay_data.child("replay") == NULL) {
cfg.add_child("replay",gamestate.replay_data);
}
cfg.add_child("snapshot",game.snapshot);
cfg.add_child("replay_start",game.starting_pos);
cfg.add_child("snapshot",gamestate.snapshot);
cfg.add_child("replay_start",gamestate.starting_pos);
cfg.add_child("statistics",statistics::write_stats());
}
//}
// }
}
void write_game(config_writer &out, const game_state& game)
void write_game(config_writer &out, const game_state& gamestate)
{
log_scope("write_game");
out.write_key_val("label", game.label);
out.write_key_val("label", gamestate.label);
out.write_key_val("version", game_config::version);
out.write_key_val("scenario", game.scenario);
out.write_key_val("campaign", game.campaign);
out.write_key_val("campaign_type", game.campaign_type);
out.write_key_val("difficulty", game.difficulty);
out.write_key_val("campaign_define", game.campaign_define);
out.write_child("variables", game.variables);
out.write_key_val("scenario", gamestate.scenario);
out.write_key_val("campaign", gamestate.campaign);
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);
for(std::map<std::string, player_info>::const_iterator i=game.players.begin();
i!=game.players.end(); ++i) {
for(std::map<std::string, player_info>::const_iterator i=gamestate.players.begin();
i!=gamestate.players.end(); ++i) {
out.open_child("player");
out.write_key_val("save_id", i->first);
write_player(out, i->second);
out.close_child("player");
}
if(game.replay_data.child("replay") == NULL) {
out.write_child("replay", game.replay_data);
if(gamestate.replay_data.child("replay") == NULL) {
out.write_child("replay", gamestate.replay_data);
}
out.write_child("snapshot",game.snapshot);
out.write_child("replay_start",game.starting_pos);
out.write_child("snapshot",gamestate.snapshot);
out.write_child("replay_start",gamestate.starting_pos);
out.open_child("statistics");
statistics::write_stats(out);
out.close_child("statistics");
@ -705,14 +705,14 @@ void read_save_file(const std::string& name, config& cfg, std::string* error_log
}
}
void load_game(const game_data& data, const std::string& name, game_state& state, std::string* error_log)
void load_game(const game_data& data, const std::string& name, game_state& gamestate, std::string* error_log)
{
log_scope("load_game");
config cfg;
read_save_file(name,cfg,error_log);
state = read_game(data,&cfg);
gamestate = read_game(data,&cfg);
}
void load_game_summary(const std::string& name, config& cfg_summary, std::string* error_log){
@ -737,7 +737,7 @@ scoped_ostream open_save_game(const std::string &label)
}
}
void finish_save_game(config_writer &out, const game_state& state, const std::string &label)
void finish_save_game(config_writer &out, const game_state& gamestate, const std::string &label)
{
std::string name = label;
std::replace(name.begin(),name.end(),' ','_');
@ -749,7 +749,7 @@ void finish_save_game(config_writer &out, const game_state& state, const std::st
}
config& summary = save_summary(label);
extract_summary_data_from_save(state,summary);
extract_summary_data_from_save(gamestate,summary);
const int mod_time = static_cast<int>(file_create_time(fname));
summary["mod_time"] = str_cast(mod_time);
write_save_index();
@ -759,12 +759,12 @@ void finish_save_game(config_writer &out, const game_state& state, const std::st
}
//throws game::save_game_failed
void save_game(const game_state& state)
void save_game(const game_state& gamestate)
{
scoped_ostream os(open_save_game(state.label));
scoped_ostream os(open_save_game(gamestate.label));
config_writer out(*os, preferences::compress_saves(), PACKAGE);
write_game(out, state);
finish_save_game(out, state, state.label);
write_game(out, gamestate);
finish_save_game(out, gamestate, gamestate.label);
}
namespace {
@ -825,25 +825,25 @@ void write_save_index()
}
}
void extract_summary_data_from_save(const game_state& state, config& out)
void extract_summary_data_from_save(const game_state& gamestate, config& out)
{
const bool has_replay = state.replay_data.empty() == false;
const bool has_snapshot = state.snapshot.child("side") != NULL;
const bool has_replay = gamestate.replay_data.empty() == false;
const bool has_snapshot = gamestate.snapshot.child("side") != NULL;
out["replay"] = has_replay ? "yes" : "no";
out["snapshot"] = has_snapshot ? "yes" : "no";
out["label"] = state.label;
out["campaign_type"] = state.campaign_type;
out["scenario"] = state.scenario;
out["difficulty"] = state.difficulty;
out["version"] = state.version;
out["label"] = gamestate.label;
out["campaign_type"] = gamestate.campaign_type;
out["scenario"] = gamestate.scenario;
out["difficulty"] = gamestate.difficulty;
out["version"] = gamestate.version;
out["corrupt"] = "";
if(has_snapshot) {
out["turn"] = state.snapshot["turn_at"];
if(state.snapshot["turns"] != "-1") {
out["turn"] = out["turn"].str() + "/" + state.snapshot["turns"].str();
out["turn"] = gamestate.snapshot["turn_at"];
if(gamestate.snapshot["turns"] != "-1") {
out["turn"] = out["turn"].str() + "/" + gamestate.snapshot["turns"].str();
}
}
@ -853,8 +853,8 @@ void extract_summary_data_from_save(const game_state& state, config& out)
//human player?
std::string leader;
for(std::map<std::string, player_info>::const_iterator p = state.players.begin();
p!=state.players.end(); ++p) {
for(std::map<std::string, player_info>::const_iterator p = gamestate.players.begin();
p!=gamestate.players.end(); ++p) {
for(std::vector<unit>::const_iterator u = p->second.available_units.begin(); u != p->second.available_units.end(); ++u) {
if(u->can_recruit()) {
leader = u->id();
@ -865,7 +865,7 @@ void extract_summary_data_from_save(const game_state& state, config& out)
bool shrouded = false;
if(leader == "") {
const config& snapshot = has_snapshot ? state.snapshot : state.starting_pos;
const config& snapshot = has_snapshot ? gamestate.snapshot : gamestate.starting_pos;
const config::child_list& sides = snapshot.get_children("side");
for(config::child_list::const_iterator s = sides.begin(); s != sides.end() && leader.empty(); ++s) {
@ -892,12 +892,12 @@ void extract_summary_data_from_save(const game_state& state, config& out)
if(!shrouded) {
if(has_snapshot) {
if(state.snapshot.find_child("side","shroud","yes") == NULL) {
out["map_data"] = state.snapshot["map_data"];
if(gamestate.snapshot.find_child("side","shroud","yes") == NULL) {
out["map_data"] = gamestate.snapshot["map_data"];
}
} else if(has_replay) {
if(state.starting_pos.find_child("side","shroud","yes") == NULL) {
out["map_data"] = state.starting_pos["map_data"];
if(gamestate.starting_pos.find_child("side","shroud","yes") == NULL) {
out["map_data"] = gamestate.starting_pos["map_data"];
}
}
}

View file

@ -171,21 +171,21 @@ std::vector<save_info> get_saves_list(const std::string *dir = NULL);
void read_save_file(const std::string& name, config& cfg, std::string* error_log);
game_state read_game(const game_data& data, const config* cfg);
void write_game(const game_state& game, config& cfg/*, WRITE_GAME_MODE mode=WRITE_FULL_GAME*/);
void write_game(config_writer &out, const game_state& game);
void write_game(const game_state& gamestate, config& cfg/*, WRITE_GAME_MODE mode=WRITE_FULL_GAME*/);
void write_game(config_writer &out, const game_state& gamestate);
// function returns true iff there is already savegame with that name
bool save_game_exists(const std::string & name);
//throws game::save_game_failed
scoped_ostream open_save_game(const std::string &label);
void finish_save_game(config_writer &out, const game_state& state, const std::string &label);
void finish_save_game(config_writer &out, const game_state& gamestate, const std::string &label);
//functions to load/save games.
void load_game(const game_data& data, const std::string& name, game_state& state, std::string* error_log);
void load_game(const game_data& data, const std::string& name, game_state& gamestate, std::string* error_log);
void load_game_summary(const std::string& name, config& cfg_summary, std::string* error_log);
//throws gamestatus::save_game_failed
void save_game(const game_state& state);
void save_game(const game_state& gamestate);
//function to delete a save
void delete_game(const std::string& name);
@ -196,7 +196,7 @@ void delete_save_summary(const std::string& save);
void write_save_index();
void extract_summary_data_from_save(const game_state& state, config& out);
void extract_summary_data_from_save(const game_state& gamestate, config& out);
void extract_summary_from_config(config& cfg_save, config& cfg_summary);
#endif

View file

@ -54,10 +54,10 @@ typedef std::map<std::string, player_controller> controller_map;
}
void play_replay(display& disp, game_state& state, const config& game_config,
void play_replay(display& disp, game_state& gamestate, const config& game_config,
const game_data& units_data, CVideo& video)
{
std::string type = state.campaign_type;
std::string type = gamestate.campaign_type;
if(type.empty())
type = "scenario";
@ -66,9 +66,9 @@ void play_replay(display& disp, game_state& state, const config& game_config,
//'starting_pos' will contain the position we start the game from.
config starting_pos;
recorder.set_save_info(state);
if (state.snapshot.child("side") != NULL){
state = read_game(units_data, &state.snapshot);
recorder.set_save_info(gamestate);
if (gamestate.snapshot.child("side") != NULL){
gamestate = read_game(units_data, &gamestate.snapshot);
}
//see if we load the scenario from the scenario data -- if there is
@ -77,31 +77,31 @@ void play_replay(display& disp, game_state& state, const config& game_config,
if(!recorder.at_end()) {
//if the starting state is specified, then use that,
//otherwise get the scenario data and start from there.
if(state.starting_pos.empty() == false) {
if(gamestate.starting_pos.empty() == false) {
LOG_G << "loading starting position...\n";
starting_pos = state.starting_pos;
starting_pos = gamestate.starting_pos;
scenario = &starting_pos;
} else {
LOG_G << "loading scenario: '" << state.scenario << "'\n";
scenario = game_config.find_child(type,"id",state.scenario);
LOG_G << "loading scenario: '" << gamestate.scenario << "'\n";
scenario = game_config.find_child(type,"id",gamestate.scenario);
LOG_G << "scenario found: " << (scenario != NULL ? "yes" : "no") << "\n";
}
}
controller_map controllers;
const std::string current_scenario = state.scenario;
const std::string current_scenario = gamestate.scenario;
try {
// preserve old label eg. replay
if (state.label.empty())
state.label = (*scenario)["name"];
if (gamestate.label.empty())
gamestate.label = (*scenario)["name"];
play_replay_level(units_data,game_config,scenario,video,state);
play_replay_level(units_data,game_config,scenario,video,gamestate);
state.snapshot = config();
gamestate.snapshot = config();
recorder.clear();
state.replay_data.clear();
gamestate.replay_data.clear();
} catch(game::load_game_failed& e) {
gui::show_error_message(disp, _("The game could not be loaded: ") + e.message);
@ -125,12 +125,12 @@ void clean_autosaves(const std::string &label)
}
}
LEVEL_RESULT play_game(display& disp, game_state& state, const config& game_config,
LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_config,
const game_data& units_data, CVideo& video,
upload_log &log,
io_type_t io_type, bool skip_replay)
{
std::string type = state.campaign_type;
std::string type = gamestate.campaign_type;
if(type.empty())
type = "scenario";
@ -139,29 +139,29 @@ LEVEL_RESULT play_game(display& disp, game_state& state, const config& game_conf
//'starting_pos' will contain the position we start the game from.
config starting_pos;
recorder.set_save_info(state);
recorder.set_save_info(gamestate);
//see if we load the scenario from the scenario data -- if there is
//no snapshot data available from a save, or if the user has selected
//to view the replay from scratch
if(state.snapshot.child("side") == NULL || !recorder.at_end()) {
if(gamestate.snapshot.child("side") == NULL || !recorder.at_end()) {
//if the starting state is specified, then use that,
//otherwise get the scenario data and start from there.
if(state.starting_pos.empty() == false) {
if(gamestate.starting_pos.empty() == false) {
LOG_G << "loading starting position...\n";
starting_pos = state.starting_pos;
starting_pos = gamestate.starting_pos;
scenario = &starting_pos;
} else {
LOG_G << "loading scenario: '" << state.scenario << "'\n";
scenario = game_config.find_child(type,"id",state.scenario);
LOG_G << "loading scenario: '" << gamestate.scenario << "'\n";
scenario = game_config.find_child(type,"id",gamestate.scenario);
LOG_G << "scenario found: " << (scenario != NULL ? "yes" : "no") << "\n";
}
} else {
LOG_G << "loading snapshot...\n";
//load from a save-snapshot.
starting_pos = state.snapshot;
starting_pos = gamestate.snapshot;
scenario = &starting_pos;
state = read_game(units_data, &state.snapshot);
gamestate = read_game(units_data, &gamestate.snapshot);
}
controller_map controllers;
@ -199,7 +199,7 @@ LEVEL_RESULT play_game(display& disp, game_state& state, const config& game_conf
}
const config::child_list& story = scenario->get_children("story");
const std::string current_scenario = state.scenario;
const std::string current_scenario = gamestate.scenario;
bool save_game_after_scenario = true;
@ -207,8 +207,8 @@ LEVEL_RESULT play_game(display& disp, game_state& state, const config& game_conf
try {
// preserve old label eg. replay
if (state.label.empty())
state.label = (*scenario)["name"];
if (gamestate.label.empty())
gamestate.label = (*scenario)["name"];
//if the entire scenario should be randomly generated
if((*scenario)["scenario_generation"] != "") {
@ -219,7 +219,7 @@ LEVEL_RESULT play_game(display& disp, game_state& state, const config& game_conf
scenario2 = random_generate_scenario((*scenario)["scenario_generation"], scenario->child("generator"));
//level_ = scenario;
state.starting_pos = scenario2;
gamestate.starting_pos = scenario2;
scenario = &scenario2;
}
@ -240,26 +240,25 @@ LEVEL_RESULT play_game(display& disp, game_state& state, const config& game_conf
new_level.values["map_data"] = map_data;
scenario = &new_level;
state.starting_pos = new_level;
gamestate.starting_pos = new_level;
LOG_G << "generated map\n";
}
sound::play_no_music();
//LEVEL_RESULT res = play_level(units_data,game_config,scenario,video,state,story,log, skip_replay);
LEVEL_RESULT res;
switch (io_type){
case IO_NONE:
res = playsingle_scenario(units_data,game_config,scenario,video,state,story,log, skip_replay);
res = playsingle_scenario(units_data,game_config,scenario,video,gamestate,story,log, skip_replay);
break;
case IO_SERVER:
case IO_CLIENT:
res = playmp_scenario(units_data,game_config,scenario,video,state,story,log, skip_replay);
res = playmp_scenario(units_data,game_config,scenario,video,gamestate,story,log, skip_replay);
break;
}
state.snapshot = config();
gamestate.snapshot = config();
if (res == DEFEAT) {
// tell all clients that the campaign won't continue
if(io_type == IO_SERVER) {
@ -279,10 +278,10 @@ LEVEL_RESULT play_game(display& disp, game_state& state, const config& game_conf
}
//ask to save a replay of the game
if(res == VICTORY || res == DEFEAT) {
const std::string orig_scenario = state.scenario;
state.scenario = current_scenario;
const std::string orig_scenario = gamestate.scenario;
gamestate.scenario = current_scenario;
std::string label = state.label + _(" replay");
std::string label = gamestate.label + _(" replay");
bool retry = true;
@ -297,7 +296,7 @@ LEVEL_RESULT play_game(display& disp, game_state& state, const config& game_conf
try {
config snapshot;
recorder.save_game(label, snapshot, state.starting_pos);
recorder.save_game(label, snapshot, gamestate.starting_pos);
} catch(game::save_game_failed&) {
gui::show_error_message(disp, _("The replay could not be saved"));
retry = true;
@ -305,11 +304,11 @@ LEVEL_RESULT play_game(display& disp, game_state& state, const config& game_conf
}
}
state.scenario = orig_scenario;
gamestate.scenario = orig_scenario;
}
recorder.clear();
state.replay_data.clear();
gamestate.replay_data.clear();
//continue without saving is like a victory, but the
//save game dialog isn't displayed
@ -331,14 +330,14 @@ LEVEL_RESULT play_game(display& disp, game_state& state, const config& game_conf
}
//if the scenario hasn't been set in-level, set it now.
if(state.scenario == current_scenario)
state.scenario = (*scenario)["next_scenario"];
if(gamestate.scenario == current_scenario)
gamestate.scenario = (*scenario)["next_scenario"];
if(io_type == IO_CLIENT) {
config cfg;
std::string msg;
if (state.scenario.empty())
if (gamestate.scenario.empty())
msg = _("Receiving data...");
else
msg = _("Downloading next scenario...");
@ -355,16 +354,16 @@ LEVEL_RESULT play_game(display& disp, game_state& state, const config& game_conf
if(cfg.child("next_scenario")) {
starting_pos = (*cfg.child("next_scenario"));
scenario = &starting_pos;
state = read_game(units_data, scenario);
gamestate = read_game(units_data, scenario);
} else if(scenario->child("end_scenarios")) {
scenario = NULL;
state.scenario = "null";
gamestate.scenario = "null";
} else {
return QUIT;
}
} else {
scenario = game_config.find_child(type,"id",state.scenario);
scenario = game_config.find_child(type,"id",gamestate.scenario);
if(io_type == IO_SERVER && scenario != NULL) {
starting_pos = *scenario;
@ -384,7 +383,7 @@ LEVEL_RESULT play_game(display& disp, game_state& state, const config& game_conf
set in the players list on side server */
controller_map::const_iterator ctr = controllers.find(id);
if(ctr != controllers.end()) {
player_info *player = state.get_player(id);
player_info *player = gamestate.get_player(id);
if (player) {
(**side)["current_player"] = player->name;
//TODO : remove (see TODO line 276 in server/game.cpp)
@ -401,7 +400,7 @@ LEVEL_RESULT play_game(display& disp, game_state& state, const config& game_conf
// Adds player information, and other state
// information, to the configuration object
wassert(cfg.child("next_scenario") != NULL);
write_game(state, *cfg.child("next_scenario")/*, WRITE_SNAPSHOT_ONLY*/);
write_game(gamestate, *cfg.child("next_scenario")/*, WRITE_SNAPSHOT_ONLY*/);
network::send_data(cfg);
} else if(io_type == IO_SERVER && scenario == NULL) {
@ -413,12 +412,12 @@ LEVEL_RESULT play_game(display& disp, game_state& state, const config& game_conf
if(scenario != NULL) {
// update the label
std::string oldlabel = state.label;
state.label = (*scenario)["name"];
std::string oldlabel = gamestate.label;
gamestate.label = (*scenario)["name"];
//if this isn't the last scenario, then save the game
if(save_game_after_scenario) {
state.starting_pos = config();
gamestate.starting_pos = config();
bool retry = true;
@ -428,12 +427,12 @@ LEVEL_RESULT play_game(display& disp, game_state& state, const config& game_conf
const int should_save = dialogs::get_save_name(disp,
_("Do you want to save your game? (Also erases Auto-Save files)"),
_("Name:"),
&state.label);
&gamestate.label);
if(should_save == 0) {
try {
save_game(state);
save_game(gamestate);
if (!oldlabel.empty())
clean_autosaves(oldlabel);
} catch(game::save_game_failed&) {
@ -446,14 +445,14 @@ LEVEL_RESULT play_game(display& disp, game_state& state, const config& game_conf
//update the replay start
//FIXME: this should only be done if the scenario was not tweaked.
state.starting_pos = *scenario;
gamestate.starting_pos = *scenario;
}
recorder.set_save_info(state);
recorder.set_save_info(gamestate);
}
if (!state.scenario.empty() && state.scenario != "null") {
gui::show_error_message(disp, _("Unknown scenario: '") + state.scenario + '\'');
if (!gamestate.scenario.empty() && gamestate.scenario != "null") {
gui::show_error_message(disp, _("Unknown scenario: '") + gamestate.scenario + '\'');
return QUIT;
}