From 4abc67c6692bc9a933be46516f3cb861aee9ce96 Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Thu, 7 Jan 2021 05:11:49 +1100 Subject: [PATCH] Play Controller: code formatting --- src/play_controller.cpp | 339 ++++++++++++++++++++++------------------ 1 file changed, 183 insertions(+), 156 deletions(-) diff --git a/src/play_controller.cpp b/src/play_controller.cpp index cd0b903487a7..6bbb627a49b5 100644 --- a/src/play_controller.cpp +++ b/src/play_controller.cpp @@ -27,25 +27,25 @@ #include "actions/vision.hpp" #include "ai/manager.hpp" #include "ai/testing.hpp" -#include "preferences/credentials.hpp" #include "display_chat_manager.hpp" #include "floating_label.hpp" #include "formula/string_utils.hpp" +#include "game_errors.hpp" #include "game_events/menu_item.hpp" #include "game_events/pump.hpp" -#include "preferences/game.hpp" #include "game_state.hpp" -#include "hotkey/hotkey_item.hpp" -#include "hotkey/hotkey_handler.hpp" -#include "map/label.hpp" -#include "game_errors.hpp" #include "gettext.hpp" #include "gui/dialogs/loading_screen.hpp" #include "gui/dialogs/transient_message.hpp" #include "hotkey/command_executor.hpp" +#include "hotkey/hotkey_handler.hpp" +#include "hotkey/hotkey_item.hpp" #include "log.hpp" +#include "map/label.hpp" #include "pathfind/teleport.hpp" +#include "preferences/credentials.hpp" #include "preferences/display.hpp" +#include "preferences/game.hpp" #include "random.hpp" #include "replay.hpp" #include "reports.hpp" @@ -61,16 +61,16 @@ #include "statistics.hpp" #include "synced_context.hpp" #include "tooltips.hpp" -#include "units/unit.hpp" -#include "units/types.hpp" #include "units/id.hpp" +#include "units/types.hpp" +#include "units/unit.hpp" #include "whiteboard/manager.hpp" #include static lg::log_domain log_aitesting("ai/testing"); #define LOG_AIT LOG_STREAM(info, log_aitesting) -//If necessary, this define can be replaced with `#define LOG_AIT std::cout` to restore previous behavior +// If necessary, this define can be replaced with `#define LOG_AIT std::cout` to restore previous behavior static lg::log_domain log_engine("engine"); #define LOG_NG LOG_STREAM(info, log_engine) @@ -92,28 +92,27 @@ static lg::log_domain log_engine_enemies("engine/enemies"); static void copy_persistent(const config& src, config& dst) { static const std::set attrs { - "description", - "name", - "victory_when_enemies_defeated", - "remove_from_carryover_on_defeat", - "disallow_recall", - "experience_modifier", - "require_scenario", - "loaded_resources" + "description", + "name", + "victory_when_enemies_defeated", + "remove_from_carryover_on_defeat", + "disallow_recall", + "experience_modifier", + "require_scenario", + "loaded_resources" }; static const std::set tags { - "terrain_graphics", - "modify_unit_type", - "lua"}; + "terrain_graphics", + "modify_unit_type", + "lua" + }; - for (const std::string& attr : attrs) - { + for(const std::string& attr : attrs) { dst[attr] = src[attr]; } - for (const std::string& tag : tags) - { + for(const std::string& tag : tags) { dst.append_children(src, tag); } } @@ -187,9 +186,10 @@ play_controller::play_controller(const config& level, saved_game& state_of_game, game_config::add_color_info(game_config_view::wrap(level)); hotkey::deactivate_all_scopes(); hotkey::set_scope_active(hotkey::SCOPE_GAME); + try { init(level); - } catch (...) { + } catch(...) { clear_resources(); throw; } @@ -247,11 +247,13 @@ void play_controller::init(const config& level) gui2::dialogs::loading_screen::progress(loading_stage::build_terrain); gui_.reset(new game_display(gamestate().board_, whiteboard_manager_, *gamestate().reports_, theme_cfg, level)); map_start_ = map_location(level.child_or_empty("display").child_or_empty("location")); - if (!gui_->video().faked()) { - if (saved_game_.mp_settings().mp_countdown) - gui_->get_theme().modify_label("time-icon", _ ("time left for current turn")); - else - gui_->get_theme().modify_label("time-icon", _ ("current local time")); + + if(!gui_->video().faked()) { + if(saved_game_.mp_settings().mp_countdown) { + gui_->get_theme().modify_label("time-icon", _("time left for current turn")); + } else { + gui_->get_theme().modify_label("time-icon", _("current local time")); + } } gui2::dialogs::loading_screen::progress(loading_stage::init_display); @@ -269,15 +271,12 @@ void play_controller::init(const config& level) if(gamestate().first_human_team_ != -1) { gui_->set_team(gamestate().first_human_team_); - } - else if(is_observer()) { + } else if(is_observer()) { // Find first team that is allowed to be observed. // If not set here observer would be without fog until // the first turn of observable side - for (const team& t : get_teams()) - { - if (!t.get_disallow_observers()) - { + for(const team& t : get_teams()) { + if(!t.get_disallow_observers()) { gui_->set_team(t.side() - 1); } } @@ -285,7 +284,7 @@ void play_controller::init(const config& level) init_managers(); gui2::dialogs::loading_screen::progress(loading_stage::start_game); - //loadscreen_manager->reset(); + // loadscreen_manager->reset(); gamestate().gamedata_.set_phase(game_data::PRELOAD); gamestate().lua_kernel_->load_game(level); @@ -295,7 +294,8 @@ void play_controller::init(const config& level) plugins_context_->set_callback("quit", throw_end_level(), false); plugins_context_->set_accessor_string("scenario_name", [this](config) { return get_scenario_name(); }); }); - //Do this after the loadingscreen, so that ita happens in the main thread. + + // Do this after the loadingscreen, so that ita happens in the main thread. gui_->join(); } @@ -317,6 +317,7 @@ void play_controller::reset_gamestate(const config& level, int replay_pos) all its member objects access the old manager instead of the new. */ gamestate_.reset(); gamestate_.reset(new game_state(level, *this)); + resources::gameboard = &gamestate().board_; resources::gamedata = &gamestate().gamedata_; resources::tod_manager = &gamestate().tod_manager_; @@ -369,6 +370,7 @@ void play_controller::fire_prestart() } pump().fire("prestart"); + // prestart event may modify start turn with WML, reflect any changes. gamestate().gamedata_.get_variable("turn_number") = static_cast(turn()); } @@ -377,7 +379,7 @@ void play_controller::refresh_objectives() const { const config cfg("side", gui_->viewing_side()); gamestate().lua_kernel_->run_wml_action("show_objectives", vconfig(cfg), - game_events::queued_event("_from_interface", "", map_location(), map_location(), config())); + game_events::queued_event("_from_interface", "", map_location(), map_location(), config())); } void play_controller::fire_start() @@ -392,12 +394,13 @@ void play_controller::fire_start() refresh_objectives(); check_objectives(); + // prestart and start events may modify the initial gold amount, // reflect any changes. - for (team& tm : get_teams()) - { + for(team& tm : get_teams()) { tm.set_start_gold(tm.gold()); } + gamestate_->init_side_done() = false; gamestate().gamedata_.set_phase(game_data::PLAY); } @@ -413,9 +416,7 @@ void play_controller::init_side_begin() mouse_handler_.set_side(current_side()); // 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() && !this->is_replay())) - { + if((is_observer() && !current_team().get_disallow_observers()) || (current_team().is_local_human() && !this->is_replay())) { update_gui_to_player(current_side() - 1); } @@ -431,24 +432,25 @@ 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 (gamestate_->init_side_done()) { + if(gamestate_->init_side_done()) { // We already executed do_init_side this can for example happe if we reload a game, // but also if we changed control of a side during it's turn return; } - if (!current_team().is_local()) { + + if(!current_team().is_local()) { // We are in a mp game and execute do_init_side as soon as we receive [init_side] from the current player // (see replay.cpp) return; } - if (is_replay()) { + if(is_replay()) { // We are in a replay and execute do_init_side as soon as we reach the next [init_side] in the replay data // (see replay.cpp) return; } - if (current_team().is_idle()) { + if(current_team().is_idle()) { // In this case it can happen that we just gave control of this side to another player so doing init_side // could lead to errors since we no longer own this side from the servers perspective. // (see playturn.cpp) @@ -474,8 +476,7 @@ void play_controller::do_init_side() gamestate().gamedata_.get_variable("side_number") = current_side(); // We might have skipped some sides because they were empty so it is not enough to check for side_num==1 - if(!gamestate().tod_manager_.has_turn_event_fired()) - { + if(!gamestate().tod_manager_.has_turn_event_fired()) { pump().fire("turn_" + turn_num); pump().fire("new_turn"); gamestate().tod_manager_.turn_event_fired(); @@ -490,30 +491,32 @@ void play_controller::do_init_side() // and the player should get income now. // Healing/income happen if it's not the first turn of processing, // or if we are loading a game. - if (turn() > 1) { + if(turn() > 1) { gamestate().board_.new_turn(current_side()); current_team().new_turn(); // If the expense is less than the number of villages owned // times the village support capacity, // then we don't have to pay anything at all - int expense = gamestate().board_.side_upkeep(current_side()) - - current_team().support(); + int expense = gamestate().board_.side_upkeep(current_side()) - current_team().support(); if(expense > 0) { current_team().spend_gold(expense); } } - if (do_healing()) { + + if(do_healing()) { calculate_healing(current_side(), !is_skipping_replay()); } + // Do healing on every side turn except the very first side turn. // (1.14 and earlier did healing whenever turn >= 2.) set_do_healing(true); + // Set resting now after the healing has been done. - for(unit &patient : resources::gameboard->units()) { - if(patient.side() == current_side()) { - patient.set_resting(true); - } + for(unit& patient : resources::gameboard->units()) { + if(patient.side() == current_side()) { + patient.set_resting(true); + } } // Prepare the undo stack. @@ -526,16 +529,19 @@ void play_controller::do_init_side() // Make sure vision is accurate. actions::clear_shroud(current_side(), true); + { const auto& active_mods = get_saved_game().classification().active_mods; - bool delay_advancements = std::find(active_mods.begin(), active_mods.end(), "delay_advancements") != active_mods.end(); + bool delay_advancements + = std::find(active_mods.begin(), active_mods.end(), "delay_advancements") != active_mods.end(); - for(unit &u : resources::gameboard->units()) { + for(unit& u : resources::gameboard->units()) { if(delay_advancements && u.side() == current_side()) { advance_unit_at(u.get_location()); } } } + init_side_end(); check_victory(); sync.do_final_checkup(); @@ -545,16 +551,18 @@ void play_controller::init_side_end() { const time_of_day& tod = gamestate().tod_manager_.get_time_of_day(); - if (current_side() == 1 || !init_side_done_now_) + if(current_side() == 1 || !init_side_done_now_) { sound::play_sound(tod.sounds, sound::SOUND_SOURCES); + } - if (!is_skipping_replay()){ + if(!is_skipping_replay()) { gui_->invalidate_all(); } - if (!is_skipping_replay() && current_team().get_scroll_to_leader() && !map_start_.valid()){ - gui_->scroll_to_leader(current_side(), game_display::ONSCREEN,false); + if(!is_skipping_replay() && current_team().get_scroll_to_leader() && !map_start_.valid()) { + gui_->scroll_to_leader(current_side(), game_display::ONSCREEN, false); } + map_start_ = map_location(); whiteboard_manager_->on_init_side(); } @@ -568,7 +576,7 @@ config play_controller::to_config() const gui_->write(cfg.add_child("display")); - //Write the soundsources. + // Write the soundsources. soundsources_manager_->write_sourcespecs(cfg); gui_->labels().write(cfg); @@ -581,7 +589,7 @@ void play_controller::finish_side_turn() { whiteboard_manager_->on_finish_side_turn(current_side()); - { //Block for set_scontext_synced + { // Block for set_scontext_synced set_scontext_synced sync(1); // Ending the turn commits all moves. undo_stack().clear(); @@ -593,7 +601,7 @@ void play_controller::finish_side_turn() actions::clear_shroud(current_side()); pump().fire("side_turn_end"); - pump().fire("side_"+ side_num + "_turn_end"); + pump().fire("side_" + side_num + "_turn_end"); pump().fire("side_turn_" + turn_num + "_end"); pump().fire("side_" + side_num + "_turn_" + turn_num + "_end"); // This is where we refog, after all of a side's events are done. @@ -618,8 +626,9 @@ void play_controller::finish_turn() bool play_controller::enemies_visible() const { // If we aren't using fog/shroud, this is easy :) - if(current_team().uses_fog() == false && current_team().uses_shroud() == false) + if(current_team().uses_fog() == false && current_team().uses_shroud() == false) { return true; + } // See if any enemies are visible for(const unit& u : get_units()) { @@ -648,7 +657,7 @@ void play_controller::enter_textbox() break; case gui::TEXTBOX_MESSAGE: menu_handler_.do_speak(); - menu_handler_.get_textbox().close(*gui_); //need to close that one after executing do_speak() ! + menu_handler_.get_textbox().close(*gui_); // need to close that one after executing do_speak() ! break; case gui::TEXTBOX_COMMAND: menu_handler_.get_textbox().close(*gui_); @@ -670,56 +679,52 @@ void play_controller::tab() std::set dictionary; switch(mode) { - case gui::TEXTBOX_SEARCH: - { + case gui::TEXTBOX_SEARCH: { for(const unit& u : get_units()) { const map_location& loc = u.get_location(); - if(!gui_->fogged(loc) && - !(get_teams()[gui_->viewing_team()].is_enemy(u.side()) && u.invisible(loc))) + if(!gui_->fogged(loc) && !(get_teams()[gui_->viewing_team()].is_enemy(u.side()) && u.invisible(loc))) dictionary.insert(u.name()); } - //TODO List map labels + // TODO List map labels break; } - case gui::TEXTBOX_COMMAND: - { + case gui::TEXTBOX_COMMAND: { std::vector commands = menu_handler_.get_commands_list(); dictionary.insert(commands.begin(), commands.end()); FALLTHROUGH; // we also want player names from the next case } - case gui::TEXTBOX_MESSAGE: - { - for (const team& t : get_teams()) { + case gui::TEXTBOX_MESSAGE: { + for(const team& t : get_teams()) { if(!t.is_empty()) dictionary.insert(t.current_player()); } // Add observers - for (const std::string& o : gui_->observers()){ + for(const std::string& o : gui_->observers()) { dictionary.insert(o); } // Add nicks who whispered you - for (const std::string& w : gui_->get_chat_manager().whisperers()){ + for(const std::string& w : gui_->get_chat_manager().whisperers()) { dictionary.insert(w); } // Add nicks from friendlist const std::map friends = preferences::get_acquaintances_nice("friend"); - for(std::map::const_iterator iter = friends.begin(); iter != friends.end(); ++iter){ + for(std::map::const_iterator iter = friends.begin(); iter != friends.end(); ++iter) { dictionary.insert((*iter).first); } - //Exclude own nick from tab-completion. - //NOTE why ? + // Exclude own nick from tab-completion. + // NOTE why ? dictionary.erase(preferences::login()); break; } default: ERR_DP << "unknown textbox mode" << std::endl; - } //switch(mode) + } // switch(mode) menu_handler_.get_textbox().tab(dictionary); } @@ -729,6 +734,7 @@ team& play_controller::current_team() if(get_teams().size() == 0) { throw game::game_error("The scenario has no sides defined"); } + assert(gamestate().board_.has_team(current_side())); return gamestate().board_.get_team(current_side()); } @@ -738,6 +744,7 @@ const team& play_controller::current_team() const if(get_teams().size() == 0) { throw game::game_error("The scenario has no sides defined"); } + assert(gamestate().board_.has_team(current_side())); return gamestate().board_.get_team(current_side()); } @@ -747,8 +754,7 @@ bool play_controller::is_team_visible(int team_num, bool observer) const const team& t = gamestate().board_.get_team(team_num); if(observer) { return !t.get_disallow_observers() && !t.is_empty(); - } - else { + } else { return t.is_local_human() && !t.is_idle(); } } @@ -765,6 +771,7 @@ int play_controller::find_last_visible_team() const return team_num; } } + return 0; } @@ -811,7 +818,7 @@ void play_controller::process_focus_keydown_event(const SDL_Event& event) void play_controller::process_keydown_event(const SDL_Event& event) { - if (event.key.keysym.sym == SDLK_TAB) { + if(event.key.keysym.sym == SDLK_TAB) { whiteboard_manager_->set_invert_behavior(true); } } @@ -821,8 +828,7 @@ void play_controller::process_keyup_event(const SDL_Event& event) // If the user has pressed 1 through 9, we want to show // how far the unit can move in that many turns if(event.key.keysym.sym >= '1' && event.key.keysym.sym <= '9') { - const int new_path_turns = (event.type == SDL_KEYDOWN) ? - event.key.keysym.sym - '1' : 0; + const int new_path_turns = (event.type == SDL_KEYDOWN) ? event.key.keysym.sym - '1' : 0; if(new_path_turns != mouse_handler_.get_path_turns()) { mouse_handler_.set_path_turns(new_path_turns); @@ -833,25 +839,24 @@ void play_controller::process_keyup_event(const SDL_Event& event) // if it's not the unit's turn, we reset its moves unit_movement_resetter move_reset(*u, u->side() != current_side()); - mouse_handler_.set_current_paths(pathfind::paths(*u, false, - true, get_teams()[gui_->viewing_team()], - mouse_handler_.get_path_turns())); + mouse_handler_.set_current_paths(pathfind::paths( + *u, false, true, get_teams()[gui_->viewing_team()], mouse_handler_.get_path_turns())); gui_->highlight_reach(mouse_handler_.current_paths()); } else { mouse_handler_.select_hex(mouse_handler_.get_selected_hex(), false, false, false); } - } - } else if (event.key.keysym.sym == SDLK_TAB) { + } else if(event.key.keysym.sym == SDLK_TAB) { CKey keys; - if (!keys[SDLK_TAB]) { + if(!keys[SDLK_TAB]) { whiteboard_manager_->set_invert_behavior(false); } } } -replay& play_controller::get_replay() { +replay& play_controller::get_replay() +{ assert(replay_); return *replay_.get(); } @@ -868,7 +873,7 @@ void play_controller::save_game() savegame::ingame_savegame save(saved_game_, preferences::save_compression_format()); save.save_game_interactive("", savegame::savegame::OK_CANCEL); } else { - save_blocker::on_unblock(this,&play_controller::save_game); + save_blocker::on_unblock(this, &play_controller::save_game); } } @@ -890,7 +895,7 @@ void play_controller::save_replay() savegame::replay_savegame save(saved_game_, preferences::save_compression_format()); save.save_game_interactive("", savegame::savegame::OK_CANCEL); } else { - save_blocker::on_unblock(this,&play_controller::save_replay); + save_blocker::on_unblock(this, &play_controller::save_replay); } } @@ -909,7 +914,7 @@ void play_controller::save_map() save_blocker::save_unblocker unblocker; menu_handler_.save_map(); } else { - save_blocker::on_unblock(this,&play_controller::save_map); + save_blocker::on_unblock(this, &play_controller::save_map); } } @@ -944,8 +949,12 @@ bool play_controller::can_redo() const const std::string& play_controller::select_music(bool victory) const { const std::vector& music_list = victory - ? (gamestate_->get_game_data()->get_victory_music().empty() ? game_config::default_victory_music : gamestate_->get_game_data()->get_victory_music()) - : (gamestate_->get_game_data()->get_defeat_music().empty() ? game_config::default_defeat_music : gamestate_->get_game_data()->get_defeat_music()); + ? (gamestate_->get_game_data()->get_victory_music().empty() + ? game_config::default_victory_music + : gamestate_->get_game_data()->get_victory_music()) + : (gamestate_->get_game_data()->get_defeat_music().empty() + ? game_config::default_defeat_music + : gamestate_->get_game_data()->get_defeat_music()); if(music_list.empty()) { // Since this function returns a reference, we can't return a temporary empty string. @@ -953,35 +962,42 @@ const std::string& play_controller::select_music(bool victory) const return empty_str; } - return music_list[randomness::rng::default_instance().get_random_int(0, music_list.size()-1)]; + return music_list[randomness::rng::default_instance().get_random_int(0, music_list.size() - 1)]; } void play_controller::check_victory() { - if(linger_) - { + if(linger_) { return; } - if (is_regular_game_end()) { + if(is_regular_game_end()) { return; } + bool continue_level, found_player, found_network_player, invalidate_all; std::set not_defeated; - gamestate().board_.check_victory(continue_level, found_player, found_network_player, invalidate_all, not_defeated, remove_from_carryover_on_defeat_); + gamestate().board_.check_victory( + continue_level, + found_player, + found_network_player, + invalidate_all, + not_defeated, + remove_from_carryover_on_defeat_ + ); - if (invalidate_all) { + if(invalidate_all) { gui_->invalidate_all(); } - if (continue_level) { - return ; + if(continue_level) { + return; } - if (found_player || found_network_player) { + if(found_player || found_network_player) { pump().fire("enemies_defeated"); - if (is_regular_game_end()) { + if(is_regular_game_end()) { return; } } @@ -990,24 +1006,26 @@ void play_controller::check_victory() DBG_EE << "found_player: " << found_player << std::endl; DBG_EE << "found_network_player: " << found_network_player << std::endl; - if (!victory_when_enemies_defeated_ && (found_player || found_network_player)) { + if(!victory_when_enemies_defeated_ && (found_player || found_network_player)) { // This level has asked not to be ended by this condition. return; } - if (gui_->video().non_interactive()) { + if(gui_->video().non_interactive()) { LOG_AIT << "winner: "; - for (unsigned l : not_defeated) { + for(unsigned l : not_defeated) { std::string ai = ai::manager::get_singleton().get_active_ai_identifier_for_side(l); - if (ai.empty()) ai = "default ai"; + if(ai.empty()) + ai = "default ai"; LOG_AIT << l << " (using " << ai << ") "; } + LOG_AIT << std::endl; ai_testing::log_victory(not_defeated); } DBG_EE << "throwing end level exception..." << std::endl; - //Also proceed to the next scenario when another player survived. + // Also proceed to the next scenario when another player survived. end_level_data el_data; el_data.proceed_to_next_level = found_player || found_network_player; el_data.is_victory = found_player; @@ -1016,10 +1034,13 @@ void play_controller::check_victory() void play_controller::process_oos(const std::string& msg) const { - if (gui_->video().non_interactive()) { + if(gui_->video().non_interactive()) { throw game::game_error(msg); } - if (game_config::ignore_replay_errors) return; + + if(game_config::ignore_replay_errors) { + return; + } std::stringstream message; message << _("The game is out of sync. It might not make much sense to continue. Do you want to save your game?"); @@ -1053,7 +1074,7 @@ void play_controller::do_consolesave(const std::string& filename) void play_controller::update_savegame_snapshot() const { - //note: this writes to level_ if this is not a replay. + // note: this writes to level_ if this is not a replay. this->saved_game_.set_snapshot(to_config()); } @@ -1087,6 +1108,7 @@ bool play_controller::is_browsing() const if(linger_ || !gamestate_->init_side_done() || gamestate().gamedata_.phase() != game_data::PLAY) { return true; } + const team& t = current_team(); return !t.is_local_human() || !t.is_proxy_human(); } @@ -1096,12 +1118,10 @@ void play_controller::play_slice_catch() if(should_return_to_play_side()) { return; } - try - { + + try { play_slice(); - } - catch(const return_to_play_side_exception&) - { + } catch(const return_to_play_side_exception&) { assert(should_return_to_play_side()); } } @@ -1110,8 +1130,7 @@ void play_controller::start_game() { fire_preload(); - if(!gamestate().start_event_fired_) - { + if(!gamestate().start_event_fired_) { gamestate().start_event_fired_ = true; map_start_ = map_location(); resources::recorder->add_start_if_not_there_yet(); @@ -1120,11 +1139,11 @@ void play_controller::start_game() set_scontext_synced sync; fire_prestart(); - if (is_regular_game_end()) { + if(is_regular_game_end()) { return; } - for (const team& t : get_teams()) { + for(const team& t : get_teams()) { actions::clear_shroud(t.side(), false, false); } @@ -1132,21 +1151,20 @@ void play_controller::start_game() LOG_NG << "first_time..." << (is_skipping_replay() ? "skipping" : "no skip") << "\n"; fire_start(); - if (is_regular_game_end()) { + if(is_regular_game_end()) { return; } + sync.do_final_checkup(); gui_->recalculate_minimap(); + // Initialize countdown clock. - for (const team& t : get_teams()) - { - if (saved_game_.mp_settings().mp_countdown) { + for(const team& t : get_teams()) { + if(saved_game_.mp_settings().mp_countdown) { t.set_countdown_time(1000 * saved_game_.mp_settings().mp_countdown_init_time); } } - } - else - { + } else { init_gui(); gamestate().gamedata_.set_phase(game_data::PLAY); gui_->recalculate_minimap(); @@ -1156,25 +1174,27 @@ void play_controller::start_game() bool play_controller::can_use_synced_wml_menu() const { const team& viewing_team = get_teams()[gui_->viewing_team()]; - return gui_->viewing_team() == gui_->playing_team() && !events::commands_disabled && viewing_team.is_local_human() && !is_lingering() && !is_browsing(); + return gui_->viewing_team() == gui_->playing_team() && !events::commands_disabled && viewing_team.is_local_human() + && !is_lingering() && !is_browsing(); } std::set play_controller::all_players() const { std::set res = gui_->observers(); - for (const team& t : get_teams()) - { - if (t.is_human()) { + for(const team& t : get_teams()) { + if(t.is_human()) { res.insert(t.current_player()); } } + return res; } void play_controller::play_side() { - //check for team-specific items in the scenario + // check for team-specific items in the scenario gui_->parse_team_overlays(); + do { update_viewing_player(); { @@ -1194,10 +1214,9 @@ void play_controller::play_side() if(is_regular_game_end()) { return; } + } while(player_type_changed_); - } while (player_type_changed_); - // Keep looping if the type of a team (human/ai/networked) - // has changed mid-turn + // Keep looping if the type of a team (human/ai/networked) has changed mid-turn sync_end_turn(); } @@ -1222,7 +1241,7 @@ void play_controller::play_turn() last_player_number = gamestate_->player_number_; // If a side is empty skip over it. - if (!current_team().is_empty()) { + if(!current_team().is_empty()) { init_side_begin(); if(gamestate_->init_side_done()) { // This is the case in a reloaded game where the side was initialized before saving the game. @@ -1231,29 +1250,35 @@ void play_controller::play_turn() ai_testing::log_turn_start(current_side()); play_side(); - //ignore any changes to next_player_number_ that happen after the [end_turn] is sended to the server, otherwise we will get OOS. + + // ignore any changes to next_player_number_ that happen after the [end_turn] is sended to the server, + // otherwise we will get OOS. next_player_number = gamestate_->next_player_number_; assert(next_player_number <= 2 * static_cast(get_teams().size())); + if(is_regular_game_end()) { return; } - //note: play_side() send the [end_turn] to the sever and finish_side_turn() callsie the side turn end events. - // this means that during the side turn end events the clients think it is still the last sides turn while - // the server thinks that it is already the next plyers turn. i don'T think this is a problem though. + + // note: play_side() send the [end_turn] to the sever and finish_side_turn() callsie the side turn end + // events. + // this means that during the side turn end events the clients think it is still the last sides turn + // while the server thinks that it is already the next plyers turn. i don'T think this is a problem + // though. finish_side_turn(); if(is_regular_game_end()) { return; } + if(gui_->video().non_interactive()) { - LOG_AIT << " Player " << current_side() << ": " << - current_team().villages().size() << " Villages" << - std::endl; + LOG_AIT << " Player " << current_side() << ": " << current_team().villages().size() << " Villages" << std::endl; ai_testing::log_turn_end(current_side()); } } gamestate_->player_number_ = next_player_number; } + // If the loop exits due to the last team having been processed. gamestate_->player_number_ = last_player_number; @@ -1262,7 +1287,7 @@ void play_controller::play_turn() // Time has run out check_time_over(); - if (!is_regular_game_end()) { + if(!is_regular_game_end()) { gamestate_->player_number_ = modulo(next_player_number, get_teams().size(), 1); } } @@ -1276,8 +1301,9 @@ void play_controller::check_time_over() 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()) { + if(gamestate().tod_manager_.is_time_left()) { return; } @@ -1287,9 +1313,10 @@ void play_controller::check_time_over() } check_victory(); - if (is_regular_game_end()) { + if(is_regular_game_end()) { return; } + end_level_data e; e.proceed_to_next_level = false; e.is_victory = false; @@ -1321,7 +1348,7 @@ void play_controller::toggle_skipping_replay() { skip_replay_ = !skip_replay_; const std::shared_ptr skip_animation_button = get_display().find_action_button("skip-animation"); - if (skip_animation_button) { + if(skip_animation_button) { skip_animation_button->set_check(skip_replay_); } }