From 9f688e6fb793aea58fd99acf7b9412fea787c2b3 Mon Sep 17 00:00:00 2001 From: SoundSSGood <87084363+SoundSSGood@users.noreply.github.com> Date: Thu, 7 Mar 2024 16:52:50 +0200 Subject: [PATCH] MoveArtifact, BulkMoveArtifacts PlayerColor player field --- client/Client.h | 2 +- client/NetPacksClient.cpp | 6 +++--- client/widgets/CArtifactsOfHeroBackpack.cpp | 6 +++--- client/widgets/CArtifactsOfHeroBackpack.h | 2 +- lib/IGameCallback.h | 2 +- lib/networkPacks/PacksForClient.h | 16 +++++++++++----- server/CGameHandler.cpp | 12 ++++++------ server/CGameHandler.h | 6 +++--- server/NetPacksServer.cpp | 8 ++++---- server/queries/MapQueries.cpp | 3 +++ test/mock/mock_IGameCallback.h | 2 +- 11 files changed, 37 insertions(+), 28 deletions(-) diff --git a/client/Client.h b/client/Client.h index 5fe64b6b1..9d700b4dc 100644 --- a/client/Client.h +++ b/client/Client.h @@ -191,7 +191,7 @@ public: bool giveHeroNewArtifact(const CGHeroInstance * h, const CArtifact * artType, ArtifactPosition pos) override {return false;} bool putArtifact(const ArtifactLocation & al, const CArtifactInstance * art, std::optional askAssemble) override {return false;}; void removeArtifact(const ArtifactLocation & al) override {}; - bool moveArtifact(const ArtifactLocation & al1, const ArtifactLocation & al2) override {return false;}; + bool moveArtifact(const PlayerColor & player, const ArtifactLocation & al1, const ArtifactLocation & al2) override {return false;}; void heroVisitCastle(const CGTownInstance * obj, const CGHeroInstance * hero) override {}; void visitCastleObjects(const CGTownInstance * obj, const CGHeroInstance * hero) override {}; diff --git a/client/NetPacksClient.cpp b/client/NetPacksClient.cpp index a6cbec495..f72519213 100644 --- a/client/NetPacksClient.cpp +++ b/client/NetPacksClient.cpp @@ -290,8 +290,8 @@ void ApplyClientNetPackVisitor::visitMoveArtifact(MoveArtifact & pack) callInterfaceIfPresent(cl, player, &IGameEventsReceiver::askToAssembleArtifact, pack.dst); }; - moveArtifact(LOCPLINT->playerID); - if(cl.getOwner(pack.src.artHolder) != cl.getOwner(pack.dst.artHolder)) + moveArtifact(pack.interfaceOwner); + if(pack.interfaceOwner != cl.getOwner(pack.dst.artHolder)) moveArtifact(cl.getOwner(pack.dst.artHolder)); cl.invalidatePaths(); // hero might have equipped/unequipped Angel Wings @@ -305,7 +305,7 @@ void ApplyClientNetPackVisitor::visitBulkMoveArtifacts(BulkMoveArtifacts & pack) { auto srcLoc = ArtifactLocation(pack.srcArtHolder, slotToMove.srcPos); auto dstLoc = ArtifactLocation(pack.dstArtHolder, slotToMove.dstPos); - MoveArtifact ma(&srcLoc, &dstLoc, pack.askAssemble); + MoveArtifact ma(pack.interfaceOwner, srcLoc, dstLoc, pack.askAssemble); visitMoveArtifact(ma); } }; diff --git a/client/widgets/CArtifactsOfHeroBackpack.cpp b/client/widgets/CArtifactsOfHeroBackpack.cpp index e3a54c647..88a82b2cb 100644 --- a/client/widgets/CArtifactsOfHeroBackpack.cpp +++ b/client/widgets/CArtifactsOfHeroBackpack.cpp @@ -42,11 +42,11 @@ CArtifactsOfHeroBackpack::CArtifactsOfHeroBackpack() initAOHbackpack(visibleCapacityMax, backpackCap < 0 || visibleCapacityMax < backpackCap); } -void CArtifactsOfHeroBackpack::scrollBackpack(int offset) +void CArtifactsOfHeroBackpack::onSliderMoved(int newVal) { if(backpackListBox) backpackListBox->resize(getActiveSlotRowsNum()); - backpackPos += offset; + backpackPos += newVal; auto slot = ArtifactPosition::BACKPACK_START + backpackPos; for(auto artPlace : backpack) { @@ -99,7 +99,7 @@ void CArtifactsOfHeroBackpack::initAOHbackpack(size_t slots, bool slider) }; CListBoxWithCallback::MovedPosCallback posMoved = [this](size_t pos) -> void { - scrollBackpack(static_cast(pos) * slotsColumnsMax - backpackPos); + onSliderMoved(static_cast(pos) * slotsColumnsMax - backpackPos); }; backpackListBox = std::make_shared( posMoved, onCreate, Point(0, 0), Point(0, 0), slotsRowsMax, 0, 0, 1, diff --git a/client/widgets/CArtifactsOfHeroBackpack.h b/client/widgets/CArtifactsOfHeroBackpack.h index 4a53bf790..1f6b1cf20 100644 --- a/client/widgets/CArtifactsOfHeroBackpack.h +++ b/client/widgets/CArtifactsOfHeroBackpack.h @@ -24,7 +24,7 @@ class CArtifactsOfHeroBackpack : public CArtifactsOfHeroBase public: CArtifactsOfHeroBackpack(size_t slotsColumnsMax, size_t slotsRowsMax); CArtifactsOfHeroBackpack(); - void scrollBackpack(int offset); + void onSliderMoved(int newVal); void updateBackpackSlots() override; size_t getActiveSlotRowsNum(); size_t getSlotsNum(); diff --git a/lib/IGameCallback.h b/lib/IGameCallback.h index 95151d30e..bbe53af1d 100644 --- a/lib/IGameCallback.h +++ b/lib/IGameCallback.h @@ -110,7 +110,7 @@ public: virtual bool giveHeroNewArtifact(const CGHeroInstance * h, const CArtifact * artType, ArtifactPosition pos) = 0; virtual bool putArtifact(const ArtifactLocation & al, const CArtifactInstance * art, std::optional askAssemble = std::nullopt) = 0; virtual void removeArtifact(const ArtifactLocation &al) = 0; - virtual bool moveArtifact(const ArtifactLocation &al1, const ArtifactLocation &al2) = 0; + virtual bool moveArtifact(const PlayerColor & player, const ArtifactLocation & al1, const ArtifactLocation & al2) = 0; virtual void heroVisitCastle(const CGTownInstance * obj, const CGHeroInstance * hero)=0; virtual void visitCastleObjects(const CGTownInstance * obj, const CGHeroInstance * hero)=0; diff --git a/lib/networkPacks/PacksForClient.h b/lib/networkPacks/PacksForClient.h index 85e07e1bd..934dbbd26 100644 --- a/lib/networkPacks/PacksForClient.h +++ b/lib/networkPacks/PacksForClient.h @@ -1030,10 +1030,11 @@ struct DLL_LINKAGE EraseArtifact : CArtifactOperationPack struct DLL_LINKAGE MoveArtifact : CArtifactOperationPack { MoveArtifact() = default; - MoveArtifact(ArtifactLocation * src, ArtifactLocation * dst, bool askAssemble = true) - : src(*src), dst(*dst), askAssemble(askAssemble) + MoveArtifact(const PlayerColor & interfaceOwner, const ArtifactLocation & src, ArtifactLocation & dst, bool askAssemble = true) + : interfaceOwner(interfaceOwner), src(src), dst(dst), askAssemble(askAssemble) { } + PlayerColor interfaceOwner; ArtifactLocation src; ArtifactLocation dst; bool askAssemble = true; @@ -1043,6 +1044,7 @@ struct DLL_LINKAGE MoveArtifact : CArtifactOperationPack template void serialize(Handler & h) { + h & interfaceOwner; h & src; h & dst; h & askAssemble; @@ -1069,13 +1071,15 @@ struct DLL_LINKAGE BulkMoveArtifacts : CArtifactOperationPack } }; + PlayerColor interfaceOwner; ObjectInstanceID srcArtHolder; ObjectInstanceID dstArtHolder; std::optional srcCreature; std::optional dstCreature; BulkMoveArtifacts() - : srcArtHolder(ObjectInstanceID::NONE) + : interfaceOwner(PlayerColor::NEUTRAL) + , srcArtHolder(ObjectInstanceID::NONE) , dstArtHolder(ObjectInstanceID::NONE) , swap(false) , askAssemble(false) @@ -1083,8 +1087,9 @@ struct DLL_LINKAGE BulkMoveArtifacts : CArtifactOperationPack , dstCreature(std::nullopt) { } - BulkMoveArtifacts(const ObjectInstanceID srcArtHolder, const ObjectInstanceID dstArtHolder, bool swap) - : srcArtHolder(std::move(srcArtHolder)) + BulkMoveArtifacts(const PlayerColor & interfaceOwner, const ObjectInstanceID srcArtHolder, const ObjectInstanceID dstArtHolder, bool swap) + : interfaceOwner(interfaceOwner) + , srcArtHolder(std::move(srcArtHolder)) , dstArtHolder(std::move(dstArtHolder)) , swap(swap) , askAssemble(false) @@ -1104,6 +1109,7 @@ struct DLL_LINKAGE BulkMoveArtifacts : CArtifactOperationPack template void serialize(Handler & h) { + h & interfaceOwner; h & artsPack0; h & artsPack1; h & srcArtHolder; diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 9ee08fb8b..76f53703d 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -2690,7 +2690,7 @@ bool CGameHandler::garrisonSwap(ObjectInstanceID tid) // With the amount of changes done to the function, it's more like transferArtifacts. // Function moves artifact from src to dst. If dst is not a backpack and is already occupied, old dst art goes to backpack and is replaced. -bool CGameHandler::moveArtifact(const ArtifactLocation & src, const ArtifactLocation & dst) +bool CGameHandler::moveArtifact(const PlayerColor & player, const ArtifactLocation & src, const ArtifactLocation & dst) { const auto srcArtSet = getArtSet(src); const auto dstArtSet = getArtSet(dst); @@ -2733,7 +2733,7 @@ bool CGameHandler::moveArtifact(const ArtifactLocation & src, const ArtifactLoca if(src.slot == dstSlot && src.artHolder == dst.artHolder) COMPLAIN_RET("Won't move artifact: Dest same as source!"); - BulkMoveArtifacts ma(src.artHolder, dst.artHolder, false); + BulkMoveArtifacts ma(player, src.artHolder, dst.artHolder, false); ma.srcCreature = src.creature; ma.dstCreature = dst.creature; @@ -2756,7 +2756,7 @@ bool CGameHandler::moveArtifact(const ArtifactLocation & src, const ArtifactLoca return true; } -bool CGameHandler::bulkMoveArtifacts(ObjectInstanceID srcId, ObjectInstanceID dstId, bool swap, bool equipped, bool backpack) +bool CGameHandler::bulkMoveArtifacts(const PlayerColor & player, ObjectInstanceID srcId, ObjectInstanceID dstId, bool swap, bool equipped, bool backpack) { // Make sure exchange is even possible between the two heroes. if(!isAllowedExchange(srcId, dstId)) @@ -2767,7 +2767,7 @@ bool CGameHandler::bulkMoveArtifacts(ObjectInstanceID srcId, ObjectInstanceID ds if((!psrcSet) || (!pdstSet)) COMPLAIN_RET("bulkMoveArtifacts: wrong hero's ID"); - BulkMoveArtifacts ma(srcId, dstId, swap); + BulkMoveArtifacts ma(player, srcId, dstId, swap); auto & slotsSrcDst = ma.artsPack0; auto & slotsDstSrc = ma.artsPack1; @@ -2857,12 +2857,12 @@ bool CGameHandler::bulkMoveArtifacts(ObjectInstanceID srcId, ObjectInstanceID ds return true; } -bool CGameHandler::scrollBackpackArtifacts(const ObjectInstanceID heroID, bool left) +bool CGameHandler::scrollBackpackArtifacts(const PlayerColor & player, const ObjectInstanceID heroID, bool left) { auto artSet = getArtSet(heroID); COMPLAIN_RET_FALSE_IF(artSet == nullptr, "scrollBackpackArtifacts: wrong hero's ID"); - BulkMoveArtifacts bma(heroID, heroID, false); + BulkMoveArtifacts bma(player, heroID, heroID, false); const auto backpackEnd = ArtifactPosition(ArtifactPosition::BACKPACK_START + artSet->artifactsInBackpack.size() - 1); if(backpackEnd > ArtifactPosition::BACKPACK_START) diff --git a/server/CGameHandler.h b/server/CGameHandler.h index 31b302082..5a9b0c95f 100644 --- a/server/CGameHandler.h +++ b/server/CGameHandler.h @@ -128,9 +128,9 @@ public: bool giveHeroNewArtifact(const CGHeroInstance * h, const CArtifact * artType, ArtifactPosition pos = ArtifactPosition::FIRST_AVAILABLE) override; bool putArtifact(const ArtifactLocation & al, const CArtifactInstance * art, std::optional askAssemble) override; void removeArtifact(const ArtifactLocation &al) override; - bool moveArtifact(const ArtifactLocation & src, const ArtifactLocation & dst) override; - bool bulkMoveArtifacts(ObjectInstanceID srcId, ObjectInstanceID dstId, bool swap, bool equipped, bool backpack); - bool scrollBackpackArtifacts(const ObjectInstanceID heroID, bool left); + bool moveArtifact(const PlayerColor & player, const ArtifactLocation & src, const ArtifactLocation & dst) override; + bool bulkMoveArtifacts(const PlayerColor & player, ObjectInstanceID srcId, ObjectInstanceID dstId, bool swap, bool equipped, bool backpack); + bool scrollBackpackArtifacts(const PlayerColor & player, const ObjectInstanceID heroID, bool left); bool eraseArtifactByClient(const ArtifactLocation & al); void synchronizeArtifactHandlerLists(); diff --git a/server/NetPacksServer.cpp b/server/NetPacksServer.cpp index 8cadd54cd..aabe9e0bf 100644 --- a/server/NetPacksServer.cpp +++ b/server/NetPacksServer.cpp @@ -136,7 +136,7 @@ void ApplyGhNetPackVisitor::visitExchangeArtifacts(ExchangeArtifacts & pack) { if(gh.getHero(pack.src.artHolder)) gh.throwIfWrongPlayer(&pack, gh.getOwner(pack.src.artHolder)); //second hero can be ally - result = gh.moveArtifact(pack.src, pack.dst); + result = gh.moveArtifact(pack.player, pack.src, pack.dst); } void ApplyGhNetPackVisitor::visitBulkExchangeArtifacts(BulkExchangeArtifacts & pack) @@ -145,7 +145,7 @@ void ApplyGhNetPackVisitor::visitBulkExchangeArtifacts(BulkExchangeArtifacts & p gh.throwIfWrongOwner(&pack, pack.srcHero); if(pack.swap) gh.throwIfWrongOwner(&pack, pack.dstHero); - result = gh.bulkMoveArtifacts(pack.srcHero, pack.dstHero, pack.swap, pack.equipped, pack.backpack); + result = gh.bulkMoveArtifacts(pack.player, pack.srcHero, pack.dstHero, pack.swap, pack.equipped, pack.backpack); } void ApplyGhNetPackVisitor::visitManageBackpackArtifacts(ManageBackpackArtifacts & pack) @@ -153,9 +153,9 @@ void ApplyGhNetPackVisitor::visitManageBackpackArtifacts(ManageBackpackArtifacts if(gh.getPlayerRelations(pack.player, gh.getOwner(pack.artHolder)) != PlayerRelations::ENEMIES) { if(pack.cmd == ManageBackpackArtifacts::ManageCmd::SCROLL_LEFT) - result = gh.scrollBackpackArtifacts(pack.artHolder, true); + result = gh.scrollBackpackArtifacts(pack.player, pack.artHolder, true); else if(pack.cmd == ManageBackpackArtifacts::ManageCmd::SCROLL_RIGHT) - result = gh.scrollBackpackArtifacts(pack.artHolder, false); + result = gh.scrollBackpackArtifacts(pack.player, pack.artHolder, false); else { gh.throwIfWrongOwner(&pack, pack.artHolder); diff --git a/server/queries/MapQueries.cpp b/server/queries/MapQueries.cpp index 69b25d9c7..f3c62cb41 100644 --- a/server/queries/MapQueries.cpp +++ b/server/queries/MapQueries.cpp @@ -206,6 +206,9 @@ bool OpenWindowQuery::blocksPack(const CPack *pack) const if(dynamic_ptr_cast(pack) != nullptr) return false; + if(dynamic_ptr_cast(pack) != nullptr) + return false; + if(dynamic_ptr_cast(pack)) return false; diff --git a/test/mock/mock_IGameCallback.h b/test/mock/mock_IGameCallback.h index 077001c85..c42e35df9 100644 --- a/test/mock/mock_IGameCallback.h +++ b/test/mock/mock_IGameCallback.h @@ -70,7 +70,7 @@ public: bool giveHeroNewArtifact(const CGHeroInstance * h, const CArtifact * artType, ArtifactPosition pos) override {return false;} bool putArtifact(const ArtifactLocation & al, const CArtifactInstance * art, std::optional askAssemble) override {return false;} void removeArtifact(const ArtifactLocation &al) override {} - bool moveArtifact(const ArtifactLocation &al1, const ArtifactLocation &al2) override {return false;} + bool moveArtifact(const PlayerColor & player, const ArtifactLocation & al1, const ArtifactLocation & al2) override {return false;} void heroVisitCastle(const CGTownInstance * obj, const CGHeroInstance * hero) override {} void stopHeroVisitCastle(const CGTownInstance * obj, const CGHeroInstance * hero) override {}