Permalink
Browse files

Don't go out of bounds when picking a race for a mission

  • Loading branch information...
SupSuper committed Dec 1, 2018
1 parent efa947e commit 70aace375f492d8360260914f82b4936469aec8c
Showing with 9 additions and 3 deletions.
  1. +5 −0 src/Geoscape/GeoscapeState.cpp
  2. +4 −3 src/Mod/RuleAlienMission.cpp
@@ -2643,6 +2643,11 @@ bool GeoscapeState::processCommand(RuleMissionScript *command)
missionRace = command->generate(month, GEN_RACE);
}
if (missionRace.empty())
{
throw Exception("Error proccessing mission script named: " + command->getType() + ", mission type: " + missionType + " has no available races");
}
// we're bound to end up with typos, so let's throw an exception instead of simply returning false
// that way, the modder can fix their mistake
if (mod->getAlienRace(missionRace) == 0)
@@ -142,9 +142,10 @@ void RuleAlienMission::load(const YAML::Node &node)
*/
std::string RuleAlienMission::generateRace(const size_t monthsPassed) const
{
std::vector<std::pair<size_t, WeightedOptions*> >::const_reverse_iterator rc = _raceDistribution.rbegin();
while (monthsPassed < rc->first)
++rc;
std::vector<std::pair<size_t, WeightedOptions*> >::const_reverse_iterator rc;
for (rc = _raceDistribution.rbegin(); rc != _raceDistribution.rend() && monthsPassed < rc->first; ++rc);
if (rc == _raceDistribution.rend())
return "";
return rc->second->choose();
}

0 comments on commit 70aace3

Please sign in to comment.