-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathgui_attack_aoe.lua
More file actions
1161 lines (1006 loc) · 35.2 KB
/
Copy pathgui_attack_aoe.lua
File metadata and controls
1161 lines (1006 loc) · 35.2 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
-- $Id: gui_attack_aoe.lua 3823 2009-01-19 23:40:49Z evil4zerggin $
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local versionNumber = "v3.1"
function widget:GetInfo()
return {
name = "Attack AoE",
desc = versionNumber .. " Cursor indicator for area of effect and scatter when giving attack command.",
author = "Evil4Zerggin",
date = "26 September 2008",
license = "GNU LGPL, v2.1 or later",
layer = 1,
enabled = true -- loaded by default?
}
end
--------------------------------------------------------------------------------
--config
--------------------------------------------------------------------------------
local numScatterPoints = 32
local aoeColor = {1, 0, 0, 1}
local cloakerColor = {0, 0.8, 0.8, 1}
local aoeLineWidthMult = 64
local scatterColor = {1, 1, 0, 1}
local scatterLineWidthMult = 1024
local depthColor = {1, 0, 0, 0.5}
local depthLineWidth = 3
local circleDivs = 64
local minSpread = 8 --weapons with this spread or less are ignored
local numAoECircles = 9
local BUFFER = 20
local startHeights = {
["missileslow_weapon"] = -40 - BUFFER,
["seismic_seismic_weapon"] = -27 - BUFFER,
["tacnuke_weapon"] = -22 - BUFFER,
["empmissile_emp_weapon"] = -17 - BUFFER,
["napalmmissile_weapon"] = -27 - BUFFER,
["shipcarrier_disarm_rocket"] = -25 - BUFFER,
["subtacmissile_tacnuke"] = -25 - BUFFER,
}
--------------------------------------------------------------------------------
--vars
--------------------------------------------------------------------------------
local aoeDefInfo = {}
local dgunInfo = {}
local extraDrawRangeDefInfo ={}
local unitAoeDefs = {}
local unitDgunDefs = {}
local unitHasBeenSetup = {}
local aoeUnitInfo
local aoeUnitID
local dgunUnitInfo
local selUnitID
local circleList
local secondPart = 0
local mouseDistance = 1000
local extraDrawRange
local sumoSelected = false
local detrimentSelected = false
local detrimentUnitID = nil
--------------------------------------------------------------------------------
--speedups
--------------------------------------------------------------------------------
local GetActiveCommand = Spring.GetActiveCommand
local GetCameraPosition = Spring.GetCameraPosition
local GetFeaturePosition = Spring.GetFeaturePosition
local GetGroundHeight = Spring.GetGroundHeight
local GetMouseState = Spring.GetMouseState
local GetUnitPosition = Spring.GetUnitPosition
local GetUnitRadius = Spring.GetUnitRadius
local TraceScreenRay = Spring.TraceScreenRay
local spGetUnitDefID = Spring.GetUnitDefID
local CMD_ATTACK = CMD.ATTACK
local CMD_MANUALFIRE = CMD.MANUALFIRE
local CMD_AIR_MANUALFIRE = Spring.Utilities.CMD.AIR_MANUALFIRE
local g = Game.gravity
local GAME_SPEED = 30
local g_f = g / GAME_SPEED / GAME_SPEED
local glBeginEnd = gl.BeginEnd
local glCallList = gl.CallList
local glCreateList = gl.CreateList
local glColor = gl.Color
local glDeleteList = gl.DeleteList
local glDepthTest = gl.DepthTest
local glDrawGroundCircle = gl.DrawGroundCircle
local glLineStipple = gl.LineStipple
local glLineWidth = gl.LineWidth
local glPointSize = gl.PointSize
local glPopMatrix = gl.PopMatrix
local glPushMatrix = gl.PushMatrix
local glRotate = gl.Rotate
local glScale = gl.Scale
local glTranslate = gl.Translate
local glVertex = gl.Vertex
local GL_LINES = GL.LINES
local GL_LINE_LOOP = GL.LINE_LOOP
local GL_POINTS = GL.POINTS
local PI = math.pi
local atan = math.atan
local cos = math.cos
local sin = math.sin
local floor = math.floor
local max = math.max
local min = math.min
local sqrt = math.sqrt
VFS.Include("LuaRules/Configs/customcmds.h.lua")
local sumoDefID = UnitDefNames.jumpsumo.id
local sumoAoE = WeaponDefNames.jumpsumo_landing.damageAreaOfEffect
local sumoEE = WeaponDefNames.jumpsumo_landing.edgeEffectiveness
local detrimentDefID = UnitDefNames.striderdetriment.id
local detrimentLandingAoE = WeaponDefNames.striderdetriment_landing.damageAreaOfEffect
local detrimentLandingEE = WeaponDefNames.striderdetriment_landing.edgeEffectiveness
--------------------------------------------------------------------------------
--utility functions
--------------------------------------------------------------------------------
local function ToBool(x)
return x and x ~= 0 and x ~= "false"
end
local function Normalize(x, y, z)
local mag = sqrt(x*x + y*y + z*z)
if (mag == 0) then
return
nil
else
return x/mag, y/mag, z/mag, mag
end
end
local function VertexList(points)
for i, point in pairs(points) do
glVertex(point)
end
end
local function GetMouseTargetPosition()
local mx, my = GetMouseState()
local alt = Spring.GetModKeyState()
local mouseTargetType, mouseTarget = TraceScreenRay(mx, my, alt, true, false, true)
if (mouseTargetType == "ground") then
return mouseTarget[1], mouseTarget[2], mouseTarget[3], true
elseif (mouseTargetType == "unit") then
local ux, uy, uz, ax, ay, az = GetUnitPosition(mouseTarget, false, true)
if not ax then
return ux, uy, uz
end
return ax, ay, az
elseif (mouseTargetType == "feature") then
local _, coords = TraceScreenRay(mx, my, true, true, false, true)
if coords and coords[3] then
return coords[1], coords[2], coords[3], true
else
return GetFeaturePosition(mouseTarget)
end
else
return nil
end
end
local function GetMouseDistance()
local cx, cy, cz = GetCameraPosition()
local mx, my, mz = GetMouseTargetPosition()
if (not mx) then
return nil
end
local dx = cx - mx
local dy = cy - my
local dz = cz - mz
return sqrt(dx*dx + dy*dy + dz*dz)
end
local function UnitCircleVertices()
for i = 1, circleDivs do
local theta = 2 * PI * i / circleDivs
glVertex(cos(theta), 0, sin(theta))
end
end
local function DrawUnitCircle()
glBeginEnd(GL_LINE_LOOP, UnitCircleVertices)
end
local function DrawCircle(x, y, z, radius)
glPushMatrix()
glTranslate(x, y, z)
glScale(radius, radius, radius)
glCallList(circleList)
glPopMatrix()
end
local function GetSecondPart(offset)
local result = secondPart + (offset or 0)
return result - floor(result)
end
--------------------------------------------------------------------------------
--initialization
--------------------------------------------------------------------------------
local function getWeaponInfo(weaponDef, unitDef)
local retData
local weaponType = weaponDef.type
local spray = (weaponDef.customParams and weaponDef.customParams.gui_sprayangle) or weaponDef.sprayAngle
local scatter = weaponDef.accuracy + spray
local aoe = tonumber(weaponDef.customParams.gui_aoe) or weaponDef.damageAreaOfEffect
local cost = unitDef.metalCost
local waterWeapon = weaponDef.waterWeapon
local ee = tonumber(weaponDef.customParams.gui_ee) or weaponDef.edgeEffectiveness
if (weaponDef.cylinderTargetting >= 100) then
retData = {type = "orbital", scatter = scatter}
elseif (weaponType == "Cannon") then
retData = {
type = "ballistic",
scatter = scatter,
v = (weaponDef.customParams.weaponvelocity or 0),
range = weaponDef.range,
mygravity = weaponDef.customParams and weaponDef.customParams.mygravity and weaponDef.customParams.mygravity*800
}
elseif (weaponType == "MissileLauncher") then
local turnRate = 0
if (weaponDef.tracks) then
turnRate = weaponDef.turnRate
end
if (weaponDef.wobble > turnRate * 1.4) then
scatter = (weaponDef.wobble - weaponDef.turnRate) * (weaponDef.customParams.weaponvelocity or 0) * 16
local rangeScatter = (8 * weaponDef.wobble - weaponDef.turnRate)
retData = {type = "wobble", scatter = scatter, rangeScatter = rangeScatter, range = weaponDef.range}
elseif (weaponDef.wobble > turnRate) then
scatter = (weaponDef.wobble - weaponDef.turnRate) * (weaponDef.customParams.weaponvelocity or 0) * 16
retData = {type = "wobble", scatter = scatter}
elseif (weaponDef.tracks) then
retData = {type = "tracking"}
else
retData = {type = "direct", scatter = scatter, range = weaponDef.range}
end
elseif (weaponType == "AircraftBomb") then
retData = {type = "dropped", scatter = scatter, v = unitDef.speed, h = unitDef.cruiseAltitude, salvoSize = weaponDef.salvoSize, salvoDelay = weaponDef.salvoDelay}
elseif (weaponType == "StarburstLauncher") then
if (weaponDef.tracks) then
retData = {type = "tracking", range = weaponDef.range}
else
retData = {type = "cruise", range = weaponDef.range}
end
elseif (weaponType == "TorpedoLauncher") then
if (weaponDef.tracks) then
retData = {type = "tracking"}
else
retData = {type = "direct", scatter = scatter, range = weaponDef.range}
end
elseif (weaponType == "Flame" or weaponDef.noExplode) then
retData = {type = "noexplode", range = weaponDef.range}
else
retData = {type = "direct", scatter = scatter, range = weaponDef.range}
end
if weaponDef.customParams.gui_aoe or not weaponDef.impactOnly then
retData.aoe = aoe
else
retData.aoe = 0
end
if (weaponDef.uptime or 0) > 0 then
-- In the first frame the projectile moves startVelocity + 2*Acceleration
local startSpeed = math.min(weaponDef.startvelocity + weaponDef.weaponAcceleration, weaponDef.projectilespeed)
retData.vlaunch = {
upFrames = math.floor(weaponDef.uptime * 30 + 0.5) - 2,
accel = weaponDef.weaponAcceleration,
turnRate = weaponDef.turnRate,
startSpeed = startSpeed,
startHeight = startHeights[weaponDef.name] or 0,
endSpeed = weaponDef.projectilespeed,
}
end
retData.cost = cost
retData.mobile = not unitDef.isImmobile
retData.waterWeapon = waterWeapon
retData.ee = ee
return retData
end
local function SetupUnit(unitDef, unitID)
if (not unitDef.weapons) then
return
end
local weapon1, weapon2, rangeMult, rangeBoost
local manualfireWeapon = unitDef.customParams.air_manual_fire_weapon and tonumber(unitDef.customParams.air_manual_fire_weapon)
if unitID then
weapon1 = Spring.GetUnitRulesParam(unitID, "comm_weapon_num_1")
weapon2 = Spring.GetUnitRulesParam(unitID, "comm_weapon_num_2")
local manual1 = Spring.GetUnitRulesParam(unitID, "comm_weapon_manual_1") == 1
local manual2 = Spring.GetUnitRulesParam(unitID, "comm_weapon_manual_2") == 1
if manual1 then
manualfireWeapon = weapon1
elseif manual2 then
manualfireWeapon = weapon2
end
rangeBoost = Spring.GetUnitRulesParam(unitID, "comm_range_boost")
rangeMult = Spring.GetUnitRulesParam(unitID, "rangeMult")
end
local retDgunInfo
local retAoeInfo
local maxSpread = minSpread
local maxWeaponDef
for num, weapon in ipairs(unitDef.weapons) do
if (weapon.weaponDef) and ((not unitID) or num == weapon1 or num == weapon2) then
local weaponDef = WeaponDefs[weapon.weaponDef]
if (weaponDef) then
local aoe = tonumber(weaponDef.customParams.gui_aoe) or weaponDef.damageAreaOfEffect
if (weaponDef.manualFire and unitDef.canManualFire) or num == manualfireWeapon then
retDgunInfo = getWeaponInfo(weaponDef, unitDef)
if retDgunInfo.range then
retDgunInfo.circleMode = weaponDef.customParams.attack_aoe_circle_mode
if weaponDef.customParams.truerange then
retDgunInfo.range = tonumber(weaponDef.customParams.truerange)
end
if weaponDef.customParams.gui_draw_range then
retDgunInfo.range = tonumber(weaponDef.customParams.gui_draw_range)
end
if weaponDef.customParams.gui_draw_leashed_to_range then
retDgunInfo.drawLeashedToRange = true
end
if rangeBoost then
retDgunInfo.range = retDgunInfo.range + rangeBoost
end
if rangeMult then
retDgunInfo.range = retDgunInfo.range * rangeMult
end
end
elseif (not weaponDef.isShield
and not ToBool(weaponDef.interceptor) and not ToBool(weaponDef.customParams.hidden)
and (aoe > maxSpread or weaponDef.range * (weaponDef.accuracy + weaponDef.sprayAngle) > maxSpread )) then
local spray = (weaponDef.customParams and weaponDef.customParams.gui_sprayangle) or weaponDef.sprayAngle
maxSpread = max(aoe, weaponDef.range * (weaponDef.accuracy + spray))
maxWeaponDef = weaponDef
end
end
end
end
if (maxWeaponDef) then
retAoeInfo = getWeaponInfo(maxWeaponDef, unitDef)
retAoeInfo.circleMode = maxWeaponDef.customParams.attack_aoe_circle_mode
if maxWeaponDef.customParams.gui_draw_range then
retAoeInfo.range = tonumber(maxWeaponDef.customParams.gui_draw_range)
end
if maxWeaponDef.customParams.gui_draw_leashed_to_range then
retAoeInfo.drawLeashedToRange = true
end
if retAoeInfo.range and rangeBoost then
retAoeInfo.range = retAoeInfo.range + rangeBoost
end
if retAoeInfo.range and rangeMult then
retAoeInfo.range = retAoeInfo.range * rangeMult
end
end
local extraDrawRangeInfo = unitDef and unitDef.customParams and unitDef.customParams.extradrawrange
return retAoeInfo, retDgunInfo, extraDrawRangeInfo
end
local function SetupDisplayLists()
circleList = glCreateList(DrawUnitCircle)
end
local function DeleteDisplayLists()
glDeleteList(circleList)
end
--------------------------------------------------------------------------------
--updates
--------------------------------------------------------------------------------
local function UpdateSelection(sel)
local maxCost = 0
dgunUnitInfo = false
aoeUnitInfo = false
aoeUnitID = false
sumoSelected = false
detrimentSelected = false
detrimentUnitID = nil
local seenCount = {}
for i = 1, #sel do
local unitID = sel[i]
local unitDefID = spGetUnitDefID(unitID)
if unitDefID then
seenCount[unitDefID] = (seenCount[unitDefID] or 0) + 1
if unitDefID == sumoDefID then
sumoSelected = true
end
if unitDefID == detrimentDefID then
detrimentSelected = true
detrimentUnitID = unitID
end
local dynamicComm = Spring.GetUnitRulesParam(unitID, "comm_level")
if dynamicComm and not unitHasBeenSetup[unitID] then
unitAoeDefs[unitID], unitDgunDefs[unitID] = SetupUnit(UnitDefs[unitDefID], unitID)
unitHasBeenSetup[unitID] = true
end
if (dgunInfo[unitDefID]) then
local dgunInfo = unitDgunDefs[unitID] or ((not dynamicComm) and dgunInfo[unitDefID])
if dgunInfo then
dgunUnitInfo = dgunUnitInfo or {}
dgunUnitInfo[unitID] = dgunInfo
end
end
if (aoeDefInfo[unitDefID]) then
local currCost = Spring.Utilities.GetUnitCost(unitID, unitDefID) * seenCount[unitDefID]
if (currCost > maxCost) then
maxCost = currCost
aoeUnitID = unitID
aoeUnitInfo = unitAoeDefs[unitID] or ((not dynamicComm) and aoeDefInfo[unitDefID])
end
end
local extraDrawParam = Spring.GetUnitRulesParam(unitID, "secondary_range")
if extraDrawParam then
extraDrawRange = extraDrawParam
else
extraDrawRange = extraDrawRangeDefInfo[unitDefID]
end
if extraDrawRange then
selUnitID = unitID
end
end
end
end
--------------------------------------------------------------------------------
--aoe
--------------------------------------------------------------------------------
local function DrawAoE(tx, ty, tz, aoe, ee, alphaMult, offset, circleMode)
glLineWidth(math.max(0.05, aoeLineWidthMult * aoe / mouseDistance))
if not circleMode then
for i = 1, numAoECircles do
local proportion = i / (numAoECircles + 1)
local radius = aoe * proportion
local alpha = aoeColor[4] * (1 - proportion) / (1 - proportion * ee) * (1 - GetSecondPart(offset or 0)) * (alphaMult or 1)
glColor(aoeColor[1], aoeColor[2], aoeColor[3], alpha)
DrawCircle(tx, ty, tz, radius)
end
elseif circleMode == "cloaker" then
for i = 1, 3 do
local proportion = (i + 17) / 20
local radius = aoe * proportion
local alpha = aoeColor[4] * (i / 3) * (1 - GetSecondPart(offset or 0)) * (alphaMult or 0.55)
glColor(cloakerColor[1], cloakerColor[2], cloakerColor[3], alpha)
DrawCircle(tx, ty, tz, radius)
end
end
glColor(1,1,1,1)
glLineWidth(1)
end
--------------------------------------------------------------------------------
-- Shared blast-radius preview, exposed via WG so other widgets (e.g. the missile
-- silo launch UI) draw their AoE footprint with THIS falloff code instead of
-- duplicating it. Nested rings at (tx,ty,tz) whose alpha decays toward the edge by
-- edgeEffectiveness `ee`. `color` (optional {r,g,b[,a]}) overrides the ring colour;
-- `alphaMult` (optional) scales overall opacity. Line width is derived from the
-- camera distance to the point, so callers need no per-frame setup of their own.
--------------------------------------------------------------------------------
local function DrawAoEPreview(tx, ty, tz, aoe, ee, color, alphaMult)
if not aoe or aoe <= 0 then
return
end
ee = ee or 1
local cx, cy, cz = GetCameraPosition()
local dx, dy, dz = cx - tx, cy - ty, cz - tz
local camDist = sqrt(dx*dx + dy*dy + dz*dz)
if camDist < 1 then
camDist = 1
end
local r = (color and color[1]) or aoeColor[1]
local g = (color and color[2]) or aoeColor[2]
local b = (color and color[3]) or aoeColor[3]
local baseAlpha = ((color and color[4]) or aoeColor[4]) * (alphaMult or 1)
glLineWidth(math.max(0.05, aoeLineWidthMult * aoe / camDist))
for i = 1, numAoECircles do
local proportion = i / (numAoECircles + 1)
local alpha = baseAlpha * (1 - proportion) / (1 - proportion * ee)
glColor(r, g, b, alpha)
DrawCircle(tx, ty, tz, aoe * proportion)
end
glColor(1, 1, 1, 1)
glLineWidth(1)
end
--------------------------------------------------------------------------------
--dgun/noexplode
--------------------------------------------------------------------------------
local function DrawNoExplode(aoe, fx, fy, fz, tx, ty, tz, range)
local dx = tx - fx
local dy = ty - fy
local dz = tz - fz
local bx, by, bz, dist = Normalize(dx, dy, dz)
if (not bx or dist > range) then
return
end
local br = sqrt(bx*bx + bz*bz)
if br <= 0.1 then
return
end
local wx = -aoe * bz / br
local wz = aoe * bx / br
local ex = range * bx / br
local ez = range * bz / br
local vertices = {{fx + wx, fy, fz + wz}, {fx + ex + wx, ty, fz + ez + wz},
{fx - wx, fy, fz - wz}, {fx + ex - wx, ty, fz + ez - wz}}
local alpha = (1 - GetSecondPart()) * aoeColor[4]
glColor(aoeColor[1], aoeColor[2], aoeColor[3], alpha)
glLineWidth(scatterLineWidthMult / mouseDistance)
glBeginEnd(GL_LINES, VertexList, vertices)
glColor(1,1,1,1)
glLineWidth(1)
end
--------------------------------------------------------------------------------
--ballistics
--------------------------------------------------------------------------------
local function GetBallisticVector(v, mg, dx, dy, dz, trajectory, range)
local dr_sq = dx*dx + dz*dz
local dr = sqrt(dr_sq)
if (dr > range) then
return nil
end
local d_sq = dr_sq + dy*dy
if (d_sq == 0) then
return 0, v * trajectory, 0
end
local root1 = v*v*v*v - 2*v*v*mg*dy - mg*mg*dr_sq
if (root1 < 0) then
return nil
end
local root2 = 2*dr_sq*d_sq*(v*v - mg*dy - trajectory*sqrt(root1))
if (root2 < 0) then
return nil
end
local vr = sqrt(root2)/(2*d_sq)
local vy
if (r == 0 or vr == 0) then
vy = v
else
vy = vr*dy/dr + dr*mg/(2*vr)
end
local bx = dx*vr/dr
local bz = dz*vr/dr
local by = vy
return Normalize(bx, by, bz)
end
local function GetBallisticImpactPoint(v, mg_f, fx, fy, fz, bx, by, bz)
local v_f = v / GAME_SPEED
local vx_f = bx * v_f
local vy_f = by * v_f
local vz_f = bz * v_f
local px = fx
local py = fy
local pz = fz
local ttl = 4 * v_f / mg_f
for i = 1, ttl do
px = px + vx_f
py = py + vy_f
pz = pz + vz_f
vy_f = vy_f - mg_f
local gwh = max(GetGroundHeight(px, pz), 0)
if (py < gwh) then
local interpolate = min((py - gwh) / vy_f, 1)
local x = px - interpolate * vx_f
local z = pz - interpolate * vz_f
return {x, max(GetGroundHeight(x, z), 0), z}
end
end
return {px, py, pz}
end
--v: weaponvelocity
--trajectory: +1 for high, -1 for low
local function DrawBallisticScatter(scatter, v, mygravity ,fx, fy, fz, tx, ty, tz, trajectory, range)
if (scatter == 0) then
return
end
local dx = tx - fx
local dy = ty - fy
local dz = tz - fz
if (dx == 0 and dz == 0) then
return
end
local mg = mygravity or g
local bx, by, bz = GetBallisticVector(v, mg, dx, dy, dz, trajectory, range)
--don't draw anything if out of range
if (not bx) then
return
end
local br = sqrt(bx*bx + bz*bz)
if br <= 0.1 then
return
end
--bars
local rx = dx / br
local rz = dz / br
local wx = -scatter * rz
local wz = scatter * rx
local barLength = sqrt(wx*wx + wz*wz) --length of bars
local barX = 0.5 * barLength * bx / br
local barZ = 0.5 * barLength * bz / br
local sx = tx - barX
local sz = tz - barZ
local lx = tx + barX
local lz = tz + barZ
local wsx = -scatter * (rz - barZ)
local wsz = scatter * (rx - barX)
local wlx = -scatter * (rz + barZ)
local wlz = scatter * (rx + barX)
local bars = {{tx + wx, ty, tz + wz}, {tx - wx, ty, tz - wz},
{sx + wsx, ty, sz + wsz}, {lx + wlx, ty, lz + wlz},
{sx - wsx, ty, sz - wsz}, {lx - wlx, ty, lz - wlz}}
local scatterDiv = scatter / numScatterPoints
local vertices = {}
local mg_f = mg / GAME_SPEED / GAME_SPEED
--trace impact points
for i = -numScatterPoints, numScatterPoints do
local currScatter = i * scatterDiv
local currScatterCos = sqrt(1 - currScatter * currScatter)
local rMult = currScatterCos - by * currScatter / br
local bx_c = bx * rMult
local by_c = by * currScatterCos + br * currScatter
local bz_c = bz * rMult
vertices[i+numScatterPoints+1] = GetBallisticImpactPoint(v, mg_f, fx, fy, fz, bx_c, by_c, bz_c)
end
glLineWidth(scatterLineWidthMult / mouseDistance)
-- FIXME ATIBUG glPointSize(pointSizeMult / mouseDistance)
glColor(scatterColor)
glDepthTest(false)
glBeginEnd(GL_LINES, VertexList, bars)
glBeginEnd(GL_POINTS, VertexList, vertices)
glDepthTest(true)
glColor(1,1,1,1)
-- FIXME ATIBUG glPointSize(1)
glLineWidth(1)
end
--------------------------------------------------------------------------------
--wobble
--------------------------------------------------------------------------------
local function DrawWobbleScatter(scatter, fx, fy, fz, tx, ty, tz, rangeScatter, range)
local dx = tx - fx
local dy = ty - fy
local dz = tz - fz
local bx, by, bz, d = Normalize(dx, dy, dz)
glColor(scatterColor)
glLineWidth(scatterLineWidthMult / mouseDistance)
if d and range then
if d <= range then
DrawCircle(tx, ty, tz, rangeScatter * d + scatter)
end
else
DrawCircle(tx, ty, tz, scatter)
end
glColor(1,1,1,1)
glLineWidth(1)
end
--------------------------------------------------------------------------------
--direct
--------------------------------------------------------------------------------
local function DrawDirectScatter(scatter, fx, fy, fz, tx, ty, tz, range, unitRadius)
local dx = tx - fx
local dy = ty - fy
local dz = tz - fz
local bx, by, bz, d = Normalize(dx, dy, dz)
if (not bx or d == 0 or d > range) then
return
end
local byInv = sqrt(1 - by*by)
if byInv == 0 then
return
end
local ux = bx * unitRadius / byInv
local uz = bz * unitRadius / byInv
local cx = -scatter * uz
local cz = scatter * ux
local wx = -scatter * dz / byInv
local wz = scatter * dx / byInv
local vertices = {{fx + ux + cx, fy, fz + uz + cz}, {tx + wx, ty, tz + wz},
{fx + ux - cx, fy, fz + uz - cz}, {tx - wx, ty, tz - wz}}
glColor(scatterColor)
glLineWidth(scatterLineWidthMult / mouseDistance)
glBeginEnd(GL_LINES, VertexList, vertices)
glColor(1,1,1,1)
glLineWidth(1)
end
--------------------------------------------------------------------------------
--dropped
--------------------------------------------------------------------------------
local function DrawDroppedScatter(aoe, ee, scatter, v, fx, fy, fz, tx, ty, tz, salvoSize, salvoDelay)
local dx = tx - fx
local dz = tz - fz
local bx, _, bz = Normalize(dx, 0, dz)
if (not bx) then
return
end
local vertices = {}
local currScatter = scatter * v * sqrt(2*fy/g)
local alphaMult = min(v * salvoDelay / aoe, 1)
for i=1,salvoSize do
local delay = salvoDelay * (i - (salvoSize + 1) / 2)
local dist = v * delay
local px_c = dist * bx + tx
local pz_c = dist * bz + tz
local py_c = max(GetGroundHeight(px_c, pz_c), 0)
DrawAoE(px_c, py_c, pz_c, aoe, ee, alphaMult, -delay)
glColor(scatterColor[1], scatterColor[2], scatterColor[3], scatterColor[4] * alphaMult)
glLineWidth(scatterLineWidthMult / mouseDistance)
DrawCircle(px_c, py_c, pz_c, currScatter)
end
glColor(1,1,1,1)
glLineWidth(1)
end
--------------------------------------------------------------------------------
--orbital
--------------------------------------------------------------------------------
local function DrawOrbitalScatter(scatter, tx, ty, tz)
glColor(scatterColor)
glLineWidth(scatterLineWidthMult / mouseDistance)
DrawCircle(tx, ty, tz, scatter)
glColor(1,1,1,1)
glLineWidth(1)
end
--------------------------------------------------------------------------------
--underwater
--------------------------------------------------------------------------------
local function DrawWaterDepth(tx, ty, tz)
glColor(depthColor)
glLineWidth(depthLineWidth)
glLineStipple(1, 255)
glBeginEnd(GL_LINES, VertexList, {{tx,0,tz},{tx,ty,tz}})
glLineStipple(false)
glColor(1,1,1,1)
glLineWidth(1)
end
local function LeashDrawRange(unitID, range, tx, ty, tz)
local ux, _, uz = GetUnitPosition(unitID)
if not ux then
return tx, ty, tz
end
vx, vz = (tx - ux), (tz - uz)
local dist = math.sqrt(vx*vx + vz*vz)
if dist < range then
return tx, ty, tz
end
tx, tz = ux + vx * range/dist, uz + vz * range/dist
ty = Spring.GetGroundHeight(tx, tz)
return tx, ty, tz
end
--------------------------------------------------------------------------------
--Vlaunch missile calculation
--------------------------------------------------------------------------------
local function DrawVlaunchImpact(hx, hy, hz, tx, ty, tz)
glColor(depthColor)
glLineWidth(depthLineWidth)
glLineStipple(1, 255)
glBeginEnd(GL_LINES, VertexList, {{hx,hy,hz},{tx,ty,tz}})
glLineStipple(false)
glColor(1,1,1,1)
glLineWidth(1)
end
local function CalculateVlaunchImpact(info, fx, fy, fz, tx, ty, tz)
local vlaunch = info.vlaunch
local speed = vlaunch.startSpeed
local y = vlaunch.startHeight
local horDist = math.sqrt((fx - tx)*(fx - tx) + (fz - tz)*(fz - tz))
local vertDist = ty - fy
if (not info.range) or info.range + 10 < horDist or horDist < 100 then
return false
end
--if doEcho then
-- Spring.Utilities.TableEcho(vlaunch)
--end
-- Fly upwards
for i = 1, vlaunch.upFrames do
speed = math.min(vlaunch.endSpeed, speed + vlaunch.accel)
y = y + speed
--if doEcho then
-- Spring.Echo("GUP", 0, fy + y, speed)
-- doEcho = false
--end
end
-- Turn to point at target
local pitch = math.pi/2
local hor = 0
local timeout = 1000
while pitch > -1.7 and timeout > 0 do
-- vlanuch missiles don't accelerate while turning.
pitch = pitch - vlaunch.turnRate
hor = hor + math.cos(pitch) * speed
y = y + math.sin(pitch) * speed
--if doEcho then
-- Spring.Echo("TRN", hor, fy + y, speed)
-- doEcho = false
--end
if horDist <= hor then
-- Impact site is within turning circle, assume no terrain is in the way.
return false
end
local pitchToTarget = math.atan((vertDist - y) / (horDist - hor))
if math.abs(pitchToTarget - pitch) < 0.14154 then -- Snap to target when within acos(0.99) radians
pitch = pitchToTarget
break
end
timeout = timeout - 1
end
-- Fly towards target and hit it.
local velUnitHor, velUnitY = math.cos(pitch), math.sin(pitch)
local toX, toZ = (tx - fx) / horDist, (tz - fz) / horDist
local oldX, oldY, oldZ = false, false, false
local distSq = (horDist - hor)*(horDist - hor) + (vertDist - y)*(vertDist - y)
local prevDistSq = false
local speedFactor = 1
local accelerating = (vlaunch.accel > 0)
timeout = 1000
while (distSq > 100000 or (((not prevDistSq) or distSq < prevDistSq) and distSq > 3000)) and timeout > 0 do
if accelerating then
speed = speed + vlaunch.accel
if speed > vlaunch.endSpeed then
speed = vlaunch.endSpeed
accelerating = false
end
end
prevDistSq = distSq
hor = hor + velUnitHor * speed * speedFactor
y = y + velUnitY * speed * speedFactor
distSq = (horDist - hor)*(horDist - hor) + (vertDist - y)*(vertDist - y)
local px, py, pz = fx + toX*hor, fy + y, fz + toZ*hor
--if doEcho then
-- Spring.Echo("FLY", hor, py, speed)
-- doEcho = false
--end
local groundHeight = (GetGroundHeight(px, pz) or 0)
if groundHeight + 5 > py then
return px, groundHeight, pz
end
local aboveGround = py - groundHeight
if aboveGround < 80 and oldX then
-- Interpolate when near the ground. Does the engine do this? Not sure.
for i = 1, 3 do
local prop = i/4
local ix, iy, iz = prop*px + (1 - prop)*oldX, prop*py + (1 - prop)*oldY, prop*pz + (1 - prop)*oldZ
groundHeight = (GetGroundHeight(ix, iz) or 0)
if groundHeight + 5 > iy then
return ix, iy, iz
end
end
end
-- Save some time if we are super high in the sky.
if not accelerating then
if aboveGround > 1200 then
speedFactor = 40
elseif aboveGround > 800 then
speedFactor = 20
elseif aboveGround > 500 then
speedFactor = 5
else
speedFactor = 1
end
end
timeout = timeout - 1
oldX, oldY, oldZ = px, py, pz
end
return false
end
--------------------------------------------------------------------------------
-- Shared terrain-impact test, exposed via WG for the missile silo launch UI. Given a
-- firing unit (a vlaunch missile sitting on its pad) and a target point, returns the
-- terrain impact point (hx, hy, hz) if that unit's vlaunch trajectory is projected to
-- strike the ground before reaching the target (a hill in the way), or nil if it
-- reaches the target -- or has no vlaunch data, so the caller simply doesn't filter.
-- Uses exactly the same CalculateVlaunchImpact path this widget draws (the depth line
-- shown when force-firing), so the silo UI's "which silo can actually hit here" filter
-- matches what a player sees.
--------------------------------------------------------------------------------
local function GetVlaunchTerrainImpact(unitID, tx, ty, tz)
local unitDefID = spGetUnitDefID(unitID)
local info = unitDefID and aoeDefInfo[unitDefID]
if not (info and info.vlaunch) or info.circleMode == "cloaker" then
return nil
end
local _,_,_,fx, fy, fz = GetUnitPosition(unitID, true)
if not fx then
return nil
end
if not info.mobile then
fy = fy + GetUnitRadius(unitID)
end
if not info.waterWeapon then