AIMovementAfterDestinationRule.cpp 8.3 KB

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