DangerHitMapAnalyzer.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 "../Engine/Nullkiller.h"
  13. namespace NKAI
  14. {
  15. HitMapInfo HitMapInfo::NoTreat;
  16. void DangerHitMapAnalyzer::updateHitMap()
  17. {
  18. if(hitMapUpToDate)
  19. return;
  20. logAi->trace("Update danger hitmap");
  21. hitMapUpToDate = 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. auto newMaxDanger = tileDanger / std::sqrt(turn / 3.0f + 1);
  57. auto currentMaxDanger = node.maximumDanger.danger / std::sqrt(node.maximumDanger.turn / 3.0f + 1);
  58. if(newMaxDanger > currentMaxDanger)
  59. {
  60. node.maximumDanger.danger = tileDanger;
  61. node.maximumDanger.turn = turn;
  62. node.maximumDanger.hero = path.targetHero;
  63. }
  64. if(turn < node.fastestDanger.turn
  65. || (turn == node.fastestDanger.turn && node.fastestDanger.danger < tileDanger))
  66. {
  67. node.fastestDanger.danger = tileDanger;
  68. node.fastestDanger.turn = turn;
  69. node.fastestDanger.hero = path.targetHero;
  70. }
  71. if(turn == 0)
  72. {
  73. auto objects = cb->getVisitableObjs(pos, false);
  74. for(auto obj : objects)
  75. {
  76. if(cb->getPlayerRelations(obj->tempOwner, ai->playerID) != PlayerRelations::ENEMIES)
  77. enemyHeroAccessibleObjects[path.targetHero].insert(obj);
  78. }
  79. }
  80. }
  81. });
  82. }
  83. logAi->trace("Danger hit map updated in %ld", timeElapsed(start));
  84. }
  85. void DangerHitMapAnalyzer::calculateTileOwners()
  86. {
  87. if(tileOwnersUpToDate) return;
  88. tileOwnersUpToDate = true;
  89. auto cb = ai->cb.get();
  90. auto mapSize = ai->cb->getMapSize();
  91. tileOwners.resize(boost::extents[mapSize.x][mapSize.y][mapSize.z]);
  92. std::map<const CGHeroInstance *, HeroRole> townHeroes;
  93. std::map<const CGHeroInstance *, const CGTownInstance *> heroTownMap;
  94. PathfinderSettings pathfinderSettings;
  95. pathfinderSettings.mainTurnDistanceLimit = 3;
  96. auto addTownHero = [&](const CGTownInstance * town)
  97. {
  98. auto townHero = new CGHeroInstance();
  99. CRandomGenerator rng;
  100. townHero->pos = town->pos;
  101. townHero->setOwner(ai->playerID); // lets avoid having multiple colors
  102. townHero->initHero(rng, static_cast<HeroTypeID>(0));
  103. townHero->initObj(rng);
  104. heroTownMap[townHero] = town;
  105. townHeroes[townHero] = HeroRole::MAIN;
  106. };
  107. for(auto obj : ai->memory->visitableObjs)
  108. {
  109. if(obj && obj->ID == Obj::TOWN)
  110. {
  111. addTownHero(dynamic_cast<const CGTownInstance *>(obj));
  112. }
  113. }
  114. for(auto town : cb->getTownsInfo())
  115. {
  116. addTownHero(town);
  117. }
  118. ai->pathfinder->updatePaths(townHeroes, PathfinderSettings());
  119. pforeachTilePos(mapSize, [&](const int3 & pos)
  120. {
  121. float ourDistance = std::numeric_limits<float>::max();
  122. float enemyDistance = std::numeric_limits<float>::max();
  123. const CGTownInstance * enemyTown = nullptr;
  124. for(AIPath & path : ai->pathfinder->getPathInfo(pos))
  125. {
  126. if(!path.targetHero || path.getFirstBlockedAction())
  127. continue;
  128. auto town = heroTownMap[path.targetHero];
  129. if(town->getOwner() == ai->playerID)
  130. {
  131. vstd::amin(ourDistance, path.movementCost());
  132. }
  133. else
  134. {
  135. if(enemyDistance > path.movementCost())
  136. {
  137. enemyDistance = path.movementCost();
  138. enemyTown = town;
  139. }
  140. }
  141. }
  142. if(ourDistance == enemyDistance)
  143. {
  144. tileOwners[pos.x][pos.y][pos.z] = PlayerColor::NEUTRAL;
  145. }
  146. else if(!enemyTown || ourDistance < enemyDistance)
  147. {
  148. tileOwners[pos.x][pos.y][pos.z] = ai->playerID;
  149. }
  150. else
  151. {
  152. tileOwners[pos.x][pos.y][pos.z] = enemyTown->getOwner();
  153. }
  154. });
  155. }
  156. uint64_t DangerHitMapAnalyzer::enemyCanKillOurHeroesAlongThePath(const AIPath & path) const
  157. {
  158. int3 tile = path.targetTile();
  159. int turn = path.turn();
  160. const HitMapNode & info = hitMap[tile.x][tile.y][tile.z];
  161. return (info.fastestDanger.turn <= turn && !isSafeToVisit(path.targetHero, path.heroArmy, info.fastestDanger.danger))
  162. || (info.maximumDanger.turn <= turn && !isSafeToVisit(path.targetHero, path.heroArmy, info.maximumDanger.danger));
  163. }
  164. const HitMapNode & DangerHitMapAnalyzer::getObjectTreat(const CGObjectInstance * obj) const
  165. {
  166. auto tile = obj->visitablePos();
  167. return getTileTreat(tile);
  168. }
  169. const HitMapNode & DangerHitMapAnalyzer::getTileTreat(const int3 & tile) const
  170. {
  171. const HitMapNode & info = hitMap[tile.x][tile.y][tile.z];
  172. return info;
  173. }
  174. const std::set<const CGObjectInstance *> empty = {};
  175. const std::set<const CGObjectInstance *> & DangerHitMapAnalyzer::getOneTurnAccessibleObjects(const CGHeroInstance * enemy) const
  176. {
  177. auto result = enemyHeroAccessibleObjects.find(enemy);
  178. if(result == enemyHeroAccessibleObjects.end())
  179. {
  180. return empty;
  181. }
  182. return result->second;
  183. }
  184. void DangerHitMapAnalyzer::reset()
  185. {
  186. hitMapUpToDate = false;
  187. }
  188. }