CCNavMeshUtils.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /****************************************************************************
  2. Copyright (c) 2015-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __CCNAV_MESH_TOOL_H__
  22. #define __CCNAV_MESH_TOOL_H__
  23. #include "base/ccConfig.h"
  24. #if CC_USE_NAVMESH
  25. #include "platform/CCPlatformMacros.h"
  26. #include "math/CCMath.h"
  27. #include "recast/Detour/DetourCommon.h"
  28. #include "recast/Detour/DetourNavMesh.h"
  29. #include "recast/Detour/DetourNavMeshQuery.h"
  30. #include "recast/DetourTileCache/DetourTileCache.h"
  31. #include "recast/DetourTileCache/DetourTileCacheBuilder.h"
  32. NS_CC_BEGIN
  33. /**
  34. * @addtogroup 3d
  35. * @{
  36. */
  37. struct LinearAllocator : public dtTileCacheAlloc
  38. {
  39. unsigned char* buffer;
  40. int capacity;
  41. int top;
  42. int high;
  43. LinearAllocator(const int cap);
  44. ~LinearAllocator();
  45. void resize(const int cap);
  46. virtual void reset();
  47. virtual void* alloc(const int size);
  48. virtual void free(void* /*ptr*/);
  49. };
  50. struct FastLZCompressor : public dtTileCacheCompressor
  51. {
  52. virtual int maxCompressedSize(const int bufferSize);
  53. virtual dtStatus compress(const unsigned char* buffer, const int bufferSize,
  54. unsigned char* compressed, const int /*maxCompressedSize*/, int* compressedSize);
  55. virtual dtStatus decompress(const unsigned char* compressed, const int compressedSize,
  56. unsigned char* buffer, const int maxBufferSize, int* bufferSize);
  57. };
  58. struct GeomData
  59. {
  60. static const int MAX_OFFMESH_CONNECTIONS = 256;
  61. float offMeshConVerts[MAX_OFFMESH_CONNECTIONS * 3 * 2];
  62. float offMeshConRads[MAX_OFFMESH_CONNECTIONS];
  63. unsigned char offMeshConDirs[MAX_OFFMESH_CONNECTIONS];
  64. unsigned char offMeshConAreas[MAX_OFFMESH_CONNECTIONS];
  65. unsigned short offMeshConFlags[MAX_OFFMESH_CONNECTIONS];
  66. unsigned int offMeshConId[MAX_OFFMESH_CONNECTIONS];
  67. int offMeshConCount;
  68. };
  69. struct MeshProcess : public dtTileCacheMeshProcess
  70. {
  71. const GeomData *data;
  72. MeshProcess(const GeomData *geom);
  73. virtual ~MeshProcess();
  74. //void init(InputGeom* geom)
  75. //{
  76. // m_geom = geom;
  77. //}
  78. virtual void process(struct dtNavMeshCreateParams* params,
  79. unsigned char* polyAreas, unsigned short* polyFlags) override;
  80. };
  81. bool inRange(const float* v1, const float* v2, const float r, const float h);
  82. int fixupCorridor(dtPolyRef* path, const int npath, const int maxPath,
  83. const dtPolyRef* visited, const int nvisited);
  84. // This function checks if the path has a small U-turn, that is,
  85. // a polygon further in the path is adjacent to the first polygon
  86. // in the path. If that happens, a shortcut is taken.
  87. // This can happen if the target (T) location is at tile boundary,
  88. // and we're (S) approaching it parallel to the tile edge.
  89. // The choice at the vertex can be arbitrary,
  90. // +---+---+
  91. // |:::|:::|
  92. // +-S-+-T-+
  93. // |:::| | <-- the step can end up in here, resulting U-turn path.
  94. // +---+---+
  95. int fixupShortcuts(dtPolyRef* path, int npath, dtNavMeshQuery* navQuery);
  96. bool getSteerTarget(dtNavMeshQuery* navQuery, const float* startPos, const float* endPos,
  97. const float minTargetDist,
  98. const dtPolyRef* path, const int pathSize,
  99. float* steerPos, unsigned char& steerPosFlag, dtPolyRef& steerPosRef,
  100. float* outPoints = nullptr, int* outPointCount = nullptr);
  101. /** @} */
  102. NS_CC_END
  103. #endif //CC_USE_NAVMESH
  104. #endif // __CCNAV_MESH_H__