--RALLYHUD by Eldog

/* Hud fade/slide vars */
local huddisplaytimemax = -1
local hudfadeintime = 5 -- Time (in tics) it takes for the HUD to fade in
local hudfadeouttime = 10 -- Time (in tics) it takes for the HUD to fade out

local hudspacingx = (320/(100/7)) -- Percentage of HUD apart patches should be
local hudspacingy = (160/(100/10)) -- Percentage of HUD patches should be from the top of the screen
local hudpatchsize = 32
local encoreoffset = 0

local function SetDrawPatches(line, mobj, sector)
	if (line.backside and line.backside.valid) then
		if (mobj.player and mobj.player.valid) then
			mobj.player.huddisplaytime = (line.backside.textureoffset/FRACUNIT)
			mobj.player.huddisplaytimemax = (line.backside.textureoffset/FRACUNIT)
			mobj.player.drawpatch1name = string.match(line.backside.text, "(%w+)_")
			mobj.player.drawpatch2name = string.match(line.backside.text, "_(%w+)")
		end
	else
		print("ERROR: Sector has no back side.")
	end
end -- Close function

local function HUDTimerDecrement()
	for player in players.iterate
		if (player.huddisplaytime and player.huddisplaytime >= 0) then
			player.huddisplaytime = $ - 1
		elseif (player.huddisplaytime and player.huddisplaytime == 0) then
			player.huddisplaytimemax = -1
			player.drawpatch1 = nil
			player.drawpatch2 = nil
		end
	end
end -- Close function

/* HUD function (((VERY FUCKING SCARY))) */
local function RenderPatches(v, player)
	if (player.huddisplaytime and player.huddisplaytime > 0) then
		local hudfadeinoutflags = V_10TRANS * (max(player.huddisplaytime - (player.huddisplaytimemax - hudfadeintime), 0) + (hudfadeouttime - min(max(player.huddisplaytime, 0), hudfadeouttime))) -- Set transparency flags in quick succession for fade in/out effect
		local hudflipflags = 0
		local hudslideinout = max(player.huddisplaytime - (player.huddisplaytimemax - hudfadeintime), 0) + (min(player.huddisplaytime, hudfadeouttime) - hudfadeouttime) -- Manipulate huddisplaytime to fade in and out linearly
		if(encoremode == true) then
			hudflipflags = V_FLIP
			hudslideinout = $ * -1
			encoreoffset = hudpatchsize
		end
		if (player.drawpatch1name and not player.drawpatch2name) then -- Only one patch specified
			local drawpatch1 = v.cachePatch(player.drawpatch1name)
			v.drawScaled((160 + encoreoffset - (hudpatchsize/2) + hudslideinout)*FRACUNIT, hudspacingy*FRACUNIT, FixedDiv(hudpatchsize, drawpatch1.width), drawpatch1, V_SNAPTOTOP|hudfadeinoutflags|hudflipflags)
		elseif(player.drawpatch1name and player.drawpatch2name) then -- Two patches specified
			local drawpatch1 = v.cachePatch(player.drawpatch1name)
			local drawpatch2 = v.cachePatch(player.drawpatch2name)
			v.drawScaled((160 + encoreoffset - (hudpatchsize/2) - hudspacingx + hudslideinout)*FRACUNIT, hudspacingy*FRACUNIT, FixedDiv(hudpatchsize, drawpatch1.width), drawpatch1, V_SNAPTOTOP|hudfadeinoutflags|hudflipflags)
			v.drawScaled((160 + encoreoffset - (hudpatchsize/2) + hudspacingx + hudslideinout)*FRACUNIT, hudspacingy*FRACUNIT, FixedDiv(hudpatchsize, drawpatch2.width), drawpatch2, V_SNAPTOTOP|hudfadeinoutflags|hudflipflags)
		end
	end
end -- Close function

addHook("ThinkFrame", 		HUDTimerDecrement)
addHook("LinedefExecute",	SetDrawPatches, "RALLYSIGNAL")
hud.add(RenderPatches,	"game")
