1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-06 09:09:40 +02:00

AI pathfinding: rename and add const to functions

This commit is contained in:
Andrii Danylchenko
2018-10-07 14:51:27 +03:00
parent cf213a5acf
commit f327c46fa3
16 changed files with 198 additions and 220 deletions

View File

@@ -21,17 +21,19 @@ AINodeStorage::~AINodeStorage()
{
}
AIPathNode * AINodeStorage::getAINode(CPathNodeInfo & nodeInfo) const
const AIPathNode * AINodeStorage::getAINode(const CGPathNode * node) const
{
return static_cast<AIPathNode *>(nodeInfo.node);
return static_cast<const AIPathNode *>(node);
}
AIPathNode * AINodeStorage::getAINode(CGPathNode * node) const
void AINodeStorage::updateAINode(CGPathNode * node, std::function<void(AIPathNode *)> updater)
{
return static_cast<AIPathNode *>(node);
auto aiNode = static_cast<AIPathNode *>(node);
updater(aiNode);
}
bool AINodeStorage::isBattleNode(CGPathNode * node) const
bool AINodeStorage::isBattleNode(const CGPathNode * node) const
{
return getAINode(node)->chainMask & BATTLE_CHAIN > 0;
}
@@ -64,25 +66,26 @@ void AINodeStorage::resetTile(const int3 & coord, EPathfindingLayer layer, CGPat
}
}
void AINodeStorage::commit(CDestinationNodeInfo & destination, CPathNodeInfo & source)
void AINodeStorage::commit(CDestinationNodeInfo & destination, const PathNodeInfo & source)
{
auto dstNode = getAINode(destination);
auto srcNode = getAINode(source);
const AIPathNode * srcNode = getAINode(source.node);
dstNode->moveRemains = destination.movementLeft;
dstNode->turns = destination.turn;
dstNode->danger = srcNode->danger;
dstNode->action = destination.action;
dstNode->theNodeBefore = srcNode->theNodeBefore;
updateAINode(destination.node, [&](AIPathNode * dstNode) {
dstNode->moveRemains = destination.movementLeft;
dstNode->turns = destination.turn;
dstNode->danger = srcNode->danger;
dstNode->action = destination.action;
dstNode->theNodeBefore = srcNode->theNodeBefore;
});
}
std::vector<CGPathNode *> AINodeStorage::calculateNeighbours(
CPathNodeInfo & source,
CPathfinderConfig * pathfinderConfig,
CPathfinderHelper * pathfinderHelper)
const PathNodeInfo & source,
const PathfinderConfig * pathfinderConfig,
const CPathfinderHelper * pathfinderHelper)
{
std::vector<CGPathNode *> neighbours;
auto srcNode = getAINode(source);
const AIPathNode * srcNode = getAINode(source.node);
auto accessibleNeighbourTiles = pathfinderHelper->getNeighbourTiles(source);
for(auto & neighbour : accessibleNeighbourTiles)
@@ -102,13 +105,13 @@ std::vector<CGPathNode *> AINodeStorage::calculateNeighbours(
}
std::vector<CGPathNode *> AINodeStorage::calculateTeleportations(
CPathNodeInfo & source,
CPathfinderConfig * pathfinderConfig,
CPathfinderHelper * pathfinderHelper)
const PathNodeInfo & source,
const PathfinderConfig * pathfinderConfig,
const CPathfinderHelper * pathfinderHelper)
{
std::vector<CGPathNode *> neighbours;
auto accessibleExits = pathfinderHelper->getTeleportExits(source);
auto srcNode = getAINode(source);
auto srcNode = getAINode(source.node);
for(auto & neighbour : accessibleExits)
{
@@ -120,11 +123,11 @@ std::vector<CGPathNode *> AINodeStorage::calculateTeleportations(
return neighbours;
}
bool AINodeStorage::hasBetterChain(CPathNodeInfo & source, CDestinationNodeInfo & destination) const
bool AINodeStorage::hasBetterChain(const PathNodeInfo & source, CDestinationNodeInfo & destination) const
{
auto pos = destination.coord;
auto chains = nodes[pos.x][pos.y][pos.z][EPathfindingLayer::LAND];
auto destinationNode = getAINode(destination);
auto destinationNode = getAINode(destination.node);
for(const AIPathNode & node : chains)
{
@@ -154,7 +157,7 @@ bool AINodeStorage::hasBetterChain(CPathNodeInfo & source, CDestinationNodeInfo
return false;
}
std::vector<AIPath> AINodeStorage::getChainInfo(int3 pos)
std::vector<AIPath> AINodeStorage::getChainInfo(int3 pos) const
{
std::vector<AIPath> paths;
auto chains = nodes[pos.x][pos.y][pos.z][EPathfindingLayer::LAND];

View File

@@ -74,23 +74,24 @@ public:
virtual void resetTile(const int3 & tile, EPathfindingLayer layer, CGPathNode::EAccessibility accessibility) override;
virtual std::vector<CGPathNode *> calculateNeighbours(
CPathNodeInfo & source,
CPathfinderConfig * pathfinderConfig,
CPathfinderHelper * pathfinderHelper) override;
const PathNodeInfo & source,
const PathfinderConfig * pathfinderConfig,
const CPathfinderHelper * pathfinderHelper) override;
virtual std::vector<CGPathNode *> calculateTeleportations(
CPathNodeInfo & source,
CPathfinderConfig * pathfinderConfig,
CPathfinderHelper * pathfinderHelper) override;
const PathNodeInfo & source,
const PathfinderConfig * pathfinderConfig,
const CPathfinderHelper * pathfinderHelper) override;
virtual void commit(CDestinationNodeInfo & destination, CPathNodeInfo & source) override;
virtual void commit(CDestinationNodeInfo & destination, const PathNodeInfo & source) override;
AIPathNode * getAINode(CPathNodeInfo & nodeInfo) const;
AIPathNode * getAINode(CGPathNode * node) const;
bool isBattleNode(CGPathNode * node) const;
bool hasBetterChain(CPathNodeInfo & source, CDestinationNodeInfo & destination) const;
const AIPathNode * getAINode(const CGPathNode * node) const;
void updateAINode(CGPathNode * node, std::function<void (AIPathNode *)> updater);
bool isBattleNode(const CGPathNode * node) const;
bool hasBetterChain(const PathNodeInfo & source, CDestinationNodeInfo & destination) const;
AIPathNode * getNode(const int3 & coord, const EPathfindingLayer layer, int chainNumber);
std::vector<AIPath> getChainInfo(int3 pos);
std::vector<AIPath> getChainInfo(int3 pos) const;
void setHero(HeroPtr heroPtr)
{

View File

@@ -11,30 +11,7 @@
#include "AIPathfinderConfig.h"
#include "../../../CCallback.h"
class AILayerTransitionRule : public CLayerTransitionRule
{
public:
virtual void process(
CPathNodeInfo & source,
CDestinationNodeInfo & destination,
CPathfinderConfig * pathfinderConfig,
CPathfinderHelper * pathfinderHelper) const override
{
CLayerTransitionRule::process(source, destination, pathfinderConfig, pathfinderHelper);
if(!destination.blocked)
{
return;
}
if(source.node->layer == EPathfindingLayer::LAND && destination.node->layer == EPathfindingLayer::SAIL)
{
logAi->debug("Check virtual boat!");
}
}
};
class AIMovementAfterDestinationRule : public CMovementAfterDestinationRule
class AIMovementAfterDestinationRule : public MovementAfterDestinationRule
{
private:
CPlayerSpecificInfoCallback * cb;
@@ -47,9 +24,9 @@ public:
}
virtual void process(
CPathNodeInfo & source,
const PathNodeInfo & source,
CDestinationNodeInfo & destination,
CPathfinderConfig * pathfinderConfig,
const PathfinderConfig * pathfinderConfig,
CPathfinderHelper * pathfinderHelper) const override
{
if(nodeStorage->hasBetterChain(source, destination))
@@ -64,7 +41,7 @@ public:
if(blocker == BlockingReason::NONE)
return;
auto srcNode = nodeStorage->getAINode(source);
auto srcNode = nodeStorage->getAINode(source.node);
if(blocker == BlockingReason::DESTINATION_BLOCKVIS && destination.nodeObject)
{
@@ -113,7 +90,7 @@ public:
return;
}
auto destNode = nodeStorage->getAINode(destination);
auto destNode = nodeStorage->getAINode(destination.node);
auto battleNode = nodeStorage->getNode(destination.coord, destination.node->layer, destNode->chainMask | AINodeStorage::BATTLE_CHAIN);
if(battleNode->locked)
@@ -152,7 +129,7 @@ public:
}
};
class AIMovementToDestinationRule : public CMovementToDestinationRule
class AIMovementToDestinationRule : public MovementToDestinationRule
{
private:
CPlayerSpecificInfoCallback * cb;
@@ -165,9 +142,9 @@ public:
}
virtual void process(
CPathNodeInfo & source,
const PathNodeInfo & source,
CDestinationNodeInfo & destination,
CPathfinderConfig * pathfinderConfig,
const PathfinderConfig * pathfinderConfig,
CPathfinderHelper * pathfinderHelper) const override
{
auto blocker = getBlockingReason(source, destination, pathfinderConfig, pathfinderHelper);
@@ -207,7 +184,7 @@ public:
}
};
class AIPreviousNodeRule : public CMovementToDestinationRule
class AIPreviousNodeRule : public MovementToDestinationRule
{
private:
CPlayerSpecificInfoCallback * cb;
@@ -220,9 +197,9 @@ public:
}
virtual void process(
CPathNodeInfo & source,
const PathNodeInfo & source,
CDestinationNodeInfo & destination,
CPathfinderConfig * pathfinderConfig,
const PathfinderConfig * pathfinderConfig,
CPathfinderHelper * pathfinderHelper) const override
{
auto blocker = getBlockingReason(source, destination, pathfinderConfig, pathfinderHelper);
@@ -261,10 +238,10 @@ std::vector<std::shared_ptr<IPathfindingRule>> makeRuleset(
std::shared_ptr<AINodeStorage> nodeStorage)
{
std::vector<std::shared_ptr<IPathfindingRule>> rules = {
std::make_shared<AILayerTransitionRule>(),
std::make_shared<CDestinationActionRule>(),
std::make_shared<LayerTransitionRule>(),
std::make_shared<DestinationActionRule>(),
std::make_shared<AIMovementToDestinationRule>(cb, nodeStorage),
std::make_shared<CMovementCostRule>(),
std::make_shared<MovementCostRule>(),
std::make_shared<AIPreviousNodeRule>(cb, nodeStorage),
std::make_shared<AIMovementAfterDestinationRule>(cb, nodeStorage)
};
@@ -275,6 +252,6 @@ std::vector<std::shared_ptr<IPathfindingRule>> makeRuleset(
AIPathfinderConfig::AIPathfinderConfig(
CPlayerSpecificInfoCallback * cb,
std::shared_ptr<AINodeStorage> nodeStorage)
:CPathfinderConfig(nodeStorage, makeRuleset(cb, nodeStorage))
:PathfinderConfig(nodeStorage, makeRuleset(cb, nodeStorage))
{
}

View File

@@ -12,7 +12,7 @@
#include "AINodeStorage.h"
class AIPathfinderConfig : public CPathfinderConfig
class AIPathfinderConfig : public PathfinderConfig
{
public:
AIPathfinderConfig(CPlayerSpecificInfoCallback * cb, std::shared_ptr<AINodeStorage> nodeStorage);

View File

@@ -8,29 +8,29 @@
*
*/
#include "StdInc.h"
#include "CPathfindingManager.h"
#include "PathfindingManager.h"
#include "AIPathfinder.h"
#include "AIPathfinderConfig.h"
#include "../../../lib/CGameInfoCallback.h"
#include "../../../lib/mapping/CMap.h"
CPathfindingManager::CPathfindingManager(CPlayerSpecificInfoCallback * CB, VCAI * AI)
PathfindingManager::PathfindingManager(CPlayerSpecificInfoCallback * CB, VCAI * AI)
: ai(AI), cb(CB)
{
}
void CPathfindingManager::setCB(CPlayerSpecificInfoCallback * CB)
void PathfindingManager::setCB(CPlayerSpecificInfoCallback * CB)
{
cb = CB;
pathfinder.reset(new AIPathfinder(cb));
}
void CPathfindingManager::setAI(VCAI * AI)
void PathfindingManager::setAI(VCAI * AI)
{
ai = AI;
}
Goals::TGoalVec CPathfindingManager::howToVisitTile(int3 tile)
Goals::TGoalVec PathfindingManager::howToVisitTile(int3 tile)
{
Goals::TGoalVec result;
@@ -44,7 +44,7 @@ Goals::TGoalVec CPathfindingManager::howToVisitTile(int3 tile)
return result;
}
Goals::TGoalVec CPathfindingManager::howToVisitObj(ObjectIdRef obj)
Goals::TGoalVec PathfindingManager::howToVisitObj(ObjectIdRef obj)
{
Goals::TGoalVec result;
@@ -58,7 +58,7 @@ Goals::TGoalVec CPathfindingManager::howToVisitObj(ObjectIdRef obj)
return result;
}
Goals::TGoalVec CPathfindingManager::howToVisitTile(HeroPtr hero, int3 tile, bool allowGatherArmy)
Goals::TGoalVec PathfindingManager::howToVisitTile(HeroPtr hero, int3 tile, bool allowGatherArmy)
{
return findPath(hero, tile, allowGatherArmy, [&](int3 firstTileToGet) -> Goals::TSubgoal
{
@@ -66,7 +66,7 @@ Goals::TGoalVec CPathfindingManager::howToVisitTile(HeroPtr hero, int3 tile, boo
});
}
Goals::TGoalVec CPathfindingManager::howToVisitObj(HeroPtr hero, ObjectIdRef obj, bool allowGatherArmy)
Goals::TGoalVec PathfindingManager::howToVisitObj(HeroPtr hero, ObjectIdRef obj, bool allowGatherArmy)
{
if(!obj)
{
@@ -81,12 +81,12 @@ Goals::TGoalVec CPathfindingManager::howToVisitObj(HeroPtr hero, ObjectIdRef obj
});
}
std::vector<AIPath> CPathfindingManager::getPathsToTile(HeroPtr hero, int3 tile)
std::vector<AIPath> PathfindingManager::getPathsToTile(HeroPtr hero, int3 tile)
{
return pathfinder->getPathInfo(hero, tile);
}
Goals::TGoalVec CPathfindingManager::findPath(
Goals::TGoalVec PathfindingManager::findPath(
HeroPtr hero,
crint3 dest,
bool allowGatherArmy,
@@ -141,7 +141,7 @@ Goals::TGoalVec CPathfindingManager::findPath(
return result;
}
Goals::TSubgoal CPathfindingManager::selectVisitingGoal(HeroPtr hero, ObjectIdRef obj) const
Goals::TSubgoal PathfindingManager::selectVisitingGoal(HeroPtr hero, ObjectIdRef obj) const
{
int3 dest = obj->visitablePos();
@@ -161,7 +161,7 @@ Goals::TSubgoal CPathfindingManager::selectVisitingGoal(HeroPtr hero, ObjectIdRe
return sptr(Goals::VisitTile(dest).sethero(hero).setisAbstract(true));
}
Goals::TSubgoal CPathfindingManager::clearWayTo(HeroPtr hero, int3 firstTileToGet)
Goals::TSubgoal PathfindingManager::clearWayTo(HeroPtr hero, int3 firstTileToGet)
{
if(isBlockedBorderGate(firstTileToGet))
{
@@ -207,7 +207,7 @@ Goals::TSubgoal CPathfindingManager::clearWayTo(HeroPtr hero, int3 firstTileToGe
return sptr(Goals::VisitTile(firstTileToGet).sethero(hero).setisAbstract(true));
}
void CPathfindingManager::resetPaths()
void PathfindingManager::resetPaths()
{
logAi->debug("AIPathfinder has been reseted.");
pathfinder->clear();

View File

@@ -13,7 +13,7 @@
#include "VCAI.h"
#include "AINodeStorage.h"
class IPathfindingManager // : pulbic IAbstractManager
class IPathfindingManager
{
public:
virtual ~IPathfindingManager() = default;
@@ -28,7 +28,7 @@ public:
virtual std::vector<AIPath> getPathsToTile(HeroPtr hero, int3 tile) = 0;
};
class CPathfindingManager : public IPathfindingManager
class PathfindingManager : public IPathfindingManager
{
friend class AIhelper;
@@ -38,8 +38,8 @@ private:
std::unique_ptr<AIPathfinder> pathfinder;
public:
CPathfindingManager() = default;
CPathfindingManager(CPlayerSpecificInfoCallback * CB, VCAI * AI = nullptr); //for tests only
PathfindingManager() = default;
PathfindingManager(CPlayerSpecificInfoCallback * CB, VCAI * AI = nullptr); //for tests only
Goals::TGoalVec howToVisitTile(HeroPtr hero, int3 tile, bool allowGatherArmy = true) override;
Goals::TGoalVec howToVisitObj(HeroPtr hero, ObjectIdRef obj, bool allowGatherArmy = true) override;