Skip to content

Commit

Permalink
move function play_turn from playsingle_controller to play_controller
Browse files Browse the repository at this point in the history
the intention is that we can use those function in replay_controller.cpp
  • Loading branch information
gfgtdf committed Sep 9, 2015
1 parent 11daa51 commit c8f9b2b
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 82 deletions.
85 changes: 82 additions & 3 deletions src/play_controller.cpp
Expand Up @@ -329,13 +329,13 @@ void play_controller::init_gui()
gui_->update_tod();
}

void play_controller::init_side_begin(bool is_replay)
void play_controller::init_side_begin(bool)
{
mouse_handler_.set_side(player_number_);

// If we are observers we move to watch next team if it is allowed
if ((is_observer() && !current_team().get_disallow_observers())
|| (current_team().is_local_human() && !is_replay))
|| (current_team().is_local_human() && !this->is_replay()))
{
update_gui_to_player(player_number_ - 1);
}
Expand All @@ -355,7 +355,7 @@ void play_controller::maybe_do_init_side()
* For all other sides it is recorded in replay and replay handler has to handle
* calling do_init_side() functions.
**/
if (init_side_done_ || !current_team().is_local() || gamestate_.gamedata_.phase() != game_data::PLAY) {
if (init_side_done_ || !current_team().is_local() || gamestate_.gamedata_.phase() != game_data::PLAY || is_replay()) {
return;
}

Expand Down Expand Up @@ -1094,3 +1094,82 @@ void play_controller::play_side()
// has changed mid-turn
sync_end_turn();
}


void play_controller::play_turn()
{
whiteboard_manager_->on_gamestate_change();
gui_->new_turn();
gui_->invalidate_game_status();
events::raise_draw_event();

LOG_NG << "turn: " << turn() << "\n";

if(non_interactive()) {
LOG_AIT << "Turn " << turn() << ":" << std::endl;
}

for (; player_number_ <= int(gamestate_.board_.teams().size()); ++player_number_)
{
// If a side is empty skip over it.
if (current_team().is_empty()) {
continue;
}
init_side_begin(false);
if(init_side_done_) {
//This is the case in a reloaded game where teh side was initilizes before saving the game.
init_side_end();
}

ai_testing::log_turn_start(player_number_);
play_side();
if(is_regular_game_end()) {
return;
}
finish_side_turn();
if(is_regular_game_end()) {
return;
}
if(non_interactive()) {
LOG_AIT << " Player " << player_number_ << ": " <<
current_team().villages().size() << " Villages" <<
std::endl;
ai_testing::log_turn_end(player_number_);
}
}
//If the loop exits due to the last team having been processed,
player_number_ = gamestate_.board_.teams().size();

finish_turn();

// Time has run out
check_time_over();
}

void play_controller::check_time_over(){
bool time_left = gamestate_.tod_manager_.next_turn(gamestate_.gamedata_);
if(!time_left) {
LOG_NG << "firing time over event...\n";
set_scontext_synced_base sync;
pump().fire("time over");
LOG_NG << "done firing time over event...\n";
//if turns are added while handling 'time over' event
if (gamestate_.tod_manager_.is_time_left()) {
return;
}

if(non_interactive()) {
LOG_AIT << "time over (draw)\n";
ai_testing::log_draw();
}

check_victory();
if (is_regular_game_end()) {
return;
}
end_level_data e;
e.proceed_to_next_level = false;
e.is_victory = false;
set_end_level_data(e);
}
}
2 changes: 2 additions & 0 deletions src/play_controller.hpp
Expand Up @@ -315,6 +315,8 @@ class play_controller : public controller_base, public events::observer, public
bool player_type_changed_;

virtual void sync_end_turn() {};
virtual void check_time_over();
void play_turn();
};


Expand Down
78 changes: 1 addition & 77 deletions src/playsingle_controller.cpp
Expand Up @@ -366,57 +366,6 @@ LEVEL_RESULT playsingle_controller::play_scenario(
return LEVEL_RESULT::QUIT;
}

void playsingle_controller::play_turn()
{
whiteboard_manager_->on_gamestate_change();
gui_->new_turn();
gui_->invalidate_game_status();
events::raise_draw_event();

LOG_NG << "turn: " << turn() << "\n";

if(non_interactive()) {
LOG_AIT << "Turn " << turn() << ":" << std::endl;
}

for (; player_number_ <= int(gamestate_.board_.teams().size()); ++player_number_)
{
// If a side is empty skip over it.
if (current_team().is_empty()) {
continue;
}
init_side_begin(false);
if(init_side_done_) {
//This is the case in a reloaded game where teh side was initilizes before saving the game.
init_side_end();
}

ai_testing::log_turn_start(player_number_);
play_side();
if(is_regular_game_end()) {
return;
}
finish_side_turn();
turn_data_.send_data();
if(is_regular_game_end()) {
return;
}
if(non_interactive()) {
LOG_AIT << " Player " << player_number_ << ": " <<
current_team().villages().size() << " Villages" <<
std::endl;
ai_testing::log_turn_end(player_number_);
}
}
//If the loop exits due to the last team having been processed,
player_number_ = gamestate_.board_.teams().size();

finish_turn();

// Time has run out
check_time_over();
}

void playsingle_controller::play_idle_loop()
{
while(!should_return_to_play_side()) {
Expand Down Expand Up @@ -659,33 +608,7 @@ void playsingle_controller::handle_generic_event(const std::string& name){
}
}

void playsingle_controller::check_time_over(){
bool time_left = gamestate_.tod_manager_.next_turn(gamestate_.gamedata_);
if(!time_left) {
LOG_NG << "firing time over event...\n";
set_scontext_synced_base sync;
pump().fire("time over");
LOG_NG << "done firing time over event...\n";
//if turns are added while handling 'time over' event
if (gamestate_.tod_manager_.is_time_left()) {
return;
}

if(non_interactive()) {
LOG_AIT << "time over (draw)\n";
ai_testing::log_draw();
}

check_victory();
if (is_regular_game_end()) {
return;
}
end_level_data e;
e.proceed_to_next_level = false;
e.is_victory = false;
set_end_level_data(e);
}
}

void playsingle_controller::end_turn(){
if (linger_)
Expand Down Expand Up @@ -731,6 +654,7 @@ void playsingle_controller::sync_end_turn()
assert(synced_context::synced_state() == synced_context::UNSYNCED);
if(end_turn_ == END_TURN_REQUIRED && current_team().is_local())
{
//TODO: we shodul also send this immideateley.
resources::recorder->end_turn();
end_turn_ = END_TURN_SYNCED;
}
Expand Down
2 changes: 0 additions & 2 deletions src/playsingle_controller.hpp
Expand Up @@ -56,7 +56,6 @@ class playsingle_controller : public play_controller
virtual bool should_return_to_play_side()
{ return player_type_changed_ || end_turn_ != END_TURN_NONE || is_regular_game_end(); }
protected:
void play_turn();
virtual void play_side_impl();
void before_human_turn();
void show_turn_dialog();
Expand All @@ -69,7 +68,6 @@ class playsingle_controller : public play_controller
virtual void do_idle_notification();
virtual void play_network_turn();
virtual void init_gui();
void check_time_over();
void store_recalls();
void store_gold(bool obs = false);

Expand Down
1 change: 1 addition & 0 deletions src/replay.cpp
Expand Up @@ -962,6 +962,7 @@ static std::map<int, config> get_user_choice_internal(const std::string &name, c

//send data to others.
//but if there wasn't any data sended during this turn, we don't want to bein wth that now.
//TODO: we should send user choices during nonundoable actions immideatley.
if(synced_context::is_simultaneously() || current_side != local_side)
{
synced_context::send_user_choice();
Expand Down

0 comments on commit c8f9b2b

Please sign in to comment.