AIPathfinderConfig.cpp 10 KB

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