1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-04 09:42:40 +02:00
vcmi/AI/Nullkiller/Pathfinding/AINodeStorage.h

175 lines
5.2 KiB
C
Raw Normal View History

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
2021-05-15 20:04:11 +02:00
#define VCMI_TRACE_PATHFINDER
2021-05-15 20:04:48 +02:00
#define VCMI_TRACE_PATHFINDER_EX
2021-05-15 20:04:11 +02:00
2021-05-15 18:22:44 +02:00
#include "../../../lib/CPathfinder.h"
#include "../../../lib/mapObjects/CGHeroInstance.h"
#include "../AIUtility.h"
#include "../FuzzyHelper.h"
#include "../Goals/AbstractGoal.h"
#include "Actions/ISpecialAction.h"
2021-05-15 19:59:43 +02:00
#include "Actors.h"
2021-05-15 20:04:48 +02:00
#include <inttypes.h>
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;
2021-05-15 18:22:44 +02:00
uint32_t manaCost;
2021-05-15 20:01:48 +02:00
const AIPathNode * chainOther;
2021-05-15 18:22:44 +02:00
std::shared_ptr<const ISpecialAction> specialAction;
2021-05-15 18:22:49 +02:00
const ChainActor * actor;
2021-05-15 18:22:44 +02:00
};
struct AIPathNodeInfo
{
float cost;
int turns;
int3 coord;
uint64_t danger;
2021-05-15 20:01:48 +02:00
const CGHeroInstance * targetHero;
2021-05-15 18:22:44 +02:00
};
struct AIPath
{
std::vector<AIPathNodeInfo> nodes;
std::shared_ptr<const ISpecialAction> specialAction;
uint64_t targetObjectDanger;
2021-05-15 20:01:48 +02:00
uint64_t armyLoss;
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-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
uint64_t getTotalDanger(HeroPtr hero) const;
int3 firstTileToGet() const;
2021-05-15 20:04:48 +02:00
const AIPathNodeInfo & firstNode() const;
2021-05-15 18:22:44 +02:00
float movementCost() const;
2021-05-15 20:01:48 +02:00
uint64_t getHeroStrength() 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-15 18:22:44 +02:00
class AINodeStorage : public INodeStorage
{
private:
int3 sizes;
/// 1-3 - position on map, 4 - layer (air, water, land), 5 - chain (normal, battle, spellcast and combinations)
boost::multi_array<AIPathNode, 5> nodes;
const CPlayerSpecificInfoCallback * cb;
const VCAI * ai;
std::unique_ptr<FuzzyHelper> dangerEvaluator;
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;
bool heroChainPass; // true if we need to calculate hero chain
2021-05-15 20:04:48 +02:00
int heroChainMaxTurns = 0;
2021-05-15 18:22:44 +02:00
public:
2021-05-15 18:22:49 +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.
2021-05-15 20:54:28 +02:00
static const int NUM_CHAINS = 5 * GameConstants::MAX_HEROES_PER_PLAYER;
2021-05-15 18:22:49 +02:00
2021-05-15 18:22:44 +02:00
AINodeStorage(const int3 & sizes);
~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-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;
const AIPathNode * getAINode(const CGPathNode * node) const;
void updateAINode(CGPathNode * node, std::function<void (AIPathNode *)> updater);
bool hasBetterChain(const PathNodeInfo & source, CDestinationNodeInfo & destination) const;
2021-05-15 20:54:58 +02:00
template<class NodeRange>
bool hasBetterChain(
const CGPathNode * source,
const AIPathNode * destinationNode,
const NodeRange & chains) const;
2021-05-15 18:22:49 +02:00
boost::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;
void setHeroes(std::vector<HeroPtr> heroes, const VCAI * ai);
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 CGHeroInstance * getHero(const CGPathNode * node) const;
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-15 18:22:44 +02:00
2021-05-15 18:23:01 +02:00
uint64_t evaluateDanger(const int3 & tile, const CGHeroInstance * hero) const
2021-05-15 18:22:44 +02:00
{
return dangerEvaluator->evaluateDanger(tile, hero, ai);
}
private:
2021-05-15 20:01:48 +02:00
STRONG_INLINE
void resetTile(const int3 & tile, EPathfindingLayer layer, CGPathNode::EAccessibility accessibility);
2021-05-15 20:54:58 +02:00
void calculateHeroChain(
AIPathNode * srcNode,
const std::vector<AIPathNode *> & variants,
std::vector<ExchangeCandidate> & result) const;
void calculateHeroChain(
AIPathNode * carrier,
AIPathNode * other,
std::vector<ExchangeCandidate> & result) const;
void cleanupInefectiveChains(std::vector<ExchangeCandidate> & result) const;
void addHeroChain(const std::vector<ExchangeCandidate> & result);
2021-05-15 18:22:44 +02:00
void calculateTownPortalTeleportations(const PathNodeInfo & source, std::vector<CGPathNode *> & neighbours);
2021-05-15 20:01:48 +02:00
void fillChainInfo(const AIPathNode * node, AIPath & path) const;
void commit(
AIPathNode * destination,
const AIPathNode * source,
CGPathNode::ENodeAction action,
int turn,
int movementLeft,
float cost) const;
2021-05-15 20:54:58 +02:00
ExchangeCandidate calculateExchange(
ChainActor * exchangeActor,
2021-05-15 20:01:48 +02:00
AIPathNode * carrierParentNode,
AIPathNode * otherParentNode) const;
2021-05-15 18:22:44 +02:00
};