AIMovementAfterDestinationRule.cpp 5.8 KB

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