2018-08-12 13:31:31 +02:00
|
|
|
/*
|
2018-12-01 10:30:37 +02:00
|
|
|
* AIPathfinder.cpp, part of VCMI engine
|
2018-08-12 13:31:31 +02:00
|
|
|
*
|
|
|
|
* 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"
|
2018-10-28 15:03:40 +02:00
|
|
|
#include "../../../lib/mapping/CMap.h"
|
2018-08-12 13:31:31 +02:00
|
|
|
|
|
|
|
std::vector<std::shared_ptr<AINodeStorage>> AIPathfinder::storagePool;
|
|
|
|
std::map<HeroPtr, std::shared_ptr<AINodeStorage>> AIPathfinder::storageMap;
|
|
|
|
|
2018-10-09 21:31:44 +02:00
|
|
|
AIPathfinder::AIPathfinder(CPlayerSpecificInfoCallback * cb, VCAI * ai)
|
|
|
|
:cb(cb), ai(ai)
|
2018-08-12 13:31:31 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-05 15:28:22 +02:00
|
|
|
void AIPathfinder::init()
|
|
|
|
{
|
|
|
|
storagePool.clear();
|
|
|
|
storageMap.clear();
|
|
|
|
}
|
|
|
|
|
2019-02-06 23:11:51 +02:00
|
|
|
bool AIPathfinder::isTileAccessible(const HeroPtr & hero, const int3 & tile) const
|
2018-12-29 15:55:20 +02:00
|
|
|
{
|
2019-02-06 23:11:51 +02:00
|
|
|
std::shared_ptr<const AINodeStorage> nodeStorage = getStorage(hero);
|
2018-12-29 15:55:20 +02:00
|
|
|
|
|
|
|
return nodeStorage->isTileAccessible(tile, EPathfindingLayer::LAND)
|
|
|
|
|| nodeStorage->isTileAccessible(tile, EPathfindingLayer::SAIL);
|
|
|
|
}
|
2019-01-07 23:25:25 +02:00
|
|
|
|
2019-02-06 23:11:51 +02:00
|
|
|
std::vector<AIPath> AIPathfinder::getPathInfo(const HeroPtr & hero, const int3 & tile) const
|
2018-08-12 13:31:31 +02:00
|
|
|
{
|
2019-02-06 23:11:51 +02:00
|
|
|
std::shared_ptr<const AINodeStorage> nodeStorage = getStorage(hero);
|
2018-12-29 15:55:20 +02:00
|
|
|
|
|
|
|
const TerrainTile * tileInfo = cb->getTile(tile, false);
|
|
|
|
|
|
|
|
if(!tileInfo)
|
|
|
|
{
|
|
|
|
return std::vector<AIPath>();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeStorage->getChainInfo(tile, !tileInfo->isWater());
|
|
|
|
}
|
|
|
|
|
2019-02-06 23:11:51 +02:00
|
|
|
void AIPathfinder::updatePaths(std::vector<HeroPtr> heroes)
|
2018-12-29 15:55:20 +02:00
|
|
|
{
|
2019-02-06 23:11:51 +02:00
|
|
|
storageMap.clear();
|
2018-08-12 13:31:31 +02:00
|
|
|
|
2019-02-06 23:11:51 +02:00
|
|
|
// TODO: go parallel?
|
|
|
|
for(HeroPtr hero : heroes)
|
2018-08-12 13:31:31 +02:00
|
|
|
{
|
2019-02-06 23:11:51 +02:00
|
|
|
std::shared_ptr<AINodeStorage> nodeStorage;
|
2018-08-12 13:31:31 +02:00
|
|
|
|
|
|
|
if(storageMap.size() < storagePool.size())
|
|
|
|
{
|
|
|
|
nodeStorage = storagePool.at(storageMap.size());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nodeStorage = std::make_shared<AINodeStorage>(cb->getMapSize());
|
|
|
|
storagePool.push_back(nodeStorage);
|
|
|
|
}
|
|
|
|
|
|
|
|
storageMap[hero] = nodeStorage;
|
2018-12-09 15:20:46 +02:00
|
|
|
nodeStorage->setHero(hero.get());
|
2018-12-29 15:55:20 +02:00
|
|
|
|
2018-11-03 15:25:14 +02:00
|
|
|
auto config = std::make_shared<AIPathfinding::AIPathfinderConfig>(cb, ai, nodeStorage);
|
2018-08-12 13:31:31 +02:00
|
|
|
|
2019-02-06 23:11:51 +02:00
|
|
|
logAi->debug("Recalculate paths for %s", hero->name);
|
2018-08-12 13:31:31 +02:00
|
|
|
cb->calculatePaths(config, hero.get());
|
|
|
|
}
|
2019-02-06 23:11:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AIPathfinder::updatePaths(const HeroPtr & hero)
|
|
|
|
{
|
|
|
|
std::shared_ptr<AINodeStorage> nodeStorage;
|
|
|
|
|
|
|
|
if(!vstd::contains(storageMap, hero))
|
|
|
|
{
|
|
|
|
if(storageMap.size() < storagePool.size())
|
|
|
|
{
|
|
|
|
nodeStorage = storagePool.at(storageMap.size());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nodeStorage = std::make_shared<AINodeStorage>(cb->getMapSize());
|
|
|
|
storagePool.push_back(nodeStorage);
|
|
|
|
}
|
|
|
|
|
|
|
|
storageMap[hero] = nodeStorage;
|
|
|
|
nodeStorage->setHero(hero.get());
|
|
|
|
}
|
2018-08-12 13:31:31 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
nodeStorage = storageMap.at(hero);
|
|
|
|
}
|
|
|
|
|
2019-02-06 23:11:51 +02:00
|
|
|
logAi->debug("Recalculate paths for %s", hero->name);
|
|
|
|
auto config = std::make_shared<AIPathfinding::AIPathfinderConfig>(cb, ai, nodeStorage);
|
|
|
|
|
|
|
|
cb->calculatePaths(config, hero.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<const AINodeStorage> AIPathfinder::getStorage(const HeroPtr & hero) const
|
|
|
|
{
|
|
|
|
return storageMap.at(hero);
|
2018-12-01 10:30:37 +02:00
|
|
|
}
|
2018-12-29 15:55:20 +02:00
|
|
|
|