2018-08-12 14:31:31 +03:00
|
|
|
/*
|
2018-12-01 10:30:37 +02:00
|
|
|
* AIPathfinder.cpp, part of VCMI engine
|
2018-08-12 14:31:31 +03: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"
|
2023-05-24 02:05:59 +03:00
|
|
|
#include "../../../lib/mapping/CMapDefines.h"
|
2018-08-12 14:31:31 +03:00
|
|
|
|
2025-03-01 21:47:53 +00:00
|
|
|
#include <tbb/task_group.h>
|
|
|
|
|
2018-08-12 14:31:31 +03:00
|
|
|
std::vector<std::shared_ptr<AINodeStorage>> AIPathfinder::storagePool;
|
|
|
|
std::map<HeroPtr, std::shared_ptr<AINodeStorage>> AIPathfinder::storageMap;
|
|
|
|
|
2018-10-09 22:31:44 +03:00
|
|
|
AIPathfinder::AIPathfinder(CPlayerSpecificInfoCallback * cb, VCAI * ai)
|
|
|
|
:cb(cb), ai(ai)
|
2018-08-12 14:31:31 +03: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-08 00:25:25 +03:00
|
|
|
|
2019-02-06 23:11:51 +02:00
|
|
|
std::vector<AIPath> AIPathfinder::getPathInfo(const HeroPtr & hero, const int3 & tile) const
|
2018-08-12 14:31:31 +03: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 14:31:31 +03:00
|
|
|
|
2019-02-09 15:09:21 +02:00
|
|
|
auto calculatePaths = [&](const CGHeroInstance * hero, std::shared_ptr<AIPathfinding::AIPathfinderConfig> config)
|
|
|
|
{
|
2023-01-02 13:27:03 +02:00
|
|
|
logAi->debug("Recalculate paths for %s", hero->getNameTranslated());
|
2021-05-16 20:53:11 +03:00
|
|
|
|
|
|
|
cb->calculatePaths(config);
|
2019-02-09 15:09:21 +02:00
|
|
|
};
|
|
|
|
|
2025-03-01 21:47:53 +00:00
|
|
|
tbb::task_group calculationTasks;
|
2019-02-09 15:09:21 +02:00
|
|
|
|
2019-02-06 23:11:51 +02:00
|
|
|
for(HeroPtr hero : heroes)
|
2018-08-12 14:31:31 +03:00
|
|
|
{
|
2019-02-06 23:11:51 +02:00
|
|
|
std::shared_ptr<AINodeStorage> nodeStorage;
|
2018-08-12 14:31:31 +03: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 14:31:31 +03:00
|
|
|
|
2025-03-01 21:47:53 +00:00
|
|
|
calculationTasks.run(std::bind(calculatePaths, hero.get(), config));
|
2019-02-09 15:09:21 +02:00
|
|
|
}
|
|
|
|
|
2025-03-01 21:47:53 +00:00
|
|
|
calculationTasks.wait();
|
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
|
|
|
|