1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-16 10:19:47 +02:00
vcmi/AI/Nullkiller/Analyzers/ObjectClusterizer.cpp

414 lines
9.9 KiB
C++
Raw Normal View History

2021-05-16 13:45:12 +02:00
/*
* DangerHitMapAnalyzer.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"
#include "ObjectClusterizer.h"
#include "../Goals/ExecuteHeroChain.h"
2021-05-16 14:39:38 +02:00
#include "../AIGateway.h"
2021-05-16 13:45:12 +02:00
#include "../Engine/Nullkiller.h"
2022-09-26 20:01:07 +02:00
namespace NKAI
{
2021-05-16 13:45:12 +02:00
void ObjectCluster::addObject(const CGObjectInstance * obj, const AIPath & path, float priority)
{
ClusterObjects::accessor info;
2021-05-16 13:45:12 +02:00
objects.insert(info, ClusterObjects::value_type(obj, ClusterObjectInfo()));
if(info->second.priority < priority)
2021-05-16 13:45:12 +02:00
{
info->second.priority = priority;
info->second.movementCost = path.movementCost() - path.firstNode().cost;
info->second.danger = path.targetObjectDanger;
info->second.turn = path.turn();
2021-05-16 13:45:12 +02:00
}
}
const CGObjectInstance * ObjectCluster::calculateCenter() const
{
auto v = getObjects();
auto tile = int3(0);
float priority = 0;
for(auto pair : objects)
{
auto newPoint = pair.first->visitablePos();
float newPriority = std::pow(pair.second.priority, 4); // lets make high priority targets more weghtful
int3 direction = newPoint - tile;
float priorityRatio = newPriority / (priority + newPriority);
tile += direction * priorityRatio;
priority += newPriority;
}
2021-05-16 13:55:57 +02:00
auto closestPair = *vstd::minElementByFun(objects, [&](const std::pair<const CGObjectInstance *, ClusterObjectInfo> & pair) -> int
2021-05-16 13:45:12 +02:00
{
return pair.first->visitablePos().dist2dSQ(tile);
});
return closestPair.first;
}
std::vector<const CGObjectInstance *> ObjectCluster::getObjects() const
{
std::vector<const CGObjectInstance *> result;
for(auto pair : objects)
{
result.push_back(pair.first);
}
return result;
}
std::vector<const CGObjectInstance *> ObjectClusterizer::getNearbyObjects() const
{
return nearObjects.getObjects();
}
2021-05-16 13:45:45 +02:00
std::vector<const CGObjectInstance *> ObjectClusterizer::getFarObjects() const
{
return farObjects.getObjects();
}
2021-05-16 13:45:12 +02:00
std::vector<std::shared_ptr<ObjectCluster>> ObjectClusterizer::getLockedClusters() const
{
std::vector<std::shared_ptr<ObjectCluster>> result;
for(auto pair : blockedObjects)
{
result.push_back(pair.second);
}
return result;
}
std::optional<const CGObjectInstance *> ObjectClusterizer::getBlocker(const AIPathNodeInfo & node) const
2021-05-16 13:45:12 +02:00
{
std::vector<const CGObjectInstance *> blockers = {};
if(node.layer == EPathfindingLayer::LAND || node.layer == EPathfindingLayer::SAIL)
2021-05-16 13:45:12 +02:00
{
auto guardPos = ai->cb->getGuardingCreaturePosition(node.coord);
2023-09-24 12:07:42 +02:00
blockers = ai->cb->getVisitableObjs(node.coord);
if(guardPos.valid())
2021-05-16 13:45:12 +02:00
{
auto guard = ai->cb->getTopObj(ai->cb->getGuardingCreaturePosition(node.coord));
2021-05-16 13:45:12 +02:00
if(guard)
2021-05-16 13:45:12 +02:00
{
blockers.insert(blockers.begin(), guard);
2021-05-16 13:45:12 +02:00
}
}
}
2021-05-16 13:45:12 +02:00
if(node.specialAction && node.actionIsBlocked)
{
auto blockerObject = node.specialAction->targetObject();
if(blockerObject)
{
blockers.insert(blockers.begin(), blockerObject);
}
}
if(blockers.empty())
return std::optional< const CGObjectInstance *>();
2021-05-16 13:45:12 +02:00
auto blocker = blockers.front();
2021-05-16 13:45:12 +02:00
if(isObjectPassable(ai, blocker))
return std::optional< const CGObjectInstance *>();
2022-09-06 20:14:22 +02:00
if(blocker->ID == Obj::GARRISON
|| blocker->ID == Obj::GARRISON2)
{
if(dynamic_cast<const CArmedInstance *>(blocker)->getArmyStrength() == 0)
return std::optional< const CGObjectInstance *>();
else
return blocker;
}
if(blocker->ID == Obj::MONSTER
|| blocker->ID == Obj::BORDERGUARD
|| blocker->ID == Obj::BORDER_GATE
|| blocker->ID == Obj::SHIPYARD
|| (blocker->ID == Obj::QUEST_GUARD && node.actionIsBlocked))
{
return blocker;
}
return std::optional< const CGObjectInstance *>();
}
const CGObjectInstance * ObjectClusterizer::getBlocker(const AIPath & path) const
{
for(auto node = path.nodes.rbegin(); node != path.nodes.rend(); node++)
{
auto blocker = getBlocker(*node);
if(blocker)
return *blocker;
2021-05-16 13:45:12 +02:00
}
return nullptr;
}
bool ObjectClusterizer::shouldVisitObject(const CGObjectInstance * obj) const
2021-05-16 13:45:12 +02:00
{
if(isObjectRemovable(obj))
{
return true;
}
const int3 pos = obj->visitablePos();
if((obj->ID != Obj::CREATURE_GENERATOR1 && vstd::contains(ai->memory->alreadyVisited, obj))
2021-05-16 13:45:12 +02:00
|| obj->wasVisited(ai->playerID))
{
return false;
}
auto playerRelations = ai->cb->getPlayerRelations(ai->playerID, obj->tempOwner);
2021-05-16 13:45:12 +02:00
if(playerRelations != PlayerRelations::ENEMIES && !isWeeklyRevisitable(obj))
{
return false;
}
//it may be hero visiting this obj
//we don't try visiting object on which allied or owned hero stands
// -> it will just trigger exchange windows and AI will be confused that obj behind doesn't get visited
const CGObjectInstance * topObj = ai->cb->getTopObj(pos);
2021-05-16 13:45:12 +02:00
if(!topObj)
return false; // partly visible obj but its visitable pos is not visible.
if(topObj->ID == Obj::HERO && ai->cb->getPlayerRelations(ai->playerID, topObj->tempOwner) != PlayerRelations::ENEMIES)
2021-05-16 13:45:12 +02:00
return false;
else
return true; //all of the following is met
}
2024-03-09 16:20:00 +02:00
Obj ObjectClusterizer::IgnoredObjectTypes[] = {
Obj::BOAT,
Obj::EYE_OF_MAGI,
2021-05-16 13:45:12 +02:00
Obj::MONOLITH_ONE_WAY_ENTRANCE,
Obj::MONOLITH_ONE_WAY_EXIT,
Obj::MONOLITH_TWO_WAY,
Obj::SUBTERRANEAN_GATE,
Obj::WHIRLPOOL,
Obj::BUOY,
Obj::SIGN,
Obj::GARRISON,
Obj::MONSTER,
Obj::GARRISON2,
Obj::BORDERGUARD,
Obj::QUEST_GUARD,
Obj::BORDER_GATE,
Obj::REDWOOD_OBSERVATORY,
Obj::CARTOGRAPHER,
Obj::PILLAR_OF_FIRE
2024-03-09 16:20:00 +02:00
};
void ObjectClusterizer::clusterize()
{
auto start = std::chrono::high_resolution_clock::now();
nearObjects.reset();
farObjects.reset();
blockedObjects.clear();
2021-05-16 13:45:12 +02:00
logAi->debug("Begin object clusterization");
std::vector<const CGObjectInstance *> objs(
ai->memory->visitableObjs.begin(),
ai->memory->visitableObjs.end());
2024-02-03 12:20:59 +02:00
#if NKAI_TRACE_LEVEL == 0
2024-03-09 16:20:00 +02:00
parallel_for(blocked_range<size_t>(0, objs.size()), [&](const blocked_range<size_t> & r) {
2024-02-03 12:20:59 +02:00
#else
blocked_range<size_t> r(0, objs.size());
#endif
auto priorityEvaluator = ai->priorityEvaluators->acquire();
auto heroes = ai->cb->getHeroesInfo();
std::vector<AIPath> pathCache;
for(int i = r.begin(); i != r.end(); i++)
{
clusterizeObject(objs[i], priorityEvaluator.get(), pathCache, heroes);
2024-03-09 16:20:00 +02:00
}
#if NKAI_TRACE_LEVEL == 0
});
#endif
2021-05-16 13:45:12 +02:00
2024-03-09 16:20:00 +02:00
logAi->trace("Near objects count: %i", nearObjects.objects.size());
logAi->trace("Far objects count: %i", farObjects.objects.size());
2024-03-09 16:20:00 +02:00
for(auto pair : blockedObjects)
{
logAi->trace("Cluster %s %s count: %i", pair.first->getObjectName(), pair.first->visitablePos().toString(), pair.second->objects.size());
2021-05-16 13:45:12 +02:00
2024-03-09 16:20:00 +02:00
#if NKAI_TRACE_LEVEL >= 1
for(auto obj : pair.second->getObjects())
{
logAi->trace("Object %s %s", obj->getObjectName(), obj->visitablePos().toString());
}
#endif
2024-03-09 16:20:00 +02:00
}
2021-05-16 13:45:12 +02:00
2024-03-09 16:20:00 +02:00
logAi->trace("Clusterization complete in %ld", timeElapsed(start));
}
2021-05-16 13:45:12 +02:00
void ObjectClusterizer::clusterizeObject(
const CGObjectInstance * obj,
PriorityEvaluator * priorityEvaluator,
std::vector<AIPath> & pathCache,
std::vector<const CGHeroInstance *> & heroes)
2024-03-09 16:20:00 +02:00
{
if(!shouldVisitObject(obj))
{
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 2
2024-03-09 16:20:00 +02:00
logAi->trace("Skip object %s%s.", obj->getObjectName(), obj->visitablePos().toString());
#endif
2024-03-09 16:20:00 +02:00
return;
}
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 2
2024-03-09 16:20:00 +02:00
logAi->trace("Check object %s%s.", obj->getObjectName(), obj->visitablePos().toString());
#endif
if(ai->settings->isObjectGraphAllowed())
{
ai->pathfinder->calculateQuickPathsWithBlocker(pathCache, heroes, obj->visitablePos());
}
else
ai->pathfinder->calculatePathInfo(pathCache, obj->visitablePos(), false);
2024-03-09 16:20:00 +02:00
if(pathCache.empty())
2024-03-09 16:20:00 +02:00
{
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 2
2024-03-09 16:20:00 +02:00
logAi->trace("No paths found.");
#endif
2024-03-09 16:20:00 +02:00
return;
}
2021-05-16 13:45:12 +02:00
std::sort(pathCache.begin(), pathCache.end(), [](const AIPath & p1, const AIPath & p2) -> bool
2024-03-09 16:20:00 +02:00
{
return p1.movementCost() < p2.movementCost();
});
if(vstd::contains(IgnoredObjectTypes, obj->ID))
{
farObjects.addObject(obj, pathCache.front(), 0);
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 2
logAi->trace("Object ignored. Moved to far objects with path %s", pathCache.front().toString());
#endif
2024-03-09 16:20:00 +02:00
return;
}
2021-05-16 13:45:12 +02:00
2024-03-09 16:20:00 +02:00
std::set<const CGHeroInstance *> heroesProcessed;
2021-05-16 13:45:12 +02:00
for(auto & path : pathCache)
2024-03-09 16:20:00 +02:00
{
#if NKAI_TRACE_LEVEL >= 2
logAi->trace("Checking path %s", path.toString());
#endif
2021-05-16 13:45:12 +02:00
if(ai->heroManager->getHeroRole(path.targetHero) == HeroRole::SCOUT)
{
if(path.movementCost() > 2.0f)
continue;
}
else if(path.movementCost() > 4.0f)
{
auto strategicalValue = valueEvaluator.getStrategicalValue(obj);
if(strategicalValue < 0.5f)
continue;
}
2024-03-09 16:20:00 +02:00
if(!shouldVisit(ai, path.targetHero, obj))
{
#if NKAI_TRACE_LEVEL >= 2
logAi->trace("Hero %s does not need to visit %s", path.targetHero->getObjectName(), obj->getObjectName());
#endif
continue;
}
2021-05-16 13:45:12 +02:00
2024-03-09 16:20:00 +02:00
if(path.nodes.size() > 1)
{
auto blocker = getBlocker(path);
2024-03-09 16:20:00 +02:00
if(blocker)
{
if(vstd::contains(heroesProcessed, path.targetHero))
{
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 2
2024-03-09 16:20:00 +02:00
logAi->trace("Hero %s is already processed.", path.targetHero->getObjectName());
#endif
2024-03-09 16:20:00 +02:00
continue;
}
2021-05-16 13:45:12 +02:00
heroesProcessed.insert(path.targetHero);
float priority = priorityEvaluator->evaluate(Goals::sptr(Goals::ExecuteHeroChain(path, obj)));
if(priority < MIN_PRIORITY)
continue;
2024-03-09 16:20:00 +02:00
ClusterMap::accessor cluster;
blockedObjects.insert(
cluster,
ClusterMap::value_type(blocker, std::make_shared<ObjectCluster>(blocker)));
2024-03-09 16:20:00 +02:00
cluster->second->addObject(obj, path, priority);
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 2
2024-03-09 16:20:00 +02:00
logAi->trace("Path added to cluster %s%s", blocker->getObjectName(), blocker->visitablePos().toString());
#endif
2024-03-09 16:20:00 +02:00
continue;
}
2021-05-16 13:45:12 +02:00
}
2024-03-09 16:20:00 +02:00
heroesProcessed.insert(path.targetHero);
2024-03-09 16:20:00 +02:00
float priority = priorityEvaluator->evaluate(Goals::sptr(Goals::ExecuteHeroChain(path, obj)));
2021-05-16 13:45:12 +02:00
2024-03-09 16:20:00 +02:00
if(priority < MIN_PRIORITY)
continue;
bool interestingObject = path.turn() <= 2 || priority > 0.5f;
if(interestingObject)
2021-05-16 13:45:12 +02:00
{
2024-03-09 16:20:00 +02:00
nearObjects.addObject(obj, path, priority);
2021-05-16 13:45:12 +02:00
}
2024-03-09 16:20:00 +02:00
else
{
farObjects.addObject(obj, path, priority);
}
#if NKAI_TRACE_LEVEL >= 2
logAi->trace("Path %s added to %s objects. Turn: %d, priority: %f",
path.toString(),
interestingObject ? "near" : "far",
path.turn(),
priority);
2021-05-16 13:45:12 +02:00
#endif
}
2021-11-23 08:41:03 +02:00
}
2022-09-26 20:01:07 +02:00
}