Skip to content

Commit

Permalink
fix custom faction not available for remote players if it was changed…
Browse files Browse the repository at this point in the history
… by the host.

or a previous player of that side.
  • Loading branch information
gfgtdf committed Jun 5, 2016
1 parent 03ef9d8 commit f36c554
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
17 changes: 9 additions & 8 deletions src/game_initialization/connect_engine.cpp
Expand Up @@ -879,6 +879,15 @@ side_engine::side_engine(const config& cfg, connect_engine& parent_engine,
waiting_to_choose_faction_(allow_changes_),
custom_color_()
{
// Save default attributes that could be overwirtten by the faction, so that correct faction lists would be
// initialized by flg_manager when the new side config is sent over network.
cfg_.add_child("default_faction", config_of
("type", cfg_["type"])
("gender", cfg_["gender"])
("faction", cfg_["faction"])
("recruit", cfg_["recruit"])
);

// Check if this side should give its control to some other side.
const size_t side_cntr_index = cfg_["controller"].to_int(-1) - 1;
if (side_cntr_index < parent_.side_engines().size()) {
Expand Down Expand Up @@ -978,14 +987,6 @@ config side_engine::new_config() const
{
config res = cfg_;

// Save default "recruit" so that correct faction lists would be
// initialized by flg_manager when the new side config is sent over network.
// In case recruit list was empty, set a flag to indicate that.
res["default_recruit"] = cfg_["recruit"].str();
if (res["default_recruit"].empty()) {
res["no_recruit"] = true;
}

// If the user is allowed to change type, faction, leader etc,
// then import their new values in the config.
if (!parent_.params_.saved_game) {
Expand Down
25 changes: 14 additions & 11 deletions src/game_initialization/flg_manager.cpp
Expand Up @@ -331,20 +331,20 @@ void flg_manager::resolve_random(rand_rng::mt_rng & rng, const std::vector<std::
void flg_manager::update_available_factions()
{
const config* custom_faction = nullptr;
const bool show_custom_faction = side_["faction"] == "Custom" || !has_no_recruits_ || faction_lock_;
const bool show_custom_faction = get_default_faction(side_)["faction"] == "Custom" || !has_no_recruits_ || faction_lock_;

for (const config* faction : era_factions_) {
if ((*faction)["id"] == "Custom" && !show_custom_faction) {

// "Custom" faction should not be available if both
// "default_recruit" and "previous_recruits" lists are empty.
// "recruit" and "previous_recruits" lists are empty.
// However, it should be available if it was explicitly stated so.
custom_faction = faction;
continue;
}

// Add default faction to the top of the list.
if (side_["faction"] == (*faction)["id"]) {
if (get_default_faction(side_)["faction"] == (*faction)["id"]) {
available_factions_.insert(available_factions_.begin(), faction);
} else {
available_factions_.push_back(faction);
Expand Down Expand Up @@ -518,7 +518,7 @@ int flg_manager::find_suitable_faction() const
std::vector<std::string> find;
std::string search_field;

if (const config::attribute_value *f = side_.get("faction")) {
if (const config::attribute_value *f = get_default_faction(side_).get("faction")) {
// Choose based on faction.
find.push_back(f->str());
search_field = "id";
Expand Down Expand Up @@ -623,15 +623,18 @@ void flg_manager::set_current_gender(const std::string& gender)

std::vector<std::string> flg_manager::get_original_recruits(const config& cfg)
{
if (cfg["no_recruit"].to_bool()) {
return std::vector<std::string>();
}
const config::attribute_value& cfg_default_recruit = cfg["default_recruit"];
if (!cfg_default_recruit.empty()) {
return utils::split(cfg_default_recruit.str());
return utils::split(get_default_faction(cfg)["recruit"].str());
}

const config& flg_manager::get_default_faction(const config& cfg)
{
const config& df = cfg.child("default_faction");
if (df) {
return df;
}
else {
return utils::split(cfg["recruit"].str());
return cfg;
}
}

} // end namespace ng
1 change: 1 addition & 0 deletions src/game_initialization/flg_manager.hpp
Expand Up @@ -132,6 +132,7 @@ class flg_manager
std::string default_leader_gender_;
const config* default_leader_cfg_;
static std::vector<std::string> get_original_recruits(const config& cfg);
static const config& get_default_faction(const config& cfg);
};

} // end namespace ng
Expand Down
2 changes: 1 addition & 1 deletion src/team.cpp
Expand Up @@ -68,7 +68,7 @@ const boost::container::flat_set<std::string> team::attributes = boost::assign::
("countdown_time")("disallow_observers")("faction")
("faction_from_recruit")("faction_name")("gold_lock")("income_lock")
("leader")("random_leader")("team_lock")("terrain_liked")
("user_description")("default_recruit")("controller_lock")("chose_random")
("user_description")("controller_lock")("chose_random")
("disallow_shuffle")("description").convert_to_container<boost::container::flat_set<std::string> >();

team::team_info::team_info() :
Expand Down
1 change: 1 addition & 0 deletions src/tests/test_mp_connect.cpp
Expand Up @@ -112,6 +112,7 @@ static ng::side_engine* create_side_engine(const config& defaults,
{
config side_cfg = connect_engine->current_config()->child("side");
side_cfg.remove_attributes("faction");
side_cfg.clear_children("default_faction");
side_cfg.append(defaults);

return new ng::side_engine(side_cfg, *connect_engine, 0);
Expand Down

0 comments on commit f36c554

Please sign in to comment.