2021-05-15 18:22:44 +02:00
|
|
|
/*
|
|
|
|
* AINodeStorage.h, 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-09-26 20:01:07 +02:00
|
|
|
#define NKAI_PATHFINDER_TRACE_LEVEL 0
|
2023-07-30 10:33:52 +02:00
|
|
|
#define NKAI_TRACE_LEVEL 0
|
2021-05-15 20:04:11 +02:00
|
|
|
|
2023-06-21 12:46:09 +02:00
|
|
|
#include "../../../lib/pathfinder/CGPathNode.h"
|
|
|
|
#include "../../../lib/pathfinder/INodeStorage.h"
|
2021-05-15 18:22:44 +02:00
|
|
|
#include "../../../lib/mapObjects/CGHeroInstance.h"
|
|
|
|
#include "../AIUtility.h"
|
2020-05-04 17:58:43 +02:00
|
|
|
#include "../Engine/FuzzyHelper.h"
|
2021-05-15 18:22:44 +02:00
|
|
|
#include "../Goals/AbstractGoal.h"
|
2021-05-16 13:38:53 +02:00
|
|
|
#include "Actions/SpecialAction.h"
|
2021-05-15 19:59:43 +02:00
|
|
|
#include "Actors.h"
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2022-09-26 20:01:07 +02:00
|
|
|
namespace NKAI
|
|
|
|
{
|
2023-06-04 15:02:02 +02:00
|
|
|
const int SCOUT_TURN_DISTANCE_LIMIT = 5;
|
|
|
|
const int MAIN_TURN_DISTANCE_LIMIT = 10;
|
2022-09-26 20:01:07 +02:00
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
namespace AIPathfinding
|
|
|
|
{
|
2022-09-06 20:14:22 +02:00
|
|
|
#ifdef ENVIRONMENT64
|
|
|
|
const int BUCKET_COUNT = 7;
|
|
|
|
#else
|
|
|
|
const int BUCKET_COUNT = 5;
|
|
|
|
#endif // ENVIRONMENT64
|
|
|
|
|
|
|
|
const int BUCKET_SIZE = 5;
|
2020-09-02 19:16:24 +02:00
|
|
|
const int NUM_CHAINS = BUCKET_COUNT * BUCKET_SIZE;
|
|
|
|
const int THREAD_COUNT = 8;
|
2022-09-06 20:14:22 +02:00
|
|
|
const int CHAIN_MAX_DEPTH = 4;
|
2020-09-02 19:16:24 +02:00
|
|
|
}
|
|
|
|
|
2021-05-15 18:22:44 +02:00
|
|
|
struct AIPathNode : public CGPathNode
|
|
|
|
{
|
|
|
|
uint64_t danger;
|
2021-05-15 18:23:01 +02:00
|
|
|
uint64_t armyLoss;
|
2023-09-16 11:33:02 +02:00
|
|
|
int32_t manaCost;
|
2021-05-15 20:01:48 +02:00
|
|
|
const AIPathNode * chainOther;
|
2021-05-16 13:38:53 +02:00
|
|
|
std::shared_ptr<const SpecialAction> specialAction;
|
2021-05-15 18:22:49 +02:00
|
|
|
const ChainActor * actor;
|
2021-05-16 13:56:53 +02:00
|
|
|
|
|
|
|
STRONG_INLINE
|
|
|
|
bool blocked() const
|
|
|
|
{
|
2023-06-21 14:38:57 +02:00
|
|
|
return accessible == EPathAccessibility::NOT_SET
|
|
|
|
|| accessible == EPathAccessibility::BLOCKED;
|
2021-05-16 13:56:53 +02:00
|
|
|
}
|
2022-12-26 20:17:39 +02:00
|
|
|
|
|
|
|
void addSpecialAction(std::shared_ptr<const SpecialAction> action);
|
2021-05-15 18:22:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct AIPathNodeInfo
|
|
|
|
{
|
|
|
|
float cost;
|
2021-05-15 20:57:36 +02:00
|
|
|
uint8_t turns;
|
2021-05-15 18:22:44 +02:00
|
|
|
int3 coord;
|
2023-09-24 12:07:42 +02:00
|
|
|
EPathfindingLayer layer;
|
2021-05-15 18:22:44 +02:00
|
|
|
uint64_t danger;
|
2021-05-15 20:01:48 +02:00
|
|
|
const CGHeroInstance * targetHero;
|
2021-05-15 20:56:31 +02:00
|
|
|
int parentIndex;
|
2021-05-16 13:09:49 +02:00
|
|
|
uint64_t chainMask;
|
2021-05-16 13:38:53 +02:00
|
|
|
std::shared_ptr<const SpecialAction> specialAction;
|
|
|
|
bool actionIsBlocked;
|
2021-05-15 18:22:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct AIPath
|
|
|
|
{
|
|
|
|
std::vector<AIPathNodeInfo> nodes;
|
|
|
|
uint64_t targetObjectDanger;
|
2021-05-15 20:01:48 +02:00
|
|
|
uint64_t armyLoss;
|
2021-05-16 13:13:40 +02:00
|
|
|
uint64_t targetObjectArmyLoss;
|
2021-05-15 18:22:49 +02:00
|
|
|
const CGHeroInstance * targetHero;
|
2021-05-15 20:01:48 +02:00
|
|
|
const CCreatureSet * heroArmy;
|
2021-05-15 20:04:48 +02:00
|
|
|
uint64_t chainMask;
|
2021-05-16 12:52:30 +02:00
|
|
|
uint8_t exchangeCount;
|
2021-05-15 18:22:44 +02:00
|
|
|
|
|
|
|
AIPath();
|
|
|
|
|
|
|
|
/// Gets danger of path excluding danger of visiting the target object like creature bank
|
|
|
|
uint64_t getPathDanger() const;
|
|
|
|
|
|
|
|
/// Gets danger of path including danger of visiting the target object like creature bank
|
2021-05-16 13:13:40 +02:00
|
|
|
uint64_t getTotalDanger() const;
|
|
|
|
|
|
|
|
/// Gets danger of path including danger of visiting the target object like creature bank
|
|
|
|
uint64_t getTotalArmyLoss() const;
|
2021-05-15 18:22:44 +02:00
|
|
|
|
|
|
|
int3 firstTileToGet() const;
|
2021-05-15 20:57:27 +02:00
|
|
|
int3 targetTile() const;
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2021-05-15 20:04:48 +02:00
|
|
|
const AIPathNodeInfo & firstNode() const;
|
|
|
|
|
2021-05-16 13:09:49 +02:00
|
|
|
const AIPathNodeInfo & targetNode() const;
|
|
|
|
|
2021-05-15 18:22:44 +02:00
|
|
|
float movementCost() const;
|
2021-05-15 20:01:48 +02:00
|
|
|
|
2021-05-15 20:57:36 +02:00
|
|
|
uint8_t turn() const;
|
|
|
|
|
2021-05-15 20:01:48 +02:00
|
|
|
uint64_t getHeroStrength() const;
|
2021-05-15 20:57:27 +02:00
|
|
|
|
2021-05-16 13:15:03 +02:00
|
|
|
std::string toString() const;
|
|
|
|
|
2021-05-16 13:38:53 +02:00
|
|
|
std::shared_ptr<const SpecialAction> getFirstBlockedAction() const;
|
2021-05-16 13:19:27 +02:00
|
|
|
|
|
|
|
bool containsHero(const CGHeroInstance * hero) const;
|
2021-05-15 18:22:44 +02:00
|
|
|
};
|
|
|
|
|
2021-05-15 20:54:58 +02:00
|
|
|
struct ExchangeCandidate : public AIPathNode
|
|
|
|
{
|
|
|
|
AIPathNode * carrierParent;
|
|
|
|
AIPathNode * otherParent;
|
|
|
|
};
|
|
|
|
|
2021-05-16 13:15:03 +02:00
|
|
|
enum EHeroChainPass
|
|
|
|
{
|
|
|
|
INITIAL, // single heroes unlimited distance
|
|
|
|
|
|
|
|
CHAIN, // chains with limited distance
|
|
|
|
|
|
|
|
FINAL // same as SINGLE but for heroes from CHAIN pass
|
|
|
|
};
|
|
|
|
|
2021-05-16 13:56:27 +02:00
|
|
|
class AISharedStorage
|
|
|
|
{
|
2022-09-18 16:39:10 +02:00
|
|
|
// 1 - layer (air, water, land)
|
|
|
|
// 2-4 - position on map[z][x][y]
|
|
|
|
// 5 - chain (normal, battle, spellcast and combinations)
|
2021-05-16 13:56:27 +02:00
|
|
|
static std::shared_ptr<boost::multi_array<AIPathNode, 5>> shared;
|
|
|
|
std::shared_ptr<boost::multi_array<AIPathNode, 5>> nodes;
|
|
|
|
public:
|
2022-12-14 22:13:26 +02:00
|
|
|
static boost::mutex locker;
|
|
|
|
|
2021-05-16 13:56:27 +02:00
|
|
|
AISharedStorage(int3 mapSize);
|
|
|
|
~AISharedStorage();
|
|
|
|
|
|
|
|
STRONG_INLINE
|
|
|
|
boost::detail::multi_array::sub_array<AIPathNode, 1> get(int3 tile, EPathfindingLayer layer) const
|
|
|
|
{
|
2022-09-18 16:39:10 +02:00
|
|
|
return (*nodes)[layer][tile.z][tile.x][tile.y];
|
2021-05-16 13:56:27 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-15 18:22:44 +02:00
|
|
|
class AINodeStorage : public INodeStorage
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
int3 sizes;
|
|
|
|
|
|
|
|
const CPlayerSpecificInfoCallback * cb;
|
2020-05-04 17:58:43 +02:00
|
|
|
const Nullkiller * ai;
|
2021-05-15 18:22:44 +02:00
|
|
|
std::unique_ptr<FuzzyHelper> dangerEvaluator;
|
2021-05-16 13:56:27 +02:00
|
|
|
AISharedStorage nodes;
|
2021-05-15 18:22:49 +02:00
|
|
|
std::vector<std::shared_ptr<ChainActor>> actors;
|
2021-05-15 20:01:48 +02:00
|
|
|
std::vector<CGPathNode *> heroChain;
|
2021-05-16 13:15:03 +02:00
|
|
|
EHeroChainPass heroChainPass; // true if we need to calculate hero chain
|
2021-05-16 13:15:12 +02:00
|
|
|
uint64_t chainMask;
|
2021-05-15 20:56:08 +02:00
|
|
|
int heroChainTurn;
|
2021-05-16 13:13:56 +02:00
|
|
|
int heroChainMaxTurns;
|
2021-05-15 20:57:36 +02:00
|
|
|
PlayerColor playerID;
|
2021-05-16 14:01:34 +02:00
|
|
|
uint8_t turnDistanceLimit[2];
|
2021-05-15 18:22:44 +02:00
|
|
|
|
|
|
|
public:
|
2021-05-16 13:56:53 +02:00
|
|
|
/// more than 1 chain layer for each hero allows us to have more than 1 path to each tile so we can chose more optimal one.
|
2020-05-04 17:58:43 +02:00
|
|
|
AINodeStorage(const Nullkiller * ai, const int3 & sizes);
|
2021-05-15 18:22:44 +02:00
|
|
|
~AINodeStorage();
|
|
|
|
|
2021-05-15 18:22:49 +02:00
|
|
|
void initialize(const PathfinderOptions & options, const CGameState * gs) override;
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2021-05-16 13:13:56 +02:00
|
|
|
bool increaseHeroChainTurnLimit();
|
2021-05-16 13:15:12 +02:00
|
|
|
bool selectFirstActor();
|
|
|
|
bool selectNextActor();
|
2021-05-16 13:13:56 +02:00
|
|
|
|
2021-05-15 18:22:49 +02:00
|
|
|
virtual std::vector<CGPathNode *> getInitialNodes() override;
|
2021-05-15 18:22:44 +02:00
|
|
|
|
|
|
|
virtual std::vector<CGPathNode *> calculateNeighbours(
|
|
|
|
const PathNodeInfo & source,
|
|
|
|
const PathfinderConfig * pathfinderConfig,
|
|
|
|
const CPathfinderHelper * pathfinderHelper) override;
|
|
|
|
|
|
|
|
virtual std::vector<CGPathNode *> calculateTeleportations(
|
|
|
|
const PathNodeInfo & source,
|
|
|
|
const PathfinderConfig * pathfinderConfig,
|
|
|
|
const CPathfinderHelper * pathfinderHelper) override;
|
|
|
|
|
|
|
|
virtual void commit(CDestinationNodeInfo & destination, const PathNodeInfo & source) override;
|
|
|
|
|
2021-05-16 13:09:49 +02:00
|
|
|
void commit(
|
|
|
|
AIPathNode * destination,
|
|
|
|
const AIPathNode * source,
|
2023-06-21 14:38:57 +02:00
|
|
|
EPathNodeAction action,
|
2021-05-16 13:09:49 +02:00
|
|
|
int turn,
|
|
|
|
int movementLeft,
|
|
|
|
float cost) const;
|
|
|
|
|
2022-12-10 10:22:54 +02:00
|
|
|
inline const AIPathNode * getAINode(const CGPathNode * node) const
|
|
|
|
{
|
|
|
|
return static_cast<const AIPathNode *>(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void updateAINode(CGPathNode * node, std::function<void (AIPathNode *)> updater)
|
|
|
|
{
|
2023-04-17 23:11:16 +02:00
|
|
|
auto * aiNode = static_cast<AIPathNode *>(node);
|
2022-12-10 10:22:54 +02:00
|
|
|
|
|
|
|
updater(aiNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const CGHeroInstance * getHero(const CGPathNode * node) const
|
|
|
|
{
|
2023-04-17 23:11:16 +02:00
|
|
|
const auto * aiNode = getAINode(node);
|
2022-12-10 10:22:54 +02:00
|
|
|
|
|
|
|
return aiNode->actor->hero;
|
|
|
|
}
|
2021-05-15 18:22:44 +02:00
|
|
|
|
|
|
|
bool hasBetterChain(const PathNodeInfo & source, CDestinationNodeInfo & destination) const;
|
2021-05-15 20:54:58 +02:00
|
|
|
|
2021-05-16 13:09:49 +02:00
|
|
|
bool isMovementIneficient(const PathNodeInfo & source, CDestinationNodeInfo & destination) const
|
|
|
|
{
|
|
|
|
return hasBetterChain(source, destination);
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:56:13 +02:00
|
|
|
bool isDistanceLimitReached(const PathNodeInfo & source, CDestinationNodeInfo & destination) const;
|
2021-05-16 13:19:00 +02:00
|
|
|
|
2021-05-15 20:54:58 +02:00
|
|
|
template<class NodeRange>
|
|
|
|
bool hasBetterChain(
|
|
|
|
const CGPathNode * source,
|
|
|
|
const AIPathNode * destinationNode,
|
|
|
|
const NodeRange & chains) const;
|
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
std::optional<AIPathNode *> getOrCreateNode(const int3 & coord, const EPathfindingLayer layer, const ChainActor * actor);
|
2021-05-15 18:22:44 +02:00
|
|
|
std::vector<AIPath> getChainInfo(const int3 & pos, bool isOnLand) const;
|
2021-05-15 18:22:49 +02:00
|
|
|
bool isTileAccessible(const HeroPtr & hero, const int3 & pos, const EPathfindingLayer layer) const;
|
2021-05-16 13:56:13 +02:00
|
|
|
void setHeroes(std::map<const CGHeroInstance *, HeroRole> heroes);
|
2021-05-16 14:01:34 +02:00
|
|
|
void setScoutTurnDistanceLimit(uint8_t distanceLimit) { turnDistanceLimit[HeroRole::SCOUT] = distanceLimit; }
|
|
|
|
void setMainTurnDistanceLimit(uint8_t distanceLimit) { turnDistanceLimit[HeroRole::MAIN] = distanceLimit; }
|
2019-03-17 23:27:09 +02:00
|
|
|
void setTownsAndDwellings(
|
|
|
|
const std::vector<const CGTownInstance *> & towns,
|
2021-05-15 20:54:28 +02:00
|
|
|
const std::set<const CGObjectInstance *> & visitableObjs);
|
2021-05-15 18:22:49 +02:00
|
|
|
const std::set<const CGHeroInstance *> getAllHeroes() const;
|
2021-05-15 18:23:01 +02:00
|
|
|
void clear();
|
2021-05-15 20:01:48 +02:00
|
|
|
bool calculateHeroChain();
|
2021-05-16 13:15:03 +02:00
|
|
|
bool calculateHeroChainFinal();
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2022-12-10 10:22:54 +02:00
|
|
|
inline uint64_t evaluateDanger(const int3 & tile, const CGHeroInstance * hero, bool checkGuards) const
|
2021-05-15 18:22:44 +02:00
|
|
|
{
|
2020-05-04 17:58:43 +02:00
|
|
|
return dangerEvaluator->evaluateDanger(tile, hero, checkGuards);
|
2021-05-15 18:22:44 +02:00
|
|
|
}
|
|
|
|
|
2022-12-10 10:22:54 +02:00
|
|
|
inline uint64_t evaluateArmyLoss(const CGHeroInstance * hero, uint64_t armyValue, uint64_t danger) const
|
2021-05-16 13:13:40 +02:00
|
|
|
{
|
|
|
|
double ratio = (double)danger / (armyValue * hero->getFightingStrength());
|
|
|
|
|
2023-07-30 10:33:52 +02:00
|
|
|
return (uint64_t)(armyValue * ratio * ratio);
|
2021-05-16 13:13:40 +02:00
|
|
|
}
|
|
|
|
|
2021-05-15 20:01:48 +02:00
|
|
|
STRONG_INLINE
|
2023-06-21 14:38:57 +02:00
|
|
|
void resetTile(const int3 & tile, EPathfindingLayer layer, EPathAccessibility accessibility);
|
2022-12-10 10:22:54 +02:00
|
|
|
|
2020-09-02 19:16:24 +02:00
|
|
|
STRONG_INLINE int getBucket(const ChainActor * actor) const
|
|
|
|
{
|
|
|
|
return ((uintptr_t)actor * 395) % AIPathfinding::BUCKET_COUNT;
|
|
|
|
}
|
2021-05-15 20:54:58 +02:00
|
|
|
|
2021-05-16 13:07:54 +02:00
|
|
|
void calculateTownPortalTeleportations(std::vector<CGPathNode *> & neighbours);
|
2021-05-15 20:56:31 +02:00
|
|
|
void fillChainInfo(const AIPathNode * node, AIPath & path, int parentIndex) const;
|
2022-09-06 20:14:22 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
template<class TVector>
|
|
|
|
void calculateTownPortal(
|
|
|
|
const ChainActor * actor,
|
|
|
|
const std::map<const CGHeroInstance *, int> & maskMap,
|
|
|
|
const std::vector<CGPathNode *> & initialNodes,
|
|
|
|
TVector & output);
|
2021-05-15 18:22:44 +02:00
|
|
|
};
|
2022-09-26 20:01:07 +02:00
|
|
|
|
|
|
|
}
|