Planeshift
DetourTileCache.h
Go to the documentation of this file.
1 #ifndef DETOURTILECACHE_H
2 #define DETOURTILECACHE_H
3 
4 #include "DetourStatus.h"
5 
6 
7 
8 typedef unsigned int dtObstacleRef;
9 
10 typedef unsigned int dtCompressedTileRef;
11 
14 {
16 };
17 
19 {
20  unsigned int salt;
22  unsigned char* compressed;
24  unsigned char* data;
25  int dataSize;
26  unsigned int flags;
28 };
29 
31 {
36 };
37 
38 static const int DT_MAX_TOUCHED_TILES = 8;
40 {
41  float pos[3], radius, height;
44  unsigned short salt;
45  unsigned char state;
46  unsigned char ntouched;
47  unsigned char npending;
49 };
50 
52 {
53  float orig[3];
54  float cs, ch;
55  int width, height;
60  int maxTiles;
62 };
63 
65 {
66  virtual void process(struct dtNavMeshCreateParams* params,
67  unsigned char* polyAreas, unsigned short* polyFlags) = 0;
68 };
69 
70 
72 {
73 public:
74  dtTileCache();
75  ~dtTileCache();
76 
77  struct dtTileCacheAlloc* getAlloc() { return m_talloc; }
78  struct dtTileCacheCompressor* getCompressor() { return m_tcomp; }
79  const dtTileCacheParams* getParams() const { return &m_params; }
80 
81  inline int getTileCount() const { return m_params.maxTiles; }
82  inline const dtCompressedTile* getTile(const int i) const { return &m_tiles[i]; }
83 
84  inline int getObstacleCount() const { return m_params.maxObstacles; }
85  inline const dtTileCacheObstacle* getObstacle(const int i) const { return &m_obstacles[i]; }
86 
87  const dtTileCacheObstacle* getObstacleByRef(dtObstacleRef ref);
88 
89  dtObstacleRef getObstacleRef(const dtTileCacheObstacle* obmin) const;
90 
91  dtStatus init(const dtTileCacheParams* params,
92  struct dtTileCacheAlloc* talloc,
93  struct dtTileCacheCompressor* tcomp,
94  struct dtTileCacheMeshProcess* tmproc);
95 
96  int getTilesAt(const int tx, const int ty, dtCompressedTileRef* tiles, const int maxTiles) const ;
97 
98  dtCompressedTile* getTileAt(const int tx, const int ty, const int tlayer);
99  dtCompressedTileRef getTileRef(const dtCompressedTile* tile) const;
100  const dtCompressedTile* getTileByRef(dtCompressedTileRef ref) const;
101 
102  dtStatus addTile(unsigned char* data, const int dataSize, unsigned char flags, dtCompressedTileRef* result);
103 
104  dtStatus removeTile(dtCompressedTileRef ref, unsigned char** data, int* dataSize);
105 
106  dtStatus addObstacle(const float* pos, const float radius, const float height, dtObstacleRef* result);
107  dtStatus removeObstacle(const dtObstacleRef ref);
108 
109  dtStatus queryTiles(const float* bmin, const float* bmax,
110  dtCompressedTileRef* results, int* resultCount, const int maxResults) const;
111 
112  dtStatus update(const float /*dt*/, class dtNavMesh* navmesh);
113 
114  dtStatus buildNavMeshTilesAt(const int tx, const int ty, class dtNavMesh* navmesh);
115 
116  dtStatus buildNavMeshTile(const dtCompressedTileRef ref, class dtNavMesh* navmesh);
117 
118  void calcTightTileBounds(const struct dtTileCacheLayerHeader* header, float* bmin, float* bmax) const;
119 
120  void getObstacleBounds(const struct dtTileCacheObstacle* ob, float* bmin, float* bmax) const;
121 
122 
124  inline dtCompressedTileRef encodeTileId(unsigned int salt, unsigned int it) const
125  {
126  return ((dtCompressedTileRef)salt << m_tileBits) | (dtCompressedTileRef)it;
127  }
128 
130  inline unsigned int decodeTileIdSalt(dtCompressedTileRef ref) const
131  {
132  const dtCompressedTileRef saltMask = ((dtCompressedTileRef)1<<m_saltBits)-1;
133  return (unsigned int)((ref >> m_tileBits) & saltMask);
134  }
135 
137  inline unsigned int decodeTileIdTile(dtCompressedTileRef ref) const
138  {
139  const dtCompressedTileRef tileMask = ((dtCompressedTileRef)1<<m_tileBits)-1;
140  return (unsigned int)(ref & tileMask);
141  }
142 
144  inline dtObstacleRef encodeObstacleId(unsigned int salt, unsigned int it) const
145  {
146  return ((dtObstacleRef)salt << 16) | (dtObstacleRef)it;
147  }
148 
150  inline unsigned int decodeObstacleIdSalt(dtObstacleRef ref) const
151  {
152  const dtObstacleRef saltMask = ((dtObstacleRef)1<<16)-1;
153  return (unsigned int)((ref >> 16) & saltMask);
154  }
155 
157  inline unsigned int decodeObstacleIdObstacle(dtObstacleRef ref) const
158  {
159  const dtObstacleRef tileMask = ((dtObstacleRef)1<<16)-1;
160  return (unsigned int)(ref & tileMask);
161  }
162 
163 
164 private:
165 
166  enum ObstacleRequestAction
167  {
168  REQUEST_ADD,
169  REQUEST_REMOVE,
170  };
171 
172  struct ObstacleRequest
173  {
174  int action;
175  dtObstacleRef ref;
176  };
177 
178  int m_tileLutSize;
179  int m_tileLutMask;
180 
181  dtCompressedTile** m_posLookup;
182  dtCompressedTile* m_nextFreeTile;
183  dtCompressedTile* m_tiles;
184 
185  unsigned int m_saltBits;
186  unsigned int m_tileBits;
187 
188  dtTileCacheParams m_params;
189 
190  dtTileCacheAlloc* m_talloc;
191  dtTileCacheCompressor* m_tcomp;
192  dtTileCacheMeshProcess* m_tmproc;
193 
194  dtTileCacheObstacle* m_obstacles;
195  dtTileCacheObstacle* m_nextFreeObstacle;
196 
197  static const int MAX_REQUESTS = 64;
198  ObstacleRequest m_reqs[MAX_REQUESTS];
199  int m_nreqs;
200 
201  static const int MAX_UPDATE = 64;
202  dtCompressedTileRef m_update[MAX_UPDATE];
203  int m_nupdate;
204 
205 };
206 
208 void dtFreeTileCache(dtTileCache* tc);
209 
210 #endif
dtObstacleRef encodeObstacleId(unsigned int salt, unsigned int it) const
Encodes an obstacle id.
int getObstacleCount() const
Represents the source data used to build an navigation mesh tile.
unsigned char * data
unsigned short salt
unsigned int dtStatus
Definition: DetourStatus.h:22
void dtFreeTileCache(dtTileCache *tc)
const dtCompressedTile * getTile(const int i) const
struct dtTileCacheCompressor * getCompressor()
unsigned int salt
Counter describing modifications to the tile.
unsigned int decodeObstacleIdObstacle(dtObstacleRef ref) const
Decodes an obstacle id.
A navigation mesh based on tiles of convex polygons.
struct dtTileCacheAlloc * getAlloc()
const dtTileCacheParams * getParams() const
Navmesh owns the tile memory and should free it.
unsigned int dtObstacleRef
dtCompressedTileFlags
Flags for addTile.
dtTileCacheObstacle * next
dtCompressedTileRef encodeTileId(unsigned int salt, unsigned int it) const
Encodes a tile id.
unsigned int decodeTileIdTile(dtCompressedTileRef ref) const
Decodes a tile id.
int getTileCount() const
ObstacleState
unsigned int decodeObstacleIdSalt(dtObstacleRef ref) const
Decodes an obstacle salt.
dtTileCache * dtAllocTileCache()
dtCompressedTile * next
static const int DT_MAX_TOUCHED_TILES
const dtTileCacheObstacle * getObstacle(const int i) const
unsigned char npending
unsigned char state
unsigned int flags
struct dtTileCacheLayerHeader * header
unsigned int decodeTileIdSalt(dtCompressedTileRef ref) const
Decodes a tile salt.
unsigned int dtCompressedTileRef
unsigned char * compressed
unsigned char ntouched