From 716dd9a43bd9c6cda3b0f6f479038cd36bcd8bf0 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Wed, 8 Mar 2023 01:32:21 +0300 Subject: [PATCH] vcmi: remove a bunch of duplicated showInfoDialog These methods duplicated in almost every map object Just replace it by one such method --- client/NetPacksClient.cpp | 20 ++++++------- lib/NetPacks.h | 7 +---- lib/NetPacksBase.h | 14 +++++++++ lib/mapObjects/CGHeroInstance.cpp | 19 +----------- lib/mapObjects/CGMarket.cpp | 14 ++------- lib/mapObjects/CGPandoraBox.cpp | 21 ++----------- lib/mapObjects/CGTownInstance.cpp | 4 +-- lib/mapObjects/CObjectHandler.cpp | 25 +++++----------- lib/mapObjects/CObjectHandler.h | 9 +++++- lib/mapObjects/CQuest.cpp | 22 ++------------ lib/mapObjects/MiscObjects.cpp | 49 ++++++++----------------------- server/CGameHandler.cpp | 2 +- 12 files changed, 63 insertions(+), 143 deletions(-) diff --git a/client/NetPacksClient.cpp b/client/NetPacksClient.cpp index dee2ebf71..e9e859018 100644 --- a/client/NetPacksClient.cpp +++ b/client/NetPacksClient.cpp @@ -875,28 +875,28 @@ void ApplyClientNetPackVisitor::visitOpenWindow(OpenWindow & pack) { switch(pack.window) { - case OpenWindow::RECRUITMENT_FIRST: - case OpenWindow::RECRUITMENT_ALL: + case EOpenWindowMode::RECRUITMENT_FIRST: + case EOpenWindowMode::RECRUITMENT_ALL: { const CGDwelling *dw = dynamic_cast(cl.getObj(ObjectInstanceID(pack.id1))); const CArmedInstance *dst = dynamic_cast(cl.getObj(ObjectInstanceID(pack.id2))); - callInterfaceIfPresent(cl, dst->tempOwner, &IGameEventsReceiver::showRecruitmentDialog, dw, dst, pack.window == OpenWindow::RECRUITMENT_FIRST ? 0 : -1); + callInterfaceIfPresent(cl, dst->tempOwner, &IGameEventsReceiver::showRecruitmentDialog, dw, dst, pack.window == EOpenWindowMode::RECRUITMENT_FIRST ? 0 : -1); } break; - case OpenWindow::SHIPYARD_WINDOW: + case EOpenWindowMode::SHIPYARD_WINDOW: { const IShipyard *sy = IShipyard::castFrom(cl.getObj(ObjectInstanceID(pack.id1))); callInterfaceIfPresent(cl, sy->o->tempOwner, &IGameEventsReceiver::showShipyardDialog, sy); } break; - case OpenWindow::THIEVES_GUILD: + case EOpenWindowMode::THIEVES_GUILD: { //displays Thieves' Guild window (when hero enters Den of Thieves) const CGObjectInstance *obj = cl.getObj(ObjectInstanceID(pack.id2)); callInterfaceIfPresent(cl, PlayerColor(pack.id1), &IGameEventsReceiver::showThievesGuildWindow, obj); } break; - case OpenWindow::UNIVERSITY_WINDOW: + case EOpenWindowMode::UNIVERSITY_WINDOW: { //displays University window (when hero enters University on adventure map) const IMarket *market = IMarket::castFrom(cl.getObj(ObjectInstanceID(pack.id1))); @@ -904,7 +904,7 @@ void ApplyClientNetPackVisitor::visitOpenWindow(OpenWindow & pack) callInterfaceIfPresent(cl, hero->tempOwner, &IGameEventsReceiver::showUniversityWindow, market, hero); } break; - case OpenWindow::MARKET_WINDOW: + case EOpenWindowMode::MARKET_WINDOW: { //displays Thieves' Guild window (when hero enters Den of Thieves) const CGObjectInstance *obj = cl.getObj(ObjectInstanceID(pack.id1)); @@ -913,7 +913,7 @@ void ApplyClientNetPackVisitor::visitOpenWindow(OpenWindow & pack) callInterfaceIfPresent(cl, cl.getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, &IGameEventsReceiver::showMarketWindow, market, hero); } break; - case OpenWindow::HILL_FORT_WINDOW: + case EOpenWindowMode::HILL_FORT_WINDOW: { //displays Hill fort window const CGObjectInstance *obj = cl.getObj(ObjectInstanceID(pack.id1)); @@ -921,12 +921,12 @@ void ApplyClientNetPackVisitor::visitOpenWindow(OpenWindow & pack) callInterfaceIfPresent(cl, cl.getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, &IGameEventsReceiver::showHillFortWindow, obj, hero); } break; - case OpenWindow::PUZZLE_MAP: + case EOpenWindowMode::PUZZLE_MAP: { callInterfaceIfPresent(cl, PlayerColor(pack.id1), &IGameEventsReceiver::showPuzzleMap); } break; - case OpenWindow::TAVERN_WINDOW: + case EOpenWindowMode::TAVERN_WINDOW: const CGObjectInstance *obj1 = cl.getObj(ObjectInstanceID(pack.id1)), *obj2 = cl.getObj(ObjectInstanceID(pack.id2)); callInterfaceIfPresent(cl, obj1->tempOwner, &IGameEventsReceiver::showTavernWindow, obj2); diff --git a/lib/NetPacks.h b/lib/NetPacks.h index c1d32d263..ce078e20a 100644 --- a/lib/NetPacks.h +++ b/lib/NetPacks.h @@ -718,12 +718,7 @@ struct DLL_LINKAGE GiveHero : public CPackForClient struct DLL_LINKAGE OpenWindow : public CPackForClient { - enum EWindow - { - EXCHANGE_WINDOW, RECRUITMENT_FIRST, RECRUITMENT_ALL, SHIPYARD_WINDOW, THIEVES_GUILD, - UNIVERSITY_WINDOW, HILL_FORT_WINDOW, MARKET_WINDOW, PUZZLE_MAP, TAVERN_WINDOW - }; - ui8 window; + EOpenWindowMode window; si32 id1 = -1; si32 id2 = -1; diff --git a/lib/NetPacksBase.h b/lib/NetPacksBase.h index 29c0d09fb..642d63638 100644 --- a/lib/NetPacksBase.h +++ b/lib/NetPacksBase.h @@ -42,6 +42,20 @@ enum class EInfoWindowMode : uint8_t INFO }; +enum class EOpenWindowMode : uint8_t +{ + EXCHANGE_WINDOW, + RECRUITMENT_FIRST, + RECRUITMENT_ALL, + SHIPYARD_WINDOW, + THIEVES_GUILD, + UNIVERSITY_WINDOW, + HILL_FORT_WINDOW, + MARKET_WINDOW, + PUZZLE_MAP, + TAVERN_WINDOW +}; + struct DLL_LINKAGE CPack { std::shared_ptr c; // Pointer to connection that pack received from diff --git a/lib/mapObjects/CGHeroInstance.cpp b/lib/mapObjects/CGHeroInstance.cpp index a55f755ef..d2f12c40c 100644 --- a/lib/mapObjects/CGHeroInstance.cpp +++ b/lib/mapObjects/CGHeroInstance.cpp @@ -36,23 +36,6 @@ VCMI_LIB_NAMESPACE_BEGIN - -///helpers -static void showInfoDialog(const PlayerColor & playerID, const ui32 txtID, const ui16 soundID = 0) -{ - InfoWindow iw; - iw.soundID = soundID; - iw.player = playerID; - iw.text.addTxt(MetaString::ADVOB_TXT,txtID); - IObjectInterface::cb->sendAndApply(&iw); -} - -static void showInfoDialog(const CGHeroInstance* h, const ui32 txtID, const ui16 soundID = 0) -{ - const PlayerColor playerID = h->getOwner(); - showInfoDialog(playerID,txtID,soundID); -} - static int lowestSpeed(const CGHeroInstance * chi) { static const CSelector selectorSTACKS_SPEED = Selector::type()(Bonus::STACKS_SPEED); @@ -462,7 +445,7 @@ void CGHeroInstance::onHeroVisit(const CGHeroInstance * h) const txt_id = 103; } - showInfoDialog(h,txt_id); + h->showInfoDialog(txt_id); } } diff --git a/lib/mapObjects/CGMarket.cpp b/lib/mapObjects/CGMarket.cpp index 82171e3f2..f711a3747 100644 --- a/lib/mapObjects/CGMarket.cpp +++ b/lib/mapObjects/CGMarket.cpp @@ -22,16 +22,6 @@ VCMI_LIB_NAMESPACE_BEGIN -///helpers -static void openWindow(const OpenWindow::EWindow type, const int id1, const int id2 = -1) -{ - OpenWindow ow; - ow.window = type; - ow.id1 = id1; - ow.id2 = id2; - IObjectInterface::cb->sendAndApply(&ow); -} - bool IMarket::getOffer(int id1, int id2, int &val1, int &val2, EMarketMode::EMarketMode mode) const { switch(mode) @@ -205,7 +195,7 @@ std::vector IMarket::availableModes() const void CGMarket::onHeroVisit(const CGHeroInstance * h) const { - openWindow(OpenWindow::MARKET_WINDOW,id.getNum(),h->id.getNum()); + openWindow(EOpenWindowMode::MARKET_WINDOW,id.getNum(),h->id.getNum()); } int CGMarket::getMarketEfficiency() const @@ -339,7 +329,7 @@ std::vector CGUniversity::availableItemsIds(EMarketMode::EMarketMode mode) void CGUniversity::onHeroVisit(const CGHeroInstance * h) const { - openWindow(OpenWindow::UNIVERSITY_WINDOW,id.getNum(),h->id.getNum()); + openWindow(EOpenWindowMode::UNIVERSITY_WINDOW,id.getNum(),h->id.getNum()); } VCMI_LIB_NAMESPACE_END diff --git a/lib/mapObjects/CGPandoraBox.cpp b/lib/mapObjects/CGPandoraBox.cpp index 37e9d8340..f5f652198 100644 --- a/lib/mapObjects/CGPandoraBox.cpp +++ b/lib/mapObjects/CGPandoraBox.cpp @@ -25,23 +25,6 @@ VCMI_LIB_NAMESPACE_BEGIN -///helpers -static void showInfoDialog(const PlayerColor & playerID, const ui32 txtID, const ui16 soundID) -{ - InfoWindow iw; - iw.type = EInfoWindowMode::AUTO; - iw.soundID = soundID; - iw.player = playerID; - iw.text.addTxt(MetaString::ADVOB_TXT,txtID); - IObjectInterface::cb->sendAndApply(&iw); -} - -static void showInfoDialog(const CGHeroInstance* h, const ui32 txtID, const ui16 soundID) -{ - const PlayerColor playerID = h->getOwner(); - showInfoDialog(playerID,txtID,soundID); -} - void CGPandoraBox::initObj(CRandomGenerator & rand) { blockVisit = (ID==Obj::PANDORAS_BOX); //block only if it's really pandora's box (events also derive from that class) @@ -342,7 +325,7 @@ void CGPandoraBox::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answe { if(stacksCount() > 0) //if pandora's box is protected by army { - showInfoDialog(hero,16,0); + hero->showInfoDialog(16, 0, EInfoWindowMode::MODAL); cb->startBattleI(hero, this); //grants things after battle } else if(message.empty() && resources.empty() @@ -351,7 +334,7 @@ void CGPandoraBox::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answe && spells.empty() && creatures.stacksCount() > 0 && gainedExp == 0 && manaDiff == 0 && moraleDiff == 0 && luckDiff == 0) //if it gives nothing without battle { - showInfoDialog(hero,15,0); + hero->showInfoDialog(15); cb->removeObject(this); } else //if it gives something without battle diff --git a/lib/mapObjects/CGTownInstance.cpp b/lib/mapObjects/CGTownInstance.cpp index 5d87f5011..296747a18 100644 --- a/lib/mapObjects/CGTownInstance.cpp +++ b/lib/mapObjects/CGTownInstance.cpp @@ -383,8 +383,8 @@ void CGDwelling::heroAcceptsCreatures( const CGHeroInstance *h) const ow.id1 = id.getNum(); ow.id2 = h->id.getNum(); ow.window = (ID == Obj::CREATURE_GENERATOR1 || ID == Obj::REFUGEE_CAMP) - ? OpenWindow::RECRUITMENT_FIRST - : OpenWindow::RECRUITMENT_ALL; + ? EOpenWindowMode::RECRUITMENT_FIRST + : EOpenWindowMode::RECRUITMENT_ALL; cb->sendAndApply(&ow); } } diff --git a/lib/mapObjects/CObjectHandler.cpp b/lib/mapObjects/CObjectHandler.cpp index a8560d9b2..f7658209e 100644 --- a/lib/mapObjects/CObjectHandler.cpp +++ b/lib/mapObjects/CObjectHandler.cpp @@ -32,7 +32,7 @@ VCMI_LIB_NAMESPACE_BEGIN IGameCallback * IObjectInterface::cb = nullptr; ///helpers -static void openWindow(const OpenWindow::EWindow type, const int id1, const int id2 = -1) +void IObjectInterface::openWindow(const EOpenWindowMode type, const int id1, const int id2) { OpenWindow ow; ow.window = type; @@ -41,27 +41,16 @@ static void openWindow(const OpenWindow::EWindow type, const int id1, const int IObjectInterface::cb->sendAndApply(&ow); } -static void showInfoDialog(const PlayerColor & playerID, const ui32 txtID, const ui16 soundID) +void IObjectInterface::showInfoDialog(const ui32 txtID, const ui16 soundID, EInfoWindowMode mode) const { InfoWindow iw; iw.soundID = soundID; - iw.player = playerID; + iw.player = getOwner(); + iw.type = mode; iw.text.addTxt(MetaString::ADVOB_TXT,txtID); IObjectInterface::cb->sendAndApply(&iw); } -/*static void showInfoDialog(const ObjectInstanceID heroID, const ui32 txtID, const ui16 soundID) -{ - const PlayerColor playerID = IObjectInterface::cb->getOwner(heroID); - showInfoDialog(playerID,txtID,soundID); -}*/ - -static void showInfoDialog(const CGHeroInstance* h, const ui32 txtID, const ui16 soundID = 0) -{ - const PlayerColor playerID = h->getOwner(); - showInfoDialog(playerID,txtID,soundID); -} - ///IObjectInterface void IObjectInterface::onHeroVisit(const CGHeroInstance * h) const {} @@ -340,18 +329,18 @@ void CGObjectInstance::onHeroVisit( const CGHeroInstance * h ) const { case Obj::HILL_FORT: { - openWindow(OpenWindow::HILL_FORT_WINDOW,id.getNum(),h->id.getNum()); + openWindow(EOpenWindowMode::HILL_FORT_WINDOW,id.getNum(),h->id.getNum()); } break; case Obj::SANCTUARY: { //You enter the sanctuary and immediately feel as if a great weight has been lifted off your shoulders. You feel safe here. - showInfoDialog(h, 114); + h->showInfoDialog(114); } break; case Obj::TAVERN: { - openWindow(OpenWindow::TAVERN_WINDOW,h->id.getNum(),id.getNum()); + openWindow(EOpenWindowMode::TAVERN_WINDOW,h->id.getNum(),id.getNum()); } break; } diff --git a/lib/mapObjects/CObjectHandler.h b/lib/mapObjects/CObjectHandler.h index e5748dd05..f79fb3d79 100644 --- a/lib/mapObjects/CObjectHandler.h +++ b/lib/mapObjects/CObjectHandler.h @@ -13,6 +13,7 @@ #include "../int3.h" #include "../HeroBonus.h" +#include "../NetPacksBase.h" VCMI_LIB_NAMESPACE_BEGIN @@ -57,7 +58,13 @@ public: virtual void garrisonDialogClosed(const CGHeroInstance *hero) const; virtual void heroLevelUpDone(const CGHeroInstance *hero) const; -//unified interface, AI helpers + //unified helper to show info dialog for object owner + virtual void showInfoDialog(const ui32 txtID, const ui16 soundID = 0, EInfoWindowMode mode = EInfoWindowMode::AUTO) const; + + //unified helper to show a specific window + static void openWindow(const EOpenWindowMode type, const int id1, const int id2 = -1); + + //unified interface, AI helpers virtual bool wasVisited (PlayerColor player) const; virtual bool wasVisited (const CGHeroInstance * h) const; diff --git a/lib/mapObjects/CQuest.cpp b/lib/mapObjects/CQuest.cpp index 6d22132fc..9388f7e35 100644 --- a/lib/mapObjects/CQuest.cpp +++ b/lib/mapObjects/CQuest.cpp @@ -50,22 +50,6 @@ CQuest::CQuest(): { } -///helpers -static void showInfoDialog(const PlayerColor & playerID, const ui32 txtID, const ui16 soundID = 0) -{ - InfoWindow iw; - iw.soundID = soundID; - iw.player = playerID; - iw.text.addTxt(MetaString::ADVOB_TXT,txtID); - IObjectInterface::cb->sendAndApply(&iw); -} - -static void showInfoDialog(const CGHeroInstance* h, const ui32 txtID, const ui16 soundID = 0) -{ - const PlayerColor playerID = h->getOwner(); - showInfoDialog(playerID,txtID,soundID); -} - static std::string visitedTxt(const bool visited) { int id = visited ? 352 : 353; @@ -1146,7 +1130,7 @@ void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const } else txt_id=20; - showInfoDialog(h, txt_id); + h->showInfoDialog(txt_id); } void CGBorderGuard::initObj(CRandomGenerator & rand) @@ -1182,7 +1166,7 @@ void CGBorderGuard::onHeroVisit(const CGHeroInstance * h) const } else { - showInfoDialog(h, 18); + h->showInfoDialog(18); AddQuest aq; aq.quest = QuestInfo (quest, this, visitablePos()); @@ -1207,7 +1191,7 @@ void CGBorderGate::onHeroVisit(const CGHeroInstance * h) const //TODO: passabili { if (!wasMyColorVisited (h->getOwner()) ) { - showInfoDialog(h,18,0); + h->showInfoDialog(18); AddQuest aq; aq.quest = QuestInfo (quest, this, visitablePos()); diff --git a/lib/mapObjects/MiscObjects.cpp b/lib/mapObjects/MiscObjects.cpp index 15370e782..7380af760 100644 --- a/lib/mapObjects/MiscObjects.cpp +++ b/lib/mapObjects/MiscObjects.cpp @@ -34,32 +34,6 @@ ui8 CGObelisk::obeliskCount = 0; //how many obelisks are on map std::map CGObelisk::visited; //map: team_id => how many obelisks has been visited ///helpers -static void openWindow(const OpenWindow::EWindow type, const int id1, const int id2 = -1) -{ - OpenWindow ow; - ow.window = type; - ow.id1 = id1; - ow.id2 = id2; - IObjectInterface::cb->sendAndApply(&ow); -} - -static void showInfoDialog(const PlayerColor & playerID, const ui32 txtID, const ui16 soundID = 0) -{ - InfoWindow iw; - iw.type = EInfoWindowMode::AUTO; - if(soundID) - iw.soundID = soundID; - iw.player = playerID; - iw.text.addTxt(MetaString::ADVOB_TXT,txtID); - IObjectInterface::cb->sendAndApply(&iw); -} - -static void showInfoDialog(const CGHeroInstance* h, const ui32 txtID, const ui16 soundID = 0) -{ - const PlayerColor playerID = h->getOwner(); - showInfoDialog(playerID,txtID,soundID); -} - static std::string visitedTxt(const bool visited) { int id = visited ? 352 : 353; @@ -386,7 +360,7 @@ void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) co } else //they fight { - showInfoDialog(h,87,0);//Insulted by your refusal of their offer, the monsters attack! + h->showInfoDialog(87, 0, EInfoWindowMode::MODAL);//Insulted by your refusal of their offer, the monsters attack! fight(h); } } @@ -600,6 +574,7 @@ void CGCreature::giveReward(const CGHeroInstance * h) const if(!iw.components.empty()) { + iw.type = EInfoWindowMode::AUTO; iw.text.addTxt(MetaString::ADVOB_TXT, 183); // % has found treasure iw.text.addReplacement(h->getNameTranslated()); cb->showInfoDialog(&iw); @@ -769,7 +744,7 @@ void CGMine::battleFinished(const CGHeroInstance *hero, const BattleResult &resu { if(isAbandoned()) { - showInfoDialog(hero->tempOwner, 85, 0); + hero->showInfoDialog(85); } flagMine(hero->tempOwner); } @@ -1080,7 +1055,7 @@ void CGMonolith::onHeroVisit( const CGHeroInstance * h ) const logGlobal->debug("All exits blocked for monolith %d at %s", id.getNum(), pos.toString()); } else - showInfoDialog(h, 70, 0); + h->showInfoDialog(70); cb->showTeleportDialog(&td); } @@ -1136,7 +1111,7 @@ void CGSubterraneanGate::onHeroVisit( const CGHeroInstance * h ) const TeleportDialog td(h->tempOwner, channel); if(cb->isTeleportChannelImpassable(channel)) { - showInfoDialog(h,153,0);//Just inside the entrance you find a large pile of rubble blocking the tunnel. You leave discouraged. + h->showInfoDialog(153);//Just inside the entrance you find a large pile of rubble blocking the tunnel. You leave discouraged. logGlobal->debug("Cannot find exit subterranean gate for %d at %s", id.getNum(), pos.toString()); td.impassable = true; } @@ -1860,7 +1835,7 @@ void CGMagi::onHeroVisit(const CGHeroInstance * h) const { if (ID == Obj::HUT_OF_MAGI) { - showInfoDialog(h, 61); + h->showInfoDialog(61); if (!eyelist[subID].empty()) { @@ -1890,7 +1865,7 @@ void CGMagi::onHeroVisit(const CGHeroInstance * h) const } else if (ID == Obj::EYE_OF_MAGI) { - showInfoDialog(h, 48); + h->showInfoDialog(48); } } @@ -1988,7 +1963,7 @@ void CGShipyard::onHeroVisit( const CGHeroInstance * h ) const } else { - openWindow(OpenWindow::SHIPYARD_WINDOW,id.getNum(),h->id.getNum()); + openWindow(EOpenWindowMode::SHIPYARD_WINDOW,id.getNum(),h->id.getNum()); } } @@ -2028,12 +2003,12 @@ void CCartographer::onHeroVisit( const CGHeroInstance * h ) const } else //if he cannot afford { - showInfoDialog(h, 28); + h->showInfoDialog(28); } } else //if he already visited carographer { - showInfoDialog(h, 24); + h->showInfoDialog(24); } } @@ -2092,7 +2067,7 @@ void CGObelisk::onHeroVisit( const CGHeroInstance * h ) const // increment general visited obelisks counter cb->setObjProperty(id, CGObelisk::OBJPROP_INC, team.getNum()); - openWindow(OpenWindow::PUZZLE_MAP, h->tempOwner.getNum()); + openWindow(EOpenWindowMode::PUZZLE_MAP, h->tempOwner.getNum()); // mark that particular obelisk as visited for all players in the team for(const auto & color : ts->players) @@ -2153,7 +2128,7 @@ void CGLighthouse::onHeroVisit( const CGHeroInstance * h ) const { PlayerColor oldOwner = tempOwner; cb->setOwner(this,h->tempOwner); //not ours? flag it! - showInfoDialog(h, 69); + h->showInfoDialog(69); giveBonusTo(h->tempOwner); if(oldOwner < PlayerColor::PLAYER_LIMIT) //remove bonus from old owner diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index ef99ad7df..59d6c5edf 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -5721,7 +5721,7 @@ void CGameHandler::showGarrisonDialog(ObjectInstanceID upobj, ObjectInstanceID h void CGameHandler::showThievesGuildWindow(PlayerColor player, ObjectInstanceID requestingObjId) { OpenWindow ow; - ow.window = OpenWindow::THIEVES_GUILD; + ow.window = EOpenWindowMode::THIEVES_GUILD; ow.id1 = player.getNum(); ow.id2 = requestingObjId.getNum(); sendAndApply(&ow);