1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

Fixed possible crash if hero class has no valid commander ID

This commit is contained in:
Ivan Savenko 2024-06-01 07:40:48 +00:00
parent 7e9c486a7a
commit b638b0b679
3 changed files with 5 additions and 6 deletions

View File

@ -218,8 +218,7 @@ void CHeroClass::serializeJson(JsonSerializeFormat & handler)
CHeroClass::CHeroClass():
faction(0),
affinity(0),
defaultTavernChance(0),
commander(nullptr)
defaultTavernChance(0)
{
}
@ -302,7 +301,7 @@ CHeroClass * CHeroClassHandler::loadFromJson(const std::string & scope, const Js
VLC->identifiers()->requestIdentifier ("creature", node["commander"],
[=](si32 commanderID)
{
heroClass->commander = CreatureID(commanderID).toCreature();
heroClass->commander = CreatureID(commanderID);
});
heroClass->defaultTavernChance = static_cast<ui32>(node["defaultTavern"].Float());

View File

@ -121,7 +121,7 @@ public:
// resulting chance = sqrt(town.chance * heroClass.chance)
ui32 defaultTavernChance;
const CCreature * commander;
CreatureID commander;
std::vector<int> primarySkillInitial; // initial primary skills
std::vector<int> primarySkillLowLevel; // probability (%) of getting point of primary skill when getting level

View File

@ -402,9 +402,9 @@ void CGHeroInstance::initHero(CRandomGenerator & rand)
addNewBonus(bonus);
}
if (VLC->settings()->getBoolean(EGameSettings::MODULE_COMMANDERS) && !commander)
if (VLC->settings()->getBoolean(EGameSettings::MODULE_COMMANDERS) && !commander && type->heroClass->commander.hasValue())
{
commander = new CCommanderInstance(type->heroClass->commander->getId());
commander = new CCommanderInstance(type->heroClass->commander);
commander->setArmyObj (castToArmyObj()); //TODO: separate function for setting commanders
commander->giveStackExp (exp); //after our exp is set
}