DangerHitMapAnalyzer.cpp 7.4 KB

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