| 1 | #include "DetourTileCache.h" |
| 2 | #include "DetourTileCacheBuilder.h" |
| 3 | #include "DetourNavMeshBuilder.h" |
| 4 | #include "DetourNavMesh.h" |
| 5 | #include "DetourCommon.h" |
| 6 | #include "DetourMath.h" |
| 7 | #include "DetourAlloc.h" |
| 8 | #include "DetourAssert.h" |
| 9 | #include <string.h> |
| 10 | #include <new> |
| 11 | |
| 12 | dtTileCache* dtAllocTileCache() |
| 13 | { |
| 14 | void* mem = dtAlloc(sizeof(dtTileCache), DT_ALLOC_PERM); |
| 15 | if (!mem) return 0; |
| 16 | return new(mem) dtTileCache; |
| 17 | } |
| 18 | |
| 19 | void dtFreeTileCache(dtTileCache* tc) |
| 20 | { |
| 21 | if (!tc) return; |
| 22 | tc->~dtTileCache(); |
| 23 | dtFree(tc); |
| 24 | } |
| 25 | |
| 26 | static bool contains(const dtCompressedTileRef* a, const int n, const dtCompressedTileRef v) |
| 27 | { |
| 28 | for (int i = 0; i < n; ++i) |
| 29 | if (a[i] == v) |
| 30 | return true; |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | inline int computeTileHash(int x, int y, const int mask) |
| 35 | { |
| 36 | const unsigned int h1 = 0x8da6b343; // Large multiplicative constants; |
| 37 | const unsigned int h2 = 0xd8163841; // here arbitrarily chosen primes |
| 38 | unsigned int n = h1 * x + h2 * y; |
| 39 | return (int)(n & mask); |
| 40 | } |
| 41 | |
| 42 | |
| 43 | struct NavMeshTileBuildContext |
| 44 | { |
| 45 | inline NavMeshTileBuildContext(struct dtTileCacheAlloc* a) : layer(0), lcset(0), lmesh(0), alloc(a) {} |
| 46 | inline ~NavMeshTileBuildContext() { purge(); } |
| 47 | void purge() |
| 48 | { |
| 49 | dtFreeTileCacheLayer(alloc, layer); |
| 50 | layer = 0; |
| 51 | dtFreeTileCacheContourSet(alloc, lcset); |
| 52 | lcset = 0; |
| 53 | dtFreeTileCachePolyMesh(alloc, lmesh); |
| 54 | lmesh = 0; |
| 55 | } |
| 56 | struct dtTileCacheLayer* layer; |
| 57 | struct dtTileCacheContourSet* lcset; |
| 58 | struct dtTileCachePolyMesh* lmesh; |
| 59 | struct dtTileCacheAlloc* alloc; |
| 60 | }; |
| 61 | |
| 62 | |
| 63 | dtTileCache::dtTileCache() : |
| 64 | m_tileLutSize(0), |
| 65 | m_tileLutMask(0), |
| 66 | m_posLookup(0), |
| 67 | m_nextFreeTile(0), |
| 68 | m_tiles(0), |
| 69 | m_saltBits(0), |
| 70 | m_tileBits(0), |
| 71 | m_talloc(0), |
| 72 | m_tcomp(0), |
| 73 | m_tmproc(0), |
| 74 | m_obstacles(0), |
| 75 | m_nextFreeObstacle(0), |
| 76 | m_nreqs(0), |
| 77 | m_nupdate(0) |
| 78 | { |
| 79 | memset(&m_params, 0, sizeof(m_params)); |
| 80 | memset(m_reqs, 0, sizeof(ObstacleRequest) * MAX_REQUESTS); |
| 81 | } |
| 82 | |
| 83 | dtTileCache::~dtTileCache() |
| 84 | { |
| 85 | for (int i = 0; i < m_params.maxTiles; ++i) |
| 86 | { |
| 87 | if (m_tiles[i].flags & DT_COMPRESSEDTILE_FREE_DATA) |
| 88 | { |
| 89 | dtFree(m_tiles[i].data); |
| 90 | m_tiles[i].data = 0; |
| 91 | } |
| 92 | } |
| 93 | dtFree(m_obstacles); |
| 94 | m_obstacles = 0; |
| 95 | dtFree(m_posLookup); |
| 96 | m_posLookup = 0; |
| 97 | dtFree(m_tiles); |
| 98 | m_tiles = 0; |
| 99 | m_nreqs = 0; |
| 100 | m_nupdate = 0; |
| 101 | } |
| 102 | |
| 103 | const dtCompressedTile* dtTileCache::getTileByRef(dtCompressedTileRef ref) const |
| 104 | { |
| 105 | if (!ref) |
| 106 | return 0; |
| 107 | unsigned int tileIndex = decodeTileIdTile(ref); |
| 108 | unsigned int tileSalt = decodeTileIdSalt(ref); |
| 109 | if ((int)tileIndex >= m_params.maxTiles) |
| 110 | return 0; |
| 111 | const dtCompressedTile* tile = &m_tiles[tileIndex]; |
| 112 | if (tile->salt != tileSalt) |
| 113 | return 0; |
| 114 | return tile; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | dtStatus dtTileCache::init(const dtTileCacheParams* params, |
| 119 | dtTileCacheAlloc* talloc, |
| 120 | dtTileCacheCompressor* tcomp, |
| 121 | dtTileCacheMeshProcess* tmproc) |
| 122 | { |
| 123 | m_talloc = talloc; |
| 124 | m_tcomp = tcomp; |
| 125 | m_tmproc = tmproc; |
| 126 | m_nreqs = 0; |
| 127 | memcpy(&m_params, params, sizeof(m_params)); |
| 128 | |
| 129 | // Alloc space for obstacles. |
| 130 | m_obstacles = (dtTileCacheObstacle*)dtAlloc(sizeof(dtTileCacheObstacle)*m_params.maxObstacles, DT_ALLOC_PERM); |
| 131 | if (!m_obstacles) |
| 132 | return DT_FAILURE | DT_OUT_OF_MEMORY; |
| 133 | memset(m_obstacles, 0, sizeof(dtTileCacheObstacle)*m_params.maxObstacles); |
| 134 | m_nextFreeObstacle = 0; |
| 135 | for (int i = m_params.maxObstacles-1; i >= 0; --i) |
| 136 | { |
| 137 | m_obstacles[i].salt = 1; |
| 138 | m_obstacles[i].next = m_nextFreeObstacle; |
| 139 | m_nextFreeObstacle = &m_obstacles[i]; |
| 140 | } |
| 141 | |
| 142 | // Init tiles |
| 143 | m_tileLutSize = dtNextPow2(m_params.maxTiles/4); |
| 144 | if (!m_tileLutSize) m_tileLutSize = 1; |
| 145 | m_tileLutMask = m_tileLutSize-1; |
| 146 | |
| 147 | m_tiles = (dtCompressedTile*)dtAlloc(sizeof(dtCompressedTile)*m_params.maxTiles, DT_ALLOC_PERM); |
| 148 | if (!m_tiles) |
| 149 | return DT_FAILURE | DT_OUT_OF_MEMORY; |
| 150 | m_posLookup = (dtCompressedTile**)dtAlloc(sizeof(dtCompressedTile*)*m_tileLutSize, DT_ALLOC_PERM); |
| 151 | if (!m_posLookup) |
| 152 | return DT_FAILURE | DT_OUT_OF_MEMORY; |
| 153 | memset(m_tiles, 0, sizeof(dtCompressedTile)*m_params.maxTiles); |
| 154 | memset(m_posLookup, 0, sizeof(dtCompressedTile*)*m_tileLutSize); |
| 155 | m_nextFreeTile = 0; |
| 156 | for (int i = m_params.maxTiles-1; i >= 0; --i) |
| 157 | { |
| 158 | m_tiles[i].salt = 1; |
| 159 | m_tiles[i].next = m_nextFreeTile; |
| 160 | m_nextFreeTile = &m_tiles[i]; |
| 161 | } |
| 162 | |
| 163 | // Init ID generator values. |
| 164 | m_tileBits = dtIlog2(dtNextPow2((unsigned int)m_params.maxTiles)); |
| 165 | // Only allow 31 salt bits, since the salt mask is calculated using 32bit uint and it will overflow. |
| 166 | m_saltBits = dtMin((unsigned int)31, 32 - m_tileBits); |
| 167 | if (m_saltBits < 10) |
| 168 | return DT_FAILURE | DT_INVALID_PARAM; |
| 169 | |
| 170 | return DT_SUCCESS; |
| 171 | } |
| 172 | |
| 173 | int dtTileCache::getTilesAt(const int tx, const int ty, dtCompressedTileRef* tiles, const int maxTiles) const |
| 174 | { |
| 175 | int n = 0; |
| 176 | |
| 177 | // Find tile based on hash. |
| 178 | int h = computeTileHash(tx,ty,m_tileLutMask); |
| 179 | dtCompressedTile* tile = m_posLookup[h]; |
| 180 | while (tile) |
| 181 | { |
| 182 | if (tile->header && |
| 183 | tile->header->tx == tx && |
| 184 | tile->header->ty == ty) |
| 185 | { |
| 186 | if (n < maxTiles) |
| 187 | tiles[n++] = getTileRef(tile); |
| 188 | } |
| 189 | tile = tile->next; |
| 190 | } |
| 191 | |
| 192 | return n; |
| 193 | } |
| 194 | |
| 195 | dtCompressedTile* dtTileCache::getTileAt(const int tx, const int ty, const int tlayer) |
| 196 | { |
| 197 | // Find tile based on hash. |
| 198 | int h = computeTileHash(tx,ty,m_tileLutMask); |
| 199 | dtCompressedTile* tile = m_posLookup[h]; |
| 200 | while (tile) |
| 201 | { |
| 202 | if (tile->header && |
| 203 | tile->header->tx == tx && |
| 204 | tile->header->ty == ty && |
| 205 | tile->header->tlayer == tlayer) |
| 206 | { |
| 207 | return tile; |
| 208 | } |
| 209 | tile = tile->next; |
| 210 | } |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | dtCompressedTileRef dtTileCache::getTileRef(const dtCompressedTile* tile) const |
| 215 | { |
| 216 | if (!tile) return 0; |
| 217 | const unsigned int it = (unsigned int)(tile - m_tiles); |
| 218 | return (dtCompressedTileRef)encodeTileId(tile->salt, it); |
| 219 | } |
| 220 | |
| 221 | dtObstacleRef dtTileCache::getObstacleRef(const dtTileCacheObstacle* ob) const |
| 222 | { |
| 223 | if (!ob) return 0; |
| 224 | const unsigned int idx = (unsigned int)(ob - m_obstacles); |
| 225 | return encodeObstacleId(ob->salt, idx); |
| 226 | } |
| 227 | |
| 228 | const dtTileCacheObstacle* dtTileCache::getObstacleByRef(dtObstacleRef ref) |
| 229 | { |
| 230 | if (!ref) |
| 231 | return 0; |
| 232 | unsigned int idx = decodeObstacleIdObstacle(ref); |
| 233 | if ((int)idx >= m_params.maxObstacles) |
| 234 | return 0; |
| 235 | const dtTileCacheObstacle* ob = &m_obstacles[idx]; |
| 236 | unsigned int salt = decodeObstacleIdSalt(ref); |
| 237 | if (ob->salt != salt) |
| 238 | return 0; |
| 239 | return ob; |
| 240 | } |
| 241 | |
| 242 | dtTileCacheMeshProcess::~dtTileCacheMeshProcess() |
| 243 | { |
| 244 | // Defined out of line to fix the weak v-tables warning |
| 245 | } |
| 246 | |
| 247 | dtStatus dtTileCache::addTile(unsigned char* data, const int dataSize, unsigned char flags, dtCompressedTileRef* result) |
| 248 | { |
| 249 | // Make sure the data is in right format. |
| 250 | dtTileCacheLayerHeader* header = (dtTileCacheLayerHeader*)data; |
| 251 | if (header->magic != DT_TILECACHE_MAGIC) |
| 252 | return DT_FAILURE | DT_WRONG_MAGIC; |
| 253 | if (header->version != DT_TILECACHE_VERSION) |
| 254 | return DT_FAILURE | DT_WRONG_VERSION; |
| 255 | |
| 256 | // Make sure the location is free. |
| 257 | if (getTileAt(header->tx, header->ty, header->tlayer)) |
| 258 | return DT_FAILURE; |
| 259 | |
| 260 | // Allocate a tile. |
| 261 | dtCompressedTile* tile = 0; |
| 262 | if (m_nextFreeTile) |
| 263 | { |
| 264 | tile = m_nextFreeTile; |
| 265 | m_nextFreeTile = tile->next; |
| 266 | tile->next = 0; |
| 267 | } |
| 268 | |
| 269 | // Make sure we could allocate a tile. |
| 270 | if (!tile) |
| 271 | return DT_FAILURE | DT_OUT_OF_MEMORY; |
| 272 | |
| 273 | // Insert tile into the position lut. |
| 274 | int h = computeTileHash(header->tx, header->ty, m_tileLutMask); |
| 275 | tile->next = m_posLookup[h]; |
| 276 | m_posLookup[h] = tile; |
| 277 | |
| 278 | // Init tile. |
| 279 | const int headerSize = dtAlign4(sizeof(dtTileCacheLayerHeader)); |
| 280 | tile->header = (dtTileCacheLayerHeader*)data; |
| 281 | tile->data = data; |
| 282 | tile->dataSize = dataSize; |
| 283 | tile->compressed = tile->data + headerSize; |
| 284 | tile->compressedSize = tile->dataSize - headerSize; |
| 285 | tile->flags = flags; |
| 286 | |
| 287 | if (result) |
| 288 | *result = getTileRef(tile); |
| 289 | |
| 290 | return DT_SUCCESS; |
| 291 | } |
| 292 | |
| 293 | dtStatus dtTileCache::removeTile(dtCompressedTileRef ref, unsigned char** data, int* dataSize) |
| 294 | { |
| 295 | if (!ref) |
| 296 | return DT_FAILURE | DT_INVALID_PARAM; |
| 297 | unsigned int tileIndex = decodeTileIdTile(ref); |
| 298 | unsigned int tileSalt = decodeTileIdSalt(ref); |
| 299 | if ((int)tileIndex >= m_params.maxTiles) |
| 300 | return DT_FAILURE | DT_INVALID_PARAM; |
| 301 | dtCompressedTile* tile = &m_tiles[tileIndex]; |
| 302 | if (tile->salt != tileSalt) |
| 303 | return DT_FAILURE | DT_INVALID_PARAM; |
| 304 | |
| 305 | // Remove tile from hash lookup. |
| 306 | const int h = computeTileHash(tile->header->tx,tile->header->ty,m_tileLutMask); |
| 307 | dtCompressedTile* prev = 0; |
| 308 | dtCompressedTile* cur = m_posLookup[h]; |
| 309 | while (cur) |
| 310 | { |
| 311 | if (cur == tile) |
| 312 | { |
| 313 | if (prev) |
| 314 | prev->next = cur->next; |
| 315 | else |
| 316 | m_posLookup[h] = cur->next; |
| 317 | break; |
| 318 | } |
| 319 | prev = cur; |
| 320 | cur = cur->next; |
| 321 | } |
| 322 | |
| 323 | // Reset tile. |
| 324 | if (tile->flags & DT_COMPRESSEDTILE_FREE_DATA) |
| 325 | { |
| 326 | // Owns data |
| 327 | dtFree(tile->data); |
| 328 | tile->data = 0; |
| 329 | tile->dataSize = 0; |
| 330 | if (data) *data = 0; |
| 331 | if (dataSize) *dataSize = 0; |
| 332 | } |
| 333 | else |
| 334 | { |
| 335 | if (data) *data = tile->data; |
| 336 | if (dataSize) *dataSize = tile->dataSize; |
| 337 | } |
| 338 | |
| 339 | tile->header = 0; |
| 340 | tile->data = 0; |
| 341 | tile->dataSize = 0; |
| 342 | tile->compressed = 0; |
| 343 | tile->compressedSize = 0; |
| 344 | tile->flags = 0; |
| 345 | |
| 346 | // Update salt, salt should never be zero. |
| 347 | tile->salt = (tile->salt+1) & ((1<<m_saltBits)-1); |
| 348 | if (tile->salt == 0) |
| 349 | tile->salt++; |
| 350 | |
| 351 | // Add to free list. |
| 352 | tile->next = m_nextFreeTile; |
| 353 | m_nextFreeTile = tile; |
| 354 | |
| 355 | return DT_SUCCESS; |
| 356 | } |
| 357 | |
| 358 | |
| 359 | dtStatus dtTileCache::addObstacle(const float* pos, const float radius, const float height, dtObstacleRef* result) |
| 360 | { |
| 361 | if (m_nreqs >= MAX_REQUESTS) |
| 362 | return DT_FAILURE | DT_BUFFER_TOO_SMALL; |
| 363 | |
| 364 | dtTileCacheObstacle* ob = 0; |
| 365 | if (m_nextFreeObstacle) |
| 366 | { |
| 367 | ob = m_nextFreeObstacle; |
| 368 | m_nextFreeObstacle = ob->next; |
| 369 | ob->next = 0; |
| 370 | } |
| 371 | if (!ob) |
| 372 | return DT_FAILURE | DT_OUT_OF_MEMORY; |
| 373 | |
| 374 | unsigned short salt = ob->salt; |
| 375 | memset(ob, 0, sizeof(dtTileCacheObstacle)); |
| 376 | ob->salt = salt; |
| 377 | ob->state = DT_OBSTACLE_PROCESSING; |
| 378 | ob->type = DT_OBSTACLE_CYLINDER; |
| 379 | dtVcopy(ob->cylinder.pos, pos); |
| 380 | ob->cylinder.radius = radius; |
| 381 | ob->cylinder.height = height; |
| 382 | |
| 383 | ObstacleRequest* req = &m_reqs[m_nreqs++]; |
| 384 | memset(req, 0, sizeof(ObstacleRequest)); |
| 385 | req->action = REQUEST_ADD; |
| 386 | req->ref = getObstacleRef(ob); |
| 387 | |
| 388 | if (result) |
| 389 | *result = req->ref; |
| 390 | |
| 391 | return DT_SUCCESS; |
| 392 | } |
| 393 | |
| 394 | dtStatus dtTileCache::addBoxObstacle(const float* bmin, const float* bmax, dtObstacleRef* result) |
| 395 | { |
| 396 | if (m_nreqs >= MAX_REQUESTS) |
| 397 | return DT_FAILURE | DT_BUFFER_TOO_SMALL; |
| 398 | |
| 399 | dtTileCacheObstacle* ob = 0; |
| 400 | if (m_nextFreeObstacle) |
| 401 | { |
| 402 | ob = m_nextFreeObstacle; |
| 403 | m_nextFreeObstacle = ob->next; |
| 404 | ob->next = 0; |
| 405 | } |
| 406 | if (!ob) |
| 407 | return DT_FAILURE | DT_OUT_OF_MEMORY; |
| 408 | |
| 409 | unsigned short salt = ob->salt; |
| 410 | memset(ob, 0, sizeof(dtTileCacheObstacle)); |
| 411 | ob->salt = salt; |
| 412 | ob->state = DT_OBSTACLE_PROCESSING; |
| 413 | ob->type = DT_OBSTACLE_BOX; |
| 414 | dtVcopy(ob->box.bmin, bmin); |
| 415 | dtVcopy(ob->box.bmax, bmax); |
| 416 | |
| 417 | ObstacleRequest* req = &m_reqs[m_nreqs++]; |
| 418 | memset(req, 0, sizeof(ObstacleRequest)); |
| 419 | req->action = REQUEST_ADD; |
| 420 | req->ref = getObstacleRef(ob); |
| 421 | |
| 422 | if (result) |
| 423 | *result = req->ref; |
| 424 | |
| 425 | return DT_SUCCESS; |
| 426 | } |
| 427 | |
| 428 | dtStatus dtTileCache::addBoxObstacle(const float* center, const float* halfExtents, const float yRadians, dtObstacleRef* result) |
| 429 | { |
| 430 | if (m_nreqs >= MAX_REQUESTS) |
| 431 | return DT_FAILURE | DT_BUFFER_TOO_SMALL; |
| 432 | |
| 433 | dtTileCacheObstacle* ob = 0; |
| 434 | if (m_nextFreeObstacle) |
| 435 | { |
| 436 | ob = m_nextFreeObstacle; |
| 437 | m_nextFreeObstacle = ob->next; |
| 438 | ob->next = 0; |
| 439 | } |
| 440 | if (!ob) |
| 441 | return DT_FAILURE | DT_OUT_OF_MEMORY; |
| 442 | |
| 443 | unsigned short salt = ob->salt; |
| 444 | memset(ob, 0, sizeof(dtTileCacheObstacle)); |
| 445 | ob->salt = salt; |
| 446 | ob->state = DT_OBSTACLE_PROCESSING; |
| 447 | ob->type = DT_OBSTACLE_ORIENTED_BOX; |
| 448 | dtVcopy(ob->orientedBox.center, center); |
| 449 | dtVcopy(ob->orientedBox.halfExtents, halfExtents); |
| 450 | |
| 451 | float coshalf= cosf(0.5f*yRadians); |
| 452 | float sinhalf = sinf(-0.5f*yRadians); |
| 453 | ob->orientedBox.rotAux[0] = coshalf*sinhalf; |
| 454 | ob->orientedBox.rotAux[1] = coshalf*coshalf - 0.5f; |
| 455 | |
| 456 | ObstacleRequest* req = &m_reqs[m_nreqs++]; |
| 457 | memset(req, 0, sizeof(ObstacleRequest)); |
| 458 | req->action = REQUEST_ADD; |
| 459 | req->ref = getObstacleRef(ob); |
| 460 | |
| 461 | if (result) |
| 462 | *result = req->ref; |
| 463 | |
| 464 | return DT_SUCCESS; |
| 465 | } |
| 466 | |
| 467 | dtStatus dtTileCache::removeObstacle(const dtObstacleRef ref) |
| 468 | { |
| 469 | if (!ref) |
| 470 | return DT_SUCCESS; |
| 471 | if (m_nreqs >= MAX_REQUESTS) |
| 472 | return DT_FAILURE | DT_BUFFER_TOO_SMALL; |
| 473 | |
| 474 | ObstacleRequest* req = &m_reqs[m_nreqs++]; |
| 475 | memset(req, 0, sizeof(ObstacleRequest)); |
| 476 | req->action = REQUEST_REMOVE; |
| 477 | req->ref = ref; |
| 478 | |
| 479 | return DT_SUCCESS; |
| 480 | } |
| 481 | |
| 482 | dtStatus dtTileCache::queryTiles(const float* bmin, const float* bmax, |
| 483 | dtCompressedTileRef* results, int* resultCount, const int maxResults) const |
| 484 | { |
| 485 | const int MAX_TILES = 32; |
| 486 | dtCompressedTileRef tiles[MAX_TILES]; |
| 487 | |
| 488 | int n = 0; |
| 489 | |
| 490 | const float tw = m_params.width * m_params.cs; |
| 491 | const float th = m_params.height * m_params.cs; |
| 492 | const int tx0 = (int)dtMathFloorf((bmin[0]-m_params.orig[0]) / tw); |
| 493 | const int tx1 = (int)dtMathFloorf((bmax[0]-m_params.orig[0]) / tw); |
| 494 | const int ty0 = (int)dtMathFloorf((bmin[2]-m_params.orig[2]) / th); |
| 495 | const int ty1 = (int)dtMathFloorf((bmax[2]-m_params.orig[2]) / th); |
| 496 | |
| 497 | for (int ty = ty0; ty <= ty1; ++ty) |
| 498 | { |
| 499 | for (int tx = tx0; tx <= tx1; ++tx) |
| 500 | { |
| 501 | const int ntiles = getTilesAt(tx,ty,tiles,MAX_TILES); |
| 502 | |
| 503 | for (int i = 0; i < ntiles; ++i) |
| 504 | { |
| 505 | const dtCompressedTile* tile = &m_tiles[decodeTileIdTile(tiles[i])]; |
| 506 | float tbmin[3], tbmax[3]; |
| 507 | calcTightTileBounds(tile->header, tbmin, tbmax); |
| 508 | |
| 509 | if (dtOverlapBounds(bmin,bmax, tbmin,tbmax)) |
| 510 | { |
| 511 | if (n < maxResults) |
| 512 | results[n++] = tiles[i]; |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | *resultCount = n; |
| 519 | |
| 520 | return DT_SUCCESS; |
| 521 | } |
| 522 | |
| 523 | dtStatus dtTileCache::update(const float /*dt*/, dtNavMesh* navmesh, |
| 524 | bool* upToDate) |
| 525 | { |
| 526 | if (m_nupdate == 0) |
| 527 | { |
| 528 | // Process requests. |
| 529 | for (int i = 0; i < m_nreqs; ++i) |
| 530 | { |
| 531 | ObstacleRequest* req = &m_reqs[i]; |
| 532 | |
| 533 | unsigned int idx = decodeObstacleIdObstacle(req->ref); |
| 534 | if ((int)idx >= m_params.maxObstacles) |
| 535 | continue; |
| 536 | dtTileCacheObstacle* ob = &m_obstacles[idx]; |
| 537 | unsigned int salt = decodeObstacleIdSalt(req->ref); |
| 538 | if (ob->salt != salt) |
| 539 | continue; |
| 540 | |
| 541 | if (req->action == REQUEST_ADD) |
| 542 | { |
| 543 | // Find touched tiles. |
| 544 | float bmin[3], bmax[3]; |
| 545 | getObstacleBounds(ob, bmin, bmax); |
| 546 | |
| 547 | int ntouched = 0; |
| 548 | queryTiles(bmin, bmax, ob->touched, &ntouched, DT_MAX_TOUCHED_TILES); |
| 549 | ob->ntouched = (unsigned char)ntouched; |
| 550 | // Add tiles to update list. |
| 551 | ob->npending = 0; |
| 552 | for (int j = 0; j < ob->ntouched; ++j) |
| 553 | { |
| 554 | if (m_nupdate < MAX_UPDATE) |
| 555 | { |
| 556 | if (!contains(m_update, m_nupdate, ob->touched[j])) |
| 557 | m_update[m_nupdate++] = ob->touched[j]; |
| 558 | ob->pending[ob->npending++] = ob->touched[j]; |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | else if (req->action == REQUEST_REMOVE) |
| 563 | { |
| 564 | // Prepare to remove obstacle. |
| 565 | ob->state = DT_OBSTACLE_REMOVING; |
| 566 | // Add tiles to update list. |
| 567 | ob->npending = 0; |
| 568 | for (int j = 0; j < ob->ntouched; ++j) |
| 569 | { |
| 570 | if (m_nupdate < MAX_UPDATE) |
| 571 | { |
| 572 | if (!contains(m_update, m_nupdate, ob->touched[j])) |
| 573 | m_update[m_nupdate++] = ob->touched[j]; |
| 574 | ob->pending[ob->npending++] = ob->touched[j]; |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | m_nreqs = 0; |
| 581 | } |
| 582 | |
| 583 | dtStatus status = DT_SUCCESS; |
| 584 | // Process updates |
| 585 | if (m_nupdate) |
| 586 | { |
| 587 | // Build mesh |
| 588 | const dtCompressedTileRef ref = m_update[0]; |
| 589 | status = buildNavMeshTile(ref, navmesh); |
| 590 | m_nupdate--; |
| 591 | if (m_nupdate > 0) |
| 592 | memmove(m_update, m_update+1, m_nupdate*sizeof(dtCompressedTileRef)); |
| 593 | |
| 594 | // Update obstacle states. |
| 595 | for (int i = 0; i < m_params.maxObstacles; ++i) |
| 596 | { |
| 597 | dtTileCacheObstacle* ob = &m_obstacles[i]; |
| 598 | if (ob->state == DT_OBSTACLE_PROCESSING || ob->state == DT_OBSTACLE_REMOVING) |
| 599 | { |
| 600 | // Remove handled tile from pending list. |
| 601 | for (int j = 0; j < (int)ob->npending; j++) |
| 602 | { |
| 603 | if (ob->pending[j] == ref) |
| 604 | { |
| 605 | ob->pending[j] = ob->pending[(int)ob->npending-1]; |
| 606 | ob->npending--; |
| 607 | break; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | // If all pending tiles processed, change state. |
| 612 | if (ob->npending == 0) |
| 613 | { |
| 614 | if (ob->state == DT_OBSTACLE_PROCESSING) |
| 615 | { |
| 616 | ob->state = DT_OBSTACLE_PROCESSED; |
| 617 | } |
| 618 | else if (ob->state == DT_OBSTACLE_REMOVING) |
| 619 | { |
| 620 | ob->state = DT_OBSTACLE_EMPTY; |
| 621 | // Update salt, salt should never be zero. |
| 622 | ob->salt = (ob->salt+1) & ((1<<16)-1); |
| 623 | if (ob->salt == 0) |
| 624 | ob->salt++; |
| 625 | // Return obstacle to free list. |
| 626 | ob->next = m_nextFreeObstacle; |
| 627 | m_nextFreeObstacle = ob; |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | if (upToDate) |
| 635 | *upToDate = m_nupdate == 0 && m_nreqs == 0; |
| 636 | |
| 637 | return status; |
| 638 | } |
| 639 | |
| 640 | |
| 641 | dtStatus dtTileCache::buildNavMeshTilesAt(const int tx, const int ty, dtNavMesh* navmesh) |
| 642 | { |
| 643 | const int MAX_TILES = 32; |
| 644 | dtCompressedTileRef tiles[MAX_TILES]; |
| 645 | const int ntiles = getTilesAt(tx,ty,tiles,MAX_TILES); |
| 646 | |
| 647 | for (int i = 0; i < ntiles; ++i) |
| 648 | { |
| 649 | dtStatus status = buildNavMeshTile(tiles[i], navmesh); |
| 650 | if (dtStatusFailed(status)) |
| 651 | return status; |
| 652 | } |
| 653 | |
| 654 | return DT_SUCCESS; |
| 655 | } |
| 656 | |
| 657 | dtStatus dtTileCache::buildNavMeshTile(const dtCompressedTileRef ref, dtNavMesh* navmesh) |
| 658 | { |
| 659 | dtAssert(m_talloc); |
| 660 | dtAssert(m_tcomp); |
| 661 | |
| 662 | unsigned int idx = decodeTileIdTile(ref); |
| 663 | if (idx > (unsigned int)m_params.maxTiles) |
| 664 | return DT_FAILURE | DT_INVALID_PARAM; |
| 665 | const dtCompressedTile* tile = &m_tiles[idx]; |
| 666 | unsigned int salt = decodeTileIdSalt(ref); |
| 667 | if (tile->salt != salt) |
| 668 | return DT_FAILURE | DT_INVALID_PARAM; |
| 669 | |
| 670 | m_talloc->reset(); |
| 671 | |
| 672 | NavMeshTileBuildContext bc(m_talloc); |
| 673 | const int walkableClimbVx = (int)(m_params.walkableClimb / m_params.ch); |
| 674 | dtStatus status; |
| 675 | |
| 676 | // Decompress tile layer data. |
| 677 | status = dtDecompressTileCacheLayer(m_talloc, m_tcomp, tile->data, tile->dataSize, &bc.layer); |
| 678 | if (dtStatusFailed(status)) |
| 679 | return status; |
| 680 | |
| 681 | // Rasterize obstacles. |
| 682 | for (int i = 0; i < m_params.maxObstacles; ++i) |
| 683 | { |
| 684 | const dtTileCacheObstacle* ob = &m_obstacles[i]; |
| 685 | if (ob->state == DT_OBSTACLE_EMPTY || ob->state == DT_OBSTACLE_REMOVING) |
| 686 | continue; |
| 687 | if (contains(ob->touched, ob->ntouched, ref)) |
| 688 | { |
| 689 | if (ob->type == DT_OBSTACLE_CYLINDER) |
| 690 | { |
| 691 | dtMarkCylinderArea(*bc.layer, tile->header->bmin, m_params.cs, m_params.ch, |
| 692 | ob->cylinder.pos, ob->cylinder.radius, ob->cylinder.height, 0); |
| 693 | } |
| 694 | else if (ob->type == DT_OBSTACLE_BOX) |
| 695 | { |
| 696 | dtMarkBoxArea(*bc.layer, tile->header->bmin, m_params.cs, m_params.ch, |
| 697 | ob->box.bmin, ob->box.bmax, 0); |
| 698 | } |
| 699 | else if (ob->type == DT_OBSTACLE_ORIENTED_BOX) |
| 700 | { |
| 701 | dtMarkBoxArea(*bc.layer, tile->header->bmin, m_params.cs, m_params.ch, |
| 702 | ob->orientedBox.center, ob->orientedBox.halfExtents, ob->orientedBox.rotAux, 0); |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | // Build navmesh |
| 708 | status = dtBuildTileCacheRegions(m_talloc, *bc.layer, walkableClimbVx); |
| 709 | if (dtStatusFailed(status)) |
| 710 | return status; |
| 711 | |
| 712 | bc.lcset = dtAllocTileCacheContourSet(m_talloc); |
| 713 | if (!bc.lcset) |
| 714 | return DT_FAILURE | DT_OUT_OF_MEMORY; |
| 715 | status = dtBuildTileCacheContours(m_talloc, *bc.layer, walkableClimbVx, |
| 716 | m_params.maxSimplificationError, *bc.lcset); |
| 717 | if (dtStatusFailed(status)) |
| 718 | return status; |
| 719 | |
| 720 | bc.lmesh = dtAllocTileCachePolyMesh(m_talloc); |
| 721 | if (!bc.lmesh) |
| 722 | return DT_FAILURE | DT_OUT_OF_MEMORY; |
| 723 | status = dtBuildTileCachePolyMesh(m_talloc, *bc.lcset, *bc.lmesh); |
| 724 | if (dtStatusFailed(status)) |
| 725 | return status; |
| 726 | |
| 727 | // Early out if the mesh tile is empty. |
| 728 | if (!bc.lmesh->npolys) |
| 729 | { |
| 730 | // Remove existing tile. |
| 731 | navmesh->removeTile(navmesh->getTileRefAt(tile->header->tx,tile->header->ty,tile->header->tlayer),0,0); |
| 732 | return DT_SUCCESS; |
| 733 | } |
| 734 | |
| 735 | dtNavMeshCreateParams params; |
| 736 | memset(¶ms, 0, sizeof(params)); |
| 737 | params.verts = bc.lmesh->verts; |
| 738 | params.vertCount = bc.lmesh->nverts; |
| 739 | params.polys = bc.lmesh->polys; |
| 740 | params.polyAreas = bc.lmesh->areas; |
| 741 | params.polyFlags = bc.lmesh->flags; |
| 742 | params.polyCount = bc.lmesh->npolys; |
| 743 | params.nvp = DT_VERTS_PER_POLYGON; |
| 744 | params.walkableHeight = m_params.walkableHeight; |
| 745 | params.walkableRadius = m_params.walkableRadius; |
| 746 | params.walkableClimb = m_params.walkableClimb; |
| 747 | params.tileX = tile->header->tx; |
| 748 | params.tileY = tile->header->ty; |
| 749 | params.tileLayer = tile->header->tlayer; |
| 750 | params.cs = m_params.cs; |
| 751 | params.ch = m_params.ch; |
| 752 | params.buildBvTree = false; |
| 753 | dtVcopy(params.bmin, tile->header->bmin); |
| 754 | dtVcopy(params.bmax, tile->header->bmax); |
| 755 | |
| 756 | if (m_tmproc) |
| 757 | { |
| 758 | m_tmproc->process(¶ms, bc.lmesh->areas, bc.lmesh->flags); |
| 759 | } |
| 760 | |
| 761 | unsigned char* navData = 0; |
| 762 | int navDataSize = 0; |
| 763 | if (!dtCreateNavMeshData(¶ms, &navData, &navDataSize)) |
| 764 | return DT_FAILURE; |
| 765 | |
| 766 | // Remove existing tile. |
| 767 | navmesh->removeTile(navmesh->getTileRefAt(tile->header->tx,tile->header->ty,tile->header->tlayer),0,0); |
| 768 | |
| 769 | // Add new tile, or leave the location empty. |
| 770 | if (navData) |
| 771 | { |
| 772 | // Let the navmesh own the data. |
| 773 | status = navmesh->addTile(navData,navDataSize,DT_TILE_FREE_DATA,0,0); |
| 774 | if (dtStatusFailed(status)) |
| 775 | { |
| 776 | dtFree(navData); |
| 777 | return status; |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | return DT_SUCCESS; |
| 782 | } |
| 783 | |
| 784 | void dtTileCache::calcTightTileBounds(const dtTileCacheLayerHeader* header, float* bmin, float* bmax) const |
| 785 | { |
| 786 | const float cs = m_params.cs; |
| 787 | bmin[0] = header->bmin[0] + header->minx*cs; |
| 788 | bmin[1] = header->bmin[1]; |
| 789 | bmin[2] = header->bmin[2] + header->miny*cs; |
| 790 | bmax[0] = header->bmin[0] + (header->maxx+1)*cs; |
| 791 | bmax[1] = header->bmax[1]; |
| 792 | bmax[2] = header->bmin[2] + (header->maxy+1)*cs; |
| 793 | } |
| 794 | |
| 795 | void dtTileCache::getObstacleBounds(const struct dtTileCacheObstacle* ob, float* bmin, float* bmax) const |
| 796 | { |
| 797 | if (ob->type == DT_OBSTACLE_CYLINDER) |
| 798 | { |
| 799 | const dtObstacleCylinder &cl = ob->cylinder; |
| 800 | |
| 801 | bmin[0] = cl.pos[0] - cl.radius; |
| 802 | bmin[1] = cl.pos[1]; |
| 803 | bmin[2] = cl.pos[2] - cl.radius; |
| 804 | bmax[0] = cl.pos[0] + cl.radius; |
| 805 | bmax[1] = cl.pos[1] + cl.height; |
| 806 | bmax[2] = cl.pos[2] + cl.radius; |
| 807 | } |
| 808 | else if (ob->type == DT_OBSTACLE_BOX) |
| 809 | { |
| 810 | dtVcopy(bmin, ob->box.bmin); |
| 811 | dtVcopy(bmax, ob->box.bmax); |
| 812 | } |
| 813 | else if (ob->type == DT_OBSTACLE_ORIENTED_BOX) |
| 814 | { |
| 815 | const dtObstacleOrientedBox &orientedBox = ob->orientedBox; |
| 816 | |
| 817 | float maxr = 1.41f*dtMax(orientedBox.halfExtents[0], orientedBox.halfExtents[2]); |
| 818 | bmin[0] = orientedBox.center[0] - maxr; |
| 819 | bmax[0] = orientedBox.center[0] + maxr; |
| 820 | bmin[1] = orientedBox.center[1] - orientedBox.halfExtents[1]; |
| 821 | bmax[1] = orientedBox.center[1] + orientedBox.halfExtents[1]; |
| 822 | bmin[2] = orientedBox.center[2] - maxr; |
| 823 | bmax[2] = orientedBox.center[2] + maxr; |
| 824 | } |
| 825 | } |