-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_mex_placement.lua
1133 lines (926 loc) · 29.3 KB
/
cmd_mex_placement.lua
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
function widget:GetInfo()
return {
name = "Mex Placement Handler",
desc = "Places mexes in the correct position DO NOT DISABLE",
author = "Google Frog with some from Niobium and Evil4Zerggin.",
version = "v1",
date = "22 April, 2012", --2 April 2013
license = "GNU GPL, v2 or later",
layer = 0,
enabled = true,
handler = true
}
end
VFS.Include("LuaRules/Configs/customcmds.h.lua")
------------------------------------------------------------
-- Speedups
------------------------------------------------------------
local spGetActiveCommand = Spring.GetActiveCommand
local spGetMouseState = Spring.GetMouseState
local spTraceScreenRay = Spring.TraceScreenRay
local spGetUnitDefID = Spring.GetUnitDefID
local spGetMyAllyTeamID = Spring.GetMyAllyTeamID
local spGetUnitAllyTeam = Spring.GetUnitAllyTeam
local spGetUnitHealth = Spring.GetUnitHealth
local spGetSelectedUnits = Spring.GetSelectedUnits
local spInsertUnitCmdDesc = Spring.InsertUnitCmdDesc
local spGiveOrderToUnit = Spring.GiveOrderToUnit
local spGetUnitPosition = Spring.GetUnitPosition
local spGetTeamUnits = Spring.GetTeamUnits
local spGetMyTeamID = Spring.GetMyTeamID
local spTestBuildOrder = Spring.TestBuildOrder
local spGetUnitsInRectangle = Spring.GetUnitsInRectangle
local spGiveOrder = Spring.GiveOrder
local spGetGroundInfo = Spring.GetGroundInfo
local spGetGroundHeight = Spring.GetGroundHeight
local spGetMapDrawMode = Spring.GetMapDrawMode
local spGetGameFrame = Spring.GetGameFrame
local spGetSpectatingState = Spring.GetSpectatingState
local spGetAllUnits = Spring.GetAllUnits
local spGetPositionLosState = Spring.GetPositionLosState
local glLineWidth = gl.LineWidth
local glColor = gl.Color
local glRect = gl.Rect
local glText = gl.Text
local glGetTextWidth = gl.GetTextWidth
local glPolygonMode = gl.PolygonMode
local glDrawGroundCircle = gl.DrawGroundCircle
local glUnitShape = gl.UnitShape
local glDepthTest = gl.DepthTest
local glLighting = gl.Lighting
local glScale = gl.Scale
local glBillboard = gl.Billboard
local glAlphaTest = gl.AlphaTest
local glTexture = gl.Texture
local glTexRect = gl.TexRect
local glVertex = gl.Vertex
local glBeginEnd = gl.BeginEnd
local glLoadIdentity = gl.LoadIdentity
local glRotate = gl.Rotate
local glPopMatrix = gl.PopMatrix
local glPushMatrix = gl.PushMatrix
local glTranslate = gl.Translate
local glCallList = gl.CallList
local glCreateList = gl.CreateList
local GL_FRONT_AND_BACK = GL.FRONT_AND_BACK
local GL_FILL = GL.FILL
local GL_GREATER = GL.GREATER
local floor = math.floor
local min, max = math.min, math.max
local strFind = string.find
local strFormat = string.format
local CMD_OPT_SHIFT = CMD.OPT_SHIFT
local sqrt = math.sqrt
local tasort = table.sort
local taremove = table.remove
local myAllyTeam = spGetMyAllyTeamID()
local mapX = Game.mapSizeX
local mapZ = Game.mapSizeZ
local mapXinv = 1/mapX
local mapZinv = 1/mapZ
local METAL_MAP_SQUARE_SIZE = 16
local MEX_RADIUS = Game.extractorRadius
local MAP_SIZE_X = Game.mapSizeX
local MAP_SIZE_X_SCALED = MAP_SIZE_X / METAL_MAP_SQUARE_SIZE
local MAP_SIZE_Z = Game.mapSizeZ
local MAP_SIZE_Z_SCALED = MAP_SIZE_Z / METAL_MAP_SQUARE_SIZE
------------------------------------------------------------
-- Config
------------------------------------------------------------
local TEXT_SIZE = 16
local TEXT_CORRECT_Y = 1.25
local MINIMAP_DRAW_SIZE = math.max(mapX,mapZ) * 0.0145
options_path = 'Settings/Interface/Map/Metal Spots'
options_order = { 'drawicons', 'size', 'rounding'}
options = {
drawicons = {
name = 'Show Income as Icon',
type = 'bool',
value = true,
noHotkey = true,
desc = "Enabled: income is shown pictorially.\nDisabled: income is shown as a number.",
OnChange = function() updateMexDrawList() end
},
size = {
name = "Income Display Size",
desc = "How large should the font or icon be?",
type = "number",
value = 40,
min = 10,
max = 150,
step = 5,
update_on_the_fly = true,
OnChange = function() updateMexDrawList() end
},
rounding = {
name = "Display decimal digits",
desc = "How precise should the number be?\nNo effect on icons.",
type = "number",
value = 1,
min = 1,
max = 4,
update_on_the_fly = true,
advanced = true,
tooltip_format = "%.0f", -- show 1 instead of 1.0 (confusion)
OnChange = function() updateMexDrawList() end
},
}
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
-- Mexes and builders
local mexDefID = UnitDefNames["staticmex"].id
local lltDefID = UnitDefNames["turretlaser"].id
local solarDefID = UnitDefNames["energysolar"].id
local windDefID = UnitDefNames["energywind"].id
local mexUnitDef = UnitDefNames["staticmex"]
local mexDefInfo = {
extraction = 0.001,
oddX = mexUnitDef.xsize % 4 == 2,
oddZ = mexUnitDef.zsize % 4 == 2,
}
local mexBuilder = {}
local mexBuilderDefs = {}
for udid, ud in ipairs(UnitDefs) do
for i, option in ipairs(ud.buildOptions) do
if mexDefID == option then
mexBuilderDefs[udid] = true
end
end
end
local addons = { -- coordinates of solars for the Alt modifier key
{ 16, -64 },
{ 64, 16 },
{-16, 64 },
{-64, -16 },
}
--------------------------------------------------------------------------------
-- Variables
--------------------------------------------------------------------------------
WG.mouseoverMexIncome = 0
local spotByID = {}
local spotData = {}
local wasSpectating = spGetSpectatingState()
local metalSpotsNil = true
------------------------------------------------------------
-- Functions
------------------------------------------------------------
local function GetClosestMetalSpot(x, z) --is used by single mex placement, not used by areamex
local bestSpot
local bestDist = math.huge
local bestIndex
for i = 1, #WG.metalSpots do
local spot = WG.metalSpots[i]
local dx, dz = x - spot.x, z - spot.z
local dist = dx*dx + dz*dz
if dist < bestDist then
bestSpot = spot
bestDist = dist
bestIndex = i
end
end
return bestSpot, sqrt(bestDist), bestIndex
end
local function Distance(x1,z1,x2,z2)
local dis = (x1-x2)*(x1-x2)+(z1-z2)*(z1-z2)
return dis
end
local function IsSpotTerraformable(index)
if not index then
return true
end
local spot = spotData[index]
if not spot then
return true
end
local unitID = spot.unitID
return unitID == nil --no unit there, can terraform
end
local function IsSpotBuildable(index)
if not index then
return true
end
local spot = spotData[index]
if not spot then
return true
end
local unitID = spot.unitID
if unitID and spGetUnitAllyTeam(unitID) == spGetMyAllyTeamID() then
local build = select(5, spGetUnitHealth(unitID))
if build and build < 1 then
return true
end
end
return false
end
local function IntegrateMetal(x, z, forceUpdate)
local newCenterX, newCenterZ
if (mexDefInfo.oddX) then
newCenterX = (floor( x / METAL_MAP_SQUARE_SIZE) + 0.5) * METAL_MAP_SQUARE_SIZE
else
newCenterX = floor( x / METAL_MAP_SQUARE_SIZE + 0.5) * METAL_MAP_SQUARE_SIZE
end
if (mexDefInfo.oddZ) then
newCenterZ = (floor( z / METAL_MAP_SQUARE_SIZE) + 0.5) * METAL_MAP_SQUARE_SIZE
else
newCenterZ = floor( z / METAL_MAP_SQUARE_SIZE + 0.5) * METAL_MAP_SQUARE_SIZE
end
if (centerX == newCenterX and centerZ == newCenterZ and not forceUpdate) then
return
end
centerX = newCenterX
centerZ = newCenterZ
local startX = floor((centerX - MEX_RADIUS) / METAL_MAP_SQUARE_SIZE)
local startZ = floor((centerZ - MEX_RADIUS) / METAL_MAP_SQUARE_SIZE)
local endX = floor((centerX + MEX_RADIUS) / METAL_MAP_SQUARE_SIZE)
local endZ = floor((centerZ + MEX_RADIUS) / METAL_MAP_SQUARE_SIZE)
startX, startZ = max(startX, 0), max(startZ, 0)
endX, endZ = min(endX, MAP_SIZE_X_SCALED - 1), min(endZ, MAP_SIZE_Z_SCALED - 1)
local mult = mexDefInfo.extraction
local result = 0
for i = startX, endX do
for j = startZ, endZ do
local cx, cz = (i + 0.5) * METAL_MAP_SQUARE_SIZE, (j + 0.5) * METAL_MAP_SQUARE_SIZE
local dx, dz = cx - centerX, cz - centerZ
local dist = sqrt(dx * dx + dz * dz)
if (dist < MEX_RADIUS) then
local _, metal = spGetGroundInfo(cx, cz)
result = result + metal
end
end
end
extraction = result * mult
end
------------------------------------------------------------
-- Command Handling
------------------------------------------------------------
local function GetLevelTerraformParams(points, constructors, teamID)
local terraParams = {}
local commandTag = WG.Terraform_GetNextTag()
local pointAveX = 0
local pointAveZ = 0
for i = 1, #points do
pointAveX = pointAveX + points[i].x
pointAveZ = pointAveZ + points[i].z
end
pointAveX = pointAveX/#points
pointAveZ = pointAveZ/#points
local height = spGetGroundHeight(pointAveX, pointAveZ)
--terraParams[1]: terraform_type -- 1 = level, 2 = raise, 3 = smooth, 4 = ramp, 5 = restore
terraParams[1] = 1
--terraParams[2]: teamID of the team doing the terraform
terraParams[2] = teamID
terraParams[3] = pointAveX
terraParams[4] = pointAveZ
terraParams[5] = commandTag
-- loop: needs to be closed
terraParams[6] = 1
--height
terraParams[7] = points[1].y
-- how many points there are in the lasso
terraParams[8] = #points
-- how many constructors are working on it
terraParams[9] = #constructors
-- volumeSelection: 0 = none, 1 = only raise, 2 = only lower
terraParams[10] = 0
local i = 11
for j = 1, #points do
terraParams[i] = points[j].x
terraParams[i + 1] = points[j].z
i = i + 2
end
--i = i + 2
for j = 1, #constructors do
terraParams[i] = constructors[j]
i = i + 1
end
local levelParams = {pointAveX, height, pointAveZ, commandTag}
return terraParams, levelParams
end
local mexElmoSize = 16
local mexBurryDepth = 60
local mexRaiseHeight = 10
local heightDiffTolerance = 2
local function DoMexTerraform(conses, commands, myTeamID, x, z, heading, shift)
local yOrig = Spring.GetGroundOrigHeight(x, z)
local y = Spring.GetGroundHeight(x, z)
local wantedHeight = y
--burry the mex
if yOrig > 0 and math.abs(yOrig - mexBurryDepth - y) > heightDiffTolerance then
wantedHeight = yOrig - mexBurryDepth
elseif yOrig <= 0 and math.abs(y - mexRaiseHeight) > heightDiffTolerance then
wantedHeight = mexRaiseHeight
end
--CommandNotifyRaiseAndBuild(unitArray, cmdID, x, y, z, h, shift)
local handledExternally = WG.GlobalBuildCommand and WG.GlobalBuildCommand.CommandNotifyRaiseAndBuild(conses, -mexDefID, x, wantedHeight, z, heading, shift)
if not handledExternally then
local terraFormPossible = false
local spotID = WG.metalSpotsByPos[x] and WG.metalSpotsByPos[x][z]
if spotID then
--Spring.Utilities.TableEcho(spotData)
terraFormPossible = (spotData[spotID] == nil)
end
if terraFormPossible and wantedHeight ~= y then --in case terraform is possible and required
--Spring.Echo(wantedHeight, y)
local mexRect = {
{x = x - mexElmoSize, y = wantedHeight, z = z - mexElmoSize},
{x = x - mexElmoSize, y = wantedHeight, z = z + mexElmoSize},
{x = x + mexElmoSize, y = wantedHeight, z = z + mexElmoSize},
{x = x + mexElmoSize, y = wantedHeight, z = z - mexElmoSize},
}
mexRect[5] = mexRect[1]
local terraParams, levelParams = GetLevelTerraformParams(mexRect, conses, myTeamID)
--CMD_TERRAFORM_INTERNAL
commands[#commands + 1] = {
units = {conses[1]},
command = {id = CMD_TERRAFORM_INTERNAL, params = terraParams, options = {"shift"}}
}
--CMD_LEVEL
commands[#commands+1] = {
units = conses,
command = {id = CMD_LEVEL, params = levelParams, options = {"shift"}}
}
end
-- build mexDefID
commands[#commands+1] = {
units = conses,
command = {id = -mexDefID, params = {x, y, z, heading} , options = {"shift"}}
}
end
end
function widget:CommandNotify(cmdID, params, options)
if (cmdID == CMD_AREA_MEX and WG.metalSpots) then
local cx, cy, cz, cr = params[1], params[2], params[3], math.max((params[4] or 60),60)
local xmin = cx-cr
local xmax = cx+cr
local zmin = cz-cr
local zmax = cz+cr
local commands = {}
local orderedCommands = {}
local dis = {}
local ux = 0
local uz = 0
local us = 0
local aveX = 0
local aveZ = 0
local units = spGetSelectedUnits()
for i = 1, #units do
local unitID = units[i]
if mexBuilder[unitID] then
local x,_,z = spGetUnitPosition(unitID)
ux = ux+x
uz = uz+z
us = us+1
end
end
if (us == 0) then
return
else
aveX = ux/us
aveZ = uz/us
end
for i = 1, #WG.metalSpots do
local mex = WG.metalSpots[i]
--if (mex.x > xmin) and (mex.x < xmax) and (mex.z > zmin) and (mex.z < zmax) then -- square area, should be faster
if (Distance(cx,cz,mex.x,mex.z) < cr*cr) then -- circle area, slower
commands[#commands+1] = {x = mex.x, z = mex.z, d = Distance(aveX,aveZ,mex.x,mex.z)}
end
end
local noCommands = #commands
while noCommands > 0 do
tasort(commands, function(a,b) return a.d < b.d end)
orderedCommands[#orderedCommands+1] = commands[1]
aveX = commands[1].x
aveZ = commands[1].z
taremove(commands, 1)
for k, com in pairs(commands) do
com.d = Distance(aveX,aveZ,com.x,com.z)
end
noCommands = noCommands-1
end
local shift = options.shift
do --issue ordered order to unit(s)
local myTeamID = Spring.GetMyTeamID()
local commandArrayToIssue={}
local unitArrayToReceive ={}
for i = 1, #units do --prepare unit list
local unitID = units[i]
if mexBuilder[unitID] then
unitArrayToReceive[#unitArrayToReceive+1] = unitID
end
end
--prepare command list
if not shift then
commandArrayToIssue[1] = {
units = unitArrayToReceive,
command = {id = CMD.STOP, params = {} , options = {}}
}
end
for i, command in ipairs(orderedCommands) do
local x = command.x
local z = command.z
local y = Spring.GetGroundHeight(x, z)
if options.ctrl and Game.mapDamage then
DoMexTerraform(unitArrayToReceive, commandArrayToIssue, myTeamID, x, z, 0, shift)
else
-- check if some other widget wants to handle the command before sending it to units.
if not WG.GlobalBuildCommand or not WG.GlobalBuildCommand.CommandNotifyMex(-mexDefID, {x, y, z, 0}, options, true) then
commandArrayToIssue[#commandArrayToIssue+1] = {
units = unitArrayToReceive,
command = {id = -mexDefID, params = {x,y,z,0} , options = {"shift"}}
}
end
if options.alt then
for i=1, #addons do
local addon = addons[i]
local xx = x+addon[1]
local zz = z+addon[2]
local yy = Spring.GetGroundHeight(xx, zz)
local buildDefID = (Spring.TestBuildOrder(solarDefID, xx, yy, zz, 0) == 0 and windDefID) or solarDefID
-- check if some other widget wants to handle the command before sending it to units.
if not WG.GlobalBuildCommand or not WG.GlobalBuildCommand.CommandNotifyMex(-buildDefID, {xx, yy, zz, 0}, options, true) then
commandArrayToIssue[#commandArrayToIssue+1] = {
units = unitArrayToReceive,
command = {id = -buildDefID, params = {xx,yy,zz,0}, options = {"shift"}}
}
end
end
end
end
end
for i = 1, #commandArrayToIssue do
local item = commandArrayToIssue[i]
local command = item.command
Spring.GiveOrderToUnitArray(item.units, command.id, command.params, command.options)
end
end
return true
end
if -mexDefID == cmdID and WG.metalSpots then
local bx, bz = params[1], params[3]
local closestSpot = GetClosestMetalSpot(bx, bz)
if closestSpot then
local units = spGetUnitsInRectangle(closestSpot.x-1, closestSpot.z-1, closestSpot.x+1, closestSpot.z+1)
local foundUnit = false
local myAlly = spGetMyAllyTeamID()
for i = 1, #units do
local unitID = units[i]
local unitDefID = Spring.GetUnitDefID(unitID)
if unitDefID and mexDefID == unitDefID and spGetUnitAllyTeam(unitID) == myAlly then
foundUnit = unitID
break
end
end
if foundUnit then
local build = select(5, spGetUnitHealth(foundUnit))
if build ~= 1 then
if options.meta then
WG.CommandInsert(CMD.REPAIR, {foundUnit}, options)
else
spGiveOrder(CMD.REPAIR, {foundUnit}, options)
end
end
return true
else
if options.ctrl and Game.mapDamage then
local unitArrayToReceive = {}
local units = spGetSelectedUnits()
for i = 1, #units do --prepare unit list
local unitID = units[i]
if mexBuilder[unitID] then
unitArrayToReceive[#unitArrayToReceive+1] = unitID
end
end
commands = {}
DoMexTerraform(unitArrayToReceive, commands, Spring.GetMyTeamID(), closestSpot.x, closestSpot.z, params[4], options.shift)
for i = 1, #commands do
local item = commands[i]
local command = item.command
Spring.GiveOrderToUnitArray(item.units, command.id, command.params, command.options)
end
else
-- check if some other widget wants to handle the command before sending it to units.
local commandHeight = math.max(0, Spring.GetGroundHeight(closestSpot.x, closestSpot.z))
local GBC_processed = WG.GlobalBuildCommand and WG.GlobalBuildCommand.CommandNotifyMex(cmdID, {closestSpot.x, commandHeight, closestSpot.z, params[4]}, options, false)
if not GBC_processed then
if options.meta then
WG.CommandInsert(cmdID, {closestSpot.x, commandHeight, closestSpot.z, params[4]}, options)
else
spGiveOrder(cmdID, {closestSpot.x, commandHeight, closestSpot.z, params[4]}, options)
end
end
end
return true
end
end
end
end
function widget:UnitEnteredLos(unitID, teamID)
if spGetSpectatingState() then
return
end
local unitDefID = Spring.GetUnitDefID(unitID)
if unitDefID ~= mexDefID or not WG.metalSpots then
return
end
local x,_,z = Spring.GetUnitPosition(unitID)
local spotID = WG.metalSpotsByPos[x] and WG.metalSpotsByPos[x][z]
if not spotID then
return
end
spotByID[unitID] = spotID
spotData[spotID] = {unitID = unitID, team = teamID, enemy = true}
updateMexDrawList()
end
local function DidMexDie(unitID, expectedSpotID) --> dead, idReusedForAnotherMex
local unitDefID = Spring.GetUnitDefID(unitID)
if unitDefID ~= mexDefID then -- not just a nil check, the unitID could have gotten recycled for another unit
return true, false
end
local spotID = spotByID[unitID]
if spotID ~= expectedSpotID then
return true, true -- the original died, unitID was recycled to another mex
end
return false
end
local function CheckEnemyMexes(spotID)
local spotD = spotData[spotID]
if not spotD or not spotD.enemy then
return
end
local spotM = WG.metalSpots[spotID]
local x = spotM.x
local z = spotM.z
local los = Spring.GetPositionLosState(x, 0, z)
if not los then
return
end
local unitID = spotD.unitID
local dead, idReusedForAnotherMex = DidMexDie(unitID, spotID)
if not dead then
return
end
if not idReusedForAnotherMex then
spotByID[unitID] = nil
end
spotData[spotID] = nil
updateMexDrawList()
end
function widget:GameFrame(n)
if not WG.metalSpots or (n % 30) ~= 0 then
return
end
for i = 1, #WG.metalSpots do
CheckEnemyMexes(i)
end
end
function widget:UnitCreated(unitID, unitDefID, teamID)
if mexBuilderDefs[unitDefID] then
mexBuilder[unitID] = true
return
end
if unitDefID ~= mexDefID or not WG.metalSpots then
return
end
local x,_,z = Spring.GetUnitPosition(unitID)
local spotID = WG.metalSpotsByPos[x] and WG.metalSpotsByPos[x][z]
if not spotID then
return
end
spotByID[unitID] = spotID
spotData[spotID] = {unitID = unitID, team = teamID}
updateMexDrawList()
end
function widget:UnitDestroyed(unitID, unitDefID)
if mexBuilder[unitID] then
mexBuilder[unitID] = nil
end
if unitDefID == mexDefID and spotByID[unitID] then
spotData[spotByID[unitID]] = nil
spotByID[unitID] = nil
updateMexDrawList()
end
end
function widget:UnitGiven(unitID, unitDefID, newTeamID, teamID)
if mexBuilderDefs[unitDefID] then
mexBuilder[unitID] = true
end
if unitDefID == mexDefID then
widget:UnitCreated(unitID, unitDefID, newTeamID)
end
end
local function Initialize()
local units = spGetAllUnits()
for i, unitID in ipairs(units) do
local unitDefID = spGetUnitDefID(unitID)
local teamID = Spring.GetUnitTeam(unitID)
widget:UnitCreated(unitID, unitDefID, teamID)
end
if WG.metalSpots then
Spring.Echo("Mex Placement Initialised with " .. #WG.metalSpots .. " spots.")
updateMexDrawList()
else
Spring.Echo("Mex Placement Initialised with metal map mode.")
end
WG.GetClosestMetalSpot = GetClosestMetalSpot
if WG.LocalColor and WG.LocalColor.RegisterListener then
WG.LocalColor.RegisterListener(widget:GetInfo().name, updateMexDrawList)
end
end
local mexSpotToDraw = false
local drawMexSpots = false
function widget:Update()
local isSpectating = spGetSpectatingState()
if WG.metalSpots and (wasSpectating ~= isSpectating) then
spotByID = {}
spotData = {}
wasSpectating = isSpectating
local units = spGetAllUnits()
for i, unitID in ipairs(units) do
local unitDefID = spGetUnitDefID(unitID)
local teamID = Spring.GetUnitTeam(unitID)
if unitDefID == mexDefID then
widget:UnitCreated(unitID, unitDefID, teamID)
end
end
end
if metalSpotsNil and WG.metalSpots ~= nil then
Initialize()
metalSpotsNil = false
end
WG.mouseoverMexIncome = false
if mexSpotToDraw and WG.metalSpots then
WG.mouseoverMexIncome = mexSpotToDraw.metal
WG.mouseoverMex = mexSpotToDraw
else
local _, cmd_id = spGetActiveCommand()
if -mexDefID ~= cmd_id then
return
end
local mx, my = spGetMouseState()
local _, coords = spTraceScreenRay(mx, my, true, true)
if (not coords) then
return
end
IntegrateMetal(coords[1], coords[3])
WG.mouseoverMexIncome = extraction
end
end
------------------------------------------------------------
-- Drawing
------------------------------------------------------------
local centerX
local centerZ
local extraction = 0
local mainMexDrawList = 0
local miniMexDrawList = 0
local function getSpotColor(id)
local teamID = spotData[id] and spotData[id].team or Spring.GetGaiaTeamID()
return Spring.GetTeamColor(teamID)
end
function calcMainMexDrawList()
local specatate = spGetSpectatingState()
if WG.metalSpots then
for i = 1, #WG.metalSpots do
local spot = WG.metalSpots[i]
local x,z = spot.x, spot.z
local y = spGetGroundHeight(x,z)
if y < 0 then y = 0 end
local r, g, b = getSpotColor(i)
local metal = spot.metal
glPushMatrix()
gl.DepthTest(true)
glColor(0,0,0,0.7)
-- glDepthTest(false)
glLineWidth(spot.metal*2.4)
glDrawGroundCircle(x, 1, z, 40, 21)
glColor(r,g,b,0.7)
glLineWidth(spot.metal*1.5)
glDrawGroundCircle(x, 1, z, 40, 21)
--glColor(0,1,1)
--glRect(x-width/2, z+18, x+width/2, z+20)
--glDepthTest(false)
glPopMatrix()
end
glColor(1,1,1)
glTexture("LuaUI/Images/ibeam.png")
glDepthTest(false)
for i = 1, #WG.metalSpots do
local spot = WG.metalSpots[i]
local x,z = spot.x, spot.z
local y = spGetGroundHeight(x,z)
if y < 0 then y = 0 end
local metal = spot.metal
glPushMatrix()
if options.drawicons.value then
local size = 1
if metal > 10 then
if metal > 100 then
metal = metal*0.01
size = 5
else
metal = metal*0.1
size = 2.5
end
end
size = options.size.value
glRotate(90,1,0,0)
glTranslate(0,0,-y-10)
local width = metal*size
glTexRect(x-width/2, z+40, x+width/2, z+40+size,0,0,metal,1)
else
-- Draws a metal bar at the center of the metal spot
glRotate(90,1,0,0)
glTranslate(0,0,-y)
glTexRect(x-25, z-25, x+25, z+25,0,0,1,1)
end
glPopMatrix()
end
glTexture(false)
if not options.drawicons.value then
--glColor(1,1,1) --already set
for i = 1, #WG.metalSpots do
local spot = WG.metalSpots[i]
local x,z = spot.x, spot.z
local y = spGetGroundHeight(x,z)
if y < 0 then y = 0 end
local metal = spot.metal
glPushMatrix()
glTranslate(x, y, z)
glRotate(-90, 1, 0, 0)
glTranslate(0, -40 - options.size.value, 0)
glText("+" .. ("%."..options.rounding.value.."f"):format(metal), 0.0, 0.0, options.size.value , "cno")
glPopMatrix()
end
end
glLineWidth(1.0)
glColor(1,1,1,1)
end
end
--[[
function calcMiniMexDrawList()
local specatate = spGetSpectatingState()
glLoadIdentity()
glTranslate(0,1,0)
glScale(mapXinv , -mapZinv, 1)
glRotate(270,1,0,0)
for i = 1, #WG.metalSpots do
local spot = WG.metalSpots[i]
local x,z = spot.x, spot.z
local y = spGetGroundHeight(x,z)
local r, g, b = getSpotColor(i)
glLineWidth(spot.metal)
glColor(r, g, b)
glDrawGroundCircle(x, 0, z, 40, 32)
glPushMatrix()
glPopMatrix()
end
glLineWidth(1.0)
glColor(1,1,1,1)
end
--]]
function updateMexDrawList()
if not WG.metalSpots then
return
end
if (mainMexDrawList) then
gl.DeleteList(mainMexDrawList);
mainMexDrawList = nil
end --delete previous list if exist (ref:gui_chicken.lua by quantum)
mainMexDrawList = glCreateList(calcMainMexDrawList)
--miniMexDrawList = glCreateList(calcMiniMexDrawList)
end
function widget:Shutdown()
gl.DeleteList(mainMexDrawList)
end
local function DoLine(x1, y1, z1, x2, y2, z2)
gl.Vertex(x1, y1, z1)
gl.Vertex(x2, y2, z2)
end
function widget:DrawWorldPreUnit()
-- Check command is to build a mex
local _, cmdID = spGetActiveCommand()
local showecoMode = WG.showeco
local peruse = spGetGameFrame() < 1 or showecoMode or spGetMapDrawMode() == 'metal'
drawMexSpots = WG.metalSpots and (-mexDefID == cmdID or CMD_AREA_MEX == cmdID or peruse)
if drawMexSpots then
gl.DepthTest(true)
gl.DepthMask(true)
glCallList(mainMexDrawList)
gl.DepthTest(false)
gl.DepthMask(false)
end
end
function widget:DrawWorld()
-- Check command is to build a mex
local _, cmdID = spGetActiveCommand()
local showecoMode = WG.showeco
local pregame = (spGetGameFrame() < 1)
local peruse = pregame or showecoMode or spGetMapDrawMode() == 'metal'
local mx, my = spGetMouseState()
local _, pos = spTraceScreenRay(mx, my, true)
mexSpotToDraw = false