AIPathfinder.cpp 4.6 KB

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