AIMovementAfterDestinationRule.cpp 8.6 KB

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