DynamicNavigationMesh.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Navigation/NavigationMesh.h"
  24. class dtTileCache;
  25. struct dtTileCacheAlloc;
  26. struct dtTileCacheCompressor;
  27. struct dtTileCacheMeshProcess;
  28. struct dtTileCacheLayer;
  29. struct dtTileCacheContourSet;
  30. struct dtTileCachePolyMesh;
  31. namespace Atomic
  32. {
  33. class OffMeshConnection;
  34. class Obstacle;
  35. class ATOMIC_API DynamicNavigationMesh : public NavigationMesh
  36. {
  37. ATOMIC_OBJECT(DynamicNavigationMesh, NavigationMesh)
  38. friend class Obstacle;
  39. friend struct MeshProcess;
  40. public:
  41. /// Constructor.
  42. DynamicNavigationMesh(Context*);
  43. /// Destructor.
  44. virtual ~DynamicNavigationMesh();
  45. /// Register with engine context.
  46. static void RegisterObject(Context*);
  47. /// Allocate the navigation mesh without building any tiles. Bounding box is not padded. Return true if successful.
  48. virtual bool Allocate(const BoundingBox& boundingBox, unsigned maxTiles);
  49. /// Build/rebuild the entire navigation mesh.
  50. virtual bool Build();
  51. /// Build/rebuild a portion of the navigation mesh.
  52. virtual bool Build(const BoundingBox& boundingBox);
  53. /// Rebuild part of the navigation mesh in the rectangular area. Return true if successful.
  54. virtual bool Build(const IntVector2& from, const IntVector2& to);
  55. /// Return tile data.
  56. virtual PODVector<unsigned char> GetTileData(const IntVector2& tile) const;
  57. /// Return whether the Obstacle is touching the given tile.
  58. bool IsObstacleInTile(Obstacle* obstacle, const IntVector2& tile) const;
  59. /// Add tile to navigation mesh.
  60. virtual bool AddTile(const PODVector<unsigned char>& tileData);
  61. /// Remove tile from navigation mesh.
  62. virtual void RemoveTile(const IntVector2& tile);
  63. /// Remove all tiles from navigation mesh.
  64. virtual void RemoveAllTiles();
  65. /// Visualize the component as debug geometry.
  66. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  67. /// Add debug geometry to the debug renderer.
  68. void DrawDebugGeometry(bool depthTest);
  69. /// Set navigation data attribute.
  70. virtual void SetNavigationDataAttr(const PODVector<unsigned char>& value);
  71. /// Return navigation data attribute.
  72. virtual PODVector<unsigned char> GetNavigationDataAttr() const;
  73. /// Set the maximum number of obstacles allowed.
  74. void SetMaxObstacles(unsigned maxObstacles) { maxObstacles_ = maxObstacles; }
  75. /// Set the maximum number of layers that navigation construction can create.
  76. void SetMaxLayers(unsigned maxLayers);
  77. /// Return the maximum number of obstacles allowed.
  78. unsigned GetMaxObstacles() const { return maxObstacles_; }
  79. /// Return the maximum number of layers permitted to build.
  80. unsigned GetMaxLayers() const { return maxLayers_; }
  81. /// Draw debug geometry for Obstacles.
  82. void SetDrawObstacles(bool enable) { drawObstacles_ = enable; }
  83. /// Return whether to draw Obstacles.
  84. bool GetDrawObstacles() const { return drawObstacles_; }
  85. protected:
  86. struct TileCacheData;
  87. /// Subscribe to events when assigned to a scene.
  88. virtual void OnSceneSet(Scene* scene);
  89. /// Trigger the tile cache to make updates to the nav mesh if necessary.
  90. void HandleSceneSubsystemUpdate(StringHash eventType, VariantMap& eventData);
  91. /// Used by Obstacle class to add itself to the tile cache, if 'silent' an event will not be raised.
  92. void AddObstacle(Obstacle* obstacle, bool silent = false);
  93. /// Used by Obstacle class to update itself.
  94. void ObstacleChanged(Obstacle* obstacle);
  95. /// Used by Obstacle class to remove itself from the tile cache, if 'silent' an event will not be raised.
  96. void RemoveObstacle(Obstacle*, bool silent = false);
  97. /// Build one tile of the navigation mesh. Return true if successful.
  98. int BuildTile(Vector<NavigationGeometryInfo>& geometryList, int x, int z, TileCacheData* tiles);
  99. /// Build tiles in the rectangular area. Return number of built tiles.
  100. unsigned BuildTiles(Vector<NavigationGeometryInfo>& geometryList, const IntVector2& from, const IntVector2& to);
  101. /// Off-mesh connections to be rebuilt in the mesh processor.
  102. PODVector<OffMeshConnection*> CollectOffMeshConnections(const BoundingBox& bounds);
  103. /// Release the navigation mesh, query, and tile cache.
  104. virtual void ReleaseNavigationMesh();
  105. private:
  106. /// Write tiles data.
  107. void WriteTiles(Serializer& dest, int x, int z) const;
  108. /// Read tiles data to the navigation mesh.
  109. bool ReadTiles(Deserializer& source, bool silent);
  110. /// Free the tile cache.
  111. void ReleaseTileCache();
  112. /// Detour tile cache instance that works with the nav mesh.
  113. dtTileCache* tileCache_;
  114. /// Used by dtTileCache to allocate blocks of memory.
  115. UniquePtr<dtTileCacheAlloc> allocator_;
  116. /// Used by dtTileCache to compress the original tiles to use when reconstructing for changes.
  117. UniquePtr<dtTileCacheCompressor> compressor_;
  118. /// Mesh processor used by Detour, in this case a 'pass-through' processor.
  119. UniquePtr<dtTileCacheMeshProcess> meshProcessor_;
  120. /// Maximum number of obstacle objects allowed.
  121. unsigned maxObstacles_;
  122. /// Maximum number of layers that are allowed to be constructed.
  123. unsigned maxLayers_;
  124. /// Debug draw Obstacles.
  125. bool drawObstacles_;
  126. /// Queue of tiles to be built.
  127. PODVector<IntVector2> tileQueue_;
  128. };
  129. }