navPath.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  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. /// Maximum size of Detour path.
  31. static const U32 MaxPathLen = 1024;
  32. public:
  33. /// @name NavPath
  34. /// Functions for planning and accessing the path.
  35. /// @{
  36. SimObjectPtr<NavMesh> mMesh;
  37. SimObjectPtr<SimPath::Path> mWaypoints;
  38. /// Location to start at.
  39. Point3F mFrom;
  40. /// Has a starting location been set?
  41. bool mFromSet;
  42. /// Location to end at.
  43. Point3F mTo;
  44. /// Has an end been set?
  45. bool mToSet;
  46. /// This path should include a segment from the end to the start.
  47. bool mIsLooping;
  48. /// Render even when not selected in the editor.
  49. bool mAlwaysRender;
  50. /// Render on top of other objects.
  51. bool mXray;
  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. /// @}
  61. /// @name Path interface
  62. /// @{
  63. /// Return world-space position of a path node.
  64. /// @param[in] idx Node index to retrieve.
  65. Point3F getNode(S32 idx);
  66. /// Return the number of nodes in this path.
  67. S32 getCount();
  68. /// Return the length of this path.
  69. F32 getLength() { return mLength; };
  70. /// @}
  71. /// @name SceneObject
  72. /// @{
  73. static void initPersistFields();
  74. bool onAdd();
  75. void onRemove();
  76. void onEditorEnable();
  77. void onEditorDisable();
  78. void inspectPostApply();
  79. void onDeleteNotify(SimObject *object);
  80. U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream);
  81. void unpackUpdate(NetConnection *conn, BitStream *stream);
  82. void prepRenderImage(SceneRenderState *state);
  83. void renderSimple(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat);
  84. DECLARE_CONOBJECT(NavPath);
  85. /// @}
  86. /// @name ProcessObject
  87. /// @{
  88. void processTick(const Move *move);
  89. /// @}
  90. NavPath();
  91. ~NavPath();
  92. protected:
  93. enum masks {
  94. PathMask = Parent::NextFreeMask << 0,
  95. NextFreeMask = Parent::NextFreeMask << 1
  96. };
  97. private:
  98. /// Create appropriate data structures and stuff.
  99. bool init();
  100. /// 'Visit' the most recent two points on our visit list.
  101. bool visitNext();
  102. /// Detour path query.
  103. dtNavMeshQuery *mQuery;
  104. /// Current status of our Detour query.
  105. dtStatus mStatus;
  106. /// Filter that provides the movement costs for paths.
  107. dtQueryFilter mFilter;
  108. /// List of points the path should visit (waypoints, if you will).
  109. Vector<Point3F> mVisitPoints;
  110. /// List of points in the final path.
  111. Vector<Point3F> mPoints;
  112. /// Total length of path in world units.
  113. F32 mLength;
  114. /// Resets our world transform and bounds to fit our point list.
  115. void resize();
  116. /// @name Protected console getters/setters
  117. /// @{
  118. static bool setProtectedMesh(void *obj, const char *index, const char *data);
  119. static const char *getProtectedMesh(void *obj, const char *data);
  120. static bool setProtectedWaypoints(void *obj, const char *index, const char *data);
  121. static bool setProtectedFrom(void *obj, const char *index, const char *data);
  122. static const char *getProtectedFrom(void *obj, const char *data);
  123. static bool setProtectedTo(void *obj, const char *index, const char *data);
  124. static const char *getProtectedTo(void *obj, const char *data);
  125. /// @}
  126. };
  127. #endif