/
astar.go
303 lines (253 loc) · 9.48 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
package goeasystar
import (
"errors"
"math"
"sort"
"github.com/facebookgo/pqueue"
)
const straightCost = 1.0
const diagonalCost = 1.4
const costPrecision = 100
// Pathfinder represents a single instance of a pathfinding configuration
type Pathfinder struct {
collisionGrid [][]int
additionalPointsToAvoid map[string]bool
costMap map[int]float64
additionalPointsToCost map[string]float64
allowCornerCutting bool
acceptableTiles []int
diagonalsEnabled bool
startX int
startY int
endX int
endY int
coordinateToNode map[string]*node
openList pqueue.PriorityQueue
}
// NewPathfinder returns a new instance of a pathfinder
func NewPathfinder() *Pathfinder {
return &Pathfinder{
additionalPointsToAvoid: make(map[string]bool),
costMap: make(map[int]float64),
additionalPointsToCost: make(map[string]float64),
allowCornerCutting: false,
acceptableTiles: make([]int, 0),
diagonalsEnabled: false,
}
}
// SetAcceptableTiles sets a list of tiles which are deemed acceptable to
// pass through
func (p *Pathfinder) SetAcceptableTiles(t []int) {
p.acceptableTiles = t
}
// EnableDiagonals enables diagonals on the Pathfinder
func (p *Pathfinder) EnableDiagonals() {
p.diagonalsEnabled = true
}
// DisableDiagonals disables diagonals on the Pathfinder
func (p *Pathfinder) DisableDiagonals() {
p.diagonalsEnabled = false
}
// SetGrid sets the grid of the Pathfinder
func (p *Pathfinder) SetGrid(grid [][]int) {
p.collisionGrid = grid
}
// SetAdditionalPointCost sets the an additional cost for a particular point
// Overrides the cost from SetTileCost
func (p *Pathfinder) SetAdditionalPointCost(x int, y int, cost float64) {
key := getHashKeyForPoint(x, y)
p.additionalPointsToCost[key] = cost
}
// RemoveAdditionalPointCost removes the additional cost for a particular point
func (p *Pathfinder) RemoveAdditionalPointCost(x int, y int) {
delete(p.additionalPointsToCost, getHashKeyForPoint(x, y))
}
// RemoveAllAdditionalPointCosts removes all additional point costs
func (p *Pathfinder) RemoveAllAdditionalPointCosts() {
p.additionalPointsToAvoid = make(map[string]bool)
}
// AvoidAdditionalPoint avoids a particular point on the grid
// regardless of whether or not it is an acceptable tile
func (p *Pathfinder) AvoidAdditionalPoint(x int, y int) {
key := getHashKeyForPoint(x, y)
p.additionalPointsToAvoid[key] = true
}
// StopAvoidingAdditionalPoint stops avoiding a particular point on the grid
func (p *Pathfinder) StopAvoidingAdditionalPoint(x int, y int) {
delete(p.additionalPointsToAvoid, getHashKeyForPoint(x, y))
}
// EnableCornerCutting enables corner cutting in diagonal movement
func (p *Pathfinder) EnableCornerCutting() {
p.allowCornerCutting = true
}
// DisableCornerCutting disables corner cutting in diagonal movement
func (p *Pathfinder) DisableCornerCutting() {
p.allowCornerCutting = false
}
// StopAvoidingAllAdditionalPoints stops avoiding all additional points on the grid
func (p *Pathfinder) StopAvoidingAllAdditionalPoints() {
p.additionalPointsToAvoid = make(map[string]bool)
}
// FindPath finds a path
// Returns a slice of points which consists the starting point up to the end point inclusively
func (p *Pathfinder) FindPath(startX int, startY int, endX int, endY int) ([]*Point, error) {
// No acceptable tiles were set
if len(p.acceptableTiles) == 0 {
return nil, errors.New("You can't find a path without first calling SetAcceptableTiles()")
}
// No grid was set
if p.collisionGrid == nil {
return nil, errors.New("You can't find a path without first calling SetGrid()")
}
// Start or endpoint outside of scope
if startX < 0 || startY < 0 || endX < 0 || endX < 0 ||
startX > len(p.collisionGrid[0])-1 || startY > len(p.collisionGrid)-1 ||
endX > len(p.collisionGrid[0])-1 || endY > len(p.collisionGrid)-1 {
return nil, errors.New("You can't find a path without first calling SetGrid()")
}
// Start and end are the same tile
if startX == endX && startY == endY {
return make([]*Point, 0), nil
}
// End point is not an acceptable tile
endTile := p.collisionGrid[endY][endX]
isAcceptable := false
for i := 0; i < len(p.acceptableTiles); i++ {
if endTile == p.acceptableTiles[i] {
isAcceptable = true
break
}
}
if isAcceptable == false {
return make([]*Point, 0), nil
}
p.startX = startX
p.startY = startY
p.endX = endX
p.endY = endY
// TODO is there a more memory-efficient way to do this ?
p.openList = pqueue.New(len(p.collisionGrid[0]) * len(p.collisionGrid))
p.coordinateToNode = make(map[string]*node)
n := newNode(p.startX, p.startY, p.endX, p.endY, nil, straightCost)
p.coordinateToNode[getHashKeyForPoint(startX, startY)] = n
item := &pqueue.Item{Value: n, Priority: int64(n.bestGuessDistance() * costPrecision)}
p.openList.Push(item)
for {
if p.openList.Len() == 0 {
return nil, errors.New("path not found")
}
item, _ := p.openList.PeekAndShift(math.MaxInt64)
parent := item.Value.(*node)
var sn searchNodes
if parent.y > 0 {
sn = append(sn, newSearchNode(0, -1, p.endX, p.endY, straightCost*p.getTileCost(parent.x, parent.y-1), parent))
}
if parent.x < len(p.collisionGrid[0])-1 {
sn = append(sn, newSearchNode(1, 0, p.endX, p.endY, straightCost*p.getTileCost(parent.x+1, parent.y), parent))
}
if parent.y < len(p.collisionGrid)-1 {
sn = append(sn, newSearchNode(0, 1, p.endX, p.endY, straightCost*p.getTileCost(parent.x, parent.y+1), parent))
}
if parent.x > 0 {
sn = append(sn, newSearchNode(-1, 0, p.endX, p.endY, straightCost*p.getTileCost(parent.x-1, parent.y), parent))
}
if p.diagonalsEnabled {
if parent.x > 0 && parent.y > 0 {
if p.allowCornerCutting ||
(isTileWalkable(p.collisionGrid, p.acceptableTiles, parent.x, parent.y-1) &&
isTileWalkable(p.collisionGrid, p.acceptableTiles, parent.x-1, parent.y)) {
sn = append(sn, newSearchNode(-1, -1, p.endX, p.endY, diagonalCost*p.getTileCost(parent.x-1, parent.y-1), parent))
}
}
if parent.x < len(p.collisionGrid[0])-1 && parent.y < len(p.collisionGrid)-1 {
if p.allowCornerCutting ||
(isTileWalkable(p.collisionGrid, p.acceptableTiles, parent.x, parent.y+1) &&
isTileWalkable(p.collisionGrid, p.acceptableTiles, parent.x+1, parent.y)) {
sn = append(sn, newSearchNode(1, 1, p.endX, p.endY, diagonalCost*p.getTileCost(parent.x+1, parent.y+1), parent))
}
}
if parent.x < len(p.collisionGrid[0])-1 && parent.y > 0 {
if p.allowCornerCutting ||
(isTileWalkable(p.collisionGrid, p.acceptableTiles, parent.x, parent.y-1) &&
isTileWalkable(p.collisionGrid, p.acceptableTiles, parent.x+1, parent.y)) {
sn = append(sn, newSearchNode(1, -1, p.endX, p.endY, diagonalCost*p.getTileCost(parent.x+1, parent.y-1), parent))
}
}
if parent.x > 0 && parent.y < len(p.collisionGrid)-1 {
if p.allowCornerCutting ||
(isTileWalkable(p.collisionGrid, p.acceptableTiles, parent.x, parent.y+1) &&
isTileWalkable(p.collisionGrid, p.acceptableTiles, parent.x-1, parent.y)) {
sn = append(sn, newSearchNode(-1, 1, p.endX, p.endY, diagonalCost*p.getTileCost(parent.x-1, parent.y+1), parent))
}
}
}
// First sort all of the potential nodes we could search by their cost + heuristic distance
sort.Sort(sn)
// Search all of the adjacent nodes
for i := 0; i < len(sn); i++ {
adjacentCoordinateX := sn[i].parent.x + sn[i].x
adjacentCoordinateY := sn[i].parent.y + sn[i].y
hashKey := getHashKeyForPoint(adjacentCoordinateX, adjacentCoordinateY)
_, exists := p.additionalPointsToAvoid[hashKey]
if !exists {
if sn[i].endX == adjacentCoordinateX && sn[i].endY == adjacentCoordinateY {
var path []*Point
path = append(path, &Point{X: adjacentCoordinateX, Y: adjacentCoordinateY}, &Point{X: sn[i].parent.x, Y: sn[i].parent.y})
parent := sn[i].parent
for {
parent = parent.parent
if parent == nil {
break
}
path = append(path, &Point{X: parent.x, Y: parent.y})
}
// Reverse path slice
for i := len(path)/2 - 1; i >= 0; i-- {
opp := len(path) - 1 - i
path[i], path[opp] = path[opp], path[i]
}
return path, nil
}
if isTileWalkable(p.collisionGrid, p.acceptableTiles, adjacentCoordinateX, adjacentCoordinateY) {
existingNode, exists := p.coordinateToNode[hashKey]
if exists {
if sn[i].parent.costSoFar+sn[i].cost < existingNode.costSoFar {
existingNode.costSoFar = sn[i].parent.costSoFar + sn[i].cost
existingNode.parent = sn[i].parent
}
} else {
n := newNode(adjacentCoordinateX, adjacentCoordinateY, p.endX, p.endY, sn[i].parent, sn[i].cost)
p.coordinateToNode[hashKey] = n
item := &pqueue.Item{Value: n, Priority: int64(n.bestGuessDistance() * costPrecision)}
p.openList.Push(item)
}
}
}
}
}
return nil, errors.New("path not found")
}
func (p *Pathfinder) getTileCost(x int, y int) float64 {
pos := getHashKeyForPoint(x, y)
cost, found := p.additionalPointsToCost[pos]
if !found {
cost = p.costMap[p.collisionGrid[y][x]]
}
return cost
}
func isTileWalkable(grid [][]int, acceptableTiles []int, x int, y int) bool {
for i := 0; i < len(acceptableTiles); i++ {
if grid[y][x] == acceptableTiles[i] {
return true
}
}
return false
}
func getHashKeyForPoint(x int, y int) string {
return string(x) + "_" + string(y)
}
func getDistance(x1 int, y1 int, x2 int, y2 int) float64 {
xx := float64(x2 - x1)
yy := float64(y2 - y1)
return math.Sqrt(xx*xx + yy*yy)
}