if not customhubhud
	rawset(_G, "customhubhud", {})
end

local modname = "customhubhud"
local altmodname = "vanilla"

customhubhud["modernsonic"] = true
customhubhud["werehog"] = true
customhubhud["takis"] = true

local function hubworldhud(v, player)
	if player.mo and not customhubhud[player.mo.skin] and not mapheaderinfo[gamemap].hubworld
		if customhud.enabled("hubhud")
			customhud.SetupItem("score", altmodname)
			customhud.SetupItem("time", altmodname)
			customhud.SetupItem("rings", altmodname)
			customhud.SetupItem("stagetitle", altmodname)
			customhud.SetupItem("lives", altmodname)
			customhud.disable("hubhud")
		end
	elseif player.mo and mapheaderinfo[gamemap].hubworld and not customhubhud[player.mo.skin]
		customhud.enable("hubhud")
		customhud.SetupItem("score", modname)
		customhud.SetupItem("time", modname)
		customhud.SetupItem("rings", modname)
		customhud.SetupItem("lives", modname)
		
		
			local hubhudx = 0
			local hubhudstartx = 0
			local hubhuddestx = -100
			
			
			local p_hud1 = v.cachePatch("HUB1")
			local p_hud2 = v.cachePatch("HUB2")
			local p_lifehud = v.getSprite2Patch(player.mo.skin, SPR2_XTRA, false, A)
			
			
			local hubendtime = 1*TICRATE
			local hubtime = min(player.hubtimer, hubendtime)
				
				
			hubhudx = ease.inquint(FixedDiv(hubtime, hubendtime), hubhudstartx, hubhuddestx)
				
			v.drawScaled(0+(hubhudx*FRACUNIT), 0, FRACUNIT, p_hud1, V_PERPLAYER|V_HUDTRANS|V_SNAPTOTOP|V_SNAPTOLEFT, v.getColormap(player.mo.skin, player.mo.color))
			v.drawScaled(0+(hubhudx*FRACUNIT), 177, FRACUNIT, p_hud2, V_PERPLAYER|V_HUDTRANS|V_SNAPTOLEFT|V_SNAPTOBOTTOM, v.getColormap(player.mo.skin, player.mo.color))
			v.drawScaled(4*FRACUNIT+(hubhudx*FRACUNIT), 179*FRACUNIT, FRACUNIT/2, p_lifehud, V_PERPLAYER|V_HUDTRANS|V_SNAPTOLEFT|V_SNAPTOBOTTOM, v.getColormap(player.mo.skin, player.mo.color))
			v.drawString(16+(hubhudx), 7, " : "+(player.rings), V_PERPLAYER|V_HUDTRANS|V_SNAPTOTOP|V_SNAPTOLEFT)
			v.drawString(22+(hubhudx), 182, " : "+(player.lives), V_PERPLAYER|V_HUDTRANS|V_SNAPTOLEFT|V_SNAPTOBOTTOM)
	end
end

local fademax = 31/2
local endtime = 1*TICRATE

addHook("PlayerThink", function(p) -- Player Hud Code
	if not p.selectingstage
		if p.stagetimer and p.stagetimer > 0
			p.stagetimer = $-1
		end
		if p.fadetimer and p.fadetimer > 0
			p.fadetimer = $-1
		end
		if p.hubtimer and p.hubtimer > 0
			p.hubtimer = $-1
		end
	else
		if p.hubtimer < endtime
			p.hubtimer = $+1
		end
		if p.stagetimer < endtime
			p.stagetimer = $+1
		end
		if p.fadetimer < fademax
			p.fadetimer = $+1
		end
	end
end)

local function stageselect(v, p)
	
	if p and p.valid
				
				local map = 1
				
				if p.mapselected
					map = p.mapselected
				end
				
				local mapfunnyname = G_BuildMapName(map)
				
				local endtime = 1*TICRATE
				
				
				//Text
				local text1 = "Stage Select"
				local text1height = v.levelTitleHeight(text1)
				
				//Map
				local mapname = "BALLER" -- Placeholder if there is no lvlttl
				
				if mapheaderinfo[map].lvlttl
					mapname = mapheaderinfo[map].lvlttl
				end
				
				local mapzone = " Zone"
				
				if mapheaderinfo[map].levelflags & LF_NOZONE
					mapzone = ""
				end
				
				local mapact = "Stage Stats" -- General Name
		
				if mapheaderinfo[map].actnum != 0 -- Act Num
					mapact = "Act "+"\130"+mapheaderinfo[map].actnum
				end
				
				if mapheaderinfo[map].levelflags & LF_WARNINGTITLE -- BOSS
					mapact = "\133Boss Act"
				end
				
				//Select
				local stage1 = v.cachePatch("STAGSLCT1")
				local stage2 = v.cachePatch("STAGSLCT2")
				local stage3 = v.cachePatch("STAGSLCT3")
				
				local prompt1 = "Jump : \131Accept"
				local prompt2 = "Spin : \133Cancel"
				
				local mapicon = v.cachePatch(mapfunnyname+"P")
				
				local imnothere = v.cachePatch("MISSING")
				
				
				local stage1x = -500 -- This stops "artifacting" as described by luigi budd
				local stage2x = -500
				local stage3x = -500
				
				local stage1startx = -500
				local stage2startx = -500
				local stage3startx = -500
				
				local stage1destx = 0
				local stage2destx = 20
				local stage3destx = 20
				
				//Timer
				local stage1time = min(p.stagetimer, endtime)
				
				//Movement
				stage1x = ease.outquint(FixedDiv(stage1time, endtime), stage1startx, stage1destx)
				stage2x = ease.outquint(FixedDiv(stage1time, endtime), stage2startx, stage2destx)
				stage3x = ease.outquint(FixedDiv(stage1time, endtime), stage3startx, stage3destx)
				
				v.fadeScreen(0xFF00, p.fadetimer)
				
				//Drawing
				v.drawScaled(stage1x*FRACUNIT, 10*FRACUNIT, FRACUNIT, stage1, V_PERPLAYER|V_HUDTRANS|V_SNAPTOTOP|V_SNAPTOLEFT)
				v.drawLevelTitle((stage1x+10), 10+text1height, text1, V_PERPLAYER|V_HUDTRANS|V_SNAPTOTOP|V_SNAPTOLEFT)
				v.drawString(stage3x+48, 184, prompt1, V_PERPLAYER|V_HUDTRANS, "left")
				v.drawString(stage3x+239, 184, prompt2, V_PERPLAYER|V_HUDTRANS, "right")
				v.drawScaled(stage2x*FRACUNIT, 0, FRACUNIT, stage2, V_PERPLAYER|V_HUDTRANS)
				v.drawString((stage2x+144), 68, mapname+mapzone, V_PERPLAYER|V_HUDTRANS, "center")
				v.drawScaled(stage3x*FRACUNIT, 0, FRACUNIT, stage3, V_PERPLAYER|V_HUDTRANS)
				if mapicon != imnothere -- Don't draw a icon if there is none.
					v.drawScaled((stage3x+151)*FRACUNIT, 122*FRACUNIT, FRACUNIT/2, mapicon, V_PERPLAYER|V_HUDTRANS)
				end
				v.drawString(stage3x+57, 105, mapact, V_PERPLAYER|V_HUDTRANS)
	end
end

addHook("HUD", function(v, p, c)

	customhud.SetupItem("hubhud", modname, hubworldhud(v, p))
	
	customhud.SetupItem("stageselect", modname, stageselect(v, p))
	
end)