AIPathfinder.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * AIhelper.h, 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::vector<std::shared_ptr<AINodeStorage>> AIPathfinder::storagePool;
  16. std::map<HeroPtr, std::shared_ptr<AINodeStorage>> AIPathfinder::storageMap;
  17. boost::mutex AIPathfinder::storageMutex;
  18. AIPathfinder::AIPathfinder(CPlayerSpecificInfoCallback * cb, VCAI * ai)
  19. :cb(cb), ai(ai)
  20. {
  21. }
  22. void AIPathfinder::clear()
  23. {
  24. boost::unique_lock<boost::mutex> storageLock(storageMutex);
  25. storageMap.clear();
  26. }
  27. std::vector<AIPath> AIPathfinder::getPathInfo(HeroPtr hero, int3 tile)
  28. {
  29. boost::unique_lock<boost::mutex> storageLock(storageMutex);
  30. std::shared_ptr<AINodeStorage> nodeStorage;
  31. if(!vstd::contains(storageMap, hero))
  32. {
  33. logAi->debug("Recalculate paths for %s", hero->name);
  34. if(storageMap.size() < storagePool.size())
  35. {
  36. nodeStorage = storagePool.at(storageMap.size());
  37. }
  38. else
  39. {
  40. nodeStorage = std::make_shared<AINodeStorage>(cb->getMapSize());
  41. storagePool.push_back(nodeStorage);
  42. }
  43. storageMap[hero] = nodeStorage;
  44. auto config = std::make_shared<AIPathfinding::AIPathfinderConfig>(cb, ai, nodeStorage);
  45. nodeStorage->setHero(hero.get());
  46. cb->calculatePaths(config, hero.get());
  47. }
  48. else
  49. {
  50. nodeStorage = storageMap.at(hero);
  51. }
  52. const TerrainTile * tileInfo = cb->getTile(tile, false);
  53. if(!tileInfo)
  54. {
  55. return std::vector<AIPath>();
  56. }
  57. return nodeStorage->getChainInfo(tile, !tileInfo->isWater());
  58. }