I want to modify the LUA of the Ahud… (http://www.garrysmod.org/img/?t=dll&id=74612) for personal usage (i’ll don’t upload to gmod.org)
I want the ammo bar dissepear (like the armor bar) when the ammo is empty or i hold a weapon don’t require ammo (when the ammo in the clip is on 0)
Here is the full LUA content
ahud = {}
ahud.ammo = {}
ahud.ammo.primary = {}
ahud.ammo.secondary = {}
ahud.timer = {}
ahud.timer.current = CurTime()
ahud.timer.previous = CurTime()
ahud.timer.agestamp = -1
ahud.timer.age = 0
ahud.gauge = {}
ahud.gauge.pos = {}
ahud.gauge.width = 100
ahud.gauge.height = 10
ahud.gauge.pos.x = ScrW() / 2
ahud.gauge.pos.y = ScrH()
ahud.gauge.border = 1
ahud.gauge.bevel = 1
ahud.gauge.depth = 2
ahud.gauge.movrate = 3.25
ahud.gauge.fillrate = 0.75
ahud.gauge.tickwidth = 1
ahud.gauge.contrast = 96
ahud.gauge.align = 1
ahud.gauge.rowspacing = 3
ahud.gauge.drawtext = 1
ahud.gauge.shakeamt = 4
ahud.gauge.rowheight = ahud.gauge.height + ahud.gauge.rowspacing
ahud.gauge.direction = 1
ahud.gauges = {}
ahud.gauges.__index = ahud.gauges
ahud.convars = {}
ahud.shoulddrawtable =
{
["CHudAmmo"] = false,
["CHudHealth"] = false,
["CHudBattery"] = false,
["CHudSecondaryAmmo"] = false
}
function ahud.HueToRGB( n1, n2, hue)
if ( hue < 0 ) then hue = hue + 255 end
if ( hue > 255 ) then hue = hue - 255 end
if ( hue < 42.5 ) then
return ( n1 + ( ( ( n2-n1 ) * hue + 21.25 ) / 42.5 ) )
end
if ( hue < 127.5 ) then
return ( n2 )
end
if ( hue < 170 ) then
return ( n1 + ( ( ( n2 - n1 ) * ( 170 - hue ) + 21.25 ) / 42.5) )
else
return ( n1 );
end
end
function ahud.HLStoRGB( hue, lum, sat, alpha )
hue = math.min( math.max( math.floor( hue ), 0), 254 )
sat = math.min( math.max( math.floor( sat ), 0), 254 )
lum = math.min( math.max( math.floor( lum ), 0), 254 )
if ( lum <= 127 ) then
m2 = (lum*(255 + sat) + 127.5 ) / 255
else
m2 = lum + sat - ( lum * sat + 127.5 ) / 255
end
m1 = 2*lum-m2
return Color( ((ahud.HueToRGB( m1, m2, hue+85)*255 + 127.5) / 255),
((ahud.HueToRGB( m1, m2, hue)*255 + 127.5) / 255),
((ahud.HueToRGB( m1, m2, hue-85)*255 + 127.5) / 255), alpha )
end
function ahud.gauges.construct( div, c1, c2 )
local ret = {}
setmetatable( ret, ahud.gauges )
ret.x = 0
ret.y = 0
ret.div = div
ret.targx = 0
ret.targy = 0
ret.at = 0
ret.targ = 1
ret.forcedraw = 0
ret.shakephase = 0
ret.shakeamt = 0
ret.monitortarg = 0
ret.color = {}
ret.color.empty = c1 -- { 0, 128, 255 }
ret.color.full = c2 -- { 30, 128, 255 }
return ret
end
function ahud.gauges:draw( text )
local ypos = ahud.gauge.pos.y - ( self.y + 1 ) * ahud.gauge.rowheight
local xpos = self.x + ahud.gauge.border + ahud.gauge.pos.x
ahud.gauge.rowheight = ahud.gauge.height + ahud.gauge.rowspacing
if ( self.forcedraw == 0 && self.at <= 0 ) then
return; end
if ( ahud.gauge.align == 1 ) then
xpos = xpos - ahud.gauge.width / 2; end
if ( ahud.gauge.align == 2 ) then
xpos = xpos - ahud.gauge.width; end
if ( self.shakeamt ) then
local vr = math.random( 0, 360 )
local vx = ahud.gauge.shakeamt * self.shakeamt * math.cos( vr )
local vy = ahud.gauge.shakeamt *self.shakeamt * math.sin( vr )
xpos = xpos + vx
ypos = ypos + vy
end
-- main bg
draw.RoundedBox( 0,
xpos, ypos,
ahud.gauge.width,
ahud.gauge.height,
Color( 128, 128, 128 ) )
-- gauge bg
draw.RoundedBox( 0,
xpos + ahud.gauge.border, ypos + ahud.gauge.border,
ahud.gauge.width - 2 * ahud.gauge.border,
ahud.gauge.height - 2 * ahud.gauge.border,
ahud.HLStoRGB( (self.color.full[1] - self.color.empty[1])*self.at + self.color.empty[1],
64,
(self.color.full[3] - self.color.empty[3])*self.at + self.color.empty[3], 255 ) )
-- main gauge, highlight
local mint = math.min( self.at, self.targ )
local tintdark = -ahud.gauge.contrast
local tintbright = ahud.gauge.contrast
-- main gauge, shadow
draw.RoundedBox( 0,
xpos + ahud.gauge.border + ahud.gauge.bevel,
ypos + ahud.gauge.border + ahud.gauge.bevel,
mint * (ahud.gauge.width - 2 * (ahud.gauge.border + ahud.gauge.bevel) ),
ahud.gauge.height - 2 * (ahud.gauge.border + ahud.gauge.bevel ),
ahud.HLStoRGB( (self.color.full[1] - self.color.empty[1])*self.at + self.color.empty[1],
(self.color.full[2] - self.color.empty[2])*self.at + self.color.empty[2] + tintdark,
(self.color.full[3] - self.color.empty[3])*self.at + self.color.empty[3], 255 ) )
draw.RoundedBox( 0,
xpos + ahud.gauge.border + ahud.gauge.bevel,
ypos + ahud.gauge.border + ahud.gauge.bevel,
mint * (ahud.gauge.width - 2 * (ahud.gauge.border + ahud.gauge.bevel) ) - 1 * ahud.gauge.depth ,
ahud.gauge.height - 2 * (ahud.gauge.border + ahud.gauge.bevel ) - ahud.gauge.depth,
ahud.HLStoRGB( (self.color.full[1] - self.color.empty[1])*self.at + self.color.empty[1],
(self.color.full[2] - self.color.empty[2])*self.at + self.color.empty[2] + tintbright,
(self.color.full[3] - self.color.empty[3])*self.at + self.color.empty[3], 255 ) )
-- main gauge, base
draw.RoundedBox( 0,
xpos + ahud.gauge.border + ahud.gauge.bevel + ahud.gauge.depth,
ypos + ahud.gauge.border + ahud.gauge.bevel + ahud.gauge.depth,
mint * (ahud.gauge.width - 2 * (ahud.gauge.border + ahud.gauge.bevel) ) - 2 * ahud.gauge.depth ,
ahud.gauge.height - 2 * (ahud.gauge.border + ahud.gauge.bevel + ahud.gauge.depth ),
ahud.HLStoRGB( (self.color.full[1] - self.color.empty[1])*self.at + self.color.empty[1],
(self.color.full[2] - self.color.empty[2])*self.at + self.color.empty[2],
(self.color.full[3] - self.color.empty[3])*self.at + self.color.empty[3], 255 ) )
--[[
draw.RoundedBox( 0,
xpos + ahud.gauge.border + ahud.gauge.bevel,
ypos + ahud.gauge.border + ahud.gauge.bevel,
self.at * (ahud.gauge.width - 2 * (ahud.gauge.border + ahud.gauge.bevel) ) ,
ahud.gauge.height - 2 * (ahud.gauge.border + ahud.gauge.bevel),
ahud.HLStoRGB( (self.color.full[1] - self.color.empty[1])*self.at + self.color.empty[1],
(self.color.full[2] - self.color.empty[2])*self.at + self.color.empty[2],
(self.color.full[3] - self.color.empty[3])*self.at + self.color.empty[3], 255 ) )
]]--
local w = math.abs( self.at - self.targ ) * (ahud.gauge.width - 2*(ahud.gauge.border + ahud.gauge.bevel))
local start = mint * (ahud.gauge.width - 2*(ahud.gauge.border + ahud.gauge.bevel))
if ( w ) then
draw.RoundedBox( 0,
xpos + ahud.gauge.border + ahud.gauge.bevel + start,
ypos + ahud.gauge.border + ahud.gauge.bevel,
w,
ahud.gauge.height - 2 * ( ahud.gauge.border + ahud.gauge.bevel ),
ahud.HLStoRGB( (self.color.full[1] - self.color.empty[1])*self.at + self.color.empty[1],
32,
(self.color.full[3] - self.color.empty[3])*self.at + self.color.empty[3], 255 ) )
end
if ( self.div && self.div*(ahud.gauge.width - 2*(ahud.gauge.border + ahud.gauge.bevel)) > 3 ) then
local i, j
j = math.max( self.at, self.targ )
for i = self.div, j, self.div do
draw.RoundedBox( 0,
xpos + ahud.gauge.border + ahud.gauge.bevel + i * (ahud.gauge.width - 2*(ahud.gauge.border + ahud.gauge.bevel)),
ypos + ahud.gauge.border + ahud.gauge.bevel,
ahud.gauge.tickwidth,
ahud.gauge.height - 2 * ( ahud.gauge.border + ahud.gauge.bevel ),
ahud.HLStoRGB( (self.color.full[1] - self.color.empty[1])*self.at + self.color.empty[1],
(self.color.full[2] - self.color.empty[2])*self.at + self.color.empty[2] + tintdark,
(self.color.full[3] - self.color.empty[3])*self.at + self.color.empty[3], 255 ) )
end
end
if ( ahud.gauge.drawtext == 1 ) then
draw.RoundedBox( 0,
xpos + ahud.gauge.width,
ypos,
string.len(text) * 8,
ahud.gauge.height,
Color( 0, 0, 0, 192 ) )
draw.SimpleText( text, "ScoreboardText",
xpos + ahud.gauge.width, ypos + ahud.gauge.height / 2, Color( 255, 255, 255, 255 ), 0, 1 )
end
end
function ahud.gauges:think( deltatime )
-- move the gauge row
local stemp
if ( self.monitortarg != self.targ ) then
if ( self.monitortarg > self.targ ) then
self.shakeamt = 1; end
self.monitortarg = self.targ
end
if ( self.shakeamt > 0 ) then
if ( math.abs( self.shakeamt - deltatime ) < deltatime ) then
self.shakeamt = 0
else
self.shakeamt = self.shakeamt - deltatime
end
end
stemp = ahud.gauge.movrate * deltatime
if ( math.abs( self.y - self.targy ) < stemp ) then
self.y = self.targy
else
if ( self.y < self.targy ) then
self.y = self.y + stemp
else
self.y = self.y - stemp
end
end
-- fill gauge
stemp = ahud.gauge.fillrate * deltatime
if ( math.abs( self.at - self.targ ) < stemp ) then
self.at = self.targ
else
if ( self.at < self.targ ) then
self.at = self.at + stemp
else
self.at = self.at - stemp
end
end
if ( self.at == 0 && self.forcedraw == 0 ) then
self.y = -1
return 0
else
return 1
end
end
function ahud.HideDefaultHUD( szShouldDraw )
return ahud.shoulddrawtable[ szShouldDraw ]
end
health = ahud.gauges.construct( .1, {0, 96, 255}, {64, 192, 255} )
armor = ahud.gauges.construct( .1, {170, 96, 255}, {170, 192, 255} )
ammo = ahud.gauges.construct( .3, {0, 96, 255}, {40, 192, 255} )
ammo.forcedraw = 1
function ahud.updateconvars()
ahud.convars[1] = CreateClientConVar( "ahud_width", "100", true, false )
ahud.convars[2] = CreateClientConVar( "ahud_height", "10", true, false )
ahud.convars[3] = CreateClientConVar( "ahud_pos_x", "0.5", true, false )
ahud.convars[4] = CreateClientConVar( "ahud_pos_y", "1.0", true, false )
ahud.convars[5] = CreateClientConVar( "ahud_border", "1", true, false )
ahud.convars[6] = CreateClientConVar( "ahud_bevel", "1", true, false )
ahud.convars[7] = CreateClientConVar( "ahud_depth", "2", true, false )
ahud.convars[8] = CreateClientConVar( "ahud_rate_mov", "3.25", true, false )
ahud.convars[9] = CreateClientConVar( "ahud_rate_fill", "0.75", true, false )
ahud.convars[10] = CreateClientConVar( "ahud_tickwidth", "1", true, false )
ahud.convars[11] = CreateClientConVar( "ahud_contrast", "96", true, false )
ahud.convars[12] = CreateClientConVar( "ahud_align", "1", true, false )
ahud.convars[13] = CreateClientConVar( "ahud_color_health_empty", "0 96 255", true, false )
ahud.convars[14] = CreateClientConVar( "ahud_color_health_full", "64 192 255", true, false )
ahud.convars[15] = CreateClientConVar( "ahud_color_armor_empty", "170 96 255", true, false )
ahud.convars[16] = CreateClientConVar( "ahud_color_armor_full", "170 192 255", true, false )
ahud.convars[17] = CreateClientConVar( "ahud_color_ammo_empty", "0 96 255", true, false )
ahud.convars[18] = CreateClientConVar( "ahud_color_ammo_full", "40 192 255", true, false )
ahud.convars[19] = CreateClientConVar( "ahud_shakeamt", "4", true, false )
ahud.convars[20] = CreateClientConVar( "ahud_movdir", "1", true, false )
ahud.convars[21] = CreateClientConVar( "ahud_override_armor", "1", true, false )
ahud.gauge.width = ahud.convars[1]:GetFloat()
ahud.gauge.height = ahud.convars[2]:GetFloat()
ahud.gauge.pos.x = ahud.convars[3]:GetFloat() * ScrW()
ahud.gauge.pos.y = ahud.convars[4]:GetFloat() * ScrH()
ahud.gauge.border = ahud.convars[5]:GetFloat()
ahud.gauge.bevel = ahud.convars[6]:GetFloat()
ahud.gauge.depth = ahud.convars[7]:GetFloat()
ahud.gauge.movrate = ahud.convars[8]:GetFloat()
ahud.gauge.fillrate = ahud.convars[9]:GetFloat()
ahud.gauge.tickwidth = ahud.convars[10]:GetFloat()
ahud.gauge.contrast = ahud.convars[11]:GetFloat()
ahud.gauge.align = ahud.convars[12]:GetFloat()
ahud.gauge.shakeamt = ahud.convars[19]:GetFloat()
ahud.gauge.direction = ahud.convars[20]:GetFloat()
ahud.shoulddrawtable["CHudBattery"] = !ahud.convars[21]:GetBool()
local temp
temp = string.Explode( ' ', ahud.convars[13]:GetString() ); health.color.empty = { tonumber( temp[1] ), tonumber( temp[2] ), tonumber( temp[3] ) }
temp = string.Explode( ' ', ahud.convars[14]:GetString() ); health.color.full = { tonumber( temp[1] ), tonumber( temp[2] ), tonumber( temp[3] ) }
temp = string.Explode( ' ', ahud.convars[15]:GetString() ); armor.color.empty = { tonumber( temp[1] ), tonumber( temp[2] ), tonumber( temp[3] ) }
temp = string.Explode( ' ', ahud.convars[16]:GetString() ); armor.color.full = { tonumber( temp[1] ), tonumber( temp[2] ), tonumber( temp[3] ) }
temp = string.Explode( ' ', ahud.convars[17]:GetString() ); ammo.color.empty = { tonumber( temp[1] ), tonumber( temp[2] ), tonumber( temp[3] ) }
temp = string.Explode( ' ', ahud.convars[18]:GetString() ); ammo.color.full = { tonumber( temp[1] ), tonumber( temp[2] ), tonumber( temp[3] ) }
ahud.gauge.rowspacing = 3
ahud.gauge.drawtext = 1
ahud.gauge.rowheight = ahud.gauge.height + ahud.gauge.rowspacing
end
function ahud.think()
ahud.timer.previous = ahud.timer.current
ahud.timer.current = CurTime()
local elapsed = ( ahud.timer.current - ahud.timer.previous )
local dispammo = 0
local dispammomax = 0
local dispammosec = 0
local client = LocalPlayer()
ahud.timer.age = ahud.timer.age + elapsed
if ( math.floor( ahud.timer.age ) != ahud.timer.agestamp ) then
if ( health.at < 0.25 ) then
health.shakeamt = 0.5; end
ahud.timer.agestamp = math.floor( ahud.timer.age )
ahud.updateconvars()
end
health.targ = math.min( math.max( client:Health(), 0 ), 100 ) / 100
armor.targ = math.min( math.max( client:Armor(), 0 ), 100 ) / 100
local currentWeapon = client:GetActiveWeapon()
local currentWeaponName
if ( currentWeapon != nil && currentWeapon:IsValid() ) then
currentWeaponName = tostring( currentWeapon:GetPrintName() )
if ( ahud.ammo.primary[ currentWeaponName ] == nil ) then
ahud.ammo.primary[ currentWeaponName ] = -1
end
if ( currentWeapon:Clip1() == -1 ) then
-- In this case, where you have rpg, slam, or grenade selected,
-- we use the maximum number of items the client has possessed.
if ( ahud.ammo.primary[ currentWeaponName ] < client:GetAmmoCount( currentWeapon:GetPrimaryAmmoType() ) ) then
ahud.ammo.primary[ currentWeaponName ] = client:GetAmmoCount( currentWeapon:GetPrimaryAmmoType() )
end
else
-- This weapon uses the regular ammo
if ( ahud.ammo.primary[ currentWeaponName ] < currentWeapon:Clip1() ) then
ahud.ammo.primary[ currentWeaponName ] = currentWeapon:Clip1()
end
end
if ( ahud.ammo.primary[ currentWeaponName ] > 0 ) then
if ( currentWeapon:Clip1() == -1 ) then
if ( client:GetAmmoCount( currentWeapon:GetPrimaryAmmoType() ) != 0 ) then
ahud.ammo.primary[ currentWeaponName ] =
math.max( ahud.ammo.primary[ currentWeaponName ],
client:GetAmmoCount( currentWeapon:GetPrimaryAmmoType() ) )
dispammo = client:GetAmmoCount( currentWeapon:GetPrimaryAmmoType() )
dispammomax = ahud.ammo.primary[ currentWeaponName ]
ammo.targ = dispammo / ahud.ammo.primary[ currentWeaponName ]
ammo.div = 1 / ahud.ammo.primary[ currentWeaponName ]
ammo.forcedraw = 0
else
dispammo = 0
dispammomax = 0
ammo.forcedraw = 0
ammo.targ = 0
ammo.at = 0
ahud.ammo.primary[ currentWeaponName ] = 0
end
else
dispammo = currentWeapon:Clip1()
dispammomax = client:GetAmmoCount( currentWeapon:GetPrimaryAmmoType() )
ammo.targ = dispammo / ahud.ammo.primary[ currentWeaponName ]
ammo.div = 1 / ahud.ammo.primary[ currentWeaponName ]
end
else
ammo.targ = 0
end
dispammosec = client:GetAmmoCount( currentWeapon:GetSecondaryAmmoType() )
--[[ print( currentWeapon:Clip1() .. " " ..
client:GetAmmoCount( currentWeapon:GetPrimaryAmmoType() ) .. " " ..
client:GetAmmoCount( currentWeapon:GetSecondaryAmmoType() ) ) ]]--
end
local i = ahud.gauge.direction
ammo.targy = i
i = i + ahud.gauge.direction * ammo:think( elapsed )
armor.targy = i
i = i + ahud.gauge.direction * armor:think( elapsed )
health.targy = i
i = i + ahud.gauge.direction * health:think( elapsed )
if ( ammo.at < ammo.targ ) then
ammo.at = ammo.targ
end
health:draw( string.format( "%i", client:Health() ) )
armor:draw( string.format( "%i", client:Armor() ) )
ammo:draw( string.format( "%i|%i(%i)", dispammo, dispammomax, dispammosec ) )
end
hook.Add( "HUDShouldDraw","ahud.HideDefaultHUD", ahud.HideDefaultHUD)
hook.Add( "HUDPaint", "CustomHud", ahud.think )