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"
|
2021-05-16 14:39:38 +02:00
|
|
|
#include "../AIGateway.h"
|
2021-05-16 13:19:00 +02:00
|
|
|
#include "../Engine/Nullkiller.h"
|
2021-05-15 18:22:44 +02:00
|
|
|
#include "../../../CCallback.h"
|
|
|
|
#include "../../../lib/mapping/CMap.h"
|
|
|
|
#include "../../../lib/mapObjects/MapObjects.h"
|
|
|
|
#include "../../../lib/PathfinderUtil.h"
|
|
|
|
#include "../../../lib/CPlayerState.h"
|
|
|
|
|
2021-05-16 13:56:27 +02:00
|
|
|
std::shared_ptr<boost::multi_array<AIPathNode, 5>> AISharedStorage::shared;
|
2021-05-16 13:56:53 +02:00
|
|
|
std::set<int3> commitedTiles;
|
|
|
|
std::set<int3> commitedTilesInitial;
|
|
|
|
|
2021-05-16 14:10:59 +02:00
|
|
|
#ifdef ENVIRONMENT64
|
2021-05-16 13:56:53 +02:00
|
|
|
const int BUCKET_COUNT = 11;
|
2021-05-16 14:10:59 +02:00
|
|
|
#else
|
|
|
|
const int BUCKET_COUNT = 7;
|
|
|
|
#endif // ENVIRONMENT64
|
|
|
|
|
|
|
|
const uint64_t FirstActorMask = 1;
|
2021-05-16 13:56:53 +02:00
|
|
|
const int BUCKET_SIZE = GameConstants::MAX_HEROES_PER_PLAYER;
|
|
|
|
const int NUM_CHAINS = BUCKET_COUNT * BUCKET_SIZE;
|
2021-05-16 14:08:39 +02:00
|
|
|
const uint64_t MIN_ARMY_STRENGTH_FOR_CHAIN = 5000;
|
|
|
|
const uint64_t MIN_ARMY_STRENGTH_FOR_NEXT_ACTOR = 1000;
|
|
|
|
|
2021-05-16 13:56:27 +02:00
|
|
|
AISharedStorage::AISharedStorage(int3 sizes)
|
|
|
|
{
|
|
|
|
if(!shared){
|
|
|
|
shared.reset(new boost::multi_array<AIPathNode, 5>(
|
2021-05-16 13:56:53 +02:00
|
|
|
boost::extents[sizes.x][sizes.y][sizes.z][EPathfindingLayer::NUM_LAYERS][NUM_CHAINS]));
|
2021-05-16 13:56:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
nodes = shared;
|
|
|
|
}
|
|
|
|
|
|
|
|
AISharedStorage::~AISharedStorage()
|
|
|
|
{
|
|
|
|
nodes.reset();
|
|
|
|
if(shared && shared.use_count() == 1)
|
|
|
|
{
|
|
|
|
shared.reset();
|
|
|
|
}
|
|
|
|
}
|
2021-05-16 13:45:30 +02:00
|
|
|
|
2020-05-04 17:58:43 +02:00
|
|
|
AINodeStorage::AINodeStorage(const Nullkiller * ai, const int3 & Sizes)
|
2021-05-16 13:56:27 +02:00
|
|
|
: sizes(Sizes), ai(ai), cb(ai->cb.get()), nodes(Sizes)
|
2021-05-15 18:22:44 +02:00
|
|
|
{
|
2020-05-04 17:58:43 +02:00
|
|
|
dangerEvaluator.reset(new FuzzyHelper(ai));
|
2021-05-15 18:22:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 20:57:36 +02:00
|
|
|
const PlayerColor fowPlayer = ai->playerID;
|
|
|
|
const auto & fow = static_cast<const CGameInfoCallback *>(gs)->getPlayerTeam(fowPlayer)->fogOfWarMap;
|
2020-09-02 19:16:24 +02:00
|
|
|
const int3 sizes = gs->getMapSize();
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
parallel_for(blocked_range<size_t>(0, sizes.x), [&](const blocked_range<size_t>& r)
|
2021-05-15 18:22:44 +02:00
|
|
|
{
|
2020-09-02 19:16:24 +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;
|
|
|
|
const PlayerColor player = playerID;
|
|
|
|
|
|
|
|
int3 pos;
|
|
|
|
|
|
|
|
for(pos.x = r.begin(); pos.x != r.end(); ++pos.x)
|
2021-05-15 18:22:44 +02:00
|
|
|
{
|
2020-09-02 19:16:24 +02:00
|
|
|
for(pos.y = 0; pos.y < sizes.y; ++pos.y)
|
2021-05-15 18:22:44 +02:00
|
|
|
{
|
2020-09-02 19:16:24 +02:00
|
|
|
for(pos.z = 0; pos.z < sizes.z; ++pos.z)
|
2021-05-15 18:22:44 +02:00
|
|
|
{
|
2020-09-02 19:16:24 +02:00
|
|
|
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:22:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-02 19:16:24 +02:00
|
|
|
});
|
2021-05-15 18:22:44 +02:00
|
|
|
}
|
|
|
|
|
2021-05-15 18:23:01 +02:00
|
|
|
void AINodeStorage::clear()
|
2021-05-15 18:22:49 +02:00
|
|
|
{
|
|
|
|
actors.clear();
|
2021-05-16 13:15:03 +02:00
|
|
|
heroChainPass = EHeroChainPass::INITIAL;
|
2021-05-16 13:13:56 +02:00
|
|
|
heroChainTurn = 0;
|
|
|
|
heroChainMaxTurns = 1;
|
2021-05-16 14:01:34 +02:00
|
|
|
turnDistanceLimit[HeroRole::MAIN] = 255;
|
|
|
|
turnDistanceLimit[HeroRole::SCOUT] = 255;
|
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
|
|
|
{
|
2021-05-16 13:56:53 +02:00
|
|
|
int bucketIndex = ((uintptr_t)actor) % BUCKET_COUNT;
|
|
|
|
int bucketOffset = bucketIndex * BUCKET_SIZE;
|
|
|
|
auto chains = nodes.get(pos, layer);
|
|
|
|
|
|
|
|
if(chains[0].blocked())
|
|
|
|
{
|
|
|
|
return boost::none;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto i = BUCKET_SIZE - 1; i >= 0; i--)
|
2021-05-15 18:22:44 +02:00
|
|
|
{
|
2021-05-16 13:56:53 +02:00
|
|
|
AIPathNode & node = chains[i + bucketOffset];
|
|
|
|
|
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)
|
2020-09-02 19:16:24 +02:00
|
|
|
{
|
2021-05-16 13:07:54 +02:00
|
|
|
calculateTownPortalTeleportations(heroChain);
|
|
|
|
|
2021-05-15 20:01:48 +02:00
|
|
|
return heroChain;
|
2021-05-16 13:07:54 +02:00
|
|
|
}
|
2021-05-15 20:01:48 +02:00
|
|
|
|
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-16 13:58:56 +02:00
|
|
|
if(!initialNode)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
initialNode->inPQ = false;
|
|
|
|
initialNode->pq = nullptr;
|
2021-05-15 18:22:49 +02:00
|
|
|
initialNode->turns = actor->initialTurn;
|
|
|
|
initialNode->moveRemains = actor->initialMovement;
|
|
|
|
initialNode->danger = 0;
|
2020-09-02 19:16:24 +02:00
|
|
|
initialNode->setCost(actor->initialTurn);
|
2021-05-15 20:04:48 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:07:54 +02:00
|
|
|
calculateTownPortalTeleportations(initialNodes);
|
|
|
|
|
2021-05-15 18:22:49 +02:00
|
|
|
return initialNodes;
|
2021-05-15 18:22:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AINodeStorage::resetTile(const int3 & coord, EPathfindingLayer layer, CGPathNode::EAccessibility accessibility)
|
|
|
|
{
|
2021-05-16 13:56:27 +02:00
|
|
|
for(AIPathNode & heroNode : nodes.get(coord, layer))
|
2020-09-02 19:16:24 +02:00
|
|
|
{
|
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: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;
|
2020-09-02 19:16:24 +02:00
|
|
|
destination->setCost(cost);
|
2021-05-15 20:01:48 +02:00
|
|
|
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-16 13:09:49 +02:00
|
|
|
|
2021-05-16 13:39:47 +02:00
|
|
|
#if PATHFINDER_TRACE_LEVEL >= 2
|
2021-05-16 13:09:49 +02:00
|
|
|
logAi->trace(
|
|
|
|
"Commited %s -> %s, cost: %f, turn: %s, mp: %d, hero: %s, mask: %x, army: %lld",
|
|
|
|
source->coord.toString(),
|
|
|
|
destination->coord.toString(),
|
2021-05-16 13:58:56 +02:00
|
|
|
destination->getCost(),
|
2021-05-16 13:09:49 +02:00
|
|
|
std::to_string(destination->turns),
|
|
|
|
destination->moveRemains,
|
|
|
|
destination->actor->toString(),
|
|
|
|
destination->actor->chainMask,
|
|
|
|
destination->actor->armyValue);
|
|
|
|
#endif
|
2021-05-16 13:56:53 +02:00
|
|
|
|
|
|
|
if(destination->turns <= heroChainTurn)
|
|
|
|
{
|
|
|
|
commitedTiles.insert(destination->coord);
|
|
|
|
}
|
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-16 13:56:53 +02:00
|
|
|
EPathfindingLayer phisycalLayers[2] = {EPathfindingLayer::LAND, EPathfindingLayer::SAIL};
|
|
|
|
|
2021-05-16 13:13:56 +02:00
|
|
|
bool AINodeStorage::increaseHeroChainTurnLimit()
|
|
|
|
{
|
|
|
|
if(heroChainTurn >= heroChainMaxTurns)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
heroChainTurn++;
|
2021-05-16 13:56:53 +02:00
|
|
|
commitedTiles.clear();
|
|
|
|
|
|
|
|
for(auto layer : phisycalLayers)
|
|
|
|
{
|
|
|
|
foreach_tile_pos([&](const int3 & pos)
|
|
|
|
{
|
|
|
|
auto chains = nodes.get(pos, layer);
|
|
|
|
|
|
|
|
if(!chains[0].blocked())
|
|
|
|
{
|
|
|
|
for(AIPathNode & node : chains)
|
|
|
|
{
|
|
|
|
if(node.turns <= heroChainTurn && node.action != CGPathNode::ENodeAction::UNKNOWN)
|
|
|
|
{
|
|
|
|
commitedTiles.insert(pos);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-05-16 13:13:56 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:15:03 +02:00
|
|
|
bool AINodeStorage::calculateHeroChainFinal()
|
|
|
|
{
|
|
|
|
heroChainPass = EHeroChainPass::FINAL;
|
|
|
|
heroChain.resize(0);
|
|
|
|
|
|
|
|
for(auto layer : phisycalLayers)
|
|
|
|
{
|
|
|
|
foreach_tile_pos([&](const int3 & pos)
|
|
|
|
{
|
2021-05-16 13:56:27 +02:00
|
|
|
auto chains = nodes.get(pos, layer);
|
2021-05-16 13:15:03 +02:00
|
|
|
|
2021-05-16 13:56:53 +02:00
|
|
|
if(!chains[0].blocked())
|
2021-05-16 13:15:03 +02:00
|
|
|
{
|
2021-05-16 13:56:53 +02:00
|
|
|
for(AIPathNode & node : chains)
|
2021-05-16 13:15:03 +02:00
|
|
|
{
|
2021-05-16 13:56:53 +02:00
|
|
|
if(node.turns > heroChainTurn
|
|
|
|
&& !node.locked
|
|
|
|
&& node.action != CGPathNode::ENodeAction::UNKNOWN
|
|
|
|
&& node.actor->actorExchangeCount > 1
|
|
|
|
&& !hasBetterChain(&node, &node, chains))
|
|
|
|
{
|
|
|
|
heroChain.push_back(&node);
|
|
|
|
}
|
2021-05-16 13:15:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-09-02 19:16:24 +02:00
|
|
|
|
2021-05-16 13:15:03 +02:00
|
|
|
return heroChain.size();
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
struct DelayedWork
|
2021-05-15 20:01:48 +02:00
|
|
|
{
|
2020-09-02 19:16:24 +02:00
|
|
|
AIPathNode * carrier;
|
|
|
|
AIPathNode * other;
|
|
|
|
|
|
|
|
DelayedWork()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DelayedWork(AIPathNode * carrier, AIPathNode * other) : carrier(carrier), other(other)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
2021-05-15 20:01:48 +02:00
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
class HeroChainCalculationTask
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
AISharedStorage & nodes;
|
|
|
|
AINodeStorage & storage;
|
2021-05-15 20:54:58 +02:00
|
|
|
std::vector<AIPathNode *> existingChains;
|
|
|
|
std::vector<ExchangeCandidate> newChains;
|
2020-09-02 19:16:24 +02:00
|
|
|
uint64_t chainMask;
|
|
|
|
int heroChainTurn;
|
|
|
|
std::vector<CGPathNode *> heroChain;
|
|
|
|
const std::vector<int3> & tiles;
|
2021-05-16 14:08:49 +02:00
|
|
|
std::vector<DelayedWork> delayedWork;
|
2020-09-02 19:16:24 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
HeroChainCalculationTask(
|
|
|
|
AINodeStorage & storage, AISharedStorage & nodes, const std::vector<int3> & tiles, uint64_t chainMask, int heroChainTurn)
|
2021-05-16 14:08:49 +02:00
|
|
|
:existingChains(), newChains(), delayedWork(), nodes(nodes), storage(storage), chainMask(chainMask), heroChainTurn(heroChainTurn), heroChain(), tiles(tiles)
|
2020-09-02 19:16:24 +02:00
|
|
|
{
|
|
|
|
existingChains.reserve(NUM_CHAINS);
|
|
|
|
newChains.reserve(NUM_CHAINS);
|
|
|
|
}
|
2021-05-15 20:04:48 +02:00
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
void execute(const blocked_range<size_t>& r)
|
2021-05-16 13:15:03 +02:00
|
|
|
{
|
2020-09-02 19:16:24 +02:00
|
|
|
for(int i = r.begin(); i != r.end(); i++)
|
2021-05-16 13:15:03 +02:00
|
|
|
{
|
2020-09-02 19:16:24 +02:00
|
|
|
auto & pos = tiles[i];
|
2021-05-15 20:01:48 +02:00
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
for(auto layer : phisycalLayers)
|
|
|
|
{
|
|
|
|
auto chains = nodes.get(pos, layer);
|
2021-05-16 13:56:53 +02:00
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
// fast cut inactive nodes
|
|
|
|
if(chains[0].blocked())
|
|
|
|
continue;
|
2021-05-15 20:04:48 +02:00
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
existingChains.clear();
|
|
|
|
newChains.clear();
|
2021-05-15 20:04:48 +02:00
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
for(AIPathNode & node : chains)
|
2021-05-16 13:15:03 +02:00
|
|
|
{
|
2020-09-02 19:16:24 +02:00
|
|
|
if(node.turns <= heroChainTurn && node.action != CGPathNode::ENodeAction::UNKNOWN)
|
|
|
|
existingChains.push_back(&node);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::random_shuffle(existingChains.begin(), existingChains.end());
|
|
|
|
|
|
|
|
for(AIPathNode * node : existingChains)
|
|
|
|
{
|
|
|
|
if(node->actor->isMovable)
|
|
|
|
{
|
|
|
|
calculateHeroChain(node, existingChains, newChains);
|
|
|
|
}
|
2021-05-16 13:15:03 +02:00
|
|
|
}
|
2021-05-15 20:54:58 +02:00
|
|
|
|
2021-05-16 14:08:49 +02:00
|
|
|
for(auto delayed = delayedWork.begin(); delayed != delayedWork.end();)
|
|
|
|
{
|
|
|
|
auto newActor = delayed->carrier->actor->tryExchangeNoLock(delayed->other->actor);
|
|
|
|
|
|
|
|
if(!newActor.lockAcquired) continue;
|
|
|
|
|
|
|
|
if(newActor.actor)
|
|
|
|
{
|
|
|
|
newChains.push_back(calculateExchange(newActor.actor, delayed->carrier, delayed->other));
|
|
|
|
}
|
|
|
|
|
|
|
|
delayed++;
|
|
|
|
}
|
|
|
|
|
|
|
|
delayedWork.clear();
|
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
cleanupInefectiveChains(newChains);
|
|
|
|
addHeroChain(newChains);
|
|
|
|
}
|
2021-05-16 13:56:53 +02:00
|
|
|
}
|
2021-05-16 13:15:03 +02:00
|
|
|
}
|
2021-05-15 20:01:48 +02:00
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
void calculateHeroChain(
|
|
|
|
AIPathNode * srcNode,
|
|
|
|
const std::vector<AIPathNode *> & variants,
|
|
|
|
std::vector<ExchangeCandidate> & result);
|
|
|
|
|
|
|
|
void calculateHeroChain(
|
|
|
|
AIPathNode * carrier,
|
|
|
|
AIPathNode * other,
|
|
|
|
std::vector<ExchangeCandidate> & result);
|
|
|
|
|
|
|
|
void cleanupInefectiveChains(std::vector<ExchangeCandidate> & result) const;
|
|
|
|
void addHeroChain(const std::vector<ExchangeCandidate> & result);
|
|
|
|
|
|
|
|
ExchangeCandidate calculateExchange(
|
|
|
|
ChainActor * exchangeActor,
|
|
|
|
AIPathNode * carrierParentNode,
|
|
|
|
AIPathNode * otherParentNode) const;
|
|
|
|
|
|
|
|
void flushResult(std::vector<CGPathNode *> & result)
|
|
|
|
{
|
|
|
|
vstd::concatenate(result, heroChain);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool AINodeStorage::calculateHeroChain()
|
|
|
|
{
|
|
|
|
heroChainPass = EHeroChainPass::CHAIN;
|
|
|
|
heroChain.clear();
|
|
|
|
|
|
|
|
std::vector<int3> data(commitedTiles.begin(), commitedTiles.end());
|
|
|
|
|
2021-05-16 14:08:49 +02:00
|
|
|
if(data.size() > 100)
|
|
|
|
{
|
2021-05-16 14:10:35 +02:00
|
|
|
boost::mutex resultMutex;
|
2021-05-16 14:08:49 +02:00
|
|
|
|
2021-05-16 14:08:56 +02:00
|
|
|
std::random_shuffle(data.begin(), data.end());
|
2021-05-16 14:08:49 +02:00
|
|
|
|
|
|
|
parallel_for(blocked_range<size_t>(0, data.size()), [&](const blocked_range<size_t>& r)
|
|
|
|
{
|
|
|
|
//auto r = blocked_range<size_t>(0, data.size());
|
|
|
|
HeroChainCalculationTask task(*this, nodes, data, chainMask, heroChainTurn);
|
|
|
|
|
|
|
|
task.execute(r);
|
|
|
|
|
|
|
|
{
|
2021-05-16 14:10:35 +02:00
|
|
|
boost::lock_guard<boost::mutex> resultLock(resultMutex);
|
2021-05-16 14:08:49 +02:00
|
|
|
|
|
|
|
task.flushResult(heroChain);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto r = blocked_range<size_t>(0, data.size());
|
|
|
|
HeroChainCalculationTask task(*this, nodes, data, chainMask, heroChainTurn);
|
2020-09-02 19:16:24 +02:00
|
|
|
|
2021-05-16 14:08:49 +02:00
|
|
|
task.execute(r);
|
2021-05-16 14:08:56 +02:00
|
|
|
task.flushResult(heroChain);
|
2021-05-16 14:08:49 +02:00
|
|
|
}
|
2020-09-02 19:16:24 +02:00
|
|
|
|
2021-05-16 13:56:53 +02:00
|
|
|
commitedTiles.clear();
|
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
return !heroChain.empty();
|
2021-05-15 20:01:48 +02:00
|
|
|
}
|
|
|
|
|
2021-05-16 13:15:12 +02:00
|
|
|
bool AINodeStorage::selectFirstActor()
|
|
|
|
{
|
2020-09-02 19:16:24 +02:00
|
|
|
if(actors.empty())
|
2021-05-16 13:15:12 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
auto strongest = *vstd::maxElementByFun(actors, [](std::shared_ptr<ChainActor> actor) -> uint64_t
|
|
|
|
{
|
|
|
|
return actor->armyValue;
|
|
|
|
});
|
|
|
|
|
|
|
|
chainMask = strongest->chainMask;
|
2021-05-16 13:56:53 +02:00
|
|
|
commitedTilesInitial = commitedTiles;
|
2021-05-16 13:15:12 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AINodeStorage::selectNextActor()
|
|
|
|
{
|
|
|
|
auto currentActor = std::find_if(actors.begin(), actors.end(), [&](std::shared_ptr<ChainActor> actor)-> bool
|
|
|
|
{
|
|
|
|
return actor->chainMask == chainMask;
|
|
|
|
});
|
|
|
|
|
|
|
|
auto nextActor = actors.end();
|
|
|
|
|
|
|
|
for(auto actor = actors.begin(); actor != actors.end(); actor++)
|
|
|
|
{
|
|
|
|
if(actor->get()->armyValue > currentActor->get()->armyValue
|
|
|
|
|| actor->get()->armyValue == currentActor->get()->armyValue && actor <= currentActor)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(nextActor == actors.end()
|
|
|
|
|| actor->get()->armyValue > nextActor->get()->armyValue)
|
|
|
|
{
|
|
|
|
nextActor = actor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(nextActor != actors.end())
|
|
|
|
{
|
2021-05-16 14:08:39 +02:00
|
|
|
if(nextActor->get()->armyValue < MIN_ARMY_STRENGTH_FOR_NEXT_ACTOR)
|
2020-09-02 19:16:24 +02:00
|
|
|
return false;
|
|
|
|
|
2021-05-16 13:15:12 +02:00
|
|
|
chainMask = nextActor->get()->chainMask;
|
2021-05-16 13:56:53 +02:00
|
|
|
commitedTiles = commitedTilesInitial;
|
2021-05-16 13:15:12 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
void HeroChainCalculationTask::cleanupInefectiveChains(std::vector<ExchangeCandidate> & result) const
|
2021-05-15 20:54:58 +02:00
|
|
|
{
|
2021-05-16 13:15:03 +02:00
|
|
|
vstd::erase_if(result, [&](const ExchangeCandidate & chainInfo) -> bool
|
2021-05-15 20:54:58 +02:00
|
|
|
{
|
|
|
|
auto pos = chainInfo.coord;
|
2021-05-16 13:56:27 +02:00
|
|
|
auto chains = nodes.get(pos, EPathfindingLayer::LAND);
|
2020-09-02 19:16:24 +02:00
|
|
|
auto isNotEffective = storage.hasBetterChain(chainInfo.carrierParent, &chainInfo, chains)
|
|
|
|
|| storage.hasBetterChain(chainInfo.carrierParent, &chainInfo, result);
|
2021-05-15 20:54:58 +02:00
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
#if PATHFINDER_TRACE_LEVEL >= 2
|
|
|
|
if(isNotEffective)
|
|
|
|
{
|
|
|
|
logAi->trace(
|
|
|
|
"Skip exchange %s[%x] -> %s[%x] at %s is ineficient",
|
|
|
|
chainInfo.otherParent->actor->toString(),
|
|
|
|
chainInfo.otherParent->actor->chainMask,
|
|
|
|
chainInfo.carrierParent->actor->toString(),
|
|
|
|
chainInfo.carrierParent->actor->chainMask,
|
|
|
|
chainInfo.carrierParent->coord.toString());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return isNotEffective;
|
2021-05-15 20:54:58 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
void HeroChainCalculationTask::calculateHeroChain(
|
2021-05-15 20:54:58 +02:00
|
|
|
AIPathNode * srcNode,
|
|
|
|
const std::vector<AIPathNode *> & variants,
|
2020-09-02 19:16:24 +02:00
|
|
|
std::vector<ExchangeCandidate> & result)
|
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
|
|
|
{
|
2021-05-16 13:15:03 +02:00
|
|
|
if(node == srcNode || !node->actor)
|
|
|
|
continue;
|
|
|
|
|
2021-05-16 13:15:12 +02:00
|
|
|
if((node->actor->chainMask & chainMask) == 0 && (srcNode->actor->chainMask & chainMask) == 0)
|
|
|
|
continue;
|
|
|
|
|
2021-05-16 13:19:27 +02:00
|
|
|
if(node->action == CGPathNode::ENodeAction::BATTLE
|
2021-05-16 13:19:44 +02:00
|
|
|
|| node->action == CGPathNode::ENodeAction::TELEPORT_BATTLE
|
|
|
|
|| node->action == CGPathNode::ENodeAction::TELEPORT_NORMAL
|
|
|
|
|| node->action == CGPathNode::ENodeAction::TELEPORT_BLOCKING_VISIT)
|
2021-05-16 13:19:27 +02:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:15:03 +02:00
|
|
|
if(node->turns > heroChainTurn
|
2021-05-15 21:04:31 +02:00
|
|
|
|| (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
|
|
|
{
|
2021-05-16 13:39:47 +02:00
|
|
|
#if PATHFINDER_TRACE_LEVEL >= 2
|
2021-05-16 13:15:03 +02:00
|
|
|
logAi->trace(
|
|
|
|
"Skip exchange %s[%x] -> %s[%x] at %s because of %s",
|
|
|
|
node->actor->toString(),
|
|
|
|
node->actor->chainMask,
|
|
|
|
srcNode->actor->toString(),
|
|
|
|
srcNode->actor->chainMask,
|
|
|
|
srcNode->coord.toString(),
|
|
|
|
(node->turns > heroChainTurn
|
|
|
|
? "turn limit"
|
|
|
|
: (node->action == CGPathNode::ENodeAction::UNKNOWN && node->actor->hero)
|
|
|
|
? "action unknown"
|
|
|
|
: "chain mask"));
|
|
|
|
#endif
|
2021-05-15 19:59:43 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:39:47 +02:00
|
|
|
#if PATHFINDER_TRACE_LEVEL >= 2
|
2021-05-15 20:04:48 +02:00
|
|
|
logAi->trace(
|
2021-05-16 13:09:49 +02:00
|
|
|
"Thy exchange %s[%x] -> %s[%x] 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
void HeroChainCalculationTask::calculateHeroChain(
|
2021-05-15 20:54:58 +02:00
|
|
|
AIPathNode * carrier,
|
|
|
|
AIPathNode * other,
|
2020-09-02 19:16:24 +02:00
|
|
|
std::vector<ExchangeCandidate> & result)
|
2021-05-15 20:56:08 +02:00
|
|
|
{
|
2021-05-15 21:02:27 +02:00
|
|
|
if(carrier->armyLoss < carrier->actor->armyValue
|
2021-05-15 21:04:31 +02:00
|
|
|
&& (carrier->action != CGPathNode::BATTLE || (carrier->actor->allowBattle && carrier->specialAction))
|
2021-05-16 12:52:30 +02:00
|
|
|
&& carrier->action != CGPathNode::BLOCKING_VISIT
|
2020-09-02 19:16:24 +02:00
|
|
|
&& (other->armyLoss == 0 || other->armyLoss < other->actor->armyValue))
|
2021-05-15 19:59:43 +02:00
|
|
|
{
|
2021-05-16 13:39:47 +02:00
|
|
|
#if PATHFINDER_TRACE_LEVEL >= 2
|
2021-05-15 20:04:48 +02:00
|
|
|
logAi->trace(
|
2021-05-16 13:09:49 +02:00
|
|
|
"Exchange allowed %s[%x] -> %s[%x] 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
|
|
|
|
|
2021-05-15 20:57:27 +02:00
|
|
|
if(other->actor->isMovable)
|
2021-05-15 20:04:48 +02:00
|
|
|
{
|
2021-05-16 13:22:37 +02:00
|
|
|
bool hasLessMp = carrier->turns > other->turns || (carrier->turns == other->turns && carrier->moveRemains < other->moveRemains);
|
2021-05-15 20:57:27 +02:00
|
|
|
bool hasLessExperience = carrier->actor->hero->exp < other->actor->hero->exp;
|
|
|
|
|
|
|
|
if(hasLessMp && hasLessExperience)
|
|
|
|
{
|
2021-05-16 13:39:47 +02:00
|
|
|
#if PATHFINDER_TRACE_LEVEL >= 2
|
2021-05-15 20:57:27 +02:00
|
|
|
logAi->trace("Exchange at %s is ineficient. Blocked.", carrier->coord.toString());
|
2021-05-15 20:04:48 +02:00
|
|
|
#endif
|
2021-05-15 20:57:27 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-05-15 20:04:48 +02:00
|
|
|
}
|
2021-05-15 19:59:43 +02:00
|
|
|
|
2021-05-16 14:08:49 +02:00
|
|
|
auto newActor = carrier->actor->tryExchangeNoLock(other->actor);
|
2021-05-15 20:54:58 +02:00
|
|
|
|
2021-05-16 14:08:49 +02:00
|
|
|
if(!newActor.lockAcquired) delayedWork.push_back(DelayedWork(carrier, other));
|
|
|
|
if(newActor.actor) result.push_back(calculateExchange(newActor.actor, carrier, other));
|
2021-05-15 20:54:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
void HeroChainCalculationTask::addHeroChain(const std::vector<ExchangeCandidate> & result)
|
2021-05-15 20:54:58 +02:00
|
|
|
{
|
|
|
|
for(const ExchangeCandidate & chainInfo : result)
|
|
|
|
{
|
|
|
|
auto carrier = chainInfo.carrierParent;
|
|
|
|
auto newActor = chainInfo.actor;
|
|
|
|
auto other = chainInfo.otherParent;
|
2020-09-02 19:16:24 +02:00
|
|
|
auto chainNodeOptional = storage.getOrCreateNode(carrier->coord, carrier->layer, newActor);
|
2021-05-15 19:59:43 +02:00
|
|
|
|
|
|
|
if(!chainNodeOptional)
|
2021-05-15 20:04:48 +02:00
|
|
|
{
|
2021-05-16 13:39:47 +02:00
|
|
|
#if PATHFINDER_TRACE_LEVEL >= 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
|
|
|
{
|
2021-05-16 13:39:47 +02:00
|
|
|
#if PATHFINDER_TRACE_LEVEL >= 2
|
2020-09-02 19:16:24 +02:00
|
|
|
logAi->trace(
|
|
|
|
"Skip exchange %s[%x] -> %s[%x] at %s because node is in use",
|
|
|
|
other->actor->toString(),
|
|
|
|
other->actor->chainMask,
|
|
|
|
carrier->actor->toString(),
|
|
|
|
carrier->actor->chainMask,
|
|
|
|
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
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
if(exchangeNode->turns != 0xFF && exchangeNode->getCost() < chainInfo.getCost())
|
2021-05-15 20:04:48 +02:00
|
|
|
{
|
2021-05-16 13:39:47 +02:00
|
|
|
#if PATHFINDER_TRACE_LEVEL >= 2
|
2021-05-15 20:04:48 +02:00
|
|
|
logAi->trace(
|
2020-09-02 19:16:24 +02:00
|
|
|
"Skip exchange %s[%x] -> %s[%x] at %s because not effective enough. %f < %f",
|
|
|
|
other->actor->toString(),
|
|
|
|
other->actor->chainMask,
|
|
|
|
carrier->actor->toString(),
|
|
|
|
carrier->actor->chainMask,
|
|
|
|
carrier->coord.toString(),
|
|
|
|
exchangeNode->getCost(),
|
2021-05-16 13:58:56 +02:00
|
|
|
chainInfo.getCost());
|
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
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
storage.commit(exchangeNode, carrier, carrier->action, chainInfo.turns, chainInfo.moveRemains, chainInfo.getCost());
|
2021-05-15 20:54:58 +02:00
|
|
|
|
2021-05-16 13:07:54 +02:00
|
|
|
if(carrier->specialAction || carrier->chainOther)
|
|
|
|
{
|
|
|
|
// there is some action on source tile which should be performed before we can bypass it
|
|
|
|
exchangeNode->theNodeBefore = carrier;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:57:26 +02:00
|
|
|
if(exchangeNode->actor->actorAction)
|
|
|
|
{
|
|
|
|
exchangeNode->theNodeBefore = carrier;
|
|
|
|
exchangeNode->specialAction = exchangeNode->actor->actorAction;
|
|
|
|
}
|
|
|
|
|
2021-05-15 20:54:58 +02:00
|
|
|
exchangeNode->chainOther = other;
|
|
|
|
exchangeNode->armyLoss = chainInfo.armyLoss;
|
|
|
|
|
2021-05-16 13:39:47 +02:00
|
|
|
#if PATHFINDER_TRACE_LEVEL >= 2
|
2021-05-15 20:54:58 +02:00
|
|
|
logAi->trace(
|
2021-05-16 13:09:49 +02:00
|
|
|
"Chain accepted at %s %s -> %s, mask %x, cost %f, turn: %s, mp: %d, army %i",
|
2021-05-15 20:54:58 +02:00
|
|
|
exchangeNode->coord.toString(),
|
|
|
|
other->actor->toString(),
|
|
|
|
exchangeNode->actor->toString(),
|
|
|
|
exchangeNode->actor->chainMask,
|
2021-05-16 13:58:56 +02:00
|
|
|
exchangeNode->getCost(),
|
2021-05-16 13:09:49 +02:00
|
|
|
std::to_string(exchangeNode->turns),
|
|
|
|
exchangeNode->moveRemains,
|
2021-05-15 20:56:08 +02:00
|
|
|
exchangeNode->actor->armyValue);
|
2021-05-15 20:54:58 +02:00
|
|
|
#endif
|
|
|
|
heroChain.push_back(exchangeNode);
|
2021-05-15 20:01:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
ExchangeCandidate HeroChainCalculationTask::calculateExchange(
|
2021-05-15 20:54:58 +02:00
|
|
|
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 21:04:31 +02:00
|
|
|
|
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;
|
2020-09-02 19:16:24 +02:00
|
|
|
candidate.setCost(carrierParentNode->getCost() + otherParentNode->getCost() / 1000.0);
|
2021-05-15 20:54:58 +02:00
|
|
|
candidate.moveRemains = carrierParentNode->moveRemains;
|
2021-05-15 20:01:48 +02:00
|
|
|
|
|
|
|
if(carrierParentNode->turns < otherParentNode->turns)
|
|
|
|
{
|
2022-05-15 10:39:06 +02:00
|
|
|
int moveRemains = exchangeActor->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;
|
2020-09-02 19:16:24 +02:00
|
|
|
candidate.setCost(candidate.getCost() + waitingCost);
|
2021-05-15 20:54:58 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:56:13 +02:00
|
|
|
bool AINodeStorage::isDistanceLimitReached(const PathNodeInfo & source, CDestinationNodeInfo & destination) const
|
|
|
|
{
|
|
|
|
if(heroChainPass == EHeroChainPass::CHAIN && destination.node->turns > heroChainTurn)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto aiNode = getAINode(destination.node);
|
|
|
|
|
2021-05-16 14:01:34 +02:00
|
|
|
if(heroChainPass != EHeroChainPass::CHAIN
|
|
|
|
&& destination.node->turns > turnDistanceLimit[aiNode->actor->heroRole])
|
2021-05-16 13:56:13 +02:00
|
|
|
{
|
2021-05-16 14:01:34 +02:00
|
|
|
return true;
|
2021-05-16 13:56:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AINodeStorage::setHeroes(std::map<const CGHeroInstance *, HeroRole> heroes)
|
2021-05-15 18:22:44 +02:00
|
|
|
{
|
2021-05-15 20:57:36 +02:00
|
|
|
playerID = ai->playerID;
|
|
|
|
|
2021-05-15 18:22:49 +02:00
|
|
|
for(auto & hero : heroes)
|
|
|
|
{
|
2021-05-16 13:56:53 +02:00
|
|
|
uint64_t mask = FirstActorMask << actors.size();
|
2021-05-16 13:56:13 +02:00
|
|
|
auto actor = std::make_shared<HeroActor>(hero.first, hero.second, mask, ai);
|
2021-05-16 12:53:32 +02:00
|
|
|
|
2021-05-16 13:56:13 +02:00
|
|
|
if(actor->hero->tempOwner != ai->playerID)
|
2021-05-16 12:53:32 +02:00
|
|
|
{
|
|
|
|
bool onLand = !actor->hero->boat;
|
|
|
|
actor->initialMovement = actor->hero->maxMovePoints(onLand);
|
|
|
|
}
|
2021-05-15 18:22:49 +02:00
|
|
|
|
2021-05-16 13:56:13 +02:00
|
|
|
playerID = actor->hero->tempOwner;
|
2021-05-16 12:53:32 +02:00
|
|
|
|
|
|
|
actors.push_back(actor);
|
2019-03-17 23:27:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AINodeStorage::setTownsAndDwellings(
|
|
|
|
const std::vector<const CGTownInstance *> & towns,
|
2021-05-15 20:54:28 +02:00
|
|
|
const std::set<const CGObjectInstance *> & visitableObjs)
|
2019-03-17 23:27:09 +02:00
|
|
|
{
|
|
|
|
for(auto town : towns)
|
|
|
|
{
|
2021-05-16 13:56:53 +02:00
|
|
|
uint64_t mask = FirstActorMask << actors.size();
|
2019-03-17 23:27:09 +02:00
|
|
|
|
2021-05-16 13:19:27 +02:00
|
|
|
// TODO: investigate logix of second condition || ai->nullkiller->getHeroLockedReason(town->garrisonHero) != HeroLockedReason::DEFENCE
|
|
|
|
// check defence imrove
|
|
|
|
if(!town->garrisonHero)
|
2019-03-17 23:27:09 +02:00
|
|
|
{
|
|
|
|
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);
|
2021-05-16 13:19:00 +02:00
|
|
|
auto waitForGrowth = dayOfWeek > 4;*/
|
2019-03-17 23:27:09 +02:00
|
|
|
|
|
|
|
for(auto obj: visitableObjs)
|
|
|
|
{
|
2021-05-16 13:19:00 +02:00
|
|
|
if(obj->ID == Obj::HILL_FORT)
|
|
|
|
{
|
2021-05-16 13:56:53 +02:00
|
|
|
uint64_t mask = FirstActorMask << actors.size();
|
2021-05-16 13:19:00 +02:00
|
|
|
|
|
|
|
actors.push_back(std::make_shared<HillFortActor>(obj, mask));
|
|
|
|
}
|
|
|
|
/*const CGDwelling * dwelling = dynamic_cast<const CGDwelling *>(obj);
|
2019-03-17 23:27:09 +02:00
|
|
|
|
|
|
|
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-16 13:19:00 +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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return neighbours;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:09:49 +02:00
|
|
|
struct TowmPortalFinder
|
2021-05-16 13:07:54 +02:00
|
|
|
{
|
2021-05-16 13:09:49 +02:00
|
|
|
const std::vector<CGPathNode *> & initialNodes;
|
|
|
|
SecSkillLevel::SecSkillLevel townPortalSkillLevel;
|
|
|
|
uint64_t movementNeeded;
|
|
|
|
const ChainActor * actor;
|
|
|
|
const CGHeroInstance * hero;
|
|
|
|
std::vector<const CGTownInstance *> targetTowns;
|
|
|
|
AINodeStorage * nodeStorage;
|
2021-05-16 13:07:54 +02:00
|
|
|
|
2021-05-16 13:09:49 +02:00
|
|
|
SpellID spellID;
|
|
|
|
const CSpell * townPortal;
|
|
|
|
|
|
|
|
TowmPortalFinder(
|
|
|
|
const ChainActor * actor,
|
|
|
|
const std::vector<CGPathNode *> & initialNodes,
|
|
|
|
std::vector<const CGTownInstance *> targetTowns,
|
|
|
|
AINodeStorage * nodeStorage)
|
|
|
|
:actor(actor), initialNodes(initialNodes), hero(actor->hero),
|
|
|
|
targetTowns(targetTowns), nodeStorage(nodeStorage)
|
|
|
|
{
|
|
|
|
spellID = SpellID::TOWN_PORTAL;
|
|
|
|
townPortal = spellID.toSpell();
|
|
|
|
|
|
|
|
// TODO: Copy/Paste from TownPortalMechanics
|
|
|
|
townPortalSkillLevel = SecSkillLevel::SecSkillLevel(hero->getSpellSchoolLevel(townPortal));
|
|
|
|
movementNeeded = GameConstants::BASE_MOVEMENT_COST * (townPortalSkillLevel >= SecSkillLevel::EXPERT ? 2 : 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool actorCanCastTownPortal()
|
|
|
|
{
|
|
|
|
return hero->canCastThisSpell(townPortal) && hero->mana >= hero->getSpellCost(townPortal);
|
|
|
|
}
|
|
|
|
|
|
|
|
CGPathNode * getBestInitialNodeForTownPortal(const CGTownInstance * targetTown)
|
|
|
|
{
|
|
|
|
CGPathNode * bestNode = nullptr;
|
|
|
|
|
|
|
|
for(CGPathNode * node : initialNodes)
|
|
|
|
{
|
|
|
|
auto aiNode = nodeStorage->getAINode(node);
|
|
|
|
|
|
|
|
if(aiNode->actor->baseActor != actor
|
|
|
|
|| node->layer != EPathfindingLayer::LAND
|
|
|
|
|| node->moveRemains < movementNeeded)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(townPortalSkillLevel < SecSkillLevel::ADVANCED)
|
|
|
|
{
|
|
|
|
const CGTownInstance * nearestTown = *vstd::minElementByFun(targetTowns, [&](const CGTownInstance * t) -> int
|
|
|
|
{
|
|
|
|
return node->coord.dist2dSQ(t->visitablePos());
|
|
|
|
});
|
|
|
|
|
|
|
|
if(targetTown != nearestTown)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
if(!bestNode || bestNode->getCost() > node->getCost())
|
2021-05-16 13:09:49 +02:00
|
|
|
bestNode = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bestNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<AIPathNode *> createTownPortalNode(const CGTownInstance * targetTown)
|
|
|
|
{
|
|
|
|
auto bestNode = getBestInitialNodeForTownPortal(targetTown);
|
|
|
|
|
|
|
|
if(!bestNode)
|
|
|
|
return boost::none;
|
|
|
|
|
|
|
|
auto nodeOptional = nodeStorage->getOrCreateNode(targetTown->visitablePos(), EPathfindingLayer::LAND, actor->castActor);
|
|
|
|
|
|
|
|
if(!nodeOptional)
|
|
|
|
return boost::none;
|
|
|
|
|
|
|
|
AIPathNode * node = nodeOptional.get();
|
|
|
|
float movementCost = (float)movementNeeded / (float)hero->maxMovePoints(EPathfindingLayer::LAND);
|
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
movementCost += bestNode->getCost();
|
2021-05-16 13:09:49 +02:00
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
if(node->action == CGPathNode::UNKNOWN || node->getCost() > movementCost)
|
2021-05-16 13:09:49 +02:00
|
|
|
{
|
|
|
|
nodeStorage->commit(
|
|
|
|
node,
|
|
|
|
nodeStorage->getAINode(bestNode),
|
|
|
|
CGPathNode::TELEPORT_NORMAL,
|
|
|
|
bestNode->turns,
|
|
|
|
bestNode->moveRemains - movementNeeded,
|
|
|
|
movementCost);
|
|
|
|
|
|
|
|
node->theNodeBefore = bestNode;
|
|
|
|
node->specialAction.reset(new AIPathfinding::TownPortalAction(targetTown));
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeOptional;
|
|
|
|
}
|
|
|
|
};
|
2021-05-16 13:07:54 +02:00
|
|
|
|
|
|
|
void AINodeStorage::calculateTownPortalTeleportations(std::vector<CGPathNode *> & initialNodes)
|
2021-05-15 18:22:44 +02:00
|
|
|
{
|
2021-05-16 13:07:54 +02:00
|
|
|
std::set<const ChainActor *> actorsOfInitial;
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2021-05-16 13:07:54 +02:00
|
|
|
for(const CGPathNode * node : initialNodes)
|
|
|
|
{
|
|
|
|
auto aiNode = getAINode(node);
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2021-05-16 13:07:54 +02:00
|
|
|
actorsOfInitial.insert(aiNode->actor->baseActor);
|
|
|
|
}
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2021-05-16 13:45:30 +02:00
|
|
|
std::map<const CGHeroInstance *, int> maskMap;
|
|
|
|
|
|
|
|
for(std::shared_ptr<ChainActor> basicActor : actors)
|
|
|
|
{
|
|
|
|
if(basicActor->hero)
|
|
|
|
maskMap[basicActor->hero] = basicActor->chainMask;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:07:54 +02:00
|
|
|
for(const ChainActor * actor : actorsOfInitial)
|
|
|
|
{
|
|
|
|
if(!actor->hero)
|
|
|
|
continue;
|
2021-05-15 21:02:57 +02:00
|
|
|
|
2021-05-16 13:09:49 +02:00
|
|
|
auto towns = cb->getTownsInfo(false);
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2021-05-16 13:09:49 +02:00
|
|
|
vstd::erase_if(towns, [&](const CGTownInstance * t) -> bool
|
2021-05-15 18:22:44 +02:00
|
|
|
{
|
2021-05-16 13:09:49 +02:00
|
|
|
return cb->getPlayerRelations(actor->hero->tempOwner, t->tempOwner) == PlayerRelations::ENEMIES;
|
|
|
|
});
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2021-05-16 13:09:49 +02:00
|
|
|
if(!towns.size())
|
|
|
|
{
|
|
|
|
return; // no towns no need to run loop further
|
|
|
|
}
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2021-05-16 13:09:49 +02:00
|
|
|
TowmPortalFinder townPortalFinder(actor, initialNodes, towns, this);
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2021-05-16 13:09:49 +02:00
|
|
|
if(townPortalFinder.actorCanCastTownPortal())
|
|
|
|
{
|
2021-05-16 13:07:54 +02:00
|
|
|
for(const CGTownInstance * targetTown : towns)
|
2021-05-15 18:22:44 +02:00
|
|
|
{
|
2021-05-16 13:07:54 +02:00
|
|
|
// TODO: allow to hide visiting hero in garrison
|
2021-05-16 13:45:30 +02:00
|
|
|
if(targetTown->visitingHero)
|
|
|
|
{
|
|
|
|
auto basicMask = maskMap[targetTown->visitingHero.get()];
|
|
|
|
bool heroIsInChain = (actor->chainMask & basicMask) != 0;
|
|
|
|
bool sameActorInTown = actor->chainMask == basicMask;
|
|
|
|
|
|
|
|
if(sameActorInTown || !heroIsInChain)
|
|
|
|
continue;
|
|
|
|
}
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2021-05-16 13:09:49 +02:00
|
|
|
auto nodeOptional = townPortalFinder.createTownPortalNode(targetTown);
|
2021-05-16 13:07:54 +02:00
|
|
|
|
|
|
|
if(nodeOptional)
|
2021-05-15 21:02:57 +02:00
|
|
|
{
|
2021-05-16 13:39:47 +02:00
|
|
|
#if PATHFINDER_TRACE_LEVEL >= 1
|
2021-05-16 13:07:54 +02:00
|
|
|
logAi->trace("Adding town portal node at %s", targetTown->name);
|
|
|
|
#endif
|
2021-05-16 13:09:49 +02:00
|
|
|
initialNodes.push_back(nodeOptional.get());
|
2021-05-15 21:02:57 +02:00
|
|
|
}
|
2021-05-15 18:22:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AINodeStorage::hasBetterChain(const PathNodeInfo & source, CDestinationNodeInfo & destination) const
|
|
|
|
{
|
|
|
|
auto pos = destination.coord;
|
2021-05-16 13:56:27 +02:00
|
|
|
auto chains = nodes.get(pos, 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
|
|
|
|
2022-04-04 09:16:32 +02:00
|
|
|
if(sameNode || node.action == CGPathNode::ENodeAction::UNKNOWN || !node.actor || !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
|
|
|
{
|
2020-09-02 19:16:24 +02:00
|
|
|
if(node.getCost() < candidateNode->getCost())
|
2021-05-15 18:22:44 +02:00
|
|
|
{
|
2021-05-16 13:39:47 +02:00
|
|
|
#if PATHFINDER_TRACE_LEVEL >= 2
|
2021-05-15 18:22:44 +02:00
|
|
|
logAi->trace(
|
2021-05-16 13:09:49 +02:00
|
|
|
"Block ineficient battle move %s->%s, hero: %s[%X], army %lld, 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(),
|
2021-05-16 13:09:49 +02:00
|
|
|
candidateNode->actor->hero->name,
|
2021-05-15 20:56:08 +02:00
|
|
|
candidateNode->actor->chainMask,
|
2021-05-16 13:09:49 +02:00
|
|
|
candidateNode->actor->armyValue,
|
2021-05-15 20:56:08 +02:00
|
|
|
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-16 13:56:27 +02:00
|
|
|
if(candidateActor->chainMask != node.actor->chainMask && heroChainPass != EHeroChainPass::FINAL)
|
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
|
2020-09-02 19:16:24 +02:00
|
|
|
&& node.getCost() <= candidateNode->getCost())
|
2021-05-15 20:56:08 +02:00
|
|
|
{
|
2021-05-16 13:39:47 +02:00
|
|
|
#if PATHFINDER_TRACE_LEVEL >= 2
|
2021-05-16 13:09:49 +02:00
|
|
|
logAi->trace(
|
|
|
|
"Block ineficient move because of stronger army %s->%s, hero: %s[%X], army %lld, mp diff: %i",
|
|
|
|
source->coord.toString(),
|
|
|
|
candidateNode->coord.toString(),
|
|
|
|
candidateNode->actor->hero->name,
|
|
|
|
candidateNode->actor->chainMask,
|
|
|
|
candidateNode->actor->armyValue,
|
|
|
|
node.moveRemains - candidateNode->moveRemains);
|
|
|
|
#endif
|
2021-05-15 20:56:08 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-05-15 20:54:58 +02:00
|
|
|
|
2021-05-16 13:56:13 +02:00
|
|
|
if(heroChainPass == EHeroChainPass::FINAL)
|
2021-05-15 20:54:58 +02:00
|
|
|
{
|
2021-05-16 13:56:13 +02:00
|
|
|
if(nodeArmyValue == candidateArmyValue
|
|
|
|
&& nodeActor->heroFightingStrength >= candidateActor->heroFightingStrength
|
2020-09-02 19:16:24 +02:00
|
|
|
&& node.getCost() <= candidateNode->getCost())
|
2021-05-16 13:09:49 +02:00
|
|
|
{
|
2021-05-16 13:56:13 +02:00
|
|
|
if(nodeActor->heroFightingStrength == candidateActor->heroFightingStrength
|
2020-09-02 19:16:24 +02:00
|
|
|
&& node.getCost() == candidateNode->getCost()
|
2021-05-16 13:56:13 +02:00
|
|
|
&& &node < candidateNode)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2021-05-16 13:09:49 +02:00
|
|
|
|
|
|
|
#if AI_TRACE_LEVEL >= 2
|
2021-05-16 13:56:13 +02:00
|
|
|
logAi->trace(
|
|
|
|
"Block ineficient move because of stronger hero %s->%s, hero: %s[%X], army %lld, mp diff: %i",
|
|
|
|
source->coord.toString(),
|
|
|
|
candidateNode->coord.toString(),
|
|
|
|
candidateNode->actor->hero->name,
|
|
|
|
candidateNode->actor->chainMask,
|
|
|
|
candidateNode->actor->armyValue,
|
|
|
|
node.moveRemains - candidateNode->moveRemains);
|
2021-05-16 13:09:49 +02:00
|
|
|
#endif
|
2021-05-16 13:56:13 +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-16 13:56:27 +02:00
|
|
|
auto chains = nodes.get(pos, layer);
|
2021-05-15 18:22:49 +02:00
|
|
|
|
|
|
|
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-16 13:56:27 +02:00
|
|
|
auto chains = nodes.get(pos, isOnLand ? EPathfindingLayer::LAND : EPathfindingLayer::SAIL);
|
2021-05-15 18:22:44 +02:00
|
|
|
|
|
|
|
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-16 19:53:11 +02:00
|
|
|
path.targetObjectDanger = evaluateDanger(pos, path.targetHero, !node.actor->allowBattle);
|
2021-05-16 13:13:40 +02:00
|
|
|
path.targetObjectArmyLoss = evaluateArmyLoss(path.targetHero, path.heroArmy->getArmyStrength(), path.targetObjectDanger);
|
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-16 13:09:49 +02:00
|
|
|
//if(node->actor->hero->visitablePos() != node->coord)
|
2021-05-15 20:04:48 +02:00
|
|
|
{
|
|
|
|
AIPathNodeInfo pathNode;
|
2020-09-02 19:16:24 +02:00
|
|
|
|
|
|
|
pathNode.cost = node->getCost();
|
2021-05-15 20:04:48 +02:00
|
|
|
pathNode.targetHero = node->actor->hero;
|
2021-05-16 13:09:49 +02:00
|
|
|
pathNode.chainMask = node->actor->chainMask;
|
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;
|
2021-05-16 13:56:27 +02:00
|
|
|
pathNode.actionIsBlocked = false;
|
2021-05-15 20:56:31 +02:00
|
|
|
|
2021-05-16 13:38:53 +02:00
|
|
|
if(pathNode.specialAction)
|
|
|
|
{
|
2021-05-16 13:56:27 +02:00
|
|
|
auto targetNode =node->theNodeBefore ? getAINode(node->theNodeBefore) : node;
|
|
|
|
|
|
|
|
pathNode.actionIsBlocked = !pathNode.specialAction->canAct(targetNode);
|
2021-05-16 13:38:53 +02:00
|
|
|
}
|
|
|
|
|
2021-05-15 20:56:31 +02:00
|
|
|
parentIndex = path.nodes.size();
|
2021-05-15 20:04:48 +02:00
|
|
|
|
|
|
|
path.nodes.push_back(pathNode);
|
|
|
|
}
|
2021-05-16 13:38:53 +02:00
|
|
|
|
2021-05-15 20:01:48 +02:00
|
|
|
node = getAINode(node->theNodeBefore);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:22:44 +02:00
|
|
|
AIPath::AIPath()
|
|
|
|
: nodes({})
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:38:53 +02:00
|
|
|
std::shared_ptr<const SpecialAction> AIPath::getFirstBlockedAction() const
|
2021-05-16 13:15:03 +02:00
|
|
|
{
|
2021-05-16 13:56:35 +02:00
|
|
|
for(auto node = nodes.rbegin(); node != nodes.rend(); node++)
|
2021-05-16 13:15:03 +02:00
|
|
|
{
|
2021-05-16 13:56:35 +02:00
|
|
|
if(node->specialAction && node->actionIsBlocked)
|
|
|
|
return node->specialAction;
|
2021-05-16 13:15:03 +02:00
|
|
|
}
|
|
|
|
|
2021-05-16 13:38:53 +02:00
|
|
|
return std::shared_ptr<const SpecialAction>();
|
2021-05-16 13:15:03 +02:00
|
|
|
}
|
|
|
|
|
2021-05-15 18:22:44 +02:00
|
|
|
int3 AIPath::firstTileToGet() const
|
|
|
|
{
|
|
|
|
if(nodes.size())
|
|
|
|
{
|
|
|
|
return nodes.back().coord;
|
|
|
|
}
|
|
|
|
|
|
|
|
return int3(-1, -1, -1);
|
|
|
|
}
|
|
|
|
|
2021-05-15 20:57:27 +02:00
|
|
|
int3 AIPath::targetTile() const
|
|
|
|
{
|
|
|
|
if(nodes.size())
|
|
|
|
{
|
2021-05-16 13:09:49 +02:00
|
|
|
return targetNode().coord;
|
2021-05-15 20:57:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return int3(-1, -1, -1);
|
|
|
|
}
|
|
|
|
|
2021-05-15 20:04:48 +02:00
|
|
|
const AIPathNodeInfo & AIPath::firstNode() const
|
|
|
|
{
|
|
|
|
return nodes.back();
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:09:49 +02:00
|
|
|
const AIPathNodeInfo & AIPath::targetNode() const
|
|
|
|
{
|
|
|
|
auto & node = nodes.front();
|
|
|
|
|
|
|
|
return targetHero == node.targetHero ? node : nodes.at(1);
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:22:44 +02:00
|
|
|
uint64_t AIPath::getPathDanger() const
|
|
|
|
{
|
2021-05-16 13:09:49 +02:00
|
|
|
if(nodes.empty())
|
|
|
|
return 0;
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2021-05-16 13:09:49 +02:00
|
|
|
return targetNode().danger;
|
2021-05-15 18:22:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
float AIPath::movementCost() const
|
|
|
|
{
|
2021-05-16 13:09:49 +02:00
|
|
|
if(nodes.empty())
|
|
|
|
return 0.0f;
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2021-05-16 13:09:49 +02:00
|
|
|
return targetNode().cost;
|
2021-05-15 18:22:44 +02:00
|
|
|
}
|
|
|
|
|
2021-05-15 20:57:36 +02:00
|
|
|
uint8_t AIPath::turn() const
|
|
|
|
{
|
2021-05-16 13:09:49 +02:00
|
|
|
if(nodes.empty())
|
|
|
|
return 0;
|
2021-05-15 20:57:36 +02:00
|
|
|
|
2021-05-16 13:09:49 +02:00
|
|
|
return targetNode().turns;
|
2021-05-15 20:57:36 +02:00
|
|
|
}
|
|
|
|
|
2021-05-15 20:01:48 +02:00
|
|
|
uint64_t AIPath::getHeroStrength() const
|
|
|
|
{
|
|
|
|
return targetHero->getFightingStrength() * heroArmy->getArmyStrength();
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:13:40 +02:00
|
|
|
uint64_t AIPath::getTotalDanger() const
|
2021-05-15 18:22:44 +02:00
|
|
|
{
|
|
|
|
uint64_t pathDanger = getPathDanger();
|
|
|
|
uint64_t danger = pathDanger > targetObjectDanger ? pathDanger : targetObjectDanger;
|
|
|
|
|
|
|
|
return danger;
|
2021-05-15 20:57:27 +02:00
|
|
|
}
|
|
|
|
|
2021-05-16 13:19:27 +02:00
|
|
|
bool AIPath::containsHero(const CGHeroInstance * hero) const
|
|
|
|
{
|
|
|
|
if(targetHero == hero)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for(auto node : nodes)
|
|
|
|
{
|
|
|
|
if(node.targetHero == hero)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:13:40 +02:00
|
|
|
uint64_t AIPath::getTotalArmyLoss() const
|
|
|
|
{
|
|
|
|
return armyLoss + targetObjectArmyLoss;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:15:03 +02:00
|
|
|
std::string AIPath::toString() const
|
2021-05-15 20:57:27 +02:00
|
|
|
{
|
|
|
|
std::stringstream str;
|
2021-05-16 13:09:49 +02:00
|
|
|
|
2021-05-16 13:56:13 +02:00
|
|
|
str << targetHero->name << "[" << std::hex << chainMask << std::dec << "]" << ", turn " << (int)(turn()) << ": ";
|
2021-05-16 13:09:49 +02:00
|
|
|
|
2021-05-15 20:57:27 +02:00
|
|
|
for(auto node : nodes)
|
2021-05-16 13:09:49 +02:00
|
|
|
str << node.targetHero->name << "[" << std::hex << node.chainMask << std::dec << "]" << "->" << node.coord.toString() << "; ";
|
2021-05-15 20:57:27 +02:00
|
|
|
|
|
|
|
return str.str();
|
2021-05-15 18:22:49 +02:00
|
|
|
}
|