AIMovementAfterDestinationRule.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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/mapObjects/CQuest.h"
  18. #include "../../../../lib/pathfinder/PathfinderOptions.h"
  19. #include "../../../../lib/pathfinder/CPathfinder.h"
  20. namespace NK2AI
  21. {
  22. namespace AIPathfinding
  23. {
  24. AIMovementAfterDestinationRule::AIMovementAfterDestinationRule(
  25. const Nullkiller * aiNk,
  26. std::shared_ptr<AINodeStorage> nodeStorage,
  27. bool allowBypassObjects)
  28. :aiNk(aiNk), 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 NK2AI_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 = aiNk->cc->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(destination.nodeObject->id);
  141. QuestAction questAction(questInfo);
  142. if(destination.nodeObject->ID == Obj::QUEST_GUARD
  143. && questObj->getQuest().mission == Rewardable::Limiter{}
  144. && questObj->getQuest().killTarget == ObjectInstanceID::NONE)
  145. {
  146. return false;
  147. }
  148. if(!questAction.canAct(aiNk, destinationNode))
  149. {
  150. if(!destinationNode->actor->allowUseResources)
  151. {
  152. const std::optional<AIPathNode *> questNode = nodeStorage->getOrCreateNode(
  153. destination.coord,
  154. destination.node->layer,
  155. destinationNode->actor->resourceActor);
  156. if (!questNode)
  157. {
  158. #if NK2AI_PATHFINDER_TRACE_LEVEL >= 2
  159. logAi->trace(
  160. "AIMovementAfterDestinationRule::bypassQuest Failed to allocate node at %s[%d]. ",
  161. destination.coord.toString(),
  162. static_cast<int32_t>(destination.node->layer)
  163. );
  164. #endif
  165. return false;
  166. }
  167. if(questNode.value()->getCost() < destination.cost)
  168. {
  169. return false;
  170. }
  171. destination.node = questNode.value();
  172. nodeStorage->commit(destination, source);
  173. AIPreviousNodeRule(nodeStorage).process(source, destination, pathfinderConfig, pathfinderHelper);
  174. }
  175. nodeStorage->updateAINode(destination.node, [&](AIPathNode * node)
  176. {
  177. node->addSpecialAction(std::make_shared<QuestAction>(questAction));
  178. });
  179. }
  180. return true;
  181. }
  182. bool AIMovementAfterDestinationRule::bypassRemovableObject(
  183. const PathNodeInfo & source,
  184. CDestinationNodeInfo & destination,
  185. const PathfinderConfig * pathfinderConfig,
  186. CPathfinderHelper * pathfinderHelper) const
  187. {
  188. if(destination.nodeObject->ID == Obj::QUEST_GUARD
  189. || destination.nodeObject->ID == Obj::BORDERGUARD
  190. || destination.nodeObject->ID == Obj::BORDER_GATE)
  191. {
  192. return bypassQuest(source, destination, pathfinderConfig, pathfinderHelper);
  193. }
  194. auto enemyHero = destination.nodeHero && destination.heroRelations == PlayerRelations::ENEMIES;
  195. if(!enemyHero && !isObjectRemovable(destination.nodeObject))
  196. {
  197. if(nodeStorage->getHero(destination.node) == destination.nodeHero)
  198. return true;
  199. return false;
  200. }
  201. auto danger = aiNk->dangerEvaluator->evaluateDanger(destination.coord, nodeStorage->getHero(destination.node), true);
  202. if(danger)
  203. {
  204. return bypassBattle(source, destination, pathfinderConfig, pathfinderHelper);
  205. }
  206. return true;
  207. }
  208. bool AIMovementAfterDestinationRule::bypassDestinationGuards(
  209. std::vector<const CGObjectInstance *> destGuardians,
  210. const PathNodeInfo & source,
  211. CDestinationNodeInfo & destination,
  212. const PathfinderConfig * pathfinderConfig,
  213. CPathfinderHelper * pathfinderHelper) const
  214. {
  215. if(destGuardians.empty())
  216. {
  217. return false;
  218. }
  219. const auto srcGuardians = aiNk->cc->getGuardingCreatures(source.coord);
  220. const auto srcNode = nodeStorage->getAINode(source.node);
  221. vstd::erase_if(destGuardians, [&](const CGObjectInstance * destGuard) -> bool
  222. {
  223. return vstd::contains(srcGuardians, destGuard);
  224. });
  225. const auto guardsAlreadyBypassed = destGuardians.empty() && srcGuardians.size();
  226. if(guardsAlreadyBypassed && srcNode->actor->allowBattle)
  227. {
  228. #if NK2AI_PATHFINDER_TRACE_LEVEL >= 1
  229. logAi->trace(
  230. "Bypass guard at destination while moving %s -> %s",
  231. source.coord.toString(),
  232. destination.coord.toString());
  233. #endif
  234. return true;
  235. }
  236. return bypassBattle(source, destination, pathfinderConfig, pathfinderHelper);
  237. }
  238. bool AIMovementAfterDestinationRule::bypassBattle(
  239. const PathNodeInfo & source,
  240. CDestinationNodeInfo & destination,
  241. const PathfinderConfig * pathfinderConfig,
  242. CPathfinderHelper * pathfinderHelper) const
  243. {
  244. const AIPathNode * srcNode = nodeStorage->getAINode(source.node);
  245. const AIPathNode * destNode = nodeStorage->getAINode(destination.node);
  246. const auto battleNodeOptional = nodeStorage->getOrCreateNode(
  247. destination.coord,
  248. destination.node->layer,
  249. destNode->actor->battleActor);
  250. if(!battleNodeOptional)
  251. {
  252. #if NK2AI_PATHFINDER_TRACE_LEVEL >= 2
  253. logAi->trace(
  254. "AIMovementAfterDestinationRule::bypassBattle Failed to allocate node at %s[%d]. "
  255. "Can not allocate battle node while moving %s -> %s",
  256. destination.coord.toString(),
  257. static_cast<int32_t>(destination.node->layer),
  258. source.coord.toString(),
  259. destination.coord.toString()
  260. );
  261. #endif
  262. return false;
  263. }
  264. auto * battleNode = battleNodeOptional.value();
  265. if(battleNode->locked)
  266. {
  267. #if NK2AI_PATHFINDER_TRACE_LEVEL >= 1
  268. logAi->trace(
  269. "AIMovementAfterDestinationRule::bypassBattle Block bypass guard at destination while moving %s -> %s",
  270. source.coord.toString(),
  271. destination.coord.toString());
  272. #endif
  273. return false;
  274. }
  275. auto hero = nodeStorage->getHero(source.node);
  276. uint64_t danger = aiNk->dangerEvaluator->evaluateDanger(destination.coord, hero, true);
  277. uint64_t actualArmyValue = srcNode->actor->armyValue - srcNode->armyLoss;
  278. uint64_t loss = nodeStorage->evaluateArmyLoss(hero, actualArmyValue, danger);
  279. if(loss < actualArmyValue)
  280. {
  281. if(destNode->specialAction)
  282. {
  283. battleNode->specialAction = destNode->specialAction;
  284. }
  285. destination.node = battleNode;
  286. nodeStorage->commit(destination, source);
  287. battleNode->armyLoss += loss;
  288. vstd::amax(battleNode->danger, danger);
  289. AIPreviousNodeRule(nodeStorage).process(source, destination, pathfinderConfig, pathfinderHelper);
  290. battleNode->addSpecialAction(std::make_shared<BattleAction>(destination.coord));
  291. #if NK2AI_PATHFINDER_TRACE_LEVEL >= 1
  292. logAi->trace(
  293. "AIMovementAfterDestinationRule::bypassBattle Begin bypass guard at destination with danger %s while moving %s -> %s",
  294. std::to_string(danger),
  295. source.coord.toString(),
  296. destination.coord.toString());
  297. #endif
  298. return true;
  299. }
  300. return false;
  301. }
  302. }
  303. }