1
0
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:
Mircea TheHonestCTO
2025-09-27 03:36:51 +02:00
parent 08ae8bef5a
commit bb7588852b
8 changed files with 20 additions and 19 deletions

View File

@@ -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);
}

View File

@@ -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));
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;
};
}

View File

@@ -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);

View File

@@ -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);

View File

@@ -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))