1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-04 00:15:53 +02:00

Remove custom casts

This commit is contained in:
Ivan Savenko 2024-02-14 12:56:37 +02:00
parent 0d74959a33
commit c23953eac5
14 changed files with 18 additions and 36 deletions

View File

@ -1407,7 +1407,7 @@ void AIGateway::tryRealize(Goals::Trade & g) //trade
int accquiredResources = 0; int accquiredResources = 0;
if(const CGObjectInstance * obj = cb->getObj(ObjectInstanceID(g.objid), false)) if(const CGObjectInstance * obj = cb->getObj(ObjectInstanceID(g.objid), false))
{ {
if(const IMarket * m = IMarket::castFrom(obj, false)) if(const auto * m = dynamic_cast<const IMarket*>(obj))
{ {
auto freeRes = cb->getResourceAmount(); //trade only resources which are not reserved auto freeRes = cb->getResourceAmount(); //trade only resources which are not reserved
for(auto it = ResourceSet::nziterator(freeRes); it.valid(); it++) for(auto it = ResourceSet::nziterator(freeRes); it.valid(); it++)

View File

@ -131,7 +131,7 @@ namespace AIPathfinding
{ {
if(obj->ID != Obj::TOWN) //towns were handled in the previous loop 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<const IShipyard *>(obj))
shipyards.push_back(shipyard); shipyards.push_back(shipyard);
} }
} }

View File

@ -124,7 +124,9 @@ TSubgoal CollectRes::whatToDoToTrade()
ai->retrieveVisitableObjs(visObjs, true); ai->retrieveVisitableObjs(visObjs, true);
for(const CGObjectInstance * obj : visObjs) for(const CGObjectInstance * obj : visObjs)
{ {
if(const IMarket * m = IMarket::castFrom(obj, false); m && m->allowsTrade(EMarketMode::RESOURCE_RESOURCE)) const auto * m = dynamic_cast<const IMarket*>(obj);
if(m && m->allowsTrade(EMarketMode::RESOURCE_RESOURCE))
{ {
if(obj->ID == Obj::TOWN) if(obj->ID == Obj::TOWN)
{ {

View File

@ -58,7 +58,7 @@ namespace AIPathfinding
{ {
if(obj->ID != Obj::TOWN) //towns were handled in the previous loop 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<const IShipyard *>(obj))
shipyards.push_back(shipyard); shipyards.push_back(shipyard);
} }
} }

View File

@ -2134,7 +2134,7 @@ void VCAI::tryRealize(Goals::Trade & g) //trade
int accquiredResources = 0; int accquiredResources = 0;
if(const CGObjectInstance * obj = cb->getObj(ObjectInstanceID(g.objid), false)) if(const CGObjectInstance * obj = cb->getObj(ObjectInstanceID(g.objid), false))
{ {
if(const IMarket * m = IMarket::castFrom(obj, false)) if(const auto * m = dynamic_cast<const IMarket*>(obj))
{ {
auto freeRes = ah->freeResources(); //trade only resources which are not reserved auto freeRes = ah->freeResources(); //trade only resources which are not reserved
for(auto it = ResourceSet::nziterator(freeRes); it.valid(); it++) for(auto it = ResourceSet::nziterator(freeRes); it.valid(); it++)

View File

@ -695,8 +695,11 @@ namespace vstd
template<typename Floating1, typename Floating2> template<typename Floating1, typename Floating2>
bool isAlmostEqual(const Floating1 & left, const Floating2 & right) bool isAlmostEqual(const Floating1 & left, const Floating2 & right)
{ {
const auto relativeEpsilon = std::max(std::abs(left), std::abs(right)) * 0.00001; using Floating = decltype(left + right);
return std::abs(left - right) <= relativeEpsilon; 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 ///compile-time version of std::abs for ints for int3, in clang++15 std::abs is constexpr

View File

@ -958,7 +958,7 @@ void ApplyClientNetPackVisitor::visitOpenWindow(OpenWindow & pack)
case EOpenWindowMode::SHIPYARD_WINDOW: case EOpenWindowMode::SHIPYARD_WINDOW:
{ {
assert(pack.queryID == QueryID::NONE); assert(pack.queryID == QueryID::NONE);
const IShipyard *sy = IShipyard::castFrom(cl.getObj(ObjectInstanceID(pack.object))); const auto * sy = dynamic_cast<const IShipyard *>(cl.getObj(ObjectInstanceID(pack.object)));
callInterfaceIfPresent(cl, sy->getObject()->getOwner(), &IGameEventsReceiver::showShipyardDialog, sy); callInterfaceIfPresent(cl, sy->getObject()->getOwner(), &IGameEventsReceiver::showShipyardDialog, sy);
} }
break; break;
@ -974,7 +974,7 @@ void ApplyClientNetPackVisitor::visitOpenWindow(OpenWindow & pack)
case EOpenWindowMode::UNIVERSITY_WINDOW: case EOpenWindowMode::UNIVERSITY_WINDOW:
{ {
//displays University window (when hero enters University on adventure map) //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<const IMarket*>(cl.getObj(ObjectInstanceID(pack.object)));
const CGHeroInstance *hero = cl.getHero(ObjectInstanceID(pack.visitor)); const CGHeroInstance *hero = cl.getHero(ObjectInstanceID(pack.visitor));
callInterfaceIfPresent(cl, hero->tempOwner, &IGameEventsReceiver::showUniversityWindow, market, hero, pack.queryID); 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) //displays Thieves' Guild window (when hero enters Den of Thieves)
const CGObjectInstance *obj = cl.getObj(ObjectInstanceID(pack.object)); const CGObjectInstance *obj = cl.getObj(ObjectInstanceID(pack.object));
const CGHeroInstance *hero = cl.getHero(ObjectInstanceID(pack.visitor)); const CGHeroInstance *hero = cl.getHero(ObjectInstanceID(pack.visitor));
const IMarket *market = IMarket::castFrom(obj); const auto *market = dynamic_cast<const IMarket*>(obj);
callInterfaceIfPresent(cl, cl.getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, &IGameEventsReceiver::showMarketWindow, market, hero, pack.queryID); callInterfaceIfPresent(cl, cl.getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, &IGameEventsReceiver::showMarketWindow, market, hero, pack.queryID);
} }
break; break;

View File

@ -852,7 +852,7 @@ Rect AdventureMapInterface::terrainAreaPixels() const
const IShipyard * AdventureMapInterface::ourInaccessibleShipyard(const CGObjectInstance *obj) const const IShipyard * AdventureMapInterface::ourInaccessibleShipyard(const CGObjectInstance *obj) const
{ {
const IShipyard *ret = IShipyard::castFrom(obj); const auto *ret = dynamic_cast<const IShipyard *>(obj);
if(!ret || if(!ret ||
obj->tempOwner != currentPlayerID || obj->tempOwner != currentPlayerID ||

View File

@ -154,20 +154,6 @@ std::vector<TradeItemBuy> IMarket::availableItemsIds(EMarketMode mode) const
return ret; return ret;
} }
const IMarket * IMarket::castFrom(const CGObjectInstance *obj, bool verbose)
{
auto * imarket = dynamic_cast<const IMarket *>(obj);
if(verbose && !imarket)
{
logGlobal->error("Cannot cast to IMarket");
if(obj)
{
logGlobal->error("Object type %s", obj->typeName);
}
}
return imarket;
}
IMarket::IMarket() IMarket::IMarket()
{ {
} }

View File

@ -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 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<EMarketMode> availableModes() const; std::vector<EMarketMode> availableModes() const;
static const IMarket *castFrom(const CGObjectInstance *obj, bool verbose = true);
}; };
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@ -156,9 +156,4 @@ void IShipyard::getBoatCost(TResources & cost) const
cost[EGameResID::GOLD] = 1000; cost[EGameResID::GOLD] = 1000;
} }
const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
{
return dynamic_cast<const IShipyard *>(obj);
}
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@ -101,8 +101,6 @@ class DLL_LINKAGE IShipyard : public IBoatGenerator
{ {
public: public:
virtual void getBoatCost(ResourceSet & cost) const; virtual void getBoatCost(ResourceSet & cost) const;
static const IShipyard *castFrom(const CGObjectInstance *obj);
}; };
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@ -3528,7 +3528,7 @@ void CGameHandler::objectVisitEnded(const CObjectVisitQuery & query)
bool CGameHandler::buildBoat(ObjectInstanceID objid, PlayerColor playerID) bool CGameHandler::buildBoat(ObjectInstanceID objid, PlayerColor playerID)
{ {
const IShipyard *obj = IShipyard::castFrom(getObj(objid)); const auto *obj = dynamic_cast<const IShipyard *>(getObj(objid));
if (obj->shipyardStatus() != IBoatGenerator::GOOD) if (obj->shipyardStatus() != IBoatGenerator::GOOD)
{ {

View File

@ -170,7 +170,7 @@ void ApplyGhNetPackVisitor::visitTradeOnMarketplace(TradeOnMarketplace & pack)
{ {
const CGObjectInstance * object = gh.getObj(pack.marketId); const CGObjectInstance * object = gh.getObj(pack.marketId);
const CGHeroInstance * hero = gh.getHero(pack.heroId); const CGHeroInstance * hero = gh.getHero(pack.heroId);
const IMarket * market = IMarket::castFrom(object); const auto * market = dynamic_cast<const IMarket*>(object);
gh.throwIfWrongPlayer(&pack); gh.throwIfWrongPlayer(&pack);