2 Debian patches touch this file
| 1 | // |
| 2 | // Copyright (c) 2009-2010 Mikko Mononen [email protected] |
| 3 | // |
| 4 | // This software is provided 'as-is', without any express or implied |
| 5 | // warranty. In no event will the authors be held liable for any damages |
| 6 | // arising from the use of this software. |
| 7 | // Permission is granted to anyone to use this software for any purpose, |
| 8 | // including commercial applications, and to alter it and redistribute it |
| 9 | // freely, subject to the following restrictions: |
| 10 | // 1. The origin of this software must not be misrepresented; you must not |
| 11 | // claim that you wrote the original software. If you use this software |
| 12 | // in a product, an acknowledgment in the product documentation would be |
| 13 | // appreciated but is not required. |
| 14 | // 2. Altered source versions must be plainly marked as such, and must not be |
| 15 | // misrepresented as being the original software. |
| 16 | // 3. This notice may not be removed or altered from any source distribution. |
| 17 | // |
| 18 | |
| 19 | #ifndef DETOURNAVMESH_H |
| 20 | #define DETOURNAVMESH_H |
| 21 | |
| 22 | #include "DetourAlloc.h" |
| 23 | #include "DetourStatus.h" |
| 24 | |
| •25 | // Undefine (or define in a build config) the following line to use 64bit polyref. |
| 26 | // Generally not needed, useful for very large worlds. |
| 27 | // Note: tiles build using 32bit refs are not compatible with 64bit refs! |
| 28 | //#define DT_POLYREF64 1 |
| 29 | |
| 30 | #ifdef DT_POLYREF64 |
| 31 | // TODO: figure out a multiplatform version of uint64_t |
| 32 | // - maybe: https://code.google.com/p/msinttypes/ |
| 33 | // - or: http://www.azillionmonkeys.com/qed/pstdint.h |
| 34 | #include <stdint.h> |
| 35 | #endif |
| 36 | |
| 37 | // Note: If you want to use 64-bit refs, change the types of both dtPolyRef & dtTileRef. |
| 38 | // It is also recommended that you change dtHashRef() to a proper 64-bit hash. |
| 39 | |
| 40 | /// A handle to a polygon within a navigation mesh tile. |
| 41 | /// @ingroup detour |
| 42 | #ifdef DT_POLYREF64 |
| 43 | static const unsigned int DT_SALT_BITS = 16; |
| 44 | static const unsigned int DT_TILE_BITS = 28; |
| 45 | static const unsigned int DT_POLY_BITS = 20; |
| 46 | typedef uint64_t dtPolyRef; |
| 47 | #else |
| 48 | typedef unsigned int dtPolyRef; |
| 49 | #endif |
| 50 | |
| 51 | /// A handle to a tile within a navigation mesh. |
| 52 | /// @ingroup detour |
| 53 | #ifdef DT_POLYREF64 |
| 54 | typedef uint64_t dtTileRef; |
| 55 | #else |
| 56 | typedef unsigned int dtTileRef; |
| 57 | #endif |
| 58 | |
| 59 | /// The maximum number of vertices per navigation polygon. |
| 60 | /// @ingroup detour |
| 61 | static const int DT_VERTS_PER_POLYGON = 6; |
| 62 | |
| 63 | /// @{ |
| 64 | /// @name Tile Serialization Constants |
| 65 | /// These constants are used to detect whether a navigation tile's data |
| 66 | /// and state format is compatible with the current build. |
| 67 | /// |
| 68 | |
| 69 | /// A magic number used to detect compatibility of navigation tile data. |
| 70 | static const int DT_NAVMESH_MAGIC = 'D'<<24 | 'N'<<16 | 'A'<<8 | 'V'; |
| 71 | |
| 72 | /// A version number used to detect compatibility of navigation tile data. |
| 73 | static const int DT_NAVMESH_VERSION = 7; |
| 74 | |
| 75 | /// A magic number used to detect the compatibility of navigation tile states. |
| 76 | static const int DT_NAVMESH_STATE_MAGIC = 'D'<<24 | 'N'<<16 | 'M'<<8 | 'S'; |
| 77 | |
| 78 | /// A version number used to detect compatibility of navigation tile states. |
| 79 | static const int DT_NAVMESH_STATE_VERSION = 1; |
| 80 | |
| 81 | /// @} |
| 82 | |
| 83 | /// A flag that indicates that an entity links to an external entity. |
| 84 | /// (E.g. A polygon edge is a portal that links to another polygon.) |
| 85 | static const unsigned short DT_EXT_LINK = 0x8000; |
| 86 | |
| 87 | /// A value that indicates the entity does not link to anything. |
| 88 | static const unsigned int DT_NULL_LINK = 0xffffffff; |
| 89 | |
| 90 | /// A flag that indicates that an off-mesh connection can be traversed in both directions. (Is bidirectional.) |
| 91 | static const unsigned int DT_OFFMESH_CON_BIDIR = 1; |
| 92 | |
| 93 | /// The maximum number of user defined area ids. |
| 94 | /// @ingroup detour |
| 95 | static const int DT_MAX_AREAS = 64; |
| 96 | |
| 97 | /// Tile flags used for various functions and fields. |
| 98 | /// For an example, see dtNavMesh::addTile(). |
| 99 | enum dtTileFlags |
| 100 | { |
| 101 | /// The navigation mesh owns the tile memory and is responsible for freeing it. |
| 102 | DT_TILE_FREE_DATA = 0x01 |
| 103 | }; |
| 104 | |
| 105 | /// Vertex flags returned by dtNavMeshQuery::findStraightPath. |
| 106 | enum dtStraightPathFlags |
| 107 | { |
| 108 | DT_STRAIGHTPATH_START = 0x01, ///< The vertex is the start position in the path. |
| 109 | DT_STRAIGHTPATH_END = 0x02, ///< The vertex is the end position in the path. |
| 110 | DT_STRAIGHTPATH_OFFMESH_CONNECTION = 0x04 ///< The vertex is the start of an off-mesh connection. |
| 111 | }; |
| 112 | |
| 113 | /// Options for dtNavMeshQuery::findStraightPath. |
| 114 | enum dtStraightPathOptions |
| 115 | { |
| 116 | DT_STRAIGHTPATH_AREA_CROSSINGS = 0x01, ///< Add a vertex at every polygon edge crossing where area changes. |
| 117 | DT_STRAIGHTPATH_ALL_CROSSINGS = 0x02 ///< Add a vertex at every polygon edge crossing. |
| 118 | }; |
| 119 | |
| 120 | |
| 121 | /// Options for dtNavMeshQuery::initSlicedFindPath and updateSlicedFindPath |
| 122 | enum dtFindPathOptions |
| 123 | { |
| 124 | DT_FINDPATH_ANY_ANGLE = 0x02 ///< use raycasts during pathfind to "shortcut" (raycast still consider costs) |
| 125 | }; |
| 126 | |
| 127 | /// Options for dtNavMeshQuery::raycast |
| 128 | enum dtRaycastOptions |
| 129 | { |
| 130 | DT_RAYCAST_USE_COSTS = 0x01 ///< Raycast should calculate movement cost along the ray and fill RaycastHit::cost |
| 131 | }; |
| 132 | |
| 133 | enum dtDetailTriEdgeFlags |
| 134 | { |
| 135 | DT_DETAIL_EDGE_BOUNDARY = 0x01 ///< Detail triangle edge is part of the poly boundary |
| 136 | }; |
| 137 | |
| 138 | |
| 139 | /// Limit raycasting during any angle pahfinding |
| 140 | /// The limit is given as a multiple of the character radius |
| 141 | static const float DT_RAY_CAST_LIMIT_PROPORTIONS = 50.0f; |
| 142 | |
| 143 | /// Flags representing the type of a navigation mesh polygon. |
| 144 | enum dtPolyTypes |
| 145 | { |
| 146 | /// The polygon is a standard convex polygon that is part of the surface of the mesh. |
| 147 | DT_POLYTYPE_GROUND = 0, |
| 148 | /// The polygon is an off-mesh connection consisting of two vertices. |
| 149 | DT_POLYTYPE_OFFMESH_CONNECTION = 1 |
| 150 | }; |
| 151 | |
| 152 | |
| 153 | /// Defines a polygon within a dtMeshTile object. |
| 154 | /// @ingroup detour |
| 155 | struct dtPoly |
| 156 | { |
| 157 | /// Index to first link in linked list. (Or #DT_NULL_LINK if there is no link.) |
| 158 | unsigned int firstLink; |
| 159 | |
| 160 | /// The indices of the polygon's vertices. |
| 161 | /// The actual vertices are located in dtMeshTile::verts. |
| 162 | unsigned short verts[DT_VERTS_PER_POLYGON]; |
| 163 | |
| 164 | /// Packed data representing neighbor polygons references and flags for each edge. |
| 165 | unsigned short neis[DT_VERTS_PER_POLYGON]; |
| 166 | |
| 167 | /// The user defined polygon flags. |
| 168 | unsigned short flags; |
| 169 | |
| 170 | /// The number of vertices in the polygon. |
| 171 | unsigned char vertCount; |
| 172 | |
| 173 | /// The bit packed area id and polygon type. |
| •174 | /// @note Use the structure's set and get methods to access this value. |
| 175 | unsigned char areaAndtype; |
| 176 | |
| 177 | /// Sets the user defined area id. [Limit: < #DT_MAX_AREAS] |
| 178 | inline void setArea(unsigned char a) { areaAndtype = (areaAndtype & 0xc0) | (a & 0x3f); } |
| 179 | |
| 180 | /// Sets the polygon type. (See: #dtPolyTypes.) |
| 181 | inline void setType(unsigned char t) { areaAndtype = (areaAndtype & 0x3f) | (t << 6); } |
| 182 | |
| 183 | /// Gets the user defined area id. |
| 184 | inline unsigned char getArea() const { return areaAndtype & 0x3f; } |
| 185 | |
| 186 | /// Gets the polygon type. (See: #dtPolyTypes) |
| 187 | inline unsigned char getType() const { return areaAndtype >> 6; } |
| 188 | }; |
| 189 | |
| 190 | /// Defines the location of detail sub-mesh data within a dtMeshTile. |
| 191 | struct dtPolyDetail |
| 192 | { |
| 193 | unsigned int vertBase; ///< The offset of the vertices in the dtMeshTile::detailVerts array. |
| 194 | unsigned int triBase; ///< The offset of the triangles in the dtMeshTile::detailTris array. |
| 195 | unsigned char vertCount; ///< The number of vertices in the sub-mesh. |
| 196 | unsigned char triCount; ///< The number of triangles in the sub-mesh. |
| 197 | }; |
| 198 | |
| 199 | /// Defines a link between polygons. |
| 200 | /// @note This structure is rarely if ever used by the end user. |
| 201 | /// @see dtMeshTile |
| 202 | struct dtLink |
| 203 | { |
| 204 | dtPolyRef ref; ///< Neighbour reference. (The neighbor that is linked to.) |
| 205 | unsigned int next; ///< Index of the next link. |
| 206 | unsigned char edge; ///< Index of the polygon edge that owns this link. |
| 207 | unsigned char side; ///< If a boundary link, defines on which side the link is. |
| 208 | unsigned char bmin; ///< If a boundary link, defines the minimum sub-edge area. |
| 209 | unsigned char bmax; ///< If a boundary link, defines the maximum sub-edge area. |
| 210 | }; |
| 211 | |
| 212 | /// Bounding volume node. |
| 213 | /// @note This structure is rarely if ever used by the end user. |
| 214 | /// @see dtMeshTile |
| 215 | struct dtBVNode |
| 216 | { |
| 217 | unsigned short bmin[3]; ///< Minimum bounds of the node's AABB. [(x, y, z)] |
| 218 | unsigned short bmax[3]; ///< Maximum bounds of the node's AABB. [(x, y, z)] |
| 219 | int i; ///< The node's index. (Negative for escape sequence.) |
| 220 | }; |
| 221 | |
| 222 | /// Defines an navigation mesh off-mesh connection within a dtMeshTile object. |
| 223 | /// An off-mesh connection is a user defined traversable connection made up to two vertices. |
| 224 | struct dtOffMeshConnection |
| 225 | { |
| 226 | /// The endpoints of the connection. [(ax, ay, az, bx, by, bz)] |
| 227 | float pos[6]; |
| 228 | |
| 229 | /// The radius of the endpoints. [Limit: >= 0] |
| 230 | float rad; |
| 231 | |
| 232 | /// The polygon reference of the connection within the tile. |
| 233 | unsigned short poly; |
| 234 | |
| 235 | /// Link flags. |
| 236 | /// @note These are not the connection's user defined flags. Those are assigned via the |
| 237 | /// connection's dtPoly definition. These are link flags used for internal purposes. |
| 238 | unsigned char flags; |
| 239 | |
| 240 | /// End point side. |
| 241 | unsigned char side; |
| 242 | |
| 243 | /// The id of the offmesh connection. (User assigned when the navigation mesh is built.) |
| 244 | unsigned int userId; |
| 245 | }; |
| 246 | |
| 247 | /// Provides high level information related to a dtMeshTile object. |
| 248 | /// @ingroup detour |
| 249 | struct dtMeshHeader |
| 250 | { |
| 251 | int magic; ///< Tile magic number. (Used to identify the data format.) |
| 252 | int version; ///< Tile data format version number. |
| 253 | int x; ///< The x-position of the tile within the dtNavMesh tile grid. (x, y, layer) |
| 254 | int y; ///< The y-position of the tile within the dtNavMesh tile grid. (x, y, layer) |
| 255 | int layer; ///< The layer of the tile within the dtNavMesh tile grid. (x, y, layer) |
| 256 | unsigned int userId; ///< The user defined id of the tile. |
| 257 | int polyCount; ///< The number of polygons in the tile. |
| 258 | int vertCount; ///< The number of vertices in the tile. |
| 259 | int maxLinkCount; ///< The number of allocated links. |
| 260 | int detailMeshCount; ///< The number of sub-meshes in the detail mesh. |
| 261 | |
| 262 | /// The number of unique vertices in the detail mesh. (In addition to the polygon vertices.) |
| 263 | int detailVertCount; |
| 264 | |
| 265 | int detailTriCount; ///< The number of triangles in the detail mesh. |
| 266 | int bvNodeCount; ///< The number of bounding volume nodes. (Zero if bounding volumes are disabled.) |
| 267 | int offMeshConCount; ///< The number of off-mesh connections. |
| 268 | int offMeshBase; ///< The index of the first polygon which is an off-mesh connection. |
| 269 | float walkableHeight; ///< The height of the agents using the tile. |
| 270 | float walkableRadius; ///< The radius of the agents using the tile. |
| 271 | float walkableClimb; ///< The maximum climb height of the agents using the tile. |
| 272 | float bmin[3]; ///< The minimum bounds of the tile's AABB. [(x, y, z)] |
| 273 | float bmax[3]; ///< The maximum bounds of the tile's AABB. [(x, y, z)] |
| 274 | |
| 275 | /// The bounding volume quantization factor. |
| 276 | float bvQuantFactor; |
| 277 | }; |
| 278 | |
| 279 | /// Defines a navigation mesh tile. |
| 280 | /// @ingroup detour |
| 281 | struct dtMeshTile |
| 282 | { |
| 283 | unsigned int salt; ///< Counter describing modifications to the tile. |
| 284 | |
| 285 | unsigned int linksFreeList; ///< Index to the next free link. |
| 286 | dtMeshHeader* header; ///< The tile header. |
| 287 | dtPoly* polys; ///< The tile polygons. [Size: dtMeshHeader::polyCount] |
| 288 | float* verts; ///< The tile vertices. [(x, y, z) * dtMeshHeader::vertCount] |
| 289 | dtLink* links; ///< The tile links. [Size: dtMeshHeader::maxLinkCount] |
| 290 | dtPolyDetail* detailMeshes; ///< The tile's detail sub-meshes. [Size: dtMeshHeader::detailMeshCount] |
| 291 | |
| 292 | /// The detail mesh's unique vertices. [(x, y, z) * dtMeshHeader::detailVertCount] |
| 293 | float* detailVerts; |
| 294 | |
| 295 | /// The detail mesh's triangles. [(vertA, vertB, vertC, triFlags) * dtMeshHeader::detailTriCount]. |
| 296 | /// See dtDetailTriEdgeFlags and dtGetDetailTriEdgeFlags. |
| 297 | unsigned char* detailTris; |
| 298 | |
| 299 | /// The tile bounding volume nodes. [Size: dtMeshHeader::bvNodeCount] |
| 300 | /// (Will be null if bounding volumes are disabled.) |
| 301 | dtBVNode* bvTree; |
| 302 | |
| 303 | dtOffMeshConnection* offMeshCons; ///< The tile off-mesh connections. [Size: dtMeshHeader::offMeshConCount] |
| 304 | |
| 305 | unsigned char* data; ///< The tile data. (Not directly accessed under normal situations.) |
| 306 | int dataSize; ///< Size of the tile data. |
| 307 | int flags; ///< Tile flags. (See: #dtTileFlags) |
| 308 | dtMeshTile* next; ///< The next free tile, or the next tile in the spatial grid. |
| 309 | private: |
| 310 | dtMeshTile(const dtMeshTile&); |
| 311 | dtMeshTile& operator=(const dtMeshTile&); |
| 312 | }; |
| 313 | |
| 314 | /// Get flags for edge in detail triangle. |
| 315 | /// @param[in] triFlags The flags for the triangle (last component of detail vertices above). |
| 316 | /// @param[in] edgeIndex The index of the first vertex of the edge. For instance, if 0, |
| 317 | /// returns flags for edge AB. |
| 318 | inline int dtGetDetailTriEdgeFlags(unsigned char triFlags, int edgeIndex) |
| 319 | { |
| 320 | return (triFlags >> (edgeIndex * 2)) & 0x3; |
| 321 | } |
| 322 | |
| 323 | /// Configuration parameters used to define multi-tile navigation meshes. |
| 324 | /// The values are used to allocate space during the initialization of a navigation mesh. |
| 325 | /// @see dtNavMesh::init() |
| 326 | /// @ingroup detour |
| 327 | struct dtNavMeshParams |
| 328 | { |
| 329 | float orig[3]; ///< The world space origin of the navigation mesh's tile space. [(x, y, z)] |
| 330 | float tileWidth; ///< The width of each tile. (Along the x-axis.) |
| 331 | float tileHeight; ///< The height of each tile. (Along the z-axis.) |
| 332 | int maxTiles; ///< The maximum number of tiles the navigation mesh can contain. This and maxPolys are used to calculate how many bits are needed to identify tiles and polygons uniquely. |
| 333 | int maxPolys; ///< The maximum number of polygons each tile can contain. This and maxTiles are used to calculate how many bits are needed to identify tiles and polygons uniquely. |
| 334 | }; |
| 335 | |
| 336 | /// A navigation mesh based on tiles of convex polygons. |
| 337 | /// @ingroup detour |
| 338 | class dtNavMesh |
| 339 | { |
| 340 | public: |
| 341 | dtNavMesh(); |
| 342 | ~dtNavMesh(); |
| 343 | |
| 344 | /// @{ |
| 345 | /// @name Initialization and Tile Management |
| 346 | |
| 347 | /// Initializes the navigation mesh for tiled use. |
| 348 | /// @param[in] params Initialization parameters. |
| 349 | /// @return The status flags for the operation. |
| 350 | dtStatus init(const dtNavMeshParams* params); |
| 351 | |
| 352 | /// Initializes the navigation mesh for single tile use. |
| 353 | /// @param[in] data Data of the new tile. (See: #dtCreateNavMeshData) |
| 354 | /// @param[in] dataSize The data size of the new tile. |
| 355 | /// @param[in] flags The tile flags. (See: #dtTileFlags) |
| 356 | /// @return The status flags for the operation. |
| 357 | /// @see dtCreateNavMeshData |
| 358 | dtStatus init(unsigned char* data, const int dataSize, const int flags); |
| 359 | |
| 360 | /// The navigation mesh initialization params. |
| 361 | const dtNavMeshParams* getParams() const; |
| 362 | |
| 363 | /// Adds a tile to the navigation mesh. |
| 364 | /// @param[in] data Data for the new tile mesh. (See: #dtCreateNavMeshData) |
| 365 | /// @param[in] dataSize Data size of the new tile mesh. |
| 366 | /// @param[in] flags Tile flags. (See: #dtTileFlags) |
| 367 | /// @param[in] lastRef The desired reference for the tile. (When reloading a tile.) [opt] [Default: 0] |
| 368 | /// @param[out] result The tile reference. (If the tile was succesfully added.) [opt] |
| 369 | /// @return The status flags for the operation. |
| 370 | dtStatus addTile(unsigned char* data, int dataSize, int flags, dtTileRef lastRef, dtTileRef* result); |
| 371 | |
| 372 | /// Removes the specified tile from the navigation mesh. |
| 373 | /// @param[in] ref The reference of the tile to remove. |
| 374 | /// @param[out] data Data associated with deleted tile. |
| 375 | /// @param[out] dataSize Size of the data associated with deleted tile. |
| 376 | /// @return The status flags for the operation. |
| 377 | dtStatus removeTile(dtTileRef ref, unsigned char** data, int* dataSize); |
| 378 | |
| 379 | /// @} |
| 380 | |
| 381 | /// @{ |
| 382 | /// @name Query Functions |
| 383 | |
| 384 | /// Calculates the tile grid location for the specified world position. |
| 385 | /// @param[in] pos The world position for the query. [(x, y, z)] |
| 386 | /// @param[out] tx The tile's x-location. (x, y) |
| 387 | /// @param[out] ty The tile's y-location. (x, y) |
| 388 | void calcTileLoc(const float* pos, int* tx, int* ty) const; |
| 389 | |
| 390 | /// Gets the tile at the specified grid location. |
| 391 | /// @param[in] x The tile's x-location. (x, y, layer) |
| 392 | /// @param[in] y The tile's y-location. (x, y, layer) |
| 393 | /// @param[in] layer The tile's layer. (x, y, layer) |
| 394 | /// @return The tile, or null if the tile does not exist. |
| 395 | const dtMeshTile* getTileAt(const int x, const int y, const int layer) const; |
| 396 | |
| 397 | /// Gets all tiles at the specified grid location. (All layers.) |
| 398 | /// @param[in] x The tile's x-location. (x, y) |
| 399 | /// @param[in] y The tile's y-location. (x, y) |
| 400 | /// @param[out] tiles A pointer to an array of tiles that will hold the result. |
| 401 | /// @param[in] maxTiles The maximum tiles the tiles parameter can hold. |
| 402 | /// @return The number of tiles returned in the tiles array. |
| 403 | int getTilesAt(const int x, const int y, |
| 404 | dtMeshTile const** tiles, const int maxTiles) const; |
| 405 | |
| 406 | /// Gets the tile reference for the tile at specified grid location. |
| 407 | /// @param[in] x The tile's x-location. (x, y, layer) |
| 408 | /// @param[in] y The tile's y-location. (x, y, layer) |
| 409 | /// @param[in] layer The tile's layer. (x, y, layer) |
| 410 | /// @return The tile reference of the tile, or 0 if there is none. |
| 411 | dtTileRef getTileRefAt(int x, int y, int layer) const; |
| 412 | |
| 413 | /// Gets the tile reference for the specified tile. |
| 414 | /// @param[in] tile The tile. |
| 415 | /// @return The tile reference of the tile. |
| 416 | dtTileRef getTileRef(const dtMeshTile* tile) const; |
| 417 | |
| 418 | /// Gets the tile for the specified tile reference. |
| 419 | /// @param[in] ref The tile reference of the tile to retrieve. |
| 420 | /// @return The tile for the specified reference, or null if the |
| 421 | /// reference is invalid. |
| 422 | const dtMeshTile* getTileByRef(dtTileRef ref) const; |
| 423 | |
| 424 | /// The maximum number of tiles supported by the navigation mesh. |
| 425 | /// @return The maximum number of tiles supported by the navigation mesh. |
| 426 | int getMaxTiles() const; |
| 427 | |
| 428 | /// Gets the tile at the specified index. |
| 429 | /// @param[in] i The tile index. [Limit: 0 >= index < #getMaxTiles()] |
| 430 | /// @return The tile at the specified index. |
| 431 | const dtMeshTile* getTile(int i) const; |
| 432 | |
| 433 | /// Gets the tile and polygon for the specified polygon reference. |
| 434 | /// @param[in] ref The reference for the a polygon. |
| 435 | /// @param[out] tile The tile containing the polygon. |
| 436 | /// @param[out] poly The polygon. |
| 437 | /// @return The status flags for the operation. |
| 438 | dtStatus getTileAndPolyByRef(const dtPolyRef ref, const dtMeshTile** tile, const dtPoly** poly) const; |
| 439 | |
| 440 | /// Returns the tile and polygon for the specified polygon reference. |
| 441 | /// @param[in] ref A known valid reference for a polygon. |
| 442 | /// @param[out] tile The tile containing the polygon. |
| 443 | /// @param[out] poly The polygon. |
| 444 | void getTileAndPolyByRefUnsafe(const dtPolyRef ref, const dtMeshTile** tile, const dtPoly** poly) const; |
| 445 | |
| 446 | /// Checks the validity of a polygon reference. |
| 447 | /// @param[in] ref The polygon reference to check. |
| 448 | /// @return True if polygon reference is valid for the navigation mesh. |
| 449 | bool isValidPolyRef(dtPolyRef ref) const; |
| 450 | |
| 451 | /// Gets the polygon reference for the tile's base polygon. |
| 452 | /// @param[in] tile The tile. |
| 453 | /// @return The polygon reference for the base polygon in the specified tile. |
| 454 | dtPolyRef getPolyRefBase(const dtMeshTile* tile) const; |
| 455 | |
| 456 | /// Gets the endpoints for an off-mesh connection, ordered by "direction of travel". |
| 457 | /// @param[in] prevRef The reference of the polygon before the connection. |
| 458 | /// @param[in] polyRef The reference of the off-mesh connection polygon. |
| 459 | /// @param[out] startPos The start position of the off-mesh connection. [(x, y, z)] |
| 460 | /// @param[out] endPos The end position of the off-mesh connection. [(x, y, z)] |
| 461 | /// @return The status flags for the operation. |
| 462 | dtStatus getOffMeshConnectionPolyEndPoints(dtPolyRef prevRef, dtPolyRef polyRef, float* startPos, float* endPos) const; |
| 463 | |
| 464 | /// Gets the specified off-mesh connection. |
| 465 | /// @param[in] ref The polygon reference of the off-mesh connection. |
| 466 | /// @return The specified off-mesh connection, or null if the polygon reference is not valid. |
| 467 | const dtOffMeshConnection* getOffMeshConnectionByRef(dtPolyRef ref) const; |
| 468 | |
| 469 | /// @} |
| 470 | |
| 471 | /// @{ |
| 472 | /// @name State Management |
| 473 | /// These functions do not effect #dtTileRef or #dtPolyRef's. |
| 474 | |
| 475 | /// Sets the user defined flags for the specified polygon. |
| 476 | /// @param[in] ref The polygon reference. |
| 477 | /// @param[in] flags The new flags for the polygon. |
| 478 | /// @return The status flags for the operation. |
| 479 | dtStatus setPolyFlags(dtPolyRef ref, unsigned short flags); |
| 480 | |
| 481 | /// Gets the user defined flags for the specified polygon. |
| 482 | /// @param[in] ref The polygon reference. |
| 483 | /// @param[out] resultFlags The polygon flags. |
| 484 | /// @return The status flags for the operation. |
| 485 | dtStatus getPolyFlags(dtPolyRef ref, unsigned short* resultFlags) const; |
| 486 | |
| 487 | /// Sets the user defined area for the specified polygon. |
| 488 | /// @param[in] ref The polygon reference. |
| 489 | /// @param[in] area The new area id for the polygon. [Limit: < #DT_MAX_AREAS] |
| 490 | /// @return The status flags for the operation. |
| 491 | dtStatus setPolyArea(dtPolyRef ref, unsigned char area); |
| 492 | |
| 493 | /// Gets the user defined area for the specified polygon. |
| 494 | /// @param[in] ref The polygon reference. |
| 495 | /// @param[out] resultArea The area id for the polygon. |
| 496 | /// @return The status flags for the operation. |
| 497 | dtStatus getPolyArea(dtPolyRef ref, unsigned char* resultArea) const; |
| 498 | |
| 499 | /// Gets the size of the buffer required by #storeTileState to store the specified tile's state. |
| 500 | /// @param[in] tile The tile. |
| 501 | /// @return The size of the buffer required to store the state. |
| 502 | int getTileStateSize(const dtMeshTile* tile) const; |
| 503 | |
| 504 | /// Stores the non-structural state of the tile in the specified buffer. (Flags, area ids, etc.) |
| 505 | /// @param[in] tile The tile. |
| 506 | /// @param[out] data The buffer to store the tile's state in. |
| 507 | /// @param[in] maxDataSize The size of the data buffer. [Limit: >= #getTileStateSize] |
| 508 | /// @return The status flags for the operation. |
| 509 | dtStatus storeTileState(const dtMeshTile* tile, unsigned char* data, const int maxDataSize) const; |
| 510 | |
| 511 | /// Restores the state of the tile. |
| 512 | /// @param[in] tile The tile. |
| 513 | /// @param[in] data The new state. (Obtained from #storeTileState.) |
| 514 | /// @param[in] maxDataSize The size of the state within the data buffer. |
| 515 | /// @return The status flags for the operation. |
| 516 | dtStatus restoreTileState(dtMeshTile* tile, const unsigned char* data, const int maxDataSize); |
| 517 | |
| 518 | /// @} |
| 519 | |
| 520 | /// @{ |
| 521 | /// @name Encoding and Decoding |
| 522 | /// These functions are generally meant for internal use only. |
| 523 | |
| 524 | /// Derives a standard polygon reference. |
| 525 | /// @note This function is generally meant for internal use only. |
| 526 | /// @param[in] salt The tile's salt value. |
| 527 | /// @param[in] it The index of the tile. |
| 528 | /// @param[in] ip The index of the polygon within the tile. |
| 529 | inline dtPolyRef encodePolyId(unsigned int salt, unsigned int it, unsigned int ip) const |
| 530 | { |
| 531 | #ifdef DT_POLYREF64 |
| 532 | return ((dtPolyRef)salt << (DT_POLY_BITS+DT_TILE_BITS)) | ((dtPolyRef)it << DT_POLY_BITS) | (dtPolyRef)ip; |
| 533 | #else |
| 534 | return ((dtPolyRef)salt << (m_polyBits+m_tileBits)) | ((dtPolyRef)it << m_polyBits) | (dtPolyRef)ip; |
| 535 | #endif |
| 536 | } |
| 537 | |
| 538 | /// Decodes a standard polygon reference. |
| 539 | /// @note This function is generally meant for internal use only. |
| 540 | /// @param[in] ref The polygon reference to decode. |
| 541 | /// @param[out] salt The tile's salt value. |
| 542 | /// @param[out] it The index of the tile. |
| 543 | /// @param[out] ip The index of the polygon within the tile. |
| 544 | /// @see #encodePolyId |
| 545 | inline void decodePolyId(dtPolyRef ref, unsigned int& salt, unsigned int& it, unsigned int& ip) const |
| 546 | { |
| 547 | #ifdef DT_POLYREF64 |
| 548 | const dtPolyRef saltMask = ((dtPolyRef)1<<DT_SALT_BITS)-1; |
| 549 | const dtPolyRef tileMask = ((dtPolyRef)1<<DT_TILE_BITS)-1; |
| 550 | const dtPolyRef polyMask = ((dtPolyRef)1<<DT_POLY_BITS)-1; |
| 551 | salt = (unsigned int)((ref >> (DT_POLY_BITS+DT_TILE_BITS)) & saltMask); |
| 552 | it = (unsigned int)((ref >> DT_POLY_BITS) & tileMask); |
| 553 | ip = (unsigned int)(ref & polyMask); |
| 554 | #else |
| 555 | const dtPolyRef saltMask = ((dtPolyRef)1<<m_saltBits)-1; |
| 556 | const dtPolyRef tileMask = ((dtPolyRef)1<<m_tileBits)-1; |
| 557 | const dtPolyRef polyMask = ((dtPolyRef)1<<m_polyBits)-1; |
| 558 | salt = (unsigned int)((ref >> (m_polyBits+m_tileBits)) & saltMask); |
| 559 | it = (unsigned int)((ref >> m_polyBits) & tileMask); |
| 560 | ip = (unsigned int)(ref & polyMask); |
| 561 | #endif |
| 562 | } |
| 563 | |
| 564 | /// Extracts a tile's salt value from the specified polygon reference. |
| 565 | /// @note This function is generally meant for internal use only. |
| 566 | /// @param[in] ref The polygon reference. |
| 567 | /// @see #encodePolyId |
| 568 | inline unsigned int decodePolyIdSalt(dtPolyRef ref) const |
| 569 | { |
| 570 | #ifdef DT_POLYREF64 |
| 571 | const dtPolyRef saltMask = ((dtPolyRef)1<<DT_SALT_BITS)-1; |
| 572 | return (unsigned int)((ref >> (DT_POLY_BITS+DT_TILE_BITS)) & saltMask); |
| 573 | #else |
| 574 | const dtPolyRef saltMask = ((dtPolyRef)1<<m_saltBits)-1; |
| 575 | return (unsigned int)((ref >> (m_polyBits+m_tileBits)) & saltMask); |
| 576 | #endif |
| 577 | } |
| 578 | |
| 579 | /// Extracts the tile's index from the specified polygon reference. |
| 580 | /// @note This function is generally meant for internal use only. |
| 581 | /// @param[in] ref The polygon reference. |
| 582 | /// @see #encodePolyId |
| 583 | inline unsigned int decodePolyIdTile(dtPolyRef ref) const |
| 584 | { |
| 585 | #ifdef DT_POLYREF64 |
| 586 | const dtPolyRef tileMask = ((dtPolyRef)1<<DT_TILE_BITS)-1; |
| 587 | return (unsigned int)((ref >> DT_POLY_BITS) & tileMask); |
| 588 | #else |
| 589 | const dtPolyRef tileMask = ((dtPolyRef)1<<m_tileBits)-1; |
| 590 | return (unsigned int)((ref >> m_polyBits) & tileMask); |
| 591 | #endif |
| 592 | } |
| 593 | |
| 594 | /// Extracts the polygon's index (within its tile) from the specified polygon reference. |
| 595 | /// @note This function is generally meant for internal use only. |
| 596 | /// @param[in] ref The polygon reference. |
| 597 | /// @see #encodePolyId |
| 598 | inline unsigned int decodePolyIdPoly(dtPolyRef ref) const |
| 599 | { |
| 600 | #ifdef DT_POLYREF64 |
| 601 | const dtPolyRef polyMask = ((dtPolyRef)1<<DT_POLY_BITS)-1; |
| 602 | return (unsigned int)(ref & polyMask); |
| 603 | #else |
| 604 | const dtPolyRef polyMask = ((dtPolyRef)1<<m_polyBits)-1; |
| 605 | return (unsigned int)(ref & polyMask); |
| 606 | #endif |
| 607 | } |
| 608 | |
| 609 | /// @} |
| 610 | |
| 611 | private: |
| 612 | // Explicitly disabled copy constructor and copy assignment operator. |
| 613 | dtNavMesh(const dtNavMesh&); |
| 614 | dtNavMesh& operator=(const dtNavMesh&); |
| 615 | |
| 616 | /// Returns pointer to tile in the tile array. |
| 617 | dtMeshTile* getTile(int i); |
| 618 | |
| 619 | /// Returns neighbour tile based on side. |
| 620 | int getTilesAt(const int x, const int y, |
| 621 | dtMeshTile** tiles, const int maxTiles) const; |
| 622 | |
| 623 | /// Returns neighbour tile based on side. |
| 624 | int getNeighbourTilesAt(const int x, const int y, const int side, |
| 625 | dtMeshTile** tiles, const int maxTiles) const; |
| 626 | |
| 627 | /// Returns all polygons in neighbour tile based on portal defined by the segment. |
| 628 | int findConnectingPolys(const float* va, const float* vb, |
| 629 | const dtMeshTile* tile, int side, |
| 630 | dtPolyRef* con, float* conarea, int maxcon) const; |
| 631 | |
| 632 | /// Builds internal polygons links for a tile. |
| 633 | void connectIntLinks(dtMeshTile* tile); |
| 634 | /// Builds internal polygons links for a tile. |
| 635 | void baseOffMeshLinks(dtMeshTile* tile); |
| 636 | |
| 637 | /// Builds external polygon links for a tile. |
| 638 | void connectExtLinks(dtMeshTile* tile, dtMeshTile* target, int side); |
| 639 | /// Builds external polygon links for a tile. |
| 640 | void connectExtOffMeshLinks(dtMeshTile* tile, dtMeshTile* target, int side); |
| 641 | |
| 642 | /// Removes external links at specified side. |
| 643 | void unconnectLinks(dtMeshTile* tile, dtMeshTile* target); |
| 644 | |
| 645 | |
| 646 | // TODO: These methods are duplicates from dtNavMeshQuery, but are needed for off-mesh connection finding. |
| 647 | |
| 648 | /// Queries polygons within a tile. |
| 649 | int queryPolygonsInTile(const dtMeshTile* tile, const float* qmin, const float* qmax, |
| 650 | dtPolyRef* polys, const int maxPolys) const; |
| 651 | /// Find nearest polygon within a tile. |
| 652 | dtPolyRef findNearestPolyInTile(const dtMeshTile* tile, const float* center, |
| 653 | const float* halfExtents, float* nearestPt) const; |
| 654 | /// Returns whether position is over the poly and the height at the position if so. |
| 655 | bool getPolyHeight(const dtMeshTile* tile, const dtPoly* poly, const float* pos, float* height) const; |
| 656 | /// Returns closest point on polygon. |
| 657 | void closestPointOnPoly(dtPolyRef ref, const float* pos, float* closest, bool* posOverPoly) const; |
| 658 | |
| 659 | dtNavMeshParams m_params; ///< Current initialization params. TODO: do not store this info twice. |
| 660 | float m_orig[3]; ///< Origin of the tile (0,0) |
| 661 | float m_tileWidth, m_tileHeight; ///< Dimensions of each tile. |
| 662 | int m_maxTiles; ///< Max number of tiles. |
| 663 | int m_tileLutSize; ///< Tile hash lookup size (must be pot). |
| 664 | int m_tileLutMask; ///< Tile hash lookup mask. |
| 665 | |
| 666 | dtMeshTile** m_posLookup; ///< Tile hash lookup. |
| 667 | dtMeshTile* m_nextFree; ///< Freelist of tiles. |
| 668 | dtMeshTile* m_tiles; ///< List of tiles. |
| 669 | |
| 670 | #ifndef DT_POLYREF64 |
| 671 | unsigned int m_saltBits; ///< Number of salt bits in the tile ID. |
| 672 | unsigned int m_tileBits; ///< Number of tile bits in the tile ID. |
| 673 | unsigned int m_polyBits; ///< Number of poly bits in the tile ID. |
| 674 | #endif |
| 675 | |
| 676 | friend class dtNavMeshQuery; |
| 677 | }; |
| 678 | |
| 679 | /// Allocates a navigation mesh object using the Detour allocator. |
| 680 | /// @return A navigation mesh that is ready for initialization, or null on failure. |
| 681 | /// @ingroup detour |
| 682 | dtNavMesh* dtAllocNavMesh(); |
| 683 | |
| 684 | /// Frees the specified navigation mesh object using the Detour allocator. |
| 685 | /// @param[in] navmesh A navigation mesh allocated using #dtAllocNavMesh |
| 686 | /// @ingroup detour |
| 687 | void dtFreeNavMesh(dtNavMesh* navmesh); |
| 688 | |
| 689 | #endif // DETOURNAVMESH_H |
| 690 | |
| 691 | /////////////////////////////////////////////////////////////////////////// |
| 692 | |
| 693 | // This section contains detailed documentation for members that don't have |
| 694 | // a source file. It reduces clutter in the main section of the header. |
| 695 | |
| 696 | /** |
| 697 | |
| 698 | @typedef dtPolyRef |
| 699 | @par |
| 700 | |
| 701 | Polygon references are subject to the same invalidate/preserve/restore |
| 702 | rules that apply to #dtTileRef's. If the #dtTileRef for the polygon's |
| 703 | tile changes, the polygon reference becomes invalid. |
| 704 | |
| 705 | Changing a polygon's flags, area id, etc. does not impact its polygon |
| 706 | reference. |
| 707 | |
| 708 | @typedef dtTileRef |
| 709 | @par |
| 710 | |
| 711 | The following changes will invalidate a tile reference: |
| 712 | |
| 713 | - The referenced tile has been removed from the navigation mesh. |
| 714 | - The navigation mesh has been initialized using a different set |
| 715 | of #dtNavMeshParams. |
| 716 | |
| 717 | A tile reference is preserved/restored if the tile is added to a navigation |
| 718 | mesh initialized with the original #dtNavMeshParams and is added at the |
| 719 | original reference location. (E.g. The lastRef parameter is used with |
| 720 | dtNavMesh::addTile.) |
| 721 | |
| 722 | Basically, if the storage structure of a tile changes, its associated |
| 723 | tile reference changes. |
| 724 | |
| 725 | |
| 726 | @var unsigned short dtPoly::neis[DT_VERTS_PER_POLYGON] |
| 727 | @par |
| 728 | |
| 729 | Each entry represents data for the edge starting at the vertex of the same index. |
| 730 | E.g. The entry at index n represents the edge data for vertex[n] to vertex[n+1]. |
| 731 | |
| 732 | A value of zero indicates the edge has no polygon connection. (It makes up the |
| 733 | border of the navigation mesh.) |
| 734 | |
| 735 | The information can be extracted as follows: |
| 736 | @code |
| 737 | neighborRef = neis[n] & 0xff; // Get the neighbor polygon reference. |
| 738 | |
| 739 | if (neis[n] & #DT_EX_LINK) |
| 740 | { |
| 741 | // The edge is an external (portal) edge. |
| 742 | } |
| 743 | @endcode |
| 744 | |
| 745 | @var float dtMeshHeader::bvQuantFactor |
| 746 | @par |
| 747 | |
| 748 | This value is used for converting between world and bounding volume coordinates. |
| 749 | For example: |
| 750 | @code |
| 751 | const float cs = 1.0f / tile->header->bvQuantFactor; |
| 752 | const dtBVNode* n = &tile->bvTree[i]; |
| 753 | if (n->i >= 0) |
| 754 | { |
| 755 | // This is a leaf node. |
| 756 | float worldMinX = tile->header->bmin[0] + n->bmin[0]*cs; |
| 757 | float worldMinY = tile->header->bmin[0] + n->bmin[1]*cs; |
| 758 | // Etc... |
| 759 | } |
| 760 | @endcode |
| 761 | |
| 762 | @struct dtMeshTile |
| 763 | @par |
| 764 | |
| 765 | Tiles generally only exist within the context of a dtNavMesh object. |
| 766 | |
| 767 | Some tile content is optional. For example, a tile may not contain any |
| 768 | off-mesh connections. In this case the associated pointer will be null. |
| 769 | |
| 770 | If a detail mesh exists it will share vertices with the base polygon mesh. |
| 771 | Only the vertices unique to the detail mesh will be stored in #detailVerts. |
| 772 | |
| 773 | @warning Tiles returned by a dtNavMesh object are not guarenteed to be populated. |
| 774 | For example: The tile at a location might not have been loaded yet, or may have been removed. |
| 775 | In this case, pointers will be null. So if in doubt, check the polygon count in the |
| 776 | tile's header to determine if a tile has polygons defined. |
| 777 | |
| 778 | @var float dtOffMeshConnection::pos[6] |
| 779 | @par |
| 780 | |
| 781 | For a properly built navigation mesh, vertex A will always be within the bounds of the mesh. |
| 782 | Vertex B is not required to be within the bounds of the mesh. |
| 783 | |
| 784 | */ |