Planeshift

DetourTileCache.h

Go to the documentation of this file.
00001 #ifndef DETOURTILECACHE_H
00002 #define DETOURTILECACHE_H
00003 
00004 #include "DetourStatus.h"
00005 
00006 
00007 
00008 typedef unsigned int dtObstacleRef;
00009 
00010 typedef unsigned int dtCompressedTileRef;
00011 
00013 enum dtCompressedTileFlags
00014 {
00015         DT_COMPRESSEDTILE_FREE_DATA = 0x01,                                     
00016 };
00017 
00018 struct dtCompressedTile
00019 {
00020         unsigned int salt;                                              
00021         struct dtTileCacheLayerHeader* header;
00022         unsigned char* compressed;
00023         int compressedSize;
00024         unsigned char* data;
00025         int dataSize;
00026         unsigned int flags;
00027         dtCompressedTile* next;
00028 };
00029 
00030 enum ObstacleState
00031 {
00032         DT_OBSTACLE_EMPTY,
00033         DT_OBSTACLE_PROCESSING,
00034         DT_OBSTACLE_PROCESSED,
00035         DT_OBSTACLE_REMOVING,
00036 };
00037 
00038 static const int DT_MAX_TOUCHED_TILES = 8;
00039 struct dtTileCacheObstacle
00040 {
00041         float pos[3], radius, height;
00042         dtCompressedTileRef touched[DT_MAX_TOUCHED_TILES];
00043         dtCompressedTileRef pending[DT_MAX_TOUCHED_TILES];
00044         unsigned short salt;
00045         unsigned char state;
00046         unsigned char ntouched;
00047         unsigned char npending;
00048         dtTileCacheObstacle* next;
00049 };
00050 
00051 struct dtTileCacheParams
00052 {
00053         float orig[3];
00054         float cs, ch;
00055         int width, height;
00056         float walkableHeight;
00057         float walkableRadius;
00058         float walkableClimb;
00059         float maxSimplificationError;
00060         int maxTiles;
00061         int maxObstacles;
00062 };
00063 
00064 struct dtTileCacheMeshProcess
00065 {
00066         virtual void process(struct dtNavMeshCreateParams* params,
00067                                                  unsigned char* polyAreas, unsigned short* polyFlags) = 0;
00068 };
00069 
00070 
00071 class dtTileCache
00072 {
00073 public:
00074         dtTileCache();
00075         ~dtTileCache();
00076         
00077         struct dtTileCacheAlloc* getAlloc() { return m_talloc; }
00078         struct dtTileCacheCompressor* getCompressor() { return m_tcomp; }
00079         const dtTileCacheParams* getParams() const { return &m_params; }
00080         
00081         inline int getTileCount() const { return m_params.maxTiles; }
00082         inline const dtCompressedTile* getTile(const int i) const { return &m_tiles[i]; }
00083         
00084         inline int getObstacleCount() const { return m_params.maxObstacles; }
00085         inline const dtTileCacheObstacle* getObstacle(const int i) const { return &m_obstacles[i]; }
00086         
00087         const dtTileCacheObstacle* getObstacleByRef(dtObstacleRef ref);
00088         
00089         dtObstacleRef getObstacleRef(const dtTileCacheObstacle* obmin) const;
00090         
00091         dtStatus init(const dtTileCacheParams* params,
00092                                   struct dtTileCacheAlloc* talloc,
00093                                   struct dtTileCacheCompressor* tcomp,
00094                                   struct dtTileCacheMeshProcess* tmproc);
00095         
00096         int getTilesAt(const int tx, const int ty, dtCompressedTileRef* tiles, const int maxTiles) const ;
00097         
00098         dtCompressedTile* getTileAt(const int tx, const int ty, const int tlayer);
00099         dtCompressedTileRef getTileRef(const dtCompressedTile* tile) const;
00100         const dtCompressedTile* getTileByRef(dtCompressedTileRef ref) const;
00101         
00102         dtStatus addTile(unsigned char* data, const int dataSize, unsigned char flags, dtCompressedTileRef* result);
00103         
00104         dtStatus removeTile(dtCompressedTileRef ref, unsigned char** data, int* dataSize);
00105         
00106         dtStatus addObstacle(const float* pos, const float radius, const float height, dtObstacleRef* result);
00107         dtStatus removeObstacle(const dtObstacleRef ref);
00108         
00109         dtStatus queryTiles(const float* bmin, const float* bmax,
00110                                                 dtCompressedTileRef* results, int* resultCount, const int maxResults) const;
00111         
00112         dtStatus update(const float /*dt*/, class dtNavMesh* navmesh);
00113         
00114         dtStatus buildNavMeshTilesAt(const int tx, const int ty, class dtNavMesh* navmesh);
00115         
00116         dtStatus buildNavMeshTile(const dtCompressedTileRef ref, class dtNavMesh* navmesh);
00117         
00118         void calcTightTileBounds(const struct dtTileCacheLayerHeader* header, float* bmin, float* bmax) const;
00119         
00120         void getObstacleBounds(const struct dtTileCacheObstacle* ob, float* bmin, float* bmax) const;
00121         
00122 
00124         inline dtCompressedTileRef encodeTileId(unsigned int salt, unsigned int it) const
00125         {
00126                 return ((dtCompressedTileRef)salt << m_tileBits) | (dtCompressedTileRef)it;
00127         }
00128         
00130         inline unsigned int decodeTileIdSalt(dtCompressedTileRef ref) const
00131         {
00132                 const dtCompressedTileRef saltMask = ((dtCompressedTileRef)1<<m_saltBits)-1;
00133                 return (unsigned int)((ref >> m_tileBits) & saltMask);
00134         }
00135         
00137         inline unsigned int decodeTileIdTile(dtCompressedTileRef ref) const
00138         {
00139                 const dtCompressedTileRef tileMask = ((dtCompressedTileRef)1<<m_tileBits)-1;
00140                 return (unsigned int)(ref & tileMask);
00141         }
00142 
00144         inline dtObstacleRef encodeObstacleId(unsigned int salt, unsigned int it) const
00145         {
00146                 return ((dtObstacleRef)salt << 16) | (dtObstacleRef)it;
00147         }
00148         
00150         inline unsigned int decodeObstacleIdSalt(dtObstacleRef ref) const
00151         {
00152                 const dtObstacleRef saltMask = ((dtObstacleRef)1<<16)-1;
00153                 return (unsigned int)((ref >> 16) & saltMask);
00154         }
00155         
00157         inline unsigned int decodeObstacleIdObstacle(dtObstacleRef ref) const
00158         {
00159                 const dtObstacleRef tileMask = ((dtObstacleRef)1<<16)-1;
00160                 return (unsigned int)(ref & tileMask);
00161         }
00162         
00163         
00164 private:
00165         
00166         enum ObstacleRequestAction
00167         {
00168                 REQUEST_ADD,
00169                 REQUEST_REMOVE,
00170         };
00171         
00172         struct ObstacleRequest
00173         {
00174                 int action;
00175                 dtObstacleRef ref;
00176         };
00177         
00178         int m_tileLutSize;                                              
00179         int m_tileLutMask;                                              
00180         
00181         dtCompressedTile** m_posLookup;                 
00182         dtCompressedTile* m_nextFreeTile;               
00183         dtCompressedTile* m_tiles;                              
00184         
00185         unsigned int m_saltBits;                                
00186         unsigned int m_tileBits;                                
00187         
00188         dtTileCacheParams m_params;
00189         
00190         dtTileCacheAlloc* m_talloc;
00191         dtTileCacheCompressor* m_tcomp;
00192         dtTileCacheMeshProcess* m_tmproc;
00193         
00194         dtTileCacheObstacle* m_obstacles;
00195         dtTileCacheObstacle* m_nextFreeObstacle;
00196         
00197         static const int MAX_REQUESTS = 64;
00198         ObstacleRequest m_reqs[MAX_REQUESTS];
00199         int m_nreqs;
00200         
00201         static const int MAX_UPDATE = 64;
00202         dtCompressedTileRef m_update[MAX_UPDATE];
00203         int m_nupdate;
00204         
00205 };
00206 
00207 dtTileCache* dtAllocTileCache();
00208 void dtFreeTileCache(dtTileCache* tc);
00209 
00210 #endif