DangerHitMapAnalyzer.cpp 3.3 KB

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