CGPathNode.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * CGPathNode.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 "CGPathNode.h"
  12. #include "CPathfinder.h"
  13. #include "../gameState/CGameState.h"
  14. #include "../mapObjects/CGHeroInstance.h"
  15. #include "../mapping/CMapDefines.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. static bool canSeeObj(const CGObjectInstance * obj)
  18. {
  19. /// Pathfinder should ignore placed events
  20. return obj != nullptr && obj->ID != Obj::EVENT;
  21. }
  22. const CGPathNode & CGPath::currNode() const
  23. {
  24. assert(nodes.size() > 1);
  25. return nodes[nodes.size()-1];
  26. }
  27. const CGPathNode & CGPath::nextNode() const
  28. {
  29. assert(nodes.size() > 1);
  30. return nodes[nodes.size()-2];
  31. }
  32. const CGPathNode & CGPath::lastNode() const
  33. {
  34. assert(nodes.size() > 1);
  35. return nodes[0];
  36. }
  37. int3 CGPath::startPos() const
  38. {
  39. return nodes[nodes.size()-1].coord;
  40. }
  41. int3 CGPath::endPos() const
  42. {
  43. return nodes[0].coord;
  44. }
  45. CPathsInfo::CPathsInfo(const int3 & Sizes, const CGHeroInstance * hero_)
  46. : sizes(Sizes), hero(hero_)
  47. {
  48. nodes.resize(boost::extents[ELayer::NUM_LAYERS][sizes.z][sizes.x][sizes.y]);
  49. heroBonusTreeVersion = hero->getTreeVersion();
  50. }
  51. CPathsInfo::~CPathsInfo() = default;
  52. const CGPathNode * CPathsInfo::getPathInfo(const int3 & tile) const
  53. {
  54. assert(vstd::iswithin(tile.x, 0, sizes.x));
  55. assert(vstd::iswithin(tile.y, 0, sizes.y));
  56. assert(vstd::iswithin(tile.z, 0, sizes.z));
  57. return getNode(tile);
  58. }
  59. bool CPathsInfo::getPath(CGPath & out, const int3 & dst) const
  60. {
  61. out.nodes.clear();
  62. const CGPathNode * curnode = getNode(dst);
  63. if(!curnode->theNodeBefore)
  64. return false;
  65. while(curnode)
  66. {
  67. const CGPathNode cpn = * curnode;
  68. curnode = curnode->theNodeBefore;
  69. out.nodes.push_back(cpn);
  70. }
  71. return true;
  72. }
  73. const CGPathNode * CPathsInfo::getNode(const int3 & coord) const
  74. {
  75. const auto * landNode = &nodes[ELayer::LAND][coord.z][coord.x][coord.y];
  76. if(landNode->reachable())
  77. return landNode;
  78. else
  79. return &nodes[ELayer::SAIL][coord.z][coord.x][coord.y];
  80. }
  81. PathNodeInfo::PathNodeInfo()
  82. : node(nullptr), nodeObject(nullptr), tile(nullptr), coord(-1, -1, -1), guarded(false), isInitialPosition(false)
  83. {
  84. }
  85. void PathNodeInfo::setNode(CGameState & gs, CGPathNode * n)
  86. {
  87. node = n;
  88. guarded = false;
  89. if(coord != node->coord)
  90. {
  91. assert(node->coord.isValid());
  92. coord = node->coord;
  93. tile = gs.getTile(coord);
  94. nodeObject = nullptr;
  95. nodeHero = nullptr;
  96. ObjectInstanceID topObjectID = tile->topVisitableObj();
  97. if (topObjectID.hasValue())
  98. {
  99. nodeObject = gs.getObjInstance(topObjectID);
  100. if (nodeObject->ID == Obj::HERO)
  101. {
  102. nodeHero = dynamic_cast<const CGHeroInstance *>(nodeObject);
  103. ObjectInstanceID bottomObjectID = tile->topVisitableObj(true);
  104. if (bottomObjectID.hasValue())
  105. nodeObject = gs.getObjInstance(bottomObjectID);
  106. }
  107. }
  108. }
  109. }
  110. void PathNodeInfo::updateInfo(CPathfinderHelper * hlp, CGameState & gs)
  111. {
  112. if(gs.guardingCreaturePosition(node->coord).isValid() && !isInitialPosition)
  113. {
  114. guarded = true;
  115. }
  116. if(nodeObject)
  117. {
  118. objectRelations = gs.getPlayerRelations(hlp->owner, nodeObject->tempOwner);
  119. }
  120. if(nodeHero)
  121. {
  122. heroRelations = gs.getPlayerRelations(hlp->owner, nodeHero->tempOwner);
  123. }
  124. }
  125. bool PathNodeInfo::isNodeObjectVisitable() const
  126. {
  127. /// Hero can't visit objects while walking on water or flying
  128. return (node->layer == EPathfindingLayer::LAND || node->layer == EPathfindingLayer::SAIL)
  129. && (canSeeObj(nodeObject) || canSeeObj(nodeHero));
  130. }
  131. CDestinationNodeInfo::CDestinationNodeInfo():
  132. blocked(false),
  133. action(EPathNodeAction::UNKNOWN)
  134. {
  135. }
  136. void CDestinationNodeInfo::setNode(CGameState & gs, CGPathNode * n)
  137. {
  138. PathNodeInfo::setNode(gs, n);
  139. blocked = false;
  140. action = EPathNodeAction::UNKNOWN;
  141. }
  142. bool CDestinationNodeInfo::isBetterWay() const
  143. {
  144. if(node->turns == 0xff) //we haven't been here before
  145. return true;
  146. else
  147. return cost < node->getCost(); //this route is faster
  148. }
  149. VCMI_LIB_NAMESPACE_END