navMesh.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2014 Daniel Buckmaster
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _NAVMESH_H_
  23. #define _NAVMESH_H_
  24. #include <queue>
  25. #include "scene/sceneObject.h"
  26. #include "collision/concretePolyList.h"
  27. #include "recastPolyList.h"
  28. #include "util/messaging/eventManager.h"
  29. #include "torqueRecast.h"
  30. #include "duDebugDrawTorque.h"
  31. #include "coverPoint.h"
  32. #include <Recast.h>
  33. #include <DetourNavMesh.h>
  34. #include <DetourNavMeshBuilder.h>
  35. #include <DebugDraw.h>
  36. #include <DetourNavMeshQuery.h>
  37. /// @class NavMesh
  38. /// Represents a set of bounds within which a Recast navigation mesh is generated.
  39. /// @see NavMeshPolyList
  40. /// @see Trigger
  41. class NavMesh : public SceneObject {
  42. typedef SceneObject Parent;
  43. friend class NavPath;
  44. public:
  45. /// @name NavMesh build
  46. /// @{
  47. /// Initiates the navmesh build process, which includes notifying the
  48. /// clients and posting an event.
  49. bool build(bool background = true, bool saveIntermediates = false);
  50. /// Stop a build in progress.
  51. void cancelBuild();
  52. /// Generate cover points from a nav mesh.
  53. bool createCoverPoints();
  54. /// Remove all cover points
  55. void deleteCoverPoints();
  56. /// Save the navmesh to a file.
  57. bool save();
  58. /// Load a saved navmesh from a file.
  59. bool load();
  60. /// Instantly rebuild the tiles in the navmesh that overlap the box.
  61. void buildTiles(const Box3F &box);
  62. /// Instantly rebuild a specific tile.
  63. void buildTile(const U32 &tile);
  64. /// Rebuild parts of the navmesh where links have changed.
  65. void buildLinks();
  66. /// Data file to store this nav mesh in. (From engine executable dir.)
  67. StringTableEntry mFileName;
  68. /// Name of the SimSet to store cover points in. (Usually a SimGroup.)
  69. StringTableEntry mCoverSet;
  70. /// Cell width and height.
  71. F32 mCellSize, mCellHeight;
  72. /// @name Actor data
  73. /// @{
  74. F32 mWalkableHeight,
  75. mWalkableClimb,
  76. mWalkableRadius,
  77. mWalkableSlope;
  78. /// @}
  79. /// @name Generation data
  80. /// @{
  81. U32 mBorderSize;
  82. F32 mDetailSampleDist, mDetailSampleMaxError;
  83. U32 mMaxEdgeLen;
  84. F32 mMaxSimplificationError;
  85. static const U32 mMaxVertsPerPoly;
  86. U32 mMinRegionArea;
  87. U32 mMergeRegionArea;
  88. F32 mTileSize;
  89. U32 mMaxPolysPerTile;
  90. /// @}
  91. /// @name Water
  92. /// @{
  93. enum WaterMethod {
  94. Ignore,
  95. Solid,
  96. Impassable
  97. };
  98. WaterMethod mWaterMethod;
  99. /// @}
  100. /// @}
  101. /// Return the index of the tile included by this point.
  102. S32 getTile(const Point3F& pos);
  103. /// Return the box of a given tile.
  104. Box3F getTileBox(U32 id);
  105. /// @name Links
  106. /// @{
  107. /// Add an off-mesh link.
  108. S32 addLink(const Point3F &from, const Point3F &to, U32 flags = 0);
  109. /// Get the ID of the off-mesh link near the point.
  110. S32 getLink(const Point3F &pos);
  111. /// Get the number of links this mesh has.
  112. S32 getLinkCount();
  113. /// Get the starting point of a link.
  114. Point3F getLinkStart(U32 idx);
  115. /// Get the ending point of a link.
  116. Point3F getLinkEnd(U32 idx);
  117. /// Get the flags used by a link.
  118. LinkData getLinkFlags(U32 idx);
  119. /// Set flags used by a link.
  120. void setLinkFlags(U32 idx, const LinkData &d);
  121. /// Set the selected state of a link.
  122. void selectLink(U32 idx, bool select, bool hover = true);
  123. /// Delete the selected link.
  124. void deleteLink(U32 idx);
  125. /// @}
  126. /// Should small characters use this mesh?
  127. bool mSmallCharacters;
  128. /// Should regular-sized characters use this mesh?
  129. bool mRegularCharacters;
  130. /// Should large characters use this mesh?
  131. bool mLargeCharacters;
  132. /// Should vehicles use this mesh?
  133. bool mVehicles;
  134. /// @name Annotations
  135. /// @{
  136. /* not implemented
  137. /// Should we automatically generate jump-down links?
  138. bool mJumpDownLinks;
  139. /// Height of a 'small' jump link.
  140. F32 mJumpLinkSmall;
  141. /// Height of a 'large' jump link.
  142. F32 mJumpLinkLarge;
  143. */
  144. /// Distance to search for cover.
  145. F32 mCoverDist;
  146. /// Distance to search horizontally when peeking around cover.
  147. F32 mPeekDist;
  148. /// Add cover to walls that don't have corners?
  149. bool mInnerCover;
  150. /// @}
  151. /// @name SimObject
  152. /// @{
  153. virtual void onEditorEnable();
  154. virtual void onEditorDisable();
  155. void write(Stream &stream, U32 tabStop, U32 flags);
  156. /// @}
  157. /// @name SceneObject
  158. /// @{
  159. static void initPersistFields();
  160. bool onAdd();
  161. void onRemove();
  162. enum flags {
  163. BuildFlag = Parent::NextFreeMask << 0,
  164. LoadFlag = Parent::NextFreeMask << 1,
  165. NextFreeMask = Parent::NextFreeMask << 2,
  166. };
  167. U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream);
  168. void unpackUpdate(NetConnection *conn, BitStream *stream);
  169. void setTransform(const MatrixF &mat);
  170. void setScale(const VectorF &scale);
  171. /// @}
  172. /// @name ProcessObject
  173. /// @{
  174. void processTick(const Move *move);
  175. /// @}
  176. /// @name Rendering
  177. /// @{
  178. void prepRenderImage(SceneRenderState *state);
  179. void render(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat);
  180. void renderLinks(duDebugDraw &dd);
  181. void renderTileData(duDebugDrawTorque &dd, U32 tile);
  182. bool mAlwaysRender;
  183. /// @}
  184. NavMesh();
  185. ~NavMesh();
  186. DECLARE_CONOBJECT(NavMesh);
  187. /// Return the server-side NavMesh SimSet.
  188. static SimSet *getServerSet();
  189. /// Return the EventManager for all NavMeshes.
  190. static EventManager *getEventManager();
  191. void inspectPostApply();
  192. protected:
  193. dtNavMesh const* getNavMesh() { return nm; }
  194. private:
  195. /// Generates a navigation mesh for the collection of objects in this
  196. /// mesh. Returns true if successful. Stores the created mesh in tnm.
  197. bool generateMesh();
  198. /// Builds the next tile in the dirty list.
  199. void buildNextTile();
  200. /// Save imtermediate navmesh creation data?
  201. bool mSaveIntermediates;
  202. /// @name Tiles
  203. /// @{
  204. struct Tile {
  205. /// Torque-space world box of this tile.
  206. Box3F box;
  207. /// Local coordinates of this box.
  208. U32 x, y;
  209. /// Recast min and max points.
  210. F32 bmin[3], bmax[3];
  211. /// Default constructor.
  212. Tile() : box(Box3F::Invalid), x(0), y(0)
  213. {
  214. bmin[0] = bmin[1] = bmin[2] = bmax[0] = bmax[1] = bmax[2] = 0.0f;
  215. }
  216. /// Value constructor.
  217. Tile(const Box3F &b, U32 _x, U32 _y, const F32 *min, const F32 *max)
  218. : box(b), x(_x), y(_y)
  219. {
  220. rcVcopy(bmin, min);
  221. rcVcopy(bmax, max);
  222. }
  223. };
  224. /// Intermediate data for tile creation.
  225. struct TileData {
  226. RecastPolyList geom;
  227. rcHeightfield *hf;
  228. rcCompactHeightfield *chf;
  229. rcContourSet *cs;
  230. rcPolyMesh *pm;
  231. rcPolyMeshDetail *pmd;
  232. TileData()
  233. {
  234. hf = NULL;
  235. chf = NULL;
  236. cs = NULL;
  237. pm = NULL;
  238. pmd = NULL;
  239. }
  240. void freeAll()
  241. {
  242. geom.clear();
  243. rcFreeHeightField(hf);
  244. rcFreeCompactHeightfield(chf);
  245. rcFreeContourSet(cs);
  246. rcFreePolyMesh(pm);
  247. rcFreePolyMeshDetail(pmd);
  248. }
  249. ~TileData()
  250. {
  251. freeAll();
  252. }
  253. };
  254. /// List of tiles.
  255. Vector<Tile> mTiles;
  256. /// List of tile intermediate data.
  257. Vector<TileData> mTileData;
  258. /// List of indices to the tile array which are dirty.
  259. Vector<U32> mDirtyTiles;
  260. /// Update tile dimensions.
  261. void updateTiles(bool dirty = false);
  262. /// Generates navmesh data for a single tile.
  263. unsigned char *buildTileData(const Tile &tile, TileData &data, U32 &dataSize);
  264. /// @}
  265. /// @name Off-mesh links
  266. /// @{
  267. enum SelectState {
  268. Unselected,
  269. Hovered,
  270. Selected
  271. };
  272. Vector<F32> mLinkVerts; ///< Coordinates of each link vertex
  273. Vector<bool> mLinksUnsynced; ///< Are the editor links unsynced from the mesh?
  274. Vector<F32> mLinkRads; ///< Radius of each link
  275. Vector<U8> mLinkDirs; ///< Direction (one-way or bidirectional)
  276. Vector<U8> mLinkAreas; ///< Area ID
  277. Vector<U16> mLinkFlags; ///< Flags
  278. Vector<U32> mLinkIDs; ///< ID number of each link
  279. Vector<U8> mLinkSelectStates; ///< Selection state of links
  280. Vector<bool> mDeleteLinks; ///< Link will be deleted next build.
  281. U32 mCurLinkID;
  282. void eraseLink(U32 idx);
  283. void eraseLinks();
  284. void setLinkCount(U32 c);
  285. /// @}
  286. /// @name Intermediate data
  287. /// @{
  288. /// Config struct.
  289. rcConfig cfg;
  290. /// Updates our config from console members.
  291. void updateConfig();
  292. dtNavMesh *nm;
  293. rcContext *ctx;
  294. /// @}
  295. /// @name Cover
  296. /// @{
  297. struct CoverPointData {
  298. MatrixF trans;
  299. CoverPoint::Size size;
  300. bool peek[3];
  301. };
  302. /// Attempt to place cover points along a given edge.
  303. bool testEdgeCover(const Point3F &pos, const VectorF &dir, CoverPointData &data);
  304. /// @}
  305. /// Used to perform non-standard validation. detailSampleDist can be 0, or >= 0.9.
  306. static bool setProtectedDetailSampleDist(void *obj, const char *index, const char *data);
  307. /// Updates the client when we check the alwaysRender option.
  308. static bool setProtectedAlwaysRender(void *obj, const char *index, const char *data);
  309. /// @name Threaded updates
  310. /// @{
  311. /// A simple flag to say we are building.
  312. bool mBuilding;
  313. /// @}
  314. /// @name Rendering
  315. /// @{
  316. duDebugDrawTorque mDbgDraw;
  317. void renderToDrawer();
  318. /// @}
  319. /// Server-side set for all NavMesh objects.
  320. static SimObjectPtr<SimSet> smServerSet;
  321. /// Use this object to manage update events.
  322. static SimObjectPtr<EventManager> smEventManager;
  323. };
  324. typedef NavMesh::WaterMethod NavMeshWaterMethod;
  325. DefineEnumType(NavMeshWaterMethod);
  326. #endif