CGPathNode.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 "../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. int3 CGPath::startPos() const
  23. {
  24. return nodes[nodes.size()-1].coord;
  25. }
  26. int3 CGPath::endPos() const
  27. {
  28. return nodes[0].coord;
  29. }
  30. CPathsInfo::CPathsInfo(const int3 & Sizes, const CGHeroInstance * hero_)
  31. : sizes(Sizes), hero(hero_)
  32. {
  33. nodes.resize(boost::extents[ELayer::NUM_LAYERS][sizes.z][sizes.x][sizes.y]);
  34. }
  35. CPathsInfo::~CPathsInfo() = default;
  36. const CGPathNode * CPathsInfo::getPathInfo(const int3 & tile) const
  37. {
  38. assert(vstd::iswithin(tile.x, 0, sizes.x));
  39. assert(vstd::iswithin(tile.y, 0, sizes.y));
  40. assert(vstd::iswithin(tile.z, 0, sizes.z));
  41. return getNode(tile);
  42. }
  43. bool CPathsInfo::getPath(CGPath & out, const int3 & dst) const
  44. {
  45. out.nodes.clear();
  46. const CGPathNode * curnode = getNode(dst);
  47. if(!curnode->theNodeBefore)
  48. return false;
  49. while(curnode)
  50. {
  51. const CGPathNode cpn = * curnode;
  52. curnode = curnode->theNodeBefore;
  53. out.nodes.push_back(cpn);
  54. }
  55. return true;
  56. }
  57. const CGPathNode * CPathsInfo::getNode(const int3 & coord) const
  58. {
  59. const auto * landNode = &nodes[ELayer::LAND][coord.z][coord.x][coord.y];
  60. if(landNode->reachable())
  61. return landNode;
  62. else
  63. return &nodes[ELayer::SAIL][coord.z][coord.x][coord.y];
  64. }
  65. PathNodeInfo::PathNodeInfo()
  66. : node(nullptr), nodeObject(nullptr), tile(nullptr), coord(-1, -1, -1), guarded(false), isInitialPosition(false)
  67. {
  68. }
  69. void PathNodeInfo::setNode(CGameState * gs, CGPathNode * n)
  70. {
  71. node = n;
  72. if(coord != node->coord)
  73. {
  74. assert(node->coord.valid());
  75. coord = node->coord;
  76. tile = gs->getTile(coord);
  77. nodeObject = tile->topVisitableObj();
  78. if(nodeObject && nodeObject->ID == Obj::HERO)
  79. {
  80. nodeHero = dynamic_cast<const CGHeroInstance *>(nodeObject);
  81. nodeObject = tile->topVisitableObj(true);
  82. if(!nodeObject)
  83. nodeObject = nodeHero;
  84. }
  85. else
  86. {
  87. nodeHero = nullptr;
  88. }
  89. }
  90. guarded = false;
  91. }
  92. void PathNodeInfo::updateInfo(CPathfinderHelper * hlp, CGameState * gs)
  93. {
  94. if(gs->guardingCreaturePosition(node->coord).valid() && !isInitialPosition)
  95. {
  96. guarded = true;
  97. }
  98. if(nodeObject)
  99. {
  100. objectRelations = gs->getPlayerRelations(hlp->owner, nodeObject->tempOwner);
  101. }
  102. if(nodeHero)
  103. {
  104. heroRelations = gs->getPlayerRelations(hlp->owner, nodeHero->tempOwner);
  105. }
  106. }
  107. bool PathNodeInfo::isNodeObjectVisitable() const
  108. {
  109. /// Hero can't visit objects while walking on water or flying
  110. return (node->layer == EPathfindingLayer::LAND || node->layer == EPathfindingLayer::SAIL)
  111. && (canSeeObj(nodeObject) || canSeeObj(nodeHero));
  112. }
  113. CDestinationNodeInfo::CDestinationNodeInfo():
  114. blocked(false),
  115. action(CGPathNode::ENodeAction::UNKNOWN)
  116. {
  117. }
  118. void CDestinationNodeInfo::setNode(CGameState * gs, CGPathNode * n)
  119. {
  120. PathNodeInfo::setNode(gs, n);
  121. blocked = false;
  122. action = CGPathNode::ENodeAction::UNKNOWN;
  123. }
  124. bool CDestinationNodeInfo::isBetterWay() const
  125. {
  126. if(node->turns == 0xff) //we haven't been here before
  127. return true;
  128. else
  129. return cost < node->getCost(); //this route is faster
  130. }
  131. VCMI_LIB_NAMESPACE_END