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

AI: refactor explore further

This commit is contained in:
Andrii Danylchenko
2018-12-29 15:55:20 +02:00
parent edc5abe49d
commit 04047d0a1a
10 changed files with 89 additions and 28 deletions

View File

@@ -189,6 +189,23 @@ bool AINodeStorage::hasBetterChain(const PathNodeInfo & source, CDestinationNode
return false;
}
bool AINodeStorage::isTileAccessible(int3 pos, const EPathfindingLayer layer) const
{
std::vector<AIPath> paths;
auto chains = nodes[pos.x][pos.y][pos.z][layer];
auto initialPos = hero->visitablePos();
for(const AIPathNode & node : chains)
{
if(node.action != CGPathNode::ENodeAction::UNKNOWN)
{
return true;
}
}
return false;
}
std::vector<AIPath> AINodeStorage::getChainInfo(int3 pos, bool isOnLand) const
{
std::vector<AIPath> paths;

View File

@@ -111,6 +111,7 @@ public:
bool hasBetterChain(const PathNodeInfo & source, CDestinationNodeInfo & destination) const;
boost::optional<AIPathNode *> getOrCreateNode(const int3 & coord, const EPathfindingLayer layer, int chainNumber);
std::vector<AIPath> getChainInfo(int3 pos, bool isOnLand) const;
bool isTileAccessible(int3 pos, const EPathfindingLayer layer) const;
void setHero(HeroPtr heroPtr)
{

View File

@@ -28,9 +28,33 @@ void AIPathfinder::clear()
storageMap.clear();
}
bool AIPathfinder::isTileAccessible(HeroPtr hero, int3 tile)
{
boost::unique_lock<boost::mutex> storageLock(storageMutex);
std::shared_ptr<AINodeStorage> nodeStorage = getOrCreateStorage(hero);
return nodeStorage->isTileAccessible(tile, EPathfindingLayer::LAND)
|| nodeStorage->isTileAccessible(tile, EPathfindingLayer::SAIL);
}
std::vector<AIPath> AIPathfinder::getPathInfo(HeroPtr hero, int3 tile)
{
boost::unique_lock<boost::mutex> storageLock(storageMutex);
std::shared_ptr<AINodeStorage> nodeStorage = getOrCreateStorage(hero);
const TerrainTile * tileInfo = cb->getTile(tile, false);
if(!tileInfo)
{
return std::vector<AIPath>();
}
return nodeStorage->getChainInfo(tile, !tileInfo->isWater());
}
std::shared_ptr<AINodeStorage> AIPathfinder::getOrCreateStorage(HeroPtr hero)
{
std::shared_ptr<AINodeStorage> nodeStorage;
if(!vstd::contains(storageMap, hero))
@@ -49,7 +73,7 @@ std::vector<AIPath> AIPathfinder::getPathInfo(HeroPtr hero, int3 tile)
storageMap[hero] = nodeStorage;
nodeStorage->setHero(hero.get());
auto config = std::make_shared<AIPathfinding::AIPathfinderConfig>(cb, ai, nodeStorage);
cb->calculatePaths(config, hero.get());
@@ -59,12 +83,6 @@ std::vector<AIPath> AIPathfinder::getPathInfo(HeroPtr hero, int3 tile)
nodeStorage = storageMap.at(hero);
}
const TerrainTile * tileInfo = cb->getTile(tile, false);
if(!tileInfo)
{
return std::vector<AIPath>();
}
return nodeStorage->getChainInfo(tile, !tileInfo->isWater());
return nodeStorage;
}

View File

@@ -26,5 +26,7 @@ private:
public:
AIPathfinder(CPlayerSpecificInfoCallback * cb, VCAI * ai);
std::vector<AIPath> getPathInfo(HeroPtr hero, int3 tile);
bool isTileAccessible(HeroPtr hero, int3 tile);
std::shared_ptr<AINodeStorage> getOrCreateStorage(HeroPtr hero);
void clear();
};

View File

@@ -104,6 +104,11 @@ std::vector<AIPath> PathfindingManager::getPathsToTile(HeroPtr hero, int3 tile)
return pathfinder->getPathInfo(hero, tile);
}
bool PathfindingManager::isTileAccessible(HeroPtr hero, int3 tile)
{
return pathfinder->isTileAccessible(hero, tile);
}
Goals::TGoalVec PathfindingManager::findPath(
HeroPtr hero,
crint3 dest,

View File

@@ -26,6 +26,7 @@ public:
virtual Goals::TGoalVec howToVisitTile(int3 tile) = 0;
virtual Goals::TGoalVec howToVisitObj(ObjectIdRef obj) = 0;
virtual std::vector<AIPath> getPathsToTile(HeroPtr hero, int3 tile) = 0;
virtual bool isTileAccessible(HeroPtr hero, int3 tile) = 0;
};
class PathfindingManager : public IPathfindingManager
@@ -46,6 +47,7 @@ public:
Goals::TGoalVec howToVisitTile(int3 tile) override;
Goals::TGoalVec howToVisitObj(ObjectIdRef obj) override;
std::vector<AIPath> getPathsToTile(HeroPtr hero, int3 tile) override;
bool isTileAccessible(HeroPtr hero, int3 tile) override;
void resetPaths() override;
private: