AILayerTransitionRule.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. namespace NKAI
  14. {
  15. namespace AIPathfinding
  16. {
  17. AILayerTransitionRule::AILayerTransitionRule(CPlayerSpecificInfoCallback * cb, Nullkiller * ai, std::shared_ptr<AINodeStorage> nodeStorage)
  18. :cb(cb), ai(ai), nodeStorage(nodeStorage)
  19. {
  20. setup();
  21. }
  22. void AILayerTransitionRule::process(
  23. const PathNodeInfo & source,
  24. CDestinationNodeInfo & destination,
  25. const PathfinderConfig * pathfinderConfig,
  26. CPathfinderHelper * pathfinderHelper) const
  27. {
  28. LayerTransitionRule::process(source, destination, pathfinderConfig, pathfinderHelper);
  29. if(!destination.blocked)
  30. {
  31. return;
  32. }
  33. if(source.node->layer == EPathfindingLayer::LAND && destination.node->layer == EPathfindingLayer::SAIL)
  34. {
  35. std::shared_ptr<const VirtualBoatAction> virtualBoat = findVirtualBoat(destination, source);
  36. if(virtualBoat && tryEmbarkVirtualBoat(destination, source, virtualBoat))
  37. {
  38. #if NKAI_PATHFINDER_TRACE_LEVEL >= 1
  39. logAi->trace("Embarking to virtual boat while moving %s -> %s!", source.coord.toString(), destination.coord.toString());
  40. #endif
  41. }
  42. }
  43. }
  44. void AILayerTransitionRule::setup()
  45. {
  46. std::vector<const IShipyard *> shipyards;
  47. for(const CGTownInstance * t : cb->getTownsInfo())
  48. {
  49. if(t->hasBuilt(BuildingID::SHIPYARD))
  50. shipyards.push_back(t);
  51. }
  52. for(const CGObjectInstance * obj : ai->memory->visitableObjs)
  53. {
  54. if(obj->ID != Obj::TOWN) //towns were handled in the previous loop
  55. {
  56. if(const IShipyard * shipyard = IShipyard::castFrom(obj))
  57. shipyards.push_back(shipyard);
  58. }
  59. }
  60. for(const IShipyard * shipyard : shipyards)
  61. {
  62. if(shipyard->shipyardStatus() == IShipyard::GOOD)
  63. {
  64. int3 boatLocation = shipyard->bestLocation();
  65. virtualBoats[boatLocation] = std::make_shared<BuildBoatAction>(cb, shipyard);
  66. logAi->debug("Virtual boat added at %s", boatLocation.toString());
  67. }
  68. }
  69. for(const CGHeroInstance * hero : nodeStorage->getAllHeroes())
  70. {
  71. auto summonBoatSpell = SpellID(SpellID::SUMMON_BOAT).toSpell();
  72. if(hero->canCastThisSpell(summonBoatSpell)
  73. && hero->getSpellSchoolLevel(summonBoatSpell) >= SecSkillLevel::ADVANCED)
  74. {
  75. // TODO: For lower school level we might need to check the existance of some boat
  76. summonableVirtualBoats[hero] = std::make_shared<SummonBoatAction>();
  77. }
  78. }
  79. }
  80. std::shared_ptr<const VirtualBoatAction> AILayerTransitionRule::findVirtualBoat(
  81. CDestinationNodeInfo & destination,
  82. const PathNodeInfo & source) const
  83. {
  84. std::shared_ptr<const VirtualBoatAction> virtualBoat;
  85. if(vstd::contains(virtualBoats, destination.coord))
  86. {
  87. virtualBoat = virtualBoats.at(destination.coord);
  88. }
  89. else
  90. {
  91. const CGHeroInstance * hero = nodeStorage->getHero(source.node);
  92. if(vstd::contains(summonableVirtualBoats, hero)
  93. && summonableVirtualBoats.at(hero)->canAct(nodeStorage->getAINode(source.node)))
  94. {
  95. virtualBoat = summonableVirtualBoats.at(hero);
  96. }
  97. }
  98. return virtualBoat;
  99. }
  100. bool AILayerTransitionRule::tryEmbarkVirtualBoat(
  101. CDestinationNodeInfo & destination,
  102. const PathNodeInfo & source,
  103. std::shared_ptr<const VirtualBoatAction> virtualBoat) const
  104. {
  105. bool result = false;
  106. nodeStorage->updateAINode(destination.node, [&](AIPathNode * node)
  107. {
  108. auto boatNodeOptional = nodeStorage->getOrCreateNode(
  109. node->coord,
  110. node->layer,
  111. virtualBoat->getActor(node->actor));
  112. if(boatNodeOptional)
  113. {
  114. AIPathNode * boatNode = boatNodeOptional.get();
  115. if(boatNode->action == CGPathNode::UNKNOWN)
  116. {
  117. boatNode->specialAction = virtualBoat;
  118. destination.blocked = false;
  119. destination.action = CGPathNode::ENodeAction::EMBARK;
  120. destination.node = boatNode;
  121. result = true;
  122. }
  123. else
  124. {
  125. #if NKAI_PATHFINDER_TRACE_LEVEL >= 1
  126. logAi->trace(
  127. "Special transition node already allocated. Blocked moving %s -> %s",
  128. source.coord.toString(),
  129. destination.coord.toString());
  130. #endif
  131. }
  132. }
  133. else
  134. {
  135. logAi->debug(
  136. "Can not allocate special transition node while moving %s -> %s",
  137. source.coord.toString(),
  138. destination.coord.toString());
  139. }
  140. });
  141. return result;
  142. }
  143. }
  144. }