Skip to content

Commit 7fe0414

Browse files
committed
Disarm effect.
1 parent b9c620d commit 7fe0414

4 files changed

Lines changed: 109 additions & 80 deletions

File tree

LuaRules/Configs/disarm_defs.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
local FRAMES_PER_SECOND = Game.gameSpeed
3+
local disarmWeapons = {}
4+
local paraWeapons = {}
5+
local overstunDamageMult = {}
6+
local wantedWeaponList = {}
7+
8+
for wid = 1, #WeaponDefs do
9+
local wd = WeaponDefs[wid]
10+
local wcp = wd.customParams or {}
11+
if wcp.disarmdamagemult then
12+
disarmWeapons[wid] = {
13+
damageMult = wcp.disarmdamagemult,
14+
normalDamage = 1 - (wcp.disarmdamageonly or 0),
15+
disarmTimer = wcp.disarmtimer*FRAMES_PER_SECOND,
16+
overstunTime = wcp.overstun_time*FRAMES_PER_SECOND,
17+
}
18+
wantedWeaponList[#wantedWeaponList + 1] = wid
19+
elseif wd.paralyzer or wd.customParams.extra_damage then
20+
local paraTime = wd.paralyzer and wd.customParams.emp_paratime or wd.customParams.extra_paratime
21+
paraWeapons[wid] = {
22+
empTime = paraTime * FRAMES_PER_SECOND,
23+
overstunTime = wcp.overstun_time*FRAMES_PER_SECOND,
24+
}
25+
wantedWeaponList[#wantedWeaponList + 1] = wid
26+
end
27+
if wd.customParams and wd.customParams.overstun_damage_mult then
28+
overstunDamageMult[wid] = tonumber(wd.customParams.overstun_damage_mult)
29+
end
30+
end
31+
return disarmWeapons, paraWeapons, overstunDamageMult, wantedWeaponList

LuaRules/Gadgets/api_widget_events.lua

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ local scriptUnitDestroyed = Script.LuaUI.UnitDestroyed
2222
local scriptUnitDestroyedByTeam = Script.LuaUI.UnitDestroyedByTeam
2323
local scriptUnitLeftRadar = Script.LuaUI.UnitLeftRadar
2424

25+
local disarmWeapons = VFS.Include("LuaRules/Configs/disarm_defs.lua")
26+
2527
local _, fullview = Spring.GetSpectatingState()
2628
local myAllyTeamID = spGetMyAllyTeamID()
2729

@@ -73,17 +75,17 @@ function gadget:PlayerChanged(playerID)
7375
_, fullview = Spring.GetSpectatingState()
7476
end
7577

76-
function gadget:UnitDamaged(unitID, unitDefID, unitTeam, damage, paralyzer)
78+
function gadget:UnitDamaged(unitID, unitDefID, unitTeam, damage, paralyzer, weaponDefID, attackerID, attackerDefID, attackerTeam)
7779
--Spring.Echo("gadget:UnitDamaged",unitID, unitDefID, unitTeam, damage, paralyzer)
78-
if paralyzer then
80+
if paralyzer or disarmWeapons[weaponDefID] then
7981
if not fullview and not Spring.IsUnitInLos(unitID, myAllyTeamID) then
8082
return
8183
end
82-
if damage > 0 then
83-
if Script.LuaUI("UnitParalyzeDamageEffect") then
84-
--Spring.Echo("UnitParalyzeDamageHealthbars", unitID, step)
85-
Script.LuaUI.UnitParalyzeDamageEffect(unitID, unitDefID, damage)
86-
end
84+
if paralyzer and damage > 0 and Script.LuaUI("UnitParalyzeDamageEffect") then
85+
--Spring.Echo("UnitParalyzeDamageHealthbars", unitID, step)
86+
Script.LuaUI.UnitParalyzeDamageEffect(unitID, unitDefID, damage)
87+
elseif disarmWeapons[weaponDefID] and Script.LuaUI("UnitDisarmDamageEffect") then
88+
Script.LuaUI.UnitDisarmDamageEffect(unitID, unitDefID)
8789
end
8890
end
8991
end

LuaRules/Gadgets/unit_boolean_disable.lua

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,7 @@ local DECAY_FRAMES = 40 * FRAMES_PER_SECOND -- time in frames it takes to decay
2727

2828
local LOS_ACCESS = {inlos = true}
2929

30-
local wantedWeaponList = {}
31-
32-
local disarmWeapons = {}
33-
local paraWeapons = {}
34-
local overstunDamageMult = {}
35-
36-
for wid = 1, #WeaponDefs do
37-
local wd = WeaponDefs[wid]
38-
local wcp = wd.customParams or {}
39-
if wcp.disarmdamagemult then
40-
disarmWeapons[wid] = {
41-
damageMult = wcp.disarmdamagemult,
42-
normalDamage = 1 - (wcp.disarmdamageonly or 0),
43-
disarmTimer = wcp.disarmtimer*FRAMES_PER_SECOND,
44-
overstunTime = wcp.overstun_time*FRAMES_PER_SECOND,
45-
}
46-
wantedWeaponList[#wantedWeaponList + 1] = wid
47-
elseif wd.paralyzer or wd.customParams.extra_damage then
48-
local paraTime = wd.paralyzer and wd.customParams.emp_paratime or wd.customParams.extra_paratime
49-
paraWeapons[wid] = {
50-
empTime = paraTime * FRAMES_PER_SECOND,
51-
overstunTime = wcp.overstun_time*FRAMES_PER_SECOND,
52-
}
53-
wantedWeaponList[#wantedWeaponList + 1] = wid
54-
end
55-
if wd.customParams and wd.customParams.overstun_damage_mult then
56-
overstunDamageMult[wid] = tonumber(wd.customParams.overstun_damage_mult)
57-
end
58-
end
30+
local disarmWeapons, paraWeapons, overstunDamageMult, wantedWeaponList = VFS.Include("LuaRules/Configs/disarm_defs.lua")
5931

6032
local partialUnits = {}
6133
local paraUnits = {}

LuaUI/Widgets/gfx_paralyze_effect.lua

Lines changed: 68 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
local widget = widget ---@type Widget
22

33
function 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+
}
1414
end
1515

1616

1717
-- Localized Spring API for performance
1818
local spGetUnitDefID = Spring.GetUnitDefID
1919
local spGetUnitHealth = Spring.GetUnitHealth
2020
local spGetGameFrame = Spring.GetGameFrame
21-
local spEcho = Spring.Echo
22-
local spGetAllUnits = Spring.GetAllUnits
21+
local spGetUnitRulesParam = Spring.GetUnitRulesParam
2322

2423
local luaShaderDir = "LuaUI/Widgets/Include/"
2524
local 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
321326
void 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
428449
end
@@ -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
462483
end
463484

@@ -479,13 +500,10 @@ local spec, fullview
479500

480501
local 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
490508
end
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
500518
end
@@ -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
511530
end
@@ -523,8 +542,7 @@ function widget:UnitEnteredLos(unitID)
523542
widget:UnitCreated(unitID, spGetUnitDefID(unitID))
524543
end
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)
529547
end
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)
570593
end
571594

572595
function widget:Shutdown()
573596
WG['DrawParalyzedUnitGL4'] = nil
574597
WG['StopDrawParalyzedUnitGL4'] = nil
575598
widgetHandler:DeregisterGlobal("UnitParalyzeDamageEffect" )
599+
widgetHandler:DeregisterGlobal("UnitDisarmDamageEffect" )
576600
end
577601

578602
function 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

Comments
 (0)