local ExtraColorsLoaded = false

local Supercolour_CategoryNames = {
	'Vanilla',
	'Pre-Packed',
	'Plain',
	'Super Mystic',
	'Extras',
	'Extra Plain'
}

local Supercolour_SMSColors = {
    ['crisis'] = {SKINCOLOR_CRIMSON, SKINCOLOR_RED, SKINCOLOR_FLAME, SKINCOLOR_KETCHUP, SKINCOLOR_SUPERORANGE5},
    ['antimatter'] = {SKINCOLOR_CARBON, SKINCOLOR_BLACK, SKINCOLOR_JET, SKINCOLOR_SILVER, SKINCOLOR_GREY},
    ['priest'] = {SKINCOLOR_GREY, SKINCOLOR_SILVER, SKINCOLOR_SUPERSILVER5, SKINCOLOR_SUPERSILVER3, SKINCOLOR_SUPERSILVER1},
    ['bluewave'] = {SKINCOLOR_COBALT, SKINCOLOR_BLUE, SKINCOLOR_CORNFLOWER, SKINCOLOR_BLUEBELL, SKINCOLOR_AZURE},
    ['arcticchi'] = {SKINCOLOR_ICY, SKINCOLOR_AETHER, SKINCOLOR_BONE, SKINCOLOR_WHITE, SKINCOLOR_SUPERSILVER5},
    ['ranger'] = {SKINCOLOR_FOREST, SKINCOLOR_GREEN, SKINCOLOR_PERIDOT, SKINCOLOR_SUPERPERIDOT5, SKINCOLOR_SUPERPERIDOT2},
    ['pinkmenace'] = {SKINCOLOR_VIOLET, SKINCOLOR_LILAC, SKINCOLOR_ROSY, SKINCOLOR_PINK, SKINCOLOR_BUBBLEGUM},
    ['metallic'] = {SKINCOLOR_SUPERRUST5, SKINCOLOR_SUPERRUST4, SKINCOLOR_COPPER, SKINCOLOR_SUNSET, SKINCOLOR_GOLD},
    ['hazelglow'] = {SKINCOLOR_BLACK, SKINCOLOR_CARBON, SKINCOLOR_BRONZE, SKINCOLOR_BROWN, SKINCOLOR_BEIGE},
    ['ultradrive'] = {SKINCOLOR_BONE, SKINCOLOR_WHITE, SKINCOLOR_SUPERSILVER3, SKINCOLOR_SUPERSILVER2, SKINCOLOR_SUPERSILVER1},
    ['lapisvapor'] = {SKINCOLOR_DUSK, SKINCOLOR_CERULEAN, SKINCOLOR_BLUE, SKINCOLOR_SAPPHIRE, SKINCOLOR_BLUEBELL},
    ['azurebreaker'] = {SKINCOLOR_WAVE, SKINCOLOR_AZURE, SKINCOLOR_TEAL, SKINCOLOR_AQUA, SKINCOLOR_MOSS}
}

local Supercolour_Categories = {
	['Vanilla'] = {
		{n = 'Silver', a = 'Sonic Team Jr', sc = SKINCOLOR_SUPERSILVER1},
		{n = 'Red', a = 'Sonic Team Jr', sc = SKINCOLOR_SUPERRED1},
		{n = 'Orange', a = 'Sonic Team Jr', sc = SKINCOLOR_SUPERORANGE1},
		{n = 'Gold', a = 'Sonic Team Jr', sc = SKINCOLOR_SUPERGOLD1},
		{n = 'Peridot', a = 'Sonic Team Jr', sc = SKINCOLOR_SUPERPERIDOT1},
		{n = 'Sky', a = 'Sonic Team Jr', sc = SKINCOLOR_SUPERSKY1},
		{n = 'Purple', a = 'Sonic Team Jr', sc = SKINCOLOR_SUPERPURPLE1},
		{n = 'Rust', a = 'Sonic Team Jr', sc = SKINCOLOR_SUPERRUST1},
		{n = 'Tan', a = 'Sonic Team Jr', sc = SKINCOLOR_SUPERTAN1}
	},
	['Pre-Packed']  =  {
		{n = 'Mint', a = 'DirkTheHusky'},
		{n = 'Bubblegum', a = 'DirkTheHusky'},
		{n = 'Sapphire', a = 'DirkTheHusky'},
		{n = 'Wave', a = 'DirkTheHusky'},
		{n = 'Copper', a = 'DirkTheHusky'},
		{n = 'Ruby', a = 'DirkTheHusky'},
		{n = 'Aether', a = 'DirkTheHusky'},
		{n = 'Burning', a = 'Biggy-n'},
		{n = 'Emerald', a = 'Biggy-n'},
		{n = 'Lavender', a = 'Biggy-n'},
		{n = 'Rosy', a = 'Biggy-n'},
		{n = 'Raspberry', a = 'Biggy-n'},
		{n = 'Dark', a = 'Biggy-n'},
		{n = 'Brown', a = 'Biggy-n'},
		{n = 'Magenta', a = 'DirkTheHusky'},
		{n = 'PurpleRust', a = 'Biggy-n'},
		{n = 'RedGold', a = 'Biggy-n'},
		{n = 'RoseGold', a = 'Biggy-n'},
		{n = 'Cobalt', a = 'SuperPhanto'},
		{n = 'Sunset', a = 'SuperPhanto'},
		{n = 'Slate', a = 'SuperPhanto'},
		{n = 'Jet', a = 'DrStephen'},
		{n = 'Lime', a = 'DirkTheHusky'},
		{n = 'Wind', a = 'DirkTheHusky'},
		{n = 'Pseudo', a = 'furless'},
		{n = 'Topaz', a = 'Icezer'},
		{n = 'Aquatic', a = 'Icezer'},
		{n = 'Yellow', a = 'BlueBlur'},
		{n = 'Grape', a = 'Icezer'},
		{n = 'Lemon', a = 'SuperPhanto'},
		{n = 'Amethyst', a = 'Icezer'},
		{n = 'Prismarine', a = 'Icezer'},
		{n = 'Chrome', a = 'Icezer'},
		{n = 'Verglas', a = 'Icezer'},
		{n = 'Wine', a = 'Icezer'},
		{n = 'Abyssal', a = 'Icezer'},
		{n = 'Toxin', a = 'Icezer'},
		{n = 'Volcanic', a = 'Icezer'},
		{n = 'Radish', a = 'Icezer'},
		{n = 'Singer', a = 'Icezer'},
		{n = 'Divine', a = 'Icezer'},
		{n = 'Pear', a = 'Icezer'},
		{n = 'Phantom', a = 'Icezer'},
		{n = 'Terraria', a = 'SuperPhanto'},
		{n = 'Celeste', a = 'Icezer'},
		{n = 'Kurai', a = 'BlueBlur'},
		{n = 'Shimmering', a = 'Icezer'},
		{n = 'Betacarotene', a = 'Icezer'},
		{n = 'Hallowed', a = 'Icezer'},
		{n = 'Unlimited', a = 'Icezer'},
		{n = 'Slayer', a = 'Icezer'},
		{n = 'Radiance', a = 'RainbowTH'},
		{n = 'Discovery', a = 'RainbowTH'},
		{n = 'Redemption', a = 'RainbowTH'},
		{n = 'Lovestruck', a = 'RainbowTH'},
		{n = 'Vortex', a = 'Icezer'},
		{n = 'Virtua', a = 'Icezer'},
		{n = 'Lightning', a = 'SuperPhanto'},
		{n = 'Spectrum', a = "Cobaltn't"},
		{n = 'LemonLime', a = "Cobaltn't"},
		{n = 'Festive', a = "Cobaltn't"},
		{n = 'VoidReborn', a = 'TheLastKitchenGun'},
		{n = 'Brimstone', a = 'TheLastKitchenGun'},
		{n = 'Xenomite', a = 'TheLastKitchenGun'},
		{n = 'PrimeSoul', a = 'TheLastKitchenGun'},
		{n = 'BurningHeart', a = 'Hajime'},
		{n = 'Aurora', a = 'Hajime'},
		{n = 'MistyRuby', a = 'Great Glass'},
		{n = 'Cyberspace', a = 'Great Glass'},
		{n = 'Paradox', a = 'Great Glass'},
		{n = 'Advance', a = 'Golden Shine'},
	},
	['Plain'] = {
	},
	['Super Mystic'] = {
		{n = 'Crisis', a = 'Golden Shine'},
		{n = 'Antimatter', a = 'Golden Shine'},
		{n = 'Priest', a = 'Golden Shine'},
		{n = 'BlueWave', a = 'Golden Shine'},
		{n = 'ArcticChi', a = 'Golden Shine'},
		{n = 'Ranger', a = 'Golden Shine'},
		{n = 'PinkMenace', a = 'Golden Shine'},
		{n = 'Metallic', a = 'Golden Shine'},
		{n = 'HazelGlow', a = 'Golden Shine'},
		{n = 'UltraDrive', a = 'Golden Shine'},
		{n = 'LapisVapor', a = 'Golden Shine'},
		{n = 'AzureBreaker', a = 'Golden Shine'},
		{n = 'GoldenShine', a = 'Golden Shine', sc = SKINCOLOR_SUPERGOLDENSHINE1}, --entry 13
		{n = 'Emperor', a = 'Golden Shine', sc = SKINCOLOR_SUPEREMPEROR1},
		{n = 'Flamebreaker', a = 'Golden Shine', sc = SKINCOLOR_SUPERFLAMEBREAKER1},
		{n = 'Mystical', a = 'Golden Shine', sc = SKINCOLOR_SUPERMYSTICAL1},
		{n = 'Blue', a = 'Golden Shine', sc = SKINCOLOR_SUPERBLUE1},
		{n = 'Transformation', a = 'Golden Shine', sc = SKINCOLOR_SUPERTRANSFORMATION1},
		{n = 'InfinitysEnd', a = 'Golden Shine', sc = SKINCOLOR_SUPERINFINITYSEND1}
	},
	['Extras'] = {
	},
	['Extra Plain'] = {
	}
}

local Supercolour_Buttons = {
	'BT_WEAPONMASK',
	'BT_ATTACK',
	'BT_FIRENORMAL',
	'BT_CUSTOM1',
	'BT_CUSTOM2',
	'BT_CUSTOM3'
}

local function CMD_MenuButton(p, arg)
	if p then
		if not arg then
			
			local theList = ''
			local counter = 1
			
			for i = 1, 3 do
				for j = 1, 2 do
					if _G[Supercolour_Buttons[counter]] == p.superColourMenu.menuButton then
						theList = $..'\x82'..Supercolour_Buttons[counter]..'\x80, '
					else
						theList = $..Supercolour_Buttons[counter]..', '
					end
					counter = $ + 1
				end
				theList = $..'\n'
			end
			theList = $:sub(1,-4)
			
			CONS_Printf(p, "Rebinds the button used for opening the supercolor menu. (Uses BT_ Constants!)")
			CONS_Printf(p, "The following binds are available (use BT_WEAPONMASK if you want to keep the menu activated by command only!)\n\n"..theList)
		else
		
			arg = tostring($)
			if not (arg:sub(1,3):upper() == 'BT_') then
				arg = 'BT_'..($:upper())
			end
			
			local inList = false
			for i = 1, #Supercolour_Buttons do
				if arg == Supercolour_Buttons[i] then
					inList = true
				end
			end
			
			if _G[arg] and inList then
				p.superColourMenu.menuButton = _G[arg]
				CONS_Printf(p, "Button rebound to "..arg:sub(4)..'!')
			else
				CONS_Printf(p, "A valid button must be inputted.")
			end
			
		end
	else
		print("You must be in a level to use this command.")
	end
end

COM_AddCommand("supercolor_rebind", CMD_MenuButton)

local function CMD_MenuOpen(p, arg)
	if p and p.superColourMenu then
		p.superColourMenu.menuActive = not $
		p.superColourMenu.storedVars.angleturn = p.cmd.angleturn<<16
		p.superColourMenu.storedVars.aiming = p.cmd.aiming<<16
		p.superColourMenu.storedVars.angle = p.mo.angle
	else
		print("You must be in a level to use this command.")
	end
end

COM_AddCommand("supercolor_menu", CMD_MenuOpen)

addHook('ThinkFrame', function()
	for p in players.iterate do
		if p.mo and p.mo.valid
			if p.superColourMenu then
				local SCM = p.superColourMenu
				if SCM.menuActive then
					p.mo.flags2 = $|MF2_DONTDRAW
					
					local ghost = P_SpawnMobjFromMobj(p.mo,0,0,0,MT_OVERLAY)
					ghost.color = p.mo.color
					ghost.angle = p.drawangle
					ghost.rollangle = p.mo.rollangle
					ghost.spritexscale = p.mo.spritexscale
					ghost.spriteyscale = p.mo.spriteyscale
					ghost.skin = p.mo.skin
					ghost.frame = p.mo.frame
					ghost.sprite = p.mo.sprite
					ghost.sprite2 = p.mo.sprite2
					ghost.frame = $|TR_TRANS60
					ghost.tics = 1
					ghost.fuse = 1
					if p.yusonictable
						ghost.spriteyoffset = p.yusonictable.superoffset -- i really get irked by how you do things sometimes, golden shine -Icy
					end
					if p.followmobj
						if ((SCRnofollowmobjcoloring) and (SCRnofollowmobjcoloring[p.followmobj.type]) and (SCRnofollowmobjcoloring[p.followmobj.type]) >= 1) return end -- same logic as the coloring. if in the blacklist, don't flicker.
						local ghost = P_SpawnMobjFromMobj(p.followmobj,0,0,0,MT_OVERLAY)
						ghost.color = p.followmobj.color
						ghost.angle = p.followmobj.angle
						ghost.rollangle = p.followmobj.rollangle
						ghost.spritexscale = p.followmobj.spritexscale
						ghost.spriteyscale = p.followmobj.spriteyscale
						ghost.skin = p.mo.skin
						ghost.frame = p.followmobj.frame
						ghost.sprite = p.followmobj.sprite
						ghost.sprite2 = p.followmobj.sprite2
						ghost.frame = $|TR_TRANS60
						ghost.tics = 1
						ghost.fuse = 1
					end
				end
			end
		end
	end
end)

addHook('PreThinkFrame', function()
	for p in players.iterate do
		if p.mo and p.mo.valid
			if p.superColourMenu then
				local SCM = p.superColourMenu
				if SCM.menuActive then
				--Lstick Up
					if p.cmd.forwardmove >= 10
					and not p.scm_forward
						SCM.buttonsDown.forward = 1
					else
						SCM.buttonsDown.forward = 0
					end
				--Lstick Down	
					if p.cmd.forwardmove <= -10
					and not p.scm_back
						SCM.buttonsDown.back = 1
					else
						SCM.buttonsDown.back = 0
					end
				--Lstick Right	
					if p.cmd.sidemove >= 10
					and not p.scm_right
						SCM.buttonsDown.right = 1
					else
						SCM.buttonsDown.right = 0
					end
				--Lstick Left	
					if p.cmd.sidemove <= -10
					and not p.scm_left
						SCM.buttonsDown.left = 1
					else
						SCM.buttonsDown.left = 0
					end
					
				--R1
					if p.cmd.buttons & BT_WEAPONPREV
					and not p.scm_turnRight
						SCM.buttonsDown.turnRight = 1
					else
						SCM.buttonsDown.turnRight = 0
					end
				--L1
					if p.cmd.buttons & BT_WEAPONNEXT
					and not p.scm_turnLeft
						SCM.buttonsDown.turnLeft = 1
					else
						SCM.buttonsDown.turnLeft = 0
					end
					
				--Jump
					if p.cmd.buttons & BT_JUMP
					and not p.scm_jump
						SCM.buttonsDown.jump = 1
					else
						SCM.buttonsDown.jump = 0
					end
				--Spin
					if p.cmd.buttons & BT_SPIN
					--and not p.scm_spin
						SCM.buttonsDown.spin = 1
					else
						SCM.buttonsDown.spin = 0
					end
				--TossFlag
					if p.cmd.buttons & BT_TOSSFLAG
					and not p.scm_tossflag
						SCM.buttonsDown.tossflag = 1
					else
						SCM.buttonsDown.tossflag = 0
					end
					
					if (p.cmd.buttons & SCM.menuButton)
					and not p.scm_menu
						SCM.buttonsDown.menuButton = 1
					else
						SCM.buttonsDown.menuButton = 0
					end
				end
				p.scm_forward = (p.cmd.forwardmove >= 10)
				p.scm_back = (p.cmd.forwardmove <= -10)
				p.scm_right = (p.cmd.sidemove >= 10)
				p.scm_left = (p.cmd.sidemove <= -10)
				p.scm_turnRight = (p.cmd.buttons & BT_WEAPONPREV)
				p.scm_turnLeft = (p.cmd.buttons & BT_WEAPONNEXT)
				p.scm_jump = (p.cmd.buttons & BT_JUMP)
				--p.scm_spin = (p.cmd.buttons & BT_SPIN)
				p.scm_tossflag = (p.cmd.buttons & BT_TOSSFLAG)
				p.scm_menu = (p.cmd.buttons & SCM.menuButton)
			end
		end
	end
end)

local function Supercolour_refresh()
	Supercolour_Categories['Extra Plain'] = {}
	Supercolour_Categories['Extras'] = {}
	
	local daSkins = {}
	local daSupers = {}
	local daSupercolors = {}
	
	for i = 1, #Supercolour_Categories['Pre-Packed']
		daSupercolors[Supercolour_Categories['Pre-Packed'][i].n] = true
	end
	for i = 13, #Supercolour_Categories['Super Mystic']
		daSupercolors[Supercolour_Categories['Super Mystic'][i].n] = true
	end
			
	for i = 1, #skins do
		if skins[i] then
			daSkins[skins[i].prefcolor] = skins[i].realname
			daSupers[skins[i].supercolor] = skins[i].realname
		end
	end
	
	for i = 114, #skincolors-1 do
	
		--if skincolors[i].accessible then
		if R_GetColorByName(R_GetNameByColor(i):lower()) and not (R_GetSuperColorByName(R_GetNameByColor(i):sub(7, -3)) or skincolors[i].name:len() == 0) then
			table.insert(Supercolour_Categories['Extra Plain'], {n = skincolors[i].name, sc = i})
			if daSkins[i] then
				Supercolour_Categories['Extra Plain'][#Supercolour_Categories['Extra Plain']].a = 'From '..daSkins[i]
			end
		end
					
		
		if (R_GetSuperColorByName(R_GetNameByColor(i):sub(7, -3))) and not daSupercolors[R_GetNameByColor(i):sub(7, -3)] then
			--print('a')
			daSupercolors[R_GetNameByColor(i):sub(7, -3)] = true
			
			table.insert(Supercolour_Categories['Extras'], {n = R_GetNameByColor(i):sub(7, -3), sc = i})
			if daSupers[i] then
				Supercolour_Categories['Extras'][#Supercolour_Categories['Extras']].a = 'From '..daSupers[i]
			end
		end
		
	end
end
COM_AddCommand("supercolor_refresh", Supercolour_refresh)

local fucktheseskins = { --for supers that use standard colors
	['adventuresonic'] = 'sadxsupersonic',
	['supersonic'] = 'firestormsuper'
}

addHook('KeyDown', function(key)
	local p = consoleplayer
	if p and p.mo and p.mo.valid and p.superColourMenu and key.name == 'backspace' and not netgame then
	
		local SCM = p.superColourMenu
		local targetcolor = skins[p.mo.skin].supercolor
		
		if fucktheseskins[p.mo.skin] then
			targetcolor = R_GetColorByName(fucktheseskins[p.mo.skin])
		end
		
		if SCM.menuActive and not (Supercolour_Categories[Supercolour_CategoryNames[SCM.currentColor[1]]][SCM.currentColor[2][SCM.currentColor[1]]].sc == targetcolor) then
			for i = 1, #Supercolour_CategoryNames
				local cate = Supercolour_Categories[Supercolour_CategoryNames[i]]
				for j = 1, #cate
					if cate[j].sc == targetcolor then
						SCM.currentColor[1] = i
						SCM.currentColor[2][i] = j
						S_StartSound(nil, sfx_s258, p)
						break
					end
				end
			end
		end
	end
end)

addHook('ThinkFrame', function()
	if not ExtraColorsLoaded then
		for i = 1, #Supercolour_Categories['Pre-Packed']
			Supercolour_Categories['Pre-Packed'][i].sc = R_GetSuperColorByName(Supercolour_Categories['Pre-Packed'][i].n)
		end
		
		Supercolour_Categories['Pre-Packed'][71].sc = SKINCOLOR_SUPERADVANCEGOLD
		
		for i = 1, 68 do
			table.insert(Supercolour_Categories['Plain'], {n = skincolors[i].name, a = 'Sonic Team Jr', sc = i})
		end
		ExtraColorsLoaded = true
		
	end
		
	if (leveltime == 1 and ExtraColorsLoaded) then
		Supercolour_refresh()
	end
end)

addHook('PlayerThink', function(p)
	if p.scm_prevangleturn == nil
		p.scm_prevangleturn = 0
	end
	if p.scm_prevaiming == nil
		p.scm_prevaiming = 0
	end			
	p.scm_movingcameravertical = p.scm_prevaiming - p.cmd.aiming<<16
	p.scm_movingcamerahorizontal = p.scm_prevangleturn - p.cmd.angleturn<<16
	if p.mo.nobuttons
		if p.cmd.buttons
			p.cmd.buttons = 0
		else
			p.mo.nobuttons = nil				
		end
	end	
	if not p.superColourMenu then
		p.superColourMenu = {
			menuButton = BT_FIRENORMAL,
			buttonsDown = {},
			menuActive = false,
			hudVars = {
				tics = 0
			},
			storedVars = {
				angleturn = 0,
				aiming = 0,
				angle = 0
			},
			currentColor = {1,{4,1,1,1,1,1}},
			angle = 2,
			inverseControls = false
		}
	else
		local SCM = p.superColourMenu
	
		if SCM.menuActive then
			
			p.powers[pw_nocontrol] = max($, 2)
			p.powers[pw_flashing] = max($, 2)
			
			SCM.hudVars.tics = min($ + 1,16)
			 
			local move = 1
			if SCM.buttonsDown.spin then
				move = 4
			end
			
			if SCM.buttonsDown.tossflag then
				SCM.inverseControls = not $
				S_StartSound(nil, sfx_s3k63, p)
			end
			
			if #Supercolour_Categories[Supercolour_CategoryNames[SCM.currentColor[1]]] > 0 then
				if SCM.buttonsDown.forward --up
					S_StartSound(nil, sfx_menu1, p)
					SCM.currentColor[2][SCM.currentColor[1]] = $ - move
					if SCM.currentColor[2][SCM.currentColor[1]] <= 0 then
						SCM.currentColor[2][SCM.currentColor[1]] = #Supercolour_Categories[Supercolour_CategoryNames[SCM.currentColor[1]]]
					end
				end
				
				if SCM.buttonsDown.back --down
					S_StartSound(nil, sfx_menu1, p)
					SCM.currentColor[2][SCM.currentColor[1]] = $ + move
					if SCM.currentColor[2][SCM.currentColor[1]] > #Supercolour_Categories[Supercolour_CategoryNames[SCM.currentColor[1]]] then
						SCM.currentColor[2][SCM.currentColor[1]] = 1
					end
				end
				
				if SCM.buttonsDown.jump then
					S_StartSound(nil, sfx_addfil, p)
					S_StartSound(nil, sfx_s1ca, p)
					SCM.menuActive = false
					
					--TODO: Does this work???
					--yes, yes it does bloops - Icy
					local def = Supercolour_Categories[Supercolour_CategoryNames[SCM.currentColor[1]]][SCM.currentColor[2][SCM.currentColor[1]]]
					if (SCM.currentColor[1] == 1 or (SCM.currentColor[1] == 2 and SCM.currentColor[2][2] < 71) or SCM.currentColor[1] == 5 or (SCM.currentColor[1] == 4 and SCM.currentColor[2][4] >= 13)) then --normally flashing supercolors
						p.scolor = R_GetSuperColorByName(def.n)
						p.scolorstring = 'Super Color'
					elseif (SCM.currentColor[1] == 3 or SCM.currentColor[1] == 6) then -- static colors
						p.scolor = R_GetColorByName(def.n)
						p.scolorstring = 'N_ Colour'
					elseif (SCM.currentColor[1] == 2 and SCM.currentColor[2][2] == 71) then -- advance
						p.scolor = 'advance'
						p.scolorstring = 'Advance'
					elseif (SCM.currentColor[1] == 4 and SCM.currentColor[2][4] < 13) -- SMS
						p.scolor = def.n:lower()
						p.scolorstring = 'SMS'
					end
				end
			end
			
			if (SCM.buttonsDown.turnLeft and not SCM.inverseControls) --left
			or (SCM.inverseControls and SCM.buttonsDown.left) then
				SCM.angle = $ + 1
				if SCM.angle == 9 then
					SCM.angle = 1
				end
			end
			
			if (SCM.buttonsDown.turnRight and not SCM.inverseControls) --right
			or (SCM.inverseControls and SCM.buttonsDown.right) then
				SCM.angle = $ - 1
				if SCM.angle == 0 then
					SCM.angle = 8
				end
			end
			
			if (SCM.buttonsDown.left and not SCM.inverseControls)
			or (SCM.inverseControls and SCM.buttonsDown.turnLeft) then
				S_StartSound(nil, sfx_s3kb7, p)
				SCM.currentColor[1] = $ - 1
				if SCM.currentColor[1] == 0 then
					SCM.currentColor[1] = #Supercolour_CategoryNames
				end
			end
			
			if (SCM.buttonsDown.right and not SCM.inverseControls)
			or (SCM.inverseControls and SCM.buttonsDown.turnRight) then
				S_StartSound(nil, sfx_s3kb7, p)
				SCM.currentColor[1] = $ + 1
				if SCM.currentColor[1] > #Supercolour_CategoryNames then
					SCM.currentColor[1] = 1
				end
			end
		else
			SCM.hudVars.tics = max($ - 1,0)
		end
	
		if (p.panim == PA_IDLE and (p.cmd.buttons & SCM.menuButton))
			p.SCMbuttontimer = p.SCMbuttontimer and p.SCMbuttontimer+1 or 1
			if p.SCMbuttontimer >= 40
				p.mo.nobuttons = true
				if SCM.menuActive
					S_StartSound(nil, sfx_s1ca, p)
				else
					S_StartSound(nil, sfx_s1c3, p)
				end
				SCM.menuActive = not $
				SCM.storedVars.angleturn = p.cmd.angleturn<<16
				SCM.storedVars.aiming = p.aiming<<16
				SCM.storedVars.angle = p.mo.angle
			end			
		end
		
		if p.SCMbuttontimer
			if p.SCMbuttontimer <= 48
			and p.SCMbuttontimer >= 40
				p.SCMbuttontimer = $+1
			elseif not (p.panim == PA_IDLE and (p.cmd.buttons == SCM.menuButton))
			or p.SCMbuttontimer >= 49
				p.SCMbuttontimer = 0
			end
		end
	
		p.scm_prevangleturn = p.cmd.angleturn<<16
		p.scm_prevaiming  = p.cmd.aiming<<16
	end
end)
/*
s - start
e - end
t - time (0 - FU)
*/
 -- trying to steal from lactozilla now

local function fixinterp(s,e,t)
	return FixedMul((FU - (t)), s) + FixedMul(t, e)
end

local function ease_OutQuad(t,s,e)
	return fixinterp(s, e, FU - FixedMul(FU - t, FU - t))
end

local function adjustHUDTrans(v, num)
	local theShift = min(10,(v.localTransFlag()>>V_ALPHASHIFT)+num)
	--print(theShift)
	if theShift >= 10
		return false
	end
	return (theShift<<V_ALPHASHIFT)
end

freeslot("SPR_SCMR")
local function Supercolour_menu(v, p) --TOUHOU HIJACK LOL (joke)
	if p.mo and p.mo.valid and p.superColourMenu then
		local SCM = p.superColourMenu
		
		
		local SCMringFlags
		if p.SCMbuttontimer
			if p.SCMbuttontimer == 1
				SCMringFlags = V_90TRANS
			elseif p.SCMbuttontimer == 2
				SCMringFlags = V_80TRANS
			elseif p.SCMbuttontimer == 3
				SCMringFlags = V_70TRANS
			elseif p.SCMbuttontimer == 4
				SCMringFlags = V_60TRANS
			elseif p.SCMbuttontimer == 5
				SCMringFlags = V_50TRANS
			elseif p.SCMbuttontimer == 6
				SCMringFlags = V_40TRANS
			elseif p.SCMbuttontimer == 7
				SCMringFlags = V_30TRANS
			elseif p.SCMbuttontimer == 8
				SCMringFlags = V_20TRANS
			elseif p.SCMbuttontimer == 9
				SCMringFlags = V_10TRANS
			elseif p.SCMbuttontimer
				SCMringFlags = 0
			end
		end
		if p.SCMbuttontimer
		and p.SCMbuttontimer > 0
		and p.SCMbuttontimer < 48
			local SCMring = v.getSpritePatch(SPR_SCMR, p.SCMbuttontimer, 1)
			v.draw(160, 120, SCMring, SCMringFlags)
		end
		local alpha = adjustHUDTrans(v, 16-SCM.hudVars.tics)
		if alpha != false then
			local alphaAdvance = adjustHUDTrans(v, 7 - (7 - (min(SCM.hudVars.tics,7))))
			local player_patch, player_flipped
			if p.charflags & SF_NOSUPERSPRITES
				player_patch, player_flipped = v.getSprite2Patch(p.skin, SPR2_STND, false, A, SCM.angle)
			else
				player_patch, player_flipped = v.getSprite2Patch(p.skin, SPR2_STND, true, A, SCM.angle)
			end
			local colormap
			local colordefs
			
			local targetcolor = skins[p.mo.skin].supercolor
					
			if fucktheseskins[p.mo.skin] then
				targetcolor = R_GetColorByName(fucktheseskins[p.mo.skin])
			end
			
			v.fadeScreen(0xFA00, min(SCM.hudVars.tics / 2, 10))
			
			if #Supercolour_Categories[Supercolour_CategoryNames[SCM.currentColor[1]]] > 0 then
				 colordefs = Supercolour_Categories[Supercolour_CategoryNames[SCM.currentColor[1]]][SCM.currentColor[2][SCM.currentColor[1]]]
				
				if (SCM.currentColor[1] == 3 or (SCM.currentColor[1] == 2 and SCM.currentColor[2][2] == 71) or SCM.currentColor[1] == 6) then -- plain
					colormap = v.getColormap(p.skin, colordefs.sc)
				elseif SCM.currentColor[1] == 4 and SCM.currentColor[2][4] < 13 then -- sms
					colormap = v.getColormap(p.skin, Supercolour_SMSColors[colordefs.n:lower()][abs( ( (leveltime >> 1) % 9) - 4) + 1])
				else
					colormap = v.getColormap(p.skin, colordefs.sc + abs(((leveltime >> 1) % 9) - 4))
				end
				
				if colordefs.a then
					v.drawString(95*FU, ease_OutQuad(min(FU, SCM.hudVars.tics*FU/16), 300*FU, 150*FU), "\x82" .. "Credits: \x80" .. colordefs.a, V_ALLOWLOWERCASE|alpha, "small-fixed-center")
				end
				
				for i = -4, 4 do
					if not Supercolour_Categories[Supercolour_CategoryNames[SCM.currentColor[1]]][SCM.currentColor[2][SCM.currentColor[1]] + i] then
						continue
					end
					
					local color_default = (Supercolour_Categories[Supercolour_CategoryNames[SCM.currentColor[1]]][SCM.currentColor[2][SCM.currentColor[1]] + i].sc == targetcolor) and V_GREENMAP or 0
					if i == 0 then
						v.drawString(ease_OutQuad(min(FU, SCM.hudVars.tics*FU/16), -150*FU, 95*FU), 101*FU, "\x1D"..(((leveltime % 20) / 10) and "  " or " ")..Supercolour_Categories[Supercolour_CategoryNames[SCM.currentColor[1]]][SCM.currentColor[2][SCM.currentColor[1]] + i].n..(((leveltime % 20) / 10) and "  " or " ").."\x1C", V_ALLOWLOWERCASE|V_YELLOWMAP|alpha, "thin-fixed-center")
					else
						if (SCM.buttonsDown.spin and abs(i) < 4) then
							color_default = $ and V_MAGENTAMAP or V_REDMAP
						end
						v.drawString(ease_OutQuad(min(FU, SCM.hudVars.tics*FU/16), -150*FU, 95*FU), 101*FU + 10*i*FU, Supercolour_Categories[Supercolour_CategoryNames[SCM.currentColor[1]]][SCM.currentColor[2][SCM.currentColor[1]] + i].n, V_ALLOWLOWERCASE|color_default|alpha, "thin-fixed-center")
					end
				end	
				
			else
				colormap = v.getColormap(p.skin, skins[p.mo.skin].supercolor + abs(((leveltime >> 1) % 9) - 4))
				if fucktheseskins[p.mo.skin] then
					colormap = v.getColormap(p.skin, targetcolor)
				end
				v.drawString(ease_OutQuad(min(FU, SCM.hudVars.tics*FU/16), -150*FU, 95*FU), 101*FU, "\x1D"..(((leveltime % 20) / 10) and "  " or " ")..'No colors here!'..(((leveltime % 20) / 10) and "  " or " ").."\x1C", V_ALLOWLOWERCASE|V_REDMAP|alpha, "thin-fixed-center")
			end
			
			v.drawNameTag(160, ease_OutQuad(min(FU, SCM.hudVars.tics*FU/16), -30*FU, 8*FU)/FU, "Supercolor Menu", V_CENTERNAMETAG|alpha, p.skincolor, skincolors[p.skincolor].invcolor)

			v.drawScaled(ease_OutQuad(min(FU, SCM.hudVars.tics*FU/16), 500*FU, 245*FU), 100*FU + ((player_patch.height / 2) * skins[p.skin].highresscale), skins[p.skin].highresscale, player_patch, (player_flipped and V_FLIP or 0)|alpha, colormap)			
			
			if ((SCM.currentColor[1] == 2 and SCM.currentColor[2][2] == 71) and leveltime % 2 == 0) then
				v.drawScaled(ease_OutQuad(min(FU, SCM.hudVars.tics*FU/16), 500*FU, 245*FU), 100*FU + ((player_patch.height / 2) * skins[p.skin].highresscale), skins[p.skin].highresscale, player_patch, (player_flipped and V_FLIP or 0)|alphaAdvance|V_ADD, colormap)
			end
			
			v.drawString(245*FU, ease_OutQuad(min(FU, SCM.hudVars.tics*FU/16), 300*FU, 115*FU + ((player_patch.height / 2) * skins[p.skin].highresscale)), "\x1C"..(((leveltime % 20) / 10) and "  " or " ").."\x1D", V_YELLOWMAP|alpha, "fixed-center")
			
			v.drawString(ease_OutQuad(min(FU, SCM.hudVars.tics*FU/16), -150*FU, 95*FU), 50*FU, Supercolour_CategoryNames[SCM.currentColor[1]], V_ALLOWLOWERCASE|V_YELLOWMAP|alpha, "fixed-center")
			
			v.drawString(ease_OutQuad(min(FU, SCM.hudVars.tics*FU/16), -500*FU, 160*FU), 165*FU, '* [JUMP] selects the colour chosen.', V_ALLOWLOWERCASE|alpha, "small-fixed-center")
			v.drawString(ease_OutQuad(min(FU, SCM.hudVars.tics*FU/16), 500*FU, 160*FU), 170*FU, '* ['..(SCM.inverseControls and 'WEAPON NEXT/PREV AND MOVE UP/DOWN' or 'MOVEMENT CONTROLS')..'] navigates the UI.', V_ALLOWLOWERCASE|alpha, "small-fixed-center")
			v.drawString(ease_OutQuad(min(FU, SCM.hudVars.tics*FU/16), -500*FU, 160*FU), 175*FU, '* ['..(SCM.inverseControls and 'MOVE LEFT/RIGHT' or 'WEAPON NEXT/PREV')..'] rotates the player.', V_ALLOWLOWERCASE|alpha, "small-fixed-center")
			v.drawString(ease_OutQuad(min(FU, SCM.hudVars.tics*FU/16), 500*FU, 160*FU), 180*FU, '* [SPIN] can be used to skip through colours faster.', V_ALLOWLOWERCASE|alpha, "small-fixed-center")
			v.drawString(ease_OutQuad(min(FU, SCM.hudVars.tics*FU/16), -500*FU, 160*FU), 185*FU, '* [TOSS FLAG] swaps the controls used in the menu.', V_ALLOWLOWERCASE|alpha, "small-fixed-center")
			if not netgame
				v.drawString(ease_OutQuad(min(FU, SCM.hudVars.tics*FU/16), 500*FU, 160*FU), 190*FU, '* [BACKSPACE] will return color selection to character default.', V_ALLOWLOWERCASE|alpha, "small-fixed-center")
			end
		end
	end
end
	
hud.add(Supercolour_menu, 'game')


addHook('NetVars', function(n)
	ExtraColorsLoaded = n($)
	Supercolour_SMSColors = n($)
	Supercolour_Categories = n($)
	Supercolour_CategoryNames = n($)
	Supercolour_Buttons = n($)
end)