PathfinderCache.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * PathfinderCache.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 "PathfinderCache.h"
  12. #include "CGPathNode.h"
  13. #include "PathfinderOptions.h"
  14. #include "../CGameInfoCallback.h"
  15. #include "../mapObjects/CGHeroInstance.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. std::shared_ptr<PathfinderConfig> PathfinderCache::createConfig(const CGHeroInstance * h, CPathsInfo & out)
  18. {
  19. auto config = std::make_shared<SingleHeroPathfinderConfig>(out, cb, h);
  20. config->options = options;
  21. return config;
  22. }
  23. std::shared_ptr<CPathsInfo> PathfinderCache::buildPaths(const CGHeroInstance * h)
  24. {
  25. std::shared_ptr<CPathsInfo> result = std::make_shared<CPathsInfo>(cb->getMapSize(), h);
  26. auto config = createConfig(h, *result);
  27. cb->calculatePaths(config);
  28. return result;
  29. }
  30. PathfinderCache::PathfinderCache(const CGameInfoCallback * cb, const PathfinderOptions & options)
  31. : cb(cb)
  32. , options(options)
  33. {
  34. }
  35. void PathfinderCache::invalidatePaths()
  36. {
  37. std::lock_guard lock(pathCacheMutex);
  38. pathCache.clear();
  39. }
  40. std::shared_ptr<const CPathsInfo> PathfinderCache::getPathsInfo(const CGHeroInstance * h)
  41. {
  42. std::lock_guard lock(pathCacheMutex);
  43. auto iter = pathCache.find(h);
  44. if(iter == std::end(pathCache) || iter->second->heroBonusTreeVersion != h->getTreeVersion())
  45. {
  46. auto result = buildPaths(h);
  47. pathCache[h] = result;
  48. return result;
  49. }
  50. else
  51. return iter->second;
  52. }
  53. VCMI_LIB_NAMESPACE_END