AIMovementAfterDestinationRule.cpp 9.6 KB

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