1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
#pragma once
#ifndef RUNTIME_NAVMESH_AGENT
#define RUNTIME_NAVMESH_AGENT
#include "Runtime/GameCode/Behaviour.h"
#include "Runtime/Graphics/Transform.h"
#include "NavMeshTypes.h"
#include "Runtime/Math/Vector2.h"
class NavMeshPath;
struct OffMeshLinkData;
struct NavMeshHit;
class dtNavMeshQuery;
class dtCrowd;
class dtQueryFilter;
struct dtCrowdAgentParams;
struct dtCrowdAgent;
class NavMeshAgent : public Behaviour
{
public:
REGISTER_DERIVED_CLASS (NavMeshAgent, Behaviour)
DECLARE_OBJECT_SERIALIZE (NavMeshAgent)
NavMeshAgent (MemLabelId& label, ObjectCreationMode mode);
// ~NavMeshAgent (); declared by a macro
static void InitializeClass ();
static void CleanupClass () {} // Avoid double free of Behavior
virtual void AwakeFromLoad (AwakeFromLoadMode mode);
virtual void CheckConsistency ();
inline bool InCrowdSystem () const;
inline void SetManagerHandle (int handle);
bool SetDestination (const Vector3f& position);
Vector3f GetDestination () const;
void SetInternalAgentPosition (const Vector3f& position);
int CalculatePolygonPath (const Vector3f& targetPosition, NavMeshPath* path);
bool SetPath (const NavMeshPath* path);
void CopyPath (NavMeshPath* path) const;
void ResetPath ();
bool PathPending () const;
bool HasPath () const;
bool DistanceToEdge (NavMeshHit* hit) const;
bool Raycast (const Vector3f& position, NavMeshHit* hitPos);
float GetRemainingDistance () const;
// Consider moving these to the 'NavMeshPath' class
bool SamplePathPosition (int collisionMask, float maxDistance, NavMeshHit* hit);
Vector3f GetEndPositionOfCurrentPath () const;
bool IsPathValid () const;
bool IsPathPartial () const;
bool IsPathStale () const;
NavMeshPathStatus GetPathStatus () const;
inline void ResetCachedPolyRef ();
UInt32 GetWalkableMask () const { return m_WalkableMask; }
void SetWalkableMask (UInt32 mask);
void SetLayerCost (unsigned int layer, float cost);
float GetLayerCost (unsigned int layer) const;
inline ObstacleAvoidanceType GetObstacleAvoidanceType () const;
void SetObstacleAvoidanceType (int type);
void SetAvoidancePriority (int value);
inline int GetAvoidancePriority () const;
inline float GetSpeed () const;
void SetSpeed (float value);
inline float GetAngularSpeed () const;
void SetAngularSpeed (float value);
inline float GetAcceleration () const;
void SetAcceleration (float value);
inline float GetStoppingDistance () const;
void SetStoppingDistance (float value);
Vector3f GetVelocity () const;
void SetVelocity (const Vector3f& vel);
Vector3f GetNextPosition () const;
Vector3f GetNextCorner () const;
Vector3f GetDesiredVelocity () const;
bool IsOnOffMeshLink () const;
void ActivateCurrentOffMeshLink (bool activated);
bool GetCurrentOffMeshLinkData (OffMeshLinkData* data) const;
bool GetNextOffMeshLinkData (OffMeshLinkData* data) const;
bool SetOffMeshLinkDataFlags (OffMeshLinkData* data, const dtPolyRef polyRef) const;
inline bool GetUpdatePosition () const;
void SetUpdatePosition (bool inbool);
inline bool GetUpdateRotation () const;
void SetUpdateRotation (bool inbool);
inline bool GetAutoTraverseOffMeshLink () const;
void SetAutoTraverseOffMeshLink (bool inbool);
inline bool GetAutoBraking () const;
void SetAutoBraking (bool inbool);
inline bool GetAutoRepath () const;
void SetAutoRepath (bool inbool);
inline float GetRadius () const;
inline float GetHeight () const;
inline float GetBaseOffset () const;
float CalculateScaledRadius () const;
float CalculateScaledHeight () const;
void SetRadius (float radius);
void SetHeight (float height);
void SetBaseOffset (float baseOffset);
bool Warp (const Vector3f& newPosition);
void Move (const Vector3f& motion);
void Stop (bool stopUpdates);
void Resume ();
void CompleteOffMeshLink ();
void UpdateState ();
void UpdateTransform (float deltaTime);
inline Vector3f GetGroundPositionFromTransform () const;
void OnNavMeshChanged ();
inline void OnNavMeshCleanup ();
protected:
virtual void AddToManager ();
virtual void RemoveFromManager ();
virtual void Reset ();
virtual void SmartReset ();
void OnTransformChanged (int mask);
private:
void AddToCrowdSystem ();
void RemoveFromCrowdSystem ();
void ReinstateInCrowdSystem ();
void StopOrResume (float remainingDistance);
void RepathIfStuck (float remainingDistance);
void UpdateRotation (Transform& transform, float deltaTime);
void FillAgentParams (dtCrowdAgentParams& params);
void UpdateActiveAgentParameters ();
Vector2f CalculateDimensionScales () const;
inline float CalculateScaledBaseOffset (const Transform& transform) const;
inline void SetTransformFromGroundPosition (const Vector3f& groundPosition);
int GetCurrentPolygonMask () const;
const dtQueryFilter& GetFilter () const;
const dtCrowdAgent* GetInternalAgent () const;
static const dtNavMeshQuery* GetInternalNavMeshQuery ();
static dtCrowd* GetCrowdSystem ();
static inline float EnsurePositive (float value);
Vector3f m_Destination;
Vector3f m_RequestedDestination;
float m_Radius;
float m_Height;
float m_BaseOffset;
float m_Speed;
float m_AngularSpeed;
float m_Angle;
float m_Acceleration;
float m_StoppingDistance;
// Internal Crowd system agent index
dtCrowdHandle m_AgentHandle;
int m_InstanceID;
// Persistent manager handle
int m_ManagerHandle;
dtPolyRef m_CachedPolyRef;
ObstacleAvoidanceType m_ObstacleAvoidanceType; ///< enum { None = 0, Low Quality, Medium Quality, Good Quality, High Quality }
UInt32 m_WalkableMask;
int m_AvoidancePriority;
bool m_AutoTraverseOffMeshLink;
bool m_AutoBraking;
bool m_AutoRepath;
bool m_UpdatePosition : 1;
bool m_UpdateRotation : 1;
bool m_StopDistance : 1;
bool m_StopExplicit : 1;
bool m_StopRotating : 1;
bool m_Request : 1;
friend void DrawNavMeshAgent (const NavMeshAgent& agent);
};
inline float NavMeshAgent::EnsurePositive (float value)
{
return std::max (0.00001F, value);
}
inline Vector3f NavMeshAgent::GetGroundPositionFromTransform () const
{
const Transform& transform = GetComponent (Transform);
return transform.TransformPoint (Vector3f (0, -m_BaseOffset, 0));
}
inline void NavMeshAgent::SetTransformFromGroundPosition (const Vector3f& groundPosition)
{
Transform& transform = GetComponent (Transform);
transform.SetPositionWithLocalOffset (groundPosition, Vector3f (0.0f, -m_BaseOffset, 0.0f));
}
inline float NavMeshAgent::CalculateScaledBaseOffset (const Transform& transform) const
{
return m_BaseOffset * Abs (transform.GetWorldScaleLossy ().y);
}
inline bool NavMeshAgent::InCrowdSystem () const
{
return m_AgentHandle.IsValid ();
}
inline void NavMeshAgent::SetManagerHandle (int handle)
{
m_ManagerHandle = handle;
}
inline void NavMeshAgent::ResetCachedPolyRef ()
{
m_CachedPolyRef = -1;
}
inline ObstacleAvoidanceType NavMeshAgent::GetObstacleAvoidanceType () const
{
return m_ObstacleAvoidanceType;
}
inline int NavMeshAgent::GetAvoidancePriority () const
{
return m_AvoidancePriority;
}
inline float NavMeshAgent::GetSpeed () const
{
return m_Speed;
}
inline float NavMeshAgent::GetAngularSpeed () const
{
return m_AngularSpeed;
}
inline float NavMeshAgent::GetAcceleration () const
{
return m_Acceleration;
}
inline float NavMeshAgent::GetStoppingDistance () const
{
return m_StoppingDistance;
}
inline bool NavMeshAgent::GetUpdatePosition () const
{
return m_UpdatePosition;
}
inline bool NavMeshAgent::GetUpdateRotation () const
{
return m_UpdateRotation;
}
inline bool NavMeshAgent::GetAutoTraverseOffMeshLink () const
{
return m_AutoTraverseOffMeshLink;
}
inline bool NavMeshAgent::GetAutoBraking () const
{
return m_AutoBraking;
}
inline bool NavMeshAgent::GetAutoRepath () const
{
return m_AutoRepath;
}
inline float NavMeshAgent::GetRadius () const
{
return m_Radius;
}
inline float NavMeshAgent::GetHeight () const
{
return m_Height;
}
inline float NavMeshAgent::GetBaseOffset () const
{
return m_BaseOffset;
}
inline void NavMeshAgent::OnNavMeshCleanup ()
{
RemoveFromCrowdSystem ();
}
#endif
|