From a0568823a99b892ca8d97a513fb392d1923aa3f5 Mon Sep 17 00:00:00 2001 From: SoundSSGood <87084363+SoundSSGood@users.noreply.github.com> Date: Fri, 30 Dec 2022 21:43:32 +0200 Subject: [PATCH] swap contitutient --- client/widgets/CArtifactHolder.cpp | 33 ++++++++++++------------------ client/widgets/CArtifactHolder.h | 2 +- client/windows/GUIClasses.cpp | 4 ---- client/windows/GUIClasses.h | 1 - lib/CArtHandler.cpp | 19 +++++++++++++---- lib/CArtHandler.h | 1 + server/CGameHandler.cpp | 4 +++- 7 files changed, 33 insertions(+), 31 deletions(-) diff --git a/client/widgets/CArtifactHolder.cpp b/client/widgets/CArtifactHolder.cpp index 46ec17742..4b12f2543 100644 --- a/client/widgets/CArtifactHolder.cpp +++ b/client/widgets/CArtifactHolder.cpp @@ -278,13 +278,12 @@ void CHeroArtPlace::select () if (locked) return; - selectSlot(true); pickSlot(true); if(ourArt->canBeDisassembled() && slotID < GameConstants::BACKPACK_START) //worn combined artifact -> locks have to disappear { - for(int i = 0; i < GameConstants::BACKPACK_START; i++) + for(auto slot : ArtifactUtils::constituentWornSlots()) { - auto ap = ourOwner->getArtPlace(i); + auto ap = ourOwner->getArtPlace(slot); if(ap)//getArtPlace may return null ap->pickSlot(ourArt->isPart(ap->ourArt)); } @@ -309,9 +308,9 @@ void CHeroArtPlace::deselect () pickSlot(false); if(ourArt && ourArt->canBeDisassembled()) //combined art returned to its slot -> restore locks { - for(int i = 0; i < GameConstants::BACKPACK_START; i++) + for(auto slot : ArtifactUtils::constituentWornSlots()) { - auto place = ourOwner->getArtPlace(i); + auto place = ourOwner->getArtPlace(slot); if(nullptr != place)//getArtPlace may return null place->pickSlot(false); @@ -735,10 +734,10 @@ void CArtifactsOfHero::artifactMoved(const ArtifactLocation & src, const Artifac if(isCurHeroDst && ArtifactUtils::isSlotBackpack(dst.slot)) updateSlot(dst.slot); // We need to update all slots, artifact might be combined and affect more slots - if(isCurHeroSrc || isCurHeroDst) + if(isCurHeroSrc || isCurHeroDst) updateWornSlots(false); - if(!src.isHolder(curHero) && !isCurHeroDst) + if(!isCurHeroSrc && !isCurHeroDst) return; // When moving one artifact onto another it leads to two art movements: dst->TRANSITION_POS; src->dst @@ -748,11 +747,12 @@ void CArtifactsOfHero::artifactMoved(const ArtifactLocation & src, const Artifac // Used when doing dragAndDrop and artifact swap multiple times if(src.slot == ArtifactPosition::TRANSITION_POS && commonInfo->src.slotID == ArtifactPosition::TRANSITION_POS && - commonInfo->dst.slotID == ArtifactPosition::PRE_FIRST) + commonInfo->dst.slotID == ArtifactPosition::PRE_FIRST && + isCurHeroDst) { auto art = curHero->getArt(ArtifactPosition::TRANSITION_POS); assert(art); - CCS->curh->dragAndDropCursor(make_unique("artifact", art->artType->getIconIndex())); + CCS->curh->dragAndDropCursor(std::make_unique("artifact", art->artType->getIconIndex())); markPossibleSlots(art); commonInfo->src.art = art; @@ -787,21 +787,14 @@ void CArtifactsOfHero::artifactMoved(const ArtifactLocation & src, const Artifac commonInfo->src.art = dst.getArt(); commonInfo->src.slotID = dst.slot; assert(commonInfo->src.AOH); - CCS->curh->dragAndDropCursor(make_unique("artifact", dst.getArt()->artType->getIconIndex())); - markPossibleSlots(dst.getArt()); + CCS->curh->dragAndDropCursor(std::make_unique("artifact", dst.getArt()->artType->getIconIndex())); } updateParentWindow(); - int shift = 0; - if(!ArtifactUtils::isSlotBackpack(src.slot) && dst.slot - GameConstants::BACKPACK_START < backpackPos) - shift++; - if(!ArtifactUtils::isSlotBackpack(dst.slot) && src.slot - GameConstants::BACKPACK_START < backpackPos) - shift--; - // If backpack is changed, update it if((isCurHeroSrc && ArtifactUtils::isSlotBackpack(src.slot)) || (isCurHeroDst && ArtifactUtils::isSlotBackpack(dst.slot))) - scrollBackpack(shift); + scrollBackpack(0); } void CArtifactsOfHero::artifactRemoved(const ArtifactLocation &al) @@ -815,7 +808,7 @@ void CArtifactsOfHero::artifactRemoved(const ArtifactLocation &al) } } -CArtifactsOfHero::ArtPlacePtr CArtifactsOfHero::getArtPlace(int slot) +CArtifactsOfHero::ArtPlacePtr CArtifactsOfHero::getArtPlace(ArtifactPosition slot) { if(slot == ArtifactPosition::TRANSITION_POS) { @@ -823,7 +816,7 @@ CArtifactsOfHero::ArtPlacePtr CArtifactsOfHero::getArtPlace(int slot) } if(slot < GameConstants::BACKPACK_START) { - if(artWorn.find(ArtifactPosition(slot)) == artWorn.end()) + if(artWorn.find(slot) == artWorn.end()) { logGlobal->error("CArtifactsOfHero::getArtPlace: invalid slot %d", slot); return nullptr; diff --git a/client/widgets/CArtifactHolder.h b/client/widgets/CArtifactHolder.h index 36e16e547..0adfb0ec9 100644 --- a/client/widgets/CArtifactHolder.h +++ b/client/widgets/CArtifactHolder.h @@ -141,7 +141,7 @@ public: void artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst); void artifactRemoved(const ArtifactLocation &al); void artifactUpdateSlots(const ArtifactLocation &al); - ArtPlacePtr getArtPlace(int slot);//may return null + ArtPlacePtr getArtPlace(ArtifactPosition slot);//may return null void setHero(const CGHeroInstance * hero); const CGHeroInstance *getHero() const; diff --git a/client/windows/GUIClasses.cpp b/client/windows/GUIClasses.cpp index a84b12a1b..54d6a1531 100644 --- a/client/windows/GUIClasses.cpp +++ b/client/windows/GUIClasses.cpp @@ -1247,10 +1247,6 @@ CExchangeWindow::CExchangeWindow(ObjectInstanceID hero1, ObjectInstanceID hero2, updateWidgets(); } -CExchangeWindow::~CExchangeWindow() -{ -} - const CGarrisonSlot * CExchangeWindow::getSelectedSlotID() const { return garr->getSelection(); diff --git a/client/windows/GUIClasses.h b/client/windows/GUIClasses.h index 56097d0b4..0ce7d8314 100644 --- a/client/windows/GUIClasses.h +++ b/client/windows/GUIClasses.h @@ -379,7 +379,6 @@ public: const CGarrisonSlot * getSelectedSlotID() const; CExchangeWindow(ObjectInstanceID hero1, ObjectInstanceID hero2, QueryID queryID); - ~CExchangeWindow(); }; /// Here you can buy ships diff --git a/lib/CArtHandler.cpp b/lib/CArtHandler.cpp index 49fcce302..96632f488 100644 --- a/lib/CArtHandler.cpp +++ b/lib/CArtHandler.cpp @@ -856,7 +856,7 @@ void CArtifactInstance::putAt(ArtifactLocation al) assert(canBePutAt(al)); al.getHolderArtSet()->setNewArtSlot(al.slot, this, false); - if(!ArtifactUtils::isSlotBackpack(al.slot) && (al.slot != ArtifactPosition::TRANSITION_POS)) + if(ArtifactUtils::isSlotEquipment(al.slot)) al.getHolderNode()->attachTo(*this); } @@ -864,7 +864,7 @@ void CArtifactInstance::removeFrom(ArtifactLocation al) { assert(al.getHolderArtSet()->getArt(al.slot) == this); al.getHolderArtSet()->eraseArtSlot(al.slot); - if(!ArtifactUtils::isSlotBackpack(al.slot) && (al.slot != ArtifactPosition::TRANSITION_POS)) + if(ArtifactUtils::isSlotEquipment(al.slot)) al.getHolderNode()->detachFrom(*this); } @@ -1003,6 +1003,8 @@ bool CArtifactInstance::isPart(const CArtifactInstance *supposedPart) const bool CCombinedArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved) const { + if(slot == ArtifactPosition::TRANSITION_POS) + return true; bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(artSet, slot, assumeDestRemoved); if(!canMainArtifactBePlaced) return false; //no is no... @@ -1075,7 +1077,11 @@ void CCombinedArtifactInstance::addAsConstituent(CArtifactInstance *art, Artifac void CCombinedArtifactInstance::putAt(ArtifactLocation al) { - if(ArtifactUtils::isSlotBackpack(al.slot)) + if(al.slot == ArtifactPosition::TRANSITION_POS) + { + CArtifactInstance::putAt(al); + } + else if(ArtifactUtils::isSlotBackpack(al.slot)) { CArtifactInstance::putAt(al); for(ConstituentInfo &ci : constituentsInfo) @@ -1113,7 +1119,7 @@ void CCombinedArtifactInstance::putAt(ArtifactLocation al) void CCombinedArtifactInstance::removeFrom(ArtifactLocation al) { - if(ArtifactUtils::isSlotBackpack(al.slot)) + if(ArtifactUtils::isSlotBackpack(al.slot) || al.slot == ArtifactPosition::TRANSITION_POS) { CArtifactInstance::removeFrom(al); } @@ -1634,4 +1640,9 @@ DLL_LINKAGE bool ArtifactUtils::isSlotBackpack(ArtifactPosition slot) return slot >= GameConstants::BACKPACK_START; } +DLL_LINKAGE bool ArtifactUtils::isSlotEquipment(ArtifactPosition slot) +{ + return slot < GameConstants::BACKPACK_START && slot >= 0; +} + VCMI_LIB_NAMESPACE_END diff --git a/lib/CArtHandler.h b/lib/CArtHandler.h index cae8abef1..504c362c7 100644 --- a/lib/CArtHandler.h +++ b/lib/CArtHandler.h @@ -393,6 +393,7 @@ namespace ArtifactUtils DLL_LINKAGE bool isArtRemovable(const std::pair & slot); DLL_LINKAGE bool checkSpellbookIsNeeded(const CGHeroInstance * heroPtr, ArtifactID artID, ArtifactPosition slot); DLL_LINKAGE bool isSlotBackpack(ArtifactPosition slot); + DLL_LINKAGE bool isSlotEquipment(ArtifactPosition slot); } VCMI_LIB_NAMESPACE_END diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 533c6f3db..ce0fb4bee 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -3892,7 +3892,7 @@ bool CGameHandler::moveArtifact(const ArtifactLocation &al1, const ArtifactLocat // Check if src/dest slots are appropriate for the artifacts exchanged. // Moving to the backpack is always allowed. - if ((!srcArtifact || dst.slot < GameConstants::BACKPACK_START) + if ((!srcArtifact || !ArtifactUtils::isSlotBackpack(dst.slot)) && srcArtifact && !srcArtifact->canBePutAt(dst, true)) COMPLAIN_RET("Cannot move artifact!"); @@ -3926,6 +3926,8 @@ bool CGameHandler::moveArtifact(const ArtifactLocation &al1, const ArtifactLocat giveHeroNewArtifact(hero, VLC->arth->objects[ArtifactID::SPELLBOOK], ArtifactPosition::SPELLBOOK); MoveArtifact ma(&src, &dst); + if(dst.slot == ArtifactPosition::TRANSITION_POS) + ma.askAssemble = false; sendAndApply(&ma); } return true;