AIMovementAfterDestinationRule.cpp 8.6 KB

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