-
Notifications
You must be signed in to change notification settings - Fork 0
/
Astar.go
334 lines (294 loc) · 7.67 KB
/
Astar.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
package main
import (
"container/heap"
"fmt"
"math"
)
const (
NO_LIST = 0
OPEN_LIST = 1
CLOSED_LIST = 2
EPSILON = 0.00001
)
type OpenList []*PathNode
func (l OpenList) Len() int { return len(l) }
func (l OpenList) Less(i, j int) bool {
return l[i].gVal+l[i].hVal < l[j].gVal+l[j].hVal
}
func (l OpenList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l *OpenList) Push(x interface{}) {
// Push and Pop use pointer receivers because they modify the slice's length,
// not just its contents.
*l = append(*l, x.(*PathNode))
}
func (l *OpenList) Pop() interface{} {
old := *l
n := len(old)
x := old[n-1]
*l = old[0 : n-1]
return x
}
//========================================================================================
type PathNode struct {
Tile *Tile
parent *PathNode
gVal float64
hVal float64
List int
SearchCnt int64
}
type PathFindManager struct {
OpenList OpenList
Nodes [][]*PathNode
ThePath []*PathNode
_SearchCnt int64 //当前寻路次数
}
func (s *PathFindManager) GetNodes(tile *Tile) *PathNode {
return s.Nodes[tile.x][tile.y]
}
func (s *PathFindManager) ClearOpenList() {
s.OpenList = make([]*PathNode, 0)
heap.Init(&s.OpenList)
}
func (s *PathFindManager) CleanRoad() {
s.ThePath = make([]*PathNode, 0)
}
func (s *PathFindManager) AddToOpen(cur *Tile) {
node := s.GetNodes(cur)
node.List = OPEN_LIST
heap.Push(&s.OpenList, node)
}
//========================================================================================
//欧几里得距离
func EuclideanDistance(s, s1 *Tile) float64 {
deltaX := math.Abs(float64(s1.x - s.x))
deltaY := math.Abs(float64(s1.y - s.y))
return math.Sqrt(deltaX*deltaX + deltaY*deltaY)
}
func NewPathNode(s *Tile, end *Tile) *PathNode {
node := _P.GetNodes(s)
if node.SearchCnt < _P._SearchCnt {
node.Tile = s
node.SearchCnt = _P._SearchCnt
node.hVal = EuclideanDistance(s, end)
node.gVal = math.MaxFloat64
node.List = NO_LIST
}
return node
}
//========================================================================================
func AStarSearch(start, end *Tile) {
_P._SearchCnt++
_P.ClearOpenList()
_P.CleanRoad()
startNode := NewPathNode(start, end)
endNode := NewPathNode(end, end)
startNode.gVal = 0
_P.AddToOpen(start)
for len(_P.OpenList) != 0 {
a := heap.Pop(&_P.OpenList)
curr := a.(*PathNode)
curr.List = CLOSED_LIST
if endNode.gVal < curr.gVal+curr.hVal+EPSILON {
fmt.Println("hahahaha")
break
}
neighbors := m.GetNeighborTraversableTiles(curr.Tile)
for _, tile := range neighbors {
succ := NewPathNode(tile, end)
if succ.List != CLOSED_LIST {
//fmt.Println(*curr.Tile, "neibor", *tile)
newGValue := curr.gVal + EuclideanDistance(curr.Tile, succ.Tile)
//fmt.Println("curr:", *curr.Tile, "succ:", *succ.Tile, "new:", newGValue, "old:", succ.gVal)
if newGValue+EPSILON < succ.gVal {
succ.gVal = newGValue
//fmt.Println("curr:", *curr.Tile, "succ:", *succ.Tile, "fvalue:", succ.gVal+succ.hVal)
succ.parent = curr
_P.AddToOpen(succ.Tile)
}
}
}
}
if endNode.gVal < math.MaxFloat64 {
curNode := endNode
for curNode != startNode {
_P.ThePath = append(_P.ThePath, curNode)
curNode = curNode.parent
}
}
}
func ThetaStarSearch(start, end *Tile) {
_P._SearchCnt++
_P.ClearOpenList()
_P.CleanRoad()
startNode := NewPathNode(start, end)
endNode := NewPathNode(end, end)
startNode.gVal = 0
//Set 'start's parent as itself. When 'start' is expanded for the first time, the grandparent of 'start's successor will be 'start' as well.
startNode.parent = startNode
_P.AddToOpen(start)
for len(_P.OpenList) != 0 {
a := heap.Pop(&_P.OpenList)
curr := a.(*PathNode)
curr.List = CLOSED_LIST
if endNode.gVal < curr.gVal+curr.hVal+EPSILON {
fmt.Println("hahahaha")
break
}
neighbors := m.GetNeighborTraversableTiles(curr.Tile)
for _, tile := range neighbors {
succ := NewPathNode(tile, end)
if succ.List != CLOSED_LIST {
var newGValue float64
var newParent *PathNode
if LineOfSight(curr.parent.Tile, succ.Tile) {
newParent = curr.parent
} else {
newParent = curr
}
newGValue = newParent.gVal + EuclideanDistance(newParent.Tile, succ.Tile)
if newGValue+EPSILON < succ.gVal {
succ.gVal = newGValue
succ.parent = newParent
_P.AddToOpen(succ.Tile)
}
}
}
}
if endNode.gVal < math.MaxFloat64 {
curNode := endNode
for curNode != startNode {
_P.ThePath = append(_P.ThePath, curNode)
curNode = curNode.parent
}
}
}
func LazyThetaStarSearch(start, end *Tile) {
_P._SearchCnt++
_P.ClearOpenList()
_P.CleanRoad()
startNode := NewPathNode(start, end)
endNode := NewPathNode(end, end)
startNode.gVal = 0
//Set 'start's parent as itself. When 'start' is expanded for the first time, the grandparent of 'start's successor will be 'start' as well.
startNode.parent = startNode
_P.AddToOpen(start)
for len(_P.OpenList) != 0 {
a := heap.Pop(&_P.OpenList)
curr := a.(*PathNode)
curr.List = CLOSED_LIST
if endNode.gVal < curr.gVal+curr.hVal+EPSILON {
fmt.Println("hahahaha")
break
}
ValidateParent(curr, endNode)
newParent := curr.parent
neighbors := m.GetNeighborTraversableTiles(curr.Tile)
for _, tile := range neighbors {
succ := NewPathNode(tile, end)
if succ.List != CLOSED_LIST {
newGValue := newParent.gVal + EuclideanDistance(newParent.Tile, succ.Tile)
if newGValue+EPSILON < succ.gVal {
succ.gVal = newGValue
succ.parent = newParent
_P.AddToOpen(succ.Tile)
}
}
}
}
if endNode.gVal < math.MaxFloat64 {
ValidateParent(endNode, endNode)
curNode := endNode
for curNode != startNode {
_P.ThePath = append(_P.ThePath, curNode)
curNode = curNode.parent
}
}
}
func ValidateParent(s, end *PathNode) {
if !LineOfSight(s.parent.Tile, s.Tile) {
s.gVal = math.MaxFloat64
neighbors := m.GetNeighborTraversableTiles(s.Tile)
for _, tile := range neighbors {
newParent := NewPathNode(tile, end.Tile)
if newParent.List == CLOSED_LIST {
newGValue := newParent.gVal + EuclideanDistance(newParent.Tile, s.Tile)
if newGValue < s.gVal {
s.gVal = newGValue
s.parent = newParent
}
}
}
}
}
func LineOfSight(l1, l2 *Tile) bool {
x1 := l1.x
y1 := l1.y
x2 := l2.x
y2 := l2.y
dy := y2 - y1
dx := x2 - x1
var f, sy, sx, x_offset, y_offset int
if dy < 0 {
dy = -dy
sy = -1
y_offset = 0
} else {
sy = 1
y_offset = 1
}
if dx < 0 {
dx = -dx
sx = -1
x_offset = 0
} else {
sx = 1
x_offset = 1
}
if dx >= dy { // Move along the x axis and increment/decrement y when f >= dx.
for x1 != x2 {
f = f + dy
if f >= dx { // We are changing rows, we might need to check two cells this iteration.
if !m.IsTraversable(x1+x_offset, y1+y_offset) {
return false
}
y1 = y1 + sy
f = f - dx
}
if f != 0 { // If f == 0, then we are crossing the row at a corner point and we don't need to check both cells.
if !m.IsTraversable(x1+x_offset, y1+y_offset) {
return false
}
}
if dy == 0 { // If we are moving along a horizontal line, either the north or the south cell should be unblocked.
if !m.IsTraversable(x1+x_offset, y1) && !m.IsTraversable(x1+x_offset, y1+1) {
return false
}
}
x1 += sx
}
} else { //if (dx < dy). Move along the y axis and increment/decrement x when f >= dy.
for y1 != y2 {
f = f + dx
if f >= dy {
if !m.IsTraversable(x1+x_offset, y1+y_offset) {
return false
}
x1 = x1 + sx
f = f - dy
}
if f != 0 {
if !m.IsTraversable(x1+x_offset, y1+y_offset) {
return false
}
}
if dx == 0 {
if !m.IsTraversable(x1, y1+y_offset) && !m.IsTraversable(x1+1, y1+y_offset) {
return false
}
}
y1 += sy
}
}
return true
}