AILayerTransitionRule.cpp 6.4 KB

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