----------------------------
--Skincolours    by   Guil--
----------------------------
--Intended    for     DRRR--
----------------------------

freeslot("SKINCOLOR_GUILGAMEBOY") --Gameboy
freeslot("SKINCOLOR_GUILHIGHLIGHT") --Highlight
freeslot("SKINCOLOR_GUILSEAFOAM") --Seafoam
freeslot("SKINCOLOR_GUILMERA") --Mera
freeslot("SKINCOLOR_GUILGEMSTONE") --Gemstone
freeslot("SKINCOLOR_GUILBERRYLEMON") --BerryLemon

freeslot("SKINCOLOR_GUILPALETTED") --Animated Rainbow, 7 colors. This one also swaps the chatcolor.
freeslot("SKINCOLOR_GUILCOLORWHEEL") --Animated Rainbow, Iridescent. This one also swaps the chatcolor.
freeslot("SKINCOLOR_GUILPULSINGBLUE") --Pulsing Blue
freeslot("SKINCOLOR_GUILCANDYCANE") --Candy Cane
freeslot("SKINCOLOR_GUILWINTERGREEN") --Wintergreen


skincolors[SKINCOLOR_GUILGAMEBOY] = {
    name = "GuilGameboy",
    ramp = {88,88,88,88,88,88,93,93,93,95,95,95,95,110,110,110},
    invcolor = SKINCOLOR_ORANGE,
    invshade = 9,
    chatcolor = V_PERIDOTMAP,
    accessible = true
}
skincolors[SKINCOLOR_GUILHIGHLIGHT] = {
    name = "GuilHighlight",
    ramp = {81,83,64,66,69,46,47,28,28,29,29,30,30,31,31,31},
    invcolor = SKINCOLOR_GUILGEMSTONE,
    invshade = 8,
    chatcolor = V_YELLOWMAP,
    accessible = true
}
skincolors[SKINCOLOR_GUILSEAFOAM] = {
    name = "GuilSeafoam",
    ramp = {120,121,123,141,135,136,151,167,168,168,169,169,253,253,254,254},
    invcolor = SKINCOLOR_RED,
    invshade = 9,
    chatcolor = V_AQUAMAP,
    accessible = true
}
skincolors[SKINCOLOR_GUILMERA] = {
    name = "GuilMera",
    ramp = {200,201,202,202,203,203,195,195,196,196,197,197,198,199,254,31},
    invcolor = SKINCOLOR_YELLOW,
    invshade = 9,
    chatcolor = V_ROSYMAP,
    accessible = true
}
skincolors[SKINCOLOR_GUILGEMSTONE] = {
    name = "GuilGemstone",
    ramp = {0,128,120,121,121,131,133,135,136,154,155,157,168,169,187,30},
    invcolor = SKINCOLOR_GUILHIGHLIGHT,
    invshade = 6,
    chatcolor = V_SKYMAP,
    accessible = true
}
skincolors[SKINCOLOR_GUILBERRYLEMON] = {
    name = "GuilBerryLemon",
    ramp = {0,80,82,74,64,142,167,167,167,168,168,158,158,159,159,31},
    invcolor = SKINCOLOR_PIGEON,
    invshade = 9,
    chatcolor = V_YELLOWMAP,
    accessible = true
}


-----------------------------
--Start of animated colours--
-----------------------------
local internal_timer = 0;


--Stuff for ANIM1
local colordelay = 12
local currentcolor = 1;
local rainbowcolors = {}
rainbowcolors[1] = {color = {32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47}, txt = V_REDMAP} --red
rainbowcolors[2] = {color = {48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63}, txt = V_ORANGEMAP} --orange
rainbowcolors[3] = {color = {80,81,82,83,72,73,74,75,75,76,76,77,77,78,78,79}, txt = V_YELLOWMAP} --yellow
rainbowcolors[4] = {color = {96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111}, txt = V_GREENMAP} --green
rainbowcolors[5] = {color = {128,129,130,131,132,133,134,135,135,136,136,137,137,138,138,139}, txt = V_SKYMAP} --cyan
rainbowcolors[6] = {color = {144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159}, txt = V_BLUEMAP} --blue
rainbowcolors[7] = {color = {160,161,162,163,164,164,165,165,166,166,167,167,168,168,169,253}, txt = V_PURPLEMAP} --purple
rainbowcolors[8] = {color = {176,177,178,179,180,181,182,183,183,184,184,185,185,186,186,187}, txt = V_MAGENTAMAP} --pink

--stuff for ANIM2
local looooooongrainbow = {
32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,
48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,63,62,61,60,59,58,57,56,55,54,53,52,51,50,49,48,
80,81,82,83,72,73,74,75,75,76,76,77,77,78,78,79,79,78,78,77,77,76,76,75,75,74,73,72,83,82,81,80,
96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,111,110,109,108,107,106,105,104,103,102,101,100,99,98,97,96,
128,129,130,131,132,133,134,135,135,136,136,137,137,138,138,139,139,138,138,137,137,136,136,135,135,134,133,132,131,130,129,128,
144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,
160,161,162,163,164,164,165,165,166,166,167,167,168,168,169,253,253,169,168,168,167,167,166,166,165,165,164,164,163,162,161,160,
176,177,178,179,180,181,182,183,183,184,184,185,185,186,186,187,187,186,186,185,185,184,184,183,183,182,181,180,179,178,177,176
}

--stuff for ANIM3, ANIM2 and 3 are handled similarly but with different tables
local bluepulse = {29,29,29,29,29,29,139,139,138,138,137,137,136,136,135,134,255,134,135,136,137,138,138,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29}

local candy = {
primary={0,200,210,32,33,34,35,37,38,40,41,43,45,46,47,30}, --red
secondary={0,1,2,3,4,5,6,8,10,12,14,16,18,20,22,24} --white
}

local winter = {
primary={0,3,89,90,98,99,101,101,102,125,126,126,127,127,139,29}, --green
secondary={0,48,220,224,229,232,233,70,44,71,46,46,47,47,47,30} --brown
}

--rainbow, changes every 12 tics to match rainbowcolors
skincolors[SKINCOLOR_GUILPALETTED] = {
    name = "GuilPaletted",
    ramp = {0,177,202,34,35,37,205,184,184,185,168,169,159,253,254,30},
    invcolor = SKINCOLOR_GUILCOLORWHEEL,
    invshade = 9,
    chatcolor = V_REDMAP,
    accessible = true
}

--rainbow, animates every few tics using looooooongrainbow
skincolors[SKINCOLOR_GUILCOLORWHEEL] = {
    name = "GuilColorWheel",
    ramp = {39,40,41,42,43,44,45,46,47,47,46,45,44,43,42,41},
    invcolor = SKINCOLOR_GUILPALETTED,
    invshade = 9,
    chatcolor = V_REDMAP,
    accessible = true
}

skincolors[SKINCOLOR_GUILPULSINGBLUE] = {
    name = "GuilPulsingBlue",
    ramp = {29,29,29,29,139,139,138,138,138,137,137,136,136,135,134,255},
    invcolor = SKINCOLOR_GUILCOLORWHEEL,
    invshade = 9,
    chatcolor = V_BLUEMAP,
    accessible = true
}

skincolors[SKINCOLOR_GUILCANDYCANE] = {
    name = "GuilCandyCane",
    ramp = {0,200,210,32,33,34,38,8,10,12,14,16,18,46,47,30},
    invcolor = SKINCOLOR_GUILWINTERGREEN,
    invshade = 9,
    chatcolor = V_REDMAP,
    accessible = true
}

skincolors[SKINCOLOR_GUILWINTERGREEN] = {
    name = "GuilWintergreen",
    ramp = {0,2,88,96,112,113,114,70,44,71,46,46,47,127,138,139},
    invcolor = SKINCOLOR_GUILCANDYCANE,
    invshade = 9,
    chatcolor = V_GREENMAP,
    accessible = true
}


local function RainbowThemColors()

	if gamestate != GS_LEVEL then --oohoho adding vote and intermission support, are we?
		internal_timer = internal_timer+1 --this attempts to continue leveltime beyond the level itself
	else
		internal_timer = leveltime
	end
	--candycane and wintergreen use these. put here for ease of access
	local thickness = 6; --copy x colors before alternating to the other table
	local alt = 0; --true if using secondary color

	--handle PALETTED
	if not (internal_timer%colordelay) then
		
		if currentcolor >= #rainbowcolors then currentcolor = 1 else currentcolor = currentcolor+1 end
		skincolors[SKINCOLOR_GUILPALETTED].ramp = rainbowcolors[currentcolor].color;
		skincolors[SKINCOLOR_GUILPALETTED].chatcolor = rainbowcolors[currentcolor].txt;
	end
	--end of PALETTED
	
	--------------------------------------------------------
	--------------------------------------------------------
	
	--handle COLORWHEEL and PULSINGBLUE together
	--originally had these separate but there's no reason to do two for loops
	
	--COLORWHEEL parameters
	local tablelen = #looooooongrainbow;
	local curpos = (internal_timer%tablelen)+1; --some index in looooooongrainbow
	local workpos = curpos
	local workramp = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
	
	--PULSINGBLUE parameters
	local tablelen2 = #bluepulse;
	local curpos2 = (internal_timer%tablelen2)+1; --some index in bluepulse
	local workpos2 = curpos2
	local workramp2 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
	

	--now assemble the ramps
	for i=1,16 do
	
		workramp[i] = looooooongrainbow[workpos]
		workpos = ((internal_timer+i)%tablelen)+1
		
		
		workramp2[i] = bluepulse[workpos2]
		workpos2 = ((internal_timer+i)%tablelen2)+1
	
	end
	
	--and assign the new ramps to the skincolors
	skincolors[SKINCOLOR_GUILCOLORWHEEL].ramp = workramp;
	skincolors[SKINCOLOR_GUILPULSINGBLUE].ramp = workramp2;
	
	--make the chat color change for this one too
	local divpos = (curpos/32)+1
	if divpos == 9 then divpos = 8 end --it ends up as 9 for one tic, that won't work
	
	skincolors[SKINCOLOR_GUILCOLORWHEEL].chatcolor = rainbowcolors[divpos].txt;
	--end of COLORWHEEL and PULSINGBLUE

	--------------------------------------------------------
	--------------------------------------------------------
	
	--start of CANDYCANE and WINTERGREEN
	
	local thickcount = 0;
	curpos = (internal_timer%16)+1;
	workramp = {} --candycane
	workramp2 = {} --windergreen
	local counter = 1
	
	for i=curpos, curpos+15 do
		
		local workpos = i%16;
		if workpos == 0 then workpos = 16 end
		
		if counter%thickness == 0 then
			alt = (alt+1)%2
		end
		
		if alt then
			workramp[workpos] = candy.secondary[workpos]
			workramp2[workpos] = winter.secondary[workpos]
		else
			workramp[workpos] = candy.primary[workpos]
			workramp2[workpos] = winter.primary[workpos]
		end
	
		counter = counter+1; --should count to 16
	
	end
	
	skincolors[SKINCOLOR_GUILCANDYCANE].ramp = workramp;
	skincolors[SKINCOLOR_GUILWINTERGREEN].ramp = workramp2;
	
end

addHook("ThinkFrame", RainbowThemColors)
addHook("VoteThinker", RainbowThemColors)
addHook("IntermissionThinker", RainbowThemColors)
addHook("NetVars", function()
	internal_timer = net($)
end)