-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindpath.go
114 lines (91 loc) · 2.65 KB
/
findpath.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
package hexgrid
import (
"fmt"
"math"
"sort"
)
type costData struct {
index int
distSoFar float64
estRemaining float64
prevIndex int
}
func (pc costData) estTotal() float64 {
return pc.distSoFar + pc.estRemaining
}
type byScore []costData
func (a byScore) Len() int { return len(a) }
func (a byScore) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byScore) Less(i, j int) bool { return a[i].estTotal() > a[j].estTotal() }
func estDistance(start Coord, end Coord) float64 {
return math.Abs(float64(start.X-end.X)) + math.Abs(float64(start.Y-end.Y))
}
// Find a path through a hex grid.
// Cost function must be symmetric
// Costs must be positive. Nonpositive costs will be considered infinite.
func (g HexGrid[T]) FindPath(
start Coord,
end Coord,
costFn func(hexA *T, rawHexB *T) float64) (
float64, []Coord, error) {
// reverse start and end so the final path is trivially in the right order.
start, end = end, start
realCosts := make([]costData, len(g.hexes))
for i, _ := range realCosts {
realCosts[i] = costData{i, -1, -1, -1}
}
startIndex := g.coordToIndex(start)
endIndex := g.coordToIndex(end)
// stack is a stack of cost estimates.
stack := []costData{
costData{startIndex, 0, estDistance(start, end), -1},
}
for len(stack) > 0 {
stackSize := len(stack)
parent := stack[stackSize-1]
stack = stack[:stackSize-1]
// If we've already processed a coord, the earlier one will definitely be
// better than the later one.
if realCosts[parent.index].distSoFar >= 0 {
continue
}
// Mark this parent as visited.
realCosts[parent.index] = parent
if parent.index == endIndex {
finalPath := []Coord{}
node := realCosts[endIndex]
finalCost := node.distSoFar
for {
finalPath = append(finalPath, g.indexToCoord(node.index))
if node.prevIndex == -1 {
break
}
node = realCosts[node.prevIndex]
}
return finalCost, finalPath, nil
}
parentCoord := g.indexToCoord(parent.index)
neighbors := g.GetNeighbors(parentCoord)
for _, neighbor := range neighbors {
neighborIndex := g.coordToIndex(neighbor)
// Don't add neighbors that are already locked in.
if realCosts[neighborIndex].distSoFar >= 0 {
continue
}
edgeCost := costFn(g.GetAt(parentCoord), g.GetAt(neighbor))
// If edge cost is negative, we could get an infinite loop through no-cost nodes.
if edgeCost < 0 {
continue
}
newComponent := costData{
neighborIndex,
parent.distSoFar + edgeCost,
estDistance(neighbor, end),
parent.index,
}
stack = append(stack, newComponent)
}
sort.Sort(byScore(stack))
}
return 0.0, []Coord{}, fmt.Errorf("no path exists")
}