AIPathfinderConfig.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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 VirtualBoatAction : public ISpecialAction
  19. {
  20. private:
  21. uint64_t specialChain;
  22. public:
  23. VirtualBoatAction(uint64_t specialChain)
  24. :specialChain(specialChain)
  25. {
  26. }
  27. uint64_t getSpecialChain() const
  28. {
  29. return specialChain;
  30. }
  31. };
  32. class BuildBoatAction : public VirtualBoatAction
  33. {
  34. private:
  35. const IShipyard * shipyard;
  36. public:
  37. BuildBoatAction(const IShipyard * shipyard)
  38. :VirtualBoatAction(AINodeStorage::RESOURCE_CHAIN), shipyard(shipyard)
  39. {
  40. }
  41. virtual Goals::TSubgoal whatToDo(HeroPtr hero) const override
  42. {
  43. return sptr(Goals::BuildBoat(shipyard));
  44. }
  45. };
  46. class SummonBoatAction : public VirtualBoatAction
  47. {
  48. public:
  49. SummonBoatAction()
  50. :VirtualBoatAction(AINodeStorage::CAST_CHAIN)
  51. {
  52. }
  53. virtual Goals::TSubgoal whatToDo(HeroPtr hero) const override
  54. {
  55. return sptr(Goals::AdventureSpellCast(hero, SpellID::SUMMON_BOAT));
  56. }
  57. virtual void applyOnDestination(
  58. HeroPtr hero,
  59. CDestinationNodeInfo & destination,
  60. const PathNodeInfo & source,
  61. AIPathNode * dstMode,
  62. const AIPathNode * srcNode) const override
  63. {
  64. dstMode->manaCost = srcNode->manaCost + getManaCost(hero);
  65. dstMode->theNodeBefore = source.node;
  66. }
  67. bool isAffordableBy(HeroPtr hero, const AIPathNode * source) const
  68. {
  69. logAi->trace(
  70. "Hero %s has %d mana and needed %d and already spent %d",
  71. hero->name,
  72. hero->mana,
  73. getManaCost(hero),
  74. source->manaCost);
  75. return hero->mana >= source->manaCost + getManaCost(hero);
  76. }
  77. private:
  78. uint32_t getManaCost(HeroPtr hero) const
  79. {
  80. SpellID summonBoat = SpellID::SUMMON_BOAT;
  81. return hero->getSpellCost(summonBoat.toSpell());
  82. }
  83. };
  84. class BattleAction : public ISpecialAction
  85. {
  86. private:
  87. const int3 target;
  88. const HeroPtr hero;
  89. public:
  90. BattleAction(const int3 target)
  91. :target(target)
  92. {
  93. }
  94. virtual Goals::TSubgoal whatToDo(HeroPtr hero) const override
  95. {
  96. return sptr(Goals::VisitTile(target).sethero(hero));
  97. }
  98. };
  99. class AILayerTransitionRule : public LayerTransitionRule
  100. {
  101. private:
  102. CPlayerSpecificInfoCallback * cb;
  103. VCAI * ai;
  104. std::map<int3, std::shared_ptr<const BuildBoatAction>> virtualBoats;
  105. std::shared_ptr<AINodeStorage> nodeStorage;
  106. std::shared_ptr<const SummonBoatAction> summonableVirtualBoat;
  107. public:
  108. AILayerTransitionRule(CPlayerSpecificInfoCallback * cb, VCAI * ai, std::shared_ptr<AINodeStorage> nodeStorage)
  109. :cb(cb), ai(ai), nodeStorage(nodeStorage)
  110. {
  111. setup();
  112. }
  113. virtual void process(
  114. const PathNodeInfo & source,
  115. CDestinationNodeInfo & destination,
  116. const PathfinderConfig * pathfinderConfig,
  117. CPathfinderHelper * pathfinderHelper) const override
  118. {
  119. LayerTransitionRule::process(source, destination, pathfinderConfig, pathfinderHelper);
  120. if(!destination.blocked)
  121. {
  122. return;
  123. }
  124. if(source.node->layer == EPathfindingLayer::LAND && destination.node->layer == EPathfindingLayer::SAIL)
  125. {
  126. std::shared_ptr<const VirtualBoatAction> virtualBoat = findVirtualBoat(destination, source);
  127. if(virtualBoat && tryEmbarkVirtualBoat(destination, source, virtualBoat))
  128. {
  129. logAi->trace("Embarking to virtual boat while moving %s -> %s!", source.coord.toString(), destination.coord.toString());
  130. }
  131. }
  132. }
  133. private:
  134. void setup()
  135. {
  136. std::vector<const IShipyard *> shipyards;
  137. for(const CGTownInstance * t : cb->getTownsInfo())
  138. {
  139. if(t->hasBuilt(BuildingID::SHIPYARD))
  140. shipyards.push_back(t);
  141. }
  142. for(const CGObjectInstance * obj : ai->visitableObjs)
  143. {
  144. if(obj->ID != Obj::TOWN) //towns were handled in the previous loop
  145. {
  146. if(const IShipyard * shipyard = IShipyard::castFrom(obj))
  147. shipyards.push_back(shipyard);
  148. }
  149. }
  150. for(const IShipyard * shipyard : shipyards)
  151. {
  152. if(shipyard->shipyardStatus() == IShipyard::GOOD)
  153. {
  154. int3 boatLocation = shipyard->bestLocation();
  155. virtualBoats[boatLocation] = std::make_shared<BuildBoatAction>(shipyard);
  156. logAi->debug("Virtual boat added at %s", boatLocation.toString());
  157. }
  158. }
  159. auto hero = nodeStorage->getHero();
  160. if(vstd::contains(hero->spells, SpellID::SUMMON_BOAT))
  161. {
  162. auto summonBoatSpell = SpellID(SpellID::SUMMON_BOAT).toSpell();
  163. if(hero->getSpellSchoolLevel(summonBoatSpell) == SecSkillLevel::EXPERT)
  164. {
  165. summonableVirtualBoat.reset(new SummonBoatAction());
  166. }
  167. }
  168. }
  169. std::shared_ptr<const VirtualBoatAction> findVirtualBoat(
  170. CDestinationNodeInfo &destination,
  171. const PathNodeInfo &source) const
  172. {
  173. std::shared_ptr<const VirtualBoatAction> virtualBoat;
  174. if(vstd::contains(virtualBoats, destination.coord))
  175. {
  176. virtualBoat = virtualBoats.at(destination.coord);
  177. }
  178. else if(
  179. summonableVirtualBoat
  180. && summonableVirtualBoat->isAffordableBy(nodeStorage->getHero(), nodeStorage->getAINode(source.node)))
  181. {
  182. virtualBoat = summonableVirtualBoat;
  183. }
  184. return virtualBoat;
  185. }
  186. bool tryEmbarkVirtualBoat(
  187. CDestinationNodeInfo &destination,
  188. const PathNodeInfo &source,
  189. std::shared_ptr<const VirtualBoatAction> virtualBoat) const
  190. {
  191. bool result = false;
  192. nodeStorage->updateAINode(destination.node, [&](AIPathNode * node)
  193. {
  194. auto boatNodeOptional = nodeStorage->getOrCreateNode(
  195. node->coord,
  196. node->layer,
  197. node->chainMask | virtualBoat->getSpecialChain());
  198. if(boatNodeOptional)
  199. {
  200. AIPathNode * boatNode = boatNodeOptional.get();
  201. if(boatNode->action == CGPathNode::NOT_SET)
  202. {
  203. boatNode->specialAction = virtualBoat;
  204. destination.blocked = false;
  205. destination.action = CGPathNode::ENodeAction::EMBARK;
  206. destination.node = boatNode;
  207. result = true;
  208. }
  209. else
  210. {
  211. logAi->trace(
  212. "Special transition node already allocated. Blocked moving %s -> %s",
  213. source.coord.toString(),
  214. destination.coord.toString());
  215. }
  216. }
  217. else
  218. {
  219. logAi->trace(
  220. "Can not allocate special transition node while moving %s -> %s",
  221. source.coord.toString(),
  222. destination.coord.toString());
  223. }
  224. });
  225. return result;
  226. }
  227. };
  228. class AIMovementAfterDestinationRule : public MovementAfterDestinationRule
  229. {
  230. private:
  231. CPlayerSpecificInfoCallback * cb;
  232. std::shared_ptr<AINodeStorage> nodeStorage;
  233. public:
  234. AIMovementAfterDestinationRule(CPlayerSpecificInfoCallback * cb, std::shared_ptr<AINodeStorage> nodeStorage)
  235. :cb(cb), nodeStorage(nodeStorage)
  236. {
  237. }
  238. virtual void process(
  239. const PathNodeInfo & source,
  240. CDestinationNodeInfo & destination,
  241. const PathfinderConfig * pathfinderConfig,
  242. CPathfinderHelper * pathfinderHelper) const override
  243. {
  244. if(nodeStorage->hasBetterChain(source, destination))
  245. {
  246. destination.blocked = true;
  247. return;
  248. }
  249. auto blocker = getBlockingReason(source, destination, pathfinderConfig, pathfinderHelper);
  250. if(blocker == BlockingReason::NONE)
  251. return;
  252. if(blocker == BlockingReason::DESTINATION_BLOCKVIS && destination.nodeObject)
  253. {
  254. auto objID = destination.nodeObject->ID;
  255. if((objID == Obj::HERO && destination.objectRelations != PlayerRelations::ENEMIES)
  256. || objID == Obj::SUBTERRANEAN_GATE || objID == Obj::MONOLITH_TWO_WAY
  257. || objID == Obj::MONOLITH_ONE_WAY_ENTRANCE || objID == Obj::MONOLITH_ONE_WAY_EXIT
  258. || objID == Obj::WHIRLPOOL)
  259. {
  260. destination.blocked = true;
  261. }
  262. return;
  263. }
  264. if(blocker == BlockingReason::DESTINATION_VISIT)
  265. {
  266. return;
  267. }
  268. if(blocker == BlockingReason::DESTINATION_GUARDED)
  269. {
  270. auto srcGuardians = cb->getGuardingCreatures(source.coord);
  271. auto destGuardians = cb->getGuardingCreatures(destination.coord);
  272. if(destGuardians.empty())
  273. {
  274. destination.blocked = true;
  275. return;
  276. }
  277. vstd::erase_if(destGuardians, [&](const CGObjectInstance * destGuard) -> bool
  278. {
  279. return vstd::contains(srcGuardians, destGuard);
  280. });
  281. auto guardsAlreadyBypassed = destGuardians.empty() && srcGuardians.size();
  282. if(guardsAlreadyBypassed && nodeStorage->isBattleNode(source.node))
  283. {
  284. //logAi->trace(
  285. // "Bypass guard at destination while moving %s -> %s",
  286. // source.coord.toString(),
  287. // destination.coord.toString());
  288. return;
  289. }
  290. const AIPathNode * destNode = nodeStorage->getAINode(destination.node);
  291. auto battleNodeOptional = nodeStorage->getOrCreateNode(
  292. destination.coord,
  293. destination.node->layer,
  294. destNode->chainMask | AINodeStorage::BATTLE_CHAIN);
  295. if(!battleNodeOptional)
  296. {
  297. //logAi->trace(
  298. // "Can not allocate battle node while moving %s -> %s",
  299. // source.coord.toString(),
  300. // destination.coord.toString());
  301. destination.blocked = true;
  302. return;
  303. }
  304. AIPathNode * battleNode = battleNodeOptional.get();
  305. if(battleNode->locked)
  306. {
  307. //logAi->trace(
  308. // "Block bypass guard at destination while moving %s -> %s",
  309. // source.coord.toString(),
  310. // destination.coord.toString());
  311. destination.blocked = true;
  312. return;
  313. }
  314. auto hero = nodeStorage->getHero();
  315. auto danger = evaluateDanger(destination.coord, hero);
  316. destination.node = battleNode;
  317. nodeStorage->commit(destination, source);
  318. if(battleNode->danger < danger)
  319. {
  320. battleNode->danger = danger;
  321. }
  322. battleNode->specialAction = std::make_shared<BattleAction>(destination.coord);
  323. //logAi->trace(
  324. // "Begin bypass guard at destination with danger %s while moving %s -> %s",
  325. // std::to_string(danger),
  326. // source.coord.toString(),
  327. // destination.coord.toString());
  328. return;
  329. }
  330. destination.blocked = true;
  331. }
  332. };
  333. class AIMovementToDestinationRule : public MovementToDestinationRule
  334. {
  335. private:
  336. CPlayerSpecificInfoCallback * cb;
  337. std::shared_ptr<AINodeStorage> nodeStorage;
  338. public:
  339. AIMovementToDestinationRule(CPlayerSpecificInfoCallback * cb, std::shared_ptr<AINodeStorage> nodeStorage)
  340. :cb(cb), nodeStorage(nodeStorage)
  341. {
  342. }
  343. virtual void process(
  344. const PathNodeInfo & source,
  345. CDestinationNodeInfo & destination,
  346. const PathfinderConfig * pathfinderConfig,
  347. CPathfinderHelper * pathfinderHelper) const override
  348. {
  349. auto blocker = getBlockingReason(source, destination, pathfinderConfig, pathfinderHelper);
  350. if(blocker == BlockingReason::NONE)
  351. return;
  352. if(blocker == BlockingReason::DESTINATION_BLOCKED
  353. && destination.action == CGPathNode::EMBARK
  354. && nodeStorage->getAINode(destination.node)->specialAction)
  355. {
  356. return;
  357. }
  358. if(blocker == BlockingReason::SOURCE_GUARDED && nodeStorage->isBattleNode(source.node))
  359. {
  360. //logAi->trace(
  361. // "Bypass src guard while moving from %s to %s",
  362. // source.coord.toString(),
  363. // destination.coord.toString());
  364. return;
  365. }
  366. destination.blocked = true;
  367. }
  368. };
  369. class AIPreviousNodeRule : public MovementToDestinationRule
  370. {
  371. private:
  372. CPlayerSpecificInfoCallback * cb;
  373. std::shared_ptr<AINodeStorage> nodeStorage;
  374. public:
  375. AIPreviousNodeRule(CPlayerSpecificInfoCallback * cb, std::shared_ptr<AINodeStorage> nodeStorage)
  376. :cb(cb), nodeStorage(nodeStorage)
  377. {
  378. }
  379. virtual void process(
  380. const PathNodeInfo & source,
  381. CDestinationNodeInfo & destination,
  382. const PathfinderConfig * pathfinderConfig,
  383. CPathfinderHelper * pathfinderHelper) const override
  384. {
  385. if(source.node->action == CGPathNode::ENodeAction::BLOCKING_VISIT || source.node->action == CGPathNode::ENodeAction::VISIT)
  386. {
  387. // we can not directly bypass objects, we need to interact with them first
  388. destination.node->theNodeBefore = source.node;
  389. //logAi->trace(
  390. // "Link src node %s to destination node %s while bypassing visitable obj",
  391. // source.coord.toString(),
  392. // destination.coord.toString());
  393. return;
  394. }
  395. auto aiSourceNode = nodeStorage->getAINode(source.node);
  396. if(aiSourceNode->specialAction)
  397. {
  398. // there is some action on source tile which should be performed before we can bypass it
  399. destination.node->theNodeBefore = source.node;
  400. }
  401. }
  402. };
  403. std::vector<std::shared_ptr<IPathfindingRule>> makeRuleset(
  404. CPlayerSpecificInfoCallback * cb,
  405. VCAI * ai,
  406. std::shared_ptr<AINodeStorage> nodeStorage)
  407. {
  408. std::vector<std::shared_ptr<IPathfindingRule>> rules = {
  409. std::make_shared<AILayerTransitionRule>(cb, ai, nodeStorage),
  410. std::make_shared<DestinationActionRule>(),
  411. std::make_shared<AIMovementToDestinationRule>(cb, nodeStorage),
  412. std::make_shared<MovementCostRule>(),
  413. std::make_shared<AIPreviousNodeRule>(cb, nodeStorage),
  414. std::make_shared<AIMovementAfterDestinationRule>(cb, nodeStorage)
  415. };
  416. return rules;
  417. }
  418. AIPathfinderConfig::AIPathfinderConfig(
  419. CPlayerSpecificInfoCallback * cb,
  420. VCAI * ai,
  421. std::shared_ptr<AINodeStorage> nodeStorage)
  422. :PathfinderConfig(nodeStorage, makeRuleset(cb, ai, nodeStorage))
  423. {
  424. }
  425. }