Merge branch 'navigator_cleanup' into 'master'

Navigator cleanup

See merge request OpenMW/openmw!612
pull/3047/head
psi29a 3 years ago
commit de28a89a43

@ -20,16 +20,7 @@ namespace DetourNavigator
dtQueryFilter queryFilter; dtQueryFilter queryFilter;
queryFilter.setIncludeFlags(includeFlags); queryFilter.setIncludeFlags(includeFlags);
dtPolyRef startRef = 0; dtPolyRef startRef = findNearestPolyExpanding(navMeshQuery, queryFilter, start, halfExtents);
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;
}
if (startRef == 0) if (startRef == 0)
return std::optional<osg::Vec3f>(); return std::optional<osg::Vec3f>();

@ -108,13 +108,13 @@ namespace DetourNavigator
{ {
// Find steer target. // Find steer target.
SteerTarget result; SteerTarget result;
const int MAX_STEER_POINTS = 3; constexpr int maxSteerPoints = 3;
std::array<float, MAX_STEER_POINTS * 3> steerPath; std::array<float, maxSteerPoints * 3> steerPath;
std::array<unsigned char, MAX_STEER_POINTS> steerPathFlags; std::array<unsigned char, maxSteerPoints> steerPathFlags;
std::array<dtPolyRef, MAX_STEER_POINTS> steerPathPolys; std::array<dtPolyRef, maxSteerPoints> steerPathPolys;
int nsteerPath = 0; int nsteerPath = 0;
navQuery.findStraightPath(startPos.ptr(), endPos.ptr(), path.data(), int(path.size()), steerPath.data(), 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); assert(nsteerPath >= 0);
if (!nsteerPath) if (!nsteerPath)
return std::nullopt; return std::nullopt;
@ -140,4 +140,17 @@ namespace DetourNavigator
return result; 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;
}
} }

@ -103,6 +103,9 @@ namespace DetourNavigator
return dtStatusSucceed(status); return dtStatusSucceed(status);
} }
dtPolyRef findNearestPolyExpanding(const dtNavMeshQuery& query, const dtQueryFilter& filter,
const osg::Vec3f& center, const osg::Vec3f& halfExtents);
struct MoveAlongSurfaceResult struct MoveAlongSurfaceResult
{ {
osg::Vec3f mResultPos; osg::Vec3f mResultPos;
@ -163,7 +166,7 @@ namespace DetourNavigator
osg::Vec3f targetPos; osg::Vec3f targetPos;
navMeshQuery.closestPointOnPoly(polygonPath.back(), end.ptr(), targetPos.ptr(), nullptr); navMeshQuery.closestPointOnPoly(polygonPath.back(), end.ptr(), targetPos.ptr(), nullptr);
const float SLOP = 0.01f; constexpr float slop = 0.01f;
*out++ = iterPos; *out++ = iterPos;
@ -174,7 +177,7 @@ namespace DetourNavigator
while (!polygonPath.empty() && smoothPathSize < maxSmoothPathSize) while (!polygonPath.empty() && smoothPathSize < maxSmoothPathSize)
{ {
// Find location to steer towards. // 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) if (!steerTarget)
break; break;
@ -206,7 +209,7 @@ namespace DetourNavigator
iterPos.y() = h; iterPos.y() = h;
// Handle end of path and off-mesh links when close enough. // 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. // Reached end of path.
iterPos = targetPos; iterPos = targetPos;
@ -214,7 +217,7 @@ namespace DetourNavigator
++smoothPathSize; ++smoothPathSize;
break; 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. // Advance the path up to and over the off-mesh connection.
dtPolyRef prevRef = 0; dtPolyRef prevRef = 0;
@ -282,29 +285,11 @@ namespace DetourNavigator
queryFilter.setAreaCost(AreaType_pathgrid, areaCosts.mPathgrid); queryFilter.setAreaCost(AreaType_pathgrid, areaCosts.mPathgrid);
queryFilter.setAreaCost(AreaType_ground, areaCosts.mGround); queryFilter.setAreaCost(AreaType_ground, areaCosts.mGround);
dtPolyRef startRef = 0; dtPolyRef startRef = findNearestPolyExpanding(navMeshQuery, queryFilter, start, halfExtents);
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;
}
if (startRef == 0) if (startRef == 0)
return Status::StartPolygonNotFound; return Status::StartPolygonNotFound;
dtPolyRef endRef = 0; dtPolyRef endRef = findNearestPolyExpanding(navMeshQuery, queryFilter, end, halfExtents);
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;
}
if (endRef == 0) if (endRef == 0)
return Status::EndPolygonNotFound; return Status::EndPolygonNotFound;

@ -66,7 +66,7 @@ namespace DetourNavigator
void update(const osg::Vec3f& /*playerPosition*/) override {} void update(const osg::Vec3f& /*playerPosition*/) override {}
void setUpdatesEnabled(bool enabled) override {} void setUpdatesEnabled(bool /*enabled*/) override {}
void wait() override {} void wait() override {}

@ -3,6 +3,8 @@
#include "recasttempallocator.hpp" #include "recasttempallocator.hpp"
#include <cstdlib>
namespace DetourNavigator namespace DetourNavigator
{ {
class RecastGlobalAllocator class RecastGlobalAllocator
@ -32,7 +34,7 @@ namespace DetourNavigator
else else
{ {
assert(BufferType_perm == getDataPtrBufferType(ptr)); assert(BufferType_perm == getDataPtrBufferType(ptr));
::free(getPermDataPtrHeapPtr(ptr)); std::free(getPermDataPtrHeapPtr(ptr));
} }
} }
@ -56,7 +58,7 @@ namespace DetourNavigator
static void* allocPerm(size_t size) 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)) if (rcUnlikely(!ptr))
return ptr; return ptr;
setPermPtrBufferType(ptr, BufferType_perm); setPermPtrBufferType(ptr, BufferType_perm);

@ -52,7 +52,7 @@ namespace DetourNavigator
inline float getTileSize(const Settings& settings) inline float getTileSize(const Settings& settings)
{ {
return settings.mTileSize * settings.mCellSize; return static_cast<float>(settings.mTileSize) * settings.mCellSize;
} }
inline TilePosition getTilePosition(const Settings& settings, const osg::Vec3f& position) inline TilePosition getTilePosition(const Settings& settings, const osg::Vec3f& position)
@ -73,7 +73,7 @@ namespace DetourNavigator
inline float getBorderSize(const Settings& settings) inline float getBorderSize(const Settings& settings)
{ {
return settings.mBorderSize * settings.mCellSize; return static_cast<float>(settings.mBorderSize) * settings.mCellSize;
} }
inline float getSwimLevel(const Settings& settings, const float agentHalfExtentsZ) inline float getSwimLevel(const Settings& settings, const float agentHalfExtentsZ)

Loading…
Cancel
Save