AIMovementAfterDestinationRule.cpp 9.8 KB

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