freeslot("S_PLAY_PEEL1", "S_PLAY_PEEL2", "S_PLAY_PEEL3", "S_PLAY_PEEL4", "SPR_PEEL", "S_PLAY_PWALK1", "S_PLAY_PWALK2", "S_PLAY_PWALK3", "S_PLAY_PWALK4", "S_PLAY_PWALK5", "S_PLAY_PWALK6", "S_PLAY_PWALK7", "S_PLAY_PWALK8", "S_PLAY_SPEEL1", "S_PLAY_SPEEL2")
states[S_PLAY_PWALK1] = {SPR_PLAY, SPR2_WALK1, 2, nil, 0, 0, S_PLAY_PWALK2}
states[S_PLAY_PWALK2] = {SPR_PLAY, SPR2_WALK2, 2, nil, 0, 0, S_PLAY_PWALK3}
states[S_PLAY_PWALK3] = {SPR_PLAY, SPR2_WALK3, 2, nil, 0, 0, S_PLAY_PWALK4}
states[S_PLAY_PWALK4] = {SPR_PLAY, SPR2_WALK4, 2, nil, 0, 0, S_PLAY_PWALK5}
states[S_PLAY_PWALK5] = {SPR_PLAY, SPR2_WALK5, 1, nil, 0, 0, S_PLAY_PWALK6}
states[S_PLAY_PWALK6] = {SPR_PLAY, SPR2_WALK6, 1, nil, 0, 0, S_PLAY_PWALK7}
states[S_PLAY_PWALK7] = {SPR_PLAY, SPR2_WALK7, 1, nil, 0, 0, S_PLAY_PWALK8}
states[S_PLAY_PWALK8] = {SPR_PLAY, SPR2_WALK8, 1, nil, 0, 0, S_PLAY_PEEL1}
states[S_PLAY_PEEL1] = {SPR_PEEL, A, 1, nil, 0, 0, S_PLAY_PEEL2}
states[S_PLAY_PEEL2] = {SPR_PEEL, B, 1, nil, 0, 0, S_PLAY_PEEL3}
states[S_PLAY_PEEL3] = {SPR_PEEL, C, 1, nil, 0, 0, S_PLAY_PEEL4}
states[S_PLAY_PEEL4] = {SPR_PEEL, D, 1, nil, 0, 0, S_PLAY_PEEL1}
states[S_PLAY_SPEEL1] = {SPR_PLAY, SPR2_RUN_1, 1, nil, 0, 0, S_PLAY_SPEEL2}
states[S_PLAY_SPEEL2] = {SPR_PLAY, SPR2_RUN_2, 1, nil, 0, 0, S_PLAY_SPEEL1}
addHook("ThinkFrame", do
	for player in players.iterate
		if not player.mo return end
			if player.mo and player.mo.skin == "ultsonic"
			//CONS_Printf(player, player.mo.peelouttimer)
				if player.mo.state == S_PLAY_STND
					player.normalspeed = 36*FRACUNIT
				end
				if player.mo.peelouttimer == nil
					player.mo.peelouttimer = 0
				end
				if player.mo.peelout == nil
					player.mo.peelout = 0
				end
				if player.mo.peeloutrun == nil
					player.mo.peeloutrun = 0
				end
				if player.mo.peelsound == nil
					player.mo.peelsound = 0
				end
				if player.mo.peelsound2 == nil
					player.mo.peelsound2 = 0
				end
				if player.pflags & PF_SPINNING
					player.mo.peelouttimer = 0
				end
				if (P_PlayerInPain(player))
				or (player.playerstate == PST_DEAD)
					player.normalspeed = 36*FRACUNIT
					player.mo.peelouttimer = 0
					player.mo.peelout = 0
					player.mo.peeloutrun = 0
					player.mo.peelsound = 0
					player.mo.peeloutsound = 0
				end
				if P_IsObjectOnGround(player.mo)
				and player.speed == 0
				and not (player.pflags & PF_SPINNING)
				and player.cmd.buttons & BT_CUSTOM1
			    and not (player.playerstate == PST_DEAD)
					if player.mo.peelsound == 0
						S_StartSound(player.mo, sfx_cdfm11)
						player.mo.peelsound = 1
						
					end
					if player.mo.peelouttimer < 60
						player.mo.peelouttimer = $1 + 1
					end
					player.normalspeed = 0
					player.pflags = $1|PF_STASIS
					if player.drawangle ~= player.mo.angle
						local diff = 0
						local factor = 0
						diff = player.mo.angle - player.drawangle
						factor = 8
						if diff > ANGLE_180
							diff = InvAngle(InvAngle(diff)/(factor or 1))
						else
							diff = $ / (factor or 1)
						end
						player.drawangle = $ + diff
					
					end
					if not (player.mo.eflags & MFE_GOOWATER) then
						for i=(leveltime%7)/2,(leveltime%7) do
							local particle = P_SpawnMobj(player.mo.x, player.mo.y,
							player.mo.z + ((player.mo.eflags & MFE_VERTICALFLIP)/MFE_VERTICALFLIP * (player.mo.height - mobjinfo[MT_SPINDUST].height)),
							MT_SPINDUST)
							if (player.powers[pw_shield] == SH_ELEMENTAL and not (player.mo.eflags & MFE_TOUCHWATER or player.mo.eflags & MFE_UNDERWATER)) then
								particle.state = S_SPINDUST_FIRE1
								particle.bubble = false
							elseif (player.mo.eflags & MFE_TOUCHWATER or player.mo.eflags & MFE_UNDERWATER) then
								particle.state = S_SPINDUST_BUBBLE1
								particle.bubble = true
							else
								particle.state = S_SPINDUST1
								particle.bubble = false
							end
					
							particle.target = player.mo
							particle.eflags = $1 | (player.mo.eflags & MFE_VERTICALFLIP)
							particle.scale = player.mo.scale*2/3
							P_SetObjectMomZ(particle, player.mo.peelouttimer*FRACUNIT/50+P_RandomByte()<<10, false)
							P_InstaThrust(particle, player.mo.angle+(P_RandomRange(-ANG30/FRACUNIT, ANG30/FRACUNIT)*FRACUNIT), FixedMul(-player.mo.peelouttimer*FRACUNIT/12-1*FRACUNIT-P_RandomByte()<<11, player.mo.scale))
							P_TryMove(particle, particle.x+particle.momx, particle.y+particle.momy, true)
						end
					end
				end
			if not (player.cmd.buttons & BT_CUSTOM1) 
				if ((player.mo.peelouttimer >= 1) and (player.mo.peelouttimer <= 35) and not (player.powers[pw_super]))
				or ((player.mo.peelouttimer >= 1) and (player.mo.peelouttimer <= 14) and (player.powers[pw_super]))
					player.normalspeed = 36*FRACUNIT
					player.mo.state = 14
					player.mo.peelouttimer = 0
					player.mo.peelsound = 0
				end
			end
			
			if player.powers[pw_super] == 0
				if player.mo.peelouttimer >= 1
				and player.mo.peelouttimer <= 10
					if not (player.mo.state >= S_PLAY_PWALK1)
					and (player.mo.state <= S_PLAY_PWALK8)
						player.mo.state = S_PLAY_PWALK1
					end
				end
				if player.mo.peelouttimer >= 15
					if not ((player.mo.state >= S_PLAY_PEEL1) and (player.mo.state <= S_PLAY_PEEL4))
					or ((player.mo.state >= S_PLAY_PWALK1) and (player.mo.state <= S_PLAY_PWALK8))
						player.mo.state = S_PLAY_PEEL1
					end
				end
			else
				if player.mo.peelouttimer >= 1
				and player.mo.peelouttimer <= 1
					if not (player.mo.state >= S_PLAY_PWALK4)
					and (player.mo.state <= S_PLAY_PWALK8)
						player.mo.state = S_PLAY_PWALK4
					end
				end
				if player.mo.peelouttimer >= 1
					if not (player.mo.state >= S_PLAY_SPEEL1)
					and (player.mo.state <= S_PLAY_SPEEL2)
						player.mo.state = S_PLAY_SPEEL1
					end
				end
			end
			if not (player.cmd.buttons & BT_CUSTOM1)
				if ((player.mo.peelouttimer >= 10) and not (player.powers[pw_super]))
				or ((player.mo.peelouttimer >= 1) and (player.powers[pw_super]))
					if player.mo.peelsound == 1
						player.mo.state = S_PLAY_RUN
						S_StartSound(player.mo, sfx_cdfm01)
						player.mo.peelsound = 0
						player.mo.peelout = 1
					end
					P_InstaThrust(player.mo, player.mo.angle, FixedMul(60*FRACUNIT, player.mo.scale))
					player.mo.peelouttimer = 0
					player.drawangle = player.mo.angle
				end
				
			end
			if player.speed > 40*FRACUNIT
			and player.mo.peelout == 1
				player.mo.peeloutrun = 1
				player.normalspeed = 60*FRACUNIT
			end
			if player.speed <= 40*FRACUNIT
			and player.mo.peeloutrun == 1
				player.mo.peelout = 0
				player.mo.peeloutrun = 0
				player.normalspeed = 36*FRACUNIT
			end
			if player.mo.peelout == 1
				player.charflags = $1|SF_RUNONWATER
			else
				player.charflags = $1 & ~SF_RUNONWATER
			end
			if player.pflags & PF_JUMPED
				if ((player.mo.state >= S_PLAY_PWALK1) and (player.mo.state <= S_PLAY_PWALK8))
				or ((player.mo.state >= S_PLAY_PEEL1) and (player.mo.state <= S_PLAY_PEEL4))
				or ((player.mo.state >= S_PLAY_SPEEL1) and (player.mo.state <= S_PLAY_SPEEL2))
					player.mo.state = S_PLAY_ROLL
				end
				if player.normalspeed == 0*FRACUNIT
					player.normalspeed = 36*FRACUNIT
				end
				player.mo.peelouttimer = 0
				player.mo.peelsound = 0
				player.mo.peelsound2 = 0
			end
		end
	end
end)

//fast run
freeslot("S_FASTER1", "S_FASTER2", "S_FASTER3", "S_FASTER4", "SPR_FAST")

states[S_FASTER1] = {SPR_FAST, A, 1, nil, 0, 0, S_FASTER2}
states[S_FASTER2] = {SPR_FAST, B, 1, nil, 0, 0, S_FASTER3}
states[S_FASTER3] = {SPR_FAST, C, 1, nil, 0, 0, S_FASTER4}
states[S_FASTER4] = {SPR_FAST, D, 1, nil, 0, 0, S_FASTER1}
addHook("ThinkFrame", do
    for player in players.iterate
        if (player.mo and player.mo.skin == "ultsonic")
		and not player.powers[pw_super] then
            if (player.speed > 60*FRACUNIT)
				if not player.powers[pw_sneakers]
					player.normalspeed = 60*FRACUNIT
				end
				player.charflags = $1|SF_RUNONWATER
				
				if (player.mo and player.mo.skin == "ultsonic")
				and not player.powers[pw_super]
					if not (player.mo.state >= S_FASTER1)
					and (player.mo.state <= S_FASTER4)
					and not (player.pflags & PF_SPINNING)
					and P_IsObjectOnGround(player.mo)
					and (player.panim == PA_RUN)
						player.mo.state = S_FASTER1
					end
				end
			elseif (player.speed >= 0)
			and (player.speed < 53*FRACUNIT)
				player.charflags = $1 & ~SF_RUNONWATER
				if (player.mo and player.mo.skin == "ultsonic")
				and not player.powers[pw_super] then
					player.normalspeed = 36*FRACUNIT
				end	
				if (player.mo.state >= S_FASTER1) and (player.mo.state <= S_FASTER4)
				and not ((player.mo.state >= S_PLAY_PWALK1) and (player.mo.state <= S_PLAY_PWALK8))
				and P_IsObjectOnGround(player.mo)
					player.mo.state = S_PLAY_SKID
				end
            end
        end
    end
end)


//Sonic & ashuro run ghosts
addHook("PlayerThink", function(player)
	if not (player.mo and player.mo.skin == "ultsonic") then return end
	if (player.powers[pw_super])
	and player.speed + abs(player.mo.momz) > FixedMul(20*FRACUNIT,player.mo.scale)
		local gmobj = P_SpawnGhostMobj(player.mo)
		gmobj.fuse = 3
		if gmobj.tracer then gmobj.tracer.fuse = 3 end
		if (leveltime & 1)
			gmobj.frame = $&~FF_TRANSMASK
			gmobj.frame = $|tr_trans70<<FF_TRANSSHIFT
			if (gmobj.tracer)
				gmobj.tracer.frame = $&~FF_TRANSMASK
				gmobj.tracer.frame = $|tr_trans70<<FF_TRANSSHIFT
			end
		end
	end
end)

COM_AddCommand("csonicsupermusic", function(player)
	if not player.cssm
		player.cssm = true
		CONS_Printf(player,"sonic 3 super music")
	else
		player.cssm = false
		CONS_Printf(player,"sonic 2 super music")
	end
end)
//Music
addHook("ThinkFrame", do
    for player in players.iterate do
        if (player.mo and player.mo.valid)
        player.hassupermusic = true
            if not player.powers[pw_extralife]
            and not (player.exiting)
            if player.powers[pw_super] and not (player.hyper and player.hyper.capable) then
                if player.mo.skin == "ultsonic"
                    if player.cssm == true
                    S_ChangeMusic("s3super",true,player) //(ALT) Sonic Mania Super theme
                    else
                    S_ChangeMusic("s2super",true,player) //SATFS Stardom Santuary's True Final Boss
                    end
                end
            end    
        else
            P_RestoreMusic(player)
            end
        end
    end
end)