DetourPathCorridor.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #ifndef DETOUTPATHCORRIDOR_H
  19. #define DETOUTPATHCORRIDOR_H
  20. #include "DetourNavMeshQuery.h"
  21. /// Represents a dynamic polygon corridor used to plan agent movement.
  22. /// @ingroup crowd, detour
  23. class dtPathCorridor
  24. {
  25. float m_pos[3];
  26. float m_target[3];
  27. dtPolyRef* m_path;
  28. int m_npath;
  29. int m_maxPath;
  30. public:
  31. dtPathCorridor();
  32. ~dtPathCorridor();
  33. /// Allocates the corridor's path buffer.
  34. /// @param[in] maxPath The maximum path size the corridor can handle.
  35. /// @return True if the initialization succeeded.
  36. bool init(const int maxPath);
  37. /// Resets the path corridor to the specified position.
  38. /// @param[in] ref The polygon reference containing the position.
  39. /// @param[in] pos The new position in the corridor. [(x, y, z)]
  40. void reset(dtPolyRef ref, const float* pos);
  41. /// Finds the corners in the corridor from the position toward the target. (The straightened path.)
  42. /// @param[out] cornerVerts The corner vertices. [(x, y, z) * cornerCount] [Size: <= maxCorners]
  43. /// @param[out] cornerFlags The flag for each corner. [(flag) * cornerCount] [Size: <= maxCorners]
  44. /// @param[out] cornerPolys The polygon reference for each corner. [(polyRef) * cornerCount]
  45. /// [Size: <= @p maxCorners]
  46. /// @param[in] maxCorners The maximum number of corners the buffers can hold.
  47. /// @param[in] navquery The query object used to build the corridor.
  48. /// @param[in] filter The filter to apply to the operation.
  49. /// @return The number of corners returned in the corner buffers. [0 <= value <= @p maxCorners]
  50. int findCorners(float* cornerVerts, unsigned char* cornerFlags,
  51. dtPolyRef* cornerPolys, const int maxCorners,
  52. dtNavMeshQuery* navquery, const dtQueryFilter* filter);
  53. /// Attempts to optimize the path if the specified point is visible from the current position.
  54. /// @param[in] next The point to search toward. [(x, y, z])
  55. /// @param[in] pathOptimizationRange The maximum range to search. [Limit: > 0]
  56. /// @param[in] navquery The query object used to build the corridor.
  57. /// @param[in] filter The filter to apply to the operation.
  58. void optimizePathVisibility(const float* next, const float pathOptimizationRange,
  59. dtNavMeshQuery* navquery, const dtQueryFilter* filter);
  60. /// Attempts to optimize the path using a local area search. (Partial replanning.)
  61. /// @param[in] navquery The query object used to build the corridor.
  62. /// @param[in] filter The filter to apply to the operation.
  63. bool optimizePathTopology(dtNavMeshQuery* navquery, const dtQueryFilter* filter);
  64. bool moveOverOffmeshConnection(dtPolyRef offMeshConRef, dtPolyRef* refs,
  65. float* startPos, float* endPos,
  66. dtNavMeshQuery* navquery);
  67. bool fixPathStart(dtPolyRef safeRef, const float* safePos);
  68. bool trimInvalidPath(dtPolyRef safeRef, const float* safePos,
  69. dtNavMeshQuery* navquery, const dtQueryFilter* filter);
  70. /// Checks the current corridor path to see if its polygon references remain valid.
  71. /// @param[in] maxLookAhead The number of polygons from the beginning of the corridor to search.
  72. /// @param[in] navquery The query object used to build the corridor.
  73. /// @param[in] filter The filter to apply to the operation.
  74. bool isValid(const int maxLookAhead, dtNavMeshQuery* navquery, const dtQueryFilter* filter);
  75. /// Moves the position from the current location to the desired location, adjusting the corridor
  76. /// as needed to reflect the change.
  77. /// @param[in] npos The desired new position. [(x, y, z)]
  78. /// @param[in] navquery The query object used to build the corridor.
  79. /// @param[in] filter The filter to apply to the operation.
  80. /// @return Returns true if move succeeded.
  81. bool movePosition(const float* npos, dtNavMeshQuery* navquery, const dtQueryFilter* filter);
  82. /// Moves the target from the curent location to the desired location, adjusting the corridor
  83. /// as needed to reflect the change.
  84. /// @param[in] npos The desired new target position. [(x, y, z)]
  85. /// @param[in] navquery The query object used to build the corridor.
  86. /// @param[in] filter The filter to apply to the operation.
  87. /// @return Returns true if move succeeded.
  88. bool moveTargetPosition(const float* npos, dtNavMeshQuery* navquery, const dtQueryFilter* filter);
  89. /// Loads a new path and target into the corridor.
  90. /// @param[in] target The target location within the last polygon of the path. [(x, y, z)]
  91. /// @param[in] path The path corridor. [(polyRef) * @p npolys]
  92. /// @param[in] npath The number of polygons in the path.
  93. void setCorridor(const float* target, const dtPolyRef* polys, const int npath);
  94. /// Gets the current position within the corridor. (In the first polygon.)
  95. /// @return The current position within the corridor.
  96. inline const float* getPos() const { return m_pos; }
  97. /// Gets the current target within the corridor. (In the last polygon.)
  98. /// @return The current target within the corridor.
  99. inline const float* getTarget() const { return m_target; }
  100. /// The polygon reference id of the first polygon in the corridor, the polygon containing the position.
  101. /// @return The polygon reference id of the first polygon in the corridor. (Or zero if there is no path.)
  102. inline dtPolyRef getFirstPoly() const { return m_npath ? m_path[0] : 0; }
  103. /// The polygon reference id of the last polygon in the corridor, the polygon containing the target.
  104. /// @return The polygon reference id of the last polygon in the corridor. (Or zero if there is no path.)
  105. inline dtPolyRef getLastPoly() const { return m_npath ? m_path[m_npath-1] : 0; }
  106. /// The corridor's path.
  107. /// @return The corridor's path. [(polyRef) * #getPathCount()]
  108. inline const dtPolyRef* getPath() const { return m_path; }
  109. /// The number of polygons in the current corridor path.
  110. /// @return The number of polygons in the current corridor path.
  111. inline int getPathCount() const { return m_npath; }
  112. private:
  113. // Explicitly disabled copy constructor and copy assignment operator.
  114. dtPathCorridor(const dtPathCorridor&);
  115. dtPathCorridor& operator=(const dtPathCorridor&);
  116. };
  117. int dtMergeCorridorStartMoved(dtPolyRef* path, const int npath, const int maxPath,
  118. const dtPolyRef* visited, const int nvisited);
  119. int dtMergeCorridorEndMoved(dtPolyRef* path, const int npath, const int maxPath,
  120. const dtPolyRef* visited, const int nvisited);
  121. int dtMergeCorridorStartShortcut(dtPolyRef* path, const int npath, const int maxPath,
  122. const dtPolyRef* visited, const int nvisited);
  123. #endif // DETOUTPATHCORRIDOR_H