DangerHitMapAnalyzer.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * DangerHitMapAnalyzer.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 "lib/mapping/CMap.h" //for victory conditions
  12. #include "../Engine/Nullkiller.h"
  13. namespace NKAI
  14. {
  15. void DangerHitMapAnalyzer::updateHitMap()
  16. {
  17. if(upToDate)
  18. return;
  19. logAi->trace("Update danger hitmap");
  20. upToDate = true;
  21. auto start = std::chrono::high_resolution_clock::now();
  22. auto cb = ai->cb.get();
  23. auto mapSize = ai->cb->getMapSize();
  24. hitMap.resize(boost::extents[mapSize.x][mapSize.y][mapSize.z]);
  25. enemyHeroAccessibleObjects.clear();
  26. std::map<PlayerColor, std::map<const CGHeroInstance *, HeroRole>> heroes;
  27. for(const CGObjectInstance * obj : ai->memory->visitableObjs)
  28. {
  29. if(obj->ID == Obj::HERO)
  30. {
  31. auto hero = dynamic_cast<const CGHeroInstance *>(obj);
  32. heroes[hero->tempOwner][hero] = HeroRole::MAIN;
  33. }
  34. }
  35. foreach_tile_pos([&](const int3 & pos){
  36. hitMap[pos.x][pos.y][pos.z].reset();
  37. });
  38. for(auto pair : heroes)
  39. {
  40. if(ai->cb->getPlayerRelations(ai->playerID, pair.first) != PlayerRelations::ENEMIES)
  41. continue;
  42. ai->pathfinder->updatePaths(pair.second, PathfinderSettings());
  43. boost::this_thread::interruption_point();
  44. pforeachTilePos(mapSize, [&](const int3 & pos)
  45. {
  46. for(AIPath & path : ai->pathfinder->getPathInfo(pos))
  47. {
  48. if(path.getFirstBlockedAction())
  49. continue;
  50. auto tileDanger = path.getHeroStrength();
  51. auto turn = path.turn();
  52. auto & node = hitMap[pos.x][pos.y][pos.z];
  53. if(tileDanger > node.maximumDanger.danger
  54. || (tileDanger == node.maximumDanger.danger && node.maximumDanger.turn > turn))
  55. {
  56. node.maximumDanger.danger = tileDanger;
  57. node.maximumDanger.turn = turn;
  58. node.maximumDanger.hero = path.targetHero;
  59. }
  60. if(turn < node.fastestDanger.turn
  61. || (turn == node.fastestDanger.turn && node.fastestDanger.danger < tileDanger))
  62. {
  63. node.fastestDanger.danger = tileDanger;
  64. node.fastestDanger.turn = turn;
  65. node.fastestDanger.hero = path.targetHero;
  66. }
  67. if(turn == 0)
  68. {
  69. auto objects = cb->getVisitableObjs(pos, false);
  70. for(auto obj : objects)
  71. {
  72. if(cb->getPlayerRelations(obj->tempOwner, ai->playerID) != PlayerRelations::ENEMIES)
  73. enemyHeroAccessibleObjects[path.targetHero].insert(obj);
  74. }
  75. }
  76. }
  77. });
  78. }
  79. logAi->trace("Danger hit map updated in %ld", timeElapsed(start));
  80. }
  81. uint64_t DangerHitMapAnalyzer::enemyCanKillOurHeroesAlongThePath(const AIPath & path) const
  82. {
  83. int3 tile = path.targetTile();
  84. int turn = path.turn();
  85. const HitMapNode & info = hitMap[tile.x][tile.y][tile.z];
  86. return (info.fastestDanger.turn <= turn && !isSafeToVisit(path.targetHero, path.heroArmy, info.fastestDanger.danger))
  87. || (info.maximumDanger.turn <= turn && !isSafeToVisit(path.targetHero, path.heroArmy, info.maximumDanger.danger));
  88. }
  89. const HitMapNode & DangerHitMapAnalyzer::getObjectTreat(const CGObjectInstance * obj) const
  90. {
  91. auto tile = obj->visitablePos();
  92. return getTileTreat(tile);
  93. }
  94. const HitMapNode & DangerHitMapAnalyzer::getTileTreat(const int3 & tile) const
  95. {
  96. const HitMapNode & info = hitMap[tile.x][tile.y][tile.z];
  97. return info;
  98. }
  99. const std::set<const CGObjectInstance *> empty = {};
  100. const std::set<const CGObjectInstance *> & DangerHitMapAnalyzer::getOneTurnAccessibleObjects(const CGHeroInstance * enemy) const
  101. {
  102. auto result = enemyHeroAccessibleObjects.find(enemy);
  103. if(result == enemyHeroAccessibleObjects.end())
  104. {
  105. return empty;
  106. }
  107. return result->second;
  108. }
  109. void DangerHitMapAnalyzer::reset()
  110. {
  111. upToDate = false;
  112. }
  113. }