CCNavMeshUtils.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. #include "navmesh/CCNavMeshUtils.h"
  22. #if CC_USE_NAVMESH
  23. #include "recast/Detour/DetourCommon.h"
  24. #include "recast/Detour/DetourNavMeshBuilder.h"
  25. #include "recast/fastlz/fastlz.h"
  26. NS_CC_BEGIN
  27. LinearAllocator::LinearAllocator(const int cap)
  28. : buffer(nullptr)
  29. , capacity(0)
  30. , top(0)
  31. , high(0)
  32. {
  33. resize(cap);
  34. }
  35. LinearAllocator::~LinearAllocator()
  36. {
  37. dtFree(buffer);
  38. }
  39. void LinearAllocator::free(void* /*ptr*/)
  40. {
  41. }
  42. void* LinearAllocator::alloc(const int size)
  43. {
  44. if (!buffer)
  45. return nullptr;
  46. if (top + size > capacity)
  47. return nullptr;
  48. unsigned char* mem = &buffer[top];
  49. top += size;
  50. return mem;
  51. }
  52. void LinearAllocator::reset()
  53. {
  54. high = dtMax(high, top);
  55. top = 0;
  56. }
  57. void LinearAllocator::resize(const int cap)
  58. {
  59. if (buffer) dtFree(buffer);
  60. buffer = (unsigned char*)dtAlloc(cap, DT_ALLOC_PERM);
  61. capacity = cap;
  62. }
  63. int FastLZCompressor::maxCompressedSize(const int bufferSize)
  64. {
  65. return (int)(bufferSize* 1.05f);
  66. }
  67. dtStatus cocos2d::FastLZCompressor::decompress(const unsigned char* compressed, const int compressedSize
  68. , unsigned char* buffer, const int maxBufferSize, int* bufferSize)
  69. {
  70. *bufferSize = fastlz_decompress(compressed, compressedSize, buffer, maxBufferSize);
  71. return *bufferSize < 0 ? DT_FAILURE : DT_SUCCESS;
  72. }
  73. dtStatus cocos2d::FastLZCompressor::compress(const unsigned char* buffer, const int bufferSize
  74. , unsigned char* compressed, const int /*maxCompressedSize*/, int* compressedSize)
  75. {
  76. *compressedSize = fastlz_compress((const void *const)buffer, bufferSize, compressed);
  77. return DT_SUCCESS;
  78. }
  79. MeshProcess::MeshProcess(const GeomData *geom)
  80. : data(geom)
  81. {
  82. }
  83. MeshProcess::~MeshProcess()
  84. {
  85. }
  86. void MeshProcess::process(struct dtNavMeshCreateParams* params
  87. , unsigned char* polyAreas, unsigned short* polyFlags)
  88. {
  89. // Update poly flags from areas.
  90. for (int i = 0; i < params->polyCount; ++i)
  91. {
  92. if (polyAreas[i] == DT_TILECACHE_WALKABLE_AREA)
  93. polyAreas[i] = 0;
  94. if (polyAreas[i] == 0)
  95. polyFlags[i] = 1;
  96. //if (polyAreas[i] == SAMPLE_POLYAREA_GROUND ||
  97. // polyAreas[i] == SAMPLE_POLYAREA_GRASS ||
  98. // polyAreas[i] == SAMPLE_POLYAREA_ROAD)
  99. //{
  100. // polyFlags[i] = SAMPLE_POLYFLAGS_WALK;
  101. //}
  102. //else if (polyAreas[i] == SAMPLE_POLYAREA_WATER)
  103. //{
  104. // polyFlags[i] = SAMPLE_POLYFLAGS_SWIM;
  105. //}
  106. //else if (polyAreas[i] == SAMPLE_POLYAREA_DOOR)
  107. //{
  108. // polyFlags[i] = SAMPLE_POLYFLAGS_WALK | SAMPLE_POLYFLAGS_DOOR;
  109. //}
  110. }
  111. // Pass in off-mesh connections.
  112. params->offMeshConVerts = data->offMeshConVerts;
  113. params->offMeshConRad = data->offMeshConRads;
  114. params->offMeshConDir = data->offMeshConDirs;
  115. params->offMeshConAreas = data->offMeshConAreas;
  116. params->offMeshConFlags = data->offMeshConFlags;
  117. params->offMeshConUserID = data->offMeshConId;
  118. params->offMeshConCount = data->offMeshConCount;
  119. }
  120. bool getSteerTarget(dtNavMeshQuery* navQuery, const float* startPos, const float* endPos, const float minTargetDist, const dtPolyRef* path, const int pathSize, float* steerPos, unsigned char& steerPosFlag, dtPolyRef& steerPosRef, float* outPoints /*= 0*/, int* outPointCount /*= 0*/)
  121. {
  122. // Find steer target.
  123. static const int MAX_STEER_POINTS = 3;
  124. float steerPath[MAX_STEER_POINTS * 3];
  125. unsigned char steerPathFlags[MAX_STEER_POINTS];
  126. dtPolyRef steerPathPolys[MAX_STEER_POINTS];
  127. int nsteerPath = 0;
  128. navQuery->findStraightPath(startPos, endPos, path, pathSize,
  129. steerPath, steerPathFlags, steerPathPolys, &nsteerPath, MAX_STEER_POINTS);
  130. if (!nsteerPath)
  131. return false;
  132. if (outPoints && outPointCount)
  133. {
  134. *outPointCount = nsteerPath;
  135. for (int i = 0; i < nsteerPath; ++i)
  136. dtVcopy(&outPoints[i * 3], &steerPath[i * 3]);
  137. }
  138. // Find vertex far enough to steer to.
  139. int ns = 0;
  140. while (ns < nsteerPath)
  141. {
  142. // Stop at Off-Mesh link or when point is further than slop away.
  143. if ((steerPathFlags[ns] & DT_STRAIGHTPATH_OFFMESH_CONNECTION) ||
  144. !inRange(&steerPath[ns * 3], startPos, minTargetDist, 1000.0f))
  145. break;
  146. ns++;
  147. }
  148. // Failed to find good point to steer to.
  149. if (ns >= nsteerPath)
  150. return false;
  151. dtVcopy(steerPos, &steerPath[ns * 3]);
  152. steerPos[1] = startPos[1];
  153. steerPosFlag = steerPathFlags[ns];
  154. steerPosRef = steerPathPolys[ns];
  155. return true;
  156. }
  157. int fixupShortcuts(dtPolyRef* path, int npath, dtNavMeshQuery* navQuery)
  158. {
  159. if (npath < 3)
  160. return npath;
  161. // Get connected polygons
  162. static const int maxNeis = 16;
  163. dtPolyRef neis[maxNeis];
  164. int nneis = 0;
  165. const dtMeshTile* tile = nullptr;
  166. const dtPoly* poly = nullptr;
  167. if (dtStatusFailed(navQuery->getAttachedNavMesh()->getTileAndPolyByRef(path[0], &tile, &poly)))
  168. return npath;
  169. for (unsigned int k = poly->firstLink; k != DT_NULL_LINK; k = tile->links[k].next)
  170. {
  171. const dtLink* link = &tile->links[k];
  172. if (link->ref != 0)
  173. {
  174. if (nneis < maxNeis)
  175. neis[nneis++] = link->ref;
  176. }
  177. }
  178. // If any of the neighbour polygons is within the next few polygons
  179. // in the path, short cut to that polygon directly.
  180. static const int maxLookAhead = 6;
  181. int cut = 0;
  182. for (int i = dtMin(maxLookAhead, npath) - 1; i > 1 && cut == 0; i--) {
  183. for (int j = 0; j < nneis; j++)
  184. {
  185. if (path[i] == neis[j]) {
  186. cut = i;
  187. break;
  188. }
  189. }
  190. }
  191. if (cut > 1)
  192. {
  193. int offset = cut - 1;
  194. npath -= offset;
  195. for (int i = 1; i < npath; i++)
  196. path[i] = path[i + offset];
  197. }
  198. return npath;
  199. }
  200. int fixupCorridor(dtPolyRef* path, const int npath, const int maxPath, const dtPolyRef* visited, const int nvisited)
  201. {
  202. int furthestPath = -1;
  203. int furthestVisited = -1;
  204. // Find furthest common polygon.
  205. for (int i = npath - 1; i >= 0; --i)
  206. {
  207. bool found = false;
  208. for (int j = nvisited - 1; j >= 0; --j)
  209. {
  210. if (path[i] == visited[j])
  211. {
  212. furthestPath = i;
  213. furthestVisited = j;
  214. found = true;
  215. }
  216. }
  217. if (found)
  218. break;
  219. }
  220. // If no intersection found just return current path.
  221. if (furthestPath == -1 || furthestVisited == -1)
  222. return npath;
  223. // Concatenate paths.
  224. // Adjust beginning of the buffer to include the visited.
  225. const int req = nvisited - furthestVisited;
  226. const int orig = dtMin(furthestPath + 1, npath);
  227. int size = dtMax(0, npath - orig);
  228. if (req + size > maxPath)
  229. size = maxPath - req;
  230. if (size)
  231. memmove(path + req, path + orig, size*sizeof(dtPolyRef));
  232. // Store visited
  233. for (int i = 0; i < req; ++i)
  234. path[i] = visited[(nvisited - 1) - i];
  235. return req + size;
  236. }
  237. bool inRange(const float* v1, const float* v2, const float r, const float h)
  238. {
  239. const float dx = v2[0] - v1[0];
  240. const float dy = v2[1] - v1[1];
  241. const float dz = v2[2] - v1[2];
  242. return (dx*dx + dz*dz) < r*r && fabsf(dy) < h;
  243. }
  244. NS_CC_END
  245. #endif //CC_USE_NAVMESH