RPG/Worms style DPS display widget v1.1

RPG/Worms style DPS display widget v1.1

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

User avatar
TheFatController
Balanced Annihilation Developer
Posts: 1177
Joined: 10 Dec 2006, 18:46

RPG/Worms style DPS display widget v1.1

Post by TheFatController »

Update: Version 1.1 - 07/08/07

New widget displays damage per second to your allies units or all units if you're speccing or watching a replay.

Paralyze damage is displayed in blue. 8)

To install it extract the zip into ..spring/luaUI/ and you're good to go!

Image

Changelog

Version 1.1
Small tweaks to text size etc.
Numbers now freeze when game paused.
No longer accumalates lots of numbers when you're alt tabbed into another window.
Optimized code a little.
Fixed a crash bug and a few other bugs.

Image

Credit to trepan for getting the fading outlined text working :)
Last edited by TheFatController on 07 Aug 2007, 23:41, edited 2 times in total.
User avatar
Neddie
Community Lead
Posts: 9406
Joined: 10 Apr 2006, 05:05

Post by Neddie »

If this keeps up, all the cool ideas will be widgets!
j5mello
Posts: 1189
Joined: 26 Aug 2005, 05:40

Post by j5mello »

OH crap u do know what this means right???

we are one step closer to being WoW... OH NOEZ TEH HORROR!!!!
User avatar
Nemo
Spring 1944 Developer
Posts: 1376
Joined: 30 Jan 2005, 19:44

Post by Nemo »

Would it be possible to change it so that all units display it (for balance testing)?
User avatar
Ishach
Posts: 1670
Joined: 02 May 2006, 06:44

Post by Ishach »

this is a cool idea
User avatar
Neddie
Community Lead
Posts: 9406
Joined: 10 Apr 2006, 05:05

Post by Neddie »

Ishach wrote:this is a cool idea
Isaac, can you play these days, or is your machine too feeble?
User avatar
DavetheBrave
Posts: 281
Joined: 22 Jun 2005, 02:52

Post by DavetheBrave »

this is awesome....should really help to see the effects of flanking too.
Warlord Zsinj
Imperial Winter Developer
Posts: 3742
Joined: 24 Aug 2004, 08:59

Post by Warlord Zsinj »

Hmm, if you could colour code this, or replace it with text, this could be very useful for you, Nemo, to do CoH style effects to show when a unit has been hit by an armour piercing round, etc.

It'd also be really nice if your guys were supressed, and this lua widget made the words "suppressed" rise above your units, and then if they continue to be fired upon, the words "pinned" appear above their heads (but maybe do not go away?)
User avatar
TheFatController
Balanced Annihilation Developer
Posts: 1177
Joined: 10 Dec 2006, 18:46

Post by TheFatController »

Nemo wrote:Would it be possible to change it so that all units display it (for balance testing)?
Unfortunatley you cant access enemies damage while playing (otherwise it would be easy to write a widget that showed enemies health bars). You have to use replays or lots of .cheat team switching in sandbox mode for useful stuff like that.
User avatar
Maelstrom
Posts: 1950
Joined: 23 Jul 2005, 14:52

Post by Maelstrom »

TheFatController wrote:
Nemo wrote:Would it be possible to change it so that all units display it (for balance testing)?
Unfortunatley you cant access enemies damage while playing (otherwise it would be easy to write a widget that showed enemies health bars). You have to use replays or lots of .cheat team switching in sandbox mode for useful stuff like that.
Or turn it into a LuaRule, that would work, but would require editing the mod instead of just adding in a LuaUI widget.
User avatar
1v0ry_k1ng
Posts: 4656
Joined: 10 Mar 2006, 10:24

Post by 1v0ry_k1ng »

this is cool, but would it be possible to show the damage done by each projectile instead? if only it was possible to show enemy damage too :(
would it be possible to make the font half the size? atm it looks a bit clumsy
User avatar
TheFatController
Balanced Annihilation Developer
Posts: 1177
Joined: 10 Dec 2006, 18:46

Post by TheFatController »

1v0ry_k1ng wrote:this is cool, but would it be possible to show the damage done by each projectile instead?
I tried that but if a flamethrower hits a group of units and it shows every hit there's horrible death 0fps lag.
1v0ry_k1ng wrote:would it be possible to make the font half the size? atm it looks a bit clumsy
I like it! :p I'll add some constants for customization tho in version 1.x
User avatar
1v0ry_k1ng
Posts: 4656
Joined: 10 Mar 2006, 10:24

Post by 1v0ry_k1ng »

you could change it from dps to damage every 0.1 sec then it woud be just like damage per projectile except really rapid fire units would be capped at 10 readings a second etc, or do it at 0.2..
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Post by zwzsg »

It gives me:
gl.DrawFuncAtUnit: error(2) = [string "LuaUI/headers/billboard.lua"]:19: gl.MultMatrix requires all 16 values
[string "LuaUI/headers/billboard.lua"]:19: gl.MultMatrix requires all 16 values
Removed widget: Display DPS
Why? Can you fix?
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Post by jK »

zwzsg wrote:It gives me:
gl.DrawFuncAtUnit: error(2) = [string "LuaUI/headers/billboard.lua"]:19: gl.MultMatrix requires all 16 values
[string "LuaUI/headers/billboard.lua"]:19: gl.MultMatrix requires all 16 values
Removed widget: Display DPS
Why? Can you fix?
it was a mistake in my healthbars widget in a previous version, please update it ;)
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Post by zwzsg »

Yes, indeed, installing your health bar widget fixes this one. Thanks.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Post by zwzsg »

However, I found that then when I use this widget in Kernel Panic, it shows lots of 0 when building. This is most probably due to the beam laser that are used to replace nano, and are actually real weapon with very low power.

From a general standpoint, I am torn about whether 0 damage should be shown or not.

But for KP I have to disable it or have "0" comes out of everything under construction. So far I disable the showing of 0 damage like that:

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- . file: . . gui_display_dps.lua
-- . brief: . Displays DPS done to your allies units
-- . author: . Owen Martindell
--
-- . Copyright (C) 2007.
-- . Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function widget:GetInfo()
. return {
. . name . . . = "Display DPS",
. . desc . . . = "Displays damage per second done to your allies units v1.1",
. . author . . = "TheFatController",
. . date . . . = "August 05, 2007",
. . license . = "GNU GPL, v2 or later",
. . layer . . = 0,
. . enabled . = true . -- . loaded by default
. }
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

-- Speed Up

local GetUnitDefID . . . . = Spring.GetUnitDefID
local GetUnitDefDimensions = Spring.GetUnitDefDimensions
local AreTeamsAllied . . . = Spring.AreTeamsAllied
local GetMyTeamID . . . . . = Spring.GetMyTeamID
local GetGameSpeed . . . . = Spring.GetGameSpeed

local glTranslate . . . = gl.Translate
local glColor . . . . . = gl.Color
local glBillboard . . . = gl.Billboard
local glText . . . . . = gl.Text
local glDepthMask . . . = gl.DepthMask
local glDepthTest . . . = gl.DepthTest
local glAlphaTest . . . = gl.AlphaTest
local glBlending . . . = gl.Blending
local glDrawFuncAtUnit = gl.DrawFuncAtUnit

local GL_GREATER . . . . . . = GL.GREATER
local GL_SRC_ALPHA . . . . . = GL.SRC_ALPHA
local GL_ONE . . . . . . . . = GL.ONE
local GL_ONE_MINUS_SRC_ALPHA = GL.ONE_MINUS_SRC_ALPHA

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local damageTable = {}
local unitParalyze = {}
local unitDamage = {}
local countDown = 0
local paused = false
local update = false

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function widget:Initialize()
. for udid, ud in ipairs(UnitDefs) do
. . ud.show_dps_height = (GetUnitDefDimensions(udid).height * 0.9)
. end
end

local function getTextSize(damage, paralyze)
. local sizeMod = 3
. if paralyze then sizeMod = 2.25 end
. return math.floor(8 * (1 + sizeMod * (1 - (200 / (200 + damage)))))
end

local function calcDPS(inTable, paralyze)
. for unitID,damage in pairs(inTable) do
. . local unitDefID . = GetUnitDefID(unitID)
. . local unitDef . . = UnitDefs[unitDefID]
. . if (unitDef ~= nil) then
. . . local textSize = getTextSize(damage, paralyze)
. . . if (damage >= 1) then
. . . . table.insert(damageTable, {
. . . . . unitID,
. . . . . math.floor(damage),
. . . . . unitDef.show_dps_height,
. . . . . (6 - math.random(0,12)),
. . . . . textSize,
. . . . . 0,
. . . . . 1,
. . . . . paralyze
. . . . })
. . . . inTable[unitID] = 0
. . . else
. . . . inTable[unitID] = nil
. . . end
. . end
. end
end

function widget:UnitDestroyed(unitID, unitDefID, unitTeam)
. unitDamage[unitID] = nil
. unitParalyze[unitID] = nil
. for i, damage in pairs(damageTable) do
. . if (damage[1] == unitID) then
. . . table.remove(damageTable, i)
. . end
. end
end

function widget:UnitTaken(unitID, unitDefID, oldTeam, newTeam)
. if (AreTeamsAllied(oldTeam, newTeam)) then
. . return
. else
. . widget:UnitDestroyed(unitID, unitDefID, newTeam)
. end
end

function widget:Update(DeltaTime)

if paused then return end

if (countDown > 1) then
. countDown = 0
. update = true
else
. countDown = countDown + DeltaTime
end

end

local function addDamage(inTable, damage, unitID, ud, paralyze)
. for uID,_ in pairs(inTable) do
. . if (uID == unitID) then
. . . inTable[uID] = inTable[uID] + damage
. . . return
. . end
. end
.
. if not update then
.
. . inTable[unitID] = 0
.
. . local textSize = getTextSize(damage, paralyze)
.
. . table.insert(damageTable, {
. . . unitID,
. . . math.floor(damage),
. . . ud.show_dps_height,
. . . (6 - math.random(0,12)),
. . . textSize,
. . . 0,
. . . 1,
. . . paralyze
. . })
.
. . table.sort(damageTable, function(m1,m2) return m1[2] < m2[2]; end)
. .
. end
end


function widget:UnitDamaged(unitID, unitDefID, unitTeam, damage, paralyzer)
. local ud = UnitDefs[unitDefID]
. if (ud == nil) then
. . return
. end
.
. if paralyzer then
. . addDamage(unitParalyze, damage, unitID, ud, true) .
. else
. . addDamage(unitDamage, damage, unitID, ud, false) .
. end
end


local function DrawUnitFunc(yshift, xshift, damage, textSize, alpha, paralyze)
. if(damage>=1) then
. . glTranslate(xshift, yshift, 0)
. . glBillboard()
. . gl.MultiTexCoord(1, 0.25 + (0.5 * alpha))
. . if paralyze then
. . . glColor(0, 0, 1)
. . . glText(damage, 0, 0, textSize, 'cnO')
. . else
. . . glColor(1, 1, 1)
. . . glText(damage, 0, 0, textSize, 'cno')
. . end
. end
end


function widget:DrawWorld()

. if update then
. . if next(unitDamage) then calcDPS(unitDamage, false) end
. . if next(unitParalyze) then calcDPS(unitParalyze, true) end
. . table.sort(damageTable, function(m1,m2) return m1[2] < m2[2]; end)
. . update = false
. end
.
. if (next(damageTable) == nil) then
. . return
. end
.
. _,_,paused = GetGameSpeed()
. .
. glDepthMask(true)
. glDepthTest(true)
. glAlphaTest(GL_GREATER, 0)
. glBlending(GL_SRC_ALPHA, GL_ONE)
. gl.Texture(1, LUAUI_DIRNAME .. "Images/gradient_alpha_2.png")

. for i, damage in pairs(damageTable) do
. . if (damage[7] <= 0) then
. . . table.remove(damageTable,i)
. . else
. . . glDrawFuncAtUnit(damage[1], false, DrawUnitFunc,
. . . . . . . . . . . . (damage[3] + damage[6]),
. . . . . . . . . . . . damage[4], damage[2], damage[5],
. . . . . . . . . . . . damage[7], damage[8])
. . . if not paused then
. . . . if damage[8] then
. . . . . damage[7] = damage[7] - 0.05
. . . . . damage[5] = damage[5] + 0.2
. . . . else
. . . . . local fadeTime = math.max(0.03 - (damage[2] / 333333), 0.015)
. . . . . local riseTime = 1 + math.min((damage[2] / 2500), 2)
. . . . . damage[6] = damage[6] + riseTime
. . . . . if (damage[6] > 25) then
. . . . . . damage[7] = damage[7] - fadeTime
. . . . . end
. . . . end
. . . end
. . end
. end

. gl.Texture(1, false)
. glBlending(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
. glAlphaTest(false)
. glDepthTest(false)
. glDepthMask(false)
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
But I don't understand most of that code, so I hope it wasn't a too awful method of removing the zeroes.
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Post by imbaczek »

A better (faster) solution would be to condition calls to Draw... function, but your isn't wrong.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Post by zwzsg »

Yes, but the only other line that mention the function DrawUnitFunc is:

glDrawFuncAtUnit(damage[1], false, DrawUnitFunc,(damage[3] + damage[6]), damage[4], damage[2], damage[5], damage[7], damage[8])

And I just do not understand that kind of syntax. I mean, the function name, without filling its argument, is passed a an argument to another function, while I would be expecting something more like DrawUnitFunc(arg1,arg2,arg3,arg4,arg5,arg6).
Kloot
Spring Developer
Posts: 1867
Joined: 08 Oct 2006, 16:58

Post by Kloot »

glDrawFuncAtUnit() is a function which takes as its
third argument a pointer to a custom Lua function
(called "DrawUnitFunc" in this script) that performs
some user-defined drawing task at a unit's location,
like showing rank icons or DPS values. The other
arguments (4 through n, there's no limit to how
many can be provided) are passed to the custom
function, DrawUnitFunc() takes 6 here. When it
(glDrawFuncAtUnit()) is called, it in turn calls
DrawUnitFunc() with those 6 values, so you can
control what the drawing task does.
Last edited by Kloot on 20 Aug 2007, 22:20, edited 1 time in total.
Post Reply

Return to “Lua Scripts”