AIPathfinder.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * AIPathfinder.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "AIPathfinder.h"
  12. #include "AIPathfinderConfig.h"
  13. #include "../../../CCallback.h"
  14. #include "../../../lib/mapping/CMap.h"
  15. std::shared_ptr<AINodeStorage> AIPathfinder::storage;
  16. AIPathfinder::AIPathfinder(CPlayerSpecificInfoCallback * cb, VCAI * ai)
  17. :cb(cb), ai(ai)
  18. {
  19. }
  20. void AIPathfinder::init()
  21. {
  22. storage.reset();
  23. }
  24. bool AIPathfinder::isTileAccessible(const HeroPtr & hero, const int3 & tile) const
  25. {
  26. return storage->isTileAccessible(hero, tile, EPathfindingLayer::LAND)
  27. || storage->isTileAccessible(hero, tile, EPathfindingLayer::SAIL);
  28. }
  29. std::vector<AIPath> AIPathfinder::getPathInfo(const HeroPtr & hero, const int3 & tile) const
  30. {
  31. const TerrainTile * tileInfo = cb->getTile(tile, false);
  32. if(!tileInfo)
  33. {
  34. return std::vector<AIPath>();
  35. }
  36. return storage->getChainInfo(tile, !tileInfo->isWater());
  37. }
  38. void AIPathfinder::updatePaths(std::vector<HeroPtr> heroes, bool useHeroChain)
  39. {
  40. if(!storage)
  41. {
  42. storage = std::make_shared<AINodeStorage>(cb->getMapSize());
  43. }
  44. logAi->debug("Recalculate all paths");
  45. int pass = 0;
  46. storage->clear();
  47. storage->setHeroes(heroes, ai);
  48. auto config = std::make_shared<AIPathfinding::AIPathfinderConfig>(cb, ai, storage);
  49. do {
  50. logAi->trace("Recalculate paths pass %" PRIi32, pass++);
  51. cb->calculatePaths(config);
  52. logAi->trace("Recalculate chain pass %" PRIi32, pass);
  53. useHeroChain = useHeroChain && storage->calculateHeroChain();
  54. } while(useHeroChain);
  55. }
  56. void AIPathfinder::updatePaths(const HeroPtr & hero)
  57. {
  58. updatePaths(std::vector<HeroPtr>{hero});
  59. }