DangerHitMapAnalyzer.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. double HitMapInfo::value() const
  17. {
  18. return danger / std::sqrt(turn / 3.0f + 1);
  19. }
  20. void DangerHitMapAnalyzer::updateHitMap()
  21. {
  22. if(hitMapUpToDate)
  23. return;
  24. logAi->trace("Update danger hitmap");
  25. hitMapUpToDate = true;
  26. auto start = std::chrono::high_resolution_clock::now();
  27. auto cb = ai->cb.get();
  28. auto mapSize = ai->cb->getMapSize();
  29. if(hitMap.shape()[0] != mapSize.x || hitMap.shape()[1] != mapSize.y || hitMap.shape()[2] != mapSize.z)
  30. hitMap.resize(boost::extents[mapSize.x][mapSize.y][mapSize.z]);
  31. enemyHeroAccessibleObjects.clear();
  32. townTreats.clear();
  33. std::map<PlayerColor, std::map<const CGHeroInstance *, HeroRole>> heroes;
  34. for(const CGObjectInstance * obj : ai->memory->visitableObjs)
  35. {
  36. if(obj->ID == Obj::HERO)
  37. {
  38. auto hero = dynamic_cast<const CGHeroInstance *>(obj);
  39. heroes[hero->tempOwner][hero] = HeroRole::MAIN;
  40. }
  41. }
  42. foreach_tile_pos([&](const int3 & pos){
  43. hitMap[pos.x][pos.y][pos.z].reset();
  44. });
  45. for(auto pair : heroes)
  46. {
  47. if(!pair.first.isValidPlayer())
  48. continue;
  49. if(ai->cb->getPlayerRelations(ai->playerID, pair.first) != PlayerRelations::ENEMIES)
  50. continue;
  51. ai->pathfinder->updatePaths(pair.second, PathfinderSettings());
  52. boost::this_thread::interruption_point();
  53. pforeachTilePos(mapSize, [&](const int3 & pos)
  54. {
  55. for(AIPath & path : ai->pathfinder->getPathInfo(pos))
  56. {
  57. if(path.getFirstBlockedAction())
  58. continue;
  59. auto & node = hitMap[pos.x][pos.y][pos.z];
  60. HitMapInfo newTreat;
  61. newTreat.hero = path.targetHero;
  62. newTreat.turn = path.turn();
  63. newTreat.danger = path.getHeroStrength();
  64. if(newTreat.value() > node.maximumDanger.value())
  65. {
  66. node.maximumDanger = newTreat;
  67. }
  68. if(newTreat.turn < node.fastestDanger.turn
  69. || (newTreat.turn == node.fastestDanger.turn && node.fastestDanger.danger < newTreat.danger))
  70. {
  71. node.fastestDanger = newTreat;
  72. }
  73. if(newTreat.turn == 0)
  74. {
  75. auto objects = cb->getVisitableObjs(pos, false);
  76. for(auto obj : objects)
  77. {
  78. if(cb->getPlayerRelations(obj->tempOwner, ai->playerID) != PlayerRelations::ENEMIES)
  79. enemyHeroAccessibleObjects[path.targetHero].insert(obj);
  80. if(obj->ID == Obj::TOWN && obj->getOwner() == ai->playerID)
  81. {
  82. auto & treats = townTreats[obj->id];
  83. auto treat = std::find_if(treats.begin(), treats.end(), [&](const HitMapInfo & i) -> bool
  84. {
  85. return i.hero.hid == path.targetHero->id;
  86. });
  87. if(treat == treats.end())
  88. {
  89. treats.emplace_back();
  90. treat = std::prev(treats.end(), 1);
  91. }
  92. if(newTreat.value() > treat->value())
  93. {
  94. *treat = newTreat;
  95. }
  96. }
  97. }
  98. }
  99. }
  100. });
  101. }
  102. logAi->trace("Danger hit map updated in %ld", timeElapsed(start));
  103. }
  104. void DangerHitMapAnalyzer::calculateTileOwners()
  105. {
  106. if(tileOwnersUpToDate) return;
  107. tileOwnersUpToDate = true;
  108. auto cb = ai->cb.get();
  109. auto mapSize = ai->cb->getMapSize();
  110. if(hitMap.shape()[0] != mapSize.x || hitMap.shape()[1] != mapSize.y || hitMap.shape()[2] != mapSize.z)
  111. hitMap.resize(boost::extents[mapSize.x][mapSize.y][mapSize.z]);
  112. std::map<const CGHeroInstance *, HeroRole> townHeroes;
  113. std::map<const CGHeroInstance *, const CGTownInstance *> heroTownMap;
  114. PathfinderSettings pathfinderSettings;
  115. pathfinderSettings.mainTurnDistanceLimit = 5;
  116. auto addTownHero = [&](const CGTownInstance * town)
  117. {
  118. auto townHero = new CGHeroInstance();
  119. CRandomGenerator rng;
  120. auto visitablePos = town->visitablePos();
  121. townHero->setOwner(ai->playerID); // lets avoid having multiple colors
  122. townHero->initHero(rng, static_cast<HeroTypeID>(0));
  123. townHero->pos = townHero->convertFromVisitablePos(visitablePos);
  124. townHero->initObj(rng);
  125. heroTownMap[townHero] = town;
  126. townHeroes[townHero] = HeroRole::MAIN;
  127. };
  128. for(auto obj : ai->memory->visitableObjs)
  129. {
  130. if(obj && obj->ID == Obj::TOWN)
  131. {
  132. addTownHero(dynamic_cast<const CGTownInstance *>(obj));
  133. }
  134. }
  135. for(auto town : cb->getTownsInfo())
  136. {
  137. addTownHero(town);
  138. }
  139. ai->pathfinder->updatePaths(townHeroes, PathfinderSettings());
  140. pforeachTilePos(mapSize, [&](const int3 & pos)
  141. {
  142. float ourDistance = std::numeric_limits<float>::max();
  143. float enemyDistance = std::numeric_limits<float>::max();
  144. const CGTownInstance * enemyTown = nullptr;
  145. const CGTownInstance * ourTown = nullptr;
  146. for(AIPath & path : ai->pathfinder->getPathInfo(pos))
  147. {
  148. if(!path.targetHero || path.getFirstBlockedAction())
  149. continue;
  150. auto town = heroTownMap[path.targetHero];
  151. if(town->getOwner() == ai->playerID)
  152. {
  153. if(ourDistance > path.movementCost())
  154. {
  155. ourDistance = path.movementCost();
  156. ourTown = town;
  157. }
  158. }
  159. else
  160. {
  161. if(enemyDistance > path.movementCost())
  162. {
  163. enemyDistance = path.movementCost();
  164. enemyTown = town;
  165. }
  166. }
  167. }
  168. if(ourDistance == enemyDistance)
  169. {
  170. hitMap[pos.x][pos.y][pos.z].closestTown = nullptr;
  171. }
  172. else if(!enemyTown || ourDistance < enemyDistance)
  173. {
  174. hitMap[pos.x][pos.y][pos.z].closestTown = ourTown;
  175. }
  176. else
  177. {
  178. hitMap[pos.x][pos.y][pos.z].closestTown = enemyTown;
  179. }
  180. });
  181. }
  182. const std::vector<HitMapInfo> & DangerHitMapAnalyzer::getTownTreats(const CGTownInstance * town) const
  183. {
  184. static const std::vector<HitMapInfo> empty = {};
  185. auto result = townTreats.find(town->id);
  186. return result == townTreats.end() ? empty : result->second;
  187. }
  188. PlayerColor DangerHitMapAnalyzer::getTileOwner(const int3 & tile) const
  189. {
  190. auto town = hitMap[tile.x][tile.y][tile.z].closestTown;
  191. return town ? town->getOwner() : PlayerColor::NEUTRAL;
  192. }
  193. const CGTownInstance * DangerHitMapAnalyzer::getClosestTown(const int3 & tile) const
  194. {
  195. return hitMap[tile.x][tile.y][tile.z].closestTown;
  196. }
  197. uint64_t DangerHitMapAnalyzer::enemyCanKillOurHeroesAlongThePath(const AIPath & path) const
  198. {
  199. int3 tile = path.targetTile();
  200. int turn = path.turn();
  201. const HitMapNode & info = hitMap[tile.x][tile.y][tile.z];
  202. return (info.fastestDanger.turn <= turn && !isSafeToVisit(path.targetHero, path.heroArmy, info.fastestDanger.danger))
  203. || (info.maximumDanger.turn <= turn && !isSafeToVisit(path.targetHero, path.heroArmy, info.maximumDanger.danger));
  204. }
  205. const HitMapNode & DangerHitMapAnalyzer::getObjectTreat(const CGObjectInstance * obj) const
  206. {
  207. auto tile = obj->visitablePos();
  208. return getTileTreat(tile);
  209. }
  210. const HitMapNode & DangerHitMapAnalyzer::getTileTreat(const int3 & tile) const
  211. {
  212. const HitMapNode & info = hitMap[tile.x][tile.y][tile.z];
  213. return info;
  214. }
  215. const std::set<const CGObjectInstance *> empty = {};
  216. const std::set<const CGObjectInstance *> & DangerHitMapAnalyzer::getOneTurnAccessibleObjects(const CGHeroInstance * enemy) const
  217. {
  218. auto result = enemyHeroAccessibleObjects.find(enemy);
  219. if(result == enemyHeroAccessibleObjects.end())
  220. {
  221. return empty;
  222. }
  223. return result->second;
  224. }
  225. void DangerHitMapAnalyzer::reset()
  226. {
  227. hitMapUpToDate = false;
  228. }
  229. }