DetourTileCache.cpp 18 KB

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