navPath.h 5.1 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. /// Plan the path.
  52. bool plan();
  53. /// Updated a sliced plan.
  54. /// @return True if we need to keep updating, false if we can stop.
  55. bool update();
  56. /// Finalise a sliced plan.
  57. /// @return True if the plan was successful overall.
  58. bool finalise();
  59. /// Did the path plan successfully?
  60. bool success() const { return dtStatusSucceed(mStatus); }
  61. /// @}
  62. /// @name Path interface
  63. /// These functions are provided to make NavPath behave
  64. /// similarly to the existing Path class, despite NavPath
  65. /// not being a SimSet.
  66. /// @{
  67. /// Return the length of this path.
  68. F32 getLength() const { return mLength; };
  69. /// Get the number of nodes in a path.
  70. S32 size() const;
  71. /// Return world-space position of a path node.
  72. Point3F getNode(S32 idx) const;
  73. /// Get the flags for a given path node.
  74. U16 getFlags(S32 idx) const;
  75. /// @}
  76. /// @name SceneObject
  77. /// @{
  78. static void initPersistFields();
  79. bool onAdd();
  80. void onRemove();
  81. void onEditorEnable();
  82. void onEditorDisable();
  83. void inspectPostApply();
  84. void onDeleteNotify(SimObject *object);
  85. U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream);
  86. void unpackUpdate(NetConnection *conn, BitStream *stream);
  87. void prepRenderImage(SceneRenderState *state);
  88. void renderSimple(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat);
  89. DECLARE_CONOBJECT(NavPath);
  90. /// @}
  91. /// @name ProcessObject
  92. /// @{
  93. void processTick(const Move *move);
  94. /// @}
  95. NavPath();
  96. ~NavPath();
  97. protected:
  98. enum masks {
  99. PathMask = Parent::NextFreeMask << 0,
  100. NextFreeMask = Parent::NextFreeMask << 1
  101. };
  102. private:
  103. /// Create appropriate data structures and stuff.
  104. bool init();
  105. /// Plan the path.
  106. bool planInstant();
  107. /// Start a sliced plan.
  108. /// @return True if the plan initialised successfully.
  109. bool planSliced();
  110. /// Add points of the path between the two specified points.
  111. //bool addPoints(Point3F from, Point3F to, Vector<Point3F> *points);
  112. /// 'Visit' the last two points on our visit list.
  113. bool visitNext();
  114. dtNavMeshQuery *mQuery;
  115. dtStatus mStatus;
  116. dtQueryFilter mFilter;
  117. S32 mCurIndex;
  118. Vector<Point3F> mPoints;
  119. Vector<U16> mFlags;
  120. Vector<Point3F> mVisitPoints;
  121. F32 mLength;
  122. /// Resets our world transform and bounds to fit our point list.
  123. void resize();
  124. /// Function used to set mMesh object from console.
  125. static bool setProtectedMesh(void *obj, const char *index, const char *data);
  126. /// Function used to set mWaypoints from console.
  127. static bool setProtectedWaypoints(void *obj, const char *index, const char *data);
  128. void checkAutoUpdate();
  129. /// Function used to protect auto-update flag.
  130. static bool setProtectedAutoUpdate(void *obj, const char *index, const char *data);
  131. /// @name Protected from and to vectors
  132. /// @{
  133. static bool setProtectedFrom(void *obj, const char *index, const char *data);
  134. static bool setProtectedTo(void *obj, const char *index, const char *data);
  135. static const char *getProtectedFrom(void *obj, const char *data);
  136. static const char *getProtectedTo(void *obj, const char *data);
  137. /// @}
  138. };
  139. #endif