-
Notifications
You must be signed in to change notification settings - Fork 17
/
path_corridor.go
350 lines (291 loc) · 9.19 KB
/
path_corridor.go
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package unityai
type PathCorridorState uint8
const (
kPathCorridorValid PathCorridorState = 1 << 0
kPathCorridorPartial PathCorridorState = 1 << 1
kPathCorridorInterrupted PathCorridorState = 1 << 2
)
type PathCorridor struct {
m_pos Vector3f
m_target Vector3f
m_path []NavMeshPolyRef
m_stateFlags PathCorridorState
}
func NewPathCorridor() *PathCorridor {
return &PathCorridor{
m_pos: Vector3f{},
m_target: Vector3f{},
m_path: make([]NavMeshPolyRef, 0, 4),
m_stateFlags: 0,
}
}
func (this *PathCorridor) GetCurrentPos() Vector3f {
return this.m_pos
}
func (this *PathCorridor) ClearPath() {
this.m_path = this.m_path[:0]
}
func (this *PathCorridor) Reset(ref NavMeshPolyRef, pos Vector3f) {
if ref == 0 {
this.Invalidate()
return
}
this.m_path = this.m_path[:0]
this.m_path = append(this.m_path, ref)
this.m_pos = pos
this.m_target = pos
this.m_stateFlags = kPathCorridorValid
}
func (this *PathCorridor) Invalidate() {
// Preserve the position and target
this.m_path = this.m_path[:0]
this.m_path = append(this.m_path, NavMeshPolyRef(0))
this.m_stateFlags = 0
}
func (this *PathCorridor) SetToEnd() {
Assert(this.GetPathCount() != 0)
this.m_pos = this.m_target
this.m_path[0] = this.GetLastPoly()
this.m_path = this.m_path[:1]
}
const kMinTargetDistSq float32 = 0.0001
func (this *PathCorridor) GetPathCount() int32 {
return int32(len(this.m_path))
}
func (this *PathCorridor) FindCorners(cornerVerts []Vector3f, cornerFlags []uint8,
cornerPolys []NavMeshPolyRef, cornerCount *int32, maxCorners int32,
navquery *NavMeshQuery) NavMeshStatus {
Assert(this.GetPathCount() != 0)
var ncorners int32 = 0
status := navquery.FindStraightPath(this.m_pos, this.m_target, this.m_path, this.GetPathCount(),
cornerVerts, cornerFlags, cornerPolys, &ncorners, maxCorners)
if ncorners == 0 {
*cornerCount = 0
return status
}
// Prune points in the beginning of the path which are too close.
var prune int32
for prune = 0; prune < ncorners; prune++ {
if (NavMeshStraightPathFlags(cornerFlags[prune])&kStraightPathOffMeshConnection) != 0 || SqrDistance2D(cornerVerts[prune], this.m_pos) > kMinTargetDistSq {
break
}
}
ncorners -= prune
if prune != 0 && ncorners != 0 {
for i := int32(0); i < ncorners; i++ {
cornerFlags[i] = cornerFlags[i+prune]
cornerPolys[i] = cornerPolys[i+prune]
cornerVerts[i] = cornerVerts[i+prune]
}
}
// Prune points after an off-mesh connection.
for prune = 0; prune < ncorners; prune++ {
if NavMeshStraightPathFlags(cornerFlags[prune])&kStraightPathOffMeshConnection != 0 {
ncorners = prune + 1
break
}
}
*cornerCount = ncorners
if NavMeshStatusDetail(status, kNavMeshPartialResult) {
return kNavMeshSuccess | kNavMeshPartialResult
}
return kNavMeshSuccess
}
func (this *PathCorridor) OptimizePathVisibility(next Vector3f, navquery *NavMeshQuery, filter *QueryFilter) {
var res [kMaxResults]NavMeshPolyRef
var nres int32 = 0
var result NavMeshRaycastResult
navquery.Raycast(this.m_path[0], this.m_pos, next, filter, &result, res[:], &nres, kMaxResults)
if nres > 1 && result.t > 0.99 {
ReplacePathStart(&this.m_path, res[:], nres)
}
}
const kMaxIterations int32 = 8
func (this *PathCorridor) OptimizePathTopology(navquery *NavMeshQuery, filter *QueryFilter) bool {
if this.GetPathCount() < 3 {
return false
}
var res [kMaxResults]NavMeshPolyRef
var nres int32 = 0
status := navquery.InitSlicedFindPath2(this.m_path[0], this.GetLastPoly(), this.m_pos, this.m_target, filter)
if !NavMeshStatusFailed(status) {
status = navquery.UpdateSlicedFindPath(kMaxIterations, nil)
}
if !NavMeshStatusSucceed(status) {
// don't accept kNavMeshInProgress
return false
}
status = navquery.FinalizeSlicedFindPathPartial(&nres, this.m_path, this.GetPathCount())
if !NavMeshStatusSucceed(status) {
return false
}
status = navquery.GetPath(res[:], &nres, kMaxResults)
if !NavMeshStatusSucceed(status) {
return false
}
return ReplacePathStart(&this.m_path, res[:], nres)
}
func (this *PathCorridor) MoveOverOffmeshConnection(offMeshConRef NavMeshPolyRef, currentPos Vector3f,
startPos Vector3f, endPos Vector3f, navquery *NavMeshQuery) bool {
Assert(navquery != nil)
Assert(this.GetPathCount() != 0)
// Advance the path up to and over the off-mesh connection.
var prevRef, nextRef NavMeshPolyRef
polyRef := this.m_path[0]
var npos int32 = 0
npath := this.GetPathCount()
for npos < npath && polyRef != offMeshConRef {
prevRef = polyRef
polyRef = this.m_path[npos]
if npos+1 < npath {
nextRef = this.m_path[npos+1]
}
npos++
}
if npos == npath {
// Could not find offMeshConRef
return false
}
// Prune path
this.m_path = this.m_path[npos:]
nav := navquery.GetAttachedNavMesh()
Assert(nav != nil)
conn := nav.GetOffMeshConnection(polyRef)
if conn == nil {
return false
}
if conn.width > 0.0 {
// Handle wide link
status := nav.GetNearestOffMeshConnectionEndPoints(prevRef, polyRef, nextRef, currentPos, &startPos, &endPos)
if NavMeshStatusSucceed(status) {
this.m_pos = endPos
return true
}
} else {
status := nav.GetOffMeshConnectionEndPoints(prevRef, polyRef, &startPos, &endPos)
if NavMeshStatusSucceed(status) {
this.m_pos = endPos
return true
}
}
return false
}
// TODO : notify callers - return success/failure
const kMaxVisited int32 = 16
func (this *PathCorridor) MovePosition(newPos Vector3f, navquery *NavMeshQuery, filter *QueryFilter) bool {
Assert(this.GetPathCount() != 0)
if SqrDistance2D(newPos, this.m_pos) == 0.0 {
return false
}
// Move along navmesh and update new position.
var result Vector3f
var visited [kMaxVisited]NavMeshPolyRef
var nvisited int32 = 0
status := navquery.MoveAlongSurface(this.m_path[0], this.m_pos, newPos, filter,
&result, visited[:], &nvisited, kMaxVisited)
if !NavMeshStatusSucceed(status) {
return false
}
ReplacePathStartReverse(&this.m_path, visited[:], nvisited)
// Adjust the position to stay on top of the navmesh.
navquery.ProjectToPoly(&this.m_pos, this.m_path[0], result)
return true
}
func (this *PathCorridor) UpdateTargetPosition(ref NavMeshPolyRef, target Vector3f) bool {
if ref != this.GetLastPoly() {
return false
}
this.m_target = target
return true
}
const kExtraCapacity uint32 = 16
func (this *PathCorridor) SetCorridor(target Vector3f, navquery *NavMeshQuery, path []NavMeshPolyRef, npath int32, partialPath bool) {
Assert(npath > 0)
// Reserving room for extra polygons allows us to subsequently extend the path a bit,
// e.g. from a thread/job, without allocating memory and possibly locking.
this.m_path = make([]NavMeshPolyRef, npath)
this.m_target = target
copy(this.m_path, path[:npath])
this.m_stateFlags = kPathCorridorValid
this.SetPathPartial(partialPath)
// Adjust the position to stay on top of the navmesh.
navquery.ProjectToPoly(&this.m_target, this.GetLastPoly(), target)
}
func (this *PathCorridor) SetStateFlag(setFlag bool, stateFlag PathCorridorState) {
if setFlag {
this.m_stateFlags |= stateFlag
} else {
this.m_stateFlags &= ^stateFlag
}
}
func (this *PathCorridor) SetPathValid(inbool bool) {
this.SetStateFlag(inbool, kPathCorridorValid)
}
func (this *PathCorridor) SetPathPartial(inbool bool) {
this.SetStateFlag(inbool, kPathCorridorPartial)
}
func (this *PathCorridor) SetPathInterrupted(inbool bool) {
this.SetStateFlag(inbool, kPathCorridorInterrupted)
}
func (this *PathCorridor) GetLastPoly() NavMeshPolyRef {
return this.m_path[this.GetPathCount()-1]
}
func (this *PathCorridor) GetPath() []NavMeshPolyRef {
return this.m_path
}
func ReplacePathStart(path *[]NavMeshPolyRef, start []NavMeshPolyRef, nstart int32) bool {
npath := int32(len(*path))
var ipath, istart int32
if !FindFurthestIntersectionIndices(*path, start, npath, nstart, &ipath, &istart) {
return false
}
// the result may only grow before the elements are moved in-place.
tmpath := make([]NavMeshPolyRef, npath)
copy(tmpath, *path)
nres := istart + (npath - ipath)
if nres > npath {
*path = append(*path, make([]NavMeshPolyRef, nres-npath)...)
}
// move elements in place
copy((*path)[istart:], tmpath[ipath:npath])
copy(*path, start[:istart])
*path = (*path)[:nres]
// shrink result to fit
return true
}
func FindFurthestIntersectionIndices(a []NavMeshPolyRef, b []NavMeshPolyRef, na, nb int32, ia, ib *int32) bool {
for i := na - 1; i >= int32(0); i-- {
for j := nb - 1; j >= 0; j-- {
if a[i] == b[j] {
*ia = i
*ib = j
return true
}
}
}
return false
}
func ReplacePathStartReverse(path *[]NavMeshPolyRef, start []NavMeshPolyRef, nstart int32) bool {
npath := int32(len(*path))
var ipath, istart int32
if !FindFurthestIntersectionIndices(*path, start, npath, nstart, &ipath, &istart) {
return false
}
// the pivot index for the reversed start segment
istartrev := nstart - 1 - istart
tmpath := make([]NavMeshPolyRef, npath)
copy(tmpath, *path)
nres := istartrev + (npath - ipath)
// the result may only grow before the elements are moved in-place.
if nres > npath {
*path = append(*path, make([]NavMeshPolyRef, nres-npath)...)
}
// move elements in place
copy((*path)[istartrev:], tmpath[ipath:])
for i := int32(0); i < istartrev; i++ {
(*path)[i] = start[nstart-1-i]
}
// shrink result to fit
*path = (*path)[:nres]
return true
}