freeslot("spr_wfog",
"S_WEREHOGFOG",
"MT_WEREHOGFOG",
"sfx_whogry",
"sfx_whogro",
"sfx_whogja",
"sfx_whoghg"
)
mobjinfo[MT_WEREHOGFOG] = {
	doomednum = -1,
	spawnstate = S_WEREHOGFOG,
	flags = MF_NOCLIP|MF_SCENERY|MF_NOGRAVITY|MF_NOCLIPHEIGHT
}

states[S_WEREHOGFOG] = {
	sprite = SPR_WFOG,
	frame = A|FF_ANIMATE|TR_TRANS40,
	var1 = 7,
	var2 = 2,
	tics = 7*2,
	nextstate = S_NULL
}
if not musicshift or not musicinvert
    rawset(_G, "musicshift", {})
	rawset(_G, "musicinvert", {})
end
if not classicskins
	rawset(_G, "classicskins", {})
end
if not nonsonicsonics
	rawset(_G, "nonsonicsonics", {})
end
if not werehogtransformvoice
	rawset(_G, "werehogtransformvoice", {})
end
if not earlesssonic
	rawset(_G, "earlesssonic", {})
end

werehogtransformvoice["adventuresonic"] = "ryan"
werehogtransformvoice["bandages"] = "roger"

nonsonicsonics["handdrawn"] = true
nonsonicsonics["pointy"] = true
nonsonicsonics["sunky"] = true
nonsonicsonics["bandages"] = true
nonsonicsonics["3dson"] = true
nonsonicsonics["chilidog"] = true
nonsonicsonics["greeneyes"] = true

earlesssonic["earlessreredo"] = true
earlesssonic["earlessredo"] = true
earlesssonic["earless"] = true

classicskins["sonic"] = true
classicskins["altsonic"] = true
classicskins["mcsonic"] = true
classicskins["pointy"] = true

musicshift["GFZ1"] = "GFZ1N"
musicshift["GFZ2"] = "GFZ2N"

musicinvert["GFZ1N"] = "GFZ1"
musicinvert["GFZ2N"] = "GFZ2"
local seclight = {}
/*addHook("MapLoad", function(e)
	--local sectorInt = 0
	seclight = {}
	for s in sectors.iterate
		--sectorInt = $+1
		seclight[#s] = s.lightlevel
	end
end)*/
local minute = TICRATE*60
local timescale = 1
local cycle = minute*4/timescale
local currenttime = 1 
local reversedir = 1
local inbetweentime = cycle
local frozentime = false
local werehogenabled = true
COM_AddCommand("werehogtransform", function(player, arg)
	if werehogenabled != nil and arg
		if arg == "true" or arg == "on" or arg == "1"
			werehogenabled = true
			print("Dark Gaia's influence grows stronger...")
		elseif  arg == "false" or arg == "off" or arg == "0"
			werehogenabled = false
			print("Dark Gaia's power wanes...")
		end
	end
end, COM_ADMIN)
COM_AddCommand("dndebug_settime", function(player, arg)
	if currenttime != nil and arg 
		currenttime = tonumber(arg)
		if arg == 0 or arg == 90
			inbetweentime = cycle
		else
			inbetweentime = 0
		end
	end
end, COM_ADMIN)
COM_AddCommand("dndebug_timescale", function(player, arg)
	if timescale != nil and arg 
		timescale = tonumber(arg)
	end
	print(timescale)
end, COM_ADMIN)
COM_AddCommand("dndebug_timedir", function(player, arg)
	if reversedir != nil and arg 
		reversedir = arg
	end
end, COM_ADMIN)
COM_AddCommand("dndebug_freezetime", function(player, arg)
	if frozentime != nil and arg
		if arg == "true" or arg == "on" or arg == "1"
			frozentime = true
			print("time frozen")
		elseif  arg == "false" or arg == "off" or arg == "0"
			frozentime = false
			print("time unfrozen")
		end
	end
end, COM_ADMIN)
addHook("ThinkFrame", do
	local maxdark = 140
	if gamemap == 13
		maxdark = 120
	else
		maxdark = 140
	end
	--local sectorInt = 0
	if (currenttime == 0 or currenttime == 90) and inbetweentime == 0
		if currenttime == 90
			reversedir = -1
		elseif currenttime == 0
			reversedir = 1
		end
		inbetweentime = cycle
		--print("doing thing")
	end
	--if (players[0].cmd.buttons & BT_CUSTOM1)
		--print(reversedir)
	--end
	if not (frozentime)
		if leveltime % (TICRATE/2) == 0 and inbetweentime == 0
			currenttime = $+reversedir
			--print(currenttime)
		end
		if inbetweentime > 0
			---print(inbetweentime)
			inbetweentime = $-1
			if inbetweentime == 1
				currenttime = $+reversedir
				inbetweentime = 0 
			end
		end
	end
	if not (mapheaderinfo[gamemap].spacemap)
		if currenttime <=10
			P_SetupLevelSky(5001)
		elseif currenttime <= 20
			P_SetupLevelSky(5002)
		elseif currenttime <= 30
			P_SetupLevelSky(5003)
		elseif currenttime <= 40
			P_SetupLevelSky(5004)
		elseif currenttime <= 50
			P_SetupLevelSky(5005)
		elseif currenttime <= 60
			P_SetupLevelSky(5006)
		elseif currenttime <= 70
			P_SetupLevelSky(5007)
		elseif currenttime <= 80
			P_SetupLevelSky(5008)
		elseif currenttime <= 90
			P_SetupLevelSky(5009)
		end
		if not inbetweentime or leveltime < 10
			--print("doing")
			for s in sectors.iterate
				if s and s.valid
					--sectorInt = $+1
					if leveltime == 0 or seclight[#s] == nil
						seclight[#s] = s.lightlevel
					else
						if seclight[#s]-(currenttime+currenttime/5) <= maxdark
							s.lightlevel = maxdark
						else
							if gamemap == 13
								s.lightlevel = seclight[#s]-(currenttime)	
							else
								s.lightlevel = seclight[#s]-(currenttime+currenttime/5)
							end
						end
					end
				end
			end
		end
	end
end)

local yourmom
addHook("PreThinkFrame", function(r)
	yourmom = S_GetMusicPosition()
end)
addHook("MusicChange", function(om, nm)
	if (consoleplayer and consoleplayer.mo and consoleplayer.mo.valid and consoleplayer.mo.health and currenttime > 90/2)
		if musicshift[nm] and currenttime > 90/2
			return musicshift[nm], 0, true, yourmom
		elseif musicinvert[nm] and currenttime < 90/2
			return musicinvert[nm], 0, true, yourmom
		end
	end
end)

//Check if you're Sonic or not. May or may not be stolen from SMS.

local function CanBecomeWerehog(s)
	if s and s.skin
		if (( (string.find(s.skin, "sonic") and not string.find(s.skin, "metal") and not string.find(s.skin, "mecha") and not string.find(s.skin, "hard")) ) 
		/*or (s.skin == "greeneyes") or (s.skin == "sunky") or (s.skin == "earless") or (s.skin == "earlessredo")
		or (s.skin == "3dson") or (s.skin == "handdrawn") or (s.skin == "pointy") or (s.skin == "sonik") or (s.skin == "bandages")*/
		or nonsonicsonics[s.skin] 
		or earlesssonic[s.skin])
		and (werehogenabled and werehogenabled == true)
		return true //Everyone is Sonic, and since everyone is Sonic, EVERYONE IS SMS- er, I mean WEREHOG.
		end
	end
	return false
end
addHook("PlayerThink", function(p)
	if (p.mo and p.mo.valid and p.mo.health)
		--if (p.cmd.buttons & BT_CUSTOM1)
			if skins["werehog"]
				--print("weg")
				--if CanBecomeWerehog(p.mo)
					--print(CanBecomeWerehog(p.mo))
				--end
				if CanBecomeWerehog(p.mo) and currenttime > 90/2 and not (p.WerehogTimer and p.WerehogTimer > 0)
					p.WerehogStoredSkin = p.mo.skin	
					p.WerehogTimer = TICRATE*4
					p.WerehogReversion = true
					--R_SetPlayerSkin(p, "werehog")
				end
				if p.mo.skin == "werehog" and currenttime < 90/2 and not (p.WerehogTimer and p.WerehogTimer > 0)
					--if p.WerehogStoredSkin
						--R_SetPlayerSkin(p, p.WerehogStoredSkin)
					--else
						--R_SetPlayerSkin(p, "sonic")
					--end
					p.WerehogTimer = TICRATE*10
					p.WerehogReversion = false
				end
		--	else
				--print("nah")
			end
		if (p.WerehogTimer and p.WerehogTimer > 0)
			p.WerehogTimer = $-1
			if P_RandomRange(5, 0) == 2 or p.WerehogTimer <= 2*TICRATE
				local fog = P_SpawnMobjFromMobj(p.mo, 0, 0, 0, MT_WEREHOGFOG)
				fog.blendmode = AST_SUBTRACT
				fog.targ = p.mo
				fog.xoff = P_RandomRange(23, -23)*p.mo.scale
				fog.yoff = P_RandomRange(23, -23)*p.mo.scale
				fog.zoff = P_RandomRange(23, -23)*p.mo.scale+p.mo.height*P_MobjFlip(p.mo)/3
				fog.scale = p.mo.scale+P_RandomRange(-2, 3)*p.mo.scale/8
				if P_RandomRange(0, 4) == 4
					fog.falloff = true
				end
			end
			if (p.WerehogTimer == 1)
				p.WerehogTransformation = FRACUNIT*3
				--print(leveltime)
				if (p.mo.skin == "werehog")
					if p.WerehogStoredSkin
						R_SetPlayerSkin(p, p.WerehogStoredSkin)
					else
						R_SetPlayerSkin(p, "sonic")
					end
					if classicskins[p.mo.skin]
					or earlesssonic[p.mo.skin]
						S_StartSound(p.mo, sfx_cdpcm2)
					elseif werehogtransformvoice[p.mo.skin] and werehogtransformvoice[p.mo.skin] == "ryan"
						S_StartSound(p.mo, sfx_whogry)
					elseif werehogtransformvoice[p.mo.skin] and werehogtransformvoice[p.mo.skin] == "roger"
						S_StartSound(p.mo, sfx_whogro)
					else
						S_StartSound(p.mo, sfx_whogja)
					end
					P_ResetPlayer(p)
				else
					R_SetPlayerSkin(p, "werehog")
					S_StartSound(p.mo, sfx_whoghg)
					P_ResetPlayer(p)
				end
			end
		end
		
		if p.WerehogTransformation and p.WerehogTransformation > 0
			if p.WerehogTransformation > FRACUNIT-FRACUNIT/4
				p.WerehogTransformation = $-FRACUNIT/7
			elseif p.WerehogTransformation < FRACUNIT/2
				p.WerehogTransformation = $-FRACUNIT/10
			else
				p.WerehogTransformation = $-FRACUNIT/30
				--print(leveltime)
			end
			p.WerehogTransformation = max($, 0)
		end
		/*if (p.cmd.buttons & BT_CUSTOM1)
			local fog = P_SpawnMobjFromMobj(p.mo, 0, 0, 0, MT_WEREHOGFOG)
			fog.blendmode = AST_SUBTRACT
			fog.targ = p.mo
			fog.xoff = P_RandomRange(23, -23)*p.mo.scale
			fog.yoff = P_RandomRange(23, -23)*p.mo.scale
			fog.zoff = P_RandomRange(23, -23)*p.mo.scale+p.mo.height*P_MobjFlip(p.mo)/3
			fog.scale = p.mo.scale+P_RandomRange(-2, 3)*p.mo.scale/8
			p.WerehogTransformation = FRACUNIT*3
			if (p.cmd.buttons & BT_CUSTOM2)
				p.WerehogReversion = true
			end
			if P_RandomRange(0, 4) == 4
				fog.falloff = true
			end
		end*/
		if musicshift[S_MusicName()] and currenttime > 90/2 and not (p.mo.QuitTryingToChangeTheFuckingMusicYouCunt)
			S_ChangeMusic(musicshift[S_MusicName()], true, p, 0, 0, MUSICRATE, MUSICRATE)
			p.mo.QuitTryingToChangeTheFuckingMusicYouCunt = TICRATE*2
		elseif musicinvert[S_MusicName()] and currenttime < 90/2 and not (p.mo.QuitTryingToChangeTheFuckingMusicYouCunt)
			S_ChangeMusic(musicinvert[S_MusicName()], true, p, 0, 0, MUSICRATE, MUSICRATE)
			p.mo.QuitTryingToChangeTheFuckingMusicYouCunt = TICRATE*2
		end
		if (p.mo.QuitTryingToChangeTheFuckingMusicYouCunt) and (p.mo.QuitTryingToChangeTheFuckingMusicYouCunt > 0)
			p.mo.QuitTryingToChangeTheFuckingMusicYouCunt = $-1
		end
		--print(S_MusicName())
		--end
	end
end)
local x = 160
local y = 100
local scale
hud.add(function(v)
    if consoleplayer and consoleplayer.mo and consoleplayer.mo.valid and (consoleplayer.WerehogTransformation and consoleplayer.WerehogTransformation != 0)
		scale = consoleplayer.WerehogTransformation --max((FRACUNIT*3)-((FRACUNIT/40)*consoleplayer.WerehogTransformation), 1)
		if (consoleplayer.WerehogReversion)
			v.drawScaled(x*FRACUNIT, y*FRACUNIT, scale, v.cachePatch("WHOGR3"), V_80TRANS)
			v.drawScaled(x*FRACUNIT, y*FRACUNIT, scale, v.cachePatch("WHOGR2"), V_60TRANS)
			v.drawScaled(x*FRACUNIT, y*FRACUNIT, scale, v.cachePatch("WHOGR1"))
		elseif (classicskins[consoleplayer.mo.skin])
			v.drawScaled(x*FRACUNIT, y*FRACUNIT, scale, v.cachePatch("VSONW3"), V_80TRANS)
			v.drawScaled(x*FRACUNIT, y*FRACUNIT, scale, v.cachePatch("VSONW2"), V_60TRANS)
			v.drawScaled(x*FRACUNIT, y*FRACUNIT, scale, v.cachePatch("VSONW1"))
		elseif (earlesssonic[consoleplayer.mo.skin])
			v.drawScaled(x*FRACUNIT, y*FRACUNIT, scale, v.cachePatch("EARLS3"), V_80TRANS)
			v.drawScaled(x*FRACUNIT, y*FRACUNIT, scale, v.cachePatch("EARLS2"), V_60TRANS)
			v.drawScaled(x*FRACUNIT, y*FRACUNIT, scale, v.cachePatch("EARLS1"))
		else
			v.drawScaled(x*FRACUNIT, y*FRACUNIT, scale, v.cachePatch("MSONW3"), V_80TRANS)
			v.drawScaled(x*FRACUNIT, y*FRACUNIT, scale, v.cachePatch("MSONW2"), V_60TRANS)
			v.drawScaled(x*FRACUNIT, y*FRACUNIT, scale, v.cachePatch("MSONW1"))
		end
    end
end, "game")
addHook("MobjThinker", function(mo)
	if mo and mo.valid and mo.health --just make sure it still fucking exists
		if mo.targ and mo.targ.valid and mo.targ.health --make sure player exists and is alive
			if not (mo.falloff and mo.tics != 7*2)
				P_TeleportMove(mo, mo.targ.x+mo.xoff, mo.targ.y+mo.yoff, mo.targ.z+mo.zoff)
			elseif (mo.falloff)
				mo.momz = mo.targ.momz/2
				mo.momx = mo.targ.momx/2
				mo.momy = mo.targ.momy/2
			end
		else--if player is dead
			P_RemoveMobj(mo)
		end
	end
end, MT_WEREHOGFOG)