AIPathfinderConfig.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * AIhelper.h, 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 "AIPathfinderConfig.h"
  12. #include "../../../CCallback.h"
  13. #include "../../../lib/mapping/CMap.h"
  14. #include "../../../lib/mapObjects/MapObjects.h"
  15. class BuildBoatAction : public ISpecialAction
  16. {
  17. private:
  18. const IShipyard * shipyard;
  19. public:
  20. BuildBoatAction(const IShipyard * shipyard)
  21. :shipyard(shipyard)
  22. {
  23. }
  24. virtual Goals::TSubgoal whatToDo(HeroPtr hero) const override
  25. {
  26. return sptr(Goals::BuildBoat(shipyard));
  27. }
  28. };
  29. class AILayerTransitionRule : public LayerTransitionRule
  30. {
  31. private:
  32. CPlayerSpecificInfoCallback * cb;
  33. VCAI * ai;
  34. std::map<int3, std::shared_ptr<const BuildBoatAction>> virtualBoats;
  35. std::shared_ptr<AINodeStorage> nodeStorage;
  36. public:
  37. AILayerTransitionRule(CPlayerSpecificInfoCallback * cb, VCAI * ai, std::shared_ptr<AINodeStorage> nodeStorage)
  38. :cb(cb), ai(ai), nodeStorage(nodeStorage)
  39. {
  40. setup();
  41. }
  42. virtual void process(
  43. const PathNodeInfo & source,
  44. CDestinationNodeInfo & destination,
  45. const PathfinderConfig * pathfinderConfig,
  46. CPathfinderHelper * pathfinderHelper) const override
  47. {
  48. LayerTransitionRule::process(source, destination, pathfinderConfig, pathfinderHelper);
  49. if(!destination.blocked)
  50. {
  51. return;
  52. }
  53. if(source.node->layer == EPathfindingLayer::LAND && destination.node->layer == EPathfindingLayer::SAIL
  54. && vstd::contains(virtualBoats, destination.coord))
  55. {
  56. logAi->trace("Bypassing virtual boat at %s!", destination.coord.toString());
  57. nodeStorage->updateAINode(destination.node, [&](AIPathNode * node)
  58. {
  59. std::shared_ptr<const BuildBoatAction> virtualBoat = virtualBoats.at(destination.coord);
  60. auto boatNodeOptional = nodeStorage->getOrCreateNode(
  61. node->coord,
  62. node->layer,
  63. node->chainMask | AINodeStorage::RESOURCE_CHAIN);
  64. if(boatNodeOptional)
  65. {
  66. AIPathNode * boatNode = boatNodeOptional.get();
  67. boatNode->specialAction = virtualBoat;
  68. destination.blocked = false;
  69. destination.action = CGPathNode::ENodeAction::EMBARK;
  70. destination.node = boatNode;
  71. }
  72. else
  73. {
  74. logAi->trace(
  75. "Can not allocate boat node while moving %s -> %s",
  76. source.coord.toString(),
  77. destination.coord.toString());
  78. }
  79. });
  80. }
  81. }
  82. private:
  83. void setup()
  84. {
  85. std::vector<const IShipyard *> shipyards;
  86. for(const CGTownInstance * t : cb->getTownsInfo())
  87. {
  88. if(t->hasBuilt(BuildingID::SHIPYARD))
  89. shipyards.push_back(t);
  90. }
  91. for(const CGObjectInstance * obj : ai->visitableObjs)
  92. {
  93. if(obj->ID != Obj::TOWN) //towns were handled in the previous loop
  94. {
  95. if(const IShipyard * shipyard = IShipyard::castFrom(obj))
  96. shipyards.push_back(shipyard);
  97. }
  98. }
  99. for(const IShipyard * shipyard : shipyards)
  100. {
  101. if(shipyard->shipyardStatus() == IShipyard::GOOD)
  102. {
  103. int3 boatLocation = shipyard->bestLocation();
  104. virtualBoats[boatLocation] = std::make_shared<BuildBoatAction>(shipyard);
  105. logAi->debug("Virtual boat added at %s", boatLocation.toString());
  106. }
  107. }
  108. }
  109. };
  110. class AIMovementAfterDestinationRule : public MovementAfterDestinationRule
  111. {
  112. private:
  113. CPlayerSpecificInfoCallback * cb;
  114. std::shared_ptr<AINodeStorage> nodeStorage;
  115. public:
  116. AIMovementAfterDestinationRule(CPlayerSpecificInfoCallback * cb, std::shared_ptr<AINodeStorage> nodeStorage)
  117. :cb(cb), nodeStorage(nodeStorage)
  118. {
  119. }
  120. virtual void process(
  121. const PathNodeInfo & source,
  122. CDestinationNodeInfo & destination,
  123. const PathfinderConfig * pathfinderConfig,
  124. CPathfinderHelper * pathfinderHelper) const override
  125. {
  126. if(nodeStorage->hasBetterChain(source, destination))
  127. {
  128. destination.blocked = true;
  129. return;
  130. }
  131. auto blocker = getBlockingReason(source, destination, pathfinderConfig, pathfinderHelper);
  132. if(blocker == BlockingReason::NONE)
  133. return;
  134. if(blocker == BlockingReason::DESTINATION_BLOCKVIS && destination.nodeObject)
  135. {
  136. auto objID = destination.nodeObject->ID;
  137. if((objID == Obj::HERO && destination.objectRelations != PlayerRelations::ENEMIES)
  138. || objID == Obj::SUBTERRANEAN_GATE || objID == Obj::MONOLITH_TWO_WAY
  139. || objID == Obj::MONOLITH_ONE_WAY_ENTRANCE || objID == Obj::MONOLITH_ONE_WAY_EXIT
  140. || objID == Obj::WHIRLPOOL)
  141. {
  142. destination.blocked = true;
  143. }
  144. return;
  145. }
  146. if(blocker == BlockingReason::DESTINATION_VISIT)
  147. {
  148. return;
  149. }
  150. if(blocker == BlockingReason::DESTINATION_GUARDED)
  151. {
  152. auto srcGuardians = cb->getGuardingCreatures(source.coord);
  153. auto destGuardians = cb->getGuardingCreatures(destination.coord);
  154. if(destGuardians.empty())
  155. {
  156. destination.blocked = true;
  157. return;
  158. }
  159. vstd::erase_if(destGuardians, [&](const CGObjectInstance * destGuard) -> bool
  160. {
  161. return vstd::contains(srcGuardians, destGuard);
  162. });
  163. auto guardsAlreadyBypassed = destGuardians.empty() && srcGuardians.size();
  164. if(guardsAlreadyBypassed && nodeStorage->isBattleNode(source.node))
  165. {
  166. logAi->trace(
  167. "Bypass guard at destination while moving %s -> %s",
  168. source.coord.toString(),
  169. destination.coord.toString());
  170. return;
  171. }
  172. const AIPathNode * destNode = nodeStorage->getAINode(destination.node);
  173. auto battleNodeOptional = nodeStorage->getOrCreateNode(
  174. destination.coord,
  175. destination.node->layer,
  176. destNode->chainMask | AINodeStorage::BATTLE_CHAIN);
  177. if(!battleNodeOptional)
  178. {
  179. logAi->trace(
  180. "Can not allocate battle node while moving %s -> %s",
  181. source.coord.toString(),
  182. destination.coord.toString());
  183. destination.blocked = true;
  184. return;
  185. }
  186. AIPathNode * battleNode = battleNodeOptional.get();
  187. if(battleNode->locked)
  188. {
  189. logAi->trace(
  190. "Block bypass guard at destination while moving %s -> %s",
  191. source.coord.toString(),
  192. destination.coord.toString());
  193. destination.blocked = true;
  194. return;
  195. }
  196. auto hero = nodeStorage->getHero();
  197. auto danger = evaluateDanger(destination.coord, hero);
  198. destination.node = battleNode;
  199. nodeStorage->commit(destination, source);
  200. if(battleNode->danger < danger)
  201. {
  202. battleNode->danger = danger;
  203. }
  204. logAi->trace(
  205. "Begin bypass guard at destination with danger %s while moving %s -> %s",
  206. std::to_string(danger),
  207. source.coord.toString(),
  208. destination.coord.toString());
  209. return;
  210. }
  211. destination.blocked = true;
  212. }
  213. };
  214. class AIMovementToDestinationRule : public MovementToDestinationRule
  215. {
  216. private:
  217. CPlayerSpecificInfoCallback * cb;
  218. std::shared_ptr<AINodeStorage> nodeStorage;
  219. public:
  220. AIMovementToDestinationRule(CPlayerSpecificInfoCallback * cb, std::shared_ptr<AINodeStorage> nodeStorage)
  221. :cb(cb), nodeStorage(nodeStorage)
  222. {
  223. }
  224. virtual void process(
  225. const PathNodeInfo & source,
  226. CDestinationNodeInfo & destination,
  227. const PathfinderConfig * pathfinderConfig,
  228. CPathfinderHelper * pathfinderHelper) const override
  229. {
  230. auto blocker = getBlockingReason(source, destination, pathfinderConfig, pathfinderHelper);
  231. if(blocker == BlockingReason::NONE)
  232. return;
  233. if(blocker == BlockingReason::DESTINATION_BLOCKED
  234. && destination.action == CGPathNode::EMBARK
  235. && nodeStorage->getAINode(destination.node)->specialAction)
  236. {
  237. return;
  238. }
  239. if(blocker == BlockingReason::SOURCE_GUARDED && nodeStorage->isBattleNode(source.node))
  240. {
  241. auto srcGuardians = cb->getGuardingCreatures(source.coord);
  242. auto destGuardians = cb->getGuardingCreatures(destination.coord);
  243. for(auto srcGuard : srcGuardians)
  244. {
  245. if(!vstd::contains(destGuardians, srcGuard))
  246. continue;
  247. auto guardPos = srcGuard->visitablePos();
  248. if(guardPos != source.coord && guardPos != destination.coord)
  249. {
  250. destination.blocked = true; // allow to pass monster only through guard tile
  251. }
  252. }
  253. if(!destination.blocked)
  254. {
  255. logAi->trace(
  256. "Bypass src guard while moving from %s to %s",
  257. source.coord.toString(),
  258. destination.coord.toString());
  259. }
  260. return;
  261. }
  262. destination.blocked = true;
  263. }
  264. };
  265. class AIPreviousNodeRule : public MovementToDestinationRule
  266. {
  267. private:
  268. CPlayerSpecificInfoCallback * cb;
  269. std::shared_ptr<AINodeStorage> nodeStorage;
  270. public:
  271. AIPreviousNodeRule(CPlayerSpecificInfoCallback * cb, std::shared_ptr<AINodeStorage> nodeStorage)
  272. :cb(cb), nodeStorage(nodeStorage)
  273. {
  274. }
  275. virtual void process(
  276. const PathNodeInfo & source,
  277. CDestinationNodeInfo & destination,
  278. const PathfinderConfig * pathfinderConfig,
  279. CPathfinderHelper * pathfinderHelper) const override
  280. {
  281. //FIXME: unused
  282. // auto blocker = getBlockingReason(source, destination, pathfinderConfig, pathfinderHelper);
  283. if(source.guarded)
  284. {
  285. auto srcGuardian = cb->guardingCreaturePosition(source.node->coord);
  286. if(srcGuardian == source.node->coord)
  287. {
  288. // guardian tile is used as chain junction
  289. destination.node->theNodeBefore = source.node;
  290. logAi->trace(
  291. "Link src node %s to destination node %s while bypassing guard",
  292. source.coord.toString(),
  293. destination.coord.toString());
  294. return;
  295. }
  296. }
  297. if(source.node->action == CGPathNode::ENodeAction::BLOCKING_VISIT || source.node->action == CGPathNode::ENodeAction::VISIT)
  298. {
  299. // we can not directly bypass objects, we need to interact with them first
  300. destination.node->theNodeBefore = source.node;
  301. logAi->trace(
  302. "Link src node %s to destination node %s while bypassing visitable obj",
  303. source.coord.toString(),
  304. destination.coord.toString());
  305. return;
  306. }
  307. auto aiSourceNode = nodeStorage->getAINode(source.node);
  308. if(aiSourceNode->specialAction)
  309. {
  310. // there is some action on source tile which should be performed before we can bypass it
  311. destination.node->theNodeBefore = source.node;
  312. }
  313. }
  314. };
  315. std::vector<std::shared_ptr<IPathfindingRule>> makeRuleset(
  316. CPlayerSpecificInfoCallback * cb,
  317. VCAI * ai,
  318. std::shared_ptr<AINodeStorage> nodeStorage)
  319. {
  320. std::vector<std::shared_ptr<IPathfindingRule>> rules = {
  321. std::make_shared<AILayerTransitionRule>(cb, ai, nodeStorage),
  322. std::make_shared<DestinationActionRule>(),
  323. std::make_shared<AIMovementToDestinationRule>(cb, nodeStorage),
  324. std::make_shared<MovementCostRule>(),
  325. std::make_shared<AIPreviousNodeRule>(cb, nodeStorage),
  326. std::make_shared<AIMovementAfterDestinationRule>(cb, nodeStorage)
  327. };
  328. return rules;
  329. }
  330. AIPathfinderConfig::AIPathfinderConfig(
  331. CPlayerSpecificInfoCallback * cb,
  332. VCAI * ai,
  333. std::shared_ptr<AINodeStorage> nodeStorage)
  334. :PathfinderConfig(nodeStorage, makeRuleset(cb, ai, nodeStorage))
  335. {
  336. }