AIPathfinder.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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::vector<std::shared_ptr<AINodeStorage>> AIPathfinder::storagePool;
  16. std::map<HeroPtr, std::shared_ptr<AINodeStorage>> AIPathfinder::storageMap;
  17. AIPathfinder::AIPathfinder(CPlayerSpecificInfoCallback * cb, VCAI * ai)
  18. :cb(cb), ai(ai)
  19. {
  20. }
  21. void AIPathfinder::init()
  22. {
  23. storagePool.clear();
  24. storageMap.clear();
  25. }
  26. bool AIPathfinder::isTileAccessible(const HeroPtr & hero, const int3 & tile) const
  27. {
  28. std::shared_ptr<const AINodeStorage> nodeStorage = getStorage(hero);
  29. return nodeStorage->isTileAccessible(tile, EPathfindingLayer::LAND)
  30. || nodeStorage->isTileAccessible(tile, EPathfindingLayer::SAIL);
  31. }
  32. std::vector<AIPath> AIPathfinder::getPathInfo(const HeroPtr & hero, const int3 & tile) const
  33. {
  34. std::shared_ptr<const AINodeStorage> nodeStorage = getStorage(hero);
  35. const TerrainTile * tileInfo = cb->getTile(tile, false);
  36. if(!tileInfo)
  37. {
  38. return std::vector<AIPath>();
  39. }
  40. return nodeStorage->getChainInfo(tile, !tileInfo->isWater());
  41. }
  42. void AIPathfinder::updatePaths(std::vector<HeroPtr> heroes)
  43. {
  44. storageMap.clear();
  45. auto calculatePaths = [&](const CGHeroInstance * hero, std::shared_ptr<AIPathfinding::AIPathfinderConfig> config)
  46. {
  47. logAi->debug("Recalculate paths for %s", hero->name);
  48. cb->calculatePaths(config, hero);
  49. };
  50. std::vector<Task> calculationTasks;
  51. // TODO: go parallel?
  52. for(HeroPtr hero : heroes)
  53. {
  54. std::shared_ptr<AINodeStorage> nodeStorage;
  55. if(storageMap.size() < storagePool.size())
  56. {
  57. nodeStorage = storagePool.at(storageMap.size());
  58. }
  59. else
  60. {
  61. nodeStorage = std::make_shared<AINodeStorage>(cb->getMapSize());
  62. storagePool.push_back(nodeStorage);
  63. }
  64. storageMap[hero] = nodeStorage;
  65. nodeStorage->setHero(hero, cb);
  66. auto config = std::make_shared<AIPathfinding::AIPathfinderConfig>(cb, ai, nodeStorage);
  67. calculationTasks.push_back(std::bind(calculatePaths, hero.get(), config));
  68. }
  69. int threadsCount = std::min(
  70. boost::thread::hardware_concurrency(),
  71. (uint32_t)calculationTasks.size());
  72. if(threadsCount <= 1)
  73. {
  74. for(auto task : calculationTasks)
  75. {
  76. task();
  77. }
  78. }
  79. else
  80. {
  81. CThreadHelper helper(&calculationTasks, threadsCount);
  82. helper.run();
  83. }
  84. }
  85. void AIPathfinder::updatePaths(const HeroPtr & hero)
  86. {
  87. std::shared_ptr<AINodeStorage> nodeStorage;
  88. if(!vstd::contains(storageMap, hero))
  89. {
  90. if(storageMap.size() < storagePool.size())
  91. {
  92. nodeStorage = storagePool.at(storageMap.size());
  93. }
  94. else
  95. {
  96. nodeStorage = std::make_shared<AINodeStorage>(cb->getMapSize());
  97. storagePool.push_back(nodeStorage);
  98. }
  99. storageMap[hero] = nodeStorage;
  100. nodeStorage->setHero(hero, cb);
  101. }
  102. else
  103. {
  104. nodeStorage = storageMap.at(hero);
  105. }
  106. logAi->debug("Recalculate paths for %s", hero->name);
  107. auto config = std::make_shared<AIPathfinding::AIPathfinderConfig>(cb, ai, nodeStorage);
  108. cb->calculatePaths(config, hero.get());
  109. }
  110. std::shared_ptr<const AINodeStorage> AIPathfinder::getStorage(const HeroPtr & hero) const
  111. {
  112. return storageMap.at(hero);
  113. }