9
3

DetourNavMesh.h 31 KB

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