-- Planel Stack Standalone
-- By Ashnal

if PanelStackStandalone then return end
rawset(_G, "PanelStackStandalone", 9)

local k_sneakertimer = k_sneakertimer
local k_floorboost = k_floorboost
local k_speedboost = k_speedboost

local boosttable = {
	{ 53740+768, 75000, 87500},
	{     32768, 49152, 60074},
	{ 17294+768, 32000, 41000}
}

local starttime = 6*TICRATE + (3*TICRATE/4)
local gamespeedsneakerstrength = {53740+768, 32768, 17294+768}
local section, special = 2, 6
local p, pks, speedboost, localgamespeed
addHook("MobjThinker", function(mo)
	if BLib then return end

	p = mo.player
	pks = p.kartstuff
	
	if not p.SPSpanelsneakertimer then
		p.SPSstackedpanels = 0
	end
	
	-- Determine the outcome of your charge.
	if leveltime == starttime+1 and pks[k_sneakertimer] then
		p.SPSpanelsneakertimer = pks[k_sneakertimer]+1
		p.SPSstackedpanels = 1
	end
	
	if pks[k_floorboost] and not p.SPSlastframefloorboost and P_PlayerTouchingSectorSpecial(mo.player, section, special) then
		p.SPSstackedpanels = min($+1,3)
	end
	
	if pks[k_floorboost] then
		p.SPSpanelsneakertimer = pks[k_sneakertimer]+1
	end

	pks[k_speedboost] = max($, boosttable[gamespeed+1][p.SPSstackedpanels or 0] or 0)
	
	p.SPSpanelsneakertimer = max(0, ($ or 0)-1)
	p.SPSlastframefloorboost = pks[k_floorboost]
end, MT_PLAYER)

addHook("PlayerSpawn", function(p)
	p.SPSpanelsneakertimer = 0
	p.SPSstackedpanels = 0
end)

local S_BOOSTFLAME = S_BOOSTFLAME
local SKINCOLOR_PURPLE = SKINCOLOR_PURPLE
local SKINCOLOR_BLUEBERRY = SKINCOLOR_BLUEBERRY
local SKINCOLOR_FLAME = SKINCOLOR_FLAME
local SKINCOLOR_MOONSLAM = SKINCOLOR_MOONSLAM
local SKINCOLOR_WHITE = SKINCOLOR_WHITE

local stacks, pmo, timer
addHook("MobjThinker", function(mo)
	if BLib or not (mo.target and mo.target.player) then return end

	if mo.state == S_BOOSTFLAME then
		stacks = (mo.target.player.SPSstackedpanels or 0)
		mo.colorized = true
		if stacks <= 1 then
			--No change
		elseif stacks == 2 then
			mo.color = SKINCOLOR_BLUEBERRY
		elseif stacks == 3 then
			mo.color = SKINCOLOR_PURPLE
		elseif stacks == 4 then
			mo.color = SKINCOLOR_MOONSLAM
		elseif stacks >= 5 then
			mo.color = SKINCOLOR_WHITE
		end
	end
end, MT_BOOSTFLAME)