AIMovementAfterDestinationRule.cpp 9.3 KB

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