Skip to content

Commit

Permalink
Make is possible to switch from replay directly into normal play
Browse files Browse the repository at this point in the history
http://gna.org/bugs/?23833

I removed the replay_controller class, instead the normal replay also
uses the mp_replay_controller class which is a 'plugin' to
playsingle_controller which can just be deactivated to switch into
normal gameplay.
  • Loading branch information
gfgtdf committed Sep 30, 2015
1 parent 2c90513 commit 9ca6678
Show file tree
Hide file tree
Showing 21 changed files with 206 additions and 837 deletions.
20 changes: 15 additions & 5 deletions src/game_initialization/playcampaign.cpp
Expand Up @@ -33,7 +33,6 @@
#include "gui/widgets/window.hpp"
#include "persist_manager.hpp"
#include "playmp_controller.hpp"
#include "replay_controller.hpp"
#include "log.hpp"
#include "map_exception.hpp"
#include "mp_game_utils.hpp"
Expand Down Expand Up @@ -141,10 +140,21 @@ LEVEL_RESULT play_replay(display& disp, saved_game& gamestate, const config& gam
gamestate.classification().label = starting_pos["name"].str();
//if (gamestate.abbrev.empty())
// gamestate.abbrev = (*scenario)["abbrev"];

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

return res;
{
#if 0
replay_controller rc(gamestate.get_replay_starting_pos(), gamestate, SDL_GetTicks(), game_config, tdata, disp.video(), is_unit_test);
//replay event-loop
rc.main_loop();
#else

playsingle_controller playcontroller(gamestate.get_replay_starting_pos(), gamestate, SDL_GetTicks(), game_config, tdata, disp.video(), false);
LOG_NG << "created objects... " << (SDL_GetTicks() - playcontroller.get_ticks()) << "\n";
playcontroller.enable_replay(is_unit_test);
playcontroller.play_scenario(gamestate.get_replay_starting_pos().child_range("story"), gamestate.get_replay_starting_pos());
#endif

}
return LEVEL_RESULT::VICTORY;
} catch(game::load_game_failed& e) {
ERR_NG << std::string(_("The game could not be loaded: ")) + " (game::load_game_failed) " + e.message << std::endl;
if (is_unit_test) {
Expand Down
3 changes: 3 additions & 0 deletions src/hotkey/command_executor.cpp
Expand Up @@ -231,6 +231,9 @@ bool command_executor::execute_command(const hotkey_command& cmd, int /*index*/
case HOTKEY_REPLAY_SKIP_ANIMATION:
replay_skip_animation();
break;
case HOTKEY_REPLAY_EXIT:
replay_exit();
break;
case HOTKEY_WB_TOGGLE:
whiteboard_toggle();
break;
Expand Down
1 change: 1 addition & 0 deletions src/hotkey/command_executor.hpp
Expand Up @@ -90,6 +90,7 @@ class command_executor
virtual void replay_show_each() {}
virtual void replay_show_team1() {}
virtual void replay_skip_animation() {}
virtual void replay_exit() {}
virtual void whiteboard_toggle() {}
virtual void whiteboard_execute_action() {}
virtual void whiteboard_execute_all_actions() {}
Expand Down
1 change: 1 addition & 0 deletions src/hotkey/hotkey_command.cpp
Expand Up @@ -105,6 +105,7 @@ hotkey::hotkey_command_temp hotkey_list_[] = {
{ hotkey::HOTKEY_REPLAY_SHOW_EACH, "replayshoweach", N_("Each Team"), false, scope_game, "" },
{ hotkey::HOTKEY_REPLAY_SHOW_TEAM1, "replayshowteam1", N_("Human Team"), false, scope_game, "" },
{ hotkey::HOTKEY_REPLAY_SKIP_ANIMATION, "replayskipanimation", N_("Skip Animation"), false, scope_game, "" },
{ hotkey::HOTKEY_REPLAY_EXIT, "replayexit", N_("End Replay"), false, scope_game, "" },
// Whiteboard commands
// TRANSLATORS: whiteboard menu entry: toggle planning mode
{ hotkey::HOTKEY_WB_TOGGLE, "wbtoggle", N_("whiteboard^Planning Mode"), false, scope_game, "" },
Expand Down
1 change: 1 addition & 0 deletions src/hotkey/hotkey_command.hpp
Expand Up @@ -59,6 +59,7 @@ enum HOTKEY_COMMAND {
HOTKEY_REPLAY_NEXT_SIDE, HOTKEY_REPLAY_NEXT_MOVE, HOTKEY_REPLAY_SHOW_EVERYTHING,
HOTKEY_REPLAY_SHOW_EACH, HOTKEY_REPLAY_SHOW_TEAM1,
HOTKEY_REPLAY_SKIP_ANIMATION,
HOTKEY_REPLAY_EXIT,

// Controls
HOTKEY_SELECT_HEX, HOTKEY_DESELECT_HEX,
Expand Down
72 changes: 0 additions & 72 deletions src/hotkey_handler_replay.cpp
@@ -1,72 +0,0 @@
/*
Copyright (C) 2014 - 2015 by Chris Beck <render787@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

#include "hotkey_handler_replay.hpp"

#include "hotkey/hotkey_command.hpp"
#include "hotkey/hotkey_item.hpp"
#include "network.hpp" //for nconnections (to determine if we are in a networked game)
#include "play_controller.hpp"
#include "replay_controller.hpp"

replay_controller::hotkey_handler::hotkey_handler(replay_controller & pc, saved_game & sg)
: play_controller::hotkey_handler(pc, sg)
, replay_controller_(pc)
{}

replay_controller::hotkey_handler::~hotkey_handler(){}

void replay_controller::hotkey_handler::preferences(){
play_controller::hotkey_handler::preferences();
replay_controller_.update_gui(); //todo: why is this needed?
}

void replay_controller::hotkey_handler::show_statistics(){
menu_handler_.show_statistics(gui()->playing_team()+1); //playing team instead of viewing team
}

bool replay_controller::hotkey_handler::can_execute_command(const hotkey::hotkey_command& cmd, int index) const
{
hotkey::HOTKEY_COMMAND command = cmd.id;
bool result = play_controller::hotkey_handler::can_execute_command(cmd,index);

switch(command) {

//commands we can always do
case hotkey::HOTKEY_REPLAY_SHOW_EVERYTHING:
case hotkey::HOTKEY_REPLAY_SHOW_EACH:
case hotkey::HOTKEY_REPLAY_SHOW_TEAM1:
case hotkey::HOTKEY_REPLAY_SKIP_ANIMATION:
case hotkey::HOTKEY_SAVE_GAME:
case hotkey::HOTKEY_SAVE_REPLAY:
case hotkey::HOTKEY_LABEL_SETTINGS:
case hotkey::HOTKEY_CHAT_LOG:
return true;

case hotkey::HOTKEY_REPLAY_RESET:
return events::commands_disabled <= 1;

//commands we only can do before the end of the replay
case hotkey::HOTKEY_REPLAY_STOP:
return !replay_controller_.recorder_at_end();
case hotkey::HOTKEY_REPLAY_PLAY:
case hotkey::HOTKEY_REPLAY_NEXT_TURN:
case hotkey::HOTKEY_REPLAY_NEXT_SIDE:
case hotkey::HOTKEY_REPLAY_NEXT_MOVE:
//we have one events_disabler when starting the replay_controller and a second when entering the synced context.
return replay_controller_.should_stop() && (events::commands_disabled <= 1 ) && !replay_controller_.recorder_at_end();
default:
return result;
}
}
62 changes: 0 additions & 62 deletions src/hotkey_handler_replay.hpp
@@ -1,62 +0,0 @@
/*
Copyright (C) 2014 - 2015 by Chris Beck <render787@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

/**
* @file
* An extension of play_controller::hotkey_handler, which has support for
* replay controller features
*/

#ifndef HOTKEY_HANDLER_REPLAY_HPP_INCL_
#define HOTKEY_HANDLER_REPLAY_HPP_INCL_

#include "replay_controller.hpp"
#include "hotkey_handler.hpp"
#include "global.hpp"

class replay_controller::hotkey_handler : public play_controller::hotkey_handler {

protected:
replay_controller & replay_controller_;

public:
hotkey_handler(replay_controller &, saved_game &);
~hotkey_handler();

virtual void preferences();
virtual void show_statistics();
virtual bool can_execute_command(const hotkey::hotkey_command& command, int index=-1) const;

virtual void reset_replay() OVERRIDE
{ return replay_controller_.reset_replay(); }
virtual void stop_replay() OVERRIDE
{ return replay_controller_.stop_replay(); }
virtual void play_replay() OVERRIDE
{ return replay_controller_.play_replay(); }
virtual void replay_next_turn() OVERRIDE
{ return replay_controller_.replay_next_turn(); }
virtual void replay_next_side() OVERRIDE
{ return replay_controller_.replay_next_side(); }
virtual void replay_next_move() OVERRIDE
{ return replay_controller_.replay_next_move(); }
virtual void replay_show_everything() OVERRIDE
{ return replay_controller_.replay_show_everything(); }
virtual void replay_show_each() OVERRIDE
{ return replay_controller_.replay_show_each(); }
virtual void replay_show_team1() OVERRIDE
{ return replay_controller_.replay_show_team1(); }

};

#endif
15 changes: 15 additions & 0 deletions src/hotkey_handler_sp.cpp
Expand Up @@ -26,6 +26,8 @@
#include "network.hpp"
#include "save_index.hpp"
#include "gui/dialogs/message.hpp"
#include "resources.hpp"
#include "replay.hpp"

#include "unit.hpp"

Expand Down Expand Up @@ -282,7 +284,13 @@ bool playsingle_controller::hotkey_handler::can_execute_command(const hotkey::ho
case hotkey::HOTKEY_REPLAY_NEXT_SIDE:
case hotkey::HOTKEY_REPLAY_NEXT_MOVE:
case hotkey::HOTKEY_REPLAY_SKIP_ANIMATION:
case hotkey::HOTKEY_REPLAY_SHOW_EVERYTHING:
case hotkey::HOTKEY_REPLAY_SHOW_EACH:
case hotkey::HOTKEY_REPLAY_SHOW_TEAM1:
case hotkey::HOTKEY_REPLAY_RESET:
return playsingle_controller_.get_replay_controller() && playsingle_controller_.get_replay_controller()->can_execute_command(cmd, index);
case hotkey::HOTKEY_REPLAY_EXIT:
return playsingle_controller_.get_replay_controller() != NULL;
default: return play_controller::hotkey_handler::can_execute_command(cmd, index);
}
return res;
Expand Down Expand Up @@ -311,3 +319,10 @@ void playsingle_controller::hotkey_handler::load_autosave(const std::string& fil
}
}

void playsingle_controller::hotkey_handler::replay_exit()
{
if(network::nconnections() == 0) {
resources::recorder->delete_upcoming_commands();
}
playsingle_controller_.set_player_type_changed();
}
10 changes: 9 additions & 1 deletion src/hotkey_handler_sp.hpp
Expand Up @@ -87,7 +87,15 @@ class playsingle_controller::hotkey_handler : public play_controller::hotkey_han
{ return get_replay_controller().replay_next_side(); }
virtual void replay_next_move() OVERRIDE
{ return get_replay_controller().replay_next_move(); }

virtual void replay_show_everything() OVERRIDE
{ return get_replay_controller().replay_show_everything(); }
virtual void replay_show_each() OVERRIDE
{ return get_replay_controller().replay_show_each(); }
virtual void replay_show_team1() OVERRIDE
{ return get_replay_controller().replay_show_team1(); }
virtual void reset_replay() OVERRIDE
{ return playsingle_controller_.reset_replay(); }
virtual void replay_exit() OVERRIDE;
virtual void load_autosave(const std::string& filename);
virtual hotkey::ACTION_STATE get_action_state(hotkey::HOTKEY_COMMAND command, int index) const;
};
Expand Down
77 changes: 65 additions & 12 deletions src/mp_replay_controller.cpp
Expand Up @@ -109,17 +109,27 @@ namespace
}
}

mp_replay_controller::mp_replay_controller(play_controller& controller)
mp_replay_controller::mp_replay_controller(play_controller& controller, bool control_view, const boost::shared_ptr<config>& reset_state, const boost::function<void()>& on_end_replay)
: controller_(controller)
, stop_condition_(new replay_stop_condition())
, disabler_()
, vision_()
, reset_state_(reset_state)
, on_end_replay_(on_end_replay)
, return_to_play_side_(false)
{
if(control_view) {
vision_ = HUMAN_TEAM;
}
controller_.get_display().get_theme().theme_reset_event().attach_handler(this);
controller_.get_display().redraw_everything();
//add_replay_theme();
}
mp_replay_controller::~mp_replay_controller()
{
if(controller_.is_skipping_replay()) {
controller_.toggle_skipping_replay();
}
controller_.get_display().get_theme().theme_reset_event().detach_handler(this);
controller_.get_display().redraw_everything();
//remove_replay_theme();
Expand Down Expand Up @@ -337,23 +347,29 @@ bool mp_replay_controller::recorder_at_end() const
return resources::recorder->at_end();
}


#include "playsingle_controller.hpp"
REPLAY_RETURN mp_replay_controller::play_side_impl()
{
stop_condition_->new_side_turn(controller_.current_side(), controller_.gamestate().tod_manager_.turn());
while(true)
while(!return_to_play_side_ && !static_cast<playsingle_controller&>(controller_).get_player_type_changed())
{
if(!stop_condition_->should_stop())
{
REPLAY_RETURN res = do_replay(true);
if(res == REPLAY_FOUND_END_MOVE) {
stop_condition_->move_done();
if(resources::recorder->at_end()) {
//Gather more replay data
on_end_replay_();
}
if(res == REPLAY_FOUND_END_TURN) {
return res;
}
if(res == REPLAY_RETURN_AT_END) {
return res;
else {
REPLAY_RETURN res = do_replay(true);
if(res == REPLAY_FOUND_END_MOVE) {
stop_condition_->move_done();
}
if(res == REPLAY_FOUND_END_TURN) {
return res;
}
if(res == REPLAY_RETURN_AT_END) {
new replay_stop_condition();
}
}
controller_.play_slice(false);
}
Expand All @@ -362,8 +378,8 @@ REPLAY_RETURN mp_replay_controller::play_side_impl()
controller_.play_slice(true);
replay_ui_playback_should_stop();
}

}
return REPLAY_FOUND_END_MOVE;
}
bool mp_replay_controller::can_execute_command(const hotkey::hotkey_command& cmd, int) const
{
Expand All @@ -372,6 +388,10 @@ bool mp_replay_controller::can_execute_command(const hotkey::hotkey_command& cmd
switch(command) {
case hotkey::HOTKEY_REPLAY_SKIP_ANIMATION:
return true;
case hotkey::HOTKEY_REPLAY_SHOW_EVERYTHING:
case hotkey::HOTKEY_REPLAY_SHOW_EACH:
case hotkey::HOTKEY_REPLAY_SHOW_TEAM1:
return is_controlling_view();
//commands we only can do before the end of the replay
case hotkey::HOTKEY_REPLAY_STOP:
return !recorder_at_end();
Expand All @@ -381,8 +401,41 @@ bool mp_replay_controller::can_execute_command(const hotkey::hotkey_command& cmd
case hotkey::HOTKEY_REPLAY_NEXT_MOVE:
//we have one events_disabler when starting the replay_controller and a second when entering the synced context.
return should_stop() && (events::commands_disabled <= 1 ) && !recorder_at_end();
case hotkey::HOTKEY_REPLAY_RESET:
return allow_reset_replay() && events::commands_disabled <= 1;
default:
assert(false);
return false;
}
}

void mp_replay_controller::replay_show_everything()
{
vision_ = SHOW_ALL;
update_teams();
}

void mp_replay_controller::replay_show_each()
{
vision_ = CURRENT_TEAM;
update_teams();
}

void mp_replay_controller::replay_show_team1()
{
vision_ = HUMAN_TEAM;
update_teams();
}

void mp_replay_controller::update_teams()
{
update_viewing_player();
controller_.get_display().invalidate_all();
update_gui();
}

void mp_replay_controller::update_viewing_player()
{
assert(vision_);
controller_.update_gui_to_player(vision_ == HUMAN_TEAM ? controller_.gamestate().first_human_team_ : controller_.current_side() - 1, *vision_ == SHOW_ALL);
}

0 comments on commit 9ca6678

Please sign in to comment.