NodeStorage.cpp 4.3 KB

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