DangerHitMapAnalyzer.cpp 7.3 KB

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