Skip to content

Commit

Permalink
Don't go out of bounds when picking a race for a mission
Browse files Browse the repository at this point in the history
  • Loading branch information
SupSuper committed Dec 1, 2018
1 parent efa947e commit 70aace3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/Geoscape/GeoscapeState.cpp
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions src/Mod/RuleAlienMission.cpp
Expand Up @@ -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();
}

Expand Down

0 comments on commit 70aace3

Please sign in to comment.