|
|
@@ -5,6 +5,8 @@
|
|
|
#include "IGameCallback.h"
|
|
|
#include "int3.h"
|
|
|
|
|
|
+#include <boost/heap/priority_queue.hpp>
|
|
|
+
|
|
|
/*
|
|
|
* CPathfinder.h, part of VCMI engine
|
|
|
*
|
|
|
@@ -18,26 +20,45 @@
|
|
|
class CGHeroInstance;
|
|
|
class CGObjectInstance;
|
|
|
struct TerrainTile;
|
|
|
+class CPathfinderHelper;
|
|
|
|
|
|
struct DLL_LINKAGE CGPathNode
|
|
|
{
|
|
|
- enum EAccessibility
|
|
|
+ typedef EPathfindingLayer ELayer;
|
|
|
+
|
|
|
+ enum ENodeAction : ui8
|
|
|
+ {
|
|
|
+ UNKNOWN = 0,
|
|
|
+ EMBARK = 1,
|
|
|
+ DISEMBARK,
|
|
|
+ NORMAL,
|
|
|
+ BATTLE,
|
|
|
+ VISIT,
|
|
|
+ BLOCKING_VISIT
|
|
|
+ };
|
|
|
+
|
|
|
+ enum EAccessibility : ui8
|
|
|
{
|
|
|
NOT_SET = 0,
|
|
|
ACCESSIBLE = 1, //tile can be entered and passed
|
|
|
VISITABLE, //tile can be entered as the last tile in path
|
|
|
BLOCKVIS, //visitable from neighbouring tile but not passable
|
|
|
+ FLYABLE, //can only be accessed in air layer
|
|
|
BLOCKED //tile can't be entered nor visited
|
|
|
};
|
|
|
|
|
|
- EAccessibility accessible;
|
|
|
- ui8 land;
|
|
|
- ui8 turns; //how many turns we have to wait before reachng the tile - 0 means current turn
|
|
|
- ui32 moveRemains; //remaining tiles after hero reaches the tile
|
|
|
CGPathNode * theNodeBefore;
|
|
|
int3 coord; //coordinates
|
|
|
+ ui32 moveRemains; //remaining tiles after hero reaches the tile
|
|
|
+ ui8 turns; //how many turns we have to wait before reachng the tile - 0 means current turn
|
|
|
+ ELayer layer;
|
|
|
+ EAccessibility accessible;
|
|
|
+ ENodeAction action;
|
|
|
+ bool locked;
|
|
|
|
|
|
CGPathNode();
|
|
|
+ void reset();
|
|
|
+ void update(const int3 & Coord, const ELayer Layer, const EAccessibility Accessible);
|
|
|
bool reachable() const;
|
|
|
};
|
|
|
|
|
|
@@ -52,27 +73,36 @@ struct DLL_LINKAGE CGPath
|
|
|
|
|
|
struct DLL_LINKAGE CPathsInfo
|
|
|
{
|
|
|
+ typedef EPathfindingLayer ELayer;
|
|
|
+
|
|
|
mutable boost::mutex pathMx;
|
|
|
|
|
|
- const CGHeroInstance *hero;
|
|
|
+ const CGHeroInstance * hero;
|
|
|
int3 hpos;
|
|
|
int3 sizes;
|
|
|
- CGPathNode ***nodes; //[w][h][level]
|
|
|
+ boost::multi_array<CGPathNode, 4> nodes; //[w][h][level][layer]
|
|
|
|
|
|
- CPathsInfo(const int3 &Sizes);
|
|
|
+ CPathsInfo(const int3 & Sizes);
|
|
|
~CPathsInfo();
|
|
|
- const CGPathNode * getPathInfo( int3 tile ) const;
|
|
|
- bool getPath(const int3 &dst, CGPath &out) const;
|
|
|
- int getDistance( int3 tile ) const;
|
|
|
+ const CGPathNode * getPathInfo(const int3 & tile) const;
|
|
|
+ bool getPath(CGPath & out, const int3 & dst) const;
|
|
|
+ int getDistance(const int3 & tile) const;
|
|
|
+ const CGPathNode * getNode(const int3 & coord) const;
|
|
|
+
|
|
|
+ CGPathNode * getNode(const int3 & coord, const ELayer layer);
|
|
|
};
|
|
|
|
|
|
class CPathfinder : private CGameInfoCallback
|
|
|
{
|
|
|
public:
|
|
|
- CPathfinder(CPathsInfo &_out, CGameState *_gs, const CGHeroInstance *_hero);
|
|
|
+ friend class CPathfinderHelper;
|
|
|
+
|
|
|
+ CPathfinder(CPathsInfo & _out, CGameState * _gs, const CGHeroInstance * _hero);
|
|
|
void calculatePaths(); //calculates possible paths for hero, uses current hero position and movement left; returns pointer to newly allocated CPath or nullptr if path does not exists
|
|
|
|
|
|
private:
|
|
|
+ typedef EPathfindingLayer ELayer;
|
|
|
+
|
|
|
struct PathfinderOptions
|
|
|
{
|
|
|
bool useFlying;
|
|
|
@@ -83,42 +113,153 @@ private:
|
|
|
bool useTeleportOneWayRandom; // One-way monoliths with more than one known exit
|
|
|
bool useTeleportWhirlpool; // Force enabled if hero protected or unaffected (have one stack of one creature)
|
|
|
|
|
|
+ /// TODO: Find out with client and server code, merge with normal teleporters.
|
|
|
+ /// Likely proper implementation would require some refactoring of CGTeleport.
|
|
|
+ /// So for now this is unfinished and disabled by default.
|
|
|
+ bool useCastleGate;
|
|
|
+
|
|
|
+ /// If true transition into air layer only possible from initial node.
|
|
|
+ /// This is drastically decrease path calculation complexity (and time).
|
|
|
+ /// Downside is less MP effective paths calculation.
|
|
|
+ ///
|
|
|
+ /// TODO: If this option end up useful for slow devices it's can be improved:
|
|
|
+ /// - Allow transition into air layer not only from initial position, but also from teleporters.
|
|
|
+ /// Movement into air can be also allowed when hero disembarked.
|
|
|
+ /// - Other idea is to allow transition into air within certain radius of N tiles around hero.
|
|
|
+ /// Patrol support need similar functionality so it's won't be ton of useless code.
|
|
|
+ /// Such limitation could be useful as it's can be scaled depend on device performance.
|
|
|
+ bool lightweightFlyingMode;
|
|
|
+
|
|
|
+ /// This option enable one turn limitation for flying and water walking.
|
|
|
+ /// So if we're out of MP while cp is blocked or water tile we won't add dest tile to queue.
|
|
|
+ ///
|
|
|
+ /// Following imitation is default H3 mechanics, but someone may want to disable it in mods.
|
|
|
+ /// After all this limit should benefit performance on maps with tons of water or blocked tiles.
|
|
|
+ ///
|
|
|
+ /// TODO:
|
|
|
+ /// - Behavior when option is disabled not implemented and will lead to crashes.
|
|
|
+ bool oneTurnSpecialLayersLimit;
|
|
|
+
|
|
|
+ /// VCMI have different movement rules to solve flaws original engine has.
|
|
|
+ /// If this option enabled you'll able to do following things in fly:
|
|
|
+ /// - Move from blocked tiles to visitable one
|
|
|
+ /// - Move from guarded tiles to blockvis tiles without being attacked
|
|
|
+ /// - Move from guarded tiles to guarded visitable tiles with being attacked after
|
|
|
+ /// TODO:
|
|
|
+ /// - Option should also allow same tile land <-> air layer transitions.
|
|
|
+ /// Current implementation only allow go into (from) air layer only to neighbour tiles.
|
|
|
+ /// I find it's reasonable limitation, but it's will make some movements more expensive than in H3.
|
|
|
+ bool originalMovementRules;
|
|
|
+
|
|
|
PathfinderOptions();
|
|
|
} options;
|
|
|
|
|
|
- CPathsInfo &out;
|
|
|
- const CGHeroInstance *hero;
|
|
|
+ CPathsInfo & out;
|
|
|
+ const CGHeroInstance * hero;
|
|
|
const std::vector<std::vector<std::vector<ui8> > > &FoW;
|
|
|
+ unique_ptr<CPathfinderHelper> hlp;
|
|
|
|
|
|
- std::list<CGPathNode*> mq; //BFS queue -> nodes to be checked
|
|
|
+ struct NodeComparer
|
|
|
+ {
|
|
|
+ bool operator()(const CGPathNode * lhs, const CGPathNode * rhs) const
|
|
|
+ {
|
|
|
+ if(rhs->turns > lhs->turns)
|
|
|
+ return false;
|
|
|
+ else if(rhs->turns == lhs->turns && rhs->moveRemains < lhs->moveRemains)
|
|
|
+ return false;
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ boost::heap::priority_queue<CGPathNode *, boost::heap::compare<NodeComparer> > pq;
|
|
|
|
|
|
+ std::vector<int3> neighbourTiles;
|
|
|
std::vector<int3> neighbours;
|
|
|
|
|
|
- CGPathNode *cp; //current (source) path node -> we took it from the queue
|
|
|
- CGPathNode *dp; //destination node -> it's a neighbour of cp that we consider
|
|
|
- const TerrainTile *ct, *dt; //tile info for both nodes
|
|
|
- const CGObjectInstance *sTileObj;
|
|
|
- ui8 useEmbarkCost; //0 - usual movement; 1 - embark; 2 - disembark
|
|
|
+ CGPathNode * cp; //current (source) path node -> we took it from the queue
|
|
|
+ CGPathNode * dp; //destination node -> it's a neighbour of cp that we consider
|
|
|
+ const TerrainTile * ct, * dt; //tile info for both nodes
|
|
|
+ const CGObjectInstance * ctObj, * dtObj;
|
|
|
+ CGPathNode::ENodeAction destAction;
|
|
|
|
|
|
- void addNeighbours(const int3 &coord);
|
|
|
- void addTeleportExits(bool noTeleportExcludes = false);
|
|
|
+ void addNeighbours();
|
|
|
+ void addTeleportExits();
|
|
|
|
|
|
- bool isMovementPossible(); //checks if current move will be between sea<->land. If so, checks it legality (returns false if movement is not possible) and sets useEmbarkCost
|
|
|
- bool checkDestinationTile();
|
|
|
+ bool isLayerTransitionPossible(const ELayer dstLayer) const;
|
|
|
+ bool isLayerTransitionPossible() const;
|
|
|
+ bool isMovementToDestPossible() const;
|
|
|
+ bool isMovementAfterDestPossible() const;
|
|
|
+ CGPathNode::ENodeAction getDestAction() const;
|
|
|
|
|
|
- int3 getSourceGuardPosition();
|
|
|
- bool isSourceGuarded();
|
|
|
- bool isDestinationGuarded();
|
|
|
- bool isDestinationGuardian();
|
|
|
+ bool isSourceInitialPosition() const;
|
|
|
+ bool isSourceVisitableObj() const;
|
|
|
+ bool isSourceGuarded() const;
|
|
|
+ bool isDestVisitableObj() const;
|
|
|
+ bool isDestinationGuarded(const bool ignoreAccessibility = true) const;
|
|
|
+ bool isDestinationGuardian() const;
|
|
|
|
|
|
void initializeGraph();
|
|
|
|
|
|
- CGPathNode *getNode(const int3 &coord);
|
|
|
- CGPathNode::EAccessibility evaluateAccessibility(const int3 &pos, const TerrainTile *tinfo) const;
|
|
|
- bool canMoveBetween(const int3 &a, const int3 &b) const; //checks only for visitable objects that may make moving between tiles impossible, not other conditions (like tiles itself accessibility)
|
|
|
+ CGPathNode::EAccessibility evaluateAccessibility(const int3 & pos, const TerrainTile * tinfo, const ELayer layer) const;
|
|
|
+ bool isVisitableObj(const CGObjectInstance * obj, const ELayer layer) const;
|
|
|
+ bool canSeeObj(const CGObjectInstance * obj) const;
|
|
|
+ bool canMoveBetween(const int3 & a, const int3 & b) const; //checks only for visitable objects that may make moving between tiles impossible, not other conditions (like tiles itself accessibility)
|
|
|
|
|
|
+ bool isAllowedTeleportEntrance(const CGTeleport * obj) const;
|
|
|
bool addTeleportTwoWay(const CGTeleport * obj) const;
|
|
|
bool addTeleportOneWay(const CGTeleport * obj) const;
|
|
|
bool addTeleportOneWayRandom(const CGTeleport * obj) const;
|
|
|
bool addTeleportWhirlpool(const CGWhirlpool * obj) const;
|
|
|
+
|
|
|
+};
|
|
|
+
|
|
|
+struct DLL_LINKAGE TurnInfo
|
|
|
+{
|
|
|
+ /// This is certainly not the best design ever and certainly can be improved
|
|
|
+ /// Unfortunately for pathfinder that do hundreds of thousands calls onus system add too big overhead
|
|
|
+ struct BonusCache {
|
|
|
+ std::vector<bool> noTerrainPenalty;
|
|
|
+ bool freeShipBoarding;
|
|
|
+ bool flyingMovement;
|
|
|
+ int flyingMovementVal;
|
|
|
+ bool waterWalking;
|
|
|
+ int waterWalkingVal;
|
|
|
+
|
|
|
+ BonusCache(TBonusListPtr bonusList);
|
|
|
+ };
|
|
|
+ unique_ptr<BonusCache> bonusCache;
|
|
|
+
|
|
|
+ const CGHeroInstance * hero;
|
|
|
+ TBonusListPtr bonuses;
|
|
|
+ mutable int maxMovePointsLand;
|
|
|
+ mutable int maxMovePointsWater;
|
|
|
+ int nativeTerrain;
|
|
|
+
|
|
|
+ TurnInfo(const CGHeroInstance * Hero, const int Turn = 0);
|
|
|
+ bool isLayerAvailable(const EPathfindingLayer layer) const;
|
|
|
+ bool hasBonusOfType(const Bonus::BonusType type, const int subtype = -1) const;
|
|
|
+ int valOfBonuses(const Bonus::BonusType type, const int subtype = -1) const;
|
|
|
+ int getMaxMovePoints(const EPathfindingLayer layer) const;
|
|
|
+};
|
|
|
+
|
|
|
+class DLL_LINKAGE CPathfinderHelper
|
|
|
+{
|
|
|
+public:
|
|
|
+ CPathfinderHelper(const CGHeroInstance * Hero, const CPathfinder::PathfinderOptions & Options);
|
|
|
+ void updateTurnInfo(const int turn = 0);
|
|
|
+ bool isLayerAvailable(const EPathfindingLayer layer) const;
|
|
|
+ const TurnInfo * getTurnInfo() const;
|
|
|
+ bool hasBonusOfType(const Bonus::BonusType type, const int subtype = -1) const;
|
|
|
+ int getMaxMovePoints(const EPathfindingLayer layer) const;
|
|
|
+
|
|
|
+ static void getNeighbours(const CMap * map, const TerrainTile & srct, const int3 & tile, std::vector<int3> & vec, const boost::logic::tribool & onLand, const bool limitCoastSailing);
|
|
|
+
|
|
|
+ static int getMovementCost(const CGHeroInstance * h, const int3 & src, const int3 & dst, const TerrainTile * ct, const TerrainTile * dt, const int remainingMovePoints =- 1, const TurnInfo * ti = nullptr, const bool checkLast = true);
|
|
|
+ static int getMovementCost(const CGHeroInstance * h, const int3 & dst);
|
|
|
+
|
|
|
+private:
|
|
|
+ int turn;
|
|
|
+ const CGHeroInstance * hero;
|
|
|
+ std::vector<TurnInfo *> turnsInfo;
|
|
|
+ const CPathfinder::PathfinderOptions & options;
|
|
|
};
|