if not CBW_Battle return end --Load Battlemod first, THEN SMS.
--============================================BATTLEMOD=====================================================
--Currently, SMS 3.2 doesn't support battlemod. He was made with Battlemod  version 5 and 6 in mind primarily.
--This code is for those versions, but hasn't been updated since SMS v2, so it may not work there either anymore.
--===========================================================================================================
local OGsonicskinvars
local SMSskinvars = {
weight = 110,
flags = 0,
shields = 0, --glass cannon, high risk.
special = CBW_Battle.Action.SMSTransform,
guard_enabled = false, --no guarding.
guard_frame = 2, --shouldn't apply anyway, but...
func_guard_trigger = CBW_Battle.GuardFunc.Parry,
func_priority = CBW_Battle.Priority_FullCommon,
func_priority_ext = nil
}

--Battlemod thinker aider for all skins.
rawset(_G, "SMS_BattleModThinker", function(s, p)
	if not CBW_Battle.SkinVars["sms"]
		OGsonicskinvars = CBW_Battle.SkinVars["sonic"] --Save Sonic's original stats
		CBW_Battle.SkinVars["sonic"] = SMSskinvars
		CBW_Battle.SkinVars["sms"] = SMSskinvars
		CWB_Battle.Action.SMSTransform = {} --Empty table for legacy reasons. We actually use the SMS_BattleMod" thinker!
	end
	if not s.supersmsrecoil
		if ((p.powers[pw_nocontrol] > 5) and (s.recoilthrust and (s.recoilthrust > 10<<16)) )
			s.supersmsrecoil = true
			P_SetObjectMomZ(s, -6<<16, true)
			A_SMSquake(s, 60<<16, 5, 1500)
			S_StartSound(s, sfx_smscrt)
			p.powers[pw_nocontrol] = $*2
			s.recoilthrust = $*5/4
			P_SpawnGhostMobj(s)
			P_SpawnMobjFromMobj(s, 0, 0, 0, MT_BURSTWAVE)
			A_SMSSTOMPSMOKE(s, 30, MT_HMSLIGHTNING2, 0, 20<<16, FRACUNIT/2, SKINCOLOR_LAVENDER)
			if not P_IsObjectOnGround(s)
				s.supersmsrecoillanding = true
			end
		end
	elseif s.supersmsrecoil
		if s.supersmsrecoillanding
			S_StartSound(s, sfx_smscrt)
			if not p.smsgraphix
				local ghs = P_SpawnMobj(s.x, s.y, s.z-20<<16, MT_HMSWAVEAURA)
				ghs.dustwave = 1
				ghs.sprite = SPR_SMMK
				ghs.frame = 3
				ghs.target = s
				ghs.fuse = 15
				ghs.scale = s.scale
				ghs.destscale = $*2
			end
			A_SMSSTOMPSMOKE(s, 60, MT_SPINDUST, nil, nil, 2<<16)
			A_SMSSTOMPQUAKE(s, 45, 14, FRACUNIT, 0, 0, 0, 0, SKINCOLOR_SUPERGOLD3)
			A_SMSSTOMPQUAKE(s, 80, 18, 2<<16, 0, ANG20, 0, 0, SKINCOLOR_SUPERSILVER4)
			s.supersmsrecoillanding = false
		end
		if (p.powers[pw_nocontrol] <= 1)
			s.supersmsrecoil = false					
		else
			if P_IsObjectOnGround(s) and CBW_Battle and CBW_Battle.DoDebris
				s.sprite = SPR_SMSO
				s.frame = Q|FF_FULLBRIGHT
				s.ghs = P_SpawnGhostMobj(s)
				s.ghs.tics = 3
				CBW_Battle.DoDebris(s,P_RandomChance(FRACUNIT/2),P_RandomRange(5,20))
			end
			if P_RandomRange(0,1)
				S_StartSound(s, sfx_cdfm42)
			end
			A_SMSquake(s, P_RandomRange(30,60)<<16, 2, 1200)
		end					
	end
	if (p.rings > 299)
		p.sms_flamedelay = 10
		if (p.cmd.buttons & (BT_ATTACK|BT_FIRENORMAL))
		and not (p.hypermysticsonic or p.sms_ringburstfly or p.sms_stompanim or p.exiting or p.homing)
		and not p.powers[pw_nocontrol]
		and not p.exiting and not P_PlayerInPain(p) and not (p.charmed and p.charmedtime)
		and not p.powers[pw_carry]
		and not (p.pflags & (PF_GAMETYPEOVER|PF_STASIS))
			if P_IsObjectOnGround(s)
			P_SetObjectMomZ(s, 50<<16, false)
			end
			p.sms_flamedelay = 15
			A_TOGOEVENFURTHERBEYOND(s) //Become Hyper Mystic Sonic!
		end		
	end
/*	if p.captureamount --Mild capture speed nerf incase SMS becomes OP here.
		if (leveltime % 5 == 0) 
		and s.lastcaptureamountsms and (s.lastcaptureamountsms < p.captureamount)
		p.captureamount = $-1 //Slightly slower capture speed for SMS.
		end			
	s.lastcaptureamountsms = p.captureamount
	end */
	
	if p.smsnobmgame == 2
		if not p.sms_transformstate
			p.presmsskin = "sonic"
			s.playerhomecharge = false
			A_SMSTOSONIC(s, nil, true, true)
			S_StartSound(s, sfx_appear, p)
			P_FlashPal(p, PAL_MIXUP, 3)		
			CONS_Printf(p, "\x82"+"Sonic's special Battle ability is now the Super Mystic transformation!")
			if s.skin != "sms" and p.sms_originalcolor
				s.ssittcpls = p.sms_originalcolor
				s.ssittcpls2 = 2
			end
			if not p.smssonicbattle
				p.smssonicbattle = true
				CBW_Battle.SkinVars["sonic"] = SMSskinvars --Turn off the original stats!
				CBW_Battle.SkinVars["sms"] = SMSskinvars return --
			end
		else
			if p.powers[pw_shield] //No extra hits while in battle mode!
				if not SMSSHIELD(p, SH_PINK, SH_PITY)
				and not s.hmsshieldconsumecd
					P_GivePlayerRings(p, 5) //But feel free to gain some extra rings!
					s.hmsshieldconsume = 18
					s.hmsshieldconsumecd = 105
				end
				P_RemoveShield(p)
				p.powers[pw_shield] = 0
			end
			if (p.sms_transformstate < 2)
				p.sms_transformstate = 2 //Only 1 hitpoint in battle mode!
			end
		end		
	elseif p.smsnobmgame == 1
  		p.presmsskin = "sonic"
	 	A_SMSTOSONIC(s, nil, true, true)	
	 	S_StartSound(s, sfx_lose)
	 	CONS_Printf(p, "\x85"+"SMS is not allowed in Battlemode matches right now")
		if p.smssonicbattle
			p.smssonicbattle = false
			if OGsonicskinvars != nil --Swap back to Sonic's original stats.
				CBW_Battle.SkinVars["sonic"] = OGsonicskinvars
			end
		end
		return --
   	end
end)

--Battlemod script. I've ended support after battlemod version 6, so it may not even work anymore.
rawset(_G, "SMS_BattleMod", function(s, p)
	local mo = s --Failsafe. Because most modders use "mo" instead of "s", I suppose.
	--Instant Transformation for battle.
	if s.skin == "sonic" and (p.cmd.buttons & (BT_ATTACK|BT_FIRENORMAL)) and (p.rings > 29) 
		if not P_PlayerInPain(p)
		and not (p.charmed and p.charmedtime)		
		and not p.powers[pw_nocontrol]
		and not (p.pflags & (PF_STASIS|PF_FULLSTASIS))
		and not p.exiting and not p.powers[pw_carry]
		and s.health	
			p.sms_originalcolor = p.skincolor //Remember the player's original color.
			P_FlashPal(p, PAL_MIXUP, 7)
				
			if p.powers[pw_super]
				p.powers[pw_super] = 0
				P_RestoreMusic(p)
			end
				
			p.presmsskin = 0
			s.skin = "sms"	
			R_SetPlayerSkin(p, "sms") //For the life icon.
				
			if not (p.powers[pw_shield] & SH_FIREFLOWER)
			and not ((mariomode) and p.powers[pw_invulnerability])
				s.color = 0
			end
			
			if not p.smscoatc5 or not p.shinyglosscoat
				p.skincolor = SKINCOLOR_MYSTICAL4
			end
			
			s.SMS_locked = true
			S_StartSound(s, sfx_s3k9f)
			p.sms_transformstate = 2
			s.state = S_PLAY_SPRING
			p.pflags = $ & ~PF_JUMPED
			p.sms_hugeflametoss = 0
			p.powers[pw_flashing] = 85 //Some invincibility upon transforming.
			s.flags2 = $|MF2_DONTDRAW
			p.sms_flamedelay = 10
			p.powers[pw_nocontrol] = 2
			P_GivePlayerRings(p, -30)			
			for d = 0,15
				P_SpawnParaloop(s.x, s.y, s.z+FixedMul(20<<16, s.scale), 10<<16, 16, MT_NIGHTSPARKLE, d*ANGLE_22h, S_SMSTSPARK1, false)
			end		
			if G_GametypeHasTeams()
				if p.ctfteam == 1
					p.sms_originalcolor = SKINCOLOR_RED
					s.color = SKINCOLOR_RED
					p.skincolor = SKINCOLOR_RED
				elseif p.ctfteam == 2
					p.sms_originalcolor = SKINCOLOR_BLUE
					s.color = SKINCOLOR_BLUE
					p.skincolor = SKINCOLOR_BLUE						
				end				
			end
		end
	end
	
	--Don't display shield gauge, SMS can't use them anyway.
	--	CBW_Battle.Action.SMSTransform = function(s, doaction)	
	p.action2text = nil
	
	if s.skin == "sms"
   		if p.hypermysticsonic
		 	p.actiontext = nil --As far as you can go!
 	 	 	p.actionrings = nil
   		else
  	 		p.actiontext = "Hyper Mystic Sonic" --Because a good time is waiting!
 	 	 	p.actionrings = 300
		end
		return true --
		
  	elseif p.smssonicbattle
	
  		p.actiontext = "Super Mystic Sonic" --You can become SMS with 30+ rings!
  		p.actionrings = 30
		if s.skin == "sonic" --You're weaker than usual when untransformed, slow down the speed thok.
			p.actionspd = 37<<16 
		end
		return true --
		
  	elseif P_IsObjectOnGround(s) --Go back to default abilities, SMSbattle is turned off.				
  		p.actiontext = "Super Jump"
  		p.actionrings = 10				
  	else			
  		p.actiontext = "Ground Pound"
  		p.actionrings = 10				
  	end
	
	--If we got this far, return to Sonic's vanilla Battlemod ability behaviour!
  	doaction = CBW_Battle.CheckRings(p,$)
	
  	local water = CBW_Battle.WaterFactor(s)
  	local thrust = 18<<16
  	local jumpflags = (p.pflags|PF_JUMPED|PF_THOKKED) & ~(PF_STARTJUMP|PF_NOJUMPDAMAGE|PF_GLIDING|PF_BOUNCING|PF_SPINNING)
	
  	if p.actionstate == 0
  		if doaction == 1 then
  			CBW_Battle.PayRings(p)
  			if P_IsObjectOnGround(s) then
  				p.actionstate = 2 
  				thrust = 18<<16/water
  				P_SetObjectMomZ(s,thrust,true) 
  				S_StartSound(s,sfx_s3k3c)
  				S_StartSound(s,sfx_kc5b)
  				p.pflags = jumpflags
  				s.state = S_PLAY_ROLL
  				p.secondjump = 0
  			else
  				thrust = 24<<16/water
  				p.actionstate = 1 
  				P_SetObjectMomZ(s,-thrust,false) 
  				S_StartSound(s,sfx_zoom)
  				s.state = S_PLAY_ROLL
  				p.pflags = jumpflags
  				p.secondjump = 0
  			end
  		end

  	elseif p.actionstate == 1 

  		p.actioncooldown = TICRATE/2
  		if s.momz*P_MobjFlip(s) > 0
  			p.actionstate = 0
  			s.state = S_PLAY_SPRING
  			p.pflags = $ & ~PF_JUMPED
  		else
  			if P_IsObjectOnGround(s) then
  				thrust = 10<<16/water
  				P_SetObjectMomZ(s,thrust,true) 
  				S_StartSound(s,sfx_s3k5f)
  				p.state = S_PLAY_SPRING
  				P_InstaThrust(s,R_PointToAngle2(0,0,s.momx,s.momy),FixedHypot(s.momx,s.momy)/4)
  				for n = 0, 23
  					local p = P_SPMAngle(s,MT_GROUNDPOUND,s.angle+n*ANG15,0)
  					if p and p.valid then
  						p.momz = s.scale*P_MobjFlip(s)*2/water
  						p.fuse = TICRATE/3
		  			end
		  		end
		  	end
	  	end
  	elseif p.actionstate == 2
  		if (s.momz*P_MobjFlip(s) < 0)
  			s.state = S_PLAY_FALL
  			p.pflags = $ & ~PF_JUMPED
  			p.actionstate = 0
  		end
  		if P_IsObjectOnGround(s)
  			p.actionstate = 0
  		end
  	end
	
  	if p.actionstate and (p.pflags & PF_JUMPED)
	  	P_SpawnThokMobj(p)
  	end
end)

--Battlemod specific MT_PLAYER thinker.
addHook("MobjThinker", function(s) if not s.player return end local p = s.player local yu = p.smsrtable
	if p.smssonicbattle or s.battlemodsms	
		if s.skin == "sms" or s.skin == "sonic"
			local VARS = CBW_Battle.SkinVars[s.skin]
			if VARS and CWB_Battle.Action.SMSTransform != nil
			and VARS.special == CWB_Battle.Action.SMSTransform
				SMS_BattleMod(s, p) --Battle Script! Let Sonic Transform into SMS! Supports only up to version 6 of Battlemod.
			end
		end
		if s.battlemodsms --Battlemod behaviour.
			if s.skin != "sms"
				if s.ssittcpls2
					s.ssittcpls2 = $-1
					p.skincolor = s.ssittcpls
					s.color = s.ssittcpls
					p.sms_originalcolor = s.ssittcpls
					if not s.ssittcpls2
						s.ssittcpls = nil
					end
				end
				if s.lastbmrings --Refund rings and maybe spawn a shield to make it easier to re-transform
					if not s.supersmsrecoil2
						if not (CBW_Battle and CBW_Battle.SkinVars)
							p.shieldstock = 1 --Extra reserve pity shield
						end
						if (s.lastbmrings > 10)
							P_GivePlayerRings(p, 10)
							if not p.powers[pw_shield]
								if (s.lastbmrings > 59)
									p.powers[pw_shield] = SH_FORCE|1
								elseif (s.lastbmrings > 29)
									p.powers[pw_shield] = SH_PITY
								end
								S_StartSound(s, sfx_shield)
								P_NukeEnemies(s, s, 0)
								P_SpawnShieldOrb(p)
							end
						else
							P_GivePlayerRings(p, s.lastbmrings)
						end
					end
					s.lastbmrings = 0
				end
			else
				SMS_BattleModThinker(s, p)
				if p.hypermysticsonic
					if p.hms_drain == 1 and CBW_Battle.ControlPoint 
					and (CBW_Battle.ControlPoint.LeadCapAmt or CBW_Battle.ControlPoint.LeadCapPlr)
						S_StartSound(s, sfx_antiri, p)
						P_GivePlayerRings(p, -1) //Drain MORE rings when control points are being captured!
					end
					if p.captureamount
						if s.lastcaptureamounthms and (s.lastcaptureamounthms < p.captureamount)
							p.captureamount = $-1 //Half the speed of capture as HMS, on top of the SMS slowdown.
						end			
						s.lastcaptureamounthms = p.captureamount
					end
				end
			end
		end
	end
	if s.health
		if s.supersmsrecoil2
			s.recoilthrust = nil
			p.powers[pw_nocontrol] = 0
			if s.supersmsrecoil2 == 17
				s.rollangle = 0
				s.supersmsrecoil2 = 16
				if s.supersmsrecoil2ang != nil
				P_InstaThrust(s, s.supersmsrecoil2ang, -65*s.scale)
				end
			elseif (s.eflags & MFE_JUSTHITFLOOR)
				s.rollangle = 0
				s.supersmsrecoil2 = 0
				s.supersmsrecoil = false						
				if CBW_Battle and CBW_Battle.DoDebris
					S_StartSound(s, sfx_cdfm42)
					for d = 0,08
					CBW_Battle.DoDebris(s,P_RandomChance(FRACUNIT/2),P_RandomRange(5,20))
					end
				end
			elseif s.supersmsrecoil2 == 16
				s.supersmsrecoil = false
				s.rollangle = $-FixedAngle(35<<16)
				if (abs(s.momx)+abs(s.momy)) < 11*s.scale
					s.state = S_PLAY_DRWN
					P_SetObjectMomZ(s, 3<<16)
					s.momx,s.momy = 0,0
					s.supersmsrecoil2 = $-1
					S_StartSound(s, sfx_cdfm42)
					S_StartSound(s, sfx_kc40)
					S_StartSound(s, sfx_s3k9b)						
					A_SMSquake(s, 60<<16, 8, 901)
					P_SpawnMobjFromMobj(s, 0, 0, 0, MT_BURSTWAVE)
					S_StartSound(s, sfx_altow1)
					p.rings = 10
					P_PlayerRingBurst(p, 10)
					p.rings = 0
					local bsl = P_SpawnMobj(s.x, s.y, s.z-12<<16, MT_BURSTLIGHT)
					bsl.color = SKINCOLOR_WHITE
					for d = 0,15
						CBW_Battle.DoDebris(s,P_RandomChance(FRACUNIT/2),P_RandomRange(5,20))
					end	
				end
			elseif (s.supersmsrecoil2 > 1)
				s.rollangle = 0
				s.supersmsrecoil = false
				s.supersmsrecoil2 = $-1
				s.state = S_PLAY_DRWN
				s.momz = $/99 s.momx = 0 s.momy = 0
			else
				s.rollangle = 0
				s.state = S_PLAY_PAIN					
				s.supersmsrecoil2 = 0
			end
		end
	end
end, MT_PLAYER)

//Recoil off enemies from tricks if you hit them! This needs to go BEFORE the transformation damage hook.
addHook("MobjDamage", function(s, i, so) if not (s.battlemodsms or s.player) return end --
	local p = s.player
	if p.hypermysticsonic and s.skin == "sms"--What damage?
		return true --
	end
	if p.footsweepanim
		if i and i.player and (i.player.pflags & (PF_SPINNING|PF_JUMPED))
			p.footsweep01 = 0
			p.footsweep02 = 0
			p.footsweep03 = 0
			p.footsweepanim = false
			p.sweepanimrecovery = 0
			p.smsdivetimer = 0
			p.smsdivefloat = 0
			p.smsatomicdivefail = 0
			s.state = S_PLAY_ROLL
			p.pflags = $|PF_SPINNING
			return true --
		end
	end
	if so and so.player and so.skin == "sms"
		if i and (i.type == MT_SMSFIREBLAST) and not (i.mysticflame and i.fuse and (i.fuse < 297))
			if p.powers[pw_nocontrol] //You CAN hurt already stunned players
			
			else
				p.panim = PA_FALL
				s.state = S_PLAY_FALL
				p.pflags = $&~(PF_GLIDING|PF_JUMPED|PF_BOUNCING|PF_THOKKED|PF_SPINNING) s.sms_spintrickspeed = 30
				p.powers[pw_nocontrol] = (i.mysticflame) and 56 or 45
				p.powers[pw_flashing] = 2
				S_StartSound(s, sfx_kc4d)
				s.flags2 = $|MF2_DONTDRAW
				s.recoilangle = i.angle
				s.recoilthrust = 48<<16 //Regular flames only stun players for balance reasons
				if i and i.valid and i.health
					P_ExplodeMissile(i)
				end
				return true --
			end
		end
	end

	if s.skin != "sms" return end --Be SMS from here on out.
	
	//If you've got a good dive going, don't let enemies stop your fun.
	if (p.smsdivetimer and (p.smsdivetimer > 9))
		if i and (i.flags & (MF_ENEMY|MF_BOSS))
			return true --
		end
	end
	--Crash with superarmor off some things that WOULD've hurt you!
	if (p.footsweepanim or ((s.state == S_SMS_AIRBOOST) and p.sms_didairboost > 1))
		if i 
			if SMS_ArmoredCrash(s, p, i) return true end --
		elseif SMS_ArmoredCrash(s, p, so)
			return true --
		end
	elseif i
		if (p.sms_boostlevel > 64 and p.speed > 44*s.scale)
		or (s.state == S_SMS_AIRBOOST)
		or ((p.cmd.buttons & BT_CUSTOM1 or p.sms_autoboosthold) and p.speed > 88*s.scale and p.panim == PA_RUN)	
			--Negate damage once if you hit stuff with an airboost.	
			--Fling with momentum off stuff you're crashing into!
			if (i.flags & MF_PAIN)
			or (i.flags & (MF_ENEMY|MF_BOSS))
			or (i.type == MT_DETON)
		
				if (so and so.player) --None of this from player-owned objects.
				or p.rings <= 0 and not p.powers[pw_shield] --You're about to die entirely at this rate.
					return
				end
				A_SMSquake(s, 65<<16, 4, 1500)
				s.momx = $*2/3
				s.momy = $*2/3
				if (s.momz*P_MobjFlip(s) < 6*s.scale)
					P_SetObjectMomZ(s, 6<<16)
				else
					P_SetObjectMomZ(s, 2<<16, true)
				end
				if (i.type == MT_DETON)
					P_DamageMobj(i)	
				end
				if p.powers[pw_shield] --Recreate SRB2's damage behaviour ,but keep your speed.
					P_RemoveShield(p)
					S_StartSound(s, sfx_shldls)
				else
					P_PlayRinglossSound(s)
					if p.sms_transformstate == 1
						p.sms_transformstate = 2
						P_PlayerRingBurst(p, -1) 
						P_GivePlayerRings(p, p.rings/-2)
					else	
						P_PlayerRingBurst(p, -1)
						P_GivePlayerRings(p, p.rings*-1)
						if p.sms_transformstate == 2 or  p.sms_transformstate == 3
							s.state = S_PLAY_PAIN 
							p.panim = PA_PAIN
							A_SMSTOSONIC(s, true)
						end
					end
				end		
				s.SMS_CrashRoll = {timer = 27, extratime = false}			
				SMS_RagDoll(s)
				p.powers[pw_flashing] = max($,105)
				return true --
			end
		end
	end
 
  	if p.powers[pw_shield] //Don't drop stuff or detransform if you're shielded
		return
	end
	
	//SMS can take 2 hits when manually transformed before detransforming, not counting shields.
	//The first hit he has an extra 50% defense, only losing half of his rings.
	//pw_bouncering, pw_railring, pw_automaticring, pw_explosionring, pw_scatterring, and pw_grenadering 
	
	if p.sms_transformstate
	   if p.sms_transformstate == 1 and i
	   and not ( (i.flags & MF_PAIN|MF_ENEMY|MF_BOSS) and ((p.sms_boostlevel > 64) or s.airboostimmune) )		 
			if (p.rings > 49)
				P_DoPlayerPain(p)
				P_PlayerRingBurst(p, -1)
				P_PlayRinglossSound(s)
				p.sms_transformstate = 2
				P_GivePlayerRings(p, p.rings/-2)
				HMS_AuraStorm(s, nil, SKINCOLOR_RED)
				SMS_LightClash(s, SKINCOLOR_RED, s)	
				return true --
				
			elseif p.rings == 1
			
				S_StartSound(s, sfx_shldls)
				P_DoPlayerPain(p)
				P_PlayerRingBurst(p, 0)
				p.sms_transformstate = 2	
				HMS_AuraStorm(s, nil, SKINCOLOR_RED)
				SMS_LightClash(s, SKINCOLOR_RED, s)	
				return true --
				
			elseif (p.rings == 0) //Block a death blow!
				P_DoPlayerPain(p)
			--	S_StartSound(s, sfx_shldls)
			
				local ghs = P_SpawnGhostMobj(s)
				ghs.destscale = $*99
				ghs.blendmode = AST_ADD
				ghs.colorized = true
				ghs.fuse = 13 ghs.tics = 99
				
				P_FlashPal(p, PAL_MIXUP, 4)
			--	S_StartSound(target, sfx_s3k4e)
				if p.presmsskin
					R_SetPlayerSkin(p, p.presmsskin)
					s.skin = p.presmsskin					
				else
					R_SetPlayerSkin(p, "sonic")
					s.skin = "sonic"
				end
				--Give score bonus and send SMS flying
				s.lastbmrings = p.rings
				if s.supersmsrecoil and i
					P_SetObjectMomZ(s, 10<<16)				
					s.supersmsrecoil2ang = R_PointToAngle2(s.x, s.y, i.x, i.y)
					P_InstaThrust(s, s.supersmsrecoil2ang, -65*s.scale)
					s.supersmsrecoil2 = 17
					if so and so.player
						P_FlashPal(so.player, PAL_MIXUP, 4)
					end
				end
				if so and so.player and (so.skin != "sms")
					if s.supersmsrecoil
						so.player.score = $+50 //Slight score boost for hitting SMS!
						CONS_Printf(so.player, "\x82"+"Gained 50 score bonus for taking down SMS hard!")
					else
						so.player.score = $+25 //Slight score boost for hitting SMS!
						CONS_Printf(so.player, "\x82"+"Gained 25 score bonus for taking down SMS!")
					end
				end
				if not (p.powers[pw_shield] & SH_FIREFLOWER)
				and not ((mariomode) and p.powers[pw_invulnerability])
					s.color = p.sms_originalcolor
				else
					s.color = SKINCOLOR_WHITE
				end
				p.skincolor = p.sms_originalcolor
				p.sms_transformstate = 0
				SMS_LightFlash(s, SKINCOLOR_RED)	
				SMS_LightningTrail(s, s)
				HMS_GigaJetBolt(s)
				SMS_AngledLightning(s, SKINCOLOR_RED, 23, 3-SMS_Graphix(s))
				P_PlayRinglossSound(s)
				S_StartSound(s, sfx_smscrt)
				S_StartSoundAtVolume(s, sfx_nerf, 120)
				S_StartSound(s, sfx_smscr2)		
				return true --
			else
				P_DoPlayerPain(p)
				P_PlayerRingBurst(p, p.rings/2)
				P_PlayRinglossSound(s)
				p.sms_transformstate = 2
				P_GivePlayerRings(p, p.rings/-2)
				return true --
			end
			
		elseif (p.sms_transformstate == 2) or (p.sms_transformstate == 3)
		
			local ghs = P_SpawnGhostMobj(s)
			ghs.destscale = $*99
			ghs.blendmode = AST_ADD
			ghs.colorized = true
			ghs.fuse = 13 ghs.tics = 99
			P_FlashPal(p, PAL_MIXUP, 4)
		--	S_StartSound(s, sfx_s3k4e)
			if p.presmsskin
				R_SetPlayerSkin(p, p.presmsskin)
				s.skin = p.presmsskin					
			else
				R_SetPlayerSkin(p, "sonic")
				s.skin = "sonic"
			end
			--Give score bonus and send SMS flying
			s.lastbmrings = p.rings
			if s.supersmsrecoil and i
				P_SetObjectMomZ(s, 10<<16)				
				s.supersmsrecoil2ang = R_PointToAngle2(s.x, s.y, i.x, i.y)
				P_InstaThrust(s, s.supersmsrecoil2ang, -65*s.scale)
				s.supersmsrecoil2 = 17
				S_StartSound(s, sfx_smscrt)
				S_StartSoundAtVolume(s, sfx_nerf, 110)
				S_StartSound(s, sfx_smscr2)
				if so and so.player
					P_FlashPal(so.player, PAL_MIXUP, 4)
				end
			end
			if so and so.player and (so.skin != "sms")
				if s.supersmsrecoil
					so.player.score = $+50 //Slight score boost for hitting SMS!
					CONS_Printf(so.player, "\x82"+"Gained 50 score bonus for taking down SMS hard!")
				else
					so.player.score = $+25 //Slight score boost for hitting SMS!
					CONS_Printf(so.player, "\x82"+"Gained 25 score bonus for taking down SMS!")
				end
			end
			if not (p.powers[pw_shield] & SH_FIREFLOWER) and not ((mariomode) and p.powers[pw_invulnerability])
				s.color = p.sms_originalcolor
			else
				s.color = SKINCOLOR_WHITE
			end
			p.skincolor = p.sms_originalcolor
			p.sms_transformstate = 0
			S_StartSoundAtVolume(s, sfx_nerf, 77)
			S_StartSound(s, sfx_smscrt)
			SMS_LightFlash(s, SKINCOLOR_WHITE)
			SMS_AngledLightning(s, SKINCOLOR_SUPERSKY2, 23, 3-SMS_Graphix(s))
			SMS_LightningTrail(s, s)
			return --
		end	
	end
end, MT_PLAYER)
