AIMovementAfterDestinationRule.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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",
  76. destination.coord.toString(),
  77. (int)destination.action,
  78. (int)blocker);
  79. #endif
  80. auto destGuardians = cb->getGuardingCreatures(destination.coord);
  81. bool allowBypass = false;
  82. switch(blocker)
  83. {
  84. case BlockingReason::DESTINATION_GUARDED:
  85. allowBypass = bypassDestinationGuards(destGuardians, source, destination, pathfinderConfig, pathfinderHelper);
  86. break;
  87. case BlockingReason::DESTINATION_BLOCKVIS:
  88. if(destination.nodeHero && destination.heroRelations != PlayerRelations::ENEMIES)
  89. {
  90. allowBypass = destination.heroRelations == PlayerRelations::SAME_PLAYER
  91. && destination.nodeHero == nodeStorage->getHero(destination.node);
  92. }
  93. else
  94. {
  95. allowBypass = destination.nodeObject && bypassRemovableObject(source, destination, pathfinderConfig, pathfinderHelper);
  96. }
  97. if(allowBypass && destGuardians.size())
  98. allowBypass = bypassDestinationGuards(destGuardians, source, destination, pathfinderConfig, pathfinderHelper);
  99. break;
  100. case BlockingReason::DESTINATION_VISIT:
  101. allowBypass = true;
  102. break;
  103. case BlockingReason::DESTINATION_BLOCKED:
  104. allowBypass = bypassBlocker(source, destination, pathfinderConfig, pathfinderHelper);
  105. break;
  106. }
  107. destination.blocked = !allowBypass || nodeStorage->isDistanceLimitReached(source, destination);
  108. destination.node->locked = !allowBypass;
  109. }
  110. bool AIMovementAfterDestinationRule::bypassBlocker(
  111. const PathNodeInfo & source,
  112. CDestinationNodeInfo & destination,
  113. const PathfinderConfig * pathfinderConfig,
  114. CPathfinderHelper * pathfinderHelper) const
  115. {
  116. auto enemyHero = destination.nodeHero && destination.heroRelations == PlayerRelations::ENEMIES;
  117. if(enemyHero)
  118. {
  119. return bypassBattle(source, destination, pathfinderConfig, pathfinderHelper);
  120. }
  121. if(destination.nodeObject
  122. && (destination.nodeObject->ID == Obj::GARRISON || destination.nodeObject->ID == Obj::GARRISON2)
  123. && destination.objectRelations == PlayerRelations::ENEMIES)
  124. {
  125. return bypassBattle(source, destination, pathfinderConfig, pathfinderHelper);
  126. }
  127. return false;
  128. }
  129. bool AIMovementAfterDestinationRule::bypassQuest(
  130. const PathNodeInfo & source,
  131. CDestinationNodeInfo & destination,
  132. const PathfinderConfig * pathfinderConfig,
  133. CPathfinderHelper * pathfinderHelper) const
  134. {
  135. const AIPathNode * destinationNode = nodeStorage->getAINode(destination.node);
  136. auto questObj = dynamic_cast<const IQuestObject *>(destination.nodeObject);
  137. auto questInfo = QuestInfo(questObj->quest, destination.nodeObject, destination.coord);
  138. QuestAction questAction(questInfo);
  139. if(destination.nodeObject->ID == Obj::QUEST_GUARD
  140. && questObj->quest->mission == Rewardable::Limiter{}
  141. && questObj->quest->killTarget == ObjectInstanceID::NONE)
  142. {
  143. return false;
  144. }
  145. if(!questAction.canAct(ai, destinationNode))
  146. {
  147. if(!destinationNode->actor->allowUseResources)
  148. {
  149. std::optional<AIPathNode *> questNode = nodeStorage->getOrCreateNode(
  150. destination.coord,
  151. destination.node->layer,
  152. destinationNode->actor->resourceActor);
  153. if(!questNode || questNode.value()->getCost() < destination.cost)
  154. {
  155. return false;
  156. }
  157. destination.node = questNode.value();
  158. nodeStorage->commit(destination, source);
  159. AIPreviousNodeRule(nodeStorage).process(source, destination, pathfinderConfig, pathfinderHelper);
  160. }
  161. nodeStorage->updateAINode(destination.node, [&](AIPathNode * node)
  162. {
  163. node->addSpecialAction(std::make_shared<QuestAction>(questAction));
  164. });
  165. }
  166. return true;
  167. }
  168. bool AIMovementAfterDestinationRule::bypassRemovableObject(
  169. const PathNodeInfo & source,
  170. CDestinationNodeInfo & destination,
  171. const PathfinderConfig * pathfinderConfig,
  172. CPathfinderHelper * pathfinderHelper) const
  173. {
  174. if(destination.nodeObject->ID == Obj::QUEST_GUARD
  175. || destination.nodeObject->ID == Obj::BORDERGUARD
  176. || destination.nodeObject->ID == Obj::BORDER_GATE)
  177. {
  178. return bypassQuest(source, destination, pathfinderConfig, pathfinderHelper);
  179. }
  180. auto enemyHero = destination.nodeHero && destination.heroRelations == PlayerRelations::ENEMIES;
  181. if(!enemyHero && !isObjectRemovable(destination.nodeObject))
  182. {
  183. if(nodeStorage->getHero(destination.node) == destination.nodeHero)
  184. return true;
  185. return false;
  186. }
  187. auto danger = nodeStorage->evaluateDanger(destination.coord, nodeStorage->getHero(destination.node), true);
  188. if(danger)
  189. {
  190. return bypassBattle(source, destination, pathfinderConfig, pathfinderHelper);
  191. }
  192. return true;
  193. }
  194. bool AIMovementAfterDestinationRule::bypassDestinationGuards(
  195. std::vector<const CGObjectInstance *> destGuardians,
  196. const PathNodeInfo & source,
  197. CDestinationNodeInfo & destination,
  198. const PathfinderConfig * pathfinderConfig,
  199. CPathfinderHelper * pathfinderHelper) const
  200. {
  201. auto srcGuardians = cb->getGuardingCreatures(source.coord);
  202. if(destGuardians.empty())
  203. {
  204. return false;
  205. }
  206. auto srcNode = nodeStorage->getAINode(source.node);
  207. vstd::erase_if(destGuardians, [&](const CGObjectInstance * destGuard) -> bool
  208. {
  209. return vstd::contains(srcGuardians, destGuard);
  210. });
  211. auto guardsAlreadyBypassed = destGuardians.empty() && srcGuardians.size();
  212. if(guardsAlreadyBypassed && srcNode->actor->allowBattle)
  213. {
  214. #if NKAI_PATHFINDER_TRACE_LEVEL >= 1
  215. logAi->trace(
  216. "Bypass guard at destination while moving %s -> %s",
  217. source.coord.toString(),
  218. destination.coord.toString());
  219. #endif
  220. return true;
  221. }
  222. return bypassBattle(source, destination, pathfinderConfig, pathfinderHelper);
  223. }
  224. bool AIMovementAfterDestinationRule::bypassBattle(
  225. const PathNodeInfo & source,
  226. CDestinationNodeInfo & destination,
  227. const PathfinderConfig * pathfinderConfig,
  228. CPathfinderHelper * pathfinderHelper) const
  229. {
  230. const AIPathNode * srcNode = nodeStorage->getAINode(source.node);
  231. const AIPathNode * destNode = nodeStorage->getAINode(destination.node);
  232. auto battleNodeOptional = nodeStorage->getOrCreateNode(
  233. destination.coord,
  234. destination.node->layer,
  235. destNode->actor->battleActor);
  236. if(!battleNodeOptional)
  237. {
  238. #if NKAI_PATHFINDER_TRACE_LEVEL >= 1
  239. logAi->trace(
  240. "Can not allocate battle node while moving %s -> %s",
  241. source.coord.toString(),
  242. destination.coord.toString());
  243. #endif
  244. return false;
  245. }
  246. auto * battleNode = battleNodeOptional.value();
  247. if(battleNode->locked)
  248. {
  249. #if NKAI_PATHFINDER_TRACE_LEVEL >= 1
  250. logAi->trace(
  251. "Block bypass guard at destination while moving %s -> %s",
  252. source.coord.toString(),
  253. destination.coord.toString());
  254. #endif
  255. return false;
  256. }
  257. auto hero = nodeStorage->getHero(source.node);
  258. uint64_t danger = nodeStorage->evaluateDanger(destination.coord, hero, true);
  259. uint64_t actualArmyValue = srcNode->actor->armyValue - srcNode->armyLoss;
  260. uint64_t loss = nodeStorage->evaluateArmyLoss(hero, actualArmyValue, danger);
  261. if(loss < actualArmyValue)
  262. {
  263. if(destNode->specialAction)
  264. {
  265. battleNode->specialAction = destNode->specialAction;
  266. }
  267. destination.node = battleNode;
  268. nodeStorage->commit(destination, source);
  269. battleNode->armyLoss += loss;
  270. vstd::amax(battleNode->danger, danger);
  271. AIPreviousNodeRule(nodeStorage).process(source, destination, pathfinderConfig, pathfinderHelper);
  272. battleNode->addSpecialAction(std::make_shared<BattleAction>(destination.coord));
  273. #if NKAI_PATHFINDER_TRACE_LEVEL >= 1
  274. logAi->trace(
  275. "Begin bypass guard at destination with danger %s while moving %s -> %s",
  276. std::to_string(danger),
  277. source.coord.toString(),
  278. destination.coord.toString());
  279. #endif
  280. return true;
  281. }
  282. return false;
  283. }
  284. }
  285. }