2024-01-20 22:54:30 +02:00
|
|
|
/*
|
|
|
|
* ObjectGraph.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 "ObjectGraph.h"
|
|
|
|
#include "AIPathfinderConfig.h"
|
|
|
|
#include "../../../CCallback.h"
|
|
|
|
#include "../../../lib/mapping/CMap.h"
|
|
|
|
#include "../Engine/Nullkiller.h"
|
2024-02-03 12:20:59 +02:00
|
|
|
#include "../../../lib/logging/VisualLogger.h"
|
2024-01-20 22:54:30 +02:00
|
|
|
|
|
|
|
namespace NKAI
|
|
|
|
{
|
|
|
|
|
|
|
|
void ObjectGraph::updateGraph(const Nullkiller * ai)
|
|
|
|
{
|
|
|
|
auto cb = ai->cb;
|
|
|
|
auto mapSize = cb->getMapSize();
|
|
|
|
|
|
|
|
std::map<const CGHeroInstance *, HeroRole> actors;
|
|
|
|
std::map<const CGHeroInstance *, const CGObjectInstance *> actorObjectMap;
|
|
|
|
|
|
|
|
auto addObjectActor = [&](const CGObjectInstance * obj)
|
|
|
|
{
|
|
|
|
auto objectActor = new CGHeroInstance(obj->cb);
|
|
|
|
CRandomGenerator rng;
|
|
|
|
auto visitablePos = obj->visitablePos();
|
|
|
|
|
|
|
|
objectActor->setOwner(ai->playerID); // lets avoid having multiple colors
|
|
|
|
objectActor->initHero(rng, static_cast<HeroTypeID>(0));
|
|
|
|
objectActor->pos = objectActor->convertFromVisitablePos(visitablePos);
|
|
|
|
objectActor->initObj(rng);
|
|
|
|
|
|
|
|
actorObjectMap[objectActor] = obj;
|
|
|
|
actors[objectActor] = obj->ID == Obj::TOWN ? HeroRole::MAIN : HeroRole::SCOUT;
|
|
|
|
addObject(obj);
|
|
|
|
};
|
|
|
|
|
|
|
|
for(auto obj : ai->memory->visitableObjs)
|
|
|
|
{
|
|
|
|
if(obj && obj->isVisitable() && obj->ID != Obj::HERO)
|
|
|
|
{
|
|
|
|
addObjectActor(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto town : cb->getTownsInfo())
|
|
|
|
{
|
|
|
|
addObjectActor(town);
|
|
|
|
}
|
|
|
|
|
|
|
|
PathfinderSettings ps;
|
|
|
|
|
|
|
|
ps.mainTurnDistanceLimit = 5;
|
|
|
|
ps.scoutTurnDistanceLimit = 1;
|
|
|
|
ps.allowBypassObjects = false;
|
|
|
|
|
|
|
|
ai->pathfinder->updatePaths(actors, ps);
|
|
|
|
|
|
|
|
foreach_tile_pos(cb.get(), [&](const CPlayerSpecificInfoCallback * cb, const int3 & pos)
|
|
|
|
{
|
2024-02-03 12:20:59 +02:00
|
|
|
if(nodes.find(pos) != nodes.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto guarded = ai->cb->getGuardingCreaturePosition(pos).valid();
|
|
|
|
|
|
|
|
if(guarded)
|
|
|
|
return;
|
|
|
|
|
2024-01-20 22:54:30 +02:00
|
|
|
auto paths = ai->pathfinder->getPathInfo(pos);
|
|
|
|
|
|
|
|
for(AIPath & path1 : paths)
|
|
|
|
{
|
|
|
|
for(AIPath & path2 : paths)
|
|
|
|
{
|
|
|
|
if(path1.targetHero == path2.targetHero)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto obj1 = actorObjectMap[path1.targetHero];
|
|
|
|
auto obj2 = actorObjectMap[path2.targetHero];
|
|
|
|
|
2024-02-03 12:20:59 +02:00
|
|
|
auto danger = ai->pathfinder->getStorage()->evaluateDanger(obj2->visitablePos(), path1.targetHero, true);
|
|
|
|
|
|
|
|
auto updated = nodes[obj1->visitablePos()].connections[obj2->visitablePos()].update(
|
2024-01-20 22:54:30 +02:00
|
|
|
path1.movementCost() + path2.movementCost(),
|
2024-02-03 12:20:59 +02:00
|
|
|
danger);
|
|
|
|
|
|
|
|
#if NKAI_GRAPH_TRACE_LEVEL >= 2
|
|
|
|
if(updated)
|
|
|
|
{
|
|
|
|
logAi->trace(
|
|
|
|
"Connected %s[%s] -> %s[%s] through [%s], cost %2f",
|
|
|
|
obj1->getObjectName(), obj1->visitablePos().toString(),
|
|
|
|
obj2->getObjectName(), obj2->visitablePos().toString(),
|
|
|
|
pos.toString(),
|
|
|
|
path1.movementCost() + path2.movementCost());
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
(void)updated;
|
|
|
|
#endif
|
2024-01-20 22:54:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
for(auto h : actors)
|
|
|
|
{
|
|
|
|
delete h.first;
|
|
|
|
}
|
2024-02-03 12:20:59 +02:00
|
|
|
|
|
|
|
#if NKAI_GRAPH_TRACE_LEVEL >= 1
|
|
|
|
dumpToLog("graph");
|
|
|
|
#endif
|
2024-01-20 22:54:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectGraph::addObject(const CGObjectInstance * obj)
|
|
|
|
{
|
|
|
|
nodes[obj->visitablePos()].init(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectGraph::connectHeroes(const Nullkiller * ai)
|
|
|
|
{
|
|
|
|
for(auto obj : ai->memory->visitableObjs)
|
|
|
|
{
|
|
|
|
if(obj && obj->ID == Obj::HERO)
|
|
|
|
{
|
|
|
|
addObject(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-03 12:20:59 +02:00
|
|
|
for(auto & node : nodes)
|
2024-01-20 22:54:30 +02:00
|
|
|
{
|
|
|
|
auto pos = node.first;
|
|
|
|
auto paths = ai->pathfinder->getPathInfo(pos);
|
|
|
|
|
|
|
|
for(AIPath & path : paths)
|
|
|
|
{
|
|
|
|
auto heroPos = path.targetHero->visitablePos();
|
|
|
|
|
|
|
|
nodes[pos].connections[heroPos].update(
|
|
|
|
path.movementCost(),
|
|
|
|
path.getPathDanger());
|
|
|
|
|
|
|
|
nodes[heroPos].connections[pos].update(
|
|
|
|
path.movementCost(),
|
|
|
|
path.getPathDanger());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-03 12:20:59 +02:00
|
|
|
void ObjectGraph::dumpToLog(std::string visualKey) const
|
|
|
|
{
|
|
|
|
logVisual->updateWithLock(visualKey, [&](IVisualLogBuilder & logBuilder)
|
|
|
|
{
|
|
|
|
for(auto & tile : nodes)
|
|
|
|
{
|
|
|
|
for(auto & node : tile.second.connections)
|
|
|
|
{
|
|
|
|
#if NKAI_GRAPH_TRACE_LEVEL >= 2
|
|
|
|
logAi->trace(
|
|
|
|
"%s -> %s: %f !%d",
|
|
|
|
node.first.toString(),
|
|
|
|
tile.first.toString(),
|
|
|
|
node.second.cost,
|
|
|
|
node.second.danger);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
logBuilder.addLine(node.first, tile.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-27 22:19:27 +02:00
|
|
|
bool GraphNodeComparer::operator()(const GraphPathNodePointer & lhs, const GraphPathNodePointer & rhs) const
|
2024-01-20 22:54:30 +02:00
|
|
|
{
|
2024-01-27 22:19:27 +02:00
|
|
|
return pathNodes.at(lhs.coord)[lhs.nodeType].cost > pathNodes.at(rhs.coord)[rhs.nodeType].cost;
|
2024-01-20 22:54:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GraphPaths::calculatePaths(const CGHeroInstance * targetHero, const Nullkiller * ai)
|
|
|
|
{
|
|
|
|
graph = *ai->baseGraph;
|
|
|
|
graph.connectHeroes(ai);
|
|
|
|
|
2024-02-03 12:20:59 +02:00
|
|
|
visualKey = std::to_string(ai->playerID) + ":" + targetHero->getNameTranslated();
|
2024-01-20 22:54:30 +02:00
|
|
|
pathNodes.clear();
|
|
|
|
|
|
|
|
GraphNodeComparer cmp(pathNodes);
|
|
|
|
GraphPathNode::TFibHeap pq(cmp);
|
|
|
|
|
2024-01-27 22:19:27 +02:00
|
|
|
pathNodes[targetHero->visitablePos()][GrapthPathNodeType::NORMAL].cost = 0;
|
|
|
|
pq.emplace(GraphPathNodePointer(targetHero->visitablePos(), GrapthPathNodeType::NORMAL));
|
2024-01-20 22:54:30 +02:00
|
|
|
|
|
|
|
while(!pq.empty())
|
|
|
|
{
|
2024-01-27 22:19:27 +02:00
|
|
|
GraphPathNodePointer pos = pq.top();
|
2024-01-20 22:54:30 +02:00
|
|
|
pq.pop();
|
|
|
|
|
2024-01-27 22:19:27 +02:00
|
|
|
auto & node = getNode(pos);
|
2024-01-20 22:54:30 +02:00
|
|
|
|
|
|
|
node.isInQueue = false;
|
|
|
|
|
2024-01-27 22:19:27 +02:00
|
|
|
graph.iterateConnections(pos.coord, [&](int3 target, ObjectLink o)
|
2024-01-20 22:54:30 +02:00
|
|
|
{
|
2024-02-03 12:20:59 +02:00
|
|
|
auto graphNode = graph.getNode(target);
|
|
|
|
auto targetNodeType = o.danger ? GrapthPathNodeType::BATTLE : pos.nodeType;
|
|
|
|
|
|
|
|
auto targetPointer = GraphPathNodePointer(target, targetNodeType);
|
2024-01-27 22:19:27 +02:00
|
|
|
auto & targetNode = getNode(targetPointer);
|
2024-01-20 22:54:30 +02:00
|
|
|
|
|
|
|
if(targetNode.tryUpdate(pos, node, o))
|
|
|
|
{
|
2024-02-03 12:20:59 +02:00
|
|
|
if(graph.getNode(target).objTypeID == Obj::HERO)
|
|
|
|
return;
|
|
|
|
|
2024-01-20 22:54:30 +02:00
|
|
|
if(targetNode.isInQueue)
|
|
|
|
{
|
|
|
|
pq.increase(targetNode.handle);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-01-27 22:19:27 +02:00
|
|
|
targetNode.handle = pq.emplace(targetPointer);
|
2024-01-20 22:54:30 +02:00
|
|
|
targetNode.isInQueue = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphPaths::dumpToLog() const
|
|
|
|
{
|
2024-02-03 12:20:59 +02:00
|
|
|
logVisual->updateWithLock(visualKey, [&](IVisualLogBuilder & logBuilder)
|
2024-01-27 22:19:27 +02:00
|
|
|
{
|
2024-02-03 12:20:59 +02:00
|
|
|
for(auto & tile : pathNodes)
|
|
|
|
{
|
|
|
|
for(auto & node : tile.second)
|
|
|
|
{
|
|
|
|
if(!node.previous.valid())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
#if NKAI_GRAPH_TRACE_LEVEL >= 2
|
|
|
|
logAi->trace(
|
|
|
|
"%s -> %s: %f !%d",
|
|
|
|
node.previous.coord.toString(),
|
|
|
|
tile.first.toString(),
|
|
|
|
node.cost,
|
|
|
|
node.danger);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
logBuilder.addLine(node.previous.coord, tile.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2024-01-20 22:54:30 +02:00
|
|
|
}
|
|
|
|
|
2024-01-27 22:19:27 +02:00
|
|
|
bool GraphPathNode::tryUpdate(const GraphPathNodePointer & pos, const GraphPathNode & prev, const ObjectLink & link)
|
|
|
|
{
|
|
|
|
auto newCost = prev.cost + link.cost;
|
|
|
|
|
|
|
|
if(newCost < cost)
|
|
|
|
{
|
2024-02-03 12:20:59 +02:00
|
|
|
if(nodeType < pos.nodeType)
|
|
|
|
{
|
|
|
|
logAi->error("Linking error");
|
|
|
|
}
|
|
|
|
|
2024-01-27 22:19:27 +02:00
|
|
|
previous = pos;
|
|
|
|
danger = prev.danger + link.danger;
|
|
|
|
cost = newCost;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-01-20 22:54:30 +02:00
|
|
|
void GraphPaths::addChainInfo(std::vector<AIPath> & paths, int3 tile, const CGHeroInstance * hero, const Nullkiller * ai) const
|
|
|
|
{
|
2024-01-27 22:19:27 +02:00
|
|
|
auto nodes = pathNodes.find(tile);
|
2024-01-20 22:54:30 +02:00
|
|
|
|
2024-01-27 22:19:27 +02:00
|
|
|
if(nodes == pathNodes.end())
|
2024-01-20 22:54:30 +02:00
|
|
|
return;
|
|
|
|
|
2024-02-03 12:20:59 +02:00
|
|
|
for(auto & node : nodes->second)
|
2024-01-27 22:19:27 +02:00
|
|
|
{
|
|
|
|
if(!node.reachable())
|
|
|
|
continue;
|
2024-01-20 22:54:30 +02:00
|
|
|
|
2024-01-27 22:19:27 +02:00
|
|
|
std::vector<int3> tilesToPass;
|
2024-01-20 22:54:30 +02:00
|
|
|
|
2024-01-27 22:19:27 +02:00
|
|
|
uint64_t danger = node.danger;
|
|
|
|
float cost = node.cost;
|
|
|
|
bool allowBattle = false;
|
2024-01-20 22:54:30 +02:00
|
|
|
|
2024-01-27 22:19:27 +02:00
|
|
|
auto current = GraphPathNodePointer(nodes->first, node.nodeType);
|
2024-01-20 22:54:30 +02:00
|
|
|
|
2024-01-27 22:19:27 +02:00
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
auto currentTile = pathNodes.find(current.coord);
|
2024-01-20 22:54:30 +02:00
|
|
|
|
2024-01-27 22:19:27 +02:00
|
|
|
if(currentTile == pathNodes.end())
|
|
|
|
break;
|
2024-01-20 22:54:30 +02:00
|
|
|
|
2024-01-27 22:19:27 +02:00
|
|
|
auto currentNode = currentTile->second[current.nodeType];
|
|
|
|
|
2024-02-03 12:20:59 +02:00
|
|
|
if(currentNode.cost == 0)
|
|
|
|
break;
|
|
|
|
|
2024-01-27 22:19:27 +02:00
|
|
|
allowBattle = allowBattle || currentNode.nodeType == GrapthPathNodeType::BATTLE;
|
|
|
|
vstd::amax(danger, currentNode.danger);
|
|
|
|
vstd::amax(cost, currentNode.cost);
|
|
|
|
|
|
|
|
tilesToPass.push_back(current.coord);
|
|
|
|
|
|
|
|
if(currentNode.cost < 2)
|
|
|
|
break;
|
|
|
|
|
|
|
|
current = currentNode.previous;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(tilesToPass.empty())
|
2024-01-20 22:54:30 +02:00
|
|
|
continue;
|
|
|
|
|
2024-01-27 22:19:27 +02:00
|
|
|
auto entryPaths = ai->pathfinder->getPathInfo(tilesToPass.back());
|
|
|
|
|
|
|
|
for(auto & path : entryPaths)
|
2024-01-20 22:54:30 +02:00
|
|
|
{
|
2024-01-27 22:19:27 +02:00
|
|
|
if(path.targetHero != hero)
|
|
|
|
continue;
|
2024-01-20 22:54:30 +02:00
|
|
|
|
2024-01-27 22:19:27 +02:00
|
|
|
for(auto graphTile = tilesToPass.rbegin(); graphTile != tilesToPass.rend(); graphTile++)
|
|
|
|
{
|
|
|
|
AIPathNodeInfo n;
|
2024-01-20 22:54:30 +02:00
|
|
|
|
2024-01-27 22:19:27 +02:00
|
|
|
n.coord = *graphTile;
|
|
|
|
n.cost = cost;
|
|
|
|
n.turns = static_cast<ui8>(cost) + 1; // just in case lets select worst scenario
|
|
|
|
n.danger = danger;
|
|
|
|
n.targetHero = hero;
|
|
|
|
|
|
|
|
for(auto & node : path.nodes)
|
|
|
|
{
|
|
|
|
node.parentIndex++;
|
|
|
|
}
|
2024-01-20 22:54:30 +02:00
|
|
|
|
2024-01-27 22:19:27 +02:00
|
|
|
path.nodes.insert(path.nodes.begin(), n);
|
|
|
|
}
|
|
|
|
|
|
|
|
path.armyLoss += ai->pathfinder->getStorage()->evaluateArmyLoss(path.targetHero, path.heroArmy->getArmyStrength(), danger);
|
|
|
|
path.targetObjectDanger = ai->pathfinder->getStorage()->evaluateDanger(tile, path.targetHero, !allowBattle);
|
|
|
|
path.targetObjectArmyLoss = ai->pathfinder->getStorage()->evaluateArmyLoss(path.targetHero, path.heroArmy->getArmyStrength(), path.targetObjectDanger);
|
|
|
|
|
|
|
|
paths.push_back(path);
|
|
|
|
}
|
2024-01-20 22:54:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|