CGPathNode.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. }
  50. CPathsInfo::~CPathsInfo() = default;
  51. const CGPathNode * CPathsInfo::getPathInfo(const int3 & tile) const
  52. {
  53. assert(vstd::iswithin(tile.x, 0, sizes.x));
  54. assert(vstd::iswithin(tile.y, 0, sizes.y));
  55. assert(vstd::iswithin(tile.z, 0, sizes.z));
  56. return getNode(tile);
  57. }
  58. bool CPathsInfo::getPath(CGPath & out, const int3 & dst) const
  59. {
  60. out.nodes.clear();
  61. const CGPathNode * curnode = getNode(dst);
  62. if(!curnode->theNodeBefore)
  63. return false;
  64. while(curnode)
  65. {
  66. const CGPathNode cpn = * curnode;
  67. curnode = curnode->theNodeBefore;
  68. out.nodes.push_back(cpn);
  69. }
  70. return true;
  71. }
  72. const CGPathNode * CPathsInfo::getNode(const int3 & coord) const
  73. {
  74. const auto * landNode = &nodes[ELayer::LAND][coord.z][coord.x][coord.y];
  75. if(landNode->reachable())
  76. return landNode;
  77. else
  78. return &nodes[ELayer::SAIL][coord.z][coord.x][coord.y];
  79. }
  80. PathNodeInfo::PathNodeInfo()
  81. : node(nullptr), nodeObject(nullptr), tile(nullptr), coord(-1, -1, -1), guarded(false), isInitialPosition(false)
  82. {
  83. }
  84. void PathNodeInfo::setNode(CGameState * gs, CGPathNode * n)
  85. {
  86. node = n;
  87. if(coord != node->coord)
  88. {
  89. assert(node->coord.valid());
  90. coord = node->coord;
  91. tile = gs->getTile(coord);
  92. nodeObject = tile->topVisitableObj();
  93. if(nodeObject && nodeObject->ID == Obj::HERO)
  94. {
  95. nodeHero = dynamic_cast<const CGHeroInstance *>(nodeObject);
  96. nodeObject = tile->topVisitableObj(true);
  97. if(!nodeObject)
  98. nodeObject = nodeHero;
  99. }
  100. else
  101. {
  102. nodeHero = nullptr;
  103. }
  104. }
  105. guarded = false;
  106. }
  107. void PathNodeInfo::updateInfo(CPathfinderHelper * hlp, CGameState * gs)
  108. {
  109. if(gs->guardingCreaturePosition(node->coord).valid() && !isInitialPosition)
  110. {
  111. guarded = true;
  112. }
  113. if(nodeObject)
  114. {
  115. objectRelations = gs->getPlayerRelations(hlp->owner, nodeObject->tempOwner);
  116. }
  117. if(nodeHero)
  118. {
  119. heroRelations = gs->getPlayerRelations(hlp->owner, nodeHero->tempOwner);
  120. }
  121. }
  122. bool PathNodeInfo::isNodeObjectVisitable() const
  123. {
  124. /// Hero can't visit objects while walking on water or flying
  125. return (node->layer == EPathfindingLayer::LAND || node->layer == EPathfindingLayer::SAIL)
  126. && (canSeeObj(nodeObject) || canSeeObj(nodeHero));
  127. }
  128. CDestinationNodeInfo::CDestinationNodeInfo():
  129. blocked(false),
  130. action(EPathNodeAction::UNKNOWN)
  131. {
  132. }
  133. void CDestinationNodeInfo::setNode(CGameState * gs, CGPathNode * n)
  134. {
  135. PathNodeInfo::setNode(gs, n);
  136. blocked = false;
  137. action = EPathNodeAction::UNKNOWN;
  138. }
  139. bool CDestinationNodeInfo::isBetterWay() const
  140. {
  141. if(node->turns == 0xff) //we haven't been here before
  142. return true;
  143. else
  144. return cost < node->getCost(); //this route is faster
  145. }
  146. VCMI_LIB_NAMESPACE_END