forked from TorchCraft/TorchCraft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
1647 lines (1553 loc) · 55.5 KB
/
Copy pathinit.lua
File metadata and controls
1647 lines (1553 loc) · 55.5 KB
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
local torchcraft = require 'torchcraft._env'
local utils = require 'torchcraft.utils'
local replayer = require 'torchcraft.replayer'
local tablex = require 'pl.tablex'
local image = require 'image'
-- All available commands
local vars = {
-- no arguments
quit = 0, -- leave the game
restart = 1, -- resetart the game. Much faster, but doesn't
-- work in multiplayer.
map_hack = 2, -- remove fog of war
request_image = 3,
exit_process = 4,
noop = 5, -- do nothing
-- one argument
set_speed = 6, -- sets the game speed (integer)
set_log = 7, -- activates logging (boolean)
set_gui = 8, -- activates drawing and text in SC (boolean)
set_frameskip = 9, -- number of frames to skip (integer)
set_cmd_optim = 10, -- reduce bot APM (0-6)
set_combine_frames = 11, -- combine n frames before sending (integer)
-- Sets the map with BWAPI->setMap and by writing to the config. Is not
-- thread-safe. However, as long as the next connect finishes after
-- set_map, you are guaranteed the map will be what you want.
set_map = 12,
set_multi = 13,
-- arguments: (unit ID, command, target id, target x, target y, extra)
-- (x, y) are walktiles instead of pixels
-- otherwise this corresponds exactly to BWAPI::UnitCommand
command_unit = 14,
command_unit_protected = 15,
-- arguments: (command, args)
-- For documentation about args, see usercommandtypes
command_user = 16,
--
MAX_ACTION = 17
}
for k,v in pairs(vars) do
torchcraft[k] = v
end
local function seal(t)
local mt = {}
mt.__newindex = function(tab, k, v)
error('Attempting to add a field to sealed table', 2)
end
setmetatable(t, mt)
end
local function build_revert_indices_and_seal(t)
do -- build reverse indices
local tmp = {}
for k, v in pairs(t) do
table.insert(tmp, k)
end
for _, k in pairs(tmp) do
t[t[k]] = k
end
end
seal(t)
end
torchcraft.usercommandtypes = {
-- one arg
Move_Screen_Up = 0, -- arguments: magnitude (amount of pixels)
Move_Screen_Down = 1, -- arguments: magnitude (amount of pixels)
Move_Screen_Left = 2, -- arguments: magnitude (amount of pixels)
Move_Screen_Right = 3, -- arguments: magnitude (amount of pixels)
-- two args
Move_Screen_To_Pos = 4, -- arguments: (x, y)
Right_Click = 5, -- arguments: (x, y)
--
USER_COMMAND_END = 7
}
build_revert_indices_and_seal(torchcraft.usercommandtypes)
torchcraft.usercmd = torchcraft.usercommandtypes -- shortcut
-- static table, is sealed after initialization
torchcraft.unitcommandtypes = {
-- corresponds to BWAPI::UnitCommandTypes::Enum
Attack_Move = 0,
Attack_Unit = 1,
Build = 2,
Build_Addon = 3,
Train = 4,
Morph = 5,
Research = 6,
Upgrade = 7,
Set_Rally_Position = 8,
Set_Rally_Unit = 9,
Move = 10,
Patrol = 11,
Hold_Position = 12,
Stop = 13,
Follow = 14,
Gather = 15,
Return_Cargo = 16,
Repair = 17,
Burrow = 18,
Unburrow = 19,
Cloak = 20,
Decloak = 21,
Siege = 22,
Unsiege = 23,
Lift = 24,
Land = 25,
Load = 26,
Unload = 27,
Unload_All = 28,
Unload_All_Position = 29,
Right_Click_Position = 30,
Right_Click_Unit = 31,
Halt_Construction = 32,
Cancel_Construction = 33,
Cancel_Addon = 34,
Cancel_Train = 35,
Cancel_Train_Slot = 36,
Cancel_Morph = 37,
Cancel_Research = 38,
Cancel_Upgrade = 39,
Use_Tech = 40,
Use_Tech_Position = 41,
Use_Tech_Unit = 42,
Place_COP = 43,
None = 44,
Unknown = 45,
MAX = 46
}
build_revert_indices_and_seal(torchcraft.unitcommandtypes)
torchcraft.cmd = torchcraft.unitcommandtypes -- shortcut
-- static table, is sealed after initialization
torchcraft.orders = {
-- corresponds to BWAPI::Orders::Enum
Die = 0,
Stop = 1,
Guard = 2,
PlayerGuard = 3,
TurretGuard = 4,
BunkerGuard = 5,
Move = 6,
ReaverStop = 7,
Attack1 = 8,
Attack2 = 9,
AttackUnit = 10,
AttackFixedRange = 11,
AttackTile = 12,
Hover = 13,
AttackMove = 14,
InfestedCommandCenter = 15,
UnusedNothing = 16,
UnusedPowerup = 17,
TowerGuard = 18,
TowerAttack = 19,
VultureMine = 20,
StayInRange = 21,
TurretAttack = 22,
Nothing = 23,
Unused_24 = 24,
DroneStartBuild = 25,
DroneBuild = 26,
CastInfestation = 27,
MoveToInfest = 28,
InfestingCommandCenter = 29,
PlaceBuilding = 30,
PlaceProtossBuilding = 31,
CreateProtossBuilding = 32,
ConstructingBuilding = 33,
Repair = 34,
MoveToRepair = 35,
PlaceAddon = 36,
BuildAddon = 37,
Train = 38,
RallyPointUnit = 39,
RallyPointTile = 40,
ZergBirth = 41,
ZergUnitMorph = 42,
ZergBuildingMorph = 43,
IncompleteBuilding = 44,
IncompleteMorphing = 45,
BuildNydusExit = 46,
EnterNydusCanal = 47,
IncompleteWarping = 48,
Follow = 49,
Carrier = 50,
ReaverCarrierMove = 51,
CarrierStop = 52,
CarrierAttack = 53,
CarrierMoveToAttack = 54,
CarrierIgnore2 = 55,
CarrierFight = 56,
CarrierHoldPosition = 57,
Reaver = 58,
ReaverAttack = 59,
ReaverMoveToAttack = 60,
ReaverFight = 61,
ReaverHoldPosition = 62,
TrainFighter = 63,
InterceptorAttack = 64,
ScarabAttack = 65,
RechargeShieldsUnit = 66,
RechargeShieldsBattery = 67,
ShieldBattery = 68,
InterceptorReturn = 69,
DroneLand = 70,
BuildingLand = 71,
BuildingLiftOff = 72,
DroneLiftOff = 73,
LiftingOff = 74,
ResearchTech = 75,
Upgrade = 76,
Larva = 77,
SpawningLarva = 78,
Harvest1 = 79,
Harvest2 = 80,
MoveToGas = 81,
WaitForGas = 82,
HarvestGas = 83,
ReturnGas = 84,
MoveToMinerals = 85,
WaitForMinerals = 86,
MiningMinerals = 87,
Harvest3 = 88,
Harvest4 = 89,
ReturnMinerals = 90,
Interrupted = 91,
EnterTransport = 92,
PickupIdle = 93,
PickupTransport = 94,
PickupBunker = 95,
Pickup4 = 96,
PowerupIdle = 97,
Sieging = 98,
Unsieging = 99,
WatchTarget = 100,
InitCreepGrowth = 101,
SpreadCreep = 102,
StoppingCreepGrowth = 103,
GuardianAspect = 104,
ArchonWarp = 105,
CompletingArchonSummon = 106,
HoldPosition = 107,
QueenHoldPosition = 108,
Cloak = 109,
Decloak = 110,
Unload = 111,
MoveUnload = 112,
FireYamatoGun = 113,
MoveToFireYamatoGun = 114,
CastLockdown = 115,
Burrowing = 116,
Burrowed = 117,
Unburrowing = 118,
CastDarkSwarm = 119,
CastParasite = 120,
CastSpawnBroodlings = 121,
CastEMPShockwave = 122,
NukeWait = 123,
NukeTrain = 124,
NukeLaunch = 125,
NukePaint = 126,
NukeUnit = 127,
CastNuclearStrike = 128,
NukeTrack = 129,
InitializeArbiter = 130,
CloakNearbyUnits = 131,
PlaceMine = 132,
RightClickAction = 133,
SuicideUnit = 134,
SuicideLocation = 135,
SuicideHoldPosition = 136,
CastRecall = 137,
Teleport = 138,
CastScannerSweep = 139,
Scanner = 140,
CastDefensiveMatrix = 141,
CastPsionicStorm = 142,
CastIrradiate = 143,
CastPlague = 144,
CastConsume = 145,
CastEnsnare = 146,
CastStasisField = 147,
CastHallucination = 148,
Hallucination2 = 149,
ResetCollision = 150,
ResetHarvestCollision = 151,
Patrol = 152,
CTFCOPInit = 153,
CTFCOPStarted = 154,
CTFCOP2 = 155,
ComputerAI = 156,
AtkMoveEP = 157,
HarassMove = 158,
AIPatrol = 159,
GuardPost = 160,
RescuePassive = 161,
Neutral = 162,
ComputerReturn = 163,
InitializePsiProvider = 164,
SelfDestructing = 165,
Critter = 166,
HiddenGun = 167,
OpenDoor = 168,
CloseDoor = 169,
HideTrap = 170,
RevealTrap = 171,
EnableDoodad = 172,
DisableDoodad = 173,
WarpIn = 174,
Medic = 175,
MedicHeal = 176,
HealMove = 177,
MedicHoldPosition = 178,
MedicHealToIdle = 179,
CastRestoration = 180,
CastDisruptionWeb = 181,
CastMindControl = 182,
DarkArchonMeld = 183,
CastFeedback = 184,
CastOpticalFlare = 185,
CastMaelstrom = 186,
JunkYardDog = 187,
Fatal = 188,
None = 189,
Unknown = 190,
MAX = 191
}
build_revert_indices_and_seal(torchcraft.orders)
-- static table, is sealed after initialization
torchcraft.techtypes = {
-- corresponds to BWAPI::TechTypes::Enum
Stim_Packs = 0,
Lockdown = 1,
EMP_Shockwave = 2,
Spider_Mines = 3,
Scanner_Sweep = 4,
Tank_Siege_Mode = 5,
Defensive_Matrix = 6,
Irradiate = 7,
Yamato_Gun = 8,
Cloaking_Field = 9,
Personnel_Cloaking = 10,
Burrowing = 11,
Infestation = 12,
Spawn_Broodlings = 13,
Dark_Swarm = 14,
Plague = 15,
Consume = 16,
Ensnare = 17,
Parasite = 18,
Psionic_Storm = 19,
Hallucination = 20,
Recall = 21,
Stasis_Field = 22,
Archon_Warp = 23,
Restoration = 24,
Disruption_Web = 25,
Unused_26 = 26,
Mind_Control = 27,
Dark_Archon_Meld = 28,
Feedback = 29,
Optical_Flare = 30,
Maelstrom = 31,
Lurker_Aspect = 32,
Unused_33 = 33,
Healing = 34,
None = 44,
Nuclear_Strike = 45,
Unknown = 46,
MAX = 47,
}
build_revert_indices_and_seal(torchcraft.techtypes)
-- static table, is sealed after initialization
torchcraft.unittypes = {
-- corresponds to BWAPI::UnitTypes::Enum
Terran_Marine = 0,
Terran_Ghost = 1,
Terran_Vulture = 2,
Terran_Goliath = 3,
Terran_Siege_Tank_Tank_Mode = 5,
Terran_SCV = 7,
Terran_Wraith = 8,
Terran_Science_Vessel = 9,
Terran_Dropship = 11,
Terran_Battlecruiser = 12,
Terran_Vulture_Spider_Mine = 13,
Terran_Nuclear_Missile = 14,
Terran_Civilian = 15,
Terran_Siege_Tank_Siege_Mode = 30,
Terran_Firebat = 32,
Spell_Scanner_Sweep = 33,
Terran_Medic = 34,
Zerg_Larva = 35,
Zerg_Egg = 36,
Zerg_Zergling = 37,
Zerg_Hydralisk = 38,
Zerg_Ultralisk = 39,
Zerg_Broodling = 40,
Zerg_Drone = 41,
Zerg_Overlord = 42,
Zerg_Mutalisk = 43,
Zerg_Guardian = 44,
Zerg_Queen = 45,
Zerg_Defiler = 46,
Zerg_Scourge = 47,
Zerg_Infested_Terran = 50,
Terran_Valkyrie = 58,
Zerg_Cocoon = 59,
Protoss_Corsair = 60,
Protoss_Dark_Templar = 61,
Zerg_Devourer = 62,
Protoss_Dark_Archon = 63,
Protoss_Probe = 64,
Protoss_Zealot = 65,
Protoss_Dragoon = 66,
Protoss_High_Templar = 67,
Protoss_Archon = 68,
Protoss_Shuttle = 69,
Protoss_Scout = 70,
Protoss_Arbiter = 71,
Protoss_Carrier = 72,
Protoss_Interceptor = 73,
Protoss_Reaver = 83,
Protoss_Observer = 84,
Protoss_Scarab = 85,
Critter_Rhynadon = 89,
Critter_Bengalaas = 90,
Critter_Scantid = 93,
Critter_Kakaru = 94,
Critter_Ragnasaur = 95,
Critter_Ursadon = 96,
Zerg_Lurker_Egg = 97,
Zerg_Lurker = 103,
Spell_Disruption_Web = 105,
Terran_Command_Center = 106,
Terran_Comsat_Station = 107,
Terran_Nuclear_Silo = 108,
Terran_Supply_Depot = 109,
Terran_Refinery = 110,
Terran_Barracks = 111,
Terran_Academy = 112,
Terran_Factory = 113,
Terran_Starport = 114,
Terran_Control_Tower = 115,
Terran_Science_Facility = 116,
Terran_Covert_Ops = 117,
Terran_Physics_Lab = 118,
Terran_Machine_Shop = 120,
Terran_Engineering_Bay = 122,
Terran_Armory = 123,
Terran_Missile_Turret = 124,
Terran_Bunker = 125,
Zerg_Infested_Command_Center = 130,
Zerg_Hatchery = 131,
Zerg_Lair = 132,
Zerg_Hive = 133,
Zerg_Nydus_Canal = 134,
Zerg_Hydralisk_Den = 135,
Zerg_Defiler_Mound = 136,
Zerg_Greater_Spire = 137,
Zerg_Queens_Nest = 138,
Zerg_Evolution_Chamber = 139,
Zerg_Ultralisk_Cavern = 140,
Zerg_Spire = 141,
Zerg_Spawning_Pool = 142,
Zerg_Creep_Colony = 143,
Zerg_Spore_Colony = 144,
Zerg_Sunken_Colony = 146,
Zerg_Extractor = 149,
Protoss_Nexus = 154,
Protoss_Robotics_Facility = 155,
Protoss_Pylon = 156,
Protoss_Assimilator = 157,
Protoss_Observatory = 159,
Protoss_Gateway = 160,
Protoss_Photon_Cannon = 162,
Protoss_Citadel_of_Adun = 163,
Protoss_Cybernetics_Core = 164,
Protoss_Templar_Archives = 165,
Protoss_Forge = 166,
Protoss_Stargate = 167,
Protoss_Fleet_Beacon = 169,
Protoss_Arbiter_Tribunal = 170,
Protoss_Robotics_Support_Bay = 171,
Protoss_Shield_Battery = 172,
Resource_Mineral_Field = 176,
Resource_Mineral_Field_Type_2 = 177,
Resource_Mineral_Field_Type_3 = 178,
Resource_Vespene_Geyser = 188,
Spell_Dark_Swarm = 202,
MAX = 233,
}
build_revert_indices_and_seal(torchcraft.unittypes)
function torchcraft:isbuilding(unittypeid)
return unittypeid >= torchcraft.unittypes.Terran_Command_Center and
unittypeid <= torchcraft.unittypes.Protoss_Shield_Battery
end
function torchcraft:isworker(unittypeid)
return unittypeid == torchcraft.unittypes.Protoss_Probe or
unittypeid == torchcraft.unittypes.Terran_SCV or
unittypeid == torchcraft.unittypes.Zerg_Drone
end
function torchcraft:is_mineral_field(unittypeid)
return unittypeid == self.unittypes.Resource_Mineral_Field or
unittypeid == self.unittypes.Resource_Mineral_Field_Type_2 or
unittypeid == self.unittypes.Resource_Mineral_Field_Type_3
end
function torchcraft:is_gas_geyser(unittypeid)
return unittypeid == self.unittypes.Resource_Vespene_Geyser or
unittypeid == self.unittypes.Protoss_Assimilator or
unittypeid == self.unittypes.Terran_Refinery or
unittypeid == self.unittypes.Zerg_Extractor
end
-- static table, is sealed after initialization
torchcraft.produces = { -- a helpful approximation (e.g. bypasses Eggs)
-- TODO remove when port to C++, use BWAPI directly for this
[torchcraft.unittypes.Terran_Vulture] =
{torchcraft.unittypes.Terran_Vulture_Spider_Mine},
[torchcraft.unittypes.Terran_SCV] = (function()
r = {}
for i = torchcraft.unittypes.Terran_Command_Center,
torchcraft.unittypes.Terran_Bunker do table.insert(r, i) end
return r
end)(),
[torchcraft.unittypes.Zerg_Larva] = (function()
r = {}
for i = torchcraft.unittypes.Zerg_Zergling,
torchcraft.unittypes.Zerg_Scourge do
if i ~= torchcraft.unittypes.Broodling and
i ~= torchcraft.unittypes.Guardian then
table.insert(r, i)
end
end
return r
end)(),
[torchcraft.unittypes.Zerg_Queen] =
{torchcraft.unittypes.Zerg_Broodling,
torchcraft.unittypes.Zerg_Infested_Command_Center},
[torchcraft.unittypes.Zerg_Hydralisk] =
{torchcraft.unittypes.Zerg_Lurker},
[torchcraft.unittypes.Zerg_Drone] = (function()
r = {}
for i = torchcraft.unittypes.Zerg_Hatchery,
torchcraft.unittypes.Zerg_Extractor do table.insert(r, i) end
return r
end)(),
[torchcraft.unittypes.Zerg_Mutalisk] =
{torchcraft.unittypes.Zerg_Guardian,
torchcraft.unittypes.Zerg_Devourer},
[torchcraft.unittypes.Protoss_Probe] = (function()
r = {}
for i = torchcraft.unittypes.Protoss_Nexus,
torchcraft.unittypes.Protoss_Shield_Battery
do
table.insert(r, i)
end
return r
end)(),
[torchcraft.unittypes.Protoss_Carrier] =
{torchcraft.unittypes.Protoss_Interceptor},
[torchcraft.unittypes.Protoss_Reaver] =
{torchcraft.unittypes.Protoss_Scarab},
[torchcraft.unittypes.Terran_Nuclear_Silo] =
{torchcraft.unittypes.Terran_Nuclear_Missile},
[torchcraft.unittypes.Terran_Barracks] =
{torchcraft.unittypes.Terran_Marine,
torchcraft.unittypes.Terran_Firebat,
torchcraft.unittypes.Terran_Medic},
[torchcraft.unittypes.Terran_Factory] =
{torchcraft.unittypes.Terran_Vulture,
torchcraft.unittypes.Terran_Siege_Tank_Tank_Mode,
torchcraft.unittypes.Terran_Goliath},
[torchcraft.unittypes.Terran_Starport] =
{torchcraft.unittypes.Terran_Wraith,
torchcraft.unittypes.Terran_Valkyrie,
torchcraft.unittypes.Terran_Science_Vessel,
torchcraft.unittypes.Terran_Dropship,
torchcraft.unittypes.Terran_Battlecruiser},
[torchcraft.unittypes.Zerg_Infested_Command_Center] =
{torchcraft.unittypes.Zerg_Infested_Terran},
[torchcraft.unittypes.Zerg_Hatchery] =
{torchcraft.unittypes.Zerg_Larva,
torchcraft.unittypes.Zerg_Lair},
[torchcraft.unittypes.Zerg_Lair] =
{torchcraft.unittypes.Zerg_Larva,
torchcraft.unittypes.Zerg_Hive},
[torchcraft.unittypes.Zerg_Hive] =
{torchcraft.unittypes.Zerg_Larva},
[torchcraft.unittypes.Zerg_Creep_Colony] =
{torchcraft.unittypes.Zerg_Spore_Colony,
torchcraft.unittypes.Zerg_Sunken_Colony},
[torchcraft.unittypes.Protoss_Nexus] =
{torchcraft.unittypes.Protoss_Probe},
[torchcraft.unittypes.Protoss_Robotics_Facility] =
{torchcraft.unittypes.Protoss_Reaver,
torchcraft.unittypes.Protoss_Observer,
torchcraft.unittypes.Protoss_Shuttle},
[torchcraft.unittypes.Protoss_High_Templar] =
{torchcraft.unittypes.Protoss_Archon},
[torchcraft.unittypes.Protoss_Dark_Templar] =
{torchcraft.unittypes.Protoss_Dark_Archon},
[torchcraft.unittypes.Protoss_Gateway] =
{torchcraft.unittypes.Protoss_Zealot,
torchcraft.unittypes.Protoss_Dragoon,
torchcraft.unittypes.Protoss_High_Templar,
torchcraft.unittypes.Protoss_Dark_Templar},
[torchcraft.unittypes.Protoss_Stargate] =
{torchcraft.unittypes.Protoss_Scout,
torchcraft.unittypes.Protoss_Corsair,
torchcraft.unittypes.Protoss_Carrier},
}
seal(torchcraft.produces)
-- static table, is sealed after initialization
torchcraft.isproducedby = {} -- a helpful (inverse) approximation
-- TODO remove when port to C++, use BWAPI directly for this
for producer, products in pairs(torchcraft.produces) do
seal(products)
for _, product in pairs(products) do
torchcraft.isproducedby[product] = producer
end
end
seal(torchcraft.isproducedby)
-- static table, is sealed after initialization
torchcraft.bullettypes = {
-- corresponds to BWAPI::BulletTypes::Enum
Melee = 0,
Fusion_Cutter_Hit = 141,
Gauss_Rifle_Hit = 142,
C_10_Canister_Rifle_Hit = 143,
Gemini_Missiles = 144,
Fragmentation_Grenade = 145,
Longbolt_Missile = 146,
Unused_Lockdown = 147,
ATS_ATA_Laser_Battery = 148,
Burst_Lasers = 149,
Arclite_Shock_Cannon_Hit = 150,
EMP_Missile = 151,
Dual_Photon_Blasters_Hit = 152,
Particle_Beam_Hit = 153,
Anti_Matter_Missile = 154,
Pulse_Cannon = 155,
Psionic_Shockwave_Hit = 156,
Psionic_Storm = 157,
Yamato_Gun = 158,
Phase_Disruptor = 159,
STA_STS_Cannon_Overlay = 160,
Sunken_Colony_Tentacle = 161,
Venom_Unused = 162,
Acid_Spore = 163,
Plasma_Drip_Unused = 164,
Glave_Wurm = 165,
Seeker_Spores = 166,
Queen_Spell_Carrier = 167,
Plague_Cloud = 168,
Consume = 169,
Ensnare = 170,
Needle_Spine_Hit = 171,
Invisible = 172,
Optical_Flare_Grenade = 201,
Halo_Rockets = 202,
Subterranean_Spines = 203,
Corrosive_Acid_Shot = 204,
Corrosive_Acid_Hit = 205,
Neutron_Flare = 206,
None = 209,
Unknown = 210,
MAX = 211,
}
build_revert_indices_and_seal(torchcraft.bullettypes)
-- static table, is sealed after initialization
torchcraft.weapontypes = {
-- corresponds to BWAPI::WeaponTypes::Enum
"Gauss_Rifle", "Gauss_Rifle_Jim_Raynor",
"C_10_Canister_Rifle", "C_10_Canister_Rifle_Sarah_Kerrigan",
"Fragmentation_Grenade", "Fragmentation_Grenade_Jim_Raynor", "Spider_Mines",
"Twin_Autocannons", "Hellfire_Missile_Pack", "Twin_Autocannons_Alan_Schezar",
"Hellfire_Missile_Pack_Alan_Schezar", "Arclite_Cannon",
"Arclite_Cannon_Edmund_Duke", "Fusion_Cutter", "", "Gemini_Missiles",
"Burst_Lasers", "Gemini_Missiles_Tom_Kazansky", "Burst_Lasers_Tom_Kazansky",
"ATS_Laser_Battery", "ATA_Laser_Battery", "ATS_Laser_Battery_Hero",
"ATA_Laser_Battery_Hero", "ATS_Laser_Battery_Hyperion",
"ATA_Laser_Battery_Hyperion", "Flame_Thrower", "Flame_Thrower_Gui_Montag",
"Arclite_Shock_Cannon", "Arclite_Shock_Cannon_Edmund_Duke", "Longbolt_Missile",
"Yamato_Gun", "Nuclear_Strike", "Lockdown", "EMP_Shockwave", "Irradiate",
"Claws", "Claws_Devouring_One", "Claws_Infested_Kerrigan", "Needle_Spines",
"Needle_Spines_Hunter_Killer", "Kaiser_Blades", "Kaiser_Blades_Torrasque",
"Toxic_Spores", "Spines", "", "", "Acid_Spore", "Acid_Spore_Kukulza",
"Glave_Wurm", "Glave_Wurm_Kukulza", "", "", "Seeker_Spores",
"Subterranean_Tentacle", "Suicide_Infested_Terran", "Suicide_Scourge",
"Parasite", "Spawn_Broodlings", "Ensnare", "Dark_Swarm", "Plague", "Consume",
"Particle_Beam", "", "Psi_Blades", "Psi_Blades_Fenix", "Phase_Disruptor",
"Phase_Disruptor_Fenix", "", "Psi_Assault", "Psionic_Shockwave",
"Psionic_Shockwave_TZ_Archon", "", "Dual_Photon_Blasters",
"Anti_Matter_Missiles", "Dual_Photon_Blasters_Mojo",
"Anti_Matter_Missiles_Mojo", "Phase_Disruptor_Cannon",
"Phase_Disruptor_Cannon_Danimoth", "Pulse_Cannon", "STS_Photon_Cannon",
"STA_Photon_Cannon", "Scarab", "Stasis_Field", "Psionic_Storm",
"Warp_Blades_Zeratul", "Warp_Blades_Hero", "", "", "", "", "",
"Platform_Laser_Battery", "Independant_Laser_Battery", "", "",
"Twin_Autocannons_Floor_Trap", "Hellfire_Missile_Pack_Wall_Trap",
"Flame_Thrower_Wall_Trap", "Hellfire_Missile_Pack_Floor_Trap", "Neutron_Flare",
"Disruption_Web", "Restoration", "Halo_Rockets", "Corrosive_Acid",
"Mind_Control", "Feedback", "Optical_Flare", "Maelstrom",
"Subterranean_Spines", "", "Warp_Blades", "C_10_Canister_Rifle_Samir_Duran",
"C_10_Canister_Rifle_Infested_Duran", "Dual_Photon_Blasters_Artanis",
"Anti_Matter_Missiles_Artanis", "C_10_Canister_Rifle_Alexei_Stukov", "", "",
"", "", "", "", "", "", "", "", "", "", "", "None", "Unknown"
}
build_revert_indices_and_seal(torchcraft.weapontypes)
-- static table, is sealed after initialization
torchcraft.unitsizes = {
-- corresponds to BWAPI::UnitSizeTypes::Enum
Independent = 0,
Small = 1,
Medium = 2,
Large = 3,
}
build_revert_indices_and_seal(torchcraft.unitsizes)
-- static table, is sealed after initialization
torchcraft.dmgtypes = {
-- corresponds to BWAPI::DamageTypes::Enum
Independent = 0,
Explosive = 1,
Concussive = 2,
Normal = 3,
Ignore_Armor = 4,
None = 5,
}
build_revert_indices_and_seal(torchcraft.dmgtypes)
local c = torchcraft.unitcommandtypes
local o = torchcraft.orders
torchcraft.command2order = {
-- corresponds to BWAPI::UnitCommandTypes to BWAPI::Orders
[c.Halt_Construction] = {o.ResetCollision},
[c.Upgrade] = {o.Upgrade},
[c.Cancel_Morph] = {o.PlayerGuard, o.ResetCollision},
[c.Return_Cargo] = {o.ReturnGas, o.ReturnMinerals, o.ResetCollision},
[c.Attack_Unit] = {o.AttackUnit, o.InterceptorAttack, o.ScarabAttack},
[c.Cloak] = {o.Cloak},
[c.Research] = {o.ResearchTech},
[c.Attack_Move] = {o.AttackMove},
[c.Build] = {o.PlaceBuilding, o.BuildNydusExit, o.CreateProtossBuilding},
[c.Right_Click_Unit] = {o.MoveToMinerals, o.MoveToGas,
o.ConstructingBuilding, o.AttackUnit, o.Follow, o.ResetCollision,
o.EnterNydusCanal, o.EnterTransport, o.Harvest1, o.Harvest2,
o.Harvest3, o.Harvest4, o.InterceptorAttack, o.HarvestGas,
o.MedicHeal, o.MiningMinerals, o.ReturnMinerals, o.ReturnGas,
o.RightClickAction, o.ScarabAttack, o.WaitForGas, o.WaitForMinerals},
[c.Cancel_Upgrade] = {o.Nothing},
[c.Siege] = {o.Sieging},
[c.Train] = {o.Train, o.TrainFighter},
[c.Unload] = {o.Unload},
[c.Stop] = {o.Stop, o.Interrupted},
[c.Cancel_Research] = {o.Nothing},
[c.Lift] = {o.BuildingLiftOff},
[c.Unburrow] = {o.Unburrowing},
[c.Cancel_Train_Slot] = {o.Nothing},
[c.Land] = {o.BuildingLand},
[c.Set_Rally_Unit] = {o.RallyPointUnit},
[c.Hold_Position] = {o.HoldPosition},
[c.Morph] = {o.ZergUnitMorph, o.ZergBuildingMorph},
[c.Cancel_Construction] = {o.ResetCollision, o.Die},
[c.Gather] = {o.MoveToMinerals, o.MoveToGas, o.Harvest1, o.Harvest2,
o.Harvest3, o.Harvest4, o.HarvestGas, o.MiningMinerals,
o.WaitForGas, o.WaitForMinerals, o.ResetCollision, o.ReturnMinerals},
[c.Cancel_Addon] = {o.Nothing},
[c.Cancel_Train] = {o.Nothing},
[c.Burrow] = {o.Burrowing},
[c.Decloak] = {o.Decloak},
[c.Unsiege] = {o.Unsieging},
[c.Right_Click_Position] = {o.Move},
[c.Unload_All] = {o.Unload, o.MoveUnload},
[c.Load] = {o.PickupBunker, o.PickupTransport, o.EnterTransport, o.Pickup4},
[c.Repair] = {o.Repair},
[c.Unload_All_Position] = {o.MoveUnload},
[c.Patrol] = {o.Patrol},
[c.Move] = {o.Move},
[c.Build_Addon] = {o.BuildAddon, o.PlaceAddon},
[c.Set_Rally_Position] = {o.RallyPointTile, o.RallyPointUnit},
[c.Follow] = {o.Follow},
[c.Use_Tech] = {o.Cloak, o.Decloak},
[c.Use_Tech_Position] = {o.CastDarkSwarm, o.CastDisruptionWeb,
o.CastEMPShockwave, o.CastEnsnare, o.CastNuclearStrike, o.CastRecall,
o.CastPsionicStorm, o.CastPlague, o.CastScannerSweep,
o.CastStasisField, o.PlaceMine},
[c.Use_Tech_Unit] = {o.ArchonWarp, o.CastConsume, o.CastDefensiveMatrix,
o.CastFeedback, o.CastHallucination, o.CastIrradiate,
o.CastInfestation, o.CastLockdown, o.CastMaelstrom, o.CastMindControl,
o.CastOpticalFlare, o.CastParasite, o.CastRestoration,
o.CastSpawnBroodlings, o.DarkArchonMeld, o.FireYamatoGun,
o.InfestingCommandCenter, o.RechargeShieldsUnit}
}
seal(torchcraft.command2order)
torchcraft.order2command = {}
-- Most of unknown orders to Idle/Stop
for command, orders in pairs(torchcraft.command2order) do
for _, order in pairs(orders) do
if torchcraft.order2command[order] == nil then
torchcraft.order2command[order] = {}
end
table.insert(torchcraft.order2command[order], command)
end
end
seal(torchcraft.order2command)
torchcraft.xy_pixels_per_walktile = 8
torchcraft.xy_pixels_per_buildtile = 32
torchcraft.xy_walktiles_per_buildtile = 4
torchcraft.hit_prob_ranged_uphill_doodad = 0.53125
torchcraft.hit_prob_ranged = 0.99609375
do -- load and seal other static data
local json = require 'cjson'
torchcraft.staticdata = json.decode(io.open( -- TODO
paths.thisfile('starcraft_static.json')) -- path!
:read("*all"))
for property, data_table in pairs(torchcraft.staticdata) do
local add = {}
for unittype, value in pairs(data_table) do
if torchcraft.unittypes[unittype] ~= nil then
add[torchcraft.unittypes[unittype]] = value
end
end
for k, v in pairs(add) do
data_table[k] = v
end
end
local tmp = {}
tmp.__newindex = function(t, k, v)
error('Attempting to add a field to sealed table torchcraft.staticdata')
end
setmetatable(torchcraft.staticdata, tmp)
end
torchcraft.total_price = {mineral = {}, gas = {}}
for ut, _ in pairs(torchcraft.produces) do
torchcraft.total_price.mineral[ut] = torchcraft.staticdata.mineralPrice[ut]
torchcraft.total_price.gas[ut] = torchcraft.staticdata.gasPrice[ut]
end
for ut, producer in pairs(torchcraft.isproducedby) do -- only 1 hop ever
if torchcraft:isbuilding(producer) or
producer == torchcraft.unittypes.Zerg_Larva then
torchcraft.total_price.mineral[ut] = torchcraft.staticdata.mineralPrice[ut]
torchcraft.total_price.gas[ut] = torchcraft.staticdata.gasPrice[ut]
elseif ut == torchcraft.unittypes.Protoss_Archon
or ut == torchcraft.unittypes.Protoss_Dark_Archon then
torchcraft.total_price.mineral[ut] =
2 * torchcraft.staticdata.mineralPrice[producer]
torchcraft.total_price.gas[ut] =
2 * torchcraft.staticdata.gasPrice[producer]
else
torchcraft.total_price.mineral[ut] =
(torchcraft.staticdata.mineralPrice[ut] or 0)
+ torchcraft.staticdata.mineralPrice[producer]
torchcraft.total_price.gas[ut] =
(torchcraft.staticdata.gasPrice[ut] or 0)
+ torchcraft.staticdata.gasPrice[producer]
end
end
seal(torchcraft.total_price)
seal(torchcraft.total_price.mineral)
seal(torchcraft.total_price.gas)
assert(torchcraft.total_price.mineral[torchcraft.unittypes.Zerg_Guardian]
== torchcraft.staticdata.mineralPrice[torchcraft.unittypes.Zerg_Mutalisk]
+ torchcraft.staticdata.mineralPrice[torchcraft.unittypes.Zerg_Guardian])
assert(torchcraft.total_price.mineral[torchcraft.unittypes.Protoss_Dark_Archon]
== 2 * torchcraft.staticdata.mineralPrice[torchcraft.unittypes.Protoss_Dark_Templar])
assert(torchcraft.total_price.gas[torchcraft.unittypes.Terran_Science_Vessel]
== 225)
torchcraft.PROTOCOL_VERSION = "16"
torchcraft.hostname = nil
torchcraft.state = {}
torchcraft.mode = {micro_battles = false, replay = false}
torchcraft.DEBUG = 0
torchcraft.initial_map = nil
torchcraft.window_size = nil
torchcraft.window_pos = nil
torchcraft.field_size = {640, 370} -- size of the field view in pixels (approximately)
--[[
state will get its content updated from bwapi, it will have
* map_data : [torch.ByteTensor] 2D. 255 (-1) where not walkable
* map_name : [string] Name on the current map
* img_mode : [string] Image mode selected (can be empty, raw, compress)
* lag_frames : [int] number of frames from order to execution
* frame_from_bwapi : [int] game frame number as seen from BWAPI
* game_ended : [boolean] did the game end? (i.e. did the map end)
* battle_just_ended : [boolean] did the battle just end? (battle!=game)
* waiting_for_restart : [boolean] are we waiting to restart a new battle?
* battle_won : [boolean] did we win the battle?
* units_myself : [table] w/ {unitIDs: unitStates} as {keys: values}
* units_enemy : [table] same as above, but for the enemy player
* bullets : [table] table with all bullets (position and type)
* screen_position : [table] Position of screen {x, y} in pixels. {0, 0} is top-left
]]
function torchcraft:init(hostname, port)
if hostname == '' or hostname == nil then
-- this is the local VM when running e.g. on a laptop + VMware windows
-- known problem: sometimes this arp command fails on VPNs...
local arpstring = 'arp -a -i vmnet8 | grep -v incomplete | '
.. 'tail -1 | cut -f2 -d" " | tr -d "()"'
print("executing: " .. arpstring)
self.hostname = sys.fexecute(arpstring)
else
self.hostname = hostname
end
self.port = port ~= nil and port or (os.getenv('TorchCraft_PORT') or 11111)
print('host: ' .. self.hostname .. ':' .. self.port)
-- we always need to make sure we alternate 1:1 send/receive, thus:
self.sent_message = false
end
function torchcraft:connect(port)
-- connect() should be called at the beginning of every game
if self.hostname == nil or self.hostname == '' then
self:init(nil, port)
end
-- initialize ZMQ if needed
if self.zmq == nil then
self.zmq = require 'lzmq'
end
if self.zcontext == nil then
self.zcontext = self.zmq.context()
end
-- initialize socket connection
self.sock = nil
while self.sock == nil do
local addr = 'tcp://' .. self.hostname .. ':' .. self.port
self.sock, self.err = self.zcontext:socket{self.zmq.REQ,
connect = addr}
if self.sock == nil then
print('Socket error (' .. addr ..
'), retrying connection in 1 second: ', self.err)
os.execute('sleep 1')
end
end
self.state = {}
-- send hello message
local hello = 'protocol=' .. self.PROTOCOL_VERSION
if self.initial_map then
hello = hello .. ",map=" .. self.initial_map
end
if self.window_size then
hello = hello .. ",window_size=" .. self.window_size[1] .. " " .. self.window_size[2]
end
if self.window_pos then
hello = hello .. ",window_pos=" .. self.window_pos[1] .. " " .. self.window_pos[2]
end
hello = hello .. ",micro_mode=" .. tostring(self.mode.micro_battles)
local ok, err = self.sock:send(hello)
if not ok then
error('tc.connect send protocol: '..err:name()..' '..err:msg())
end
-- receive setup message
local msg, more = self.sock:recv()
if not msg then
local err = more
error('tc.connect receive setup: '..err:name()..' '..err:msg())
end
assert(not more, "Expected single message to be received.")
local setup = loadstring('return ' .. msg)()
for k, v in pairs(setup) do
self.state[k] = v
end