-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAStarAlgo.go
286 lines (225 loc) · 6.87 KB
/
AStarAlgo.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
// If I have a function that accepts a pointer to a struct
// instead of just the struct type, is it more efficient? Yes.
// If I pass the struct am I then copying the whole thing? Yes.
package main
import (
"container/heap"
"errors"
"fmt"
"math"
"math/rand"
"time"
)
type MapElement struct {
pos_x int
pos_y int
name string
passable bool
path []*MapElement
}
type Map2d struct {
x int
y int
two_d [][]*MapElement
}
func get_map_element(x int, y int, name string, passable bool) MapElement {
return MapElement{x, y, name, passable, nil}
}
func new_map(width, height int) Map2d {
a_map := Map2d{width, height, make([][]*MapElement, width)}
for i := 0; i < width; i++ {
a_map.two_d[i] = make([]*MapElement, height)
for j := 0; j < height; j++ {
a_map.two_d[i][j] = &MapElement{i, j, ".", true, nil}
}
}
return a_map
}
func print_map(a_map *Map2d) string {
printed := " "
for i := 0; i < a_map.x; i++ {
printed = fmt.Sprintf("%s %2d ", printed, i)
}
for n := 0; n < a_map.y; n++ {
row := fmt.Sprintf(" %2d", n)
for m := 0; m < a_map.x; m++ {
row = fmt.Sprintf("%s %s", row, a_map.two_d[m][n].name)
}
printed = fmt.Sprintf("%s\n%s", printed, row)
}
return fmt.Sprintf("%s\n\n", printed)
}
func get_passable_neighbours(x, y int, a_map *Map2d) []*MapElement {
l := make([]*MapElement, 0)
// left
if x-1 >= 0 && x-1 < a_map.x && y >= 0 && y < a_map.y {
if a_map.two_d[x-1][y].passable {
l = append(l, a_map.two_d[x-1][y])
}
}
// top
if x >= 0 && x < a_map.x && y-1 >= 0 && y-1 < a_map.y {
if a_map.two_d[x][y-1].passable {
l = append(l, a_map.two_d[x][y-1])
}
}
// right
if x+1 >= 0 && x+1 < a_map.x && y >= 0 && y < a_map.y {
if a_map.two_d[x+1][y].passable {
l = append(l, a_map.two_d[x+1][y])
}
}
// bottom
if x >= 0 && x < a_map.x && y+1 >= 0 && y+1 < a_map.y {
if a_map.two_d[x][y+1].passable {
l = append(l, a_map.two_d[x][y+1])
}
}
return l
}
func getPathToGoal(cameFrom map[*MapElement]*MapElement, start, goal *MapElement) []*MapElement {
nodes := make([]*MapElement, 0)
for e := goal; e != start; {
// prepend trick so I don't have to reverse the list later
nodes = append([]*MapElement{e}, nodes...) // this is prob inneficient.
e = cameFrom[e]
}
return nodes
}
func paintThem(elements []*MapElement) {
for _, e := range elements {
e.name = "+"
}
}
func heuristic(a, b *MapElement) int {
return int(math.Abs(float64(a.pos_x-b.pos_x)) + math.Abs(float64(a.pos_y-b.pos_y)))
}
func aStarAlgorithm(a_map *Map2d, start *MapElement, goal *MapElement) []*MapElement {
frontier := make(PriorityQueue, 0)
heap.Init(&frontier)
heap.Push(&frontier, &Item{value: start, priority: 0})
cameFrom := make(map[*MapElement]*MapElement)
costSoFar := make(map[*MapElement]int)
cameFrom[start] = nil
costSoFar[start] = 0
finished := false
for i := len(frontier); i > 0; {
topItem := heap.Pop(&frontier).(*Item)
current := topItem.value
if current.pos_x == goal.pos_x && current.pos_y == goal.pos_y {
finished = true
break
}
neighbours := get_passable_neighbours(current.pos_x, current.pos_y, a_map)
// paintThem(neighbours) // debug help
// current.name = "o"
// fmt.Printf(print_map(a_map))
// fmt.Printf("%v\n", current)
for _, nextElement := range neighbours {
newCost := costSoFar[current] + 1 // static cost for now.
_, exists := costSoFar[nextElement]
if (!exists) || (newCost < costSoFar[nextElement]) {
costSoFar[nextElement] = newCost
priority := newCost + heuristic(goal, nextElement)
heap.Push(&frontier, &Item{value: nextElement, priority: priority})
cameFrom[nextElement] = current
}
}
i = len(frontier)
}
if finished {
path := getPathToGoal(cameFrom, start, goal)
return path
} else {
path := make([]*MapElement, 0)
return path
}
}
func putElementinMap2d(a_map *Map2d, name string, passable bool, x, y int) (*MapElement, error) {
if x < 0 || y < 0 || x > (a_map.x-1) || y > (a_map.y-1) {
return nil, errors.New("Out of bounds of the map2d")
}
// fmt.Printf("%v %v | ", x, y)
element := MapElement{x, y, name, passable, nil}
a_map.two_d[x][y] = &element
return a_map.two_d[x][y], nil
}
func generateDungeon(width int, height int, maxTunnels int, maxLength int) Map2d {
dungeon := Map2d{width, height, make([][]*MapElement, width)}
for i := 0; i < width; i++ {
dungeon.two_d[i] = make([]*MapElement, height)
for j := 0; j < height; j++ {
dungeon.two_d[i][j] = &MapElement{i, j, "#", false, nil}
}
}
rand.Seed(time.Now().UnixNano())
// a couple of good examples of how to deal with 2d array declarations:
startingPoint := []int{rand.Intn(dungeon.x - 1), rand.Intn(dungeon.y - 1)}
directions := [][]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
randomDirection := rand.Intn(len(directions))
lastDirection := randomDirection
for i := 0; i < maxTunnels; i++ {
if lastDirection <= 1 {
randomDirection = rand.Intn(2) + 2
} else {
randomDirection = rand.Intn(2)
}
thisTunnelLength := rand.Intn(maxLength) + 1
// fmt.Printf("Pos: %v | length: %v | direction:%v\n", startingPoint, thisTunnelLength, directions[randomDirection])
for j := 0; j <= thisTunnelLength; j++ {
x := startingPoint[0] + directions[randomDirection][0]
y := startingPoint[1] + directions[randomDirection][1]
if ele, err := putElementinMap2d(&dungeon, ".", true, x, y); err != nil {
//hit edge of map.
break
} else {
startingPoint[0] = ele.pos_x
startingPoint[1] = ele.pos_y
}
}
lastDirection = randomDirection
}
//fmt.Printf("%s | %s", startingPoint, directions)
return dungeon
}
func testPath() {
fmt.Print("\n")
a_map := new_map(6, 6)
bob, _ := putElementinMap2d(&a_map, "b", false, 0, 2)
goal, _ := putElementinMap2d(&a_map, "*", true, 4, 1)
a_map.two_d[1][0] = &MapElement{1, 0, "#", false, nil}
a_map.two_d[2][0] = &MapElement{2, 0, "#", false, nil}
a_map.two_d[2][1] = &MapElement{2, 1, "#", false, nil}
a_map.two_d[2][2] = &MapElement{2, 2, "#", false, nil}
a_map.two_d[2][3] = &MapElement{2, 3, "#", false, nil}
a_map.two_d[1][3] = &MapElement{1, 3, "#", false, nil}
fmt.Printf(print_map(&a_map))
results := aStarAlgorithm(&a_map, bob, goal)
paintThem(results)
fmt.Printf(print_map(&a_map))
}
func testDungeon() {
fmt.Println("Generating dungeon...")
dungeon := generateDungeon(10000, 10000, 8000, 3000)
var bob *MapElement
var goal *MapElement
rand.Seed(time.Now().UnixNano())
for {
bob = dungeon.two_d[rand.Intn(dungeon.x-1)][rand.Intn(dungeon.y-1)]
if bob.passable {
bob, _ = putElementinMap2d(&dungeon, "b", false, bob.pos_x, bob.pos_y)
break
}
}
for {
goal = dungeon.two_d[rand.Intn(dungeon.x-1)][rand.Intn(dungeon.y-1)]
if goal.passable {
// goal.name = "*"
goal, _ = putElementinMap2d(&dungeon, "0", true, goal.pos_x, goal.pos_y)
break
}
}
fmt.Println("Done. Algo running...")
way := aStarAlgorithm(&dungeon, bob, goal)
fmt.Println("Algo done, len: ", len(way))
}