//The Wechidna transformation

freeslot("SKINCOLOR_WECHIDNA")

skincolors[SKINCOLOR_WECHIDNA] = {
    name = "Blood-top White",
    ramp = {32,33,34,35,4,5,6,7,8,9,10,11,12,13,14,15},
    invcolor = SKINCOLOR_MINT,
    invshade = 3,
    chatcolor = V_WHITEMAP,
    accessible = false
}

local function GLITCHFORMS_IsKnux(player_t)
	if not (player_t.mo and player_t.mo.valid) then return false end
	
	local glitchforms_knuxs = {"knuckles", "knux"}
	local glitchforms_charname = string.lower(skins[player_t.mo.skin].realname)

	for i = 1, #glitchforms_charname
		if string.find(glitchforms_charname, glitchforms_knuxs[i])
			return true
		else
			return false
		end
	end
end

addHook("PlayerThink", function(player)
	if player.mo and player.mo.valid
		if player.transformbutton == nil
			player.transformbutton = BT_TOSSFLAG
		end
		if player.wechidnadelay == nil
			player.wechidnadelay = 45
		end
		if GLITCHFORMS_IsKnux(player)
			player.wechidna = true
		else
			player.wechidna = false
		end
		if player.wechidnadelay > 0
			player.wechidnadelay = $-1
		end
		if player.wechidnadelay > 60
			player.mo.colorized = true
		elseif player.wechidnadelay == 59
			player.mo.colorized = false
		end
		if not player.powers[pw_super]
		and player.wechidna == true
			if player.iswechidna != true
				player.mo.color = player.skincolor
			else
				player.mo.color = SKINCOLOR_WECHIDNA
			end
		end
		if player.cmd.buttons & player.transformbutton
		and player.wechidna == true
		and player.iswechidna != true
		and player.wechidnadelay == 0
			P_FlashPal(player, PAL_WHITE, 10)
			S_StartSound(player.mo, sfx_zoom, player)
			player.iswechidna = true
			player.wechidnadelay = 70
			player.glidetime = 0
			player.actionspd = skins["tails"].actionspd
		elseif player.cmd.buttons & player.transformbutton
		and player.wechidna == true
		and player.iswechidna == true
		and player.wechidnadelay == 0
		and player.wechidnacrashthegame != true
			player.wechidnacrashfreeze = false
			player.wechidnacrashtime = 30*TICRATE
			P_FlashPal(player, PAL_NUKE, 10)
			S_StartSound(player.mo, sfx_zoom, player)
			player.iswechidna = false
			player.wechidnadelay = 70
			player.powers[pw_tailsfly] = 0
			player.charability = skins[player.mo.skin].ability
			player.actionspd = skins[player.mo.skin].actionspd
		end
	end
end)