AIMovementAfterDestinationRule.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. destinationNode = questNode.get();
  125. destination.node = questNode.get();
  126. nodeStorage->commit(destination, source);
  127. AIPreviousNodeRule(nodeStorage).process(source, destination, pathfinderConfig, pathfinderHelper);
  128. }
  129. nodeStorage->updateAINode(destination.node, [&](AIPathNode * node)
  130. {
  131. node->specialAction.reset(new QuestAction(questAction));
  132. });
  133. }
  134. return true;
  135. }
  136. bool AIMovementAfterDestinationRule::bypassRemovableObject(
  137. const PathNodeInfo & source,
  138. CDestinationNodeInfo & destination,
  139. const PathfinderConfig * pathfinderConfig,
  140. CPathfinderHelper * pathfinderHelper) const
  141. {
  142. if(destination.nodeObject->ID == Obj::QUEST_GUARD
  143. || destination.nodeObject->ID == Obj::BORDERGUARD
  144. || destination.nodeObject->ID == Obj::BORDER_GATE)
  145. {
  146. return bypassQuest(source, destination, pathfinderConfig, pathfinderHelper);
  147. }
  148. auto enemyHero = destination.nodeHero && destination.heroRelations == PlayerRelations::ENEMIES;
  149. if(!enemyHero && !isObjectRemovable(destination.nodeObject))
  150. {
  151. if(nodeStorage->getHero(destination.node) == destination.nodeHero)
  152. return true;
  153. return false;
  154. }
  155. auto danger = nodeStorage->evaluateDanger(destination.coord, nodeStorage->getHero(destination.node), true);
  156. if(danger)
  157. {
  158. return bypassBattle(source, destination, pathfinderConfig, pathfinderHelper);
  159. }
  160. return true;
  161. }
  162. bool AIMovementAfterDestinationRule::bypassDestinationGuards(
  163. std::vector<const CGObjectInstance *> destGuardians,
  164. const PathNodeInfo & source,
  165. CDestinationNodeInfo & destination,
  166. const PathfinderConfig * pathfinderConfig,
  167. CPathfinderHelper * pathfinderHelper) const
  168. {
  169. auto srcGuardians = cb->getGuardingCreatures(source.coord);
  170. if(destGuardians.empty())
  171. {
  172. return false;
  173. }
  174. auto srcNode = nodeStorage->getAINode(source.node);
  175. vstd::erase_if(destGuardians, [&](const CGObjectInstance * destGuard) -> bool
  176. {
  177. return vstd::contains(srcGuardians, destGuard);
  178. });
  179. auto guardsAlreadyBypassed = destGuardians.empty() && srcGuardians.size();
  180. if(guardsAlreadyBypassed && srcNode->actor->allowBattle)
  181. {
  182. #if PATHFINDER_TRACE_LEVEL >= 1
  183. logAi->trace(
  184. "Bypass guard at destination while moving %s -> %s",
  185. source.coord.toString(),
  186. destination.coord.toString());
  187. #endif
  188. return true;
  189. }
  190. return bypassBattle(source, destination, pathfinderConfig, pathfinderHelper);
  191. }
  192. bool AIMovementAfterDestinationRule::bypassBattle(
  193. const PathNodeInfo & source,
  194. CDestinationNodeInfo & destination,
  195. const PathfinderConfig * pathfinderConfig,
  196. CPathfinderHelper * pathfinderHelper) const
  197. {
  198. const AIPathNode * srcNode = nodeStorage->getAINode(source.node);
  199. const AIPathNode * destNode = nodeStorage->getAINode(destination.node);
  200. auto battleNodeOptional = nodeStorage->getOrCreateNode(
  201. destination.coord,
  202. destination.node->layer,
  203. destNode->actor->battleActor);
  204. if(!battleNodeOptional)
  205. {
  206. #if PATHFINDER_TRACE_LEVEL >= 1
  207. logAi->trace(
  208. "Can not allocate battle node while moving %s -> %s",
  209. source.coord.toString(),
  210. destination.coord.toString());
  211. #endif
  212. return false;
  213. }
  214. AIPathNode * battleNode = battleNodeOptional.get();
  215. if(battleNode->locked)
  216. {
  217. #if PATHFINDER_TRACE_LEVEL >= 1
  218. logAi->trace(
  219. "Block bypass guard at destination while moving %s -> %s",
  220. source.coord.toString(),
  221. destination.coord.toString());
  222. #endif
  223. return false;
  224. }
  225. auto hero = nodeStorage->getHero(source.node);
  226. uint64_t danger = nodeStorage->evaluateDanger(destination.coord, hero, true);
  227. uint64_t actualArmyValue = srcNode->actor->armyValue - srcNode->armyLoss;
  228. uint64_t loss = nodeStorage->evaluateArmyLoss(hero, actualArmyValue, danger);
  229. if(loss < actualArmyValue)
  230. {
  231. destination.node = battleNode;
  232. nodeStorage->commit(destination, source);
  233. battleNode->armyLoss += loss;
  234. vstd::amax(battleNode->danger, danger);
  235. AIPreviousNodeRule(nodeStorage).process(source, destination, pathfinderConfig, pathfinderHelper);
  236. battleNode->specialAction = std::make_shared<BattleAction>(destination.coord);
  237. #if PATHFINDER_TRACE_LEVEL >= 1
  238. logAi->trace(
  239. "Begin bypass guard at destination with danger %s while moving %s -> %s",
  240. std::to_string(danger),
  241. source.coord.toString(),
  242. destination.coord.toString());
  243. #endif
  244. return true;
  245. }
  246. return false;
  247. }
  248. }