Refactor to make campaign_type an enum

This commit is contained in:
Chris Beck 2014-05-26 14:01:32 -04:00
parent 861a2bffe6
commit 793cbaf479
17 changed files with 135 additions and 104 deletions

View file

@ -803,36 +803,47 @@ void save_preview_pane::draw_contents()
const std::string& campaign_type = summary["campaign_type"];
if (summary["corrupt"].to_bool()) {
str << "\n" << _("#(Invalid)");
} else if (!campaign_type.empty()) {
} else {
str << "\n";
if(campaign_type == "scenario") {
const std::string campaign_id = summary["campaign"];
const config *campaign = NULL;
if (!campaign_id.empty()) {
if (const config &c = game_config_->find_child("campaign", "id", campaign_id))
campaign = &c;
}
utils::string_map symbols;
if (campaign != NULL) {
symbols["campaign_name"] = (*campaign)["name"];
} else {
// Fallback to nontranslatable campaign id.
symbols["campaign_name"] = "(" + campaign_id + ")";
}
str << vgettext("Campaign: $campaign_name", symbols);
try {
game_classification::CAMPAIGN_TYPE ct = lexical_cast<game_classification::CAMPAIGN_TYPE> (campaign_type);
// Display internal id for debug purposes if we didn't above
if (game_config::debug && (campaign != NULL)) {
str << '\n' << "(" << campaign_id << ")";
}
} else if(campaign_type == "multiplayer") {
str << _("Multiplayer");
} else if(campaign_type == "tutorial") {
str << _("Tutorial");
} else if(campaign_type == "test") {
str << _("Test scenario");
} else {
switch (ct) {
case game_classification::SCENARIO:
{
const std::string campaign_id = summary["campaign"];
const config *campaign = NULL;
if (!campaign_id.empty()) {
if (const config &c = game_config_->find_child("campaign", "id", campaign_id))
campaign = &c;
}
utils::string_map symbols;
if (campaign != NULL) {
symbols["campaign_name"] = (*campaign)["name"];
} else {
// Fallback to nontranslatable campaign id.
symbols["campaign_name"] = "(" + campaign_id + ")";
}
str << vgettext("Campaign: $campaign_name", symbols);
// Display internal id for debug purposes if we didn't above
if (game_config::debug && (campaign != NULL)) {
str << '\n' << "(" << campaign_id << ")";
}
break;
}
case game_classification::MULTIPLAYER:
str << _("Multiplayer");
break;
case game_classification::TUTORIAL:
str << _("Tutorial");
break;
case game_classification::TEST:
str << _("Test scenario");
break;
}
} catch (bad_lexical_cast &) {
str << campaign_type;
}

View file

@ -143,7 +143,7 @@ void game_config_manager::load_game_config(FORCE_RELOAD_CONFIG force_reload,
// become [multiplayer] tags and campaign's id should be added to them
// to allow to recognize which scenarios belongs to a loaded campaign.
if (classification != NULL) {
if (classification->campaign_type == "multiplayer" &&
if (classification->campaign_type == game_classification::MULTIPLAYER &&
!classification->campaign_define.empty()) {
const config& campaign = game_config().find_child("campaign",
@ -161,7 +161,7 @@ void game_config_manager::load_game_config(FORCE_RELOAD_CONFIG force_reload,
BOOST_FOREACH(config& cfg, scenarios) {
cfg["campaign_id"] = campaign_id;
cfg["require_scenario"] = require_campaign;
game_config_.add_child("multiplayer", cfg);
game_config_.add_child(lexical_cast<std::string>(game_classification::MULTIPLAYER), cfg);
}
}
}
@ -374,7 +374,7 @@ void game_config_manager::load_game_config_for_game(
game_config::scoped_preproc_define campaign(classification.campaign_define,
!classification.campaign_define.empty());
game_config::scoped_preproc_define multiplayer("MULTIPLAYER",
classification.campaign_type == "multiplayer");
classification.campaign_type == game_classification::MULTIPLAYER);
typedef boost::shared_ptr<game_config::scoped_preproc_define> define;
std::deque<define> extra_defines;

View file

@ -425,7 +425,7 @@ bool game_controller::play_test()
first_time = false;
state_.classification().campaign_type = "test";
state_.classification().campaign_type = game_classification::TEST;
state_.carryover_sides_start["next_scenario"] = test_scenario_;
state_.classification().campaign_define = "TEST";
@ -454,7 +454,7 @@ int game_controller::unit_test()
first_time_unit = false;
state_.classification().campaign_type = "test";
state_.classification().campaign_type = game_classification::TEST;
state_.carryover_sides_start["next_scenario"] = test_scenario_;
state_.classification().campaign_define = "TEST";
@ -615,7 +615,7 @@ bool game_controller::load_game()
}
}
if(state_.classification().campaign_type == "multiplayer") {
if(state_.classification().campaign_type == game_classification::MULTIPLAYER) {
BOOST_FOREACH(config &side, state_.snapshot.child_range("side"))
{
if (side["controller"] == "network")
@ -644,7 +644,7 @@ bool game_controller::load_game()
void game_controller::set_tutorial()
{
state_ = game_state();
state_.classification().campaign_type = "tutorial";
state_.classification().campaign_type = game_classification::TUTORIAL;
state_.carryover_sides_start["next_scenario"] = "tutorial";
state_.classification().campaign_define = "TUTORIAL";
}
@ -659,7 +659,7 @@ void game_controller::mark_completed_campaigns(std::vector<config> &campaigns)
bool game_controller::new_campaign()
{
state_ = game_state();
state_.classification().campaign_type = "scenario";
state_.classification().campaign_type = game_classification::SCENARIO;
std::vector<config> campaigns;
BOOST_FOREACH(const config& campaign,
@ -882,7 +882,7 @@ bool game_controller::play_multiplayer()
int res;
state_ = game_state();
state_.classification().campaign_type = "multiplayer";
state_.classification().campaign_type = game_classification::MULTIPLAYER;
//Print Gui only if the user hasn't specified any server
if( multiplayer_server_.empty() ){
@ -1003,7 +1003,7 @@ bool game_controller::play_multiplayer_commandline()
// These are all the relevant lines taken literally from play_multiplayer() above
state_ = game_state();
state_.classification().campaign_type = "multiplayer";
state_.classification().campaign_type = game_classification::MULTIPLAYER;
resources::config_manager->
load_game_config_for_game(state_.classification());
@ -1063,7 +1063,7 @@ void game_controller::launch_game(RELOAD_GAME_DATA reload)
resources::config_manager->game_config());
// don't show The End for multiplayer scenario
// change this if MP campaigns are implemented
if(result == VICTORY && (state_.classification().campaign_type.empty() || state_.classification().campaign_type != "multiplayer")) {
if(result == VICTORY && state_.classification().campaign_type != game_classification::MULTIPLAYER) {
preferences::add_completed_campaign(state_.classification().campaign);
the_end(disp(), state_.classification().end_text, state_.classification().end_text_duration);
if(state_.classification().end_credits) {

View file

@ -830,7 +830,7 @@ game_classification::game_classification(const config& cfg):
label(cfg["label"]),
parent(cfg["parent"]),
version(cfg["version"]),
campaign_type(cfg["campaign_type"].empty() ? "scenario" : cfg["campaign_type"].str()),
campaign_type(lexical_cast_default<game_classification::CAMPAIGN_TYPE> (cfg["campaign_type"], game_classification::SCENARIO)),
campaign_define(cfg["campaign_define"]),
campaign_xtra_defines(utils::split(cfg["campaign_extra_defines"])),
campaign(cfg["campaign"]),
@ -869,7 +869,7 @@ config game_classification::to_config() const
cfg["label"] = label;
cfg["parent"] = parent;
cfg["version"] = game_config::version;
cfg["campaign_type"] = campaign_type;
cfg["campaign_type"] = lexical_cast<std::string> (campaign_type);
cfg["campaign_define"] = campaign_define;
cfg["campaign_extra_defines"] = utils::join(campaign_xtra_defines);
cfg["campaign"] = campaign;
@ -1068,7 +1068,7 @@ void game_state::write_snapshot(config& cfg, game_display* gui) const
cfg["completion"] = classification_.completion;
cfg["campaign"] = classification_.campaign;
cfg["campaign_type"] = classification_.campaign_type;
cfg["campaign_type"] = lexical_cast<std::string> (classification_.campaign_type);
cfg["campaign_define"] = classification_.campaign_define;
cfg["campaign_extra_defines"] = utils::join(classification_.campaign_xtra_defines);
@ -1217,7 +1217,8 @@ game_state& game_state::operator=(const game_state& state)
void game_state::write_config(config_writer& out) const
{
out.write(classification_.to_config());
if (classification_.campaign_type == "multiplayer")
out.write_child("multiplayer", mp_settings_.to_config());
if (classification_.campaign_type == game_classification::MULTIPLAYER) {
out.write_child(lexical_cast<std::string>(game_classification::MULTIPLAYER), mp_settings_.to_config());
}
}

View file

@ -20,6 +20,7 @@
#include "config.hpp"
#include "game_end_exceptions.hpp"
#include "game_events/wmi_container.hpp"
#include "make_enum.hpp"
#include "map_location.hpp"
#include "mp_game_settings.hpp"
#include "simple_rng.hpp"
@ -237,7 +238,13 @@ public:
std::string label; /**< Name of the game (e.g. name of save file). */
std::string parent; /**< Parent of the game (for save-threading purposes). */
std::string version; /**< Version game was created with. */
std::string campaign_type; /**< Type of the game - campaign, multiplayer etc. */
MAKE_ENUM (CAMPAIGN_TYPE, /**< Type of the game - campaign, multiplayer etc. */
(SCENARIO, "scenario")
(MULTIPLAYER, "multiplayer")
(TEST, "test")
(TUTORIAL, "tutorial")
)
CAMPAIGN_TYPE campaign_type;
std::string campaign_define; /**< If there is a define the campaign uses to customize data */
std::vector<std::string> campaign_xtra_defines; /**< more customization of data */
@ -253,6 +260,7 @@ public:
std::string difficulty; /**< The difficulty level the game is being played on. */
std::string random_mode;
};
MAKE_ENUM_STREAM_OPS2(game_classification, CAMPAIGN_TYPE)
class game_state
{

View file

@ -270,40 +270,52 @@ void tgame_load::evaluate_summary_string(std::stringstream& str,
{
const std::string& campaign_type = cfg_summary["campaign_type"];
if(cfg_summary["corrupt"].to_bool()) {
if (cfg_summary["corrupt"].to_bool()) {
str << "\n" << _("#(Invalid)");
} else if(!campaign_type.empty()) {
} else {
str << "\n";
if(campaign_type == "scenario") {
const std::string campaign_id = cfg_summary["campaign"];
const config* campaign = NULL;
if(!campaign_id.empty()) {
if(const config& c
= cache_config_.find_child("campaign", "id", campaign_id))
try {
game_classification::CAMPAIGN_TYPE ct = lexical_cast<game_classification::CAMPAIGN_TYPE> (campaign_type);
campaign = &c;
}
utils::string_map symbols;
if(campaign != NULL) {
symbols["campaign_name"] = (*campaign)["name"];
} else {
// Fallback to nontranslatable campaign id.
symbols["campaign_name"] = "(" + campaign_id + ")";
}
str << vgettext("Campaign: $campaign_name", symbols);
switch (ct) {
case game_classification::SCENARIO:
{
const std::string campaign_id = cfg_summary["campaign"];
const config* campaign = NULL;
if(!campaign_id.empty()) {
if(const config& c
= cache_config_.find_child("campaign", "id", campaign_id)) {
// Display internal id for debug purposes if we didn't above
if(game_config::debug && (campaign != NULL)) {
str << '\n' << "(" << campaign_id << ")";
}
} else if(campaign_type == "multiplayer") {
str << _("Multiplayer");
} else if(campaign_type == "tutorial") {
str << _("Tutorial");
} else if(campaign_type == "test") {
str << _("Test scenario");
} else {
campaign = &c;
}
}
utils::string_map symbols;
if(campaign != NULL) {
symbols["campaign_name"] = (*campaign)["name"];
} else {
// Fallback to nontranslatable campaign id.
symbols["campaign_name"] = "(" + campaign_id + ")";
}
str << vgettext("Campaign: $campaign_name", symbols);
// Display internal id for debug purposes if we didn't above
if(game_config::debug && (campaign != NULL)) {
str << '\n' << "(" << campaign_id << ")";
}
break;
}
case game_classification::MULTIPLAYER:
str << _("Multiplayer");
break;
case game_classification::TUTORIAL:
str << _("Tutorial");
break;
case game_classification::TEST:
str << _("Test scenario");
break;
}
} catch (bad_lexical_cast &) {
str << campaign_type;
}

View file

@ -104,7 +104,7 @@ void status_table(display& gui, int selected)
leader_bools.push_back(false);
leader_name = "Unknown";
}
// if (gamestate_.classification().campaign_type == "multiplayer")
// if (gamestate_.classification().campaign_type == game_classification::MULTIPLAYER)
// leader_name = teams[n].current_player();
#ifndef LOW_MEM

View file

@ -255,7 +255,7 @@ void menu_handler::status_table(int selected)
leader_name = "Unknown";
}
if (gamestate_.classification().campaign_type == "multiplayer")
if (gamestate_.classification().campaign_type == game_classification::MULTIPLAYER)
leader_name = teams_[n].current_player();
} else {

View file

@ -202,8 +202,8 @@ void level_to_gamestate(config& level, game_state& state)
// all needed information here already).
state.replay_start() = level.child("replay_start");
level["campaign_type"] = "multiplayer";
state.classification().campaign_type = "multiplayer";
level["campaign_type"] = lexical_cast<std::string> (game_classification::MULTIPLAYER);
state.classification().campaign_type = game_classification::MULTIPLAYER;
state.classification().completion = level["completion"].str();
state.classification().version = level["version"].str();

View file

@ -350,7 +350,7 @@ create_engine::create_engine(game_display& disp, game_state& state) :
// Restore game config for multiplayer.
state_ = game_state();
state_.classification().campaign_type = "multiplayer";
state_.classification().campaign_type = game_classification::MULTIPLAYER;
resources::config_manager->
load_game_config_for_game(state_.classification());
@ -432,7 +432,8 @@ void create_engine::prepare_for_campaign(const std::string& difficulty)
load_game_config_for_game(state_.classification());
current_level().set_data(
resources::config_manager->game_config().find_child("multiplayer",
resources::config_manager->game_config().find_child(
lexical_cast<std::string> (game_classification::MULTIPLAYER),
"id", current_level().data()["first_scenario"]));
parameters_.mp_campaign = current_level().id();
@ -849,7 +850,8 @@ void create_engine::init_all_levels()
// Stand-alone scenarios.
BOOST_FOREACH(const config &data,
resources::config_manager->game_config().child_range("multiplayer"))
resources::config_manager->game_config().child_range(
lexical_cast<std::string> (game_classification::MULTIPLAYER)))
{
if (!data["allow_new_game"].to_bool(true))
continue;

View file

@ -201,7 +201,7 @@ wait::~wait()
{
if (get_result() == QUIT) {
state_ = game_state();
state_.classification().campaign_type = "multiplayer";
state_.classification().campaign_type = game_classification::MULTIPLAYER;
resources::config_manager->
load_game_config_for_game(state_.classification());
@ -227,14 +227,14 @@ void wait::join_game(bool observe)
if (first_scenario_) {
state_ = game_state();
state_.classification().campaign_type = "multiplayer";
state_.classification().campaign_type = game_classification::MULTIPLAYER;
const config* campaign = &resources::config_manager->
game_config().find_child("campaign", "id",
level_.child("multiplayer")["mp_campaign"]);
level_.child(lexical_cast<std::string>(game_classification::MULTIPLAYER))["mp_campaign"]);
if (*campaign) {
state_.classification().difficulty =
level_.child("multiplayer")["difficulty_define"].str();
level_.child(lexical_cast<std::string>(game_classification::MULTIPLAYER))["difficulty_define"].str();
state_.classification().campaign_define =
(*campaign)["define"].str();
state_.classification().campaign_xtra_defines =

View file

@ -131,7 +131,7 @@ static void store_carryover(game_state& gamestate, playsingle_controller& playco
}
if (persistent_teams > 0 && (has_next_scenario ||
gamestate.classification().campaign_type == "test"))
gamestate.classification().campaign_type == game_classification::TEST))
{
gamemap map = playcontroller.get_map_const();
int finishing_bonus_per_turn =
@ -209,16 +209,14 @@ static void generate_map(config const*& scenario)
LEVEL_RESULT play_replay(display& disp, game_state& gamestate, const config& game_config,
CVideo& video, bool is_unit_test)
{
std::string type = gamestate.classification().campaign_type;
if(type.empty())
type = "scenario";
const std::string campaign_type_str = lexical_cast<std::string> (gamestate.classification().campaign_type);
// 'starting_pos' will contain the position we start the game from.
config starting_pos;
if (gamestate.replay_start().empty()){
// Backwards compatibility code for 1.2 and 1.2.1
const config &scenario = game_config.find_child(type,"id",gamestate.carryover_sides_start["next_scenario"]);
const config &scenario = game_config.find_child(campaign_type_str,"id",gamestate.carryover_sides_start["next_scenario"]);
assert(scenario);
gamestate.replay_start() = scenario;
}
@ -402,9 +400,7 @@ LEVEL_RESULT play_game(game_display& disp, game_state& gamestate,
const config& game_config, io_type_t io_type, bool skip_replay,
bool network_game, bool blindfold_replay, bool is_unit_test)
{
std::string type = gamestate.classification().campaign_type;
if(type.empty())
type = "scenario";
const std::string campaign_type_str = lexical_cast_default<std::string> (gamestate.classification().campaign_type);
config const* scenario = NULL;
@ -434,7 +430,7 @@ LEVEL_RESULT play_game(game_display& disp, game_state& gamestate,
} else {
//reload of the scenario, as starting_pos contains carryover information only
LOG_G << "loading scenario: '" << sides.next_scenario() << "'\n";
scenario = &game_config.find_child(type, "id", sides.next_scenario());
scenario = &game_config.find_child(campaign_type_str, "id", sides.next_scenario());
if(!*scenario){
scenario = NULL;
@ -606,7 +602,7 @@ LEVEL_RESULT play_game(game_display& disp, game_state& gamestate,
gamestate.carryover_sides_start = sides.to_config();
} else {
// Retrieve next scenario data.
scenario = &game_config.find_child(type, "id",
scenario = &game_config.find_child(campaign_type_str, "id",
gamestate.carryover_sides_start["next_scenario"]);
if (!*scenario) {
scenario = NULL;
@ -724,9 +720,10 @@ LEVEL_RESULT play_game(game_display& disp, game_state& gamestate,
return QUIT;
}
if (gamestate.classification().campaign_type == "scenario"){
if (preferences::delete_saves())
if (gamestate.classification().campaign_type == game_classification::SCENARIO){
if (preferences::delete_saves()) {
savegame::clean_saves(gamestate.classification().label);
}
}
return VICTORY;
}

View file

@ -84,7 +84,7 @@ replay_controller::replay_controller(const config& level,
current_turn_(1),
is_playing_(false),
show_everything_(false),
show_team_(state_of_game.classification().campaign_type == "multiplayer" ? 0 : 1)
show_team_(state_of_game.classification().campaign_type == game_classification::MULTIPLAYER ? 0 : 1)
{
tod_manager_start_ = tod_manager_;

View file

@ -579,7 +579,7 @@ void loadgame::load_game(
}
gamestate_.classification().campaign_define = load_config_["campaign_define"].str();
gamestate_.classification().campaign_type = load_config_["campaign_type"].str();
gamestate_.classification().campaign_type = lexical_cast_default<game_classification::CAMPAIGN_TYPE> (load_config_["campaign_type"], game_classification::SCENARIO);
gamestate_.classification().campaign_xtra_defines = utils::split(load_config_["campaign_extra_defines"]);
gamestate_.classification().version = load_config_["version"].str();
if (config & carryover_sides_start = load_config_.child("carryover_sides_start")) {
@ -692,7 +692,7 @@ void loadgame::load_multiplayer_game()
throw load_game_cancelled_exception();
}
if(gamestate_.classification().campaign_type != "multiplayer") {
if(gamestate_.classification().campaign_type != game_classification::MULTIPLAYER) {
gui2::show_transient_error_message(gui_.video(), _("This is not a multiplayer save."));
throw load_game_cancelled_exception();
}

View file

@ -1539,8 +1539,8 @@ static int impl_game_config_get(lua_State *L)
return_bool_attrib("mp_debug", game_config::mp_debug);
const game_state & game_state_ = *resources::state_of_game;
return_string_attrib("campaign_type", game_state_.classification().campaign_type);
if(game_state_.classification().campaign_type=="multiplayer") {
return_string_attrib("campaign_type", lexical_cast<std::string>(game_state_.classification().campaign_type));
if(game_state_.classification().campaign_type==game_classification::MULTIPLAYER) {
return_cfgref_attrib("mp_settings", game_state_.mp_settings().to_config());
return_cfgref_attrib("era", resources::config_manager->game_config().find_child("era","id",game_state_.mp_settings().mp_era));
//^ finds the era with name matching mp_era, and creates a lua reference from the config of that era.

View file

@ -75,7 +75,7 @@ struct mp_connect_fixture {
config_manager->init_game_config(game_config_manager::NO_FORCE_RELOAD);
state.reset(new game_state());
state->classification().campaign_type = "multiplayer";
state->classification().campaign_type = game_classification::MULTIPLAYER;
config_manager->load_game_config_for_game(state->classification());
params.reset(new mp_game_settings());
@ -85,7 +85,7 @@ struct mp_connect_fixture {
params->saved_game = false;
params->num_turns = params->scenario_data["turns"];
params->scenario_data = config_manager->
game_config().find_child("multiplayer", "id", params->name);
game_config().find_child(lexical_cast<std::string>(game_classification::MULTIPLAYER), "id", params->name);
}
~mp_connect_fixture()
{

View file

@ -109,7 +109,7 @@ namespace test_utils {
source_.type_key(current_time_++, SDLK_RETURN);
game_state& state = end->get_state();
state.classification().campaign_type = "test";
state.classification().campaign_type = game_classification::TEST;
state.carryover_sides_start["next_scenario"] = id_;
play_game(get_fake_display(1024, 768), state, game_config_);
}