From f7caeefddbb84c98396e31fc8f703ab29eb86eae Mon Sep 17 00:00:00 2001 From: elsid Date: Tue, 28 Jan 2020 22:22:24 +0100 Subject: [PATCH] Fallback to straight path when navmesh and pathgrind are not available --- apps/openmw/mwmechanics/pathfinding.cpp | 9 ++++++--- apps/openmw/mwmechanics/pathfinding.hpp | 2 +- components/detournavigator/navigator.hpp | 4 ++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/apps/openmw/mwmechanics/pathfinding.cpp b/apps/openmw/mwmechanics/pathfinding.cpp index f7e7c277c..f8736c062 100644 --- a/apps/openmw/mwmechanics/pathfinding.cpp +++ b/apps/openmw/mwmechanics/pathfinding.cpp @@ -314,7 +314,9 @@ namespace MWMechanics { mPath.clear(); - buildPathByNavigatorImpl(actor, startPoint, endPoint, halfExtents, flags, std::back_inserter(mPath)); + // If it's not possible to build path over navmesh due to disabled navmesh generation fallback to straight path + if (!buildPathByNavigatorImpl(actor, startPoint, endPoint, halfExtents, flags, std::back_inserter(mPath))) + mPath.push_back(endPoint); mConstructed = true; } @@ -335,7 +337,7 @@ namespace MWMechanics mConstructed = true; } - void PathFinder::buildPathByNavigatorImpl(const MWWorld::ConstPtr& actor, const osg::Vec3f& startPoint, + bool PathFinder::buildPathByNavigatorImpl(const MWWorld::ConstPtr& actor, const osg::Vec3f& startPoint, const osg::Vec3f& endPoint, const osg::Vec3f& halfExtents, const DetourNavigator::Flags flags, std::back_insert_iterator> out) { @@ -344,7 +346,7 @@ namespace MWMechanics const auto world = MWBase::Environment::get().getWorld(); const auto stepSize = getPathStepSize(actor); const auto navigator = world->getNavigator(); - navigator->findPath(halfExtents, stepSize, startPoint, endPoint, flags, out); + return navigator->findPath(halfExtents, stepSize, startPoint, endPoint, flags, out).is_initialized(); } catch (const DetourNavigator::NavigatorException& exception) { @@ -352,6 +354,7 @@ namespace MWMechanics << "\" for \"" << actor.getClass().getName(actor) << "\" (" << actor.getBase() << ") from " << startPoint << " to " << endPoint << " with flags (" << DetourNavigator::WriteFlags {flags} << ")"; + return true; } } diff --git a/apps/openmw/mwmechanics/pathfinding.hpp b/apps/openmw/mwmechanics/pathfinding.hpp index b413810f4..06b4aa10d 100644 --- a/apps/openmw/mwmechanics/pathfinding.hpp +++ b/apps/openmw/mwmechanics/pathfinding.hpp @@ -201,7 +201,7 @@ namespace MWMechanics void buildPathByPathgridImpl(const osg::Vec3f& startPoint, const osg::Vec3f& endPoint, const PathgridGraph& pathgridGraph, std::back_insert_iterator> out); - void buildPathByNavigatorImpl(const MWWorld::ConstPtr& actor, const osg::Vec3f& startPoint, + bool buildPathByNavigatorImpl(const MWWorld::ConstPtr& actor, const osg::Vec3f& startPoint, const osg::Vec3f& endPoint, const osg::Vec3f& halfExtents, const DetourNavigator::Flags flags, std::back_insert_iterator> out); }; diff --git a/components/detournavigator/navigator.hpp b/components/detournavigator/navigator.hpp index 74daab5d7..1b7af7e8b 100644 --- a/components/detournavigator/navigator.hpp +++ b/components/detournavigator/navigator.hpp @@ -159,7 +159,7 @@ namespace DetourNavigator * Equal to out if no path is found. */ template - OutputIterator findPath(const osg::Vec3f& agentHalfExtents, const float stepSize, const osg::Vec3f& start, + boost::optional findPath(const osg::Vec3f& agentHalfExtents, const float stepSize, const osg::Vec3f& start, const osg::Vec3f& end, const Flags includeFlags, OutputIterator out) const { static_assert( @@ -171,7 +171,7 @@ namespace DetourNavigator ); const auto navMesh = getNavMesh(agentHalfExtents); if (!navMesh) - return out; + return {}; const auto settings = getSettings(); return findSmoothPath(navMesh->lockConst()->getImpl(), toNavMeshCoordinates(settings, agentHalfExtents), toNavMeshCoordinates(settings, stepSize), toNavMeshCoordinates(settings, start),