Browse Source

Artifacts swap check

SoundSSGood 1 year ago
parent
commit
e8a6308a15
2 changed files with 12 additions and 7 deletions
  1. 8 5
      lib/CArtHandler.cpp
  2. 4 2
      server/CGameHandler.cpp

+ 8 - 5
lib/CArtHandler.cpp

@@ -840,15 +840,18 @@ CArtifactSet::ArtPlacementMap CArtifactSet::putArtifact(ArtifactPosition slot, C
 
 void CArtifactSet::removeArtifact(ArtifactPosition slot)
 {
-	auto art = getArt(slot, false);
-	if(art)
+	if(const auto art = getArt(slot, false))
 	{
 		if(art->isCombined())
 		{
-			for(auto & part : art->getPartsInfo())
+			for(const auto & part : art->getPartsInfo())
 			{
-				if(getArt(part.slot, false))
-					eraseArtSlot(part.slot);
+				if(part.slot != ArtifactPosition::PRE_FIRST)
+				{
+					assert(getArt(part.slot, false));
+					assert(getArt(part.slot, false) == part.art);
+				}
+				eraseArtSlot(part.slot);
 			}
 		}
 		eraseArtSlot(slot);

+ 4 - 2
server/CGameHandler.cpp

@@ -2706,7 +2706,8 @@ bool CGameHandler::moveArtifact(const PlayerColor & player, const ArtifactLocati
 		COMPLAIN_RET("That heroes cannot make any exchange!");
 
 	const auto srcArtifact = srcArtSet->getArt(src.slot);
-	const bool isDstSlotOccupied = dstArtSet->bearerType() == ArtBearer::ALTAR ? false : dstArtSet->getArt(dst.slot) != nullptr;
+	const auto dstArtifact = dstArtSet->getArt(dst.slot);
+	const bool isDstSlotOccupied = dstArtSet->bearerType() == ArtBearer::ALTAR ? false : dstArtifact != nullptr;
 	const bool isDstSlotBackpack = dstArtSet->bearerType() == ArtBearer::HERO ? ArtifactUtils::isSlotBackpack(dst.slot) : false;
 
 	if(srcArtifact == nullptr)
@@ -2744,7 +2745,8 @@ bool CGameHandler::moveArtifact(const PlayerColor & player, const ArtifactLocati
 	// Check if dst slot is occupied
 	if(!isDstSlotBackpack && isDstSlotOccupied)
 	{
-		// Previous artifact must be removed
+		// Previous artifact must be swapped
+		COMPLAIN_RET_FALSE_IF(!dstArtifact->canBePutAt(srcArtSet, src.slot, true), "Cannot swap artifacts!");
 		ma.artsPack1.push_back(BulkMoveArtifacts::LinkedSlots(dstSlot, src.slot));
 		ma.swap = true;
 	}