// --------------------------------------
// Lua_Title
// Puts ~~Kirby~~ Meta Knight on the title screen!
// --------------------------------------
freeslot("sfx_warpst", "sfx_warps2")

if not(kirbytitle)
// Yes, I'm not indenting.

rawset(_G, "metatitle", true)

local maxstars = 512
local startime = TICRATE
local staraccel = FRACUNIT/8
local starmaxspeed = FRACUNIT

local kirbywidth = 20
local kirbyheight = 20

local function spawnStar(v, x, y)
	local snum = 0
	while(snum < maxstars)
		if not(v.metastars[snum].exists)
			v.metastars[snum].exists = true
			v.metastars[snum].x = x
			v.metastars[snum].y = y
			v.metastars[snum].speed = 0
			v.metastars[snum].time = 0
			return
		end
		snum = $1 + 1
	end
end

local titlereset = false
addHook("MapLoad", function()
	titlereset = true
end)

local playsound = 0
addHook("ThinkFrame", function()
	// Only play on the title screen; with no players!
	for player in players.iterate
		playsound = 0
		return
	end
	
	if(playsound == 1)
		S_StartSound(nil, sfx_warpst)
		playsound = 0
	elseif(playsound == 2)
		S_StartSound(nil, sfx_warps2)
	end
end)

local function drawKirbyTitle(v)
	if(kirbytitle)
		return		// Kirby's version takes priority!
	end
	
	if not(v.metatitle and v.metatitle.valid)
		v.metatitle = {
			[0] = {
				[0] = v.cachePatch("METTTL51"),
				[1] = v.cachePatch("METTTL52"),
				[2] = v.cachePatch("METTTL53"),
				[3] = v.cachePatch("METTTL54"),
				[4] = v.cachePatch("METTTL55"),
			},
			[1] = {
				[0] = v.cachePatch("METTTL41"),
				[1] = v.cachePatch("METTTL42"),
				[2] = v.cachePatch("METTTL43"),
				[3] = v.cachePatch("METTTL44"),
				[4] = v.cachePatch("METTTL45"),
			},
			[2] = {
				[0] = v.cachePatch("METTTL21"),
				[1] = v.cachePatch("METTTL22"),
				[2] = v.cachePatch("METTTL23"),
				[3] = v.cachePatch("METTTL24"),
				[4] = v.cachePatch("METTTL25"),
			},
		}
		v.metastargfx = {
			[0] =  v.cachePatch("METASTR1"),
			[1] =  v.cachePatch("METASTR2"),
			[2] =  v.cachePatch("METASTR3"),
		}
	end
	
	if(v.metastars == nil)
		v.metastars = {}
	end
	
	if(v.titlemeta == nil)
		v.titlemeta = {
			x = 0,
			y = 0,
			animtime = -5,
			animmode = 2,		// 0: Does not appear | 1: Sine wave around the top | 2: Appearing
			frameanimtime = 0,	// Fun
		}
	end
	
	if(titlereset)
		v.titlemeta.animtime = -5
		v.titlemeta.animmode = 2
		local snum = 0
		while(snum < maxstars)
			if(v.metastars[snum])
				v.metastars[snum].exists = false
				v.metastars[snum].x = 0
				v.metastars[snum].y = 0
				v.metastars[snum].speed = 0
				v.metastars[snum].time = 0
			end
			snum = $1 + 1
		end
		titlereset = false
	end
	
	local lod = 0
	
	local xs = v.dupx()
	local us = 5
	if(xs >= 5)
		lod = 0
	elseif(xs == 4)
		lod = 1
		us = 4
	else
		lod = 2
		us = 2
	end
	
	local graphicscale = (FRACUNIT/xs)*xs/us//FixedMul(FRACUNIT, lodmul[lod])
	local starscale = FRACUNIT/25
	
	// Kirby
	v.titlemeta.animtime = $1 + 1
	
	if(v.titlemeta.animmode == 0)
		and(v.titlemeta.animtime > 5*TICRATE)
		v.titlemeta.animtime = -TICRATE*2
		v.titlemeta.animmode = 1
	elseif(v.titlemeta.animmode == 1)
		and(v.titlemeta.animtime > 10*TICRATE)
		v.titlemeta.animtime = -5
		v.titlemeta.animmode = 0
	elseif(v.titlemeta.animmode == 2)
		and(v.titlemeta.animtime > 3*TICRATE/2)
		v.titlemeta.animtime = -5
		v.titlemeta.animmode = 3
	elseif(v.titlemeta.animmode == 3)
		and(v.titlemeta.animtime > 2*TICRATE)
		v.titlemeta.animtime = -TICRATE*2
		v.titlemeta.animmode = 1
	end
	
	// Hacked animation
	v.titlemeta.frameanimtime = $1 + 1
	local animframe = (v.titlemeta.frameanimtime/3)%5
	
	if(v.titlemeta.animmode == 1)
		local basex = -50*FRACUNIT+(v.titlemeta.animtime*2*FRACUNIT)
		local basey = (35*FRACUNIT)-(v.titlemeta.animtime*FRACUNIT/10)
		v.titlemeta.x = basex
		v.titlemeta.y = basey+FixedMul(sin(v.titlemeta.animtime*ANG1*7), 10*FRACUNIT)
		v.drawScaled(v.titlemeta.x, v.titlemeta.y, graphicscale, v.metatitle[lod][animframe], V_FLIP)
	elseif(v.titlemeta.animmode == 2)
		local basex = -55*FRACUNIT+(v.titlemeta.animtime*10*FRACUNIT)
		local basey = (50*FRACUNIT)+(v.titlemeta.animtime*2*FRACUNIT)
		v.titlemeta.x = basex
		v.titlemeta.y = basey+FixedMul(sin(v.titlemeta.animtime*ANG2*2), 20*FRACUNIT)
		v.drawScaled(v.titlemeta.x, v.titlemeta.y, graphicscale, v.metatitle[lod][animframe], V_FLIP)
		
		if(v.titlemeta.animtime == 1)
			playsound = 1
		end
	elseif(v.titlemeta.animmode == 3)
		local basex = 375*FRACUNIT-(v.titlemeta.animtime*10*FRACUNIT)
		local basey = (55*FRACUNIT)-(v.titlemeta.animtime*FRACUNIT/2)
		v.titlemeta.x = basex
		v.titlemeta.y = basey-FixedMul(sin(v.titlemeta.animtime*ANG2*2), 10*FRACUNIT)
		v.drawScaled(v.titlemeta.x, v.titlemeta.y, graphicscale, v.metatitle[lod][animframe])
	end
	
	// Process star particles
	local snum = 0
	while(snum < maxstars)
		// Create an empty one if it doesn't exist
		if not(v.metastars[snum])
			v.metastars[snum] = {
				exists = false,
				x = 0,
				y = 0,
				speed = 0,
				time = 0,
			}
		elseif(v.metastars[snum].exists)
			local star = v.metastars[snum]
			star.time = $1 + 1
			if(star.time > startime)
				star.exists = false
			else
				star.speed = $1 + staraccel
				if(star.speed > starmaxspeed)
					star.speed = starmaxspeed
				end
				star.y = $1 + star.speed
				
				// Draw the star
				local starsprite = (star.time*3)/startime
				if(starsprite > 2)
					starsprite = 2
				end
				v.drawScaled(star.x, star.y, starscale, v.metastargfx[starsprite])
			end
		end
		snum = $1 + 1
	end
	
	// Spawn stars around Kirby
	if(v.titlemeta.animtime & 1)
		and(v.titlemeta.animmode)
		and not(v.titlemeta.animmode == 1)
		local xoff = (((v.RandomByte()*kirbywidth)/256)-(kirbywidth/2))*FRACUNIT
		local yoff = (((v.RandomByte()*kirbyheight)/256)-(kirbyheight/2))*FRACUNIT
		spawnStar(v, v.titlemeta.x+xoff, v.titlemeta.y+yoff)
	end
	
end
hud.add(drawKirbyTitle, "title")

// End of not indenting
end
