1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-06-15 00:05:02 +02:00

Scholar is now configurable object (partial)

This commit is contained in:
Ivan Savenko
2023-10-04 19:32:16 +03:00
parent ca368f606f
commit e10de0594e
10 changed files with 113 additions and 165 deletions

View File

@ -874,132 +874,6 @@ void CGSignBottle::serializeJsonOptions(JsonSerializeFormat& handler)
handler.serializeStruct("text", message);
}
void CGScholar::onHeroVisit( const CGHeroInstance * h ) const
{
EBonusType type = bonusType;
int bid = bonusID;
//check if the bonus if applicable, if not - give primary skill (always possible)
int ssl = h->getSecSkillLevel(SecondarySkill(bid)); //current sec skill level, used if bonusType == 1
if((type == SECONDARY_SKILL && ((ssl == 3) || (!ssl && !h->canLearnSkill()))) ////hero already has expert level in the skill or (don't know skill and doesn't have free slot)
|| (type == SPELL && !h->canLearnSpell(SpellID(bid).toSpell())))
{
type = PRIM_SKILL;
bid = CRandomGenerator::getDefault().nextInt(GameConstants::PRIMARY_SKILLS - 1);
}
InfoWindow iw;
iw.type = EInfoWindowMode::AUTO;
iw.player = h->getOwner();
iw.text.appendLocalString(EMetaText::ADVOB_TXT,115);
switch (type)
{
case PRIM_SKILL:
cb->changePrimSkill(h,static_cast<PrimarySkill>(bid),+1);
iw.components.emplace_back(Component::EComponentType::PRIM_SKILL, bid, +1, 0);
break;
case SECONDARY_SKILL:
cb->changeSecSkill(h,SecondarySkill(bid),+1);
iw.components.emplace_back(Component::EComponentType::SEC_SKILL, bid, ssl + 1, 0);
break;
case SPELL:
{
std::set<SpellID> hlp;
hlp.insert(SpellID(bid));
cb->changeSpells(h,true,hlp);
iw.components.emplace_back(Component::EComponentType::SPELL, bid, 0, 0);
}
break;
default:
logGlobal->error("Error: wrong bonus type (%d) for Scholar!\n", static_cast<int>(type));
return;
}
cb->showInfoDialog(&iw);
cb->removeObject(this, h->getOwner());
}
void CGScholar::initObj(CRandomGenerator & rand)
{
blockVisit = true;
if(bonusType == RANDOM)
{
bonusType = static_cast<EBonusType>(rand.nextInt(2));
switch(bonusType)
{
case PRIM_SKILL:
bonusID = rand.nextInt(GameConstants::PRIMARY_SKILLS -1);
break;
case SECONDARY_SKILL:
bonusID = rand.nextInt(static_cast<int>(VLC->skillh->size()) - 1);
break;
case SPELL:
std::vector<SpellID> possibilities;
cb->getAllowedSpells (possibilities);
bonusID = *RandomGeneratorUtil::nextItem(possibilities, rand);
break;
}
}
}
void CGScholar::serializeJsonOptions(JsonSerializeFormat & handler)
{
if(handler.saving)
{
std::string value;
switch(bonusType)
{
case PRIM_SKILL:
value = NPrimarySkill::names[bonusID];
handler.serializeString("rewardPrimSkill", value);
break;
case SECONDARY_SKILL:
value = CSkillHandler::encodeSkill(bonusID);
handler.serializeString("rewardSkill", value);
break;
case SPELL:
value = SpellID::encode(bonusID);
handler.serializeString("rewardSpell", value);
break;
case RANDOM:
break;
}
}
else
{
//TODO: unify
const JsonNode & json = handler.getCurrent();
bonusType = RANDOM;
if(!json["rewardPrimSkill"].String().empty())
{
auto raw = VLC->identifiers()->getIdentifier(ModScope::scopeBuiltin(), "primSkill", json["rewardPrimSkill"].String());
if(raw)
{
bonusType = PRIM_SKILL;
bonusID = raw.value();
}
}
else if(!json["rewardSkill"].String().empty())
{
auto raw = VLC->identifiers()->getIdentifier(ModScope::scopeBuiltin(), "skill", json["rewardSkill"].String());
if(raw)
{
bonusType = SECONDARY_SKILL;
bonusID = raw.value();
}
}
else if(!json["rewardSpell"].String().empty())
{
auto raw = VLC->identifiers()->getIdentifier(ModScope::scopeBuiltin(), "spell", json["rewardSpell"].String());
if(raw)
{
bonusType = SPELL;
bonusID = raw.value();
}
}
}
}
void CGGarrison::onHeroVisit (const CGHeroInstance *h) const
{
auto relations = cb->gameState()->getPlayerRelations(h->tempOwner, tempOwner);