| 1 | // |
| 2 | // Copyright (c) 2009-2010 Mikko Mononen [email protected] |
| 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 | |
| 19 | #include <float.h> |
| 20 | #include <string.h> |
| 21 | #include <stdio.h> |
| 22 | #include "DetourNavMesh.h" |
| 23 | #include "DetourNode.h" |
| 24 | #include "DetourCommon.h" |
| 25 | #include "DetourMath.h" |
| 26 | #include "DetourAlloc.h" |
| 27 | #include "DetourAssert.h" |
| 28 | #include <new> |
| 29 | |
| 30 | |
| 31 | inline bool overlapSlabs(const float* amin, const float* amax, |
| 32 | const float* bmin, const float* bmax, |
| 33 | const float px, const float py) |
| 34 | { |
| 35 | // Check for horizontal overlap. |
| 36 | // The segment is shrunken a little so that slabs which touch |
| 37 | // at end points are not connected. |
| 38 | const float minx = dtMax(amin[0]+px,bmin[0]+px); |
| 39 | const float maxx = dtMin(amax[0]-px,bmax[0]-px); |
| 40 | if (minx > maxx) |
| 41 | return false; |
| 42 | |
| 43 | // Check vertical overlap. |
| 44 | const float ad = (amax[1]-amin[1]) / (amax[0]-amin[0]); |
| 45 | const float ak = amin[1] - ad*amin[0]; |
| 46 | const float bd = (bmax[1]-bmin[1]) / (bmax[0]-bmin[0]); |
| 47 | const float bk = bmin[1] - bd*bmin[0]; |
| 48 | const float aminy = ad*minx + ak; |
| 49 | const float amaxy = ad*maxx + ak; |
| 50 | const float bminy = bd*minx + bk; |
| 51 | const float bmaxy = bd*maxx + bk; |
| 52 | const float dmin = bminy - aminy; |
| 53 | const float dmax = bmaxy - amaxy; |
| 54 | |
| 55 | // Crossing segments always overlap. |
| 56 | if (dmin*dmax < 0) |
| 57 | return true; |
| 58 | |
| 59 | // Check for overlap at endpoints. |
| 60 | const float thr = dtSqr(py*2); |
| 61 | if (dmin*dmin <= thr || dmax*dmax <= thr) |
| 62 | return true; |
| 63 | |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | static float getSlabCoord(const float* va, const int side) |
| 68 | { |
| 69 | if (side == 0 || side == 4) |
| 70 | return va[0]; |
| 71 | else if (side == 2 || side == 6) |
| 72 | return va[2]; |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static void calcSlabEndPoints(const float* va, const float* vb, float* bmin, float* bmax, const int side) |
| 77 | { |
| 78 | if (side == 0 || side == 4) |
| 79 | { |
| 80 | if (va[2] < vb[2]) |
| 81 | { |
| 82 | bmin[0] = va[2]; |
| 83 | bmin[1] = va[1]; |
| 84 | bmax[0] = vb[2]; |
| 85 | bmax[1] = vb[1]; |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | bmin[0] = vb[2]; |
| 90 | bmin[1] = vb[1]; |
| 91 | bmax[0] = va[2]; |
| 92 | bmax[1] = va[1]; |
| 93 | } |
| 94 | } |
| 95 | else if (side == 2 || side == 6) |
| 96 | { |
| 97 | if (va[0] < vb[0]) |
| 98 | { |
| 99 | bmin[0] = va[0]; |
| 100 | bmin[1] = va[1]; |
| 101 | bmax[0] = vb[0]; |
| 102 | bmax[1] = vb[1]; |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | bmin[0] = vb[0]; |
| 107 | bmin[1] = vb[1]; |
| 108 | bmax[0] = va[0]; |
| 109 | bmax[1] = va[1]; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | inline int computeTileHash(int x, int y, const int mask) |
| 115 | { |
| 116 | const unsigned int h1 = 0x8da6b343; // Large multiplicative constants; |
| 117 | const unsigned int h2 = 0xd8163841; // here arbitrarily chosen primes |
| 118 | unsigned int n = h1 * x + h2 * y; |
| 119 | return (int)(n & mask); |
| 120 | } |
| 121 | |
| 122 | inline unsigned int allocLink(dtMeshTile* tile) |
| 123 | { |
| 124 | if (tile->linksFreeList == DT_NULL_LINK) |
| 125 | return DT_NULL_LINK; |
| 126 | unsigned int link = tile->linksFreeList; |
| 127 | tile->linksFreeList = tile->links[link].next; |
| 128 | return link; |
| 129 | } |
| 130 | |
| 131 | inline void freeLink(dtMeshTile* tile, unsigned int link) |
| 132 | { |
| 133 | tile->links[link].next = tile->linksFreeList; |
| 134 | tile->linksFreeList = link; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | dtNavMesh* dtAllocNavMesh() |
| 139 | { |
| 140 | void* mem = dtAlloc(sizeof(dtNavMesh), DT_ALLOC_PERM); |
| 141 | if (!mem) return 0; |
| 142 | return new(mem) dtNavMesh; |
| 143 | } |
| 144 | |
| 145 | /// @par |
| 146 | /// |
| 147 | /// This function will only free the memory for tiles with the #DT_TILE_FREE_DATA |
| 148 | /// flag set. |
| 149 | void dtFreeNavMesh(dtNavMesh* navmesh) |
| 150 | { |
| 151 | if (!navmesh) return; |
| 152 | navmesh->~dtNavMesh(); |
| 153 | dtFree(navmesh); |
| 154 | } |
| 155 | |
| 156 | ////////////////////////////////////////////////////////////////////////////////////////// |
| 157 | |
| 158 | /** |
| 159 | @class dtNavMesh |
| 160 | |
| 161 | The navigation mesh consists of one or more tiles defining three primary types of structural data: |
| 162 | |
| 163 | A polygon mesh which defines most of the navigation graph. (See rcPolyMesh for its structure.) |
| 164 | A detail mesh used for determining surface height on the polygon mesh. (See rcPolyMeshDetail for its structure.) |
| 165 | Off-mesh connections, which define custom point-to-point edges within the navigation graph. |
| 166 | |
| 167 | The general build process is as follows: |
| 168 | |
| 169 | -# Create rcPolyMesh and rcPolyMeshDetail data using the Recast build pipeline. |
| 170 | -# Optionally, create off-mesh connection data. |
| 171 | -# Combine the source data into a dtNavMeshCreateParams structure. |
| 172 | -# Create a tile data array using dtCreateNavMeshData(). |
| 173 | -# Allocate at dtNavMesh object and initialize it. (For single tile navigation meshes, |
| 174 | the tile data is loaded during this step.) |
| 175 | -# For multi-tile navigation meshes, load the tile data using dtNavMesh::addTile(). |
| 176 | |
| 177 | Notes: |
| 178 | |
| 179 | - This class is usually used in conjunction with the dtNavMeshQuery class for pathfinding. |
| 180 | - Technically, all navigation meshes are tiled. A 'solo' mesh is simply a navigation mesh initialized |
| 181 | to have only a single tile. |
| 182 | - This class does not implement any asynchronous methods. So the ::dtStatus result of all methods will |
| 183 | always contain either a success or failure flag. |
| 184 | |
| 185 | @see dtNavMeshQuery, dtCreateNavMeshData, dtNavMeshCreateParams, #dtAllocNavMesh, #dtFreeNavMesh |
| 186 | */ |
| 187 | |
| 188 | dtNavMesh::dtNavMesh() : |
| 189 | m_tileWidth(0), |
| 190 | m_tileHeight(0), |
| 191 | m_maxTiles(0), |
| 192 | m_tileLutSize(0), |
| 193 | m_tileLutMask(0), |
| 194 | m_posLookup(0), |
| 195 | m_nextFree(0), |
| 196 | m_tiles(0) |
| 197 | { |
| 198 | #ifndef DT_POLYREF64 |
| 199 | m_saltBits = 0; |
| 200 | m_tileBits = 0; |
| 201 | m_polyBits = 0; |
| 202 | #endif |
| 203 | memset(&m_params, 0, sizeof(dtNavMeshParams)); |
| 204 | m_orig[0] = 0; |
| 205 | m_orig[1] = 0; |
| 206 | m_orig[2] = 0; |
| 207 | } |
| 208 | |
| 209 | dtNavMesh::~dtNavMesh() |
| 210 | { |
| 211 | for (int i = 0; i < m_maxTiles; ++i) |
| 212 | { |
| 213 | if (m_tiles[i].flags & DT_TILE_FREE_DATA) |
| 214 | { |
| 215 | dtFree(m_tiles[i].data); |
| 216 | m_tiles[i].data = 0; |
| 217 | m_tiles[i].dataSize = 0; |
| 218 | } |
| 219 | } |
| 220 | dtFree(m_posLookup); |
| 221 | dtFree(m_tiles); |
| 222 | } |
| 223 | |
| 224 | dtStatus dtNavMesh::init(const dtNavMeshParams* params) |
| 225 | { |
| 226 | memcpy(&m_params, params, sizeof(dtNavMeshParams)); |
| 227 | dtVcopy(m_orig, params->orig); |
| 228 | m_tileWidth = params->tileWidth; |
| 229 | m_tileHeight = params->tileHeight; |
| 230 | |
| 231 | // Init tiles |
| 232 | m_maxTiles = params->maxTiles; |
| 233 | m_tileLutSize = dtNextPow2(params->maxTiles/4); |
| 234 | if (!m_tileLutSize) m_tileLutSize = 1; |
| 235 | m_tileLutMask = m_tileLutSize-1; |
| 236 | |
| 237 | m_tiles = (dtMeshTile*)dtAlloc(sizeof(dtMeshTile)*m_maxTiles, DT_ALLOC_PERM); |
| 238 | if (!m_tiles) |
| 239 | return DT_FAILURE | DT_OUT_OF_MEMORY; |
| 240 | m_posLookup = (dtMeshTile**)dtAlloc(sizeof(dtMeshTile*)*m_tileLutSize, DT_ALLOC_PERM); |
| 241 | if (!m_posLookup) |
| 242 | return DT_FAILURE | DT_OUT_OF_MEMORY; |
| 243 | memset(m_tiles, 0, sizeof(dtMeshTile)*m_maxTiles); |
| 244 | memset(m_posLookup, 0, sizeof(dtMeshTile*)*m_tileLutSize); |
| 245 | m_nextFree = 0; |
| 246 | for (int i = m_maxTiles-1; i >= 0; --i) |
| 247 | { |
| 248 | m_tiles[i].salt = 1; |
| 249 | m_tiles[i].next = m_nextFree; |
| 250 | m_nextFree = &m_tiles[i]; |
| 251 | } |
| 252 | |
| 253 | // Init ID generator values. |
| 254 | #ifndef DT_POLYREF64 |
| 255 | m_tileBits = dtIlog2(dtNextPow2((unsigned int)params->maxTiles)); |
| 256 | m_polyBits = dtIlog2(dtNextPow2((unsigned int)params->maxPolys)); |
| 257 | // Only allow 31 salt bits, since the salt mask is calculated using 32bit uint and it will overflow. |
| 258 | m_saltBits = dtMin((unsigned int)31, 32 - m_tileBits - m_polyBits); |
| 259 | |
| 260 | if (m_saltBits < 10) |
| 261 | return DT_FAILURE | DT_INVALID_PARAM; |
| 262 | #endif |
| 263 | |
| 264 | return DT_SUCCESS; |
| 265 | } |
| 266 | |
| 267 | dtStatus dtNavMesh::init(unsigned char* data, const int dataSize, const int flags) |
| 268 | { |
| 269 | // Make sure the data is in right format. |
| 270 | dtMeshHeader* header = (dtMeshHeader*)data; |
| 271 | if (header->magic != DT_NAVMESH_MAGIC) |
| 272 | return DT_FAILURE | DT_WRONG_MAGIC; |
| 273 | if (header->version != DT_NAVMESH_VERSION) |
| 274 | return DT_FAILURE | DT_WRONG_VERSION; |
| 275 | |
| 276 | dtNavMeshParams params; |
| 277 | dtVcopy(params.orig, header->bmin); |
| 278 | params.tileWidth = header->bmax[0] - header->bmin[0]; |
| 279 | params.tileHeight = header->bmax[2] - header->bmin[2]; |
| 280 | params.maxTiles = 1; |
| 281 | params.maxPolys = header->polyCount; |
| 282 | |
| 283 | dtStatus status = init(¶ms); |
| 284 | if (dtStatusFailed(status)) |
| 285 | return status; |
| 286 | |
| 287 | return addTile(data, dataSize, flags, 0, 0); |
| 288 | } |
| 289 | |
| 290 | /// @par |
| 291 | /// |
| 292 | /// @note The parameters are created automatically when the single tile |
| 293 | /// initialization is performed. |
| 294 | const dtNavMeshParams* dtNavMesh::getParams() const |
| 295 | { |
| 296 | return &m_params; |
| 297 | } |
| 298 | |
| 299 | ////////////////////////////////////////////////////////////////////////////////////////// |
| 300 | int dtNavMesh::findConnectingPolys(const float* va, const float* vb, |
| 301 | const dtMeshTile* tile, int side, |
| 302 | dtPolyRef* con, float* conarea, int maxcon) const |
| 303 | { |
| 304 | if (!tile) return 0; |
| 305 | |
| 306 | float amin[2], amax[2]; |
| 307 | calcSlabEndPoints(va, vb, amin, amax, side); |
| 308 | const float apos = getSlabCoord(va, side); |
| 309 | |
| 310 | // Remove links pointing to 'side' and compact the links array. |
| 311 | float bmin[2], bmax[2]; |
| 312 | unsigned short m = DT_EXT_LINK | (unsigned short)side; |
| 313 | int n = 0; |
| 314 | |
| 315 | dtPolyRef base = getPolyRefBase(tile); |
| 316 | |
| 317 | for (int i = 0; i < tile->header->polyCount; ++i) |
| 318 | { |
| 319 | dtPoly* poly = &tile->polys[i]; |
| 320 | const int nv = poly->vertCount; |
| 321 | for (int j = 0; j < nv; ++j) |
| 322 | { |
| 323 | // Skip edges which do not point to the right side. |
| 324 | if (poly->neis[j] != m) continue; |
| 325 | |
| 326 | const float* vc = &tile->verts[poly->verts[j]*3]; |
| 327 | const float* vd = &tile->verts[poly->verts[(j+1) % nv]*3]; |
| 328 | const float bpos = getSlabCoord(vc, side); |
| 329 | |
| 330 | // Segments are not close enough. |
| 331 | if (dtAbs(apos-bpos) > 0.01f) |
| 332 | continue; |
| 333 | |
| 334 | // Check if the segments touch. |
| 335 | calcSlabEndPoints(vc,vd, bmin,bmax, side); |
| 336 | |
| 337 | if (!overlapSlabs(amin,amax, bmin,bmax, 0.01f, tile->header->walkableClimb)) continue; |
| 338 | |
| 339 | // Add return value. |
| 340 | if (n < maxcon) |
| 341 | { |
| 342 | conarea[n*2+0] = dtMax(amin[0], bmin[0]); |
| 343 | conarea[n*2+1] = dtMin(amax[0], bmax[0]); |
| 344 | con[n] = base | (dtPolyRef)i; |
| 345 | n++; |
| 346 | } |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | return n; |
| 351 | } |
| 352 | |
| 353 | void dtNavMesh::unconnectLinks(dtMeshTile* tile, dtMeshTile* target) |
| 354 | { |
| 355 | if (!tile || !target) return; |
| 356 | |
| 357 | const unsigned int targetNum = decodePolyIdTile(getTileRef(target)); |
| 358 | |
| 359 | for (int i = 0; i < tile->header->polyCount; ++i) |
| 360 | { |
| 361 | dtPoly* poly = &tile->polys[i]; |
| 362 | unsigned int j = poly->firstLink; |
| 363 | unsigned int pj = DT_NULL_LINK; |
| 364 | while (j != DT_NULL_LINK) |
| 365 | { |
| 366 | if (decodePolyIdTile(tile->links[j].ref) == targetNum) |
| 367 | { |
| 368 | // Remove link. |
| 369 | unsigned int nj = tile->links[j].next; |
| 370 | if (pj == DT_NULL_LINK) |
| 371 | poly->firstLink = nj; |
| 372 | else |
| 373 | tile->links[pj].next = nj; |
| 374 | freeLink(tile, j); |
| 375 | j = nj; |
| 376 | } |
| 377 | else |
| 378 | { |
| 379 | // Advance |
| 380 | pj = j; |
| 381 | j = tile->links[j].next; |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | void dtNavMesh::connectExtLinks(dtMeshTile* tile, dtMeshTile* target, int side) |
| 388 | { |
| 389 | if (!tile) return; |
| 390 | |
| 391 | // Connect border links. |
| 392 | for (int i = 0; i < tile->header->polyCount; ++i) |
| 393 | { |
| 394 | dtPoly* poly = &tile->polys[i]; |
| 395 | |
| 396 | // Create new links. |
| 397 | // unsigned short m = DT_EXT_LINK | (unsigned short)side; |
| 398 | |
| 399 | const int nv = poly->vertCount; |
| 400 | for (int j = 0; j < nv; ++j) |
| 401 | { |
| 402 | // Skip non-portal edges. |
| 403 | if ((poly->neis[j] & DT_EXT_LINK) == 0) |
| 404 | continue; |
| 405 | |
| 406 | const int dir = (int)(poly->neis[j] & 0xff); |
| 407 | if (side != -1 && dir != side) |
| 408 | continue; |
| 409 | |
| 410 | // Create new links |
| 411 | const float* va = &tile->verts[poly->verts[j]*3]; |
| 412 | const float* vb = &tile->verts[poly->verts[(j+1) % nv]*3]; |
| 413 | dtPolyRef nei[4]; |
| 414 | float neia[4*2]; |
| 415 | int nnei = findConnectingPolys(va,vb, target, dtOppositeTile(dir), nei,neia,4); |
| 416 | for (int k = 0; k < nnei; ++k) |
| 417 | { |
| 418 | unsigned int idx = allocLink(tile); |
| 419 | if (idx != DT_NULL_LINK) |
| 420 | { |
| 421 | dtLink* link = &tile->links[idx]; |
| 422 | link->ref = nei[k]; |
| 423 | link->edge = (unsigned char)j; |
| 424 | link->side = (unsigned char)dir; |
| 425 | |
| 426 | link->next = poly->firstLink; |
| 427 | poly->firstLink = idx; |
| 428 | |
| 429 | // Compress portal limits to a byte value. |
| 430 | if (dir == 0 || dir == 4) |
| 431 | { |
| 432 | float tmin = (neia[k*2+0]-va[2]) / (vb[2]-va[2]); |
| 433 | float tmax = (neia[k*2+1]-va[2]) / (vb[2]-va[2]); |
| 434 | if (tmin > tmax) |
| 435 | dtSwap(tmin,tmax); |
| 436 | link->bmin = (unsigned char)roundf(dtClamp(tmin, 0.0f, 1.0f)*255.0f); |
| 437 | link->bmax = (unsigned char)roundf(dtClamp(tmax, 0.0f, 1.0f)*255.0f); |
| 438 | } |
| 439 | else if (dir == 2 || dir == 6) |
| 440 | { |
| 441 | float tmin = (neia[k*2+0]-va[0]) / (vb[0]-va[0]); |
| 442 | float tmax = (neia[k*2+1]-va[0]) / (vb[0]-va[0]); |
| 443 | if (tmin > tmax) |
| 444 | dtSwap(tmin,tmax); |
| 445 | link->bmin = (unsigned char)roundf(dtClamp(tmin, 0.0f, 1.0f)*255.0f); |
| 446 | link->bmax = (unsigned char)roundf(dtClamp(tmax, 0.0f, 1.0f)*255.0f); |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | void dtNavMesh::connectExtOffMeshLinks(dtMeshTile* tile, dtMeshTile* target, int side) |
| 455 | { |
| 456 | if (!tile) return; |
| 457 | |
| 458 | // Connect off-mesh links. |
| 459 | // We are interested on links which land from target tile to this tile. |
| 460 | const unsigned char oppositeSide = (side == -1) ? 0xff : (unsigned char)dtOppositeTile(side); |
| 461 | |
| 462 | for (int i = 0; i < target->header->offMeshConCount; ++i) |
| 463 | { |
| 464 | dtOffMeshConnection* targetCon = &target->offMeshCons[i]; |
| 465 | if (targetCon->side != oppositeSide) |
| 466 | continue; |
| 467 | |
| 468 | dtPoly* targetPoly = &target->polys[targetCon->poly]; |
| 469 | // Skip off-mesh connections which start location could not be connected at all. |
| 470 | if (targetPoly->firstLink == DT_NULL_LINK) |
| 471 | continue; |
| 472 | |
| 473 | const float halfExtents[3] = { targetCon->rad, target->header->walkableClimb, targetCon->rad }; |
| 474 | |
| 475 | // Find polygon to connect to. |
| 476 | const float* p = &targetCon->pos[3]; |
| 477 | float nearestPt[3]; |
| 478 | dtPolyRef ref = findNearestPolyInTile(tile, p, halfExtents, nearestPt); |
| 479 | if (!ref) |
| 480 | continue; |
| 481 | // findNearestPoly may return too optimistic results, further check to make sure. |
| 482 | if (dtSqr(nearestPt[0]-p[0])+dtSqr(nearestPt[2]-p[2]) > dtSqr(targetCon->rad)) |
| 483 | continue; |
| 484 | // Make sure the location is on current mesh. |
| 485 | float* v = &target->verts[targetPoly->verts[1]*3]; |
| 486 | dtVcopy(v, nearestPt); |
| 487 | |
| 488 | // Link off-mesh connection to target poly. |
| 489 | unsigned int idx = allocLink(target); |
| 490 | if (idx != DT_NULL_LINK) |
| 491 | { |
| 492 | dtLink* link = &target->links[idx]; |
| 493 | link->ref = ref; |
| 494 | link->edge = (unsigned char)1; |
| 495 | link->side = oppositeSide; |
| 496 | link->bmin = link->bmax = 0; |
| 497 | // Add to linked list. |
| 498 | link->next = targetPoly->firstLink; |
| 499 | targetPoly->firstLink = idx; |
| 500 | } |
| 501 | |
| 502 | // Link target poly to off-mesh connection. |
| 503 | if (targetCon->flags & DT_OFFMESH_CON_BIDIR) |
| 504 | { |
| 505 | unsigned int tidx = allocLink(tile); |
| 506 | if (tidx != DT_NULL_LINK) |
| 507 | { |
| 508 | const unsigned short landPolyIdx = (unsigned short)decodePolyIdPoly(ref); |
| 509 | dtPoly* landPoly = &tile->polys[landPolyIdx]; |
| 510 | dtLink* link = &tile->links[tidx]; |
| 511 | link->ref = getPolyRefBase(target) | (dtPolyRef)(targetCon->poly); |
| 512 | link->edge = 0xff; |
| 513 | link->side = (unsigned char)(side == -1 ? 0xff : side); |
| 514 | link->bmin = link->bmax = 0; |
| 515 | // Add to linked list. |
| 516 | link->next = landPoly->firstLink; |
| 517 | landPoly->firstLink = tidx; |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | } |
| 523 | |
| 524 | void dtNavMesh::connectIntLinks(dtMeshTile* tile) |
| 525 | { |
| 526 | if (!tile) return; |
| 527 | |
| 528 | dtPolyRef base = getPolyRefBase(tile); |
| 529 | |
| 530 | for (int i = 0; i < tile->header->polyCount; ++i) |
| 531 | { |
| 532 | dtPoly* poly = &tile->polys[i]; |
| 533 | poly->firstLink = DT_NULL_LINK; |
| 534 | |
| 535 | if (poly->getType() == DT_POLYTYPE_OFFMESH_CONNECTION) |
| 536 | continue; |
| 537 | |
| 538 | // Build edge links backwards so that the links will be |
| 539 | // in the linked list from lowest index to highest. |
| 540 | for (int j = poly->vertCount-1; j >= 0; --j) |
| 541 | { |
| 542 | // Skip hard and non-internal edges. |
| 543 | if (poly->neis[j] == 0 || (poly->neis[j] & DT_EXT_LINK)) continue; |
| 544 | |
| 545 | unsigned int idx = allocLink(tile); |
| 546 | if (idx != DT_NULL_LINK) |
| 547 | { |
| 548 | dtLink* link = &tile->links[idx]; |
| 549 | link->ref = base | (dtPolyRef)(poly->neis[j]-1); |
| 550 | link->edge = (unsigned char)j; |
| 551 | link->side = 0xff; |
| 552 | link->bmin = link->bmax = 0; |
| 553 | // Add to linked list. |
| 554 | link->next = poly->firstLink; |
| 555 | poly->firstLink = idx; |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | void dtNavMesh::baseOffMeshLinks(dtMeshTile* tile) |
| 562 | { |
| 563 | if (!tile) return; |
| 564 | |
| 565 | dtPolyRef base = getPolyRefBase(tile); |
| 566 | |
| 567 | // Base off-mesh connection start points. |
| 568 | for (int i = 0; i < tile->header->offMeshConCount; ++i) |
| 569 | { |
| 570 | dtOffMeshConnection* con = &tile->offMeshCons[i]; |
| 571 | dtPoly* poly = &tile->polys[con->poly]; |
| 572 | |
| 573 | const float halfExtents[3] = { con->rad, tile->header->walkableClimb, con->rad }; |
| 574 | |
| 575 | // Find polygon to connect to. |
| 576 | const float* p = &con->pos[0]; // First vertex |
| 577 | float nearestPt[3]; |
| 578 | dtPolyRef ref = findNearestPolyInTile(tile, p, halfExtents, nearestPt); |
| 579 | if (!ref) continue; |
| 580 | // findNearestPoly may return too optimistic results, further check to make sure. |
| 581 | if (dtSqr(nearestPt[0]-p[0])+dtSqr(nearestPt[2]-p[2]) > dtSqr(con->rad)) |
| 582 | continue; |
| 583 | // Make sure the location is on current mesh. |
| 584 | float* v = &tile->verts[poly->verts[0]*3]; |
| 585 | dtVcopy(v, nearestPt); |
| 586 | |
| 587 | // Link off-mesh connection to target poly. |
| 588 | unsigned int idx = allocLink(tile); |
| 589 | if (idx != DT_NULL_LINK) |
| 590 | { |
| 591 | dtLink* link = &tile->links[idx]; |
| 592 | link->ref = ref; |
| 593 | link->edge = (unsigned char)0; |
| 594 | link->side = 0xff; |
| 595 | link->bmin = link->bmax = 0; |
| 596 | // Add to linked list. |
| 597 | link->next = poly->firstLink; |
| 598 | poly->firstLink = idx; |
| 599 | } |
| 600 | |
| 601 | // Start end-point is always connect back to off-mesh connection. |
| 602 | unsigned int tidx = allocLink(tile); |
| 603 | if (tidx != DT_NULL_LINK) |
| 604 | { |
| 605 | const unsigned short landPolyIdx = (unsigned short)decodePolyIdPoly(ref); |
| 606 | dtPoly* landPoly = &tile->polys[landPolyIdx]; |
| 607 | dtLink* link = &tile->links[tidx]; |
| 608 | link->ref = base | (dtPolyRef)(con->poly); |
| 609 | link->edge = 0xff; |
| 610 | link->side = 0xff; |
| 611 | link->bmin = link->bmax = 0; |
| 612 | // Add to linked list. |
| 613 | link->next = landPoly->firstLink; |
| 614 | landPoly->firstLink = tidx; |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | namespace |
| 620 | { |
| 621 | template<bool onlyBoundary> |
| 622 | void closestPointOnDetailEdges(const dtMeshTile* tile, const dtPoly* poly, const float* pos, float* closest) |
| 623 | { |
| 624 | const unsigned int ip = (unsigned int)(poly - tile->polys); |
| 625 | const dtPolyDetail* pd = &tile->detailMeshes[ip]; |
| 626 | |
| 627 | float dmin = FLT_MAX; |
| 628 | float tmin = 0; |
| 629 | const float* pmin = 0; |
| 630 | const float* pmax = 0; |
| 631 | |
| 632 | for (int i = 0; i < pd->triCount; i++) |
| 633 | { |
| 634 | const unsigned char* tris = &tile->detailTris[(pd->triBase + i) * 4]; |
| 635 | const int ANY_BOUNDARY_EDGE = |
| 636 | (DT_DETAIL_EDGE_BOUNDARY << 0) | |
| 637 | (DT_DETAIL_EDGE_BOUNDARY << 2) | |
| 638 | (DT_DETAIL_EDGE_BOUNDARY << 4); |
| 639 | if (onlyBoundary && (tris[3] & ANY_BOUNDARY_EDGE) == 0) |
| 640 | continue; |
| 641 | |
| 642 | const float* v[3]; |
| 643 | for (int j = 0; j < 3; ++j) |
| 644 | { |
| 645 | if (tris[j] < poly->vertCount) |
| 646 | v[j] = &tile->verts[poly->verts[tris[j]] * 3]; |
| 647 | else |
| 648 | v[j] = &tile->detailVerts[(pd->vertBase + (tris[j] - poly->vertCount)) * 3]; |
| 649 | } |
| 650 | |
| 651 | for (int k = 0, j = 2; k < 3; j = k++) |
| 652 | { |
| 653 | if ((dtGetDetailTriEdgeFlags(tris[3], j) & DT_DETAIL_EDGE_BOUNDARY) == 0 && |
| 654 | (onlyBoundary || tris[j] < tris[k])) |
| 655 | { |
| 656 | // Only looking at boundary edges and this is internal, or |
| 657 | // this is an inner edge that we will see again or have already seen. |
| 658 | continue; |
| 659 | } |
| 660 | |
| 661 | float t; |
| 662 | float d = dtDistancePtSegSqr2D(pos, v[j], v[k], t); |
| 663 | if (d < dmin) |
| 664 | { |
| 665 | dmin = d; |
| 666 | tmin = t; |
| 667 | pmin = v[j]; |
| 668 | pmax = v[k]; |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | dtVlerp(closest, pmin, pmax, tmin); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | bool dtNavMesh::getPolyHeight(const dtMeshTile* tile, const dtPoly* poly, const float* pos, float* height) const |
| 678 | { |
| 679 | // Off-mesh connections do not have detail polys and getting height |
| 680 | // over them does not make sense. |
| 681 | if (poly->getType() == DT_POLYTYPE_OFFMESH_CONNECTION) |
| 682 | return false; |
| 683 | |
| 684 | const unsigned int ip = (unsigned int)(poly - tile->polys); |
| 685 | const dtPolyDetail* pd = &tile->detailMeshes[ip]; |
| 686 | |
| 687 | float verts[DT_VERTS_PER_POLYGON*3]; |
| 688 | const int nv = poly->vertCount; |
| 689 | for (int i = 0; i < nv; ++i) |
| 690 | dtVcopy(&verts[i*3], &tile->verts[poly->verts[i]*3]); |
| 691 | |
| 692 | if (!dtPointInPolygon(pos, verts, nv)) |
| 693 | return false; |
| 694 | |
| 695 | if (!height) |
| 696 | return true; |
| 697 | |
| 698 | // Find height at the location. |
| 699 | for (int j = 0; j < pd->triCount; ++j) |
| 700 | { |
| 701 | const unsigned char* t = &tile->detailTris[(pd->triBase+j)*4]; |
| 702 | const float* v[3]; |
| 703 | for (int k = 0; k < 3; ++k) |
| 704 | { |
| 705 | if (t[k] < poly->vertCount) |
| 706 | v[k] = &tile->verts[poly->verts[t[k]]*3]; |
| 707 | else |
| 708 | v[k] = &tile->detailVerts[(pd->vertBase+(t[k]-poly->vertCount))*3]; |
| 709 | } |
| 710 | float h; |
| 711 | if (dtClosestHeightPointTriangle(pos, v[0], v[1], v[2], h)) |
| 712 | { |
| 713 | *height = h; |
| 714 | return true; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | // If all triangle checks failed above (can happen with degenerate triangles |
| 719 | // or larger floating point values) the point is on an edge, so just select |
| 720 | // closest. This should almost never happen so the extra iteration here is |
| 721 | // ok. |
| 722 | float closest[3]; |
| 723 | closestPointOnDetailEdges<false>(tile, poly, pos, closest); |
| 724 | *height = closest[1]; |
| 725 | return true; |
| 726 | } |
| 727 | |
| 728 | void dtNavMesh::closestPointOnPoly(dtPolyRef ref, const float* pos, float* closest, bool* posOverPoly) const |
| 729 | { |
| 730 | const dtMeshTile* tile = 0; |
| 731 | const dtPoly* poly = 0; |
| 732 | getTileAndPolyByRefUnsafe(ref, &tile, &poly); |
| 733 | |
| 734 | dtVcopy(closest, pos); |
| 735 | if (getPolyHeight(tile, poly, pos, &closest[1])) |
| 736 | { |
| 737 | if (posOverPoly) |
| 738 | *posOverPoly = true; |
| 739 | return; |
| 740 | } |
| 741 | |
| 742 | if (posOverPoly) |
| 743 | *posOverPoly = false; |
| 744 | |
| 745 | // Off-mesh connections don't have detail polygons. |
| 746 | if (poly->getType() == DT_POLYTYPE_OFFMESH_CONNECTION) |
| 747 | { |
| 748 | const float* v0 = &tile->verts[poly->verts[0]*3]; |
| 749 | const float* v1 = &tile->verts[poly->verts[1]*3]; |
| 750 | float t; |
| 751 | dtDistancePtSegSqr2D(pos, v0, v1, t); |
| 752 | dtVlerp(closest, v0, v1, t); |
| 753 | return; |
| 754 | } |
| 755 | |
| 756 | // Outside poly that is not an offmesh connection. |
| 757 | closestPointOnDetailEdges<true>(tile, poly, pos, closest); |
| 758 | } |
| 759 | |
| 760 | dtPolyRef dtNavMesh::findNearestPolyInTile(const dtMeshTile* tile, |
| 761 | const float* center, const float* halfExtents, |
| 762 | float* nearestPt) const |
| 763 | { |
| 764 | float bmin[3], bmax[3]; |
| 765 | dtVsub(bmin, center, halfExtents); |
| 766 | dtVadd(bmax, center, halfExtents); |
| 767 | |
| 768 | // Get nearby polygons from proximity grid. |
| 769 | dtPolyRef polys[128]; |
| 770 | int polyCount = queryPolygonsInTile(tile, bmin, bmax, polys, 128); |
| 771 | |
| 772 | // Find nearest polygon amongst the nearby polygons. |
| 773 | dtPolyRef nearest = 0; |
| 774 | float nearestDistanceSqr = FLT_MAX; |
| 775 | for (int i = 0; i < polyCount; ++i) |
| 776 | { |
| 777 | dtPolyRef ref = polys[i]; |
| 778 | float closestPtPoly[3]; |
| 779 | float diff[3]; |
| 780 | bool posOverPoly = false; |
| 781 | float d; |
| 782 | closestPointOnPoly(ref, center, closestPtPoly, &posOverPoly); |
| 783 | |
| 784 | // If a point is directly over a polygon and closer than |
| 785 | // climb height, favor that instead of straight line nearest point. |
| 786 | dtVsub(diff, center, closestPtPoly); |
| 787 | if (posOverPoly) |
| 788 | { |
| 789 | d = dtAbs(diff[1]) - tile->header->walkableClimb; |
| 790 | d = d > 0 ? d*d : 0; |
| 791 | } |
| 792 | else |
| 793 | { |
| 794 | d = dtVlenSqr(diff); |
| 795 | } |
| 796 | |
| 797 | if (d < nearestDistanceSqr) |
| 798 | { |
| 799 | dtVcopy(nearestPt, closestPtPoly); |
| 800 | nearestDistanceSqr = d; |
| 801 | nearest = ref; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | return nearest; |
| 806 | } |
| 807 | |
| 808 | int dtNavMesh::queryPolygonsInTile(const dtMeshTile* tile, const float* qmin, const float* qmax, |
| 809 | dtPolyRef* polys, const int maxPolys) const |
| 810 | { |
| 811 | if (tile->bvTree) |
| 812 | { |
| 813 | const dtBVNode* node = &tile->bvTree[0]; |
| 814 | const dtBVNode* end = &tile->bvTree[tile->header->bvNodeCount]; |
| 815 | const float* tbmin = tile->header->bmin; |
| 816 | const float* tbmax = tile->header->bmax; |
| 817 | const float qfac = tile->header->bvQuantFactor; |
| 818 | |
| 819 | // Calculate quantized box |
| 820 | unsigned short bmin[3], bmax[3]; |
| 821 | // dtClamp query box to world box. |
| 822 | float minx = dtClamp(qmin[0], tbmin[0], tbmax[0]) - tbmin[0]; |
| 823 | float miny = dtClamp(qmin[1], tbmin[1], tbmax[1]) - tbmin[1]; |
| 824 | float minz = dtClamp(qmin[2], tbmin[2], tbmax[2]) - tbmin[2]; |
| 825 | float maxx = dtClamp(qmax[0], tbmin[0], tbmax[0]) - tbmin[0]; |
| 826 | float maxy = dtClamp(qmax[1], tbmin[1], tbmax[1]) - tbmin[1]; |
| 827 | float maxz = dtClamp(qmax[2], tbmin[2], tbmax[2]) - tbmin[2]; |
| 828 | // Quantize |
| 829 | bmin[0] = (unsigned short)(qfac * minx) & 0xfffe; |
| 830 | bmin[1] = (unsigned short)(qfac * miny) & 0xfffe; |
| 831 | bmin[2] = (unsigned short)(qfac * minz) & 0xfffe; |
| 832 | bmax[0] = (unsigned short)(qfac * maxx + 1) | 1; |
| 833 | bmax[1] = (unsigned short)(qfac * maxy + 1) | 1; |
| 834 | bmax[2] = (unsigned short)(qfac * maxz + 1) | 1; |
| 835 | |
| 836 | // Traverse tree |
| 837 | dtPolyRef base = getPolyRefBase(tile); |
| 838 | int n = 0; |
| 839 | while (node < end) |
| 840 | { |
| 841 | const bool overlap = dtOverlapQuantBounds(bmin, bmax, node->bmin, node->bmax); |
| 842 | const bool isLeafNode = node->i >= 0; |
| 843 | |
| 844 | if (isLeafNode && overlap) |
| 845 | { |
| 846 | if (n < maxPolys) |
| 847 | polys[n++] = base | (dtPolyRef)node->i; |
| 848 | } |
| 849 | |
| 850 | if (overlap || isLeafNode) |
| 851 | node++; |
| 852 | else |
| 853 | { |
| 854 | const int escapeIndex = -node->i; |
| 855 | node += escapeIndex; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | return n; |
| 860 | } |
| 861 | else |
| 862 | { |
| 863 | float bmin[3], bmax[3]; |
| 864 | int n = 0; |
| 865 | dtPolyRef base = getPolyRefBase(tile); |
| 866 | for (int i = 0; i < tile->header->polyCount; ++i) |
| 867 | { |
| 868 | dtPoly* p = &tile->polys[i]; |
| 869 | // Do not return off-mesh connection polygons. |
| 870 | if (p->getType() == DT_POLYTYPE_OFFMESH_CONNECTION) |
| 871 | continue; |
| 872 | // Calc polygon bounds. |
| 873 | const float* v = &tile->verts[p->verts[0]*3]; |
| 874 | dtVcopy(bmin, v); |
| 875 | dtVcopy(bmax, v); |
| 876 | for (int j = 1; j < p->vertCount; ++j) |
| 877 | { |
| 878 | v = &tile->verts[p->verts[j]*3]; |
| 879 | dtVmin(bmin, v); |
| 880 | dtVmax(bmax, v); |
| 881 | } |
| 882 | if (dtOverlapBounds(qmin,qmax, bmin,bmax)) |
| 883 | { |
| 884 | if (n < maxPolys) |
| 885 | polys[n++] = base | (dtPolyRef)i; |
| 886 | } |
| 887 | } |
| 888 | return n; |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | /// @par |
| 893 | /// |
| 894 | /// The add operation will fail if the data is in the wrong format, the allocated tile |
| 895 | /// space is full, or there is a tile already at the specified reference. |
| 896 | /// |
| 897 | /// The lastRef parameter is used to restore a tile with the same tile |
| 898 | /// reference it had previously used. In this case the #dtPolyRef's for the |
| 899 | /// tile will be restored to the same values they were before the tile was |
| 900 | /// removed. |
| 901 | /// |
| 902 | /// The nav mesh assumes exclusive access to the data passed and will make |
| 903 | /// changes to the dynamic portion of the data. For that reason the data |
| 904 | /// should not be reused in other nav meshes until the tile has been successfully |
| 905 | /// removed from this nav mesh. |
| 906 | /// |
| 907 | /// @see dtCreateNavMeshData, #removeTile |
| 908 | dtStatus dtNavMesh::addTile(unsigned char* data, int dataSize, int flags, |
| 909 | dtTileRef lastRef, dtTileRef* result) |
| 910 | { |
| 911 | // Make sure the data is in right format. |
| 912 | dtMeshHeader* header = (dtMeshHeader*)data; |
| 913 | if (header->magic != DT_NAVMESH_MAGIC) |
| 914 | return DT_FAILURE | DT_WRONG_MAGIC; |
| 915 | if (header->version != DT_NAVMESH_VERSION) |
| 916 | return DT_FAILURE | DT_WRONG_VERSION; |
| 917 | |
| 918 | #ifndef DT_POLYREF64 |
| 919 | // Do not allow adding more polygons than specified in the NavMesh's maxPolys constraint. |
| 920 | // Otherwise, the poly ID cannot be represented with the given number of bits. |
| 921 | if (m_polyBits < dtIlog2(dtNextPow2((unsigned int)header->polyCount))) |
| 922 | return DT_FAILURE | DT_INVALID_PARAM; |
| 923 | #endif |
| 924 | |
| 925 | // Make sure the location is free. |
| 926 | if (getTileAt(header->x, header->y, header->layer)) |
| 927 | return DT_FAILURE | DT_ALREADY_OCCUPIED; |
| 928 | |
| 929 | // Allocate a tile. |
| 930 | dtMeshTile* tile = 0; |
| 931 | if (!lastRef) |
| 932 | { |
| 933 | if (m_nextFree) |
| 934 | { |
| 935 | tile = m_nextFree; |
| 936 | m_nextFree = tile->next; |
| 937 | tile->next = 0; |
| 938 | } |
| 939 | } |
| 940 | else |
| 941 | { |
| 942 | // Try to relocate the tile to specific index with same salt. |
| 943 | int tileIndex = (int)decodePolyIdTile((dtPolyRef)lastRef); |
| 944 | if (tileIndex >= m_maxTiles) |
| 945 | return DT_FAILURE | DT_OUT_OF_MEMORY; |
| 946 | // Try to find the specific tile id from the free list. |
| 947 | dtMeshTile* target = &m_tiles[tileIndex]; |
| 948 | dtMeshTile* prev = 0; |
| 949 | tile = m_nextFree; |
| 950 | while (tile && tile != target) |
| 951 | { |
| 952 | prev = tile; |
| 953 | tile = tile->next; |
| 954 | } |
| 955 | // Could not find the correct location. |
| 956 | if (tile != target) |
| 957 | return DT_FAILURE | DT_OUT_OF_MEMORY; |
| 958 | // Remove from freelist |
| 959 | if (!prev) |
| 960 | m_nextFree = tile->next; |
| 961 | else |
| 962 | prev->next = tile->next; |
| 963 | |
| 964 | // Restore salt. |
| 965 | tile->salt = decodePolyIdSalt((dtPolyRef)lastRef); |
| 966 | } |
| 967 | |
| 968 | // Make sure we could allocate a tile. |
| 969 | if (!tile) |
| 970 | return DT_FAILURE | DT_OUT_OF_MEMORY; |
| 971 | |
| 972 | // Insert tile into the position lut. |
| 973 | int h = computeTileHash(header->x, header->y, m_tileLutMask); |
| 974 | tile->next = m_posLookup[h]; |
| 975 | m_posLookup[h] = tile; |
| 976 | |
| 977 | // Patch header pointers. |
| 978 | const int headerSize = dtAlign4(sizeof(dtMeshHeader)); |
| 979 | const int vertsSize = dtAlign4(sizeof(float)*3*header->vertCount); |
| 980 | const int polysSize = dtAlign4(sizeof(dtPoly)*header->polyCount); |
| 981 | const int linksSize = dtAlign4(sizeof(dtLink)*(header->maxLinkCount)); |
| 982 | const int detailMeshesSize = dtAlign4(sizeof(dtPolyDetail)*header->detailMeshCount); |
| 983 | const int detailVertsSize = dtAlign4(sizeof(float)*3*header->detailVertCount); |
| 984 | const int detailTrisSize = dtAlign4(sizeof(unsigned char)*4*header->detailTriCount); |
| 985 | const int bvtreeSize = dtAlign4(sizeof(dtBVNode)*header->bvNodeCount); |
| 986 | const int offMeshLinksSize = dtAlign4(sizeof(dtOffMeshConnection)*header->offMeshConCount); |
| 987 | |
| 988 | unsigned char* d = data + headerSize; |
| 989 | tile->verts = dtGetThenAdvanceBufferPointer<float>(d, vertsSize); |
| 990 | tile->polys = dtGetThenAdvanceBufferPointer<dtPoly>(d, polysSize); |
| 991 | tile->links = dtGetThenAdvanceBufferPointer<dtLink>(d, linksSize); |
| 992 | tile->detailMeshes = dtGetThenAdvanceBufferPointer<dtPolyDetail>(d, detailMeshesSize); |
| 993 | tile->detailVerts = dtGetThenAdvanceBufferPointer<float>(d, detailVertsSize); |
| 994 | tile->detailTris = dtGetThenAdvanceBufferPointer<unsigned char>(d, detailTrisSize); |
| 995 | tile->bvTree = dtGetThenAdvanceBufferPointer<dtBVNode>(d, bvtreeSize); |
| 996 | tile->offMeshCons = dtGetThenAdvanceBufferPointer<dtOffMeshConnection>(d, offMeshLinksSize); |
| 997 | |
| 998 | // If there are no items in the bvtree, reset the tree pointer. |
| 999 | if (!bvtreeSize) |
| 1000 | tile->bvTree = 0; |
| 1001 | |
| 1002 | // Build links freelist |
| 1003 | tile->linksFreeList = 0; |
| 1004 | tile->links[header->maxLinkCount-1].next = DT_NULL_LINK; |
| 1005 | for (int i = 0; i < header->maxLinkCount-1; ++i) |
| 1006 | tile->links[i].next = i+1; |
| 1007 | |
| 1008 | // Init tile. |
| 1009 | tile->header = header; |
| 1010 | tile->data = data; |
| 1011 | tile->dataSize = dataSize; |
| 1012 | tile->flags = flags; |
| 1013 | |
| 1014 | connectIntLinks(tile); |
| 1015 | |
| 1016 | // Base off-mesh connections to their starting polygons and connect connections inside the tile. |
| 1017 | baseOffMeshLinks(tile); |
| 1018 | connectExtOffMeshLinks(tile, tile, -1); |
| 1019 | |
| 1020 | // Create connections with neighbour tiles. |
| 1021 | static const int MAX_NEIS = 32; |
| 1022 | dtMeshTile* neis[MAX_NEIS]; |
| 1023 | int nneis; |
| 1024 | |
| 1025 | // Connect with layers in current tile. |
| 1026 | nneis = getTilesAt(header->x, header->y, neis, MAX_NEIS); |
| 1027 | for (int j = 0; j < nneis; ++j) |
| 1028 | { |
| 1029 | if (neis[j] == tile) |
| 1030 | continue; |
| 1031 | |
| 1032 | connectExtLinks(tile, neis[j], -1); |
| 1033 | connectExtLinks(neis[j], tile, -1); |
| 1034 | connectExtOffMeshLinks(tile, neis[j], -1); |
| 1035 | connectExtOffMeshLinks(neis[j], tile, -1); |
| 1036 | } |
| 1037 | |
| 1038 | // Connect with neighbour tiles. |
| 1039 | for (int i = 0; i < 8; ++i) |
| 1040 | { |
| 1041 | nneis = getNeighbourTilesAt(header->x, header->y, i, neis, MAX_NEIS); |
| 1042 | for (int j = 0; j < nneis; ++j) |
| 1043 | { |
| 1044 | connectExtLinks(tile, neis[j], i); |
| 1045 | connectExtLinks(neis[j], tile, dtOppositeTile(i)); |
| 1046 | connectExtOffMeshLinks(tile, neis[j], i); |
| 1047 | connectExtOffMeshLinks(neis[j], tile, dtOppositeTile(i)); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | if (result) |
| 1052 | *result = getTileRef(tile); |
| 1053 | |
| 1054 | return DT_SUCCESS; |
| 1055 | } |
| 1056 | |
| 1057 | const dtMeshTile* dtNavMesh::getTileAt(const int x, const int y, const int layer) const |
| 1058 | { |
| 1059 | // Find tile based on hash. |
| 1060 | int h = computeTileHash(x,y,m_tileLutMask); |
| 1061 | dtMeshTile* tile = m_posLookup[h]; |
| 1062 | while (tile) |
| 1063 | { |
| 1064 | if (tile->header && |
| 1065 | tile->header->x == x && |
| 1066 | tile->header->y == y && |
| 1067 | tile->header->layer == layer) |
| 1068 | { |
| 1069 | return tile; |
| 1070 | } |
| 1071 | tile = tile->next; |
| 1072 | } |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
| 1076 | int dtNavMesh::getNeighbourTilesAt(const int x, const int y, const int side, dtMeshTile** tiles, const int maxTiles) const |
| 1077 | { |
| 1078 | int nx = x, ny = y; |
| 1079 | switch (side) |
| 1080 | { |
| 1081 | case 0: nx++; break; |
| 1082 | case 1: nx++; ny++; break; |
| 1083 | case 2: ny++; break; |
| 1084 | case 3: nx--; ny++; break; |
| 1085 | case 4: nx--; break; |
| 1086 | case 5: nx--; ny--; break; |
| 1087 | case 6: ny--; break; |
| 1088 | case 7: nx++; ny--; break; |
| 1089 | }; |
| 1090 | |
| 1091 | return getTilesAt(nx, ny, tiles, maxTiles); |
| 1092 | } |
| 1093 | |
| 1094 | int dtNavMesh::getTilesAt(const int x, const int y, dtMeshTile** tiles, const int maxTiles) const |
| 1095 | { |
| 1096 | int n = 0; |
| 1097 | |
| 1098 | // Find tile based on hash. |
| 1099 | int h = computeTileHash(x,y,m_tileLutMask); |
| 1100 | dtMeshTile* tile = m_posLookup[h]; |
| 1101 | while (tile) |
| 1102 | { |
| 1103 | if (tile->header && |
| 1104 | tile->header->x == x && |
| 1105 | tile->header->y == y) |
| 1106 | { |
| 1107 | if (n < maxTiles) |
| 1108 | tiles[n++] = tile; |
| 1109 | } |
| 1110 | tile = tile->next; |
| 1111 | } |
| 1112 | |
| 1113 | return n; |
| 1114 | } |
| 1115 | |
| 1116 | /// @par |
| 1117 | /// |
| 1118 | /// This function will not fail if the tiles array is too small to hold the |
| 1119 | /// entire result set. It will simply fill the array to capacity. |
| 1120 | int dtNavMesh::getTilesAt(const int x, const int y, dtMeshTile const** tiles, const int maxTiles) const |
| 1121 | { |
| 1122 | int n = 0; |
| 1123 | |
| 1124 | // Find tile based on hash. |
| 1125 | int h = computeTileHash(x,y,m_tileLutMask); |
| 1126 | dtMeshTile* tile = m_posLookup[h]; |
| 1127 | while (tile) |
| 1128 | { |
| 1129 | if (tile->header && |
| 1130 | tile->header->x == x && |
| 1131 | tile->header->y == y) |
| 1132 | { |
| 1133 | if (n < maxTiles) |
| 1134 | tiles[n++] = tile; |
| 1135 | } |
| 1136 | tile = tile->next; |
| 1137 | } |
| 1138 | |
| 1139 | return n; |
| 1140 | } |
| 1141 | |
| 1142 | |
| 1143 | dtTileRef dtNavMesh::getTileRefAt(const int x, const int y, const int layer) const |
| 1144 | { |
| 1145 | // Find tile based on hash. |
| 1146 | int h = computeTileHash(x,y,m_tileLutMask); |
| 1147 | dtMeshTile* tile = m_posLookup[h]; |
| 1148 | while (tile) |
| 1149 | { |
| 1150 | if (tile->header && |
| 1151 | tile->header->x == x && |
| 1152 | tile->header->y == y && |
| 1153 | tile->header->layer == layer) |
| 1154 | { |
| 1155 | return getTileRef(tile); |
| 1156 | } |
| 1157 | tile = tile->next; |
| 1158 | } |
| 1159 | return 0; |
| 1160 | } |
| 1161 | |
| 1162 | const dtMeshTile* dtNavMesh::getTileByRef(dtTileRef ref) const |
| 1163 | { |
| 1164 | if (!ref) |
| 1165 | return 0; |
| 1166 | unsigned int tileIndex = decodePolyIdTile((dtPolyRef)ref); |
| 1167 | unsigned int tileSalt = decodePolyIdSalt((dtPolyRef)ref); |
| 1168 | if ((int)tileIndex >= m_maxTiles) |
| 1169 | return 0; |
| 1170 | const dtMeshTile* tile = &m_tiles[tileIndex]; |
| 1171 | if (tile->salt != tileSalt) |
| 1172 | return 0; |
| 1173 | return tile; |
| 1174 | } |
| 1175 | |
| 1176 | int dtNavMesh::getMaxTiles() const |
| 1177 | { |
| 1178 | return m_maxTiles; |
| 1179 | } |
| 1180 | |
| 1181 | dtMeshTile* dtNavMesh::getTile(int i) |
| 1182 | { |
| 1183 | return &m_tiles[i]; |
| 1184 | } |
| 1185 | |
| 1186 | const dtMeshTile* dtNavMesh::getTile(int i) const |
| 1187 | { |
| 1188 | return &m_tiles[i]; |
| 1189 | } |
| 1190 | |
| 1191 | void dtNavMesh::calcTileLoc(const float* pos, int* tx, int* ty) const |
| 1192 | { |
| 1193 | *tx = (int)floorf((pos[0]-m_orig[0]) / m_tileWidth); |
| 1194 | *ty = (int)floorf((pos[2]-m_orig[2]) / m_tileHeight); |
| 1195 | } |
| 1196 | |
| 1197 | dtStatus dtNavMesh::getTileAndPolyByRef(const dtPolyRef ref, const dtMeshTile** tile, const dtPoly** poly) const |
| 1198 | { |
| 1199 | if (!ref) return DT_FAILURE; |
| 1200 | unsigned int salt, it, ip; |
| 1201 | decodePolyId(ref, salt, it, ip); |
| 1202 | if (it >= (unsigned int)m_maxTiles) return DT_FAILURE | DT_INVALID_PARAM; |
| 1203 | if (m_tiles[it].salt != salt || m_tiles[it].header == 0) return DT_FAILURE | DT_INVALID_PARAM; |
| 1204 | if (ip >= (unsigned int)m_tiles[it].header->polyCount) return DT_FAILURE | DT_INVALID_PARAM; |
| 1205 | *tile = &m_tiles[it]; |
| 1206 | *poly = &m_tiles[it].polys[ip]; |
| 1207 | return DT_SUCCESS; |
| 1208 | } |
| 1209 | |
| 1210 | /// @par |
| 1211 | /// |
| 1212 | /// @warning Only use this function if it is known that the provided polygon |
| 1213 | /// reference is valid. This function is faster than #getTileAndPolyByRef, but |
| 1214 | /// it does not validate the reference. |
| 1215 | void dtNavMesh::getTileAndPolyByRefUnsafe(const dtPolyRef ref, const dtMeshTile** tile, const dtPoly** poly) const |
| 1216 | { |
| 1217 | unsigned int salt, it, ip; |
| 1218 | decodePolyId(ref, salt, it, ip); |
| 1219 | *tile = &m_tiles[it]; |
| 1220 | *poly = &m_tiles[it].polys[ip]; |
| 1221 | } |
| 1222 | |
| 1223 | bool dtNavMesh::isValidPolyRef(dtPolyRef ref) const |
| 1224 | { |
| 1225 | if (!ref) return false; |
| 1226 | unsigned int salt, it, ip; |
| 1227 | decodePolyId(ref, salt, it, ip); |
| 1228 | if (it >= (unsigned int)m_maxTiles) return false; |
| 1229 | if (m_tiles[it].salt != salt || m_tiles[it].header == 0) return false; |
| 1230 | if (ip >= (unsigned int)m_tiles[it].header->polyCount) return false; |
| 1231 | return true; |
| 1232 | } |
| 1233 | |
| 1234 | /// @par |
| 1235 | /// |
| 1236 | /// This function returns the data for the tile so that, if desired, |
| 1237 | /// it can be added back to the navigation mesh at a later point. |
| 1238 | /// |
| 1239 | /// @see #addTile |
| 1240 | dtStatus dtNavMesh::removeTile(dtTileRef ref, unsigned char** data, int* dataSize) |
| 1241 | { |
| 1242 | if (!ref) |
| 1243 | return DT_FAILURE | DT_INVALID_PARAM; |
| 1244 | unsigned int tileIndex = decodePolyIdTile((dtPolyRef)ref); |
| 1245 | unsigned int tileSalt = decodePolyIdSalt((dtPolyRef)ref); |
| 1246 | if ((int)tileIndex >= m_maxTiles) |
| 1247 | return DT_FAILURE | DT_INVALID_PARAM; |
| 1248 | dtMeshTile* tile = &m_tiles[tileIndex]; |
| 1249 | if (tile->salt != tileSalt) |
| 1250 | return DT_FAILURE | DT_INVALID_PARAM; |
| 1251 | |
| 1252 | // Remove tile from hash lookup. |
| 1253 | int h = computeTileHash(tile->header->x,tile->header->y,m_tileLutMask); |
| 1254 | dtMeshTile* prev = 0; |
| 1255 | dtMeshTile* cur = m_posLookup[h]; |
| 1256 | while (cur) |
| 1257 | { |
| 1258 | if (cur == tile) |
| 1259 | { |
| 1260 | if (prev) |
| 1261 | prev->next = cur->next; |
| 1262 | else |
| 1263 | m_posLookup[h] = cur->next; |
| 1264 | break; |
| 1265 | } |
| 1266 | prev = cur; |
| 1267 | cur = cur->next; |
| 1268 | } |
| 1269 | |
| 1270 | // Remove connections to neighbour tiles. |
| 1271 | static const int MAX_NEIS = 32; |
| 1272 | dtMeshTile* neis[MAX_NEIS]; |
| 1273 | int nneis; |
| 1274 | |
| 1275 | // Disconnect from other layers in current tile. |
| 1276 | nneis = getTilesAt(tile->header->x, tile->header->y, neis, MAX_NEIS); |
| 1277 | for (int j = 0; j < nneis; ++j) |
| 1278 | { |
| 1279 | if (neis[j] == tile) continue; |
| 1280 | unconnectLinks(neis[j], tile); |
| 1281 | } |
| 1282 | |
| 1283 | // Disconnect from neighbour tiles. |
| 1284 | for (int i = 0; i < 8; ++i) |
| 1285 | { |
| 1286 | nneis = getNeighbourTilesAt(tile->header->x, tile->header->y, i, neis, MAX_NEIS); |
| 1287 | for (int j = 0; j < nneis; ++j) |
| 1288 | unconnectLinks(neis[j], tile); |
| 1289 | } |
| 1290 | |
| 1291 | // Reset tile. |
| 1292 | if (tile->flags & DT_TILE_FREE_DATA) |
| 1293 | { |
| 1294 | // Owns data |
| 1295 | dtFree(tile->data); |
| 1296 | tile->data = 0; |
| 1297 | tile->dataSize = 0; |
| 1298 | if (data) *data = 0; |
| 1299 | if (dataSize) *dataSize = 0; |
| 1300 | } |
| 1301 | else |
| 1302 | { |
| 1303 | if (data) *data = tile->data; |
| 1304 | if (dataSize) *dataSize = tile->dataSize; |
| 1305 | } |
| 1306 | |
| 1307 | tile->header = 0; |
| 1308 | tile->flags = 0; |
| 1309 | tile->linksFreeList = 0; |
| 1310 | tile->polys = 0; |
| 1311 | tile->verts = 0; |
| 1312 | tile->links = 0; |
| 1313 | tile->detailMeshes = 0; |
| 1314 | tile->detailVerts = 0; |
| 1315 | tile->detailTris = 0; |
| 1316 | tile->bvTree = 0; |
| 1317 | tile->offMeshCons = 0; |
| 1318 | |
| 1319 | // Update salt, salt should never be zero. |
| 1320 | #ifdef DT_POLYREF64 |
| 1321 | tile->salt = (tile->salt+1) & ((1<<DT_SALT_BITS)-1); |
| 1322 | #else |
| 1323 | tile->salt = (tile->salt+1) & ((1<<m_saltBits)-1); |
| 1324 | #endif |
| 1325 | if (tile->salt == 0) |
| 1326 | tile->salt++; |
| 1327 | |
| 1328 | // Add to free list. |
| 1329 | tile->next = m_nextFree; |
| 1330 | m_nextFree = tile; |
| 1331 | |
| 1332 | return DT_SUCCESS; |
| 1333 | } |
| 1334 | |
| 1335 | dtTileRef dtNavMesh::getTileRef(const dtMeshTile* tile) const |
| 1336 | { |
| 1337 | if (!tile) return 0; |
| 1338 | const unsigned int it = (unsigned int)(tile - m_tiles); |
| 1339 | return (dtTileRef)encodePolyId(tile->salt, it, 0); |
| 1340 | } |
| 1341 | |
| 1342 | /// @par |
| 1343 | /// |
| 1344 | /// Example use case: |
| 1345 | /// @code |
| 1346 | /// |
| 1347 | /// const dtPolyRef base = navmesh->getPolyRefBase(tile); |
| 1348 | /// for (int i = 0; i < tile->header->polyCount; ++i) |
| 1349 | /// { |
| 1350 | /// const dtPoly* p = &tile->polys[i]; |
| 1351 | /// const dtPolyRef ref = base | (dtPolyRef)i; |
| 1352 | /// |
| 1353 | /// // Use the reference to access the polygon data. |
| 1354 | /// } |
| 1355 | /// @endcode |
| 1356 | dtPolyRef dtNavMesh::getPolyRefBase(const dtMeshTile* tile) const |
| 1357 | { |
| 1358 | if (!tile) return 0; |
| 1359 | const unsigned int it = (unsigned int)(tile - m_tiles); |
| 1360 | return encodePolyId(tile->salt, it, 0); |
| 1361 | } |
| 1362 | |
| 1363 | struct dtTileState |
| 1364 | { |
| 1365 | int magic; // Magic number, used to identify the data. |
| 1366 | int version; // Data version number. |
| 1367 | dtTileRef ref; // Tile ref at the time of storing the data. |
| 1368 | }; |
| 1369 | |
| 1370 | struct dtPolyState |
| 1371 | { |
| 1372 | unsigned short flags; // Flags (see dtPolyFlags). |
| 1373 | unsigned char area; // Area ID of the polygon. |
| 1374 | }; |
| 1375 | |
| 1376 | /// @see #storeTileState |
| 1377 | int dtNavMesh::getTileStateSize(const dtMeshTile* tile) const |
| 1378 | { |
| 1379 | if (!tile) return 0; |
| 1380 | const int headerSize = dtAlign4(sizeof(dtTileState)); |
| 1381 | const int polyStateSize = dtAlign4(sizeof(dtPolyState) * tile->header->polyCount); |
| 1382 | return headerSize + polyStateSize; |
| 1383 | } |
| 1384 | |
| 1385 | /// @par |
| 1386 | /// |
| 1387 | /// Tile state includes non-structural data such as polygon flags, area ids, etc. |
| 1388 | /// @note The state data is only valid until the tile reference changes. |
| 1389 | /// @see #getTileStateSize, #restoreTileState |
| 1390 | dtStatus dtNavMesh::storeTileState(const dtMeshTile* tile, unsigned char* data, const int maxDataSize) const |
| 1391 | { |
| 1392 | // Make sure there is enough space to store the state. |
| 1393 | const int sizeReq = getTileStateSize(tile); |
| 1394 | if (maxDataSize < sizeReq) |
| 1395 | return DT_FAILURE | DT_BUFFER_TOO_SMALL; |
| 1396 | |
| 1397 | dtTileState* tileState = dtGetThenAdvanceBufferPointer<dtTileState>(data, dtAlign4(sizeof(dtTileState))); |
| 1398 | dtPolyState* polyStates = dtGetThenAdvanceBufferPointer<dtPolyState>(data, dtAlign4(sizeof(dtPolyState) * tile->header->polyCount)); |
| 1399 | |
| 1400 | // Store tile state. |
| 1401 | tileState->magic = DT_NAVMESH_STATE_MAGIC; |
| 1402 | tileState->version = DT_NAVMESH_STATE_VERSION; |
| 1403 | tileState->ref = getTileRef(tile); |
| 1404 | |
| 1405 | // Store per poly state. |
| 1406 | for (int i = 0; i < tile->header->polyCount; ++i) |
| 1407 | { |
| 1408 | const dtPoly* p = &tile->polys[i]; |
| 1409 | dtPolyState* s = &polyStates[i]; |
| 1410 | s->flags = p->flags; |
| 1411 | s->area = p->getArea(); |
| 1412 | } |
| 1413 | |
| 1414 | return DT_SUCCESS; |
| 1415 | } |
| 1416 | |
| 1417 | /// @par |
| 1418 | /// |
| 1419 | /// Tile state includes non-structural data such as polygon flags, area ids, etc. |
| 1420 | /// @note This function does not impact the tile's #dtTileRef and #dtPolyRef's. |
| 1421 | /// @see #storeTileState |
| 1422 | dtStatus dtNavMesh::restoreTileState(dtMeshTile* tile, const unsigned char* data, const int maxDataSize) |
| 1423 | { |
| 1424 | // Make sure there is enough space to store the state. |
| 1425 | const int sizeReq = getTileStateSize(tile); |
| 1426 | if (maxDataSize < sizeReq) |
| 1427 | return DT_FAILURE | DT_INVALID_PARAM; |
| 1428 | |
| 1429 | const dtTileState* tileState = dtGetThenAdvanceBufferPointer<const dtTileState>(data, dtAlign4(sizeof(dtTileState))); |
| 1430 | const dtPolyState* polyStates = dtGetThenAdvanceBufferPointer<const dtPolyState>(data, dtAlign4(sizeof(dtPolyState) * tile->header->polyCount)); |
| 1431 | |
| 1432 | // Check that the restore is possible. |
| 1433 | if (tileState->magic != DT_NAVMESH_STATE_MAGIC) |
| 1434 | return DT_FAILURE | DT_WRONG_MAGIC; |
| 1435 | if (tileState->version != DT_NAVMESH_STATE_VERSION) |
| 1436 | return DT_FAILURE | DT_WRONG_VERSION; |
| 1437 | if (tileState->ref != getTileRef(tile)) |
| 1438 | return DT_FAILURE | DT_INVALID_PARAM; |
| 1439 | |
| 1440 | // Restore per poly state. |
| 1441 | for (int i = 0; i < tile->header->polyCount; ++i) |
| 1442 | { |
| 1443 | dtPoly* p = &tile->polys[i]; |
| 1444 | const dtPolyState* s = &polyStates[i]; |
| 1445 | p->flags = s->flags; |
| 1446 | p->setArea(s->area); |
| 1447 | } |
| 1448 | |
| 1449 | return DT_SUCCESS; |
| 1450 | } |
| 1451 | |
| 1452 | /// @par |
| 1453 | /// |
| 1454 | /// Off-mesh connections are stored in the navigation mesh as special 2-vertex |
| 1455 | /// polygons with a single edge. At least one of the vertices is expected to be |
| 1456 | /// inside a normal polygon. So an off-mesh connection is "entered" from a |
| 1457 | /// normal polygon at one of its endpoints. This is the polygon identified by |
| 1458 | /// the prevRef parameter. |
| 1459 | dtStatus dtNavMesh::getOffMeshConnectionPolyEndPoints(dtPolyRef prevRef, dtPolyRef polyRef, float* startPos, float* endPos) const |
| 1460 | { |
| 1461 | unsigned int salt, it, ip; |
| 1462 | |
| 1463 | if (!polyRef) |
| 1464 | return DT_FAILURE; |
| 1465 | |
| 1466 | // Get current polygon |
| 1467 | decodePolyId(polyRef, salt, it, ip); |
| 1468 | if (it >= (unsigned int)m_maxTiles) return DT_FAILURE | DT_INVALID_PARAM; |
| 1469 | if (m_tiles[it].salt != salt || m_tiles[it].header == 0) return DT_FAILURE | DT_INVALID_PARAM; |
| 1470 | const dtMeshTile* tile = &m_tiles[it]; |
| 1471 | if (ip >= (unsigned int)tile->header->polyCount) return DT_FAILURE | DT_INVALID_PARAM; |
| 1472 | const dtPoly* poly = &tile->polys[ip]; |
| 1473 | |
| 1474 | // Make sure that the current poly is indeed off-mesh link. |
| 1475 | if (poly->getType() != DT_POLYTYPE_OFFMESH_CONNECTION) |
| 1476 | return DT_FAILURE; |
| 1477 | |
| 1478 | // Figure out which way to hand out the vertices. |
| 1479 | int idx0 = 0, idx1 = 1; |
| 1480 | |
| 1481 | // Find link that points to first vertex. |
| 1482 | for (unsigned int i = poly->firstLink; i != DT_NULL_LINK; i = tile->links[i].next) |
| 1483 | { |
| 1484 | if (tile->links[i].edge == 0) |
| 1485 | { |
| 1486 | if (tile->links[i].ref != prevRef) |
| 1487 | { |
| 1488 | idx0 = 1; |
| 1489 | idx1 = 0; |
| 1490 | } |
| 1491 | break; |
| 1492 | } |
| 1493 | } |
| 1494 | |
| 1495 | dtVcopy(startPos, &tile->verts[poly->verts[idx0]*3]); |
| 1496 | dtVcopy(endPos, &tile->verts[poly->verts[idx1]*3]); |
| 1497 | |
| 1498 | return DT_SUCCESS; |
| 1499 | } |
| 1500 | |
| 1501 | |
| 1502 | const dtOffMeshConnection* dtNavMesh::getOffMeshConnectionByRef(dtPolyRef ref) const |
| 1503 | { |
| 1504 | unsigned int salt, it, ip; |
| 1505 | |
| 1506 | if (!ref) |
| 1507 | return 0; |
| 1508 | |
| 1509 | // Get current polygon |
| 1510 | decodePolyId(ref, salt, it, ip); |
| 1511 | if (it >= (unsigned int)m_maxTiles) return 0; |
| 1512 | if (m_tiles[it].salt != salt || m_tiles[it].header == 0) return 0; |
| 1513 | const dtMeshTile* tile = &m_tiles[it]; |
| 1514 | if (ip >= (unsigned int)tile->header->polyCount) return 0; |
| 1515 | const dtPoly* poly = &tile->polys[ip]; |
| 1516 | |
| 1517 | // Make sure that the current poly is indeed off-mesh link. |
| 1518 | if (poly->getType() != DT_POLYTYPE_OFFMESH_CONNECTION) |
| 1519 | return 0; |
| 1520 | |
| 1521 | const unsigned int idx = ip - tile->header->offMeshBase; |
| 1522 | dtAssert(idx < (unsigned int)tile->header->offMeshConCount); |
| 1523 | return &tile->offMeshCons[idx]; |
| 1524 | } |
| 1525 | |
| 1526 | |
| 1527 | dtStatus dtNavMesh::setPolyFlags(dtPolyRef ref, unsigned short flags) |
| 1528 | { |
| 1529 | if (!ref) return DT_FAILURE; |
| 1530 | unsigned int salt, it, ip; |
| 1531 | decodePolyId(ref, salt, it, ip); |
| 1532 | if (it >= (unsigned int)m_maxTiles) return DT_FAILURE | DT_INVALID_PARAM; |
| 1533 | if (m_tiles[it].salt != salt || m_tiles[it].header == 0) return DT_FAILURE | DT_INVALID_PARAM; |
| 1534 | dtMeshTile* tile = &m_tiles[it]; |
| 1535 | if (ip >= (unsigned int)tile->header->polyCount) return DT_FAILURE | DT_INVALID_PARAM; |
| 1536 | dtPoly* poly = &tile->polys[ip]; |
| 1537 | |
| 1538 | // Change flags. |
| 1539 | poly->flags = flags; |
| 1540 | |
| 1541 | return DT_SUCCESS; |
| 1542 | } |
| 1543 | |
| 1544 | dtStatus dtNavMesh::getPolyFlags(dtPolyRef ref, unsigned short* resultFlags) const |
| 1545 | { |
| 1546 | if (!ref) return DT_FAILURE; |
| 1547 | unsigned int salt, it, ip; |
| 1548 | decodePolyId(ref, salt, it, ip); |
| 1549 | if (it >= (unsigned int)m_maxTiles) return DT_FAILURE | DT_INVALID_PARAM; |
| 1550 | if (m_tiles[it].salt != salt || m_tiles[it].header == 0) return DT_FAILURE | DT_INVALID_PARAM; |
| 1551 | const dtMeshTile* tile = &m_tiles[it]; |
| 1552 | if (ip >= (unsigned int)tile->header->polyCount) return DT_FAILURE | DT_INVALID_PARAM; |
| 1553 | const dtPoly* poly = &tile->polys[ip]; |
| 1554 | |
| 1555 | *resultFlags = poly->flags; |
| 1556 | |
| 1557 | return DT_SUCCESS; |
| 1558 | } |
| 1559 | |
| 1560 | dtStatus dtNavMesh::setPolyArea(dtPolyRef ref, unsigned char area) |
| 1561 | { |
| 1562 | if (!ref) return DT_FAILURE; |
| 1563 | unsigned int salt, it, ip; |
| 1564 | decodePolyId(ref, salt, it, ip); |
| 1565 | if (it >= (unsigned int)m_maxTiles) return DT_FAILURE | DT_INVALID_PARAM; |
| 1566 | if (m_tiles[it].salt != salt || m_tiles[it].header == 0) return DT_FAILURE | DT_INVALID_PARAM; |
| 1567 | dtMeshTile* tile = &m_tiles[it]; |
| 1568 | if (ip >= (unsigned int)tile->header->polyCount) return DT_FAILURE | DT_INVALID_PARAM; |
| 1569 | dtPoly* poly = &tile->polys[ip]; |
| 1570 | |
| 1571 | poly->setArea(area); |
| 1572 | |
| 1573 | return DT_SUCCESS; |
| 1574 | } |
| 1575 | |
| 1576 | dtStatus dtNavMesh::getPolyArea(dtPolyRef ref, unsigned char* resultArea) const |
| 1577 | { |
| 1578 | if (!ref) return DT_FAILURE; |
| 1579 | unsigned int salt, it, ip; |
| 1580 | decodePolyId(ref, salt, it, ip); |
| 1581 | if (it >= (unsigned int)m_maxTiles) return DT_FAILURE | DT_INVALID_PARAM; |
| 1582 | if (m_tiles[it].salt != salt || m_tiles[it].header == 0) return DT_FAILURE | DT_INVALID_PARAM; |
| 1583 | const dtMeshTile* tile = &m_tiles[it]; |
| 1584 | if (ip >= (unsigned int)tile->header->polyCount) return DT_FAILURE | DT_INVALID_PARAM; |
| 1585 | const dtPoly* poly = &tile->polys[ip]; |
| 1586 | |
| 1587 | *resultArea = poly->getArea(); |
| 1588 | |
| 1589 | return DT_SUCCESS; |
| 1590 | } |
| 1591 |