Blame view

ios/cocos2d/cocos/navmesh/CCNavMeshUtils.cpp 8.48 KB
520389e3   xiaoyu   接入cocos源码,编译未通过,继续修改
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
  /****************************************************************************
   Copyright (c) 2015-2016 Chukong Technologies Inc.
   Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
   
   http://www.cocos2d-x.org
   
   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:
   
   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.
   
   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.
   ****************************************************************************/
  #include "navmesh/CCNavMeshUtils.h"
  #if CC_USE_NAVMESH
  
  #include "recast/Detour/DetourCommon.h"
  #include "recast/Detour/DetourNavMeshBuilder.h"
  #include "recast/fastlz/fastlz.h"
  
  NS_CC_BEGIN
  
  LinearAllocator::LinearAllocator(const int cap)
  : buffer(nullptr)
  , capacity(0)
  , top(0)
  , high(0)
  {
      resize(cap);
  }
  
  LinearAllocator::~LinearAllocator()
  {
      dtFree(buffer);
  }
  
  void LinearAllocator::free(void* /*ptr*/)
  {
  
  }
  
  void* LinearAllocator::alloc(const int size)
  {
      if (!buffer)
          return nullptr;
      if (top + size > capacity)
          return nullptr;
      unsigned char* mem = &buffer[top];
      top += size;
      return mem;
  }
  
  void LinearAllocator::reset()
  {
      high = dtMax(high, top);
      top = 0;
  }
  
  void LinearAllocator::resize(const int cap)
  {
      if (buffer) dtFree(buffer);
      buffer = (unsigned char*)dtAlloc(cap, DT_ALLOC_PERM);
      capacity = cap;
  }
  
  int FastLZCompressor::maxCompressedSize(const int bufferSize)
  {
      return (int)(bufferSize* 1.05f);
  }
  
  dtStatus cocos2d::FastLZCompressor::decompress(const unsigned char* compressed, const int compressedSize
                                               , unsigned char* buffer, const int maxBufferSize, int* bufferSize)
  {
      *bufferSize = fastlz_decompress(compressed, compressedSize, buffer, maxBufferSize);
      return *bufferSize < 0 ? DT_FAILURE : DT_SUCCESS;
  }
  
  dtStatus cocos2d::FastLZCompressor::compress(const unsigned char* buffer, const int bufferSize
      , unsigned char* compressed, const int /*maxCompressedSize*/, int* compressedSize)
  {
      *compressedSize = fastlz_compress((const void *const)buffer, bufferSize, compressed);
      return DT_SUCCESS;
  }
  
  MeshProcess::MeshProcess(const GeomData *geom)
      : data(geom)
  {
  }
  
  MeshProcess::~MeshProcess()
  {
  
  }
  
  void MeshProcess::process(struct dtNavMeshCreateParams* params
      , unsigned char* polyAreas, unsigned short* polyFlags)
  {
      // Update poly flags from areas.
      for (int i = 0; i < params->polyCount; ++i)
      {
          if (polyAreas[i] == DT_TILECACHE_WALKABLE_AREA)
              polyAreas[i] = 0;
  
          if (polyAreas[i] == 0)
              polyFlags[i] = 1;
  
          //if (polyAreas[i] == SAMPLE_POLYAREA_GROUND ||
          //	polyAreas[i] == SAMPLE_POLYAREA_GRASS ||
          //	polyAreas[i] == SAMPLE_POLYAREA_ROAD)
          //{
          //	polyFlags[i] = SAMPLE_POLYFLAGS_WALK;
          //}
          //else if (polyAreas[i] == SAMPLE_POLYAREA_WATER)
          //{
          //	polyFlags[i] = SAMPLE_POLYFLAGS_SWIM;
          //}
          //else if (polyAreas[i] == SAMPLE_POLYAREA_DOOR)
          //{
          //	polyFlags[i] = SAMPLE_POLYFLAGS_WALK | SAMPLE_POLYFLAGS_DOOR;
          //}
      }
  
      // Pass in off-mesh connections.
      params->offMeshConVerts = data->offMeshConVerts;
      params->offMeshConRad = data->offMeshConRads;
      params->offMeshConDir = data->offMeshConDirs;
      params->offMeshConAreas = data->offMeshConAreas;
      params->offMeshConFlags = data->offMeshConFlags;
      params->offMeshConUserID = data->offMeshConId;
      params->offMeshConCount = data->offMeshConCount;
  }
  
  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*/)
  {
      // Find steer target.
      static const int MAX_STEER_POINTS = 3;
      float steerPath[MAX_STEER_POINTS * 3];
      unsigned char steerPathFlags[MAX_STEER_POINTS];
      dtPolyRef steerPathPolys[MAX_STEER_POINTS];
      int nsteerPath = 0;
      navQuery->findStraightPath(startPos, endPos, path, pathSize,
          steerPath, steerPathFlags, steerPathPolys, &nsteerPath, MAX_STEER_POINTS);
      if (!nsteerPath)
          return false;
  
      if (outPoints && outPointCount)
      {
          *outPointCount = nsteerPath;
          for (int i = 0; i < nsteerPath; ++i)
              dtVcopy(&outPoints[i * 3], &steerPath[i * 3]);
      }
  
  
      // Find vertex far enough to steer to.
      int ns = 0;
      while (ns < nsteerPath)
      {
          // Stop at Off-Mesh link or when point is further than slop away.
          if ((steerPathFlags[ns] & DT_STRAIGHTPATH_OFFMESH_CONNECTION) ||
              !inRange(&steerPath[ns * 3], startPos, minTargetDist, 1000.0f))
              break;
          ns++;
      }
      // Failed to find good point to steer to.
      if (ns >= nsteerPath)
          return false;
  
      dtVcopy(steerPos, &steerPath[ns * 3]);
      steerPos[1] = startPos[1];
      steerPosFlag = steerPathFlags[ns];
      steerPosRef = steerPathPolys[ns];
  
      return true;
  }
  
  int fixupShortcuts(dtPolyRef* path, int npath, dtNavMeshQuery* navQuery)
  {
      if (npath < 3)
          return npath;
  
      // Get connected polygons
      static const int maxNeis = 16;
      dtPolyRef neis[maxNeis];
      int nneis = 0;
  
      const dtMeshTile* tile = nullptr;
      const dtPoly* poly = nullptr;
      if (dtStatusFailed(navQuery->getAttachedNavMesh()->getTileAndPolyByRef(path[0], &tile, &poly)))
          return npath;
  
      for (unsigned int k = poly->firstLink; k != DT_NULL_LINK; k = tile->links[k].next)
      {
          const dtLink* link = &tile->links[k];
          if (link->ref != 0)
          {
              if (nneis < maxNeis)
                  neis[nneis++] = link->ref;
          }
      }
  
      // If any of the neighbour polygons is within the next few polygons
      // in the path, short cut to that polygon directly.
      static const int maxLookAhead = 6;
      int cut = 0;
      for (int i = dtMin(maxLookAhead, npath) - 1; i > 1 && cut == 0; i--) {
          for (int j = 0; j < nneis; j++)
          {
              if (path[i] == neis[j]) {
                  cut = i;
                  break;
              }
          }
      }
      if (cut > 1)
      {
          int offset = cut - 1;
          npath -= offset;
          for (int i = 1; i < npath; i++)
              path[i] = path[i + offset];
      }
  
      return npath;
  }
  
  int fixupCorridor(dtPolyRef* path, const int npath, const int maxPath, const dtPolyRef* visited, const int nvisited)
  {
      int furthestPath = -1;
      int furthestVisited = -1;
  
      // Find furthest common polygon.
      for (int i = npath - 1; i >= 0; --i)
      {
          bool found = false;
          for (int j = nvisited - 1; j >= 0; --j)
          {
              if (path[i] == visited[j])
              {
                  furthestPath = i;
                  furthestVisited = j;
                  found = true;
              }
          }
          if (found)
              break;
      }
  
      // If no intersection found just return current path. 
      if (furthestPath == -1 || furthestVisited == -1)
          return npath;
  
      // Concatenate paths.	
  
      // Adjust beginning of the buffer to include the visited.
      const int req = nvisited - furthestVisited;
      const int orig = dtMin(furthestPath + 1, npath);
      int size = dtMax(0, npath - orig);
      if (req + size > maxPath)
          size = maxPath - req;
      if (size)
          memmove(path + req, path + orig, size*sizeof(dtPolyRef));
  
      // Store visited
      for (int i = 0; i < req; ++i)
          path[i] = visited[(nvisited - 1) - i];
  
      return req + size;
  }
  
  bool inRange(const float* v1, const float* v2, const float r, const float h)
  {
      const float dx = v2[0] - v1[0];
      const float dy = v2[1] - v1[1];
      const float dz = v2[2] - v1[2];
      return (dx*dx + dz*dz) < r*r && fabsf(dy) < h;
  }
  
  NS_CC_END
  
  #endif //CC_USE_NAVMESH