AILayerTransitionRule.cpp 6.7 KB

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