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 srcX, float srcY, float srcZ, float destX, float destY, float destZ, bool forceDest = false);
69 bool CalculatePath(float destX, float destY, float destZ, bool forceDest = false);
70 bool IsInvalidDestinationZ(WorldObject const* target) const;
71
72 // option setters - use optional
73 void SetUseStraightPath(bool useStraightPath) { _useStraightPath = useStraightPath; }
74 void SetPathLengthLimit(float distance) { _pointPathLimit = std::min<uint32>(uint32(distance/SMOOTH_PATH_STEP_SIZE), MAX_POINT_PATH_LENGTH); }
75 void SetUseRaycast(bool useRaycast) { _useRaycast = useRaycast; }
76
77 // result getters
78 G3D::Vector3 const& GetStartPosition() const { return _startPosition; }
79 G3D::Vector3 const& GetEndPosition() const { return _endPosition; }
80 G3D::Vector3 const& GetActualEndPosition() const { return _actualEndPosition; }
81
82 Movement::PointsArray const& GetPath() const { return _pathPoints; }
83 float GetPathLength() const;
84
85 PathType GetPathType() const { return _type; }
86
87 // shortens the path until the destination is the specified distance from the target point
88 void ShortenPathUntilDist(G3D::Vector3 const& target, float dist);
89
90 private:
91
92 dtPolyRef _pathPolyRefs[MAX_PATH_LENGTH]; // array of detour polygon references
93 uint32 _polyLength; // number of polygons in the path
94
95 Movement::PointsArray _pathPoints; // our actual (x,y,z) path to the target
96 PathType _type; // tells what kind of path this is
97
98 bool _useStraightPath; // type of path will be generated
99 bool _forceDestination; // when set, we will always arrive at given point
100 uint32 _pointPathLimit; // limit point path size; min(this, MAX_POINT_PATH_LENGTH)
101 bool _useRaycast; // use raycast if true for a straight line path
102
103 G3D::Vector3 _startPosition; // {x, y, z} of current location
104 G3D::Vector3 _endPosition; // {x, y, z} of the destination
105 G3D::Vector3 _actualEndPosition; // {x, y, z} of the closest possible point to given destination
106
107 WorldObject const* const _source; // the object that is moving
108 dtNavMesh const* _navMesh; // the nav mesh
109 dtNavMeshQuery const* _navMeshQuery; // the nav mesh query used to find the path
110
111 dtQueryFilter _filter; // use single filter for all movements, update it when needed
112
113 void SetStartPosition(G3D::Vector3 const& point) { _startPosition = point; }
114 void SetEndPosition(G3D::Vector3 const& point) { _actualEndPosition = point; _endPosition = point; }
115 void SetActualEndPosition(G3D::Vector3 const& point) { _actualEndPosition = point; }
116 void NormalizePath();
117
118 void Clear()
119 {
120 _polyLength = 0;
121 _pathPoints.clear();
122 }
123
124 bool InRange(G3D::Vector3 const& p1, G3D::Vector3 const& p2, float r, float h) const;
125 float Dist3DSqr(G3D::Vector3 const& p1, G3D::Vector3 const& p2) const;
126 bool InRangeYZX(float const* v1, float const* v2, float r, float h) const;
127
128 dtPolyRef GetPathPolyByPosition(dtPolyRef const* polyPath, uint32 polyPathSize, float const* Point, float* Distance = nullptr) const;
129 dtPolyRef GetPolyByLocation(float const* Point, float* Distance) const;
130 bool HaveTile(G3D::Vector3 const& p) const;
131
132 void BuildPolyPath(G3D::Vector3 const& startPos, G3D::Vector3 const& endPos);
133 void BuildPointPath(float const* startPoint, float const* endPoint);
134 void BuildShortcut();
135
136 NavTerrainFlag GetNavTerrain(float x, float y, float z) const;
137 void CreateFilter();
138 void UpdateFilter();
139
140 // smooth path aux functions
141 uint32 FixupCorridor(dtPolyRef* path, uint32 npath, uint32 maxPath, dtPolyRef const* visited, uint32 nvisited);
142 bool GetSteerTarget(float const* startPos, float const* endPos, float minTargetDist, dtPolyRef const* path, uint32 pathSize, float* steerPos,
143 unsigned char& steerPosFlag, dtPolyRef& steerPosRef);
144 dtStatus FindSmoothPath(float const* startPos, float const* endPos,
145 dtPolyRef const* polyPath, uint32 polyPathSize,
146 float* smoothPath, int* smoothPathSize, uint32 maxSmoothPathSize);
147
148 void AddFarFromPolyFlags(bool startFarFromPoly, bool endFarFromPoly);
149};
150
151#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:96
void SetUseRaycast(bool useRaycast)
Definition: PathGenerator.h:75
Movement::PointsArray const & GetPath() const
Definition: PathGenerator.h:82
G3D::Vector3 _startPosition
PathGenerator(PathGenerator const &right)=delete
void SetActualEndPosition(G3D::Vector3 const &point)
G3D::Vector3 const & GetStartPosition() const
Definition: PathGenerator.h:78
dtQueryFilter _filter
G3D::Vector3 _actualEndPosition
PathType GetPathType() const
Definition: PathGenerator.h:85
uint32 _pointPathLimit
bool _useStraightPath
Definition: PathGenerator.h:98
dtNavMeshQuery const * _navMeshQuery
WorldObject const *const _source
G3D::Vector3 const & GetEndPosition() const
Definition: PathGenerator.h:79
uint32 _polyLength
Definition: PathGenerator.h:93
bool _forceDestination
Definition: PathGenerator.h:99
void SetPathLengthLimit(float distance)
Definition: PathGenerator.h:74
void SetStartPosition(G3D::Vector3 const &point)
Movement::PointsArray _pathPoints
Definition: PathGenerator.h:95
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:73
PathGenerator & operator=(PathGenerator const &right)=delete
G3D::Vector3 const & GetActualEndPosition() const
Definition: PathGenerator.h:80
G3D::Vector3 _endPosition
std::vector< Vector3 > PointsArray