-
Notifications
You must be signed in to change notification settings - Fork 1
/
pathing.go
296 lines (270 loc) · 6.93 KB
/
pathing.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
package scl
import (
"bitbucket.org/aisee/minilog"
"github.com/aiseeq/s2l/lib/grid"
"github.com/aiseeq/s2l/lib/point"
"math"
"time"
)
type Cell struct {
point.Point
Distance float64
}
type CellsPair struct{ src, dst *Cell }
type Queue map[float64][]CellsPair
type Steps map[point.Point]*Cell // from -> next step
type Paths struct {
Steps Steps
Queue Queue
B MapAccessor
}
func (paths *Paths) Present(p point.Point) bool {
_, ok := paths.Steps[p]
return ok
}
func (paths *Paths) AddCell(src, dst *Cell, reaper bool) {
if paths.Present(src.Point) {
return
}
paths.Steps[src.Point] = dst
var d float64
for n, p := range src.Neighbours8(1) {
if paths.Present(p) {
continue
}
distMul := 1.0
if !paths.B.IsPathable(p) {
if !reaper || paths.B.IsBuildable(p) {
continue
}
p += p - src.Point // Shift 1 cell further
if !paths.B.IsBuildable(p) || !paths.B.IsPathable(p) ||
paths.B.HeightAt(src.Point) == paths.B.HeightAt(p) {
continue
}
// So, here p should be 2 cells from src, it is buildable and pathable at different height
// But point between them isn't buildable or pathable => reaper can jump here
distMul = 2.0
}
if n < 4 {
d = src.Distance + distMul // Straight
} else {
d = src.Distance + distMul*math.Sqrt2 // Diagonal
}
nc := &Cell{
Point: p,
Distance: d,
}
paths.Queue[d] = append(paths.Queue[d], CellsPair{src: nc, dst: src})
}
}
func (q Queue) MinDist() ([]CellsPair, float64) {
var cps []CellsPair
minK := math.Inf(1)
for k, v := range q {
if k < minK {
cps = v
minK = k
}
}
return cps, minK
}
func (steps Steps) From(ptr point.Pointer) point.Points {
p := ptr.Point().Floor()
ps := point.Points{p}
for x := 0; x < 1000; x++ {
c, ok := steps[p]
if !ok {
return nil
}
p = c.Point
ps.Add(p)
if c.Distance == 0 {
return ps
}
}
log.Errorf("Can't find path from %v", ptr)
return nil
}
func (steps Steps) Follow(ptr point.Pointer, limit int) point.Point {
p := ptr.Point().Floor()
for x := 0; x < limit; x++ {
c, ok := steps[p]
if !ok {
return 0
}
p = c.Point
}
return p
}
func (b *Bot) FindPaths(grid *grid.Grid, ptr point.Pointer, reaper bool) Steps {
paths := Paths{
Steps: Steps{},
Queue: Queue{},
B: grid,
}
initCell := &Cell{Point: ptr.Point().Floor(), Distance: 0}
// Last point of path links to itself
paths.AddCell(initCell, initCell, reaper)
for cps, key := paths.Queue.MinDist(); cps != nil; cps, key = paths.Queue.MinDist() {
delete(paths.Queue, key) // Clear queue element to not repeat it
for _, cp := range cps {
paths.AddCell(cp.src, cp.dst, reaper)
}
}
return paths.Steps
}
func (paths *Paths) FindPathableCell(src, dst *Cell) point.Point {
if paths.Present(src.Point) {
return 0
}
paths.Steps[src.Point] = dst
var d float64
for n, p := range src.Neighbours8(1) {
if paths.Present(p) {
continue
}
if paths.B.IsPathable(p) {
return p
}
if n < 4 {
d = src.Distance + 1 // Straight
} else {
d = src.Distance + math.Sqrt2 // Diagonal
}
nc := &Cell{
Point: p,
Distance: d,
}
paths.Queue[d] = append(paths.Queue[d], CellsPair{src: nc, dst: src})
}
return 0
}
func (b *Bot) FindClosestPathable(grid *grid.Grid, ptr point.Pointer) point.Point {
if grid.IsPathable(ptr) {
return ptr.Point().Floor()
}
paths := Paths{
Steps: Steps{},
Queue: Queue{},
B: grid,
}
initCell := &Cell{Point: ptr.Point().Floor(), Distance: 0}
// Last point of path links to itself
if p := paths.FindPathableCell(initCell, initCell); p != 0 {
return p
}
for cps, key := paths.Queue.MinDist(); cps != nil; cps, key = paths.Queue.MinDist() {
delete(paths.Queue, key) // Clear queue element to not repeat it
for _, cp := range cps {
if p := paths.FindPathableCell(cp.src, cp.dst); p != 0 {
return p
}
}
}
return 0
}
func (b *Bot) FindHomeMineralsVector() {
var vec point.Point
homeMinerals := b.Units.Minerals.All().CloserThan(ResourceSpreadDistance, b.Locs.MyStart)
if homeMinerals.Exists() {
vec = homeMinerals.Center().Dir(b.Locs.MyStart)
}
if vec.Len() <= 1 {
vec = b.Locs.MyStart.Dir(b.Locs.MapCenter)
}
b.Locs.MyStartMinVec = vec
}
func (b *Bot) RenewPaths(stop <-chan struct{}) {
for {
b.Grid.Lock()
navGrid := grid.New(b.Grid.StartRaw, b.Grid.MapState)
b.Grid.Unlock()
safeGrid := grid.New(navGrid.StartRaw, navGrid.MapState)
reapersExists := b.ReaperExists
var reaperGrid, reaperSafeGrid *grid.Grid
if reapersExists {
reaperGrid = grid.New(navGrid.StartRaw, navGrid.MapState)
reaperSafeGrid = grid.New(navGrid.StartRaw, navGrid.MapState)
}
lastLoop := b.Loop
// s := time.Now()
b.WayMap = b.FindWaypointsMap(navGrid)
if reapersExists {
pa := b.Info.StartRaw.PlayableArea
for y := pa.P0.Y; y <= pa.P1.Y; y++ {
for x := pa.P0.X; x <= pa.P1.X; x++ {
p := point.Pt(float64(x), float64(y))
if reaperGrid.IsPathable(p) {
continue
}
// if points on left & right or up & down are pathable and on different height,
// reaper can jump there
pr := p + 1
pl := p - 1
pu := p + 1i
pd := p - 1i
pur := p + 1 + 1i
pdr := p + 1 - 1i
pdl := p - 1 - 1i
pul := p - 1 + 1i
if (reaperGrid.IsPathable(pl) && reaperGrid.IsPathable(pr) &&
reaperGrid.HeightAt(pl) != reaperGrid.HeightAt(pr)) ||
(reaperGrid.IsPathable(pu) && reaperGrid.IsPathable(pd) &&
reaperGrid.HeightAt(pu) != reaperGrid.HeightAt(pd)) ||
(reaperGrid.IsPathable(pur) && reaperGrid.IsPathable(pdl) &&
reaperGrid.HeightAt(pur) != reaperGrid.HeightAt(pdl)) ||
(reaperGrid.IsPathable(pul) && reaperGrid.IsPathable(pdr) &&
reaperGrid.HeightAt(pul) != reaperGrid.HeightAt(pdr)) {
reaperGrid.SetPathable(p, true)
reaperSafeGrid.SetPathable(p, true)
}
}
}
b.ReaperGrid = reaperGrid
b.ReaperWayMap = b.FindWaypointsMap(b.ReaperGrid)
}
// s := time.Now()
for _, u := range b.Enemies.AllReady {
pos := u.Point().Floor()
ps := b.U.GroundAttackCircle[u.UnitType]
for _, p := range ps {
safeGrid.SetPathable(pos+p, false)
if reapersExists {
reaperSafeGrid.SetPathable(pos+p, false)
}
}
}
// todo: add effects
b.SafeGrid = safeGrid
b.SafeWayMap = b.FindWaypointsMap(b.SafeGrid)
if reapersExists {
b.ReaperSafeGrid = reaperSafeGrid
b.ReaperSafeWayMap = b.FindWaypointsMap(b.ReaperSafeGrid)
}
/*log.Info(time.Now().Sub(s))
wps := point.Points{}
for p := range b.SafeWayMap {
wps = append(wps, p.Point)
}
path, _ := NavPath(b.SafeGrid, b.SafeWayMap, b.Locs.MyStart-3, b.Locs.EnemyStart-3)
b.Grid.Lock() // prevents grid rewrite because debug uses b.Grid
b.DebugPath(wps, White)
b.DebugPath(path, Yellow)
b.Grid.Unlock()
b.DebugSend()*/
select {
case <-stop:
return
default:
}
for lastLoop+B.FramesPerOrder > b.Loop {
time.Sleep(time.Millisecond)
select { // Yes, twice. Because if b.Loop doesn't change this will loop forever
case <-stop:
return
default:
}
}
}
}