AILayerTransitionRule.cpp 4.3 KB

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