CGPathNode.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. if(coord != node->coord)
  89. {
  90. assert(node->coord.valid());
  91. coord = node->coord;
  92. tile = gs->getTile(coord);
  93. nodeObject = tile->topVisitableObj();
  94. if(nodeObject && nodeObject->ID == Obj::HERO)
  95. {
  96. nodeHero = dynamic_cast<const CGHeroInstance *>(nodeObject);
  97. nodeObject = tile->topVisitableObj(true);
  98. if(!nodeObject)
  99. nodeObject = nodeHero;
  100. }
  101. else
  102. {
  103. nodeHero = nullptr;
  104. }
  105. }
  106. guarded = false;
  107. }
  108. void PathNodeInfo::updateInfo(CPathfinderHelper * hlp, CGameState * gs)
  109. {
  110. if(gs->guardingCreaturePosition(node->coord).valid() && !isInitialPosition)
  111. {
  112. guarded = true;
  113. }
  114. if(nodeObject)
  115. {
  116. objectRelations = gs->getPlayerRelations(hlp->owner, nodeObject->tempOwner);
  117. }
  118. if(nodeHero)
  119. {
  120. heroRelations = gs->getPlayerRelations(hlp->owner, nodeHero->tempOwner);
  121. }
  122. }
  123. bool PathNodeInfo::isNodeObjectVisitable() const
  124. {
  125. /// Hero can't visit objects while walking on water or flying
  126. return (node->layer == EPathfindingLayer::LAND || node->layer == EPathfindingLayer::SAIL)
  127. && (canSeeObj(nodeObject) || canSeeObj(nodeHero));
  128. }
  129. CDestinationNodeInfo::CDestinationNodeInfo():
  130. blocked(false),
  131. action(EPathNodeAction::UNKNOWN)
  132. {
  133. }
  134. void CDestinationNodeInfo::setNode(CGameState * gs, CGPathNode * n)
  135. {
  136. PathNodeInfo::setNode(gs, n);
  137. blocked = false;
  138. action = EPathNodeAction::UNKNOWN;
  139. }
  140. bool CDestinationNodeInfo::isBetterWay() const
  141. {
  142. if(node->turns == 0xff) //we haven't been here before
  143. return true;
  144. else
  145. return cost < node->getCost(); //this route is faster
  146. }
  147. VCMI_LIB_NAMESPACE_END