1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-13 19:54:17 +02:00

Merge pull request #1175 from SoundSSGood/refactoring-art

Refactoring of artifacts code
This commit is contained in:
Andrii Danylchenko
2022-12-01 11:13:31 +02:00
committed by GitHub
14 changed files with 103 additions and 65 deletions

View File

@@ -1140,7 +1140,7 @@ HeroPtr AIGateway::getHeroWithGrail() const
{ {
for(const CGHeroInstance * h : cb->getHeroesInfo()) for(const CGHeroInstance * h : cb->getHeroesInfo())
{ {
if(h->hasArt(2)) //grail if(h->hasArt(ArtifactID::GRAIL))
return h; return h;
} }
return nullptr; return nullptr;

View File

@@ -1755,7 +1755,7 @@ HeroPtr VCAI::getHeroWithGrail() const
{ {
for(const CGHeroInstance * h : cb->getHeroesInfo()) for(const CGHeroInstance * h : cb->getHeroesInfo())
{ {
if(h->hasArt(2)) //grail if(h->hasArt(ArtifactID::GRAIL))
return h; return h;
} }
return nullptr; return nullptr;

View File

@@ -914,7 +914,7 @@ void CBattleInterface::bSpellf()
if (blockingBonus->source == Bonus::ARTIFACT) if (blockingBonus->source == Bonus::ARTIFACT)
{ {
const int32_t artID = blockingBonus->sid; const auto artID = ArtifactID(blockingBonus->sid);
//If we have artifact, put name of our hero. Otherwise assume it's the enemy. //If we have artifact, put name of our hero. Otherwise assume it's the enemy.
//TODO check who *really* is source of bonus //TODO check who *really* is source of bonus
std::string heroName = myHero->hasArt(artID) ? myHero->name : enemyHero().name; std::string heroName = myHero->hasArt(artID) ? myHero->name : enemyHero().name;

View File

@@ -155,10 +155,11 @@ std::string CComponent::getDescription()
case creature: return ""; case creature: return "";
case artifact: case artifact:
{ {
auto artID = ArtifactID(subtype);
std::unique_ptr<CArtifactInstance> art; std::unique_ptr<CArtifactInstance> art;
if (subtype != ArtifactID::SPELL_SCROLL) if (artID != ArtifactID::SPELL_SCROLL)
{ {
art.reset(CArtifactInstance::createNewArtifactInstance(subtype)); art.reset(CArtifactInstance::createNewArtifactInstance(artID));
} }
else else
{ {

View File

@@ -935,7 +935,7 @@ void CCastleBuildings::enterMagesGuild()
void CCastleBuildings::enterTownHall() void CCastleBuildings::enterTownHall()
{ {
if(town->visitingHero && town->visitingHero->hasArt(2) && if(town->visitingHero && town->visitingHero->hasArt(ArtifactID::GRAIL) &&
!vstd::contains(town->builtBuildings, BuildingID::GRAIL)) //hero has grail, but town does not have it !vstd::contains(town->builtBuildings, BuildingID::GRAIL)) //hero has grail, but town does not have it
{ {
if(!vstd::contains(town->forbiddenBuildings, BuildingID::GRAIL)) if(!vstd::contains(town->forbiddenBuildings, BuildingID::GRAIL))

View File

@@ -851,7 +851,7 @@ void CArtifactInstance::putAt(ArtifactLocation al)
assert(canBePutAt(al)); assert(canBePutAt(al));
al.getHolderArtSet()->setNewArtSlot(al.slot, this, false); al.getHolderArtSet()->setNewArtSlot(al.slot, this, false);
if(al.slot < GameConstants::BACKPACK_START) if(!ArtifactUtils::isSlotBackpack(al.slot))
al.getHolderNode()->attachTo(*this); al.getHolderNode()->attachTo(*this);
} }
@@ -859,7 +859,7 @@ void CArtifactInstance::removeFrom(ArtifactLocation al)
{ {
assert(al.getHolderArtSet()->getArt(al.slot) == this); assert(al.getHolderArtSet()->getArt(al.slot) == this);
al.getHolderArtSet()->eraseArtSlot(al.slot); al.getHolderArtSet()->eraseArtSlot(al.slot);
if(al.slot < GameConstants::BACKPACK_START) if(!ArtifactUtils::isSlotBackpack(al.slot))
al.getHolderNode()->detachFrom(*this); al.getHolderNode()->detachFrom(*this);
//TODO delete me? //TODO delete me?
@@ -938,12 +938,12 @@ CArtifactInstance * CArtifactInstance::createNewArtifactInstance(CArtifact *Art)
} }
} }
CArtifactInstance * CArtifactInstance::createNewArtifactInstance(int aid) CArtifactInstance * CArtifactInstance::createNewArtifactInstance(ArtifactID aid)
{ {
return createNewArtifactInstance(VLC->arth->objects[aid]); return createNewArtifactInstance(VLC->arth->objects[aid]);
} }
CArtifactInstance * CArtifactInstance::createArtifact(CMap * map, int aid, int spellID) CArtifactInstance * CArtifactInstance::createArtifact(CMap * map, ArtifactID aid, int spellID)
{ {
CArtifactInstance * a = nullptr; CArtifactInstance * a = nullptr;
if(aid >= 0) if(aid >= 0)
@@ -1003,7 +1003,7 @@ bool CCombinedArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactP
bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(artSet, slot, assumeDestRemoved); bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(artSet, slot, assumeDestRemoved);
if(!canMainArtifactBePlaced) if(!canMainArtifactBePlaced)
return false; //no is no... return false; //no is no...
if(slot >= GameConstants::BACKPACK_START) if(ArtifactUtils::isSlotBackpack(slot))
return true; //we can always remove combined art to the backapck return true; //we can always remove combined art to the backapck
@@ -1019,11 +1019,12 @@ bool CCombinedArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactP
} }
//we iterate over all active slots and check if constituents fits them //we iterate over all active slots and check if constituents fits them
for (int i = 0; i < GameConstants::BACKPACK_START; i++) for(const auto pos : ArtifactUtils::constituentWornSlots())
{ {
for(auto art = constituentsToBePlaced.begin(); art != constituentsToBePlaced.end(); art++) for(auto art = constituentsToBePlaced.begin(); art != constituentsToBePlaced.end(); art++)
{ {
if(art->art->canBePutAt(artSet, ArtifactPosition(i), i == slot)) // i == al.slot because we can remove already worn artifact only from that slot that is our main destination // pos == slot because we can remove already worn artifact only from that slot. That is our main destination
if(art->art->canBePutAt(artSet, pos, pos == slot))
{ {
constituentsToBePlaced.erase(art); constituentsToBePlaced.erase(art);
break; break;
@@ -1069,7 +1070,7 @@ void CCombinedArtifactInstance::addAsConstituent(CArtifactInstance *art, Artifac
void CCombinedArtifactInstance::putAt(ArtifactLocation al) void CCombinedArtifactInstance::putAt(ArtifactLocation al)
{ {
if(al.slot >= GameConstants::BACKPACK_START) if(ArtifactUtils::isSlotBackpack(al.slot))
{ {
CArtifactInstance::putAt(al); CArtifactInstance::putAt(al);
for(ConstituentInfo &ci : constituentsInfo) for(ConstituentInfo &ci : constituentsInfo)
@@ -1094,7 +1095,7 @@ void CCombinedArtifactInstance::putAt(ArtifactLocation al)
else else
ci.slot = pos = ci.art->firstAvailableSlot(al.getHolderArtSet()); ci.slot = pos = ci.art->firstAvailableSlot(al.getHolderArtSet());
assert(pos < GameConstants::BACKPACK_START); assert(!ArtifactUtils::isSlotBackpack(pos));
al.getHolderArtSet()->setNewArtSlot(pos, ci.art, true); //sets as lock al.getHolderArtSet()->setNewArtSlot(pos, ci.art, true); //sets as lock
} }
else else
@@ -1107,7 +1108,7 @@ void CCombinedArtifactInstance::putAt(ArtifactLocation al)
void CCombinedArtifactInstance::removeFrom(ArtifactLocation al) void CCombinedArtifactInstance::removeFrom(ArtifactLocation al)
{ {
if(al.slot >= GameConstants::BACKPACK_START) if(ArtifactUtils::isSlotBackpack(al.slot))
{ {
CArtifactInstance::removeFrom(al); CArtifactInstance::removeFrom(al);
} }
@@ -1199,19 +1200,19 @@ CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked
return const_cast<CArtifactInstance*>((const_cast<const CArtifactSet*>(this))->getArt(pos, excludeLocked)); return const_cast<CArtifactInstance*>((const_cast<const CArtifactSet*>(this))->getArt(pos, excludeLocked));
} }
ArtifactPosition CArtifactSet::getArtPos(int aid, bool onlyWorn, bool allowLocked) const ArtifactPosition CArtifactSet::getArtPos(ArtifactID aid, bool onlyWorn, bool allowLocked) const
{ {
const auto result = getAllArtPositions(aid, onlyWorn, allowLocked, false); const auto result = getAllArtPositions(aid, onlyWorn, allowLocked, false);
return result.empty() ? ArtifactPosition{ArtifactPosition::PRE_FIRST} : result[0]; return result.empty() ? ArtifactPosition{ArtifactPosition::PRE_FIRST} : result[0];
} }
ArtifactPosition CArtifactSet::getArtBackpackPos(int aid) const ArtifactPosition CArtifactSet::getArtBackpackPos(ArtifactID aid) const
{ {
const auto result = getBackpackArtPositions(aid); const auto result = getBackpackArtPositions(aid);
return result.empty() ? ArtifactPosition{ArtifactPosition::PRE_FIRST} : result[0]; return result.empty() ? ArtifactPosition{ArtifactPosition::PRE_FIRST} : result[0];
} }
std::vector<ArtifactPosition> CArtifactSet::getAllArtPositions(int aid, bool onlyWorn, bool allowLocked, bool getAll) const std::vector<ArtifactPosition> CArtifactSet::getAllArtPositions(ArtifactID aid, bool onlyWorn, bool allowLocked, bool getAll) const
{ {
std::vector<ArtifactPosition> result; std::vector<ArtifactPosition> result;
for(auto & slotInfo : artifactsWorn) for(auto & slotInfo : artifactsWorn)
@@ -1228,7 +1229,7 @@ std::vector<ArtifactPosition> CArtifactSet::getAllArtPositions(int aid, bool onl
return result; return result;
} }
std::vector<ArtifactPosition> CArtifactSet::getBackpackArtPositions(int aid) const std::vector<ArtifactPosition> CArtifactSet::getBackpackArtPositions(ArtifactID aid) const
{ {
std::vector<ArtifactPosition> result; std::vector<ArtifactPosition> result;
@@ -1270,7 +1271,7 @@ const CArtifactInstance * CArtifactSet::getArtByInstanceId( ArtifactInstanceID a
} }
bool CArtifactSet::hasArt( bool CArtifactSet::hasArt(
ui32 aid, ArtifactID aid,
bool onlyWorn, bool onlyWorn,
bool searchBackpackAssemblies, bool searchBackpackAssemblies,
bool allowLocked) const bool allowLocked) const
@@ -1278,12 +1279,12 @@ bool CArtifactSet::hasArt(
return getArtPosCount(aid, onlyWorn, searchBackpackAssemblies, allowLocked) > 0; return getArtPosCount(aid, onlyWorn, searchBackpackAssemblies, allowLocked) > 0;
} }
bool CArtifactSet::hasArtBackpack(ui32 aid) const bool CArtifactSet::hasArtBackpack(ArtifactID aid) const
{ {
return getBackpackArtPositions(aid).size() > 0; return getBackpackArtPositions(aid).size() > 0;
} }
unsigned CArtifactSet::getArtPosCount(int aid, bool onlyWorn, bool searchBackpackAssemblies, bool allowLocked) const unsigned CArtifactSet::getArtPosCount(ArtifactID aid, bool onlyWorn, bool searchBackpackAssemblies, bool allowLocked) const
{ {
const auto allPositions = getAllArtPositions(aid, onlyWorn, allowLocked, true); const auto allPositions = getAllArtPositions(aid, onlyWorn, allowLocked, true);
if(!allPositions.empty()) if(!allPositions.empty())
@@ -1296,7 +1297,7 @@ unsigned CArtifactSet::getArtPosCount(int aid, bool onlyWorn, bool searchBackpac
} }
std::pair<const CCombinedArtifactInstance *, const CArtifactInstance *> std::pair<const CCombinedArtifactInstance *, const CArtifactInstance *>
CArtifactSet::searchForConstituent(int aid) const CArtifactSet::searchForConstituent(ArtifactID aid) const
{ {
for(auto & slot : artifactsInBackpack) for(auto & slot : artifactsInBackpack)
{ {
@@ -1316,12 +1317,12 @@ CArtifactSet::searchForConstituent(int aid) const
return {nullptr, nullptr}; return {nullptr, nullptr};
} }
const CArtifactInstance *CArtifactSet::getHiddenArt(int aid) const const CArtifactInstance *CArtifactSet::getHiddenArt(ArtifactID aid) const
{ {
return searchForConstituent(aid).second; return searchForConstituent(aid).second;
} }
const CCombinedArtifactInstance *CArtifactSet::getAssemblyByConstituent(int aid) const const CCombinedArtifactInstance *CArtifactSet::getAssemblyByConstituent(ArtifactID aid) const
{ {
return searchForConstituent(aid).first; return searchForConstituent(aid).first;
} }
@@ -1353,7 +1354,7 @@ bool CArtifactSet::isPositionFree(ArtifactPosition pos, bool onlyLockCheck) cons
ArtSlotInfo & CArtifactSet::retrieveNewArtSlot(ArtifactPosition slot) ArtSlotInfo & CArtifactSet::retrieveNewArtSlot(ArtifactPosition slot)
{ {
assert(!vstd::contains(artifactsWorn, slot)); assert(!vstd::contains(artifactsWorn, slot));
ArtSlotInfo &ret = slot < GameConstants::BACKPACK_START ArtSlotInfo &ret = !ArtifactUtils::isSlotBackpack(slot)
? artifactsWorn[slot] ? artifactsWorn[slot]
: *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - GameConstants::BACKPACK_START), ArtSlotInfo()); : *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - GameConstants::BACKPACK_START), ArtSlotInfo());
@@ -1369,14 +1370,15 @@ void CArtifactSet::setNewArtSlot(ArtifactPosition slot, CArtifactInstance *art,
void CArtifactSet::eraseArtSlot(ArtifactPosition slot) void CArtifactSet::eraseArtSlot(ArtifactPosition slot)
{ {
if(slot < GameConstants::BACKPACK_START) if(ArtifactUtils::isSlotBackpack(slot))
{ {
artifactsWorn.erase(slot); assert(artifactsInBackpack.begin() + slot < artifactsInBackpack.end());
slot = ArtifactPosition(slot - GameConstants::BACKPACK_START);
artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
} }
else else
{ {
slot = ArtifactPosition(slot - GameConstants::BACKPACK_START); artifactsWorn.erase(slot);
artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
} }
} }
@@ -1543,16 +1545,41 @@ DLL_LINKAGE ArtifactPosition ArtifactUtils::getArtifactDstPosition( const CArtif
return ArtifactPosition(GameConstants::BACKPACK_START); return ArtifactPosition(GameConstants::BACKPACK_START);
} }
DLL_LINKAGE std::vector<ArtifactPosition> ArtifactUtils::unmovablePositions() DLL_LINKAGE const std::vector<ArtifactPosition::EArtifactPosition> & ArtifactUtils::unmovableSlots()
{ {
return { ArtifactPosition::SPELLBOOK, ArtifactPosition::MACH4 }; return
{
ArtifactPosition::SPELLBOOK,
ArtifactPosition::MACH4
};
}
DLL_LINKAGE const std::vector<ArtifactPosition::EArtifactPosition> & ArtifactUtils::constituentWornSlots()
{
return
{
ArtifactPosition::HEAD,
ArtifactPosition::SHOULDERS,
ArtifactPosition::NECK,
ArtifactPosition::RIGHT_HAND,
ArtifactPosition::LEFT_HAND,
ArtifactPosition::TORSO,
ArtifactPosition::RIGHT_RING,
ArtifactPosition::LEFT_RING,
ArtifactPosition::FEET,
ArtifactPosition::MISC1,
ArtifactPosition::MISC2,
ArtifactPosition::MISC3,
ArtifactPosition::MISC4,
ArtifactPosition::MISC5,
};
} }
DLL_LINKAGE bool ArtifactUtils::isArtRemovable(const std::pair<ArtifactPosition, ArtSlotInfo> & slot) DLL_LINKAGE bool ArtifactUtils::isArtRemovable(const std::pair<ArtifactPosition, ArtSlotInfo> & slot)
{ {
return slot.second.artifact return slot.second.artifact
&& !slot.second.locked && !slot.second.locked
&& !vstd::contains(unmovablePositions(), slot.first); && !vstd::contains(unmovableSlots(), slot.first);
} }
DLL_LINKAGE bool ArtifactUtils::checkSpellbookIsNeeded(const CGHeroInstance * heroPtr, ArtifactID artID, ArtifactPosition slot) DLL_LINKAGE bool ArtifactUtils::checkSpellbookIsNeeded(const CGHeroInstance * heroPtr, ArtifactID artID, ArtifactPosition slot)

View File

@@ -176,7 +176,7 @@ public:
static CArtifactInstance *createScroll(SpellID sid); static CArtifactInstance *createScroll(SpellID sid);
static CArtifactInstance *createNewArtifactInstance(CArtifact *Art); static CArtifactInstance *createNewArtifactInstance(CArtifact *Art);
static CArtifactInstance *createNewArtifactInstance(int aid); static CArtifactInstance *createNewArtifactInstance(ArtifactID aid);
/** /**
* Creates an artifact instance. * Creates an artifact instance.
@@ -185,7 +185,7 @@ public:
* @param spellID optional. the id of a spell if a spell scroll object should be created * @param spellID optional. the id of a spell if a spell scroll object should be created
* @return the created artifact instance * @return the created artifact instance
*/ */
static CArtifactInstance * createArtifact(CMap * map, int aid, int spellID = -1); static CArtifactInstance * createArtifact(CMap * map, ArtifactID aid, int spellID = -1);
}; };
class DLL_LINKAGE CCombinedArtifactInstance : public CArtifactInstance class DLL_LINKAGE CCombinedArtifactInstance : public CArtifactInstance
@@ -327,20 +327,20 @@ public:
CArtifactInstance* getArt(ArtifactPosition pos, bool excludeLocked = true); //nullptr - no artifact CArtifactInstance* getArt(ArtifactPosition pos, bool excludeLocked = true); //nullptr - no artifact
/// Looks for equipped artifact with given ID and returns its slot ID or -1 if none /// Looks for equipped artifact with given ID and returns its slot ID or -1 if none
/// (if more than one such artifact lower ID is returned) /// (if more than one such artifact lower ID is returned)
ArtifactPosition getArtPos(int aid, bool onlyWorn = true, bool allowLocked = true) const; ArtifactPosition getArtPos(ArtifactID aid, bool onlyWorn = true, bool allowLocked = true) const;
ArtifactPosition getArtPos(const CArtifactInstance *art) const; ArtifactPosition getArtPos(const CArtifactInstance *art) const;
ArtifactPosition getArtBackpackPos(int aid) const; ArtifactPosition getArtBackpackPos(ArtifactID aid) const;
std::vector<ArtifactPosition> getAllArtPositions(int aid, bool onlyWorn, bool allowLocked, bool getAll) const; std::vector<ArtifactPosition> getAllArtPositions(ArtifactID aid, bool onlyWorn, bool allowLocked, bool getAll) const;
std::vector<ArtifactPosition> getBackpackArtPositions(int aid) const; std::vector<ArtifactPosition> getBackpackArtPositions(ArtifactID aid) const;
const CArtifactInstance *getArtByInstanceId(ArtifactInstanceID artInstId) const; const CArtifactInstance *getArtByInstanceId(ArtifactInstanceID artInstId) const;
/// Search for constituents of assemblies in backpack which do not have an ArtifactPosition /// Search for constituents of assemblies in backpack which do not have an ArtifactPosition
const CArtifactInstance *getHiddenArt(int aid) const; const CArtifactInstance *getHiddenArt(ArtifactID aid) const;
const CCombinedArtifactInstance *getAssemblyByConstituent(int aid) const; const CCombinedArtifactInstance *getAssemblyByConstituent(ArtifactID aid) const;
/// Checks if hero possess artifact of given id (either in backack or worn) /// Checks if hero possess artifact of given id (either in backack or worn)
bool hasArt(ui32 aid, bool onlyWorn = false, bool searchBackpackAssemblies = false, bool allowLocked = true) const; bool hasArt(ArtifactID aid, bool onlyWorn = false, bool searchBackpackAssemblies = false, bool allowLocked = true) const;
bool hasArtBackpack(ui32 aid) const; bool hasArtBackpack(ArtifactID aid) const;
bool isPositionFree(ArtifactPosition pos, bool onlyLockCheck = false) const; bool isPositionFree(ArtifactPosition pos, bool onlyLockCheck = false) const;
unsigned getArtPosCount(int aid, bool onlyWorn = true, bool searchBackpackAssemblies = true, bool allowLocked = true) const; unsigned getArtPosCount(ArtifactID aid, bool onlyWorn = true, bool searchBackpackAssemblies = true, bool allowLocked = true) const;
virtual ArtBearer::ArtBearer bearerType() const = 0; virtual ArtBearer::ArtBearer bearerType() const = 0;
virtual void putArtifact(ArtifactPosition pos, CArtifactInstance * art) = 0; virtual void putArtifact(ArtifactPosition pos, CArtifactInstance * art) = 0;
@@ -358,7 +358,7 @@ public:
protected: protected:
std::pair<const CCombinedArtifactInstance *, const CArtifactInstance *> searchForConstituent(int aid) const; std::pair<const CCombinedArtifactInstance *, const CArtifactInstance *> searchForConstituent(ArtifactID aid) const;
private: private:
void serializeJsonHero(JsonSerializeFormat & handler, CMap * map); void serializeJsonHero(JsonSerializeFormat & handler, CMap * map);
void serializeJsonCreature(JsonSerializeFormat & handler, CMap * map); void serializeJsonCreature(JsonSerializeFormat & handler, CMap * map);
@@ -386,7 +386,9 @@ namespace ArtifactUtils
DLL_LINKAGE ArtifactPosition getArtifactDstPosition( const CArtifactInstance * artifact, DLL_LINKAGE ArtifactPosition getArtifactDstPosition( const CArtifactInstance * artifact,
const CArtifactSet * target, const CArtifactSet * target,
ArtBearer::ArtBearer bearer); ArtBearer::ArtBearer bearer);
DLL_LINKAGE std::vector<ArtifactPosition> unmovablePositions(); // TODO: Make this constexpr when the toolset is upgraded // TODO: Make this constexpr when the toolset is upgraded
DLL_LINKAGE const std::vector<ArtifactPosition::EArtifactPosition> & unmovableSlots();
DLL_LINKAGE const std::vector<ArtifactPosition::EArtifactPosition> & constituentWornSlots();
DLL_LINKAGE bool isArtRemovable(const std::pair<ArtifactPosition, ArtSlotInfo> & slot); DLL_LINKAGE bool isArtRemovable(const std::pair<ArtifactPosition, ArtSlotInfo> & slot);
DLL_LINKAGE bool checkSpellbookIsNeeded(const CGHeroInstance * heroPtr, ArtifactID artID, ArtifactPosition slot); DLL_LINKAGE bool checkSpellbookIsNeeded(const CGHeroInstance * heroPtr, ArtifactID artID, ArtifactPosition slot);
DLL_LINKAGE bool isSlotBackpack(ArtifactPosition slot); DLL_LINKAGE bool isSlotBackpack(ArtifactPosition slot);

View File

@@ -2256,7 +2256,7 @@ bool CGameState::checkForVictory(PlayerColor player, const EventCondition & cond
case EventCondition::HAVE_ARTIFACT: //check if any hero has winning artifact case EventCondition::HAVE_ARTIFACT: //check if any hero has winning artifact
{ {
for(auto & elem : p->heroes) for(auto & elem : p->heroes)
if(elem->hasArt(condition.objectType)) if(elem->hasArt(ArtifactID(condition.objectType)))
return true; return true;
return false; return false;
} }
@@ -2342,8 +2342,8 @@ bool CGameState::checkForVictory(PlayerColor player, const EventCondition & cond
case EventCondition::TRANSPORT: case EventCondition::TRANSPORT:
{ {
const CGTownInstance *t = static_cast<const CGTownInstance *>(condition.object); const CGTownInstance *t = static_cast<const CGTownInstance *>(condition.object);
if((t->visitingHero && t->visitingHero->hasArt(condition.objectType)) if((t->visitingHero && t->visitingHero->hasArt(ArtifactID(condition.objectType)))
|| (t->garrisonHero && t->garrisonHero->hasArt(condition.objectType))) || (t->garrisonHero && t->garrisonHero->hasArt(ArtifactID(condition.objectType))))
{ {
return true; return true;
} }

View File

@@ -1054,6 +1054,14 @@ public:
ID_LIKE_CLASS_COMMON(ArtifactID, EArtifactID) ID_LIKE_CLASS_COMMON(ArtifactID, EArtifactID)
EArtifactID num; EArtifactID num;
struct hash
{
size_t operator()(const ArtifactID & aid) const
{
return std::hash<int>()(aid.num);
}
};
}; };
ID_LIKE_OPERATORS(ArtifactID, ArtifactID::EArtifactID) ID_LIKE_OPERATORS(ArtifactID, ArtifactID::EArtifactID)

View File

@@ -422,7 +422,7 @@ void CQuest::getCompletionText(MetaString &iwText, std::vector<Component> &compo
} }
} }
void CQuest::addArtifactID(ui16 id) void CQuest::addArtifactID(ArtifactID id)
{ {
m5arts.push_back(id); m5arts.push_back(id);
++artifactsRequirements[id]; ++artifactsRequirements[id];
@@ -474,7 +474,7 @@ void CQuest::serializeJson(JsonSerializeFormat & handler, const std::string & fi
break; break;
case MISSION_ART: case MISSION_ART:
//todo: ban artifacts //todo: ban artifacts
handler.serializeIdArray<ui16, ArtifactID>("artifacts", m5arts); handler.serializeIdArray<ArtifactID>("artifacts", m5arts);
break; break;
case MISSION_ARMY: case MISSION_ARMY:
{ {

View File

@@ -21,7 +21,7 @@ class CGCreature;
class DLL_LINKAGE CQuest final class DLL_LINKAGE CQuest final
{ {
mutable std::unordered_map<ui16, unsigned> artifactsRequirements; // artifact ID -> required count mutable std::unordered_map<ArtifactID, unsigned, ArtifactID::hash> artifactsRequirements; // artifact ID -> required count
public: public:
enum Emission {MISSION_NONE = 0, MISSION_LEVEL = 1, MISSION_PRIMARY_STAT = 2, MISSION_KILL_HERO = 3, MISSION_KILL_CREATURE = 4, enum Emission {MISSION_NONE = 0, MISSION_LEVEL = 1, MISSION_PRIMARY_STAT = 2, MISSION_KILL_HERO = 3, MISSION_KILL_CREATURE = 4,
@@ -36,7 +36,7 @@ public:
ui32 m13489val; ui32 m13489val;
std::vector<ui32> m2stats; std::vector<ui32> m2stats;
std::vector<ui16> m5arts; // artifact IDs. Add IDs through addArtifactID(), not directly to the field. std::vector<ArtifactID> m5arts; // artifact IDs. Add IDs through addArtifactID(), not directly to the field.
std::vector<CStackBasicDescriptor> m6creatures; //pair[cre id, cre count], CreatureSet info irrelevant std::vector<CStackBasicDescriptor> m6creatures; //pair[cre id, cre count], CreatureSet info irrelevant
std::vector<ui32> m7resources; //TODO: use resourceset? std::vector<ui32> m7resources; //TODO: use resourceset?
@@ -62,7 +62,7 @@ public:
virtual void getRolloverText (MetaString &text, bool onHover) const; //hover or quest log entry virtual void getRolloverText (MetaString &text, bool onHover) const; //hover or quest log entry
virtual void completeQuest (const CGHeroInstance * h) const {}; virtual void completeQuest (const CGHeroInstance * h) const {};
virtual void addReplacements(MetaString &out, const std::string &base) const; virtual void addReplacements(MetaString &out, const std::string &base) const;
void addArtifactID(ui16 id); void addArtifactID(ArtifactID id);
bool operator== (const CQuest & quest) const bool operator== (const CQuest & quest) const
{ {

View File

@@ -870,15 +870,15 @@ void CMapLoaderH3M::loadArtifactsOfHero(CGHeroInstance * hero)
bool CMapLoaderH3M::loadArtifactToSlot(CGHeroInstance * hero, int slot) bool CMapLoaderH3M::loadArtifactToSlot(CGHeroInstance * hero, int slot)
{ {
const int artmask = map->version == EMapFormat::ROE ? 0xff : 0xffff; const int artmask = map->version == EMapFormat::ROE ? 0xff : 0xffff;
int aid; ArtifactID aid;
if(map->version == EMapFormat::ROE) if(map->version == EMapFormat::ROE)
{ {
aid = reader.readUInt8(); aid = ArtifactID(reader.readUInt8());
} }
else else
{ {
aid = reader.readUInt16(); aid = ArtifactID(reader.readUInt16());
} }
bool isArt = aid != artmask; bool isArt = aid != artmask;
@@ -1207,7 +1207,7 @@ void CMapLoaderH3M::readObjects()
case Obj::RANDOM_RELIC_ART: case Obj::RANDOM_RELIC_ART:
case Obj::SPELL_SCROLL: case Obj::SPELL_SCROLL:
{ {
int artID = ArtifactID::NONE; //random, set later auto artID = ArtifactID::NONE; //random, set later
int spellID = -1; int spellID = -1;
auto art = new CGArtifact(); auto art = new CGArtifact();
nobj = art; nobj = art;
@@ -1222,7 +1222,7 @@ void CMapLoaderH3M::readObjects()
else if(objTempl->id == Obj::ARTIFACT) else if(objTempl->id == Obj::ARTIFACT)
{ {
//specific artifact //specific artifact
artID = objTempl->subid; artID = ArtifactID(objTempl->subid);
} }
art->storedArtifact = CArtifactInstance::createArtifact(map, artID, spellID); art->storedArtifact = CArtifactInstance::createArtifact(map, artID, spellID);
@@ -1754,7 +1754,7 @@ CGSeerHut * CMapLoaderH3M::readSeerHut()
else else
{ {
//RoE //RoE
int artID = reader.readUInt8(); auto artID = ArtifactID(reader.readUInt8());
if (artID != 255) if (artID != 255)
{ {
//not none quest //not none quest
@@ -1886,7 +1886,7 @@ void CMapLoaderH3M::readQuest(IQuestObject * guard)
int artNumber = reader.readUInt8(); int artNumber = reader.readUInt8();
for(int yy = 0; yy < artNumber; ++yy) for(int yy = 0; yy < artNumber; ++yy)
{ {
int artid = reader.readUInt16(); auto artid = ArtifactID(reader.readUInt16());
guard->quest->addArtifactID(artid); guard->quest->addArtifactID(artid);
map->allowedArtifact[artid] = false; //these are unavailable for random generation map->allowedArtifact[artid] = false; //these are unavailable for random generation
} }

View File

@@ -1152,7 +1152,7 @@ void CMapLoaderJson::MapObjectLoader::configure()
if(auto art = dynamic_cast<CGArtifact *>(instance)) if(auto art = dynamic_cast<CGArtifact *>(instance))
{ {
int artID = ArtifactID::NONE; auto artID = ArtifactID::NONE;
int spellID = -1; int spellID = -1;
if(art->ID == Obj::SPELL_SCROLL) if(art->ID == Obj::SPELL_SCROLL)
@@ -1168,7 +1168,7 @@ void CMapLoaderJson::MapObjectLoader::configure()
else if(art->ID == Obj::ARTIFACT) else if(art->ID == Obj::ARTIFACT)
{ {
//specific artifact //specific artifact
artID = art->subID; artID = ArtifactID(art->subID);
} }
art->storedArtifact = CArtifactInstance::createArtifact(owner->map, artID, spellID); art->storedArtifact = CArtifactInstance::createArtifact(owner->map, artID, spellID);

View File

@@ -134,7 +134,7 @@ QString QuestWidget::commitChanges()
return QString("N/A"); return QString("N/A");
case CQuest::Emission::MISSION_ART: case CQuest::Emission::MISSION_ART:
seerhut.quest->m5arts.clear(); seerhut.quest->m5arts.clear();
seerhut.quest->m5arts.push_back(ui->targetId->currentIndex()); seerhut.quest->m5arts.push_back(ArtifactID(ui->targetId->currentIndex()));
//TODO: support multiple artifacts //TODO: support multiple artifacts
return ui->targetId->currentText(); return ui->targetId->currentText();
case CQuest::Emission::MISSION_ARMY: case CQuest::Emission::MISSION_ARMY: