2021-05-15 18:22:44 +02:00
|
|
|
/*
|
|
|
|
* AIPathfinder.cpp, 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "AIPathfinder.h"
|
|
|
|
#include "AIPathfinderConfig.h"
|
|
|
|
#include "../../../CCallback.h"
|
|
|
|
#include "../../../lib/mapping/CMap.h"
|
|
|
|
|
2021-05-15 18:22:49 +02:00
|
|
|
std::shared_ptr<AINodeStorage> AIPathfinder::storage;
|
2021-05-15 18:22:44 +02:00
|
|
|
|
|
|
|
AIPathfinder::AIPathfinder(CPlayerSpecificInfoCallback * cb, VCAI * ai)
|
|
|
|
:cb(cb), ai(ai)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void AIPathfinder::init()
|
|
|
|
{
|
2021-05-15 18:22:49 +02:00
|
|
|
storage.reset();
|
2021-05-15 18:22:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AIPathfinder::isTileAccessible(const HeroPtr & hero, const int3 & tile) const
|
|
|
|
{
|
2021-05-15 18:22:49 +02:00
|
|
|
return storage->isTileAccessible(hero, tile, EPathfindingLayer::LAND)
|
|
|
|
|| storage->isTileAccessible(hero, tile, EPathfindingLayer::SAIL);
|
2021-05-15 18:22:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<AIPath> AIPathfinder::getPathInfo(const HeroPtr & hero, const int3 & tile) const
|
|
|
|
{
|
|
|
|
const TerrainTile * tileInfo = cb->getTile(tile, false);
|
|
|
|
|
|
|
|
if(!tileInfo)
|
|
|
|
{
|
|
|
|
return std::vector<AIPath>();
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:22:49 +02:00
|
|
|
return storage->getChainInfo(tile, !tileInfo->isWater());
|
2021-05-15 18:22:44 +02:00
|
|
|
}
|
|
|
|
|
2021-05-15 20:01:48 +02:00
|
|
|
void AIPathfinder::updatePaths(std::vector<HeroPtr> heroes, bool useHeroChain)
|
2021-05-15 18:22:44 +02:00
|
|
|
{
|
2021-05-15 18:22:49 +02:00
|
|
|
if(!storage)
|
2021-05-15 18:22:44 +02:00
|
|
|
{
|
2021-05-15 18:22:49 +02:00
|
|
|
storage = std::make_shared<AINodeStorage>(cb->getMapSize());
|
2021-05-15 18:22:44 +02:00
|
|
|
}
|
|
|
|
|
2021-05-15 18:22:49 +02:00
|
|
|
logAi->debug("Recalculate all paths");
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2021-05-15 18:23:01 +02:00
|
|
|
storage->clear();
|
2021-05-15 18:22:49 +02:00
|
|
|
storage->setHeroes(heroes, ai);
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2021-05-15 20:01:48 +02:00
|
|
|
auto config = std::make_shared<AIPathfinding::AIPathfinderConfig>(cb, ai, storage);
|
|
|
|
cb->calculatePaths(config);
|
|
|
|
|
|
|
|
while(useHeroChain && storage->calculateHeroChain())
|
|
|
|
{
|
|
|
|
config = std::make_shared<AIPathfinding::AIPathfinderConfig>(cb, ai, storage);
|
2021-05-15 18:22:49 +02:00
|
|
|
cb->calculatePaths(config);
|
2021-05-15 20:01:48 +02:00
|
|
|
}
|
2021-05-15 18:22:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AIPathfinder::updatePaths(const HeroPtr & hero)
|
|
|
|
{
|
2021-05-15 18:22:49 +02:00
|
|
|
updatePaths(std::vector<HeroPtr>{hero});
|
2021-05-15 18:22:44 +02:00
|
|
|
}
|