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
314
315
|
#include "UnityPrefix.h"
#include "OffMeshLink.h"
#include "NavMesh.h"
#include "NavMeshSettings.h"
#include "NavMeshManager.h"
#include "DetourNavMesh.h"
#include "NavMeshLayers.h"
#include "DetourCrowd.h"
#include "Runtime/Utilities/ValidateArgs.h"
#include "Runtime/Serialize/TransferFunctions/SerializeTransfer.h"
OffMeshLink::OffMeshLink (MemLabelId& label, ObjectCreationMode mode)
: Super (label, mode)
{
m_NavMeshLayer = 0;
m_StaticPolyRef = 0;
m_DynamicPolyRef = 0;
m_ManagerHandle = -1;
m_CostOverride = -1.0f;
m_BiDirectional = true;
m_AutoUpdatePositions = false;
m_ShouldUpdateDynamic = false;
m_Activated = true;
}
void OffMeshLink::Reset ()
{
Super::Reset ();
}
void OffMeshLink::SmartReset ()
{
Super::SmartReset ();
#if UNITY_EDITOR
m_NavMeshLayer = GetGameObject ().GetNavMeshLayer ();
#endif
}
OffMeshLink::~OffMeshLink ()
{
DisableDynamic ();
}
void OffMeshLink::AddToManager ()
{
if (m_StaticPolyRef)
{
GetNavMeshSettings ().SetOffMeshPolyAccess (m_StaticPolyRef, m_Activated);
// TODO: We really should be uging users to update here - but this warning breaks runtime tests because of unexpected log.
//WarningStringObject ("This OffMeshLink is static. To make it dynamic please re-bake the navmesh or reset this OffMeshLink component", this);
}
else
{
UpdatePositions ();
}
}
void OffMeshLink::RemoveFromManager ()
{
if (m_StaticPolyRef)
GetNavMeshSettings ().SetOffMeshPolyAccess (m_StaticPolyRef, m_Activated);
DisableDynamic ();
}
template<class TransferFunc>
void OffMeshLink::Transfer (TransferFunc& transfer)
{
Super::Transfer (transfer);
transfer.SetVersion (2);
TRANSFER (m_NavMeshLayer); //< Bake time only
#if UNITY_EDITOR
// NavMeshLayer has been moved from the game object to OffMeshLink in Unity 4.0
// This ensures that the value is pulled from the game object in AwakeFromLoad
if (transfer.IsOldVersion (1))
{
m_NavMeshLayer = 0xFFFFFFFF;
}
#endif
TRANSFER (m_Start); //< Bake time only
TRANSFER (m_End); //< Bake time only
///@TODO: Get rid of the static polygon reference when breaking backwards compatibility
/// all component-based offmeshlinks should be dynamic.
transfer.Transfer (m_StaticPolyRef, "m_DtPolyRef", kHideInEditorMask);
// from being copied when duplicating an OML
TRANSFER (m_CostOverride); //< Changes propagated to navmesh at runtime
transfer.Align ();
TRANSFER (m_BiDirectional); //< Bake time only
TRANSFER (m_Activated); //< Changes propagated to navmesh at runtime
TRANSFER (m_AutoUpdatePositions); //< Changes propagated to navmesh at runtime
}
void OffMeshLink::AwakeFromLoad (AwakeFromLoadMode mode)
{
Super::AwakeFromLoad (mode);
#if UNITY_EDITOR
// NavMeshLayer has been moved from the game object to OffMeshLink in Unity 4.0
// This ensures that the value is pulled from the game object in AwakeFromLoad
if (m_NavMeshLayer == 0xFFFFFFFF && GetGameObjectPtr ())
{
m_NavMeshLayer = GetGameObject ().GetNavMeshLayer ();
}
#endif
NavMeshSettings& settings = GetNavMeshSettings ();
// Prioritize static polyref (baked) over dynamic
if (m_StaticPolyRef && !m_AutoUpdatePositions)
{
DisableDynamic ();
// The object instanceIDs may change from load to load.
// So storing the instanceID (of OffMeshLink) in baked data is not an option.
//
// Instead we attempt to set it given the polyref we got from the bake process.
//
// However: storing references (polyRef) to baked data in an object (OffMeshLink) is unsafe!
// eg: a user does not save the scene after baking navmesh - and later opens scene.
// Or: user disables an OffMeshLink, bakes navmesh, and re-enables offmeshlink.
if (settings.SetOffMeshPolyInstanceID (m_StaticPolyRef, GetInstanceID ()))
{
settings.SetOffMeshPolyCostOverride (m_StaticPolyRef, m_CostOverride);
settings.SetOffMeshPolyAccess (m_StaticPolyRef, m_Activated);
}
}
else
{
m_ShouldUpdateDynamic = true;
}
}
void OffMeshLink::UpdatePositions ()
{
if (!IsActive ())
return;
DisableStatic ();
DisableDynamic (false);
EnableDynamic ();
m_ShouldUpdateDynamic = false;
}
void OffMeshLink::EnableDynamic ()
{
#if DT_DYNAMIC_OFFMESHLINK
if (m_ManagerHandle == -1)
GetNavMeshManager ().RegisterOffMeshLink (*this, m_ManagerHandle);
if (m_DynamicPolyRef || !m_Activated || !m_End || !m_Start || m_NavMeshLayer == NavMeshLayers::kNotWalkable)
return;
NavMeshSettings& settings = GetNavMeshSettings ();
dtNavMesh* navmesh = settings.GetInternalNavMesh ();
if (!navmesh)
return;
const int instanceID = GetInstanceID ();
const Vector3f startPos = m_Start->GetPosition ();
const Vector3f endPos = m_End->GetPosition ();
m_DynamicPolyRef = navmesh->AddDynamicOffMeshLink (startPos.GetPtr (), endPos.GetPtr (), instanceID, m_BiDirectional, (unsigned char)m_NavMeshLayer);
if (m_DynamicPolyRef)
{
settings.SetOffMeshPolyCostOverride (m_DynamicPolyRef, m_CostOverride);
}
#endif
}
void OffMeshLink::CheckDynamicPositions ()
{
#if DT_DYNAMIC_OFFMESHLINK
if (m_DynamicPolyRef == 0 || m_ShouldUpdateDynamic)
return;
NavMeshSettings& settings = GetNavMeshSettings ();
dtNavMesh* navmesh = settings.GetInternalNavMesh ();
if (!navmesh)
return;
if (!m_Start || !m_End)
return;
const Vector3f objectStartPos = m_Start->GetPosition ();
const Vector3f objectEndPos = m_End->GetPosition ();
const dtOffMeshConnection* con = navmesh->GetDynamicOffMeshLink (m_DynamicPolyRef);
if (con)
{
const float connectionRadius = con->rad;
const Vector3f startPos = Vector3f (&con->pos[0]);
const Vector3f endPos = Vector3f (&con->pos[3]);
m_ShouldUpdateDynamic = !CompareApproximately (startPos, objectStartPos, connectionRadius) || !CompareApproximately (endPos, objectEndPos, connectionRadius);
}
#endif
}
void OffMeshLink::OnNavMeshCleanup ()
{
ClearDynamicPolyRef ();
}
void OffMeshLink::OnNavMeshChanged ()
{
ClearDynamicPolyRef ();
m_ShouldUpdateDynamic = true;
}
void OffMeshLink::UpdateMovedPositions ()
{
if (m_AutoUpdatePositions)
CheckDynamicPositions ();
if (m_ShouldUpdateDynamic || m_DynamicPolyRef == 0)
{
UpdatePositions ();
}
}
void OffMeshLink::DisableStatic ()
{
GetNavMeshSettings ().SetOffMeshPolyAccess (m_StaticPolyRef, false);
}
void OffMeshLink::DisableDynamic (bool unregister)
{
#if DT_DYNAMIC_OFFMESHLINK
if (unregister && m_ManagerHandle != -1)
GetNavMeshManager ().UnregisterOffMeshLink (m_ManagerHandle);
if (m_DynamicPolyRef == 0)
return;
if (dtNavMesh* navmesh = GetNavMeshSettings ().GetInternalNavMesh ())
navmesh->RemoveDynamicOffMeshLink (m_DynamicPolyRef);
m_DynamicPolyRef = 0;
#endif
}
void OffMeshLink::SetCostOverride (float costOverride)
{
ABORT_INVALID_FLOAT (costOverride, costOverride, OffMeshLink);
if (m_CostOverride == costOverride)
return;
dtPolyRef ref = GetStaticOrDynamicPolyRef ();
GetNavMeshSettings ().SetOffMeshPolyCostOverride (ref, costOverride);
m_CostOverride = costOverride;
SetDirty ();
}
void OffMeshLink::SetBiDirectional (bool bidirectional)
{
if (m_BiDirectional == bidirectional)
return;
m_BiDirectional = bidirectional;
SetDirty ();
}
void OffMeshLink::SetActivated (bool activated)
{
if (m_Activated == activated)
return;
m_Activated = activated;
if (m_StaticPolyRef)
{
GetNavMeshSettings ().SetOffMeshPolyAccess (m_StaticPolyRef, activated);
}
else
{
if (activated && !m_DynamicPolyRef)
{
EnableDynamic ();
}
else if (!activated && m_DynamicPolyRef)
{
DisableDynamic ();
}
}
SetDirty ();
}
void OffMeshLink::SetNavMeshLayer (UInt32 layer)
{
if (m_NavMeshLayer == layer)
return;
m_NavMeshLayer = layer;
UpdatePositions ();
SetDirty ();
}
bool OffMeshLink::GetOccupied () const
{
if (const dtCrowd* crowd = GetNavMeshManager ().GetCrowdSystem ())
{
const dtPolyRef ref = GetStaticOrDynamicPolyRef ();
return crowd->IsRefOccupied (ref);
}
return false;
}
IMPLEMENT_CLASS (OffMeshLink)
IMPLEMENT_OBJECT_SERIALIZE (OffMeshLink)
|