DangerHitMapAnalyzer.cpp 7.5 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.scoutTurnDistanceLimit = ps.mainTurnDistanceLimit = ai->settings->getMainHeroTurnDistanceLimit();
  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::vector<std::unique_ptr<CGHeroInstance>> temporaryHeroes;
  122. std::map<const CGHeroInstance *, const CGTownInstance *> heroTownMap;
  123. std::map<const CGHeroInstance *, HeroRole> townHeroes;
  124. auto addTownHero = [&](const CGTownInstance * town)
  125. {
  126. auto townHero = temporaryHeroes.emplace_back(std::make_unique<CGHeroInstance>(town->cb)).get();
  127. CRandomGenerator rng;
  128. auto visitablePos = town->visitablePos();
  129. townHero->setOwner(ai->playerID); // lets avoid having multiple colors
  130. townHero->initHero(rng, static_cast<HeroTypeID>(0));
  131. townHero->pos = townHero->convertFromVisitablePos(visitablePos);
  132. townHero->initObj(rng);
  133. heroTownMap[townHero] = town;
  134. townHeroes[townHero] = HeroRole::MAIN;
  135. };
  136. for(auto obj : ai->memory->visitableObjs)
  137. {
  138. if(obj && obj->ID == Obj::TOWN)
  139. {
  140. addTownHero(dynamic_cast<const CGTownInstance *>(obj));
  141. }
  142. }
  143. for(auto town : cb->getTownsInfo())
  144. {
  145. addTownHero(town);
  146. }
  147. PathfinderSettings ps;
  148. ps.mainTurnDistanceLimit = ps.scoutTurnDistanceLimit = ai->settings->getMainHeroTurnDistanceLimit();
  149. ai->pathfinder->updatePaths(townHeroes, ps);
  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. }