freeslot(
	"SPR_SPAR",
	"MT_SPARROW",
	"S_SPARROW"
)

mobjinfo[MT_SPARROW] = {
	doomednum = -1,
	spawnstate = S_SPARROW,
	flags = MF_NOCLIP|MF_SCENERY
}

states[S_SPARROW] = {
	sprite = SPR_SPAR,
	frame = TR_TRANS30|FF_PAPERSPRITE|A,
	tics = -1,
	nextstate = S_NULL
}

local amountposts = 0

local function drawsphud(v, player, cam)
	if (player.starhudtimer) and (player.startouch) and (player.starpostnum) and (amountposts > 0)
		local sphud
		local sphud1
		local sphud2
		if (v.patchExists("SPHUD"))
			sphud = v.cachePatch("SPHUD")
		end
		if (v.patchExists("SPHUD1"))
			sphud1 = v.cachePatch("SPHUD1")
		end
		if (v.patchExists("SPHUD2"))
			sphud2 = v.cachePatch("SPHUD2")
		end
		
		local xscroll = max(player.starhudtimer - 99, 0)
		if (player.starhudtimer <= 7)
			xscroll =  8 - player.starhudtimer
		end
		
		local xoffset = 9 * xscroll
		
		local drawamt = 0
		local scroll = 0
		if (amountposts >= 5)
			scroll = max(0, player.startouch - 4) * -36
		end
		
		while(drawamt < amountposts)
			local screenx = 277
			local screeny = 18
			local yoffset = drawamt * 36
			
			drawamt = $1 + 1
			local transpr = V_50TRANS
			if (drawamt == player.startouch)
				transpr = V_10TRANS
			end
			if (drawamt <= player.starpostnum)
				v.draw(screenx + 15 + xoffset, screeny + yoffset + scroll, sphud, transpr | V_SNAPTORIGHT | V_SNAPTOTOP, nil)
			else
				transpr = V_70TRANS
				v.draw(screenx + 15 + xoffset, screeny + yoffset + scroll, sphud1, transpr | V_SNAPTORIGHT | V_SNAPTOTOP, nil)
			end
			if (drawamt < amountposts)
				v.draw(screenx + 15 + xoffset, screeny + yoffset + scroll + 18, sphud2, V_50TRANS | V_SNAPTORIGHT | V_SNAPTOTOP, nil)
			end
			v.drawNum(screenx + xoffset, screeny + yoffset + scroll - 5, drawamt, transpr | V_SNAPTORIGHT | V_SNAPTOTOP)
		end
	end
end

addHook("MapLoad", function(mapnum)
	amountposts = 0
end)

addHook("MobjThinker", function(mo)
	amountposts = max(amountposts, mo.health)
	if (mo.thearrow == nil)
		mo.thearrow = P_SpawnMobjFromMobj(mo, 0, 0, 0, MT_SPARROW)
		mo.thearrow.angle = mo.angle
		mo.thearrow.colorized = true
		mo.thearrow.color = SKINCOLOR_WHITE
		mo.thearrow.flashing = false
		mo.thearrow.flash = 0
	end
	if (mo.state == S_STARPOST_SPIN) or (mo.state == S_STARPOST_FLASH)
		mo.thearrow.flashing = true
	end
	if (mo.thearrow.flashing)
		if (mo.thearrow.flash == 0) or (mo.thearrow.flash == 3) or (mo.thearrow.flash == 2)
			mo.thearrow.color = SKINCOLOR_ORANGE
		elseif (mo.thearrow.flash == 1)
			mo.thearrow.color = SKINCOLOR_YELLOW
		else
			mo.thearrow.color = SKINCOLOR_RED
		end
		
		mo.thearrow.flash = $1 + 1
		if (mo.thearrow.flash == 6)
			mo.thearrow.flash = 0
		end
	end
end, MT_STARPOST)

addHook("TouchSpecial", function(special, mo)
	if mo.player and (special.type == MT_STARPOST)
		mo.player.startouch = special.health
		
		if (mo.player.starhudtimer == nil)
			mo.player.starhudtimer = 107
		elseif (mo.player.starhudtimer == 0)
			mo.player.starhudtimer = 107
		elseif (mo.player.starhudtimer < 100)
			mo.player.starhudtimer = min($1 + 2, 100)
		end
	end
end, MT_STARPOST)

addHook("ThinkFrame", do
	for player in players.iterate
		if not (player.starhudtimer == nil)
			if (player.starhudtimer > 0)
				player.starhudtimer = $1 - 1
			end
		end
	end
end)

hud.add(drawsphud, "game")