AIMovementAfterDestinationRule.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * AIMovementAfterDestinationRule.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 "AIMovementAfterDestinationRule.h"
  12. #include "../Actions/BattleAction.h"
  13. #include "../Actions/QuestAction.h"
  14. #include "../Actions/WhirlpoolAction.h"
  15. #include "../../Goals/Invalid.h"
  16. #include "AIPreviousNodeRule.h"
  17. #include "../../../../lib/pathfinder/PathfinderOptions.h"
  18. #include "../../../../lib/pathfinder/CPathfinder.h"
  19. namespace NKAI
  20. {
  21. namespace AIPathfinding
  22. {
  23. AIMovementAfterDestinationRule::AIMovementAfterDestinationRule(
  24. const Nullkiller * ai,
  25. CPlayerSpecificInfoCallback * cb,
  26. std::shared_ptr<AINodeStorage> nodeStorage,
  27. bool allowBypassObjects)
  28. :ai(ai), cb(cb), nodeStorage(nodeStorage), allowBypassObjects(allowBypassObjects)
  29. {
  30. }
  31. void AIMovementAfterDestinationRule::process(
  32. const PathNodeInfo & source,
  33. CDestinationNodeInfo & destination,
  34. const PathfinderConfig * pathfinderConfig,
  35. CPathfinderHelper * pathfinderHelper) const
  36. {
  37. if(nodeStorage->isMovementInefficient(source, destination))
  38. {
  39. destination.node->locked = true;
  40. destination.blocked = true;
  41. return;
  42. }
  43. if(!allowBypassObjects
  44. && destination.action == EPathNodeAction::EMBARK
  45. && source.node->layer == EPathfindingLayer::LAND
  46. && destination.node->layer == EPathfindingLayer::SAIL)
  47. {
  48. destination.blocked = true;
  49. return;
  50. }
  51. auto blocker = getBlockingReason(source, destination, pathfinderConfig, pathfinderHelper);
  52. if(blocker == BlockingReason::NONE)
  53. {
  54. destination.blocked = nodeStorage->isDistanceLimitReached(source, destination);
  55. if(destination.nodeObject
  56. && !destination.blocked
  57. && !allowBypassObjects
  58. && !dynamic_cast<const CGTeleport *>(destination.nodeObject)
  59. && destination.nodeObject->ID != Obj::EVENT)
  60. {
  61. destination.blocked = true;
  62. destination.node->locked = true;
  63. }
  64. return;
  65. }
  66. if(!allowBypassObjects)
  67. {
  68. if(destination.nodeObject)
  69. {
  70. destination.blocked = true;
  71. destination.node->locked = true;
  72. }
  73. return;
  74. }
  75. #if NKAI_PATHFINDER_TRACE_LEVEL >= 2
  76. logAi->trace(
  77. "Movement from tile %s is blocked. Try to bypass. Action: %d, blocker: %d, source: %s",
  78. destination.coord.toString(),
  79. (int)destination.action,
  80. (int)blocker,
  81. source.coord.toString());
  82. #endif
  83. auto destGuardians = cb->getGuardingCreatures(destination.coord);
  84. bool allowBypass = false;
  85. switch(blocker)
  86. {
  87. case BlockingReason::DESTINATION_GUARDED:
  88. allowBypass = bypassDestinationGuards(destGuardians, source, destination, pathfinderConfig, pathfinderHelper);
  89. break;
  90. case BlockingReason::DESTINATION_BLOCKVIS:
  91. if(destination.nodeHero && destination.heroRelations != PlayerRelations::ENEMIES)
  92. {
  93. allowBypass = destination.heroRelations == PlayerRelations::SAME_PLAYER
  94. && destination.nodeHero == nodeStorage->getHero(destination.node);
  95. }
  96. else
  97. {
  98. allowBypass = destination.nodeObject && bypassRemovableObject(source, destination, pathfinderConfig, pathfinderHelper);
  99. }
  100. if(allowBypass && destGuardians.size())
  101. allowBypass = bypassDestinationGuards(destGuardians, source, destination, pathfinderConfig, pathfinderHelper);
  102. break;
  103. case BlockingReason::DESTINATION_VISIT:
  104. allowBypass = true;
  105. break;
  106. case BlockingReason::DESTINATION_BLOCKED:
  107. allowBypass = bypassBlocker(source, destination, pathfinderConfig, pathfinderHelper);
  108. break;
  109. }
  110. destination.blocked = !allowBypass || nodeStorage->isDistanceLimitReached(source, destination);
  111. destination.node->locked = !allowBypass;
  112. }
  113. bool AIMovementAfterDestinationRule::bypassBlocker(
  114. const PathNodeInfo & source,
  115. CDestinationNodeInfo & destination,
  116. const PathfinderConfig * pathfinderConfig,
  117. CPathfinderHelper * pathfinderHelper) const
  118. {
  119. auto enemyHero = destination.nodeHero && destination.heroRelations == PlayerRelations::ENEMIES;
  120. if(enemyHero)
  121. {
  122. return bypassBattle(source, destination, pathfinderConfig, pathfinderHelper);
  123. }
  124. if(destination.nodeObject
  125. && (destination.nodeObject->ID == Obj::GARRISON || destination.nodeObject->ID == Obj::GARRISON2)
  126. && destination.objectRelations == PlayerRelations::ENEMIES)
  127. {
  128. return bypassBattle(source, destination, pathfinderConfig, pathfinderHelper);
  129. }
  130. return false;
  131. }
  132. bool AIMovementAfterDestinationRule::bypassQuest(
  133. const PathNodeInfo & source,
  134. CDestinationNodeInfo & destination,
  135. const PathfinderConfig * pathfinderConfig,
  136. CPathfinderHelper * pathfinderHelper) const
  137. {
  138. const AIPathNode * destinationNode = nodeStorage->getAINode(destination.node);
  139. auto questObj = dynamic_cast<const IQuestObject *>(destination.nodeObject);
  140. auto questInfo = QuestInfo(questObj->quest, destination.nodeObject, destination.coord);
  141. QuestAction questAction(questInfo);
  142. if(destination.nodeObject->ID == Obj::QUEST_GUARD
  143. && questObj->quest->mission == Rewardable::Limiter{}
  144. && questObj->quest->killTarget == ObjectInstanceID::NONE)
  145. {
  146. return false;
  147. }
  148. if(!questAction.canAct(ai, destinationNode))
  149. {
  150. if(!destinationNode->actor->allowUseResources)
  151. {
  152. std::optional<AIPathNode *> questNode = nodeStorage->getOrCreateNode(
  153. destination.coord,
  154. destination.node->layer,
  155. destinationNode->actor->resourceActor);
  156. if(!questNode || questNode.value()->getCost() < destination.cost)
  157. {
  158. return false;
  159. }
  160. destination.node = questNode.value();
  161. nodeStorage->commit(destination, source);
  162. AIPreviousNodeRule(nodeStorage).process(source, destination, pathfinderConfig, pathfinderHelper);
  163. }
  164. nodeStorage->updateAINode(destination.node, [&](AIPathNode * node)
  165. {
  166. node->addSpecialAction(std::make_shared<QuestAction>(questAction));
  167. });
  168. }
  169. return true;
  170. }
  171. bool AIMovementAfterDestinationRule::bypassRemovableObject(
  172. const PathNodeInfo & source,
  173. CDestinationNodeInfo & destination,
  174. const PathfinderConfig * pathfinderConfig,
  175. CPathfinderHelper * pathfinderHelper) const
  176. {
  177. if(destination.nodeObject->ID == Obj::QUEST_GUARD
  178. || destination.nodeObject->ID == Obj::BORDERGUARD
  179. || destination.nodeObject->ID == Obj::BORDER_GATE)
  180. {
  181. return bypassQuest(source, destination, pathfinderConfig, pathfinderHelper);
  182. }
  183. auto enemyHero = destination.nodeHero && destination.heroRelations == PlayerRelations::ENEMIES;
  184. if(!enemyHero && !isObjectRemovable(destination.nodeObject))
  185. {
  186. if(nodeStorage->getHero(destination.node) == destination.nodeHero)
  187. return true;
  188. return false;
  189. }
  190. auto danger = ai->dangerEvaluator->evaluateDanger(destination.coord, nodeStorage->getHero(destination.node), true);
  191. if(danger)
  192. {
  193. return bypassBattle(source, destination, pathfinderConfig, pathfinderHelper);
  194. }
  195. return true;
  196. }
  197. bool AIMovementAfterDestinationRule::bypassDestinationGuards(
  198. std::vector<const CGObjectInstance *> destGuardians,
  199. const PathNodeInfo & source,
  200. CDestinationNodeInfo & destination,
  201. const PathfinderConfig * pathfinderConfig,
  202. CPathfinderHelper * pathfinderHelper) const
  203. {
  204. auto srcGuardians = cb->getGuardingCreatures(source.coord);
  205. if(destGuardians.empty())
  206. {
  207. return false;
  208. }
  209. auto srcNode = nodeStorage->getAINode(source.node);
  210. vstd::erase_if(destGuardians, [&](const CGObjectInstance * destGuard) -> bool
  211. {
  212. return vstd::contains(srcGuardians, destGuard);
  213. });
  214. auto guardsAlreadyBypassed = destGuardians.empty() && srcGuardians.size();
  215. if(guardsAlreadyBypassed && srcNode->actor->allowBattle)
  216. {
  217. #if NKAI_PATHFINDER_TRACE_LEVEL >= 1
  218. logAi->trace(
  219. "Bypass guard at destination while moving %s -> %s",
  220. source.coord.toString(),
  221. destination.coord.toString());
  222. #endif
  223. return true;
  224. }
  225. return bypassBattle(source, destination, pathfinderConfig, pathfinderHelper);
  226. }
  227. bool AIMovementAfterDestinationRule::bypassBattle(
  228. const PathNodeInfo & source,
  229. CDestinationNodeInfo & destination,
  230. const PathfinderConfig * pathfinderConfig,
  231. CPathfinderHelper * pathfinderHelper) const
  232. {
  233. const AIPathNode * srcNode = nodeStorage->getAINode(source.node);
  234. const AIPathNode * destNode = nodeStorage->getAINode(destination.node);
  235. auto battleNodeOptional = nodeStorage->getOrCreateNode(
  236. destination.coord,
  237. destination.node->layer,
  238. destNode->actor->battleActor);
  239. if(!battleNodeOptional)
  240. {
  241. #if NKAI_PATHFINDER_TRACE_LEVEL >= 1
  242. logAi->trace(
  243. "Can not allocate battle node while moving %s -> %s",
  244. source.coord.toString(),
  245. destination.coord.toString());
  246. #endif
  247. return false;
  248. }
  249. auto * battleNode = battleNodeOptional.value();
  250. if(battleNode->locked)
  251. {
  252. #if NKAI_PATHFINDER_TRACE_LEVEL >= 1
  253. logAi->trace(
  254. "Block bypass guard at destination while moving %s -> %s",
  255. source.coord.toString(),
  256. destination.coord.toString());
  257. #endif
  258. return false;
  259. }
  260. auto hero = nodeStorage->getHero(source.node);
  261. uint64_t danger = ai->dangerEvaluator->evaluateDanger(destination.coord, hero, true);
  262. uint64_t actualArmyValue = srcNode->actor->armyValue - srcNode->armyLoss;
  263. uint64_t loss = nodeStorage->evaluateArmyLoss(hero, actualArmyValue, danger);
  264. if(loss < actualArmyValue)
  265. {
  266. if(destNode->specialAction)
  267. {
  268. battleNode->specialAction = destNode->specialAction;
  269. }
  270. destination.node = battleNode;
  271. nodeStorage->commit(destination, source);
  272. battleNode->armyLoss += loss;
  273. vstd::amax(battleNode->danger, danger);
  274. AIPreviousNodeRule(nodeStorage).process(source, destination, pathfinderConfig, pathfinderHelper);
  275. battleNode->addSpecialAction(std::make_shared<BattleAction>(destination.coord));
  276. #if NKAI_PATHFINDER_TRACE_LEVEL >= 1
  277. logAi->trace(
  278. "Begin bypass guard at destination with danger %s while moving %s -> %s",
  279. std::to_string(danger),
  280. source.coord.toString(),
  281. destination.coord.toString());
  282. #endif
  283. return true;
  284. }
  285. return false;
  286. }
  287. }
  288. }