DangerHitMapAnalyzer.cpp 7.6 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. #include "../pforeach.h"
  14. #include "../../../lib/CRandomGenerator.h"
  15. namespace NKAI
  16. {
  17. const HitMapInfo HitMapInfo::NoThreat;
  18. double HitMapInfo::value() const
  19. {
  20. return danger / std::sqrt(turn / 3.0f + 1);
  21. }
  22. void DangerHitMapAnalyzer::updateHitMap()
  23. {
  24. if(hitMapUpToDate)
  25. return;
  26. logAi->trace("Update danger hitmap");
  27. hitMapUpToDate = true;
  28. auto start = std::chrono::high_resolution_clock::now();
  29. auto cb = ai->cb.get();
  30. auto mapSize = ai->cb->getMapSize();
  31. if(hitMap.shape()[0] != mapSize.x || hitMap.shape()[1] != mapSize.y || hitMap.shape()[2] != mapSize.z)
  32. hitMap.resize(boost::extents[mapSize.x][mapSize.y][mapSize.z]);
  33. enemyHeroAccessibleObjects.clear();
  34. townThreats.clear();
  35. std::map<PlayerColor, std::map<const CGHeroInstance *, HeroRole>> heroes;
  36. for(const CGObjectInstance * obj : ai->memory->visitableObjs)
  37. {
  38. if(obj->ID == Obj::HERO)
  39. {
  40. auto hero = dynamic_cast<const CGHeroInstance *>(obj);
  41. heroes[hero->tempOwner][hero] = HeroRole::MAIN;
  42. }
  43. }
  44. auto ourTowns = cb->getTownsInfo();
  45. for(auto town : ourTowns)
  46. {
  47. townThreats[town->id]; // insert empty list
  48. }
  49. foreach_tile_pos([&](const int3 & pos){
  50. hitMap[pos.x][pos.y][pos.z].reset();
  51. });
  52. for(auto pair : heroes)
  53. {
  54. if(!pair.first.isValidPlayer())
  55. continue;
  56. if(ai->cb->getPlayerRelations(ai->playerID, pair.first) != PlayerRelations::ENEMIES)
  57. continue;
  58. PathfinderSettings ps;
  59. ps.scoutTurnDistanceLimit = ps.mainTurnDistanceLimit = ai->settings->getMainHeroTurnDistanceLimit();
  60. ps.useHeroChain = false;
  61. ai->pathfinder->updatePaths(pair.second, ps);
  62. boost::this_thread::interruption_point();
  63. pforeachTilePaths(mapSize, ai, [&](const int3 & pos, const std::vector<AIPath> & paths)
  64. {
  65. for(const AIPath & path : paths)
  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::vector<std::unique_ptr<CGHeroInstance>> temporaryHeroes;
  123. std::map<const CGHeroInstance *, const CGTownInstance *> heroTownMap;
  124. std::map<const CGHeroInstance *, HeroRole> townHeroes;
  125. auto addTownHero = [&](const CGTownInstance * town)
  126. {
  127. auto townHero = temporaryHeroes.emplace_back(std::make_unique<CGHeroInstance>(town->cb)).get();
  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. PathfinderSettings ps;
  149. ps.mainTurnDistanceLimit = ps.scoutTurnDistanceLimit = ai->settings->getMainHeroTurnDistanceLimit();
  150. ai->pathfinder->updatePaths(townHeroes, ps);
  151. pforeachTilePaths(mapSize, ai, [&](const int3 & pos, const std::vector<AIPath> & paths)
  152. {
  153. float ourDistance = std::numeric_limits<float>::max();
  154. float enemyDistance = std::numeric_limits<float>::max();
  155. const CGTownInstance * enemyTown = nullptr;
  156. const CGTownInstance * ourTown = nullptr;
  157. for(const AIPath & path : paths)
  158. {
  159. if(!path.targetHero || path.getFirstBlockedAction())
  160. continue;
  161. auto town = heroTownMap[path.targetHero];
  162. if(town->getOwner() == ai->playerID)
  163. {
  164. if(ourDistance > path.movementCost())
  165. {
  166. ourDistance = path.movementCost();
  167. ourTown = town;
  168. }
  169. }
  170. else
  171. {
  172. if(enemyDistance > path.movementCost())
  173. {
  174. enemyDistance = path.movementCost();
  175. enemyTown = town;
  176. }
  177. }
  178. }
  179. if(vstd::isAlmostEqual(ourDistance, enemyDistance))
  180. {
  181. hitMap[pos.x][pos.y][pos.z].closestTown = nullptr;
  182. }
  183. else if(!enemyTown || ourDistance < enemyDistance)
  184. {
  185. hitMap[pos.x][pos.y][pos.z].closestTown = ourTown;
  186. }
  187. else
  188. {
  189. hitMap[pos.x][pos.y][pos.z].closestTown = enemyTown;
  190. }
  191. });
  192. }
  193. const std::vector<HitMapInfo> & DangerHitMapAnalyzer::getTownThreats(const CGTownInstance * town) const
  194. {
  195. static const std::vector<HitMapInfo> empty = {};
  196. auto result = townThreats.find(town->id);
  197. return result == townThreats.end() ? empty : result->second;
  198. }
  199. PlayerColor DangerHitMapAnalyzer::getTileOwner(const int3 & tile) const
  200. {
  201. auto town = hitMap[tile.x][tile.y][tile.z].closestTown;
  202. return town ? town->getOwner() : PlayerColor::NEUTRAL;
  203. }
  204. const CGTownInstance * DangerHitMapAnalyzer::getClosestTown(const int3 & tile) const
  205. {
  206. return hitMap[tile.x][tile.y][tile.z].closestTown;
  207. }
  208. uint64_t DangerHitMapAnalyzer::enemyCanKillOurHeroesAlongThePath(const AIPath & path) const
  209. {
  210. int3 tile = path.targetTile();
  211. int turn = path.turn();
  212. const auto& info = getTileThreat(tile);
  213. return (info.fastestDanger.turn <= turn && !isSafeToVisit(path.targetHero, path.heroArmy, info.fastestDanger.danger))
  214. || (info.maximumDanger.turn <= turn && !isSafeToVisit(path.targetHero, path.heroArmy, info.maximumDanger.danger));
  215. }
  216. const HitMapNode & DangerHitMapAnalyzer::getObjectThreat(const CGObjectInstance * obj) const
  217. {
  218. auto tile = obj->visitablePos();
  219. return getTileThreat(tile);
  220. }
  221. const HitMapNode & DangerHitMapAnalyzer::getTileThreat(const int3 & tile) const
  222. {
  223. return hitMap[tile.x][tile.y][tile.z];
  224. }
  225. std::set<const CGObjectInstance *> DangerHitMapAnalyzer::getOneTurnAccessibleObjects(const CGHeroInstance * enemy) const
  226. {
  227. std::set<const CGObjectInstance *> result;
  228. for(auto & obj : enemyHeroAccessibleObjects)
  229. {
  230. if(obj.hero == enemy)
  231. result.insert(obj.obj);
  232. }
  233. return result;
  234. }
  235. void DangerHitMapAnalyzer::reset()
  236. {
  237. hitMapUpToDate = false;
  238. }
  239. }