TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PathGenerator.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2011 MaNGOS <http://getmangos.com/>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #ifndef _PATH_GENERATOR_H
20 #define _PATH_GENERATOR_H
21 
22 #include "MapDefines.h"
23 #include "DetourNavMesh.h"
24 #include "DetourNavMeshQuery.h"
25 #include "MoveSplineInitArgs.h"
26 
27 class Unit;
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 };
51 
53 {
54  public:
55  explicit PathGenerator(Unit const* owner);
56  ~PathGenerator();
57 
58  // Calculate the path from owner to given destination
59  // return: true if new path was calculated, false otherwise (no change needed)
60  bool CalculatePath(float destX, float destY, float destZ, bool forceDest = false, bool straightLine = false);
61 
62  // option setters - use optional
63  void SetUseStraightPath(bool useStraightPath) { _useStraightPath = useStraightPath; }
64  void SetPathLengthLimit(float distance) { _pointPathLimit = std::min<uint32>(uint32(distance/SMOOTH_PATH_STEP_SIZE), MAX_POINT_PATH_LENGTH); }
65 
66  // result getters
67  G3D::Vector3 const& GetStartPosition() const { return _startPosition; }
68  G3D::Vector3 const& GetEndPosition() const { return _endPosition; }
69  G3D::Vector3 const& GetActualEndPosition() const { return _actualEndPosition; }
70 
71  Movement::PointsArray const& GetPath() const { return _pathPoints; }
72 
73  PathType GetPathType() const { return _type; }
74 
75  void ReducePathLenghtByDist(float dist); // path must be already built
76 
77  private:
78 
79  dtPolyRef _pathPolyRefs[MAX_PATH_LENGTH]; // array of detour polygon references
80  uint32 _polyLength; // number of polygons in the path
81 
82  Movement::PointsArray _pathPoints; // our actual (x,y,z) path to the target
83  PathType _type; // tells what kind of path this is
84 
85  bool _useStraightPath; // type of path will be generated
86  bool _forceDestination; // when set, we will always arrive at given point
87  uint32 _pointPathLimit; // limit point path size; min(this, MAX_POINT_PATH_LENGTH)
88  bool _straightLine; // use raycast if true for a straight line path
89 
90  G3D::Vector3 _startPosition; // {x, y, z} of current location
91  G3D::Vector3 _endPosition; // {x, y, z} of the destination
92  G3D::Vector3 _actualEndPosition; // {x, y, z} of the closest possible point to given destination
93 
94  Unit const* const _sourceUnit; // the unit that is moving
95  dtNavMesh const* _navMesh; // the nav mesh
96  dtNavMeshQuery const* _navMeshQuery; // the nav mesh query used to find the path
97 
98  dtQueryFilter _filter; // use single filter for all movements, update it when needed
99 
100  void SetStartPosition(G3D::Vector3 const& point) { _startPosition = point; }
101  void SetEndPosition(G3D::Vector3 const& point) { _actualEndPosition = point; _endPosition = point; }
102  void SetActualEndPosition(G3D::Vector3 const& point) { _actualEndPosition = point; }
103  void NormalizePath();
104 
105  void Clear()
106  {
107  _polyLength = 0;
108  _pathPoints.clear();
109  }
110 
111  bool InRange(G3D::Vector3 const& p1, G3D::Vector3 const& p2, float r, float h) const;
112  float Dist3DSqr(G3D::Vector3 const& p1, G3D::Vector3 const& p2) const;
113  bool InRangeYZX(float const* v1, float const* v2, float r, float h) const;
114 
115  dtPolyRef GetPathPolyByPosition(dtPolyRef const* polyPath, uint32 polyPathSize, float const* Point, float* Distance = NULL) const;
116  dtPolyRef GetPolyByLocation(float const* Point, float* Distance) const;
117  bool HaveTile(G3D::Vector3 const& p) const;
118 
119  void BuildPolyPath(G3D::Vector3 const& startPos, G3D::Vector3 const& endPos);
120  void BuildPointPath(float const* startPoint, float const* endPoint);
121  void BuildShortcut();
122 
123  NavTerrain GetNavTerrain(float x, float y, float z);
124  void CreateFilter();
125  void UpdateFilter();
126 
127  // smooth path aux functions
128  uint32 FixupCorridor(dtPolyRef* path, uint32 npath, uint32 maxPath, dtPolyRef const* visited, uint32 nvisited);
129  bool GetSteerTarget(float const* startPos, float const* endPos, float minTargetDist, dtPolyRef const* path, uint32 pathSize, float* steerPos,
130  unsigned char& steerPosFlag, dtPolyRef& steerPosRef);
131  dtStatus FindSmoothPath(float const* startPos, float const* endPos,
132  dtPolyRef const* polyPath, uint32 polyPathSize,
133  float* smoothPath, int* smoothPathSize, uint32 smoothPathMaxSize);
134 };
135 
136 #endif
uint32 _polyLength
Definition: PathGenerator.h:80
Definition: PathGenerator.h:45
uint64_d dtPolyRef
Definition: DetourNavMesh.h:49
PathType
Definition: PathGenerator.h:41
Definition: PathGenerator.h:47
Definition: PathGenerator.h:44
void SetActualEndPosition(G3D::Vector3 const &point)
Definition: PathGenerator.h:102
bool _straightLine
Definition: PathGenerator.h:88
G3D::Vector3 const & GetEndPosition() const
Definition: PathGenerator.h:68
G3D::Vector3 _startPosition
Definition: PathGenerator.h:90
dtNavMesh const * _navMesh
Definition: PathGenerator.h:95
void SetEndPosition(G3D::Vector3 const &point)
Definition: PathGenerator.h:101
G3D::Vector3 const & GetActualEndPosition() const
Definition: PathGenerator.h:69
arena_t NULL
Definition: jemalloc_internal.h:624
Unit const *const _sourceUnit
Definition: PathGenerator.h:94
G3D::Vector3 const & GetStartPosition() const
Definition: PathGenerator.h:67
NavTerrain
Definition: MapDefines.h:22
Definition: DetourNavMeshQuery.h:35
unsigned int dtStatus
Definition: DetourStatus.h:22
double distance(double x, double y)
Definition: g3dmath.h:731
Definition: Vector3.h:58
Definition: DetourNavMesh.h:323
Definition: PathGenerator.h:43
G3D::int16 z
Definition: Vector3int16.h:46
std::vector< Vector3 > PointsArray
Definition: MoveSplineInitArgs.h:30
uint32_t uint32
Definition: Define.h:150
Movement::PointsArray _pathPoints
Definition: PathGenerator.h:82
Movement::PointsArray const & GetPath() const
Definition: PathGenerator.h:71
G3D::int16 y
Definition: Vector2int16.h:38
dtQueryFilter _filter
Definition: PathGenerator.h:98
void SetPathLengthLimit(float distance)
Definition: PathGenerator.h:64
Definition: PathGenerator.h:52
bool _forceDestination
Definition: PathGenerator.h:86
Definition: PathGenerator.h:46
bool _useStraightPath
Definition: PathGenerator.h:85
Definition: PathGenerator.h:49
void SetUseStraightPath(bool useStraightPath)
Definition: PathGenerator.h:63
Definition: PathGenerator.h:48
dtNavMeshQuery const * _navMeshQuery
Definition: PathGenerator.h:96
PathType _type
Definition: PathGenerator.h:83
#define TC_GAME_API
Definition: Define.h:134
void SetStartPosition(G3D::Vector3 const &point)
Definition: PathGenerator.h:100
uint32_t uint32
Definition: g3dmath.h:168
G3D::int16 x
Definition: Vector2int16.h:37
#define MAX_PATH_LENGTH
Definition: PathGenerator.h:32
Definition: DetourNavMeshQuery.h:153
G3D::Vector3 _actualEndPosition
Definition: PathGenerator.h:92
void Clear()
Definition: PathGenerator.h:105
PathType GetPathType() const
Definition: PathGenerator.h:73
#define SMOOTH_PATH_STEP_SIZE
Definition: PathGenerator.h:35
Definition: Unit.h:1305
#define MAX_POINT_PATH_LENGTH
Definition: PathGenerator.h:33
uint32 _pointPathLimit
Definition: PathGenerator.h:87
G3D::Vector3 _endPosition
Definition: PathGenerator.h:91