-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathfinding.go
66 lines (49 loc) · 1.85 KB
/
pathfinding.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
package main
import (
"sync"
paths "github.com/SolarLune/paths"
)
var (
wg sync.WaitGroup
astarChannel chan *paths.Path // Stores all channels for astar paths
)
/*
Possible solution for waitgroup based crashes:
Make a counter for how many concurrent processes are finished or running, then only
reuse the waitgroup when when Wait has returned?
Or multiple waitgroups? Every enemy could have one
*/
func calculatePath(channel chan *paths.Path, mapNodes []string, start Rolumn, end Rolumn) {
// This line creates a new Grid, comprised of Cells. The size is 10x10. By default, all Cells are
// walkable and have a cost of 1, and a blank character of ' '.
//firstMap := paths.NewGrid(10, 10)
defer wg.Done()
start = newRolumn(start.column/smallTileSize.x, start.row/smallTileSize.y)
end = newRolumn(end.column/smallTileSize.x, end.row/smallTileSize.y)
mapLayout := paths.NewGridFromStringArrays(mapNodes)
// After creating the Grid, you can edit it using the Grid's functions. Note that here, we're using 'x'
// to get Cells that have the rune for the lowercase x character 'x', not the string "x".
for _, cell := range mapLayout.GetCellsByRune('x') {
cell.Walkable = false
}
for _, goop := range mapLayout.GetCellsByRune('g') {
goop.Cost = 3
}
for _, lava := range mapLayout.GetCellsByRune('l') {
lava.Cost = 15
}
// This gets a new Path (a slice of Cells) from the starting Cell to the destination Cell. If the path's length
// is greater than 0, then it was successful.
for {
// Break out with a nil path if unable to path (start/end on non-walkable cells)
if !mapLayout.Get(start.column, start.row).Walkable || !mapLayout.Get(end.column, end.row).Walkable {
channel <- nil
return
}
p := mapLayout.GetPath(mapLayout.Get(start.column, start.row), mapLayout.Get(end.column, end.row), true)
if p != nil {
channel <- p
break
}
}
}