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

added toEntity overload that accepts generic Services class

This commit is contained in:
Ivan Savenko 2023-11-04 17:04:53 +02:00
parent 04aeea9b68
commit 5487f07d3b
11 changed files with 43 additions and 23 deletions

View File

@ -1944,7 +1944,7 @@ CBlacksmithDialog::CBlacksmithDialog(bool possible, CreatureID creMachineID, Art
cancelText.appendTextID("core.genrltxt.596"); cancelText.appendTextID("core.genrltxt.596");
cancelText.replaceTextID(creature->getNameSingularTextID()); cancelText.replaceTextID(creature->getNameSingularTextID());
std::string costString = std::to_string(aid.toEntity(CGI->artifacts())->getPrice()); std::string costString = std::to_string(aid.toEntity(CGI)->getPrice());
title = std::make_shared<CLabel>(165, 28, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, titleString.toString()); title = std::make_shared<CLabel>(165, 28, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, titleString.toString());
costText = std::make_shared<CLabel>(165, 218, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->jktexts[43]); costText = std::make_shared<CLabel>(165, 218, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->jktexts[43]);

View File

@ -114,35 +114,35 @@ std::string MetaString::getLocalString(const std::pair<EMetaText, ui32> & txt) c
{ {
case EMetaText::ART_NAMES: case EMetaText::ART_NAMES:
{ {
const auto * art = ArtifactID(ser).toEntity(VLC->artifacts()); const auto * art = ArtifactID(ser).toEntity(VLC);
if(art) if(art)
return art->getNameTranslated(); return art->getNameTranslated();
return "#!#"; return "#!#";
} }
case EMetaText::ART_DESCR: case EMetaText::ART_DESCR:
{ {
const auto * art = ArtifactID(ser).toEntity(VLC->artifacts()); const auto * art = ArtifactID(ser).toEntity(VLC);
if(art) if(art)
return art->getDescriptionTranslated(); return art->getDescriptionTranslated();
return "#!#"; return "#!#";
} }
case EMetaText::ART_EVNTS: case EMetaText::ART_EVNTS:
{ {
const auto * art = ArtifactID(ser).toEntity(VLC->artifacts()); const auto * art = ArtifactID(ser).toEntity(VLC);
if(art) if(art)
return art->getEventTranslated(); return art->getEventTranslated();
return "#!#"; return "#!#";
} }
case EMetaText::CRE_PL_NAMES: case EMetaText::CRE_PL_NAMES:
{ {
const auto * cre = CreatureID(ser).toEntity(VLC->creatures()); const auto * cre = CreatureID(ser).toEntity(VLC);
if(cre) if(cre)
return cre->getNamePluralTranslated(); return cre->getNamePluralTranslated();
return "#!#"; return "#!#";
} }
case EMetaText::CRE_SING_NAMES: case EMetaText::CRE_SING_NAMES:
{ {
const auto * cre = CreatureID(ser).toEntity(VLC->creatures()); const auto * cre = CreatureID(ser).toEntity(VLC);
if(cre) if(cre)
return cre->getNameSingularTranslated(); return cre->getNameSingularTranslated();
return "#!#"; return "#!#";
@ -157,7 +157,7 @@ std::string MetaString::getLocalString(const std::pair<EMetaText, ui32> & txt) c
} }
case EMetaText::SPELL_NAME: case EMetaText::SPELL_NAME:
{ {
const auto * spell = SpellID(ser).toEntity(VLC->spells()); const auto * spell = SpellID(ser).toEntity(VLC);
if(spell) if(spell)
return spell->getNameTranslated(); return spell->getNameTranslated();
return "#!#"; return "#!#";

View File

@ -100,13 +100,13 @@ std::string Bonus::Description(std::optional<si32> customValue) const
switch(source) switch(source)
{ {
case BonusSource::ARTIFACT: case BonusSource::ARTIFACT:
str << sid.as<ArtifactID>().toEntity(VLC->artifacts())->getNameTranslated(); str << sid.as<ArtifactID>().toEntity(VLC)->getNameTranslated();
break; break;
case BonusSource::SPELL_EFFECT: case BonusSource::SPELL_EFFECT:
str << sid.as<SpellID>().toEntity(VLC->spells())->getNameTranslated(); str << sid.as<SpellID>().toEntity(VLC)->getNameTranslated();
break; break;
case BonusSource::CREATURE_ABILITY: case BonusSource::CREATURE_ABILITY:
str << sid.as<CreatureID>().toEntity(VLC->creatures())->getNamePluralTranslated(); str << sid.as<CreatureID>().toEntity(VLC)->getNamePluralTranslated();
break; break;
case BonusSource::SECONDARY_SKILL: case BonusSource::SECONDARY_SKILL:
str << VLC->skills()->getById(sid.as<SecondarySkill>())->getNameTranslated(); str << VLC->skills()->getById(sid.as<SecondarySkill>())->getNameTranslated();

View File

@ -21,6 +21,7 @@
#include <vcmi/HeroTypeService.h> #include <vcmi/HeroTypeService.h>
#include <vcmi/HeroClass.h> #include <vcmi/HeroClass.h>
#include <vcmi/HeroClassService.h> #include <vcmi/HeroClassService.h>
#include <vcmi/Services.h>
#include <vcmi/spells/Spell.h> #include <vcmi/spells/Spell.h>
#include <vcmi/spells/Service.h> #include <vcmi/spells/Service.h>
@ -192,12 +193,12 @@ std::string HeroTypeID::entityType()
const CArtifact * ArtifactIDBase::toArtifact() const const CArtifact * ArtifactIDBase::toArtifact() const
{ {
return dynamic_cast<const CArtifact*>(toEntity(VLC->artifacts())); return dynamic_cast<const CArtifact*>(toEntity(VLC));
} }
const Artifact * ArtifactIDBase::toEntity(const ArtifactService * service) const const Artifact * ArtifactIDBase::toEntity(const Services * services) const
{ {
return service->getByIndex(num); return services->artifacts()->getByIndex(num);
} }
si32 ArtifactID::decode(const std::string & identifier) si32 ArtifactID::decode(const std::string & identifier)
@ -238,6 +239,11 @@ const CCreature * CreatureIDBase::toCreature() const
return VLC->creh->objects.at(num); return VLC->creh->objects.at(num);
} }
const Creature * CreatureIDBase::toEntity(const Services * services) const
{
return toEntity(services->creatures());
}
const Creature * CreatureIDBase::toEntity(const CreatureService * creatures) const const Creature * CreatureIDBase::toEntity(const CreatureService * creatures) const
{ {
return creatures->getByIndex(num); return creatures->getByIndex(num);
@ -272,6 +278,11 @@ const CSpell * SpellIDBase::toSpell() const
return VLC->spellh->objects[num]; return VLC->spellh->objects[num];
} }
const spells::Spell * SpellIDBase::toEntity(const Services * services) const
{
return toEntity(services->spells());
}
const spells::Spell * SpellIDBase::toEntity(const spells::Service * service) const const spells::Spell * SpellIDBase::toEntity(const spells::Service * service) const
{ {
return service->getByIndex(num); return service->getByIndex(num);
@ -279,12 +290,12 @@ const spells::Spell * SpellIDBase::toEntity(const spells::Service * service) con
const CHero * HeroTypeID::toHeroType() const const CHero * HeroTypeID::toHeroType() const
{ {
return dynamic_cast<const CHero*>(toEntity(VLC->heroTypes())); return dynamic_cast<const CHero*>(toEntity(VLC));
} }
const HeroType * HeroTypeID::toEntity(const HeroTypeService * service) const const HeroType * HeroTypeID::toEntity(const Services * services) const
{ {
return service->getByIndex(num); return services->heroTypes()->getByIndex(num);
} }
si32 SpellID::decode(const std::string & identifier) si32 SpellID::decode(const std::string & identifier)

View File

@ -14,6 +14,7 @@
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
class Services;
class Artifact; class Artifact;
class ArtifactService; class ArtifactService;
class Creature; class Creature;
@ -190,7 +191,7 @@ public:
static std::string entityType(); static std::string entityType();
const CHero * toHeroType() const; const CHero * toHeroType() const;
const HeroType * toEntity(const HeroTypeService * service) const; const HeroType * toEntity(const Services * services) const;
DLL_LINKAGE static const HeroTypeID NONE; DLL_LINKAGE static const HeroTypeID NONE;
DLL_LINKAGE static const HeroTypeID RANDOM; DLL_LINKAGE static const HeroTypeID RANDOM;
@ -681,7 +682,7 @@ public:
}; };
DLL_LINKAGE const CArtifact * toArtifact() const; DLL_LINKAGE const CArtifact * toArtifact() const;
DLL_LINKAGE const Artifact * toEntity(const ArtifactService * service) const; DLL_LINKAGE const Artifact * toEntity(const Services * service) const;
}; };
class ArtifactID : public IdentifierWithEnum<ArtifactID, ArtifactIDBase> class ArtifactID : public IdentifierWithEnum<ArtifactID, ArtifactIDBase>
@ -721,6 +722,7 @@ public:
}; };
DLL_LINKAGE const CCreature * toCreature() const; DLL_LINKAGE const CCreature * toCreature() const;
DLL_LINKAGE const Creature * toEntity(const Services * services) const;
DLL_LINKAGE const Creature * toEntity(const CreatureService * creatures) const; DLL_LINKAGE const Creature * toEntity(const CreatureService * creatures) const;
}; };
@ -839,6 +841,7 @@ public:
}; };
const CSpell * toSpell() const; //deprecated const CSpell * toSpell() const; //deprecated
const spells::Spell * toEntity(const Services * service) const;
const spells::Spell * toEntity(const spells::Service * service) const; const spells::Spell * toEntity(const spells::Service * service) const;
}; };

View File

@ -760,7 +760,7 @@ void CGameState::initStartingBonus()
logGlobal->error("Cannot give starting artifact - no heroes!"); logGlobal->error("Cannot give starting artifact - no heroes!");
break; break;
} }
const Artifact * toGive = VLC->arth->pickRandomArtifact(getRandomGenerator(), CArtifact::ART_TREASURE).toEntity(VLC->artifacts()); const Artifact * toGive = VLC->arth->pickRandomArtifact(getRandomGenerator(), CArtifact::ART_TREASURE).toEntity(VLC);
CGHeroInstance *hero = elem.second.heroes[0]; CGHeroInstance *hero = elem.second.heroes[0];
if(!giveHeroArtifact(hero, toGive->getId())) if(!giveHeroArtifact(hero, toGive->getId()))

View File

@ -73,7 +73,7 @@ void CArmedInstance::updateMoraleBonusFromArmy()
for(const auto & slot : Slots()) for(const auto & slot : Slots())
{ {
const CStackInstance * inst = slot.second; const CStackInstance * inst = slot.second;
const auto * creature = inst->getCreatureID().toEntity(VLC->creatures()); const auto * creature = inst->getCreatureID().toEntity(VLC);
factions.insert(creature->getFaction()); factions.insert(creature->getFaction());
// Check for undead flag instead of faction (undead mummies are neutral) // Check for undead flag instead of faction (undead mummies are neutral)

View File

@ -314,7 +314,7 @@ void CBank::doVisit(const CGHeroInstance * hero) const
} }
for(const SpellID & spellId : bc->spells) for(const SpellID & spellId : bc->spells)
{ {
const auto * spell = spellId.toEntity(VLC->spells()); const auto * spell = spellId.toEntity(VLC);
iw.text.appendLocalString(EMetaText::SPELL_NAME, spellId); iw.text.appendLocalString(EMetaText::SPELL_NAME, spellId);
if(spell->getLevel() <= hero->maxSpellLevel()) if(spell->getLevel() <= hero->maxSpellLevel())
{ {

View File

@ -926,7 +926,7 @@ bool CMapLoaderH3M::loadArtifactToSlot(CGHeroInstance * hero, int slot)
if(artifactID == ArtifactID::NONE) if(artifactID == ArtifactID::NONE)
return false; return false;
const Artifact * art = artifactID.toEntity(VLC->artifacts()); const Artifact * art = artifactID.toEntity(VLC);
if(!art) if(!art)
{ {

View File

@ -128,7 +128,7 @@ bool Rewardable::Limiter::heroAllowed(const CGHeroInstance * hero) const
for(const auto & spell : canLearnSpells) for(const auto & spell : canLearnSpells)
{ {
if (!hero->canLearnSpell(spell.toEntity(VLC->spells()), true)) if (!hero->canLearnSpell(spell.toEntity(VLC), true))
return false; return false;
} }

View File

@ -23,6 +23,7 @@ VCMI_LIB_NAMESPACE_BEGIN
struct Query; struct Query;
class IBattleState; class IBattleState;
class CRandomGenerator; class CRandomGenerator;
class CreatureService;
class CMap; class CMap;
class CGameInfoCallback; class CGameInfoCallback;
class CBattleInfoCallback; class CBattleInfoCallback;
@ -32,6 +33,11 @@ class CStack;
class CGObjectInstance; class CGObjectInstance;
class CGHeroInstance; class CGHeroInstance;
namespace spells
{
class Service;
}
namespace vstd namespace vstd
{ {
class RNG; class RNG;