// --------------------------------------
// Lua_KHud
// Kirby's HUD
// TODO: Fix switching renderers!
// --------------------------------------
local xoff = 0
local flags = V_SNAPTORIGHT|V_SNAPTOBOTTOM|V_PERPLAYER
local usepausegfx = false

local numsimple = 6
local function drawKirbyHud(v, player, camera)
	if not(player.mo and player.mo.valid and player.mo.skin == "kirby")
		or(player.kvars == nil)
		or(player.powers[pw_carry] == CR_NIGHTSMODE)
	//	or(player.powers[pw_carry] == CR_NIGHTSFALL)
		return
	end
	if(v.abilitypatches == nil)
		v.abilitypatches = {}
		v.abilitynames = {}
	end
	
	local anum = 0
	while(anum <= numabilities)
		if not(v.abilitypatches[anum] and v.abilitypatches[anum].valid)
			if(abilityicons[anum] == nil)
				v.abilitypatches[anum] = v.cachePatch("ICON"+anum)
				v.abilitynames[anum] = v.cachePatch("NAME"+anum)
			else
				v.abilitypatches[anum] = v.cachePatch("I"+abilityicons[anum])
				v.abilitynames[anum] = v.cachePatch("N"+abilityicons[anum])
			end
		end
		anum = $1 + 1
	end
	v.secretpatches = {}
	v.secretnames = {}
	anum = 0
	while(anum < numsecretabilities)
		if not(v.secretpatches[anum] and v.secretpatches[anum].valid)
			if(secretabilityicons[anum] == nil)
				v.secretpatches[anum] = v.cachePatch("SECRETI"+(anum+1))
				v.secretnames[anum] = v.cachePatch("SECRETN"+(anum+1))
			else
				v.secretpatches[anum] = v.cachePatch("I"+secretabilityicons[anum])
				v.secretnames[anum] = v.cachePatch("N"+secretabilityicons[anum])
			end
		end
		anum = $1 + 1
	end
	v.secretsimple = {}
	anum = 0
	while(anum < numsimple)
		if not(v.secretsimple[anum] and v.secretsimple[anum].valid)
			v.secretsimple[anum] = v.cachePatch("SIMPLE"+(anum+1))
		end
		anum = $1 + 1
	end
	if(v.badpatch == nil)
		v.badpatch = v.cachePatch("ICONB")
		v.badname = v.cachePatch("NAMEB")
		v.itempatch = v.cachePatch("ICONI")
		v.itemname = v.cachePatch("NAMEI")
		v.nousepatch = v.cachePatch("ITSNOUSE")
		v.nousename = v.cachePatch("NTSNOUSE")
		v.starshippatch = v.cachePatch("ICONSTAR")
		v.starshipname = v.cachePatch("NAMESTAR")
		v.abilityborder = v.cachePatch("ABLBORDR")
		v.pausedgraphic = v.cachePatch("PAUSEGFX")
	end
	
	// Ability flash
	if(player.kvars.flashtime)
		v.fadeScreen(31, 3)
	end
	if(player.kvars.supertransflash)
		v.fadeScreen(0, 8)
	end
	
	local yoff = min(player.kvars.eattime*2, 32)-32
	if(player.kvars.eattime == (TICRATE*3/2)-1)
		yoff = -16
	end
	if(player.kvars.roulette)
		and not(player.kvars.rouletteability == -1)
		yoff = 0
	end
	
	local dontdrawbottom = false
	if(splitscreen)
		and(yoff < 0)
		yoff = -32
		dontdrawbottom = true
	end
	
	local useability = player.kvars.ability
	local usesecret = player.kvars.secretability
	if(player.kvars.roulette)
		and not(player.kvars.rouletteability == -1)
		useability = player.kvars.rouletteability
		usesecret = player.kvars.specialroulette
	end
	if not(usesecret)
		or not(useability)
		if(player.kvars.badtime)
			if not(dontdrawbottom)
				v.draw(280-xoff, 200-yoff, v.badpatch, flags)
			end
			v.draw(280-xoff, 200-yoff, v.badname, flags)
		elseif(player.kvars.itemtime)
			if not(dontdrawbottom)
				v.draw(280-xoff, 200-yoff, v.itempatch, flags)
			end
			v.draw(280-xoff, 200-yoff, v.itemname, flags)
		elseif(player.kvars.itsnouse)
			if not(dontdrawbottom)
				v.draw(280-xoff, 200-yoff, v.nousepatch, flags, v.getColormap(-1, SKINCOLOR_AETHER))
			end
			v.draw(280-xoff, 200-yoff, v.nousename, flags)
		elseif(player.powers[pw_super])
			if not(dontdrawbottom)
				v.draw(280-xoff, 200-yoff, v.starshippatch, flags)
			end
			v.draw(280-xoff, 200-yoff, v.starshipname, flags)
		else
			if not(dontdrawbottom)
				v.draw(280-xoff, 200-yoff, v.abilitypatches[useability], flags)
			end
			v.draw(280-xoff, 200-yoff, v.abilitynames[useability], flags)
		end
	elseif(useability == 7)	// Special high res icon
		if not(dontdrawbottom)
			v.drawScaled((280-xoff)*FRACUNIT, (200-yoff)*FRACUNIT, FRACUNIT/10, v.secretpatches[useability-1], flags)
		end
		v.draw(280-xoff, 200-yoff, v.secretnames[useability-1], flags)
	elseif(useability == 8)	// Gay eediot
		if not(dontdrawbottom)
			v.draw(280-xoff, 200-yoff, v.secretsimple[player.kvars.abilityrandom % numsimple], flags)
		end
		v.draw(280-xoff, 200-yoff, v.secretnames[useability-1], flags)
	else
		if not(dontdrawbottom)
			v.draw(280-xoff, 200-yoff, v.secretpatches[useability-1], flags)
		end
		v.draw(280-xoff, 200-yoff, v.secretnames[useability-1], flags)
	end
	if not(dontdrawbottom)
		v.draw(280-xoff, 200-yoff, v.abilityborder, flags)
	end
	
	// Pause!!
	if(paused)
		and(usepausegfx)
		v.draw(0, 0, v.pausedgraphic)
	end
end
hud.add(drawKirbyHud, "game")