DangerHitMapAnalyzer.cpp 7.4 KB

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