DangerHitMapAnalyzer.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. ai->pathfinder->updatePaths(pair.second, PathfinderSettings());
  39. boost::this_thread::interruption_point();
  40. foreach_tile_pos([&](const int3 & pos)
  41. {
  42. for(AIPath & path : ai->pathfinder->getPathInfo(pos))
  43. {
  44. if(path.getFirstBlockedAction())
  45. continue;
  46. auto tileDanger = path.getHeroStrength();
  47. auto turn = path.turn();
  48. auto & node = hitMap[pos.x][pos.y][pos.z];
  49. if(tileDanger > node.maximumDanger.danger
  50. || tileDanger == node.maximumDanger.danger && node.maximumDanger.turn > turn)
  51. {
  52. node.maximumDanger.danger = tileDanger;
  53. node.maximumDanger.turn = turn;
  54. node.maximumDanger.hero = path.targetHero;
  55. }
  56. if(turn < node.fastestDanger.turn
  57. || turn == node.fastestDanger.turn && node.fastestDanger.danger < tileDanger)
  58. {
  59. node.fastestDanger.danger = tileDanger;
  60. node.fastestDanger.turn = turn;
  61. node.fastestDanger.hero = path.targetHero;
  62. }
  63. if(turn == 0)
  64. {
  65. auto objects = cb->getVisitableObjs(pos, false);
  66. for(auto obj : objects)
  67. {
  68. if(cb->getPlayerRelations(obj->tempOwner, ai->playerID) != PlayerRelations::ENEMIES)
  69. enemyHeroAccessibleObjects[path.targetHero].insert(obj);
  70. }
  71. }
  72. }
  73. });
  74. }
  75. logAi->trace("Danger hit map updated in %ld", timeElapsed(start));
  76. }
  77. uint64_t DangerHitMapAnalyzer::enemyCanKillOurHeroesAlongThePath(const AIPath & path) const
  78. {
  79. int3 tile = path.targetTile();
  80. int turn = path.turn();
  81. const HitMapNode & info = hitMap[tile.x][tile.y][tile.z];
  82. return info.fastestDanger.turn <= turn && !isSafeToVisit(path.targetHero, path.heroArmy, info.fastestDanger.danger)
  83. || info.maximumDanger.turn <= turn && !isSafeToVisit(path.targetHero, path.heroArmy, info.maximumDanger.danger);
  84. }
  85. const HitMapNode & DangerHitMapAnalyzer::getObjectTreat(const CGObjectInstance * obj) const
  86. {
  87. auto tile = obj->visitablePos();
  88. return getTileTreat(tile);
  89. }
  90. const HitMapNode & DangerHitMapAnalyzer::getTileTreat(const int3 & tile) const
  91. {
  92. const HitMapNode & info = hitMap[tile.x][tile.y][tile.z];
  93. return info;
  94. }
  95. const std::set<const CGObjectInstance *> empty = {};
  96. const std::set<const CGObjectInstance *> & DangerHitMapAnalyzer::getOneTurnAccessibleObjects(const CGHeroInstance * enemy) const
  97. {
  98. auto result = enemyHeroAccessibleObjects.find(enemy);
  99. if(result == enemyHeroAccessibleObjects.end())
  100. {
  101. return empty;
  102. }
  103. return result->second;
  104. }
  105. void DangerHitMapAnalyzer::reset()
  106. {
  107. upToDate = false;
  108. }