DangerHitMapAnalyzer.cpp 3.7 KB

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