// Lost World HUD Code by TrickyTex with help from Jimita
// meow

local lw,num = "LW","NUM"
local lwtallnum0,lwtallnum1,lwtallnum2,lwtallnum3 = {},{},{},{}
local function fmt(n) return lw+tostring(n)+num end
for i=0,9 do
	lwtallnum0[i] = fmt(0)+i
	lwtallnum1[i] = fmt(1)+i
	lwtallnum2[i] = fmt(2)+i
	lwtallnum3[i] = fmt(3)+i
end
local function V_DrawLWNum(v,x,y,num,digits,flags,sh,c,red)
	local vcp = v.cachePatch
	local vds = v.drawScaled
	local vgc = v.getColormap
	local fmul = FixedMul
	if (num < 0) num = -num end
	/*start*/
	local ox,oy,on,od = x,y,num,digits
	oy=$-(sh or 2)
	local w = (((vcp("LW0NUM0").width)/2)-6)
	/*bgn*/
	x,y = ox,oy
	if (c==1 or c==2) c=3 end
	if (c==23) c=24 end
	repeat
		x=$-w
		vds(x<<16,y<<16,(not red) and FRACUNIT/2 or FRACUNIT/696969,vcp((c and lwtallnum2 or lwtallnum1)[num%10]),flags,vgc("sonic",c))
		num=($/10);digits=($-1)
	until (digits<1)
	/*num*/
	x,y = ox,oy
	num,digits=on,od
	repeat
		x=$-w
		vds(x<<16,y<<16,FRACUNIT/2,vcp(((not red) and lwtallnum0 or lwtallnum3)[num%10]),flags)
		num=($/10);digits=($-1)
	until (digits<1)
end

freeslot(
	"sfx_itemu1",
	"sfx_itemu2",
	"sfx_itemus",
	"S_REDRINGY"
)

mobjinfo[MT_REDRING].spawnstate = S_REDRINGY //lazy way of giving the sling rings runners graphics

S_sfx[sfx_itemu1] = {true, 255} //cannot play this sound more than once or the new sound will take over
S_sfx[sfx_itemu2] = {true, 255} //read the comment above.


local cv_lwhudshow = CV_RegisterVar({
    name = "lwhudshow",
    defaultvalue = "0",
    flags = 0,
    PossibleValue = CV_OnOff,
    func = nil,
})

addHook("ThinkFrame", function()
for player in players.iterate do if player and player.valid and player.mo and player.mo.valid then
	if player.ringseq == nil then player.ringseq = 0 end //init the ring sequencer
	if player.ringdif == nil then player.ringdif = 0 end
	if player.ringseq > 1 then player.ringseq = 0 end //the sequencer can only be 0 or 1
	if player.ringtimer == nil then player.ringtimer = 0 end //init the hud timer for rings
	if player.ringtimer > 0 then player.ringtimer = $ - 1 end //it decreases each frame.
	if player.timetimer == nil then player.timetimer = 0 end //init hud timer for time
	if player.timetimer > 0 then player.timetimer = $ - 1 end // decreases each frame
	if player.scoretimer == nil then player.scoretimer = 0 end //init score timer
	if player.scoretimer > 0 then player.scoretimer = $ - 1 end //decreases each frame
	if player.health == 1 then player.ringtimer = 3*TICRATE end //if 000 then always display
	if player.lastrings == nil then player.lastrings = 1 end //init counter for the rings you've had before
	if player.prevscore == nil then player.prevscore = 1 end //same here for score
	if player.ringanim == nil then player.ringanim = 0 end //init animation engine
	if player.ringanim > 0 then player.ringanim = $ + 1 end //each frame in ring animation lasts a single tic
	if player.ringanim > 15 then player.ringanim = 0 end //once the anitmation ends, set back to 0
	if player.rings > player.lastrings and player.ringtimer < 3*TICRATE then player.ringtimer = (player.ringtimer < 35 and 4 or 3)*TICRATE end //if you gain a ring, display rings
	if player.rings < player.lastrings and player.ringtimer < 3*TICRATE then player.ringtimer = (player.ringtimer < 35 and 4 or 3)*TICRATE end //if you lose a ring, display rings
	if player.ringtimer > 4*TICRATE then player.ringtimer = 4*TICRATE end
	if player.rings > player.lastrings then player.ringanim = 1 end //if your ring value increases, play the animation
	if player.rings < player.lastrings then player.ringanim = 1 end //if your ring value decreases, play the animation
	if player.flicktimer == nil then player.flicktimer = 0 end //init flicky meter timer
	if player.flicktimer > 1 then player.flicktimer = $ - 1 end //it decreases
	if player.lifetimer == nil then player.lifetimer = 0 end // init life timer
	if player.lifetimer > 0 then player.lifetimer = $ - 1 end // it decreases
	if player.prevlife == nil then player.prevlife = 0 end // init previous life checker
	if player.prevlife > player.lives then player.lifetimer = 4*TICRATE end // if you lose a life show counter
	if player.prevlife < player.lives then player.lifetimer = 4*TICRATE end // if you gain a life show counter
	if player.flickoffset == nil then player.flickoffset = 0 end
	if player.flickfadetimer == nil then player.flickfadetimer = 0 end
	if player.idletimer == nil then player.idletimer = 0 end
	if player.ringoffset == nil then player.ringoffset = 0 end
	if player.ringfadetimer == nil then player.ringfadetimer = 0 end
	if player.justringed == nil then player.justringed = 0 end
	if player.timefadetimer == nil then player.timerfadetimer = 0 end
	if player.lifeoffset == nil then player.lifeoffset = 0 end
	if player.lifefadetimer == nil then player.lifefadetimer = 0 end
	if player.timeoffset == nil then player.timeoffset = 0 end
	if player.timefadetimer == nil then player.timefadetimer = 0 end
	if player.scorefadetimer == nil then player.scorefadetimer = 0 end
	if player.check == nil then player.check = cv_lwhudshow.value end
	if player.check != cv_lwhudshow.value then player.check = cv_lwhudshow.value end
	if player.check == 1 then player.lifetimer = 2*TICRATE end
	if player.check == 1 then player.ringtimer = 2*TICRATE end
	if player.check == 1 then player.scoretimer = 2*TICRATE end
	if player.check == 1 then player.flicktimer = 2*TICRATE end
	if player.check == 1 then player.timetimer = 2*TICRATE end
	if player.redflash == nil then player.redflash = 0 end
	if player.rings <= 0 then player.redflash = $ + 1 end
	if player.rings >= 1 then player.redflash = 0 end
	if player.redflash > 53 then player.redflash = 0 end
end end
end)

addHook("ThinkFrame", function()
for player in players.iterate do if player and player.valid and player.mo and player.mo.valid and gametype == GT_COOP then
if player.rings > player.lastrings then player.ringdif = player.rings - player.lastrings end
if player.rings > player.lastrings then P_AddPlayerScore(player, (100*player.ringdif)) end
end end
end)

addHook("ThinkFrame", function()
for player in players.iterate do if player and player.valid and player.mo and player.mo.valid and gametype == GT_MATCH then
player.ringtimer = 44
player.scoretimer = 44
player.flickfadetimer = 0
player.timetimer = 44
player.lifefadetimer = 0
end end
end)

addHook("ThinkFrame", function()
for player in players.iterate do if player and player.valid and player.mo and player.mo.valid and gametype == GT_TEAMMATCH then
player.ringtimer = 44
player.scoretimer = 44
player.flickfadetimer = 0
player.timetimer = 44
player.lifefadetimer = 0
end end
end)

addHook("ThinkFrame", function()
for player in players.iterate do if player and player.valid and player.mo and player.mo.valid and gametype == GT_CTF then
player.ringtimer = 44
player.scoretimer = 44
player.flickfadetimer = 0
player.timefadetimer = 0
player.lifefadetimer = 0
end end
end)

addHook("ThinkFrame", function()
for player in players.iterate do if player and player.valid and player.mo and player.mo.valid and gametype == GT_TAG then
player.ringtimer = 44
player.scoretimer = 44
player.flickfadetimer = 0
player.timefadetimer = 0
player.lifefadetimer = 0
end end
end)

addHook("ThinkFrame", function()
for player in players.iterate do if player and player.valid and player.mo and player.mo.valid and gametype == GT_HIDEANDSEEK then
player.ringtimer = 44
player.scoretimer = 44
player.flickfadetimer = 0
player.timefadetimer = 0
player.lifefadetimer = 0
end end
end)

addHook("ThinkFrame", function()
for player in players.iterate do if player and player.valid then
if player.ringanim != 0 then player.justringed = 1 end
if player.ringanim == 0 then player.justringed = 0 end
end end
end)

addHook("ThinkFrame", function()
for player in players.iterate do if player and player.valid then
if player.lastrings > player.rings then player.lastrings = player.rings end //sync last rings with current rings
if player.lastrings < player.rings then player.lastrings = player.rings end //sync last rings with current rings
end end
end)

addHook("ThinkFrame", function()
for player in players.iterate do if player and player.valid then
if player.prevscore > player.score then player.prevscore = player.score end //sync last score with current score
if player.prevscore < player.score then player.prevscore = player.score end //sync last score with current score
end end
end)

addHook("ThinkFrame", function()
for player in players.iterate do if player and player.valid then
if player.prevlife > player.lives then player.prevlife = player.lives end //sync last lives with current lives
if player.prevlife < player.lives then player.prevlife = player.lives end //sync last lives with current lives
end end
end)

//If standing still long enough to play idle animation, display the HUD
addHook("ThinkFrame", function()
 for player in players.iterate do
	local mo = player.mo
	if mo and P_IsObjectOnGround(mo) then
				if (mo.state==S_PLAY_WAIT)
				 if player.ringtimer < 3*TICRATE then player.ringtimer = (player.ringtimer < 35 and 4 or 3)*TICRATE end
				 if player.scoretimer < 3*TICRATE then player.scoretimer = (player.scoretimer < 35 and 4 or 3)*TICRATE end
				 if player.timetimer < 3*TICRATE then player.timetimer = (player.timetimer < 35 and 4 or 3)*TICRATE end
				 if player.flicktimer < 3*TICRATE then player.flicktimer = (player.flicktimer < 35 and 4 or 3)*TICRATE end
				 if player.lifetimer < 3*TICRATE then player.lifetimer = (player.lifetimer < 35 and 4 or 3)*TICRATE end
end end end end)

addHook("ThinkFrame", do
    for player in players.iterate do
		if player.ringtimer == 140 then player.ringoffset = 3 end
		if player.ringtimer == 139 then player.ringoffset = 2 end
		if player.ringtimer == 138 then player.ringoffset = 1 end
		if player.ringtimer < 137 then player.ringoffset = 0 end
		if player.ringtimer == 38 then player.ringfadetimer = 1 end
		if player.ringtimer == 37 then player.ringfadetimer = 2 end
		if player.ringtimer == 36 then player.ringfadetimer = 3 end
		if player.ringtimer == 35 then player.ringfadetimer = 4 end
		if player.ringtimer > 38 then player.ringfadetimer = 0 end
		end
		end)
		
addHook("ThinkFrame", do
    for player in players.iterate do
		if player.timetimer == 140 then player.timeoffset = 3 end
		if player.timetimer == 139 then player.timeoffset = 2 end
		if player.timetimer == 138 then player.timeoffset = 1 end
		if player.timetimer < 137 then player.timeoffset = 0 end
		if player.timetimer == 38 then player.timefadetimer = 1 end
		if player.timetimer == 37 then player.timefadetimer = 2 end
		if player.timetimer == 36 then player.timefadetimer = 3 end
		if player.timetimer == 35 then player.timefadetimer = 4 end
		if player.timetimer > 38 then player.timefadetimer = 0 end
		end
		end)

//ring counter
hud.add(function(v, player)
if player and player.valid and player.mo and player.mo.valid and not (player.powers[pw_carry] == CR_NIGHTSMODE) then
if player.ringtimer < 1*TICRATE
return
else
//animation engine
local ring1 = v.cachePatch("HINGA0")
local ring6 = v.cachePatch("HINGB0P0")
local ring2 = v.cachePatch("HINGC0O0")
local ring7 = v.cachePatch("HINGD0N0")
local ring3 = v.cachePatch("HINGE0M0")
local ring8 = v.cachePatch("HINGF0L0")
local ring4 = v.cachePatch("HINGG0K0")
local ring9 = v.cachePatch("HINGH0J0")
local ring5 = v.cachePatch("HINGI0")
if player.ringtimer < 3*TICRATE
if player.ringanim == 0
v.drawScaled(16*FRACUNIT, 48*FRACUNIT, FRACUNIT/3, ring1, (player.ringfadetimer == 1 and V_20TRANS|V_SNAPTOLEFT  or player.ringfadetimer == 2 and V_40TRANS|V_SNAPTOLEFT  or player.ringfadetimer == 3 and V_60TRANS|V_SNAPTOLEFT  or player.ringfadetimer == 4 and V_80TRANS|V_SNAPTOLEFT  or V_SNAPTOLEFT ))
elseif player.ringanim == 1
v.drawScaled(16*FRACUNIT, 48*FRACUNIT, FRACUNIT/3, ring6, V_SNAPTOLEFT )
elseif player.ringanim == 2
v.drawScaled(16*FRACUNIT, 48*FRACUNIT, FRACUNIT/3, ring2, V_SNAPTOLEFT )
elseif player.ringanim == 3
v.drawScaled(16*FRACUNIT, 48*FRACUNIT, FRACUNIT/3, ring7, V_SNAPTOLEFT )
elseif player.ringanim == 4
v.drawScaled(16*FRACUNIT, 48*FRACUNIT, FRACUNIT/3, ring3, V_SNAPTOLEFT )
elseif player.ringanim == 5
v.drawScaled(16*FRACUNIT, 48*FRACUNIT, FRACUNIT/3, ring8, V_SNAPTOLEFT )
elseif player.ringanim == 6
v.drawScaled(16*FRACUNIT, 48*FRACUNIT, FRACUNIT/3, ring4, V_SNAPTOLEFT )
elseif player.ringanim == 7
v.drawScaled(16*FRACUNIT, 48*FRACUNIT, FRACUNIT/3, ring9, V_SNAPTOLEFT )
elseif player.ringanim == 8
v.drawScaled(16*FRACUNIT, 48*FRACUNIT, FRACUNIT/3, ring5, V_SNAPTOLEFT )
elseif player.ringanim == 9
v.drawScaled(16*FRACUNIT, 48*FRACUNIT, FRACUNIT/3, ring9, V_FLIP|V_SNAPTOLEFT )
elseif player.ringanim == 10
v.drawScaled(16*FRACUNIT, 48*FRACUNIT, FRACUNIT/3, ring4, V_FLIP|V_SNAPTOLEFT )
elseif player.ringanim == 11
v.drawScaled(16*FRACUNIT, 48*FRACUNIT, FRACUNIT/3, ring8, V_FLIP|V_SNAPTOLEFT )
elseif player.ringanim == 12
v.drawScaled(16*FRACUNIT, 48*FRACUNIT, FRACUNIT/3, ring3, V_FLIP|V_SNAPTOLEFT )
elseif player.ringanim == 13
v.drawScaled(16*FRACUNIT, 48*FRACUNIT, FRACUNIT/3, ring7, V_FLIP|V_SNAPTOLEFT )
elseif player.ringanim == 14
v.drawScaled(16*FRACUNIT, 48*FRACUNIT, FRACUNIT/3, ring2, V_FLIP|V_SNAPTOLEFT )
elseif player.ringanim == 15
v.drawScaled(16*FRACUNIT, 48*FRACUNIT, FRACUNIT/3, ring6, V_FLIP|V_SNAPTOLEFT )
end
else
v.drawScaled((16 - player.ringoffset)*FRACUNIT, (48 - player.ringoffset)*FRACUNIT, FRACUNIT/3, ring1, (player.ringoffset == 3 and V_60TRANS|V_SNAPTOLEFT  or player.ringoffset == 2 and V_40TRANS|V_SNAPTOLEFT  or player.ringoffset == 1 and V_20TRANS|V_SNAPTOLEFT  or player.ringfadetimer == 1 and V_20TRANS|V_SNAPTOLEFT  or player.ringfadetimer == 2 and V_40TRANS|V_SNAPTOLEFT  or player.ringfadetimer == 3 and V_60TRANS|V_SNAPTOLEFT  or player.ringfadetimer == 4 and V_80TRANS|V_SNAPTOLEFT  or V_SNAPTOLEFT ))
end end
//actual number counter
 local h = player.rings
 V_DrawLWNum(v,(h > 999 and (69 - player.ringoffset) or (59 - player.ringoffset)),
                (42 - player.ringoffset),
                (h >= 0 and h or 0),
                (h > 999 and 4 or 3),
				(player.ringoffset == 3 and V_60TRANS|V_SNAPTOLEFT  or player.ringoffset == 2 and V_40TRANS|V_SNAPTOLEFT  or player.ringoffset == 1 and V_20TRANS|V_SNAPTOLEFT  or player.ringfadetimer == 1 and V_20TRANS|V_SNAPTOLEFT  or player.ringfadetimer == 2 and V_40TRANS|V_SNAPTOLEFT  or player.ringfadetimer == 3 and V_60TRANS|V_SNAPTOLEFT  or player.ringfadetimer == 4 and V_80TRANS|V_SNAPTOLEFT  or V_SNAPTOLEFT )
,nil,player.skincolor)
if player.rings < 1 then
 local f = V_SNAPTOLEFT
 if (player.redflash < 1)
	f=f|V_90TRANS
 elseif (player.redflash < 3)
	f=f|V_80TRANS
 elseif (player.redflash < 5)
	f=f|V_70TRANS
 elseif (player.redflash < 7)
	f=f|V_60TRANS
 elseif (player.redflash < 9)
	f=f|V_50TRANS
 elseif (player.redflash < 11)
	f=f|V_40TRANS
 elseif (player.redflash < 13)
	f=f|V_30TRANS
 elseif (player.redflash < 15)
	f=f|V_20TRANS
 elseif (player.redflash > 17 and player.redflash < 37)
	f=f|V_10TRANS
 elseif (player.redflash < 39)
	f=f|V_20TRANS
 elseif (player.redflash < 41)
	f=f|V_30TRANS
 elseif (player.redflash < 43)
	f=f|V_40TRANS
 elseif (player.redflash < 45)
	f=f|V_50TRANS
 elseif (player.redflash < 47)
	f=f|V_60TRANS
 elseif (player.redflash < 49)
	f=f|V_70TRANS
 elseif (player.redflash < 51)
	f=f|V_80TRANS
 elseif (player.redflash < 54)
	f=f|V_90TRANS
 end
 V_DrawLWNum(v,(h > 999 and (69 - player.ringoffset) or (59 - player.ringoffset)),
                (42 - player.ringoffset),
                (h >= 0 and h or 0),
                (h > 999 and 4 or 3),
				f
,nil,player.skincolor,true)
 
end end end, "game")

addHook("ThinkFrame", do
    for player in players.iterate do
		if player.flicktimer == 140 then player.flickoffset = 3 end
		if player.flicktimer == 139 then player.flickoffset = 2 end
		if player.flicktimer == 138 then player.flickoffset = 1 end
		if player.flicktimer < 137 then player.flickoffset = 0 end
		if player.flicktimer == 38 then player.flickfadetimer = 1 end
		if player.flicktimer == 37 then player.flickfadetimer = 2 end
		if player.flicktimer == 36 then player.flickfadetimer = 3 end
		if player.flicktimer == 35 then player.flickfadetimer = 4 end
		if player.flicktimer > 38 then player.flickfadetimer = 0 end
		end
		end)
		
	addHook("ThinkFrame", do
    for player in players.iterate do
		if player.lifetimer == 140 then player.lifeoffset = 3 end
		if player.lifetimer == 139 then player.lifeoffset = 2 end
		if player.lifetimer == 138 then player.lifeoffset = 1 end
		if player.lifetimer < 137 then player.lifeoffset = 0 end
		if player.lifetimer == 38 then player.lifefadetimer = 1 end
		if player.lifetimer == 37 then player.lifefadetimer = 2 end
		if player.lifetimer == 36 then player.lifefadetimer = 3 end
		if player.lifetimer == 35 then player.lifefadetimer = 4 end
		if player.lifetimer > 38 then player.lifefadetimer = 0 end
		end
		end)
		



addHook("ThinkFrame", do
    for player in players.iterate do
		if player.combo == nil then player.combo = 0 end
	local mo = player.mo
	
		if P_IsObjectOnGround(mo)
		mo.player.combo = 1
		end
		end
		end)

addHook("ThinkFrame", function()
	for player in players.iterate do
		if (player.cmd.buttons & BT_TOSSFLAG) then
				 if player.ringtimer < 3*TICRATE then player.ringtimer = (player.ringtimer < 35 and 4 or 3)*TICRATE end
				 if player.scoretimer < 3*TICRATE then player.scoretimer = (player.scoretimer < 35 and 4 or 3)*TICRATE end
				 if player.timetimer < 3*TICRATE then player.timetimer = (player.timetimer < 35 and 4 or 3)*TICRATE end
				 if player.flicktimer < 3*TICRATE then player.flicktimer = (player.flicktimer < 35 and 4 or 3)*TICRATE end
				 if player.lifetimer < 3*TICRATE then player.lifetimer = (player.lifetimer < 35 and 4 or 3)*TICRATE end
		end
	end
end)

local function robotded(mo, inflictor)
    
    if inflictor and inflictor.valid and inflictor.type == MT_PLAYER and inflictor.player then
        
        if
            inflictor.player.animals_freed == nil then inflictor.player.animals_freed = 0
        end    
        
        inflictor.player.animals_freed = $1 + (1 * inflictor.player.combo)
		inflictor.player.combo = $1 + 1
		inflictor.player.flicktimer = 4*TICRATE
    end    
end

do
    for i = 1,#mobjinfo-1 do
        if mobjinfo[i].flags & MF_ENEMY then
            addHook("MobjDeath", robotded, i)
        end
    end
end

//Timer
hud.add(function(v, player)
if player and player.valid and player.mo and player.mo.valid and not (player.powers[pw_carry] == CR_NIGHTSMODE) then
if player.timetimer < 1*TICRATE
return
else
local timeicon = v.cachePatch("SBOTIMER")
v.drawScaled((6 - player.timeoffset)*FRACUNIT, (18 - player.timeoffset)*FRACUNIT, FRACUNIT/3, timeicon, (player.timeoffset == 3 and V_60TRANS|V_SNAPTOLEFT  or player.timeoffset == 2 and V_40TRANS|V_SNAPTOLEFT  or player.timeoffset == 1 and V_20TRANS|V_SNAPTOLEFT  or player.timefadetimer == 1 and V_20TRANS|V_SNAPTOLEFT  or player.timefadetimer == 2 and V_40TRANS|V_SNAPTOLEFT  or player.timefadetimer == 3 and V_60TRANS|V_SNAPTOLEFT  or player.timefadetimer == 4 and V_80TRANS|V_SNAPTOLEFT  or V_SNAPTOLEFT ))
 local t = player.realtime/TICRATE
V_DrawLWNum(v,(t > 999 and 69 - player.timeoffset or 59 - player.timeoffset),
                23 - player.timeoffset,
                (t >= 0 and t or 0),
                (t > 999 and 4 or 3),
				(player.timefadetimer == 1 and V_20TRANS|V_SNAPTOLEFT  or player.timefadetimer == 2 and V_40TRANS|V_SNAPTOLEFT  or player.timefadetimer == 3 and V_60TRANS|V_SNAPTOLEFT  or player.timefadetimer == 4 and V_80TRANS|V_SNAPTOLEFT or player.timeoffset == 3 and V_60TRANS|V_SNAPTOLEFT  or player.timeoffset == 2 and V_40TRANS|V_SNAPTOLEFT  or player.timeoffset == 1 and V_20TRANS|V_SNAPTOLEFT or V_SNAPTOLEFT )
,nil,player.skincolor)
end end end, "game")

//Life counter
hud.add(function(v, player)
if player and player.valid and player.mo and player.mo.valid and not (player.powers[pw_carry] == CR_NIGHTSMODE) then
if player.lifetimer < 1*TICRATE
return
else
local lifeicon = v.cachePatch("SBOLIFE")
v.drawScaled((6 - player.lifeoffset)*FRACUNIT, (55 - player.lifeoffset)*FRACUNIT, FRACUNIT/3, lifeicon, (player.lifefadetimer == 1 and V_20TRANS|V_SNAPTOLEFT  or player.lifefadetimer == 2 and V_40TRANS|V_SNAPTOLEFT  or player.lifefadetimer == 3 and V_60TRANS|V_SNAPTOLEFT  or player.lifefadetimer == 4 and V_80TRANS|V_SNAPTOLEFT  or player.lifeoffset == 3 and V_60TRANS|V_SNAPTOLEFT  or player.lifeoffset == 2 and V_40TRANS|V_SNAPTOLEFT  or player.lifeoffset == 1 and V_20TRANS|V_SNAPTOLEFT  or V_SNAPTOLEFT ))
 local f = player.lives
V_DrawLWNum(v,49 - player.lifeoffset,
                60 - player.lifeoffset,
                (f >= 0 and f or 0),
                2,
				(player.lifefadetimer == 1 and V_20TRANS|V_SNAPTOLEFT  or player.lifefadetimer == 2 and V_40TRANS|V_SNAPTOLEFT  or player.lifefadetimer == 3 and V_60TRANS|V_SNAPTOLEFT  or player.lifefadetimer == 4 and V_80TRANS|V_SNAPTOLEFT  or player.lifeoffset == 3 and V_60TRANS|V_SNAPTOLEFT  or player.lifeoffset == 2 and V_40TRANS|V_SNAPTOLEFT  or player.lifeoffset == 1 and V_20TRANS|V_SNAPTOLEFT  or V_SNAPTOLEFT )
,nil,player.skincolor)
end end end, "game")

addHook("TouchSpecial", function(special, toucher)
if special and special.valid and not (special.state==S_STARPOST_FLASH) and not (special.state==S_STARPOST_SPIN) and toucher and toucher.valid and toucher.player and toucher.player.valid then
	local player = toucher.player
player.timetimer = 4*TICRATE
end
end, MT_STARPOST)

//Flicky counter
hud.add(function(v, player)
if player and player.valid and player.mo and player.mo.valid and not (player.powers[pw_carry] == CR_NIGHTSMODE) then
if player.flicktimer < 35
return
else
local flickicon = v.cachePatch("SBOFLICK")
if player.flickoffset == 3
v.drawScaled(3*FRACUNIT, 71*FRACUNIT, FRACUNIT/3, flickicon, V_60TRANS|V_SNAPTOLEFT )
elseif player.flickoffset == 2
v.drawScaled(4*FRACUNIT, 72*FRACUNIT, FRACUNIT/3, flickicon, V_40TRANS|V_SNAPTOLEFT )
elseif player.flickoffset == 1
v.drawScaled(5*FRACUNIT, 73*FRACUNIT, FRACUNIT/3, flickicon, V_20TRANS|V_SNAPTOLEFT )
elseif player.flickoffset == 0
v.drawScaled(6*FRACUNIT, 74*FRACUNIT, FRACUNIT/3, flickicon, player.flickfadetimer == 1 and V_20TRANS|V_SNAPTOLEFT  or player.flickfadetimer == 2 and V_40TRANS|V_SNAPTOLEFT  or player.flickfadetimer == 3 and V_60TRANS|V_SNAPTOLEFT  or player.flickfadetimer == 4 and V_80TRANS|V_SNAPTOLEFT  or V_SNAPTOLEFT )
end
 local f = player.animals_freed
 if f == nil then f = 0 end
V_DrawLWNum(v,(69 - player.flickoffset),
                (78 - player.flickoffset),
                (f >= 0 and f or 0),
                4,
				(player.flickfadetimer == 1 and V_20TRANS|V_SNAPTOLEFT  or player.flickfadetimer == 2 and V_40TRANS|V_SNAPTOLEFT  or player.flickfadetimer == 3 and V_60TRANS|V_SNAPTOLEFT  or player.flickfadetimer == 4 and V_80TRANS|V_SNAPTOLEFT  or player.flickoffset == 3 and V_60TRANS|V_SNAPTOLEFT  or player.flickoffset == 2 and V_40TRANS|V_SNAPTOLEFT  or player.flickoffset == 1 and V_20TRANS|V_SNAPTOLEFT or V_SNAPTOLEFT),
				/*sonic
,*/nil,player.skincolor)
	end end end, "game")
	
	addHook("ThinkFrame", do
    for player in players.iterate do
		if player.scoretimer == 140 then player.scorefadetimer = 3 end
		if player.scoretimer == 139 then player.scorefadetimer = 2 end
		if player.scoretimer == 138 then player.scorefadetimer = 1 end
		if player.scoretimer < 137 then player.scorefadetimer = 0 end
		if player.scoretimer == 38 then player.scorefadetimer = 1 end
		if player.scoretimer == 37 then player.scorefadetimer = 2 end
		if player.scoretimer == 36 then player.scorefadetimer = 3 end
		if player.scoretimer == 35 then player.scorefadetimer = 4 end
		if player.scoretimer > 38 then player.scorefadetimer = 0 end
		end
		end)
		
//Score
hud.add(function(v, player)
if player and player.valid and player.mo and player.mo.valid and not (player.powers[pw_carry] == CR_NIGHTSMODE) then
if player.scoretimer < 1*TICRATE
return
else
local scoreicon = v.cachePatch("SBOSCORE")
v.drawScaled(105*FRACUNIT, (gametype == (GT_MATCH or GT_CTF) and 162 or 188)*FRACUNIT, FRACUNIT, scoreicon, (player.scorefadetimer == 1 and V_20TRANS|V_SNAPTOBOTTOM or player.scorefadetimer == 2 and V_40TRANS|V_SNAPTOBOTTOM or player.scorefadetimer == 3 and V_60TRANS|V_SNAPTOBOTTOM or player.scorefadetimer == 4 and V_80TRANS|V_SNAPTOBOTTOM or V_SNAPTOBOTTOM))
 local s = player.score
V_DrawLWNum(v,238,
                (gametype == (GT_MATCH or GT_CTF) and 162 or 188),
                (s >= 0 and s or 0),
                9,
				(player.scorefadetimer == 1 and V_20TRANS|V_SNAPTOBOTTOM or player.scorefadetimer == 2 and V_40TRANS|V_SNAPTOBOTTOM or player.scorefadetimer == 3 and V_60TRANS|V_SNAPTOBOTTOM or player.scorefadetimer == 4 and V_80TRANS|V_SNAPTOBOTTOM or V_SNAPTOBOTTOM)
,4,player.skincolor)
end end end, "game")

	hud.disable("rings")
	hud.disable("score")
	hud.disable("time")
	hud.disable("lives")

//ring sound engine
local sound_engines = {
MT_RING,
MT_REDTEAMRING,
MT_BLUETEAMRING,
MT_FLINGRING,
MT_AUTOMATICRING,
MT_BOUNCERING,
MT_SCATTERRING,
MT_EXPLOSIONRING,
MT_GRENADERING,
MT_RAILRING,
MT_INFINITYRING

}

local function ringEngine(special, toucher)
    if special and special.valid and toucher and toucher.valid and toucher.player and toucher.player.valid then
        local player = toucher.player
        mobjinfo[special.type].deathsound = player.ringseq == 0 and sfx_itemu1 or sfx_itemu2
        player.ringseq = $ + 1 //increase the sequence by 1
    else
        mobjinfo[special.type].deathsound = sfx_itemup
    end
end

for i = 1,#sound_engines do
    addHook("TouchSpecial", ringEngine, sound_engines[i])
end