Skip to content

Commit

Permalink
recorder writes directly into saved_game object
Browse files Browse the repository at this point in the history
Previously recorder had config memaber and saved_game had a config
memeber.
And when saving a game the config was copied from recorder to saved_game
and the other way when loading a game.

Now the recorder object directly writes into the saved_game object. This
saves some copying when saving and loading data.

I also moved the pos_ variable from the recorder object to the
saved_game replay_recorder_base object, This fixes a bug where saving a
game during a replay also caused to not yet played turns to be written
into the savefile.
  • Loading branch information
gfgtdf committed Mar 6, 2015
1 parent 9aa6bb3 commit bb0ecd1
Show file tree
Hide file tree
Showing 30 changed files with 312 additions and 197 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -956,6 +956,7 @@ set(wesnoth-main_SRC
replay.cpp
replay_helper.cpp
replay_controller.cpp
replay_recorder_base.cpp
resources.cpp
save_blocker.cpp
save_index.cpp
Expand Down
1 change: 1 addition & 0 deletions src/SConscript
Expand Up @@ -529,6 +529,7 @@ wesnoth_sources = Split("""
replay.cpp
replay_helper.cpp
replay_controller.cpp
replay_recorder_base.cpp
resources.cpp
save_blocker.cpp
save_index.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/actions/move.cpp
Expand Up @@ -1266,7 +1266,7 @@ size_t move_unit_and_record(const std::vector<map_location> &steps,
/*
enter the synced mode and do the actual movement.
*/
recorder.add_synced_command("move",replay_helper::get_movement(steps, continued_move, skip_ally_sighted));
resources::recorder->add_synced_command("move",replay_helper::get_movement(steps, continued_move, skip_ally_sighted));
set_scontext_synced sync;
size_t r = move_unit_internal(undo_stack, show_move, interrupted, mover);
resources::controller->check_victory();
Expand Down
18 changes: 9 additions & 9 deletions src/actions/undo.cpp
Expand Up @@ -659,7 +659,7 @@ void undo_list::undo()
n_unit::id_manager::instance().set_save_id(last_unit_id - action->unit_id_diff);

// Bookkeeping.
recorder.undo_cut(action->get_replay_data());
resources::recorder->undo_cut(action->get_replay_data());
redos_.push_back(action.release());
resources::whiteboard->on_gamestate_change();

Expand Down Expand Up @@ -822,10 +822,10 @@ bool undo_list::auto_shroud_action::undo(int /*side*/, undo_list & undos)
{
// This does not count as an undoable action, so undo the next
// action instead.
recorder.undo();
resources::recorder->undo();
undos.undo();
// Now keep the auto-shroud toggle at the top of the undo stack.
recorder.add_synced_command("auto_shroud", replay_helper::get_auto_shroud(active));
resources::recorder->add_synced_command("auto_shroud", replay_helper::get_auto_shroud(active));
undos.add_auto_shroud(active, this->unit_id_diff);
// Shroud actions never get moved to the redo stack, so claim an error.
return false;
Expand All @@ -839,10 +839,10 @@ bool undo_list::update_shroud_action::undo(int /*side*/, undo_list & undos)
{
// This does not count as an undoable action, so undo the next
// action instead.
recorder.undo();
resources::recorder->undo();
undos.undo();
// Now keep the shroud update at the top of the undo stack.
recorder.add_synced_command("update_shroud", replay_helper::get_update_shroud());
resources::recorder->add_synced_command("update_shroud", replay_helper::get_update_shroud());

undos.add_update_shroud(this->unit_id_diff);
// Shroud actions never get moved to the redo stack, so claim an error.
Expand Down Expand Up @@ -898,7 +898,7 @@ bool undo_list::dismiss_action::redo(int side)
<< ", which has no recall list!\n";
return false;
}
recorder.redo(replay_data);
resources::recorder->redo(replay_data);
replay_data.clear();
current_team.recall_list().erase_if_matches_id(dismissed_unit->id());
return true;
Expand Down Expand Up @@ -931,7 +931,7 @@ bool undo_list::recall_action::redo(int side)

const std::string &msg = find_recall_location(side, loc, from, *un);
if ( msg.empty() ) {
recorder.redo(replay_data);
resources::recorder->redo(replay_data);
replay_data.clear();
set_scontext_synced sync;
recall_unit(id, current_team, loc, from, true, false);
Expand Down Expand Up @@ -979,7 +979,7 @@ bool undo_list::recruit_action::redo(int side)
if ( msg.empty() ) {
//MP_COUNTDOWN: restore recruitment bonus
current_team.set_action_bonus_count(1 + current_team.action_bonus_count());
recorder.redo(replay_data);
resources::recorder->redo(replay_data);
replay_data.clear();
set_scontext_synced sync;
recruit_unit(u_type, side, loc, from, true, false);
Expand Down Expand Up @@ -1044,7 +1044,7 @@ bool undo_list::move_action::redo(int side)
}

gui.invalidate_unit_after_move(route.front(), route.back());
recorder.redo(replay_data);
resources::recorder->redo(replay_data);
replay_data.clear();
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ai/actions.cpp
Expand Up @@ -287,7 +287,7 @@ void attack_result::do_execute()
if(synced_context::get_synced_state() != synced_context::SYNCED) //RAII block for set_scontext_synced
{
//we don't use synced_context::run_in_synced_context because that wouldn't allow us to pass advancements_
recorder.add_synced_command("attack", replay_helper::get_attack(attacker_loc_, defender_loc_, attacker_weapon, defender_weapon, a_->type_id(),
resources::recorder->add_synced_command("attack", replay_helper::get_attack(attacker_loc_, defender_loc_, attacker_weapon, defender_weapon, a_->type_id(),
d_->type_id(), a_->level(), d_->level(), resources::tod_manager->turn(),
resources::tod_manager->get_time_of_day()));
set_scontext_synced sync;
Expand Down
4 changes: 2 additions & 2 deletions src/ai/formula/function_table.cpp
Expand Up @@ -1368,8 +1368,8 @@ class debug_label_function : public function_expression {

const terrain_label *res;
res = gui->labels().set_label(location, text, team_name, color);
if (res)
recorder.add_label(res);
if (res && resources::recorder)
resources::recorder->add_label(res);
}

const formula_ai& ai_;
Expand Down
20 changes: 10 additions & 10 deletions src/ai/testing.cpp
Expand Up @@ -69,21 +69,21 @@ void ai_testing::log_turn(const char* msg, unsigned int side)
c["units_cost"] = _units_cost;
c["gold"] = _gold;
c["villages"] = _villages;
recorder.add_log_data("ai_log","turn_info",c);
resources::recorder->add_log_data("ai_log","turn_info",c);
}

void ai_testing::log_draw()
{
LOG_AI_TESTING << "DRAW:" << std::endl;
recorder.add_log_data("ai_log","result","draw");
resources::recorder->add_log_data("ai_log","result","draw");
}

void ai_testing::log_victory(std::set<unsigned int> winners)
{
recorder.add_log_data("ai_log","result","victory");
resources::recorder->add_log_data("ai_log","result","victory");
for(std::set<unsigned int>::const_iterator w = winners.begin(); w != winners.end(); ++w) {
LOG_AI_TESTING << "WINNER: "<< *w <<std::endl;
recorder.add_log_data("ai_log","winner",str_cast(*w));
resources::recorder->add_log_data("ai_log","winner",str_cast(*w));
}
}

Expand All @@ -93,22 +93,22 @@ void ai_testing::log_game_start()
int side = tm-resources::teams->begin()+1;
LOG_AI_TESTING << "AI_IDENTIFIER"<<side<<": " << ai::manager::get_active_ai_identifier_for_side(side) <<std::endl;
LOG_AI_TESTING << "TEAM"<<side<<": " << tm->name() << std::endl;
recorder.add_log_data("ai_log","ai_id"+str_cast(side),ai::manager::get_active_ai_identifier_for_side(side));
recorder.add_log_data("ai_log","faction"+str_cast(side),tm->name());
resources::recorder->add_log_data("ai_log","ai_id"+str_cast(side),ai::manager::get_active_ai_identifier_for_side(side));
resources::recorder->add_log_data("ai_log","faction"+str_cast(side),tm->name());
///@todo 1.9: add information about ai_config
}
LOG_AI_TESTING << "VERSION: " << game_config::revision << std::endl;
recorder.add_log_data("ai_log","version",game_config::revision);
resources::recorder->add_log_data("ai_log","version",game_config::revision);
}

void ai_testing::log_game_end()
{
LOG_AI_TESTING << "GAME_END_TURN: "<< resources::tod_manager->turn() <<std::endl;
recorder.add_log_data("ai_log","end_turn",
resources::recorder->add_log_data("ai_log","end_turn",
str_cast(resources::tod_manager->turn()));
for (std::vector<team>::const_iterator tm = resources::teams->begin(); tm != resources::teams->end(); ++tm) {
int side = tm-resources::teams->begin()+1;
recorder.add_log_data("ai_log","end_gold"+str_cast(side),str_cast(tm->gold()));
recorder.add_log_data("ai_log","end_units"+str_cast(side),str_cast(resources::gameboard->side_units(side)));
resources::recorder->add_log_data("ai_log","end_gold"+str_cast(side),str_cast(tm->gold()));
resources::recorder->add_log_data("ai_log","end_units"+str_cast(side),str_cast(resources::gameboard->side_units(side)));
}
}
4 changes: 2 additions & 2 deletions src/ai/testing/ca_global_fallback.cpp
Expand Up @@ -58,8 +58,8 @@ static void display_label(int /*side*/, const map_location& location, const std:

const terrain_label *res;
res = gui->labels().set_label(location, text, team_name, color);
if (res)
recorder.add_label(res);
if (res && resources::recorder)
resources::recorder->add_label(res);
}


Expand Down
2 changes: 0 additions & 2 deletions src/dialogs.cpp
Expand Up @@ -163,8 +163,6 @@ gui::dialog_button_action::RESULT delete_recall_unit::button_pressed(int menu_se

// Record the dismissal, then delete the unit.
synced_context::run_and_throw("disband", replay_helper::get_disband(dismissed_unit->id()));
//recorder.add_disband(dismissed_unit->id());
//recall_list.erase(dismissed_unit);

return gui::DELETE_ITEM;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/game_events/action_wml.cpp
Expand Up @@ -497,7 +497,7 @@ wml_action::wml_action(const std::string & tag, handler function)
/// @todo Finish experimenting.
WML_HANDLER_FUNCTION(clear_global_variable,/**/,pcfg)
{
if (get_replay_source().at_end() || (network::nconnections() != 0))
if (!resources::controller->is_replay())
verify_and_clear_global_variable(pcfg);
}

Expand Down
8 changes: 2 additions & 6 deletions src/game_initialization/multiplayer.cpp
Expand Up @@ -476,8 +476,6 @@ static void enter_wait_mode(game_display& disp, const config& game_config,
case mp::ui::PLAY:
play_game(disp, state, game_config, game_config_manager::get()->terrain_types(), IO_CLIENT,
preferences::skip_mp_replay() && observe, true, preferences::blindfold_replay() && observe);
recorder.clear();

break;
case mp::ui::QUIT:
default:
Expand Down Expand Up @@ -520,8 +518,6 @@ static bool enter_connect_mode(game_display& disp, const config& game_config,
case mp::ui::PLAY:
play_game(disp, state, game_config, game_config_manager::get()->terrain_types(), IO_SERVER, false,
!local_players_only);
recorder.clear();

break;
case mp::ui::CREATE:
enter_create_mode(disp, game_config, state, local_players_only);
Expand Down Expand Up @@ -895,14 +891,14 @@ void start_local_game_commandline(game_display& disp, const config& game_config,

std::string label = "";
if (cmdline_opts.multiplayer_label) label = *cmdline_opts.multiplayer_label;
recorder.add_log_data("ai_log","ai_label",label);

//resources::recorder->add_log_data("ai_log","ai_label",label);

unsigned int repeat = (cmdline_opts.multiplayer_repeat) ? *cmdline_opts.multiplayer_repeat : 1;
for(unsigned int i = 0; i < repeat; i++){
saved_game state_copy(state);
play_game(disp, state_copy, game_config, game_config_manager::get()->terrain_types(), IO_SERVER, false, false);
}
recorder.clear();
}

void start_client(game_display& disp, const config& game_config,
Expand Down
9 changes: 2 additions & 7 deletions src/game_initialization/playcampaign.cpp
Expand Up @@ -127,7 +127,7 @@ static void show_carryover_message(saved_game& gamestate, playsingle_controller&
LEVEL_RESULT play_replay(display& disp, saved_game& gamestate, const config& game_config,
const tdata_cache & tdata, bool is_unit_test)
{
recorder = replay(gamestate.replay_data);
gamestate.get_replay().set_pos(0);
// 'starting_pos' will contain the position we start the game from.
// this call also might expand [scenario] in case thatt there is no replay_start
const config& starting_pos = gamestate.get_replay_starting_pos();
Expand All @@ -141,9 +141,6 @@ LEVEL_RESULT play_replay(display& disp, saved_game& gamestate, const config& gam

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

recorder.clear();
gamestate.replay_data.clear();

return res;
} catch(game::load_game_failed& e) {
ERR_NG << std::string(_("The game could not be loaded: ")) + " (game::load_game_failed) " + e.message << std::endl;
Expand Down Expand Up @@ -256,8 +253,7 @@ LEVEL_RESULT play_game(game_display& disp, saved_game& gamestate,
io_type_t io_type, bool skip_replay,
bool network_game, bool blindfold_replay, bool is_unit_test)
{
recorder = replay(gamestate.replay_data);
recorder.set_to_end();
gamestate.get_replay().set_to_end();

gamestate.expand_scenario();

Expand Down Expand Up @@ -333,7 +329,6 @@ LEVEL_RESULT play_game(game_display& disp, saved_game& gamestate,
}

gamestate.convert_to_start_save();
recorder.clear();

//If there is no next scenario we're done now.
if(gamestate.get_scenario_id().empty())
Expand Down
13 changes: 7 additions & 6 deletions src/menu_events.cpp
Expand Up @@ -65,6 +65,7 @@
#include "preferences_display.hpp"
#include "replay.hpp"
#include "replay_helper.hpp"
#include "resources.hpp"
#include "savegame.hpp"
#include "save_index.hpp"
#include "scripting/game_lua_kernel.hpp"
Expand Down Expand Up @@ -513,9 +514,9 @@ void menu_handler::show_chat_log()
{
config c;
c["name"] = "prototype of chat log";
gui2::tchat_log chat_log_dialog(vconfig(c),&recorder);
gui2::tchat_log chat_log_dialog(vconfig(c), resources::recorder);
chat_log_dialog.show(gui_->video());
//std::string text = recorder.build_chat_log();
//std::string text = resources::recorder->build_chat_log();
//gui::show_dialog(*gui_,NULL,_("Chat Log"),"",gui::CLOSE_ONLY,NULL,NULL,"",&text);

}
Expand Down Expand Up @@ -935,7 +936,7 @@ void menu_handler::rename_unit()
const std::string label(N_("Name:"));

if(gui2::tedit_text::execute(title, label, name, gui_->video())) {
recorder.add_rename(name, un->get_location());
resources::recorder->add_rename(name, un->get_location());
un->rename(name);
gui_->invalidate_unit();
}
Expand Down Expand Up @@ -1205,7 +1206,7 @@ void menu_handler::label_terrain(mouse_handler& mousehandler, bool team_only)
}
const terrain_label* res = gui_->labels().set_label(loc, label, team_name, color);
if (res)
recorder.add_label(res);
resources::recorder->add_label(res);
}
}

Expand All @@ -1215,7 +1216,7 @@ void menu_handler::clear_labels()
&& !board().is_observer())
{
gui_->labels().clear(gui_->current_team_name(), false);
recorder.clear_labels(gui_->current_team_name(), false);
resources::recorder->clear_labels(gui_->current_team_name(), false);
}
}

Expand Down Expand Up @@ -2581,7 +2582,7 @@ void menu_handler::send_chat_message(const std::string& message, bool allies_onl
}
}

recorder.speak(cfg);
resources::recorder->speak(cfg);

add_chat_message(time, cfg["id"], side, message,
private_message ? events::chat_handler::MESSAGE_PRIVATE : events::chat_handler::MESSAGE_PUBLIC);
Expand Down
5 changes: 4 additions & 1 deletion src/play_controller.cpp
Expand Up @@ -99,6 +99,7 @@ static void clear_resources()
resources::tod_manager = NULL;
resources::tunnels = NULL;
resources::undo_stack = NULL;
resources::recorder = NULL;
resources::units = NULL;
resources::whiteboard.reset();

Expand Down Expand Up @@ -132,6 +133,7 @@ play_controller::play_controller(const config& level, saved_game& state_of_game,
, xp_mod_(new unit_experience_accelerator(level["experience_modifier"].to_int(100)))
, statistics_context_(new statistics::scenario_context(level["name"]))
, undo_stack_(new actions::undo_list(level.child("undo_stack")))
, replay_(new replay(state_of_game.get_replay()))
, loading_game_(level["playing_team"].empty() == false)
, player_number_(1)
, first_player_(level["playing_team"].to_int() + 1)
Expand All @@ -156,6 +158,7 @@ play_controller::play_controller(const config& level, saved_game& state_of_game,
resources::teams = &gamestate_.board_.teams_;
resources::tod_manager = &gamestate_.tod_manager_;
resources::undo_stack = undo_stack_.get();
resources::recorder = replay_.get();
resources::units = &gamestate_.board_.units_;
resources::filter_con = &gamestate_;

Expand Down Expand Up @@ -364,7 +367,7 @@ void play_controller::maybe_do_init_side()
return;
}

recorder.init_side();
resources::recorder->init_side();
do_init_side();
}

Expand Down
3 changes: 2 additions & 1 deletion src/play_controller.hpp
Expand Up @@ -35,7 +35,7 @@ class game_data;
class team;
class unit;
class wmi_pager;

class replay;
class saved_game;
struct mp_game_settings;
class game_classification;
Expand Down Expand Up @@ -273,6 +273,7 @@ class play_controller : public controller_base, public events::observer, public
/// undo_list can be an incomplete type at this point (which reduces the
/// number of files that depend on actions/undo.hpp).
boost::scoped_ptr<actions::undo_list> undo_stack_;
boost::scoped_ptr<replay> replay_;

//if a team is specified whose turn it is, it means we're loading a game
//instead of starting a fresh one. Gets reset to false after init_side
Expand Down

0 comments on commit bb0ecd1

Please sign in to comment.