2
0

AILayerTransitionRule.cpp 6.7 KB

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