AILayerTransitionRule.cpp 7.7 KB

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