浏览代码

CArtHandler refactoring

SoundSSGood 2 年之前
父节点
当前提交
8a6052fd54
共有 2 个文件被更改,包括 46 次插入21 次删除
  1. 44 20
      lib/CArtHandler.cpp
  2. 2 1
      lib/CArtHandler.h

+ 44 - 20
lib/CArtHandler.cpp

@@ -851,7 +851,7 @@ void CArtifactInstance::putAt(ArtifactLocation al)
 	assert(canBePutAt(al));
 
 	al.getHolderArtSet()->setNewArtSlot(al.slot, this, false);
-	if(al.slot < GameConstants::BACKPACK_START)
+	if(!ArtifactUtils::isSlotBackpack(al.slot))
 		al.getHolderNode()->attachTo(*this);
 }
 
@@ -859,7 +859,7 @@ void CArtifactInstance::removeFrom(ArtifactLocation al)
 {
 	assert(al.getHolderArtSet()->getArt(al.slot) == this);
 	al.getHolderArtSet()->eraseArtSlot(al.slot);
-	if(al.slot < GameConstants::BACKPACK_START)
+	if(!ArtifactUtils::isSlotBackpack(al.slot))
 		al.getHolderNode()->detachFrom(*this);
 
 	//TODO delete me?
@@ -1003,7 +1003,7 @@ bool CCombinedArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactP
 	bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(artSet, slot, assumeDestRemoved);
 	if(!canMainArtifactBePlaced)
 		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
 
 
@@ -1019,11 +1019,12 @@ bool CCombinedArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactP
 	}
 
 	//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++)
 		{
-			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);
 				break;
@@ -1069,7 +1070,7 @@ void CCombinedArtifactInstance::addAsConstituent(CArtifactInstance *art, Artifac
 
 void CCombinedArtifactInstance::putAt(ArtifactLocation al)
 {
-	if(al.slot >= GameConstants::BACKPACK_START)
+	if(ArtifactUtils::isSlotBackpack(al.slot))
 	{
 		CArtifactInstance::putAt(al);
 		for(ConstituentInfo &ci : constituentsInfo)
@@ -1094,7 +1095,7 @@ void CCombinedArtifactInstance::putAt(ArtifactLocation al)
 				else
 					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
 			}
 			else
@@ -1107,7 +1108,7 @@ void CCombinedArtifactInstance::putAt(ArtifactLocation al)
 
 void CCombinedArtifactInstance::removeFrom(ArtifactLocation al)
 {
-	if(al.slot >= GameConstants::BACKPACK_START)
+	if(ArtifactUtils::isSlotBackpack(al.slot))
 	{
 		CArtifactInstance::removeFrom(al);
 	}
@@ -1353,7 +1354,7 @@ bool CArtifactSet::isPositionFree(ArtifactPosition pos, bool onlyLockCheck) cons
 ArtSlotInfo & CArtifactSet::retrieveNewArtSlot(ArtifactPosition slot)
 {
 	assert(!vstd::contains(artifactsWorn, slot));
-	ArtSlotInfo &ret = slot < GameConstants::BACKPACK_START
+	ArtSlotInfo &ret = !ArtifactUtils::isSlotBackpack(slot)
 		? artifactsWorn[slot]
 		: *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - GameConstants::BACKPACK_START), ArtSlotInfo());
 
@@ -1369,14 +1370,14 @@ void CArtifactSet::setNewArtSlot(ArtifactPosition slot, CArtifactInstance *art,
 
 void CArtifactSet::eraseArtSlot(ArtifactPosition slot)
 {
-	if(slot < GameConstants::BACKPACK_START)
+	if(ArtifactUtils::isSlotBackpack(slot))
 	{
-		artifactsWorn.erase(slot);
+		slot = ArtifactPosition(slot - GameConstants::BACKPACK_START);
+		artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
 	}
 	else
 	{
-		slot = ArtifactPosition(slot - GameConstants::BACKPACK_START);
-		artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
+		artifactsWorn.erase(slot);
 	}
 }
 
@@ -1543,19 +1544,41 @@ DLL_LINKAGE ArtifactPosition ArtifactUtils::getArtifactDstPosition(	const CArtif
 	return ArtifactPosition(GameConstants::BACKPACK_START);
 }
 
-DLL_LINKAGE std::vector<ArtifactPosition> ArtifactUtils::unmovablePositions()
+namespace ArtifactUtils
 {
-	return { ArtifactPosition::SPELLBOOK, ArtifactPosition::MACH4 };
-}
 
-DLL_LINKAGE bool ArtifactUtils::isArtRemovable(const std::pair<ArtifactPosition, ArtSlotInfo> & slot)
+DLL_LINKAGE const std::array<ArtifactPosition::EArtifactPosition, 2> unmovableSlots =
+{
+	ArtifactPosition::SPELLBOOK, 
+	ArtifactPosition::MACH4
+};
+
+DLL_LINKAGE const std::array<ArtifactPosition::EArtifactPosition, 14> constituentWornSlots =
+{
+	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 isArtRemovable(const std::pair<ArtifactPosition, ArtSlotInfo> & slot)
 {
 	return slot.second.artifact
 		&& !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 checkSpellbookIsNeeded(const CGHeroInstance * heroPtr, ArtifactID artID, ArtifactPosition slot)
 {
 	// TODO what'll happen if Titan's thunder is equipped by pickin git up or the start of game?
 	// Titan's Thunder creates new spellbook on equip
@@ -1570,9 +1593,10 @@ DLL_LINKAGE bool ArtifactUtils::checkSpellbookIsNeeded(const CGHeroInstance * he
 	return false;
 }
 
-DLL_LINKAGE bool ArtifactUtils::isSlotBackpack(ArtifactPosition slot)
+DLL_LINKAGE bool isSlotBackpack(ArtifactPosition slot)
 {
 	return slot >= GameConstants::BACKPACK_START;
 }
+};
 
 VCMI_LIB_NAMESPACE_END

+ 2 - 1
lib/CArtHandler.h

@@ -386,7 +386,8 @@ namespace ArtifactUtils
 	DLL_LINKAGE ArtifactPosition getArtifactDstPosition(	const CArtifactInstance * artifact, 
 								const CArtifactSet * target,
 								ArtBearer::ArtBearer bearer);
-	DLL_LINKAGE std::vector<ArtifactPosition> unmovablePositions(); // TODO: Make this constexpr when the toolset is upgraded
+	DLL_LINKAGE extern const std::array<ArtifactPosition::EArtifactPosition, 2> unmovableSlots;
+	DLL_LINKAGE extern const std::array<ArtifactPosition::EArtifactPosition, 14> constituentWornSlots;
 	DLL_LINKAGE bool isArtRemovable(const std::pair<ArtifactPosition, ArtSlotInfo> & slot);
 	DLL_LINKAGE bool checkSpellbookIsNeeded(const CGHeroInstance * heroPtr, ArtifactID artID, ArtifactPosition slot);
 	DLL_LINKAGE bool isSlotBackpack(ArtifactPosition slot);