2
0

AIPathfinder.cpp 4.6 KB

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