Skip to content

Commit

Permalink
refactor random_faction_mode to be an enum rather than an int
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeck88 committed Mar 13, 2015
1 parent 8312cd1 commit bf5fd17
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 22 deletions.
9 changes: 5 additions & 4 deletions src/game_initialization/configure_engine.cpp
@@ -1,6 +1,7 @@
#include "configure_engine.hpp"
#include "formula_string_utils.hpp"
#include "game_config_manager.hpp"
#include "mp_game_settings.hpp"
#include "settings.hpp"

#include <boost/foreach.hpp>
Expand Down Expand Up @@ -78,7 +79,7 @@ bool configure_engine::fog_game() const { return parameters_.fog_game; }
bool configure_engine::shroud_game() const { return parameters_.shroud_game; }
bool configure_engine::allow_observers() const { return parameters_.allow_observers; }
bool configure_engine::shuffle_sides() const { return parameters_.shuffle_sides; }
int configure_engine::random_faction_mode() const { return parameters_.random_faction_mode; }
mp_game_settings::RANDOM_FACTION_MODE configure_engine::random_faction_mode() const { return parameters_.random_faction_mode; }
const config& configure_engine::options() const { return parameters_.options; }

void configure_engine::set_game_name(std::string val) { parameters_.name = val; }
Expand All @@ -98,7 +99,7 @@ void configure_engine::set_shroud_game(bool val) { parameters_.shroud_game = val
void configure_engine::set_allow_observers(bool val) { parameters_.allow_observers = val; }
void configure_engine::set_oos_debug(bool val) { state_.classification().oos_debug = val; }
void configure_engine::set_shuffle_sides(bool val) { parameters_.shuffle_sides = val; }
void configure_engine::set_random_faction_mode(int val) { parameters_.random_faction_mode = val;}
void configure_engine::set_random_faction_mode(mp_game_settings::RANDOM_FACTION_MODE val) { parameters_.random_faction_mode = val;}
void configure_engine::set_options(const config& cfg) { parameters_.options = cfg; }

void configure_engine::set_scenario(size_t scenario_num) {
Expand Down Expand Up @@ -183,8 +184,8 @@ bool configure_engine::allow_observers_default() const {
bool configure_engine::shuffle_sides_default() const {
return preferences::shuffle_sides();
}
int configure_engine::random_faction_mode_default() const {
return preferences::random_faction_mode();
mp_game_settings::RANDOM_FACTION_MODE configure_engine::random_faction_mode_default() const {
return mp_game_settings::string_to_RANDOM_FACTION_MODE_default(preferences::random_faction_mode(), mp_game_settings::DEFAULT);
}

const config& configure_engine::options_default() const {
Expand Down
7 changes: 4 additions & 3 deletions src/game_initialization/configure_engine.hpp
Expand Up @@ -17,6 +17,7 @@

#include "gettext.hpp"
#include "game_preferences.hpp"
#include "mp_game_settings.hpp"
#include "saved_game.hpp"

namespace ng {
Expand Down Expand Up @@ -55,7 +56,7 @@ class configure_engine
bool shroud_game() const;
bool allow_observers() const;
bool shuffle_sides() const;
int random_faction_mode() const;
mp_game_settings::RANDOM_FACTION_MODE random_faction_mode() const;
const config& options() const;

// setter methods
Expand All @@ -76,7 +77,7 @@ class configure_engine
void set_allow_observers(bool val);
void set_oos_debug(bool val);
void set_shuffle_sides(bool val);
void set_random_faction_mode(int val);
void set_random_faction_mode(mp_game_settings::RANDOM_FACTION_MODE val);
void set_options(const config& cfg);

void set_scenario(size_t scenario_num);
Expand All @@ -99,7 +100,7 @@ class configure_engine
bool shroud_game_default() const;
bool allow_observers_default() const;
bool shuffle_sides_default() const;
int random_faction_mode_default() const;
mp_game_settings::RANDOM_FACTION_MODE random_faction_mode_default() const;
const config& options_default() const;

// parameters_ accessor
Expand Down
6 changes: 3 additions & 3 deletions src/game_initialization/connect_engine.cpp
Expand Up @@ -399,14 +399,14 @@ void connect_engine::start_game(LOAD_USERS load_users)
std::vector<std::string> avoid_faction_ids;

// If we aren't resolving random factions independently at random, calculate which factions should not appear for this side.
if (params_.random_faction_mode != 0) {
if (params_.random_faction_mode != mp_game_settings::DEFAULT) {
BOOST_FOREACH(side_engine_ptr side2, side_engines_) {
if (!side2->flg().is_random_faction()) {
switch(params_.random_faction_mode) {
case 1: //no mirror
case mp_game_settings::NO_MIRROR:
avoid_faction_ids.push_back(side2->flg().current_faction()["id"].str());
break;
case 2: //no ally mirror
case mp_game_settings::NO_ALLY_MIRROR:
if (side2->team() == side->team()) {// TODO: When the connect engines are fixed to allow multiple teams, this should be changed to "if side1 and side2 are allied, i.e. their list of teams has nonempty intersection"
avoid_faction_ids.push_back(side2->flg().current_faction()["id"].str());
}
Expand Down
12 changes: 9 additions & 3 deletions src/game_initialization/multiplayer_configure.cpp
Expand Up @@ -26,6 +26,7 @@
#include "gui/dialogs/mp_create_game_set_password.hpp"
#include "gui/dialogs/transient_message.hpp"
#include "minimap.hpp"
#include "mp_game_settings.hpp"
#include "multiplayer_configure.hpp"
#include "filesystem.hpp"
#include "log.hpp"
Expand Down Expand Up @@ -185,7 +186,12 @@ configure::configure(game_display& disp, const config &cfg, chat& c, config& gam
shuffle_sides_.set_check(engine_.shuffle_sides_default());
shuffle_sides_.set_help_string(_("Assign sides to players at random"));

random_faction_mode_.set_items(boost::assign::list_of(_("Independent"))(_("No Mirror"))( _("No Ally Mirror")));
std::vector<std::string> translated_modes;
for(size_t i = 0; i < mp_game_settings::RANDOM_FACTION_MODE_COUNT; ++i) {
std::string mode_str = mp_game_settings::RANDOM_FACTION_MODE_to_string(static_cast<mp_game_settings::RANDOM_FACTION_MODE> (i));
translated_modes.push_back(translation::gettext(mode_str.c_str()));
}
random_faction_mode_.set_items(translated_modes);
random_faction_mode_.set_selected(engine_.random_faction_mode());
random_faction_mode_.set_help_string(_("Independent: Random factions assigned independently uniformly at random\nNo Mirror: No two players will get the same faction\nNo Ally Mirror: No two allied players will get the same faction"));

Expand Down Expand Up @@ -235,7 +241,7 @@ configure::~configure()
// Save values for next game
DBG_MP << "storing parameter values in preferences" << std::endl;
preferences::set_shuffle_sides(engine_.shuffle_sides());
preferences::set_random_faction_mode(engine_.random_faction_mode());
preferences::set_random_faction_mode(mp_game_settings::RANDOM_FACTION_MODE_to_string(engine_.random_faction_mode()));
preferences::set_use_map_settings(engine_.use_map_settings());
preferences::set_countdown(engine_.mp_countdown());
preferences::set_countdown_init_time(engine_.mp_countdown_init_time());
Expand Down Expand Up @@ -299,7 +305,7 @@ const mp_game_settings& configure::get_parameters()
engine_.set_allow_observers(observers_game_.checked());
engine_.set_oos_debug(oos_debug_.checked());
engine_.set_shuffle_sides(shuffle_sides_.checked());
engine_.set_random_faction_mode(random_faction_mode_.selected());
engine_.set_random_faction_mode(static_cast<mp_game_settings::RANDOM_FACTION_MODE>(random_faction_mode_.selected()));

engine_.set_options(options_manager_.get_values());

Expand Down
6 changes: 3 additions & 3 deletions src/game_preferences.cpp
Expand Up @@ -568,11 +568,11 @@ void set_shuffle_sides(bool value)
preferences::set("shuffle_sides", value);
}

int random_faction_mode(){
return lexical_cast_default<int>(preferences::get("random_faction_mode"), 0);
std::string random_faction_mode(){
return preferences::get("random_faction_mode");
}

void set_random_faction_mode(int value) {
void set_random_faction_mode(const std::string & value) {
preferences::set("random_faction_mode", value);
}

Expand Down
4 changes: 2 additions & 2 deletions src/game_preferences.hpp
Expand Up @@ -125,8 +125,8 @@ class acquaintance;
bool shuffle_sides();
void set_shuffle_sides(bool value);

int random_faction_mode();
void set_random_faction_mode(int value);
std::string random_faction_mode();
void set_random_faction_mode(const std::string & value);

bool use_map_settings();
void set_use_map_settings(bool value);
Expand Down
6 changes: 3 additions & 3 deletions src/mp_game_settings.cpp
Expand Up @@ -50,8 +50,8 @@ mp_game_settings::mp_game_settings() :
shroud_game(false),
allow_observers(false),
shuffle_sides(false),
random_faction_mode(0),
saved_game(false),
random_faction_mode(DEFAULT),
options()
{}

Expand Down Expand Up @@ -84,8 +84,8 @@ mp_game_settings::mp_game_settings(const config& cfg)
, shroud_game(cfg["mp_shroud"].to_bool())
, allow_observers(cfg["observer"].to_bool())
, shuffle_sides(cfg["shuffle_sides"].to_bool())
, random_faction_mode(cfg["random_faction_mode"].to_int(0))
, saved_game(cfg["savegame"].to_bool())
, random_faction_mode(string_to_RANDOM_FACTION_MODE_default(cfg["random_faction_mode"].str(), DEFAULT))
, options(cfg.child_or_empty("options"))
{
}
Expand Down Expand Up @@ -120,7 +120,7 @@ config mp_game_settings::to_config() const
cfg["mp_random_start_time"] = random_start_time;
cfg["observer"] = allow_observers;
cfg["shuffle_sides"] = shuffle_sides;
cfg["random_faction_mode"] = random_faction_mode;
cfg["random_faction_mode"] = RANDOM_FACTION_MODE_to_string (random_faction_mode);
cfg["savegame"] = saved_game;
cfg.add_child("options", options);

Expand Down
13 changes: 12 additions & 1 deletion src/mp_game_settings.hpp
Expand Up @@ -18,6 +18,8 @@
#define MP_GAME_SETTINGS_HPP_INCLUDED

#include "config.hpp"
#include "gettext.hpp"
#include "make_enum.hpp"
#include "savegame_config.hpp"

struct mp_game_settings : public savegame::savegame_config
Expand Down Expand Up @@ -58,11 +60,20 @@ struct mp_game_settings : public savegame::savegame_config
bool shroud_game;
bool allow_observers;
bool shuffle_sides;
int random_faction_mode;

bool saved_game;

MAKE_ENUM(RANDOM_FACTION_MODE,
(DEFAULT, N_("Independent"))
(NO_MIRROR, N_("No Mirror"))
(NO_ALLY_MIRROR, N_("No Ally Mirror"))
)

RANDOM_FACTION_MODE random_faction_mode;

config options;
};

MAKE_ENUM_STREAM_OPS2(mp_game_settings, RANDOM_FACTION_MODE)

#endif

0 comments on commit bf5fd17

Please sign in to comment.