From 646ec15f5ac87a7ebf475143941440b2402567dc Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 5 Nov 2019 15:22:38 +0800 Subject: [PATCH] 1 --- cpp/navigation.cc | 11 +++++ cpp/navigation.h | 1 + cpp/navigation_handle.cc | 90 +++++++++++++++++++++++++++++++++++++--- cpp/navigation_handle.h | 44 +++++++++++++++++--- 4 files changed, 134 insertions(+), 12 deletions(-) diff --git a/cpp/navigation.cc b/cpp/navigation.cc index 2406544..3a1b44b 100644 --- a/cpp/navigation.cc +++ b/cpp/navigation.cc @@ -2,6 +2,7 @@ #include "behaviac/behaviac.h" #include "navigation.h" +#include "navigation_handle.h" namespace f8 { @@ -15,6 +16,16 @@ namespace f8 } + f8::NavigationHandle* Navigation::NewNavigation(std::string& unique_str) + { + if (HasNavigation(unique_str)) { + abort(); + } + f8::NavigationHandle* handle = new f8::NavigationHandle; + navhandles_[unique_str] = handle; + return handle; + } + f8::NavigationHandle* Navigation::LoadNavigation(const std::string& res_path, const std::map& params) { diff --git a/cpp/navigation.h b/cpp/navigation.h index 8cd6993..8d4188d 100644 --- a/cpp/navigation.h +++ b/cpp/navigation.h @@ -13,6 +13,7 @@ namespace f8 void Init(); void UnInit(); + f8::NavigationHandle* NewNavigation(std::string& unique_str); f8::NavigationHandle* LoadNavigation(const std::string& res_path, const std::map& params); bool HasNavigation(const std::string& res_path); diff --git a/cpp/navigation_handle.cc b/cpp/navigation_handle.cc index e8e0e14..8b26016 100644 --- a/cpp/navigation_handle.cc +++ b/cpp/navigation_handle.cc @@ -4,22 +4,100 @@ namespace f8 { - int NavigationHandle::FindStraightPath(int layer, const a8::Vec3& start, const a8::Vec3& end, - std::vector& path_list) + + int NavMeshLayer::FindStraightPath(const a8::Vec3& start, const a8::Vec3& end, + std::vector& path_list) { return 0; } - int NavigationHandle::FindRandomPointAroundCircle(int layer, const a8::Vec3& center_pos, - std::vector& points, int max_points, float max_radius) + int NavMeshLayer::FindRandomPointAroundCircle(const a8::Vec3& center_pos, + std::vector& points, + int max_points, float max_radius) { return 0; } - int NavigationHandle::Raycast(int layer, const a8::Vec3& start, const a8::Vec3& end, - std::vector& hit_points) + int NavMeshLayer::Raycast(const a8::Vec3& start, const a8::Vec3& end, + std::vector& hit_points) { return 0; } + bool NavMeshLayer::AddObstacle(a8::Vec3& pos, const float radius, const float height, + ObstacleRef* obstacle_ref) + { + if (obstacle_ref) { + *obstacle_ref = 0; + } + float p[3]; + p[0] = pos.x; + p[1] = pos.y; + p[2] = pos.z; + dtStatus status = tile_cache_->addObstacle(p, radius, height, obstacle_ref); + return status == DT_SUCCESS || status == DT_IN_PROGRESS; + } + + bool NavMeshLayer::AddBoxObstacle(a8::Vec3& bmin, a8::Vec3& bmax, + ObstacleRef* obstacle_ref) + { + if (obstacle_ref) { + *obstacle_ref = 0; + } + float pmin[3]; + pmin[0] = bmin.x; + pmin[1] = bmin.y; + pmin[2] = bmin.z; + + float pmax[3]; + pmax[0] = bmax.x; + pmax[1] = bmax.y; + pmax[2] = bmax.z; + + dtStatus status = tile_cache_->addBoxObstacle(pmin, pmax, obstacle_ref); + return status == DT_SUCCESS || status == DT_IN_PROGRESS; + } + + bool NavMeshLayer::RemoveObstacle(const ObstacleRef& obstacle_ref) + { + dtStatus status = tile_cache_->removeObstacle(obstacle_ref); + return status == DT_SUCCESS || status == DT_IN_PROGRESS; + } + + void NavMeshLayer::WaitRebuildOk() + { + while (!Update()) {} + } + + bool NavMeshLayer::Update() + { + bool up_to_date = false; + tile_cache_->update(0, navmesh_, &up_to_date); + return up_to_date; + } + + NavMeshLayer* NavigationHandle::GetLayer(int layer) + { + auto itr = navmesh_layers_.find(layer); + return itr != navmesh_layers_.end() ? &itr->second : nullptr; + } + + void NavigationHandle::WaitRebuildOk() + { + for (auto& pair : navmesh_layers_) { + pair.second.WaitRebuildOk(); + } + } + + bool NavigationHandle::Update() + { + bool up_to_date = true; + for (auto& pair : navmesh_layers_) { + if (!pair.second.Update() && up_to_date) { + up_to_date = false; + } + } + return up_to_date; + } + } diff --git a/cpp/navigation_handle.h b/cpp/navigation_handle.h index f4c1ef2..01a8722 100644 --- a/cpp/navigation_handle.h +++ b/cpp/navigation_handle.h @@ -2,19 +2,51 @@ #include +#include "DetourTileCache.h" + +class dtTileCache; +class dtNavMesh; +class dtNavMeshQuery; namespace f8 { + + typedef dtObstacleRef ObstacleRef; + + class NavMeshLayer + { + public: + int FindStraightPath(const a8::Vec3& start, const a8::Vec3& end, + std::vector& path_list); + int FindRandomPointAroundCircle(const a8::Vec3& center_pos, + std::vector& points, int max_points, float max_radius); + int Raycast(const a8::Vec3& start, const a8::Vec3& end, + std::vector& hit_points); + + bool AddObstacle(a8::Vec3& pos, const float radius, const float height, ObstacleRef* obstacle_ref); + bool AddBoxObstacle(a8::Vec3& bmin, a8::Vec3& bmax, ObstacleRef* obstacle_ref); + bool RemoveObstacle(const ObstacleRef& obstacle_ref); + void WaitRebuildOk(); + bool Update(); + + private: + + dtNavMesh* navmesh_ = nullptr; + dtTileCache* tile_cache_ = nullptr; + dtNavMeshQuery* navmesh_query_ = nullptr; + }; + class NavigationHandle { public: NavigationHandle() {}; ~NavigationHandle() {}; - int FindStraightPath(int layer, const a8::Vec3& start, const a8::Vec3& end, - std::vector& path_list); - int FindRandomPointAroundCircle(int layer, const a8::Vec3& center_pos, - std::vector& points, int max_points, float max_radius); - int Raycast(int layer, const a8::Vec3& start, const a8::Vec3& end, - std::vector& hit_points); + NavMeshLayer* GetLayer(int layer); + void WaitRebuildOk(); + bool Update(); + + private: + std::map navmesh_layers_; }; + }