DangerHitMapAnalyzer.cpp 3.6 KB

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