Custom resources

Custom resources

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

Moderator: Moderators

kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Custom resources

Post by kalda341 »

Is it possible to have resources other than metal and energy? And have them different for different factions? Eg. Worshipers for a faith faction, cells for micro-organisms gains from kills. How would I do this?
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Custom resources

Post by zwzsg »

Yes, but you'd have to code it yourself (with a Lua gadget). Or to nab a gadget from another mod with custom resource, but then in order to use it you'd have to understand and adapt it, which in the end could end up almost as much complicated as writing your own.

The engine only handle two resources, if it's just their name that bothers it's easier to change the GUI than to code a new resources. If it is their method of acquisition that bothers you, it's also easier to code new way to gain energy and metal but otherwise let the engine manage them than to recode a whole new resource.

But if you really need more than two resources, or a resource that behave very differently, then you have to code all the logic to handle these resources in a gadget. Depending on what kind of event modify that resources, than can be a rather simple gadget or a long and complex monster gadget.

GundamRTS had custom resource (basic/refined/exotic, plus the power supply har), but don't rush for it as GundamRTS is not compatible with current Spring.

In Kernel Panic, the network "buffer" and the "SoS" could maybe be considered like simplified sort of custom resources.

Some other KDR_11k mods like Fibre had some custom resource system as well.
User avatar
SinbadEV
Posts: 6475
Joined: 02 May 2005, 03:56

Re: Custom resources

Post by SinbadEV »

this would actually make a good question/answer for http://answers.springlobby.info/
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Custom resources

Post by knorke »

kalda341 wrote:cells for micro-organisms gains from kills. How would I do this?
That is easy to do with Lua.
Use something like

Code: Select all

gadget:function Spring.UnitKilled (...)
if (killer==that and killed==this) then
Spring.AddTeamResource (team of killer, "metal", 100)
end
end
(pseudeo code)

http://springrts.com/wiki/Lua_SyncedCtrl#Resources
http://springrts.com/wiki/LuaCallinReturn

For display:
Here is a quite simple widget that replaces the default resource bars.
http://code.google.com/p/conflictterra/ ... isplay.lua
ie for metal it just displays
Metal: 450
Of course that could easily by changed into
Faith: 450
And have them different for different factions?
Yes, use something like this:

Code: Select all

faction = Spring.GetTeamInfo (teamID blabla).side
if (faction=="Worshippers") then resourcename = "Faith" end
if (faction=="micro-organisms") then resourcename = "Cells" end
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Custom resources

Post by zwzsg »

SinbadEV wrote:this would actually make a good question/answer for http://answers.springlobby.info/
No, because there isn't a simple answer.

It requires extensive discussion to get the initial poster to develop his idea, and a solution that'll suit one modder won't another.
kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Re: Custom resources

Post by kalda341 »

Thanks, I'll try these out and see if they work for me.
kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Re: Custom resources

Post by kalda341 »

knorke wrote:
kalda341 wrote:cells for micro-organisms gains from kills. How would I do this?
That is easy to do with Lua.
Use something like

Code: Select all

gadget:function Spring.UnitKilled (...)
if (killer==that and killed==this) then
Spring.AddTeamResource (team of killer, "metal", 100)
end
end
(pseudeo code)

http://springrts.com/wiki/Lua_SyncedCtrl#Resources
http://springrts.com/wiki/LuaCallinReturn

For display:
Here is a quite simple widget that replaces the default resource bars.
http://code.google.com/p/conflictterra/ ... isplay.lua
ie for metal it just displays
Metal: 450
Of course that could easily by changed into
Faith: 450
And have them different for different factions?
Yes, use something like this:

Code: Select all

faction = Spring.GetTeamInfo (teamID blabla).side
if (faction=="Worshippers") then resourcename = "Faith" end
if (faction=="micro-organisms") then resourcename = "Cells" end
So this is the line I want to vary based on the faction:

Code: Select all

glText("Carbon: " .. metal, vsx/2, vsy-UIscale*1.4, UIscale*1.4, "oc")
How would I do this?
I am assuming it would be something like:

Code: Select all

If (faction=="Macrobes") then
glText("Carbon: " .. metal, vsx/2, vsy-UIscale*1.4, UIscale*1.4, "oc")
end if
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Custom resources

Post by knorke »

yes should work like that.
do get faction use
local _,_,_,_,faction = Spring.GetTeamInfo(localplayerID)

if you use my resourcedisplay, localplayerID will already be read, if you use or own widget do
local localplayerID = Spring.GetLocalTeamID()

to check if all that reads faction correctly try adding
Spring.Echo ("your faction is" .. faction)
kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Re: Custom resources

Post by kalda341 »

I've played around with various syntaxes but cannot get it to function. I am using your widget:

Code: Select all

--display for resources
--because 99999[..]9 storage make the engine resbar look ugly ;)

function widget:GetInfo()
  return {
    name      = "resouce display",
    desc      = "display metal as number, without storage bar",
    author    = "knorke",
    date      = "Oct 19, 2010",
    license   = "weeeeeee iam on horse",
    layer     = -3,
    enabled   = true  --  loaded by default?
  }
end


--thanks to trepan and his "com ends" widget :)
local glPopMatrix      = gl.PopMatrix
local glPushMatrix     = gl.PushMatrix
local glText           = gl.Text

local font = 'FreeMonoBold'
local fontSize = 32
local fontName = LUAUI_DIRNAME..'Fonts/'..font..'_'..fontSize
local fh = fontHandler.UseFont(fontName)
local gameframe = 0

local vsx, vsy = widgetHandler:GetViewSizes()
local UIscale, fontsize = vsy/55
function widget:ViewResize(viewSizeX, viewSizeY)
        vsx = viewSizeX
        vsy = viewSizeY
        UIscale = vsx/75
end

function widget:GameFrame(f)
gameframe = f
end

function widget:DrawScreen()
local localplayerID = Spring.GetLocalTeamID()
local metal = math.floor (Spring.GetTeamResources (localplayerID ,"metal"))
local energy, energystorage, energypull, energyincome=  Spring.GetTeamResources (localplayerID ,"energy")
glPushMatrix()
if (fh) then
        fh = fontHandler.UseFont(fontName)
        fontHandler.DrawCentered(msg)
else    
        local ebar_startx = vsx*0.6
        local ebar_endx = vsx*0.95
        local ebar_length =  ebar_endx-ebar_startx
        local ebar_currentendx = ((energy/energystorage)*ebar_length)+ebar_startx
        local ebar_height = UIscale*2
        local ebar_height2 = UIscale/2
        gl.Color(0.8, 0.8, 0.8, 0.5)
        gl.Rect(ebar_endx, vsy-ebar_height, (vsx/2)-UIscale*8,vsy ) --background of metal display
        glText("Metal: " .. metal, vsx/2, vsy-UIscale*1.5, UIscale*1.5, "oc")
        gl.Color(1, 1, 0, 0.5)
        gl.Rect(ebar_startx, vsy, ebar_endx, vsy-ebar_height) --background of energy bar in total size/storage  
        gl.Color(1, 1, 0, 0.8)
        gl.Rect(ebar_startx, vsy-(ebar_height/2)+ebar_height2 , ebar_currentendx, vsy-(ebar_height/2)-ebar_height2) --actual energy bar
        --sinus_bar (ebar_startx, vsy-(ebar_height/2), ebar_height/2, ebar_currentendx, 50)
        sinus_bar2 (ebar_startx, vsy-(ebar_height/2), ebar_height/2, ebar_currentendx, energystorage/50)
        gl.Color(0.8, 0.8, 0.8, 0.5)
        --glText("Energy: " .. energy, (ebar_startx+ebar_endx)/2, vsy-UIscale*1.5, UIscale*1.5, "oc")  
        local msg = ""--"Energy: " .. math.floor(energy)
        if (energy < 100) then
                msg = "Low Energy"
                if (energyincome < 10) then msg = "Energy Critical" end
        end
        glText(msg , (ebar_startx+ebar_endx)/2, vsy-UIscale*1.5, UIscale*1.5, "oc")
end
glPopMatrix()
end

function sinus_bar (x,y, h, endx, r)
local SINn = r
local stepx = (endx-x)/(SINn+1)
local a = 2
local SINx = x
local SINy = y
gl.Color(1, 0, 0, 1)
        for i = 0, SINn, 1 do
                SINy = math.sin ((gameframe+SINx)/(UIscale/2)) * (h/2) +y
                SINx = SINx + stepx            
                gl.Rect (SINx-stepx,SINy-a, SINx, SINy+a)              
        end
end

function sinus_bar2 (x,y, h, endx, r)
local SINn = r
local stepx = (endx-x)/(SINn+1)
local a = 2
local SINx = x
local SINy = y
gl.Color(1, 0, 0, 1)
        for i = 0, SINn, 1 do
                a = math.sin ((gameframe+SINx)/(UIscale/2)) * (h/2)
                SINx = SINx + stepx
                gl.Rect (SINx-stepx,SINy-a, SINx, SINy+a)
        end
end

function widget:Initialize()
Spring.SendCommands ("resbar 0")
Spring.Echo ("tp_ressourcedisplay.lua: type /resbar 1 to get the engines resource bars back")
end

User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Custom resources

Post by knorke »

i will look at it later.
possibly also needs Spring.GetSideData
kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Re: Custom resources

Post by kalda341 »

Thanks.
kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Re: Custom resources

Post by kalda341 »

Can you see anything that might be stopping it from working?
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: Custom resources

Post by smoth »

zwzsg wrote:GundamRTS had custom resource (basic/refined/exotic, plus the power supply har), but don't rush for it as GundamRTS is not compatible with current Spring.
SUP!?!
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Custom resources

Post by knorke »

oh yes, sorry.
kalda341 wrote:Can you see anything that might be stopping it from working?
something about Spring.GetSideData got me confused..did not return parameters in order i expected or it was stupidly set up in the mod i tested with. did not feel like hunting that down sorry :/

smoth:
he just wants to replace the "metal" name with something else if i understood correctly.
kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Re: Custom resources

Post by kalda341 »

I have added this, but for some reason it won't work:

Code: Select all

local team = Spring.GetSideData()
if team = "MACROBES" then
		resourcea = carbon
		resourceb = energy
end
kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Re: Custom resources

Post by kalda341 »

Don't worry, it needed a ==.
kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Re: Custom resources

Post by kalda341 »

Code: Select all

glText(resourcea.. metal, vsx/2, vsy-UIscale*1.5, UIscale*1.5, "oc")
is the problem.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Custom resources

Post by knorke »

GetSideData() expects a sideID, like this:
sideID = some other stuff to find the correct sideID
local team = Spring.GetSideData(sideID)

Code: Select all

resourcea = "Carbon"
resourceb = "Energy"
Needs " " because that means it is a string.

Code: Select all

glText(resourcea .. ": " .. metal, vsx/2, vsy-UIscale*1.5, UIscale*1.5, "oc")
kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Re: Custom resources

Post by kalda341 »

Of course. I am fairly new to lua and quite often get it mixed up with python and basic. Does anyone know how to get the sideid?
kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Re: Custom resources

Post by kalda341 »

Actually, I think sideName is more applicable. Does anyone know how it functions?
Post Reply

Return to “Lua Scripts”