AIMovementAfterDestinationRule.cpp 6.7 KB

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