AIMovementAfterDestinationRule.cpp 6.8 KB

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