1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-04-25 12:14:46 +02:00

- fixed bug where having towns with less than 16 heroes would result in

invalid random heroes on map start.
This commit is contained in:
Ivan Savenko 2013-11-12 10:09:55 +00:00
parent 29d655c621
commit f56cccb716

View File

@ -463,35 +463,24 @@ int CGameState::pickHero(PlayerColor owner)
} }
} }
//list of heroes for this faction and others //list of available heroes for this faction and others
std::vector<HeroTypeID> factionHeroes, otherHeroes; std::vector<HeroTypeID> factionHeroes, otherHeroes;
const size_t firstHero = ps.castle*GameConstants::HEROES_PER_TYPE*2; for(HeroTypeID hid : getUnusedAllowedHeroes())
const size_t lastHero = std::min(firstHero + GameConstants::HEROES_PER_TYPE*2, VLC->heroh->heroes.size()) - 1;
const auto heroesToConsider = getUnusedAllowedHeroes();
for(auto hid : heroesToConsider)
{ {
if(vstd::iswithin(hid.getNum(), firstHero, lastHero)) if(VLC->heroh->heroes[hid.getNum()]->heroClass->faction == ps.castle)
factionHeroes.push_back(hid); factionHeroes.push_back(hid);
else else
otherHeroes.push_back(hid); otherHeroes.push_back(hid);
} }
// we need random order to select hero // select random hero native to "our" faction
auto randGen = [](size_t range) if (!factionHeroes.empty())
{ return factionHeroes.at(ran() % factionHeroes.size()).getNum();
return ran() % range;
};
boost::random_shuffle(factionHeroes, randGen); // generator must be reference
if(factionHeroes.size())
return factionHeroes.front().getNum();
logGlobal->warnStream() << "Cannot find free hero of appropriate faction for player " << owner << " - trying to get first available..."; logGlobal->warnStream() << "Cannot find free hero of appropriate faction for player " << owner << " - trying to get first available...";
if(otherHeroes.size()) if(!otherHeroes.empty())
return otherHeroes.front().getNum(); return otherHeroes.at(ran() % otherHeroes.size()).getNum();
logGlobal->errorStream() << "No free allowed heroes!"; logGlobal->errorStream() << "No free allowed heroes!";
auto notAllowedHeroesButStillBetterThanCrash = getUnusedAllowedHeroes(true); auto notAllowedHeroesButStillBetterThanCrash = getUnusedAllowedHeroes(true);
@ -503,7 +492,6 @@ int CGameState::pickHero(PlayerColor owner)
return -1; // no available heroes at all return -1; // no available heroes at all
} }
std::pair<Obj,int> CGameState::pickObject (CGObjectInstance *obj) std::pair<Obj,int> CGameState::pickObject (CGObjectInstance *obj)
{ {
switch(obj->ID) switch(obj->ID)