1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-12-01 23:12:49 +02:00

Reworked and fixed selection of secondary skills:

- Fixed off-by-one error when checking for obligatory skills
- If both wisdom and magic school must be offered in the same slot, magic
school will be correctly offered on next levelup
- Obligatory skill can now be proposed for upgrade
- Obligatory skills are now offered using hero class weight instead of
simple random
- If hero has multiple skills not available to his class game will
select random skill instead of first one
- Moved storage of random seed to server instead of mutable member
This commit is contained in:
Ivan Savenko
2023-11-20 18:44:27 +02:00
parent ff6260e5c5
commit e9ac8c67c1
10 changed files with 106 additions and 154 deletions

View File

@@ -123,26 +123,30 @@ void CHero::serializeJson(JsonSerializeFormat & handler)
SecondarySkill CHeroClass::chooseSecSkill(const std::set<SecondarySkill> & possibles, CRandomGenerator & rand) const //picks secondary skill out from given possibilities
{
assert(!possibles.empty());
if (possibles.size() == 1)
return *possibles.begin();
int totalProb = 0;
for(const auto & possible : possibles)
if (secSkillProbability.count(possible) != 0)
totalProb += secSkillProbability.at(possible);
if (totalProb != 0) // may trigger if set contains only banned skills (0 probability)
{
auto ran = rand.nextInt(totalProb - 1);
for(const auto & possible : possibles)
{
if (secSkillProbability.count(possible) != 0)
ran -= secSkillProbability.at(possible);
if (totalProb == 0) // may trigger if set contains only banned skills (0 probability)
return *RandomGeneratorUtil::nextItem(possibles, rand);
if(ran < 0)
{
return possible;
}
}
auto ran = rand.nextInt(totalProb - 1);
for(const auto & possible : possibles)
{
if (secSkillProbability.count(possible) != 0)
ran -= secSkillProbability.at(possible);
if(ran < 0)
return possible;
}
// FIXME: select randomly? How H3 handles such rare situation?
assert(0); // should not be possible
return *possibles.begin();
}