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

882 lines
22 KiB
C++
Raw Normal View History

2021-05-15 18:22:44 +02:00
/*
* AINodeStorage.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 "AINodeStorage.h"
#include "Actions/TownPortalAction.h"
#include "../Goals/Goals.h"
#include "../../../CCallback.h"
#include "../../../lib/mapping/CMap.h"
#include "../../../lib/mapObjects/MapObjects.h"
#include "../../../lib/PathfinderUtil.h"
#include "../../../lib/CPlayerState.h"
AINodeStorage::AINodeStorage(const int3 & Sizes)
: sizes(Sizes)
{
nodes.resize(boost::extents[sizes.x][sizes.y][sizes.z][EPathfindingLayer::NUM_LAYERS][NUM_CHAINS]);
dangerEvaluator.reset(new FuzzyHelper());
}
AINodeStorage::~AINodeStorage() = default;
2021-05-15 18:22:49 +02:00
void AINodeStorage::initialize(const PathfinderOptions & options, const CGameState * gs)
2021-05-15 18:22:44 +02:00
{
2021-05-15 20:01:48 +02:00
if(heroChainPass)
return;
2021-05-15 18:22:44 +02:00
2021-05-15 20:01:48 +02:00
//TODO: fix this code duplication with NodeStorage::initialize, problem is to keep `resetTile` inline
2021-05-15 18:22:44 +02:00
int3 pos;
const PlayerColor player = playerID;
const PlayerColor fowPlayer = ai->playerID;
2021-05-15 18:22:44 +02:00
const int3 sizes = gs->getMapSize();
const auto & fow = static_cast<const CGameInfoCallback *>(gs)->getPlayerTeam(fowPlayer)->fogOfWarMap;
2021-05-15 18:22:44 +02:00
//make 200% sure that these are loop invariants (also a bit shorter code), let compiler do the rest(loop unswitching)
const bool useFlying = options.useFlying;
const bool useWaterWalking = options.useWaterWalking;
for(pos.x=0; pos.x < sizes.x; ++pos.x)
{
for(pos.y=0; pos.y < sizes.y; ++pos.y)
{
for(pos.z=0; pos.z < sizes.z; ++pos.z)
{
const TerrainTile * tile = &gs->map->getTile(pos);
switch(tile->terType)
{
case ETerrainType::ROCK:
break;
case ETerrainType::WATER:
resetTile(pos, ELayer::SAIL, PathfinderUtil::evaluateAccessibility<ELayer::SAIL>(pos, tile, fow, player, gs));
if(useFlying)
resetTile(pos, ELayer::AIR, PathfinderUtil::evaluateAccessibility<ELayer::AIR>(pos, tile, fow, player, gs));
if(useWaterWalking)
resetTile(pos, ELayer::WATER, PathfinderUtil::evaluateAccessibility<ELayer::WATER>(pos, tile, fow, player, gs));
break;
default:
resetTile(pos, ELayer::LAND, PathfinderUtil::evaluateAccessibility<ELayer::LAND>(pos, tile, fow, player, gs));
if(useFlying)
resetTile(pos, ELayer::AIR, PathfinderUtil::evaluateAccessibility<ELayer::AIR>(pos, tile, fow, player, gs));
break;
}
}
}
}
}
2021-05-15 18:23:01 +02:00
void AINodeStorage::clear()
2021-05-15 18:22:49 +02:00
{
actors.clear();
2021-05-15 20:01:48 +02:00
heroChainPass = false;
heroChainTurn = 1;
2021-05-15 18:22:49 +02:00
}
2021-05-15 18:22:44 +02:00
const AIPathNode * AINodeStorage::getAINode(const CGPathNode * node) const
{
return static_cast<const AIPathNode *>(node);
}
void AINodeStorage::updateAINode(CGPathNode * node, std::function<void(AIPathNode *)> updater)
{
auto aiNode = static_cast<AIPathNode *>(node);
updater(aiNode);
}
2021-05-15 19:59:43 +02:00
boost::optional<AIPathNode *> AINodeStorage::getOrCreateNode(
const int3 & pos,
const EPathfindingLayer layer,
const ChainActor * actor)
2021-05-15 18:22:44 +02:00
{
auto chains = nodes[pos.x][pos.y][pos.z][layer];
for(AIPathNode & node : chains)
{
2021-05-15 18:22:49 +02:00
if(node.actor == actor)
2021-05-15 18:22:44 +02:00
{
return &node;
}
2021-05-15 18:22:49 +02:00
if(!node.actor)
2021-05-15 18:22:44 +02:00
{
2021-05-15 18:22:49 +02:00
node.actor = actor;
2021-05-15 18:22:44 +02:00
return &node;
}
}
return boost::none;
}
2021-05-15 18:22:49 +02:00
std::vector<CGPathNode *> AINodeStorage::getInitialNodes()
2021-05-15 18:22:44 +02:00
{
2021-05-15 20:01:48 +02:00
if(heroChainPass)
return heroChain;
2021-05-15 18:22:49 +02:00
std::vector<CGPathNode *> initialNodes;
for(auto actorPtr : actors)
{
2021-05-15 19:59:43 +02:00
ChainActor * actor = actorPtr.get();
2021-05-15 18:22:49 +02:00
AIPathNode * initialNode =
getOrCreateNode(actor->initialPosition, actor->layer, actor)
.get();
2021-05-15 18:22:44 +02:00
2021-05-15 18:22:49 +02:00
initialNode->turns = actor->initialTurn;
initialNode->moveRemains = actor->initialMovement;
initialNode->danger = 0;
2021-05-15 20:04:48 +02:00
initialNode->cost = actor->initialTurn;
initialNode->action = CGPathNode::ENodeAction::NORMAL;
2021-05-15 18:22:44 +02:00
2021-05-15 18:22:49 +02:00
if(actor->isMovable)
{
initialNodes.push_back(initialNode);
}
else
{
initialNode->locked = true;
}
}
return initialNodes;
2021-05-15 18:22:44 +02:00
}
void AINodeStorage::resetTile(const int3 & coord, EPathfindingLayer layer, CGPathNode::EAccessibility accessibility)
{
for(int i = 0; i < NUM_CHAINS; i++)
{
AIPathNode & heroNode = nodes[coord.x][coord.y][coord.z][layer][i];
2021-05-15 18:22:49 +02:00
heroNode.actor = nullptr;
2021-05-15 18:22:44 +02:00
heroNode.danger = 0;
heroNode.manaCost = 0;
heroNode.specialAction.reset();
2021-05-15 18:23:01 +02:00
heroNode.armyLoss = 0;
2021-05-15 20:01:48 +02:00
heroNode.chainOther = nullptr;
2021-05-15 18:22:44 +02:00
heroNode.update(coord, layer, accessibility);
}
}
void AINodeStorage::commit(CDestinationNodeInfo & destination, const PathNodeInfo & source)
{
const AIPathNode * srcNode = getAINode(source.node);
updateAINode(destination.node, [&](AIPathNode * dstNode)
{
2021-05-15 20:01:48 +02:00
commit(dstNode, srcNode, destination.action, destination.turn, destination.movementLeft, destination.cost);
2021-05-15 18:22:44 +02:00
2021-05-15 20:54:28 +02:00
if(srcNode->specialAction || srcNode->chainOther)
{
// there is some action on source tile which should be performed before we can bypass it
destination.node->theNodeBefore = source.node;
}
2021-05-15 18:22:49 +02:00
if(dstNode->specialAction && dstNode->actor)
2021-05-15 18:22:44 +02:00
{
2021-05-15 18:22:49 +02:00
dstNode->specialAction->applyOnDestination(dstNode->actor->hero, destination, source, dstNode, srcNode);
2021-05-15 18:22:44 +02:00
}
2021-05-15 20:04:48 +02:00
#if VCMI_TRACE_PATHFINDER >= 2
2021-05-15 20:04:48 +02:00
logAi->trace(
2021-05-15 20:56:08 +02:00
"Commited %s -> %s, cost: %f, hero: %s, mask: %x, army: %i",
2021-05-15 20:04:48 +02:00
source.coord.toString(),
destination.coord.toString(),
destination.cost,
2021-05-15 20:54:28 +02:00
dstNode->actor->toString(),
2021-05-15 20:56:08 +02:00
dstNode->actor->chainMask,
dstNode->actor->armyValue);
2021-05-15 20:04:48 +02:00
#endif
2021-05-15 18:22:44 +02:00
});
}
2021-05-15 20:01:48 +02:00
void AINodeStorage::commit(
AIPathNode * destination,
const AIPathNode * source,
CGPathNode::ENodeAction action,
int turn,
int movementLeft,
float cost) const
{
2021-05-15 20:04:11 +02:00
destination->action = action;
2021-05-15 20:01:48 +02:00
destination->cost = cost;
destination->moveRemains = movementLeft;
destination->turns = turn;
destination->armyLoss = source->armyLoss;
destination->manaCost = source->manaCost;
destination->danger = source->danger;
destination->theNodeBefore = source->theNodeBefore;
2021-05-15 20:04:48 +02:00
destination->chainOther = nullptr;
2021-05-15 20:01:48 +02:00
}
2021-05-15 18:22:44 +02:00
std::vector<CGPathNode *> AINodeStorage::calculateNeighbours(
const PathNodeInfo & source,
const PathfinderConfig * pathfinderConfig,
const CPathfinderHelper * pathfinderHelper)
{
std::vector<CGPathNode *> neighbours;
neighbours.reserve(16);
const AIPathNode * srcNode = getAINode(source.node);
auto accessibleNeighbourTiles = pathfinderHelper->getNeighbourTiles(source);
for(auto & neighbour : accessibleNeighbourTiles)
{
for(EPathfindingLayer i = EPathfindingLayer::LAND; i <= EPathfindingLayer::AIR; i.advance(1))
{
2021-05-15 18:22:49 +02:00
auto nextNode = getOrCreateNode(neighbour, i, srcNode->actor);
2021-05-15 18:22:44 +02:00
if(!nextNode || nextNode.get()->accessible == CGPathNode::NOT_SET)
continue;
neighbours.push_back(nextNode.get());
}
}
2021-05-15 19:59:43 +02:00
2021-05-15 18:22:44 +02:00
return neighbours;
}
2021-05-15 20:01:48 +02:00
bool AINodeStorage::calculateHeroChain()
{
heroChainPass = true;
heroChain.resize(0);
2021-05-15 20:54:58 +02:00
std::vector<AIPathNode *> existingChains;
std::vector<ExchangeCandidate> newChains;
2021-05-15 20:04:48 +02:00
2021-05-15 20:54:58 +02:00
existingChains.reserve(NUM_CHAINS);
newChains.reserve(NUM_CHAINS);
2021-05-15 20:04:48 +02:00
2021-05-15 20:01:48 +02:00
foreach_tile_pos([&](const int3 & pos) {
auto layer = EPathfindingLayer::LAND;
auto chains = nodes[pos.x][pos.y][pos.z][layer];
2021-05-15 20:54:58 +02:00
existingChains.resize(0);
newChains.resize(0);
2021-05-15 20:04:48 +02:00
2021-05-15 20:01:48 +02:00
for(AIPathNode & node : chains)
{
2021-05-15 20:56:08 +02:00
if(node.turns <= heroChainTurn && node.action != CGPathNode::ENodeAction::UNKNOWN)
2021-05-15 20:54:58 +02:00
existingChains.push_back(&node);
2021-05-15 20:04:48 +02:00
}
2021-05-15 20:54:58 +02:00
for(AIPathNode * node : existingChains)
2021-05-15 20:04:48 +02:00
{
2021-05-15 20:56:08 +02:00
if(node->actor->isMovable)
2021-05-15 20:54:28 +02:00
{
2021-05-15 20:54:58 +02:00
calculateHeroChain(node, existingChains, newChains);
2021-05-15 20:54:28 +02:00
}
2021-05-15 20:01:48 +02:00
}
2021-05-15 20:54:58 +02:00
cleanupInefectiveChains(newChains);
addHeroChain(newChains);
2021-05-15 20:01:48 +02:00
});
return heroChain.size();
}
2021-05-15 20:54:58 +02:00
void AINodeStorage::cleanupInefectiveChains(std::vector<ExchangeCandidate> & result) const
{
vstd::erase_if(result, [&](ExchangeCandidate & chainInfo) -> bool
{
auto pos = chainInfo.coord;
auto chains = nodes[pos.x][pos.y][pos.z][EPathfindingLayer::LAND];
return hasBetterChain(chainInfo.carrierParent, &chainInfo, chains)
|| hasBetterChain(chainInfo.carrierParent, &chainInfo, result);
});
}
void AINodeStorage::calculateHeroChain(
AIPathNode * srcNode,
const std::vector<AIPathNode *> & variants,
std::vector<ExchangeCandidate> & result) const
2021-05-15 19:59:43 +02:00
{
2021-05-15 20:04:48 +02:00
for(AIPathNode * node : variants)
2021-05-15 19:59:43 +02:00
{
if(node == srcNode
|| !node->actor
|| node->turns > heroChainTurn
|| (node->action == CGPathNode::ENodeAction::UNKNOWN && node->actor->hero)
2021-05-15 20:56:08 +02:00
|| (node->actor->chainMask & srcNode->actor->chainMask) != 0)
2021-05-15 19:59:43 +02:00
{
continue;
}
#if VCMI_TRACE_PATHFINDER >= 2
2021-05-15 20:04:48 +02:00
logAi->trace(
"Thy exchange %s[%i] -> %s[%i] at %s",
2021-05-15 20:54:28 +02:00
node->actor->toString(),
2021-05-15 20:04:48 +02:00
node->actor->chainMask,
2021-05-15 20:54:28 +02:00
srcNode->actor->toString(),
2021-05-15 20:04:48 +02:00
srcNode->actor->chainMask,
srcNode->coord.toString());
#endif
2021-05-15 20:54:58 +02:00
calculateHeroChain(srcNode, node, result);
2021-05-15 19:59:43 +02:00
}
}
2021-05-15 20:54:58 +02:00
void AINodeStorage::calculateHeroChain(
AIPathNode * carrier,
AIPathNode * other,
std::vector<ExchangeCandidate> & result) const
2021-05-15 20:56:08 +02:00
{
if(carrier->armyLoss < carrier->actor->armyValue
&& (carrier->action != CGPathNode::BATTLE || (carrier->actor->allowBattle && carrier->specialAction))
2021-05-16 12:52:30 +02:00
&& carrier->action != CGPathNode::BLOCKING_VISIT
&& other->armyLoss < other->actor->armyValue
&& carrier->actor->canExchange(other->actor))
2021-05-15 19:59:43 +02:00
{
#if VCMI_TRACE_PATHFINDER >= 2
2021-05-15 20:04:48 +02:00
logAi->trace(
"Exchange allowed %s[%i] -> %s[%i] at %s",
2021-05-15 20:54:28 +02:00
other->actor->toString(),
2021-05-15 20:04:48 +02:00
other->actor->chainMask,
2021-05-15 20:54:28 +02:00
carrier->actor->toString(),
2021-05-15 20:04:48 +02:00
carrier->actor->chainMask,
carrier->coord.toString());
#endif
if(other->actor->isMovable)
2021-05-15 20:04:48 +02:00
{
bool hasLessMp = carrier->turns > other->turns || carrier->moveRemains < other->moveRemains;
bool hasLessExperience = carrier->actor->hero->exp < other->actor->hero->exp;
if(hasLessMp && hasLessExperience)
{
#if VCMI_TRACE_PATHFINDER >= 2
logAi->trace("Exchange at %s is ineficient. Blocked.", carrier->coord.toString());
2021-05-15 20:04:48 +02:00
#endif
return;
}
2021-05-15 20:04:48 +02:00
}
2021-05-15 19:59:43 +02:00
auto newActor = carrier->actor->exchange(other->actor);
2021-05-15 20:54:58 +02:00
result.push_back(calculateExchange(newActor, carrier, other));
}
}
void AINodeStorage::addHeroChain(const std::vector<ExchangeCandidate> & result)
{
for(const ExchangeCandidate & chainInfo : result)
{
auto carrier = chainInfo.carrierParent;
auto newActor = chainInfo.actor;
auto other = chainInfo.otherParent;
2021-05-15 19:59:43 +02:00
auto chainNodeOptional = getOrCreateNode(carrier->coord, carrier->layer, newActor);
if(!chainNodeOptional)
2021-05-15 20:04:48 +02:00
{
#if VCMI_TRACE_PATHFINDER >= 2
2021-05-15 20:04:48 +02:00
logAi->trace("Exchange at %s can not allocate node. Blocked.", carrier->coord.toString());
#endif
2021-05-15 20:54:58 +02:00
continue;
2021-05-15 20:04:48 +02:00
}
2021-05-15 19:59:43 +02:00
2021-05-15 20:54:58 +02:00
auto exchangeNode = chainNodeOptional.get();
2021-05-15 19:59:43 +02:00
2021-05-15 20:54:58 +02:00
if(exchangeNode->action != CGPathNode::ENodeAction::UNKNOWN)
2021-05-15 20:04:48 +02:00
{
#if VCMI_TRACE_PATHFINDER >= 2
2021-05-15 20:04:48 +02:00
logAi->trace("Exchange at %s node is already in use. Blocked.", carrier->coord.toString());
2021-05-15 20:01:48 +02:00
#endif
2021-05-15 20:54:58 +02:00
continue;
2021-05-15 20:04:48 +02:00
}
2021-05-15 20:01:48 +02:00
2021-05-15 20:54:58 +02:00
if(exchangeNode->turns != 0xFF && exchangeNode->cost < chainInfo.cost)
2021-05-15 20:04:48 +02:00
{
#if VCMI_TRACE_PATHFINDER >= 2
2021-05-15 20:04:48 +02:00
logAi->trace(
2021-05-15 20:54:58 +02:00
"Exchange at %s is is not effective enough. %f < %f",
exchangeNode->coord.toString(),
exchangeNode->cost,
chainInfo.cost);
2021-05-15 20:04:48 +02:00
#endif
2021-05-15 20:54:58 +02:00
continue;
2021-05-15 20:04:48 +02:00
}
2021-05-15 20:54:58 +02:00
commit(exchangeNode, carrier, carrier->action, chainInfo.turns, chainInfo.moveRemains, chainInfo.cost);
exchangeNode->chainOther = other;
exchangeNode->armyLoss = chainInfo.armyLoss;
#if VCMI_TRACE_PATHFINDER >= 2
2021-05-15 20:54:58 +02:00
logAi->trace(
2021-05-15 20:56:08 +02:00
"Chain accepted at %s %s -> %s, mask %x, cost %f, army %i",
2021-05-15 20:54:58 +02:00
exchangeNode->coord.toString(),
other->actor->toString(),
exchangeNode->actor->toString(),
exchangeNode->actor->chainMask,
2021-05-15 20:56:08 +02:00
exchangeNode->cost,
exchangeNode->actor->armyValue);
2021-05-15 20:54:58 +02:00
#endif
heroChain.push_back(exchangeNode);
2021-05-15 20:01:48 +02:00
}
}
2021-05-15 20:54:58 +02:00
ExchangeCandidate AINodeStorage::calculateExchange(
ChainActor * exchangeActor,
2021-05-15 20:01:48 +02:00
AIPathNode * carrierParentNode,
AIPathNode * otherParentNode) const
{
2021-05-15 20:54:58 +02:00
ExchangeCandidate candidate;
2021-05-15 20:54:58 +02:00
candidate.layer = carrierParentNode->layer;
candidate.coord = carrierParentNode->coord;
candidate.carrierParent = carrierParentNode;
candidate.otherParent = otherParentNode;
candidate.actor = exchangeActor;
candidate.armyLoss = carrierParentNode->armyLoss + otherParentNode->armyLoss;
candidate.turns = carrierParentNode->turns;
candidate.cost = carrierParentNode->cost + otherParentNode->cost / 1000.0;
candidate.moveRemains = carrierParentNode->moveRemains;
2021-05-15 20:01:48 +02:00
if(carrierParentNode->turns < otherParentNode->turns)
{
2021-05-15 20:54:58 +02:00
int moveRemains = exchangeActor->hero->maxMovePoints(carrierParentNode->layer);
2021-05-15 20:01:48 +02:00
float waitingCost = otherParentNode->turns - carrierParentNode->turns - 1
+ carrierParentNode->moveRemains / (float)moveRemains;
2021-05-15 19:59:43 +02:00
2021-05-15 20:54:58 +02:00
candidate.turns = otherParentNode->turns;
candidate.cost += waitingCost;
candidate.moveRemains = moveRemains;
2021-05-15 19:59:43 +02:00
}
2021-05-15 20:01:48 +02:00
2021-05-15 20:54:58 +02:00
return candidate;
2021-05-15 19:59:43 +02:00
}
2021-05-15 18:22:49 +02:00
const CGHeroInstance * AINodeStorage::getHero(const CGPathNode * node) const
{
auto aiNode = getAINode(node);
return aiNode->actor->hero;
}
const std::set<const CGHeroInstance *> AINodeStorage::getAllHeroes() const
{
std::set<const CGHeroInstance *> heroes;
for(auto actor : actors)
{
if(actor->hero)
2021-05-15 18:23:01 +02:00
heroes.insert(actor->hero);
2021-05-15 18:22:49 +02:00
}
return heroes;
}
void AINodeStorage::setHeroes(std::vector<HeroPtr> heroes, const VCAI * _ai)
2021-05-15 18:22:44 +02:00
{
cb = _ai->myCb.get();
ai = _ai;
2021-05-15 18:22:49 +02:00
playerID = ai->playerID;
2021-05-15 18:22:49 +02:00
for(auto & hero : heroes)
{
uint64_t mask = 1 << actors.size();
2021-05-16 12:53:32 +02:00
auto actor = std::make_shared<HeroActor>(hero.get(), mask, ai);
if(hero->tempOwner != ai->playerID)
{
bool onLand = !actor->hero->boat;
actor->initialMovement = actor->hero->maxMovePoints(onLand);
}
2021-05-15 18:22:49 +02:00
playerID = hero->tempOwner;
2021-05-16 12:53:32 +02:00
actors.push_back(actor);
}
}
void AINodeStorage::setTownsAndDwellings(
const std::vector<const CGTownInstance *> & towns,
2021-05-15 20:54:28 +02:00
const std::set<const CGObjectInstance *> & visitableObjs)
{
for(auto town : towns)
{
uint64_t mask = 1 << actors.size();
2021-05-15 21:02:57 +02:00
if(!town->garrisonHero && town->getUpperArmy()->getArmyStrength())
{
actors.push_back(std::make_shared<TownGarrisonActor>(town, mask));
}
}
2021-05-15 20:56:08 +02:00
/*auto dayOfWeek = cb->getDate(Date::DAY_OF_WEEK);
auto waitForGrowth = dayOfWeek > 4;
for(auto obj: visitableObjs)
{
const CGDwelling * dwelling = dynamic_cast<const CGDwelling *>(obj);
if(dwelling)
{
uint64_t mask = 1 << actors.size();
auto dwellingActor = std::make_shared<DwellingActor>(dwelling, mask, false, dayOfWeek);
if(dwellingActor->creatureSet->getArmyStrength())
{
actors.push_back(dwellingActor);
}
if(waitForGrowth)
{
mask = 1 << actors.size();
dwellingActor = std::make_shared<DwellingActor>(dwelling, mask, waitForGrowth, dayOfWeek);
if(dwellingActor->creatureSet->getArmyStrength())
{
actors.push_back(dwellingActor);
}
}
}
2021-05-15 20:56:08 +02:00
}*/
2021-05-15 18:22:44 +02:00
}
std::vector<CGPathNode *> AINodeStorage::calculateTeleportations(
const PathNodeInfo & source,
const PathfinderConfig * pathfinderConfig,
const CPathfinderHelper * pathfinderHelper)
{
std::vector<CGPathNode *> neighbours;
if(source.isNodeObjectVisitable())
{
auto accessibleExits = pathfinderHelper->getTeleportExits(source);
auto srcNode = getAINode(source.node);
for(auto & neighbour : accessibleExits)
{
2021-05-15 18:22:49 +02:00
auto node = getOrCreateNode(neighbour, source.node->layer, srcNode->actor);
2021-05-15 18:22:44 +02:00
if(!node)
continue;
neighbours.push_back(node.get());
}
}
2021-05-15 18:23:01 +02:00
if(source.isInitialPosition)
2021-05-15 18:22:44 +02:00
{
2021-05-15 21:02:57 +02:00
calculateTownPortalTeleportations(source, neighbours, pathfinderHelper);
2021-05-15 18:22:44 +02:00
}
return neighbours;
}
void AINodeStorage::calculateTownPortalTeleportations(
const PathNodeInfo & source,
2021-05-15 21:02:57 +02:00
std::vector<CGPathNode *> & neighbours,
const CPathfinderHelper * pathfinderHelper)
2021-05-15 18:22:44 +02:00
{
SpellID spellID = SpellID::TOWN_PORTAL;
const CSpell * townPortal = spellID.toSpell();
auto srcNode = getAINode(source.node);
2021-05-15 18:23:01 +02:00
auto hero = srcNode->actor->hero;
2021-05-15 18:22:44 +02:00
if(hero->canCastThisSpell(townPortal) && hero->mana >= hero->getSpellCost(townPortal))
{
auto towns = cb->getTownsInfo(false);
vstd::erase_if(towns, [&](const CGTownInstance * t) -> bool
{
return cb->getPlayerRelations(hero->tempOwner, t->tempOwner) == PlayerRelations::ENEMIES;
});
if(!towns.size())
{
return;
}
// TODO: Copy/Paste from TownPortalMechanics
auto skillLevel = hero->getSpellSchoolLevel(townPortal);
2021-05-15 21:02:57 +02:00
auto movementNeeded = GameConstants::BASE_MOVEMENT_COST * (skillLevel >= 3 ? 2 : 3);
float movementCost = (float)movementNeeded / (float)pathfinderHelper->getMaxMovePoints(EPathfindingLayer::LAND);
movementCost += source.node->cost;
2021-05-15 18:22:44 +02:00
2021-05-15 21:02:57 +02:00
if(source.node->moveRemains < movementNeeded)
2021-05-15 18:22:44 +02:00
{
return;
}
if(skillLevel < SecSkillLevel::ADVANCED)
{
const CGTownInstance * nearestTown = *vstd::minElementByFun(towns, [&](const CGTownInstance * t) -> int
{
2021-05-15 21:02:57 +02:00
return source.coord.dist2dSQ(t->visitablePos());
2021-05-15 18:22:44 +02:00
});
towns = std::vector<const CGTownInstance *>{ nearestTown };
}
for(const CGTownInstance * targetTown : towns)
{
2021-05-15 21:02:57 +02:00
// TODO: allow to hide visiting hero in garrison
2021-05-15 18:22:44 +02:00
if(targetTown->visitingHero)
continue;
2021-05-15 18:22:49 +02:00
auto nodeOptional = getOrCreateNode(targetTown->visitablePos(), EPathfindingLayer::LAND, srcNode->actor->castActor);
2021-05-15 18:22:44 +02:00
if(nodeOptional)
{
#ifdef VCMI_TRACE_PATHFINDER
logAi->trace("Adding town portal node at %s", targetTown->name);
#endif
AIPathNode * node = nodeOptional.get();
2021-05-15 21:02:57 +02:00
if(node->action == CGPathNode::UNKNOWN || node->cost > movementCost)
{
node->theNodeBefore = source.node;
node->specialAction.reset(new AIPathfinding::TownPortalAction(targetTown));
node->moveRemains = source.node->moveRemains + movementNeeded;
node->cost = movementCost;
}
2021-05-15 18:22:44 +02:00
neighbours.push_back(node);
}
}
}
}
bool AINodeStorage::hasBetterChain(const PathNodeInfo & source, CDestinationNodeInfo & destination) const
{
auto pos = destination.coord;
auto chains = nodes[pos.x][pos.y][pos.z][EPathfindingLayer::LAND];
2021-05-15 20:54:58 +02:00
return hasBetterChain(source.node, getAINode(destination.node), chains);
}
template<class NodeRange>
bool AINodeStorage::hasBetterChain(
const CGPathNode * source,
2021-05-15 20:56:08 +02:00
const AIPathNode * candidateNode,
2021-05-15 20:54:58 +02:00
const NodeRange & chains) const
{
2021-05-15 20:56:08 +02:00
auto candidateActor = candidateNode->actor;
2021-05-15 18:22:44 +02:00
for(const AIPathNode & node : chains)
{
2021-05-15 20:56:08 +02:00
auto sameNode = node.actor == candidateNode->actor;
2021-05-15 18:22:49 +02:00
2021-05-15 20:54:58 +02:00
if(sameNode || node.action == CGPathNode::ENodeAction::UNKNOWN || !node.actor->hero)
2021-05-15 18:22:44 +02:00
{
continue;
}
2021-05-15 20:56:08 +02:00
if(node.danger <= candidateNode->danger && candidateNode->actor == node.actor->battleActor)
2021-05-15 18:22:44 +02:00
{
2021-05-15 20:56:08 +02:00
if(node.cost < candidateNode->cost)
2021-05-15 18:22:44 +02:00
{
#ifdef VCMI_TRACE_PATHFINDER
logAi->trace(
"Block ineficient move %s:->%s, mask=%i, mp diff: %i",
2021-05-15 20:54:58 +02:00
source->coord.toString(),
2021-05-15 20:56:08 +02:00
candidateNode->coord.toString(),
candidateNode->actor->chainMask,
node.moveRemains - candidateNode->moveRemains);
2021-05-15 18:22:44 +02:00
#endif
return true;
}
}
2021-05-15 20:54:58 +02:00
2021-05-15 20:56:08 +02:00
if(candidateActor->actorExchangeCount == 1
&& (candidateActor->chainMask & node.actor->chainMask) == 0)
2021-05-15 20:54:58 +02:00
continue;
auto nodeActor = node.actor;
2021-05-15 20:56:08 +02:00
auto nodeArmyValue = nodeActor->armyValue - node.armyLoss;
auto candidateArmyValue = candidateActor->armyValue - candidateNode->armyLoss;
if(nodeArmyValue > candidateArmyValue
&& node.cost <= candidateNode->cost)
{
return true;
}
2021-05-15 20:54:58 +02:00
2021-05-15 20:56:08 +02:00
if(nodeArmyValue == candidateArmyValue
&& nodeActor->heroFightingStrength >= candidateActor->heroFightingStrength
&& node.cost <= candidateNode->cost)
2021-05-15 20:54:58 +02:00
{
return true;
}
2021-05-15 18:22:44 +02:00
}
return false;
}
2021-05-15 18:22:49 +02:00
bool AINodeStorage::isTileAccessible(const HeroPtr & hero, const int3 & pos, const EPathfindingLayer layer) const
2021-05-15 18:22:44 +02:00
{
2021-05-15 18:22:49 +02:00
auto chains = nodes[pos.x][pos.y][pos.z][layer];
for(const AIPathNode & node : chains)
{
if(node.action != CGPathNode::ENodeAction::UNKNOWN
&& node.actor && node.actor->hero == hero.h)
{
return true;
}
}
2021-05-15 18:22:44 +02:00
2021-05-15 18:22:49 +02:00
return false;
2021-05-15 18:22:44 +02:00
}
std::vector<AIPath> AINodeStorage::getChainInfo(const int3 & pos, bool isOnLand) const
{
std::vector<AIPath> paths;
2021-05-15 20:01:48 +02:00
paths.reserve(NUM_CHAINS / 4);
2021-05-15 18:22:44 +02:00
auto chains = nodes[pos.x][pos.y][pos.z][isOnLand ? EPathfindingLayer::LAND : EPathfindingLayer::SAIL];
for(const AIPathNode & node : chains)
{
2021-05-15 18:23:01 +02:00
if(node.action == CGPathNode::ENodeAction::UNKNOWN || !node.actor || !node.actor->hero)
2021-05-15 18:22:44 +02:00
{
continue;
}
AIPath path;
2021-05-15 18:22:49 +02:00
path.targetHero = node.actor->hero;
2021-05-15 20:01:48 +02:00
path.heroArmy = node.actor->creatureSet;
path.armyLoss = node.armyLoss;
2021-05-15 18:23:01 +02:00
path.targetObjectDanger = evaluateDanger(pos, path.targetHero);
2021-05-15 20:04:48 +02:00
path.chainMask = node.actor->chainMask;
2021-05-16 12:52:30 +02:00
path.exchangeCount = node.actor->actorExchangeCount;
2021-05-15 20:01:48 +02:00
2021-05-15 20:56:31 +02:00
fillChainInfo(&node, path, -1);
2021-05-15 18:22:44 +02:00
paths.push_back(path);
}
return paths;
}
2021-05-15 20:56:31 +02:00
void AINodeStorage::fillChainInfo(const AIPathNode * node, AIPath & path, int parentIndex) const
2021-05-15 20:01:48 +02:00
{
while(node != nullptr)
{
2021-05-15 20:04:48 +02:00
if(!node->actor->hero)
2021-05-15 20:01:48 +02:00
return;
if(node->chainOther)
2021-05-15 20:56:31 +02:00
fillChainInfo(node->chainOther, path, parentIndex);
2021-05-15 20:01:48 +02:00
2021-05-15 20:04:48 +02:00
if(node->actor->hero->visitablePos() != node->coord)
{
AIPathNodeInfo pathNode;
pathNode.cost = node->cost;
pathNode.targetHero = node->actor->hero;
2021-05-15 21:02:57 +02:00
pathNode.specialAction = node->specialAction;
2021-05-15 20:04:48 +02:00
pathNode.turns = node->turns;
pathNode.danger = node->danger;
pathNode.coord = node->coord;
2021-05-15 20:56:31 +02:00
pathNode.parentIndex = parentIndex;
parentIndex = path.nodes.size();
2021-05-15 20:04:48 +02:00
path.nodes.push_back(pathNode);
}
path.specialAction = node->specialAction;
2021-05-15 20:01:48 +02:00
node = getAINode(node->theNodeBefore);
}
}
2021-05-15 18:22:44 +02:00
AIPath::AIPath()
: nodes({})
{
}
int3 AIPath::firstTileToGet() const
{
if(nodes.size())
{
return nodes.back().coord;
}
return int3(-1, -1, -1);
}
int3 AIPath::targetTile() const
{
if(nodes.size())
{
return nodes.front().coord;
}
return int3(-1, -1, -1);
}
2021-05-15 20:04:48 +02:00
const AIPathNodeInfo & AIPath::firstNode() const
{
return nodes.back();
}
2021-05-15 18:22:44 +02:00
uint64_t AIPath::getPathDanger() const
{
if(nodes.size())
{
return nodes.front().danger;
}
return 0;
}
float AIPath::movementCost() const
{
if(nodes.size())
{
return nodes.front().cost;
}
// TODO: boost:optional?
return 0.0;
}
uint8_t AIPath::turn() const
{
if(nodes.size())
{
return nodes.front().turns;
}
// TODO: boost:optional?
return 0;
}
2021-05-15 20:01:48 +02:00
uint64_t AIPath::getHeroStrength() const
{
return targetHero->getFightingStrength() * heroArmy->getArmyStrength();
}
2021-05-15 18:22:44 +02:00
uint64_t AIPath::getTotalDanger(HeroPtr hero) const
{
uint64_t pathDanger = getPathDanger();
uint64_t danger = pathDanger > targetObjectDanger ? pathDanger : targetObjectDanger;
return danger;
}
std::string AIPath::toString()
{
std::stringstream str;
for(auto node : nodes)
str << node.targetHero->name << "->" << node.coord.toString() << "; ";
return str.str();
2021-05-15 18:22:49 +02:00
}