TrinityCore
PathGenerator.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef _PATH_GENERATOR_H
19#define _PATH_GENERATOR_H
20
21#include "DetourNavMesh.h"
22#include "DetourNavMeshQuery.h"
23#include "MMapDefines.h"
24#include "MoveSplineInitArgs.h"
25#include <G3D/Vector3.h>
26
27class WorldObject;
28
29// 74*4.0f=296y number_of_points*interval = max_path_len
30// this is way more than actual evade range
31// I think we can safely cut those down even more
32#define MAX_PATH_LENGTH 74
33#define MAX_POINT_PATH_LENGTH 74
34
35#define SMOOTH_PATH_STEP_SIZE 4.0f
36#define SMOOTH_PATH_SLOP 0.3f
37
38#define VERTEX_SIZE 3
39#define INVALID_POLYREF 0
40
42{
43 PATHFIND_BLANK = 0x00, // path not built yet
44 PATHFIND_NORMAL = 0x01, // normal path
45 PATHFIND_SHORTCUT = 0x02, // travel through obstacles, terrain, air, etc (old behavior)
46 PATHFIND_INCOMPLETE = 0x04, // we have partial path to follow - getting closer to target
47 PATHFIND_NOPATH = 0x08, // no valid path at all or error in generating one
48 PATHFIND_NOT_USING_PATH = 0x10, // used when we are either flying/swiming or on map w/o mmaps
49 PATHFIND_SHORT = 0x20, // path is longer or equal to its limited path length
50 PATHFIND_FARFROMPOLY_START = 0x40, // start position is far from the mmap poligon
51 PATHFIND_FARFROMPOLY_END = 0x80, // end positions is far from the mmap poligon
52 PATHFIND_FARFROMPOLY = PATHFIND_FARFROMPOLY_START | PATHFIND_FARFROMPOLY_END, // start or end positions are far from the mmap poligon
53};
54
56{
57 public:
58 explicit PathGenerator(WorldObject const* owner);
60
61 PathGenerator(PathGenerator const& right) = delete;
62 PathGenerator(PathGenerator&& right) = delete;
63 PathGenerator& operator=(PathGenerator const& right) = delete;
65
66 // Calculate the path from owner to given destination
67 // return: true if new path was calculated, false otherwise (no change needed)
68 bool CalculatePath(float destX, float destY, float destZ, bool forceDest = false);
69 bool IsInvalidDestinationZ(WorldObject const* target) const;
70
71 // option setters - use optional
72 void SetUseStraightPath(bool useStraightPath) { _useStraightPath = useStraightPath; }
73 void SetPathLengthLimit(float distance) { _pointPathLimit = std::min<uint32>(uint32(distance/SMOOTH_PATH_STEP_SIZE), MAX_POINT_PATH_LENGTH); }
74 void SetUseRaycast(bool useRaycast) { _useRaycast = useRaycast; }
75
76 // result getters
77 G3D::Vector3 const& GetStartPosition() const { return _startPosition; }
78 G3D::Vector3 const& GetEndPosition() const { return _endPosition; }
79 G3D::Vector3 const& GetActualEndPosition() const { return _actualEndPosition; }
80
81 Movement::PointsArray const& GetPath() const { return _pathPoints; }
82 float GetPathLength() const;
83
84 PathType GetPathType() const { return _type; }
85
86 // shortens the path until the destination is the specified distance from the target point
87 void ShortenPathUntilDist(G3D::Vector3 const& point, float dist);
88
89 private:
90
91 dtPolyRef _pathPolyRefs[MAX_PATH_LENGTH]; // array of detour polygon references
92 uint32 _polyLength; // number of polygons in the path
93
94 Movement::PointsArray _pathPoints; // our actual (x,y,z) path to the target
95 PathType _type; // tells what kind of path this is
96
97 bool _useStraightPath; // type of path will be generated
98 bool _forceDestination; // when set, we will always arrive at given point
99 uint32 _pointPathLimit; // limit point path size; min(this, MAX_POINT_PATH_LENGTH)
100 bool _useRaycast; // use raycast if true for a straight line path
101
102 G3D::Vector3 _startPosition; // {x, y, z} of current location
103 G3D::Vector3 _endPosition; // {x, y, z} of the destination
104 G3D::Vector3 _actualEndPosition; // {x, y, z} of the closest possible point to given destination
105
106 WorldObject const* const _source; // the object that is moving
107 dtNavMesh const* _navMesh; // the nav mesh
108 dtNavMeshQuery const* _navMeshQuery; // the nav mesh query used to find the path
109
110 dtQueryFilter _filter; // use single filter for all movements, update it when needed
111
112 void SetStartPosition(G3D::Vector3 const& point) { _startPosition = point; }
113 void SetEndPosition(G3D::Vector3 const& point) { _actualEndPosition = point; _endPosition = point; }
114 void SetActualEndPosition(G3D::Vector3 const& point) { _actualEndPosition = point; }
115 void NormalizePath();
116
117 void Clear()
118 {
119 _polyLength = 0;
120 _pathPoints.clear();
121 }
122
123 bool InRange(G3D::Vector3 const& p1, G3D::Vector3 const& p2, float r, float h) const;
124 float Dist3DSqr(G3D::Vector3 const& p1, G3D::Vector3 const& p2) const;
125 bool InRangeYZX(float const* v1, float const* v2, float r, float h) const;
126
127 dtPolyRef GetPathPolyByPosition(dtPolyRef const* polyPath, uint32 polyPathSize, float const* Point, float* Distance = nullptr) const;
128 dtPolyRef GetPolyByLocation(float const* Point, float* Distance) const;
129 bool HaveTile(G3D::Vector3 const& p) const;
130
131 void BuildPolyPath(G3D::Vector3 const& startPos, G3D::Vector3 const& endPos);
132 void BuildPointPath(float const* startPoint, float const* endPoint);
133 void BuildShortcut();
134
135 NavTerrainFlag GetNavTerrain(float x, float y, float z) const;
136 void CreateFilter();
137 void UpdateFilter();
138
139 // smooth path aux functions
140 uint32 FixupCorridor(dtPolyRef* path, uint32 npath, uint32 maxPath, dtPolyRef const* visited, uint32 nvisited);
141 bool GetSteerTarget(float const* startPos, float const* endPos, float minTargetDist, dtPolyRef const* path, uint32 pathSize, float* steerPos,
142 unsigned char& steerPosFlag, dtPolyRef& steerPosRef);
143 dtStatus FindSmoothPath(float const* startPos, float const* endPos,
144 dtPolyRef const* polyPath, uint32 polyPathSize,
145 float* smoothPath, int* smoothPathSize, uint32 smoothPathMaxSize);
146
147 void AddFarFromPolyFlags(bool startFarFromPoly, bool endFarFromPoly);
148};
149
150#endif
#define TC_GAME_API
Definition: Define.h:123
uint32_t uint32
Definition: Define.h:142
NavTerrainFlag
Definition: MMapDefines.h:64
#define MAX_PATH_LENGTH
Definition: PathGenerator.h:32
#define SMOOTH_PATH_STEP_SIZE
Definition: PathGenerator.h:35
#define MAX_POINT_PATH_LENGTH
Definition: PathGenerator.h:33
PathType
Definition: PathGenerator.h:42
@ PATHFIND_FARFROMPOLY_END
Definition: PathGenerator.h:51
@ PATHFIND_NOT_USING_PATH
Definition: PathGenerator.h:48
@ PATHFIND_NORMAL
Definition: PathGenerator.h:44
@ PATHFIND_NOPATH
Definition: PathGenerator.h:47
@ PATHFIND_FARFROMPOLY_START
Definition: PathGenerator.h:50
@ PATHFIND_FARFROMPOLY
Definition: PathGenerator.h:52
@ PATHFIND_SHORT
Definition: PathGenerator.h:49
@ PATHFIND_SHORTCUT
Definition: PathGenerator.h:45
@ PATHFIND_BLANK
Definition: PathGenerator.h:43
@ PATHFIND_INCOMPLETE
Definition: PathGenerator.h:46
PathType _type
Definition: PathGenerator.h:95
void SetUseRaycast(bool useRaycast)
Definition: PathGenerator.h:74
Movement::PointsArray const & GetPath() const
Definition: PathGenerator.h:81
G3D::Vector3 _startPosition
PathGenerator(PathGenerator const &right)=delete
void SetActualEndPosition(G3D::Vector3 const &point)
G3D::Vector3 const & GetStartPosition() const
Definition: PathGenerator.h:77
dtQueryFilter _filter
G3D::Vector3 _actualEndPosition
PathType GetPathType() const
Definition: PathGenerator.h:84
uint32 _pointPathLimit
Definition: PathGenerator.h:99
bool _useStraightPath
Definition: PathGenerator.h:97
dtNavMeshQuery const * _navMeshQuery
WorldObject const *const _source
G3D::Vector3 const & GetEndPosition() const
Definition: PathGenerator.h:78
uint32 _polyLength
Definition: PathGenerator.h:92
bool _forceDestination
Definition: PathGenerator.h:98
void SetPathLengthLimit(float distance)
Definition: PathGenerator.h:73
void SetStartPosition(G3D::Vector3 const &point)
Movement::PointsArray _pathPoints
Definition: PathGenerator.h:94
PathGenerator(PathGenerator &&right)=delete
dtNavMesh const * _navMesh
PathGenerator & operator=(PathGenerator &&right)=delete
void SetEndPosition(G3D::Vector3 const &point)
void SetUseStraightPath(bool useStraightPath)
Definition: PathGenerator.h:72
PathGenerator & operator=(PathGenerator const &right)=delete
G3D::Vector3 const & GetActualEndPosition() const
Definition: PathGenerator.h:79
G3D::Vector3 _endPosition
std::vector< Vector3 > PointsArray