diff --git a/components/detournavigator/findrandompointaroundcircle.cpp b/components/detournavigator/findrandompointaroundcircle.cpp index f2e815c918..29d4e9da44 100644 --- a/components/detournavigator/findrandompointaroundcircle.cpp +++ b/components/detournavigator/findrandompointaroundcircle.cpp @@ -20,16 +20,7 @@ namespace DetourNavigator dtQueryFilter queryFilter; 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()); - 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..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; @@ -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 a351f8279c..29a3ce8050 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; @@ -163,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; @@ -174,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; @@ -206,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; @@ -214,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; @@ -282,29 +285,11 @@ namespace DetourNavigator queryFilter.setAreaCost(AreaType_pathgrid, areaCosts.mPathgrid); 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()); - if (!dtStatusFailed(status) && startRef != 0) - break; - } - + dtPolyRef startRef = findNearestPolyExpanding(navMeshQuery, queryFilter, start, halfExtents); if (startRef == 0) 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()); - if (!dtStatusFailed(status) && endRef != 0) - break; - } - + dtPolyRef endRef = findNearestPolyExpanding(navMeshQuery, queryFilter, end, halfExtents); if (endRef == 0) return Status::EndPolygonNotFound; 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 {} 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); 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)