AIMovementAfterDestinationRule.cpp 8.7 KB

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