| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- /*
- * 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::shared_ptr<AINodeStorage> AIPathfinder::storage;
- AIPathfinder::AIPathfinder(CPlayerSpecificInfoCallback * cb, VCAI * ai)
- :cb(cb), ai(ai)
- {
- }
- void AIPathfinder::init()
- {
- storage.reset();
- }
- bool AIPathfinder::isTileAccessible(const HeroPtr & hero, const int3 & tile) const
- {
- return storage->isTileAccessible(hero, tile, EPathfindingLayer::LAND)
- || storage->isTileAccessible(hero, tile, EPathfindingLayer::SAIL);
- }
- std::vector<AIPath> AIPathfinder::getPathInfo(const int3 & tile) const
- {
- const TerrainTile * tileInfo = cb->getTile(tile, false);
- if(!tileInfo)
- {
- return std::vector<AIPath>();
- }
- return storage->getChainInfo(tile, !tileInfo->isWater());
- }
- void AIPathfinder::updatePaths(std::vector<HeroPtr> heroes, bool useHeroChain)
- {
- if(!storage)
- {
- storage = std::make_shared<AINodeStorage>(cb->getMapSize());
- }
- logAi->debug("Recalculate all paths");
- int pass = 0;
- storage->clear();
- storage->setHeroes(heroes, ai);
- if(useHeroChain)
- {
- storage->setTownsAndDwellings(cb->getTownsInfo(), ai->visitableObjs);
- }
- auto config = std::make_shared<AIPathfinding::AIPathfinderConfig>(cb, ai, storage);
- logAi->trace("Recalculate paths pass %d", pass++);
- cb->calculatePaths(config);
- if(!useHeroChain)
- return;
- 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());
- }
|