-
Notifications
You must be signed in to change notification settings - Fork 1
/
navmesh.go
191 lines (174 loc) · 4.29 KB
/
navmesh.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
package scl
import (
"github.com/aiseeq/s2l/lib/grid"
"github.com/aiseeq/s2l/lib/point"
"github.com/beefsack/go-astar"
"math"
)
type Waypoint struct {
point.Point
M WaypointsMap
}
type Waypoints []*Waypoint
type WaypointsMap map[*Waypoint]Waypoints
func (wpm WaypointsMap) NewWaypoint(p point.Point) *Waypoint {
return &Waypoint{
Point: p,
M: wpm,
}
}
func BresenhamsLineDrawable(p0, p1 point.Point, grid *grid.Grid) bool {
x0 := int(p0.X())
y0 := int(p0.Y())
x1 := int(p1.X())
y1 := int(p1.Y())
// implemented straight from WP pseudocode
dx := x1 - x0
if dx < 0 {
dx = -dx
}
dy := y1 - y0
if dy < 0 {
dy = -dy
}
var sx, sy int
if x0 < x1 {
sx = 1
} else {
sx = -1
}
if y0 < y1 {
sy = 1
} else {
sy = -1
}
err := dx - dy
for {
if !grid.IsPathableFast(x0, y0) {
return false
}
if x0 == x1 && y0 == y1 {
return true
}
e2 := 2 * err
if e2 > -dy {
err -= dy
x0 += sx
}
if e2 < dx {
err += dx
y0 += sy
}
}
}
func (b *Bot) FindWaypoints(wpm WaypointsMap, grid *grid.Grid) Waypoints {
waypoints := Waypoints{}
pa := b.Info.StartRaw.PlayableArea
p0x := int(pa.P0.X)
p0y := int(pa.P0.Y)
p1x := int(pa.P1.X)
p1y := int(pa.P1.Y)
for y := p0y; y <= p1y; y++ {
for x := p0x; x <= p1x; x++ {
if !grid.IsPathableFast(x, y) {
continue
}
for _, dy := range []int{-1, 1} {
for _, dx := range []int{-1, 1} {
if !grid.IsPathableFast(x+dx, y+dy) &&
grid.IsPathableFast(x+dx, y) &&
grid.IsPathableFast(x, y+dy) &&
// Diagonal lines optimization
(grid.IsPathableFast(x+2*dx, y) || grid.IsPathableFast(x, y+2*dy)) {
waypoints = append(waypoints, wpm.NewWaypoint(point.Pt(float64(x), float64(y))))
break
}
}
}
}
}
return waypoints
}
func FindNeighbours(wpm WaypointsMap, from *Waypoint, waypoints Waypoints, skip int, grid *grid.Grid) Waypoints {
neighbours := Waypoints{}
for _, to := range waypoints[skip:] {
if to != from && BresenhamsLineDrawable(from.Point, to.Point, grid) {
wpm[from] = append(wpm[from], to)
wpm[to] = append(wpm[to], from)
}
}
return neighbours
}
func (b *Bot) FindWaypointsMap(grid *grid.Grid) WaypointsMap {
wpm := WaypointsMap{}
// start := time.Now()
waypoints := b.FindWaypoints(wpm, grid)
// log.Info(time.Now().Sub(start))
// start := time.Now()
for skip, waypoint := range waypoints {
FindNeighbours(wpm, waypoint, waypoints, skip+1, grid)
}
// log.Info(time.Now().Sub(start))
return wpm
}
// PathNeighbors returns the neighbors of the tile, excluding blockers and
// tiles off the edge of the board.
func (t *Waypoint) PathNeighbors() []astar.Pather {
// return t.M[t] - possible if: type WaypointsMap map[*Waypoint][]astar.Pather
var neighbors = make([]astar.Pather, len(t.M[t]))
for k, p := range t.M[t] {
neighbors[k] = p
}
return neighbors
}
// PathNeighborCost returns the movement cost of the directly neighboring tile.
func (t *Waypoint) PathNeighborCost(to astar.Pather) float64 {
return t.PathEstimatedCost(to)
}
func (t *Waypoint) PathEstimatedCost(to astar.Pather) float64 {
t2 := to.(*Waypoint)
delta := t.Point - t2.Point
return math.Hypot(real(delta), imag(delta))
}
func NavPath(grid *grid.Grid, wpm WaypointsMap, fromPtr, toPtr point.Pointer) (point.Points, float64) {
var f, t *Waypoint
from := fromPtr.Point().Floor()
to := toPtr.Point().Floor()
if BresenhamsLineDrawable(from, to, grid) {
// point.Points are on the pathable line, we don't need to search anything
return point.Points{from, to}, from.Dist(to)
}
var wps = make(Waypoints, 0, len(wpm)+2)
// Maybe one of points is already in the waypoints map, let's find it
for wp := range wpm {
if wp.Point == from {
f = wp
} else if wp.Point == to {
t = wp
}
wps = append(wps, wp)
}
// If it isn't, we need to add start and end points to the map
if f == nil {
f = wpm.NewWaypoint(from)
FindNeighbours(wpm, f, wps, 0, grid)
wps = append(wps, f)
}
if t == nil {
t = wpm.NewWaypoint(to)
FindNeighbours(wpm, t, wps, 0, grid)
wps = append(wps, t)
}
// start := time.Now()
path, dist, found := astar.Path(t, f) // Params in reverse order because astar.Path returns reversed list
// log.Info(time.Now().Sub(start))
if !found {
return nil, 0
}
var ps point.Points
for _, i := range path {
t := i.(*Waypoint)
ps.Add(t.Point)
}
return ps, dist
}