From cd7ebea9e3cd30472a13113a95bd530e46477603 Mon Sep 17 00:00:00 2001 From: SoundSSGood <87084363+SoundSSGood@users.noreply.github.com> Date: Sat, 17 Aug 2024 20:19:48 +0300 Subject: [PATCH] cleanup unused code --- client/CPlayerInterface.cpp | 6 +++--- client/widgets/markets/CAltarArtifacts.cpp | 16 ++++++++-------- client/widgets/markets/CAltarArtifacts.h | 2 +- client/windows/CMarketWindow.cpp | 8 ++++---- lib/IGameCallback.cpp | 10 ++++------ .../CommonConstructors.cpp | 5 ----- lib/mapObjects/CGMarket.cpp | 5 ----- lib/mapObjects/CGMarket.h | 14 -------------- lib/mapObjects/IMarket.cpp | 18 +++++++++--------- lib/mapObjects/IMarket.h | 8 ++++---- lib/registerTypes/RegisterTypesMapObjects.h | 2 -- server/CGameHandler.cpp | 12 ++++++------ server/CGameHandler.h | 2 +- server/NetPacksServer.cpp | 2 +- 14 files changed, 41 insertions(+), 69 deletions(-) diff --git a/client/CPlayerInterface.cpp b/client/CPlayerInterface.cpp index b75a3216c..5c180f8b1 100644 --- a/client/CPlayerInterface.cpp +++ b/client/CPlayerInterface.cpp @@ -1639,7 +1639,7 @@ void CPlayerInterface::showMarketWindow(const IMarket * market, const ObjectInst cb->selectionMade(0, queryID); }; - if (market->allowsTrade(EMarketMode::ARTIFACT_EXP) && dynamic_cast(market) == nullptr) + if (market->allowsTrade(EMarketMode::ARTIFACT_EXP) && market->getArtifactsStorage() == nullptr) { // compatibility check, safe to remove for 1.6 // 1.4 saves loaded in 1.5 will not be able to visit Altar of Sacrifice due to Altar now requiring different map object class @@ -1654,8 +1654,8 @@ void CPlayerInterface::showMarketWindow(const IMarket * market, const ObjectInst GH.windows().createAndPushWindow(market, visitor, marketId, onWindowClosed, EMarketMode::CREATURE_EXP); else if(market->allowsTrade(EMarketMode::CREATURE_UNDEAD)) GH.windows().createAndPushWindow(market, visitor, onWindowClosed); - else if(!market->availableModes().empty()) - GH.windows().createAndPushWindow(market, visitor, marketId, onWindowClosed, market->availableModes().front()); + else if(vstd::contains(market->availableModes(), EMarketMode::RESOURCE_RESOURCE)) + GH.windows().createAndPushWindow(market, visitor, marketId, onWindowClosed, EMarketMode::RESOURCE_RESOURCE); } void CPlayerInterface::showUniversityWindow(const IMarket *market, const CGHeroInstance *visitor, QueryID queryID) diff --git a/client/widgets/markets/CAltarArtifacts.cpp b/client/widgets/markets/CAltarArtifacts.cpp index 4a409188b..1c8cfb3dd 100644 --- a/client/widgets/markets/CAltarArtifacts.cpp +++ b/client/widgets/markets/CAltarArtifacts.cpp @@ -31,8 +31,8 @@ CAltarArtifacts::CAltarArtifacts(const IMarket * market, const CGHeroInstance * { OBJECT_CONSTRUCTION; - assert(dynamic_cast(market)); - altarArtifacts = dynamic_cast(market); + assert(market->getArtifactsStorage()); + altarArtifactsStorage = market->getArtifactsStorage(); deal = std::make_shared(Point(269, 520), AnimationPath::builtin("ALTSACR.DEF"), CGI->generaltexth->zelp[585], [this]() {CAltarArtifacts::makeDeal(); }, EShortcut::MARKET_DEAL); @@ -124,7 +124,7 @@ std::shared_ptr CAltarArtifacts::getAOHset() const void CAltarArtifacts::updateAltarSlots() { - assert(altarArtifacts->artifactsInBackpack.size() <= GameConstants::ALTAR_ARTIFACTS_SLOTS); + assert(altarArtifactsStorage->artifactsInBackpack.size() <= GameConstants::ALTAR_ARTIFACTS_SLOTS); assert(tradeSlotsMap.size() <= GameConstants::ALTAR_ARTIFACTS_SLOTS); auto tradeSlotsMapNewArts = tradeSlotsMap; @@ -145,12 +145,12 @@ void CAltarArtifacts::updateAltarSlots() for(auto & tradeSlot : tradeSlotsMapNewArts) { assert(tradeSlot.first->id == -1); - assert(altarArtifacts->getArtPos(tradeSlot.second) != ArtifactPosition::PRE_FIRST); + assert(altarArtifactsStorage->getArtPos(tradeSlot.second) != ArtifactPosition::PRE_FIRST); tradeSlot.first->setID(tradeSlot.second->getTypeId().num); tradeSlot.first->subtitle->setText(std::to_string(calcExpCost(tradeSlot.second->getTypeId()))); } - auto newArtsFromBulkMove = altarArtifacts->artifactsInBackpack; + auto newArtsFromBulkMove = altarArtifactsStorage->artifactsInBackpack; for(const auto & [altarSlot, art] : tradeSlotsMap) { newArtsFromBulkMove.erase(std::remove_if(newArtsFromBulkMove.begin(), newArtsFromBulkMove.end(), [artForRemove = art](auto & slotInfo) @@ -178,7 +178,7 @@ void CAltarArtifacts::putBackArtifacts() { // TODO: If the backpack capacity limit is enabled, artifacts may remain on the altar. // Perhaps should be erased in CGameHandler::objectVisitEnded if id of visited object will be available - if(!altarArtifacts->artifactsInBackpack.empty()) + if(!altarArtifactsStorage->artifactsInBackpack.empty()) LOCPLINT->cb->bulkMoveArtifacts(heroArts->altarId, heroArts->getHero()->id, false, true, true); } @@ -199,7 +199,7 @@ void CAltarArtifacts::onSlotClickPressed(const std::shared_ptr & if(const auto pickedArtInst = heroArts->getPickedArtifact()) { - if(pickedArtInst->canBePutAt(altarArtifacts)) + if(pickedArtInst->canBePutAt(altarArtifactsStorage.get())) { if(pickedArtInst->artType->isTradable()) { @@ -220,7 +220,7 @@ void CAltarArtifacts::onSlotClickPressed(const std::shared_ptr & else if(altarSlot->id != -1) { assert(tradeSlotsMap.at(altarSlot)); - const auto slot = altarArtifacts->getArtPos(tradeSlotsMap.at(altarSlot)); + const auto slot = altarArtifactsStorage->getArtPos(tradeSlotsMap.at(altarSlot)); assert(slot != ArtifactPosition::PRE_FIRST); LOCPLINT->cb->swapArtifacts(ArtifactLocation(heroArts->altarId, slot), ArtifactLocation(hero->id, GH.isKeyboardCtrlDown() ? ArtifactPosition::FIRST_AVAILABLE : ArtifactPosition::TRANSITION_POS)); diff --git a/client/widgets/markets/CAltarArtifacts.h b/client/widgets/markets/CAltarArtifacts.h index ab9aed3ce..6566cb20a 100644 --- a/client/widgets/markets/CAltarArtifacts.h +++ b/client/widgets/markets/CAltarArtifacts.h @@ -26,7 +26,7 @@ public: void putBackArtifacts(); private: - const CArtifactSet * altarArtifacts; + std::shared_ptr altarArtifactsStorage; std::shared_ptr sacrificeBackpackButton; std::shared_ptr heroArts; std::map, const CArtifactInstance*> tradeSlotsMap; diff --git a/client/windows/CMarketWindow.cpp b/client/windows/CMarketWindow.cpp index 8e65ee379..b81f0b90e 100644 --- a/client/windows/CMarketWindow.cpp +++ b/client/windows/CMarketWindow.cpp @@ -239,10 +239,10 @@ void CMarketWindow::createAltarArtifacts(const IMarket * market, const CGHeroIns OBJECT_CONSTRUCTION; background = createBg(ImagePath::builtin("ALTRART2.bmp"), PLAYER_COLORED); - auto altarArtifacts = std::make_shared(market, hero, marketId); - marketWidget = altarArtifacts; + auto altarArtifactsStorage = std::make_shared(market, hero, marketId); + marketWidget = altarArtifactsStorage; artSets.clear(); - const auto heroArts = altarArtifacts->getAOHset(); + const auto heroArts = altarArtifactsStorage->getAOHset(); heroArts->clickPressedCallback = [this, heroArts](const CArtPlace & artPlace, const Point & cursorPosition) { clickPressedOnArtPlace(heroArts->getHero(), artPlace.slot, true, true, false); @@ -258,7 +258,7 @@ void CMarketWindow::createAltarArtifacts(const IMarket * market, const CGHeroIns addSet(heroArts); initWidgetInternals(EMarketMode::ARTIFACT_EXP, CGI->generaltexth->zelp[568]); updateExperience(); - quitButton->addCallback([altarArtifacts](){altarArtifacts->putBackArtifacts();}); + quitButton->addCallback([altarArtifactsStorage](){altarArtifactsStorage->putBackArtifacts();}); } void CMarketWindow::createAltarCreatures(const IMarket * market, const CGHeroInstance * hero) diff --git a/lib/IGameCallback.cpp b/lib/IGameCallback.cpp index 3a70daa64..8eceeaa9c 100644 --- a/lib/IGameCallback.cpp +++ b/lib/IGameCallback.cpp @@ -291,14 +291,12 @@ CArtifactSet * CNonConstInfoCallback::getArtSet(const ArtifactLocation & loc) { return army->getStackPtr(loc.creature.value()); } - else if(auto market = dynamic_cast(getObjInstance(loc.artHolder))) + else if(auto market = dynamic_cast(getObjInstance(loc.artHolder))) { - return market; - } - else - { - return nullptr; + if(auto artSet = market->getArtifactsStorage()) + return artSet.get(); } + return nullptr; } bool IGameCallback::isVisitCoveredByAnotherQuery(const CGObjectInstance *obj, const CGHeroInstance *hero) diff --git a/lib/mapObjectConstructors/CommonConstructors.cpp b/lib/mapObjectConstructors/CommonConstructors.cpp index 3d2f12be1..094c1a357 100644 --- a/lib/mapObjectConstructors/CommonConstructors.cpp +++ b/lib/mapObjectConstructors/CommonConstructors.cpp @@ -238,11 +238,6 @@ CGMarket * MarketInstanceConstructor::createObject(IGameCallback * cb) const return new CGUniversity(cb); } } - else if(marketModes.size() == 2) - { - if(vstd::contains(marketModes, EMarketMode::ARTIFACT_EXP)) - return new CGArtifactsAltar(cb); - } return new CGMarket(cb); } diff --git a/lib/mapObjects/CGMarket.cpp b/lib/mapObjects/CGMarket.cpp index 2de4ef257..9ae871bc6 100644 --- a/lib/mapObjects/CGMarket.cpp +++ b/lib/mapObjects/CGMarket.cpp @@ -108,9 +108,4 @@ void CGUniversity::onHeroVisit(const CGHeroInstance * h) const cb->showObjectWindow(this, EOpenWindowMode::UNIVERSITY_WINDOW, h, true); } -ArtBearer::ArtBearer CGArtifactsAltar::bearerType() const -{ - return ArtBearer::ALTAR; -} - VCMI_LIB_NAMESPACE_END diff --git a/lib/mapObjects/CGMarket.h b/lib/mapObjects/CGMarket.h index 0c69354b6..ae0b0c5c3 100644 --- a/lib/mapObjects/CGMarket.h +++ b/lib/mapObjects/CGMarket.h @@ -76,18 +76,4 @@ public: } }; -class DLL_LINKAGE CGArtifactsAltar : public CGMarket, public CArtifactSet -{ -public: - using CGMarket::CGMarket; - - ArtBearer::ArtBearer bearerType() const override; - - template void serialize(Handler & h) - { - h & static_cast(*this); - h & static_cast(*this); - } -}; - VCMI_LIB_NAMESPACE_END diff --git a/lib/mapObjects/IMarket.cpp b/lib/mapObjects/IMarket.cpp index fab8a4c2d..11315260a 100644 --- a/lib/mapObjects/IMarket.cpp +++ b/lib/mapObjects/IMarket.cpp @@ -145,7 +145,7 @@ void IMarket::addMarketMode(const EMarketMode mode) marketModes.insert(mode); if(mode == EMarketMode::ARTIFACT_EXP) - altarArtifacts = std::make_shared(); + altarArtifactsStorage = std::make_shared(); } void IMarket::addMarketMode(const std::set & modes) @@ -159,7 +159,12 @@ void IMarket::removeMarketMode(const EMarketMode mode) marketModes.erase(mode); if(mode == EMarketMode::ARTIFACT_EXP) - altarArtifacts.reset(); + altarArtifactsStorage.reset(); +} + +std::shared_ptr IMarket::getArtifactsStorage() const +{ + return altarArtifactsStorage; } std::vector IMarket::availableItemsIds(const EMarketMode mode) const @@ -176,14 +181,9 @@ std::vector IMarket::availableItemsIds(const EMarketMode mode) con return ret; } -std::vector IMarket::availableModes() const +std::set IMarket::availableModes() const { - std::vector ret; - for (EMarketMode i = static_cast(0); i < EMarketMode::MARKET_AFTER_LAST_PLACEHOLDER; i = vstd::next(i, 1)) - if(allowsTrade(i)) - ret.push_back(i); - - return ret; + return marketModes; } VCMI_LIB_NAMESPACE_END diff --git a/lib/mapObjects/IMarket.h b/lib/mapObjects/IMarket.h index fa64e81c4..efdd8e53e 100644 --- a/lib/mapObjects/IMarket.h +++ b/lib/mapObjects/IMarket.h @@ -31,20 +31,20 @@ public: void addMarketMode(const EMarketMode mode); void addMarketMode(const std::set & modes); void removeMarketMode(const EMarketMode mode); - + std::set availableModes() const; + std::shared_ptr getArtifactsStorage() const; bool getOffer(int id1, int id2, int &val1, int &val2, EMarketMode mode) const; //val1 - how many units of id1 player has to give to receive val2 units - std::vector availableModes() const; template void serialize(Handler & h) { h & marketModes; if(h.loadingGamestate && vstd::contains(marketModes, EMarketMode::ARTIFACT_EXP)) - altarArtifacts = std::make_shared(); + altarArtifactsStorage = std::make_shared(); } private: - std::shared_ptr altarArtifacts; + std::shared_ptr altarArtifactsStorage; std::set marketModes; }; diff --git a/lib/registerTypes/RegisterTypesMapObjects.h b/lib/registerTypes/RegisterTypesMapObjects.h index 501ea0e72..bc3802e04 100644 --- a/lib/registerTypes/RegisterTypesMapObjects.h +++ b/lib/registerTypes/RegisterTypesMapObjects.h @@ -132,8 +132,6 @@ void registerTypesMapObjects(Serializer &s) //s.template registerType(); s.template registerType(); - - s.template registerType(); } VCMI_LIB_NAMESPACE_END diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 52139e9e5..c757eaf40 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -3909,15 +3909,15 @@ bool CGameHandler::sacrificeCreatures(const IMarket * market, const CGHeroInstan return true; } -bool CGameHandler::sacrificeArtifact(const IMarket * m, const CGHeroInstance * hero, const std::vector & arts) +bool CGameHandler::sacrificeArtifact(const IMarket * market, const CGHeroInstance * hero, const ObjectInstanceID & marketId, const std::vector & arts) { if (!hero) COMPLAIN_RET("You need hero to sacrifice artifact!"); if(hero->getAlignment() == EAlignment::EVIL) COMPLAIN_RET("Evil hero can't sacrifice artifact!"); - assert(m); - auto altarObj = dynamic_cast(m); + assert(market); + const auto artSet = getArtSet(marketId); int expSum = 0; auto finish = [this, &hero, &expSum]() @@ -3927,15 +3927,15 @@ bool CGameHandler::sacrificeArtifact(const IMarket * m, const CGHeroInstance * h for(const auto & artInstId : arts) { - if(auto art = altarObj->getArtByInstanceId(artInstId)) + if(auto art = artSet->getArtByInstanceId(artInstId)) { if(art->artType->isTradable()) { int dmp; int expToGive; - m->getOffer(art->getTypeId(), 0, dmp, expToGive, EMarketMode::ARTIFACT_EXP); + market->getOffer(art->getTypeId(), 0, dmp, expToGive, EMarketMode::ARTIFACT_EXP); expSum += expToGive; - removeArtifact(ArtifactLocation(altarObj->id, altarObj->getArtPos(art))); + removeArtifact(ArtifactLocation(marketId, artSet->getArtPos(art))); } else { diff --git a/server/CGameHandler.h b/server/CGameHandler.h index 4ce8a0d85..cba7bd142 100644 --- a/server/CGameHandler.h +++ b/server/CGameHandler.h @@ -281,7 +281,7 @@ public: void start(bool resume); void tick(int millisecondsPassed); - bool sacrificeArtifact(const IMarket * m, const CGHeroInstance * hero, const std::vector & arts); + bool sacrificeArtifact(const IMarket * market, const CGHeroInstance * hero, const ObjectInstanceID & marketId, const std::vector & arts); void spawnWanderingMonsters(CreatureID creatureID); // Check for victory and loss conditions diff --git a/server/NetPacksServer.cpp b/server/NetPacksServer.cpp index 17217dac6..29d5462d8 100644 --- a/server/NetPacksServer.cpp +++ b/server/NetPacksServer.cpp @@ -347,7 +347,7 @@ void ApplyGhNetPackVisitor::visitTradeOnMarketplace(TradeOnMarketplace & pack) for(auto const & artInstId : pack.r1) positions.push_back(artInstId.as()); - result = gh.sacrificeArtifact(market, hero, positions); + result = gh.sacrificeArtifact(market, hero, pack.marketId, positions); return; } default: