ExplorationHelper.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * ExplorationHelper.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 "ExplorationHelper.h"
  12. #include "../../../lib/mapObjects/CGTownInstance.h"
  13. #include "../Engine/Nullkiller.h"
  14. #include "../Goals/Invalid.h"
  15. #include "../Goals/Composition.h"
  16. #include "../Goals/ExecuteHeroChain.h"
  17. #include "../Markers/ExplorationPoint.h"
  18. #include "../../../lib/CPlayerState.h"
  19. #include "../Behaviors/CaptureObjectsBehavior.h"
  20. #include "../Goals/ExploreNeighbourTile.h"
  21. namespace NKAI
  22. {
  23. using namespace Goals;
  24. ExplorationHelper::ExplorationHelper(const CGHeroInstance * hero, const Nullkiller * ai, bool useCPathfinderAccessibility)
  25. :ai(ai), cbp(ai->cb.get()), hero(hero), useCPathfinderAccessibility(useCPathfinderAccessibility)
  26. {
  27. ts = cbp->getPlayerTeam(ai->playerID);
  28. sightRadius = hero->getSightRadius();
  29. bestGoal = sptr(Goals::Invalid());
  30. bestValue = 0;
  31. bestTilesDiscovered = 0;
  32. ourPos = hero->visitablePos();
  33. allowDeadEndCancellation = true;
  34. }
  35. TSubgoal ExplorationHelper::makeComposition() const
  36. {
  37. Composition c;
  38. c.addNext(ExplorationPoint(bestTile, bestTilesDiscovered));
  39. c.addNextSequence({bestGoal, sptr(ExploreNeighbourTile(hero, 5))});
  40. return sptr(c);
  41. }
  42. bool ExplorationHelper::scanSector(int scanRadius)
  43. {
  44. int3 tile = int3(0, 0, ourPos.z);
  45. const auto & slice = ts->fogOfWarMap[ourPos.z];
  46. for(tile.x = ourPos.x - scanRadius; tile.x <= ourPos.x + scanRadius; tile.x++)
  47. {
  48. for(tile.y = ourPos.y - scanRadius; tile.y <= ourPos.y + scanRadius; tile.y++)
  49. {
  50. if(cbp->isInTheMap(tile) && slice[tile.x][tile.y])
  51. {
  52. scanTile(tile);
  53. }
  54. }
  55. }
  56. return !bestGoal->invalid();
  57. }
  58. bool ExplorationHelper::scanMap()
  59. {
  60. int3 mapSize = cbp->getMapSize();
  61. int perimeter = 2 * sightRadius * (mapSize.x + mapSize.y);
  62. std::vector<int3> edgeTiles;
  63. edgeTiles.reserve(perimeter);
  64. foreach_tile_pos([&](const int3 & pos)
  65. {
  66. if(ts->fogOfWarMap[pos.z][pos.x][pos.y])
  67. {
  68. bool hasInvisibleNeighbor = false;
  69. foreach_neighbour(cbp, pos, [&](CCallback * cbp, int3 neighbour)
  70. {
  71. if(!ts->fogOfWarMap[neighbour.z][neighbour.x][neighbour.y])
  72. {
  73. hasInvisibleNeighbor = true;
  74. }
  75. });
  76. if(hasInvisibleNeighbor)
  77. edgeTiles.push_back(pos);
  78. }
  79. });
  80. logAi->debug("Exploration scan visible area perimeter for hero %s", hero->getNameTranslated());
  81. for(const int3 & tile : edgeTiles)
  82. {
  83. scanTile(tile);
  84. }
  85. if(!bestGoal->invalid())
  86. {
  87. return true;
  88. }
  89. allowDeadEndCancellation = false;
  90. logAi->debug("Exploration scan all possible tiles for hero %s", hero->getNameTranslated());
  91. boost::multi_array<ui8, 3> potentialTiles = ts->fogOfWarMap;
  92. std::vector<int3> tilesToExploreFrom = edgeTiles;
  93. // WARNING: POTENTIAL BUG
  94. // AI attempts to move to any tile within sight radius to reveal some new tiles
  95. // however sight radius is circular, while this method assumes square radius
  96. // standing on the edge of a square will NOT reveal tile in opposite corner
  97. for(int i = 0; i < sightRadius; i++)
  98. {
  99. std::vector<int3> newTilesToExploreFrom;
  100. for(const int3 & tile : tilesToExploreFrom)
  101. {
  102. foreach_neighbour(cbp, tile, [&](CCallback * cbp, int3 neighbour)
  103. {
  104. if(potentialTiles[neighbour.z][neighbour.x][neighbour.y])
  105. {
  106. newTilesToExploreFrom.push_back(neighbour);
  107. potentialTiles[neighbour.z][neighbour.x][neighbour.y] = false;
  108. }
  109. });
  110. }
  111. for(const int3 & tile : newTilesToExploreFrom)
  112. {
  113. scanTile(tile);
  114. }
  115. std::swap(tilesToExploreFrom, newTilesToExploreFrom);
  116. }
  117. return !bestGoal->invalid();
  118. }
  119. void ExplorationHelper::scanTile(const int3 & tile)
  120. {
  121. if(tile == ourPos
  122. || !ai->cb->getTile(tile, false)
  123. || !ai->pathfinder->isTileAccessible(hero, tile)) //shouldn't happen, but it does
  124. return;
  125. int tilesDiscovered = howManyTilesWillBeDiscovered(tile);
  126. if(!tilesDiscovered)
  127. return;
  128. auto paths = ai->pathfinder->getPathInfo(tile);
  129. auto waysToVisit = CaptureObjectsBehavior::getVisitGoals(paths, ai, ai->cb->getTopObj(tile));
  130. for(int i = 0; i != paths.size(); i++)
  131. {
  132. auto & path = paths[i];
  133. auto goal = waysToVisit[i];
  134. if(path.exchangeCount > 1 || path.targetHero != hero || path.movementCost() <= 0.0 || goal->invalid())
  135. continue;
  136. float ourValue = (float)tilesDiscovered * tilesDiscovered / path.movementCost();
  137. if(ourValue > bestValue) //avoid costly checks of tiles that don't reveal much
  138. {
  139. auto obj = cb->getTopObj(tile);
  140. // picking up resources does not yield any exploration at all.
  141. // if it blocks the way to some explorable tile AIPathfinder will take care of it
  142. if(obj && obj->isBlockedVisitable())
  143. {
  144. continue;
  145. }
  146. if(isSafeToVisit(hero, path.heroArmy, path.getTotalDanger(), ai->settings->getSafeAttackRatio()))
  147. {
  148. bestGoal = goal;
  149. bestValue = ourValue;
  150. bestTile = tile;
  151. bestTilesDiscovered = tilesDiscovered;
  152. }
  153. }
  154. }
  155. }
  156. int ExplorationHelper::howManyTilesWillBeDiscovered(const int3 & pos) const
  157. {
  158. int ret = 0;
  159. int3 npos = int3(0, 0, pos.z);
  160. const auto & slice = ts->fogOfWarMap[pos.z];
  161. for(npos.x = pos.x - sightRadius; npos.x <= pos.x + sightRadius; npos.x++)
  162. {
  163. for(npos.y = pos.y - sightRadius; npos.y <= pos.y + sightRadius; npos.y++)
  164. {
  165. if(cbp->isInTheMap(npos)
  166. && pos.dist2d(npos) - 0.5 < sightRadius
  167. && !slice[npos.x][npos.y])
  168. {
  169. if(allowDeadEndCancellation
  170. && !hasReachableNeighbor(npos))
  171. {
  172. continue;
  173. }
  174. ret++;
  175. }
  176. }
  177. }
  178. return ret;
  179. }
  180. bool ExplorationHelper::hasReachableNeighbor(const int3 & pos) const
  181. {
  182. for(const int3 & dir : int3::getDirs())
  183. {
  184. int3 tile = pos + dir;
  185. if(cbp->isInTheMap(tile))
  186. {
  187. auto isAccessible = useCPathfinderAccessibility
  188. ? ai->getPathsInfo(hero)->getPathInfo(tile)->reachable()
  189. : ai->pathfinder->isTileAccessible(hero, tile);
  190. if(isAccessible)
  191. return true;
  192. }
  193. }
  194. return false;
  195. }
  196. }