ExplorationBehavior.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * ExplorationBehavior.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 "ExplorationBehavior.h"
  12. #include "../AIGateway.h"
  13. #include "../AIUtility.h"
  14. #include "../Goals/Invalid.h"
  15. #include "../Goals/Composition.h"
  16. #include "../Goals/ExecuteHeroChain.h"
  17. #include "../Markers/ExplorationPoint.h"
  18. #include "CaptureObjectsBehavior.h"
  19. #include "../Goals/CaptureObject.h"
  20. #include "../../../lib/CPlayerState.h"
  21. namespace NKAI
  22. {
  23. using namespace Goals;
  24. struct ExplorationHelper
  25. {
  26. const CGHeroInstance * hero;
  27. int sightRadius;
  28. float bestValue;
  29. TSubgoal bestGoal;
  30. int3 bestTile;
  31. int bestTilesDiscovered;
  32. const Nullkiller * ai;
  33. CCallback * cbp;
  34. const TeamState * ts;
  35. int3 ourPos;
  36. bool allowDeadEndCancellation;
  37. ExplorationHelper(const CGHeroInstance * hero, const Nullkiller * ai)
  38. :ai(ai), cbp(ai->cb.get()), hero(hero)
  39. {
  40. ts = cbp->getPlayerTeam(ai->playerID);
  41. sightRadius = hero->getSightRadius();
  42. bestGoal = sptr(Goals::Invalid());
  43. bestValue = 0;
  44. bestTilesDiscovered = 0;
  45. ourPos = hero->visitablePos();
  46. allowDeadEndCancellation = true;
  47. }
  48. TSubgoal makeComposition() const
  49. {
  50. Composition c;
  51. c.addNext(ExplorationPoint(bestTile, bestTilesDiscovered));
  52. c.addNext(bestGoal);
  53. return sptr(c);
  54. }
  55. void scanSector(int scanRadius)
  56. {
  57. int3 tile = int3(0, 0, ourPos.z);
  58. const auto & slice = (*(ts->fogOfWarMap))[ourPos.z];
  59. for(tile.x = ourPos.x - scanRadius; tile.x <= ourPos.x + scanRadius; tile.x++)
  60. {
  61. for(tile.y = ourPos.y - scanRadius; tile.y <= ourPos.y + scanRadius; tile.y++)
  62. {
  63. if(cbp->isInTheMap(tile) && slice[tile.x][tile.y])
  64. {
  65. scanTile(tile);
  66. }
  67. }
  68. }
  69. }
  70. void scanMap()
  71. {
  72. int3 mapSize = cbp->getMapSize();
  73. int perimeter = 2 * sightRadius * (mapSize.x + mapSize.y);
  74. std::vector<int3> from;
  75. std::vector<int3> to;
  76. from.reserve(perimeter);
  77. to.reserve(perimeter);
  78. foreach_tile_pos([&](const int3 & pos)
  79. {
  80. if((*(ts->fogOfWarMap))[pos.z][pos.x][pos.y])
  81. {
  82. bool hasInvisibleNeighbor = false;
  83. foreach_neighbour(cbp, pos, [&](CCallback * cbp, int3 neighbour)
  84. {
  85. if(!(*(ts->fogOfWarMap))[neighbour.z][neighbour.x][neighbour.y])
  86. {
  87. hasInvisibleNeighbor = true;
  88. }
  89. });
  90. if(hasInvisibleNeighbor)
  91. from.push_back(pos);
  92. }
  93. });
  94. logAi->debug("Exploration scan visible area perimeter for hero %s", hero->getNameTranslated());
  95. for(const int3 & tile : from)
  96. {
  97. scanTile(tile);
  98. }
  99. if(!bestGoal->invalid())
  100. {
  101. return;
  102. }
  103. allowDeadEndCancellation = false;
  104. for(int i = 0; i < sightRadius; i++)
  105. {
  106. getVisibleNeighbours(from, to);
  107. vstd::concatenate(from, to);
  108. vstd::removeDuplicates(from);
  109. }
  110. logAi->debug("Exploration scan all possible tiles for hero %s", hero->getNameTranslated());
  111. for(const int3 & tile : from)
  112. {
  113. scanTile(tile);
  114. }
  115. }
  116. void scanTile(const int3 & tile)
  117. {
  118. if(tile == ourPos
  119. || !ai->pathfinder->isTileAccessible(hero, tile)) //shouldn't happen, but it does
  120. return;
  121. int tilesDiscovered = howManyTilesWillBeDiscovered(tile);
  122. if(!tilesDiscovered)
  123. return;
  124. auto paths = ai->pathfinder->getPathInfo(tile);
  125. auto waysToVisit = CaptureObjectsBehavior::getVisitGoals(paths, ai, ai->cb->getTopObj(tile));
  126. for(int i = 0; i != paths.size(); i++)
  127. {
  128. auto & path = paths[i];
  129. auto goal = waysToVisit[i];
  130. if(path.exchangeCount > 1 || path.targetHero != hero || path.movementCost() <= 0.0 || goal->invalid())
  131. continue;
  132. float ourValue = (float)tilesDiscovered * tilesDiscovered / path.movementCost();
  133. if(ourValue > bestValue) //avoid costly checks of tiles that don't reveal much
  134. {
  135. auto obj = cb->getTopObj(tile);
  136. // picking up resources does not yield any exploration at all.
  137. // if it blocks the way to some explorable tile AIPathfinder will take care of it
  138. if(obj && obj->isBlockedVisitable())
  139. {
  140. continue;
  141. }
  142. if(isSafeToVisit(hero, path.heroArmy, path.getTotalDanger()))
  143. {
  144. bestGoal = goal;
  145. bestValue = ourValue;
  146. bestTile = tile;
  147. bestTilesDiscovered = tilesDiscovered;
  148. }
  149. }
  150. }
  151. }
  152. void getVisibleNeighbours(const std::vector<int3> & tiles, std::vector<int3> & out) const
  153. {
  154. for(const int3 & tile : tiles)
  155. {
  156. foreach_neighbour(cbp, tile, [&](CCallback * cbp, int3 neighbour)
  157. {
  158. if((*(ts->fogOfWarMap))[neighbour.z][neighbour.x][neighbour.y])
  159. {
  160. out.push_back(neighbour);
  161. }
  162. });
  163. }
  164. }
  165. int howManyTilesWillBeDiscovered(const int3 & pos) const
  166. {
  167. int ret = 0;
  168. int3 npos = int3(0, 0, pos.z);
  169. const auto & slice = (*(ts->fogOfWarMap))[pos.z];
  170. for(npos.x = pos.x - sightRadius; npos.x <= pos.x + sightRadius; npos.x++)
  171. {
  172. for(npos.y = pos.y - sightRadius; npos.y <= pos.y + sightRadius; npos.y++)
  173. {
  174. if(cbp->isInTheMap(npos)
  175. && pos.dist2d(npos) - 0.5 < sightRadius
  176. && !slice[npos.x][npos.y])
  177. {
  178. if(allowDeadEndCancellation
  179. && !hasReachableNeighbor(npos))
  180. {
  181. continue;
  182. }
  183. ret++;
  184. }
  185. }
  186. }
  187. return ret;
  188. }
  189. bool hasReachableNeighbor(const int3 & pos) const
  190. {
  191. for(const int3 & dir : int3::getDirs())
  192. {
  193. int3 tile = pos + dir;
  194. if(cbp->isInTheMap(tile))
  195. {
  196. auto isAccessible = ai->pathfinder->isTileAccessible(hero, tile);
  197. if(isAccessible)
  198. return true;
  199. }
  200. }
  201. return false;
  202. }
  203. };
  204. std::string ExplorationBehavior::toString() const
  205. {
  206. return "Explore";
  207. }
  208. Goals::TGoalVec ExplorationBehavior::decompose(const Nullkiller * ai) const
  209. {
  210. Goals::TGoalVec tasks;
  211. for(auto obj : ai->memory->visitableObjs)
  212. {
  213. if(!vstd::contains(ai->memory->alreadyVisited, obj))
  214. {
  215. switch(obj->ID.num)
  216. {
  217. case Obj::REDWOOD_OBSERVATORY:
  218. case Obj::PILLAR_OF_FIRE:
  219. tasks.push_back(sptr(Composition().addNext(ExplorationPoint(obj->visitablePos(), 200)).addNext(CaptureObject(obj))));
  220. break;
  221. case Obj::MONOLITH_ONE_WAY_ENTRANCE:
  222. case Obj::MONOLITH_TWO_WAY:
  223. case Obj::SUBTERRANEAN_GATE:
  224. auto tObj = dynamic_cast<const CGTeleport *>(obj);
  225. if(TeleportChannel::IMPASSABLE != ai->memory->knownTeleportChannels[tObj->channel]->passability)
  226. {
  227. tasks.push_back(sptr(Composition().addNext(ExplorationPoint(obj->visitablePos(), 50)).addNext(CaptureObject(obj))));
  228. }
  229. break;
  230. }
  231. }
  232. else
  233. {
  234. switch(obj->ID.num)
  235. {
  236. case Obj::MONOLITH_TWO_WAY:
  237. case Obj::SUBTERRANEAN_GATE:
  238. auto tObj = dynamic_cast<const CGTeleport *>(obj);
  239. if(TeleportChannel::IMPASSABLE == ai->memory->knownTeleportChannels[tObj->channel]->passability)
  240. break;
  241. for(auto exit : ai->memory->knownTeleportChannels[tObj->channel]->exits)
  242. {
  243. if(!cb->getObj(exit))
  244. {
  245. // Always attempt to visit two-way teleports if one of channel exits is not visible
  246. tasks.push_back(sptr(Composition().addNext(ExplorationPoint(obj->visitablePos(), 50)).addNext(CaptureObject(obj))));
  247. break;
  248. }
  249. }
  250. break;
  251. }
  252. }
  253. }
  254. auto heroes = ai->cb->getHeroesInfo();
  255. for(const CGHeroInstance * hero : heroes)
  256. {
  257. ExplorationHelper scanResult(hero, ai);
  258. scanResult.scanSector(1);
  259. if(!scanResult.bestGoal->invalid())
  260. {
  261. tasks.push_back(scanResult.makeComposition());
  262. continue;
  263. }
  264. scanResult.scanSector(15);
  265. if(!scanResult.bestGoal->invalid())
  266. {
  267. tasks.push_back(scanResult.makeComposition());
  268. continue;
  269. }
  270. if(ai->getScanDepth() == ScanDepth::ALL_FULL)
  271. {
  272. scanResult.scanMap();
  273. if(!scanResult.bestGoal->invalid())
  274. {
  275. tasks.push_back(scanResult.makeComposition());
  276. }
  277. }
  278. }
  279. return tasks;
  280. }
  281. }