11local widget = widget --- @type Widget
22
33function widget :GetInfo ()
4- return {
5- name = " Paralyze Effect" ,
6- version = " v0.2" ,
7- desc = " Faster gl.UnitShape, Use WG.UnitShapeGL4" ,
8- author = " Beherith" ,
9- date = " 2021.11.04" ,
10- license = " GPL V2" ,
11- layer = 0 ,
12- enabled = true ,
13- }
4+ return {
5+ name = " Paralyze Effect" ,
6+ version = " v0.2" ,
7+ desc = " Faster gl.UnitShape, Use WG.UnitShapeGL4" ,
8+ author = " Beherith" ,
9+ date = " 2021.11.04" ,
10+ license = " GPL V2" ,
11+ layer = 0 ,
12+ enabled = true ,
13+ }
1414end
1515
1616
1717-- Localized Spring API for performance
1818local spGetUnitDefID = Spring .GetUnitDefID
1919local spGetUnitHealth = Spring .GetUnitHealth
2020local spGetGameFrame = Spring .GetGameFrame
21- local spEcho = Spring .Echo
22- local spGetAllUnits = Spring .GetAllUnits
21+ local spGetUnitRulesParam = Spring .GetUnitRulesParam
2322
2423local luaShaderDir = " LuaUI/Widgets/Include/"
2524local LuaShader = VFS .Include (luaShaderDir .. " LuaShader.lua" )
@@ -196,7 +195,13 @@ void main() {
196195
197196 float paralyzestrength = uni[instData.y].userDefined[1].x; // this (paralyzedamage/maxhealth), so >=1.0 is paralyzed
198197 v_endcolor_alpha.a = clamp(pow(paralyzestrength, 2.0), 0.0, 1.1);
199- if ((uni[instData.y].composite & 0x00000003u) < 1u ) v_endcolor_alpha.a = 0.0; // this checks the drawFlag of wether the unit is actually being drawn (this is ==1 when then unit is both visible and drawn as a full model (not icon))
198+ v_endcolor_alpha.r = uni[instData.y].userDefined[1].y;
199+
200+ // this checks the drawFlag of wether the unit is actually being drawn (this is ==1 when then unit is both visible and drawn as a full model (not icon))
201+ if ((uni[instData.y].composite & 0x00000003u) < 1u ) {
202+ v_endcolor_alpha.a = 0.0;
203+ v_endcolor_alpha.r = 0.0;
204+ }
200205
201206 v_startcolorpower = startcolorpower;
202207
@@ -320,6 +325,7 @@ out vec4 fragColor;
320325#line 25000
321326void main() {
322327 float paralysis_level = v_endcolor_alpha.a; // values of 1 are fully paralyzed
328+ float disarmed = v_endcolor_alpha.r; // can be 1 or 0
323329
324330 float noisescale;
325331 float persistance;
@@ -331,23 +337,38 @@ void main() {
331337 float lighting_sharpness;
332338 float lighting_width;
333339 float lightning_speed;
340+ float effect_level;
334341
335342 // ------------------ CONFIG START --------------------
336343
337- if (paralysis_level < 0.98) { // not fully paralyzed
344+ if (paralysis_level > 0.98) { // not fully paralyzed
345+ effect_level = paralysis_level;
346+ noisescale = 0.31;
347+ persistance = 0.45;
348+ lacunarity = 2.5;
349+ minlightningcolor = vec3(0.1, 0.1, 1.0); //blue
350+ maxlightningcolor = vec3(1.0, 1.0, 1.0); //white
351+ wholeunitbasecolor = vec4(0.49, 0.5, 1.0, 1.0); // light blue base tone
352+ lightningalpha = 1.2;
353+ lighting_sharpness = 4.8;
354+ lighting_width = 3.8;
355+ lightning_speed = 0.95;
356+ } else if (disarmed > 0.5) {
357+ effect_level = 0.996;
358+ noisescale = 0.31;
359+ persistance = 0.45;
360+ lacunarity = 2.5;
361+ minlightningcolor = vec3(0.5, 0.5, 1.0); //blue
362+ maxlightningcolor = vec3(1.0, 1.0, 1.0); //white
363+ wholeunitbasecolor = vec4(0.9, 0.9, 0.7, 1.0); // light blue base tone
364+ lightningalpha = 1.2;
365+ lighting_sharpness = 4.8;
366+ lighting_width = 3.8;
367+ lightning_speed = 0.95;
368+ } else {
338369 fragColor = vec4(0);
339370 return;
340371 }
341- noisescale = 0.31;
342- persistance = 0.45;
343- lacunarity = 2.5;
344- minlightningcolor = vec3(0.1, 0.1, 1.0); //blue
345- maxlightningcolor = vec3(1.0, 1.0, 1.0); //white
346- wholeunitbasecolor = vec4(0.49, 0.5, 1.0, 1.0); // light blue base tone
347- lightningalpha = 1.2;
348- lighting_sharpness = 4.8;
349- lighting_width = 3.8;
350- lightning_speed = 0.95;
351372 // ------------------ CONFIG END --------------------
352373
353374 vec4 noiseposition = noisescale * vec4(v_modelPosOrig, (timeInfo.x + timeInfo.w) * lightning_speed);
@@ -363,7 +384,7 @@ void main() {
363384 vec3 lightningcolor;
364385 float effectalpha;
365386 lightningcolor = mix(minlightningcolor, maxlightningcolor, electricity);
366- effectalpha = clamp(paralysis_level * lightningalpha, 0.0, 1.0);
387+ effectalpha = clamp(effect_level * lightningalpha, 0.0, 1.0);
367388 float flash = abs((2.0 * fract((timeInfo.x + timeInfo.w) * 0.07)) - 1.0);
368389
369390 fragColor = vec4(lightningcolor, electricity*effectalpha);
@@ -374,7 +395,7 @@ void main() {
374395 wholeunitbasecolor.r = wholeunitbasecolor.r + baseItensity * 0.33;
375396 wholeunitbasecolor.g = wholeunitbasecolor.g + baseItensity * 0.45;
376397 fragColor = max(wholeunitbasecolor, fragColor); // apply whole unit base color
377- fragColor.a *= clamp((paralysis_level - 0.98) * 50.0, 0.0, 1.0);
398+ fragColor.a *= clamp((effect_level - 0.98) * 50.0, 0.0, 1.0);
378399}
379400]]
380401
@@ -422,7 +443,7 @@ local function initGL4()
422443 paralyzedUnitShader = LuaShader .CheckShaderUpdates (paralyzeSourceShaderCache )
423444
424445 if not paralyzedUnitShader then
425- spEcho (" paralyzedUnitShaderCompiled shader compilation failed" , paralyzedUnitShader )
446+ Spring . Echo (" paralyzedUnitShaderCompiled shader compilation failed" , paralyzedUnitShader )
426447 widgetHandler :RemoveWidget ()
427448 end
428449end
@@ -434,7 +455,7 @@ local function DrawParalyzedUnitGL4(unitID, unitDefID, red_start, green_start,
434455 -- returns: a unique handler ID number that you should store and call StopDrawParalyzedUnitGL4(uniqueID) with to stop drawing it
435456 -- note that widgets are responsible for stopping the drawing of every unit that they submit!
436457
437- -- spEcho ("DrawParalyzedUnitGL4",unitID, unitDefID, UnitDefs[unitDefID].name)
458+ -- Spring.Echo ("DrawParalyzedUnitGL4",unitID, unitDefID, UnitDefs[unitDefID].name)
438459 if paralyzedDrawUnitVBOTable .instanceIDtoIndex [unitID ] then return end -- already got this unit
439460 if Spring .ValidUnitID (unitID ) ~= true or Spring .GetUnitIsDead (unitID ) == true then return end
440461 red_start = red_start or 1.0
@@ -446,7 +467,7 @@ local function DrawParalyzedUnitGL4(unitID, unitDefID, red_start, green_start,
446467 blue_end = blue_end or 1.0
447468 time_end = 500000 -- time_end or spGetGameFrame()
448469 unitDefID = unitDefID or spGetUnitDefID (unitID )
449-
470+
450471 pushElementInstance (paralyzedDrawUnitVBOTable , {
451472 red_start , green_start ,blue_start , power_start ,
452473 red_end , green_end , blue_end , time_end ,
@@ -457,7 +478,7 @@ local function DrawParalyzedUnitGL4(unitID, unitDefID, red_start, green_start,
457478 nil ,
458479 unitID ,
459480 " unitID" )
460- -- spEcho ("Pushed", unitID, elementID)
481+ -- Spring.Echo ("Pushed", unitID, elementID)
461482 return unitID
462483end
463484
@@ -479,13 +500,10 @@ local spec, fullview
479500
480501local function init ()
481502 InstanceVBOTable .clearInstanceTable (paralyzedDrawUnitVBOTable )
482- local allUnits = spGetAllUnits ()
503+ local allUnits = Spring . GetAllUnits ()
483504 for i = 1 , # allUnits do
484505 local unitID = allUnits [i ]
485- local health ,maxHealth ,paralyzeDamage ,capture ,build = spGetUnitHealth (unitID )
486- if paralyzeDamage and paralyzeDamage > 0 then
487- widget :UnitCreated (unitID , spGetUnitDefID (unitID ))
488- end
506+ widget :UnitCreated (unitID , spGetUnitDefID (unitID ))
489507 end
490508end
491509
@@ -494,7 +512,7 @@ function widget:PlayerChanged(playerID)
494512 local prevMyTeamID = myTeamID
495513 myTeamID = Spring .GetMyTeamID ()
496514 if myTeamID ~= prevMyTeamID then -- TODO only really needed if onlyShowOwnTeam, or if allyteam changed?
497- -- spEcho ("Initializing Paralyze Effect")
515+ -- Spring.Echo ("Initializing Paralyze Effect")
498516 init ()
499517 end
500518end
@@ -505,7 +523,8 @@ function widget:UnitCreated(unitID, unitDefID)
505523 end
506524
507525 local health ,maxHealth ,paralyzeDamage ,capture ,build = spGetUnitHealth (unitID )
508- if paralyzeDamage and paralyzeDamage > 0 then
526+ local disarmed = spGetUnitRulesParam (unitID , " disarmed" )
527+ if (paralyzeDamage and paralyzeDamage > 0 ) or (disarmed == 1 ) then
509528 DrawParalyzedUnitGL4 (unitID , unitDefID )
510529 end
511530end
@@ -523,8 +542,7 @@ function widget:UnitEnteredLos(unitID)
523542 widget :UnitCreated (unitID , spGetUnitDefID (unitID ))
524543end
525544
526- local function UnitParalyzeDamageEffect (unitID , unitDefID , damage ) -- called from Healthbars Widget Forwarding GADGET!!!
527- -- spEcho("UnitParalyzeDamageEffect",unitID, unitDefID, damage, Spring.GetUnitIsStunned(unitID)) -- DO NOTE THAT: return: nil | bool stunned_or_inbuild, bool stunned, bool inbuild
545+ local function UnitParalyzeOrDisarmDamageEffect (unitID , unitDefID ) -- called from Healthbars Widget Forwarding GADGET!!!
528546 widget :UnitCreated (unitID , unitDefID )
529547end
530548
@@ -536,11 +554,15 @@ function widget:GameFrame(n)
536554 if n % 3 == 0 then
537555 for unitID , index in pairs (paralyzedDrawUnitVBOTable .instanceIDtoIndex ) do
538556 local health , maxHealth , paralyzeDamage , capture , build = spGetUnitHealth (unitID )
539- if paralyzeDamage == 0 or paralyzeDamage == nil then
557+ local disarmed = spGetUnitRulesParam (unitID , " disarmed" )
558+ if (not paralyzeDamage or paralyzeDamage == 0 ) and disarmed ~= 1 then
540559 toremove [unitID ] = true
541560 else
542- uniformcache [1 ] = (paralyzeDamage or 0 ) / (maxHealth or 1 ) -- 1 to avoid div0
561+ local para = (paralyzeDamage or 0 ) / (maxHealth or 1 )
562+ uniformcache [1 ] = para -- 1 to avoid div0
543563 gl .SetUnitBufferUniforms (unitID , uniformcache , 4 )
564+ uniformcache [1 ] = disarmed
565+ gl .SetUnitBufferUniforms (unitID , uniformcache , 5 )
544566 end
545567 end
546568 end
@@ -559,25 +581,27 @@ function widget:Initialize()
559581 initGL4 ()
560582 init ()
561583 if TESTMODE then
562- for i , unitID in ipairs (spGetAllUnits ()) do
584+ for i , unitID in ipairs (Spring . GetAllUnits ()) do
563585 widget :UnitCreated (unitID )
564586 gl .SetUnitBufferUniforms (unitID , {1.01 }, 4 )
565587 end
566588 end
567589 WG [' DrawParalyzedUnitGL4' ] = DrawParalyzedUnitGL4
568590 WG [' StopDrawParalyzedUnitGL4' ] = StopDrawParalyzedUnitGL4
569- widgetHandler :RegisterGlobal (" UnitParalyzeDamageEffect" ,UnitParalyzeDamageEffect )
591+ widgetHandler :RegisterGlobal (" UnitParalyzeDamageEffect" , UnitParalyzeOrDisarmDamageEffect )
592+ widgetHandler :RegisterGlobal (" UnitDisarmDamageEffect" , UnitParalyzeOrDisarmDamageEffect )
570593end
571594
572595function widget :Shutdown ()
573596 WG [' DrawParalyzedUnitGL4' ] = nil
574597 WG [' StopDrawParalyzedUnitGL4' ] = nil
575598 widgetHandler :DeregisterGlobal (" UnitParalyzeDamageEffect" )
599+ widgetHandler :DeregisterGlobal (" UnitDisarmDamageEffect" )
576600end
577601
578602function widget :DrawWorld ()
579603 if paralyzedDrawUnitVBOTable .usedElements > 0 then
580- -- if spGetGameFrame() % 90 == 0 then spEcho ("Drawing paralyzed units #", paralyzedDrawUnitVBOTable.usedElements) end
604+ -- if spGetGameFrame() % 90 == 0 then Spring.Echo ("Drawing paralyzed units #", paralyzedDrawUnitVBOTable.usedElements) end
581605 gl .Culling (GL .BACK )
582606 gl .DepthMask (false ) -- "BK OpenGL state resets", default is already false, could remove
583607 gl .DepthTest (true )
0 commit comments