AILayerTransitionRule.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * AILayerTransitionRule.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 "AILayerTransitionRule.h"
  12. #include "../../Engine/Nullkiller.h"
  13. #include "../../../../lib/pathfinder/CPathfinder.h"
  14. #include "../../../../lib/pathfinder/TurnInfo.h"
  15. #include "../../../../lib/spells/ISpellMechanics.h"
  16. #include "../../../../lib/spells/adventure/SummonBoatEffect.h"
  17. namespace NKAI
  18. {
  19. namespace AIPathfinding
  20. {
  21. AILayerTransitionRule::AILayerTransitionRule(
  22. CPlayerSpecificInfoCallback * cb,
  23. Nullkiller * ai,
  24. std::shared_ptr<AINodeStorage> nodeStorage)
  25. :cb(cb), ai(ai), nodeStorage(nodeStorage)
  26. {
  27. setup();
  28. }
  29. void AILayerTransitionRule::process(
  30. const PathNodeInfo & source,
  31. CDestinationNodeInfo & destination,
  32. const PathfinderConfig * pathfinderConfig,
  33. CPathfinderHelper * pathfinderHelper) const
  34. {
  35. LayerTransitionRule::process(source, destination, pathfinderConfig, pathfinderHelper);
  36. #if NKAI_PATHFINDER_TRACE_LEVEL >= 2
  37. logAi->trace("Layer transitioning %s -> %s, action: %d, blocked: %s",
  38. source.coord.toString(),
  39. destination.coord.toString(),
  40. static_cast<int32_t>(destination.action),
  41. destination.blocked ? "true" : "false");
  42. #endif
  43. if(!destination.blocked)
  44. {
  45. if(source.node->layer == EPathfindingLayer::LAND
  46. && (destination.node->layer == EPathfindingLayer::AIR || destination.node->layer == EPathfindingLayer::WATER))
  47. {
  48. if(pathfinderHelper->getTurnInfo()->isLayerAvailable(destination.node->layer))
  49. return;
  50. else
  51. destination.blocked = true;
  52. }
  53. else
  54. {
  55. return;
  56. }
  57. }
  58. if(source.node->layer == EPathfindingLayer::LAND && destination.node->layer == EPathfindingLayer::SAIL)
  59. {
  60. std::shared_ptr<const VirtualBoatAction> virtualBoat = findVirtualBoat(destination, source);
  61. if(virtualBoat && tryUseSpecialAction(destination, source, virtualBoat, EPathNodeAction::EMBARK))
  62. {
  63. #if NKAI_PATHFINDER_TRACE_LEVEL >= 1
  64. logAi->trace("Embarking to virtual boat while moving %s -> %s!", source.coord.toString(), destination.coord.toString());
  65. #endif
  66. }
  67. }
  68. if(source.node->layer == EPathfindingLayer::LAND && destination.node->layer == EPathfindingLayer::WATER)
  69. {
  70. if(nodeStorage->getAINode(source.node)->dayFlags & DayFlags::WATER_WALK_CAST)
  71. {
  72. destination.blocked = false;
  73. return;
  74. }
  75. auto action = waterWalkingActions.find(nodeStorage->getHero(source.node));
  76. if(action != waterWalkingActions.end() && tryUseSpecialAction(destination, source, action->second, EPathNodeAction::NORMAL))
  77. {
  78. #if NKAI_PATHFINDER_TRACE_LEVEL >= 2
  79. logAi->trace("Casting water walk while moving %s -> %s!", source.coord.toString(), destination.coord.toString());
  80. #endif
  81. }
  82. }
  83. if(source.node->layer == EPathfindingLayer::LAND && destination.node->layer == EPathfindingLayer::AIR)
  84. {
  85. if(nodeStorage->getAINode(source.node)->dayFlags & DayFlags::FLY_CAST)
  86. {
  87. destination.blocked = false;
  88. return;
  89. }
  90. auto action = airWalkingActions.find(nodeStorage->getHero(source.node));
  91. if(action != airWalkingActions.end() && tryUseSpecialAction(destination, source, action->second, EPathNodeAction::NORMAL))
  92. {
  93. #if NKAI_PATHFINDER_TRACE_LEVEL >= 2
  94. logAi->trace("Casting fly while moving %s -> %s!", source.coord.toString(), destination.coord.toString());
  95. #endif
  96. }
  97. }
  98. }
  99. void AILayerTransitionRule::setup()
  100. {
  101. SpellID waterWalk = SpellID::WATER_WALK;
  102. SpellID airWalk = SpellID::FLY;
  103. for(const CGHeroInstance * hero : nodeStorage->getAllHeroes())
  104. {
  105. if(hero->canCastThisSpell(waterWalk.toSpell()) && hero->mana >= hero->getSpellCost(waterWalk.toSpell()))
  106. {
  107. waterWalkingActions[hero] = std::make_shared<WaterWalkingAction>(hero);
  108. }
  109. if(hero->canCastThisSpell(airWalk.toSpell()) && hero->mana >= hero->getSpellCost(airWalk.toSpell()))
  110. {
  111. airWalkingActions[hero] = std::make_shared<AirWalkingAction>(hero);
  112. }
  113. }
  114. collectVirtualBoats();
  115. }
  116. void AILayerTransitionRule::collectVirtualBoats()
  117. {
  118. std::vector<const IShipyard *> shipyards;
  119. for(const CGTownInstance * t : cb->getTownsInfo())
  120. {
  121. if(t->hasBuilt(BuildingID::SHIPYARD))
  122. shipyards.push_back(t);
  123. }
  124. for(const CGObjectInstance * obj : ai->memory->visitableObjs)
  125. {
  126. if(obj->ID != Obj::TOWN) //towns were handled in the previous loop
  127. {
  128. if(const auto * shipyard = dynamic_cast<const IShipyard *>(obj))
  129. shipyards.push_back(shipyard);
  130. }
  131. }
  132. for(const IShipyard * shipyard : shipyards)
  133. {
  134. if(shipyard->shipyardStatus() == IShipyard::GOOD)
  135. {
  136. int3 boatLocation = shipyard->bestLocation();
  137. virtualBoats[boatLocation] = std::make_shared<BuildBoatAction>(cb, shipyard);
  138. logAi->debug("Virtual boat added at %s", boatLocation.toString());
  139. }
  140. }
  141. for(const CGHeroInstance * hero : nodeStorage->getAllHeroes())
  142. {
  143. for (const auto & spell : LIBRARY->spellh->objects)
  144. {
  145. if (!spell || !spell->isAdventure())
  146. continue;
  147. auto effect = spell->getAdventureMechanics().getEffectAs<SummonBoatEffect>(hero);
  148. if (!effect || !hero->canCastThisSpell(spell.get()))
  149. continue;
  150. if (effect->canCreateNewBoat() && effect->getSuccessChance(hero) == 100)
  151. {
  152. // TODO: For lower school level we might need to check the existence of some boat
  153. summonableVirtualBoats[hero] = std::make_shared<SummonBoatAction>(spell->id);
  154. }
  155. }
  156. }
  157. }
  158. std::shared_ptr<const VirtualBoatAction> AILayerTransitionRule::findVirtualBoat(
  159. CDestinationNodeInfo & destination,
  160. const PathNodeInfo & source) const
  161. {
  162. std::shared_ptr<const VirtualBoatAction> virtualBoat;
  163. if(vstd::contains(virtualBoats, destination.coord))
  164. {
  165. virtualBoat = virtualBoats.at(destination.coord);
  166. }
  167. else
  168. {
  169. const CGHeroInstance * hero = nodeStorage->getHero(source.node);
  170. if(vstd::contains(summonableVirtualBoats, hero)
  171. && summonableVirtualBoats.at(hero)->canAct(ai, nodeStorage->getAINode(source.node)))
  172. {
  173. virtualBoat = summonableVirtualBoats.at(hero);
  174. }
  175. }
  176. return virtualBoat;
  177. }
  178. bool AILayerTransitionRule::tryUseSpecialAction(
  179. CDestinationNodeInfo & destination,
  180. const PathNodeInfo & source,
  181. std::shared_ptr<const SpecialAction> specialAction,
  182. EPathNodeAction targetAction) const
  183. {
  184. bool result = false;
  185. nodeStorage->updateAINode(destination.node, [&](AIPathNode * node)
  186. {
  187. auto castNodeOptional = nodeStorage->getOrCreateNode(
  188. node->coord,
  189. node->layer,
  190. specialAction->getActor(node->actor));
  191. if(castNodeOptional)
  192. {
  193. AIPathNode * castNode = castNodeOptional.value();
  194. if(castNode->action == EPathNodeAction::UNKNOWN)
  195. {
  196. castNode->addSpecialAction(specialAction);
  197. destination.blocked = false;
  198. destination.action = targetAction;
  199. destination.node = castNode;
  200. result = true;
  201. }
  202. else
  203. {
  204. #if NKAI_PATHFINDER_TRACE_LEVEL >= 1
  205. logAi->trace(
  206. "Special transition node already allocated. Blocked moving %s -> %s",
  207. source.coord.toString(),
  208. destination.coord.toString());
  209. #endif
  210. }
  211. }
  212. else
  213. {
  214. logAi->debug(
  215. "Can not allocate special transition node while moving %s -> %s",
  216. source.coord.toString(),
  217. destination.coord.toString());
  218. }
  219. });
  220. return result;
  221. }
  222. }
  223. }