Reverts changes from 2006-10-10T19:57:48Z!joerg.hinrichs@alice-dsl.de

(savegame patch), since it causes more problems and i can't care for
it for at least a week.
This commit is contained in:
Jörg Hinrichs 2006-10-21 19:00:08 +00:00
parent bfa5deba2b
commit 04f1a065dc
13 changed files with 138 additions and 144 deletions

View file

@ -459,44 +459,42 @@ void write_player(config_writer &out, const player_info& player)
// Deprecated, use other write_game below.
void write_game(const game_state& gamestate, config& cfg/*, WRITE_GAME_MODE mode*/)
void write_game(const game_state& game, config& cfg/*, WRITE_GAME_MODE mode*/)
{
log_scope("write_game");
cfg["label"] = gamestate.label;
cfg["label"] = game.label;
cfg["version"] = game_config::version;
cfg["scenario"] = gamestate.scenario;
cfg["scenario"] = game.scenario;
cfg["campaign"] = gamestate.campaign;
cfg["campaign"] = game.campaign;
cfg["campaign_type"] = gamestate.campaign_type;
cfg["campaign_type"] = game.campaign_type;
cfg["difficulty"] = gamestate.difficulty;
cfg["difficulty"] = game.difficulty;
cfg["campaign_define"] = gamestate.campaign_define;
cfg["campaign_define"] = game.campaign_define;
cfg.add_child("variables",gamestate.variables);
cfg.add_child("variables",game.variables);
for(std::map<std::string, player_info>::const_iterator i=gamestate.players.begin();
i!=gamestate.players.end(); ++i) {
for(std::map<std::string, player_info>::const_iterator i=game.players.begin();
i!=game.players.end(); ++i) {
config new_cfg;
write_player(i->second, new_cfg);
new_cfg["save_id"]=i->first;
cfg.add_child("player", new_cfg);
}
// if(mode == WRITE_FULL_GAME) {
if(gamestate.replay_data.child("replay") == NULL) {
cfg.add_child("replay",gamestate.replay_data);
if(game.replay_data.child("replay") == NULL) {
cfg.add_child("replay",game.replay_data);
}
cfg.add_child("snapshot",gamestate.snapshot);
cfg.add_child("replay_start",gamestate.starting_pos);
cfg.add_child("snapshot",game.snapshot);
cfg.add_child("replay_start",game.starting_pos);
cfg.add_child("statistics",statistics::write_stats());
// }
}
}
//}
void write_game(config_writer &out, const game_state& game)
{
@ -633,7 +631,7 @@ scoped_ostream open_save_game(const std::string &label)
}
}
void finish_save_game(config_writer &out, const game_state& gamestate, const std::string &label)
void finish_save_game(config_writer &out, const game_state& state, const std::string &label)
{
std::string name = label;
std::replace(name.begin(),name.end(),' ','_');
@ -645,7 +643,7 @@ void finish_save_game(config_writer &out, const game_state& gamestate, const std
}
config& summary = save_summary(label);
extract_summary_data_from_save(gamestate,summary);
extract_summary_data_from_save(state,summary);
const int mod_time = static_cast<int>(file_create_time(fname));
summary["mod_time"] = str_cast(mod_time);
write_save_index();
@ -655,12 +653,12 @@ void finish_save_game(config_writer &out, const game_state& gamestate, const std
}
//throws game::save_game_failed
void save_game(const game_state& gamestate)
void save_game(const game_state& state)
{
scoped_ostream os(open_save_game(gamestate.label));
scoped_ostream os(open_save_game(state.label));
config_writer out(*os, preferences::compress_saves(), PACKAGE);
write_game(out, gamestate);
finish_save_game(out, gamestate, gamestate.label);
write_game(out, state);
finish_save_game(out, state, state.label);
}
namespace {

View file

@ -163,7 +163,7 @@ 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& gamestate, config& cfg/*, WRITE_GAME_MODE mode=WRITE_FULL_GAME*/);
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);
// function returns true iff there is already savegame with that name
@ -171,13 +171,13 @@ 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& gamestate, const std::string &label);
void finish_save_game(config_writer &out, const game_state& state, 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_summary(const std::string& name, config& cfg_summary, std::string* error_log);
//throws gamestatus::save_game_failed
void save_game(const game_state& gamestate);
void save_game(const game_state& state);
//function to delete a save
void delete_game(const std::string& name);

View file

@ -465,9 +465,10 @@ namespace events{
return;
}
write_game_snapshot(gamestate_.snapshot);
config snapshot;
write_game_snapshot(snapshot);
try {
recorder.save_game(label, gamestate_);
recorder.save_game(label, snapshot, gamestate_.starting_pos);
if(dialog_type != gui::NULL_DIALOG) {
gui::show_dialog(*gui_,NULL,_("Saved"),_("The game has been saved"), gui::OK_ONLY);
}
@ -516,7 +517,6 @@ namespace events{
void menu_handler::write_game_snapshot(config& start) const
{
start.clear();
start.values = level_.values;
start["snapshot"] = "yes";
@ -573,20 +573,21 @@ namespace events{
gui_->labels().write(start);
}
void menu_handler::autosave(const std::string &label, unsigned turn) const
void menu_handler::autosave(const std::string &label, unsigned turn, const config &starting_pos) const
{
if(game_config::disable_autosave)
return;
Uint32 start, end;
start = SDL_GetTicks();
config snapshot;
write_game_snapshot(gamestate_.snapshot);
write_game_snapshot(snapshot);
try {
if (label.empty()) {
recorder.save_game(_("Auto-Save"), gamestate_);
recorder.save_game(_("Auto-Save"), snapshot, starting_pos);
} else {
recorder.save_game(label + "-" + _("Auto-Save") + lexical_cast<std::string>(turn), gamestate_);
recorder.save_game(label + "-" + _("Auto-Save") + lexical_cast<std::string>(turn), snapshot, starting_pos);
}
} catch(game::save_game_failed&) {
gui::show_dialog(*gui_,NULL,"",_("Could not auto save the game. Please save the game manually."),gui::MESSAGE);

View file

@ -109,7 +109,7 @@ public:
void do_search(const std::string& new_search);
void do_command(const std::string& str, const unsigned int team_num, mouse_handler& mousehandler);
void clear_undo_stack(const unsigned int team_num);
void autosave(const std::string &label, unsigned turn) const;
void autosave(const std::string &label, unsigned turn, const config &starting_pos) const;
protected:
void add_chat_message(const std::string& speaker, int side, const std::string& message, display::MESSAGE_TYPE type=display::MESSAGE_PRIVATE);

View file

@ -751,7 +751,7 @@ connect::connect(display& disp, const config& game_config, const game_data& data
//The number of human-controlled sides is important to know to let the server decide,
//how many players can join this game
int human_sides = 0;
const config::child_list cfg_sides = get_snapshot()->get_children("side");
const config::child_list cfg_sides = level_.get_children("side");
for (config::child_list::const_iterator side = cfg_sides.begin(); side != cfg_sides.end(); side++){
if ((**side)["controller"] == "human"){
human_sides++;
@ -845,7 +845,7 @@ void connect::start_game()
update_and_send_diff();
// Build the gamestate object after updating the level
level_to_gamestate(level_, state_, params_.saved_game);
level_to_gamestate(level_, state_);
config cfg;
cfg.add_child("start_game");
@ -1126,7 +1126,7 @@ void connect::lists_init()
ai_algorithms_ = get_available_ais();
//Factions
const config::child_itors sides = get_snapshot()->child_range("side");
const config::child_itors sides = level_.child_range("side");
//Teams
config::child_iterator sd;
@ -1232,14 +1232,18 @@ void connect::load_game()
}
}
level_.add_child("snapshot") = state_.snapshot;
level_ = state_.snapshot;
// Adds the replay data, and the replay start, to the level, so
// clients can receive it.
level_.add_child("replay") = state_.replay_data;
if(!state_.starting_pos.empty())
level_.add_child("replay_start") = state_.starting_pos;
//TO-DO: this should be done only if the player has selected for the replay to be viewed
if(false) {
level_.add_child("replay") = state_.replay_data;
if(!state_.starting_pos.empty())
level_.add_child("replay_start") = state_.starting_pos;
}
level_.add_child("statistics") = statistics::write_stats();
} else {
@ -1288,21 +1292,6 @@ void connect::load_game()
}
}
config* connect::get_snapshot(){
config* cfg_snapshot = NULL;
if (level_.child("snapshot") != NULL){
//savegame
cfg_snapshot = level_.child("snapshot");
}
else{
//fresh game, no snapshot available
cfg_snapshot = &level_;
}
return cfg_snapshot;
}
void connect::update_level()
{
// Import all sides into the level

View file

@ -169,9 +169,6 @@ private:
void load_game();
void lists_init();
//convenience function
config* get_snapshot();
// Updates the level_ variable to reflect the sides in the sides_ vector
void update_level();

View file

@ -41,38 +41,30 @@ void check_response(network::connection res, const config& data)
}
}
void level_to_gamestate(config& level, game_state& state, bool saved_game)
void level_to_gamestate(config& level, game_state& state)
{
//any replay data is only temporary and should be removed from
//the level data in case we want to save the game later
config * const replay_data = level.child("replay");
config replay_data_store;
if(replay_data != NULL) {
replay_data_store = *replay_data;
LOG_NW << "setting replay\n";
state.replay_data = *replay_data;
recorder = replay(replay_data_store);
if(!recorder.empty()) {
recorder.set_skip(false);
recorder.set_to_end();
}
level.clear_children("replay");
}
//adds the starting pos to the level
if(level.child("replay_start") == NULL){
level.add_child("replay_start", level);
}
state.starting_pos = *(level.child("replay_start"));
if(level.child("replay_start") == NULL)
level.add_child("replay_start") = level;
level["campaign_type"] = "multiplayer";
state.campaign_type = "multiplayer";
state.scenario = level["id"];
if (saved_game){
state.snapshot = *(level.child("snapshot"));
const config* const vars = level.child("variables");
if(vars != NULL) {
state.variables = *vars;
}
}
state.snapshot = level;
}

View file

@ -35,7 +35,7 @@ enum controller { CNTR_NETWORK = 0, CNTR_LOCAL, CNTR_COMPUTER, CNTR_EMPTY, CNTR_
void check_response(network::connection res, const config& data);
void level_to_gamestate(config& level, game_state& state, bool saved_game = false);
void level_to_gamestate(config& level, game_state& state);
std::string get_colour_string(int id);

View file

@ -310,10 +310,10 @@ void play_controller::init_side(const unsigned int team_index, bool is_replay){
recorder.add_event("turn 1");
recorder.add_event("new turn");
recorder.add_event("side turn");
game_events::fire("turn 1");
game_events::fire("new turn");
game_events::fire("side turn");
}
game_events::fire("turn 1");
game_events::fire("new turn");
game_events::fire("side turn");
}
first_turn_ = false;
} else
@ -322,8 +322,8 @@ void play_controller::init_side(const unsigned int team_index, bool is_replay){
if(!current_team.is_network()) {
if(!is_replay) {
recorder.add_event("side turn");
game_events::fire("side turn");
}
game_events::fire("side turn");
}
}

View file

@ -54,10 +54,10 @@ typedef std::map<std::string, player_controller> controller_map;
}
void play_replay(display& disp, game_state& gamestate, const config& game_config,
void play_replay(display& disp, game_state& state, const config& game_config,
const game_data& units_data, CVideo& video)
{
std::string type = gamestate.campaign_type;
std::string type = state.campaign_type;
if(type.empty())
type = "scenario";
@ -66,25 +66,42 @@ void play_replay(display& disp, game_state& gamestate, const config& game_config
//'starting_pos' will contain the position we start the game from.
config starting_pos;
recorder.set_save_info(gamestate);
recorder.set_save_info(state);
if (state.snapshot.child("side") != NULL){
state = read_game(units_data, &state.snapshot);
}
starting_pos = gamestate.starting_pos;
scenario = &starting_pos;
//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(!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) {
LOG_G << "loading starting position...\n";
starting_pos = state.starting_pos;
scenario = &starting_pos;
} else {
LOG_G << "loading scenario: '" << state.scenario << "'\n";
scenario = game_config.find_child(type,"id",state.scenario);
LOG_G << "scenario found: " << (scenario != NULL ? "yes" : "no") << "\n";
}
}
controller_map controllers;
const std::string current_scenario = gamestate.scenario;
const std::string current_scenario = state.scenario;
try {
// preserve old label eg. replay
if (gamestate.label.empty())
gamestate.label = (*scenario)["name"];
if (state.label.empty())
state.label = (*scenario)["name"];
play_replay_level(units_data,game_config,scenario,video,gamestate);
play_replay_level(units_data,game_config,scenario,video,state);
gamestate.snapshot = config();
state.snapshot = config();
recorder.clear();
gamestate.replay_data.clear();
state.replay_data.clear();
} catch(game::load_game_failed& e) {
gui::show_error_message(disp, _("The game could not be loaded: ") + e.message);
@ -108,12 +125,12 @@ void clean_autosaves(const std::string &label)
}
}
LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_config,
LEVEL_RESULT play_game(display& disp, game_state& state, 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 = gamestate.campaign_type;
std::string type = state.campaign_type;
if(type.empty())
type = "scenario";
@ -122,31 +139,29 @@ LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_
//'starting_pos' will contain the position we start the game from.
config starting_pos;
recorder.set_save_info(gamestate);
recorder.set_save_info(state);
//see if we load the scenario from the scenario data -- if there is
//no snapshot data available from a save
if(gamestate.snapshot.child("side") == NULL || !recorder.at_end()) {
//if the gamestate already contains a starting_pos, then
//we are starting a fresh multiplayer game. Otherwise this is the start
//of a campaign scenario.
if(gamestate.starting_pos.empty() == false) {
//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 the starting state is specified, then use that,
//otherwise get the scenario data and start from there.
if(state.starting_pos.empty() == false) {
LOG_G << "loading starting position...\n";
starting_pos = gamestate.starting_pos;
starting_pos = state.starting_pos;
scenario = &starting_pos;
} else {
LOG_G << "loading scenario: '" << gamestate.scenario << "'\n";
scenario = game_config.find_child(type,"id",gamestate.scenario);
starting_pos = *scenario;
gamestate.starting_pos = *scenario;
LOG_G << "loading scenario: '" << state.scenario << "'\n";
scenario = game_config.find_child(type,"id",state.scenario);
LOG_G << "scenario found: " << (scenario != NULL ? "yes" : "no") << "\n";
}
} else {
//This game was started from a savegame
LOG_G << "loading snapshot...\n";
//load from a save-snapshot.
starting_pos = gamestate.starting_pos;
scenario = &gamestate.snapshot;
starting_pos = state.snapshot;
scenario = &starting_pos;
state = read_game(units_data, &state.snapshot);
}
controller_map controllers;
@ -184,7 +199,7 @@ LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_
}
const config::child_list& story = scenario->get_children("story");
const std::string current_scenario = gamestate.scenario;
const std::string current_scenario = state.scenario;
bool save_game_after_scenario = true;
@ -192,8 +207,8 @@ LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_
try {
// preserve old label eg. replay
if (gamestate.label.empty())
gamestate.label = (*scenario)["name"];
if (state.label.empty())
state.label = (*scenario)["name"];
//if the entire scenario should be randomly generated
if((*scenario)["scenario_generation"] != "") {
@ -204,7 +219,7 @@ LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_
scenario2 = random_generate_scenario((*scenario)["scenario_generation"], scenario->child("generator"));
//level_ = scenario;
gamestate.starting_pos = scenario2;
state.starting_pos = scenario2;
scenario = &scenario2;
}
@ -225,25 +240,26 @@ LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_
new_level.values["map_data"] = map_data;
scenario = &new_level;
gamestate.starting_pos = new_level;
state.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,gamestate,story,log, skip_replay);
res = playsingle_scenario(units_data,game_config,scenario,video,state,story,log, skip_replay);
break;
case IO_SERVER:
case IO_CLIENT:
res = playmp_scenario(units_data,game_config,scenario,video,gamestate,story,log, skip_replay);
res = playmp_scenario(units_data,game_config,scenario,video,state,story,log, skip_replay);
break;
}
gamestate.snapshot = config();
state.snapshot = config();
if (res == DEFEAT) {
// tell all clients that the campaign won't continue
if(io_type == IO_SERVER) {
@ -263,10 +279,10 @@ LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_
}
//ask to save a replay of the game
if(res == VICTORY || res == DEFEAT) {
const std::string orig_scenario = gamestate.scenario;
gamestate.scenario = current_scenario;
const std::string orig_scenario = state.scenario;
state.scenario = current_scenario;
std::string label = gamestate.label + _(" replay");
std::string label = state.label + _(" replay");
bool retry = true;
@ -281,7 +297,7 @@ LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_
try {
config snapshot;
recorder.save_game(label, gamestate);
recorder.save_game(label, snapshot, state.starting_pos);
} catch(game::save_game_failed&) {
gui::show_error_message(disp, _("The replay could not be saved"));
retry = true;
@ -289,11 +305,11 @@ LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_
}
}
gamestate.scenario = orig_scenario;
state.scenario = orig_scenario;
}
recorder.clear();
gamestate.replay_data.clear();
state.replay_data.clear();
//continue without saving is like a victory, but the
//save game dialog isn't displayed
@ -315,8 +331,8 @@ LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_
}
//if the scenario hasn't been set in-level, set it now.
if(gamestate.scenario == current_scenario)
gamestate.scenario = (*scenario)["next_scenario"];
if(state.scenario == current_scenario)
state.scenario = (*scenario)["next_scenario"];
if(io_type == IO_CLIENT) {
config cfg;
@ -332,16 +348,16 @@ LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_
if(cfg.child("next_scenario")) {
starting_pos = (*cfg.child("next_scenario"));
scenario = &starting_pos;
gamestate = read_game(units_data, scenario);
state = read_game(units_data, scenario);
} else if(scenario->child("end_scenarios")) {
scenario = NULL;
gamestate.scenario = "null";
state.scenario = "null";
} else {
return QUIT;
}
} else {
scenario = game_config.find_child(type,"id",gamestate.scenario);
scenario = game_config.find_child(type,"id",state.scenario);
if(io_type == IO_SERVER && scenario != NULL) {
starting_pos = *scenario;
@ -371,7 +387,7 @@ LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_
// Adds player information, and other state
// information, to the configuration object
wassert(cfg.child("next_scenario") != NULL);
write_game(gamestate, *cfg.child("next_scenario")/*, WRITE_SNAPSHOT_ONLY*/);
write_game(state, *cfg.child("next_scenario")/*, WRITE_SNAPSHOT_ONLY*/);
network::send_data(cfg);
} else if(io_type == IO_SERVER && scenario == NULL) {
@ -383,12 +399,12 @@ LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_
if(scenario != NULL) {
// update the label
std::string oldlabel = gamestate.label;
gamestate.label = (*scenario)["name"];
std::string oldlabel = state.label;
state.label = (*scenario)["name"];
//if this isn't the last scenario, then save the game
if(save_game_after_scenario) {
gamestate.starting_pos = config();
state.starting_pos = config();
bool retry = true;
@ -398,12 +414,12 @@ LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_
const int should_save = dialogs::get_save_name(disp,
_("Do you want to save your game? (Also erases Auto-Save files)"),
_("Name:"),
&gamestate.label);
&state.label);
if(should_save == 0) {
try {
save_game(gamestate);
save_game(state);
if (!oldlabel.empty())
clean_autosaves(oldlabel);
} catch(game::save_game_failed&) {
@ -416,14 +432,14 @@ LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_
//update the replay start
//FIXME: this should only be done if the scenario was not tweaked.
gamestate.starting_pos = *scenario;
state.starting_pos = *scenario;
}
recorder.set_save_info(gamestate);
recorder.set_save_info(state);
}
if (!gamestate.scenario.empty() && gamestate.scenario != "null") {
gui::show_error_message(disp, _("Unknown scenario: '") + gamestate.scenario + '\'');
if (!state.scenario.empty() && state.scenario != "null") {
gui::show_error_message(disp, _("Unknown scenario: '") + state.scenario + '\'');
return QUIT;
}

View file

@ -457,7 +457,7 @@ void playsingle_controller::before_human_turn(bool save)
gui_->update_display();
if (save) {
menu_handler_.autosave(gamestate_.label, status_.turn());
menu_handler_.autosave(gamestate_.label, status_.turn(), gamestate_.starting_pos);
}
if(preferences::turn_bell()) {

View file

@ -209,12 +209,12 @@ bool replay::is_skipping() const
return skip_;
}
void replay::save_game(const std::string& label, const game_state& gamestate, bool include_replay)
void replay::save_game(const std::string& label, const config& snapshot,
const config& starting_pos, bool include_replay)
{
log_scope("replay::save_game");
saveInfo_.snapshot = gamestate.snapshot;
saveInfo_.starting_pos = gamestate.starting_pos;
saveInfo_.variables = gamestate.variables;
saveInfo_.snapshot = snapshot;
saveInfo_.starting_pos = starting_pos;
if(include_replay) {
saveInfo_.replay_data = cfg_;

View file

@ -42,7 +42,8 @@ public:
void set_skip(bool skip);
bool is_skipping() const;
void save_game(const std::string& label, const game_state& gamestate, bool include_replay = true);
void save_game(const std::string& label, const config& snapshot,
const config& starting_pos, bool include_replay = true);
void add_start();
void add_recruit(int unit_index, const gamemap::location& loc);