AIPathfinder.cpp 1.5 KB

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