Skip to content

Commit

Permalink
play_human_turn signals rather than throwing
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeck88 committed Jun 8, 2014
1 parent 154f3db commit 13f5b27
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 34 deletions.
20 changes: 13 additions & 7 deletions src/playmp_controller.cpp
Expand Up @@ -162,13 +162,13 @@ void playmp_controller::think_about_countdown(int ticks) {
}
}

void playmp_controller::play_human_turn(){
possible_end_play_signal playmp_controller::play_human_turn(){
LOG_NG << "playmp::play_human_turn...\n";

remove_blindfold();
int cur_ticks = SDL_GetTicks();
show_turn_dialog();
execute_gotos();
HANDLE_END_PLAY_SIGNAL( execute_gotos() );

if (!linger_ || is_host()) {
end_turn_enable(true);
Expand All @@ -178,7 +178,8 @@ void playmp_controller::play_human_turn(){
config cfg;

if(network_reader_.read(cfg)) {
turn_info::PROCESS_DATA_RESULT res = turn_data_.process_network_data(cfg, skip_replay_);
turn_info::PROCESS_DATA_RESULT res;
HANDLE_END_PLAY_SIGNAL( res = turn_data_.process_network_data(cfg, skip_replay_) );
//PROCESS_RESTART_TURN_TEMPORARY_LOCAL should be impossible because that's means the currently active side (that's us) left.
if (res == turn_info::PROCESS_RESTART_TURN || res == turn_info::PROCESS_RESTART_TURN_TEMPORARY_LOCAL)
{
Expand All @@ -199,7 +200,10 @@ void playmp_controller::play_human_turn(){

while( undo_stack_->can_undo() )
undo_stack_->undo();
throw end_turn_exception(gui_->playing_side());

end_turn_struct ets = {gui_->playing_side()};
return possible_end_play_signal(ets);
//throw end_turn_exception(gui_->playing_side());
}
else if(res == turn_info::PROCESS_END_LINGER)
{
Expand All @@ -213,8 +217,8 @@ void playmp_controller::play_human_turn(){
}
}

play_slice();
check_end_level();
HANDLE_END_PLAY_SIGNAL( play_slice() );
HANDLE_END_PLAY_SIGNAL( check_end_level() );

if (!linger_ && (current_team().countdown_time() > 0) && gamestate_.mp_settings().mp_countdown) {
SDL_Delay(1);
Expand All @@ -241,12 +245,14 @@ void playmp_controller::play_human_turn(){
current_team().set_countdown_time(10);
}

throw end_turn_exception();
return possible_end_play_signal(end_turn_exception().to_struct());
//throw end_turn_exception();
}
}

gui_->draw();
}
return boost::none;
}

void playmp_controller::play_idle_loop()
Expand Down
2 changes: 1 addition & 1 deletion src/playmp_controller.hpp
Expand Up @@ -50,7 +50,7 @@ class playmp_controller : public playsingle_controller, public events::pump_moni

virtual possible_end_play_signal play_side();
virtual void before_human_turn();
virtual void play_human_turn();
virtual possible_end_play_signal play_human_turn();
virtual void after_human_turn();
virtual void finish_side_turn();
virtual possible_end_play_signal play_network_turn();
Expand Down
65 changes: 40 additions & 25 deletions src/playsingle_controller.cpp
Expand Up @@ -723,31 +723,44 @@ possible_end_play_signal playsingle_controller::play_side()
if(current_team().is_human() || temporary_human) {
LOG_NG << "is human...\n";
temporary_human = false;
try{
// If a side is dead end the turn, but play at least side=1's
// turn in case all sides are dead
if (side_units(player_number_) != 0
|| (resources::units->size() == 0 && player_number_ == 1))
{
before_human_turn();
play_human_turn();
// If a side is dead end the turn, but play at least side=1's
// turn in case all sides are dead
if (side_units(player_number_) != 0
|| (resources::units->size() == 0 && player_number_ == 1))
{
possible_end_play_signal signal;
try {
before_human_turn(); //This line throws! the ai manager "raise" line throws exception from here: https://github.com/wesnoth/wesnoth/blob/ac96a2b91b3276e20b682210617cf87d1e0d366a/src/playsingle_controller.cpp#L954
signal = play_human_turn();
} catch (end_level_exception & e) {
signal = e.to_struct();
} catch (end_turn_exception & e) {
signal = e.to_struct();
}
} catch(end_turn_exception& end_turn) {
if (int(end_turn.redo) == player_number_) {
player_type_changed_ = true;
// If new controller is not human,
// reset gui to prev human one
if (!gameboard_.teams_[player_number_-1].is_human()) {
browse_ = true;
int s = find_human_team_before_current_player();
if (s <= 0)
s = gui_->playing_side();
update_gui_to_player(s-1);


if (signal) {
switch (boost::apply_visitor(get_signal_type(), *signal)) {
case END_LEVEL:
return signal;
case END_TURN:
if (int(boost::apply_visitor(get_redo(),*signal)) == player_number_) {
player_type_changed_ = true;
// If new controller is not human,
// reset gui to prev human one
if (!gameboard_.teams_[player_number_-1].is_human()) {
browse_ = true;
int s = find_human_team_before_current_player();
if (s <= 0)
s = gui_->playing_side();
update_gui_to_player(s-1);
}
}

}
}
} catch (end_level_exception & e) {
return possible_end_play_signal(e.to_struct());
}

// Ending the turn commits all moves.
undo_stack_->clear();
if ( !player_type_changed_ )
Expand Down Expand Up @@ -842,16 +855,18 @@ void playsingle_controller::execute_gotos(){
menu_handler_.execute_gotos(mouse_handler_, player_number_);
}

void playsingle_controller::play_human_turn() {
possible_end_play_signal playsingle_controller::play_human_turn() {
show_turn_dialog();
execute_gotos();
HANDLE_END_PLAY_SIGNAL( execute_gotos() );

end_turn_enable(true);
while(!end_turn_) {
play_slice();
check_end_level();
HANDLE_END_PLAY_SIGNAL( play_slice() );
HANDLE_END_PLAY_SIGNAL( check_end_level() );
gui_->draw();
}

return boost::none;
}

void playsingle_controller::linger()
Expand Down
2 changes: 1 addition & 1 deletion src/playsingle_controller.hpp
Expand Up @@ -96,7 +96,7 @@ class playsingle_controller : public play_controller
virtual void before_human_turn();
void show_turn_dialog();
void execute_gotos();
virtual void play_human_turn();
virtual possible_end_play_signal play_human_turn();
virtual void after_human_turn();
void end_turn_enable(bool enable);
virtual hotkey::ACTION_STATE get_action_state(hotkey::HOTKEY_COMMAND command, int index) const;
Expand Down

0 comments on commit 13f5b27

Please sign in to comment.