From 4b124100861ad253b7c95aef3ee8122d884e1c2f Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Sun, 3 Jan 2021 04:45:49 +1100 Subject: [PATCH] Game Classification: added convenience type check functions Shortens a bunch of code --- src/game_classification.cpp | 15 ++++------ src/game_classification.hpp | 34 ++++++++++++++++++---- src/game_config_manager.cpp | 4 +-- src/game_initialization/connect_engine.cpp | 2 +- src/game_initialization/create_engine.cpp | 13 ++++----- src/game_initialization/playcampaign.cpp | 12 +++----- src/game_launcher.cpp | 2 +- src/gui/dialogs/game_stats.cpp | 2 +- src/scripting/game_lua_kernel.cpp | 2 +- 9 files changed, 50 insertions(+), 36 deletions(-) diff --git a/src/game_classification.cpp b/src/game_classification.cpp index 3ac4f54cfd65..2f0f377d4717 100644 --- a/src/game_classification.cpp +++ b/src/game_classification.cpp @@ -84,24 +84,19 @@ config game_classification::to_config() const std::string game_classification::get_tagname() const { - if(this->campaign_type == CAMPAIGN_TYPE::MULTIPLAYER) { - return this->campaign.empty() ? "multiplayer" : "scenario"; + if(is_multiplayer()) { + return campaign.empty() ? "multiplayer" : "scenario"; } - if(this->campaign_type == CAMPAIGN_TYPE::TUTORIAL) { + if(is_tutorial()) { return "scenario"; } - return this->campaign_type.to_string(); + return campaign_type.to_string(); } -bool game_classification::is_normal_mp_game() const +namespace { - return this->campaign_type == CAMPAIGN_TYPE::MULTIPLAYER && this->campaign.empty(); -} - -namespace { - // helper objects for saved_game::expand_mp_events() struct modevents_entry { diff --git a/src/game_classification.hpp b/src/game_classification.hpp index 88706a45dd71..ce10d817421b 100644 --- a/src/game_classification.hpp +++ b/src/game_classification.hpp @@ -31,11 +31,6 @@ class game_classification game_classification() = default; explicit game_classification(const config& cfg); - config to_config() const; - std::string get_tagname() const; - bool is_normal_mp_game() const; - std::set active_addons(const std::string& scenario_id) const; - std::string label; /**< Name of the game (e.g. name of save file). */ std::string version; /**< Version game was created with. */ MAKE_ENUM (CAMPAIGN_TYPE, /**< Type of the game - campaign, multiplayer etc. */ @@ -64,4 +59,33 @@ class game_classification std::string difficulty = DEFAULT_DIFFICULTY; /**< The difficulty level the game is being played on. */ std::string random_mode = ""; bool oos_debug = false; + + config to_config() const; + std::string get_tagname() const; + std::set active_addons(const std::string& scenario_id) const; + + bool is_normal_mp_game() const + { + return is_multiplayer() && campaign.empty(); + } + + bool is_scenario() const + { + return campaign_type == CAMPAIGN_TYPE::SCENARIO; + } + + bool is_multiplayer() const + { + return campaign_type == CAMPAIGN_TYPE::MULTIPLAYER; + } + + bool is_test() const + { + return campaign_type == CAMPAIGN_TYPE::TEST; + } + + bool is_tutorial() const + { + return campaign_type == CAMPAIGN_TYPE::TUTORIAL; + } }; diff --git a/src/game_config_manager.cpp b/src/game_config_manager.cpp index 9346ff001a8c..cf1df9b83a33 100644 --- a/src/game_config_manager.cpp +++ b/src/game_config_manager.cpp @@ -668,9 +668,9 @@ void game_config_manager::load_game_config_for_game( game_config::scoped_preproc_define era(classification.era_define, !classification.era_define.empty()); game_config::scoped_preproc_define multiplayer("MULTIPLAYER", - classification.campaign_type == game_classification::CAMPAIGN_TYPE::MULTIPLAYER); + classification.is_multiplayer()); game_config::scoped_preproc_define mptest("MP_TEST", cmdline_opts_.mptest && - classification.campaign_type == game_classification::CAMPAIGN_TYPE::MULTIPLAYER); + classification.is_multiplayer()); // // NOTE: these deques aren't used here, but the objects within are utilized as RAII helpers. diff --git a/src/game_initialization/connect_engine.cpp b/src/game_initialization/connect_engine.cpp index c1efbeb6fa4f..ebc5a6500c1a 100644 --- a/src/game_initialization/connect_engine.cpp +++ b/src/game_initialization/connect_engine.cpp @@ -904,7 +904,7 @@ side_engine::side_engine(const config& cfg, connect_engine& parent_engine, const update_controller_options(); // Tweak the controllers. - if(parent_.state_.classification().campaign_type == game_classification::CAMPAIGN_TYPE::SCENARIO && cfg_["controller"].blank()) { + if(parent_.state_.classification().is_scenario() && cfg_["controller"].blank()) { cfg_["controller"] = "ai"; } diff --git a/src/game_initialization/create_engine.cpp b/src/game_initialization/create_engine.cpp index dff8a12b06e9..cf018d957968 100644 --- a/src/game_initialization/create_engine.cpp +++ b/src/game_initialization/create_engine.cpp @@ -258,11 +258,10 @@ create_engine::create_engine(saved_game& state) state_.clear(); state_.classification().campaign_type = type; - game_config_manager::get()->load_game_config_for_create(type == game_classification::CAMPAIGN_TYPE::MULTIPLAYER); + game_config_manager::get()->load_game_config_for_create(state_.classification().is_multiplayer()); // Initialize dependency_manager_ after refreshing game config. - dependency_manager_.reset(new depcheck::manager( - game_config_, type == game_classification::CAMPAIGN_TYPE::MULTIPLAYER)); + dependency_manager_.reset(new depcheck::manager(game_config_, state_.classification().is_multiplayer())); // TODO: the editor dir is already configurable, is the preferences value filesystem::get_files_in_dir(filesystem::get_user_data_dir() + "/editor/maps", &user_map_names_, @@ -279,7 +278,7 @@ create_engine::create_engine(saved_game& state) state_.mp_settings().saved_game = mp_game_settings::SAVED_GAME_MODE::NONE; - for(const std::string& str : preferences::modifications(state_.classification().campaign_type == game_classification::CAMPAIGN_TYPE::MULTIPLAYER)) { + for(const std::string& str : preferences::modifications(state_.classification().is_multiplayer())) { if(game_config_.find_child("modification", "id", str)) { state_.classification().active_mods.push_back(str); } @@ -531,7 +530,7 @@ void create_engine::set_current_level(const std::size_t index) generator_.reset(nullptr); } - if(state_.classification().campaign_type == game_classification::CAMPAIGN_TYPE::MULTIPLAYER) { + if(state_.classification().is_multiplayer()) { dependency_manager_->try_scenario(current_level().id()); } } @@ -733,7 +732,7 @@ void create_engine::init_all_levels() } const std::string& type = data["type"]; - bool mp = state_.classification().campaign_type == game_classification::CAMPAIGN_TYPE::MULTIPLAYER; + const bool mp = state_.classification().is_multiplayer(); if(type == "mp" || (type == "hybrid" && mp)) { type_map_[level::TYPE::CAMPAIGN].games.emplace_back(new campaign(data)); @@ -770,7 +769,7 @@ void create_engine::init_extras(const MP_EXTRA extra_type) for(const config& extra : game_config_.child_range(extra_name)) { ng::depcheck::component_availability type = extra["type"].to_enum(default_availabilty); - bool mp = state_.classification().campaign_type == game_classification::CAMPAIGN_TYPE::MULTIPLAYER; + const bool mp = state_.classification().is_multiplayer(); if((type != ng::depcheck::component_availability::MP || mp) && (type != ng::depcheck::component_availability::SP || !mp) ) { diff --git a/src/game_initialization/playcampaign.cpp b/src/game_initialization/playcampaign.cpp index 650b23ef2741..a0c3a023aad3 100644 --- a/src/game_initialization/playcampaign.cpp +++ b/src/game_initialization/playcampaign.cpp @@ -93,9 +93,7 @@ void campaign_controller::show_carryover_message( } } - if(persistent_teams > 0 && ((has_next_scenario && end_level.proceed_to_next_level)|| - state_.classification().campaign_type == game_classification::CAMPAIGN_TYPE::TEST)) - { + if(persistent_teams > 0 && ((has_next_scenario && end_level.proceed_to_next_level) || state_.classification().is_test())) { const gamemap& map = playcontroller.get_map_const(); const tod_manager& tod = playcontroller.get_tod_manager_const(); @@ -256,8 +254,6 @@ LEVEL_RESULT campaign_controller::play_game() state_.expand_scenario(); - game_classification::CAMPAIGN_TYPE game_type = state_.classification().campaign_type; - while(state_.valid()) { LEVEL_RESULT res = LEVEL_RESULT::VICTORY; end_level_data end_level; @@ -276,7 +272,7 @@ LEVEL_RESULT campaign_controller::play_game() state_.expand_mp_options(); #if !defined(ALWAYS_USE_MP_CONTROLLER) - if(game_type != game_classification::CAMPAIGN_TYPE::MULTIPLAYER || is_replay_) { + if(!state_.classification().is_multiplayer() || is_replay_) { res = playsingle_scenario(end_level); if(is_replay_) { return res; @@ -376,7 +372,7 @@ LEVEL_RESULT campaign_controller::play_game() ng::connect_engine connect_engine(state_, false, mp_info_); - if(!connect_engine.can_start_game() || (game_config::debug && game_type == game_classification::CAMPAIGN_TYPE::MULTIPLAYER)) { + if(!connect_engine.can_start_game() || (game_config::debug && state_.classification().is_multiplayer())) { // Opens staging dialog to allow users to make an adjustments for scenario. if(!mp::goto_mp_connect(connect_engine, mp_info_ ? &mp_info_->connection : nullptr)) { return LEVEL_RESULT::QUIT; @@ -414,7 +410,7 @@ LEVEL_RESULT campaign_controller::play_game() return LEVEL_RESULT::QUIT; } - if(game_type == game_classification::CAMPAIGN_TYPE::SCENARIO) { + if(state_.classification().is_scenario()) { if(preferences::delete_saves()) { savegame::clean_saves(state_.classification().label); } diff --git a/src/game_launcher.cpp b/src/game_launcher.cpp index cd724b25f5d8..a86dcd00524b 100644 --- a/src/game_launcher.cpp +++ b/src/game_launcher.cpp @@ -727,7 +727,7 @@ bool game_launcher::load_game() statistics::clear_current_scenario(); } - if(state_.classification().campaign_type == game_classification::CAMPAIGN_TYPE::MULTIPLAYER) { + if(state_.classification().is_multiplayer()) { state_.unify_controllers(); } diff --git a/src/gui/dialogs/game_stats.cpp b/src/gui/dialogs/game_stats.cpp index 1acf34e057e9..a2cb02693144 100644 --- a/src/gui/dialogs/game_stats.cpp +++ b/src/gui/dialogs/game_stats.cpp @@ -111,7 +111,7 @@ void game_stats::pre_show(window& window) } if(resources::controller) { - if(resources::controller->get_classification().campaign_type == game_classification::CAMPAIGN_TYPE::MULTIPLAYER) { + if(resources::controller->get_classification().is_multiplayer()) { leader_name = team.side_name(); } } diff --git a/src/scripting/game_lua_kernel.cpp b/src/scripting/game_lua_kernel.cpp index 877b6425f8fc..3f4e601e23a3 100644 --- a/src/scripting/game_lua_kernel.cpp +++ b/src/scripting/game_lua_kernel.cpp @@ -4282,7 +4282,7 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports } luaL_setfuncs(L, callbacks, 0); - if(play_controller_.get_classification().campaign_type == game_classification::CAMPAIGN_TYPE::TEST) { + if(play_controller_.get_classification().is_test()) { static luaL_Reg const test_callbacks[] { { "fire_wml_menu_item", &dispatch<&game_lua_kernel::intf_fire_wml_menu_item > }, { nullptr, nullptr }