1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-10 22:31:40 +02:00

Fix calculation of new secondary skill level. Fixes bug with removal

This commit is contained in:
Ivan Savenko
2025-05-15 15:47:53 +03:00
parent 456525109e
commit cb82c6a4f6

View File

@@ -133,38 +133,33 @@ ui8 CGHeroInstance::getSecSkillLevel(const SecondarySkill & skill) const
return 0; return 0;
} }
void CGHeroInstance::setSecSkillLevel(const SecondarySkill & which, int val, bool abs) void CGHeroInstance::setSecSkillLevel(const SecondarySkill & which, int val, ChangeValueMode mode)
{ {
if (val == 0) // skill removal int currentLevel = getSecSkillLevel(which);
int newLevel = mode == ChangeValueMode::ABSOLUTE ? val : currentLevel + val;
int newLevelClamped = std::clamp<int>(newLevel, MasteryLevel::NONE, MasteryLevel::EXPERT);
if (currentLevel == newLevelClamped)
return; // no change
if (newLevelClamped == 0) // skill removal
{ {
vstd::erase_if(secSkills, [which](const std::pair<SecondarySkill, ui8>& pair) { return pair.first == which; }); vstd::erase_if(secSkills, [which](const std::pair<SecondarySkill, ui8>& pair) { return pair.first == which; });
updateSkillBonus(which, val);
} }
else if(getSecSkillLevel(which) == 0) else if(currentLevel == 0) // gained new skill
{ {
secSkills.emplace_back(which, val); secSkills.emplace_back(which, newLevelClamped);
updateSkillBonus(which, val);
} }
else else
{ {
for (auto & elem : secSkills) for (auto & elem : secSkills)
{ {
if(elem.first == which) if(elem.first == which)
{ elem.second = newLevelClamped;
if(abs) }
elem.second = val; }
else
elem.second += val;
if(elem.second > 3) //workaround to avoid crashes when same sec skill is given more than once updateSkillBonus(which, newLevelClamped);
{
logGlobal->warn("Skill %d increased over limit! Decreasing to Expert.", static_cast<int>(which.toEnum()));
elem.second = 3;
}
updateSkillBonus(which, elem.second); //when we know final value
}
}
}
} }
int3 CGHeroInstance::convertToVisitablePos(const int3 & position) const int3 CGHeroInstance::convertToVisitablePos(const int3 & position) const