2025-08-13 14:41:15 +02:00
|
|
|
/*
|
|
|
|
|
* AIMemory.cpp, part of VCMI engine
|
|
|
|
|
*
|
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
|
*
|
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
#include "../StdInc.h"
|
2025-09-25 17:09:42 +02:00
|
|
|
|
2025-08-13 14:41:15 +02:00
|
|
|
#include "AIMemory.h"
|
|
|
|
|
|
2025-08-13 17:16:27 +02:00
|
|
|
namespace NK2AI
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
void AIMemory::removeFromMemory(const CGObjectInstance * obj)
|
|
|
|
|
{
|
2025-09-25 17:09:42 +02:00
|
|
|
vstd::erase_if_present(visitableObjs, obj->id);
|
|
|
|
|
vstd::erase_if_present(alreadyVisited, obj->id);
|
2025-08-13 14:41:15 +02:00
|
|
|
|
|
|
|
|
//TODO: Find better way to handle hero boat removal
|
2025-09-25 17:09:42 +02:00
|
|
|
if(const auto * hero = dynamic_cast<const CGHeroInstance *>(obj))
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
|
|
|
|
if(hero->inBoat())
|
|
|
|
|
{
|
2025-09-25 17:09:42 +02:00
|
|
|
vstd::erase_if_present(visitableObjs, hero->getBoat()->id);
|
|
|
|
|
vstd::erase_if_present(alreadyVisited, hero->getBoat()->id);
|
2025-08-13 14:41:15 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-25 17:09:42 +02:00
|
|
|
void AIMemory::removeFromMemory(const ObjectIdRef obj)
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
2025-09-25 17:09:42 +02:00
|
|
|
auto matchesId = [&](const ObjectInstanceID & objId) -> bool
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
2025-09-25 17:09:42 +02:00
|
|
|
return objId == obj.id;
|
2025-08-13 14:41:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
vstd::erase_if(visitableObjs, matchesId);
|
|
|
|
|
vstd::erase_if(alreadyVisited, matchesId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AIMemory::addSubterraneanGate(const CGObjectInstance * entrance, const CGObjectInstance * exit)
|
|
|
|
|
{
|
|
|
|
|
knownSubterraneanGates[entrance] = exit;
|
|
|
|
|
knownSubterraneanGates[exit] = entrance;
|
|
|
|
|
|
2025-09-25 17:09:42 +02:00
|
|
|
logAi->trace("Found a pair of subterranean gates between %s and %s!", entrance->visitablePos().toString(), exit->visitablePos().toString());
|
2025-08-13 14:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AIMemory::addVisitableObject(const CGObjectInstance * obj)
|
|
|
|
|
{
|
2025-09-25 17:09:42 +02:00
|
|
|
visitableObjs.insert(obj->id);
|
2025-08-13 14:41:15 +02:00
|
|
|
|
|
|
|
|
// All teleport objects seen automatically assigned to appropriate channels
|
2025-09-25 17:09:42 +02:00
|
|
|
if(const auto teleportObj = dynamic_cast<const CGTeleport *>(obj))
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
|
|
|
|
CGTeleport::addToChannel(knownTeleportChannels, teleportObj);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AIMemory::markObjectVisited(const CGObjectInstance * obj)
|
|
|
|
|
{
|
|
|
|
|
if(!obj)
|
|
|
|
|
return;
|
2025-09-25 17:09:42 +02:00
|
|
|
|
2025-08-13 14:41:15 +02:00
|
|
|
// TODO: maybe this logic belongs to CaptureObjects::shouldVisit
|
|
|
|
|
if(const auto * rewardable = dynamic_cast<const CRewardableObject *>(obj))
|
|
|
|
|
{
|
2025-09-25 17:09:42 +02:00
|
|
|
if(rewardable->configuration.getVisitMode() == Rewardable::VISIT_HERO) //we may want to visit it with another hero
|
2025-08-13 14:41:15 +02:00
|
|
|
return;
|
|
|
|
|
|
2025-09-25 17:09:42 +02:00
|
|
|
if(rewardable->configuration.getVisitMode() == Rewardable::VISIT_BONUS) //or another time
|
2025-08-13 14:41:15 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(obj->ID == Obj::MONSTER)
|
|
|
|
|
return;
|
|
|
|
|
|
2025-09-25 17:09:42 +02:00
|
|
|
alreadyVisited.insert(obj->id);
|
2025-08-13 14:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AIMemory::markObjectUnvisited(const CGObjectInstance * obj)
|
|
|
|
|
{
|
2025-09-25 17:09:42 +02:00
|
|
|
vstd::erase_if_present(alreadyVisited, obj->id);
|
2025-08-13 14:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AIMemory::wasVisited(const CGObjectInstance * obj) const
|
|
|
|
|
{
|
2025-09-25 17:09:42 +02:00
|
|
|
return vstd::contains(alreadyVisited, obj->id);
|
2025-08-13 14:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-25 17:09:42 +02:00
|
|
|
void AIMemory::removeInvisibleOrDeletedObjects(const CCallback & cb)
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
2025-09-25 17:09:42 +02:00
|
|
|
auto shouldBeErased = [&](const ObjectInstanceID objId) -> bool
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
2025-09-25 17:09:42 +02:00
|
|
|
return !cb.getObj(objId, false);
|
2025-08-13 14:41:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
vstd::erase_if(visitableObjs, shouldBeErased);
|
|
|
|
|
vstd::erase_if(alreadyVisited, shouldBeErased);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-25 17:09:42 +02:00
|
|
|
std::vector<const CGObjectInstance *> AIMemory::visitableIdsToObjsVector(const CCallback & cb) const
|
|
|
|
|
{
|
|
|
|
|
auto objs = std::vector<const CGObjectInstance *>();
|
|
|
|
|
for(const ObjectInstanceID objId : visitableObjs)
|
|
|
|
|
{
|
|
|
|
|
if(const auto * obj = cb.getObj(objId, false))
|
|
|
|
|
objs.push_back(obj);
|
|
|
|
|
}
|
|
|
|
|
return objs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::set<const CGObjectInstance *> AIMemory::visitableIdsToObjsSet(const CCallback & cb) const
|
|
|
|
|
{
|
|
|
|
|
auto objs = std::set<const CGObjectInstance *>();
|
|
|
|
|
for(const ObjectInstanceID objId : visitableObjs)
|
|
|
|
|
{
|
|
|
|
|
if(const auto * obj = cb.getObj(objId, false))
|
|
|
|
|
objs.insert(obj);
|
|
|
|
|
}
|
|
|
|
|
return objs;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-13 14:41:15 +02:00
|
|
|
}
|