1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-14 10:12:59 +02:00
vcmi/AI/Nullkiller/Pathfinding/AIPathfinder.cpp

97 lines
2.3 KiB
C++
Raw Normal View History

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"
#include "../Engine/Nullkiller.h"
2021-05-15 18:22:44 +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
}
2021-05-15 20:56:31 +02:00
std::vector<AIPath> AIPathfinder::getPathInfo(const int3 & tile) const
2021-05-15 18:22:44 +02:00
{
const TerrainTile * tileInfo = cb->getTile(tile, false);
if(!tileInfo)
{
return std::vector<AIPath>();
}
2021-05-15 18:22:49 +02:00
return storage->getChainInfo(tile, !tileInfo->isWater());
2021-05-15 18:22:44 +02:00
}
void AIPathfinder::updatePaths(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
{
storage.reset(new AINodeStorage(ai, cb->getMapSize()));
2021-05-15 18:22:44 +02:00
}
auto start = boost::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();
storage->setHeroes(heroes);
storage->setScoutTurnDistanceLimit(pathfinderSettings.scoutTurnDistanceLimit);
2021-05-15 18:22:44 +02:00
if(pathfinderSettings.useHeroChain)
2021-05-15 20:54:28 +02:00
{
storage->setTownsAndDwellings(cb->getTownsInfo(), ai->memory->visitableObjs);
2021-05-15 20:54:28 +02:00
}
2021-05-15 20:01:48 +02:00
auto config = std::make_shared<AIPathfinding::AIPathfinderConfig>(cb, ai, storage);
logAi->trace("Recalculate paths pass %d", pass++);
cb->calculatePaths(config);
if(!pathfinderSettings.useHeroChain)
return;
2021-05-15 20:01:48 +02:00
do
{
storage->selectFirstActor();
do
{
while(storage->calculateHeroChain())
{
logAi->trace("Recalculate paths pass %d", pass++);
cb->calculatePaths(config);
}
logAi->trace("Select next actor");
} while(storage->selectNextActor());
if(storage->calculateHeroChainFinal())
{
logAi->trace("Recalculate paths pass final");
cb->calculatePaths(config);
}
} while(storage->increaseHeroChainTurnLimit());
logAi->trace("Recalculated paths in %ld", timeElapsed(start));
}