AIMovementAfterDestinationRule.cpp 9.0 KB

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