AIMovementAfterDestinationRule.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. namespace AIPathfinding
  14. {
  15. AIMovementAfterDestinationRule::AIMovementAfterDestinationRule(
  16. CPlayerSpecificInfoCallback * cb,
  17. std::shared_ptr<AINodeStorage> nodeStorage)
  18. :cb(cb), nodeStorage(nodeStorage)
  19. {
  20. }
  21. void AIMovementAfterDestinationRule::process(
  22. const PathNodeInfo & source,
  23. CDestinationNodeInfo & destination,
  24. const PathfinderConfig * pathfinderConfig,
  25. CPathfinderHelper * pathfinderHelper) const
  26. {
  27. if(nodeStorage->hasBetterChain(source, destination))
  28. {
  29. destination.blocked = true;
  30. return;
  31. }
  32. auto blocker = getBlockingReason(source, destination, pathfinderConfig, pathfinderHelper);
  33. if(blocker == BlockingReason::NONE)
  34. return;
  35. if(blocker == BlockingReason::DESTINATION_BLOCKVIS && destination.nodeObject)
  36. {
  37. auto objID = destination.nodeObject->ID;
  38. auto enemyHero = objID == Obj::HERO && destination.objectRelations == PlayerRelations::ENEMIES;
  39. if(!enemyHero && !isObjectRemovable(destination.nodeObject))
  40. {
  41. destination.blocked = true;
  42. }
  43. return;
  44. }
  45. if(blocker == BlockingReason::DESTINATION_VISIT)
  46. {
  47. return;
  48. }
  49. if(blocker == BlockingReason::DESTINATION_GUARDED)
  50. {
  51. auto srcGuardians = cb->getGuardingCreatures(source.coord);
  52. auto destGuardians = cb->getGuardingCreatures(destination.coord);
  53. if(destGuardians.empty())
  54. {
  55. destination.blocked = true;
  56. return;
  57. }
  58. vstd::erase_if(destGuardians, [&](const CGObjectInstance * destGuard) -> bool
  59. {
  60. return vstd::contains(srcGuardians, destGuard);
  61. });
  62. auto guardsAlreadyBypassed = destGuardians.empty() && srcGuardians.size();
  63. if(guardsAlreadyBypassed && nodeStorage->isBattleNode(source.node))
  64. {
  65. #ifdef VCMI_TRACE_PATHFINDER
  66. logAi->trace(
  67. "Bypass guard at destination while moving %s -> %s",
  68. source.coord.toString(),
  69. destination.coord.toString());
  70. #endif
  71. return;
  72. }
  73. const AIPathNode * destNode = nodeStorage->getAINode(destination.node);
  74. auto battleNodeOptional = nodeStorage->getOrCreateNode(
  75. destination.coord,
  76. destination.node->layer,
  77. destNode->chainMask | AINodeStorage::BATTLE_CHAIN);
  78. if(!battleNodeOptional)
  79. {
  80. #ifdef VCMI_TRACE_PATHFINDER
  81. logAi->trace(
  82. "Can not allocate battle node while moving %s -> %s",
  83. source.coord.toString(),
  84. destination.coord.toString());
  85. #endif
  86. destination.blocked = true;
  87. return;
  88. }
  89. auto * battleNode = battleNodeOptional.value();
  90. if(battleNode->locked)
  91. {
  92. #ifdef VCMI_TRACE_PATHFINDER
  93. logAi->trace(
  94. "Block bypass guard at destination while moving %s -> %s",
  95. source.coord.toString(),
  96. destination.coord.toString());
  97. #endif
  98. destination.blocked = true;
  99. return;
  100. }
  101. auto danger = nodeStorage->evaluateDanger(destination.coord);
  102. destination.node = battleNode;
  103. nodeStorage->commit(destination, source);
  104. if(battleNode->danger < danger)
  105. {
  106. battleNode->danger = danger;
  107. }
  108. battleNode->specialAction = std::make_shared<BattleAction>(destination.coord);
  109. #ifdef VCMI_TRACE_PATHFINDER
  110. logAi->trace(
  111. "Begin bypass guard at destination with danger %s while moving %s -> %s",
  112. std::to_string(danger),
  113. source.coord.toString(),
  114. destination.coord.toString());
  115. #endif
  116. return;
  117. }
  118. destination.blocked = true;
  119. }
  120. }