-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
947 lines (842 loc) · 29.5 KB
/
main.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
package main
import (
"errors"
"flag"
"fmt"
"log"
"math"
"math/rand"
"os"
"os/signal"
"sort"
"syscall"
"github.com/golang/protobuf/proto"
"github.com/grantmd/go-s2client/sc2proto"
)
var gamePort = flag.Int("GamePort", 5677, "Ladder server port")
var startPort = flag.Int("StartPort", 5690, "Ladder server game port")
var ladderServer = flag.String("LadderServer", "localhost", "Address of ladder server")
var opponentID = flag.String("OpponentId", "", "Ladder ID of opponent")
var quitRequested bool
var isMultiplayer bool
var realtime bool
var abilities []*SC2APIProtocol.AbilityData // Most Useful: AbilityId, Available, FootprintRadius
var units []*SC2APIProtocol.UnitTypeData // Most Useful: UnitId, Available, MineralCost, VespeneCost, FoodRequired
var upgrades []*SC2APIProtocol.UpgradeData // Most Useful: UpgradeId, MineralCost, VespeneCost
var buffs []*SC2APIProtocol.BuffData // Most Useful: BuffId
var mapUnits [][]uint32
func main() {
// Parse command line args and configure logging
flag.Parse()
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds)
realtime = false
isMultiplayer = true
// Connect to game server
addr := fmt.Sprintf("%s:%d", *ladderServer, *gamePort)
var c Conn
log.Printf("Connecting to %s…", addr)
err := c.Dial(&addr)
if err != nil {
log.Fatal("dial:", err)
}
log.Println("Successfully connected!")
protocol := &Protocol{
conn: &c,
}
defer protocol.Disconnect()
// Setup signal handling
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
log.Println("Quit requested")
protocol.Disconnect()
os.Exit(1)
}()
// Join the game
CurrentPort := *startPort
CurrentPort++
ReqGamePort := CurrentPort
CurrentPort++
ReqServerGamePort := CurrentPort
CurrentPort++
ReqServerBasePort := CurrentPort
CurrentPort++
ReqClientGamePort := CurrentPort
CurrentPort++
ReqClientBasePort := CurrentPort
req := &SC2APIProtocol.Request{
Request: &SC2APIProtocol.Request_JoinGame{
JoinGame: &SC2APIProtocol.RequestJoinGame{
Participation: &SC2APIProtocol.RequestJoinGame_Race{
Race: SC2APIProtocol.Race_Terran,
},
Options: &SC2APIProtocol.InterfaceOptions{
Raw: proto.Bool(true),
Score: proto.Bool(true),
},
ServerPorts: &SC2APIProtocol.PortSet{
GamePort: proto.Int(ReqServerGamePort),
BasePort: proto.Int(ReqServerBasePort),
},
ClientPorts: []*SC2APIProtocol.PortSet{
&SC2APIProtocol.PortSet{
GamePort: proto.Int(ReqClientGamePort),
BasePort: proto.Int(ReqClientBasePort),
},
},
SharedPort: proto.Int(ReqGamePort),
},
},
}
// Should change indentation here, probably need some auto-format tool at some point, but cba right now
log.Println("Joining game…")
err = protocol.SendRequest(req)
if err != nil {
log.Fatal("Could not send join game request:", err)
}
resp, err := protocol.ReadResponse()
if err != nil {
log.Fatal("Could not receive join game response:", err)
}
log.Println("Game joined:", resp)
// Get game info
req = &SC2APIProtocol.Request{
Request: &SC2APIProtocol.Request_GameInfo{
GameInfo: &SC2APIProtocol.RequestGameInfo{},
},
}
log.Println("Requesting game info…")
err = protocol.SendRequest(req)
if err != nil {
log.Fatal("Could not send game info request:", err)
}
resp, err = protocol.ReadResponse()
if err != nil {
log.Fatal("Could not receive game info response:", err)
}
if len(resp.GetGameInfo().PlayerInfo) > 1 {
isMultiplayer = true
log.Println("Game is multiplayer")
}
// TODO: StartRaw has tons of map data in raw.pb.go
// Need to figure out what ImageData.Data is. Unit type per map unit? Top to bottom or left to right?
// PathingGrid might be great for figuring out where to send units, but the game kind of figures this out for us
// PlacementGrid is maybe indicative of where units are on map start?
// PlayableArea is maybe where units are placeable vs full map size?
mapX := resp.GetGameInfo().GetStartRaw().GetMapSize().GetX()
mapY := resp.GetGameInfo().GetStartRaw().GetMapSize().GetY()
mapUnits := make([][]uint32, mapY)
for i := range mapUnits {
mapUnits[i] = make([]uint32, mapX)
}
log.Println("Game data received")
// Get game data
req = &SC2APIProtocol.Request{
Request: &SC2APIProtocol.Request_Data{
Data: &SC2APIProtocol.RequestData{
AbilityId: proto.Bool(true),
UnitTypeId: proto.Bool(true),
UpgradeId: proto.Bool(true),
BuffId: proto.Bool(true),
},
},
}
log.Println("Requesting game data")
err = protocol.SendRequest(req)
if err != nil {
log.Fatal("Could not send game data request:", err)
}
resp, err = protocol.ReadResponse()
if err != nil {
log.Fatal("Could not receive game data response:", err)
}
abilities = resp.GetData().GetAbilities()
units = resp.GetData().GetUnits()
upgrades = resp.GetData().GetUpgrades()
buffs = resp.GetData().GetBuffs()
log.Println("Game data received")
// TODO: Grab all units now and record on the map where they are?
// Game loop
for {
// Request observation
req = &SC2APIProtocol.Request{
Request: &SC2APIProtocol.Request_Observation{
Observation: &SC2APIProtocol.RequestObservation{},
},
}
err = protocol.SendRequest(req)
if err != nil {
log.Fatal("Could not send request:", err)
}
// Read the game state result
resp, err = protocol.ReadResponse()
if err != nil {
log.Fatal("Could not receive response:", err)
}
respObs := resp.GetObservation()
// respObs.Actions - list of actions performed
// respObs.ActionErrors - list of actions which did not complete
// respObs.Observation - whole mess of observation data. see struct and notes below
// respObs.PlayerResult - result of game, only if ended that step
// respObs.Chat - chat messages received. could be fun. maybe take commands from chat?
obs := respObs.GetObservation()
rawData := obs.GetRawData()
// obs.PlayerCommon - looks like player state. minerals, vespene, units, etc. very useful
// obs.Alerts - critical end game actions like nuclear launch detected
// obs.Abilities - list of available abilities? unclear. definitiion of AvailableAbility is in common.pb.go
// obs.Score - I think how well you're doing, if you request score mode at game start
// obs.RawData - Raw game data. Where the meat is of what we can see and do. Look at raw.pb.go for all the info
// obs.FeatureLayerData - Probably not available unless you pick that game mode. Image based?
// obs.RenderData - Full fidelity rendered image of the game. Not available yet
// obs.UiData - Also not available yet
// Print our rough state of game every 10 steps
if obs.GetGameLoop()%100 == 0 {
//log.Println(rawData.Units)
log.Println(obs.PlayerCommon)
log.Printf("%s Score: %d", SC2APIProtocol.Score_ScoreType_name[int32(*obs.Score.ScoreType)], int32(*obs.Score.Score))
}
// Are we done?
if resp.GetStatus() == SC2APIProtocol.Status_ended {
log.Println("Game over, man")
log.Println(respObs.PlayerResult)
log.Println(obs.Score)
break
}
// Prep request for action in case we need it
req = &SC2APIProtocol.Request{
Request: &SC2APIProtocol.Request_Action{
Action: &SC2APIProtocol.RequestAction{},
},
}
action := req.GetAction()
// Examine game state
var unitType uint32
var alliance SC2APIProtocol.Alliance
var target *SC2APIProtocol.Unit
for _, unit := range rawData.Units {
unitType = unit.GetUnitType()
alliance = unit.GetAlliance()
if unitType == 48 { // Marine
if alliance == SC2APIProtocol.Alliance_Self && len(unit.GetOrders()) == 0 {
// This is for "MoveToBeacon"
target = FindClosestUnit(rawData.Units, unit, SC2APIProtocol.Alliance_Neutral, 317) // beacon
if target != nil {
var abilityID int32 = 1 // "SMART". Could also be 16, which is "MOVE"
a := &SC2APIProtocol.Action{
ActionRaw: &SC2APIProtocol.ActionRaw{
Action: &SC2APIProtocol.ActionRaw_UnitCommand{
UnitCommand: &SC2APIProtocol.ActionRawUnitCommand{
AbilityId: &abilityID,
Target: &SC2APIProtocol.ActionRawUnitCommand_TargetWorldSpacePos{
TargetWorldSpacePos: &SC2APIProtocol.Point2D{
X: target.Pos.X,
Y: target.Pos.Y,
},
},
UnitTags: []uint64{unit.GetTag()},
},
},
},
}
action.Actions = append(action.Actions, a)
log.Printf("Moving marine %d to beacon %d", unit.GetTag(), target.GetTag())
continue
}
// This is for "CollectMineralShards"
target = FindClosestUnit(rawData.Units, unit, SC2APIProtocol.Alliance_Neutral, 1680) // mineral shard
// TODO: Make sure this isn't already someone else's target, somehow
if target != nil {
var abilityID int32 = 1 // "SMART". Could also be 16, which is "MOVE"
a := &SC2APIProtocol.Action{
ActionRaw: &SC2APIProtocol.ActionRaw{
Action: &SC2APIProtocol.ActionRaw_UnitCommand{
UnitCommand: &SC2APIProtocol.ActionRawUnitCommand{
AbilityId: &abilityID,
Target: &SC2APIProtocol.ActionRawUnitCommand_TargetWorldSpacePos{
TargetWorldSpacePos: &SC2APIProtocol.Point2D{
X: target.Pos.X,
Y: target.Pos.Y,
},
},
UnitTags: []uint64{unit.GetTag()},
},
},
},
}
action.Actions = append(action.Actions, a)
log.Printf("Moving marine %d to mineral shard %d", unit.GetTag(), target.GetTag())
continue
}
// Attack any enemy we can see
target = FindClosestAnyEnemy(rawData.Units, unit)
if target != nil {
var abilityID int32 = 3674 // "ATTACK".
a := &SC2APIProtocol.Action{
ActionRaw: &SC2APIProtocol.ActionRaw{
Action: &SC2APIProtocol.ActionRaw_UnitCommand{
UnitCommand: &SC2APIProtocol.ActionRawUnitCommand{
AbilityId: &abilityID,
Target: &SC2APIProtocol.ActionRawUnitCommand_TargetUnitTag{
TargetUnitTag: target.GetTag(),
},
UnitTags: []uint64{unit.GetTag()},
},
},
},
}
action.Actions = append(action.Actions, a)
log.Printf("Moving marine %d to attack enemy %d of type %d", unit.GetTag(), target.GetTag(), target.GetUnitType())
continue
}
// Explore randomly (TODO: Could maybe better explore together)
var abilityID int32 = 1 // "SMART". Could also be 16, which is "MOVE"
offset := float32(32.0)
rx := float32(*unit.Pos.X + rand.Float32()*offset)
ry := float32(*unit.Pos.Y + rand.Float32()*offset)
a := &SC2APIProtocol.Action{
ActionRaw: &SC2APIProtocol.ActionRaw{
Action: &SC2APIProtocol.ActionRaw_UnitCommand{
UnitCommand: &SC2APIProtocol.ActionRawUnitCommand{
AbilityId: &abilityID,
Target: &SC2APIProtocol.ActionRawUnitCommand_TargetWorldSpacePos{
TargetWorldSpacePos: &SC2APIProtocol.Point2D{
X: &rx,
Y: &ry,
},
},
UnitTags: []uint64{unit.GetTag()},
},
},
},
}
action.Actions = append(action.Actions, a)
log.Printf("Moving marine %d to explore at %f,%f", unit.GetTag(), rx, ry)
continue
}
}
if unitType == 45 { // TERRAN SCV
// This is for "CollectMineralsAndGas"
if alliance == SC2APIProtocol.Alliance_Self {
if obs.PlayerCommon.GetMinerals() >= 100 && obs.PlayerCommon.GetFoodCap()-obs.PlayerCommon.GetFoodUsed() <= 2 && AnyUnitHasOrder(rawData.Units, 45, 319) == false && HasActionQueued(action.Actions, 319) == false { // TODO: Way to find out cost programmatically?
var abilityID int32 = 319 // "BUILD_SUPPLYDEPOT"
offset := float32(15.0)
rx := float32(*unit.Pos.X + rand.Float32()*offset)
ry := float32(*unit.Pos.Y + rand.Float32()*offset)
a := &SC2APIProtocol.Action{
ActionRaw: &SC2APIProtocol.ActionRaw{
Action: &SC2APIProtocol.ActionRaw_UnitCommand{
UnitCommand: &SC2APIProtocol.ActionRawUnitCommand{
AbilityId: &abilityID,
Target: &SC2APIProtocol.ActionRawUnitCommand_TargetWorldSpacePos{
TargetWorldSpacePos: &SC2APIProtocol.Point2D{
X: &rx,
Y: &ry,
},
},
UnitTags: []uint64{unit.GetTag()},
},
},
},
}
action.Actions = append(action.Actions, a)
log.Printf("SCV %d building supply depot at %f,%f", unit.GetTag(), rx, ry)
continue
}
if len(unit.GetOrders()) == 0 {
if obs.PlayerCommon.GetMinerals() >= 150 && isMultiplayer && (CountUnitsOfType(rawData.Units, SC2APIProtocol.Alliance_Self, 21)) == 0 { // TODO: Way to find out cost programmatically? Check available actions instead of map name
if AnyUnitHasOrder(rawData.Units, 45, 321) == false { // Only build one at a time
var abilityID int32 = 321 // "BUILD_BARRACKS"
offset := float32(30.0)
rx := float32(*unit.Pos.X + rand.Float32()*offset)
ry := float32(*unit.Pos.Y + rand.Float32()*offset)
a := &SC2APIProtocol.Action{
ActionRaw: &SC2APIProtocol.ActionRaw{
Action: &SC2APIProtocol.ActionRaw_UnitCommand{
UnitCommand: &SC2APIProtocol.ActionRawUnitCommand{
AbilityId: &abilityID,
Target: &SC2APIProtocol.ActionRawUnitCommand_TargetWorldSpacePos{
TargetWorldSpacePos: &SC2APIProtocol.Point2D{
X: &rx,
Y: &ry,
},
},
UnitTags: []uint64{unit.GetTag()},
},
},
},
}
action.Actions = append(action.Actions, a)
log.Printf("SCV %d building barracks at %f,%f", unit.GetTag(), rx, ry)
continue
}
}
if obs.PlayerCommon.GetMinerals() >= 75 { // TODO: Way to find out cost programmatically?
if AnyUnitHasOrder(rawData.Units, 45, 320) == false { // Only build one at a time
target = FindClosestUnit(rawData.Units, unit, SC2APIProtocol.Alliance_Neutral, 342) // vespene geyser
if target != nil && IsUnitTypeAtPoint(rawData.Units, 20, *target.GetPos()) == false {
var abilityID int32 = 320 // "BUILD_REFINERY"
a := &SC2APIProtocol.Action{
ActionRaw: &SC2APIProtocol.ActionRaw{
Action: &SC2APIProtocol.ActionRaw_UnitCommand{
UnitCommand: &SC2APIProtocol.ActionRawUnitCommand{
AbilityId: &abilityID,
Target: &SC2APIProtocol.ActionRawUnitCommand_TargetUnitTag{
TargetUnitTag: target.GetTag(),
},
UnitTags: []uint64{unit.GetTag()},
},
},
},
}
action.Actions = append(action.Actions, a)
log.Printf("SCV %d building refinery at %d", unit.GetTag(), target.GetTag())
continue
}
}
}
target = FindClosestUnit(rawData.Units, unit, SC2APIProtocol.Alliance_Self, 20) // terran refinery
if target != nil && target.GetAssignedHarvesters() < target.GetIdealHarvesters() {
var abilityID int32 = 3666 // "HARVEST_GATHER". There are other "harvest gather" abilities. What are they for?
a := &SC2APIProtocol.Action{
ActionRaw: &SC2APIProtocol.ActionRaw{
Action: &SC2APIProtocol.ActionRaw_UnitCommand{
UnitCommand: &SC2APIProtocol.ActionRawUnitCommand{
AbilityId: &abilityID,
Target: &SC2APIProtocol.ActionRawUnitCommand_TargetUnitTag{
TargetUnitTag: target.GetTag(),
},
UnitTags: []uint64{unit.GetTag()},
},
},
},
}
action.Actions = append(action.Actions, a)
log.Printf("Moving SCV %d to finery %d", unit.GetTag(), target.GetTag())
continue
}
target = FindClosestUnit(rawData.Units, unit, SC2APIProtocol.Alliance_Neutral, 341) // mineral field
if target != nil && target.GetAssignedHarvesters() < 2 {
var abilityID int32 = 3666 // "HARVEST_GATHER". There are other "harvest gather" abilities. What are they for?
a := &SC2APIProtocol.Action{
ActionRaw: &SC2APIProtocol.ActionRaw{
Action: &SC2APIProtocol.ActionRaw_UnitCommand{
UnitCommand: &SC2APIProtocol.ActionRawUnitCommand{
AbilityId: &abilityID,
Target: &SC2APIProtocol.ActionRawUnitCommand_TargetUnitTag{
TargetUnitTag: target.GetTag(),
},
UnitTags: []uint64{unit.GetTag()},
},
},
},
}
action.Actions = append(action.Actions, a)
log.Printf("Moving SCV %d to mineral field %d", unit.GetTag(), target.GetTag())
continue
}
}
}
}
if unitType == 18 { // Terran command center
// This is for "CollectMineralsAndGas"
if alliance == SC2APIProtocol.Alliance_Self && len(unit.GetOrders()) == 0 && unit.GetBuildProgress() == 1.0 {
if obs.PlayerCommon.GetMinerals() >= 400 && unit.GetAssignedHarvesters() > 0 && ((unit.GetIdealHarvesters()/2 <= unit.GetAssignedHarvesters() || unit.GetIdealHarvesters() <= unit.GetAssignedHarvesters()) && AnyUnitHasOrder(rawData.Units, 45, 318) == false && CountUnitsOfType(rawData.Units, SC2APIProtocol.Alliance_Self, 18) == 1) { // TODO: Way to find out cost programmatically?
target = FindClosestUnit(rawData.Units, unit, SC2APIProtocol.Alliance_Self, 45) // SCV
if target != nil {
var abilityID int32 = 318 // "BUILD_COMMANDCENTER"
offset := float32(20.0)
rx := float32(*target.Pos.X + rand.Float32()*offset)
ry := float32(*target.Pos.Y + rand.Float32()*offset)
a := &SC2APIProtocol.Action{
ActionRaw: &SC2APIProtocol.ActionRaw{
Action: &SC2APIProtocol.ActionRaw_UnitCommand{
UnitCommand: &SC2APIProtocol.ActionRawUnitCommand{
AbilityId: &abilityID,
Target: &SC2APIProtocol.ActionRawUnitCommand_TargetWorldSpacePos{
TargetWorldSpacePos: &SC2APIProtocol.Point2D{
X: &rx,
Y: &ry,
},
},
UnitTags: []uint64{target.GetTag()},
},
},
},
}
action.Actions = append(action.Actions, a)
log.Printf("SCV %d building command center at %f,%f", target.GetTag(), rx, ry)
continue
}
}
if obs.PlayerCommon.GetMinerals() >= 50 && obs.PlayerCommon.GetFoodCap() > obs.PlayerCommon.GetFoodUsed() && unit.GetIdealHarvesters() >= unit.GetAssignedHarvesters() { // TODO: Way to find out cost programmatically?
var abilityID int32 = 524 // "TRAIN_SCV"
a := &SC2APIProtocol.Action{
ActionRaw: &SC2APIProtocol.ActionRaw{
Action: &SC2APIProtocol.ActionRaw_UnitCommand{
UnitCommand: &SC2APIProtocol.ActionRawUnitCommand{
AbilityId: &abilityID,
UnitTags: []uint64{unit.GetTag()},
},
},
},
}
action.Actions = append(action.Actions, a)
log.Printf("Command center %d training SCV", unit.GetTag())
continue
}
}
}
if unitType == 19 { // Supply depot
// This is for "CollectMineralsAndGas"
if alliance == SC2APIProtocol.Alliance_Self && unit.GetBuildProgress() == 1.0 { // TODO: Check available actions instead of map name
var abilityID int32 = 556 // "MORPH_SUPPLYDEPOT_LOWER"
a := &SC2APIProtocol.Action{
ActionRaw: &SC2APIProtocol.ActionRaw{
Action: &SC2APIProtocol.ActionRaw_UnitCommand{
UnitCommand: &SC2APIProtocol.ActionRawUnitCommand{
AbilityId: &abilityID,
UnitTags: []uint64{unit.GetTag()},
},
},
},
}
action.Actions = append(action.Actions, a)
log.Printf("Supply depot %d lowering", unit.GetTag())
continue
}
}
if unitType == 21 { // Barracks
// This is for "BuildMarines" (or any multiplayer map)
if alliance == SC2APIProtocol.Alliance_Self && len(unit.GetOrders()) == 0 && unit.GetBuildProgress() == 1.0 {
if obs.PlayerCommon.GetMinerals() >= 50 && obs.PlayerCommon.GetFoodCap() > obs.PlayerCommon.GetFoodUsed() { // TODO: Way to find out cost programmatically?
var abilityID int32 = 560 // "TRAIN_MARINE"
a := &SC2APIProtocol.Action{
ActionRaw: &SC2APIProtocol.ActionRaw{
Action: &SC2APIProtocol.ActionRaw_UnitCommand{
UnitCommand: &SC2APIProtocol.ActionRawUnitCommand{
AbilityId: &abilityID,
UnitTags: []uint64{unit.GetTag()},
},
},
},
}
action.Actions = append(action.Actions, a)
log.Printf("Barracks %d training marine", unit.GetTag())
continue
}
}
}
}
if len(action.Actions) > 0 {
// Send actions
err = protocol.SendRequest(req)
if err != nil {
log.Fatal("Could not send action request:", err)
}
resp, err = protocol.ReadResponse()
if err != nil {
log.Fatal("Could not receive action response:", err)
}
log.Println(resp)
}
// Keep this reasonably paced
//time.Sleep(100 * time.Millisecond)
if realtime == false {
// Prep request for action in case we need it
req = &SC2APIProtocol.Request{
Request: &SC2APIProtocol.Request_Step{
Step: &SC2APIProtocol.RequestStep{
Count: proto.Uint32(1),
},
},
}
err = protocol.SendRequest(req)
if err != nil {
log.Fatal("Could not send step request:", err)
}
resp, err = protocol.ReadResponse()
if err != nil {
log.Fatal("Could not receive step response:", err)
}
}
}
// Leave game
req = &SC2APIProtocol.Request{
Request: &SC2APIProtocol.Request_LeaveGame{
LeaveGame: &SC2APIProtocol.RequestLeaveGame{},
},
}
log.Println("Leaving game…")
err = protocol.SendRequest(req)
if err != nil {
log.Fatal("Could not send leave request:", err)
}
resp, err = protocol.ReadResponse()
if err != nil {
log.Fatal("Could not receive leave response:", err)
}
log.Println(resp)
log.Println("gg")
// Disconnect
log.Println("Disconnecting…")
err = protocol.Disconnect()
if err != nil {
log.Fatal("Error disconnecting:", err)
}
log.Println("BYE!")
// Extra requests I've tested
/*req = &SC2APIProtocol.Request{
Request: &SC2APIProtocol.Request_Ping{
Ping: &SC2APIProtocol.RequestPing{},
},
}*/
/*req = &SC2APIProtocol.Request{
Request: &SC2APIProtocol.Request_Debug{
Debug: &SC2APIProtocol.RequestDebug{},
},
}*/
}
func FindClosestUnit(units []*SC2APIProtocol.Unit, ourUnit *SC2APIProtocol.Unit, desiredAlliance SC2APIProtocol.Alliance, desiredUnitType uint32) *SC2APIProtocol.Unit {
var closestUnit *SC2APIProtocol.Unit
var bestDistance float64
for _, unit := range units {
if unit.GetUnitType() != desiredUnitType {
continue
}
if unit.GetAlliance() != desiredAlliance {
continue
}
dx := float64(*ourUnit.Pos.X - *unit.Pos.X)
dy := float64(*ourUnit.Pos.Y - *unit.Pos.Y)
distance := math.Sqrt(math.Pow(dx, 2) + math.Pow(dy, 2))
if distance < bestDistance || bestDistance == 0 {
bestDistance = distance
closestUnit = unit
}
}
return closestUnit
}
func FindFarthestUnit(units []*SC2APIProtocol.Unit, ourUnit *SC2APIProtocol.Unit, desiredAlliance SC2APIProtocol.Alliance, desiredUnitType uint32) *SC2APIProtocol.Unit {
var farthestUnit *SC2APIProtocol.Unit
var bestDistance float64
for _, unit := range units {
if unit.GetUnitType() != desiredUnitType {
continue
}
if unit.GetAlliance() != desiredAlliance {
continue
}
dx := float64(*ourUnit.Pos.X - *unit.Pos.X)
dy := float64(*ourUnit.Pos.Y - *unit.Pos.Y)
distance := math.Sqrt(math.Pow(dx, 2) + math.Pow(dy, 2))
if distance > bestDistance || bestDistance == 0 {
bestDistance = distance
farthestUnit = unit
}
}
return farthestUnit
}
// This only works for non-enemy units
func AnyUnitHasOrder(units []*SC2APIProtocol.Unit, desiredUnitType uint32, desiredabilityID uint32) bool {
for _, unit := range units {
if unit.GetUnitType() != desiredUnitType {
continue
}
for _, order := range unit.GetOrders() {
if order.GetAbilityId() == desiredabilityID {
return true
}
}
}
return false
}
func CountUnitsOfType(units []*SC2APIProtocol.Unit, desiredAlliance SC2APIProtocol.Alliance, desiredUnitType uint32) uint32 {
var count uint32
for _, unit := range units {
if unit.GetUnitType() != desiredUnitType {
continue
}
if unit.GetAlliance() != desiredAlliance {
continue
}
count++
}
return count
}
func IsUnitTypeAtPoint(units []*SC2APIProtocol.Unit, desiredUnitType uint32, point SC2APIProtocol.Point) bool {
for _, unit := range units {
if unit.GetUnitType() != desiredUnitType {
continue
}
unitPos := unit.GetPos()
if unitPos.GetX() == point.GetX() && unitPos.GetY() == point.GetY() && unitPos.GetZ() == point.GetZ() {
return true
}
}
return false
}
func HasActionQueued(actions []*SC2APIProtocol.Action, abilityID int32) bool {
for _, action := range actions {
if action.ActionRaw.GetUnitCommand().GetAbilityId() == abilityID {
return true
}
}
return false
}
func AbilityIsAvailable(desiredabilityID uint32) (bool, error) {
for _, ability := range abilities {
if ability.GetAbilityId() == desiredabilityID {
return ability.GetAvailable(), nil
}
}
return false, errors.New("Ability not found")
}
func GetAbilityFootprintRadius(desiredabilityID uint32) (float32, error) {
for _, ability := range abilities {
if ability.GetAbilityId() == desiredabilityID {
return ability.GetFootprintRadius(), nil
}
}
return 0, errors.New("Ability not found")
}
func UnitIsAvailable(desiredUnitType uint32) (bool, error) {
for _, unit := range units {
if unit.GetUnitId() == desiredUnitType {
return unit.GetAvailable(), nil
}
}
return false, errors.New("Unit type not found")
}
func GetUnitMineralCost(desiredUnitType uint32) (uint32, error) {
for _, unit := range units {
if unit.GetUnitId() == desiredUnitType {
return unit.GetMineralCost(), nil
}
}
return 0, errors.New("Unit type not found")
}
func GetUnitVespeneCost(desiredUnitType uint32) (uint32, error) {
for _, unit := range units {
if unit.GetUnitId() == desiredUnitType {
return unit.GetVespeneCost(), nil
}
}
return 0, errors.New("Unit type not found")
}
func FindClosestAnyEnemy(units []*SC2APIProtocol.Unit, ourUnit *SC2APIProtocol.Unit) *SC2APIProtocol.Unit {
var closestUnit *SC2APIProtocol.Unit
var bestDistance float64
for _, unit := range units {
if unit.GetAlliance() != SC2APIProtocol.Alliance_Enemy {
continue
}
dx := float64(*ourUnit.Pos.X - *unit.Pos.X)
dy := float64(*ourUnit.Pos.Y - *unit.Pos.Y)
distance := math.Sqrt(math.Pow(dx, 2) + math.Pow(dy, 2))
if distance < bestDistance || bestDistance == 0 {
bestDistance = distance
closestUnit = unit
}
}
return closestUnit
}
func ListMaps(sc2 Protocol) {
// Start sending commands/reading responses
var req *SC2APIProtocol.Request
var resp *SC2APIProtocol.Response
req = &SC2APIProtocol.Request{
Request: &SC2APIProtocol.Request_AvailableMaps{
AvailableMaps: &SC2APIProtocol.RequestAvailableMaps{},
},
}
log.Println("Listing available maps…")
err := sc2.SendRequest(req)
if err != nil {
log.Fatal("Could not send available maps request:", err)
}
resp, err = sc2.ReadResponse()
if err != nil {
log.Fatal("Could not receive available maps response:", err)
}
availableMaps := resp.GetAvailableMaps()
sort.Strings(availableMaps.LocalMapPaths)
fmt.Println("Local maps:")
for _, localMap := range availableMaps.LocalMapPaths {
fmt.Println(localMap)
}
sort.Strings(availableMaps.BattlenetMapNames)
fmt.Println("\nBattlenet maps:")
for _, bnetMap := range availableMaps.BattlenetMapNames {
fmt.Println(bnetMap)
}
}
func CreateGame(sc2 Protocol, mapName string, mapPath string) {
var req *SC2APIProtocol.Request
var resp *SC2APIProtocol.Response
if mapName != "" || mapPath != "" {
// Create a new game
ourPlayer := &SC2APIProtocol.PlayerSetup{
Type: SC2APIProtocol.PlayerType_Participant.Enum(),
Race: SC2APIProtocol.Race_Terran.Enum(),
}
opponentPlayer := &SC2APIProtocol.PlayerSetup{
Type: SC2APIProtocol.PlayerType_Computer.Enum(),
Race: SC2APIProtocol.Race_Terran.Enum(),
Difficulty: SC2APIProtocol.Difficulty_VeryHard.Enum(),
}
// Set either a battlenet map or a local map from args
if mapName != "" {
req = &SC2APIProtocol.Request{
Request: &SC2APIProtocol.Request_CreateGame{
CreateGame: &SC2APIProtocol.RequestCreateGame{
Map: &SC2APIProtocol.RequestCreateGame_BattlenetMapName{
BattlenetMapName: mapName,
},
PlayerSetup: []*SC2APIProtocol.PlayerSetup{ourPlayer, opponentPlayer},
DisableFog: proto.Bool(false),
Realtime: proto.Bool(false),
},
},
}
}
if mapPath != "" {
req = &SC2APIProtocol.Request{
Request: &SC2APIProtocol.Request_CreateGame{
CreateGame: &SC2APIProtocol.RequestCreateGame{
Map: &SC2APIProtocol.RequestCreateGame_LocalMap{
LocalMap: &SC2APIProtocol.LocalMap{
MapPath: &mapPath,
},
},
PlayerSetup: []*SC2APIProtocol.PlayerSetup{ourPlayer, opponentPlayer},
DisableFog: proto.Bool(false),
Realtime: proto.Bool(false),
},
},
}
}
log.Println("Starting game…")
err := sc2.SendRequest(req)
if err != nil {
log.Fatal("Could not send game start request:", err)
}
log.Println("Request sent")
resp, err = sc2.ReadResponse()
if err != nil {
log.Fatal("Could not receive game start response:", err)
}
log.Println("Game started:", resp)
// TODO: Handle this: "Game started: create_game:<error:InvalidMapPath error_details:"map_path '/SC2/StarCraftII/maps/CollectMineralsAndGas.SC2Map' file doesn't exist." > status:launched"
}
}
// List of unit/ability/upgrade/buff types:
// https://github.com/Blizzard/s2client-api/blob/master/include/sc2api/sc2_typeenums.h
// Best Scores:
// MoveToBeacon: 27
// CollectMineralShards: 109
// CollectMineralsAndGas: 5986
// BuildMarines: 30
// DefeatZerglingsAndBanelings: 67
// DefeatRoaches: 46
// FindAndDefeatZerglings: 10