From 2d078132bfc3f0059cdd1f4c3ee4038036584cfb Mon Sep 17 00:00:00 2001 From: SoundSSGood <87084363+SoundSSGood@users.noreply.github.com> Date: Thu, 17 Nov 2022 19:43:54 +0200 Subject: [PATCH 1/7] assemble in backpack initial --- client/widgets/CArtifactHolder.cpp | 44 +++++++++++------------ lib/CArtHandler.cpp | 58 ++++++++++++++++++++++-------- lib/CArtHandler.h | 4 ++- lib/NetPacksLib.cpp | 2 +- server/CGameHandler.cpp | 2 +- 5 files changed, 70 insertions(+), 40 deletions(-) diff --git a/client/widgets/CArtifactHolder.cpp b/client/widgets/CArtifactHolder.cpp index 2800b579e..d5178fa71 100644 --- a/client/widgets/CArtifactHolder.cpp +++ b/client/widgets/CArtifactHolder.cpp @@ -206,12 +206,17 @@ void CHeroArtPlace::clickLeft(tribool down, bool previousState) bool CHeroArtPlace::askToAssemble(const CArtifactInstance *art, ArtifactPosition slot, const CGHeroInstance *hero) { - assert(art != nullptr); - assert(hero != nullptr); - std::vector assemblyPossibilities = art->assemblyPossibilities(hero); + assert(art); + assert(hero); + bool assembleEqipped = true; + if(slot >= GameConstants::BACKPACK_START) + { + assembleEqipped = false; + } + auto assemblyPossibilities = art->assemblyPossibilities(hero, assembleEqipped); // If the artifact can be assembled, display dialog. - for(const CArtifact *combination : assemblyPossibilities) + for(auto combination : assemblyPossibilities) { LOCPLINT->showArtifactAssemblyDialog( art->artType, @@ -229,27 +234,22 @@ void CHeroArtPlace::clickRight(tribool down, bool previousState) { if(ourArt && down && !locked && text.size() && !picked) //if there is no description or it's a lock, do nothing ;] { - if(slotID < GameConstants::BACKPACK_START) + if(ourOwner->allowedAssembling) { - if(ourOwner->allowedAssembling) + // If the artifact can be assembled, display dialog. + if(askToAssemble(ourArt, slotID, ourOwner->curHero)) { - std::vector assemblyPossibilities = ourArt->assemblyPossibilities(ourOwner->curHero); + return; + } - // If the artifact can be assembled, display dialog. - if(askToAssemble(ourArt, slotID, ourOwner->curHero)) - { - return; - } - - // Otherwise if the artifact can be diasassembled, display dialog. - if(ourArt->canBeDisassembled()) - { - LOCPLINT->showArtifactAssemblyDialog( - ourArt->artType, - nullptr, - std::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), ourOwner->curHero, slotID, false, ArtifactID())); - return; - } + // Otherwise if the artifact can be diasassembled, display dialog. + if(ourArt->canBeDisassembled()) + { + LOCPLINT->showArtifactAssemblyDialog( + ourArt->artType, + nullptr, + std::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), ourOwner->curHero, slotID, false, ArtifactID())); + return; } } diff --git a/lib/CArtHandler.cpp b/lib/CArtHandler.cpp index 0f62c029d..eb53025b1 100644 --- a/lib/CArtHandler.cpp +++ b/lib/CArtHandler.cpp @@ -870,26 +870,36 @@ bool CArtifactInstance::canBeDisassembled() const return bool(artType->constituents); } -std::vector CArtifactInstance::assemblyPossibilities(const CArtifactSet *h) const +std::vector CArtifactInstance::assemblyPossibilities(const CArtifactSet * h, bool equipped) const { std::vector ret; if(artType->constituents) //combined artifact already: no combining of combined artifacts... for now. return ret; - for(const CArtifact * artifact : artType->constituentOf) + for(auto artifact : artType->constituentOf) { assert(artifact->constituents); bool possible = true; - for(const CArtifact * constituent : *artifact->constituents) //check if all constituents are available + for(auto constituent : *artifact->constituents) //check if all constituents are available { - const bool noBackpack = false; - const bool notAlreadyAssembled = false; - - if(!h->hasArt(constituent->id, true, noBackpack, notAlreadyAssembled)) //constituent must be equipped + if (equipped) { - possible = false; - break; + // Search for equipped arts + if (!h->hasArt(constituent->id, true, false, false)) + { + possible = false; + break; + } + } + else + { + // Search in backpack + if(!h->hasArtBackpack(constituent->id)) + { + possible = false; + break; + } } } @@ -1198,19 +1208,32 @@ ArtifactPosition CArtifactSet::getArtPos(int aid, bool onlyWorn, bool allowLocke std::vector CArtifactSet::getAllArtPositions(int aid, bool onlyWorn, bool allowLocked, bool getAll) const { std::vector result; - for(auto i = artifactsWorn.cbegin(); i != artifactsWorn.cend(); i++) - if(i->second.artifact->artType->id == aid && (allowLocked || !i->second.locked)) - result.push_back(i->first); + for(auto & slotInfo : artifactsWorn) + if(slotInfo.second.artifact->artType->id == aid && (allowLocked || !slotInfo.second.locked)) + result.push_back(slotInfo.first); if(onlyWorn) return result; if(!getAll && !result.empty()) return result; - for(int i = 0; i < artifactsInBackpack.size(); i++) - if(artifactsInBackpack[i].artifact->artType->id == aid) - result.push_back(ArtifactPosition(GameConstants::BACKPACK_START + i)); + auto backpackPositions = getBackpackArtPositions(aid); + result.insert(result.end(), backpackPositions.begin(), backpackPositions.end()); + return result; +} +std::vector CArtifactSet::getBackpackArtPositions(int aid) const +{ + std::vector result; + + si32 backpackPosition = GameConstants::BACKPACK_START; + for(auto & artInfo : artifactsInBackpack) + { + auto art = artInfo.getArt(); + if (art && art->artType->id == aid) + result.push_back(ArtifactPosition(backpackPosition)); + backpackPosition++; + } return result; } @@ -1249,6 +1272,11 @@ bool CArtifactSet::hasArt( return getArtPosCount(aid, onlyWorn, searchBackpackAssemblies, allowLocked) > 0; } +bool CArtifactSet::hasArtBackpack(ui32 aid) const +{ + return getBackpackArtPositions(aid).size() > 0; +} + unsigned CArtifactSet::getArtPosCount(int aid, bool onlyWorn, bool searchBackpackAssemblies, bool allowLocked) const { const auto allPositions = getAllArtPositions(aid, onlyWorn, allowLocked, true); diff --git a/lib/CArtHandler.h b/lib/CArtHandler.h index 46f14d490..6ea674c70 100644 --- a/lib/CArtHandler.h +++ b/lib/CArtHandler.h @@ -163,7 +163,7 @@ public: /// of itself, additionally truth is returned for constituents of combined arts virtual bool isPart(const CArtifactInstance *supposedPart) const; - std::vector assemblyPossibilities(const CArtifactSet *h) const; + std::vector assemblyPossibilities(const CArtifactSet * h, bool equipped) const; void move(ArtifactLocation src, ArtifactLocation dst); template void serialize(Handler &h, const int version) @@ -330,12 +330,14 @@ public: ArtifactPosition getArtPos(int aid, bool onlyWorn = true, bool allowLocked = true) const; ArtifactPosition getArtPos(const CArtifactInstance *art) const; std::vector getAllArtPositions(int aid, bool onlyWorn, bool allowLocked, bool getAll) const; + std::vector getBackpackArtPositions(int aid) const; const CArtifactInstance *getArtByInstanceId(ArtifactInstanceID artInstId) const; /// Search for constituents of assemblies in backpack which do not have an ArtifactPosition const CArtifactInstance *getHiddenArt(int aid) const; const CCombinedArtifactInstance *getAssemblyByConstituent(int aid) const; /// 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 hasArtBackpack(ui32 aid) const; bool isPositionFree(ArtifactPosition pos, bool onlyLockCheck = false) const; unsigned getArtPosCount(int aid, bool onlyWorn = true, bool searchBackpackAssemblies = true, bool allowLocked = true) const; diff --git a/lib/NetPacksLib.cpp b/lib/NetPacksLib.cpp index ca8ae4c8b..2d4a56427 100644 --- a/lib/NetPacksLib.cpp +++ b/lib/NetPacksLib.cpp @@ -1170,7 +1170,7 @@ DLL_LINKAGE void AssembledArtifact::applyGs(CGameState *gs) CArtifactSet *artSet = al.getHolderArtSet(); const CArtifactInstance *transformedArt = al.getArt(); assert(transformedArt); - assert(vstd::contains(transformedArt->assemblyPossibilities(artSet), builtArt)); + assert(vstd::contains(transformedArt->assemblyPossibilities(artSet, true), builtArt)); UNUSED(transformedArt); auto combinedArt = new CCombinedArtifactInstance(builtArt); diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 064dfb7c1..6f2ddbd20 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -4036,7 +4036,7 @@ bool CGameHandler::assembleArtifacts (ObjectInstanceID heroID, ArtifactPosition CArtifact *combinedArt = VLC->arth->objects[assembleTo]; if (!combinedArt->constituents) COMPLAIN_RET("assembleArtifacts: Artifact being attempted to assemble is not a combined artifacts!"); - if (!vstd::contains(destArtifact->assemblyPossibilities(hero), combinedArt)) + if (!vstd::contains(destArtifact->assemblyPossibilities(hero, true), combinedArt)) COMPLAIN_RET("assembleArtifacts: It's impossible to assemble requested artifact!"); if(ArtifactUtils::checkSpellbookIsNeeded(hero, assembleTo, artifactSlot)) From 16b650136a6ae8886f46801b0c1feb42938bf994 Mon Sep 17 00:00:00 2001 From: SoundSSGood <87084363+SoundSSGood@users.noreply.github.com> Date: Fri, 18 Nov 2022 00:24:06 +0200 Subject: [PATCH 2/7] backpack disassemble --- client/widgets/CArtifactHolder.cpp | 21 +++++++++++++++++++-- client/widgets/CArtifactHolder.h | 3 ++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/client/widgets/CArtifactHolder.cpp b/client/widgets/CArtifactHolder.cpp index d5178fa71..1a62e495f 100644 --- a/client/widgets/CArtifactHolder.cpp +++ b/client/widgets/CArtifactHolder.cpp @@ -825,10 +825,17 @@ void CArtifactsOfHero::artifactAssembled(const ArtifactLocation &al) updateWornSlots(); } -void CArtifactsOfHero::artifactDisassembled(const ArtifactLocation &al) +void CArtifactsOfHero::artifactDisassembled(const ArtifactLocation & al) { if(al.isHolder(curHero)) - updateWornSlots(); + { + if(al.slot >= GameConstants::BACKPACK_START) + { + updateBackpackSlots(); + } + else + updateWornSlots(); + } } void CArtifactsOfHero::updateWornSlots(bool redrawParent) @@ -840,6 +847,16 @@ void CArtifactsOfHero::updateWornSlots(bool redrawParent) updateParentWindow(); } +void CArtifactsOfHero::updateBackpackSlots(bool redrawParent) +{ + for(auto & artPlace : backpack) + updateSlot(artPlace->slotID); + scrollBackpack(0); + + if(redrawParent) + updateParentWindow(); +} + const CGHeroInstance * CArtifactsOfHero::getHero() const { return curHero; diff --git a/client/widgets/CArtifactHolder.h b/client/widgets/CArtifactHolder.h index eff474737..272e99437 100644 --- a/client/widgets/CArtifactHolder.h +++ b/client/widgets/CArtifactHolder.h @@ -153,7 +153,8 @@ public: void markPossibleSlots(const CArtifactInstance* art); void unmarkSlots(bool withRedraw = true); //unmarks slots in all visible AOHs void unmarkLocalSlots(bool withRedraw = true); //unmarks slots in that particular AOH - void updateWornSlots (bool redrawParent = true); + void updateWornSlots(bool redrawParent = true); + void updateBackpackSlots(bool redrawParent = true); void updateSlot(ArtifactPosition i); From e9ab894638faa83b8183c544f2c6039949e648d2 Mon Sep 17 00:00:00 2001 From: SoundSSGood <87084363+SoundSSGood@users.noreply.github.com> Date: Fri, 18 Nov 2022 01:32:25 +0200 Subject: [PATCH 3/7] backpack assemble --- client/widgets/CArtifactHolder.cpp | 12 +++--------- client/widgets/CArtifactHolder.h | 3 +-- lib/NetPacksLib.cpp | 26 +++++++++++++++++++------- server/CGameHandler.cpp | 10 +++++++--- 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/client/widgets/CArtifactHolder.cpp b/client/widgets/CArtifactHolder.cpp index 1a62e495f..b2f881338 100644 --- a/client/widgets/CArtifactHolder.cpp +++ b/client/widgets/CArtifactHolder.cpp @@ -819,13 +819,7 @@ CArtifactsOfHero::ArtPlacePtr CArtifactsOfHero::getArtPlace(int slot) } } -void CArtifactsOfHero::artifactAssembled(const ArtifactLocation &al) -{ - if(al.isHolder(curHero)) - updateWornSlots(); -} - -void CArtifactsOfHero::artifactDisassembled(const ArtifactLocation & al) +void CArtifactsOfHero::artifactUpdateSlots(const ArtifactLocation & al) { if(al.isHolder(curHero)) { @@ -927,7 +921,7 @@ void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation &artLoc) { std::shared_ptr realPtr = artSetWeak.lock(); if(realPtr) - realPtr->artifactDisassembled(artLoc); + realPtr->artifactUpdateSlots(artLoc); } } @@ -937,7 +931,7 @@ void CWindowWithArtifacts::artifactAssembled(const ArtifactLocation &artLoc) { std::shared_ptr realPtr = artSetWeak.lock(); if(realPtr) - realPtr->artifactAssembled(artLoc); + realPtr->artifactUpdateSlots(artLoc); } } diff --git a/client/widgets/CArtifactHolder.h b/client/widgets/CArtifactHolder.h index 272e99437..215d41d3a 100644 --- a/client/widgets/CArtifactHolder.h +++ b/client/widgets/CArtifactHolder.h @@ -140,8 +140,7 @@ public: void realizeCurrentTransaction(); //calls callback with parameters stored in commonInfo void artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst); void artifactRemoved(const ArtifactLocation &al); - void artifactAssembled(const ArtifactLocation &al); - void artifactDisassembled(const ArtifactLocation &al); + void artifactUpdateSlots(const ArtifactLocation &al); ArtPlacePtr getArtPlace(int slot);//may return null void setHero(const CGHeroInstance * hero); diff --git a/lib/NetPacksLib.cpp b/lib/NetPacksLib.cpp index 2d4a56427..18f0af3f0 100644 --- a/lib/NetPacksLib.cpp +++ b/lib/NetPacksLib.cpp @@ -1167,26 +1167,38 @@ DLL_LINKAGE void BulkMoveArtifacts::applyGs(CGameState * gs) DLL_LINKAGE void AssembledArtifact::applyGs(CGameState *gs) { - CArtifactSet *artSet = al.getHolderArtSet(); + CArtifactSet * artSet = al.getHolderArtSet(); const CArtifactInstance *transformedArt = al.getArt(); assert(transformedArt); - assert(vstd::contains(transformedArt->assemblyPossibilities(artSet, true), builtArt)); + bool combineEquipped = true; + if(al.slot >= GameConstants::BACKPACK_START) + combineEquipped = false; + assert(vstd::contains(transformedArt->assemblyPossibilities(artSet, combineEquipped), builtArt)); UNUSED(transformedArt); auto combinedArt = new CCombinedArtifactInstance(builtArt); gs->map->addNewArtifactInstance(combinedArt); - //retrieve all constituents + // Retrieve all constituents for(const CArtifact * constituent : *builtArt->constituents) { - ArtifactPosition pos = artSet->getArtPos(constituent->id); + ArtifactPosition pos = artSet->getArtPos(constituent->id, combineEquipped); assert(pos >= 0); - CArtifactInstance *constituentInstance = artSet->getArt(pos); + CArtifactInstance * constituentInstance = artSet->getArt(pos); //move constituent from hero to be part of new, combined artifact constituentInstance->removeFrom(ArtifactLocation(al.artHolder, pos)); combinedArt->addAsConstituent(constituentInstance, pos); - if(!vstd::contains(combinedArt->artType->possibleSlots[artSet->bearerType()], al.slot) && vstd::contains(combinedArt->artType->possibleSlots[artSet->bearerType()], pos)) - al.slot = pos; + if(combineEquipped) + { + if(!vstd::contains(combinedArt->artType->possibleSlots[artSet->bearerType()], al.slot) + && vstd::contains(combinedArt->artType->possibleSlots[artSet->bearerType()], pos)) + al.slot = pos; + } + else + { + if(al.slot > pos) + al.slot = pos; + } } //put new combined artifacts diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 6f2ddbd20..f8542dae7 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -4031,13 +4031,17 @@ bool CGameHandler::assembleArtifacts (ObjectInstanceID heroID, ArtifactPosition if (!destArtifact) COMPLAIN_RET("assembleArtifacts: there is no such artifact instance!"); - if (assemble) + if(assemble) { CArtifact *combinedArt = VLC->arth->objects[assembleTo]; - if (!combinedArt->constituents) + if(!combinedArt->constituents) COMPLAIN_RET("assembleArtifacts: Artifact being attempted to assemble is not a combined artifacts!"); - if (!vstd::contains(destArtifact->assemblyPossibilities(hero, true), combinedArt)) + bool combineEquipped = true; + if(artifactSlot >= GameConstants::BACKPACK_START) + combineEquipped = false; + if(!vstd::contains(destArtifact->assemblyPossibilities(hero, combineEquipped), combinedArt)) COMPLAIN_RET("assembleArtifacts: It's impossible to assemble requested artifact!"); + if(ArtifactUtils::checkSpellbookIsNeeded(hero, assembleTo, artifactSlot)) giveHeroNewArtifact(hero, VLC->arth->objects[ArtifactID::SPELLBOOK], ArtifactPosition::SPELLBOOK); From 30bbb57619e83414553aa846bee09d7c81e93fa1 Mon Sep 17 00:00:00 2001 From: SoundSSGood <87084363+SoundSSGood@users.noreply.github.com> Date: Fri, 18 Nov 2022 22:15:03 +0200 Subject: [PATCH 4/7] Apply suggestions from code review Co-authored-by: Nordsoft91 --- client/widgets/CArtifactHolder.cpp | 6 ++---- lib/CArtHandler.cpp | 8 ++++---- lib/NetPacksLib.cpp | 3 +-- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/client/widgets/CArtifactHolder.cpp b/client/widgets/CArtifactHolder.cpp index b2f881338..6de8535e8 100644 --- a/client/widgets/CArtifactHolder.cpp +++ b/client/widgets/CArtifactHolder.cpp @@ -216,7 +216,7 @@ bool CHeroArtPlace::askToAssemble(const CArtifactInstance *art, ArtifactPosition auto assemblyPossibilities = art->assemblyPossibilities(hero, assembleEqipped); // If the artifact can be assembled, display dialog. - for(auto combination : assemblyPossibilities) + for(const auto * combination : assemblyPossibilities) { LOCPLINT->showArtifactAssemblyDialog( art->artType, @@ -824,9 +824,7 @@ void CArtifactsOfHero::artifactUpdateSlots(const ArtifactLocation & al) if(al.isHolder(curHero)) { if(al.slot >= GameConstants::BACKPACK_START) - { updateBackpackSlots(); - } else updateWornSlots(); } @@ -843,7 +841,7 @@ void CArtifactsOfHero::updateWornSlots(bool redrawParent) void CArtifactsOfHero::updateBackpackSlots(bool redrawParent) { - for(auto & artPlace : backpack) + for(auto * artPlace : backpack) updateSlot(artPlace->slotID); scrollBackpack(0); diff --git a/lib/CArtHandler.cpp b/lib/CArtHandler.cpp index eb53025b1..1a58ed947 100644 --- a/lib/CArtHandler.cpp +++ b/lib/CArtHandler.cpp @@ -876,12 +876,12 @@ std::vector CArtifactInstance::assemblyPossibilities(const CA if(artType->constituents) //combined artifact already: no combining of combined artifacts... for now. return ret; - for(auto artifact : artType->constituentOf) + for(const auto * artifact : artType->constituentOf) { assert(artifact->constituents); bool possible = true; - for(auto constituent : *artifact->constituents) //check if all constituents are available + for(const auto * constituent : *artifact->constituents) //check if all constituents are available { if (equipped) { @@ -1229,9 +1229,9 @@ std::vector CArtifactSet::getBackpackArtPositions(int aid) con si32 backpackPosition = GameConstants::BACKPACK_START; for(auto & artInfo : artifactsInBackpack) { - auto art = artInfo.getArt(); + auto * art = artInfo.getArt(); if (art && art->artType->id == aid) - result.push_back(ArtifactPosition(backpackPosition)); + result.emplace_back(backpackPosition); backpackPosition++; } return result; diff --git a/lib/NetPacksLib.cpp b/lib/NetPacksLib.cpp index 18f0af3f0..2b3b71376 100644 --- a/lib/NetPacksLib.cpp +++ b/lib/NetPacksLib.cpp @@ -1196,8 +1196,7 @@ DLL_LINKAGE void AssembledArtifact::applyGs(CGameState *gs) } else { - if(al.slot > pos) - al.slot = pos; + al.slot = std::min(al.slot, pos) } } From e6e669d0242153ccaf2c5811c94db26fbf443d3c Mon Sep 17 00:00:00 2001 From: SoundSSGood <87084363+SoundSSGood@users.noreply.github.com> Date: Fri, 18 Nov 2022 22:49:57 +0200 Subject: [PATCH 5/7] ArtifactUtils::isSlotBackpack() func + refactoring --- client/widgets/CArtifactHolder.cpp | 10 +++------- lib/CArtHandler.cpp | 7 ++++++- lib/CArtHandler.h | 1 + lib/NetPacksLib.cpp | 10 ++++------ server/CGameHandler.cpp | 4 +--- 5 files changed, 15 insertions(+), 17 deletions(-) diff --git a/client/widgets/CArtifactHolder.cpp b/client/widgets/CArtifactHolder.cpp index 6de8535e8..814fc3c07 100644 --- a/client/widgets/CArtifactHolder.cpp +++ b/client/widgets/CArtifactHolder.cpp @@ -208,11 +208,7 @@ bool CHeroArtPlace::askToAssemble(const CArtifactInstance *art, ArtifactPosition { assert(art); assert(hero); - bool assembleEqipped = true; - if(slot >= GameConstants::BACKPACK_START) - { - assembleEqipped = false; - } + bool assembleEqipped = !ArtifactUtils::isSlotBackpack(slot); auto assemblyPossibilities = art->assemblyPossibilities(hero, assembleEqipped); // If the artifact can be assembled, display dialog. @@ -823,7 +819,7 @@ void CArtifactsOfHero::artifactUpdateSlots(const ArtifactLocation & al) { if(al.isHolder(curHero)) { - if(al.slot >= GameConstants::BACKPACK_START) + if(ArtifactUtils::isSlotBackpack(al.slot)) updateBackpackSlots(); else updateWornSlots(); @@ -841,7 +837,7 @@ void CArtifactsOfHero::updateWornSlots(bool redrawParent) void CArtifactsOfHero::updateBackpackSlots(bool redrawParent) { - for(auto * artPlace : backpack) + for(auto artPlace : backpack) updateSlot(artPlace->slotID); scrollBackpack(0); diff --git a/lib/CArtHandler.cpp b/lib/CArtHandler.cpp index 1a58ed947..3c1f457f5 100644 --- a/lib/CArtHandler.cpp +++ b/lib/CArtHandler.cpp @@ -883,7 +883,7 @@ std::vector CArtifactInstance::assemblyPossibilities(const CA for(const auto * constituent : *artifact->constituents) //check if all constituents are available { - if (equipped) + if(equipped) { // Search for equipped arts if (!h->hasArt(constituent->id, true, false, false)) @@ -1564,4 +1564,9 @@ DLL_LINKAGE bool ArtifactUtils::checkSpellbookIsNeeded(const CGHeroInstance * he return false; } +DLL_LINKAGE bool ArtifactUtils::isSlotBackpack(ArtifactPosition slot) +{ + return slot >= GameConstants::BACKPACK_START; +} + VCMI_LIB_NAMESPACE_END diff --git a/lib/CArtHandler.h b/lib/CArtHandler.h index 6ea674c70..21b7f26d3 100644 --- a/lib/CArtHandler.h +++ b/lib/CArtHandler.h @@ -388,6 +388,7 @@ namespace ArtifactUtils DLL_LINKAGE std::vector unmovablePositions(); // TODO: Make this constexpr when the toolset is upgraded 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); } VCMI_LIB_NAMESPACE_END diff --git a/lib/NetPacksLib.cpp b/lib/NetPacksLib.cpp index 2b3b71376..539514f16 100644 --- a/lib/NetPacksLib.cpp +++ b/lib/NetPacksLib.cpp @@ -1089,7 +1089,7 @@ DLL_LINKAGE void EraseArtifact::applyGs(CGameState *gs) DLL_LINKAGE void MoveArtifact::applyGs(CGameState * gs) { CArtifactInstance * art = src.getArt(); - if(dst.slot < GameConstants::BACKPACK_START) + if(!ArtifactUtils::isSlotBackpack(dst.slot)) assert(!dst.getArt()); art->move(src, dst); @@ -1114,7 +1114,7 @@ DLL_LINKAGE void BulkMoveArtifacts::applyGs(CGameState * gs) // so all the following indices will be affected. Thus, we need to update // the subsequent artifact slots to account for that auto srcPos = slot.srcPos; - if((srcPos >= GameConstants::BACKPACK_START) && (operation != EBulkArtsOp::BULK_PUT)) + if(ArtifactUtils::isSlotBackpack(srcPos) && (operation != EBulkArtsOp::BULK_PUT)) { srcPos = ArtifactPosition(srcPos.num - numBackpackArtifactsMoved); } @@ -1170,9 +1170,7 @@ DLL_LINKAGE void AssembledArtifact::applyGs(CGameState *gs) CArtifactSet * artSet = al.getHolderArtSet(); const CArtifactInstance *transformedArt = al.getArt(); assert(transformedArt); - bool combineEquipped = true; - if(al.slot >= GameConstants::BACKPACK_START) - combineEquipped = false; + bool combineEquipped = !ArtifactUtils::isSlotBackpack(al.slot); assert(vstd::contains(transformedArt->assemblyPossibilities(artSet, combineEquipped), builtArt)); UNUSED(transformedArt); @@ -1196,7 +1194,7 @@ DLL_LINKAGE void AssembledArtifact::applyGs(CGameState *gs) } else { - al.slot = std::min(al.slot, pos) + al.slot = std::min(al.slot, pos); } } diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index f8542dae7..473ad5ae6 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -4036,9 +4036,7 @@ bool CGameHandler::assembleArtifacts (ObjectInstanceID heroID, ArtifactPosition CArtifact *combinedArt = VLC->arth->objects[assembleTo]; if(!combinedArt->constituents) COMPLAIN_RET("assembleArtifacts: Artifact being attempted to assemble is not a combined artifacts!"); - bool combineEquipped = true; - if(artifactSlot >= GameConstants::BACKPACK_START) - combineEquipped = false; + bool combineEquipped = !ArtifactUtils::isSlotBackpack(artifactSlot); if(!vstd::contains(destArtifact->assemblyPossibilities(hero, combineEquipped), combinedArt)) COMPLAIN_RET("assembleArtifacts: It's impossible to assemble requested artifact!"); From 4b52d38c57158fd220fa15527f765f843d47f12e Mon Sep 17 00:00:00 2001 From: SoundSSGood <87084363+SoundSSGood@users.noreply.github.com> Date: Sat, 19 Nov 2022 16:09:32 +0200 Subject: [PATCH 6/7] artifact assemble fixed --- lib/CArtHandler.cpp | 6 ++++++ lib/CArtHandler.h | 1 + lib/NetPacksLib.cpp | 3 ++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/CArtHandler.cpp b/lib/CArtHandler.cpp index 3c1f457f5..b44d56330 100644 --- a/lib/CArtHandler.cpp +++ b/lib/CArtHandler.cpp @@ -1205,6 +1205,12 @@ ArtifactPosition CArtifactSet::getArtPos(int aid, bool onlyWorn, bool allowLocke return result.empty() ? ArtifactPosition{ArtifactPosition::PRE_FIRST} : result[0]; } +ArtifactPosition CArtifactSet::getArtBackpackPos(int aid) const +{ + const auto result = getBackpackArtPositions(aid); + return result.empty() ? ArtifactPosition{ArtifactPosition::PRE_FIRST} : result[0]; +} + std::vector CArtifactSet::getAllArtPositions(int aid, bool onlyWorn, bool allowLocked, bool getAll) const { std::vector result; diff --git a/lib/CArtHandler.h b/lib/CArtHandler.h index 21b7f26d3..978a00bbc 100644 --- a/lib/CArtHandler.h +++ b/lib/CArtHandler.h @@ -329,6 +329,7 @@ public: /// (if more than one such artifact lower ID is returned) ArtifactPosition getArtPos(int aid, bool onlyWorn = true, bool allowLocked = true) const; ArtifactPosition getArtPos(const CArtifactInstance *art) const; + ArtifactPosition getArtBackpackPos(int aid) const; std::vector getAllArtPositions(int aid, bool onlyWorn, bool allowLocked, bool getAll) const; std::vector getBackpackArtPositions(int aid) const; const CArtifactInstance *getArtByInstanceId(ArtifactInstanceID artInstId) const; diff --git a/lib/NetPacksLib.cpp b/lib/NetPacksLib.cpp index 539514f16..788c11437 100644 --- a/lib/NetPacksLib.cpp +++ b/lib/NetPacksLib.cpp @@ -1179,7 +1179,8 @@ DLL_LINKAGE void AssembledArtifact::applyGs(CGameState *gs) // Retrieve all constituents for(const CArtifact * constituent : *builtArt->constituents) { - ArtifactPosition pos = artSet->getArtPos(constituent->id, combineEquipped); + ArtifactPosition pos = combineEquipped ? artSet->getArtPos(constituent->id, true, false) : + artSet->getArtBackpackPos(constituent->id); assert(pos >= 0); CArtifactInstance * constituentInstance = artSet->getArt(pos); From 584f6a62306779d6c09b92a58e803d330bb1c872 Mon Sep 17 00:00:00 2001 From: SoundSSGood <87084363+SoundSSGood@users.noreply.github.com> Date: Sun, 20 Nov 2022 10:15:57 +0200 Subject: [PATCH 7/7] Update lib/CArtHandler.cpp Co-authored-by: Nordsoft91 --- lib/CArtHandler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/CArtHandler.cpp b/lib/CArtHandler.cpp index b44d56330..f8b3a8584 100644 --- a/lib/CArtHandler.cpp +++ b/lib/CArtHandler.cpp @@ -1236,7 +1236,7 @@ std::vector CArtifactSet::getBackpackArtPositions(int aid) con for(auto & artInfo : artifactsInBackpack) { auto * art = artInfo.getArt(); - if (art && art->artType->id == aid) + if(art && art->artType->id == aid) result.emplace_back(backpackPosition); backpackPosition++; }