-
Notifications
You must be signed in to change notification settings - Fork 17
/
nav_mesh_node.go
108 lines (88 loc) · 2.38 KB
/
nav_mesh_node.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
package unityai
import (
"unsafe"
)
type NavMeshNodeFlags uint8
const (
kNew NavMeshNodeFlags = 0x00
kOpen NavMeshNodeFlags = 0x01
kClosed NavMeshNodeFlags = 0x02
)
type NavMeshNode struct {
pos Vector3f // Position of the node.
cost float32 // Cost from previous node to current node.
total float32 // Cost up to the node.
pidx uint32 // Index to parent node.
flags NavMeshNodeFlags // NavMeshNode flags new/open/closed.
id NavMeshPolyRef // Polygon ref the node corresponds to.
}
type NavMeshNodeIndex uint16
const (
kNavMeshNodeNullIndex NavMeshNodeIndex = 0xffff
)
type NavMeshNodePool struct {
m_MaxNavMeshNodes int32
m_HashSize int32
m_NavMeshNodeCount int32
m_NavMeshNodes []NavMeshNode
m_First []NavMeshNodeIndex
m_Next []NavMeshNodeIndex
}
var navMeshNodeSize = unsafe.Sizeof(NavMeshNode{})
func GetNavMeshNodeIndex(start, end *NavMeshNode) int32 {
nstart := uintptr(unsafe.Pointer(start))
nend := uintptr(unsafe.Pointer(end))
return int32((nend - nstart) / navMeshNodeSize)
}
func (this *NavMeshNodePool) GetNodeIdx(node *NavMeshNode) uint32 {
if node == nil {
return 0
}
idx := GetNavMeshNodeIndex(&this.m_NavMeshNodes[0], node) + 1
Assert(idx <= this.m_MaxNavMeshNodes)
return uint32(idx)
}
func (this *NavMeshNodePool) GetNodeAtIdx(idx uint32) *NavMeshNode {
Assert(idx <= uint32(this.m_MaxNavMeshNodes))
if idx == 0 {
return nil
}
return &this.m_NavMeshNodes[idx-1]
}
func (this *NavMeshNodePool) GetHashSize() int32 {
return this.m_HashSize
}
func (this *NavMeshNodePool) GetFirst(bucket int32) NavMeshNodeIndex {
return this.m_First[bucket]
}
func (this *NavMeshNodePool) GetNext(i int32) NavMeshNodeIndex {
return this.m_Next[i]
}
type NavMeshNodeQueue struct {
m_Heap []*NavMeshNode
m_Size int32
}
func (this *NavMeshNodeQueue) empty() bool {
return this.m_Size == 0
}
func (this *NavMeshNodeQueue) Clear() {
this.m_Size = 0
}
func (this *NavMeshNodeQueue) Pop() *NavMeshNode {
result := this.m_Heap[0]
this.m_Size--
this.TrickleDown(0, this.m_Heap[this.m_Size])
return result
}
func (this *NavMeshNodeQueue) Push(node *NavMeshNode) {
this.m_Size++
this.BubbleUp(this.m_Size-1, node)
}
func (this *NavMeshNodeQueue) Modify(node *NavMeshNode) {
for i := int32(0); i < this.m_Size; i++ {
if this.m_Heap[i] == node {
this.BubbleUp(i, node)
return
}
}
}