AIPathfinder.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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)
  39. {
  40. if(!storage)
  41. {
  42. storage = std::make_shared<AINodeStorage>(cb->getMapSize());
  43. }
  44. logAi->debug("Recalculate all paths");
  45. storage->clear();
  46. storage->setHeroes(heroes, ai);
  47. auto config = std::make_shared<AIPathfinding::AIPathfinderConfig>(cb, ai, storage);
  48. cb->calculatePaths(config);
  49. }
  50. void AIPathfinder::updatePaths(const HeroPtr & hero)
  51. {
  52. updatePaths(std::vector<HeroPtr>{hero});
  53. }