Skip to content

Commit

Permalink
change saved_game & savefile format
Browse files Browse the repository at this point in the history
we move code from playcampaign.cpp to saved_game.cpp
we also change the savefile format so that ingame savegames don't
contain [carryover_sides_start] anymore becaus all that was already
merged to both [snapshot] and replay_start.

This commit is likley to break something. If so that will be fixed in
later commits.
  • Loading branch information
gfgtdf committed Jun 15, 2014
1 parent 9645fc4 commit 12dec70
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 109 deletions.
10 changes: 5 additions & 5 deletions src/game_controller.cpp
Expand Up @@ -589,10 +589,10 @@ bool game_controller::load_game()
recorder.start_replay();
recorder.set_skip(false);

LOG_CONFIG << "has snapshot: " << (state_.snapshot.child("side") ? "yes" : "no") << "\n";
LOG_CONFIG << "has is middle game savefile: " << (state_.is_mid_game_save() ? "yes" : "no") << "\n";

if (!state_.snapshot.child("side")) {
// No snapshot; this is a start-of-scenario
if (!state_.is_mid_game_save()) {
//this is a start-of-scenario
if (load.show_replay()) {
// There won't be any turns to replay, but the
// user gets to watch the intro sequence again ...
Expand All @@ -616,7 +616,7 @@ bool game_controller::load_game()
}

if(state_.classification().campaign_type == game_classification::MULTIPLAYER) {
BOOST_FOREACH(config &side, state_.snapshot.child_range("side"))
BOOST_FOREACH(config &side, state_.get_starting_pos().child_range("side"))
{
if (side["controller"] == "network")
side["controller"] = "human";
Expand All @@ -627,7 +627,7 @@ bool game_controller::load_game()
}

if (load.cancel_orders()) {
BOOST_FOREACH(config &side, state_.snapshot.child_range("side"))
BOOST_FOREACH(config &side, state_.get_starting_pos().child_range("side"))
{
if (side["controller"] != "human") continue;
BOOST_FOREACH(config &unit, side.child_range("unit"))
Expand Down
4 changes: 2 additions & 2 deletions src/mp_game_utils.cpp
Expand Up @@ -229,7 +229,7 @@ void level_to_gamestate(config& level, saved_game& state)
// If we start a fresh game, there won't be any snapshot information.
// If however this is a savegame, we got a valid snapshot here.
if (saved_game) {
state.snapshot = snapshot;
state.set_snapshot(snapshot);
if (const config& v = snapshot.child("variables")) {
sides.set_variables(v);
}
Expand All @@ -240,7 +240,7 @@ void level_to_gamestate(config& level, saved_game& state)
// could have changed and need to be replaced.
if (saved_game || start_of_scenario){
config::child_itors saved_sides = saved_game ?
state.snapshot.child_range("side") :
state.get_starting_pos().child_range("side") :
state.replay_start().child_range("side");
config::const_child_itors level_sides = level.child_range("side");

Expand Down
109 changes: 57 additions & 52 deletions src/playcampaign.cpp
Expand Up @@ -52,10 +52,11 @@

static lg::log_domain log_engine("engine");
#define LOG_NG LOG_STREAM(info, log_engine)
#define ERR_NG LOG_STREAM(err, log_engine)

static lg::log_domain log_enginerefac("enginerefac");
#define LOG_RG LOG_STREAM(info, log_enginerefac)

#if 0
static void team_init(config& level, saved_game& gamestate){
//if we are at the start of a new scenario, initialize carryover_sides
if(gamestate.snapshot.child_or_empty("variables")["turn_number"].to_int(-1)<1){
Expand All @@ -72,6 +73,7 @@ static void team_init(config& level, saved_game& gamestate){
gamestate.carryover_sides = sides.to_config();
}
}
#endif

static void store_carryover(saved_game& gamestate, playsingle_controller& playcontroller, display& disp, const end_level_data& end_level, const LEVEL_RESULT res){
bool has_next_scenario = !resources::gamedata->next_scenario().empty() &&
Expand Down Expand Up @@ -207,7 +209,6 @@ LEVEL_RESULT play_replay(display& disp, saved_game& gamestate, const config& gam

LEVEL_RESULT res = play_replay_level(game_config, video, gamestate, is_unit_test);

gamestate.snapshot = config();
recorder.clear();
gamestate.replay_data.clear();

Expand Down Expand Up @@ -246,17 +247,16 @@ LEVEL_RESULT play_replay(display& disp, saved_game& gamestate, const config& gam
}

static LEVEL_RESULT playsingle_scenario(const config& game_config,
const config* level, display& disp, saved_game& state_of_game,
display& disp, saved_game& state_of_game,
const config::const_child_itors &story,
bool skip_replay, end_level_data &end_level)
{
const int ticks = SDL_GetTicks();

config init_level = *level;
team_init(init_level, state_of_game);


state_of_game.expand_carryover();

LOG_NG << "creating objects... " << (SDL_GetTicks() - ticks) << "\n";
playsingle_controller playcontroller(init_level, state_of_game, ticks, game_config, disp.video(), skip_replay);
playsingle_controller playcontroller(state_of_game.get_starting_pos(), state_of_game, ticks, game_config, disp.video(), skip_replay);
LOG_NG << "created objects... " << (SDL_GetTicks() - playcontroller.get_ticks()) << "\n";

LEVEL_RESULT res = playcontroller.play_scenario(story, skip_replay);
Expand Down Expand Up @@ -290,16 +290,14 @@ static LEVEL_RESULT playsingle_scenario(const config& game_config,


static LEVEL_RESULT playmp_scenario(const config& game_config,
const config* level, display& disp, saved_game& state_of_game,
display& disp, saved_game& state_of_game,
const config::const_child_itors &story, bool skip_replay,
bool blindfold_replay, io_type_t& io_type, end_level_data &end_level)
{
const int ticks = SDL_GetTicks();
state_of_game.expand_carryover();

config init_level = *level;
team_init(init_level, state_of_game);

playmp_controller playcontroller(init_level, state_of_game, ticks,
playmp_controller playcontroller(state_of_game.get_starting_pos(), state_of_game, ticks,
game_config, disp.video(), skip_replay, blindfold_replay, io_type == IO_SERVER);
LEVEL_RESULT res = playcontroller.play_scenario(story, skip_replay);

Expand Down Expand Up @@ -345,17 +343,18 @@ LEVEL_RESULT play_game(game_display& disp, saved_game& gamestate,
{
const std::string campaign_type_str = lexical_cast_default<std::string> (gamestate.classification().campaign_type);

config const* scenario = NULL;

//config const* scenario = NULL;

config& starting_pos = gamestate.get_starting_pos();
#if 0
// 'starting_pos' will contain the position we start the game from.
config starting_pos;

carryover_info sides = carryover_info(gamestate.carryover_sides_start);

// Do we have any snapshot data?
// yes => this must be a savegame
// no => we are starting a fresh scenario
if (!gamestate.snapshot.child("side"))
if (!gamestate.is_mid_game_save())
{
gamestate.classification().completion = "running";
// Campaign or Multiplayer?
Expand Down Expand Up @@ -385,32 +384,34 @@ LEVEL_RESULT play_game(game_display& disp, saved_game& gamestate,
// This game was started from a savegame
LOG_G << "loading snapshot...\n";
starting_pos = gamestate.replay_start();
scenario = &gamestate.snapshot;
scenario = &gamestate.get_starting_pos();
// When starting wesnoth --multiplayer there might be
// no variables which leads to a segfault
if (const config &vars = gamestate.snapshot.child("variables")) {
sides.set_variables(vars);
}
sides.get_wml_menu_items().set_menu_items(gamestate.snapshot);
// Replace game label with that from snapshot
if (!gamestate.snapshot["label"].empty()){
gamestate.classification().label = gamestate.snapshot["label"].str();
if (!gamestate.get_starting_pos()["label"].empty()){
gamestate.classification().label = gamestate.get_starting_pos()["label"].str();
}
}

gamestate.carryover_sides_start = sides.to_config();

while(scenario != NULL) {
#else
gamestate.expand_scenario();
#endif
while(gamestate.valid()) {
// If we are a multiplayer client, tweak the controllers
// (actually, moved to server. do we still need this starting_pos thing?)
if(io_type == IO_CLIENT) {
/*if(io_type == IO_CLIENT) {
if(scenario != &starting_pos) {
starting_pos = *scenario;
scenario = &starting_pos;
}
}

config::const_child_itors story = scenario->child_range("story");
*/
config::const_child_itors story = starting_pos.child_range("story");
//TODO: remove once scenario in carryover_info/gamedata is confirmed
// gamestate.classification().next_scenario = (*scenario)["next_scenario"].str();

Expand All @@ -423,39 +424,43 @@ LEVEL_RESULT play_game(game_display& disp, saved_game& gamestate,
// Preserve old label eg. replay
if (gamestate.classification().label.empty()) {
if (gamestate.classification().abbrev.empty())
gamestate.classification().label = (*scenario)["name"].str();
gamestate.classification().label = starting_pos["name"].str();
else {
gamestate.classification().label = gamestate.classification().abbrev;
gamestate.classification().label.append("-");
gamestate.classification().label.append((*scenario)["name"]);
gamestate.classification().label.append(starting_pos["name"]);
}
}

// If the entire scenario should be randomly generated
if((*scenario)["scenario_generation"] != "") {
generate_scenario(scenario);
if(starting_pos["scenario_generation"] != "") {
const config * tmp = &starting_pos;
generate_scenario(tmp);
}
std::string map_data = (*scenario)["map_data"];
if(map_data.empty() && (*scenario)["map"] != "") {
map_data = read_map((*scenario)["map"]);
std::string map_data = starting_pos["map_data"];
if(map_data.empty() && starting_pos["map"] != "") {
map_data = read_map(starting_pos["map"]);
}

// If the map should be randomly generated
if(map_data.empty() && (*scenario)["map_generation"] != "") {
generate_map(scenario);
if(map_data.empty() && starting_pos["map_generation"] != "") {
const config * tmp = &starting_pos;
generate_map(tmp);
}

sound::empty_playlist();

switch (io_type){
case IO_NONE:
res = playsingle_scenario(game_config, scenario, disp, gamestate, story, skip_replay, end_level);
res = playsingle_scenario(game_config, disp, gamestate, story, skip_replay, end_level);
break;
case IO_SERVER:
case IO_CLIENT:
res = playmp_scenario(game_config, scenario, disp, gamestate, story, skip_replay, blindfold_replay, io_type, end_level);
res = playmp_scenario(game_config, disp, gamestate, story, skip_replay, blindfold_replay, io_type, end_level);
break;
}
gamestate.carryover_sides = config();
gamestate.remove_snapshot();
} catch(game::load_game_failed& e) {
gui2::show_error_message(disp.video(), _("The game could not be loaded: ") + e.message);
return QUIT;
Expand Down Expand Up @@ -504,7 +509,7 @@ LEVEL_RESULT play_game(game_display& disp, saved_game& gamestate,
//If there is no next scenario we're done now.
if(res == QUIT || !end_level.proceed_to_next_level || gamestate.carryover_sides_start["next_scenario"].empty())
{
gamestate.snapshot = config();
gamestate.set_snapshot(config());
return res;
}
else if(res == OBSERVER_END)
Expand All @@ -514,7 +519,7 @@ LEVEL_RESULT play_game(game_display& disp, saved_game& gamestate,
gui2::tmessage::yes_no_buttons);

if(dlg_res == gui2::twindow::CANCEL) {
gamestate.snapshot = config();
gamestate.set_snapshot(config());
return res;
}
}
Expand All @@ -528,7 +533,7 @@ LEVEL_RESULT play_game(game_display& disp, saved_game& gamestate,
// Switch to the next scenario.
//gamestate.classification().scenario = gamestate.classification().next_scenario;

sides = carryover_info(gamestate.carryover_sides_start);
carryover_info sides = carryover_info(gamestate.carryover_sides_start);
sides.rng().rotate_random();
gamestate.carryover_sides_start = sides.to_config();

Expand All @@ -537,27 +542,27 @@ LEVEL_RESULT play_game(game_display& disp, saved_game& gamestate,
mp::ui::result wait_res = mp::goto_mp_wait(gamestate, disp,
game_config, res == OBSERVER_END);
if (wait_res == mp::ui::QUIT) {
gamestate.snapshot = config();
gamestate.set_snapshot(config());
return QUIT;
}

starting_pos = gamestate.replay_start();
gamestate.set_scenario(gamestate.replay_start());
gamestate = saved_game(starting_pos);
// Retain carryover_sides_start, as the config from the server
// doesn't contain it.
gamestate.carryover_sides_start = sides.to_config();
} else {
// Retrieve next scenario data.
scenario = &game_config.find_child(campaign_type_str, "id",
const config & scentemp = game_config.find_child(campaign_type_str, "id",
gamestate.carryover_sides_start["next_scenario"]);
if (!*scenario) {
scenario = NULL;
if (!scentemp) {
gamestate.set_scenario(config());
} else {
starting_pos = *scenario;
scenario = &starting_pos;
gamestate.set_scenario(scentemp);
}
config * scenario = &gamestate.get_starting_pos();

if (io_type == IO_SERVER && scenario != NULL) {
if (io_type == IO_SERVER && scentemp) {
mp_game_settings& params = gamestate.mp_settings();

// A hash have to be generated using an unmodified
Expand All @@ -566,7 +571,8 @@ LEVEL_RESULT play_game(game_display& disp, saved_game& gamestate,

// Apply carryover before passing a scenario data to the
// mp::connect_engine.
team_init(starting_pos, gamestate);
gamestate.expand_carryover();
//team_init(starting_pos, gamestate);

//We don't merge WML until start of next scenario, but if we want to allow user to disable MP ui in transition,
//then we have to move "allow_new_game" attribute over now.
Expand Down Expand Up @@ -628,14 +634,14 @@ LEVEL_RESULT play_game(game_display& disp, saved_game& gamestate,
}
}

if(scenario != NULL) {
if(gamestate.valid()) {
// Update the label
if (gamestate.classification().abbrev.empty())
gamestate.classification().label = (*scenario)["name"].str();
gamestate.classification().label = starting_pos["name"].str();
else {
gamestate.classification().label = gamestate.classification().abbrev;
gamestate.classification().label.append("-");
gamestate.classification().label.append((*scenario)["name"]);
gamestate.classification().label.append(starting_pos["name"]);
}

// If this isn't the last scenario, then save the game
Expand All @@ -654,7 +660,6 @@ LEVEL_RESULT play_game(game_display& disp, saved_game& gamestate,
}

}
gamestate.snapshot = config();
}

if (!gamestate.carryover_sides_start["next_scenario"].empty() && gamestate.carryover_sides_start["next_scenario"] != "null") {
Expand Down
9 changes: 5 additions & 4 deletions src/playsingle_controller.cpp
Expand Up @@ -343,7 +343,8 @@ void playsingle_controller::report_victory(

possible_end_play_signal playsingle_controller::play_scenario_init(end_level_data & /*eld*/, bool & past_prestart) {
// At the beginning of the scenario, save a snapshot as replay_start
if(gamestate_.snapshot.child_or_empty("variables")["turn_number"].to_int(-1)<1){
if(gamestate_.replay_start().empty())
{
gamestate_.replay_start() = to_config();
}
HANDLE_END_PLAY_SIGNAL( fire_preload() );
Expand Down Expand Up @@ -497,7 +498,7 @@ LEVEL_RESULT playsingle_controller::play_scenario(
if (gameboard_.teams().empty())
{
//store persistent teams
gamestate_.snapshot = config();
gamestate_.set_snapshot(config());

return VICTORY; // this is probably only a story scenario, i.e. has its endlevel in the prestart event
}
Expand Down Expand Up @@ -568,7 +569,7 @@ LEVEL_RESULT playsingle_controller::play_scenario(
LOG_NG << "Add units that survived the scenario to the recall list.\n";
gameboard_.all_survivors_to_recall();

gamestate_.snapshot = config();
gamestate_.set_snapshot(config());
if(!is_observer()) {
persist_.end_transaction();
}
Expand All @@ -579,7 +580,7 @@ LEVEL_RESULT playsingle_controller::play_scenario(
{
LOG_NG << "resuming from loaded linger state...\n";
//as carryover information is stored in the snapshot, we have to re-store it after loading a linger state
gamestate_.snapshot = config();
gamestate_.set_snapshot(config());
if(!is_observer()) {
persist_.end_transaction();
}
Expand Down

0 comments on commit 12dec70

Please sign in to comment.