void flg_manager::set_current_faction(const unsigned index) { assert(index < choosable_factions_.size()); current_faction_ = choosable_factions_[index]; update_available_leaders(); set_current_leader(0); }
void flg_manager::resolve_random() { if (is_random_faction()) { std::vector<std::string> faction_choices, faction_excepts; faction_choices = utils::split((*current_faction_)["choices"]); if (faction_choices.size() == 1 && faction_choices.front() == "") { faction_choices.clear(); } faction_excepts = utils::split((*current_faction_)["except"]); if (faction_excepts.size() == 1 && faction_excepts.front() == "") { faction_excepts.clear(); } // Builds the list of factions eligible for choice // (non-random factions). std::vector<int> nonrandom_sides; int num = -1; BOOST_FOREACH(const config* i, available_factions_) { ++num; if (!(*i)["random_faction"].to_bool()) { const std::string& faction_id = (*i)["id"]; if (!faction_choices.empty() && std::find(faction_choices.begin(), faction_choices.end(), faction_id) == faction_choices.end()) { continue; } if (!faction_excepts.empty() && std::find(faction_excepts.begin(), faction_excepts.end(), faction_id) != faction_excepts.end()) { continue; } nonrandom_sides.push_back(num); } } if (nonrandom_sides.empty()) { throw config::error(_("Only random sides in the current era.")); } const int faction_index = nonrandom_sides[rand() % nonrandom_sides.size()]; current_faction_ = available_factions_[faction_index]; update_available_leaders(); set_current_leader(0); }
// When we use a random mode like "no mirror", "no ally mirror", the list of // faction ids to avoid is past as an argument. // It may be that for some scenario configuration, a strict no mirror // assignment is not possible, because there are too many sides, or some users // have forced their faction choices to be matching, etc. // In that case we gracefully continue by ignoring the no mirror rule and // assigning as we would have if it were off. // If there is still no options we throw a config error because it means the // era is misconfigured. void flg_manager::resolve_random(rand_rng::mt_rng & rng, const std::vector<std::string> & avoid) { if (is_random_faction()) { std::vector<std::string> faction_choices, faction_excepts; faction_choices = utils::split((*current_faction_)["choices"]); if (faction_choices.size() == 1 && faction_choices.front() == "") { faction_choices.clear(); } faction_excepts = utils::split((*current_faction_)["except"]); if (faction_excepts.size() == 1 && faction_excepts.front() == "") { faction_excepts.clear(); } // Builds the list of factions eligible for choice // (non-random factions). std::vector<int> nonrandom_sides; std::vector<int> fallback_nonrandom_sides; int num = -1; BOOST_FOREACH(const config* i, available_factions_) { ++num; if (!(*i)["random_faction"].to_bool()) { const std::string& faction_id = (*i)["id"]; if (!faction_choices.empty() && std::find(faction_choices.begin(), faction_choices.end(), faction_id) == faction_choices.end()) { continue; } if (!faction_excepts.empty() && std::find(faction_excepts.begin(), faction_excepts.end(), faction_id) != faction_excepts.end()) { continue; } fallback_nonrandom_sides.push_back(num); //This side is consistent with this random faction, remember as a fallback. if (!avoid.empty() && std::find(avoid.begin(), avoid.end(), faction_id) != avoid.end()) { continue; } nonrandom_sides.push_back(num); //This side is consistent with this random faction, and the avoid factions argument. } } if (nonrandom_sides.empty()) { nonrandom_sides = fallback_nonrandom_sides; // There was no way to succeed consistently with the avoid factions argument, so ignore it as a fallback. } if (nonrandom_sides.empty()) { throw config::error(_("Only random sides in the current era.")); } const int faction_index = nonrandom_sides[rng.get_next_random() % nonrandom_sides.size()]; current_faction_ = available_factions_[faction_index]; update_available_leaders(); set_current_leader(0); }
void set_current_leader(const std::string& leader) { set_current_leader(leader_index(leader)); }