1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-27 03:35:27 +00:00

Update and check for complete Pathfinder path by different methods

This commit is contained in:
elsid 2018-08-20 01:26:58 +03:00
parent b6dd2119a6
commit 4fe764c3a5
No known key found for this signature in database
GPG Key ID: B845CB9FEE18AB40
4 changed files with 19 additions and 9 deletions

View File

@ -171,7 +171,9 @@ bool MWMechanics::AiPackage::pathTo(const MWWorld::Ptr& actor, const osg::Vec3f&
mTimer = 0; 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 // turn to destination point
zTurn(actor, getZAngleToPoint(start, dest)); zTurn(actor, getZAngleToPoint(start, dest));

View File

@ -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); completeManualWalking(actor, storage);
} }

View File

@ -121,6 +121,7 @@ namespace MWMechanics
void PathFinder::buildPath(const osg::Vec3f& startPoint, const osg::Vec3f& endPoint, void PathFinder::buildPath(const osg::Vec3f& startPoint, const osg::Vec3f& endPoint,
const MWWorld::CellStore* cell, const PathgridGraph& pathgridGraph) const MWWorld::CellStore* cell, const PathgridGraph& pathgridGraph)
{ {
mConstructed = true;
mPath.clear(); mPath.clear();
mCell = cell; mCell = cell;
@ -250,12 +251,10 @@ namespace MWMechanics
return getXAngleToDir(dir); 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) if (!mPath.empty() && sqrDistanceIgnoreZ(mPath.front(), position) < tolerance*tolerance)
mPath.pop_front(); mPath.pop_front();
return mPath.empty();
} }
// see header for the rationale // see header for the rationale

View File

@ -57,12 +57,14 @@ namespace MWMechanics
{ {
public: public:
PathFinder() PathFinder()
: mCell(nullptr) : mConstructed(false)
, mCell(nullptr)
{ {
} }
void clearPath() void clearPath()
{ {
mConstructed = false;
mPath.clear(); mPath.clear();
mCell = nullptr; mCell = nullptr;
} }
@ -70,8 +72,13 @@ namespace MWMechanics
void buildPath(const osg::Vec3f& startPoint, const osg::Vec3f& endPoint, void buildPath(const osg::Vec3f& startPoint, const osg::Vec3f& endPoint,
const MWWorld::CellStore* cell, const PathgridGraph& pathgridGraph); const MWWorld::CellStore* cell, const PathgridGraph& pathgridGraph);
bool checkPathCompleted(const osg::Vec3f& position, const float tolerance = DEFAULT_TOLERANCE); /// Remove front point if exist and within tolerance
///< \Returns true if we are within \a tolerance units of the last path point. void update(const osg::Vec3f& position, const float tolerance = DEFAULT_TOLERANCE);
bool checkPathCompleted() const
{
return mConstructed && mPath.empty();
}
/// In radians /// In radians
float getZAngleToNext(float x, float y) const; float getZAngleToNext(float x, float y) const;
@ -80,7 +87,7 @@ namespace MWMechanics
bool isPathConstructed() const bool isPathConstructed() const
{ {
return !mPath.empty(); return mConstructed && !mPath.empty();
} }
std::size_t getPathSize() const std::size_t getPathSize() const
@ -110,6 +117,7 @@ namespace MWMechanics
void addPointToPath(const osg::Vec3f& point) void addPointToPath(const osg::Vec3f& point)
{ {
mConstructed = true;
mPath.push_back(point); mPath.push_back(point);
} }
@ -168,6 +176,7 @@ namespace MWMechanics
} }
private: private:
bool mConstructed;
std::deque<osg::Vec3f> mPath; std::deque<osg::Vec3f> mPath;
const MWWorld::CellStore* mCell; const MWWorld::CellStore* mCell;