AIMovementAfterDestinationRule.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 enemyHero = destination.nodeHero && destination.heroRelations == PlayerRelations::ENEMIES;
  38. if(!enemyHero && !isObjectRemovable(destination.nodeObject))
  39. {
  40. destination.blocked = true;
  41. }
  42. return;
  43. }
  44. if(blocker == BlockingReason::DESTINATION_VISIT)
  45. {
  46. return;
  47. }
  48. if(blocker == BlockingReason::DESTINATION_GUARDED)
  49. {
  50. auto srcGuardians = cb->getGuardingCreatures(source.coord);
  51. auto destGuardians = cb->getGuardingCreatures(destination.coord);
  52. if(destGuardians.empty())
  53. {
  54. destination.blocked = true;
  55. return;
  56. }
  57. vstd::erase_if(destGuardians, [&](const CGObjectInstance * destGuard) -> bool
  58. {
  59. return vstd::contains(srcGuardians, destGuard);
  60. });
  61. auto guardsAlreadyBypassed = destGuardians.empty() && srcGuardians.size();
  62. auto srcNode = nodeStorage->getAINode(source.node);
  63. if(guardsAlreadyBypassed && srcNode->actor->allowBattle)
  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->actor->battleActor);
  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. AIPathNode * battleNode = battleNodeOptional.get();
  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 hero = nodeStorage->getHero(source.node);
  102. auto danger = nodeStorage->evaluateDanger(destination.coord, hero);
  103. double actualArmyValue = srcNode->actor->armyValue - srcNode->armyLoss;
  104. double ratio = (double)danger / actualArmyValue;
  105. uint64_t loss = (uint64_t)(actualArmyValue * ratio * ratio * ratio);
  106. if(loss < actualArmyValue)
  107. {
  108. destination.node = battleNode;
  109. nodeStorage->commit(destination, source);
  110. battleNode->armyLoss += loss;
  111. vstd::amax(battleNode->danger, danger);
  112. battleNode->specialAction = std::make_shared<BattleAction>(destination.coord);
  113. #ifdef VCMI_TRACE_PATHFINDER
  114. logAi->trace(
  115. "Begin bypass guard at destination with danger %s while moving %s -> %s",
  116. std::to_string(danger),
  117. source.coord.toString(),
  118. destination.coord.toString());
  119. #endif
  120. return;
  121. }
  122. }
  123. destination.blocked = true;
  124. }
  125. }