2
0

AILayerTransitionRule.cpp 6.9 KB

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