navPath.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 _NAVPATH_H_
  23. #define _NAVPATH_H_
  24. #include "scene/sceneObject.h"
  25. #include "scene/simPath.h"
  26. #include "navMesh.h"
  27. #include <DetourNavMeshQuery.h>
  28. class NavPath: public SceneObject {
  29. typedef SceneObject Parent;
  30. static const U32 MaxPathLen = 2048;
  31. public:
  32. /// @name NavPath
  33. /// Functions for planning and accessing the path.
  34. /// @{
  35. String mMeshName;
  36. NavMesh *mMesh;
  37. SimPath::Path *mWaypoints;
  38. Point3F mFrom;
  39. bool mFromSet;
  40. Point3F mTo;
  41. bool mToSet;
  42. bool mIsLooping;
  43. bool mAutoUpdate;
  44. bool mIsSliced;
  45. S32 mMaxIterations;
  46. bool mAlwaysRender;
  47. bool mXray;
  48. bool mRenderSearch;
  49. /// What sort of link types are we allowed to move on?
  50. LinkData mLinkTypes;
  51. dtQueryFilter mFilter;
  52. /// Plan the path.
  53. bool plan();
  54. /// Updated a sliced plan.
  55. /// @return True if we need to keep updating, false if we can stop.
  56. bool update();
  57. /// Finalise a sliced plan.
  58. /// @return True if the plan was successful overall.
  59. bool finalise();
  60. /// Did the path plan successfully?
  61. bool success() const { return dtStatusSucceed(mStatus); }
  62. /// @}
  63. /// @name Path interface
  64. /// These functions are provided to make NavPath behave
  65. /// similarly to the existing Path class, despite NavPath
  66. /// not being a SimSet.
  67. /// @{
  68. /// Return the length of this path.
  69. F32 getLength() const { return mLength; };
  70. /// Get the number of nodes in a path.
  71. S32 size() const;
  72. /// Return world-space position of a path node.
  73. Point3F getNode(S32 idx) const;
  74. /// Get the flags for a given path node.
  75. U16 getFlags(S32 idx) const;
  76. /// @}
  77. /// @name SceneObject
  78. /// @{
  79. static void initPersistFields();
  80. bool onAdd() override;
  81. void onRemove() override;
  82. void onEditorEnable() override;
  83. void onEditorDisable() override;
  84. void inspectPostApply() override;
  85. void onDeleteNotify(SimObject *object) override;
  86. U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream) override;
  87. void unpackUpdate(NetConnection *conn, BitStream *stream) override;
  88. void prepRenderImage(SceneRenderState *state) override;
  89. void renderSimple(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat);
  90. DECLARE_CONOBJECT(NavPath);
  91. DECLARE_CATEGORY("Navigation");
  92. /// @}
  93. /// @name ProcessObject
  94. /// @{
  95. void processTick(const Move *move) override;
  96. /// @}
  97. NavPath();
  98. ~NavPath();
  99. protected:
  100. enum masks {
  101. PathMask = Parent::NextFreeMask << 0,
  102. NextFreeMask = Parent::NextFreeMask << 1
  103. };
  104. private:
  105. /// Create appropriate data structures and stuff.
  106. bool init();
  107. /// Plan the path.
  108. bool planInstant();
  109. /// Start a sliced plan.
  110. /// @return True if the plan initialised successfully.
  111. bool planSliced();
  112. /// Add points of the path between the two specified points.
  113. //bool addPoints(Point3F from, Point3F to, Vector<Point3F> *points);
  114. /// 'Visit' the last two points on our visit list.
  115. bool visitNext();
  116. dtNavMeshQuery *mQuery;
  117. dtStatus mStatus;
  118. S32 mCurIndex;
  119. Vector<Point3F> mPoints;
  120. Vector<U16> mFlags;
  121. Vector<Point3F> mVisitPoints;
  122. F32 mLength;
  123. /// Resets our world transform and bounds to fit our point list.
  124. void resize();
  125. /// Function used to set mMesh object from console.
  126. static bool setProtectedMesh(void *obj, const char *index, const char *data);
  127. /// Function used to set mWaypoints from console.
  128. static bool setProtectedWaypoints(void *obj, const char *index, const char *data);
  129. void checkAutoUpdate();
  130. /// Function used to protect auto-update flag.
  131. static bool setProtectedAutoUpdate(void *obj, const char *index, const char *data);
  132. /// @name Protected from and to vectors
  133. /// @{
  134. static bool setProtectedFrom(void *obj, const char *index, const char *data);
  135. static bool setProtectedTo(void *obj, const char *index, const char *data);
  136. static const char *getProtectedFrom(void *obj, const char *data);
  137. static const char *getProtectedTo(void *obj, const char *data);
  138. /// @}
  139. };
  140. #endif