DetourTileCache.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #ifndef DETOURTILECACHE_H
  2. #define DETOURTILECACHE_H
  3. #include "DetourStatus.h"
  4. typedef unsigned int dtObstacleRef;
  5. typedef unsigned int dtCompressedTileRef;
  6. /// Flags for addTile
  7. enum dtCompressedTileFlags
  8. {
  9. DT_COMPRESSEDTILE_FREE_DATA = 0x01, ///< Navmesh owns the tile memory and should free it.
  10. };
  11. struct dtCompressedTile
  12. {
  13. unsigned int salt; ///< Counter describing modifications to the tile.
  14. struct dtTileCacheLayerHeader* header;
  15. unsigned char* compressed;
  16. int compressedSize;
  17. unsigned char* data;
  18. int dataSize;
  19. unsigned int flags;
  20. dtCompressedTile* next;
  21. };
  22. enum ObstacleState
  23. {
  24. DT_OBSTACLE_EMPTY,
  25. DT_OBSTACLE_PROCESSING,
  26. DT_OBSTACLE_PROCESSED,
  27. DT_OBSTACLE_REMOVING,
  28. };
  29. enum ObstacleType
  30. {
  31. DT_OBSTACLE_CYLINDER,
  32. DT_OBSTACLE_BOX, // AABB
  33. DT_OBSTACLE_ORIENTED_BOX, // OBB
  34. };
  35. struct dtObstacleCylinder
  36. {
  37. float pos[ 3 ];
  38. float radius;
  39. float height;
  40. };
  41. struct dtObstacleBox
  42. {
  43. float bmin[ 3 ];
  44. float bmax[ 3 ];
  45. };
  46. struct dtObstacleOrientedBox
  47. {
  48. float center[ 3 ];
  49. float halfExtents[ 3 ];
  50. float rotAux[ 2 ]; //{ cos(0.5f*angle)*sin(-0.5f*angle); cos(0.5f*angle)*cos(0.5f*angle) - 0.5 }
  51. };
  52. static const int DT_MAX_TOUCHED_TILES = 8;
  53. struct dtTileCacheObstacle
  54. {
  55. union
  56. {
  57. dtObstacleCylinder cylinder;
  58. dtObstacleBox box;
  59. dtObstacleOrientedBox orientedBox;
  60. };
  61. dtCompressedTileRef touched[DT_MAX_TOUCHED_TILES];
  62. dtCompressedTileRef pending[DT_MAX_TOUCHED_TILES];
  63. unsigned short salt;
  64. unsigned char type;
  65. unsigned char state;
  66. unsigned char ntouched;
  67. unsigned char npending;
  68. dtTileCacheObstacle* next;
  69. };
  70. struct dtTileCacheParams
  71. {
  72. float orig[3];
  73. float cs, ch;
  74. int width, height;
  75. float walkableHeight;
  76. float walkableRadius;
  77. float walkableClimb;
  78. float maxSimplificationError;
  79. int maxTiles;
  80. int maxObstacles;
  81. };
  82. struct dtTileCacheMeshProcess
  83. {
  84. virtual ~dtTileCacheMeshProcess() { }
  85. virtual void process(struct dtNavMeshCreateParams* params,
  86. unsigned char* polyAreas, unsigned short* polyFlags) = 0;
  87. };
  88. class dtTileCache
  89. {
  90. public:
  91. dtTileCache();
  92. ~dtTileCache();
  93. struct dtTileCacheAlloc* getAlloc() { return m_talloc; }
  94. struct dtTileCacheCompressor* getCompressor() { return m_tcomp; }
  95. const dtTileCacheParams* getParams() const { return &m_params; }
  96. inline int getTileCount() const { return m_params.maxTiles; }
  97. inline const dtCompressedTile* getTile(const int i) const { return &m_tiles[i]; }
  98. inline int getObstacleCount() const { return m_params.maxObstacles; }
  99. inline const dtTileCacheObstacle* getObstacle(const int i) const { return &m_obstacles[i]; }
  100. const dtTileCacheObstacle* getObstacleByRef(dtObstacleRef ref);
  101. dtObstacleRef getObstacleRef(const dtTileCacheObstacle* obmin) const;
  102. dtStatus init(const dtTileCacheParams* params,
  103. struct dtTileCacheAlloc* talloc,
  104. struct dtTileCacheCompressor* tcomp,
  105. struct dtTileCacheMeshProcess* tmproc);
  106. int getTilesAt(const int tx, const int ty, dtCompressedTileRef* tiles, const int maxTiles) const ;
  107. dtCompressedTile* getTileAt(const int tx, const int ty, const int tlayer);
  108. dtCompressedTileRef getTileRef(const dtCompressedTile* tile) const;
  109. const dtCompressedTile* getTileByRef(dtCompressedTileRef ref) const;
  110. dtStatus addTile(unsigned char* data, const int dataSize, unsigned char flags, dtCompressedTileRef* result);
  111. dtStatus removeTile(dtCompressedTileRef ref, unsigned char** data, int* dataSize);
  112. // Cylinder obstacle.
  113. dtStatus addObstacle(const float* pos, const float radius, const float height, dtObstacleRef* result);
  114. // Aabb obstacle.
  115. dtStatus addBoxObstacle(const float* bmin, const float* bmax, dtObstacleRef* result);
  116. // Box obstacle: can be rotated in Y.
  117. dtStatus addBoxObstacle(const float* center, const float* halfExtents, const float yRadians, dtObstacleRef* result);
  118. dtStatus removeObstacle(const dtObstacleRef ref);
  119. dtStatus queryTiles(const float* bmin, const float* bmax,
  120. dtCompressedTileRef* results, int* resultCount, const int maxResults) const;
  121. /// Updates the tile cache by rebuilding tiles touched by unfinished obstacle requests.
  122. /// @param[in] dt The time step size. Currently not used.
  123. /// @param[in] navmesh The mesh to affect when rebuilding tiles.
  124. /// @param[out] upToDate Whether the tile cache is fully up to date with obstacle requests and tile rebuilds.
  125. /// If the tile cache is up to date another (immediate) call to update will have no effect;
  126. /// otherwise another call will continue processing obstacle requests and tile rebuilds.
  127. dtStatus update(const float dt, class dtNavMesh* navmesh, bool* upToDate = 0);
  128. dtStatus buildNavMeshTilesAt(const int tx, const int ty, class dtNavMesh* navmesh);
  129. dtStatus buildNavMeshTile(const dtCompressedTileRef ref, class dtNavMesh* navmesh);
  130. void calcTightTileBounds(const struct dtTileCacheLayerHeader* header, float* bmin, float* bmax) const;
  131. void getObstacleBounds(const struct dtTileCacheObstacle* ob, float* bmin, float* bmax) const;
  132. /// Encodes a tile id.
  133. inline dtCompressedTileRef encodeTileId(unsigned int salt, unsigned int it) const
  134. {
  135. return ((dtCompressedTileRef)salt << m_tileBits) | (dtCompressedTileRef)it;
  136. }
  137. /// Decodes a tile salt.
  138. inline unsigned int decodeTileIdSalt(dtCompressedTileRef ref) const
  139. {
  140. const dtCompressedTileRef saltMask = ((dtCompressedTileRef)1<<m_saltBits)-1;
  141. return (unsigned int)((ref >> m_tileBits) & saltMask);
  142. }
  143. /// Decodes a tile id.
  144. inline unsigned int decodeTileIdTile(dtCompressedTileRef ref) const
  145. {
  146. const dtCompressedTileRef tileMask = ((dtCompressedTileRef)1<<m_tileBits)-1;
  147. return (unsigned int)(ref & tileMask);
  148. }
  149. /// Encodes an obstacle id.
  150. inline dtObstacleRef encodeObstacleId(unsigned int salt, unsigned int it) const
  151. {
  152. return ((dtObstacleRef)salt << 16) | (dtObstacleRef)it;
  153. }
  154. /// Decodes an obstacle salt.
  155. inline unsigned int decodeObstacleIdSalt(dtObstacleRef ref) const
  156. {
  157. const dtObstacleRef saltMask = ((dtObstacleRef)1<<16)-1;
  158. return (unsigned int)((ref >> 16) & saltMask);
  159. }
  160. /// Decodes an obstacle id.
  161. inline unsigned int decodeObstacleIdObstacle(dtObstacleRef ref) const
  162. {
  163. const dtObstacleRef tileMask = ((dtObstacleRef)1<<16)-1;
  164. return (unsigned int)(ref & tileMask);
  165. }
  166. private:
  167. // Explicitly disabled copy constructor and copy assignment operator.
  168. dtTileCache(const dtTileCache&);
  169. dtTileCache& operator=(const dtTileCache&);
  170. enum ObstacleRequestAction
  171. {
  172. REQUEST_ADD,
  173. REQUEST_REMOVE,
  174. };
  175. struct ObstacleRequest
  176. {
  177. int action;
  178. dtObstacleRef ref;
  179. };
  180. int m_tileLutSize; ///< Tile hash lookup size (must be pot).
  181. int m_tileLutMask; ///< Tile hash lookup mask.
  182. dtCompressedTile** m_posLookup; ///< Tile hash lookup.
  183. dtCompressedTile* m_nextFreeTile; ///< Freelist of tiles.
  184. dtCompressedTile* m_tiles; ///< List of tiles.
  185. unsigned int m_saltBits; ///< Number of salt bits in the tile ID.
  186. unsigned int m_tileBits; ///< Number of tile bits in the tile ID.
  187. dtTileCacheParams m_params;
  188. dtTileCacheAlloc* m_talloc;
  189. dtTileCacheCompressor* m_tcomp;
  190. dtTileCacheMeshProcess* m_tmproc;
  191. dtTileCacheObstacle* m_obstacles;
  192. dtTileCacheObstacle* m_nextFreeObstacle;
  193. static const int MAX_REQUESTS = 64;
  194. ObstacleRequest m_reqs[MAX_REQUESTS];
  195. int m_nreqs;
  196. static const int MAX_UPDATE = 64;
  197. dtCompressedTileRef m_update[MAX_UPDATE];
  198. int m_nupdate;
  199. };
  200. dtTileCache* dtAllocTileCache();
  201. void dtFreeTileCache(dtTileCache* tc);
  202. #endif