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"
|
2023-05-24 01:05:59 +02:00
|
|
|
#include "../../../lib/mapping/CMapDefines.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)
|
|
|
|
{
|
2023-01-02 13:27:03 +02:00
|
|
|
logAi->debug("Recalculate paths for %s", hero->getNameTranslated());
|
2021-05-16 19:53:11 +02:00
|
|
|
|
|
|
|
cb->calculatePaths(config);
|
2019-02-09 15:09:21 +02:00
|
|
|
};
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
std::vector<CThreadHelper::Task> calculationTasks;
|
2019-02-09 15:09:21 +02:00
|
|
|
|
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
|
|
|
|