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"
|
2024-02-11 15:27:56 +02:00
|
|
|
#include "../../../lib/CRandomGenerator.h"
|
2024-01-20 22:54:30 +02:00
|
|
|
#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-03-17 09:34:54 +02:00
|
|
|
#include "Actions/QuestAction.h"
|
2024-01-20 22:54:30 +02:00
|
|
|
|
|
|
|
namespace NKAI
|
|
|
|
{
|
|
|
|
|
2024-03-17 09:34:54 +02:00
|
|
|
struct ConnectionCostInfo
|
|
|
|
{
|
|
|
|
float totalCost = 0;
|
|
|
|
float avg = 0;
|
|
|
|
int connectionsCount = 0;
|
|
|
|
};
|
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
class ObjectGraphCalculator
|
2024-01-20 22:54:30 +02:00
|
|
|
{
|
2024-03-09 16:20:00 +02:00
|
|
|
private:
|
|
|
|
ObjectGraph * target;
|
|
|
|
const Nullkiller * ai;
|
2024-01-20 22:54:30 +02:00
|
|
|
|
|
|
|
std::map<const CGHeroInstance *, HeroRole> actors;
|
|
|
|
std::map<const CGHeroInstance *, const CGObjectInstance *> actorObjectMap;
|
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
std::vector<std::unique_ptr<CGBoat>> temporaryBoats;
|
|
|
|
std::vector<std::unique_ptr<CGHeroInstance>> temporaryActorHeroes;
|
2024-01-20 22:54:30 +02:00
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
public:
|
|
|
|
ObjectGraphCalculator(ObjectGraph * target, const Nullkiller * ai)
|
|
|
|
:ai(ai), target(target)
|
2024-03-17 09:34:54 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void setGraphObjects()
|
2024-03-09 16:20:00 +02:00
|
|
|
{
|
|
|
|
for(auto obj : ai->memory->visitableObjs)
|
2024-02-11 15:27:56 +02:00
|
|
|
{
|
2024-03-17 09:34:54 +02:00
|
|
|
if(obj && obj->isVisitable() && obj->ID != Obj::HERO && obj->ID != Obj::EVENT)
|
2024-03-09 16:20:00 +02:00
|
|
|
{
|
|
|
|
addObjectActor(obj);
|
|
|
|
}
|
2024-02-11 15:27:56 +02:00
|
|
|
}
|
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
for(auto town : ai->cb->getTownsInfo())
|
2024-01-20 22:54:30 +02:00
|
|
|
{
|
2024-03-09 16:20:00 +02:00
|
|
|
addObjectActor(town);
|
2024-01-20 22:54:30 +02:00
|
|
|
}
|
2024-03-17 09:34:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void calculateConnections()
|
|
|
|
{
|
|
|
|
updatePaths();
|
|
|
|
|
|
|
|
foreach_tile_pos(ai->cb.get(), [this](const CPlayerSpecificInfoCallback * cb, const int3 & pos)
|
|
|
|
{
|
|
|
|
calculateConnections(pos);
|
|
|
|
});
|
|
|
|
|
|
|
|
removeExtraConnections();
|
|
|
|
}
|
|
|
|
|
2024-03-23 11:44:15 +02:00
|
|
|
float getNeighborConnectionsCost(const int3 & pos)
|
|
|
|
{
|
|
|
|
float neighborCost = std::numeric_limits<float>::max();
|
|
|
|
|
|
|
|
if(NKAI_GRAPH_TRACE_LEVEL >= 2)
|
|
|
|
{
|
|
|
|
logAi->trace("Checking junction %s", pos.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach_neighbour(
|
|
|
|
ai->cb.get(),
|
|
|
|
pos,
|
|
|
|
[this, &neighborCost](const CPlayerSpecificInfoCallback * cb, const int3 & neighbor)
|
|
|
|
{
|
|
|
|
auto costTotal = this->getConnectionsCost(neighbor);
|
|
|
|
|
|
|
|
if(costTotal.connectionsCount > 2 && costTotal.avg < neighborCost)
|
|
|
|
{
|
|
|
|
neighborCost = costTotal.avg;
|
|
|
|
|
|
|
|
if(NKAI_GRAPH_TRACE_LEVEL >= 2)
|
|
|
|
{
|
|
|
|
logAi->trace("Better node found at %s", neighbor.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return neighborCost;
|
|
|
|
}
|
|
|
|
|
2024-03-17 09:34:54 +02:00
|
|
|
void addMinimalDistanceJunctions()
|
|
|
|
{
|
|
|
|
foreach_tile_pos(ai->cb.get(), [this](const CPlayerSpecificInfoCallback * cb, const int3 & pos)
|
|
|
|
{
|
|
|
|
if(target->hasNodeAt(pos))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(ai->cb->getGuardingCreaturePosition(pos).valid())
|
|
|
|
return;
|
2024-03-09 16:20:00 +02:00
|
|
|
|
2024-03-17 09:34:54 +02:00
|
|
|
ConnectionCostInfo currentCost = getConnectionsCost(pos);
|
|
|
|
|
|
|
|
if(currentCost.connectionsCount <= 2)
|
|
|
|
return;
|
|
|
|
|
2024-03-23 11:44:15 +02:00
|
|
|
float neighborCost = getNeighborConnectionsCost(pos);
|
2024-03-17 09:34:54 +02:00
|
|
|
|
|
|
|
if(currentCost.avg < neighborCost)
|
|
|
|
{
|
|
|
|
addJunctionActor(pos);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void updatePaths()
|
|
|
|
{
|
2024-03-09 16:20:00 +02:00
|
|
|
PathfinderSettings ps;
|
|
|
|
|
|
|
|
ps.mainTurnDistanceLimit = 5;
|
|
|
|
ps.scoutTurnDistanceLimit = 1;
|
|
|
|
ps.allowBypassObjects = false;
|
|
|
|
|
|
|
|
ai->pathfinder->updatePaths(actors, ps);
|
2024-01-20 22:54:30 +02:00
|
|
|
}
|
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
void calculateConnections(const int3 & pos)
|
2024-01-20 22:54:30 +02:00
|
|
|
{
|
2024-03-17 09:34:54 +02:00
|
|
|
if(target->hasNodeAt(pos))
|
|
|
|
{
|
|
|
|
foreach_neighbour(
|
|
|
|
ai->cb.get(),
|
|
|
|
pos,
|
|
|
|
[this, &pos](const CPlayerSpecificInfoCallback * cb, const int3 & neighbor)
|
|
|
|
{
|
|
|
|
if(target->hasNodeAt(neighbor))
|
|
|
|
{
|
|
|
|
auto paths = ai->pathfinder->getPathInfo(neighbor);
|
|
|
|
|
|
|
|
for(auto & path : paths)
|
|
|
|
{
|
|
|
|
if(pos == path.targetHero->visitablePos())
|
|
|
|
{
|
|
|
|
target->tryAddConnection(pos, neighbor, path.movementCost(), path.getTotalDanger());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2024-01-20 22:54:30 +02:00
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
return;
|
2024-03-17 09:34:54 +02:00
|
|
|
}
|
2024-01-20 22:54:30 +02:00
|
|
|
|
2024-03-17 09:34:54 +02:00
|
|
|
auto guardPos = ai->cb->getGuardingCreaturePosition(pos);
|
2024-03-09 16:20:00 +02:00
|
|
|
auto paths = ai->pathfinder->getPathInfo(pos);
|
2024-01-20 22:54:30 +02:00
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
for(AIPath & path1 : paths)
|
2024-01-20 22:54:30 +02:00
|
|
|
{
|
2024-03-09 16:20:00 +02:00
|
|
|
for(AIPath & path2 : paths)
|
|
|
|
{
|
|
|
|
if(path1.targetHero == path2.targetHero)
|
|
|
|
continue;
|
2024-02-03 12:20:59 +02:00
|
|
|
|
2024-03-17 09:34:54 +02:00
|
|
|
auto pos1 = path1.targetHero->visitablePos();
|
|
|
|
auto pos2 = path2.targetHero->visitablePos();
|
|
|
|
|
|
|
|
if(guardPos.valid() && guardPos != pos1 && guardPos != pos2)
|
|
|
|
continue;
|
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
auto obj1 = actorObjectMap[path1.targetHero];
|
|
|
|
auto obj2 = actorObjectMap[path2.targetHero];
|
2024-02-03 12:20:59 +02:00
|
|
|
|
2024-03-17 09:34:54 +02:00
|
|
|
auto tile1 = cb->getTile(pos1);
|
|
|
|
auto tile2 = cb->getTile(pos2);
|
2024-02-03 12:20:59 +02:00
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
if(tile2->isWater() && !tile1->isWater())
|
2024-01-20 22:54:30 +02:00
|
|
|
{
|
2024-03-17 09:34:54 +02:00
|
|
|
if(!cb->getTile(pos)->isWater())
|
|
|
|
continue;
|
2024-03-09 16:20:00 +02:00
|
|
|
|
2024-03-17 09:34:54 +02:00
|
|
|
if(obj1 && (obj1->ID != Obj::BOAT || obj1->ID != Obj::SHIPYARD))
|
2024-01-20 22:54:30 +02:00
|
|
|
continue;
|
2024-03-09 16:20:00 +02:00
|
|
|
}
|
2024-01-20 22:54:30 +02:00
|
|
|
|
2024-03-17 09:34:54 +02:00
|
|
|
auto danger = ai->pathfinder->getStorage()->evaluateDanger(pos2, path1.targetHero, true);
|
2024-01-20 22:54:30 +02:00
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
auto updated = target->tryAddConnection(
|
2024-03-17 09:34:54 +02:00
|
|
|
pos1,
|
|
|
|
pos2,
|
2024-03-09 16:20:00 +02:00
|
|
|
path1.movementCost() + path2.movementCost(),
|
|
|
|
danger);
|
2024-02-11 15:27:56 +02:00
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
if(NKAI_GRAPH_TRACE_LEVEL >= 2 && updated)
|
|
|
|
{
|
|
|
|
logAi->trace(
|
|
|
|
"Connected %s[%s] -> %s[%s] through [%s], cost %2f",
|
2024-03-17 09:34:54 +02:00
|
|
|
obj1 ? obj1->getObjectName() : "J", pos1.toString(),
|
|
|
|
obj2 ? obj2->getObjectName() : "J", pos2.toString(),
|
2024-03-09 16:20:00 +02:00
|
|
|
pos.toString(),
|
|
|
|
path1.movementCost() + path2.movementCost());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-11 15:27:56 +02:00
|
|
|
|
2024-03-17 09:34:54 +02:00
|
|
|
bool isExtraConnection(float direct, float side1, float side2) const
|
|
|
|
{
|
|
|
|
float sideRatio = (side1 + side2) / direct;
|
|
|
|
|
|
|
|
return sideRatio < 1.25f && direct > side1 && direct > side2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeExtraConnections()
|
|
|
|
{
|
|
|
|
std::vector<std::pair<int3, int3>> connectionsToRemove;
|
|
|
|
|
|
|
|
for(auto & actor : temporaryActorHeroes)
|
|
|
|
{
|
|
|
|
auto pos = actor->visitablePos();
|
|
|
|
auto & currentNode = target->getNode(pos);
|
|
|
|
|
|
|
|
target->iterateConnections(pos, [this, &pos, &connectionsToRemove, ¤tNode](int3 n1, ObjectLink o1)
|
|
|
|
{
|
|
|
|
target->iterateConnections(n1, [&pos, &o1, ¤tNode, &connectionsToRemove, this](int3 n2, ObjectLink o2)
|
|
|
|
{
|
|
|
|
auto direct = currentNode.connections.find(n2);
|
|
|
|
|
|
|
|
if(direct != currentNode.connections.end() && isExtraConnection(direct->second.cost, o1.cost, o2.cost))
|
|
|
|
{
|
|
|
|
connectionsToRemove.push_back({pos, n2});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
vstd::removeDuplicates(connectionsToRemove);
|
|
|
|
|
|
|
|
for(auto & c : connectionsToRemove)
|
|
|
|
{
|
|
|
|
target->removeConnection(c.first, c.second);
|
|
|
|
|
|
|
|
if(NKAI_GRAPH_TRACE_LEVEL >= 2)
|
|
|
|
{
|
|
|
|
logAi->trace("Remove ineffective connection %s->%s", c.first.toString(), c.second.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
void addObjectActor(const CGObjectInstance * obj)
|
|
|
|
{
|
|
|
|
auto objectActor = temporaryActorHeroes.emplace_back(std::make_unique<CGHeroInstance>(obj->cb)).get();
|
2024-02-11 15:27:56 +02:00
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
CRandomGenerator rng;
|
|
|
|
auto visitablePos = obj->visitablePos();
|
2024-02-03 12:20:59 +02:00
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
objectActor->setOwner(ai->playerID); // lets avoid having multiple colors
|
|
|
|
objectActor->initHero(rng, static_cast<HeroTypeID>(0));
|
|
|
|
objectActor->pos = objectActor->convertFromVisitablePos(visitablePos);
|
|
|
|
objectActor->initObj(rng);
|
2024-02-03 12:20:59 +02:00
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
if(cb->getTile(visitablePos)->isWater())
|
|
|
|
{
|
|
|
|
objectActor->boat = temporaryBoats.emplace_back(std::make_unique<CGBoat>(objectActor->cb)).get();
|
|
|
|
}
|
2024-01-20 22:54:30 +02:00
|
|
|
|
2024-03-17 09:34:54 +02:00
|
|
|
assert(objectActor->visitablePos() == visitablePos);
|
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
actorObjectMap[objectActor] = obj;
|
|
|
|
actors[objectActor] = obj->ID == Obj::TOWN || obj->ID == Obj::SHIPYARD ? HeroRole::MAIN : HeroRole::SCOUT;
|
2024-02-03 12:20:59 +02:00
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
target->addObject(obj);
|
2024-03-17 09:34:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void addJunctionActor(const int3 & visitablePos)
|
|
|
|
{
|
|
|
|
auto internalCb = temporaryActorHeroes.front()->cb;
|
|
|
|
auto objectActor = temporaryActorHeroes.emplace_back(std::make_unique<CGHeroInstance>(internalCb)).get();
|
|
|
|
|
|
|
|
CRandomGenerator rng;
|
|
|
|
|
|
|
|
objectActor->setOwner(ai->playerID); // lets avoid having multiple colors
|
|
|
|
objectActor->initHero(rng, static_cast<HeroTypeID>(0));
|
|
|
|
objectActor->pos = objectActor->convertFromVisitablePos(visitablePos);
|
|
|
|
objectActor->initObj(rng);
|
|
|
|
|
|
|
|
if(cb->getTile(visitablePos)->isWater())
|
|
|
|
{
|
|
|
|
objectActor->boat = temporaryBoats.emplace_back(std::make_unique<CGBoat>(objectActor->cb)).get();
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(objectActor->visitablePos() == visitablePos);
|
|
|
|
|
|
|
|
actorObjectMap[objectActor] = nullptr;
|
|
|
|
actors[objectActor] = HeroRole::SCOUT;
|
|
|
|
|
|
|
|
target->registerJunction(visitablePos);
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionCostInfo getConnectionsCost(const int3 & pos) const
|
|
|
|
{
|
|
|
|
auto paths = ai->pathfinder->getPathInfo(pos);
|
|
|
|
std::map<int3, float> costs;
|
|
|
|
|
|
|
|
for(auto & path : paths)
|
|
|
|
{
|
|
|
|
auto fromPos = path.targetHero->visitablePos();
|
|
|
|
auto cost = costs.find(fromPos);
|
|
|
|
|
|
|
|
if(cost == costs.end())
|
|
|
|
{
|
|
|
|
costs.emplace(fromPos, path.movementCost());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(path.movementCost() < cost->second)
|
|
|
|
{
|
|
|
|
costs[fromPos] = path.movementCost();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionCostInfo result;
|
|
|
|
|
|
|
|
for(auto & cost : costs)
|
|
|
|
{
|
|
|
|
result.totalCost += cost.second;
|
|
|
|
result.connectionsCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(result.connectionsCount)
|
|
|
|
{
|
|
|
|
result.avg = result.totalCost / result.connectionsCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2024-03-09 16:20:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
bool ObjectGraph::tryAddConnection(
|
|
|
|
const int3 & from,
|
|
|
|
const int3 & to,
|
|
|
|
float cost,
|
|
|
|
uint64_t danger)
|
|
|
|
{
|
|
|
|
return nodes[from].connections[to].update(cost, danger);
|
|
|
|
}
|
|
|
|
|
2024-03-17 09:34:54 +02:00
|
|
|
void ObjectGraph::removeConnection(const int3 & from, const int3 & to)
|
|
|
|
{
|
|
|
|
nodes[from].connections.erase(to);
|
|
|
|
}
|
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
void ObjectGraph::updateGraph(const Nullkiller * ai)
|
|
|
|
{
|
|
|
|
auto cb = ai->cb;
|
|
|
|
|
|
|
|
ObjectGraphCalculator calculator(this, ai);
|
|
|
|
|
2024-03-17 09:34:54 +02:00
|
|
|
calculator.setGraphObjects();
|
|
|
|
calculator.calculateConnections();
|
|
|
|
calculator.addMinimalDistanceJunctions();
|
|
|
|
calculator.calculateConnections();
|
2024-03-09 16:20:00 +02:00
|
|
|
|
|
|
|
if(NKAI_GRAPH_TRACE_LEVEL >= 1)
|
|
|
|
dumpToLog("graph");
|
2024-01-20 22:54:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectGraph::addObject(const CGObjectInstance * obj)
|
|
|
|
{
|
|
|
|
nodes[obj->visitablePos()].init(obj);
|
|
|
|
}
|
|
|
|
|
2024-03-17 09:34:54 +02:00
|
|
|
void ObjectGraph::registerJunction(const int3 & pos)
|
|
|
|
{
|
|
|
|
nodes[pos].initJunction();
|
|
|
|
}
|
|
|
|
|
2024-02-11 15:27:56 +02:00
|
|
|
void ObjectGraph::removeObject(const CGObjectInstance * obj)
|
|
|
|
{
|
|
|
|
nodes[obj->visitablePos()].objectExists = false;
|
|
|
|
|
|
|
|
if(obj->ID == Obj::BOAT)
|
|
|
|
{
|
|
|
|
vstd::erase_if(nodes[obj->visitablePos()].connections, [&](const std::pair<int3, ObjectLink> & link) -> bool
|
|
|
|
{
|
|
|
|
auto tile = cb->getTile(link.first, false);
|
|
|
|
|
|
|
|
return tile && tile->isWater();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-20 22:54:30 +02:00
|
|
|
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)
|
|
|
|
{
|
2024-03-17 09:34:54 +02:00
|
|
|
if(path.getFirstBlockedAction())
|
|
|
|
continue;
|
|
|
|
|
2024-01-20 22:54:30 +02:00
|
|
|
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)
|
|
|
|
{
|
2024-03-09 16:20:00 +02:00
|
|
|
if(NKAI_GRAPH_TRACE_LEVEL >= 2)
|
|
|
|
{
|
|
|
|
logAi->trace(
|
|
|
|
"%s -> %s: %f !%d",
|
|
|
|
node.first.toString(),
|
|
|
|
tile.first.toString(),
|
|
|
|
node.second.cost,
|
|
|
|
node.second.danger);
|
|
|
|
}
|
2024-02-03 12:20:59 +02:00
|
|
|
|
2024-02-11 15:27:56 +02:00
|
|
|
logBuilder.addLine(tile.first, node.first);
|
2024-02-03 12:20:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-03-17 09:34:54 +02:00
|
|
|
auto & node = getOrCreateNode(pos);
|
|
|
|
std::shared_ptr<SpecialAction> transitionAction;
|
|
|
|
|
|
|
|
if(node.obj)
|
|
|
|
{
|
|
|
|
if(node.obj->ID == Obj::QUEST_GUARD
|
|
|
|
|| node.obj->ID == Obj::BORDERGUARD
|
|
|
|
|| node.obj->ID == Obj::BORDER_GATE)
|
|
|
|
{
|
|
|
|
auto questObj = dynamic_cast<const IQuestObject *>(node.obj);
|
|
|
|
auto questInfo = QuestInfo(questObj->quest, node.obj, pos.coord);
|
|
|
|
|
|
|
|
if(node.obj->ID == Obj::QUEST_GUARD
|
|
|
|
&& questObj->quest->mission == Rewardable::Limiter{}
|
|
|
|
&& questObj->quest->killTarget == ObjectInstanceID::NONE)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto questAction = std::make_shared<AIPathfinding::QuestAction>(questInfo);
|
|
|
|
|
|
|
|
if(!questAction->canAct(targetHero))
|
|
|
|
{
|
|
|
|
transitionAction = questAction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-20 22:54:30 +02:00
|
|
|
|
|
|
|
node.isInQueue = false;
|
|
|
|
|
2024-03-17 09:34:54 +02:00
|
|
|
graph.iterateConnections(pos.coord, [this, ai, &pos, &node, &transitionAction, &pq](int3 target, ObjectLink o)
|
2024-01-20 22:54:30 +02:00
|
|
|
{
|
2024-03-17 09:34:54 +02:00
|
|
|
auto targetNodeType = o.danger || transitionAction ? GrapthPathNodeType::BATTLE : pos.nodeType;
|
2024-02-03 12:20:59 +02:00
|
|
|
auto targetPointer = GraphPathNodePointer(target, targetNodeType);
|
2024-03-17 09:34:54 +02:00
|
|
|
auto & targetNode = getOrCreateNode(targetPointer);
|
2024-01-20 22:54:30 +02:00
|
|
|
|
|
|
|
if(targetNode.tryUpdate(pos, node, o))
|
|
|
|
{
|
2024-03-17 09:34:54 +02:00
|
|
|
targetNode.specialAction = transitionAction;
|
|
|
|
|
|
|
|
auto targetGraphNode = graph.getNode(target);
|
|
|
|
|
|
|
|
if(targetGraphNode.objID.hasValue())
|
|
|
|
{
|
|
|
|
targetNode.obj = ai->cb->getObj(targetGraphNode.objID, false);
|
|
|
|
|
|
|
|
if(targetNode.obj && targetNode.obj->ID == Obj::HERO)
|
|
|
|
return;
|
|
|
|
}
|
2024-02-03 12:20:59 +02:00
|
|
|
|
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;
|
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
if(NKAI_GRAPH_TRACE_LEVEL >= 2)
|
|
|
|
{
|
|
|
|
logAi->trace(
|
|
|
|
"%s -> %s: %f !%d",
|
|
|
|
node.previous.coord.toString(),
|
|
|
|
tile.first.toString(),
|
|
|
|
node.cost,
|
|
|
|
node.danger);
|
|
|
|
}
|
2024-02-03 12:20:59 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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-03-17 09:34:54 +02:00
|
|
|
std::vector<GraphPathNodePointer> 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-03-09 16:20:00 +02:00
|
|
|
if(!currentNode.previous.valid())
|
2024-02-03 12:20:59 +02:00
|
|
|
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);
|
|
|
|
|
2024-03-17 09:34:54 +02:00
|
|
|
tilesToPass.push_back(current);
|
2024-01-27 22:19:27 +02:00
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
if(currentNode.cost < 2.0f)
|
2024-01-27 22:19:27 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
current = currentNode.previous;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(tilesToPass.empty())
|
2024-01-20 22:54:30 +02:00
|
|
|
continue;
|
|
|
|
|
2024-03-17 09:34:54 +02:00
|
|
|
auto entryPaths = ai->pathfinder->getPathInfo(tilesToPass.back().coord);
|
2024-01-27 22:19:27 +02:00
|
|
|
|
|
|
|
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-03-17 09:34:54 +02:00
|
|
|
n.coord = graphTile->coord;
|
2024-01-27 22:19:27 +02:00
|
|
|
n.cost = cost;
|
|
|
|
n.turns = static_cast<ui8>(cost) + 1; // just in case lets select worst scenario
|
|
|
|
n.danger = danger;
|
|
|
|
n.targetHero = hero;
|
2024-03-17 09:34:54 +02:00
|
|
|
n.parentIndex = -1;
|
|
|
|
n.specialAction = getNode(*graphTile).specialAction;
|
2024-01-27 22:19:27 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|