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