DetourTileCache.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. #include "DetourTileCache.h"
  2. #include "DetourTileCacheBuilder.h"
  3. #include "recast/Detour/DetourNavMeshBuilder.h"
  4. #include "recast/Detour/DetourNavMesh.h"
  5. #include "recast/Detour/DetourCommon.h"
  6. #include "recast/Detour/DetourMath.h"
  7. #include "recast/Detour/DetourAlloc.h"
  8. #include "recast/Detour/DetourAssert.h"
  9. #include <string.h>
  10. #include <new>
  11. dtTileCache* dtAllocTileCache()
  12. {
  13. void* mem = dtAlloc(sizeof(dtTileCache), DT_ALLOC_PERM);
  14. if (!mem) return 0;
  15. return new(mem) dtTileCache;
  16. }
  17. void dtFreeTileCache(dtTileCache* tc)
  18. {
  19. if (!tc) return;
  20. tc->~dtTileCache();
  21. dtFree(tc);
  22. }
  23. static bool contains(const dtCompressedTileRef* a, const int n, const dtCompressedTileRef v)
  24. {
  25. for (int i = 0; i < n; ++i)
  26. if (a[i] == v)
  27. return true;
  28. return false;
  29. }
  30. inline int computeTileHash(int x, int y, const int mask)
  31. {
  32. const unsigned int h1 = 0x8da6b343; // Large multiplicative constants;
  33. const unsigned int h2 = 0xd8163841; // here arbitrarily chosen primes
  34. unsigned int n = h1 * x + h2 * y;
  35. return (int)(n & mask);
  36. }
  37. struct BuildContext
  38. {
  39. inline BuildContext(struct dtTileCacheAlloc* a) : layer(0), lcset(0), lmesh(0), alloc(a) {}
  40. inline ~BuildContext() { purge(); }
  41. void purge()
  42. {
  43. dtFreeTileCacheLayer(alloc, layer);
  44. layer = 0;
  45. dtFreeTileCacheContourSet(alloc, lcset);
  46. lcset = 0;
  47. dtFreeTileCachePolyMesh(alloc, lmesh);
  48. lmesh = 0;
  49. }
  50. struct dtTileCacheLayer* layer;
  51. struct dtTileCacheContourSet* lcset;
  52. struct dtTileCachePolyMesh* lmesh;
  53. struct dtTileCacheAlloc* alloc;
  54. };
  55. dtTileCache::dtTileCache() :
  56. m_tileLutSize(0),
  57. m_tileLutMask(0),
  58. m_posLookup(0),
  59. m_nextFreeTile(0),
  60. m_tiles(0),
  61. m_saltBits(0),
  62. m_tileBits(0),
  63. m_talloc(0),
  64. m_tcomp(0),
  65. m_tmproc(0),
  66. m_obstacles(0),
  67. m_nextFreeObstacle(0),
  68. m_nreqs(0),
  69. m_nupdate(0)
  70. {
  71. memset(&m_params, 0, sizeof(m_params));
  72. }
  73. dtTileCache::~dtTileCache()
  74. {
  75. for (int i = 0; i < m_params.maxTiles; ++i)
  76. {
  77. if (m_tiles[i].flags & DT_COMPRESSEDTILE_FREE_DATA)
  78. {
  79. dtFree(m_tiles[i].data);
  80. m_tiles[i].data = 0;
  81. }
  82. }
  83. dtFree(m_obstacles);
  84. m_obstacles = 0;
  85. dtFree(m_posLookup);
  86. m_posLookup = 0;
  87. dtFree(m_tiles);
  88. m_tiles = 0;
  89. m_nreqs = 0;
  90. m_nupdate = 0;
  91. }
  92. const dtCompressedTile* dtTileCache::getTileByRef(dtCompressedTileRef ref) const
  93. {
  94. if (!ref)
  95. return 0;
  96. unsigned int tileIndex = decodeTileIdTile(ref);
  97. unsigned int tileSalt = decodeTileIdSalt(ref);
  98. if ((int)tileIndex >= m_params.maxTiles)
  99. return 0;
  100. const dtCompressedTile* tile = &m_tiles[tileIndex];
  101. if (tile->salt != tileSalt)
  102. return 0;
  103. return tile;
  104. }
  105. dtStatus dtTileCache::init(const dtTileCacheParams* params,
  106. dtTileCacheAlloc* talloc,
  107. dtTileCacheCompressor* tcomp,
  108. dtTileCacheMeshProcess* tmproc)
  109. {
  110. m_talloc = talloc;
  111. m_tcomp = tcomp;
  112. m_tmproc = tmproc;
  113. m_nreqs = 0;
  114. memcpy(&m_params, params, sizeof(m_params));
  115. // Alloc space for obstacles.
  116. m_obstacles = (dtTileCacheObstacle*)dtAlloc(sizeof(dtTileCacheObstacle)*m_params.maxObstacles, DT_ALLOC_PERM);
  117. if (!m_obstacles)
  118. return DT_FAILURE | DT_OUT_OF_MEMORY;
  119. memset(m_obstacles, 0, sizeof(dtTileCacheObstacle)*m_params.maxObstacles);
  120. m_nextFreeObstacle = 0;
  121. for (int i = m_params.maxObstacles-1; i >= 0; --i)
  122. {
  123. m_obstacles[i].salt = 1;
  124. m_obstacles[i].next = m_nextFreeObstacle;
  125. m_nextFreeObstacle = &m_obstacles[i];
  126. }
  127. // Init tiles
  128. m_tileLutSize = dtNextPow2(m_params.maxTiles/4);
  129. if (!m_tileLutSize) m_tileLutSize = 1;
  130. m_tileLutMask = m_tileLutSize-1;
  131. m_tiles = (dtCompressedTile*)dtAlloc(sizeof(dtCompressedTile)*m_params.maxTiles, DT_ALLOC_PERM);
  132. if (!m_tiles)
  133. return DT_FAILURE | DT_OUT_OF_MEMORY;
  134. m_posLookup = (dtCompressedTile**)dtAlloc(sizeof(dtCompressedTile*)*m_tileLutSize, DT_ALLOC_PERM);
  135. if (!m_posLookup)
  136. return DT_FAILURE | DT_OUT_OF_MEMORY;
  137. memset(m_tiles, 0, sizeof(dtCompressedTile)*m_params.maxTiles);
  138. memset(m_posLookup, 0, sizeof(dtCompressedTile*)*m_tileLutSize);
  139. m_nextFreeTile = 0;
  140. for (int i = m_params.maxTiles-1; i >= 0; --i)
  141. {
  142. m_tiles[i].salt = 1;
  143. m_tiles[i].next = m_nextFreeTile;
  144. m_nextFreeTile = &m_tiles[i];
  145. }
  146. // Init ID generator values.
  147. m_tileBits = dtIlog2(dtNextPow2((unsigned int)m_params.maxTiles));
  148. // Only allow 31 salt bits, since the salt mask is calculated using 32bit uint and it will overflow.
  149. m_saltBits = dtMin((unsigned int)31, 32 - m_tileBits);
  150. if (m_saltBits < 10)
  151. return DT_FAILURE | DT_INVALID_PARAM;
  152. return DT_SUCCESS;
  153. }
  154. int dtTileCache::getTilesAt(const int tx, const int ty, dtCompressedTileRef* tiles, const int maxTiles) const
  155. {
  156. int n = 0;
  157. // Find tile based on hash.
  158. int h = computeTileHash(tx,ty,m_tileLutMask);
  159. dtCompressedTile* tile = m_posLookup[h];
  160. while (tile)
  161. {
  162. if (tile->header &&
  163. tile->header->tx == tx &&
  164. tile->header->ty == ty)
  165. {
  166. if (n < maxTiles)
  167. tiles[n++] = getTileRef(tile);
  168. }
  169. tile = tile->next;
  170. }
  171. return n;
  172. }
  173. dtCompressedTile* dtTileCache::getTileAt(const int tx, const int ty, const int tlayer)
  174. {
  175. // Find tile based on hash.
  176. int h = computeTileHash(tx,ty,m_tileLutMask);
  177. dtCompressedTile* tile = m_posLookup[h];
  178. while (tile)
  179. {
  180. if (tile->header &&
  181. tile->header->tx == tx &&
  182. tile->header->ty == ty &&
  183. tile->header->tlayer == tlayer)
  184. {
  185. return tile;
  186. }
  187. tile = tile->next;
  188. }
  189. return 0;
  190. }
  191. dtCompressedTileRef dtTileCache::getTileRef(const dtCompressedTile* tile) const
  192. {
  193. if (!tile) return 0;
  194. const unsigned int it = (unsigned int)(tile - m_tiles);
  195. return (dtCompressedTileRef)encodeTileId(tile->salt, it);
  196. }
  197. dtObstacleRef dtTileCache::getObstacleRef(const dtTileCacheObstacle* ob) const
  198. {
  199. if (!ob) return 0;
  200. const unsigned int idx = (unsigned int)(ob - m_obstacles);
  201. return encodeObstacleId(ob->salt, idx);
  202. }
  203. const dtTileCacheObstacle* dtTileCache::getObstacleByRef(dtObstacleRef ref)
  204. {
  205. if (!ref)
  206. return 0;
  207. unsigned int idx = decodeObstacleIdObstacle(ref);
  208. if ((int)idx >= m_params.maxObstacles)
  209. return 0;
  210. const dtTileCacheObstacle* ob = &m_obstacles[idx];
  211. unsigned int salt = decodeObstacleIdSalt(ref);
  212. if (ob->salt != salt)
  213. return 0;
  214. return ob;
  215. }
  216. dtStatus dtTileCache::addTile(unsigned char* data, const int dataSize, unsigned char flags, dtCompressedTileRef* result)
  217. {
  218. // Make sure the data is in right format.
  219. dtTileCacheLayerHeader* header = (dtTileCacheLayerHeader*)data;
  220. if (header->magic != DT_TILECACHE_MAGIC)
  221. return DT_FAILURE | DT_WRONG_MAGIC;
  222. if (header->version != DT_TILECACHE_VERSION)
  223. return DT_FAILURE | DT_WRONG_VERSION;
  224. // Make sure the location is free.
  225. if (getTileAt(header->tx, header->ty, header->tlayer))
  226. return DT_FAILURE;
  227. // Allocate a tile.
  228. dtCompressedTile* tile = 0;
  229. if (m_nextFreeTile)
  230. {
  231. tile = m_nextFreeTile;
  232. m_nextFreeTile = tile->next;
  233. tile->next = 0;
  234. }
  235. // Make sure we could allocate a tile.
  236. if (!tile)
  237. return DT_FAILURE | DT_OUT_OF_MEMORY;
  238. // Insert tile into the position lut.
  239. int h = computeTileHash(header->tx, header->ty, m_tileLutMask);
  240. tile->next = m_posLookup[h];
  241. m_posLookup[h] = tile;
  242. // Init tile.
  243. const int headerSize = dtAlign4(sizeof(dtTileCacheLayerHeader));
  244. tile->header = (dtTileCacheLayerHeader*)data;
  245. tile->data = data;
  246. tile->dataSize = dataSize;
  247. tile->compressed = tile->data + headerSize;
  248. tile->compressedSize = tile->dataSize - headerSize;
  249. tile->flags = flags;
  250. if (result)
  251. *result = getTileRef(tile);
  252. return DT_SUCCESS;
  253. }
  254. dtStatus dtTileCache::removeTile(dtCompressedTileRef ref, unsigned char** data, int* dataSize)
  255. {
  256. if (!ref)
  257. return DT_FAILURE | DT_INVALID_PARAM;
  258. unsigned int tileIndex = decodeTileIdTile(ref);
  259. unsigned int tileSalt = decodeTileIdSalt(ref);
  260. if ((int)tileIndex >= m_params.maxTiles)
  261. return DT_FAILURE | DT_INVALID_PARAM;
  262. dtCompressedTile* tile = &m_tiles[tileIndex];
  263. if (tile->salt != tileSalt)
  264. return DT_FAILURE | DT_INVALID_PARAM;
  265. // Remove tile from hash lookup.
  266. const int h = computeTileHash(tile->header->tx,tile->header->ty,m_tileLutMask);
  267. dtCompressedTile* prev = 0;
  268. dtCompressedTile* cur = m_posLookup[h];
  269. while (cur)
  270. {
  271. if (cur == tile)
  272. {
  273. if (prev)
  274. prev->next = cur->next;
  275. else
  276. m_posLookup[h] = cur->next;
  277. break;
  278. }
  279. prev = cur;
  280. cur = cur->next;
  281. }
  282. // Reset tile.
  283. if (tile->flags & DT_COMPRESSEDTILE_FREE_DATA)
  284. {
  285. // Owns data
  286. dtFree(tile->data);
  287. tile->data = 0;
  288. tile->dataSize = 0;
  289. if (data) *data = 0;
  290. if (dataSize) *dataSize = 0;
  291. }
  292. else
  293. {
  294. if (data) *data = tile->data;
  295. if (dataSize) *dataSize = tile->dataSize;
  296. }
  297. tile->header = 0;
  298. tile->data = 0;
  299. tile->dataSize = 0;
  300. tile->compressed = 0;
  301. tile->compressedSize = 0;
  302. tile->flags = 0;
  303. // Update salt, salt should never be zero.
  304. tile->salt = (tile->salt+1) & ((1<<m_saltBits)-1);
  305. if (tile->salt == 0)
  306. tile->salt++;
  307. // Add to free list.
  308. tile->next = m_nextFreeTile;
  309. m_nextFreeTile = tile;
  310. return DT_SUCCESS;
  311. }
  312. dtObstacleRef dtTileCache::addObstacle(const float* pos, const float radius, const float height, dtObstacleRef* result)
  313. {
  314. if (m_nreqs >= MAX_REQUESTS)
  315. return DT_FAILURE | DT_BUFFER_TOO_SMALL;
  316. dtTileCacheObstacle* ob = 0;
  317. if (m_nextFreeObstacle)
  318. {
  319. ob = m_nextFreeObstacle;
  320. m_nextFreeObstacle = ob->next;
  321. ob->next = 0;
  322. }
  323. if (!ob)
  324. return DT_FAILURE | DT_OUT_OF_MEMORY;
  325. unsigned short salt = ob->salt;
  326. memset(ob, 0, sizeof(dtTileCacheObstacle));
  327. ob->salt = salt;
  328. ob->state = DT_OBSTACLE_PROCESSING;
  329. dtVcopy(ob->pos, pos);
  330. ob->radius = radius;
  331. ob->height = height;
  332. ObstacleRequest* req = &m_reqs[m_nreqs++];
  333. memset(req, 0, sizeof(ObstacleRequest));
  334. req->action = REQUEST_ADD;
  335. req->ref = getObstacleRef(ob);
  336. if (result)
  337. *result = req->ref;
  338. return DT_SUCCESS;
  339. }
  340. dtObstacleRef dtTileCache::removeObstacle(const dtObstacleRef ref)
  341. {
  342. if (!ref)
  343. return DT_SUCCESS;
  344. if (m_nreqs >= MAX_REQUESTS)
  345. return DT_FAILURE | DT_BUFFER_TOO_SMALL;
  346. ObstacleRequest* req = &m_reqs[m_nreqs++];
  347. memset(req, 0, sizeof(ObstacleRequest));
  348. req->action = REQUEST_REMOVE;
  349. req->ref = ref;
  350. return DT_SUCCESS;
  351. }
  352. dtStatus dtTileCache::queryTiles(const float* bmin, const float* bmax,
  353. dtCompressedTileRef* results, int* resultCount, const int maxResults) const
  354. {
  355. const int MAX_TILES = 32;
  356. dtCompressedTileRef tiles[MAX_TILES];
  357. int n = 0;
  358. const float tw = m_params.width * m_params.cs;
  359. const float th = m_params.height * m_params.cs;
  360. const int tx0 = (int)dtMathFloorf((bmin[0]-m_params.orig[0]) / tw);
  361. const int tx1 = (int)dtMathFloorf((bmax[0]-m_params.orig[0]) / tw);
  362. const int ty0 = (int)dtMathFloorf((bmin[2]-m_params.orig[2]) / th);
  363. const int ty1 = (int)dtMathFloorf((bmax[2]-m_params.orig[2]) / th);
  364. for (int ty = ty0; ty <= ty1; ++ty)
  365. {
  366. for (int tx = tx0; tx <= tx1; ++tx)
  367. {
  368. const int ntiles = getTilesAt(tx,ty,tiles,MAX_TILES);
  369. for (int i = 0; i < ntiles; ++i)
  370. {
  371. const dtCompressedTile* tile = &m_tiles[decodeTileIdTile(tiles[i])];
  372. float tbmin[3], tbmax[3];
  373. calcTightTileBounds(tile->header, tbmin, tbmax);
  374. if (dtOverlapBounds(bmin,bmax, tbmin,tbmax))
  375. {
  376. if (n < maxResults)
  377. results[n++] = tiles[i];
  378. }
  379. }
  380. }
  381. }
  382. *resultCount = n;
  383. return DT_SUCCESS;
  384. }
  385. dtStatus dtTileCache::update(const float /*dt*/, dtNavMesh* navmesh)
  386. {
  387. if (m_nupdate == 0)
  388. {
  389. // Process requests.
  390. for (int i = 0; i < m_nreqs; ++i)
  391. {
  392. ObstacleRequest* req = &m_reqs[i];
  393. unsigned int idx = decodeObstacleIdObstacle(req->ref);
  394. if ((int)idx >= m_params.maxObstacles)
  395. continue;
  396. dtTileCacheObstacle* ob = &m_obstacles[idx];
  397. unsigned int salt = decodeObstacleIdSalt(req->ref);
  398. if (ob->salt != salt)
  399. continue;
  400. if (req->action == REQUEST_ADD)
  401. {
  402. // Find touched tiles.
  403. float bmin[3], bmax[3];
  404. getObstacleBounds(ob, bmin, bmax);
  405. int ntouched = 0;
  406. queryTiles(bmin, bmax, ob->touched, &ntouched, DT_MAX_TOUCHED_TILES);
  407. ob->ntouched = (unsigned char)ntouched;
  408. // Add tiles to update list.
  409. ob->npending = 0;
  410. for (int j = 0; j < ob->ntouched; ++j)
  411. {
  412. if (m_nupdate < MAX_UPDATE)
  413. {
  414. if (!contains(m_update, m_nupdate, ob->touched[j]))
  415. m_update[m_nupdate++] = ob->touched[j];
  416. ob->pending[ob->npending++] = ob->touched[j];
  417. }
  418. }
  419. }
  420. else if (req->action == REQUEST_REMOVE)
  421. {
  422. // Prepare to remove obstacle.
  423. ob->state = DT_OBSTACLE_REMOVING;
  424. // Add tiles to update list.
  425. ob->npending = 0;
  426. for (int j = 0; j < ob->ntouched; ++j)
  427. {
  428. if (m_nupdate < MAX_UPDATE)
  429. {
  430. if (!contains(m_update, m_nupdate, ob->touched[j]))
  431. m_update[m_nupdate++] = ob->touched[j];
  432. ob->pending[ob->npending++] = ob->touched[j];
  433. }
  434. }
  435. }
  436. }
  437. m_nreqs = 0;
  438. }
  439. // Process updates
  440. if (m_nupdate)
  441. {
  442. // Build mesh
  443. const dtCompressedTileRef ref = m_update[0];
  444. dtStatus status = buildNavMeshTile(ref, navmesh);
  445. m_nupdate--;
  446. if (m_nupdate > 0)
  447. memmove(m_update, m_update+1, m_nupdate*sizeof(dtCompressedTileRef));
  448. // Update obstacle states.
  449. for (int i = 0; i < m_params.maxObstacles; ++i)
  450. {
  451. dtTileCacheObstacle* ob = &m_obstacles[i];
  452. if (ob->state == DT_OBSTACLE_PROCESSING || ob->state == DT_OBSTACLE_REMOVING)
  453. {
  454. // Remove handled tile from pending list.
  455. for (int j = 0; j < (int)ob->npending; j++)
  456. {
  457. if (ob->pending[j] == ref)
  458. {
  459. ob->pending[j] = ob->pending[(int)ob->npending-1];
  460. ob->npending--;
  461. break;
  462. }
  463. }
  464. // If all pending tiles processed, change state.
  465. if (ob->npending == 0)
  466. {
  467. if (ob->state == DT_OBSTACLE_PROCESSING)
  468. {
  469. ob->state = DT_OBSTACLE_PROCESSED;
  470. }
  471. else if (ob->state == DT_OBSTACLE_REMOVING)
  472. {
  473. ob->state = DT_OBSTACLE_EMPTY;
  474. // Update salt, salt should never be zero.
  475. ob->salt = (ob->salt+1) & ((1<<16)-1);
  476. if (ob->salt == 0)
  477. ob->salt++;
  478. // Return obstacle to free list.
  479. ob->next = m_nextFreeObstacle;
  480. m_nextFreeObstacle = ob;
  481. }
  482. }
  483. }
  484. }
  485. if (dtStatusFailed(status))
  486. return status;
  487. }
  488. return DT_SUCCESS;
  489. }
  490. dtStatus dtTileCache::buildNavMeshTilesAt(const int tx, const int ty, dtNavMesh* navmesh)
  491. {
  492. const int MAX_TILES = 32;
  493. dtCompressedTileRef tiles[MAX_TILES];
  494. const int ntiles = getTilesAt(tx,ty,tiles,MAX_TILES);
  495. for (int i = 0; i < ntiles; ++i)
  496. {
  497. dtStatus status = buildNavMeshTile(tiles[i], navmesh);
  498. if (dtStatusFailed(status))
  499. return status;
  500. }
  501. return DT_SUCCESS;
  502. }
  503. dtStatus dtTileCache::buildNavMeshTile(const dtCompressedTileRef ref, dtNavMesh* navmesh)
  504. {
  505. dtAssert(m_talloc);
  506. dtAssert(m_tcomp);
  507. unsigned int idx = decodeTileIdTile(ref);
  508. if (idx > (unsigned int)m_params.maxTiles)
  509. return DT_FAILURE | DT_INVALID_PARAM;
  510. const dtCompressedTile* tile = &m_tiles[idx];
  511. unsigned int salt = decodeTileIdSalt(ref);
  512. if (tile->salt != salt)
  513. return DT_FAILURE | DT_INVALID_PARAM;
  514. m_talloc->reset();
  515. BuildContext bc(m_talloc);
  516. const int walkableClimbVx = (int)(m_params.walkableClimb / m_params.ch);
  517. dtStatus status;
  518. // Decompress tile layer data.
  519. status = dtDecompressTileCacheLayer(m_talloc, m_tcomp, tile->data, tile->dataSize, &bc.layer);
  520. if (dtStatusFailed(status))
  521. return status;
  522. // Rasterize obstacles.
  523. for (int i = 0; i < m_params.maxObstacles; ++i)
  524. {
  525. const dtTileCacheObstacle* ob = &m_obstacles[i];
  526. if (ob->state == DT_OBSTACLE_EMPTY || ob->state == DT_OBSTACLE_REMOVING)
  527. continue;
  528. if (contains(ob->touched, ob->ntouched, ref))
  529. {
  530. dtMarkCylinderArea(*bc.layer, tile->header->bmin, m_params.cs, m_params.ch,
  531. ob->pos, ob->radius, ob->height, 0);
  532. }
  533. }
  534. // Build navmesh
  535. status = dtBuildTileCacheRegions(m_talloc, *bc.layer, walkableClimbVx);
  536. if (dtStatusFailed(status))
  537. return status;
  538. bc.lcset = dtAllocTileCacheContourSet(m_talloc);
  539. if (!bc.lcset)
  540. return status;
  541. status = dtBuildTileCacheContours(m_talloc, *bc.layer, walkableClimbVx,
  542. m_params.maxSimplificationError, *bc.lcset);
  543. if (dtStatusFailed(status))
  544. return status;
  545. bc.lmesh = dtAllocTileCachePolyMesh(m_talloc);
  546. if (!bc.lmesh)
  547. return status;
  548. status = dtBuildTileCachePolyMesh(m_talloc, *bc.lcset, *bc.lmesh);
  549. if (dtStatusFailed(status))
  550. return status;
  551. // Early out if the mesh tile is empty.
  552. if (!bc.lmesh->npolys)
  553. return DT_SUCCESS;
  554. dtNavMeshCreateParams params;
  555. memset(&params, 0, sizeof(params));
  556. params.verts = bc.lmesh->verts;
  557. params.vertCount = bc.lmesh->nverts;
  558. params.polys = bc.lmesh->polys;
  559. params.polyAreas = bc.lmesh->areas;
  560. params.polyFlags = bc.lmesh->flags;
  561. params.polyCount = bc.lmesh->npolys;
  562. params.nvp = DT_VERTS_PER_POLYGON;
  563. params.walkableHeight = m_params.walkableHeight;
  564. params.walkableRadius = m_params.walkableRadius;
  565. params.walkableClimb = m_params.walkableClimb;
  566. params.tileX = tile->header->tx;
  567. params.tileY = tile->header->ty;
  568. params.tileLayer = tile->header->tlayer;
  569. params.cs = m_params.cs;
  570. params.ch = m_params.ch;
  571. params.buildBvTree = false;
  572. dtVcopy(params.bmin, tile->header->bmin);
  573. dtVcopy(params.bmax, tile->header->bmax);
  574. if (m_tmproc)
  575. {
  576. m_tmproc->process(&params, bc.lmesh->areas, bc.lmesh->flags);
  577. }
  578. unsigned char* navData = 0;
  579. int navDataSize = 0;
  580. if (!dtCreateNavMeshData(&params, &navData, &navDataSize))
  581. return DT_FAILURE;
  582. // Remove existing tile.
  583. navmesh->removeTile(navmesh->getTileRefAt(tile->header->tx,tile->header->ty,tile->header->tlayer),0,0);
  584. // Add new tile, or leave the location empty.
  585. if (navData)
  586. {
  587. // Let the navmesh own the data.
  588. status = navmesh->addTile(navData,navDataSize,DT_TILE_FREE_DATA,0,0);
  589. if (dtStatusFailed(status))
  590. {
  591. dtFree(navData);
  592. return status;
  593. }
  594. }
  595. return DT_SUCCESS;
  596. }
  597. void dtTileCache::calcTightTileBounds(const dtTileCacheLayerHeader* header, float* bmin, float* bmax) const
  598. {
  599. const float cs = m_params.cs;
  600. bmin[0] = header->bmin[0] + header->minx*cs;
  601. bmin[1] = header->bmin[1];
  602. bmin[2] = header->bmin[2] + header->miny*cs;
  603. bmax[0] = header->bmin[0] + (header->maxx+1)*cs;
  604. bmax[1] = header->bmax[1];
  605. bmax[2] = header->bmin[2] + (header->maxy+1)*cs;
  606. }
  607. void dtTileCache::getObstacleBounds(const struct dtTileCacheObstacle* ob, float* bmin, float* bmax) const
  608. {
  609. bmin[0] = ob->pos[0] - ob->radius;
  610. bmin[1] = ob->pos[1];
  611. bmin[2] = ob->pos[2] - ob->radius;
  612. bmax[0] = ob->pos[0] + ob->radius;
  613. bmax[1] = ob->pos[1] + ob->height;
  614. bmax[2] = ob->pos[2] + ob->radius;
  615. }