1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-26 03:52:01 +02:00

Merge pull request #1650 from SoundSSGood/arts-swap-regression-fix

Arts swap regression fixed
This commit is contained in:
Ivan Savenko 2023-03-16 14:47:31 +02:00 committed by GitHub
commit 2a2af34788
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 48 deletions

View File

@ -633,7 +633,7 @@ CArtifactsOfHero::~CArtifactsOfHero()
if(!curHero->artifactsTransitionPos.empty()) if(!curHero->artifactsTransitionPos.empty())
{ {
auto artPlace = getArtPlace( auto artPlace = getArtPlace(
ArtifactUtils::getArtifactDstPosition(curHero->artifactsTransitionPos.begin()->artifact, curHero, curHero->bearerType())); ArtifactUtils::getArtifactDstPosition(curHero->artifactsTransitionPos.begin()->artifact, curHero));
assert(artPlace); assert(artPlace);
assert(artPlace->ourOwner); assert(artPlace->ourOwner);
artPlace->setMeAsDest(); artPlace->setMeAsDest();
@ -748,10 +748,7 @@ void CArtifactsOfHero::artifactMoved(const ArtifactLocation & src, const Artifac
if(withUIUpdate) if(withUIUpdate)
{ {
updateParentWindow(); updateParentWindow();
// If backpack is changed, update it scrollBackpack(0);
if((isCurHeroSrc && ArtifactUtils::isSlotBackpack(src.slot))
|| (isCurHeroDst && ArtifactUtils::isSlotBackpack(dst.slot)))
scrollBackpack(0);
} }
} }

View File

@ -841,7 +841,7 @@ void CExchangeController::moveArtifact(
{ {
auto srcLocation = ArtifactLocation(source, srcPosition); auto srcLocation = ArtifactLocation(source, srcPosition);
auto dstLocation = ArtifactLocation(target, auto dstLocation = ArtifactLocation(target,
ArtifactUtils::getArtifactDstPosition(source->getArt(srcPosition), target, target->bearerType())); ArtifactUtils::getArtifactDstPosition(source->getArt(srcPosition), target));
cb->swapArtifacts(srcLocation, dstLocation); cb->swapArtifacts(srcLocation, dstLocation);
} }

View File

@ -410,7 +410,7 @@ void CArtHandler::addSlot(CArtifact * art, const std::string & slotID)
static const std::vector<ArtifactPosition> ringSlots = static const std::vector<ArtifactPosition> ringSlots =
{ {
ArtifactPosition::LEFT_RING, ArtifactPosition::RIGHT_RING ArtifactPosition::RIGHT_RING, ArtifactPosition::LEFT_RING
}; };
if (slotID == "MISC") if (slotID == "MISC")
@ -425,7 +425,7 @@ void CArtHandler::addSlot(CArtifact * art, const std::string & slotID)
{ {
auto slot = ArtifactPosition(slotID); auto slot = ArtifactPosition(slotID);
if (slot != ArtifactPosition::PRE_FIRST) if (slot != ArtifactPosition::PRE_FIRST)
art->possibleSlots[ArtBearer::HERO].push_back (slot); art->possibleSlots[ArtBearer::HERO].push_back(slot);
} }
} }
@ -440,6 +440,7 @@ void CArtHandler::loadSlots(CArtifact * art, const JsonNode & node)
for (const JsonNode & slot : node["slot"].Vector()) for (const JsonNode & slot : node["slot"].Vector())
addSlot(art, slot.String()); addSlot(art, slot.String());
} }
std::sort(art->possibleSlots.at(ArtBearer::HERO).begin(), art->possibleSlots.at(ArtBearer::HERO).end());
} }
} }
@ -1565,17 +1566,14 @@ ArtBearer::ArtBearer CArtifactFittingSet::bearerType() const
return this->Bearer; return this->Bearer;
} }
DLL_LINKAGE ArtifactPosition ArtifactUtils::getArtifactDstPosition( const CArtifactInstance * artifact, DLL_LINKAGE ArtifactPosition ArtifactUtils::getArtifactDstPosition(const CArtifactInstance * artifact,
const CArtifactSet * target, const CArtifactSet * target)
ArtBearer::ArtBearer bearer)
{ {
for(auto slot : artifact->artType->possibleSlots.at(bearer)) for(auto slot : artifact->artType->possibleSlots.at(target->bearerType()))
{ {
auto existingArtifact = target->getArt(slot);
auto existingArtInfo = target->getSlot(slot); auto existingArtInfo = target->getSlot(slot);
if(!existingArtifact if((!existingArtInfo || !existingArtInfo->locked)
&& (!existingArtInfo || !existingArtInfo->locked)
&& artifact->canBePutAt(target, slot)) && artifact->canBePutAt(target, slot))
{ {
return slot; return slot;

View File

@ -384,9 +384,8 @@ protected:
namespace ArtifactUtils namespace ArtifactUtils
{ {
// Calculates where an artifact gets placed when it gets transferred from one hero to another. // Calculates where an artifact gets placed when it gets transferred from one hero to another.
DLL_LINKAGE ArtifactPosition getArtifactDstPosition( const CArtifactInstance * artifact, DLL_LINKAGE ArtifactPosition getArtifactDstPosition(const CArtifactInstance * artifact,
const CArtifactSet * target, const CArtifactSet * target);
ArtBearer::ArtBearer bearer);
// 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> & unmovableSlots();
DLL_LINKAGE const std::vector<ArtifactPosition::EArtifactPosition> & constituentWornSlots(); DLL_LINKAGE const std::vector<ArtifactPosition::EArtifactPosition> & constituentWornSlots();

View File

@ -4001,36 +4001,45 @@ bool CGameHandler::bulkMoveArtifacts(ObjectInstanceID srcHero, ObjectInstanceID
auto & slotsSrcDst = ma.artsPack0; auto & slotsSrcDst = ma.artsPack0;
auto & slotsDstSrc = ma.artsPack1; auto & slotsDstSrc = ma.artsPack1;
// Temporary fitting set for artifacts. Used to select available slots before sending data.
CArtifactFittingSet artFittingSet(pdstHero->bearerType());
auto moveArtifact = [this, &artFittingSet](const CArtifactInstance * artifact,
ArtifactPosition srcSlot, const CGHeroInstance * dstHero,
std::vector<BulkMoveArtifacts::LinkedSlots> & slots) -> void
{
assert(artifact);
auto dstSlot = ArtifactUtils::getArtifactDstPosition(artifact, &artFittingSet);
artFittingSet.putArtifact(dstSlot, static_cast<ConstTransitivePtr<CArtifactInstance>>(artifact));
slots.push_back(BulkMoveArtifacts::LinkedSlots(srcSlot, dstSlot));
if(ArtifactUtils::checkSpellbookIsNeeded(dstHero, artifact->artType->getId(), dstSlot))
giveHeroNewArtifact(dstHero, VLC->arth->objects[ArtifactID::SPELLBOOK], ArtifactPosition::SPELLBOOK);
};
if(swap) if(swap)
{ {
auto moveArtsWorn = [this](const CGHeroInstance * srcHero, const CGHeroInstance * dstHero, auto moveArtsWorn = [moveArtifact](const CGHeroInstance * srcHero, const CGHeroInstance * dstHero,
std::vector<BulkMoveArtifacts::LinkedSlots> & slots) -> void std::vector<BulkMoveArtifacts::LinkedSlots> & slots) -> void
{ {
for(auto & artifact : srcHero->artifactsWorn) for(auto & artifact : srcHero->artifactsWorn)
{ {
if(artifact.second.locked) if(ArtifactUtils::isArtRemovable(artifact))
continue; moveArtifact(artifact.second.getArt(), artifact.first, dstHero, slots);
if(!ArtifactUtils::isArtRemovable(artifact))
continue;
slots.push_back(BulkMoveArtifacts::LinkedSlots(artifact.first, artifact.first));
auto art = artifact.second.getArt();
assert(art);
if(ArtifactUtils::checkSpellbookIsNeeded(dstHero, art->artType->getId(), artifact.first))
giveHeroNewArtifact(dstHero, VLC->arth->objects[ArtifactID::SPELLBOOK], ArtifactPosition::SPELLBOOK);
} }
}; };
auto moveArtsInBackpack = [](const CGHeroInstance * pHero, auto moveArtsInBackpack = [](const CArtifactSet * artSet,
std::vector<BulkMoveArtifacts::LinkedSlots> & slots) -> void std::vector<BulkMoveArtifacts::LinkedSlots> & slots) -> void
{ {
for(auto & slotInfo : pHero->artifactsInBackpack) for(auto & slotInfo : artSet->artifactsInBackpack)
{ {
auto slot = pHero->getArtPos(slotInfo.artifact); auto slot = artSet->getArtPos(slotInfo.artifact);
slots.push_back(BulkMoveArtifacts::LinkedSlots(slot, slot)); slots.push_back(BulkMoveArtifacts::LinkedSlots(slot, slot));
} }
}; };
// Move over artifacts that are worn srcHero -> dstHero // Move over artifacts that are worn srcHero -> dstHero
moveArtsWorn(psrcHero, pdstHero, slotsSrcDst); moveArtsWorn(psrcHero, pdstHero, slotsSrcDst);
artFittingSet.artifactsWorn.clear();
// Move over artifacts that are worn dstHero -> srcHero // Move over artifacts that are worn dstHero -> srcHero
moveArtsWorn(pdstHero, psrcHero, slotsDstSrc); moveArtsWorn(pdstHero, psrcHero, slotsDstSrc);
// Move over artifacts that are in backpack srcHero -> dstHero // Move over artifacts that are in backpack srcHero -> dstHero
@ -4040,35 +4049,22 @@ bool CGameHandler::bulkMoveArtifacts(ObjectInstanceID srcHero, ObjectInstanceID
} }
else else
{ {
// Temporary fitting set for artifacts. Used to select available slots before sending data.
CArtifactFittingSet artFittingSet(pdstHero->bearerType());
artFittingSet.artifactsInBackpack = pdstHero->artifactsInBackpack; artFittingSet.artifactsInBackpack = pdstHero->artifactsInBackpack;
artFittingSet.artifactsWorn = pdstHero->artifactsWorn; artFittingSet.artifactsWorn = pdstHero->artifactsWorn;
auto moveArtifact = [this, &artFittingSet, &slotsSrcDst](const CArtifactInstance * artifact,
ArtifactPosition srcSlot, const CGHeroInstance * pdstHero) -> void
{
assert(artifact);
auto dstSlot = ArtifactUtils::getArtifactDstPosition(artifact, &artFittingSet, pdstHero->bearerType());
artFittingSet.putArtifact(dstSlot, static_cast<ConstTransitivePtr<CArtifactInstance>>(artifact));
slotsSrcDst.push_back(BulkMoveArtifacts::LinkedSlots(srcSlot, dstSlot));
if(ArtifactUtils::checkSpellbookIsNeeded(pdstHero, artifact->artType->getId(), dstSlot))
giveHeroNewArtifact(pdstHero, VLC->arth->objects[ArtifactID::SPELLBOOK], ArtifactPosition::SPELLBOOK);
};
// Move over artifacts that are worn // Move over artifacts that are worn
for(auto & artInfo : psrcHero->artifactsWorn) for(auto & artInfo : psrcHero->artifactsWorn)
{ {
if(ArtifactUtils::isArtRemovable(artInfo)) if(ArtifactUtils::isArtRemovable(artInfo))
{ {
moveArtifact(psrcHero->getArt(artInfo.first), artInfo.first, pdstHero); moveArtifact(psrcHero->getArt(artInfo.first), artInfo.first, pdstHero, slotsSrcDst);
} }
} }
// Move over artifacts that are in backpack // Move over artifacts that are in backpack
for(auto & slotInfo : psrcHero->artifactsInBackpack) for(auto & slotInfo : psrcHero->artifactsInBackpack)
{ {
moveArtifact(psrcHero->getArt(psrcHero->getArtPos(slotInfo.artifact)), psrcHero->getArtPos(slotInfo.artifact), pdstHero); moveArtifact(psrcHero->getArt(psrcHero->getArtPos(slotInfo.artifact)),
psrcHero->getArtPos(slotInfo.artifact), pdstHero, slotsSrcDst);
} }
} }
sendAndApply(&ma); sendAndApply(&ma);