NodeStorage.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.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.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. void NodeStorage::calculateNeighbours(
  56. std::vector<CGPathNode *> & result,
  57. const PathNodeInfo & source,
  58. EPathfindingLayer layer,
  59. const PathfinderConfig * pathfinderConfig,
  60. const CPathfinderHelper * pathfinderHelper)
  61. {
  62. NeighbourTilesVector accessibleNeighbourTiles;
  63. result.clear();
  64. pathfinderHelper->calculateNeighbourTiles(accessibleNeighbourTiles, source);
  65. for(auto & neighbour : accessibleNeighbourTiles)
  66. {
  67. auto * node = getNode(neighbour, layer);
  68. if(node->accessible == EPathAccessibility::NOT_SET)
  69. continue;
  70. result.push_back(node);
  71. }
  72. }
  73. std::vector<CGPathNode *> NodeStorage::calculateTeleportations(
  74. const PathNodeInfo & source,
  75. const PathfinderConfig * pathfinderConfig,
  76. const CPathfinderHelper * pathfinderHelper)
  77. {
  78. std::vector<CGPathNode *> neighbours;
  79. if(!source.isNodeObjectVisitable())
  80. return neighbours;
  81. auto accessibleExits = pathfinderHelper->getTeleportExits(source);
  82. for(auto & neighbour : accessibleExits)
  83. {
  84. auto * node = getNode(neighbour, source.node->layer);
  85. if(!node->coord.isValid())
  86. {
  87. logAi->debug("Teleportation exit is blocked " + neighbour.toString());
  88. continue;
  89. }
  90. neighbours.push_back(node);
  91. }
  92. return neighbours;
  93. }
  94. NodeStorage::NodeStorage(CPathsInfo & pathsInfo, const CGHeroInstance * hero)
  95. :out(pathsInfo)
  96. {
  97. out.hero = hero;
  98. out.hpos = hero->visitablePos();
  99. }
  100. void NodeStorage::resetTile(const int3 & tile, const EPathfindingLayer & layer, EPathAccessibility accessibility)
  101. {
  102. getNode(tile, layer)->update(tile, layer, accessibility);
  103. }
  104. std::vector<CGPathNode *> NodeStorage::getInitialNodes()
  105. {
  106. auto * initialNode = getNode(out.hpos, out.hero->boat ? out.hero->boat->layer : EPathfindingLayer::LAND);
  107. initialNode->turns = 0;
  108. initialNode->moveRemains = out.hero->movementPointsRemaining();
  109. initialNode->setCost(0.0);
  110. if(!initialNode->coord.isValid())
  111. {
  112. initialNode->coord = out.hpos;
  113. }
  114. return std::vector<CGPathNode *> { initialNode };
  115. }
  116. void NodeStorage::commit(CDestinationNodeInfo & destination, const PathNodeInfo & source)
  117. {
  118. assert(destination.node != source.node->theNodeBefore); //two tiles can't point to each other
  119. destination.node->setCost(destination.cost);
  120. destination.node->moveRemains = destination.movementLeft;
  121. destination.node->turns = destination.turn;
  122. destination.node->theNodeBefore = source.node;
  123. destination.node->action = destination.action;
  124. }
  125. VCMI_LIB_NAMESPACE_END