Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
PathGenerator.h
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#ifndef SF_PATH_GENERATOR_H
7#define SF_PATH_GENERATOR_H
8
9#include "DetourNavMesh.h"
10#include "DetourNavMeshQuery.h"
11#include "MoveSplineInitArgs.h"
12#include "SharedDefines.h"
13
14class Unit;
15
16// 74*4.0f=296y number_of_points*interval = max_path_len
17// this is way more than actual evade range
18// I think we can safely cut those down even more
19#define MAX_PATH_LENGTH 74
20#define MAX_POINT_PATH_LENGTH 74
21
22#define SMOOTH_PATH_STEP_SIZE 4.0f
23#define SMOOTH_PATH_SLOP 0.3f
24
25#define VERTEX_SIZE 3
26#define INVALID_POLYREF 0
27
29{
30 PATHFIND_BLANK = 0x00, // path not built yet
31 PATHFIND_NORMAL = 0x01, // normal path
32 PATHFIND_SHORTCUT = 0x02, // travel through obstacles, terrain, air, etc (old behavior)
33 PATHFIND_INCOMPLETE = 0x04, // we have partial path to follow - getting closer to target
34 PATHFIND_NOPATH = 0x08, // no valid path at all or error in generating one
35 PATHFIND_NOT_USING_PATH = 0x10, // used when we are either flying/swiming or on map w/o mmaps
36 PATHFIND_SHORT = 0x20, // path is longer or equal to its limited path length
37};
38
40{
41public:
42 explicit PathGenerator(Unit const* owner);
44
45 // Calculate the path from owner to given destination
46 // return: true if new path was calculated, false otherwise (no change needed)
47 bool CalculatePath(float destX, float destY, float destZ, bool forceDest = false);
48
49 // option setters - use optional
50 void SetUseStraightPath(bool useStraightPath) { _useStraightPath = useStraightPath; }
51 void SetPathLengthLimit(float distance) { _pointPathLimit = std::min<uint32>(uint32(distance / SMOOTH_PATH_STEP_SIZE), MAX_POINT_PATH_LENGTH); }
52
53 // result getters
54 G3D::Vector3 const& GetStartPosition() const { return _startPosition; }
55 G3D::Vector3 const& GetEndPosition() const { return _endPosition; }
56 G3D::Vector3 const& GetActualEndPosition() const { return _actualEndPosition; }
57
58 Movement::PointsArray const& GetPath() const { return _pathPoints; }
59
60 PathType GetPathType() const { return _type; }
61
62private:
63 dtPolyRef _pathPolyRefs[MAX_PATH_LENGTH]; // array of detour polygon references
64 uint32 _polyLength; // number of polygons in the path
65
66 Movement::PointsArray _pathPoints; // our actual (x,y,z) path to the target
67 PathType _type; // tells what kind of path this is
68
69 bool _useStraightPath; // type of path will be generated
70 bool _forceDestination; // when set, we will always arrive at given point
71 uint32 _pointPathLimit; // limit point path size; min(this, MAX_POINT_PATH_LENGTH)
72
73 G3D::Vector3 _startPosition; // {x, y, z} of current location
74 G3D::Vector3 _endPosition; // {x, y, z} of the destination
75 G3D::Vector3 _actualEndPosition; // {x, y, z} of the closest possible point to given destination
76
77 Unit const* const _sourceUnit; // the unit that is moving
78 dtNavMesh const* _navMesh; // the nav mesh
79 dtNavMeshQuery const* _navMeshQuery; // the nav mesh query used to find the path
80
81 dtQueryFilter _filter; // use single filter for all movements, update it when needed
82
83 void SetStartPosition(G3D::Vector3 const& point) { _startPosition = point; }
84 void SetEndPosition(G3D::Vector3 const& point) { _actualEndPosition = point; _endPosition = point; }
85 void SetActualEndPosition(G3D::Vector3 const& point) { _actualEndPosition = point; }
86 void NormalizePath();
87 void DensifyGroundPath();
88 static float ResolveTerrainZ(Unit const* unit, float x, float y, float z);
89
90 void Clear()
91 {
92 _polyLength = 0;
93 _pathPoints.clear();
94 }
95
96 bool InRange(G3D::Vector3 const& p1, G3D::Vector3 const& p2, float r, float h) const;
97 float Dist3DSqr(G3D::Vector3 const& p1, G3D::Vector3 const& p2) const;
98 bool InRangeYZX(float const* v1, float const* v2, float r, float h) const;
99
100 dtPolyRef GetPathPolyByPosition(dtPolyRef const* polyPath, uint32 polyPathSize, float const* Point, float* Distance = NULL) const;
101 dtPolyRef GetPolyByLocation(float const* Point, float* Distance) const;
102 bool HaveTile(G3D::Vector3 const& p) const;
103
104 void BuildPolyPath(G3D::Vector3 const& startPos, G3D::Vector3 const& endPos);
105 void BuildPointPath(float const* startPoint, float const* endPoint);
106 void BuildShortcut();
107
108 NavTerrain GetNavTerrain(float x, float y, float z);
109 void CreateFilter();
110 void UpdateFilter();
111
112 // smooth path aux functions
113 uint32 FixupCorridor(dtPolyRef* path, uint32 npath, uint32 maxPath, dtPolyRef const* visited, uint32 nvisited);
114 bool GetSteerTarget(float const* startPos, float const* endPos, float minTargetDist, dtPolyRef const* path, uint32 pathSize, float* steerPos,
115 unsigned char& steerPosFlag, dtPolyRef& steerPosRef);
116 dtStatus FindSmoothPath(float const* startPos, float const* endPos,
117 dtPolyRef const* polyPath, uint32 polyPathSize,
118 float* smoothPath, int* smoothPathSize, uint32 smoothPathMaxSize);
119};
120
121#endif
std::uint32_t uint32
Definition Define.h:77
#define MAX_PATH_LENGTH
#define SMOOTH_PATH_STEP_SIZE
#define MAX_POINT_PATH_LENGTH
PathType
@ PATHFIND_NOT_USING_PATH
@ PATHFIND_NORMAL
@ PATHFIND_NOPATH
@ PATHFIND_SHORT
@ PATHFIND_SHORTCUT
@ PATHFIND_BLANK
@ PATHFIND_INCOMPLETE
NavTerrain
PathGenerator(Unit const *owner)
Movement::PointsArray const & GetPath() const
G3D::Vector3 _startPosition
bool HaveTile(G3D::Vector3 const &p) const
void SetActualEndPosition(G3D::Vector3 const &point)
G3D::Vector3 const & GetStartPosition() const
dtQueryFilter _filter
float Dist3DSqr(G3D::Vector3 const &p1, G3D::Vector3 const &p2) const
G3D::Vector3 _actualEndPosition
PathType GetPathType() const
uint32 _pointPathLimit
dtStatus FindSmoothPath(float const *startPos, float const *endPos, dtPolyRef const *polyPath, uint32 polyPathSize, float *smoothPath, int *smoothPathSize, uint32 smoothPathMaxSize)
Unit const *const _sourceUnit
dtNavMeshQuery const * _navMeshQuery
G3D::Vector3 const & GetEndPosition() const
void SetPathLengthLimit(float distance)
dtPolyRef GetPolyByLocation(float const *Point, float *Distance) const
void BuildPolyPath(G3D::Vector3 const &startPos, G3D::Vector3 const &endPos)
bool InRangeYZX(float const *v1, float const *v2, float r, float h) const
void SetStartPosition(G3D::Vector3 const &point)
uint32 FixupCorridor(dtPolyRef *path, uint32 npath, uint32 maxPath, dtPolyRef const *visited, uint32 nvisited)
dtPolyRef _pathPolyRefs[MAX_PATH_LENGTH]
Movement::PointsArray _pathPoints
bool InRange(G3D::Vector3 const &p1, G3D::Vector3 const &p2, float r, float h) const
NavTerrain GetNavTerrain(float x, float y, float z)
void BuildPointPath(float const *startPoint, float const *endPoint)
dtNavMesh const * _navMesh
dtPolyRef GetPathPolyByPosition(dtPolyRef const *polyPath, uint32 polyPathSize, float const *Point, float *Distance=NULL) const
bool CalculatePath(float destX, float destY, float destZ, bool forceDest=false)
bool GetSteerTarget(float const *startPos, float const *endPos, float minTargetDist, dtPolyRef const *path, uint32 pathSize, float *steerPos, unsigned char &steerPosFlag, dtPolyRef &steerPosRef)
void SetEndPosition(G3D::Vector3 const &point)
void SetUseStraightPath(bool useStraightPath)
G3D::Vector3 const & GetActualEndPosition() const
static float ResolveTerrainZ(Unit const *unit, float x, float y, float z)
G3D::Vector3 _endPosition
Definition Unit.h:1367
std::vector< Vector3 > PointsArray