AILayerTransitionRule.cpp 4.3 KB

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