NodeStorage.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * NodeStorage.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 "NodeStorage.h"
  12. #include "CPathfinder.h"
  13. #include "PathfinderUtil.h"
  14. #include "PathfinderOptions.h"
  15. #include "../CPlayerState.h"
  16. #include "../mapObjects/CGHeroInstance.h"
  17. #include "../mapObjects/MiscObjects.h"
  18. #include "../mapping/CMap.h"
  19. VCMI_LIB_NAMESPACE_BEGIN
  20. void NodeStorage::initialize(const PathfinderOptions & options, const CGameState * gs)
  21. {
  22. //TODO: fix this code duplication with AINodeStorage::initialize, problem is to keep `resetTile` inline
  23. int3 pos;
  24. const PlayerColor player = out.hero->tempOwner;
  25. const int3 sizes = gs->getMapSize();
  26. const auto & fow = static_cast<const CGameInfoCallback *>(gs)->getPlayerTeam(player)->fogOfWarMap;
  27. //make 200% sure that these are loop invariants (also a bit shorter code), let compiler do the rest(loop unswitching)
  28. const bool useFlying = options.useFlying;
  29. const bool useWaterWalking = options.useWaterWalking;
  30. for(pos.z=0; pos.z < sizes.z; ++pos.z)
  31. {
  32. for(pos.x=0; pos.x < sizes.x; ++pos.x)
  33. {
  34. for(pos.y=0; pos.y < sizes.y; ++pos.y)
  35. {
  36. const TerrainTile tile = gs->map->getTile(pos);
  37. if(tile.terType->isWater())
  38. {
  39. resetTile(pos, ELayer::SAIL, PathfinderUtil::evaluateAccessibility<ELayer::SAIL>(pos, tile, fow, player, gs));
  40. if(useFlying)
  41. resetTile(pos, ELayer::AIR, PathfinderUtil::evaluateAccessibility<ELayer::AIR>(pos, tile, fow, player, gs));
  42. if(useWaterWalking)
  43. resetTile(pos, ELayer::WATER, PathfinderUtil::evaluateAccessibility<ELayer::WATER>(pos, tile, fow, player, gs));
  44. }
  45. if(tile.terType->isLand())
  46. {
  47. resetTile(pos, ELayer::LAND, PathfinderUtil::evaluateAccessibility<ELayer::LAND>(pos, tile, fow, player, gs));
  48. if(useFlying)
  49. resetTile(pos, ELayer::AIR, PathfinderUtil::evaluateAccessibility<ELayer::AIR>(pos, tile, fow, player, gs));
  50. }
  51. }
  52. }
  53. }
  54. }
  55. std::vector<CGPathNode *> NodeStorage::calculateNeighbours(
  56. const PathNodeInfo & source,
  57. const PathfinderConfig * pathfinderConfig,
  58. const CPathfinderHelper * pathfinderHelper)
  59. {
  60. std::vector<CGPathNode *> neighbours;
  61. neighbours.reserve(16);
  62. auto accessibleNeighbourTiles = pathfinderHelper->getNeighbourTiles(source);
  63. for(auto & neighbour : accessibleNeighbourTiles)
  64. {
  65. for(EPathfindingLayer i = EPathfindingLayer::LAND; i < EPathfindingLayer::NUM_LAYERS; i.advance(1))
  66. {
  67. auto * node = getNode(neighbour, i);
  68. if(node->accessible == EPathAccessibility::NOT_SET)
  69. continue;
  70. neighbours.push_back(node);
  71. }
  72. }
  73. return neighbours;
  74. }
  75. std::vector<CGPathNode *> NodeStorage::calculateTeleportations(
  76. const PathNodeInfo & source,
  77. const PathfinderConfig * pathfinderConfig,
  78. const CPathfinderHelper * pathfinderHelper)
  79. {
  80. std::vector<CGPathNode *> neighbours;
  81. if(!source.isNodeObjectVisitable())
  82. return neighbours;
  83. auto accessibleExits = pathfinderHelper->getTeleportExits(source);
  84. for(auto & neighbour : accessibleExits)
  85. {
  86. auto * node = getNode(neighbour, source.node->layer);
  87. if(!node->coord.valid())
  88. {
  89. logAi->debug("Teleportation exit is blocked " + neighbour.toString());
  90. continue;
  91. }
  92. neighbours.push_back(node);
  93. }
  94. return neighbours;
  95. }
  96. NodeStorage::NodeStorage(CPathsInfo & pathsInfo, const CGHeroInstance * hero)
  97. :out(pathsInfo)
  98. {
  99. out.hero = hero;
  100. out.hpos = hero->visitablePos();
  101. }
  102. void NodeStorage::resetTile(const int3 & tile, const EPathfindingLayer & layer, EPathAccessibility accessibility)
  103. {
  104. getNode(tile, layer)->update(tile, layer, accessibility);
  105. }
  106. std::vector<CGPathNode *> NodeStorage::getInitialNodes()
  107. {
  108. auto * initialNode = getNode(out.hpos, out.hero->boat ? out.hero->boat->layer : EPathfindingLayer::LAND);
  109. initialNode->turns = 0;
  110. initialNode->moveRemains = out.hero->movementPointsRemaining();
  111. initialNode->setCost(0.0);
  112. if(!initialNode->coord.valid())
  113. {
  114. initialNode->coord = out.hpos;
  115. }
  116. return std::vector<CGPathNode *> { initialNode };
  117. }
  118. void NodeStorage::commit(CDestinationNodeInfo & destination, const PathNodeInfo & source)
  119. {
  120. assert(destination.node != source.node->theNodeBefore); //two tiles can't point to each other
  121. destination.node->setCost(destination.cost);
  122. destination.node->moveRemains = destination.movementLeft;
  123. destination.node->turns = destination.turn;
  124. destination.node->theNodeBefore = source.node;
  125. destination.node->action = destination.action;
  126. }
  127. VCMI_LIB_NAMESPACE_END