/
gui_healthbars.lua
1543 lines (1279 loc) · 55.6 KB
/
gui_healthbars.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 = "Health Bars",
desc = "Options: /healthbars_style, /healthbars_percentage",
author = "Floris (original plain bars by jK)",
date = "28 march 2015",
license = "GNU GPL, v2 or later",
layer = -10,
enabled = true
}
end
--------------------------------------------------------------------------------
-- Console commands
--------------------------------------------------------------------------------
-- /healthbars_percentage -- toggles rendering of the textual percentage beside each bar
-- /healthbars_compercentage -- toggles always rendering health precentagees for coms
-- /healthbars_style -- toggles different styles
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local barScale = 1
local barHeightOffset = 34 -- set value that healthbars for units that can unfold and become larger than its unitdef.height are still visible
local barHeight = 5
local barWidth = 24 --// (barWidth)x2 total width!!!
local barAlpha = 0.85
local barOutlineAlpha = 0.8
local barInnerAlpha = 0.5
local barValueAlpha = 0.9 -- alpha of the colored part
local featureBarHeight = 1.6
local featureBarWidth = 7.5
local featureBarAlpha = 0.6
local drawBarTitles = true -- (I disabled the healthbar text, cause that one doesnt need an explanation)
local titlesAlpha = 0.3*barAlpha
local drawBarPercentage = 0 -- wont draw heath percentage text above this percentage
local alwaysDrawBarPercentageForComs = true
local drawFeatureBarPercentage = 0 -- true: commanders always will show health percentage number
local choppedCornerSize = 0.45
local outlineSize = 0.75
local drawFullHealthBars = false
local unitHpThreshold = 0.99
local drawFeatureHealth = true
local featureTitlesAlpha = featureBarAlpha * titlesAlpha/barAlpha
local featureHpThreshold = 0.85
local featureResurrectVisibility= true -- draw feature bars for resurrected features on same distance as normal unit bars
local featureReclaimVisibility = true -- draw feature bars for reclaimed features on same distance as normal unit bars
local minPercentageDistance = 120000 -- always show health percentage text below this distance
local infoDistance = 800000
local maxFeatureInfoDistance = 300000 --max squared distance at which text it drawn for features
local maxFeatureDistance = 550000 --max squared distance at which any info is drawn for features
local maxUnitDistance = 11000000 --max squared distance at which any info is drawn for units MUST BE LARGER THAN FOR FEATURES!
local minReloadTime = 4 --// in seconds
local destructableFeature = {}
local drawnFeature = {}
for i = 1, #FeatureDefs do
destructableFeature[i] = FeatureDefs[i].destructable
drawnFeature[i] = (FeatureDefs[i].drawTypeString=="model")
end
local drawStunnedOverlay = true
--// this table is used to shows the hp of perimeter defence, and filter it for default wreckages
local walls = {dragonsteeth=true,dragonsteeth_core=true,fortification=true,fortification_core=true,floatingteeth=true,floatingteeth_core=true}
local stockpileH = 24
local stockpileW = 12
local notificationTimeout = 0
local OPTIONS = {}
OPTIONS[1] = {
choppedCorners = false,
showOutline = false,
showInnerBg = false,
}
OPTIONS[2] = {
choppedCorners = true,
showOutline = true,
showInnerBg = true,
}
local currentOption = 2
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--// colors
local bkBottom = { 0.25,0.25,0.25,barAlpha }
local bkTop = { 0.10,0.10,0.10,barAlpha }
local bkOutlineBottom = { 0.25,0.25,0.25,barOutlineAlpha }
local bkOutlineTop = { 0.10,0.10,0.10,barOutlineAlpha }
local bkInnerBottom = { 0.06,0.06,0.06,barInnerAlpha }
local bkInnerTop = { 0.33,0.33,0.33,barInnerAlpha*0.5 }
local hpcolormap = { {1, 0.0, 0.0, barValueAlpha}, {0.8, 0.60, 0.0, barValueAlpha}, {0.0,0.75,0.0,barValueAlpha} }
local bfcolormap = {}
local fbkBottom = { 0.40,0.40,0.40,featureBarAlpha }
local fbkTop = { 0.06,0.06,0.06,featureBarAlpha }
local fhpcolormap = { {0.33, 0.33, 0.33, featureBarAlpha*1.5}, {0.33, 0.33, 0.33, featureBarAlpha*1.5}, {0.33,0.33,0.33,featureBarAlpha*1.5} }
local barColors = {
emp = { 0.50,0.50,1.00,barValueAlpha },
emp_p = { 0.40,0.40,0.80,barValueAlpha },
emp_b = { 0.60,0.60,0.90,barValueAlpha },
capture = { 1.00,0.50,0.00,barValueAlpha },
build = { 0.75,0.75,0.75,barValueAlpha },
stock = { 0.50,0.50,0.50,barValueAlpha },
reload = { 0.05,0.60,0.60,barValueAlpha },
shield = { 0.20,0.60,0.60,barValueAlpha },
resurrect = { 1.00,0.50,0.00,barValueAlpha },
reclaim = { 0.75,0.75,0.75,barValueAlpha },
}
local ignoreUnits = {}
for udefID,def in ipairs(UnitDefs) do
if def.customParams['nohealthbars'] then
ignoreUnits[udefID] = true
end
end
local ignoreFeatures = {}
for fdefID,def in pairs(FeatureDefs) do
if def.customParams['nohealthbars'] then
ignoreFeatures[fdefID] = true
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local blink = false;
local gameFrame = 0;
local empDecline = 32/30/40;
local cx, cy, cz = 0,0,0; --// camera pos
local smoothheight = 0
local paraUnits = {};
local UnitMorphs = {};
local barShader;
local barDList;
local barFeatureDList;
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--// speedup (there are a lot more localizations, but they are in limited scope cos we are running out of upvalues)
local glColor = gl.Color
local glMyText = gl.FogCoord
local floor = math.floor
local sub = string.sub
local GetUnitDefID = Spring.GetUnitDefID
local glDepthTest = gl.DepthTest
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- tiny perf improvement this way:
local unitsUnitDefCache = {}
function aGetUnitDefID(unitID)
if unitsUnitDefCache[unitID] == nil then
unitsUnitDefCache[unitID] = spGetUnitDefID(unitID)
end
return unitsUnitDefCache[unitID]
end
function widget:UnitDestroyed(unitID, unitDefID, unitTeam)
unitsUnitDefCache[unitID] = nil
end
do
local deactivated = false
function showhealthbars(cmd, line, words)
if ((words[1])and(words[1]~="0"))or(deactivated) then
widgetHandler:UpdateCallIn('DrawWorld')
deactivated = false
else
widgetHandler:RemoveCallIn('DrawWorld')
deactivated = true
end
end
end --//end do
-- if you change code inside this function, then dont forget to change drawFeatureBarGl() aswell
function drawBarGl()
local cs = choppedCornerSize
local heightAddition = 0
if OPTIONS[currentOption].showOutline then
heightAddition = outlineSize
end
gl.PushMatrix()
gl.Scale(barScale,barScale,barScale)
if (OPTIONS[currentOption].choppedCorners) then
gl.BeginEnd(GL.QUADS,function()
-- center background piece
gl.Color((OPTIONS[currentOption].showOutline and bkOutlineBottom or bkBottom));
gl.Vertex(barWidth-cs, 0-heightAddition, 0, 1);
gl.Vertex(barWidth-cs, 0-heightAddition, (barWidth*2)-cs*2, 1);
gl.Color((OPTIONS[currentOption].showOutline and bkOutlineTop or bkTop));
gl.Vertex(barWidth-cs, barHeight+heightAddition, (barWidth*2)-cs*2, 1);
gl.Vertex(barWidth-cs, barHeight+heightAddition, 0, 1);
if OPTIONS[currentOption].showOutline and OPTIONS[currentOption].showInnerBg then
cs = choppedCornerSize
-- inner center background piece
gl.Color(bkInnerBottom[1],bkInnerBottom[2],bkInnerBottom[3],bkInnerBottom[4]);
gl.Vertex(barWidth-cs-cs, 0, 0, 2);
gl.Vertex(barWidth-cs, 0, (barWidth*2)-cs*2, 2);
gl.Color(bkInnerTop[1],bkInnerTop[2],bkInnerTop[3],bkInnerTop[4]);
gl.Vertex(barWidth-cs, barHeight, (barWidth*2)-cs*2, 2);
gl.Vertex(barWidth-cs-cs, barHeight, 0, 2);
-- inner background right piece
local cs2 = cs
gl.Color(bkInnerBottom[1],bkInnerBottom[2],bkInnerBottom[3],bkInnerBottom[4]);
gl.Vertex(barWidth-cs-cs+cs2, cs2, 0, 2);
gl.Vertex(barWidth-cs-cs, 0, 0, 2);
gl.Color(bkInnerTop[1],bkInnerTop[2],bkInnerTop[3],bkInnerTop[4]);
gl.Vertex(barWidth-cs-cs, barHeight, 0, 2);
gl.Vertex(barWidth-cs-cs+cs2, barHeight-cs2, 0, 2);
end
-- background right piece
if OPTIONS[currentOption].choppedCorners or OPTIONS[currentOption].showOutline then
local cs2 = cs
if OPTIONS[currentOption].showOutline then
cs2 = outlineSize
end
gl.Color((OPTIONS[currentOption].showOutline and bkOutlineBottom or bkBottom));
gl.Vertex(barWidth-cs+cs2, cs2-heightAddition, 0, 1);
gl.Vertex(barWidth-cs, 0-heightAddition, 0, 1);
gl.Color((OPTIONS[currentOption].showOutline and bkOutlineTop or bkTop));
gl.Vertex(barWidth-cs, barHeight+heightAddition, 0, 1);
gl.Vertex(barWidth-cs+cs2, barHeight+heightAddition-cs2, 0, 1);
if heightAddition > 0 then
cs = outlineSize
-- background left piece
gl.Color((OPTIONS[currentOption].showOutline and bkOutlineBottom or bkBottom));
gl.Vertex(-barWidth-cs, cs-heightAddition, 0, 1);
gl.Vertex(-barWidth, -heightAddition, 0, 1);
gl.Color((OPTIONS[currentOption].showOutline and bkOutlineTop or bkTop));
gl.Vertex(-barWidth, barHeight+heightAddition, 0, 1);
gl.Vertex(-barWidth-cs, barHeight+heightAddition-cs, 0, 1);
cs = choppedCornerSize
-- top middle piece
local usedColor = bkOutlineTop[1]+((bkOutlineBottom[1]-bkOutlineTop[1]) * (heightAddition/(barHeight+heightAddition+heightAddition)))
gl.Color(usedColor,usedColor,usedColor,bkOutlineTop[4]);
gl.Vertex(-barWidth, barHeight, 0, 1);
gl.Vertex(barWidth-cs, barHeight, (barWidth*2)-cs*2, 1);
gl.Color(bkOutlineTop);
gl.Vertex(barWidth-cs, barHeight+heightAddition, (barWidth*2)-cs*2, 1);
gl.Vertex(-barWidth, barHeight+heightAddition, 0, 1);
-- bottom middle piece
usedColor = bkOutlineBottom[1]-((bkOutlineBottom[1]-bkOutlineTop[1]) * (heightAddition/(barHeight+heightAddition+heightAddition)))
gl.Color(usedColor,usedColor,usedColor,bkOutlineTop[4]);
gl.Vertex(-barWidth, 0, 0, 1);
gl.Vertex(barWidth-cs, 0, (barWidth*2)-cs*2, 1);
gl.Color(bkOutlineBottom);
gl.Vertex(barWidth-cs, -heightAddition, (barWidth*2)-cs*2, 1);
gl.Vertex(-barWidth, -heightAddition, 0, 1);
end
end
-- color (value part) mid piece
gl.Vertex(-barWidth+cs, 0, 0, 0);
gl.Vertex(-barWidth, 0, (barWidth*2)-cs*2, 0);
gl.Vertex(-barWidth, barHeight, (barWidth*2)-cs*2, 0);
gl.Vertex(-barWidth+cs, barHeight, 0, 0);
-- color (value part) left piece
gl.Vertex(-barWidth, cs, 0, 0);
gl.Vertex(-barWidth+cs, 0, 0, 0);
gl.Vertex(-barWidth+cs, barHeight, 0, 0);
gl.Vertex(-barWidth, barHeight-cs, 0, 0);
-- color (value part) right piece
gl.Vertex(-barWidth+cs,cs, (barWidth*2)-cs*2, 0);
gl.Vertex(-barWidth,0, (barWidth*2)-cs*2, 0);
gl.Vertex(-barWidth,barHeight, (barWidth*2)-cs*2, 0);
gl.Vertex(-barWidth+cs,barHeight-cs,(barWidth*2)-cs*2, 0);
end)
-- corner fillers
gl.BeginEnd(GL.TRIANGLES,function()
cs = choppedCornerSize
local inputBottomColor = bkBottom
local inputTopColor = bkTop
if OPTIONS[currentOption].showOutline then
inputBottomColor = bkOutlineBottom
inputTopColor = bkOutlineTop
end
-- top right
local usedColor = inputTopColor[1]+((inputBottomColor[1]-inputTopColor[1]) * ((heightAddition+cs)/(barHeight+heightAddition+heightAddition)))
gl.Color(usedColor,usedColor,usedColor,inputTopColor[4]);
gl.Vertex(barWidth-cs, barHeight-cs, (barWidth*2)-cs*2, 1);
usedColor = inputTopColor[1]+((inputBottomColor[1]-inputTopColor[1]) * (heightAddition/(barHeight+heightAddition+heightAddition)))
gl.Color(usedColor,usedColor,usedColor,inputTopColor[4]);
gl.Vertex(barWidth-cs, barHeight, (barWidth*2)-cs*2, 1);
gl.Vertex(barWidth-cs-cs, barHeight, (barWidth*2)-cs*2, 1);
-- bottom right
usedColor = inputBottomColor[1]-((inputBottomColor[1]-inputTopColor[1]) * ((heightAddition+cs)/(barHeight+heightAddition+heightAddition)))
gl.Color(usedColor,usedColor,usedColor,inputBottomColor[4]);
gl.Vertex(barWidth-cs, cs, (barWidth*2)-cs*2, 1);
usedColor = inputBottomColor[1]-((inputBottomColor[1]-inputTopColor[1]) * (heightAddition/(barHeight+heightAddition+heightAddition)))
gl.Color(usedColor,usedColor,usedColor,inputBottomColor[4]);
gl.Vertex(barWidth-cs, 0, (barWidth*2)-cs*2, 1);
gl.Vertex(barWidth-cs-cs, 0, (barWidth*2)-cs*2, 1);
-- inner background corners
if OPTIONS[currentOption].showOutline and OPTIONS[currentOption].showInnerBg then
-- top right
usedColor = bkInnerTop[1]-((bkInnerTop[1]-bkInnerBottom[1]) * ((cs)/(barHeight)))
gl.Color(usedColor,usedColor,usedColor,bkInnerTop[4]);
gl.Vertex(barWidth-cs, barHeight-cs, (barWidth*2)-cs*2, 1.0000001);
gl.Color(bkInnerTop[1],bkInnerTop[2],bkInnerTop[3],bkInnerTop[4]);
gl.Vertex(barWidth-cs, barHeight, (barWidth*2)-cs*2, 1.0000001);
gl.Vertex(barWidth-cs-cs, barHeight, (barWidth*2)-cs*2, 1.0000001);
-- bottom right
usedColor = bkInnerBottom[1]+((bkInnerTop[1]-bkInnerBottom[1]) * ((cs)/(barHeight)))
gl.Color(usedColor,usedColor,usedColor,bkInnerBottom[4]);
gl.Vertex(barWidth-cs, cs, (barWidth*2)-cs*2, 1.0000001);
gl.Color(bkInnerBottom[1],bkInnerBottom[2],bkInnerBottom[3],bkInnerBottom[4]);
gl.Vertex(barWidth-cs, 0, (barWidth*2)-cs*2, 1.0000001);
gl.Vertex(barWidth-cs-cs, 0, (barWidth*2)-cs*2, 1.0000001);
end
if heightAddition > 0 then
-- top left
usedColor = inputTopColor[1]+((inputBottomColor[1]-inputTopColor[1]) * ((heightAddition+cs)/(barHeight+heightAddition+heightAddition)))
gl.Color(usedColor,usedColor,usedColor,inputTopColor[4]);
gl.Vertex(-barWidth, barHeight-cs, 0, 1);
usedColor = inputTopColor[1]+((inputBottomColor[1]-inputTopColor[1]) * (heightAddition/(barHeight+heightAddition+heightAddition)))
gl.Color(usedColor,usedColor,usedColor,inputTopColor[4]);
gl.Vertex(-barWidth, barHeight, 0, 1);
gl.Vertex(-barWidth+cs, barHeight, 0, 1);
-- bottom left
usedColor = inputBottomColor[1]-((inputBottomColor[1]-inputTopColor[1]) * ((heightAddition+cs)/(barHeight+heightAddition+heightAddition)))
gl.Color(usedColor,usedColor,usedColor,inputBottomColor[4]);
gl.Vertex(-barWidth, cs, 0, 1);
usedColor = inputBottomColor[1]-((inputBottomColor[1]-inputTopColor[1]) * (heightAddition/(barHeight+heightAddition+heightAddition)))
gl.Color(usedColor,usedColor,usedColor,inputBottomColor[4]);
gl.Vertex(-barWidth, 0, 0, 1);
gl.Vertex(-barWidth+cs, 0, 0, 1);
end
end)
else
gl.BeginEnd(GL.QUADS,function()
gl.Vertex(-barWidth,0, 0,0);
gl.Vertex(-barWidth,0, barWidth*2,0);
gl.Vertex(-barWidth,barHeight,barWidth*2,0);
gl.Vertex(-barWidth,barHeight,0,0);
gl.Color(bkBottom);
gl.Vertex(barWidth,0, 0, 1);
gl.Vertex(barWidth,0, barWidth*2,1);
gl.Color(bkTop);
gl.Vertex(barWidth,barHeight,barWidth*2,1);
gl.Vertex(barWidth,barHeight,0, 1);
end)
end
gl.PopMatrix()
end
-- is a copy of drawBarGl(), some vars changed (only at top: barHeight, barWidth, bkBottom, bkTop)
function drawFeatureBarGl()
local barHeight = featureBarHeight
local barWidth = featureBarWidth
local bkBottom = fbkBottom
local bkTop = fbkTop
local cs = choppedCornerSize
local heightAddition = 0
if OPTIONS[currentOption].showOutline then
heightAddition = outlineSize
end
if (OPTIONS[currentOption].choppedCorners) then
gl.BeginEnd(GL.QUADS,function()
-- center background piece
gl.Color((OPTIONS[currentOption].showOutline and bkOutlineBottom or bkBottom));
gl.Vertex(barWidth-cs, 0-heightAddition, 0, 1);
gl.Vertex(barWidth-cs, 0-heightAddition, (barWidth*2)-cs*2, 1);
gl.Color((OPTIONS[currentOption].showOutline and bkOutlineTop or bkTop));
gl.Vertex(barWidth-cs, barHeight+heightAddition, (barWidth*2)-cs*2, 1);
gl.Vertex(barWidth-cs, barHeight+heightAddition, 0, 1);
if OPTIONS[currentOption].showOutline and OPTIONS[currentOption].showInnerBg then
cs = choppedCornerSize
-- inner center background piece
gl.Color(bkInnerBottom[1],bkInnerBottom[2],bkInnerBottom[3],bkInnerBottom[4]);
gl.Vertex(barWidth-cs-cs, 0, 0, 2);
gl.Vertex(barWidth-cs, 0, (barWidth*2)-cs*2, 2);
gl.Color(bkInnerTop[1],bkInnerTop[2],bkInnerTop[3],bkInnerTop[4]);
gl.Vertex(barWidth-cs, barHeight, (barWidth*2)-cs*2, 2);
gl.Vertex(barWidth-cs-cs, barHeight, 0, 2);
-- inner background right piece
local cs2 = cs
gl.Color(bkInnerBottom[1],bkInnerBottom[2],bkInnerBottom[3],bkInnerBottom[4]);
gl.Vertex(barWidth-cs-cs+cs2, cs2, 0, 2);
gl.Vertex(barWidth-cs-cs, 0, 0, 2);
gl.Color(bkInnerTop[1],bkInnerTop[2],bkInnerTop[3],bkInnerTop[4]);
gl.Vertex(barWidth-cs-cs, barHeight, 0, 2);
gl.Vertex(barWidth-cs-cs+cs2, barHeight-cs2, 0, 2);
end
-- background right piece
if OPTIONS[currentOption].choppedCorners or OPTIONS[currentOption].showOutline then
local cs2 = cs
if OPTIONS[currentOption].showOutline then
cs2 = outlineSize
end
gl.Color((OPTIONS[currentOption].showOutline and bkOutlineBottom or bkBottom));
gl.Vertex(barWidth-cs+cs2, cs2-heightAddition, 0, 1);
gl.Vertex(barWidth-cs, 0-heightAddition, 0, 1);
gl.Color((OPTIONS[currentOption].showOutline and bkOutlineTop or bkTop));
gl.Vertex(barWidth-cs, barHeight+heightAddition, 0, 1);
gl.Vertex(barWidth-cs+cs2, barHeight+heightAddition-cs2, 0, 1);
if heightAddition > 0 then
cs = outlineSize
-- background left piece
gl.Color((OPTIONS[currentOption].showOutline and bkOutlineBottom or bkBottom));
gl.Vertex(-barWidth-cs, cs-heightAddition, 0, 1);
gl.Vertex(-barWidth, -heightAddition, 0, 1);
gl.Color((OPTIONS[currentOption].showOutline and bkOutlineTop or bkTop));
gl.Vertex(-barWidth, barHeight+heightAddition, 0, 1);
gl.Vertex(-barWidth-cs, barHeight+heightAddition-cs, 0, 1);
cs = choppedCornerSize
-- top middle piece
local usedColor = bkOutlineTop[1]+((bkOutlineBottom[1]-bkOutlineTop[1]) * (heightAddition/(barHeight+heightAddition+heightAddition)))
gl.Color(usedColor,usedColor,usedColor,bkOutlineTop[4]);
gl.Vertex(-barWidth, barHeight, 0, 1);
gl.Vertex(barWidth-cs, barHeight, (barWidth*2)-cs*2, 1);
gl.Color(bkOutlineTop);
gl.Vertex(barWidth-cs, barHeight+heightAddition, (barWidth*2)-cs*2, 1);
gl.Vertex(-barWidth, barHeight+heightAddition, 0, 1);
-- bottom middle piece
usedColor = bkOutlineBottom[1]-((bkOutlineBottom[1]-bkOutlineTop[1]) * (heightAddition/(barHeight+heightAddition+heightAddition)))
gl.Color(usedColor,usedColor,usedColor,bkOutlineTop[4]);
gl.Vertex(-barWidth, 0, 0, 1);
gl.Vertex(barWidth-cs, 0, (barWidth*2)-cs*2, 1);
gl.Color(bkOutlineBottom);
gl.Vertex(barWidth-cs, -heightAddition, (barWidth*2)-cs*2, 1);
gl.Vertex(-barWidth, -heightAddition, 0, 1);
end
end
-- color (value part) mid piece
gl.Vertex(-barWidth+cs, 0, 0, 0);
gl.Vertex(-barWidth, 0, (barWidth*2)-cs*2, 0);
gl.Vertex(-barWidth, barHeight, (barWidth*2)-cs*2, 0);
gl.Vertex(-barWidth+cs, barHeight, 0, 0);
-- color (value part) left piece
gl.Vertex(-barWidth, cs, 0, 0);
gl.Vertex(-barWidth+cs, 0, 0, 0);
gl.Vertex(-barWidth+cs, barHeight, 0, 0);
gl.Vertex(-barWidth, barHeight-cs, 0, 0);
-- color (value part) right piece
gl.Vertex(-barWidth+cs,cs, (barWidth*2)-cs*2, 0);
gl.Vertex(-barWidth,0, (barWidth*2)-cs*2, 0);
gl.Vertex(-barWidth,barHeight, (barWidth*2)-cs*2, 0);
gl.Vertex(-barWidth+cs,barHeight-cs,(barWidth*2)-cs*2, 0);
end)
-- corner fillers
gl.BeginEnd(GL.TRIANGLES,function()
cs = choppedCornerSize
local inputBottomColor = bkBottom
local inputTopColor = bkTop
if OPTIONS[currentOption].showOutline then
inputBottomColor = bkOutlineBottom
inputTopColor = bkOutlineTop
end
-- top right
local usedColor = inputTopColor[1]+((inputBottomColor[1]-inputTopColor[1]) * ((heightAddition+cs)/(barHeight+heightAddition+heightAddition)))
gl.Color(usedColor,usedColor,usedColor,inputTopColor[4]);
gl.Vertex(barWidth-cs, barHeight-cs, (barWidth*2)-cs*2, 1);
usedColor = inputTopColor[1]+((inputBottomColor[1]-inputTopColor[1]) * (heightAddition/(barHeight+heightAddition+heightAddition)))
gl.Color(usedColor,usedColor,usedColor,inputTopColor[4]);
gl.Vertex(barWidth-cs, barHeight, (barWidth*2)-cs*2, 1);
gl.Vertex(barWidth-cs-cs, barHeight, (barWidth*2)-cs*2, 1);
-- bottom right
usedColor = inputBottomColor[1]-((inputBottomColor[1]-inputTopColor[1]) * ((heightAddition+cs)/(barHeight+heightAddition+heightAddition)))
gl.Color(usedColor,usedColor,usedColor,inputBottomColor[4]);
gl.Vertex(barWidth-cs, cs, (barWidth*2)-cs*2, 1);
usedColor = inputBottomColor[1]-((inputBottomColor[1]-inputTopColor[1]) * (heightAddition/(barHeight+heightAddition+heightAddition)))
gl.Color(usedColor,usedColor,usedColor,inputBottomColor[4]);
gl.Vertex(barWidth-cs, 0, (barWidth*2)-cs*2, 1);
gl.Vertex(barWidth-cs-cs, 0, (barWidth*2)-cs*2, 1);
-- inner background corners
if OPTIONS[currentOption].showOutline and OPTIONS[currentOption].showInnerBg then
-- top right
usedColor = bkInnerTop[1]-((bkInnerTop[1]-bkInnerBottom[1]) * ((cs)/(barHeight)))
gl.Color(usedColor,usedColor,usedColor,bkInnerTop[4]);
gl.Vertex(barWidth-cs, barHeight-cs, (barWidth*2)-cs*2, 1.0000001);
gl.Color(bkInnerTop[1],bkInnerTop[2],bkInnerTop[3],bkInnerTop[4]);
gl.Vertex(barWidth-cs, barHeight, (barWidth*2)-cs*2, 1.0000001);
gl.Vertex(barWidth-cs-cs, barHeight, (barWidth*2)-cs*2, 1.0000001);
-- bottom right
usedColor = bkInnerBottom[1]+((bkInnerTop[1]-bkInnerBottom[1]) * ((cs)/(barHeight)))
gl.Color(usedColor,usedColor,usedColor,bkInnerBottom[4]);
gl.Vertex(barWidth-cs, cs, (barWidth*2)-cs*2, 1.0000001);
gl.Color(bkInnerBottom[1],bkInnerBottom[2],bkInnerBottom[3],bkInnerBottom[4]);
gl.Vertex(barWidth-cs, 0, (barWidth*2)-cs*2, 1.0000001);
gl.Vertex(barWidth-cs-cs, 0, (barWidth*2)-cs*2, 1.0000001);
end
if heightAddition > 0 then
-- top left
usedColor = inputTopColor[1]+((inputBottomColor[1]-inputTopColor[1]) * ((heightAddition+cs)/(barHeight+heightAddition+heightAddition)))
gl.Color(usedColor,usedColor,usedColor,inputTopColor[4]);
gl.Vertex(-barWidth, barHeight-cs, 0, 1);
usedColor = inputTopColor[1]+((inputBottomColor[1]-inputTopColor[1]) * (heightAddition/(barHeight+heightAddition+heightAddition)))
gl.Color(usedColor,usedColor,usedColor,inputTopColor[4]);
gl.Vertex(-barWidth, barHeight, 0, 1);
gl.Vertex(-barWidth+cs, barHeight, 0, 1);
-- bottom left
usedColor = inputBottomColor[1]-((inputBottomColor[1]-inputTopColor[1]) * ((heightAddition+cs)/(barHeight+heightAddition+heightAddition)))
gl.Color(usedColor,usedColor,usedColor,inputBottomColor[4]);
gl.Vertex(-barWidth, cs, 0, 1);
usedColor = inputBottomColor[1]-((inputBottomColor[1]-inputTopColor[1]) * (heightAddition/(barHeight+heightAddition+heightAddition)))
gl.Color(usedColor,usedColor,usedColor,inputBottomColor[4]);
gl.Vertex(-barWidth, 0, 0, 1);
gl.Vertex(-barWidth+cs, 0, 0, 1);
end
end)
else
gl.BeginEnd(GL.QUADS,function()
gl.Vertex(-barWidth,0, 0,0);
gl.Vertex(-barWidth,0, barWidth*2,0);
gl.Vertex(-barWidth,barHeight,barWidth*2,0);
gl.Vertex(-barWidth,barHeight,0,0);
gl.Color(bkBottom);
gl.Vertex(barWidth,0, 0, 1);
gl.Vertex(barWidth,0, barWidth*2,1);
gl.Color(bkTop);
gl.Vertex(barWidth,barHeight,barWidth*2,1);
gl.Vertex(barWidth,barHeight,0, 1);
end)
end
end
function init()
--// find real primary weapon and its reloadtime
for _,ud in pairs(UnitDefs) do
ud.reloadTime = 0;
ud.primaryWeapon = 0;
ud.shieldPower = 0;
for i=1,#ud.weapons do
local WeaponDefID = ud.weapons[i].weaponDef;
local WeaponDef = WeaponDefs[ WeaponDefID ];
if (WeaponDef.reload>ud.reloadTime) then
ud.reloadTime = WeaponDef.reload;
ud.primaryWeapon = i;
end
end
local shieldDefID = ud.shieldWeaponDef
ud.shieldPower = ((shieldDefID)and(WeaponDefs[shieldDefID].shieldPower))or(-1)
end
--// wow, using a buffered list can give 1-2 frames in extreme(!) situations :p
for hp=0,100 do
bfcolormap[hp] = {GetColor(hpcolormap,hp*0.01)}
end
--// create bar shader
if (gl.CreateShader) then
if (barShader) then
gl.DeleteShader(barShader)
end
barShader = gl.CreateShader({
vertex = [[
#version 120
#define barColor gl_MultiTexCoord1
#define progress gl_MultiTexCoord2.x
#define offset gl_MultiTexCoord2.y
void main()
{
// switch between font rendering and bar rendering
if (gl_FogCoord>0.5f) {
gl_TexCoord[0]= gl_TextureMatrix[0]*gl_MultiTexCoord0;
gl_FrontColor = gl_Color;
gl_Position = ftransform();
//return; -- commenting out this line fixes glitchy healthbars on intel gfx
}
vec4 vertex = gl_Vertex;
if (vertex.w==1) {
gl_FrontColor = gl_Color;
if (vertex.z>0.0) {
vertex.x -= (1.0-progress)*gl_Vertex.z;
vertex.z = 0.0;
}
}else if (vertex.w==-2 ) {
gl_FrontColor = vec4(barColor.rgb,barColor.a);
if (gl_Vertex.z>1.0) {
vertex.x += progress*gl_Vertex.z;
vertex.z = 0.0;
}
vertex.w = 1;
}else if (vertex.w==-3 ) {
gl_FrontColor = vec4(barColor.rgb,barColor.a);
if (vertex.z>1.0) {
vertex.x += progress*gl_Vertex.z;
vertex.z = 0.0;
}
vertex.w = 1;
}else if (vertex.w==-4 ) {
gl_FrontColor = vec4(barColor.rgb,0);
if (gl_Vertex.z>1.0) {
vertex.x += progress*gl_Vertex.z;
vertex.z = 0.0;
}
vertex.w = 1;
}else if (vertex.w>1 ) {
if (progress >= 0.92) { // smooth out because else the bar wil overlap and look ugly at the end point
gl_FrontColor = vec4(gl_Color[0]+(barColor.r/4),gl_Color[1]+(barColor.g/4),gl_Color[2]+(barColor.b/4),((0.08-(progress-0.92))*12.5)*gl_Color[3]);
}else{
gl_FrontColor = vec4(gl_Color[0]+(barColor.r/4),gl_Color[1]+(barColor.g/4),gl_Color[2]+(barColor.b/4),gl_Color[3]);
}
if (vertex.z>0.0) {
vertex.x -= (1.0-progress)*gl_Vertex.z;
vertex.z = 0.0;
}
vertex.w = 1.0;
}else{
if (vertex.y>0.0) {
gl_FrontColor = vec4(barColor.rgb*1.8,barColor.a);
}else{
gl_FrontColor = vec4(barColor.rgb*0.85,barColor.a);
}
if (vertex.z>1.0) {
vertex.x += progress*gl_Vertex.z;
vertex.z = 0.0;
}
vertex.w = 1.0;
}
vertex.y += offset;
gl_Position = gl_ModelViewProjectionMatrix*vertex;
}
]],
uniform = {
},
});
if (barShader) then
if (barDList) then
gl.DeleteList(barDList)
gl.DeleteList(barFeatureDList)
end
barDList = gl.CreateList(drawBarGl)
barFeatureDList = gl.CreateList(drawFeatureBarGl)
end
end
end
local function toggleOption()
currentOption = currentOption + 1
if not OPTIONS[currentOption] then
currentOption = 1
end
init()
end
function widget:Initialize()
--// catch f9
Spring.SendCommands({"showhealthbars 0"})
Spring.SendCommands({"showrezbars 0"})
widgetHandler:AddAction("showhealthbars", showhealthbars)
Spring.SendCommands({"unbind f9 showhealthbars"})
Spring.SendCommands({"bind f9 luaui showhealthbars"})
--// link morph callins
widgetHandler:RegisterGlobal('MorphUpdate', MorphUpdate)
widgetHandler:RegisterGlobal('MorphFinished', MorphFinished)
widgetHandler:RegisterGlobal('MorphStart', MorphStart)
widgetHandler:RegisterGlobal('MorphStop', MorphStop)
--// deactivate cheesy progress text
widgetHandler:RegisterGlobal('MorphDrawProgress', function() return true end)
WG['healthbars'] = {}
WG['healthbars'].getScale = function()
return barScale
end
WG['healthbars'].setScale = function(value)
barScale = value
init()
end
init()
end
function widget:Shutdown()
--// catch f9
widgetHandler:RemoveAction("showhealthbars", showhealthbars)
Spring.SendCommands({"unbind f9 luaui"})
Spring.SendCommands({"bind f9 showhealthbars"})
--Spring.SendCommands({"showhealthbars 1"}) -- don't re-enable, nobody ever uses engines built in healthbars
--Spring.SendCommands({"showrezbars 1"})
widgetHandler:DeregisterGlobal('MorphUpdate', MorphUpdate)
widgetHandler:DeregisterGlobal('MorphFinished', MorphFinished)
widgetHandler:DeregisterGlobal('MorphStart', MorphStart)
widgetHandler:DeregisterGlobal('MorphStop', MorphStop)
widgetHandler:DeregisterGlobal('MorphDrawProgress')
if (barShader) then
gl.DeleteShader(barShader)
end
if (barDList) then
gl.DeleteList(barDList)
gl.DeleteList(barFeatureDList)
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function GetColor(colormap,slider)
local coln = #colormap
if (slider>=1) then
local col = colormap[coln]
return col[1],col[2],col[3],col[4]
end
if (slider<0) then slider=0 elseif(slider>1) then slider=1 end
local posn = 1+(coln-1) * slider
local iposn = floor(posn)
local aa = posn - iposn
local ia = 1-aa
local col1,col2 = colormap[iposn],colormap[iposn+1]
return col1[1]*ia + col2[1]*aa, col1[2]*ia + col2[2]*aa,
col1[3]*ia + col2[3]*aa, col1[4]*ia + col2[4]*aa
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local DrawUnitBar
local DrawFeatureBar
local DrawStockpile
do
--//speedup
local GL_QUADS = GL.QUADS
local glVertex = gl.Vertex
local glBeginEnd = gl.BeginEnd
local glMultiTexCoord = gl.MultiTexCoord
local glTexRect = gl.TexRect
local glTexture = gl.Texture
local glCallList = gl.CallList
local glText = gl.Text
local function DrawGradient(left,top,right,bottom,topclr,bottomclr)
glColor(bottomclr)
glVertex(left,bottom)
glVertex(right,bottom)
glColor(topclr)
glVertex(right,top)
glVertex(left,top)
end
local brightClr = {}
function DrawUnitBar(offsetY,percent,color)
if (barShader) then
glMultiTexCoord(1,color)
glMultiTexCoord(2,percent,offsetY)
glCallList(barDList,progress)
return;
end
brightClr[1] = color[1]*1.5; brightClr[2] = color[2]*1.5; brightClr[3] = color[3]*1.5; brightClr[4] = color[4]
local progress_pos= -barWidth+barWidth*2*percent-1
local bar_Height = barHeight+offsetY
if percent<1 then glBeginEnd(GL_QUADS,DrawGradient,progress_pos, bar_Height, barWidth, offsetY, bkTop,bkBottom) end
glBeginEnd(GL_QUADS,DrawGradient,-barWidth, bar_Height, progress_pos, offsetY,brightClr,color)
end
function DrawFeatureBar(offsetY,percent,color)
if (barShader) then
glMultiTexCoord(1,color)
glMultiTexCoord(2,percent,offsetY)
glCallList(barFeatureDList)
return;
end
brightClr[1] = color[1]*1.5; brightClr[2] = color[2]*1.5; brightClr[3] = color[3]*1.5; brightClr[4] = color[4]
local progress_pos = -featureBarWidth+featureBarWidth*2*percent
glBeginEnd(GL_QUADS,DrawGradient,progress_pos, featureBarHeight+offsetY, featureBarWidth, offsetY, fbkTop,fbkBottom)
glBeginEnd(GL_QUADS,DrawGradient,-featureBarWidth, featureBarHeight+offsetY, progress_pos, offsetY, brightClr,color)
end
function DrawStockpile(numStockpiled,numStockpileQued)
--// DRAW STOCKPILED MISSLES
glColor(1,1,1,1)
glTexture("LuaUI/Images/nuke.png")
local xoffset = barWidth+16
for i=1,((numStockpiled>3) and 3) or numStockpiled do
glTexRect(xoffset,-(11*barHeight-2)-stockpileH,xoffset-stockpileW,-(11*barHeight-2))
xoffset = xoffset-8
end
glTexture(false)
glText(numStockpiled..'/'..numStockpileQued,barWidth+1.7,-(11*barHeight-2)-16,6.5,"cno")
end
end --//end do
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local AddBar
local DrawBars
local barsN = 0
do
--//speedup
local glColor = gl.Color
local glText = gl.Text
local maxBars = 20
local bars = {}
local barHeightL = barHeight + 1.5 + (OPTIONS[currentOption].showOutline and outlineSize or 0)
local barStart = -(barWidth + 1.5) - (OPTIONS[currentOption].showOutline and outlineSize or 0)
local fBarHeightL = featureBarHeight + 1.5 + (OPTIONS[currentOption].showOutline and outlineSize or 0)
local fBarStart = -(featureBarWidth + 1.5) - (OPTIONS[currentOption].showOutline and outlineSize or 0)
for i=1,maxBars do bars[i] = {} end
function AddBar(title,progress,color_index,text,color)
barsN = barsN + 1
local barInfo = bars[barsN]
barInfo.title = title
barInfo.progress = progress
barInfo.color = color or barColors[color_index]
barInfo.text = text
end
function DrawBars(fullText)
local yoffset = 0
for i=1,barsN do
local barInfo = bars[i]
DrawUnitBar(yoffset,barInfo.progress,barInfo.color)
if (fullText) then
gl.PushMatrix()
gl.Scale(barScale,barScale,barScale)
if (barShader) then
glMyText(1)
end
glColor(1,1,1,barAlpha)
glText(barInfo.text,barStart,yoffset-outlineSize,4,"ro")
if (drawBarTitles and barInfo.title ~= "health") then
glColor(1,1,1,titlesAlpha)
glText(barInfo.title,0,yoffset-outlineSize,4.70,"cdo")
end
if (barShader) then glMyText(0) end
gl.PopMatrix()
end
yoffset = yoffset - barHeightL
end
barsN = 0 --//reset!
end
function DrawBarsFeature(fullText)
local yoffset = 0
for i=1,barsN do
local barInfo = bars[i]
DrawFeatureBar(yoffset,barInfo.progress,barInfo.color)
if (fullText) then
if (barShader) then glMyText(1) end
if (drawBarPercentage > 0) then
glColor(1,1,1,featureBarAlpha)
glText(barInfo.text,fBarStart,yoffset-outlineSize,4,"r")
end
if (drawBarTitles and barInfo.title ~= "health") then
glColor(1,1,1,featureTitlesAlpha)
glText(barInfo.title,0,yoffset-outlineSize,4.70,"cdo")
end
if (barShader) then glMyText(0) end
end
yoffset = yoffset - fBarHeightL
end
barsN = 0 --//reset!
end
end --//end do
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local DrawUnitInfos
do
--//speedup
local glTranslate = gl.Translate
local glPushMatrix = gl.PushMatrix
local glPopMatrix = gl.PopMatrix
local glBillboard = gl.Billboard
local GetUnitIsStunned = Spring.GetUnitIsStunned
local GetUnitHealth = Spring.GetUnitHealth
local GetUnitWeaponState = Spring.GetUnitWeaponState
local GetUnitShieldState = Spring.GetUnitShieldState
local GetUnitViewPosition = Spring.GetUnitViewPosition
local GetUnitStockpile = Spring.GetUnitStockpile
local GetUnitRulesParam = Spring.GetUnitRulesParam
local fullText
local ux, uy, uz
local dx, dy, dz, dist
local health,maxHealth,paralyzeDamage,capture,build