ExplorationHelper.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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)
  25. :ai(ai), cbp(ai->cb.get()), hero(hero)
  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> from;
  63. std::vector<int3> to;
  64. from.reserve(perimeter);
  65. to.reserve(perimeter);
  66. foreach_tile_pos([&](const int3 & pos)
  67. {
  68. if((*(ts->fogOfWarMap))[pos.z][pos.x][pos.y])
  69. {
  70. bool hasInvisibleNeighbor = false;
  71. foreach_neighbour(cbp, pos, [&](CCallback * cbp, int3 neighbour)
  72. {
  73. if(!(*(ts->fogOfWarMap))[neighbour.z][neighbour.x][neighbour.y])
  74. {
  75. hasInvisibleNeighbor = true;
  76. }
  77. });
  78. if(hasInvisibleNeighbor)
  79. from.push_back(pos);
  80. }
  81. });
  82. logAi->debug("Exploration scan visible area perimeter for hero %s", hero->getNameTranslated());
  83. for(const int3 & tile : from)
  84. {
  85. scanTile(tile);
  86. }
  87. if(!bestGoal->invalid())
  88. {
  89. return false;
  90. }
  91. allowDeadEndCancellation = false;
  92. for(int i = 0; i < sightRadius; i++)
  93. {
  94. getVisibleNeighbours(from, to);
  95. vstd::concatenate(from, to);
  96. vstd::removeDuplicates(from);
  97. }
  98. logAi->debug("Exploration scan all possible tiles for hero %s", hero->getNameTranslated());
  99. for(const int3 & tile : from)
  100. {
  101. scanTile(tile);
  102. }
  103. return !bestGoal->invalid();
  104. }
  105. void ExplorationHelper::scanTile(const int3 & tile)
  106. {
  107. if(tile == ourPos
  108. || !ai->cb->getTile(tile, false)
  109. || !ai->pathfinder->isTileAccessible(hero, tile)) //shouldn't happen, but it does
  110. return;
  111. int tilesDiscovered = howManyTilesWillBeDiscovered(tile);
  112. if(!tilesDiscovered)
  113. return;
  114. auto paths = ai->pathfinder->getPathInfo(tile);
  115. auto waysToVisit = CaptureObjectsBehavior::getVisitGoals(paths, ai, ai->cb->getTopObj(tile));
  116. for(int i = 0; i != paths.size(); i++)
  117. {
  118. auto & path = paths[i];
  119. auto goal = waysToVisit[i];
  120. if(path.exchangeCount > 1 || path.targetHero != hero || path.movementCost() <= 0.0 || goal->invalid())
  121. continue;
  122. float ourValue = (float)tilesDiscovered * tilesDiscovered / path.movementCost();
  123. if(ourValue > bestValue) //avoid costly checks of tiles that don't reveal much
  124. {
  125. auto obj = cb->getTopObj(tile);
  126. // picking up resources does not yield any exploration at all.
  127. // if it blocks the way to some explorable tile AIPathfinder will take care of it
  128. if(obj && obj->isBlockedVisitable())
  129. {
  130. continue;
  131. }
  132. if(isSafeToVisit(hero, path.heroArmy, path.getTotalDanger()))
  133. {
  134. bestGoal = goal;
  135. bestValue = ourValue;
  136. bestTile = tile;
  137. bestTilesDiscovered = tilesDiscovered;
  138. }
  139. }
  140. }
  141. }
  142. void ExplorationHelper::getVisibleNeighbours(const std::vector<int3> & tiles, std::vector<int3> & out) const
  143. {
  144. for(const int3 & tile : tiles)
  145. {
  146. foreach_neighbour(cbp, tile, [&](CCallback * cbp, int3 neighbour)
  147. {
  148. if((*(ts->fogOfWarMap))[neighbour.z][neighbour.x][neighbour.y])
  149. {
  150. out.push_back(neighbour);
  151. }
  152. });
  153. }
  154. }
  155. int ExplorationHelper::howManyTilesWillBeDiscovered(const int3 & pos) const
  156. {
  157. int ret = 0;
  158. int3 npos = int3(0, 0, pos.z);
  159. const auto & slice = (*(ts->fogOfWarMap))[pos.z];
  160. for(npos.x = pos.x - sightRadius; npos.x <= pos.x + sightRadius; npos.x++)
  161. {
  162. for(npos.y = pos.y - sightRadius; npos.y <= pos.y + sightRadius; npos.y++)
  163. {
  164. if(cbp->isInTheMap(npos)
  165. && pos.dist2d(npos) - 0.5 < sightRadius
  166. && !slice[npos.x][npos.y])
  167. {
  168. if(allowDeadEndCancellation
  169. && !hasReachableNeighbor(npos))
  170. {
  171. continue;
  172. }
  173. ret++;
  174. }
  175. }
  176. }
  177. return ret;
  178. }
  179. bool ExplorationHelper::hasReachableNeighbor(const int3 & pos) const
  180. {
  181. for(const int3 & dir : int3::getDirs())
  182. {
  183. int3 tile = pos + dir;
  184. if(cbp->isInTheMap(tile))
  185. {
  186. auto isAccessible = ai->pathfinder->isTileAccessible(hero, tile);
  187. if(isAccessible)
  188. return true;
  189. }
  190. }
  191. return false;
  192. }
  193. }