-
Notifications
You must be signed in to change notification settings - Fork 1
/
workers.go
427 lines (404 loc) · 12.7 KB
/
workers.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package scl
import (
log "bitbucket.org/aisee/minilog"
"github.com/aiseeq/s2l/lib/point"
"github.com/aiseeq/s2l/protocol/api"
"github.com/aiseeq/s2l/protocol/enums/ability"
"github.com/aiseeq/s2l/protocol/enums/protoss"
"github.com/aiseeq/s2l/protocol/enums/terran"
"github.com/aiseeq/s2l/protocol/enums/zerg"
)
func (b *Bot) InitCCMinerals(cc *Unit, turrets point.Points) {
mfs := B.Units.Minerals.All().CloserThan(ResourceSpreadDistance, cc)
ts := turrets.CloserThan(ResourceSpreadDistance, cc)
dist := float64(mfs.First().Radius + 0.2)
for _, mf := range mfs {
target := mf.Towards(cc, dist)
if mf2 := mfs.CloserThan(dist, target).ClosestTo(target); mf2 != nil && mf.Tag != mf2.Tag {
targetMineral := point.NewCircle(float64(mf.Pos.X), float64(mf.Pos.Y), dist)
closeMineral := point.NewCircle(float64(mf2.Pos.X), float64(mf2.Pos.Y), dist)
pts := point.Intersect(targetMineral, closeMineral)
if len(pts) == 2 {
target = pts.ClosestTo(target)
}
}
if t := ts.CloserThan(1.4, target).ClosestTo(target); t != 0 {
targetMineral := point.NewCircle(float64(mf.Pos.X), float64(mf.Pos.Y), dist)
turret := point.NewCircle(t.X(), t.Y(), 1.4)
pts := point.Intersect(targetMineral, turret)
if len(pts) == 2 {
target = pts.ClosestTo(target)
}
}
b.Miners.TargetForMineral[mf.Tag] = target
}
log.Debugf("Minerals inited for base @ %v", cc.Pos)
}
func (b *Bot) InitMining(turrets point.Points) {
b.Miners.CCForMiner = map[api.UnitTag]api.UnitTag{}
b.Miners.GasForMiner = map[api.UnitTag]api.UnitTag{}
b.Miners.MineralForMiner = map[api.UnitTag]api.UnitTag{}
b.Miners.TargetForMineral = map[api.UnitTag]point.Point{}
b.Miners.LastSeen = map[api.UnitTag]int{}
cc := b.Units.My.OfType(terran.CommandCenter, terran.OrbitalCommand, terran.PlanetaryFortress,
zerg.Hatchery, zerg.Lair, zerg.Hive, protoss.Nexus).First(Ready)
b.InitCCMinerals(cc, turrets)
miners := b.Units.My.OfType(terran.SCV, zerg.Drone, protoss.Probe)
mfs := b.Units.Minerals.All().CloserThan(ResourceSpreadDistance, cc)
for _, mf := range mfs {
miner := miners.ClosestTo(mf)
b.addMinerToMineral(miner, mf, cc)
miners.Remove(miner)
if miners.Empty() {
break
}
}
for _, miner := range miners {
mf := mfs.ClosestTo(miner)
b.addMinerToMineral(miner, mf, cc)
mfs.Remove(mf)
if mfs.Empty() {
break
}
}
}
func (b *Bot) addMinerToMineral(miner, mf, cc *Unit) {
miner.CommandTag(ability.Smart, mf.Tag)
b.Miners.MineralForMiner[miner.Tag] = mf.Tag
b.Miners.CCForMiner[miner.Tag] = cc.Tag
}
func (b *Bot) GetMineralsSaturation(mfs Units) map[api.UnitTag]int {
saturation := map[api.UnitTag]int{}
for _, mf := range mfs {
saturation[mf.Tag] = 0
}
for _, mfTag := range b.Miners.MineralForMiner {
saturation[mfTag]++
}
return saturation
}
func (b *Bot) FillMineralsUpTo2(miners *Units, ccs, allMfs Units) {
if miners.Empty() {
return
}
// Calculate how many workers are already on each crystal
saturation := b.GetMineralsSaturation(allMfs)
nextMiner:
for _, miner := range *miners {
ccs.OrderByDistanceTo(miner, false)
for _, cc := range ccs {
mfs := allMfs.CloserThan(ResourceSpreadDistance, cc)
bestMfs := Units{}
for _, mf := range mfs {
if saturation[mf.Tag] == 0 {
// We found free crystal, use it
b.addMinerToMineral(miner, mf, cc)
miners.Remove(miner)
continue nextMiner
}
if saturation[mf.Tag] == 1 {
// Non-saturated crystal
bestMfs.Add(mf)
}
}
if bestMfs.Exists() {
// Send to closest mineral
mf := bestMfs.ClosestTo(cc)
b.addMinerToMineral(miner, mf, cc)
miners.Remove(miner)
}
}
}
}
func (b *Bot) FillMineralsUpTo3(miners *Units, ccs, allMfs Units) {
if miners.Empty() {
return
}
// Calculate how many workers are already on each crystal
saturation := b.GetMineralsSaturation(allMfs)
for _, miner := range *miners {
ccs.OrderByDistanceTo(miner, false)
for _, cc := range ccs {
mfs := allMfs.CloserThan(ResourceSpreadDistance, cc)
bestMfs := Units{}
for _, mf := range mfs {
if saturation[mf.Tag] == 2 {
// Non-oversaturated crystal
bestMfs.Add(mf)
}
}
if bestMfs.Exists() {
// Send to farest mineral
mf := bestMfs.FurthestTo(cc)
b.addMinerToMineral(miner, mf, cc)
miners.Remove(miner)
}
}
}
}
func (b *Bot) GetGasSaturation(gases Units) map[api.UnitTag]int {
saturation := map[api.UnitTag]int{}
for _, gas := range gases {
saturation[gas.Tag] = 0
}
for _, gasTag := range b.Miners.GasForMiner {
saturation[gasTag]++
}
return saturation
}
func (b *Bot) addMinerToGas(miner, gas, cc *Unit) {
miner.CommandTag(ability.Smart, gas.Tag)
b.Miners.GasForMiner[miner.Tag] = gas.Tag
b.Miners.CCForMiner[miner.Tag] = cc.Tag
}
func (b *Bot) FillGases(miners *Units, ccs, gases Units) {
if miners.Empty() {
return
}
// Calculate how many workers are already on each gas
saturation := b.GetGasSaturation(gases)
nextMiner: // First, fill gases up to 2 workers
for _, miner := range *miners {
ccs.OrderByDistanceTo(miner, false)
for _, cc := range ccs {
localGases := gases.CloserThan(ResourceSpreadDistance, cc)
for _, gas := range localGases {
if saturation[gas.Tag] <= 1 {
// We found free gas, use it
b.addMinerToGas(miner, gas, cc)
saturation[gas.Tag]++
miners.Remove(miner)
continue nextMiner
}
}
}
}
nextMiner2: // If someone is left, fill up to 3
for _, miner := range *miners {
ccs.OrderByDistanceTo(miner, false)
for _, cc := range ccs {
localGases := gases.CloserThan(ResourceSpreadDistance, cc)
for _, gas := range localGases {
if saturation[gas.Tag] == 2 {
// We found free gas, use it
b.addMinerToGas(miner, gas, cc)
saturation[gas.Tag]++
miners.Remove(miner)
continue nextMiner2
}
}
}
}
}
func (b *Bot) MicroMinerals(miners, ccs, enemies Units, safePos point.Pointer, turrets point.Points) {
for _, miner := range miners {
mfTag := b.Miners.MineralForMiner[miner.Tag]
if mfTag == 0 {
continue
}
// This should help in case of reapers
if enemies.CanAttack(miner, 2).Exists() && ccs.Exists() && miner.Hits <= miner.HitsMax/2 {
miner.GroundFallback(safePos, false)
continue
}
if miner.EvadeEffects() {
continue
}
cc := ccs.ByTag(b.Miners.CCForMiner[miner.Tag])
target := b.Miners.TargetForMineral[mfTag]
if target == 0 {
// Minerals are not inited for this CC yet
b.InitCCMinerals(cc, turrets)
}
if miner.IsFurtherThan(4, target) {
miner.GroundFallback(target, true)
continue
}
// If miner not mining the mineral he assigned to (happens if game decides to change worker target)
if miner.IsGathering() && miner.TargetTag() != mfTag {
miner.SpamCmds = true // We need to send the same command again
miner.CommandTag(ability.Smart, mfTag) // To mine designated mineral
continue
}
if len(miner.Orders) == 1 && miner.Orders[0].Progress == -1 {
// Hotfix for B.FramesPerOrder == 1, don't repeat last order again, todo: better?
continue
}
if !miner.IsReturning() && len(miner.Orders) < 2 &&
miner.IsFurtherThan(0.75, target) && miner.IsCloserThan(2, target) {
miner.CommandPos(ability.Move_Move, target)
miner.CommandTagQueue(ability.Smart, mfTag)
}
target = cc.Towards(miner, float64(cc.Radius+miner.Radius))
if miner.IsReturning() && len(miner.Orders) < 2 &&
miner.IsFurtherThan(1, target) && miner.IsCloserThan(2, target) {
miner.CommandPos(ability.Move_Move, target)
miner.CommandTagQueue(ability.Smart, cc.Tag)
}
}
}
func (b *Bot) MicroGas(miners, gases, ccs, enemies Units, safePos point.Pointer) {
if gases.Empty() {
return
}
for _, miner := range miners {
gasTag := b.Miners.GasForMiner[miner.Tag]
if gasTag == 0 {
continue
}
if enemies.CanAttack(miner, 2).Exists() && ccs.Exists() && miner.Hits <= miner.HitsMax/2 {
miner.GroundFallback(safePos, false)
continue
}
if miner.EvadeEffects() {
continue
}
ref := gases.ByTag(gasTag)
target := ref.Towards(miner, float64(gases.First().Radius+miner.Radius))
if miner.IsFurtherThan(4, target) {
miner.GroundFallback(target, true)
continue
}
// If miner not mining the refinery he assigned to
if miner.IsGathering() && miner.TargetTag() != gasTag {
miner.SpamCmds = true // We need to send the same command again
miner.CommandTag(ability.Smart, gasTag)
continue
}
/*if ref.AssignedHarvesters == 3 {
continue // Don't micro them
}
if !miner.IsReturning() && len(miner.Orders) < 2 &&
miner.IsFurtherThan(1, target) && miner.IsCloserThan(2, target) {
miner.CommandPos(ability.Move_Move, target)
miner.CommandTagQueue(ability.Smart, gasTag)
}
cc := ccs.ByTag(b.Miners.CCForMiner[miner.Tag])
target = cc.Towards(miner, float64(cc.Radius+miner.Radius))
if miner.IsReturning() && len(miner.Orders) < 2 &&
miner.IsFurtherThan(1, target) && miner.IsCloserThan(2, target) {
miner.CommandPos(ability.Move_Move, target)
miner.CommandTagQueue(ability.Smart, cc.Tag)
}*/
}
}
func (b *Bot) HandleOversaturation(ccs, allMfs Units) {
undersaturated := 0
oversaturated := map[api.UnitTag]bool{}
for _, cc := range ccs {
mfs := allMfs.CloserThan(ResourceSpreadDistance, cc)
for mfTag, qty := range b.GetMineralsSaturation(mfs) {
if qty < 2 {
undersaturated++
} else if qty > 2 {
oversaturated[mfTag] = true
}
}
}
if undersaturated == 0 || len(oversaturated) == 0 {
// There is nothing we can do
return
}
log.Debugf("Undersaturated: %d, oversaturated: %d", undersaturated, len(oversaturated))
for minerTag, mfTag := range b.Miners.MineralForMiner {
if !oversaturated[mfTag] {
continue
}
// Free miner
delete(b.Miners.MineralForMiner, minerTag)
delete(b.Miners.CCForMiner, minerTag)
oversaturated[mfTag] = false
undersaturated--
if undersaturated == 0 {
return
}
}
}
// balance - minerals to gas gather ratio, ex: 2 => gather more vespene if it is less than minerals * 2
func (b *Bot) HandleMiners(miners, ccs, enemies Units, balance float64, safePos point.Pointer, turrets point.Points) {
if miners.Empty() || ccs.Empty() {
return
}
// Some CCs may be abandoned because of enemy attacks. Workers should leave them
ccTags := ccs.TagsMap()
// Some minerals may be gone
mfs := b.Units.Minerals.All()
mfsTags := mfs.TagsMap()
// Gases too
gases := b.Units.My.OfType(terran.Refinery, terran.RefineryRich, zerg.Extractor, zerg.ExtractorRich,
protoss.Assimilator, protoss.AssimilatorRich).Filter(Ready, func(unit *Unit) bool {
return unit.VespeneContents > 0
})
gasesTags := gases.TagsMap()
if mfs.Empty() && gases.Empty() {
return
}
b.HandleOversaturation(ccs, mfs) // Call it less often if it will overload cpu
// Free workers pool
pool := miners.Filter(func(unit *Unit) bool {
if unit.IsIdle() || !ccTags[b.Miners.CCForMiner[unit.Tag]] {
delete(b.Miners.MineralForMiner, unit.Tag)
delete(b.Miners.GasForMiner, unit.Tag)
return true
}
// Assigned mineral doesn't exist anymore
if b.Miners.MineralForMiner[unit.Tag] != 0 && !mfsTags[b.Miners.MineralForMiner[unit.Tag]] {
delete(b.Miners.MineralForMiner, unit.Tag)
delete(b.Miners.CCForMiner, unit.Tag)
return true
}
// Assigned refinery empty or doesn't exist anymore
if b.Miners.GasForMiner[unit.Tag] != 0 && !gasesTags[b.Miners.GasForMiner[unit.Tag]] {
delete(b.Miners.GasForMiner, unit.Tag)
delete(b.Miners.CCForMiner, unit.Tag)
return true
}
return false
})
// Update memory list
for _, miner := range miners {
b.Miners.LastSeen[miner.Tag] = b.Loop
}
// Check if some units were excluded from miners group
for minerTag, loop := range b.Miners.LastSeen {
if loop+45 > b.Loop {
continue // Probably, still alive but maybe somewhere in refinery
}
// Remove unit from all lists, so his vacant place can be occupied again
delete(b.Miners.CCForMiner, minerTag)
delete(b.Miners.GasForMiner, minerTag)
delete(b.Miners.MineralForMiner, minerTag)
}
b.MicroMinerals(miners, ccs, enemies, safePos, turrets)
b.MicroGas(miners, gases, ccs, enemies, safePos)
if pool.Empty() {
return
}
if b.MineralsPerFrame*balance <= b.VespenePerFrame {
b.FillMineralsUpTo2(&pool, ccs, mfs)
if balance > 0 {
b.FillGases(&pool, ccs, gases)
}
} else {
b.FillGases(&pool, ccs, gases)
b.FillMineralsUpTo2(&pool, ccs, mfs)
}
b.FillMineralsUpTo3(&pool, ccs, mfs)
// todo: something with the rest?
}
func (b *Bot) RedistributeWorkersToRefineryIfNeeded(ref *Unit, miners Units, limit int) {
miners.OrderByDistanceTo(ref, false)
freed := 0
for _, miner := range miners {
// Should mine mineral and not carrying anything
if b.Miners.MineralForMiner[miner.Tag] != 0 && len(miner.BuffIds) == 0 {
// Move to free pool
delete(b.Miners.CCForMiner, miner.Tag)
delete(b.Miners.MineralForMiner, miner.Tag)
freed++
if freed >= limit {
return
}
}
}
}