1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-30 21:32:42 +00:00
OpenMW/apps/openmw/mwmechanics/pathfinding.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

219 lines
7.9 KiB
C++
Raw Normal View History

2013-03-31 17:30:03 +00:00
#ifndef GAME_MWMECHANICS_PATHFINDING_H
#define GAME_MWMECHANICS_PATHFINDING_H
#include <cassert>
2018-08-18 13:40:04 +03:00
#include <deque>
2018-08-23 00:20:25 +03:00
#include <iterator>
#include <components/detournavigator/areatype.hpp>
#include <components/detournavigator/flags.hpp>
#include <components/detournavigator/status.hpp>
#include <components/esm/position.hpp>
#include <components/esm3/loadpgrd.hpp>
namespace MWWorld
{
class CellStore;
class ConstPtr;
2019-10-19 12:40:34 +02:00
class Ptr;
}
2013-03-31 17:30:03 +00:00
namespace DetourNavigator
{
struct AgentBounds;
}
2013-03-31 17:30:03 +00:00
namespace MWMechanics
{
class PathgridGraph;
2019-10-19 12:40:34 +02:00
template <class T>
inline float distance(const T& lhs, const T& rhs)
{
2019-10-19 12:40:34 +02:00
static_assert(std::is_same<T, osg::Vec2f>::value || std::is_same<T, osg::Vec3f>::value, "T is not a position");
return (lhs - rhs).length();
}
2019-10-19 12:40:34 +02:00
inline float distanceIgnoreZ(const osg::Vec3f& lhs, const osg::Vec3f& rhs)
{
return distance(osg::Vec2f(lhs.x(), lhs.y()), osg::Vec2f(rhs.x(), rhs.y()));
}
float getPathDistance(const MWWorld::Ptr& actor, const osg::Vec3f& lhs, const osg::Vec3f& rhs);
inline float getZAngleToDir(const osg::Vec3f& dir)
{
return std::atan2(dir.x(), dir.y());
}
inline float getXAngleToDir(const osg::Vec3f& dir)
{
float dirLen = dir.length();
return (dirLen != 0) ? -std::asin(dir.z() / dirLen) : 0;
}
inline float getZAngleToPoint(const osg::Vec3f& origin, const osg::Vec3f& dest)
{
return getZAngleToDir(dest - origin);
}
inline float getXAngleToPoint(const osg::Vec3f& origin, const osg::Vec3f& dest)
{
return getXAngleToDir(dest - origin);
}
2018-08-18 13:43:29 +03:00
const float PATHFIND_Z_REACH = 50.0f;
// distance after which actor (failed previously to shortcut) will try again
const float PATHFIND_SHORTCUT_RETRY_DIST = 300.0f;
const float MIN_TOLERANCE = 1.0f;
2018-08-18 23:46:06 +03:00
const float DEFAULT_TOLERANCE = 32.0f;
// cast up-down ray with some offset from actor position to check for pits/obstacles on the way to target;
// magnitude of pits/obstacles is defined by PATHFIND_Z_REACH
bool checkWayIsClear(const osg::Vec3f& from, const osg::Vec3f& to, float offsetXY);
enum class PathType
{
Full,
Partial,
};
2013-03-31 17:30:03 +00:00
class PathFinder
{
public:
using UpdateFlags = unsigned;
enum UpdateFlag : UpdateFlags
2018-08-20 01:13:59 +03:00
{
UpdateFlag_CanMoveByZ = 1 << 0,
UpdateFlag_ShortenIfAlmostStraight = 1 << 1,
UpdateFlag_RemoveLoops = 1 << 2,
};
PathFinder() = default;
2022-09-22 21:26:05 +03:00
2018-08-20 01:13:16 +03:00
void clearPath()
2022-09-22 21:26:05 +03:00
{
mConstructed = false;
2018-08-20 01:13:16 +03:00
mPath.clear();
mCell = nullptr;
2022-09-22 21:26:05 +03:00
}
void buildStraightPath(const osg::Vec3f& endPoint);
2022-09-22 21:26:05 +03:00
2018-08-23 00:20:25 +03:00
void buildPathByPathgrid(const osg::Vec3f& startPoint, const osg::Vec3f& endPoint,
const MWWorld::CellStore* cell, const PathgridGraph& pathgridGraph);
2022-09-22 21:26:05 +03:00
void buildPathByNavMesh(const MWWorld::ConstPtr& actor, const osg::Vec3f& startPoint,
const osg::Vec3f& endPoint, const DetourNavigator::AgentBounds& agentBounds,
const DetourNavigator::Flags flags, const DetourNavigator::AreaCosts& areaCosts, float endTolerance,
PathType pathType);
2022-09-22 21:26:05 +03:00
void buildPath(const MWWorld::ConstPtr& actor, const osg::Vec3f& startPoint, const osg::Vec3f& endPoint,
const MWWorld::CellStore* cell, const PathgridGraph& pathgridGraph,
const DetourNavigator::AgentBounds& agentBounds, const DetourNavigator::Flags flags,
const DetourNavigator::AreaCosts& areaCosts, float endTolerance, PathType pathType);
2022-09-22 21:26:05 +03:00
void buildLimitedPath(const MWWorld::ConstPtr& actor, const osg::Vec3f& startPoint, const osg::Vec3f& endPoint,
const MWWorld::CellStore* cell, const PathgridGraph& pathgridGraph,
const DetourNavigator::AgentBounds& agentBounds, const DetourNavigator::Flags flags,
const DetourNavigator::AreaCosts& areaCosts, float endTolerance, PathType pathType);
2022-09-22 21:26:05 +03:00
/// Remove front point if exist and within tolerance
void update(const osg::Vec3f& position, float pointTolerance, float destinationTolerance,
UpdateFlags updateFlags, const DetourNavigator::AgentBounds& agentBounds, DetourNavigator::Flags pathFlags);
2022-09-22 21:26:05 +03:00
bool checkPathCompleted() const { return mConstructed && mPath.empty(); }
2022-09-22 21:26:05 +03:00
/// In radians
2013-08-29 19:17:27 -07:00
float getZAngleToNext(float x, float y) const;
2022-09-22 21:26:05 +03:00
float getXAngleToNext(float x, float y, float z) const;
2022-09-22 21:26:05 +03:00
bool isPathConstructed() const { return mConstructed && !mPath.empty(); }
2022-09-22 21:26:05 +03:00
2018-08-18 13:35:37 +03:00
std::size_t getPathSize() const { return mPath.size(); }
2022-09-22 21:26:05 +03:00
2018-08-18 13:40:04 +03:00
const std::deque<osg::Vec3f>& getPath() const { return mPath; }
2022-09-22 21:26:05 +03:00
2018-08-20 01:08:14 +03:00
const MWWorld::CellStore* getPathCell() const { return mCell; }
2022-09-22 21:26:05 +03:00
void addPointToPath(const osg::Vec3f& point)
2022-09-22 21:26:05 +03:00
{
mConstructed = true;
2014-01-07 21:10:57 +01:00
mPath.push_back(point);
2022-09-22 21:26:05 +03:00
}
2015-06-03 19:41:19 +02:00
/// utility function to convert a osg::Vec3f to a Pathgrid::Point
2018-08-18 13:42:11 +03:00
static ESM::Pathgrid::Point makePathgridPoint(const osg::Vec3f& v)
2022-09-22 21:26:05 +03:00
{
return ESM::Pathgrid::Point(static_cast<int>(v[0]), static_cast<int>(v[1]), static_cast<int>(v[2]));
2022-09-22 21:26:05 +03:00
}
/// utility function to convert an ESM::Position to a Pathgrid::Point
2018-08-18 13:42:11 +03:00
static ESM::Pathgrid::Point makePathgridPoint(const ESM::Position& p)
2022-09-22 21:26:05 +03:00
{
return ESM::Pathgrid::Point(
static_cast<int>(p.pos[0]), static_cast<int>(p.pos[1]), static_cast<int>(p.pos[2]));
2022-09-22 21:26:05 +03:00
}
2018-08-18 13:42:11 +03:00
static osg::Vec3f makeOsgVec3(const ESM::Pathgrid::Point& p)
2022-09-22 21:26:05 +03:00
{
2015-05-02 22:45:27 +02:00
return osg::Vec3f(static_cast<float>(p.mX), static_cast<float>(p.mY), static_cast<float>(p.mZ));
2022-09-22 21:26:05 +03:00
}
// Slightly cheaper version for comparisons.
// Caller needs to be careful for very short distances (i.e. less than 1)
// or when accumuating the results i.e. (a + b)^2 != a^2 + b^2
2022-09-22 21:26:05 +03:00
//
2021-06-23 23:13:59 +02:00
static float distanceSquared(const ESM::Pathgrid::Point& point, const osg::Vec3f& pos)
2022-09-22 21:26:05 +03:00
{
2018-08-18 13:42:11 +03:00
return (MWMechanics::PathFinder::makeOsgVec3(point) - pos).length2();
2022-09-22 21:26:05 +03:00
}
2016-12-14 22:11:22 +01:00
// Return the closest pathgrid point index from the specified position
// coordinates. NOTE: Does not check if there is a sensible way to get there
// (e.g. a cliff in front).
2022-09-22 21:26:05 +03:00
//
2016-12-14 22:11:22 +01:00
// NOTE: pos is expected to be in local coordinates, as is grid->mPoints
2022-09-22 21:26:05 +03:00
//
2018-08-18 13:42:11 +03:00
static int getClosestPoint(const ESM::Pathgrid* grid, const osg::Vec3f& pos)
2022-09-22 21:26:05 +03:00
{
assert(grid && !grid->mPoints.empty());
2022-09-22 21:26:05 +03:00
2018-08-18 13:42:11 +03:00
float distanceBetween = distanceSquared(grid->mPoints[0], pos);
int closestIndex = 0;
2022-09-22 21:26:05 +03:00
// TODO: if this full scan causes performance problems mapping pathgrid
// points to a quadtree may help
for (unsigned int counter = 1; counter < grid->mPoints.size(); counter++)
2018-08-20 01:13:16 +03:00
{
float potentialDistBetween = distanceSquared(grid->mPoints[counter], pos);
if (potentialDistBetween < distanceBetween)
{
distanceBetween = potentialDistBetween;
closestIndex = counter;
}
}
return closestIndex;
2022-09-22 21:26:05 +03:00
}
private:
bool mConstructed = false;
2018-08-18 13:40:04 +03:00
std::deque<osg::Vec3f> mPath;
const MWWorld::CellStore* mCell = nullptr;
2018-08-23 00:20:25 +03:00
void buildPathByPathgridImpl(const osg::Vec3f& startPoint, const osg::Vec3f& endPoint,
const PathgridGraph& pathgridGraph, std::back_insert_iterator<std::deque<osg::Vec3f>> out);
[[nodiscard]] DetourNavigator::Status buildPathByNavigatorImpl(const MWWorld::ConstPtr& actor,
const osg::Vec3f& startPoint, const osg::Vec3f& endPoint, const DetourNavigator::AgentBounds& agentBounds,
const DetourNavigator::Flags flags, const DetourNavigator::AreaCosts& areaCosts, float endTolerance,
PathType pathType, std::back_insert_iterator<std::deque<osg::Vec3f>> out);
2013-03-31 17:30:03 +00:00
};
}
#endif