if GS_RAILS_SKINS==nil rawset(_G,"GS_RAILS_SKINS",{}) end  --Make sure your own lump has this line too!

local RAIL = GS_RAILS if RAIL and RAIL.blockload return end --Don't include this part in your own file
if GS_RAILS_SKINS["metalsonic"] return end --Already have a table.

--Metal Sonic is difficult to handle, but can auto-accelerate gradually up to very high speeds with his thrusters!
GS_RAILS_SKINS["metalsonic"] = {}
local SKIN = GS_RAILS_SKINS["metalsonic"]

SKIN["S_SKIN"] = {
SIDEHOP = 38, --Superfast sidehop!
SIDEHOPARC = 5,

CROUCH = true,
AUTOBALANCE = false,
MUSTBALANCE = true, --Balance well or else!

HEROESTWIST = 0, --Cannot Heroes Twist
MAXSPEED = 199, --Higher maxspeed than most.

SLOPEMULT = -10, --Much less affected by slopes.
ACCELSPEED = 30, --Can self-accelerate, albeit slower than Tails.
ACCELCAP = 79, --But to a much higher speed!

NOSIDEHOPGFX = 2,
}

--Enter edge animation, spin horizontally and spawn purple afterimages like in the Metal Sonic fight.
SKIN["CustomSideHop"] = function(p, s, GS, rail, SIDEJUMP)
	S_StartSoundAtVolume(s, sfx_mswarp, 69) --Play this on top of the other sound
	GS.forcesprite = SPR2_EDGE
	
	s.rollangle,s.spriteyoffset,s.spritexoffset = 0,-2<<16,0 
	s.spritexscale,s.spriteyscale = FRACUNIT+20000,FRACUNIT-5000	
	GS.stretch = {timer = 2, Xspeed = 10000, Yspeed = 2500}
	
	return "nostretch"
end

SKIN["PlayerThink"] = function(p, s, GS, rail)
	if GS.railswitching
		if (p.pflags & (PF_JUMPED|PF_NOJUMPDAMAGE)) and not GS.grinding and not P_IsObjectOnGround(s)
		and not (p.pflags & (PF_THOKKED|PF_SLIDING|PF_SPINNING|PF_STARTDASH)) and not p.powers[pw_carry]
			if (GS.railswitching > 1)
				if (p.speed > 5*s.scale) and GS.railswitchANG!=nil and (RAIL) --Speen.
					local _,P = RAIL.AngleDifference(GS.railswitchANG, R_PointToAngle2(s.x,s.y, s.x+s.momx,s.y+s.momy),2,ANG60,2)
					GS.forcesprite = SPR2_EDGE
					GS.forceangle = GS.railswitchANG - (GS.railswitching*ANG1*43*P)
					
					local ghs = P_SpawnGhostMobj(s) --Metal Sonic's boss fight ghosts.
					ghs.scale = s.scale
					ghs.spritexscale,ghs.spriteyscale = s.spritexscale,s.spriteyscale
					ghs.flags = $|MF_NOBLOCKMAP|MF_NOCLIPTHING|MF_NOCLIP|MF_NOCLIPHEIGHT
					ghs.sprite = SPR_METL
					ghs.tics,ghs.fuse = 16,16
					ghs.frame = Q|FF_FULLBRIGHT
				end
				p.pflags = $|PF_JUMPDOWN
				if (s.state!=S_PLAY_ROLL)
					if p.panim==PA_FALL or p.panim==PA_SPRING or p.panim==PA_ROLL or p.panim==PA_JUMP
						s.state = S_PLAY_ROLL
					end
				end
			elseif (p.cmd.buttons & BT_JUMP) --Auto-float afterwards if player is holding jump!
				p.pflags = $ & ~PF_JUMPDOWN
			end
		end
	end
end