1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-16 10:19:47 +02:00
vcmi/AI/VCAI/Pathfinding/AIPathfinder.cpp

116 lines
2.7 KiB
C++
Raw Normal View History

/*
2018-12-01 10:30:37 +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"
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)
{
}
2019-01-05 15:28:22 +02:00
void AIPathfinder::init()
{
storagePool.clear();
storageMap.clear();
}
bool AIPathfinder::isTileAccessible(const HeroPtr & hero, const int3 & tile) const
2018-12-29 15:55:20 +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);
}
std::vector<AIPath> AIPathfinder::getPathInfo(const HeroPtr & hero, const int3 & tile) const
{
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());
}
void AIPathfinder::updatePaths(std::vector<HeroPtr> heroes)
2018-12-29 15:55:20 +02:00
{
storageMap.clear();
// TODO: go parallel?
for(HeroPtr hero : heroes)
{
std::shared_ptr<AINodeStorage> nodeStorage;
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-12-29 15:55:20 +02:00
auto config = std::make_shared<AIPathfinding::AIPathfinderConfig>(cb, ai, nodeStorage);
logAi->debug("Recalculate paths for %s", hero->name);
cb->calculatePaths(config, hero.get());
}
}
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());
}
else
{
nodeStorage = storageMap.at(hero);
}
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