2
0

AILayerTransitionRule.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 ObjectInstanceID objId : aiNk->memory->visitableObjs)
  129. {
  130. const CGObjectInstance * obj = aiNk->cc->getObjInstance(objId);
  131. if(obj && obj->ID != Obj::TOWN) //towns were handled in the previous loop
  132. {
  133. if(const auto * shipyard = dynamic_cast<const IShipyard *>(obj))
  134. shipyards.push_back(shipyard);
  135. }
  136. }
  137. for(const IShipyard * shipyard : shipyards)
  138. {
  139. if(shipyard->shipyardStatus() == IShipyard::GOOD)
  140. {
  141. int3 boatLocation = shipyard->bestLocation();
  142. virtualBoats[boatLocation] = std::make_shared<BuildBoatAction>(aiNk->cc.get(), shipyard);
  143. logAi->debug("Virtual boat added at %s", boatLocation.toString());
  144. }
  145. }
  146. for(const CGHeroInstance * hero : nodeStorage->getAllHeroes())
  147. {
  148. for(const auto & spell : LIBRARY->spellh->objects)
  149. {
  150. if(!spell || !spell->isAdventure())
  151. continue;
  152. auto effect = spell->getAdventureMechanics().getEffectAs<SummonBoatEffect>(hero);
  153. if(!effect || !hero->canCastThisSpell(spell.get()))
  154. continue;
  155. if(effect->canCreateNewBoat() && effect->getSuccessChance(hero) == 100)
  156. {
  157. // TODO: For lower school level we might need to check the existence of some boat
  158. summonableVirtualBoats[hero] = std::make_shared<SummonBoatAction>(spell->id);
  159. }
  160. }
  161. }
  162. }
  163. std::shared_ptr<const VirtualBoatAction> AILayerTransitionRule::findVirtualBoat(CDestinationNodeInfo & destination, const PathNodeInfo & source) const
  164. {
  165. std::shared_ptr<const VirtualBoatAction> virtualBoat;
  166. if(vstd::contains(virtualBoats, destination.coord))
  167. {
  168. virtualBoat = virtualBoats.at(destination.coord);
  169. }
  170. else
  171. {
  172. const CGHeroInstance * hero = nodeStorage->getHero(source.node);
  173. if(vstd::contains(summonableVirtualBoats, hero) && summonableVirtualBoats.at(hero)->canAct(aiNk, nodeStorage->getAINode(source.node)))
  174. {
  175. virtualBoat = summonableVirtualBoats.at(hero);
  176. }
  177. }
  178. return virtualBoat;
  179. }
  180. bool AILayerTransitionRule::tryUseSpecialAction(
  181. CDestinationNodeInfo & destination,
  182. const PathNodeInfo & source,
  183. std::shared_ptr<const SpecialAction> specialAction,
  184. EPathNodeAction targetAction
  185. ) const
  186. {
  187. bool result = false;
  188. nodeStorage->updateAINode(
  189. destination.node,
  190. [&](const AIPathNode * node)
  191. {
  192. const auto castNodeOptional = nodeStorage->getOrCreateNode(node->coord, node->layer, specialAction->getActor(node->actor));
  193. if(!castNodeOptional)
  194. {
  195. #if NK2AI_PATHFINDER_TRACE_LEVEL >= 2
  196. logAi->trace(
  197. "AILayerTransitionRule::tryUseSpecialAction Failed to allocate node at %s[%d]. "
  198. "Can not allocate special transition node while moving %s -> %s",
  199. node->coord.toString(),
  200. static_cast<int32_t>(node->layer),
  201. source.coord.toString(),
  202. destination.coord.toString()
  203. );
  204. #endif
  205. }
  206. else
  207. {
  208. AIPathNode * castNode = castNodeOptional.value();
  209. if(castNode->action == EPathNodeAction::UNKNOWN)
  210. {
  211. castNode->addSpecialAction(specialAction);
  212. destination.blocked = false;
  213. destination.action = targetAction;
  214. destination.node = castNode;
  215. result = true;
  216. }
  217. else
  218. {
  219. #if NK2AI_PATHFINDER_TRACE_LEVEL >= 2
  220. logAi->trace(
  221. "AILayerTransitionRule::tryUseSpecialAction Special transition node already allocated. Blocked moving %s -> %s",
  222. source.coord.toString(),
  223. destination.coord.toString()
  224. );
  225. #endif
  226. }
  227. }
  228. }
  229. );
  230. return result;
  231. }
  232. }
  233. }