From d3ab6c972fe2876e889ddabba81d38884e3526ad Mon Sep 17 00:00:00 2001 From: elsid Date: Sun, 14 Feb 2021 01:00:43 +0100 Subject: [PATCH 1/6] Avoid set unused position from dtNavMeshQuery::findNearestPoly result --- components/detournavigator/findrandompointaroundcircle.cpp | 3 +-- components/detournavigator/findsmoothpath.hpp | 6 ++---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/components/detournavigator/findrandompointaroundcircle.cpp b/components/detournavigator/findrandompointaroundcircle.cpp index f2e815c918..444050ed7c 100644 --- a/components/detournavigator/findrandompointaroundcircle.cpp +++ b/components/detournavigator/findrandompointaroundcircle.cpp @@ -21,11 +21,10 @@ namespace DetourNavigator queryFilter.setIncludeFlags(includeFlags); dtPolyRef startRef = 0; - osg::Vec3f startPolygonPosition; for (int i = 0; i < 3; ++i) { const auto status = navMeshQuery.findNearestPoly(start.ptr(), (halfExtents * (1 << i)).ptr(), &queryFilter, - &startRef, startPolygonPosition.ptr()); + &startRef, nullptr); if (!dtStatusFailed(status) && startRef != 0) break; } diff --git a/components/detournavigator/findsmoothpath.hpp b/components/detournavigator/findsmoothpath.hpp index a351f8279c..f757a511f4 100644 --- a/components/detournavigator/findsmoothpath.hpp +++ b/components/detournavigator/findsmoothpath.hpp @@ -283,11 +283,10 @@ namespace DetourNavigator queryFilter.setAreaCost(AreaType_ground, areaCosts.mGround); dtPolyRef startRef = 0; - osg::Vec3f startPolygonPosition; for (int i = 0; i < 3; ++i) { const auto status = navMeshQuery.findNearestPoly(start.ptr(), (halfExtents * (1 << i)).ptr(), &queryFilter, - &startRef, startPolygonPosition.ptr()); + &startRef, nullptr); if (!dtStatusFailed(status) && startRef != 0) break; } @@ -296,11 +295,10 @@ namespace DetourNavigator return Status::StartPolygonNotFound; dtPolyRef endRef = 0; - osg::Vec3f endPolygonPosition; for (int i = 0; i < 3; ++i) { const auto status = navMeshQuery.findNearestPoly(end.ptr(), (halfExtents * (1 << i)).ptr(), &queryFilter, - &endRef, endPolygonPosition.ptr()); + &endRef, nullptr); if (!dtStatusFailed(status) && endRef != 0) break; } From a7fe6c7ba16ea16775d9324fa00cc346f07c4959 Mon Sep 17 00:00:00 2001 From: elsid Date: Sun, 14 Feb 2021 01:08:14 +0100 Subject: [PATCH 2/6] Move duplicated usage patter of dtNavMeshQuery::findNearestPoly into a separate function --- .../findrandompointaroundcircle.cpp | 10 +------- components/detournavigator/findsmoothpath.cpp | 13 +++++++++++ components/detournavigator/findsmoothpath.hpp | 23 ++++--------------- 3 files changed, 19 insertions(+), 27 deletions(-) diff --git a/components/detournavigator/findrandompointaroundcircle.cpp b/components/detournavigator/findrandompointaroundcircle.cpp index 444050ed7c..29d4e9da44 100644 --- a/components/detournavigator/findrandompointaroundcircle.cpp +++ b/components/detournavigator/findrandompointaroundcircle.cpp @@ -20,15 +20,7 @@ namespace DetourNavigator dtQueryFilter queryFilter; queryFilter.setIncludeFlags(includeFlags); - dtPolyRef startRef = 0; - for (int i = 0; i < 3; ++i) - { - const auto status = navMeshQuery.findNearestPoly(start.ptr(), (halfExtents * (1 << i)).ptr(), &queryFilter, - &startRef, nullptr); - if (!dtStatusFailed(status) && startRef != 0) - break; - } - + dtPolyRef startRef = findNearestPolyExpanding(navMeshQuery, queryFilter, start, halfExtents); if (startRef == 0) return std::optional(); diff --git a/components/detournavigator/findsmoothpath.cpp b/components/detournavigator/findsmoothpath.cpp index a13b83abd8..84ccf34a44 100644 --- a/components/detournavigator/findsmoothpath.cpp +++ b/components/detournavigator/findsmoothpath.cpp @@ -140,4 +140,17 @@ namespace DetourNavigator return result; } + + dtPolyRef findNearestPolyExpanding(const dtNavMeshQuery& query, const dtQueryFilter& filter, + const osg::Vec3f& center, const osg::Vec3f& halfExtents) + { + dtPolyRef ref = 0; + for (int i = 0; i < 3; ++i) + { + const dtStatus status = query.findNearestPoly(center.ptr(), (halfExtents * (1 << i)).ptr(), &filter, &ref, nullptr); + if (!dtStatusFailed(status) && ref != 0) + break; + } + return ref; + } } diff --git a/components/detournavigator/findsmoothpath.hpp b/components/detournavigator/findsmoothpath.hpp index f757a511f4..3dc4999740 100644 --- a/components/detournavigator/findsmoothpath.hpp +++ b/components/detournavigator/findsmoothpath.hpp @@ -103,6 +103,9 @@ namespace DetourNavigator return dtStatusSucceed(status); } + dtPolyRef findNearestPolyExpanding(const dtNavMeshQuery& query, const dtQueryFilter& filter, + const osg::Vec3f& center, const osg::Vec3f& halfExtents); + struct MoveAlongSurfaceResult { osg::Vec3f mResultPos; @@ -282,27 +285,11 @@ namespace DetourNavigator queryFilter.setAreaCost(AreaType_pathgrid, areaCosts.mPathgrid); queryFilter.setAreaCost(AreaType_ground, areaCosts.mGround); - dtPolyRef startRef = 0; - for (int i = 0; i < 3; ++i) - { - const auto status = navMeshQuery.findNearestPoly(start.ptr(), (halfExtents * (1 << i)).ptr(), &queryFilter, - &startRef, nullptr); - if (!dtStatusFailed(status) && startRef != 0) - break; - } - + dtPolyRef startRef = findNearestPolyExpanding(navMeshQuery, queryFilter, start, halfExtents); if (startRef == 0) return Status::StartPolygonNotFound; - dtPolyRef endRef = 0; - for (int i = 0; i < 3; ++i) - { - const auto status = navMeshQuery.findNearestPoly(end.ptr(), (halfExtents * (1 << i)).ptr(), &queryFilter, - &endRef, nullptr); - if (!dtStatusFailed(status) && endRef != 0) - break; - } - + dtPolyRef endRef = findNearestPolyExpanding(navMeshQuery, queryFilter, end, halfExtents); if (endRef == 0) return Status::EndPolygonNotFound; From 3a9b1ce63a365d8e759f51b4e156303728c79cd5 Mon Sep 17 00:00:00 2001 From: elsid Date: Sun, 14 Feb 2021 23:11:57 +0100 Subject: [PATCH 3/6] Use camel case for local constant --- components/detournavigator/findsmoothpath.cpp | 10 +++++----- components/detournavigator/findsmoothpath.hpp | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/components/detournavigator/findsmoothpath.cpp b/components/detournavigator/findsmoothpath.cpp index 84ccf34a44..8974be5321 100644 --- a/components/detournavigator/findsmoothpath.cpp +++ b/components/detournavigator/findsmoothpath.cpp @@ -108,13 +108,13 @@ namespace DetourNavigator { // Find steer target. SteerTarget result; - const int MAX_STEER_POINTS = 3; - std::array steerPath; - std::array steerPathFlags; - std::array steerPathPolys; + constexpr int maxSteerPoints = 3; + std::array steerPath; + std::array steerPathFlags; + std::array steerPathPolys; int nsteerPath = 0; navQuery.findStraightPath(startPos.ptr(), endPos.ptr(), path.data(), int(path.size()), steerPath.data(), - steerPathFlags.data(), steerPathPolys.data(), &nsteerPath, MAX_STEER_POINTS); + steerPathFlags.data(), steerPathPolys.data(), &nsteerPath, maxSteerPoints); assert(nsteerPath >= 0); if (!nsteerPath) return std::nullopt; diff --git a/components/detournavigator/findsmoothpath.hpp b/components/detournavigator/findsmoothpath.hpp index 3dc4999740..29a3ce8050 100644 --- a/components/detournavigator/findsmoothpath.hpp +++ b/components/detournavigator/findsmoothpath.hpp @@ -166,7 +166,7 @@ namespace DetourNavigator osg::Vec3f targetPos; navMeshQuery.closestPointOnPoly(polygonPath.back(), end.ptr(), targetPos.ptr(), nullptr); - const float SLOP = 0.01f; + constexpr float slop = 0.01f; *out++ = iterPos; @@ -177,7 +177,7 @@ namespace DetourNavigator while (!polygonPath.empty() && smoothPathSize < maxSmoothPathSize) { // Find location to steer towards. - const auto steerTarget = getSteerTarget(navMeshQuery, iterPos, targetPos, SLOP, polygonPath); + const auto steerTarget = getSteerTarget(navMeshQuery, iterPos, targetPos, slop, polygonPath); if (!steerTarget) break; @@ -209,7 +209,7 @@ namespace DetourNavigator iterPos.y() = h; // Handle end of path and off-mesh links when close enough. - if (endOfPath && inRange(iterPos, steerTarget->steerPos, SLOP, 1.0f)) + if (endOfPath && inRange(iterPos, steerTarget->steerPos, slop, 1.0f)) { // Reached end of path. iterPos = targetPos; @@ -217,7 +217,7 @@ namespace DetourNavigator ++smoothPathSize; break; } - else if (offMeshConnection && inRange(iterPos, steerTarget->steerPos, SLOP, 1.0f)) + else if (offMeshConnection && inRange(iterPos, steerTarget->steerPos, slop, 1.0f)) { // Advance the path up to and over the off-mesh connection. dtPolyRef prevRef = 0; From bb0c4789540c4da23da1975473df288bb3d2d7a1 Mon Sep 17 00:00:00 2001 From: elsid Date: Mon, 15 Feb 2021 00:01:34 +0100 Subject: [PATCH 4/6] Add missing include and use std malloc and free --- components/detournavigator/recastglobalallocator.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/components/detournavigator/recastglobalallocator.hpp b/components/detournavigator/recastglobalallocator.hpp index 7c4b2c5343..313d311009 100644 --- a/components/detournavigator/recastglobalallocator.hpp +++ b/components/detournavigator/recastglobalallocator.hpp @@ -3,6 +3,8 @@ #include "recasttempallocator.hpp" +#include + namespace DetourNavigator { class RecastGlobalAllocator @@ -32,7 +34,7 @@ namespace DetourNavigator else { assert(BufferType_perm == getDataPtrBufferType(ptr)); - ::free(getPermDataPtrHeapPtr(ptr)); + std::free(getPermDataPtrHeapPtr(ptr)); } } @@ -56,7 +58,7 @@ namespace DetourNavigator static void* allocPerm(size_t size) { - const auto ptr = ::malloc(size + sizeof(std::size_t)); + const auto ptr = std::malloc(size + sizeof(std::size_t)); if (rcUnlikely(!ptr)) return ptr; setPermPtrBufferType(ptr, BufferType_perm); From 4983684fda3266ca3308a13b5e7668fd63f24f0f Mon Sep 17 00:00:00 2001 From: elsid Date: Mon, 15 Feb 2021 00:07:20 +0100 Subject: [PATCH 5/6] Fix implicit int to float conversion warning --- components/detournavigator/settingsutils.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/detournavigator/settingsutils.hpp b/components/detournavigator/settingsutils.hpp index a22205b2aa..39ffc03d19 100644 --- a/components/detournavigator/settingsutils.hpp +++ b/components/detournavigator/settingsutils.hpp @@ -52,7 +52,7 @@ namespace DetourNavigator inline float getTileSize(const Settings& settings) { - return settings.mTileSize * settings.mCellSize; + return static_cast(settings.mTileSize) * settings.mCellSize; } inline TilePosition getTilePosition(const Settings& settings, const osg::Vec3f& position) @@ -73,7 +73,7 @@ namespace DetourNavigator inline float getBorderSize(const Settings& settings) { - return settings.mBorderSize * settings.mCellSize; + return static_cast(settings.mBorderSize) * settings.mCellSize; } inline float getSwimLevel(const Settings& settings, const float agentHalfExtentsZ) From bc67669a970df2e58e3571f2b9fa4d8050434090 Mon Sep 17 00:00:00 2001 From: elsid Date: Mon, 15 Feb 2021 00:22:48 +0100 Subject: [PATCH 6/6] Comment unused argument --- components/detournavigator/navigatorstub.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/detournavigator/navigatorstub.hpp b/components/detournavigator/navigatorstub.hpp index 8b81bde197..f1f9e06ef3 100644 --- a/components/detournavigator/navigatorstub.hpp +++ b/components/detournavigator/navigatorstub.hpp @@ -66,7 +66,7 @@ namespace DetourNavigator void update(const osg::Vec3f& /*playerPosition*/) override {} - void setUpdatesEnabled(bool enabled) override {} + void setUpdatesEnabled(bool /*enabled*/) override {} void wait() override {}