Skip to content

Commit

Permalink
refactor play_side
Browse files Browse the repository at this point in the history
  • Loading branch information
gfgtdf committed Mar 2, 2015
1 parent f8a2514 commit e676f89
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 53 deletions.
45 changes: 33 additions & 12 deletions src/play_controller.cpp
Expand Up @@ -641,24 +641,45 @@ const team& play_controller::current_team() const
return gamestate_.board_.teams()[player_number_-1];
}

int play_controller::find_human_team_before_current_player() const
/// @returns: the number n in [min, min+mod ) so that (n - num) is a multiple of mod.
static int modulo(int num, int mod, int min)
{
if (player_number_ > int(gamestate_.board_.teams().size()))
return -2;
assert(mod > 0);
int n = (num - min) % mod;
if (n < 0)
n += mod;
//n is now in [0, mod)
n = n + min;
return n;
// the folowing properties are easy to verfy:
// 1) For all m: modulo(num, mod, min) == modulo(num + mod*m, mod, min)
// 2) For all 0 <= m < mod: modulo(min + m, mod, min) == min + m
}

for (int i = player_number_-2; i >= 0; --i) {
if (gamestate_.board_.teams()[i].is_local_human()) {
return i+1;
}
bool play_controller::is_team_visible(int team_num, bool observer) const
{
const team& t = gamestate_.board_.teams()[team_num - 1];
if(observer) {
return !t.get_disallow_observers();
}
else {
return t.is_local_human() && !t.is_idle();
}
}

int play_controller::find_last_visible_team() const
{
assert(player_number_ <= int(gamestate_.board_.teams().size()));
const int num_teams = gamestate_.board_.teams().size();
const bool is_observer = this->is_observer();

for (int i = gamestate_.board_.teams().size()-1; i > player_number_-1; --i) {
if (gamestate_.board_.teams()[i].is_local_human()) {
return i+1;
for(int i = 0; i < num_teams; i++) {
const int team_num = modulo(player_number_ - i, num_teams, 1);
if(is_team_visible(team_num, is_observer)) {
return team_num;
}
}

return -1;
return 0;
}

events::mouse_handler& play_controller::get_mouse_handler_base() {
Expand Down
5 changes: 3 additions & 2 deletions src/play_controller.hpp
Expand Up @@ -233,8 +233,9 @@ class play_controller : public controller_base, public events::observer, public
team& current_team();
const team& current_team() const;

/** Find a human team (ie one we own) starting backwards from current player. */
int find_human_team_before_current_player() const;
bool is_team_visible(int team_num, bool observer) const;
/// returns 0 if no such team was found.
int find_last_visible_team() const;

//gamestate
game_state gamestate_;
Expand Down
49 changes: 10 additions & 39 deletions src/playsingle_controller.cpp
Expand Up @@ -539,6 +539,12 @@ possible_end_play_signal playsingle_controller::play_side()
//flag used when we fallback from ai and give temporarily control to human
bool temporary_human = false;
do {
//Update viewing team in case it has changed during the loop.
if(int side_num = play_controller::find_last_visible_team()) {
if(side_num != this->gui_->viewing_side()) {
update_gui_to_player(side_num - 1);
}
}
// This flag can be set by derived classes (in overridden functions).
player_type_changed_ = false;
if (!skip_next_turn_)
Expand All @@ -554,23 +560,10 @@ possible_end_play_signal playsingle_controller::play_side()
if (gamestate_.board_.side_units(player_number_) != 0
|| (gamestate_.board_.units().size() == 0 && player_number_ == 1))
{
possible_end_play_signal signal;
before_human_turn();
if (!end_turn_) {
signal = play_human_turn();
}
if (signal) {
return signal;
}
if (player_type_changed_) {
// If new controller is not human,
// reset gui to prev human one
if (!gamestate_.board_.teams()[player_number_-1].is_local_human()) {
int s = find_human_team_before_current_player();
if (s <= 0) {
s = gui_->playing_side();
}
update_gui_to_player(s-1);
if(possible_end_play_signal signal = play_idle_loop()) {
return signal;
}
}
}
Expand Down Expand Up @@ -599,34 +592,12 @@ possible_end_play_signal playsingle_controller::play_side()
} else if(current_team().is_local_human() && current_team().is_idle()) {
end_turn_enable(false);
do_idle_notification();

possible_end_play_signal signal;
before_human_turn();

if (!end_turn_) {
signal = play_idle_loop();
}

if (signal) {
return signal;
}
if (player_type_changed_) {
// If new controller is not human,
// reset gui to prev human one
if (!gamestate_.board_.teams()[player_number_-1].is_local_human()) {
int s = find_human_team_before_current_player();
if (s <= 0) {
s = gui_->playing_side();
}
update_gui_to_player(s-1);
}
else {
//This side was previously not human controlled.
update_gui_to_player(player_number_ - 1);
if(possible_end_play_signal signal = play_idle_loop()) {
return signal;
}

}

}
else {
assert(current_team().is_empty()); // Do nothing.
Expand Down

0 comments on commit e676f89

Please sign in to comment.