2021-05-15 18:22:44 +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"
|
2020-05-04 17:58:43 +02:00
|
|
|
#include "../Engine/Nullkiller.h"
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2022-09-26 20:01:07 +02:00
|
|
|
namespace NKAI
|
|
|
|
{
|
|
|
|
|
2024-03-31 17:39:00 +02:00
|
|
|
std::map<ObjectInstanceID, std::unique_ptr<GraphPaths>> AIPathfinder::heroGraphs;
|
|
|
|
|
2020-05-04 17:58:43 +02:00
|
|
|
AIPathfinder::AIPathfinder(CPlayerSpecificInfoCallback * cb, Nullkiller * ai)
|
2021-05-15 18:22:44 +02:00
|
|
|
:cb(cb), ai(ai)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void AIPathfinder::init()
|
|
|
|
{
|
2021-05-15 18:22:49 +02:00
|
|
|
storage.reset();
|
2021-05-15 18:22:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AIPathfinder::isTileAccessible(const HeroPtr & hero, const int3 & tile) const
|
|
|
|
{
|
2021-05-15 18:22:49 +02:00
|
|
|
return storage->isTileAccessible(hero, tile, EPathfindingLayer::LAND)
|
|
|
|
|| storage->isTileAccessible(hero, tile, EPathfindingLayer::SAIL);
|
2021-05-15 18:22:44 +02:00
|
|
|
}
|
|
|
|
|
2024-03-24 09:32:29 +02:00
|
|
|
void AIPathfinder::calculateQuickPathsWithBlocker(std::vector<AIPath> & result, const std::vector<const CGHeroInstance *> & heroes, const int3 & tile)
|
|
|
|
{
|
|
|
|
result.clear();
|
|
|
|
|
|
|
|
for(auto hero : heroes)
|
|
|
|
{
|
|
|
|
auto graph = heroGraphs.find(hero->id);
|
|
|
|
|
|
|
|
if(graph != heroGraphs.end())
|
|
|
|
graph->second->quickAddChainInfoWithBlocker(result, tile, hero, ai);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AIPathfinder::calculatePathInfo(std::vector<AIPath> & result, const int3 & tile, bool includeGraph) const
|
2021-05-15 18:22:44 +02:00
|
|
|
{
|
|
|
|
const TerrainTile * tileInfo = cb->getTile(tile, false);
|
|
|
|
|
2024-03-24 09:32:29 +02:00
|
|
|
result.clear();
|
|
|
|
|
2021-05-15 18:22:44 +02:00
|
|
|
if(!tileInfo)
|
|
|
|
{
|
2024-03-24 09:32:29 +02:00
|
|
|
return;
|
2021-05-15 18:22:44 +02:00
|
|
|
}
|
|
|
|
|
2024-03-24 09:32:29 +02:00
|
|
|
storage->calculateChainInfo(result, tile, !tileInfo->isWater());
|
2024-01-20 22:54:30 +02:00
|
|
|
|
|
|
|
if(includeGraph)
|
|
|
|
{
|
|
|
|
for(auto hero : cb->getHeroesInfo())
|
|
|
|
{
|
2024-02-03 12:20:59 +02:00
|
|
|
auto graph = heroGraphs.find(hero->id);
|
2024-01-20 22:54:30 +02:00
|
|
|
|
2024-02-03 12:20:59 +02:00
|
|
|
if(graph != heroGraphs.end())
|
2024-03-24 09:32:29 +02:00
|
|
|
graph->second->addChainInfo(result, tile, hero, ai);
|
2024-01-20 22:54:30 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-15 18:22:44 +02:00
|
|
|
}
|
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
void AIPathfinder::updatePaths(const std::map<const CGHeroInstance *, HeroRole> & heroes, PathfinderSettings pathfinderSettings)
|
2021-05-15 18:22:44 +02:00
|
|
|
{
|
2021-05-15 18:22:49 +02:00
|
|
|
if(!storage)
|
2021-05-15 18:22:44 +02:00
|
|
|
{
|
2020-05-04 17:58:43 +02:00
|
|
|
storage.reset(new AINodeStorage(ai, cb->getMapSize()));
|
2021-05-15 18:22:44 +02:00
|
|
|
}
|
|
|
|
|
2021-11-23 08:41:03 +02:00
|
|
|
auto start = std::chrono::high_resolution_clock::now();
|
2021-05-15 18:22:49 +02:00
|
|
|
logAi->debug("Recalculate all paths");
|
2021-05-15 20:04:48 +02:00
|
|
|
int pass = 0;
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2021-05-15 18:23:01 +02:00
|
|
|
storage->clear();
|
2020-05-04 17:58:43 +02:00
|
|
|
storage->setHeroes(heroes);
|
2021-05-16 13:56:21 +02:00
|
|
|
storage->setScoutTurnDistanceLimit(pathfinderSettings.scoutTurnDistanceLimit);
|
2021-05-16 14:01:34 +02:00
|
|
|
storage->setMainTurnDistanceLimit(pathfinderSettings.mainTurnDistanceLimit);
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2023-07-25 16:26:35 +02:00
|
|
|
logAi->trace(
|
|
|
|
"Scout turn distance: %s, main %s",
|
|
|
|
std::to_string(pathfinderSettings.scoutTurnDistanceLimit),
|
|
|
|
std::to_string(pathfinderSettings.mainTurnDistanceLimit));
|
|
|
|
|
2021-05-16 13:56:21 +02:00
|
|
|
if(pathfinderSettings.useHeroChain)
|
2021-05-15 20:54:28 +02:00
|
|
|
{
|
2020-05-04 17:58:43 +02:00
|
|
|
storage->setTownsAndDwellings(cb->getTownsInfo(), ai->memory->visitableObjs);
|
2021-05-15 20:54:28 +02:00
|
|
|
}
|
|
|
|
|
2024-01-20 22:54:30 +02:00
|
|
|
auto config = std::make_shared<AIPathfinding::AIPathfinderConfig>(cb, ai, storage, pathfinderSettings.allowBypassObjects);
|
2021-05-16 13:15:12 +02:00
|
|
|
|
|
|
|
logAi->trace("Recalculate paths pass %d", pass++);
|
|
|
|
cb->calculatePaths(config);
|
|
|
|
|
2021-05-16 13:56:21 +02:00
|
|
|
if(!pathfinderSettings.useHeroChain)
|
2024-04-14 14:23:44 +02:00
|
|
|
{
|
|
|
|
logAi->trace("Recalculated paths in %ld", timeElapsed(start));
|
|
|
|
|
2021-05-16 13:15:12 +02:00
|
|
|
return;
|
2024-04-14 14:23:44 +02:00
|
|
|
}
|
2021-05-15 20:01:48 +02:00
|
|
|
|
2021-05-16 13:13:56 +02:00
|
|
|
do
|
|
|
|
{
|
2021-05-16 13:15:12 +02:00
|
|
|
storage->selectFirstActor();
|
|
|
|
|
2021-05-16 13:15:03 +02:00
|
|
|
do
|
2021-05-16 13:13:56 +02:00
|
|
|
{
|
2022-12-14 22:13:26 +02:00
|
|
|
boost::this_thread::interruption_point();
|
|
|
|
|
2021-05-16 13:15:12 +02:00
|
|
|
while(storage->calculateHeroChain())
|
|
|
|
{
|
2021-05-16 13:57:33 +02:00
|
|
|
boost::this_thread::interruption_point();
|
|
|
|
|
2021-05-16 13:15:12 +02:00
|
|
|
logAi->trace("Recalculate paths pass %d", pass++);
|
|
|
|
cb->calculatePaths(config);
|
|
|
|
}
|
2021-05-16 13:13:56 +02:00
|
|
|
|
2021-05-16 13:15:12 +02:00
|
|
|
logAi->trace("Select next actor");
|
|
|
|
} while(storage->selectNextActor());
|
2021-05-16 13:13:56 +02:00
|
|
|
|
2022-12-14 22:13:26 +02:00
|
|
|
boost::this_thread::interruption_point();
|
|
|
|
|
2021-05-16 13:15:03 +02:00
|
|
|
if(storage->calculateHeroChainFinal())
|
|
|
|
{
|
2021-05-16 13:57:33 +02:00
|
|
|
boost::this_thread::interruption_point();
|
|
|
|
|
2021-05-16 13:15:03 +02:00
|
|
|
logAi->trace("Recalculate paths pass final");
|
|
|
|
cb->calculatePaths(config);
|
2021-05-16 13:13:56 +02:00
|
|
|
}
|
2021-05-16 13:15:12 +02:00
|
|
|
} while(storage->increaseHeroChainTurnLimit());
|
2021-05-16 13:56:13 +02:00
|
|
|
|
|
|
|
logAi->trace("Recalculated paths in %ld", timeElapsed(start));
|
2019-03-17 23:27:09 +02:00
|
|
|
}
|
2022-09-26 20:01:07 +02:00
|
|
|
|
2024-03-29 20:39:03 +02:00
|
|
|
void AIPathfinder::updateGraphs(
|
|
|
|
const std::map<const CGHeroInstance *, HeroRole> & heroes,
|
|
|
|
uint8_t mainScanDepth,
|
|
|
|
uint8_t scoutScanDepth)
|
2024-01-20 22:54:30 +02:00
|
|
|
{
|
|
|
|
auto start = std::chrono::high_resolution_clock::now();
|
|
|
|
std::vector<const CGHeroInstance *> heroesVector;
|
|
|
|
|
|
|
|
heroGraphs.clear();
|
|
|
|
|
|
|
|
for(auto hero : heroes)
|
|
|
|
{
|
2024-03-24 09:32:29 +02:00
|
|
|
if(heroGraphs.try_emplace(hero.first->id).second)
|
|
|
|
{
|
|
|
|
heroGraphs[hero.first->id] = std::make_unique<GraphPaths>();
|
2024-03-09 16:20:00 +02:00
|
|
|
heroesVector.push_back(hero.first);
|
2024-03-24 09:32:29 +02:00
|
|
|
}
|
2024-01-20 22:54:30 +02:00
|
|
|
}
|
|
|
|
|
2024-03-29 20:39:03 +02:00
|
|
|
tbb::parallel_for(tbb::blocked_range<size_t>(0, heroesVector.size()), [this, &heroesVector, &heroes, mainScanDepth, scoutScanDepth](const tbb::blocked_range<size_t> & r)
|
2024-01-20 22:54:30 +02:00
|
|
|
{
|
|
|
|
for(auto i = r.begin(); i != r.end(); i++)
|
2024-03-29 20:39:03 +02:00
|
|
|
{
|
|
|
|
auto role = heroes.at(heroesVector[i]);
|
|
|
|
auto scanLimit = role == HeroRole::MAIN ? mainScanDepth : scoutScanDepth;
|
|
|
|
|
|
|
|
heroGraphs.at(heroesVector[i]->id)->calculatePaths(heroesVector[i], ai, scanLimit);
|
|
|
|
}
|
2024-01-20 22:54:30 +02:00
|
|
|
});
|
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
if(NKAI_GRAPH_TRACE_LEVEL >= 1)
|
2024-01-20 22:54:30 +02:00
|
|
|
{
|
2024-03-09 16:20:00 +02:00
|
|
|
for(auto hero : heroes)
|
|
|
|
{
|
2024-03-24 09:32:29 +02:00
|
|
|
heroGraphs[hero.first->id]->dumpToLog();
|
2024-03-09 16:20:00 +02:00
|
|
|
}
|
2024-01-20 22:54:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
logAi->trace("Graph paths updated in %lld", timeElapsed(start));
|
|
|
|
}
|
|
|
|
|
2022-09-26 20:01:07 +02:00
|
|
|
}
|