diff --git a/apps/openmw/mwmechanics/aipackage.cpp b/apps/openmw/mwmechanics/aipackage.cpp index 43f555d319..0b06d8c1dc 100644 --- a/apps/openmw/mwmechanics/aipackage.cpp +++ b/apps/openmw/mwmechanics/aipackage.cpp @@ -171,7 +171,9 @@ bool MWMechanics::AiPackage::pathTo(const MWWorld::Ptr& actor, const osg::Vec3f& mTimer = 0; } - if (isDestReached || mPathFinder.checkPathCompleted(pos.asVec3())) // if path is finished + mPathFinder.update(pos.asVec3(), std::max(destTolerance, DEFAULT_TOLERANCE)); + + if (isDestReached || mPathFinder.checkPathCompleted()) // if path is finished { // turn to destination point zTurn(actor, getZAngleToPoint(start, dest)); diff --git a/apps/openmw/mwmechanics/aiwander.cpp b/apps/openmw/mwmechanics/aiwander.cpp index 6ca23df955..4a452e1f70 100644 --- a/apps/openmw/mwmechanics/aiwander.cpp +++ b/apps/openmw/mwmechanics/aiwander.cpp @@ -250,7 +250,7 @@ namespace MWMechanics } } } - else if (storage.mIsWanderingManually && mPathFinder.checkPathCompleted(pos.asVec3(), DESTINATION_TOLERANCE)) + else if (storage.mIsWanderingManually && mPathFinder.checkPathCompleted()) { completeManualWalking(actor, storage); } diff --git a/apps/openmw/mwmechanics/pathfinding.cpp b/apps/openmw/mwmechanics/pathfinding.cpp index 23a468d21a..e0a83910f3 100644 --- a/apps/openmw/mwmechanics/pathfinding.cpp +++ b/apps/openmw/mwmechanics/pathfinding.cpp @@ -121,6 +121,7 @@ namespace MWMechanics void PathFinder::buildPath(const osg::Vec3f& startPoint, const osg::Vec3f& endPoint, const MWWorld::CellStore* cell, const PathgridGraph& pathgridGraph) { + mConstructed = true; mPath.clear(); mCell = cell; @@ -250,12 +251,10 @@ namespace MWMechanics return getXAngleToDir(dir); } - bool PathFinder::checkPathCompleted(const osg::Vec3f& position, const float tolerance) + void PathFinder::update(const osg::Vec3f& position, const float tolerance) { if (!mPath.empty() && sqrDistanceIgnoreZ(mPath.front(), position) < tolerance*tolerance) mPath.pop_front(); - - return mPath.empty(); } // see header for the rationale diff --git a/apps/openmw/mwmechanics/pathfinding.hpp b/apps/openmw/mwmechanics/pathfinding.hpp index 47d5640ad3..419b185852 100644 --- a/apps/openmw/mwmechanics/pathfinding.hpp +++ b/apps/openmw/mwmechanics/pathfinding.hpp @@ -57,12 +57,14 @@ namespace MWMechanics { public: PathFinder() - : mCell(nullptr) + : mConstructed(false) + , mCell(nullptr) { } void clearPath() { + mConstructed = false; mPath.clear(); mCell = nullptr; } @@ -70,8 +72,13 @@ namespace MWMechanics void buildPath(const osg::Vec3f& startPoint, const osg::Vec3f& endPoint, const MWWorld::CellStore* cell, const PathgridGraph& pathgridGraph); - bool checkPathCompleted(const osg::Vec3f& position, const float tolerance = DEFAULT_TOLERANCE); - ///< \Returns true if we are within \a tolerance units of the last path point. + /// Remove front point if exist and within tolerance + void update(const osg::Vec3f& position, const float tolerance = DEFAULT_TOLERANCE); + + bool checkPathCompleted() const + { + return mConstructed && mPath.empty(); + } /// In radians float getZAngleToNext(float x, float y) const; @@ -80,7 +87,7 @@ namespace MWMechanics bool isPathConstructed() const { - return !mPath.empty(); + return mConstructed && !mPath.empty(); } std::size_t getPathSize() const @@ -110,6 +117,7 @@ namespace MWMechanics void addPointToPath(const osg::Vec3f& point) { + mConstructed = true; mPath.push_back(point); } @@ -168,6 +176,7 @@ namespace MWMechanics } private: + bool mConstructed; std::deque mPath; const MWWorld::CellStore* mCell;