-- comedy of the HUD variety
local cooplives = CV_FindVar("cooplives")
local function TextPlacement(v,p)
	local screen_width = (v.width() / v.dupx());
	local screen_xcenter = ((screen_width - (screen_width - 320))/2);
	local RKNum = 0
	local Flags = V_SNAPTOTOP|V_PERPLAYER
	local Hori = 0
	local Vert = 0
	local Kern = 12
	local RKCountdown = (mapheaderinfo[gamemap].countdown * TICRATE)
	for i = ((p.lives == 127) and 2 or 1), 4
		if i == 1 -- Lives
			if p.lives == 127 or (multiplayer and cooplives and cooplives.value == 0) or not G_GametypeUsesLives() then continue end
			Hori = 56
			Vert = 6
			RKNum = p.lives
			Flags = V_SNAPTOLEFT
		elseif i == 2 -- Rings
			if gametype ~= GT_CHAOS
				Hori = screen_xcenter + 28
				Vert = 6
				Flags = V_SNAPTORIGHT
			else
				Hori = 56
				Vert = 24
				Flags = V_SNAPTOLEFT
			end
			RKNum = p.rings
		elseif i == 3 -- Timer
			if gametype == GT_CHAOS then continue end
			Hori = screen_xcenter + 54
			Vert = 32
			if (mapheaderinfo[gamemap].countdown > 0)
				RKNum = string.format("%d;%02d:%02d", G_TicsToMinutes(RKCountdown - leveltime, true), G_TicsToSeconds(RKCountdown - leveltime), G_TicsToCentiseconds(RKCountdown - leveltime))
			else
				RKNum = string.format("%d;%02d:%02d", G_TicsToMinutes(p.realtime, true), G_TicsToSeconds(p.realtime), G_TicsToCentiseconds(p.realtime))
			end
			Flags = V_SNAPTORIGHT
		elseif i == 4 -- Star counter
			if gametype ~= GT_CHAOS
				Hori = screen_xcenter + 112
				Vert = 6
				Flags = V_SNAPTORIGHT
			else
				Hori = 56
				Vert = 42
				Flags = V_SNAPTOLEFT
			end
			RKNum = 6*p.powers[pw_invulnerability]/TICRATE
		end
		RKNum = tostring(RKNum)
		for Byte = 1, #RKNum
			v.draw(Hori, Vert, v.cachePatch( string.format("RKYNUM".."%03d", RKNum:byte(Byte,Byte)) ), Flags|V_HUDTRANS|V_SNAPTOTOP|V_PERPLAYER);
			Hori = $+Kern
		end
	end		
end
local function rkyHealthHud(v, p)
	local screen_width = (v.width() / v.dupx());
	local screen_xcenter = ((screen_width - (screen_width - 320))/2);
    local phealth = 0 + max(0, ((p.rkyDisplayHealth ~= nil) and (p.rkyDisplayHealth/272) or 8));
    phealth = min($, 8);
	if not (p.playerstate == PST_DEAD or p.rk_deadtime) and (p.rkyDisplayHealth ~= nil) and (p.rkyDisplayHealth < 272)
		phealth = 1
	end
	if (p.playerstate == PST_REBORN)
		phealth = 0
	end
	if (p.playerstate == PST_DEAD or p.rk_deadtime) and (p.rkyDisplayHealth ~= nil) and (p.rkyDisplayHealth < 272)
		phealth = 0; 
	end
	if (p.playerstate == PST_DEAD or p.rk_deadtime) and (ultimatemode)
		phealth = 0; 
	end
	
	if G_PlatformGametype() and not ((splitscreen) or G_RingSlingerGametype() or (CBW_Battle) or G_IsSpecialStage(gamemap) or (maptol & TOL_NIGHTS) or (OLDC and (gamemap == OLDC.hub)) or rawget(_G, "customhud"))
		if hud.enabled("rings")
			hud.disable("rings")
		end
		if hud.enabled("time")
			hud.disable("time")
		end
		if hud.enabled("score")
			hud.disable("score")
		end
	end
	
	local ultmodecheck = (ultimatemode and (p.mo and p.mo.valid) and (not (p.mo.eflags & MFE_UNDERWATER)) and (p.playerstate ~= PST_DEAD));
	if p.mo.skin == "n64mario" then
		if not ((splitscreen) or G_RingSlingerGametype() or (CBW_Battle) or gametype == GT_CHAOS or (OLDC and (gamemap == OLDC.hub)) or rawget(_G, "customhud")) then v.draw(screen_xcenter - 32, 0 + min(32, ((p.rky_damagetime) and p.rky_damagetime or 0)) - min(64, (max(0, ((p.rky_maxhtime) and (p.rky_maxhtime-(TICRATE*2)) or 0))*4)), ((ultmodecheck) and v.cachePatch("RKUMP") or v.cachePatch("RKPW"..phealth)), V_HUDTRANS|V_SNAPTOTOP);
		elseif (splitscreen) then v.draw(screen_xcenter + 64, 0, ((ultmodecheck) and v.cachePatch("RKUMP") or v.cachePatch("RKPW"..phealth)), V_HUDTRANS|V_SNAPTORIGHT|V_SNAPTOTOP|V_PERPLAYER);
		elseif (gametype == GT_CHAOS) then v.draw(screen_xcenter + 48, 0 + min(32, ((p.rky_damagetime) and p.rky_damagetime or 0)) - min(64, (max(0, ((p.rky_maxhtime) and (p.rky_maxhtime-(TICRATE*2)) or 0))*4)), v.cachePatch("RKPW"..phealth), V_HUDTRANS|V_SNAPTOTOP);
		elseif G_RingSlingerGametype() or (CBW_Battle) then v.draw(screen_xcenter + 64, 0 + min(32, ((p.rky_damagetime) and p.rky_damagetime or 0)) - min(64, (max(0, ((p.rky_maxhtime) and (p.rky_maxhtime-(TICRATE*2)) or 0))*4)), ((ultmodecheck) and v.cachePatch("RKUMP") or v.cachePatch("RKPW"..phealth)), V_HUDTRANS|V_SNAPTOTOP);
		elseif (OLDC and (gamemap == OLDC.hub)) or rawget(_G, "customhud") then v.draw(screen_xcenter, 0 + min(32, ((p.rky_damagetime) and p.rky_damagetime or 0)) - min(64, (max(0, ((p.rky_maxhtime) and (p.rky_maxhtime-(TICRATE*2)) or 0))*4)), ((ultmodecheck) and v.cachePatch("RKUMP") or v.cachePatch("RKPW"..phealth)), V_HUDTRANS|V_SNAPTOTOP);
		end
		if (G_GametypeUsesLives() and G_PlatformGametype()) and not ((splitscreen) or (CBW_Battle) or (OLDC and (gamemap == OLDC.hub)) or rawget(_G, "customhud"))
			if p.powers[pw_super] and (p.playerstate ~= PST_DEAD)
				if p.mo.colorized == true then
					v.draw(24, 6, v.cachePatch("RKMMINVHUD"), V_HUDTRANS|V_SNAPTOLEFT|V_SNAPTOTOP|V_PERPLAYER, v.getColormap(p.mo.skin, p.mo.color));
				else
					v.draw(24, 6, v.cachePatch("RKMMHUD"), V_HUDTRANS|V_SNAPTOLEFT|V_SNAPTOTOP|V_PERPLAYER, v.getColormap(p.mo.skin, p.skincolor));
				end
			else
				if p.mo.colorized == true then
					v.draw(24, 6, v.cachePatch("RKMINVHUD"), V_HUDTRANS|V_SNAPTOLEFT|V_SNAPTOTOP|V_PERPLAYER, v.getColormap(p.mo.skin, p.mo.color));
				else
					v.draw(24, 6, v.cachePatch("RKMHUD"), V_HUDTRANS|V_SNAPTOLEFT|V_SNAPTOTOP|V_PERPLAYER, v.getColormap(p.mo.skin, p.skincolor));
				end
			end
			v.draw(40, 6, v.cachePatch("RKYX"), V_HUDTRANS|V_SNAPTOLEFT|V_SNAPTOTOP|V_PERPLAYER);
			if p.lives == 127 or (multiplayer and cooplives and cooplives.value == 0)
				v.draw(56, 6, v.cachePatch("INFHUD"), V_HUDTRANS|V_SNAPTOLEFT|V_SNAPTOTOP|V_PERPLAYER);
			end
			if not (splitscreen) and p == consoleplayer
				if hud.enabled("lives")
					hud.disable("lives")
				end
			end
		end
	elseif p.mo.skin == "n64luigi" then
		if not ((splitscreen) or  G_RingSlingerGametype() or (CBW_Battle) or gametype == GT_CHAOS or (OLDC and (gamemap == OLDC.hub)) or rawget(_G, "customhud")) then v.draw(screen_xcenter - 32, 0 + min(32, ((p.rky_damagetime) and p.rky_damagetime or 0)) - min(64, (max(0, ((p.rky_maxhtime) and (p.rky_maxhtime-(TICRATE*2)) or 0))*4)), ((ultmodecheck) and v.cachePatch("RLUMP") or v.cachePatch("RLPW"..phealth)), V_HUDTRANS|V_SNAPTOTOP);
		elseif (splitscreen) then v.draw(screen_xcenter + 64, 0, ((ultmodecheck) and v.cachePatch("RLUMP") or v.cachePatch("RLPW"..phealth)), V_HUDTRANS|V_SNAPTORIGHT|V_SNAPTOTOP|V_PERPLAYER);
		elseif (gametype == GT_CHAOS) then v.draw(screen_xcenter + 48, 0 + min(32, ((p.rky_damagetime) and p.rky_damagetime or 0)) - min(64, (max(0, ((p.rky_maxhtime) and (p.rky_maxhtime-(TICRATE*2)) or 0))*4)), v.cachePatch("RLPW"..phealth), V_HUDTRANS|V_SNAPTOTOP);
		elseif  G_RingSlingerGametype() or (CBW_Battle) then v.draw(screen_xcenter + 64, 0 + min(32, ((p.rky_damagetime) and p.rky_damagetime or 0)) - min(64, (max(0, ((p.rky_maxhtime) and (p.rky_maxhtime-(TICRATE*2)) or 0))*4)), ((ultmodecheck) and v.cachePatch("RLUMP") or v.cachePatch("RLPW"..phealth)), V_HUDTRANS|V_SNAPTOTOP);
		elseif (OLDC and (gamemap == OLDC.hub)) or rawget(_G, "customhud") then v.draw(screen_xcenter, 0 + min(32, ((p.rky_damagetime) and p.rky_damagetime or 0)) - min(64, (max(0, ((p.rky_maxhtime) and (p.rky_maxhtime-(TICRATE*2)) or 0))*4)), ((ultmodecheck) and v.cachePatch("RLUMP") or v.cachePatch("RLPW"..phealth)), V_HUDTRANS|V_SNAPTOTOP);
		end
		if (G_GametypeUsesLives() and G_PlatformGametype()) and not ((splitscreen) or (CBW_Battle) or (OLDC and (gamemap == OLDC.hub)) or rawget(_G, "customhud"))
			if p.powers[pw_super] and (p.playerstate ~= PST_DEAD)
				if p.mo.colorized == true then
					v.draw(24, 6, v.cachePatch("RKLINVHUD"), V_HUDTRANSHALF|V_SNAPTOLEFT|V_SNAPTOTOP|V_PERPLAYER, v.getColormap(p.mo.skin, p.mo.color));
				else
					v.draw(24, 6, v.cachePatch("RKLHUD"), V_HUDTRANSHALF|V_SNAPTOLEFT|V_SNAPTOTOP|V_PERPLAYER, v.getColormap(p.mo.skin, p.skincolor));
				end
			else
				if p.mo.colorized == true then
					v.draw(24, 6, v.cachePatch("RKLINVHUD"), V_HUDTRANS|V_SNAPTOLEFT|V_SNAPTOTOP|V_PERPLAYER, v.getColormap(p.mo.skin, p.mo.color));
				else
					v.draw(24, 6, v.cachePatch("RKLHUD"), V_HUDTRANS|V_SNAPTOLEFT|V_SNAPTOTOP|V_PERPLAYER, v.getColormap(p.mo.skin, p.skincolor));
				end
			end
			v.draw(40, 6, v.cachePatch("RKYX"), V_HUDTRANS|V_SNAPTOLEFT|V_SNAPTOTOP|V_PERPLAYER);
			if p.lives == 127 or (multiplayer and cooplives and cooplives.value == 0)
				v.draw(56, 6, v.cachePatch("INFHUD"), V_HUDTRANS|V_SNAPTOLEFT|V_SNAPTOTOP|V_PERPLAYER);
			end
			if not (splitscreen) and p == consoleplayer
				if hud.enabled("lives")
					hud.disable("lives")
				end
			end
		end
	end
	if G_PlatformGametype() and not ((splitscreen) or G_RingSlingerGametype() or (CBW_Battle) or G_IsSpecialStage(gamemap) or (maptol & TOL_NIGHTS) or (OLDC and (gamemap == OLDC.hub)) or rawget(_G, "customhud"))
		if not (multiplayer)
			v.draw(screen_xcenter - 4, 6,  v.cachePatch("COINHUD"), V_HUDTRANS|V_SNAPTORIGHT|V_SNAPTOTOP|V_PERPLAYER);
			v.draw(screen_xcenter + 12, 6, v.cachePatch("RKYX"), V_HUDTRANS|V_SNAPTORIGHT|V_SNAPTOTOP|V_PERPLAYER);
		else
			if gametype ~= GT_CHAOS
				v.draw(screen_xcenter - 4, 6,  v.cachePatch("RINGHUD"), V_HUDTRANS|V_SNAPTORIGHT|V_SNAPTOTOP|V_PERPLAYER);
				v.draw(screen_xcenter + 12, 6, v.cachePatch("RKYX"), V_HUDTRANS|V_SNAPTORIGHT|V_SNAPTOTOP|V_PERPLAYER);
			else
				v.draw(24, 24,  v.cachePatch("RINGHUD"), V_HUDTRANS|V_SNAPTOLEFT|V_SNAPTOTOP|V_PERPLAYER);
				v.draw(40, 24, v.cachePatch("RKYX"), V_HUDTRANS|V_SNAPTOLEFT|V_SNAPTOTOP|V_PERPLAYER);
			end
		end
		if gametype ~= GT_CHAOS
			v.draw(screen_xcenter + 80, 6,  v.cachePatch("STARHUD"), V_HUDTRANS|V_SNAPTORIGHT|V_SNAPTOTOP|V_PERPLAYER);
			v.draw(screen_xcenter + 96, 6,  v.cachePatch("RKYX"), V_HUDTRANS|V_SNAPTORIGHT|V_SNAPTOTOP|V_PERPLAYER);
			v.draw(screen_xcenter - 4, 32,  v.cachePatch("TIME"), V_HUDTRANS|V_SNAPTORIGHT|V_SNAPTOTOP|V_PERPLAYER);
		else
			v.draw(24, 42,  v.cachePatch("STARHUD"), V_HUDTRANS|V_SNAPTOLEFT|V_SNAPTOTOP|V_PERPLAYER);
			v.draw(40, 42, v.cachePatch("RKYX"), V_HUDTRANS|V_SNAPTOLEFT|V_SNAPTOTOP|V_PERPLAYER);
		end
		TextPlacement(v, p)
	end
end


local function n64MarioHud(v, p)
    if (p.mo and p.mo.valid and (p.mo.skin == "n64mario" or p.mo.skin == "n64luigi") and (not (p.nightstime or p.rk_dontusehealth)))
		rkyHealthHud(v, p);
	else
		if not (OLDC and (gamemap == OLDC.hub))
			if not hud.enabled("rings")
				hud.enable("rings")
			end
			if not hud.enabled("time")
				hud.enable("time")
			end
			if not hud.enabled("score")
				hud.enable("score")
			end
			if not hud.enabled("lives")
				hud.enable("lives")
			end
		end
	end
end
hud.add(n64MarioHud, game)