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-09 15:09:21 +02:00
|
|
|
auto calculatePaths = [&](const CGHeroInstance * hero, std::shared_ptr<AIPathfinding::AIPathfinderConfig> config)
|
|
|
|
{
|
|
|
|
logAi->debug("Recalculate paths for %s", hero->name);
|
|
|
|
|
|
|
|
cb->calculatePaths(config, hero);
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<Task> calculationTasks;
|
|
|
|
|
2019-02-06 23:11:51 +02:00
|
|
|
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;
|
2019-02-16 10:33:24 +02:00
|
|
|
nodeStorage->setHero(hero, ai);
|
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-09 15:09:21 +02:00
|
|
|
calculationTasks.push_back(std::bind(calculatePaths, hero.get(), config));
|
|
|
|
}
|
|
|
|
|
|
|
|
int threadsCount = std::min(
|
|
|
|
boost::thread::hardware_concurrency(),
|
|
|
|
(uint32_t)calculationTasks.size());
|
|
|
|
|
2019-02-10 15:25:17 +02:00
|
|
|
if(threadsCount <= 1)
|
2019-02-09 15:09:21 +02:00
|
|
|
{
|
|
|
|
for(auto task : calculationTasks)
|
|
|
|
{
|
|
|
|
task();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CThreadHelper helper(&calculationTasks, threadsCount);
|
|
|
|
|
|
|
|
helper.run();
|
2018-08-12 13:31:31 +02:00
|
|
|
}
|
2019-02-06 23:11:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|