AIPathfinder.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #include "../Engine/Nullkiller.h"
  16. namespace NKAI
  17. {
  18. std::map<ObjectInstanceID, std::unique_ptr<GraphPaths>> AIPathfinder::heroGraphs;
  19. AIPathfinder::AIPathfinder(CPlayerSpecificInfoCallback * cb, Nullkiller * ai)
  20. :cb(cb), ai(ai)
  21. {
  22. }
  23. void AIPathfinder::init()
  24. {
  25. storage.reset();
  26. }
  27. bool AIPathfinder::isTileAccessible(const HeroPtr & hero, const int3 & tile) const
  28. {
  29. return storage->isTileAccessible(hero, tile, EPathfindingLayer::LAND)
  30. || storage->isTileAccessible(hero, tile, EPathfindingLayer::SAIL);
  31. }
  32. void AIPathfinder::calculateQuickPathsWithBlocker(std::vector<AIPath> & result, const std::vector<const CGHeroInstance *> & heroes, const int3 & tile)
  33. {
  34. result.clear();
  35. for(auto hero : heroes)
  36. {
  37. auto graph = heroGraphs.find(hero->id);
  38. if(graph != heroGraphs.end())
  39. graph->second->quickAddChainInfoWithBlocker(result, tile, hero, ai);
  40. }
  41. }
  42. void AIPathfinder::calculatePathInfo(std::vector<AIPath> & result, const int3 & tile, bool includeGraph) const
  43. {
  44. const TerrainTile * tileInfo = cb->getTile(tile, false);
  45. result.clear();
  46. if(!tileInfo)
  47. {
  48. return;
  49. }
  50. storage->calculateChainInfo(result, tile, !tileInfo->isWater());
  51. if(includeGraph)
  52. {
  53. for(auto hero : cb->getHeroesInfo())
  54. {
  55. auto graph = heroGraphs.find(hero->id);
  56. if(graph != heroGraphs.end())
  57. graph->second->addChainInfo(result, tile, hero, ai);
  58. }
  59. }
  60. }
  61. void AIPathfinder::updatePaths(const std::map<const CGHeroInstance *, HeroRole> & heroes, PathfinderSettings pathfinderSettings)
  62. {
  63. if(!storage)
  64. {
  65. storage.reset(new AINodeStorage(ai, cb->getMapSize()));
  66. }
  67. auto start = std::chrono::high_resolution_clock::now();
  68. logAi->debug("Recalculate all paths");
  69. int pass = 0;
  70. storage->clear();
  71. storage->setHeroes(heroes);
  72. storage->setScoutTurnDistanceLimit(pathfinderSettings.scoutTurnDistanceLimit);
  73. storage->setMainTurnDistanceLimit(pathfinderSettings.mainTurnDistanceLimit);
  74. logAi->trace(
  75. "Scout turn distance: %s, main %s",
  76. std::to_string(pathfinderSettings.scoutTurnDistanceLimit),
  77. std::to_string(pathfinderSettings.mainTurnDistanceLimit));
  78. if(pathfinderSettings.useHeroChain)
  79. {
  80. storage->setTownsAndDwellings(cb->getTownsInfo(), ai->memory->visitableObjs);
  81. }
  82. auto config = std::make_shared<AIPathfinding::AIPathfinderConfig>(cb, ai, storage, pathfinderSettings.allowBypassObjects);
  83. logAi->trace("Recalculate paths pass %d", pass++);
  84. cb->calculatePaths(config);
  85. if(!pathfinderSettings.useHeroChain)
  86. {
  87. logAi->trace("Recalculated paths in %ld ms", timeElapsed(start));
  88. return;
  89. }
  90. do
  91. {
  92. storage->selectFirstActor();
  93. do
  94. {
  95. boost::this_thread::interruption_point();
  96. while(storage->calculateHeroChain())
  97. {
  98. boost::this_thread::interruption_point();
  99. logAi->trace("Recalculate paths pass %d", pass++);
  100. cb->calculatePaths(config);
  101. }
  102. logAi->trace("Select next actor");
  103. } while(storage->selectNextActor());
  104. boost::this_thread::interruption_point();
  105. if(storage->calculateHeroChainFinal())
  106. {
  107. boost::this_thread::interruption_point();
  108. logAi->trace("Recalculate paths pass final");
  109. cb->calculatePaths(config);
  110. }
  111. } while(storage->increaseHeroChainTurnLimit());
  112. logAi->trace("Recalculated paths in %ld ms", timeElapsed(start));
  113. }
  114. void AIPathfinder::updateGraphs(
  115. const std::map<const CGHeroInstance *, HeroRole> & heroes,
  116. uint8_t mainScanDepth,
  117. uint8_t scoutScanDepth)
  118. {
  119. auto start = std::chrono::high_resolution_clock::now();
  120. std::vector<const CGHeroInstance *> heroesVector;
  121. heroGraphs.clear();
  122. for(auto hero : heroes)
  123. {
  124. if(heroGraphs.try_emplace(hero.first->id).second)
  125. {
  126. heroGraphs[hero.first->id] = std::make_unique<GraphPaths>();
  127. heroesVector.push_back(hero.first);
  128. }
  129. }
  130. tbb::parallel_for(tbb::blocked_range<size_t>(0, heroesVector.size()), [this, &heroesVector, &heroes, mainScanDepth, scoutScanDepth](const tbb::blocked_range<size_t> & r)
  131. {
  132. for(auto i = r.begin(); i != r.end(); i++)
  133. {
  134. auto role = heroes.at(heroesVector[i]);
  135. auto scanLimit = role == HeroRole::MAIN ? mainScanDepth : scoutScanDepth;
  136. heroGraphs.at(heroesVector[i]->id)->calculatePaths(heroesVector[i], ai, scanLimit);
  137. }
  138. });
  139. if(NKAI_GRAPH_TRACE_LEVEL >= 1)
  140. {
  141. for(auto hero : heroes)
  142. {
  143. heroGraphs[hero.first->id]->dumpToLog();
  144. }
  145. }
  146. logAi->trace("Graph paths updated in %lld", timeElapsed(start));
  147. }
  148. }