diff --git a/AI/Nullkiller/AIGateway.cpp b/AI/Nullkiller/AIGateway.cpp index 1fced0f95..0015ed995 100644 --- a/AI/Nullkiller/AIGateway.cpp +++ b/AI/Nullkiller/AIGateway.cpp @@ -1407,7 +1407,7 @@ void AIGateway::tryRealize(Goals::Trade & g) //trade int accquiredResources = 0; if(const CGObjectInstance * obj = cb->getObj(ObjectInstanceID(g.objid), false)) { - if(const IMarket * m = IMarket::castFrom(obj, false)) + if(const auto * m = dynamic_cast(obj)) { auto freeRes = cb->getResourceAmount(); //trade only resources which are not reserved for(auto it = ResourceSet::nziterator(freeRes); it.valid(); it++) diff --git a/AI/Nullkiller/Pathfinding/Rules/AILayerTransitionRule.cpp b/AI/Nullkiller/Pathfinding/Rules/AILayerTransitionRule.cpp index a43f8f04e..d71aa9cb0 100644 --- a/AI/Nullkiller/Pathfinding/Rules/AILayerTransitionRule.cpp +++ b/AI/Nullkiller/Pathfinding/Rules/AILayerTransitionRule.cpp @@ -131,7 +131,7 @@ namespace AIPathfinding { if(obj->ID != Obj::TOWN) //towns were handled in the previous loop { - if(const IShipyard * shipyard = IShipyard::castFrom(obj)) + if(const auto * shipyard = dynamic_cast(obj)) shipyards.push_back(shipyard); } } diff --git a/AI/VCAI/Goals/CollectRes.cpp b/AI/VCAI/Goals/CollectRes.cpp index a5600d8df..cb780c5ac 100644 --- a/AI/VCAI/Goals/CollectRes.cpp +++ b/AI/VCAI/Goals/CollectRes.cpp @@ -124,7 +124,9 @@ TSubgoal CollectRes::whatToDoToTrade() ai->retrieveVisitableObjs(visObjs, true); for(const CGObjectInstance * obj : visObjs) { - if(const IMarket * m = IMarket::castFrom(obj, false); m && m->allowsTrade(EMarketMode::RESOURCE_RESOURCE)) + const auto * m = dynamic_cast(obj); + + if(m && m->allowsTrade(EMarketMode::RESOURCE_RESOURCE)) { if(obj->ID == Obj::TOWN) { diff --git a/AI/VCAI/Pathfinding/Rules/AILayerTransitionRule.cpp b/AI/VCAI/Pathfinding/Rules/AILayerTransitionRule.cpp index 9603189fc..4e857df98 100644 --- a/AI/VCAI/Pathfinding/Rules/AILayerTransitionRule.cpp +++ b/AI/VCAI/Pathfinding/Rules/AILayerTransitionRule.cpp @@ -58,7 +58,7 @@ namespace AIPathfinding { if(obj->ID != Obj::TOWN) //towns were handled in the previous loop { - if(const IShipyard * shipyard = IShipyard::castFrom(obj)) + if(const auto * shipyard = dynamic_cast(obj)) shipyards.push_back(shipyard); } } diff --git a/AI/VCAI/VCAI.cpp b/AI/VCAI/VCAI.cpp index 18d42a511..7ce4679dd 100644 --- a/AI/VCAI/VCAI.cpp +++ b/AI/VCAI/VCAI.cpp @@ -2134,7 +2134,7 @@ void VCAI::tryRealize(Goals::Trade & g) //trade int accquiredResources = 0; if(const CGObjectInstance * obj = cb->getObj(ObjectInstanceID(g.objid), false)) { - if(const IMarket * m = IMarket::castFrom(obj, false)) + if(const auto * m = dynamic_cast(obj)) { auto freeRes = ah->freeResources(); //trade only resources which are not reserved for(auto it = ResourceSet::nziterator(freeRes); it.valid(); it++) diff --git a/Global.h b/Global.h index 7834b9a76..3ef1a64e4 100644 --- a/Global.h +++ b/Global.h @@ -695,8 +695,11 @@ namespace vstd template bool isAlmostEqual(const Floating1 & left, const Floating2 & right) { - const auto relativeEpsilon = std::max(std::abs(left), std::abs(right)) * 0.00001; - return std::abs(left - right) <= relativeEpsilon; + using Floating = decltype(left + right); + constexpr Floating epsilon(0.00001); + const Floating relativeEpsilon = std::max(std::abs(left), std::abs(right)) * epsilon; + const Floating value = std::abs(left - right); + return value <= relativeEpsilon; } ///compile-time version of std::abs for ints for int3, in clang++15 std::abs is constexpr diff --git a/client/NetPacksClient.cpp b/client/NetPacksClient.cpp index d01890f37..ff6c037b1 100644 --- a/client/NetPacksClient.cpp +++ b/client/NetPacksClient.cpp @@ -958,7 +958,7 @@ void ApplyClientNetPackVisitor::visitOpenWindow(OpenWindow & pack) case EOpenWindowMode::SHIPYARD_WINDOW: { assert(pack.queryID == QueryID::NONE); - const IShipyard *sy = IShipyard::castFrom(cl.getObj(ObjectInstanceID(pack.object))); + const auto * sy = dynamic_cast(cl.getObj(ObjectInstanceID(pack.object))); callInterfaceIfPresent(cl, sy->getObject()->getOwner(), &IGameEventsReceiver::showShipyardDialog, sy); } break; @@ -974,7 +974,7 @@ void ApplyClientNetPackVisitor::visitOpenWindow(OpenWindow & pack) case EOpenWindowMode::UNIVERSITY_WINDOW: { //displays University window (when hero enters University on adventure map) - const IMarket *market = IMarket::castFrom(cl.getObj(ObjectInstanceID(pack.object))); + const auto * market = dynamic_cast(cl.getObj(ObjectInstanceID(pack.object))); const CGHeroInstance *hero = cl.getHero(ObjectInstanceID(pack.visitor)); callInterfaceIfPresent(cl, hero->tempOwner, &IGameEventsReceiver::showUniversityWindow, market, hero, pack.queryID); } @@ -984,7 +984,7 @@ void ApplyClientNetPackVisitor::visitOpenWindow(OpenWindow & pack) //displays Thieves' Guild window (when hero enters Den of Thieves) const CGObjectInstance *obj = cl.getObj(ObjectInstanceID(pack.object)); const CGHeroInstance *hero = cl.getHero(ObjectInstanceID(pack.visitor)); - const IMarket *market = IMarket::castFrom(obj); + const auto *market = dynamic_cast(obj); callInterfaceIfPresent(cl, cl.getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, &IGameEventsReceiver::showMarketWindow, market, hero, pack.queryID); } break; diff --git a/client/adventureMap/AdventureMapInterface.cpp b/client/adventureMap/AdventureMapInterface.cpp index 4cb1ef6e6..7fd0528c7 100644 --- a/client/adventureMap/AdventureMapInterface.cpp +++ b/client/adventureMap/AdventureMapInterface.cpp @@ -852,7 +852,7 @@ Rect AdventureMapInterface::terrainAreaPixels() const const IShipyard * AdventureMapInterface::ourInaccessibleShipyard(const CGObjectInstance *obj) const { - const IShipyard *ret = IShipyard::castFrom(obj); + const auto *ret = dynamic_cast(obj); if(!ret || obj->tempOwner != currentPlayerID || diff --git a/lib/mapObjects/IMarket.cpp b/lib/mapObjects/IMarket.cpp index 02b6d4631..06fb94f1e 100644 --- a/lib/mapObjects/IMarket.cpp +++ b/lib/mapObjects/IMarket.cpp @@ -154,20 +154,6 @@ std::vector IMarket::availableItemsIds(EMarketMode mode) const return ret; } -const IMarket * IMarket::castFrom(const CGObjectInstance *obj, bool verbose) -{ - auto * imarket = dynamic_cast(obj); - if(verbose && !imarket) - { - logGlobal->error("Cannot cast to IMarket"); - if(obj) - { - logGlobal->error("Object type %s", obj->typeName); - } - } - return imarket; -} - IMarket::IMarket() { } diff --git a/lib/mapObjects/IMarket.h b/lib/mapObjects/IMarket.h index cc20300ab..caf3e4cb4 100644 --- a/lib/mapObjects/IMarket.h +++ b/lib/mapObjects/IMarket.h @@ -29,8 +29,6 @@ public: 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; - - static const IMarket *castFrom(const CGObjectInstance *obj, bool verbose = true); }; VCMI_LIB_NAMESPACE_END diff --git a/lib/mapObjects/IObjectInterface.cpp b/lib/mapObjects/IObjectInterface.cpp index 1c2c15548..e7dac1ca0 100644 --- a/lib/mapObjects/IObjectInterface.cpp +++ b/lib/mapObjects/IObjectInterface.cpp @@ -156,9 +156,4 @@ void IShipyard::getBoatCost(TResources & cost) const cost[EGameResID::GOLD] = 1000; } -const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj ) -{ - return dynamic_cast(obj); -} - VCMI_LIB_NAMESPACE_END diff --git a/lib/mapObjects/IObjectInterface.h b/lib/mapObjects/IObjectInterface.h index 8c5b94b8b..12d2baeb1 100644 --- a/lib/mapObjects/IObjectInterface.h +++ b/lib/mapObjects/IObjectInterface.h @@ -101,8 +101,6 @@ class DLL_LINKAGE IShipyard : public IBoatGenerator { public: virtual void getBoatCost(ResourceSet & cost) const; - - static const IShipyard *castFrom(const CGObjectInstance *obj); }; VCMI_LIB_NAMESPACE_END diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index aaa7793f1..7b7fca060 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -3528,7 +3528,7 @@ void CGameHandler::objectVisitEnded(const CObjectVisitQuery & query) bool CGameHandler::buildBoat(ObjectInstanceID objid, PlayerColor playerID) { - const IShipyard *obj = IShipyard::castFrom(getObj(objid)); + const auto *obj = dynamic_cast(getObj(objid)); if (obj->shipyardStatus() != IBoatGenerator::GOOD) { diff --git a/server/NetPacksServer.cpp b/server/NetPacksServer.cpp index fc5b297fb..6ad658784 100644 --- a/server/NetPacksServer.cpp +++ b/server/NetPacksServer.cpp @@ -170,7 +170,7 @@ void ApplyGhNetPackVisitor::visitTradeOnMarketplace(TradeOnMarketplace & pack) { const CGObjectInstance * object = gh.getObj(pack.marketId); const CGHeroInstance * hero = gh.getHero(pack.heroId); - const IMarket * market = IMarket::castFrom(object); + const auto * market = dynamic_cast(object); gh.throwIfWrongPlayer(&pack);