//colsel v6
//product of pastel

local colsel = 0
local twosel = 0

local function SafeFreeslot(...)
	for _, item in ipairs({...})
		if rawget(_G, item) == nil
			freeslot(item)
		end
	end
end

SafeFreeslot(
	"SKINCOLOR_CRYSTAL",
	"SKINCOLOR_HAZE",
	"SKINCOLOR_FOUNTAIN",
	"SKINCOLOR_GLEAM",
	"SKINCOLOR_SPARKLING",
	"SKINCOLOR_BREEZE",
	"SKINCOLOR_CARMINE",
	"SKINCOLOR_THORN",
	"SKINCOLOR_RADIANT",
	"SKINCOLOR_GRAPE",
	"SKINCOLOR_NOCTURNE",
	"SKINCOLOR_LAKE",
	"SKINCOLOR_PERIWINKLE",
	"SKINCOLOR_ANTICHROME")

skincolors[SKINCOLOR_CRYSTAL] = {
    name = "Crystal",
    ramp = {144,145,146,147,147,148,148,149,149,165,165,166,166,167,167,168},
    invcolor = SKINCOLOR_HAZE,
    invshade = 7,
    chatcolor = V_BLUEMAP,
    accessible = true}

skincolors[SKINCOLOR_HAZE] = {
    name = "Haze",
    ramp = {88,188,188,189,189,190,190,191,191,18,20,22,24,26,28,30},
    invcolor = SKINCOLOR_CRYSTAL,
    invshade = 7,
    chatcolor = V_PERIDOTMAP,
    accessible = true}
	
skincolors[SKINCOLOR_FOUNTAIN] = {
    name = "Fountain",
    ramp = {130,131,132,133,134,134,135,135,136,136,137,137,138,138,139,139},
    invcolor = SKINCOLOR_GLEAM,
    invshade = 6,
    chatcolor = V_SKYMAP,
    accessible = true}

skincolors[SKINCOLOR_GLEAM] = {
    name = "Gleam",
    ramp = {0,48,49,50,52,54,56,58,60,70,70,71,71,46,46,47},
    invcolor = SKINCOLOR_FOUNTAIN,
    invshade = 6,
    chatcolor = V_ORANGEMAP,
    accessible = true}
	
skincolors[SKINCOLOR_SPARKLING] = {
    name = "Sparkling",
    ramp = {252,176,176,177,177,178,178,179,179,180,180,181,181,182,182,183},
    invcolor = SKINCOLOR_BREEZE,
    invshade = 5,
    chatcolor = V_MAGENTAMAP,
    accessible = true}

skincolors[SKINCOLOR_BREEZE] = {
    name = "Breeze",
    ramp = {120,121,122,123,123,141,141,135,135,136,136,137,137,138,138,139},
    invcolor = SKINCOLOR_SPARKLING,
    invshade = 9,
    chatcolor = V_AQUAMAP,
    accessible = true}
	
skincolors[SKINCOLOR_CARMINE] = {
    name = "Carmine",
    ramp = {33,34,36,38,40,41,42,43,44,44,45,45,46,46,47,47},
    invcolor = SKINCOLOR_THORN,
    invshade = 7,
    chatcolor = V_REDMAP,
    accessible = true}
	
skincolors[SKINCOLOR_THORN] = {
    name = "Thorn",
    ramp = {89,90,91,91,92,92,93,93,94,94,95,95,109,109,110,110},
    invcolor = SKINCOLOR_CARMINE,
    invshade = 6,
    chatcolor = V_GREENMAP,
    accessible = true}
	
skincolors[SKINCOLOR_RADIANT] = {
    name = "Radiant",
    ramp = {80,81,82,83,83,64,64,65,65,66,66,67,67,68,69,70},
    invcolor = SKINCOLOR_GRAPE,
    invshade = 5,
    chatcolor = V_YELLOWMAP,
    accessible = true}
	
skincolors[SKINCOLOR_GRAPE] = {
    name = "Grape",
    ramp = {160,161,162,162,163,163,195,195,196,196,197,197,198,198,199,199},
    invcolor = SKINCOLOR_RADIANT,
    invshade = 4,
    chatcolor = V_PURPLEMAP,
    accessible = true}
	
skincolors[SKINCOLOR_NOCTURNE] = {
    name = "Nocturne",
    ramp = {170,171,171,172,172,173,173,174,174,175,175,159,253,253,254,254},
    invcolor = SKINCOLOR_LAKE,
    invshade = 8,
    chatcolor = V_AZUREMAP,
    accessible = true}

skincolors[SKINCOLOR_LAKE] = {
    name = "Lake",
    ramp = {202,203,204,205,205,206,206,207,207,44,44,45,45,46,46,47},
    invcolor = SKINCOLOR_NOCTURNE,
    invshade = 8,
    chatcolor = V_ROSYMAP,
    accessible = true}
	
skincolors[SKINCOLOR_PERIWINKLE] = {
    name = "Periwinkle",
    ramp = {0,144,145,146,147,147,148,148,149,149,150,150,151,151,152,152},
    invcolor = SKINCOLOR_ANTICHROME,
    invshade = 11,
    chatcolor = V_BLUEMAP,
    accessible = true}
	
skincolors[SKINCOLOR_ANTICHROME] = {
    name = "Antichrome",
    ramp = {30,28,26,24,22,20,18,16,14,12,10,8,6,4,2,0},
    invcolor = SKINCOLOR_PERIWINKLE,
    invshade = 3,
    chatcolor = V_INVERTMAP,
    accessible = true}
	
COM_AddCommand("recolor", function(player, dye)
	if multiplayer
		CONS_Printf(player, "This only works in single player.")
		return
	end
	if not (dye)
		CONS_Printf(player, "recolor <color> - Choose any valid color to override your skincolor.")
	elseif (dye == "none") or (dye == "0") or (dye == "off") or (dye == "no") or (dye == "default")
		colsel = -1
	elseif (R_GetColorByName(dye) == SKINCOLOR_NONE)
		CONS_Printf(player, "Color "..dye.." not found.")
	else
		colsel = R_GetColorByName(dye)
	end
end)

COM_AddCommand("botcolor", function(player, dye)
	if multiplayer
		CONS_Printf(player, "This only works in single player.")
		return
	end
	if not (dye)
		CONS_Printf(player, "botcolor <color> - Choose any valid color to override the bot's skincolor.")
	elseif (dye == "none") or (dye == "0") or (dye == "off") or (dye == "no") or (dye == "default")
		twosel = -1
	elseif (R_GetColorByName(dye) == SKINCOLOR_NONE)
		CONS_Printf(player, "Color "..dye.." not found.")
	else
		twosel = R_GetColorByName(dye)
	end
end)


COM_AddCommand("savecolor", function(player)
	if multiplayer
		CONS_Printf(player, "This only works in single player.")
		return
	end
	local file = io.openlocal("colsel.cfg", "w")
	if (colsel == -1)
		file:write(0)
	else
		file:write(colsel)
	end
	file:close()
	local tile = io.openlocal("twosel.cfg", "w")
	if (twosel == -1)
		tile:write(0)
	else
		tile:write(twosel)
	end
	tile:close()
	CONS_Printf(player, "Color saved to file.")
end)

local restore = io.openlocal("colsel.cfg")
if restore
	local num = restore:read("*n")
	if num then
		colsel = R_GetColorByName(num)
	end
	restore:close()
end

local reboot = io.openlocal("twosel.cfg")
if reboot
	local num = reboot:read("*n")
	if num then
		twosel = R_GetColorByName(num)
	end
	reboot:close()
end

addHook("PlayerThink", function(player)
	if (player.mo) and not multiplayer and not player.powers[pw_super] and not player.powers[pw_dye]
		if (player.bot)
			if not (twosel)
				return
			elseif (twosel == -1)
				twosel = 0
				player.mo.color = player.skincolor
				return
			end
			if (player.mo.color != twosel)
				player.mo.color = twosel
			end
		else
			if not (colsel)
				return
			elseif (colsel == -1)
				colsel = 0
				player.mo.color = player.skincolor
				return
			end
			if (player.mo.color != colsel)
				player.mo.color = colsel
			end
		end
	end
end)
