mirror of
https://github.com/vcmi/vcmi.git
synced 2025-11-29 23:07:48 +02:00
refactor AIMemory replacing getObj with getObjInstance to avoid unnecessary visibility filtering when just a simple iteration over objs is desired
This commit is contained in:
@@ -1010,7 +1010,7 @@ std::vector<const CGObjectInstance *> AIGateway::getFlaggedObjects() const
|
||||
std::vector<const CGObjectInstance *> ret;
|
||||
for(const ObjectInstanceID objId : nullkiller->memory->visitableObjs)
|
||||
{
|
||||
const CGObjectInstance * obj = cc->getObj(objId, false);
|
||||
const CGObjectInstance * obj = cc->getObjInstance(objId);
|
||||
if(obj && obj->tempOwner == playerID)
|
||||
ret.push_back(obj);
|
||||
}
|
||||
@@ -1631,7 +1631,7 @@ void AIGateway::memorizeRevisitableObjs(const std::unique_ptr<AIMemory> & memory
|
||||
{
|
||||
for(const ObjectInstanceID objId : memory->visitableObjs)
|
||||
{
|
||||
const CGObjectInstance * obj = cc->getObj(objId, false);
|
||||
const CGObjectInstance * obj = cc->getObjInstance(objId);
|
||||
if(obj && isWeeklyRevisitable(playerID, obj))
|
||||
memory->markObjectUnvisited(obj);
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ void DangerHitMapAnalyzer::updateHitMap()
|
||||
|
||||
for(const ObjectInstanceID objId : aiNk->memory->visitableObjs)
|
||||
{
|
||||
const CGObjectInstance * obj = cc->getObj(objId, false);
|
||||
const CGObjectInstance * obj = cc->getObjInstance(objId);
|
||||
if(!obj)
|
||||
continue;
|
||||
|
||||
@@ -242,7 +242,7 @@ void DangerHitMapAnalyzer::calculateTileOwners()
|
||||
|
||||
for(const ObjectInstanceID objId : aiNk->memory->visitableObjs)
|
||||
{
|
||||
const CGObjectInstance * obj = cc->getObj(objId, false);
|
||||
const CGObjectInstance * obj = cc->getObjInstance(objId);
|
||||
if(obj && obj->ID == Obj::TOWN)
|
||||
addTownHero(dynamic_cast<const CGTownInstance *>(obj));
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ Goals::TGoalVec ExplorationBehavior::decompose(const Nullkiller * aiNk) const
|
||||
|
||||
for (const ObjectInstanceID objId : aiNk->memory->visitableObjs)
|
||||
{
|
||||
const CGObjectInstance * obj = aiNk->cc->getObj(objId, false);
|
||||
const CGObjectInstance * obj = aiNk->cc->getObjInstance(objId);
|
||||
if(!obj)
|
||||
continue;
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ void AIMemory::addVisitableObject(const CGObjectInstance * obj)
|
||||
visitableObjs.insert(obj->id);
|
||||
|
||||
// All teleport objects seen automatically assigned to appropriate channels
|
||||
if(const auto teleportObj = dynamic_cast<const CGTeleport *>(obj))
|
||||
if(const auto * const teleportObj = dynamic_cast<const CGTeleport *>(obj))
|
||||
{
|
||||
CGTeleport::addToChannel(knownTeleportChannels, teleportObj);
|
||||
}
|
||||
@@ -91,34 +91,34 @@ bool AIMemory::wasVisited(const CGObjectInstance * obj) const
|
||||
return vstd::contains(alreadyVisited, obj->id);
|
||||
}
|
||||
|
||||
void AIMemory::removeInvisibleOrDeletedObjects(const CCallback & cb)
|
||||
void AIMemory::removeInvisibleOrDeletedObjects(const CCallback & cc)
|
||||
{
|
||||
auto shouldBeErased = [&](const ObjectInstanceID objId) -> bool
|
||||
{
|
||||
return !cb.getObj(objId, false);
|
||||
return !cc.getObj(objId, false);
|
||||
};
|
||||
|
||||
vstd::erase_if(visitableObjs, shouldBeErased);
|
||||
vstd::erase_if(alreadyVisited, shouldBeErased);
|
||||
}
|
||||
|
||||
std::vector<const CGObjectInstance *> AIMemory::visitableIdsToObjsVector(const CCallback & cb) const
|
||||
std::vector<const CGObjectInstance *> AIMemory::visitableIdsToObjsVector(const CCallback & cc) const
|
||||
{
|
||||
auto objs = std::vector<const CGObjectInstance *>();
|
||||
for(const ObjectInstanceID objId : visitableObjs)
|
||||
{
|
||||
if(const auto * obj = cb.getObj(objId, false))
|
||||
if(const auto * obj = cc.getObjInstance(objId))
|
||||
objs.push_back(obj);
|
||||
}
|
||||
return objs;
|
||||
}
|
||||
|
||||
std::set<const CGObjectInstance *> AIMemory::visitableIdsToObjsSet(const CCallback & cb) const
|
||||
std::set<const CGObjectInstance *> AIMemory::visitableIdsToObjsSet(const CCallback & cc) const
|
||||
{
|
||||
auto objs = std::set<const CGObjectInstance *>();
|
||||
for(const ObjectInstanceID objId : visitableObjs)
|
||||
{
|
||||
if(const auto * obj = cb.getObj(objId, false))
|
||||
if(const auto * obj = cc.getObjInstance(objId))
|
||||
objs.insert(obj);
|
||||
}
|
||||
return objs;
|
||||
|
||||
@@ -30,10 +30,11 @@ public:
|
||||
void markObjectVisited(const CGObjectInstance * obj);
|
||||
void markObjectUnvisited(const CGObjectInstance * obj);
|
||||
bool wasVisited(const CGObjectInstance * obj) const;
|
||||
void removeInvisibleOrDeletedObjects(const CCallback & cb);
|
||||
// Utility method to reuse code, use visitableIds directly where possible to a
|
||||
std::vector<const CGObjectInstance *> visitableIdsToObjsVector(const CCallback & cb) const;
|
||||
std::set<const CGObjectInstance *> visitableIdsToObjsSet(const CCallback & cb) const;
|
||||
void removeInvisibleOrDeletedObjects(const CCallback & cc);
|
||||
// Utility method to reuse code, use visitableIds directly where possible to avoid time-of-check-to-time-of-use (TOCTOU) race condition
|
||||
std::vector<const CGObjectInstance *> visitableIdsToObjsVector(const CCallback & cc) const;
|
||||
// Utility method to reuse code, use visitableIds directly where possible to avoid time-of-check-to-time-of-use (TOCTOU) race condition
|
||||
std::set<const CGObjectInstance *> visitableIdsToObjsSet(const CCallback & cc) const;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -96,7 +96,7 @@ void ObjectGraph::connectHeroes(const Nullkiller * aiNk)
|
||||
{
|
||||
for(const ObjectInstanceID objId : aiNk->memory->visitableObjs)
|
||||
{
|
||||
const CGObjectInstance * obj = aiNk->cc->getObj(objId, false);
|
||||
const CGObjectInstance * obj = aiNk->cc->getObjInstance(objId);
|
||||
if(obj && obj->ID == Obj::HERO)
|
||||
{
|
||||
addObject(obj);
|
||||
|
||||
@@ -29,7 +29,7 @@ void ObjectGraphCalculator::setGraphObjects()
|
||||
{
|
||||
for(const ObjectInstanceID objId : aiNk->memory->visitableObjs)
|
||||
{
|
||||
const CGObjectInstance * obj = aiNk->cc->getObj(objId, false);
|
||||
const CGObjectInstance * obj = aiNk->cc->getObjInstance(objId);
|
||||
if(obj && obj->isVisitable() && obj->ID != Obj::HERO && obj->ID != Obj::EVENT)
|
||||
{
|
||||
addObjectActor(obj);
|
||||
|
||||
@@ -147,7 +147,7 @@ namespace AIPathfinding
|
||||
|
||||
for(const ObjectInstanceID objId : aiNk->memory->visitableObjs)
|
||||
{
|
||||
const CGObjectInstance * obj = aiNk->cc->getObj(objId, false);
|
||||
const CGObjectInstance * obj = aiNk->cc->getObjInstance(objId);
|
||||
if(obj && obj->ID != Obj::TOWN) //towns were handled in the previous loop
|
||||
{
|
||||
if(const auto * shipyard = dynamic_cast<const IShipyard *>(obj))
|
||||
|
||||
Reference in New Issue
Block a user