AILayerTransitionRule.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. // do not allow ally shipyards because of bug
  50. if(t->hasBuilt(BuildingID::SHIPYARD) && t->getOwner() == ai->playerID)
  51. shipyards.push_back(t);
  52. }
  53. for(const CGObjectInstance * obj : ai->memory->visitableObjs)
  54. {
  55. // do not allow ally shipyards because of bug
  56. if(obj->ID != Obj::TOWN && obj->getOwner() == ai->playerID) //towns were handled in the previous loop
  57. {
  58. if(const IShipyard * shipyard = IShipyard::castFrom(obj))
  59. shipyards.push_back(shipyard);
  60. }
  61. }
  62. for(const IShipyard * shipyard : shipyards)
  63. {
  64. if(shipyard->shipyardStatus() == IShipyard::GOOD)
  65. {
  66. int3 boatLocation = shipyard->bestLocation();
  67. virtualBoats[boatLocation] = std::make_shared<BuildBoatAction>(cb, shipyard);
  68. logAi->debug("Virtual boat added at %s", boatLocation.toString());
  69. }
  70. }
  71. for(const CGHeroInstance * hero : nodeStorage->getAllHeroes())
  72. {
  73. auto summonBoatSpell = SpellID(SpellID::SUMMON_BOAT).toSpell();
  74. if(hero->canCastThisSpell(summonBoatSpell)
  75. && hero->getSpellSchoolLevel(summonBoatSpell) >= SecSkillLevel::ADVANCED)
  76. {
  77. // TODO: For lower school level we might need to check the existance of some boat
  78. summonableVirtualBoats[hero] = std::make_shared<SummonBoatAction>();
  79. }
  80. }
  81. }
  82. std::shared_ptr<const VirtualBoatAction> AILayerTransitionRule::findVirtualBoat(
  83. CDestinationNodeInfo & destination,
  84. const PathNodeInfo & source) const
  85. {
  86. std::shared_ptr<const VirtualBoatAction> virtualBoat;
  87. if(vstd::contains(virtualBoats, destination.coord))
  88. {
  89. virtualBoat = virtualBoats.at(destination.coord);
  90. }
  91. else
  92. {
  93. const CGHeroInstance * hero = nodeStorage->getHero(source.node);
  94. if(vstd::contains(summonableVirtualBoats, hero)
  95. && summonableVirtualBoats.at(hero)->canAct(nodeStorage->getAINode(source.node)))
  96. {
  97. virtualBoat = summonableVirtualBoats.at(hero);
  98. }
  99. }
  100. return virtualBoat;
  101. }
  102. bool AILayerTransitionRule::tryEmbarkVirtualBoat(
  103. CDestinationNodeInfo & destination,
  104. const PathNodeInfo & source,
  105. std::shared_ptr<const VirtualBoatAction> virtualBoat) const
  106. {
  107. bool result = false;
  108. nodeStorage->updateAINode(destination.node, [&](AIPathNode * node)
  109. {
  110. auto boatNodeOptional = nodeStorage->getOrCreateNode(
  111. node->coord,
  112. node->layer,
  113. virtualBoat->getActor(node->actor));
  114. if(boatNodeOptional)
  115. {
  116. AIPathNode * boatNode = boatNodeOptional.get();
  117. if(boatNode->action == CGPathNode::UNKNOWN)
  118. {
  119. boatNode->specialAction = virtualBoat;
  120. destination.blocked = false;
  121. destination.action = CGPathNode::ENodeAction::EMBARK;
  122. destination.node = boatNode;
  123. result = true;
  124. }
  125. else
  126. {
  127. #if NKAI_PATHFINDER_TRACE_LEVEL >= 1
  128. logAi->trace(
  129. "Special transition node already allocated. Blocked moving %s -> %s",
  130. source.coord.toString(),
  131. destination.coord.toString());
  132. #endif
  133. }
  134. }
  135. else
  136. {
  137. logAi->debug(
  138. "Can not allocate special transition node while moving %s -> %s",
  139. source.coord.toString(),
  140. destination.coord.toString());
  141. }
  142. });
  143. return result;
  144. }
  145. }
  146. }