1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-06 09:09:40 +02:00

Use ArtifactInstanceID in CGArtifact

This commit is contained in:
Ivan Savenko
2025-03-18 20:38:12 +00:00
parent ab11d2b075
commit 16a06179cf
11 changed files with 53 additions and 39 deletions

View File

@@ -238,7 +238,7 @@ uint64_t RewardEvaluator::getArmyReward(
case Obj::SPELL_SCROLL:
//FALL_THROUGH
case Obj::ARTIFACT:
return evaluateArtifactArmyValue(dynamic_cast<const CGArtifact *>(target)->storedArtifact->getType());
return evaluateArtifactArmyValue(dynamic_cast<const CGArtifact *>(target)->getArtifactInstance()->getType());
case Obj::HERO:
return relations == PlayerRelations::ENEMIES
? enemyArmyEliminationRewardRatio * dynamic_cast<const CGHeroInstance *>(target)->getArmyStrength()

View File

@@ -92,7 +92,7 @@ std::optional<int> MapObjectsEvaluator::getObjectValue(const CGObjectInstance *
else if(obj->ID == Obj::ARTIFACT)
{
auto artifactObject = dynamic_cast<const CGArtifact *>(obj);
switch(artifactObject->storedArtifact->getType()->aClass)
switch(artifactObject->getArtifactInstance()->getType()->aClass)
{
case CArtifact::EartClass::ART_TREASURE:
return 2000;
@@ -111,7 +111,7 @@ std::optional<int> MapObjectsEvaluator::getObjectValue(const CGObjectInstance *
else if(obj->ID == Obj::SPELL_SCROLL)
{
auto scrollObject = dynamic_cast<const CGArtifact *>(obj);
auto spell = scrollObject->storedArtifact->getScrollSpellID().toSpell();
auto spell = scrollObject->getArtifactInstance()->getScrollSpellID().toSpell();
if(spell)
{
switch(spell->getLevel())
@@ -126,7 +126,7 @@ std::optional<int> MapObjectsEvaluator::getObjectValue(const CGObjectInstance *
}
}
else
logAi->warn("AI found spell scroll with invalid spell ID: %s", scrollObject->storedArtifact->getScrollSpellID());
logAi->warn("AI found spell scroll with invalid spell ID: %s", scrollObject->getArtifactInstance()->getScrollSpellID());
}
return getObjectValue(obj->ID, obj->subID);

View File

@@ -1764,7 +1764,7 @@ const CGObjectInstance * VCAI::lookForArt(ArtifactID aid) const
{
for(const CGObjectInstance * obj : ai->visitableObjs)
{
if(obj->ID == Obj::ARTIFACT && dynamic_cast<const CGArtifact *>(obj)->getArtifact() == aid)
if(obj->ID == Obj::ARTIFACT && dynamic_cast<const CGArtifact *>(obj)->getArtifactType() == aid)
return obj;
}

View File

@@ -610,7 +610,12 @@ bool CGWhirlpool::isProtected(const CGHeroInstance * h)
|| (h->stacksCount() == 0 && h->getCommander() && h->getCommander()->alive);
}
ArtifactID CGArtifact::getArtifact() const
const CArtifactInstance * CGArtifact::getArtifactInstance() const
{
return cb->getArtInstance(storedArtifact);
}
ArtifactID CGArtifact::getArtifactType() const
{
if(ID == Obj::SPELL_SCROLL)
return ArtifactID::SPELL_SCROLL;
@@ -648,37 +653,44 @@ void CGArtifact::pickRandomObject(vstd::RNG & rand)
ID = MapObjectID::ARTIFACT;
}
void CGArtifact::setArtifactInstance(const CArtifactInstance * instance)
{
storedArtifact = instance->getId();
}
void CGArtifact::initObj(vstd::RNG & rand)
{
blockVisit = true;
if(ID == Obj::ARTIFACT)
{
if (!storedArtifact)
storedArtifact = cb->gameState()->createArtifact(ArtifactID());
if (!storedArtifact.hasValue())
setArtifactInstance(cb->gameState()->createArtifact(ArtifactID()));
if(!storedArtifact->getType())
storedArtifact->setType(getArtifact().toArtifact());
auto * artifact = cb->gameState()->getArtInstance(storedArtifact);
if(!artifact->getType())
artifact->setType(getArtifactType().toArtifact());
}
if(ID == Obj::SPELL_SCROLL)
subID = 1;
assert(storedArtifact->getType());
assert(!storedArtifact->getParentNodes().empty());
assert(getArtifactInstance()->getType());
assert(!getArtifactInstance()->getParentNodes().empty());
//assert(storedArtifact->artType->id == subID); //this does not stop desync
}
std::string CGArtifact::getObjectName() const
{
return LIBRARY->artifacts()->getById(getArtifact())->getNameTranslated();
return getArtifactType().toEntity(LIBRARY)->getNameTranslated();
}
std::string CGArtifact::getPopupText(PlayerColor player) const
{
if (settings["general"]["enableUiEnhancements"].Bool())
{
std::string description = LIBRARY->artifacts()->getById(getArtifact())->getDescriptionTranslated();
if (getArtifact() == ArtifactID::SPELL_SCROLL)
std::string description = getArtifactType().toEntity(LIBRARY)->getDescriptionTranslated();
if (getArtifactType() == ArtifactID::SPELL_SCROLL)
ArtifactUtils::insertScrrollSpellName(description, SpellID::NONE); // erase text placeholder
return description;
}
@@ -694,7 +706,7 @@ std::string CGArtifact::getPopupText(const CGHeroInstance * hero) const
std::vector<Component> CGArtifact::getPopupComponents(PlayerColor player) const
{
return {
Component(ComponentType::ARTIFACT, getArtifact())
Component(ComponentType::ARTIFACT, getArtifactType())
};
}
@@ -706,22 +718,22 @@ void CGArtifact::onHeroVisit(const CGHeroInstance * h) const
iw.type = EInfoWindowMode::AUTO;
iw.player = h->tempOwner;
if(storedArtifact->getType()->canBePutAt(h))
if(getArtifactInstance()->getType()->canBePutAt(h))
{
switch (ID.toEnum())
{
case Obj::ARTIFACT:
{
iw.components.emplace_back(ComponentType::ARTIFACT, getArtifact());
iw.components.emplace_back(ComponentType::ARTIFACT, getArtifactType());
if(!message.empty())
iw.text = message;
else
iw.text.appendTextID(getArtifact().toArtifact()->getEventTextID());
iw.text.appendTextID(getArtifactType().toArtifact()->getEventTextID());
}
break;
case Obj::SPELL_SCROLL:
{
SpellID spell = storedArtifact->getScrollSpellID();
SpellID spell = getArtifactInstance()->getScrollSpellID();
iw.components.emplace_back(ComponentType::SPELL, spell);
if(!message.empty())
iw.text = message;
@@ -781,7 +793,7 @@ void CGArtifact::onHeroVisit(const CGHeroInstance * h) const
void CGArtifact::pick(const CGHeroInstance * h) const
{
if(cb->putArtifact(ArtifactLocation(h->id, ArtifactPosition::FIRST_AVAILABLE), storedArtifact->getId()))
if(cb->putArtifact(ArtifactLocation(h->id, ArtifactPosition::FIRST_AVAILABLE), getArtifactInstance()->getId()))
cb->removeObject(this, h->getOwner());
}
@@ -811,7 +823,7 @@ void CGArtifact::serializeJsonOptions(JsonSerializeFormat& handler)
if(handler.saving && ID == Obj::SPELL_SCROLL)
{
const auto & b = storedArtifact->getFirstBonus(Selector::type()(BonusType::SPELL));
const auto & b = getArtifactInstance()->getFirstBonus(Selector::type()(BonusType::SPELL));
SpellID spellId(b->subtype.as<SpellID>());
handler.serializeId("spell", spellId, SpellID::NONE);

View File

@@ -90,10 +90,10 @@ protected:
class DLL_LINKAGE CGArtifact : public CArmedInstance
{
ArtifactInstanceID storedArtifact;
public:
using CArmedInstance::CArmedInstance;
CArtifactInstance * storedArtifact = nullptr;
MetaString message;
void onHeroVisit(const CGHeroInstance * h) const override;
@@ -111,7 +111,9 @@ public:
BattleField getBattlefield() const override;
ArtifactID getArtifact() const;
ArtifactID getArtifactType() const;
const CArtifactInstance * getArtifactInstance() const;
void setArtifactInstance(const CArtifactInstance *);
template <typename Handler> void serialize(Handler &h)
{

View File

@@ -1406,7 +1406,7 @@ std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readArtifact(const int3 & mapPo
logGlobal->warn("Map '%s': Artifact %s: not implemented pickup mode %d (flags: %d)", mapName, mapPosition.toString(), pickupMode, static_cast<int>(pickupFlags));
}
object->storedArtifact = map->createArtifact(artID, SpellID::NONE);
object->setArtifactInstance(map->createArtifact(artID, SpellID::NONE));
return object;
}
@@ -1416,7 +1416,7 @@ std::shared_ptr<CGObjectInstance> CMapLoaderH3M::readScroll(const int3 & mapPosi
readMessageAndGuards(object->message, object.get(), mapPosition);
SpellID spellID = reader->readSpell32();
object->storedArtifact = map->createArtifact(ArtifactID::SPELL_SCROLL, spellID.getNum());
object->setArtifactInstance(map->createArtifact(ArtifactID::SPELL_SCROLL, spellID.getNum()));
return object;
}

View File

@@ -1089,10 +1089,10 @@ void CMapLoaderJson::MapObjectLoader::configure()
else if(art->ID == Obj::ARTIFACT)
{
//specific artifact
artID = art->getArtifact();
artID = art->getArtifactType();
}
art->storedArtifact = owner->map->createArtifact(artID, spellID.getNum());
art->setArtifactInstance(owner->map->createArtifact(artID, spellID.getNum()));
}
if(auto hero = std::dynamic_pointer_cast<CGHeroInstance>(instance))

View File

@@ -279,7 +279,7 @@ void TreasurePlacer::addScrolls()
out.push_back(spellID);
}
auto * a = map.mapInstance->createScroll(*RandomGeneratorUtil::nextItem(out, zone.getRand()));
obj->storedArtifact = a;
obj->setArtifactInstance(a);
return obj;
};
oi.setTemplates(Obj::SPELL_SCROLL, 0, zone.getTerrainType());

View File

@@ -206,7 +206,7 @@ void Initializer::initialize(CGArtifact * o)
}
}
auto a = controller.map()->createScroll(*RandomGeneratorUtil::nextItem(out, CRandomGenerator::getDefault()));
o->storedArtifact = a;
o->setArtifactInstance(a);
}
}
@@ -370,7 +370,7 @@ void Inspector::updateProperties(CGArtifact * o)
addProperty(QObject::tr("Message"), o->message, false);
CArtifactInstance * instance = o->storedArtifact;
const CArtifactInstance * instance = o->getArtifactInstance();
if(instance)
{
SpellID spellId = instance->getScrollSpellID();
@@ -640,9 +640,9 @@ void Inspector::setProperty(CGArtifact * o, const QString & key, const QVariant
o->message = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller.map(),
TextIdentifier("guards", o->instanceName, "message"), value.toString().toStdString()));
if(o->storedArtifact && key == QObject::tr("Spell"))
if(key == QObject::tr("Spell"))
{
o->storedArtifact = controller.map()->createScroll(SpellID(value.toInt()));
o->setArtifactInstance(controller.map()->createScroll(SpellID(value.toInt())));
}
}

View File

@@ -167,7 +167,7 @@ void MapController::repairMap(CMap * map)
//fix spell scrolls
if(auto * art = dynamic_cast<CGArtifact*>(obj))
{
if(art->ID == Obj::SPELL_SCROLL && !art->storedArtifact)
if(art->ID == Obj::SPELL_SCROLL && !art->getArtifactInstance())
{
std::vector<SpellID> out;
for(auto const & spell : LIBRARY->spellh->objects) //spellh size appears to be greater (?)
@@ -178,7 +178,7 @@ void MapController::repairMap(CMap * map)
}
}
auto a = map->createScroll(*RandomGeneratorUtil::nextItem(out, CRandomGenerator::getDefault()));
art->storedArtifact = a;
art->setArtifactInstance(a);
}
}
//fix mines

View File

@@ -137,17 +137,17 @@ std::set<Validator::Issue> Validator::validate(const CMap * map)
{
if(ins->ID == Obj::SPELL_SCROLL)
{
if (ins->storedArtifact)
if (ins->getArtifactInstance())
{
if (map->allowedSpells.count(ins->storedArtifact->getScrollSpellID()) == 0)
issues.insert({ tr("Spell scroll %1 is prohibited by map settings").arg(ins->storedArtifact->getScrollSpellID().toEntity(LIBRARY->spells())->getNameTranslated().c_str()), false });
if (map->allowedSpells.count(ins->getArtifactInstance()->getScrollSpellID()) == 0)
issues.insert({ tr("Spell scroll %1 is prohibited by map settings").arg(ins->getArtifactInstance()->getScrollSpellID().toEntity(LIBRARY->spells())->getNameTranslated().c_str()), false });
}
else
issues.insert({ tr("Spell scroll %1 doesn't have instance assigned and must be removed").arg(ins->instanceName.c_str()), true });
}
else
{
if(ins->ID == Obj::ARTIFACT && map->allowedArtifact.count(ins->getArtifact()) == 0)
if(ins->ID == Obj::ARTIFACT && map->allowedArtifact.count(ins->getArtifactType()) == 0)
{
issues.insert({ tr("Artifact %1 is prohibited by map settings").arg(ins->getObjectName().c_str()), false });
}