-
Notifications
You must be signed in to change notification settings - Fork 1
/
unit.go
1009 lines (902 loc) · 27.8 KB
/
unit.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package scl
import (
"github.com/aiseeq/s2l/lib/grid"
"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/effect"
"github.com/aiseeq/s2l/protocol/enums/neutral"
"github.com/aiseeq/s2l/protocol/enums/protoss"
"github.com/aiseeq/s2l/protocol/enums/terran"
"github.com/aiseeq/s2l/protocol/enums/zerg"
"math"
)
type Unit struct {
api.Unit
SpamCmds bool
CmdSet bool
HPS float64
Hits float64
HitsMax float64
HitsLost float64
Abilities []api.AbilityID
IrrAbilities []api.AbilityID
PosDelta point.Point
Neighbours Units
Cluster *Cluster
}
type Cost struct {
Minerals int
Vespene int
Food int
Time int
}
type Weapon struct {
ground, air *api.Weapon
groundDps, airDps float64
groundDamage, airDamage float64
}
type UnitOrder struct {
Loop int
Ability api.AbilityID
Pos point.Point
Tag api.UnitTag
}
func (b *Bot) InitUnits(typeData []*api.UnitTypeData) {
b.U.Types = typeData
for _, td := range b.U.Types {
b.U.Attributes[td.UnitId] = map[api.Attribute]bool{}
for _, attribute := range td.Attributes {
b.U.Attributes[td.UnitId][attribute] = true
}
cost := Cost{
Minerals: int(td.MineralCost),
Vespene: int(td.VespeneCost),
Food: int(td.FoodRequired - td.FoodProvided),
Time: int(td.BuildTime),
}
// fixes
if td.Race == api.Race_Zerg && b.U.Attributes[td.UnitId][api.Attribute_Structure] {
cost.Minerals -= 50 // Why there is drone cost in buildings cost?
}
if td.AbilityId == ability.Train_Zergling {
cost.Minerals = 50
cost.Food = 1
}
if td.AbilityId == ability.Morph_OrbitalCommand || td.AbilityId == ability.Morph_PlanetaryFortress {
cost.Minerals -= 400 // Deduct CC price
}
if td.UnitId == terran.Bunker {
weapon := *b.U.Types[terran.Marine].Weapons[0] // Make copy
td.Weapons = append(td.Weapons, &weapon)
td.Weapons[0].Attacks *= 4 // 4 marines
td.Weapons[0].Range++ // Bunker range boost
}
if td.UnitId == terran.Battlecruiser && len(td.Weapons) == 0 { // No weapons defined
td.Weapons = []*api.Weapon{
{
Type: api.Weapon_Air,
Damage: 5,
Attacks: 1,
Range: 6,
Speed: 0.224, // Cooldown: 0.16 seconds / 16 frames * 22.4 frames = 0.224
},
{
Type: api.Weapon_Ground,
Damage: 8,
Attacks: 1,
Range: 6,
Speed: 0.224,
},
}
}
b.U.UnitCost[td.UnitId] = cost
b.U.AbilityCost[td.AbilityId] = cost
b.U.AbilityUnit[td.AbilityId] = td.UnitId
b.U.UnitAbility[td.UnitId] = td.AbilityId
w := Weapon{}
for _, weapon := range td.Weapons {
if weapon.Type == api.Weapon_Ground || weapon.Type == api.Weapon_Any {
w.ground = weapon
w.groundDamage = float64(weapon.Damage * float32(weapon.Attacks))
w.groundDps = w.groundDamage / float64(weapon.Speed)
// No ground weapons radius for liberators. Evading via effects
if td.UnitId == terran.Liberator || td.UnitId == terran.LiberatorAG {
w.ground.Range = -2
}
}
if weapon.Type == api.Weapon_Air || weapon.Type == api.Weapon_Any {
w.air = weapon
w.airDamage = float64(weapon.Damage * float32(weapon.Attacks))
w.airDps = w.airDamage / float64(weapon.Speed)
}
b.U.Weapons[td.UnitId] = w
}
b.U.UnitAliases.Add(td)
// find cells of ground attack circles for units
if b.U.Weapons[td.UnitId].ground != nil {
r := float64(b.U.Weapons[td.UnitId].ground.Range)
r += 2 // Max unit radius + max target radius
r2 := r * r
ps := point.Points{}
// Count from center of the cell where unit is
for y := -math.Ceil(r); y <= math.Ceil(r); y++ {
for x := -math.Ceil(r); x <= math.Ceil(r); x++ {
if x*x+y*y <= r2 {
ps.Add(point.Pt(x, y))
}
}
}
b.U.GroundAttackCircle[td.UnitId] = ps
}
}
}
func (b *Bot) InitUpgrades(upgradeData []*api.UpgradeData) {
b.U.Upgrades = upgradeData
for _, ud := range b.U.Upgrades {
cost := Cost{
Minerals: int(ud.MineralCost),
Vespene: int(ud.VespeneCost),
Food: 0,
Time: int(ud.ResearchTime),
}
b.U.AbilityCost[ud.AbilityId] = cost
// api bug workaround: TerranVehicleArmorsLevel1 -> Research_TerranVehicleAndShipPlatingLevel1
if ud.AbilityId == 852 {
b.U.AbilityCost[864] = cost
}
if ud.AbilityId == 853 {
b.U.AbilityCost[865] = cost
}
if ud.AbilityId == 854 {
b.U.AbilityCost[866] = cost
}
// log.Info(ud)
}
}
func (b *Bot) InitEffects(effectData []*api.EffectData) {
b.U.Effects = effectData
}
func (b *Bot) NewUnit(unit *api.Unit) (*Unit, bool) {
u := &Unit{
Unit: *unit,
Hits: float64(unit.Health + unit.Shield),
HitsMax: float64(unit.HealthMax + unit.ShieldMax),
}
if u.Alliance == api.Alliance_Neutral || u.DisplayType == api.DisplayType_Snapshot {
return u, false
}
// Check saved orders, because order itself is not in observation yet if B.FramesPerOrder not passed
order, ok := b.U.UnitsOrders[u.Tag]
// If B.FramesPerOrder == 1, game takes order only on second frame
if ok && (order.Loop+B.FramesPerOrder > B.Loop || (B.FramesPerOrder == 1 && order.Loop+1 == B.Loop)) {
uo := api.UnitOrder{AbilityId: order.Ability, Progress: -1} // Progress == -1 means that order is from my DB
if order.Pos != 0 {
uo.Target = &api.UnitOrder_TargetWorldSpacePos{TargetWorldSpacePos: order.Pos.To3D()}
}
if order.Tag != 0 {
uo.Target = &api.UnitOrder_TargetUnitTag{TargetUnitTag: order.Tag}
}
u.Orders = []*api.UnitOrder{&uo} // append(u.Orders, &uo)
}
isNew := true
pu, ok := b.U.PrevUnits[u.Tag]
if ok {
isNew = false
} else {
pu = u
}
hits := b.U.HitsHistory[u.Tag]
if len(hits) > 0 && hits[0] < b.Loop-HitHistoryLoops {
hits = hits[2:]
}
if u.Hits < pu.Hits {
// Received damage
u.HitsLost = pu.Hits - u.Hits
hits = append(hits, b.Loop, int(u.HitsLost))
}
if len(hits) > 0 {
for x, hit := range hits {
if x%2 == 0 {
continue // Skip time
}
u.HPS += float64(hit)
}
u.HPS /= float64(len(hits) / 2)
}
u.PosDelta = pu.Point() - u.Point()
b.U.HitsHistory[u.Tag] = hits
b.U.PrevUnits[u.Tag] = u
return u, isNew
}
func (u *Unit) Point() point.Point {
return point.Pt3(u.Pos)
}
func (u *Unit) Dist(ptr point.Pointer) float64 {
return u.Point().Dist(ptr)
}
func (u *Unit) Dist2(ptr point.Pointer) float64 {
return u.Point().Dist2(ptr)
}
func (u *Unit) Towards(ptr point.Pointer, offset float64) point.Point {
return u.Point().Towards(ptr, offset)
}
func (u *Unit) GetWayMap(safe bool) (*grid.Grid, WaypointsMap) {
var navGrid *grid.Grid
var waymap WaypointsMap
if safe {
navGrid = B.SafeGrid
waymap = B.SafeWayMap
if u.UnitType == terran.Reaper && B.ReaperGrid != nil && B.ReaperWayMap != nil {
navGrid = B.ReaperSafeGrid
waymap = B.ReaperSafeWayMap
}
} else {
navGrid = B.Grid
waymap = B.WayMap
if u.UnitType == terran.Reaper && B.ReaperGrid != nil && B.ReaperWayMap != nil {
navGrid = B.ReaperGrid
waymap = B.ReaperWayMap
}
}
return navGrid, waymap
}
func (u *Unit) GroundTowards(ptr point.Pointer, offset float64, safe bool) point.Point {
navGrid, waymap := u.GetWayMap(safe)
path, _ := NavPath(navGrid, waymap, u, ptr) // This function eats most (~20%) of the main thread
if path.Len() > 1 {
return u.Towards(path[1], offset)
}
return 0
}
func (u *Unit) Is(ids ...api.UnitTypeID) bool {
for _, id := range ids {
if u.UnitType == id {
return true
}
}
return false
}
func (u *Unit) IsNot(ids ...api.UnitTypeID) bool {
return !u.Is(ids...)
}
func (u *Unit) IsIdle() bool {
return len(u.Orders) == 0
}
func (u *Unit) IsUnused() bool {
if u.AddOnTag == 0 {
return len(u.Orders) == 0
}
reactor := B.Units.My.OfType(B.U.UnitAliases.For(terran.Reactor)...).ByTag(u.AddOnTag)
if reactor != nil && reactor.IsReady() {
return len(u.Orders) < 2
}
return len(u.Orders) == 0
}
func (u *Unit) IsMoving() bool {
return len(u.Orders) > 0 && (u.Orders[0].AbilityId == ability.Move || u.Orders[0].AbilityId == ability.Move_Move)
}
// Used to check if it is ok to issue next _move_ order without interrupting current attack action
func (u *Unit) IsCoolToMove() bool {
if delay, ok := B.U.AfterAttack[u.UnitType]; ok && B.Loop-B.U.LastAttack[u.Tag] < delay {
return false
}
return true
// return B.U.AfterAttack.UnitIsCool(u)
}
// Тут нужно определять способен ли юнит нанести удар развернувшись без дополнительной задержки
// Но так же надо следить, успеют ли укусить рипера или другой отступающий юнит
// Пока что надёжнее дожидаться u.WeaponCooldown == 0, тогда
// Рипер убегая от лингов или зилота атакует реже и тем самым может сохранять дистанцию
// Used to check if unit is ready to attack right away
func (u *Unit) IsCoolToAttack() bool {
return u.WeaponCooldown <= 0 // Sometimes it is NEGATIVE!
}
// Used to prevent switches between targets with same priority without actually attacking anything
func (u *Unit) IsAlreadyAttackingTargetInRange() bool {
target := B.Enemies.All.ByTag(u.TargetTag())
if target != nil && u.InRange(target, 0) {
return true
}
return false
}
func (u *Unit) IsVisible() bool {
return u.DisplayType == api.DisplayType_Visible
}
func (u *Unit) IsHidden() bool {
return u.DisplayType == api.DisplayType_Hidden
}
func (u *Unit) IsPosVisible() bool {
return B.Grid.IsVisible(u)
}
var GatheringAbilities = map[api.AbilityID]bool{
ability.Harvest_Gather_SCV: true,
ability.Harvest_Gather_Mule: true,
ability.Harvest_Gather_Drone: true,
ability.Harvest_Gather_Probe: true,
}
func (u *Unit) IsGathering() bool {
return len(u.Orders) > 0 && GatheringAbilities[u.Orders[0].AbilityId]
}
var ReturningAbilities = map[api.AbilityID]bool{
ability.Harvest_Return_SCV: true,
ability.Harvest_Return_Mule: true,
ability.Harvest_Return_Drone: true,
ability.Harvest_Return_Probe: true,
}
func (u *Unit) IsReturning() bool {
return len(u.Orders) > 0 && ReturningAbilities[u.Orders[0].AbilityId]
}
var MineralTypes = map[api.UnitTypeID]bool{
neutral.MineralField: true, neutral.MineralField750: true,
neutral.RichMineralField: true, neutral.RichMineralField750: true,
neutral.PurifierMineralField: true, neutral.PurifierMineralField750: true,
neutral.PurifierRichMineralField: true, neutral.PurifierRichMineralField750: true,
neutral.BattleStationMineralField: true, neutral.BattleStationMineralField750: true,
neutral.LabMineralField: true, neutral.LabMineralField750: true,
}
func (u *Unit) IsMineral() bool {
return MineralTypes[u.UnitType]
}
var GeyserTypes = map[api.UnitTypeID]bool{
neutral.ProtossVespeneGeyser: true, neutral.PurifierVespeneGeyser: true,
neutral.RichVespeneGeyser: true, neutral.ShakurasVespeneGeyser: true,
neutral.SpacePlatformGeyser: true, neutral.VespeneGeyser: true,
}
func (u *Unit) IsGeyser() bool {
return GeyserTypes[u.UnitType]
}
func (u *Unit) IsReady() bool {
return u.BuildProgress == 1
}
func (u *Unit) IsStructure() bool {
return B.U.Attributes[u.UnitType][api.Attribute_Structure]
}
func (u *Unit) IsArmored() bool {
return B.U.Attributes[u.UnitType][api.Attribute_Armored]
}
func (u *Unit) IsLight() bool {
return B.U.Attributes[u.UnitType][api.Attribute_Light]
}
func (u *Unit) IsMechanical() bool {
return B.U.Attributes[u.UnitType][api.Attribute_Mechanical]
}
func (u *Unit) IsWorker() bool {
return u.UnitType == terran.SCV || u.UnitType == terran.MULE ||
u.UnitType == zerg.Drone || u.UnitType == protoss.Probe
}
func (u *Unit) IsDefensive() bool {
return u.UnitType == terran.Bunker || u.UnitType == terran.MissileTurret || u.UnitType == terran.AutoTurret ||
u.UnitType == terran.PlanetaryFortress || u.UnitType == zerg.SpineCrawler || u.UnitType == zerg.SporeCrawler ||
u.UnitType == protoss.PhotonCannon
}
func (u *Unit) HasBuff(b api.BuffID) bool {
for _, buff := range u.BuffIds {
if buff == b {
return true
}
}
return false
}
func (u *Unit) HasAbility(a api.AbilityID) bool {
for _, abil := range u.Abilities {
if abil == a {
return true
}
}
return false
}
func (u *Unit) HasIrrAbility(a api.AbilityID) bool { // Ignore resource requirement
for _, abil := range u.IrrAbilities {
if abil == a {
return true
}
}
return false
}
func (u *Unit) HasTechlab() bool {
if u.AddOnTag != 0 {
tl := B.Units.My.OfType(B.U.UnitAliases.For(terran.TechLab)...).ByTag(u.AddOnTag)
if tl != nil && tl.IsReady() {
return true
}
}
return false
}
func (u *Unit) HasReactor() bool {
if u.AddOnTag != 0 {
tl := B.Units.My.OfType(B.U.UnitAliases.For(terran.Reactor)...).ByTag(u.AddOnTag)
if tl != nil && tl.IsReady() {
return true
}
}
return false
}
func (u *Unit) Speed() float64 {
return float64(B.U.Types[u.UnitType].MovementSpeed)
}
func (u *Unit) GroundDPS() float64 {
return B.U.Weapons[u.UnitType].groundDps
}
func (u *Unit) AirDPS() float64 {
return B.U.Weapons[u.UnitType].airDps
}
func (u *Unit) GroundDamage() float64 {
return B.U.Weapons[u.UnitType].groundDamage
}
func (u *Unit) AirDamage() float64 {
return B.U.Weapons[u.UnitType].airDamage
}
func (u *Unit) IsArmed() bool {
return u.GroundDamage() > 0 || u.AirDamage() > 0
}
func (u *Unit) GroundRange() float64 {
if weapon := B.U.Weapons[u.UnitType].ground; weapon != nil {
return float64(weapon.Range)
}
return -1
}
func (u *Unit) AirRange() float64 {
if weapon := B.U.Weapons[u.UnitType].air; weapon != nil {
return float64(weapon.Range)
}
return -1
}
func (u *Unit) SightRange() float64 {
return float64(B.U.Types[u.UnitType].SightRange)
}
func (u *Unit) RangeDelta(target *Unit, gap float64) float64 {
unitRange := -100.0
if u.GroundDPS() > 0 && Ground(target) {
unitRange = u.GroundRange()
}
// Air range is always larger than ground
if u.AirDPS() > 0 && Flying(target) {
unitRange = u.AirRange()
}
// todo: remove after unit upgrades analysis will be done
if u.Alliance == api.Alliance_Enemy {
if u.UnitType == zerg.Hydralisk || u.UnitType == terran.PlanetaryFortress ||
u.UnitType == terran.MissileTurret || u.UnitType == terran.AutoTurret {
unitRange += 1
}
if u.UnitType == protoss.Phoenix || u.UnitType == protoss.Colossus {
unitRange += 2
}
}
dist := u.Dist(target)
return dist - gap - float64(u.Radius+target.Radius) - unitRange
}
func (u *Unit) InRange(target *Unit, gap float64) bool {
return u.RangeDelta(target, gap) <= 0
}
func (u *Unit) InRangeOf(us Units, gap float64) Units {
return us.CanAttack(u, gap)
}
func (u *Unit) CanAttack(us Units, gap float64) Units {
return us.InRangeOf(u, gap)
}
func (u *Unit) AssessStrength(attackers Units) (outranged, stronger bool) {
closestUnit := attackers.CanAttack(u, 4).ClosestTo(u)
if closestUnit == nil {
stronger = true
return
}
if Ground(u) {
outranged = closestUnit.GroundRange() >= u.GroundRange()
}
if Flying(u) {
outranged = closestUnit.AirRange() >= math.Max(u.GroundRange(), u.AirRange())
}
if outranged {
// 14 - max possible unit range (Tempest)
friendsScore := B.Units.MyAll.CloserThan(14, u).Filter(DpsGt5).Sum(CmpTotalScore)
enemiesScore := B.Enemies.AllReady.CloserThan(14, closestUnit).Filter(DpsGt5).Sum(CmpTotalScore)
// log.Info(friendsScore, enemiesScore, friendsScore*1.25 >= enemiesScore)
if friendsScore*1.25 >= enemiesScore {
stronger = true
}
}
return
}
// these two functions are bad because they consider only one unit to evade. Todo: rewrite
func (u *Unit) AirEvade(enemies Units, gap float64, ptr point.Pointer) (point.Point, bool) { // bool = is safe
pos := ptr.Point()
if enemies.Empty() {
return pos, true // Unit can just move to desired position
}
// Copy of unit
cu := *u
delta := (pos - u.Point()).Norm()
// Move it 1 cell to the new desirable position
cu.Pos = (cu.Point() + delta).To3D()
// Enemy with largest range overlap
hazard := enemies.Min(func(unit *Unit) float64 {
return unit.RangeDelta(&cu, gap)
})
outrange := hazard.RangeDelta(&cu, gap)
// No one can reach our unit
if outrange >= 0 {
return pos, true // Unit can just move to desired position
}
// Move to enemy range border
if outrange > -1 && u.IsNot(terran.Medivac, terran.Raven) { // Close to the border, less than 1
// Not for units without attack
rangeVec := (u.Point() - hazard.Point()).Norm()
tangVec := (rangeVec * 1i).Mul(airSpeedBoostRange)
p1 := u.Point() + tangVec
p2 := u.Point() - tangVec
if p1.Dist2(pos) < p2.Dist2(pos) {
return p1, false
}
return p2, false
}
// Move directly from enemy
return u.Towards(hazard, -airSpeedBoostRange), false
}
func (u *Unit) GroundEvade(enemies Units, gap float64, ptr point.Pointer) (point.Point, bool) { // bool = is safe
pos := ptr.Point()
if enemies.Empty() {
return pos, true // Unit can just move to desired position
}
delta := (pos - u.Point()).Norm()
// Copy of unit
cu := *u
// Move it 1 cell further to the new position
cu.Pos = (cu.Point() + delta).To3D()
// Enemy with largest range overlap
hazard := enemies.Min(func(unit *Unit) float64 {
return unit.RangeDelta(&cu, gap)
})
outrange := hazard.RangeDelta(&cu, gap)
// No one can reach our unit
if outrange >= 0 {
return pos, true // Unit can just move to desired position
}
// Move to enemy range border
var escVec point.Point
if outrange > -1 {
rangeVec := (u.Point() - hazard.Point()).Norm()
tangVec := rangeVec * 1i
p1 := u.Point() + tangVec
p2 := u.Point() - tangVec
if p1.Dist2(pos) < p2.Dist2(pos) {
escVec = p1
} else {
escVec = p2
}
} else {
// Move directly from enemy
escVec = (u.Point() - hazard.Point()).Norm()
}
if !B.Grid.IsPathable(u.Point() + escVec) {
for x := 1.0; x < 4; x++ {
esc1 := u.Point() + escVec.Rotate(math.Pi*2.0/16.0*x)
if B.Grid.IsPathable(esc1) {
return esc1, false
}
esc2 := u.Point() + escVec.Rotate(-math.Pi*2.0/16.0*x)
if B.Grid.IsPathable(esc2) {
return esc2, false
}
}
return B.Locs.MyStart.Towards(B.Locs.MapCenter, -3), false // Try to go home
}
return u.Point() + escVec, false
}
func (u *Unit) GroundFallback(safePos point.Pointer, ignoreAttackAbility bool) {
// ignoreAttackAbility is for cyclone
if !u.IsCoolToMove() || !ignoreAttackAbility && u.IsCoolToAttack() && u.IsAlreadyAttackingTargetInRange() {
return // Don't move until attack is done
}
// fbp, _ := u.GroundFallbackPos(enemies, gap, safePath, 5)
fbp := safePos
navGrid, waymap := u.GetWayMap(true)
if !navGrid.IsPathable(fbp) {
if pos := B.FindClosestPathable(navGrid, fbp); pos != 0 {
fbp = pos
}
}
from := u.Point()
if !navGrid.IsPathable(from) {
if pos := B.FindClosestPathable(navGrid, from); pos != 0 {
from = pos
}
}
if from != 0 {
path, _ := NavPath(navGrid, waymap, u, fbp)
pos := path.FirstFurtherThan(2, u)
if pos != 0 {
fbp = pos
}
}
u.CommandPos(ability.Move, fbp)
}
func (u *Unit) IsCloserThan(dist float64, ptr point.Pointer) bool {
return u.Dist2(ptr) < dist*dist
}
func (u *Unit) IsFurtherThan(dist float64, ptr point.Pointer) bool {
return u.Dist2(ptr) > dist*dist
}
func (u *Unit) IsFarFrom(ptr point.Pointer) bool {
return u.IsFurtherThan(u.SightRange()/2, ptr)
}
func (u *Unit) EstimatePositionAfter(frames int) point.Point {
return u.Point() + u.PosDelta.Norm().Mul(u.Speed()*float64(frames)/22.4)
}
func (u *Unit) FramesToPos(ptr point.Pointer) float64 {
return u.Dist(ptr) / u.Speed() * 22.4
}
func (u *Unit) FramesToDistantPos(ptr point.Pointer) float64 {
return B.RequestPathing(u, ptr) / u.Speed() * 22.4
}
func (u *Unit) TargetAbility() api.AbilityID {
if len(u.Orders) == 0 {
return 0
}
return u.Orders[0].AbilityId
}
func (u *Unit) TargetPos() point.Point {
if len(u.Orders) == 0 {
return 0
}
return point.Pt3(u.Orders[0].GetTargetWorldSpacePos())
}
func (u *Unit) TargetTag() api.UnitTag {
if len(u.Orders) == 0 {
return 0
}
return u.Orders[0].GetTargetUnitTag()
}
func (u *Unit) FindAssignedBuilder(builders Units) *Unit {
for _, builder := range builders {
// log.Info(builder.TargetAbility(), builder.TargetPos(), u.Point(), builder.TargetTag(), u.Tag)
if builder.TargetAbility() == ability.Build_Refinery {
geyser := B.Units.Geysers.All().ByTag(builder.TargetTag())
// log.Info(geyser.Point(), u.Point())
if geyser != nil && geyser.Point() == u.Point() {
return builder
}
}
if builder.TargetPos() == u.Point() || builder.TargetTag() == u.Tag {
return builder
}
}
return nil
}
func (u *Unit) FindAssignedRepairers(reps Units) Units {
us := Units{}
for _, rep := range reps {
if rep.TargetTag() == u.Tag {
us.Add(rep)
}
}
return us
}
type AttackFunc func(u *Unit, priority int, targets Units) bool
type MoveFunc func(u *Unit, target *Unit)
func DefaultAttackFunc(u *Unit, priority int, targets Units) bool {
if closeTargets := targets.Filter(Visible).InRangeOf(u, 0); closeTargets.Exists() {
target := closeTargets.Min(func(unit *Unit) float64 {
return unit.Hits
})
// log.Info(u.UnitType, B.U.BeforeAttack[u.UnitType], B.Loop - B.U.LastAttack[u.Tag])
u.CommandTag(ability.Attack_Attack, target.Tag)
B.U.LastAttack[u.Tag] = B.Loop
return true
} else if u.EvadeEffects() {
return true
} else if !u.IsFlying {
if pos, safe := u.SpreadFromSplash(B.Enemies.AllReady, u); !safe {
u.CommandPos(ability.Move, pos)
return true
}
}
return false
}
func (u *Unit) GetEffectsList() []api.EffectID {
// todo: move into safe grids
effects := []api.EffectID{effect.PsiStorm, effect.CorrosiveBile}
if !u.IsFlying {
effects = append(effects, effect.LiberatorDefenderZoneSetup, effect.LiberatorDefenderZone,
effect.ThermalLance, effect.BlindingCloud, effect.LurkerSpines, effect.TemporalFieldGrowing,
effect.TemporalField)
}
return effects
}
func (u *Unit) EvadeEffects() bool {
pos, safe := u.EvadeEffectsPos(u, true, u.GetEffectsList()...)
if !safe {
u.CommandPos(ability.Move, pos)
return true
}
return false
}
func DefaultMoveFunc(u *Unit, target *Unit) {
// Unit need to be closer to the target to shoot?
if !u.InRange(target, -0.1) || !target.IsVisible() || !target.IsPosVisible() {
u.AttackMove(target)
} else if !u.EvadeEffects() {
if !u.IsFlying {
if pos, safe := u.SpreadFromSplash(B.Enemies.AllReady, u); !safe {
u.CommandPos(ability.Move, pos)
}
}
}
}
func (u *Unit) EvadeEffectsPos(ptr point.Pointer, checkKD8 bool, eids ...api.EffectID) (point.Point, bool) { // bool - is safe
upos := ptr.Point()
// And also reaper mines
if !u.IsFlying && checkKD8 {
kds := append(B.Units.My[terran.KD8Charge], B.Units.Enemy[terran.KD8Charge]...)
if kds.Exists() {
kd := kds.ClosestTo(upos)
gap := upos.Dist(kd) - float64(u.Radius) - KD8Radius - 0.5
// Negative if under effect
if gap < 0 {
// Negative towards = outwards
pos := upos.Towards(kd, gap-1)
if B.Grid.IsPathable(pos) {
return pos, false
}
}
}
}
for _, e := range append(B.Obs.RawData.Effects, B.RecentEffects[0]...) {
for _, eid := range eids {
if e.EffectId == eid {
for _, p2 := range e.Pos {
p := point.Pt2(p2)
gap := upos.Dist(p) - float64(B.U.Effects[eid].Radius+u.Radius) - 0.5
if eid == effect.LiberatorDefenderZone {
gap -= 0.5 // Try to be a little bit safer (or units tend to touch circle and die)
}
if gap < 0 {
pos := upos.Towards(p, gap-1)
if upos == p {
// Rare case when effect is directly above the unit (not so rare vs bots)
pos = upos.Towards(B.Locs.MapCenter, gap-1)
}
return pos, false
}
}
}
}
}
return upos, true
}
var splashRadius = map[api.UnitTypeID]float64{
terran.SiegeTankSieged: 1.25,
terran.WidowMineBurrowed: 1.75,
terran.PlanetaryFortress: 1.25,
protoss.Archon: 1,
zerg.Baneling: 2.2,
zerg.Ultralisk: 2,
}
func (u *Unit) SpreadFromSplash(enemies Units, ptr point.Pointer) (point.Point, bool) { // bool = is safe
var maxRad float64
for _, enemy := range enemies {
if enemy.IsFurtherThan(14, u) {
continue // Calculations optimization
}
rad := splashRadius[enemy.UnitType]
if rad == 0 || !enemy.InRange(u, rad*2) {
continue
}
if rad > maxRad {
maxRad = rad
}
}
if maxRad == 0 || float64(u.Radius) >= maxRad {
return ptr.Point(), true
}
friends := B.Units.MyAll.Filter(func(unit *Unit) bool {
// Take only units that are the same size or larger. So smaller units will move and larger will stand
return unit.Tag != u.Tag && unit.Radius >= u.Radius && unit.IsCloserThan(maxRad+float64(u.Radius), u)
})
if friends.Empty() {
return ptr.Point(), true
}
return u.Towards(friends.Center(), -1), false
}
func (u *Unit) AttackMove(target *Unit) {
dist := u.Dist(target)
rads := float64(u.Radius + target.Radius)
npos := u.Towards(target, math.Min(2, dist-rads))
if !u.IsFlying && dist-rads > 2 {
if p := u.GroundTowards(target, 2, false); p != 0 {
npos = p
}
}
effects := u.GetEffectsList()
pos, safe := u.EvadeEffectsPos(u, true, effects...)
if safe {
pos, safe = u.EvadeEffectsPos(npos, true, effects...)
if safe {
enemies := B.Enemies.AllReady
if !u.IsFlying {
pos, safe = u.SpreadFromSplash(enemies, npos)
}
if safe {
if u.IsFlying {
pos, safe = u.AirEvade(enemies, 2, npos)
} else {
pos, safe = u.GroundEvade(enemies, 2, npos)
}
if !safe && target.Cloak != api.CloakState_Cloaked {
outranged, stronger := u.AssessStrength(enemies)
if !outranged || stronger {
safe = true
}
}
}
}
}
if safe {
// Move closer
u.CommandPos(ability.Move, target)
} else {
u.CommandPos(ability.Move, pos)
}
}
func (u *Unit) AttackCustom(attackFunc AttackFunc, moveFunc MoveFunc, targetsGroups ...Units) {
if B.U.UnitsOrders[u.Tag].Loop+B.FramesPerOrder > B.Loop {
return // Not more than FramesPerOrder
}
// Don't send another attack command or that could switch targets and attack will fail
if u.IsCoolToAttack() && !u.IsAlreadyAttackingTargetInRange() {
// Here we try to shoot at any target close enough
for priority, targets := range targetsGroups {
if attackFunc(u, priority, targets) {
return
}
}
}
// Don't move until previous attack is done
if u.IsCoolToMove() {
// Can't shoot anyone. Move closer to targets
for _, targets := range targetsGroups {
target := targets.ClosestTo(u)
if target == nil {
continue
}
moveFunc(u, target)
return // No orders if unit is close enough
}
}
}
func (u *Unit) Attack(targetsGroups ...Units) { // Targets in priority from higher to lower
u.AttackCustom(DefaultAttackFunc, DefaultMoveFunc, targetsGroups...)
}
func (u *Unit) IsSafeToApproach(p point.Pointer) bool {
if !B.SafeGrid.IsPathable(p) {
if pathablePos := B.FindClosestPathable(B.SafeGrid, p); pathablePos != 0 {
p = pathablePos
}
}
navGrid, waymap := u.GetWayMap(true)
if path, _ := NavPath(navGrid, waymap, u, p); path == nil {
return false
}
return true
}
// Filters
func Idle(u *Unit) bool { return u.IsIdle() }
func Unused(u *Unit) bool { return u.IsUnused() }
func Ready(u *Unit) bool { return u.IsReady() || u.Cloak == api.CloakState_Cloaked }
func NotReady(u *Unit) bool { return !u.IsReady() && u.Cloak != api.CloakState_Cloaked }
func Gathering(u *Unit) bool { return u.IsGathering() }
func Visible(u *Unit) bool { return u.IsVisible() }
func PosVisible(u *Unit) bool { return u.IsPosVisible() }
func Hidden(u *Unit) bool { return u.IsHidden() }
func Structure(u *Unit) bool { return u.IsStructure() }
func NotStructure(u *Unit) bool { return !u.IsStructure() }