--===========================MAIN THINKERS==============================
--Main gameplay related thinkers. Most of Samus' behaviour is here.
--======================================================================

local SamOncePerMap1 = true
local SamOncePerMap2 = true

addHook("MapChange", function(gamemap)
	SamOncePerMap1 = false
	SamOncePerMap2 = false
	for z in players.iterate
		z.sam_oncepermap = true
		//Remove bonus items at the start of matches
		if z.sam_saveselect
			z.sam_dontsave = true
		end
		if z.sam_intoship
		z.pflags = $ & ~PF_INVIS & ~PF_GODMODE & ~PF_FORCESTRAFE
		end
		if (gametype != GT_COOP)
		
			z.sam_dontsave = true
			z.sam_saveselected = 0 --Reset all save settings outside coop, please.
			z.samuschosefile = nil
			z.crystalflashchance = 0
			z.sam_emeraldflags = 0
			if z.sam_saveselect
			z.sam_saveselect = nil
			end
			if z.sam_autotank
				z.sam_autotank = nil
			end
			if z.sam_ammodigitizer and z.sam_ammodigitizer != 1
				z.sam_ammodigitizer = 1
			end
		elseif (z.jointime > 1) and z.mo
			if z.sam_dontsave
			z.sam_dontsave = false
			else
			z.sam_dosave = true --Gotta do this to update the gamemap properly
			end
			z.sam_changedmap = true
		end
	z.scannedmons = nil
	z.scannedplays = nil
	end
end)

local function StartSamSPBS(s,p)
	if p.samspbschargefull
		return
	end
	s.spbssfx = P_SpawnMobj(s.x, s.y, s.z, MT_THOK) --Don't make this a local value.
	s.spbssfx.flags2 = $|MF2_DONTDRAW
	s.spbssfx.tics = 90
	S_StartSound(s.spbssfx, sfx_sprnc)

	S_StartSound(s, sfx_sprna)
	s.sam_flashfade = 8
	s.sam_flashfadecolor = SKINCOLOR_SAPPHIRE						
	SamusTutorial(s, "variasuit", true)
	p.charflags = $ & ~SF_NOSKID
	p.runspeed = 2199594
	s.sam_supertilt = 25
	s.sam_afterboost = 7
	p.samspbschargefull = true
	s.sam_spbsstart = 15
	local ghs = P_SpawnMobj(s.x, s.y, s.z, MT_PBEXPANDI)
		if ghs and ghs.valid
			ghs.color = SKINCOLOR_AZURE
			ghs.scale = s.scale+5*FRACUNIT
			ghs.momz = s.momz*10/11
			ghs.momy = s.momy*10/11
			ghs.momx = s.momx*10/11
			ghs.scalespeed = $*11
			ghs.destscale = $*45
		end

		local ghs = P_SpawnMobjFromMobj(s, 
		FixedMul(120*FRACUNIT, cos(s.angle)),
		FixedMul(120*FRACUNIT, sin(s.angle)),
		-5*s.scale,
		MT_THOK)
		
		ghs.sprite = SPR_MXP4
		ghs.frame = V|FF_FULLBRIGHT|TR_TRANS80
		ghs.blendmode = AST_ADD
		ghs.scale = s.scale/3
		ghs.momx = s.momx*2/3
		ghs.momy = s.momy*2/3
		ghs.momz = s.momz+((-9*FRACUNIT)*P_MobjFlip(ghs))
		ghs.tics = 22
		ghs.fuse = 11
		ghs.scalespeed = $*2
		ghs.destscale = $*99			
		
end

local function MorphballReturn(s, var1) --Unmorph from Morph Ball.
if not s.player or s.player and s.player.sugoigolfmode or (s and s.samspinpad and s.samspinpad < 5)
return end
local p = s.player

			p.visorclosein = 15
			p.sammorphed = false
			p.sams_usecd = 9
			p.sams_morphcd = 35
			p.sams_notyet = 3
			p.sams_jumpcd = 10
			p.sams_firecd = 0
			p.sams_beamcombocharge = 0
			p.sams_shotcd = 22
			p.samspbschargefull = false
			p.sam_spacejumped = false
			p.samspbscharge = 0
			p.pflags = $ & ~PF_STARTDASH & ~PF_SPINNING
			p.sam_shinesparkstop = 0
			p.sambsblchargefull = false
			p.sams_supermissileready = 0
			p.normalspeed = skins[s.skin].normalspeed
			p.acceleration = skins[s.skin].acceleration
			p.charflags = $ & ~SF_RUNONWATER
			
			s.samunmorphed = 4
			s.samslopespin = 0
			s.samunmx = s.x
			s.samunmy = s.y
			s.samunmz = s.z
			SamSpiderCharge(p, s, 103)
			s.sam_mbspeedf = nil
			s.friction = 59392
			s.sambsblchargelvl = 0
			s.morphedmachactive = false
			s.morphedmachspeed = 0
			s.scale = 90000							
			s.flags = $ & ~MF_BOUNCE
			s.momx = ($*5)/8
			s.momy = ($*5)/8
			p.sam_framejump = 0
			
				if not var1
					S_StartSound(s, sfx_morphb)
				end
				if p.bsblboost and p.samspbs
					p.samspbscharge = 65
				end
				if s.sam_deathball
					p.sams_firecd = 99
				end
				if p.sam_firstperson and not var1
					SamCam(p, false)
					p.visorclosein = 15
				end
				if P_IsObjectOnGround(s) or p.powers[pw_carry]
					s.state = S_PLAY_RUN
				else
					s.state = S_PLAY_FALL
				end
				
			p.bsblboost = 0
			p.bsblboostattack = 0
			
				if s.samspinpad 
					s.samspinpad = 0
				end
				
			--We did it, bois! We finally stopped Samus getting stuck in walls when unscaling. Praise space jesus!
				if not (P_TryMove(s, s.x, s.y, false))	
					for d = 1,18
						if (P_TryMove(s, s.x+FixedMul(-20*FRACUNIT, cos(ANG20*d)), s.y+FixedMul(-20*FRACUNIT, sin (ANG20*d)), false))
							return
						end
					end
				end	
end

local function ScrewAttackParticles(s,p)
if (leveltime % 2 == 0)
	local ghs = P_SpawnMobj(s.x, s.y, s.z+(s.height/2), MT_PBEXPANDW)
	if ghs
	ghs.blendmode = AST_ADD
	ghs.scale = $*P_RandomRange(1,3)
	ghs.scalespeed = $*P_RandomRange(1, 8)
	ghs.destscale = $*20
	end
	ghs = P_SpawnMobj(s.x+FixedMul(-150*FRACUNIT, cos(s.angle)), s.y+FixedMul(-150*FRACUNIT, sin (s.angle)), (s.z+(s.height/2))-(s.momz*2), MT_PBEXPAND)
	if ghs
	ghs.blendmode = AST_ADD
	ghs.scale = $*P_RandomRange(1,2)
	ghs.scalespeed = $*P_RandomRange(2, 4)
	ghs.destscale = $*25
	return ghs
	end
else
A_SAMPARTICLE(s, MT_MPR4, 35, 4, -4, 4, SKINCOLOR_GREEN, P_RandomRange(75355, 98355))
local ghs = P_SpawnMobj(s.x, s.y, s.z, MT_PBEXPANDI)
	if ghs
	ghs.blendmode = AST_ADD
	ghs.scale = $*P_RandomRange(1,3)
	ghs.scalespeed = $*P_RandomRange(3, 9)
	ghs.destscale = $*25
	return ghs
	end
end
end
local function ScrewAttackLandingGFX(s,p)
		local ghs = SamShinySplat(s,SKINCOLOR_SUPERGOLD5, 12)
		ghs.frame = A|TR_TRANS30|FF_FULLBRIGHT
		ghs.scale = $*3
		ghs.momx = s.momx/2
		ghs.momy = s.momy/2
			for d = -2,2,1
				local ghs = P_SpawnMobjFromMobj(s,0,0,s.height/5, MT_THOK)
				ghs.angle = (s.angle+ANGLE_90)+(ANG10*d)
				P_InstaThrust(ghs, ghs.angle-ANGLE_90, p.speed)
				P_SetObjectMomZ(ghs, s.scale)
				ghs.sprite = SPR_PHAZ
				ghs.spriteyscale = 42000
				ghs.spritexscale = 85000
				ghs.color = SKINCOLOR_SUPERGOLD5+2-(d*3)
				ghs.colorized = true
				ghs.tics = 25
				ghs.fuse = 15
				ghs.frame = J|TR_TRANS30|FF_PAPERSPRITE|FF_FULLBRIGHT
				ghs.blendmode = AST_ADD
				ghs.destscale = $*99	
			end
end
local function ScrewAttackBonk(s,p)
	if P_IsObjectOnGround(s) or ( (p.speed < (FixedMul(22*FRACUNIT, s.scale))) and not ((s.momz > (FixedMul(13*FRACUNIT, s.scale)) or (s.momz < (FixedMul(-13*FRACUNIT, s.scale))))))
	P_SetObjectMomZ(s, 4*FRACUNIT)
	P_SamFP(p, PAL_NUKE, 6)
	s.momx = $/3
	s.momy = $/3
	
		if P_IsObjectOnGround(s) --Screw Attack ground slam!
			ScrewAttackLandingGFX(s,p)
		elseif (p.speed < (FixedMul(22*FRACUNIT, s.scale)))
			P_Thrust(s, s.angle, -14*FRACUNIT)
			if (p.cmd.buttons & BT_JUMP)
			s.momz = ($*3)/2
			end
		end
	
		if not (p.powers[pw_carry] == CR_MACESPIN)
		and not p.sammorphed
			s.state = S_PLAY_FALL
		end
		
		A_Samuskillstuff(s, 265*FRACUNIT, 150*FRACUNIT, s, true) --Do this first

		if not (gametyperules & GTR_POINTLIMIT)
			P_RadiusAttack(s, s, 320*FRACUNIT)
		end

		CancelScrewAttack(s,p)
		p.sam_c1ready = 0
		p.sams_screwattackcd = 45
		for d = 1,20
			local t = P_SpawnMobj(s.x, s.y, s.z+s.height/2, MT_MINECARTSPARK)
			t.blendmode = AST_ADD
			t.scale = s.scale/12
			t.fuse = 30
			P_InstaThrust(t, P_RandomRange(1,359)*ANG1, P_RandomRange(12,24)*FRACUNIT)
			t.momx = $+(s.momx*2) t.momy = $+(s.momy*2)
			t.momz = P_RandomRange(4,7)*FRACUNIT
		end	
		A_SAMPARTICLE(s, MT_MPR1, 30, 18, -1, 5, SKINCOLOR_YELLOW, 35000, true)
		A_SAMPARTICLE(s, MT_MPR1, 50, 14, -1, 6, SKINCOLOR_WHITE, 23000, true)
		A_SAMPARTICLE(s, MT_MPR1, 50, 15, -1, 8, SKINCOLOR_GREEN, 23000, true)
		A_SAMPARTICLE(s, MT_MPR1, 50, 12, -1, 8, SKINCOLOR_GREEN, 23000, true)
		A_SAMPARTICLE(s, MT_MPR5, 50, 14, -1, 16, SKINCOLOR_WHITE, 89355)
		A_SAMPARTICLE(s, MT_MPR4, 50, 22, -1, 8, SKINCOLOR_GREEN, 99999)
		A_SAMPARTICLE(s, MT_MPR4, 50, 11, -1, 8, SKINCOLOR_EMERALD, 94955)
		A_SAMPARTICLE(s, MT_MPR5, 40, 15, -1, 16, s.color, 98999)
		A_SAMPARTICLE(s, MT_MPR5, 50, 16, -1, 16, SKINCOLOR_WHITE, 93999)
		A_SAMPARTICLE(s, MT_MPR4, 50, 22, -1, 8, SKINCOLOR_GREEN, 99000)
		A_SAMPARTICLE(s, MT_MPR4, 50, 19, -1, 8, SKINCOLOR_EMERALD, 96000)
		A_SAMUSEXPLOSIONS(s, 12, MT_SAMMISSILESMOKE, 25*FRACUNIT, 400000)
		A_SAMUSEXPLOSIONS(s, 26, MT_SAMMISSILESMOKE, false, 80000)
		S_StartSound(s, sfx_satkh)
	end
end

--Originally Chargehomer.
local function AttractSamPickUps(s, dist, speed)
local truth = false

	searchBlockmap("objects", function(s, m)
		if m and ((m.type >= MT_MPKU) and (m.type <= MT_NRGH))
		and (m.z < s.z+dist)
		and (m.z > s.z-dist)
		and m.health
		and not (m.flags2 & MF2_AMBUSH) and not m.dontsuckme
		
			if (speed < 2)	--Regular homing
			local ghs = P_SpawnGhostMobj(m)
			ghs.fuse = 7
			ghs.tics = 9
			ghs.scale = $/2
			ghs.destscale = ghs.scalespeed+1
			ghs.blendmode = AST_ADD	
			P_HomingAttack(m, s)
			
			else --Instant!
			--Keep counting down by -1 until we reach 0
			local ang = R_PointToAngle2(m.x, m.y, s.x+s.momx, s.y+s.momy)
			local ex = FixedHypot(m.x-s.x, m.y-s.y)
			if ex <= 4
			ex = 4
			end
			ex = $/(8*FRACUNIT)
			if ex > 24
			ex = 24
			elseif ex < 4
			ex = 4
			end
			local ghs
				for i = 1,ex
					local zet = s.z
					ghs = P_SpawnGhostMobj(m)
					ghs.blendmode = AST_ADD
					P_MoveOrigin(ghs, ghs.x+FixedMul(24*FRACUNIT*i, cos(ang)), ghs.y+FixedMul(24*FRACUNIT*i, sin(ang)), s.z)
					ghs.destscale = ghs.scalespeed+1
					ghs.momx = s.momx
					ghs.momy = s.momy
					ghs.momz = s.momz
					
					if i >= ex and m
						local ghs2 = P_SpawnGhostMobj(m)
						ghs2.sprite = SPR_MBOM
						ghs2.frame = I|FF_FULLBRIGHT|TR_TRANS60
						ghs2.blendmode = AST_ADD
						ghs2.scale = $/8
						ghs2.destscale = $*99
						ghs2.scalespeed = $*2
						ghs2.colorized = true
						ghs2.color = SKINCOLOR_SUPERSILVER1
						P_HomingAttack(ghs2, s)
						truth = true
						P_MoveOrigin(m, s.x+s.momx, s.y+s.momy, s.z+s.momz)
					return
					end
				end
			end
			if m and m.valid
			m.chargehomertime = 3
			truth = true
			end
		end
  	end, s, s.x-dist, s.x+dist, s.y-dist, s.y+dist)
	
	if truth
	return true
	else
	return false
	end
end

local function DoAreaScan(s, p, scanmsg) --Does the scan visor finding thing.

	s.sam_scanning = 0
	s.waittoscan = 0
	s.samusjustscanned = true
	p.scannedmons = {}
	p.scannedplays = {}
	s.totalscannedmons = 0
	s.totalscannedplays = 0
	s.totalscannedplayshidden = 0
	S_StopSoundByID(s, sfx_sscan1)
		
	if p.sam_hyperbeam and p.sam_hyperbeampanic
	return
	end		
		
--Scan!
	for z in players.iterate
		if z.mo and (z.mo != s) and z.mo.health
			if ((gametype == GT_COOP) or not ((z.sammorphed or z.mo.wavebuffering or z.mo.samspawnprotect) and not z.sam_hyperbeam))
			table.insert(p.scannedplays, z.mo)
			s.totalscannedplays = $+1
			else
			s.totalscannedplayshidden = $+1
			end
		end
	end
	for m in mobjs.iterate()
			if m.valid
			and ((m.type == MT_TOKEN) or ((m.type >= MT_EMERALD1) and (m.type <= MT_EMERALD7)) or (m.flags & MF_MONITOR) and m.health and not (m.flags2 & MF2_DONTDRAW))
			and (FixedHypot(p.mo.x-m.x, p.mo.y-m.y) <= 4000*FRACUNIT)
			and not ((m.type == MT_EGGMAN_BOX) or (m.type == MT_EGGMAN_GOLDBOX) or (m.type == MT_GRAVITY_BOX))
			and not (G_GametypeHasTeams() and ((((m.type == MT_RING_REDBOX) and p.ctfteam == 2)) or ((m.type == MT_RING_BLUEBOX) and p.ctfteam == 1)))
				if (m.type == MT_TOKEN) or ((m.type >= MT_EMERALD1) and (m.type <= MT_EMERALD7)) or G_GametypeHasSpectators() 
				or not ( (m.samgotme != nil and not (players[0] and p == players[0]) and (p.samusnum and (m.samgotme & (p.samusnum)) or m.samgotme == p.samusnum) )
	  			and not (players[0] and p == players[0] and m.samgotmehost) and not (mobjinfo[m.type].doomednum == 429) )
				table.insert(p.scannedmons, m)
				s.totalscannedmons = $+1
				end
			end
	end
--Found something?
	if s.totalscannedmons <= 0 --Nope.
		p.scannedmons = nil
		p.scannedplays = nil
		s.sam_scanpowerup = 0
		s.samradaranim = 9
		s.samradaranimd = false
		S_StartSound(s, sfx_sdenid, p)
		P_SamFP(p, PAL_NUKE, 4)
	else --Yes!
		s.samradaranim = 1
		s.samradaranimd = true
		P_SamFP(p, PAL_RECYCLE, 5)
		S_StartSound(s, sfx_sscan2, p)
		if (gametype == GT_COOP)
		s.sam_scanpowerup = 2100 
		else
		s.sam_scanpowerup = 800
		end
	end
	if scanmsg and not (s.samusmission and s.samusmission.active)
			if (gametype != GT_COOP)
				if s.totalscannedplays
					if s.totalscannedplayshidden
					SamusMission(s, "   TRACKING\x82 "+s.totalscannedplays+"\x80 TARGET LOCATION(S).", "\x85"+s.totalscannedplayshidden+"\x80 TARGET(S) EVADED TRACKING.", nil, 4, 0, false)
					else
					SamusMission(s, "   TRACKING\x82 "+s.totalscannedplays+"\x80 TARGET LOCATION(S).", nil, nil, 4, 0, false)
					end
				elseif s.totalscannedplayshidden
					if s.totalscannedplayshidden == 1
					SamusMission(s, "THE TARGET HAS EVADED DETECTION. ", nil, nil, 4, 0, false)
					else
					SamusMission(s, "ALL TARGETS HAVE EVADED DETECTION.", nil, nil, 4, 0, false)
					end
				else
				SamusMission(s, "FAILED TO ACQUIRE NEARBY TARGET(S).", nil, nil, 4, 0, false)
				end
			else
			
				if not s.totalscannedmons
				SamusMission(s, "FAILED TO ACQUIRE NEARBY TARGET(S).", nil, nil, 4, 0, false)
				else
				SamusMission(s, "ACQUIRED\x84 "+s.totalscannedmons+"\x80 NEARBY ARTIFACT SIGNAL(S). ", nil, nil, 4, 0, false)
				s.totalscannedmons = $+1
				end
			end	
	end


end


local function SpeedBoosterGhosts(s, p, cf) --Spawn Speed Booster ghosts.
if cf
	s.speedghost = P_SpawnMobjFromMobj(s, 0, 0, 0, MT_OVERLAY)
	s.speedghost.target = s
	s.speedghost.state = S_THOK
	s.speedghost.skin = s.skin
	s.speedghost.sprite = s.sprite
	s.speedghost.sprite2 = s.sprite2
	s.speedghost.frame = s.frame|TR_TRANS60|FF_FULLBRIGHT
	s.speedghost.angle = p.drawangle
	s.speedghost.tics = 3 s.speedghost.fuse = 3
	s.speedghost.blendmode = AST_ADD
	s.speedghost.scale = s.scale
	s.speedghost.flags2 = s.flags2	
	s.speedghost.eflags = s.eflags
	s.speedghost.color = SKINCOLOR_SUPERRED2
	s.speedghost.colorized = true
	if SAM_displayplayer(p) and not CV_FindVar("chasecam").value and not netgame --Don't render in first person, it'll just clutter your view.
		s.speedghost.flags2 = $|MF2_DONTDRAW
	end
	return s.speedghost
end
					
	if (leveltime % 2 == 0)
	s.speedghost = P_SpawnMobjFromMobj(s, 0, 0, 0, MT_OVERLAY)
	s.speedghost.target = s
	s.speedghost.skin = s.skin
	s.speedghost.sprite = s.sprite
	s.speedghost.sprite2 = s.sprite2
	s.speedghost.frame = s.frame|TR_TRANS70|FF_FULLBRIGHT
	s.speedghost.angle = p.drawangle
	s.speedghost.tics = 2
	s.speedghost.blendmode = AST_ADD
	s.speedghost.scale = s.scale
	s.speedghost.flags2 = s.flags2
	s.speedghost.eflags = s.eflags
	s.speedghost.color = SKINCOLOR_SUPERSKY3
	s.speedghost.colorized = true
		if SAM_displayplayer(p) and not CV_FindVar("chasecam").value
			s.speedghost.flags2 = $|MF2_DONTDRAW
		end		
	end

	if (s.eflags & MFE_VERTICALFLIP)
	s.speedghost = P_SpawnMobj(s.x, s.y, s.z-s.height*5/13, MT_THOK)
	else
	s.speedghost = P_SpawnMobj(s.x, s.y, s.z, MT_THOK)
	end
	s.speedghost.skin = s.skin
	s.speedghost.sprite = s.sprite
	s.speedghost.sprite2 = s.sprite2
	s.speedghost.frame = s.frame|TR_TRANS90|FF_FULLBRIGHT
	s.speedghost.angle = p.drawangle
	s.speedghost.tics = 7
	s.speedghost.scale = s.scale
	s.speedghost.blendmode = AST_ADD
	s.speedghost.flags2 = s.flags2
	s.speedghost.eflags = s.eflags
	s.speedghost.colorized = true
	if SAM_displayplayer(p) and not CV_FindVar("chasecam").value
			s.speedghost.flags2 = $|MF2_DONTDRAW
	end	
	
	if s.speedghost and s.speedghost.valid and (leveltime % 3 == 0 )
		s.speedghost.color = SKINCOLOR_ICY
		s.speedghost.frame = s.frame|TR_TRANS60|FF_FULLBRIGHT
	elseif s.speedghost and s.speedghost.valid
		s.speedghost.color = SKINCOLOR_SUPERSKY3
	end
	
end
local function CheckForShineSpark(s, p) --Enter the shinespark state?
	if p.skidtime
	and not (twodlevel or (s.flags2 & MF2_TWOD)) 

	or (twodlevel or (s.flags2 & MF2_TWOD))
	and (p.cmd.forwardmove < 0)
			if (p.sam_energy and p.sam_energy > 29)
 			and not s.samspinpadconfirmed
			p.samspbscharge = 0
			p.sampbschargefull = false
			s.ghs = P_SpawnMobj(s.x, s.y, s.z, MT_EXPLODE)
			s.ghs.scale = s.scale
			p.bsblboost = 0
			p.bsblboostattack = 0
			p.sam_shinespark = 80
			S_StartSound(s, sfx_sprnp)

				if (twodlevel or (s.flags2 & MF2_TWOD))
				p.pflags = $ & ~PF_SPINNING
				P_InstaThrust(s, s.angle, 0)
				p.powers[pw_nocontrol] = $+4
				else
				P_InstaThrust(s, s.angle, FixedMul(9*FRACUNIT, s.scale))
				end
			return true
			end
	end
	return false
end
local function PrimeRunPhysics(s, p) --Special physics for Samus.
if p.noprimerun
	p.noprimerun = $-1
return
end
if ((s.flags2 & MF2_TWOD) or twodlevel) or (p.pflags & PF_SPINNING)
return
end
--Start of prime run
			--Update our controls every frame.
			local spbonus = 0 --speedbooster adjustments.
			local forwardmove = p.cmd.forwardmove
			local sidemove = p.cmd.sidemove
			if (p.samspbschargefull and (p.speed > 20*s.scale))	
			spbonus = 195
			end
			if (sidemove < 0)
				sidemove = $*-1
			end
			if (forwardmove < 0)
				forwardmove = $*-1
			end
			if forwardmove > sidemove
				p.sam_direct = forwardmove
			else
				p.sam_direct = sidemove --Decide how hard we're pressing the stick! From 0 to 50!
			end
			p.sam_turnang = (p.cmd.angleturn<<16)+(R_PointToAngle2(0, 0, p.cmd.forwardmove<<16, -(p.cmd.sidemove<<16)))
			
			--Morph Ball physics
			
			if p.sammorphed
				if not P_IsObjectOnGround(s)
				or (p.sam_direct < 20)
					if s.friction > 62200
					s.friction = 59392
					end
					return
				end
				local momangle = R_PointToAngle2(s.x, s.y, s.x+s.momx, s.y+s.momy)
				local trueturn = (momangle-p.sam_turnang)
				if trueturn != 0
					trueturn = $/ANG1
					if trueturn < 0
						trueturn = $*-1
					end
				end	
				if (trueturn < 80) and (p.speed >= 35*FRACUNIT)
					s.friction = 64930-(trueturn*16)
					s.sam_mbspeedf = true
				elseif s.friction > 62200
					s.friction = 59392
				end
			return --Always return here if morphed.
			end
			
			if P_IsObjectOnGround(s) --Grounded.
			and (p.speed > 5*s.scale) and (p.sam_direct > 3)
			local momangle = R_PointToAngle2(s.x, s.y, s.x+s.momx, s.y+s.momy)
			local trueturn = (momangle-p.sam_turnang)
				if trueturn != 0
					trueturn = $/ANG1
					if trueturn < 0
						trueturn = $*-1
					end
				end	
				if (trueturn > 14)
					if (trueturn > 150)
						return
					end					
					local premomx = s.momx
					local premomy = s.momy
						local oldtrueturn = trueturn				
						trueturn = ($*10)/40
						
						if trueturn > 100					
							trueturn = 100  --Max turn value! ...just incase.
						end
						local moveang = R_PointToDist2(s.x, s.y, s.x+s.momx, s.y+s.momy)
						local xadd = P_ReturnThrustX(s, p.sam_turnang, moveang)+p.cmomx
						local yadd = P_ReturnThrustY(s, p.sam_turnang, moveang)+p.cmomy
						
						if (xadd or yadd)
						P_InstaThrust(s, R_PointToAngle2(s.x-s.momx,s.y-s.momy, s.x+xadd, s.y+yadd), moveang)						
											
								--It's CALCULATION TIME. The harder you turn the stick, the more
								--your old momentum will be converted into this fancy new direction

								s.momx = FixedMul(s.momx, FixedDiv(trueturn, 100)) + FixedMul(premomx, FixedDiv(100-trueturn, 100))
								s.momy = FixedMul(s.momy, FixedDiv(trueturn, 100)) + FixedMul(premomy, FixedDiv(100-trueturn, 100))
								
								if oldtrueturn > 40
								--Now gradually slow down, moreso if turning harshly!
									if oldtrueturn > 80
									s.momy = FixedDiv(FixedMul($, 2000), 2000+((trueturn*1000)/(600+spbonus)))
									s.momx = FixedDiv(FixedMul($, 2000), 2000+((trueturn*1000)/(600+spbonus)))
									elseif oldtrueturn > 69
									s.momy = FixedDiv(FixedMul($, 2000), 2000+((trueturn*1000)/(750+spbonus)))
									s.momx = FixedDiv(FixedMul($, 2000), 2000+((trueturn*1000)/(750+spbonus)))										
									else
									s.momy = FixedDiv(FixedMul($, 2000), 2000+((trueturn*1000)/(800+spbonus)))
									s.momx = FixedDiv(FixedMul($, 2000), 2000+((trueturn*1000)/(800+spbonus)))
									end
								end
						end
				end
				
			elseif (p.speed > 5*s.scale) --AIR CONTROL!!!				
				if (p.sam_direct > 3)		
				local momangle = R_PointToAngle2(s.x, s.y, s.x+s.momx, s.y+s.momy)
				local trueturn = (momangle-p.sam_turnang)
					if trueturn != 0
						trueturn = $/ANG1
						if trueturn < 0
						trueturn = $*-1
						end
					end
					if (trueturn > 6) --If the input's not significant, no need for complex physic alterations.
					
					local premomx = s.momx
					local premomy = s.momy
					local moveang = s.angle
							
							trueturn = ($*10)/100
							if (trueturn > 14)
								return
							end	
							moveang = R_PointToDist2(s.x, s.y, s.x+s.momx, s.y+s.momy)
							local xadd = P_ReturnThrustX(s, p.sam_turnang, moveang)+p.cmomx
							local yadd = P_ReturnThrustY(s, p.sam_turnang, moveang)+p.cmomy

							if xadd or yadd
							
							P_InstaThrust(s, R_PointToAngle2(s.x-s.momx,s.y-s.momy, s.x+xadd, s.y+yadd), moveang)

							--It's CALCULATION TIME. The harder you turn the stick, the more
							--your old momentum will be converted into this fancy new direction

							s.momx = FixedMul(s.momx, FixedDiv(trueturn, 100)) + FixedMul(premomx, FixedDiv(100-trueturn, 100))
							s.momy = FixedMul(s.momy, FixedDiv(trueturn, 100)) + FixedMul(premomy, FixedDiv(100-trueturn, 100))
							
							s.momy = FixedDiv(FixedMul($, 2000), 2000+((trueturn*1000)/500)) s.momx = FixedDiv(FixedMul($, 2000), 2000+((trueturn*1000)/500))
							end
					end				
				end
			end	
end

local function OtherTutorials(t,s)
		if (t.missiles or t.beams or t.supermissiles or t.beamcombo or t.gravitysuit1 or t.gravitysuit2 or t.scanvisor
		or t.screwattack or t.bombs or t.hyperbeam)
			return true --There's more
		else
			return false --Nothing left!
		end
end
local function SwitchTutorialBox(t,s)
t.textboxflash = V_90TRANS
t.textboxtrns = V_90TRANS
t.textboxtrns2 = V_90TRANS
t.goodbyedialogue = 0
t.active = true
end
--Check if that's the end of the tutorials or if there's more...
rawset(_G, "EndSamusTutorial", function(t, s, item)

local totaldone = 0
			
	if t.lockoncomplete and not t.lockon
	totaldone = $+1
	elseif t.lockon
	SwitchTutorialBox(t,s)
	return
	end
	
	if t.beamscomplete and not t.beams
	totaldone = $+1
	elseif t.beams
	SwitchTutorialBox(t,s)
	return
	end

	if t.monitorhuntcomplete and not t.monitorhunt
	totaldone = $+1
	elseif t.monitorhunt
	SwitchTutorialBox(t,s)
	return
	end

	if t.chargebeamcomplete and not t.chargebeam
	totaldone = $+1
	elseif t.chargebeam
	SwitchTutorialBox(t,s)
	return
	end
	
	if t.missilescomplete and not t.missiles
	totaldone = $+1
	elseif t.missiles
	SwitchTutorialBox(t,s)
	return
	end
	
	if t.supermissilescomplete and not t.supermissiles
	totaldone = $+1
	elseif t.supermissiles
	SwitchTutorialBox(t,s)
	return
	end

	if t.beamcombocomplete and not t.beamcombo
	totaldone = $+1
	elseif t.beamcombo
	SwitchTutorialBox(t,s)
	return
	end
	
	if t.gravitysuit1complete and not t.gravitysuit1
	totaldone = $+1
	elseif t.gravitysuit1
	SwitchTutorialBox(t,s)
	return
	end	
	
	if t.gravitysuit2complete and not t.gravitysuit2
	totaldone = $+1
	elseif t.gravitysuit2
	SwitchTutorialBox(t,s)
	return
	end

	if t.scanvisorcomplete and not t.scanvisor
	totaldone = $+1
	elseif t.scanvisor
	SwitchTutorialBox(t,s)
	return
	end
	
	if t.screwattackcomplete and not t.screwattack
	totaldone = $+1
	elseif t.screwattack
	SwitchTutorialBox(t,s)
	return
	end	

	if t.bombscomplete and not t.bombs
	totaldone = $+1
	elseif t.bombs
	SwitchTutorialBox(t,s)
	return
	end
	
	if t.pwcomplete and not t.pw
	totaldone = $+1
	elseif t.pw
	SwitchTutorialBox(t,s)
	return
	end	
	
	if t.spiderballcomplete and not t.spiderball
	totaldone = $+1
	elseif t.spiderball
	SwitchTutorialBox(t,s)
	return
	end
	
	if t.hyperbeamcomplete and not t.hyperbeam
	totaldone = $+1
	elseif t.hyperbeam
	SwitchTutorialBox(t,s)
	return
	end	
	
	if not (OtherTutorials(t,s) and t.active)
		t.goodbyedialogue = 12
	end
end)


local function RunSamusTutorial(s,p) --Mobjthinker extension
if p.nosamustutorials
or p.sam_saveselect --Everything else has priority over tutorials.
s.player.samustutorial.dontrender = 1
return false
end
local t = s.player.samustutorial
if p.mo.samusdialogue and p.mo.samusdialogue.active
t.textboxscale = 356
t.textboxtrns = V_90TRANS
return
end
if t.dontrender
t.dontrender = 0
t.textboxscale = 356
t.textboxtrns = V_90TRANS
end

		if not YouAreSamus(s) or p.nosamustutorials
				t.dontrender = 1 return
		elseif t.beams and not p.sam_powerbeamequip
				t.dontrender = 1 return
		elseif t.missiles and not p.sammissiles	
				t.dontrender = 1 return
		elseif t.supermissiles and not p.samsupermissiles
				t.dontrender = 1 return
		elseif t.beamcombo and not (p.sammissiles and p.sammissiles > 14)		
				t.dontrender = 1 return
		elseif t.gravitysuit1 and not p.samspbs	
				t.dontrender = 1 return
		elseif t.scanvisor and not (p.sam_scanvisor or gametype != GT_COOP)
				t.dontrender = 1 return
		elseif t.screwattack and not p.sam_screwattack 
				t.dontrender = 1 return
		elseif t.bombs and not p.sam_bombs
				t.dontrender = 1 return
		elseif t.pw and not p.sampw
				t.dontrender = 1 return
		elseif t.spiderball and not p.sam_spiderball
				t.dontrender = 1 return
		elseif t.hyperbeam and not p.sam_hyperbeam 
				t.dontrender = 1 return --I failed you, amperbee-sama
		end
				
		if not t.goodbyedialogue
				if t.textboxflash
					t.textboxflash = $-FRACUNIT
				end
				if t.textboxscale < FRACUNIT --Gradually expand.
				t.textboxscale = $+5000
				elseif t.textboxtrns2
					t.textboxtrns2 = $-FRACUNIT
				end
				if t.textboxtrns
				t.textboxtrns = $-FRACUNIT
				end				
		else	
			--END THE TUTORIAL THINKER?
		
				t.goodbyedialogue = $-1
						if (t.textboxtrns2 != V_90TRANS)
						t.textboxtrns2 = $+FRACUNIT
						end
						if (t.textboxtrns != V_90TRANS)
						t.textboxtrns = $+FRACUNIT
						end
						if (t.textboxscale > 5000)
						t.textboxscale = $-5000
						end
						if not t.goodbyedialogue
						t.active = false
						end
		end
		if p.exiting
		and not s.sam_tutdismiss --Sometimes things don't go as planned.
		s.sam_tutdismiss = true
		SamusTutorial(s, "beams", true)
		SamusTutorial(s, "missiles", true)
		SamusTutorial(s, "supermissiles", true)
		SamusTutorial(s, "beamcombo", true)
		SamusTutorial(s, "variasuit", true)
		SamusTutorial(s, "gravitysuit", true)
		SamusTutorial(s, "scanvisor", true)
		SamusTutorial(s, "screwattack", true)
		SamusTutorial(s, "bombs", true)
		SamusTutorial(s, "pw", true)
		SamusTutorial(s, "spiderball", true)
		SamusTutorial(s, "hyperbeam", true)			
		end
end

local function RunSamusDialogue(s,p) --Mobjthinker extension
if not s and p
or p.sam_saveselect
return
end
if p.sam_achievement1 and not s.samusforcetext
	if gamemap == 28
		s.samusdialogue = nil
	end
	return	
end
			local t = s.samusdialogue
			--Keep updating colors live
			if s.player.sam_screwattacking and s.player.prescrewcolor --Don't use the screw attack colors!
			t.samuscolor = s.player.prescrewcolor
			else
			t.samuscolor = s.color
			end
			if (t.textboxstyle == 1)
				if (leveltime % 4 == 0)
					if t.tbscolor <= SKINCOLOR_SUPERRED1
					t.tbscolor = SKINCOLOR_SUPERRED1
					t.tbsd = 1
					elseif t.tbscolor >= SKINCOLOR_SUPERRED5
					t.tbscolor = SKINCOLOR_SUPERRED5
					t.tbsd = -1
					end
					t.tbscolor = $+t.tbsd
				end
			end			
			 if t.dontrender --Don't render yet....
			 t.dontrender = $-1
			 	if not t.dontrender
				and not (t.textboxstyle == 5)
			 	S_StartSound(nil, sfx_samtm1, p)
				end
			 else
			 	if not t.goodbyedialogue
					if t.transitflash
					t.transitflash = $-FRACUNIT
					end
					if t.textboxscale < FRACUNIT --Gradually expand dong
					t.textboxscale = $+5000
						if t.textboxtrns
						t.textboxtrns = $-FRACUNIT
						end
					end
				end
				if ( (p.cmd.buttons & BT_TOSSFLAG)
				or not s.health
				or not (YouAreSamus(s) or s.samusforcetext) --If player.mo.samusforcetext is added, don't stop even if you're the wrong skin.
				or s.crystalflashanim or p.sam_intoship
				or p.sam_saveselect)
				and not t.goodbyedialogue --End the text entirely.
				t.goodbyedialogue = 12
				S_StartSound(nil, sfx_ssmenc, p)
				t.textwait = 99
				end
				if t.textwait
				t.textwait = $-1
					for d = 1,2
						if (p.cmd.buttons & BT_CUSTOM1)
						and t.textwait
						t.textwait = $-1
						end
					end
				elseif t.animtimer --Start with animation
				t.animtimer = $-1		
				elseif t.maxchars1 and t.currentchars1 < t.maxchars1 --Do first line
					if leveltime % 2 == 0 or (p.cmd.buttons & BT_CUSTOM1)
						if not (string.sub(t.text1, t.currentchars1, t.currentchars1) == " ") --Don't make a sound if there's a space!
						and not (t.textboxstyle == 5)
							if (t.textboxstyle == 1)
							S_StartSound(nil, sfx_samtm2, p)
							else
							S_StartSound(nil, sfx_samtm3, p)
							end
						end
						if (string.sub(t.text1, t.currentchars1, t.currentchars1) == ".")
						t.textwait = 7
						
						elseif (string.sub(t.text1, t.currentchars1, t.currentchars1) == "?")
						or (string.sub(t.text1, t.currentchars1, t.currentchars1) == "!")
						t.textwait = 15
						end		
					t.currentchars1 = $+1
					end
				elseif t.maxchars2
				and t.currentchars1 >= t.maxchars1 --Do second line
				and t.currentchars2 < t.maxchars2
					if leveltime % 2 == 0 or (p.cmd.buttons & BT_CUSTOM1)
						if not (string.sub(t.text2, t.currentchars2, t.currentchars2) == " ") --Don't make a sound if there's a space!
							if (t.textboxstyle == 1)
							S_StartSound(nil, sfx_samtm2, p)
							else
							S_StartSound(nil, sfx_samtm3, p)
							end
						end
						if (string.sub(t.text2, t.currentchars2, t.currentchars2) == ".")
						t.textwait = 7
						elseif (string.sub(t.text2, t.currentchars2, t.currentchars2) == "?")
						or (string.sub(t.text2, t.currentchars2, t.currentchars2) == "!")
						t.textwait = 15
						end							
					t.currentchars2 = $+1
					end
				elseif t.maxchars3
				and t.currentchars1 >= t.maxchars1 --Do second line
				and t.currentchars2 >= t.maxchars2
				and t.currentchars3 < t.maxchars3
					if leveltime % 2 == 0 or (p.cmd.buttons & BT_CUSTOM1)
						if not (string.sub(t.text3, t.currentchars3, t.currentchars3) == " ") --Don't make a sound if there's a space!
							if (t.textboxstyle == 1)
							S_StartSound(nil, sfx_samtm2, p)
							else
							S_StartSound(nil, sfx_samtm3, p)
							end
						end
						if (string.sub(t.text3, t.currentchars3, t.currentchars3) == ".")
						t.textwait = 7
						elseif (string.sub(t.text3, t.currentchars3, t.currentchars3) == "?")
						or (string.sub(t.text3, t.currentchars3, t.currentchars3) == "!")
						t.textwait = 15
						end							
					t.currentchars3 = $+1
					end				
				elseif not t.goodbyedialogue
					if t.animendtimer --Wait a bit...
					t.animendtimer = $-1
						for d = 1,2
							if ((p.cmd.buttons & BT_CUSTOM1) or (t.textboxstyle == 5))
							and t.animendtimer
							t.animendtimer  = $-1
							end
						end		
					else --Is there another box to go to? They're set up here!
						if t.nextbox
							t.noscale = true
							t.active = false
						--playermobj, line1, line2, icon, style, nextbox, waittime, align1, align2 
							if t.nextbox == 1 --GFZ1
								SamusDialogue(s, "Nothing else to it, the rest of the", "way will have to be on foot.", "SAMPRE", 2, 3, nil)
							elseif t.nextbox == 2 --DSZ2
								if p.sam_icebeam and (p.sams_itemcompletion != nil and (p.sams_itemcompletion < 20))
								SamusDialogue(s, "This is foolishness. Relying on only", "the Ice Beam is much too dangerous.", "SAMDEF", 2, 0)
								elseif p.sam_icebeam
								SamusDialogue(s, "Deep breaths. I should be ready... but ", "Metroids never fail to be unpredictable.", "SAMSAD", 2, 0)
								else
								SamusDialogue(s, "Walking into this without Ice equipment", "is just suicide. Gotta power up. Fast.", "SAMPRE", 2, 0)
								end
							elseif t.nextbox == 3 --GFZ1
								SamusDialogue(s, "But who fired that laser? Does the", "target have access to such firepower?", "SAMSAD", 2, 4)
							elseif t.nextbox == 4 --GFZ1
								SamusDialogue(s, "So much for a simple bounty.", nil, "SAMDEF", 2, 0)
							elseif t.nextbox == 5 --GFZ2	
								SamusDialogue(s, "My arrival was intended as an ambush. ", "Surely Aether's escapee couldn't have taken control over all these machines? ", "SAMPRE", 2, 0, nil, nil, "small")
							elseif t.nextbox == 6 --THZ1	
								SamusDialogue(s, " ...let's focus on the mission. ", nil, "SAMDEF", 2, 0)
							elseif t.nextbox == 7 --THZ2	
								SamusDialogue(s, "Here? So they must've encountered...", nil, "SAMDEF", 2, 0)
							elseif t.nextbox == 8 --CEZ1	
								SamusDialogue(s, "Propagating that would take years of", "experience and specialty knowledge.", "SAMPRE", 2, 9)
							elseif t.nextbox == 9 --CEZ1
								SamusDialogue(s, "Even Space Pirates lack that specialty", "knowledge. What IS this 'smuggler'?", "SAMSAD", 2, 12)
							elseif t.nextbox == 12 --CEZ1
								SamusDialogue(s, "At least an alliance can be ruled out. ", nil, "SAMPRE", 2, 0)
							elseif t.nextbox == 13 --ACZ1	
								SamusDialogue(s, "They're moving fast. How am I", "going to catch up with this gear?", "SAMSAD", 2, 0)
							elseif t.nextbox == 14 --ACZ1	
								SamusDialogue(s, "How's it even possible to move so", "swiftly whilst transporting Metroids?", "SAMPRE", 2, 0)
							elseif t.nextbox == 15 --ACZ2	
								SamusDialogue(s, "Nowhere to run. ", "I won't let you escape this time.", "SAMDEF", 2, 0)
							elseif t.nextbox == 11 --ACZ2	
								SamusDialogue(s, "This mystery smuggler's turned out", "interesting. Can't let my guard down. ", "SAMSAD", 2, 0)
							elseif t.nextbox == 10 --ACZ2	
								SamusDialogue(s, "It stopped for me? This is a trap... ", "So it knows I've been following... ", "SAMDEF", 2, 0)
							elseif t.nextbox == 17 --RVZ1
								if p.sams_itemcompletion != nil and (p.sams_itemcompletion < 4) --Hesitant when unequipped
									SamusDialogue(s, "Thank the stars... but am I finished? ", "To depart, even if I'm like this... ", "SAMSAD", 2, 18)
								elseif p.sams_itemcompletion != nil and (p.sams_itemcompletion > 94) --Cocky when powered up
									SamusDialogue(s, "How convenient. Since I'm still here... ", "Let's tie up some ... loose ends.", "SAMPRE", 2, 0)
								else 
									SamusDialogue(s, "I've seen enough.     ", nil, "SAMSAD", 2, 19)
								end				
							elseif t.nextbox == 18
								SamusDialogue(s, "I will not leave this planet to its", "cruel fate. Someone must defend it. ", "SAMDEF", 2, 0)
							elseif t.nextbox == 19
								SamusDialogue(s, "Who knows what'll become of this world. ", "...but it won't belong to one man. ", "SAMDEF", 2, 0)	
							elseif t.nextbox == 22 --ERZ
								SamusDialogue(s, "Infiltrate the core and obliterate it! ", "That'll end this. Permanently. ", "SAMDEF", 2, 0)
							elseif t.nextbox == 23
								SamusDialogue(s, "As long as I'm alive, there's hope. I won't", "simply watch this world fall to ruin. ", "SAMDEF", 2, 0)	
							elseif t.nextbox == 500 --BCZ
								SamusDialogue(s, "\x82 !!", nil, "SAMDEF", 2, 0)
							end
						elseif t.customnextbox 
						
						--Custom dialogue trees! Allows for continuous custom text conversations.
						--Use CTRL+F in this Lua file and look for "gamemap == 11" to get an idea how to use this.
						
							t.customnextboxready = true
							t.noscale = true
							
						else --No more boxes? Then setup the dialogue ending animation	 					
						t.goodbyedialogue = 12
						t.customnextbox = 0
							if not (t.textboxstyle == 5)
							S_StartSound(nil, sfx_ssmenb, p)
							end
						end
					end
				end
				
			--AAAAND END THE DIALOGUE THINKER!!
			
				if t.goodbyedialogue
					t.goodbyedialogue = $-1
					t.endoffset = $+FRACUNIT
						if (t.textboxtrns != V_90TRANS)
						t.textboxtrns = $+FRACUNIT
						end
						if (t.textboxscale > 5000)
						t.textboxscale = $-5000
						end
						if not t.goodbyedialogue
						t.active = false
						end
					end
			  end
end

local function RunSamusMission(s,p) --Mobjthinker extension
if not s and p
return
end
local r = s.samusmission
	if r.dontrender --Don't render yet....
	r.dontrender = $-1
		if not r.dontrender
		S_StartSound(nil, sfx_samtm1, p)
		end
	else
				if not r.goodbyedialogue and not r.animtimer
					if r.textboxtrns and leveltime % 2 == 0
					r.textboxtrns = $-FRACUNIT
					end
				end	
				if (string.find(r.text1, "Gruesome"))	
				-- Don't skip this one.
				elseif ( (p.cmd.buttons & BT_TOSSFLAG)
				or not s.health
				or not (YouAreSamus(s) or s.samusforcetext)
				or s.crystalflashanim or p.sam_intoship)
				and not r.goodbyedialogue --End the text entirely.
				r.goodbyedialogue = 12
				S_StartSound(nil, sfx_samtm6, p)
				r.textwait = 99
				end				
				if r.textwait
				r.textwait = $-1
					if r.textwait
					r.textwait = $-1
					end
					
				elseif r.animtimer --Start with animation
				r.animtimer = $-1
				elseif r.maxchars1 and r.currentchars1 < r.maxchars1 --Do first line		
					if leveltime
						if not (string.sub(r.text1, r.currentchars1, r.currentchars1) == " ") --Don't make a sound if there's a space!
							if (r.textboxstyle == 1) or (r.textboxstyle == 4)
							S_StartSound(nil, sfx_samtm4, p)
							else
							S_StartSound(nil, sfx_samtm5, p)
							end
						end
						if (string.sub(r.text1, r.currentchars1, r.currentchars1) == ".")
						r.textwait = 7						
						elseif (string.sub(r.text1, r.currentchars1, r.currentchars1) == "?")
						or (string.sub(r.text1, r.currentchars1, r.currentchars1) == "!")
						r.textwait = 15
						end		
					r.currentchars1 = $+1
					end
					
				elseif r.maxchars2
				and r.currentchars1 >= r.maxchars1 --Do second line
				and r.currentchars2 < r.maxchars2
				
					if leveltime
						if not (string.sub(r.text2, r.currentchars2, r.currentchars2) == " ") --Don't make a sound if there's a space!
							if (r.textboxstyle == 1) or (r.textboxstyle == 4)
							S_StartSound(nil, sfx_samtm4, p)
							else
							S_StartSound(nil, sfx_samtm5, p)
							end
						end
						if (string.sub(r.text2, r.currentchars2, r.currentchars2) == ".")
						r.textwait = 7
						elseif (string.sub(r.text2, r.currentchars2, r.currentchars2) == "?")
						or (string.sub(r.text2, r.currentchars2, r.currentchars2) == "!")
						r.textwait = 15
						end			
					r.currentchars2 = $+1
					end
					
				elseif r.maxchars3
				and r.currentchars1 >= r.maxchars1 --Do second line
				and r.currentchars2 >= r.maxchars2
				and r.currentchars3 < r.maxchars3
				
					if leveltime
						if not (string.sub(r.text3, r.currentchars3, r.currentchars3) == " ") --Don't make a sound if there's a space!
							if (r.textboxstyle == 1) or (r.textboxstyle == 4)
							S_StartSound(nil, sfx_samtm4, p)
							else
							S_StartSound(nil, sfx_samtm5, p)
							end
						end
						if (string.sub(r.text3, r.currentchars3, r.currentchars3) == ".")
						r.textwait = 7
						elseif (string.sub(r.text3, r.currentchars3, r.currentchars3) == "?")
						or (string.sub(r.text3, r.currentchars3, r.currentchars3) == "!")
						r.textwait = 15
						end							
					r.currentchars3 = $+1
					end			
					
				elseif not r.goodbyedialogue
				
					if r.animendtimer --Wait a bir...
					r.animendtimer = $-1
						if r.animendtimer == 119
							if (r.textboxstyle == 3) --Ya did it.
							S_StartSound(nil, sfx_spsmsh, p)
							S_StartSound(nil, sfx_spsmsh, p)
							S_StartSound(nil, sfx_spsmsh, p)
							end
						end	
					else --Is there more? Here it goes.	
						if r.delay
							r.delay = $-1
							if(r.delay == 9)
							S_StartSound(nil, sfx_samtm6, p)
							end
							if (r.textboxtrns < 9*FRACUNIT)
							r.textboxtrns = $+FRACUNIT
							end
						elseif r.nextbox
							r.active = false
							if r.nextbox == 1 --What's next?
							SamusDialogue(p.mo, "Fuck. I really want some gum.", nil, "SAMDEF", 2)	
							end
						else --No more? Then fade away.
						r.goodbyedialogue = 12
						end
					end
				end

			--AAAAND END THE MISSION TEXT THINKER!!

				if r.goodbyedialogue
					r.goodbyedialogue = $-1
						if (r.textboxtrns < 9*FRACUNIT)
						r.textboxtrns = $+FRACUNIT
						end
						if not r.goodbyedialogue
						r.active = false
						end
				end
	end
end
--Everything's in tables these days....
local function RunSamusSaveMenu(s, p)
	p.pflags = $|PF_FORCESTRAFE|PF_INVIS
	if (p.visorclosein < 15)
	p.visorclosein = $+1
	end
	p.powers[pw_underwater] = 1000
	p.powers[pw_spacetime] = 1000
	p.sams_chargetimecd = 2
	p.sams_mshotcd = 2
	p.sams_shotcd = 2
	p.sams_usecd = 2
	p.sams_morphcd = 2
	p.sams_notyet = 2
	p.sam_gravitysuitjustgot = 0
	if s.valid
	s.flags = $|MF_NOCLIP|MF_NOCLIPHEIGHT|MF_NOGRAVITY & ~MF_SHOOTABLE
	s.flags2 = $|MF2_DONTDRAW|MF2_SHADOW
	s.momx = 0
	s.momy = 0
	s.momz = 0
		if P_IsObjectOnGround(s)
		s.z = s.z+10*FRACUNIT
		end
		if p.sammorphed
		MorphballReturn(s, true)
		end
	s.state = S_PLAY_FALL	
	S_StopSound(s)
	end	
	
	local t = p.sam_saveselect
			if not t.randomsparkscale
				t.randomsparkscale = 356
				t.randomsparkx = P_RandomRange(1,14)*FRACUNIT
				t.randomsparky = P_RandomRange(1,14)*FRACUNIT
				t.randomsparkt = P_RandomRange(0,9)*FRACUNIT
			elseif t.randomsparkscale
				t.randomsparkscale = $+5000
				if (t.randomsparkscale > FRACUNIT/2)
				t.randomsparkscale = nil
				end
			end
			if t.backgroundx == nil or t.backgroundx >= 32			
			t.backgroundx = 0
			else
			t.backgroundx = $+1
			end
			if t.backgroundy == nil or t.backgroundy >= 256		
			t.backgroundy = 0
			else
			t.backgroundy = $+1
			end
			if t.fadein --Slides in the menu parts!
			t.fadein = $-FRACUNIT
			end
			if (t.transfadebg != V_10TRANS)
			t.transfadebg = $-FRACUNIT
			end
			if (t.textbgfade != V_30TRANS)
			t.textbgfade = $-FRACUNIT
			end
			if t.transfadebuttons
			t.transfadebuttons = $-FRACUNIT
			end
			if t.transfadebuttons2
			t.transfadebuttons2 = $-FRACUNIT
			end					
			if t.scale <= FRACUNIT
			t.scale = $+5000
			end
			if t.arrowanim <= 0
			t.arrowanim = 16*FRACUNIT
			else
			t.arrowanim = $-FRACUNIT
			end			
			if (leveltime % 6 == 0)
				if t.cursoranim == "SAMSAVS3"
				t.cursoranim = "SAMSAVS2"
				else
				t.cursoranim = "SAMSAVS3"
				end
			end
			if p.sam_savedgamemap
			t.chosenstage = p.sam_savedgamemap
			end

			if t.begintrans
				t.begintrans = $-1 
				t.jump = 5
				if (p.jointime < 105) and not p == server --wait, damnit.
				t.begintrans = $+1
				end
				if not t.begintrans --Give the game 3 tics to process all of Samus, or it might crash.
				ProfileSamusSave(s, p, 9001, false, true, true) --Load the first save!
				end
			end			
			--Menus dashing on and off-screen
			
			if t.submenu == 50
				if t.menuvertical == 1
					if t.huddash1
					t.huddash1 = $-15
					end
				elseif t.huddash1 < 75
				t.huddash1 = $+15
				end
				
				if t.menuvertical == 2
					if t.huddash2
					t.huddash2 = $-15
					end
				elseif t.huddash2 < 75
				t.huddash2 = $+15
				end
				
				if t.menuvertical == 3
					if t.huddash3
					t.huddash3 = $-15
					end
				elseif t.huddash3 < 75
				t.huddash3 = $+15
				end
			end
			
			if t.submenu >= 10 and t.submenu <= 15 --Menu trans and growth stuff....
				if t.menuhorizontal == 1
					if t.firsttrans1 
					t.firsttrans1 = $-FRACUNIT
					end
					if t.firsttrans2 < V_70TRANS
					t.firsttrans2 = $+FRACUNIT
					end
					if t.firsttrans3 < V_70TRANS
					t.firsttrans3 = $+FRACUNIT
					end
				elseif t.menuhorizontal == 2
					if t.firsttrans1 < V_70TRANS
					t.firsttrans1 = $+FRACUNIT
					end
					if t.firsttrans2
					t.firsttrans2 = $-FRACUNIT
					end
					if t.firsttrans3 < V_70TRANS
					t.firsttrans3 = $+FRACUNIT
					end
				elseif t.menuhorizontal == 3
					if t.firsttrans1 < V_70TRANS
					t.firsttrans1 = $+FRACUNIT
					end
					if t.firsttrans2 < V_70TRANS
					t.firsttrans2 = $+FRACUNIT
					end
					if t.firsttrans3
					t.firsttrans3 = $-FRACUNIT
					end							
				end
				--Dunno how letter addition works, so....
				if t.runanim == D
					t.runanim = A
				elseif t.runanim == A
					t.runanim = B
				elseif t.runanim == B
					t.runanim = C
				else
					t.runanim = D
				end
				if ((p.cmd.buttons & BT_CUSTOM1) and t.menuhorizontal == 2) or (leveltime % 5 == 0)
					if t.custom1 == 1
					S_StartSound(nil, sfx_sprna, p)
					end
					if t.runanim2 == D
					t.runanim2 = A
					elseif t.runanim2 == A
					t.runanim2 = B
					elseif t.runanim2 == B
					t.runanim2 = C
					else
					t.runanim2 = D
					end
				end
			end

			if t.submenu == 14
				if t.delaycrash --Give game some time....
				t.delaycrash = $-1
				p.powers[pw_nocontrol] = 2
				t.backgroundx = $+20
				t.backgroundy = $+6
					if t.delaycrash == 100
					ConfigSamusSave(s, p, true, false)
					elseif t.delaycrash == 50			
						if io and p == consoleplayer
						local file = io.openlocal("client/MetroidVanguard/firstsetup.dat", "w+")
							file:write("COMPLETE "+tostring(21473636))
							file:close()
							CONS_Printf(p, "\x89"+"FIRST-TIME SETUP COMPLETE!")
						end
					end
				else --Alright, we're good!
				p.samusloadconfig = 20
				t.fadein = 20*FRACUNIT
				t.transfadebg = V_90TRANS
				t.transfadebuttons = V_90TRANS
				t.transfadebuttons2 = V_90TRANS
				t.begintrans = 3
				t.textbgfade = V_90TRANS			
				S_StartSound(nil, sfx_samtm7, p)
				t.submenu = 1
				t.menuhorizontal = 1
				t.menuvertical = 2
				end
				
			elseif t.submenu == 10  //First Time - Lock On
					
			
					if t.left == 1
						if t.menuhorizontal > 1
						t.menuhorizontal = $-1
						S_StartSound(nil, sfx_ssmenm, p)
						end
					elseif t.right == 1
						if t.menuhorizontal < 3
						t.menuhorizontal = $+1
						S_StartSound(nil, sfx_ssmenm, p)
						end
					elseif t.jump == 1
						if t.menuhorizontal == 1
							CONS_Printf(p, "\x83"+"The Lock-On can be activated with Custom 1 or Custom 3.")
			  				p.sam_lockonpower = 1							
						elseif t.menuhorizontal == 2
							CONS_Printf(p, "\x84"+"The Lock-On can be activated with Custom 3.")
			  				p.sam_lockonpower = 2								
						else
							CONS_Printf(p, "\x87"+"The Lock-On has been de-activated. Using the mouse to aim is recommended.")
							p.sam_lockonpower = 0	
							
							if p.mo
							SamusTutorial(p.mo, "lockon", true)
							end
						end
						t.menuhorizontal = 1
						t.submenu = 11
						S_StartSound(nil, sfx_ssmeno, p)
						SAM_COM_BufInsertText(p, "crosshair angle")
						
					end
			elseif t.submenu == 11 // First Time - AutoSpeedBooster
					if t.left == 1
						if t.menuhorizontal == 2
						t.menuhorizontal = $-1
						S_StartSound(nil, sfx_ssmenm, p)
						end
					elseif t.right == 1
						if t.menuhorizontal == 1
						t.menuhorizontal = $+1
						S_StartSound(nil, sfx_ssmenm, p)
						end
					elseif t.jump == 1
						if t.menuhorizontal == 1
							CONS_Printf(p, "\x82"+"Auto Speed Booster toggled on")
			  				p.autospeedbooster = 1
						else
							CONS_Printf(p, "\x85"+"Auto Speed Booster toggled off") 
			  				p.autospeedbooster = 0
						end
						t.menuhorizontal = 1
						t.submenu = 12
						S_StartSound(nil, sfx_ssmeno, p)
					end
			elseif t.submenu == 12  //First Time - Samus Music
					if t.left == 1
						if t.menuhorizontal > 1
						t.menuhorizontal = $-1
						S_StartSound(nil, sfx_ssmenm, p)
						end
					elseif t.right == 1
						if t.menuhorizontal < 3
						t.menuhorizontal = $+1
						S_StartSound(nil, sfx_ssmenm, p)
						end
					elseif t.jump --wariomon
						if t.jump == 1 and t.menuhorizontal == 1
							if not p.samusmusicadded
								SAM_COM_BufInsertText(p, "addfile SamusMusic.pk3; wait 7; tunes SAMMU1")
								p.samusmusicadded = true
							else
								SAM_COM_BufInsertText(p, "tunes SAMMU1")
							end
							p.sam_autoaddmusic = true
							
							t.submenu = 14	--enable music
							S_StartSound(nil, sfx_ssmeno, p)
					--		S_ChangeMusic("SAMMU1", true, p, 0, 0, 699, 699) --Don't use this, SRB2's lua music change system is broken.
							P_SamFP(p, PAL_RECYCLE, 5)
						elseif t.jump == 1 and t.menuhorizontal == 2
							S_StartSound(nil, sfx_samtm7, p)
							t.submenu = 13	--get link for music	
						elseif t.jump and t.menuhorizontal == 3
						
							if (t.jump == 1)
							S_StartSound(nil, sfx_rumble, p)
							end
							
							if (t.jump == 160)
							P_SamFP(p, PAL_NUKE, 12)
							S_StartSound(nil, sfx_crumbl, p)
							t.submenu = 14	--disable music. Seriously not recommended, but possible and totally fine with some mods.
							end
							
						end
					end
			elseif t.submenu == 13  //Samus Music link
				if t.jump == 1 or t.spin == 1
				S_StartSound(nil, sfx_ssmenc, p)
				t.submenu = 12
				t.menuhorizontal = 1
				end
			elseif t.submenu == 50  //Difficulty Select
			
				if t.down == 1
					if t.menuvertical == 1 
					t.menuvertical = 2
					elseif t.menuvertical == 2
					t.menuvertical = 3
					elseif t.menuvertical == 3
					t.menuvertical = 1
					end
					S_StartSound(nil, sfx_ssmenm, p)
				elseif t.up == 1
					if t.menuvertical == 1
					t.menuvertical = 3
					elseif t.menuvertical == 2 
					t.menuvertical = 1
					elseif t.menuvertical == 3
					t.menuvertical = 2
					end
					S_StartSound(nil, sfx_ssmenm, p)					
				elseif t.jump == 1
					S_StartSound(nil, sfx_ssmenc, p)
					if t.menuvertical == 1
						for z in players.iterate
						z.crazysamusmobs = false
						z.fusionmode = false
						end							
					elseif t.menuvertical == 2
						for z in players.iterate
						z.crazysamusmobs = true
						z.fusionmode = false
						end					
					elseif t.menuvertical == 3
						for z in players.iterate
						z.crazysamusmobs = true
						z.fusionmode = true
						end
					end
					CollectSamusTax(p)
					FinishSamusSaveSelect(p, true)
					CollectSamusTax(p)
					p.samcheat = nil
				end				
			elseif t.submenu == 1  //Save Select
				if t.noloadsave 
				and (t.menuvertical != 1 or not t.jump)
				t.noloadsave = 0
				S_StartSound(nil, sfx_sdenid, p)
				end
				if t.menuvertical == 1 --"play without saving"?
					if t.down == 1 or t.up == 1
						t.menuvertical = 2
						S_StartSound(nil, sfx_ssmenm, p)
					elseif t.jump --Go without a save!
						if (t.noloadsave < 40)
						t.noloadsave = $+1
						else
							t.noloadsave = 0
							S_StartSound(nil, sfx_ssmeno, p)
							p.sam_emeraldflags = 0
							CollectSamusTax(p)
							p.sam_saveselected = 0
							p.sam_scanvisor = false
							if p == server
								t.menuvertical = 1
								t.submenu = 50
							else
								FinishSamusSaveSelect(p, true)
								CollectSamusTax(p)
								p.samcheat = nil				
							end
						end
					end
				elseif t.menuvertical == 2 --Samus Save File 1/2/3/4/etc
					if t.down == 1 or t.up == 1
						t.menuvertical = 1
						S_StartSound(nil, sfx_ssmenm, p)
					elseif t.left == 1
						if t.menuhorizontal <= 1
							t.menuhorizontal = 99
						else
							t.menuhorizontal = $-1
						end
						ProfileSamusSave(s, p, t.menuhorizontal, false, true, true)
						t.transfadebuttons2 = V_90TRANS
						S_StartSound(nil, sfx_ssmenm, p)
						p.powers[pw_nocontrol] = 4
						if p.sam_initmenu
							p.sam_initmenu = nil
						end
					elseif t.right == 1
						if t.menuhorizontal >= 99
							t.menuhorizontal = 1
						else
							t.menuhorizontal = $+1
							if p.sam_initmenu
							p.sam_initmenu = nil
							t.menuhorizontal = 1
							end
						end
						ProfileSamusSave(s, p, t.menuhorizontal, false, true, true)
						t.transfadebuttons2 = V_90TRANS
						S_StartSound(nil, sfx_ssmenm, p)
						p.powers[pw_nocontrol] = 4
					elseif t.jump == 1
						if p.sam_initmenu
							--nope
						elseif p.newsamfile
							S_StartSound(nil, sfx_ssmeno, p)
							p.sam_emeraldflags = 0
							p.sam_saveselected = t.menuhorizontal
							if p == server
								t.menuvertical = 1
								t.submenu = 50
							else
								FinishSamusSaveSelect(p, true)
								CollectSamusTax(p)
								p.samcheat = nil				
							end							
						else
							t.submenu = 2 --Go to save menu.
							t.tempsavenum = t.menuhorizontal
							ProfileSamusSave(s, p, t.tempsavenum, false, true, true)
							S_StartSound(nil, sfx_ssmenc, p)
							t.transfadebuttons = V_90TRANS
							t.menuvertical = 1
							t.textbgfade = V_90TRANS
						end
					end
				end
				
			elseif t.submenu == 2  //Continue? From which stage? Or restart altogether?
				if t.spin == 1 --Return to main save menu?			
					t.submenu = 1
					S_StartSound(nil, sfx_ssmenb, p)
					t.transfadebuttons = V_90TRANS
					t.menuvertical = 2
					
				elseif t.down == 1 or t.up == 1 --Movement.
					if t.menuvertical == 1 
					t.menuvertical = 2
					else
					t.menuvertical = 1
					end
					S_StartSound(nil, sfx_ssmenm, p)
					
				elseif t.jump == 1 --Confirm!
					if t.menuvertical == 1 --Continue game
						if (netgame and (IsPlayerAdmin(p) or (p == server)) ) or not netgame
						or p.sam_netsaves
						t.submenu = 3 --Transition to the continue menu.					
						S_StartSound(nil, sfx_ssmeno, p)
						t.transfadebuttons = V_90TRANS
						t.menuvertical = 1
						else
						S_StartSound(nil, sfx_sdenid, p)
						CONS_Printf(p, "\x85"+"The use of existing saves are turned off in this netgame!")
						end
					elseif t.menuvertical == 2 --Start Fresh
						t.submenu = 4 --transition to Restart Menu
						S_StartSound(nil, sfx_ssmeno, p)
						t.transfadebuttons = V_90TRANS
						t.menuvertical = 1							
					end
					S_StartSound(nil, sfx_ssmeno, p)
				end

			elseif t.submenu == 3  //Continue? From last stage or this stage?
				if t.spin == 1 --Return to save/restart select
				or not (IsPlayerAdmin(p) or (p == server)) and not p.sam_netsaves --You shouldn't be here.
					t.submenu = 2
					S_StartSound(nil, sfx_ssmenb, p)
					t.transfadebuttons = V_90TRANS
					t.menuvertical = 1					
					
				elseif t.jump == 1 --Return to main save menu?
				or not (IsPlayerAdmin(p) or (p == server))
				
					if t.menuvertical == 2 --From current stage. Automatically goes here if you're not the server.
					or not (IsPlayerAdmin(p) or (p == server))
						p.sam_saveselected = t.tempsavenum
						FinishSamusSaveSelect(p, true)				
					elseif t.menuvertical == 1 --From last stage?
						if (netgame and (IsPlayerAdmin(p) or (p == server)) ) or not netgame
						p.sam_saveselected = t.tempsavenum
						FinishSamusSaveSelect(p)
						else --Hold on, only the host and admins can change the stage!
						CONS_Printf(p, "\x85"+"You can't change the stage if you're not the server!")
						S_StartSound(nil, sfx_sdenid, p)
						end
					end
				elseif t.down == 1 or t.up == 1 --Movement.
					if t.menuvertical == 1 
					t.menuvertical = 2
					else
					t.menuvertical = 1
					end
					S_StartSound(nil, sfx_ssmenm, p)					
				end
				
			elseif t.submenu == 4  //Restart altogether or just from this stage?
			
				if t.spin == 1 --Return to save/restart select		
					t.submenu = 2
					S_StartSound(nil, sfx_ssmenb, p)
					t.transfadebuttons = V_90TRANS
					t.menuvertical = 2
					
				elseif t.down == 1 or t.up == 1 --Movement.
					if t.menuvertical == 1 
					t.menuvertical = 2
					else
					t.menuvertical = 1
					end
					S_StartSound(nil, sfx_ssmenm, p)
				elseif t.jump == 1
				or not (IsPlayerAdmin(p) or (p == server))
					if t.menuvertical == 1  --Start Fresh from current stage
					or not (IsPlayerAdmin(p) or (p == server))
						p.sam_saveselected = t.tempsavenum			
						FinishSamusSaveSelect(p, true)
						S_StartSound(nil, sfx_ssmeno, p)
						CollectSamusTax(p, true)
						p.sam_emeraldflags = 0
					else --Start a True Restart from GFZ1!
						if not (IsPlayerAdmin(p) or (p == server))
							S_StartSound(nil, sfx_sdenid, p)
							CONS_Printf(p, "\x85"+"Joiners cannot use true restart in netgames!")
						else
							CollectSamusTax(p, true)
							p.sam_saveselected = t.tempsavenum
							FinishSamusSaveSelect(p, true, true)
							S_StartSound(nil, sfx_ssmeno, p)
							p.sam_emeraldflags = 0
						end
					end
				end
			end
			
	--Menu controls handler.
	
				if (p.cmd.sidemove < 0) //Keep these below the menu Lua or it'll break.
				t.left = $+1
				else
				t.left = 0
				end
				if (p.cmd.sidemove > 0)
				t.right = $+1
				t.left = 0
				else
				t.right = 0
				end
				if (p.cmd.forwardmove > 0)
				t.up = $+1
				else
				t.up = 0
				end
				if (p.cmd.forwardmove < 0)
				t.down = $+1
				else
				t.down = 0
				end
				if (p.cmd.buttons & BT_JUMP)
				t.jump = $+1
				else
				t.jump = 0
				end
				if (p.cmd.buttons & BT_USE)
				t.spin = $+1
				else
				t.spin = 0
				end
				if (p.cmd.buttons & BT_CUSTOM1)
				t.custom1 = $+1
				else
				t.custom1 = 0
				end				
end

--Holding onto that glimmer of hope.
local function CrystalRebirth(s, p) 
	if (leveltime % 2 == 0) and (p.sam_energy < 99+(p.sam_energytanksmax*50))
		p.sam_energy = $+1 --Practically invincible.
	end
	if s.crystalflashanim --Phase 1, the animation
		if p.sammorphed
			MorphballReturn(s, true)
		end
		if leveltime % 4 == 0
		S_StartSound(s, sfx_nrga)
		p.sammissiles = $+5
		p.sampw = $+1
		p.samsupermissiles = $+1
			if (s.crystalflashanim > 28)		
			local ghs = P_SpawnMobj(s.x+((P_RandomRange(FixedMul(-300, s.scale), FixedMul(300, s.scale)))*FRACUNIT), s.y+((P_RandomRange(FixedMul(-300, s.scale), FixedMul(300, s.scale)))*FRACUNIT), s.z+((P_RandomRange(FixedMul(300, s.scale), FixedMul(-300, s.scale)))*FRACUNIT), MT_MSGATHER)
			ghs.scale = s.scale/P_RandomRange(1,2)	
			ghs.fuse = P_RandomRange(10,20)
			ghs.flags = $|MF_SPECIAL
			ghs.frame = $|FF_FULLBRIGHT|TR_TRANS50
			ghs.color = SKINCOLOR_PURPLE
			ghs.colorized = true
			P_HomingAttack(ghs, s)
			end
		end

		p.powers[pw_nocontrol] = 2
		s.momx = 0 s.momy = 0
		P_SetObjectMomZ(s, 500)
		s.flags = MF_SOLID|MF_SHOOTABLE
		s.state = S_PLAY_FALL
		p.playerstate = PST_LIVE
		p.pflags = $|PF_JUMPED
		
		if (s.crystalflashanim > 120)
		p.camerascale = FRACUNIT*2
		s.thesamcam = 1		
		P_SetObjectMomZ(s, 3*FRACUNIT)
		s.sprite2 = SPR2_DEAD
		s.frame = C|FF_FULLBRIGHT //|(s.crystalffade)
			if (s.crystalflashanim % 5 == 0)
			s.clight = P_SpawnMobj(s.x, s.y, s.z+s.height/2, MT_SAMMISSILELIGHT)
			s.clight.crystalflashl = true
			s.clight.scale = s.scale/8
			s.clight.layers = 8
				if s.lastclightc
				s.lastclightc = false
				s.clight.color = SKINCOLOR_SUPERGOLD1
				else
				s.lastclightc = true
				s.clight.color = SKINCOLOR_SUPERRED3			
				end
			end
			
		elseif (s.crystalflashanim > 28)
		p.camerascale = FRACUNIT*3
		s.thesamcam = 1				
				if (s.crystalflashanim == 60)
				s.sprite2 = SPR2_DEAD
				s.frame = F|FF_FULLBRIGHT|TR_TRANS90
				S_StartSound(s, sfx_cmcha)
				s.crystalffade = TR_TRANS90
				end
			if (s.crystalflashanim % 2 == 0)
			s.sprite2 = SPR2_GLID
			s.frame = B|FF_FULLBRIGHT
			else
			s.sprite2 = SPR2_GLID
			s.frame = A|FF_FULLBRIGHT
			end
			
			P_SetObjectMomZ(s, 500)	
			
			s.crystalfroll = $+(ANG1*(s.crystalflashanim/20)) --Do a fancy lil' roll
			s.rollangle = s.crystalfroll
			
			if (s.crystalflashanim % 5 == 0)
			s.clight = P_SpawnMobj(s.x, s.y, s.z+s.height/2, MT_SAMMISSILELIGHT)
			s.clight.scale = s.scale/7
			s.clight.crystalflashl = true
			s.clight.scalespeed = $/2
			s.clight.layers = 6
				if s.lastclightc
				s.lastclightc = false
				s.clight.color = SKINCOLOR_SUPERSILVER1
				else
				s.lastclightc = true
				s.clight.color = SKINCOLOR_SUPERRED5			
				end
			end
		else				
				if (s.crystalflashanim > 20)
					s.rollangle = 0
					s.sprite2 = SPR2_DEAD
					s.frame = D|FF_FULLBRIGHT //|(s.crystalffade)
				
				elseif (s.crystalflashanim < 21)
				
					if (s.crystalflashanim == 20) --And you're back!
					P_RadiusAttack(s, s, 500*FRACUNIT)
						if p.sam_spacejumped
						p.sam_spacejumped = false
						end
						s.flags2 = $ & ~MF2_DONTDRAW
						if not p.powers[pw_flashing]
						p.powers[pw_flashing] = 95
						end
					p.camerascale = FRACUNIT
					s.rollangle = 0
					S_StartSound(s, sfx_wvcm2)
					S_StartSound(s, sfx_spcm)			
					A_samquake(s, 70*FRACUNIT, 23, 1)
						local ghs = P_SpawnMobj(s.x, s.y, s.z, MT_PBEXPANDI)
						ghs.scale = $*2
						ghs.scalespeed = $*7
						ghs.destscale = $*55
						
						P_SamFP(p, PAL_RECYCLE, 9)
						s.plexpand3 = P_SpawnMobj(s.x, s.y, s.z+s.height/2, MT_PBEXPANDW)
						s.plexpand3.scalespeed = $*5
						s.plexpand3.destscale = FRACUNIT*90
						s.plexpand3 = P_SpawnMobj(s.x, s.y, s.z+s.height/2, MT_PBEXPAND)
						s.plexpand3.scalespeed = $*6
						s.plexpand3.destscale = FRACUNIT*90						
						s.clight = P_SpawnMobj(s.x, s.y, s.z+s.height/2, MT_SAMMISSILELIGHT)
						s.clight.layers = 5
						s.clight.crystalflashl = true
						s.clight.color = SKINCOLOR_SUPERSILVER1		
						
					end

					SpeedBoosterGhosts(s, p, true)
						
					A_SAMPARTICLE(s, MT_MPR5, 35, 4, -4, 4, SKINCOLOR_SUPERGOLD2, P_RandomRange(75355, 98355))
					
					s.sprite2 = SPR2_DEAD
					s.frame = A|FF_FULLBRIGHT	
				end
		
		end
--Crystal Flash, the closest thing to a Super Samus	there is	
	else
	
--This is where the actual Crystal Flash behaviour is.

		if s.crystalflashftime
		s.crystalflashftime = $-1
		s.crystalflashftimec = 25
			if not s.crystalflashftime
			s.crystalflashftimec = 0
			end
		end
		if p.sammorphed
		SamSpiderCharge(p, s, 103)
			if not s.spiderbanim
			s.spiderbanim = 7
			end
		else
			if s.crystalflashtime == 2280
				SamusMission(s, "\x8E"+"CRYSTAL FLASH ACTIVATED.", nil, nil, 3, 0, false)
				if p.sam_firstperson
				SamCam(p, false)
				p.visorclosein = 15
				S_StartSound(s, sfx_samspa, p)
				end
			end		
		end
		if s.crystalflashsymtrns == nil
		s.crystalflashftimec = 0
		s.crystalflashsymtrns = V_90TRANS
		s.crystalflashsymtrnsd = -1
		s.crystalflashsymcolor = SKINCOLOR_SUPERRED1
		elseif (s.crystalflashsym % 6 == 0)
			if s.crystalflashsymtrns == V_40TRANS
			s.crystalflashsymcolor = SKINCOLOR_SUPERRED1
			s.crystalflashsymtrnsd = 1
			elseif s.crystalflashsymtrns == V_90TRANS
			s.crystalflashsymcolor = SKINCOLOR_SUPERRED5
			s.crystalflashsymtrnsd = -1
			end
			s.crystalflashsymcolor = $+s.crystalflashsymtrnsd
			s.crystalflashsymtrns = $+(FRACUNIT*s.crystalflashsymtrnsd)
		end
		if s.crystalflashsym == nil or (s.crystalflashsym > 29) --HUD effects.
		s.crystalflashsym = 0
		else
		s.crystalflashsym = $+1
		end
		
		if (s.eflags & MFE_JUSTHITFLOOR)
		A_samquake(s, 25*FRACUNIT, 5, 300*FRACUNIT)
		S_StartSound(s, sfx_wvbc)
		S_StartSound(s, sfx_rocks1)
				for d = 0,7
				s.ghs = P_SpawnMobj(s.x+FixedMul(25*FRACUNIT, cos(ANGLE_45*d)), s.y+FixedMul(25*FRACUNIT, sin(ANGLE_45*d)), s.z, MT_SPINDUST)
				P_InstaThrust(s.ghs, ANGLE_45*d, 30*FRACUNIT)
				end
				for d = 1,20
					local t = P_SpawnMobj(s.x, s.y, s.z+s.height/2, MT_MINECARTSPARK)
					t.scale = s.scale/15
					t.fuse = 30
					P_InstaThrust(t, P_RandomRange(1,359)*ANG1, P_RandomRange(12,24)*FRACUNIT)
					t.momx = $+(s.momx*2) t.momy = $+(s.momy*2)
					t.momz = P_RandomRange(4,7)*FRACUNIT
				end	
		end
		
//		p.sam_shinespark = 3

		if (p.speed == 0)
			s.ghs = P_SpawnGhostMobj(s)
			s.ghs.rollangle = s.rollangle
			s.ghs.tics = 6
			P_SetObjectMomZ(s.ghs, -2*FRACUNIT, true)
			s.ghs.scalespeed = $*P_RandomRange(1,3)
			s.ghs.destscale = $*5
			s.ghs.colorized = true
			s.ghs.blendmode = AST_ADD
			s.ghs.color = SKINCOLOR_SUPERRED2
		else
			SpeedBoosterGhosts(s, p, true)
		end
		
		if (s.crystalflashtime % 4 == 0) --Effects	
		local totali = 1
			if p.speed > 45*FRACUNIT
			totali = 3
			elseif  p.speed > 10*FRACUNIT
			totali = 2
			end
			for i = 1,totali
				local rngx = P_RandomRange(-405,405)*FRACUNIT+(s.momx*35)
				local rngy = P_RandomRange(-405,405)*FRACUNIT+(s.momy*35)
				local rngz = P_RandomRange(5,80)*FRACUNIT+(s.momz*15)
				local scale = P_RandomRange(7,12)
					for d = 0,16,3
					s.ghs = P_SpawnMobj(s.x+rngx, s.y+rngy, s.z+rngz, MT_THOK)
					s.ghs.scale = s.scale/(scale+d)
					s.ghs.sprite = SPR_OILF
					s.ghs.frame = A|FF_FULLBRIGHT|TR_TRANS80
					s.ghs.tics = P_RandomRange(75,150)
					s.ghs.momz = P_RandomRange(2,5)*FRACUNIT
						if s.lastclightc
						s.lastclightc = false
						else
						s.lastclightc = true
						s.ghs.color = SKINCOLOR_SUPERRED2
						s.ghs.colorized = true
						end
		--				if not p.sammorphed and (SAM_displayplayer(p) and not CV_FindVar("chasecam").value) --Don't bother rendering in first person.
		--					s.ghs.flags2 = $|MF2_DONTDRAW
		--				end
					end
			end
		end
		
		if s.crystalflashtime % 70 == 0
			s.crystalflashftime = 10
			P_RadiusAttack(s, s, 128*FRACUNIT)
			s.clight = P_SpawnMobj(s.x, s.y, s.z+s.height/2, MT_SAMMISSILELIGHT)
			s.clight.scale = s.scale/10
			s.clight.crystalflashlt = s
			s.clight.color = SKINCOLOR_SUPERSILVER1
			s.clight.blendmode = AST_ADD
			s.clight.layers = 12
			s.clight.superlayered = true
			S_StartSound(s, sfx_nrga)
			S_StartSound(s, sfx_mbop)
			
			if (p.sam_energy < 99+(p.sam_energytanksmax*50))			
			p.sam_energy = $+10
			p.sammissiles = $+2
			end
			if P_IsObjectOnGround(s)
				for d = 1,16
				s.ghs = P_SpawnMobj(s.x+FixedMul(25*FRACUNIT, cos((ANGLE_45/2)*d)), s.y+FixedMul(25*FRACUNIT, sin((ANGLE_45/2)*d)), s.z, MT_SPINDUST)
				P_InstaThrust(s.ghs, ANGLE_45*d, 50*FRACUNIT)
				end
			end
		else
			s.clight = P_SpawnMobj(s.x, s.y, s.z+s.height/2, MT_SAMMISSILELIGHT)
			if p.sammorphed
			s.clight.scale = s.scale/30
			s.clight.scalespeed = $/3
			else
			s.clight.scale = s.scale/10
			end
			s.clight.crystalflashl = true
			s.clight.crystalflashlt = s
			s.clight.color = SKINCOLOR_SUPERSKY2
			s.clight.layers = 2		
		end
	end
end

//Set up Samus' quake chained to a thinkframe. Samus' quake is slightly different from regular P_StartQuake, with an epicenter.
//Var1 = Intensity, Var2 = Duration, Var3 = Distance(optional), Var4 = Don't overwrite existing quakes

function A_samquake(s, var1, var2, var3, var4)
if not (var1 and var2)
return end
if s.player and s.player.sam_sensitive
return end
if var4 and s.samqtime //Already a quake active? Don't start another if var4 is there.
return end
	s.samqtime = var2
	s.samqlvl = var1
	if var3
		s.samqdist = var3
	else
		s.samqdist = 850
	end
end

addHook("ThinkFrame", do
	for p in players.iterate
		if (p == server) and not p.samusloadconfig2 --Kind of important to load ASAP....
		AdminSamusSave(p, false, true)
		p.samusloadconfig2 = true
		end
		if p.mo
		local s = p.mo
		
		--Proximity-based quake behaviour.
		if s.samqtime and s.samqlvl and s.samqdist
			if (p == displayplayer) and (displayplayer.mo)
			local quakestrength = s.samqlvl
				if FixedHypot(FixedHypot(displayplayer.mo.x-s.x, displayplayer.mo.y-s.y), displayplayer.mo.z-s.z) < p.mo.samqdist*FRACUNIT
				or splitscreen				
					P_StartQuake(quakestrength, 1)
				end
			end
			s.samqtime = max($-1, 0)
		end
		  if YouAreSamus(p.mo)
			p.weapondelay = max($, 9)
			p.currentweapon = 0
			if s.sams_shineframe and not p.sammorphed
			s.sprite2 = SPR2_FLT
			s.frame = A
			end
			if (s.state == S_SAMUFIRE)
				if not p.sam_gravitysuit
				s.frame = F
				else
				s.frame = A
				end
			end
			if p.sammorphed and p.sammorphanimcd != nil
				if (p.sammorphanimcd <= 8)
					s.state = S_SAMUMB4
				elseif ((p.sammorphanimcd > 8) and (p.sammorphanimcd <= 16))
					s.state = S_SAMUMB3
				elseif ((p.sammorphanimcd > 16) and (p.sammorphanimcd <= 24))
					s.state = S_SAMUMB2
				elseif (p.sammorphanimcd > 24)
					s.state = S_SAMUMB1
				end
				if not (p.pflags & PF_ANALOGMODE) and (p.speed > 2*s.scale) and not p.powers[pw_nocontrol]
				and not s.spiderballing
					p.drawangle = R_PointToAngle2(s.x, s.y, s.x+s.momx, s.y+s.momy) 
				end
			elseif (p.pflags & PF_SLIDING)
				s.state = S_PLAY_FALL
			end
			if s.gravitylights and s.gravitylights.valid
				s.gravitylights.skin = "samus" --Skin must always be Samus! BaseSamus has no Super sprites!
				if p.sam_gravitysuit and not (s.sprite == SPR_SAMU)// and not (s.sprite == SPR_ZSSA)
					s.gravitylights.sprite = SPR_PLAY
					s.gravitylights.sprite2 = s.sprite2|FF_SPR2SUPER
				elseif (s.sprite == SPR_SAMU)
					s.gravitylights.sprite = SPR_YGTM
				end
					if p.gltrans
					s.gravitylights.frame = s.frame|FF_FULLBRIGHT|(p.gltrans)
						if s.orangespring
						s.gravitylights.frame = A|FF_FULLBRIGHT|(p.gltrans)
							if p.sammorphed
							MorphballReturn(s)
							end
						end
					end
										
					s.gravitylights.tics = 2					
					s.gravitylights.flags2 = s.flags2
					s.gravitylights.rollangle = s.rollangle
					s.gravitylights.spritexscale = s.spritexscale
					s.gravitylights.spriteyscale = s.spriteyscale
					s.gravitylights.spritexoffset = s.spritexoffset
					s.gravitylights.spriteyoffset = s.spriteyoffset					
					s.gravitylights.angle = p.drawangle
					s.gravitylights.samtracer = p
					
					if p.samvisorcolor and (p.sam_gravitysuit and p.sam_gravitysuit == 2)
					s.gravitylights.color = p.samvisorcolor
					else
					s.gravitylights.color = SKINCOLOR_EMERALD
					end
					if s.sam_scanning and (s.sam_scanning % 2 == 0)
					s.gravitylights.color = SKINCOLOR_SUPERSKY3
					s.gravitylights.frame = s.frame|FF_FULLBRIGHT
					end
					if not netgame and not p.sammorphed and (SAM_displayplayer(p) and not CV_FindVar("chasecam").value)
						s.gravitylights.flags2 = $|MF2_DONTDRAW
					end
					if (s.skin == "basesamus") and (s.sprite != SPR_SAMU)
					or (s.state == S_SAMDEAD5) or (s.crystalflashanim and s.crystalflashanim > 20)
					or p.sam_intoship or p.sam_exitship
					s.gravitylights.sprite = SPR_NULL
					end

			end
			if p.sam_screwattacking and (leveltime % 2 == 0)
			s.frame = $|FF_FULLBRIGHT
			end
			if s.tempsamdye != nil	
					s.ghs = P_SpawnGhostMobj(s) 
					s.ghs.color = s.tempsamdye
					s.ghs.tics = 1
					s.ghs.colorized = true
					s.ghs.momx = s.momx s.ghs.momy = s.momy s.ghs.momz = s.momz
					if s.tempsamdyetr
					s.ghs.frame = s.frame|FF_FULLBRIGHT|(s.tempsamdyetr)
					else
					s.ghs.frame = s.frame|FF_FULLBRIGHT|TR_TRANS30
					end
					s.tempsamdye = nil
			end
			if s.delayedghost
			P_SpawnGhostMobj(s)
			s.delayedghost = 0
			end
		  end
		end
	end
end)

addHook("PostThinkFrame", do
	for p in players.iterate
		if (p.mo and YouAreSamus(p.mo))
		local s = p.mo
			if s.sams_shineframe and not p.sammorphed
			s.sprite2 = SPR2_FLT
			s.frame = A
			end
			if s.bsblglowticket
				s.bsblglowticket = $-1
					if not (s.bsblglow and s.bsblglow.valid)
						s.bsblglow = P_SpawnMobjFromMobj(s, 0, 0, 0-s.height/11, MT_FAKESAMUSTHOK)
					end
					s.bsblglow.target = s
					s.bsblglow.tracer = s
					s.bsblglow.blendmode = AST_ADD
					s.bsblglow.state = S_THOKGLOW
					s.bsblglow.fuse = 3
					s.bsblglow.spriteyoffset = -4*FRACUNIT
						if s.bsblglow and s.bsblglow.valid and (s.sambsblchargelvl < 4)
						s.bsblglow.frame = A|FF_FULLBRIGHT|TR_TRANS90 
						elseif s.bsblglow and s.bsblglow.valid and (s.sambsblchargelvl < 8)
						s.bsblglow.frame = A|FF_FULLBRIGHT|TR_TRANS80 
						elseif s.bsblglow and s.bsblglow.valid and (s.sambsblchargelvl < 12)
						s.bsblglow.frame = A|FF_FULLBRIGHT|TR_TRANS70 
						else
						s.bsblglow.frame = A|FF_FULLBRIGHT|TR_TRANS60 
						end
					s.bsblglow.tics = 3
					s.bsblglow.color = SKINCOLOR_SUPERGOLD4

					if ((p.sams_c1cd == 11) or (p.sams_firecd == 11))
					S_StartSound(s, sfx_spbsc)
					end
					if ((p.sams_c1cd >= 24) or (p.sams_firecd > 24))
					p.sambsblchargefull = true
					end
					A_CapeChase(s.bsblglow, 1, 0)
			end
			if not CV_FindVar("chasecam").value and not p.sammorphed and not (s.eflags & MFE_VERTICALFLIP)
				if not ((s.z+s.height+s.momz+(66*FRACUNIT)) <= s.ceilingz)
				p.viewz = $+(16*FRACUNIT)
				else
				p.viewz = s.z+p.viewheight+p.bob
				end
			end
			if p.mo.gravitylights and p.mo.gravitylights.valid
			p.mo.gravitylights.angle = p.drawangle
				if (p.mo.gravitylights.z > s.z+250*FRACUNIT) --WHAT THE HELL, MYSTIC REALM?! Get back down here!!
				p.mo.gravitylights.z = p.mo.z
				end
			end
		end
	end
end)

addHook("MapLoad", function(gamemap)
	if (G_GametypeHasSpectators() and not (gamemap == 543))
	    if not ((players[0] and players[0].sammatchmonitors) or (players[1] and players[1].sammatchmonitors))
		local sammatchmons = 0
		for m in mobjs.iterate(mobjs)
			if m and m.valid and m.flags and (m.flags & MF_MONITOR) and m.health
			sammatchmons = $+1
			end
		end
		if not ((sammatchmons) > 9)
		local samnewmatchmon1 = 0
		local samnewmatchmon2 = 0
		local samnewmatchmon3 = 0
		local samnewmatchmon4 = 0
		local samnewmatchmon5 = 0
		local samnewmatchmon6 = 0
		local samnewmatchmon7 = 0
		local samnewmatchmon8 = 0
		local samnewmatchmonr = 0
			print("\x85"+"Found less than 9 item containers in arena; Placing extra...\x86 (Disable this by setting Monitor Spawns to 'SRB2')")
			for m in mobjs.iterate(mobjs)
				if m and m.valid and ((m.type == MT_RING) or (m.type == MT_COIN)) and (m.z <= m.floorz+24*FRACUNIT) and m.health		
					if samnewmatchmon1 == 0 and (P_RandomRange(1, 5)) == 2
						samnewmatchmon1 = P_SpawnMobj(m.x, m.y, m.z, MT_WHIRLWIND_BOX)
						m.flags2 = $|MF2_DONTRESPAWN 
						P_RemoveMobj(m)
					elseif samnewmatchmon2 == 0
						if samnewmatchmon1 and (FixedHypot(m.x-samnewmatchmon1.x, m.y-samnewmatchmon1.y) > (P_RandomRange(55, 1500)*FRACUNIT))
						samnewmatchmon2 = P_SpawnMobj(m.x, m.y, m.z, MT_RING_BOX)
						m.flags2 = $|MF2_DONTRESPAWN
						P_RemoveMobj(m)
						end
					elseif samnewmatchmon3 == 0
						if samnewmatchmon2 and (FixedHypot(m.x-samnewmatchmon2.x, m.y-samnewmatchmon2.y) > (P_RandomRange(111, 2000)*FRACUNIT))
						samnewmatchmon3 = P_SpawnMobj(m.x, m.y, m.z, MT_ATTRACT_BOX )
						m.flags2 = $|MF2_DONTRESPAWN
						P_RemoveMobj(m)
						end
					elseif samnewmatchmon4 == 0
						if samnewmatchmon3 and (FixedHypot(m.x-samnewmatchmon3.x, m.y-samnewmatchmon3.y) > (P_RandomRange(200, 2000)*FRACUNIT))
						samnewmatchmon4 = P_SpawnMobj(m.x, m.y, m.z, MT_RING_BOX)
						m.flags2 = $|MF2_DONTRESPAWN
						P_RemoveMobj(m)
						end
					elseif samnewmatchmon5 == 0
						if samnewmatchmon4 and (FixedHypot(m.x-samnewmatchmon4.x, m.y-samnewmatchmon4.y) > (P_RandomRange(900, 2500)*FRACUNIT))
						samnewmatchmon5 = P_SpawnMobj(m.x, m.y, m.z, MT_ELEMENTAL_BOX)
						m.flags2 = $|MF2_DONTRESPAWN 
						P_RemoveMobj(m)
						end
					elseif samnewmatchmon6 == 0 
						if samnewmatchmon5 and (FixedHypot(m.x-samnewmatchmon5.x, m.y-samnewmatchmon5.y) > (P_RandomRange(1400, 3500)*FRACUNIT))
						samnewmatchmon6 = P_SpawnMobj(m.x, m.y, m.z, MT_RING_BOX)
						m.flags2 = $|MF2_DONTRESPAWN 
						P_RemoveMobj(m)
						end 
					elseif samnewmatchmon7 == 0
						if samnewmatchmon6 and (FixedHypot(m.x-samnewmatchmon6.x, m.y-samnewmatchmon6.y) > (P_RandomRange(2500, 4500)*FRACUNIT))
						samnewmatchmon7 = P_SpawnMobj(m.x, m.y, m.z, MT_SNEAKERS_BOX)
						m.flags2 = $|MF2_DONTRESPAWN
						P_RemoveMobj(m)
						end
					elseif samnewmatchmon8 == 0 
						if samnewmatchmon1 and (FixedHypot(m.x-samnewmatchmon1.x, m.y-samnewmatchmon1.y) > (P_RandomRange(5000, 9000)*FRACUNIT))
						samnewmatchmon8 = P_SpawnMobj(m.x, m.y, m.z, MT_RING_BOX)
						m.flags2 = $|MF2_DONTRESPAWN
						P_RemoveMobj(m)
						end 
					end
				end
			end

		end
	end
		
	//Turn boxes to ammo?

		if ((players[0] and not players[0].sammatchmonitors) or (players[1] and not players[1].sammatchmonitors))
		or ((players[0] and players[0].boxammospawns) or (players[1] and players[1].boxammospawns))
			for m in mobjs.iterate(mobjs)
				if ((players[0] and players[0].boxammospawns) or (players[1] and players[1].boxammospawns))
					if m and m.valid and m.flags and (m.flags & MF_MONITOR) and m.health
					and not ((m.type == MT_EGGMAN_BOX) or (m.type == MT_EGGMAN_GOLDBOX))
					m.samspawnitem = P_SpawnMobj(m.x, m.y, m.z, MT_SAMSPAWNITEM)
					m.samspawnitem.chozobox = m
					m.samspawnitem.eflags = m.eflags
					m.samspawnitem.flags2 = m.flags2
					m.samspawnitem.ammospawner = P_RandomRange(1, 100)
					P_RemoveMobj(m)
					end
				end
				if m and m.valid and m.flags and (m.flags & MF_MONITOR) and m.health
				and not (((m.type == MT_EGGMAN_BOX) or (m.type == MT_EGGMAN_GOLDBOX)) or (m.type == MT_1UP_BOX))
				m.samspawnitem = P_SpawnMobj(m.x, m.y, m.z, MT_SAMSPAWNITEM)
				m.samspawnitem.chozobox = m
				m.samspawnitem.eflags = m.eflags
				m.samspawnitem.flags2 = m.flags2
				m.flags = $|MF_NOCLIP & ~MF_SHOOTABLE & ~MF_SOLID
				m.flags2 = $|MF2_DONTDRAW
				m.samspawnitem.state = S_SAMSPAWNITEM3
				end
			end
		end
	end
	for p in players.iterate
		if gamemap == 812
			if not p.sam_lastlvl --Backups.
				if spstage_start
					p.sam_lastlvl = spstage_start
				elseif bootmap
					p.sam_lastlvl = bootmap
				end
			end
		else
			p.sam_lastlvl = gamemap
		end
		if p.camerascale == 196071 or p.camerascale == 261427
		p.camerascale = FRACUNIT
		end
		if not G_GametypeHasSpectators()	
		p.sam_topscorewinnerset = false
		p.sam_score = 0
		p.sam_currenttopscore = 0
		p.sam_topscore = 0
		p.sam_topscoreman = nil
		p.sam_topscore = 0
		end	
		
		if p.samhudmessagetime and p.samhudmessagetime < 229
		p.samhudmessagetime = 0	
		p.samhudmessageget = nil
		end
		if gamemap != 807 and gamemap != 806 and gamemap != 28
		p.superbrakdeath = nil
		end
		
		if G_IsSpecialStage(gamemap)
			p.sam_specialstage = true
		else
			p.sam_specialstage = false
		end
			
		if p.sam_energytanksmax == nil
			if (p == server)
				p.sam_energytanksmax = 2
			elseif players[0]
				p.sam_energytanksmax = players[0].sam_energytanksmax
			else
				p.sam_energytanksmax = 2
			end
		end
			
		if (G_GametypeHasSpectators() or (gametype == GT_RACE))
		SamResetBeam(p)
			if not p.samusmatch and not (gametype == GT_RACE)
				p.samspbs = false
				p.sambsbl = false
				p.sam_spiderball = false
				p.sam_gravitysuit = 0
				p.sam_highjump = false
				p.sam_bombs = false
				p.sam_spacejump = false
				p.sam_chargebeam = true
				p.sam_spazerbeam = false
				p.sam_wavebeam = false
				p.sam_icebeam = false
				p.sam_plasmabeam = false
				p.sam_hyperbeam = false
				p.sam_powerbeamcombo = false
				p.sam_spazerbeamcombo = false
				p.sam_wavebeamcombo = false
				p.sam_icebeamcombo = false
				p.sam_plasmabeamcombo = false
				p.sam_fullypoweredsuit = false
				p.sam_screwattack = false
				p.sam_power_grip = false
				p.sammissilesmax = 5
				p.sampwmax = 0
				p.samsupermissilesmax = 0
				SamResetBeam(p)
					if (p.sam_energytanksstart != nil)
					p.sam_energytanksmax = p.sam_energytanksstart
					else
					p.sam_energytanksmax = 2
					end

			elseif (p.samusmatch == 1) and not (gametype == GT_RACE)
				p.sam_energytanks = p.sam_energytanksmax
					if (leveltime < 350) and not (p.sam_energytanksmax < 2)
					p.sam_energy = (p.sam_energytanksmax*50)-1
					else
					p.sam_energy = 99+(p.sam_energytanksmax*50)
					end
				p.sam_fullypoweredsuit = false

			elseif (p.samusmatch == 2) or (gametype == GT_RACE)
					if not (gametype == GT_RACE)
						if (p.sam_energytanksstart != nil)
						p.sam_energytanksmax = p.sam_energytanksstart
						else
						p.sam_energytanksmax = 2
						end
					p.sam_energytanks = p.sam_energytanksmax
					p.sam_energy = 99+(p.sam_energytanksmax*50)
					end
					
				p.samspbs = true
				p.sambsbl = true
				p.sam_spiderball = true
				p.sam_gravitysuit = 2
				p.sam_gravitysuitjustgot = 0
				p.sam_highjump = true
				p.sam_spacejump = true
				p.sam_chargebeam = true
				p.sam_spazerbeam = true
				p.sam_wavebeam = true
				p.sam_icebeam = true
				p.sam_bombs = true
				p.sam_plasmabeam = true
				p.sam_screwattack = true
				p.sam_power_grip = true
				
				if p.sam_beamcombosrestrict
					p.sam_powerbeamcombo = false
					p.sam_spazerbeamcombo = false
					p.sam_wavebeamcombo = false
					p.sam_icebeamcombo = false
					p.sam_plasmabeamcombo = false
				else
					p.sam_powerbeamcombo = true
					p.sam_spazerbeamcombo = true
					p.sam_wavebeamcombo = true
					p.sam_icebeamcombo = true
					p.sam_plasmabeamcombo = true
				end
				
				p.sam_hyperbeam = false
				p.sam_fullypoweredsuit = false
				p.sammissilesmax = 15
				p.sammissiles = 10
				p.sampwmax = 1
				p.sampw = 0
				p.samsupermissilesmax = 2
				p.samsupermissiles = 0

			elseif (p.samusmatch == 5) --Start with basics.

				if not (gametype == GT_RACE)
					if (p.sam_energytanksstart != nil)
					p.sam_energytanksmax = p.sam_energytanksstart
					else
					p.sam_energytanksmax = 2
					end
					
					p.sam_energytanks = p.sam_energytanksmax
					p.sam_energy = 99+(p.sam_energytanksmax*50)				
				end
				
				p.samspbs = true
				p.sambsbl = true
				p.sam_spiderball = false
				p.sam_gravitysuit = 1
				p.sam_gravitysuitjustgot = 0
				p.sam_highjump = false
				p.sam_spacejump = true
				p.sam_chargebeam = true
				p.sam_spazerbeam = false
				p.sam_wavebeam = false
				p.sam_icebeam = false
				p.sam_plasmabeam = false
				p.sam_ranchoice = (P_RandomRange(1,7))
				
				if p.sam_ranchoice
 					if p.sam_ranchoice <= 2
						p.sam_spazerbeam = true
					elseif p.sam_ranchoice <= 4
						p.sam_wavebeam = true
					elseif p.sam_ranchoice <= 6
						p.sam_icebeam = true
					elseif p.sam_ranchoice >= 7
						p.sam_plasmabeam = true
					end
				end
				
				p.sam_bombs = true
				p.sam_screwattack = false
				p.sam_powerbeamcombo = false
				p.sam_spazerbeamcombo = false
				p.sam_wavebeamcombo = false
				p.sam_icebeamcombo = false
				p.sam_plasmabeamcombo = false
				p.sam_hyperbeam = false
				p.sam_fullypoweredsuit = false
				p.sam_power_grip = false
				p.sammissilesmax = 10
				p.sammissiles = 5
				p.sampwmax = 0
				p.sampw = 0
				p.samsupermissilesmax = 2
				p.samsupermissiles = 0

			elseif (p.samusmatch == 9)

					if not (gametype == GT_RACE)
						if (p.sam_energytanksstart != nil)
						p.sam_energytanksmax = p.sam_energytanksstart
						else
						p.sam_energytanksmax = 2
						end
					p.sam_energytanks = p.sam_energytanksmax
					p.sam_energy = 99+(p.sam_energytanksmax*50)
					end
				p.samspbs = false
				p.sambsbl = false
				p.sam_spiderball = false
				p.sam_gravitysuit = 0
				p.sam_gravitysuitjustgot = 0
				p.sam_highjump = false
				p.sam_spacejump = true
				p.sam_chargebeam = true
				p.sam_spazerbeam = true
				p.sam_wavebeam = true
				p.sam_icebeam = true
				p.sam_plasmabeam = true
				p.sam_bombs = false
				p.sam_screwattack = false
				p.sam_power_grip = false
				
				if not  p.sam_beamcombosrestrict
				p.sam_powerbeamcombo = true
				else
				p.sam_powerbeamcombo = false
				end
				
				p.sam_spazerbeamcombo = false
				p.sam_wavebeamcombo = false
				p.sam_icebeamcombo = false
				p.sam_plasmabeamcombo = false
				p.sam_hyperbeam = false
				p.sam_fullypoweredsuit = false
				p.sammissilesmax = 10
				p.sammissiles = 5
				p.sampwmax = 0
				p.sampw = 0
				p.samsupermissilesmax = 0
				p.samsupermissiles = 0


			elseif (p.samusmatch == 4) --Start with Morph Ball powerups

					if not (gametype == GT_RACE)
						if (p.sam_energytanksstart != nil)
						p.sam_energytanksmax = p.sam_energytanksstart
						else
						p.sam_energytanksmax = 2
						end
					p.sam_energytanks = p.sam_energytanksmax
					p.sam_energy = 99+(p.sam_energytanksmax*50)
					end
				p.samspbs = false
				p.sambsbl = true
				p.sam_spiderball = true
				p.sam_gravitysuit = 0
				p.sam_gravitysuitjustgot = 0
				p.sam_highjump = true
				p.sam_spacejump = false
				p.sam_chargebeam = true
				p.sam_spazerbeam = false
				p.sam_icebeam = false
				p.sam_plasmabeam = false
				p.sam_bombs = true
				p.sam_screwattack = false
				p.sam_powerbeamcombo = false
				p.sam_spazerbeamcombo = false
				p.sam_power_grip = false
				
				if not  p.sam_beamcombosrestrict
				p.sam_wavebeam = true
				p.sam_wavebeamcombo = true
				else
				p.sam_wavebeam = false
				p.sam_wavebeamcombo = false
				end
				
				p.sam_icebeamcombo = false
				p.sam_plasmabeamcombo = false
				p.sam_hyperbeam = false
				p.sam_fullypoweredsuit = false
				p.sammissilesmax = 15
				p.sammissiles = 5
				p.sampwmax = 3
				p.sampw = 1
				p.samsupermissilesmax = 0
				p.samsupermissiles = 0
			end
		end
	end
	SetMetroidVisuals(s)
	if (gametype == GT_COOP)
		local sammobjmons = 0
		local montype1 = 1
		local montype2 = 1
		local montype3 = 1
		local montype4 = 1
		local montype5 = 1
		local montype6 = 1
		local montype7 = 1
		local montype8 = 1
		local montype9 = 1
		for m in mobjs.iterate(mobjs)
			if m and m.valid and m.flags and (m.flags & MF_MONITOR) and m.health
				if (m.type == MT_RING_BOX) or (m.type == MT_RING_REDBOX) or (m.type == MT_RING_BLUEBOX)
				or (m.type == MT_RECYCLER_BOX) or (m.type == MT_SCORE1K_BOX) or (m.type == MT_SCORE10K_BOX)
				or (m.type ==  MT_MYSTERY_BOX)
				montype1 = 0
				end
				if (m.type == MT_SNEAKERS_BOX) montype2 = 0 end
				if (m.type == MT_INVULN_BOX) montype3 = 0 end
				if (m.type == MT_ATTRACT_BOX ) montype4 = 0 end
				if (m.type == MT_FORCE_BOX) montype5 = 0 end
				if (m.type == MT_ARMAGEDDON_BOX) montype6 = 0 end
				if (m.type == MT_WHIRLWIND_BOX) montype7 = 0 end
				if (m.type == MT_ELEMENTAL_BOX) montype8 = 0 end
				if (m.type == MT_PITY_BOX) montype9 = 0 end
			end
		end
		sammobjmons = 0+montype1+montype2+montype3+montype4+montype5+montype6+montype7+montype8+montype9
			if sammobjmons and (sammobjmons > 4)
				for m in mobjs.iterate(mobjs)
					if m and m.valid and m.flags and (m.flags & MF_ENEMY) and not (m.flags & MF_BOSS) and m.health
					m.dropmonforsam = sammobjmons
					end
				end
			end		
	end	
end)

local function SamusWeaponReady(s, p)
		local ghs = P_SpawnMobjFromMobj(s, FixedMul(s.radius*5/2, cos(s.angle-(ANG2*2))), FixedMul(s.radius*5/2, sin(s.angle-(ANG2*2))), (s.height*4/11),  MT_SAMCHARGEGFX)
		if ghs and ghs.valid
			ghs.z = $+(FixedMul(FixedMul(50*FRACUNIT, s.scale), sin(p.aiming)))
			ghs.state = S_SAMCHARGEGFX4
			ghs.renderflags = $|RF_FULLBRIGHT
			ghs.ret2sign = true
			ghs.colorized = true
			ghs.color = SKINCOLOR_GREY
			ghs.target = s
			return ghs
		end
end

addHook("MobjThinker", function(s)
	if not s.player --Glorified PlayerThink hook that's only active with a mobj.
		return
	end
	local p = s.player
		if p.sams_shotcd == nil or p.sam_energytanksmax == nil
			SamusSetUp(p) --Set up Samus' values. In future projects we'll use tables.
		end
		if not p.samcmult
		or ((p.samcmult == 11 and not p.samchallenger) or (p.samcmult == 7 and not p.buffbottomtier))
			p.samcmult = 8 --Damage multiplier in Match. 8 is 1:1, 11 is about a 30% reduction, 7 is a slight increase.
		end
 		if p.samchallenger
			if p.samqchallenger and not p.sam_hyperbeam
				p.samqchallenger = nil
				p.samchallenger = 2
				S_StartSound(nil, sfx_kc59, p)
				CONS_Printf(p, "\x87"+"Your commendation rank has been set back to rank 1!")
			end
			if YouAreSamus(s)
				if p.samchallenger == 1
					s.sammedal = P_SpawnMobj(s.x, s.y, s.z+((s.height+22*FRACUNIT)*P_MobjFlip(s)), MT_THOK)
					s.sammedal.sprite = SPR_PWBM
					s.sammedal.frame = B|TR_TRANS50|FF_FULLBRIGHT
					s.sammedal.tics = 2
					s.sammedal.momx = s.momx s.sammedal.momy = s.momy s.sammedal.momz = s.momz
					s.sammedal.scale = 79000
				elseif p.samchallenger == 2
					s.sammedal = P_SpawnMobj(s.x, s.y, s.z+((s.height+22*FRACUNIT)*P_MobjFlip(s)), MT_THOK)
					s.sammedal.sprite = SPR_PWBM
					s.sammedal.frame = C|TR_TRANS20|FF_FULLBRIGHT
					s.sammedal.tics = 2
					s.sammedal.momx = s.momx s.sammedal.momy = s.momy s.sammedal.momz = s.momz
					s.sammedal.scale = 80350
				elseif p.samchallenger == 3
					s.sammedal = P_SpawnMobj(s.x, s.y, s.z+((s.height+22*FRACUNIT)*P_MobjFlip(s)), MT_THOK)
					s.sammedal.sprite = SPR_PWBM
					s.sammedal.frame = D|TR_TRANS30|FF_FULLBRIGHT
					s.sammedal.tics = 2
					s.sammedal.momx = s.momx s.sammedal.momy = s.momy s.sammedal.momz = s.momz
					s.sammedal.scale = FRACUNIT*2
				elseif p.samchallenger >= 4
					s.sammedal = P_SpawnMobj(s.x, s.y, s.z+((s.height+22*FRACUNIT)*P_MobjFlip(s)), MT_THOK)
					s.sammedal.sprite = SPR_PWBM
					s.sammedal.frame = E|TR_TRANS10|FF_FULLBRIGHT
					s.sammedal.tics = 2
					s.sammedal.momx = s.momx s.sammedal.momy = s.momy s.sammedal.momz = s.momz
					s.sammedal.scale = 165000
				end
				if not G_GametypeHasSpectators()
				p.samchallenger = 0
				S_StartSound(nil, sfx_kc59, p)
				CONS_Printf(p, "\x85"+"Turned off challenger mode!")	
				end
			else
			p.samchallenger = 0
			S_StartSound(nil, sfx_kc59, p)
			CONS_Printf(p, "\x85"+"Turned off challenger mode!")
			end
			p.samcmult = 11
		end
		
		if p.sam_joinsync == nil
			if (p == server)
			p.sam_joinsync = true
			elseif players[0]
			p.nogunshipspawn = players[0].nogunshipspawn
			p.sam_respawnboxer =  players[0].sam_respawnboxer
			p.tokensrb2style = players[0].tokensrb2style
			p.samescapehud = players[0].samescapehud
			p.crazysamusmobs = players[0].crazysamusmobs
			p.samworldevents = players[0].samworldevents
			p.sam_beamcombosrestrict = players[0].sam_beamcombosrestrict
			p.hypernetgo = players[0].hypernetgo
			p.nosuperpowert = players[0].nosuperpowert
			p.fusionmode = players[0].fusionmode
			p.morphcoop = players[0].morphcoop
			p.metroidvisuals  =  players[0].metroidvisuals	
			p.coopboxrespawn = players[0].coopboxrespawn
			p.boxammospawns = players[0].boxammospawns
			p.sammatchmonitors = players[0].sammatchmonitors
			p.sammatchchars = players[0].sammatchchars
			p.samdoubleammo = players[0].samdoubleammo
			p.samusragdolls = players[0].samusragdolls
			p.speedrunoverride = players[0].speedrunoverride
					if p.speedrunoverride
					p.sams_completiontime = players[0].sams_completiontime
					end
					if (players[0].sam_energytanksstart != nil)
					p.sam_energytanksstart = players[0].sam_energytanksstart
					end
			p.sam_netsaves = players[0].sam_netsaves
			p.samusmatch = players[0].samusmatch
				if p.samusmatch and (p.samusmatch == 1) and not (gametype == GT_RACE)
				p.sam_energytanks = p.sam_energytanksmax
					if (leveltime < 350) and not (p.sam_energytanksmax < 2)
					p.sam_energy = (p.sam_energytanksmax*50)-1
					else
					p.sam_energy = 99+(p.sam_energytanksmax*50)
					end
				elseif p.samusmatch and ((p.samusmatch == 2) or (gametype == GT_RACE))
				p.samspbs = true
				p.sambsbl = true
				p.sam_spiderball = true
				p.sam_gravitysuit = 2
				p.sam_highjump = true
				p.sam_bombs = true
				p.sam_spacejump = true
				p.sam_chargebeam = true
				p.sam_spazerbeam = true
				p.sam_wavebeam = true
				p.sam_icebeam = true
				p.sam_plasmabeam = true
				p.sam_screwattack = true
				p.sam_powerbeamcombo = false
				p.sam_spazerbeamcombo = false
				p.sam_wavebeamcombo = false
				p.sam_icebeamcombo = false
				p.sam_plasmabeamcombo = false
				p.sam_hyperbeam = false
				p.sam_fullypoweredsuit = false
				p.sammissilesmax = 10
				p.sammissiles = 10
				p.sampw = 0
				p.sampwmax = 1
				p.sampw = 0
				p.samsupermissilesmax = 2
				p.samsupermissiles = 0

				elseif (p.samusmatch == 5)

					if not (gametype == GT_RACE)
						if (p.sam_energytanksstart != nil)
						p.sam_energytanksmax = p.sam_energytanksstart
						else
						p.sam_energytanksmax = 2
						end
					p.sam_energytanks = p.sam_energytanksmax
					p.sam_energy = 99+(p.sam_energytanksmax*50)
					end
				p.samspbs = true
				p.sambsbl = true
				p.sam_spiderball = false
				p.sam_gravitysuit = 1
				p.sam_gravitysuitjustgot = 0
				p.sam_highjump = false
				p.sam_spacejump = true
				p.sam_chargebeam = true
				p.sam_spazerbeam = false
				p.sam_wavebeam = false
				p.sam_icebeam = false
				p.sam_plasmabeam = false
				p.sam_ranchoice = (P_RandomRange(1,7))
				if p.sam_ranchoice
 						if p.sam_ranchoice <= 2
						p.sam_spazerbeam = true
						elseif p.sam_ranchoice <= 4
						p.sam_wavebeam = true
						elseif p.sam_ranchoice <= 6
						p.sam_icebeam = true
						elseif p.sam_ranchoice >= 7
						p.sam_plasmabeam = true
						end
				end
				p.sam_bombs = true
				p.sam_screwattack = false
				p.sam_powerbeamcombo = false
				p.sam_spazerbeamcombo = false
				p.sam_wavebeamcombo = false
				p.sam_icebeamcombo = false
				p.sam_plasmabeamcombo = false
				p.sam_hyperbeam = false
				p.sam_fullypoweredsuit = false
				p.sammissilesmax = 10
				p.sammissiles = 5
				p.sampwmax = 0
				p.sampw = 0
				p.samsupermissilesmax = 2
				p.samsupermissiles = 0

				elseif (p.samusmatch == 9)

					if not (gametype == GT_RACE)
						if (p.sam_energytanksstart != nil)
						p.sam_energytanksmax = p.sam_energytanksstart
						else
						p.sam_energytanksmax = 2
						end
					p.sam_energytanks = p.sam_energytanksmax
					p.sam_energy = 99+(p.sam_energytanksmax*50)
					end
				p.samspbs = false
				p.sambsbl = false
				p.sam_spiderball = false
				p.sam_gravitysuit = 0
				p.sam_gravitysuitjustgot = 0
				p.sam_highjump = false
				p.sam_spacejump = true
				p.sam_chargebeam = true
				p.sam_spazerbeam = true
				p.sam_wavebeam = true
				p.sam_icebeam = true
				p.sam_plasmabeam = true
				p.sam_bombs = false
				p.sam_screwattack = false
				if not  p.sam_beamcombosrestrict
				p.sam_powerbeamcombo = true
				else
				p.sam_powerbeamcombo = false
				end
				p.sam_spazerbeamcombo = false
				p.sam_wavebeamcombo = false
				p.sam_icebeamcombo = false
				p.sam_plasmabeamcombo = false
				p.sam_hyperbeam = false
				p.sam_fullypoweredsuit = false
				p.sammissilesmax = 10
				p.sammissiles = 5
				p.sampwmax = 0
				p.sampw = 0
				p.samsupermissilesmax = 0
				p.samsupermissiles = 0

				elseif (p.samusmatch == 4)

					if not (gametype == GT_RACE)
						if (p.sam_energytanksstart != nil)
						p.sam_energytanksmax = p.sam_energytanksstart
						else
						p.sam_energytanksmax = 2
						end
					p.sam_energytanks = p.sam_energytanksmax
					p.sam_energy = 99+(p.sam_energytanksmax*50)
					end
				p.samspbs = false
				p.sambsbl = true
				p.sam_spiderball = true
				p.sam_gravitysuit = 0
				p.sam_gravitysuitjustgot = 0
				p.sam_highjump = true
				p.sam_spacejump = false
				p.sam_chargebeam = true
				p.sam_spazerbeam = false
				p.sam_icebeam = false
				p.sam_plasmabeam = false
				p.sam_bombs = true
				p.sam_screwattack = false
				p.sam_powerbeamcombo = false
				p.sam_spazerbeamcombo = false
				if not  p.sam_beamcombosrestrict
				p.sam_wavebeam = true
				p.sam_wavebeamcombo = true
				else
				p.sam_wavebeam = false
				p.sam_wavebeamcombo = false
				end
				p.sam_icebeamcombo = false
				p.sam_plasmabeamcombo = false
				p.sam_hyperbeam = false
				p.sam_fullypoweredsuit = false
				p.sammissilesmax = 15
				p.sammissiles = 5
				p.sampwmax = 3
				p.sampw = 1
				p.samsupermissilesmax = 0
				p.samsupermissiles = 0
				end

			p.sam_joinsync = true
			end
		end	
		if p.samhudmessagetime
			p.samhudmessagetime = $-1
			if not p.samhudmessageoffset
				if p.samhudmessagetime < 30
					p.samhudmessageoffset = 1
				end
				p.samhudmessageoffset = 30
			elseif p.samhudmessageoffset and not ((p.samhudmessageoffset) < 2)
				p.samhudmessageoffset = $-1
			end
			p.samhudmessageflags = 0
			--Let's kill our old elseif chain, we're better than that now.
			if p.samhudmessagetime > 210
				p.samhudmessageflags = min(FRACUNIT*9, (p.samhudmessagetime-210)*FRACUNIT)
			elseif p.samhudmessagetime < 30
				p.samhudmessageflags = max(FRACUNIT, 9-((p.samhudmessagetime or 1)/3)*FRACUNIT )
			end	
		elseif p.samhudmessageoffset
			p.samhudmessageoffset = 0
		end

		if G_GametypeHasSpectators()
			if p.score != nil and (leveltime > 3)
					if p.plushudscoretime
					p.plushudscoretime = $-1
						--AmperBee'll kill me for this...
						if p.plushudscoretime > 17
						p.plushudscoreflags = V_40TRANS 
						elseif p.plushudscoretime > 14
						p.plushudscoreflags = V_10TRANS
						elseif p.plushudscoretime > 12
						p.plushudscoreflags = V_30TRANS 
						elseif p.plushudscoretime > 8
						p.plushudscoreflags = V_50TRANS 
						elseif p.plushudscoretime >= 6
						p.plushudscoreflags = V_70TRANS 
						elseif p.plushudscoretime < 6
						p.plushudscoreflags = V_80TRANS 
						end
					end
					if p.prevsamscore != nil and (p.prevsamscore < p.score)
					p.plushudscore = p.score-p.prevsamscore
					p.plushudscoretime = 19
					p.plushudscoreflags = V_30TRANS 
					end
					p.prevsamscore = p.score
			elseif p.score != nil
					p.prevsamscore = p.score
			end		
			
			if p.powers[pw_flashing] and s.valid
				if s.sam_lastflashing and (s.sam_lastflashing == p.powers[pw_flashing])
				s.sam_lastflashtotal = s.sam_lastflashtotal and $+1 or 1
				else
				s.sam_lastflashtotal = 1
				end
				s.sam_lastflashing = p.powers[pw_flashing]
				
				if (s.sam_lastflashtotal > 150) and p.powers[pw_flashing] and (s.sam_lastflashing == p.powers[pw_flashing])
				p.powers[pw_flashing] = 0 --Undo this godforsaken glitch.
				s.sam_lastflashtotal = 1
				end
			end
			
			if p.buffbottomtier 
				if not p.samchallenger 
				p.samcmult = 6
				end
			elseif p.samcmult == 6
			p.samcmult = 8
			end
			if p.cooptanks and not p.matchtanks
			or (p.cooptanks == nil and p.matchtanks == nil)
				if (p.sam_energytanksstart != nil)
				p.sam_energytanksmax = p.sam_energytanksstart
				else
				p.sam_energytanksmax = 2
				end
				p.sam_energytanks = p.sam_energytanksmax
				p.sam_energy = 99+(p.sam_energytanksmax*50)
				p.cooptanks = false
				if (p.samusmatch and p.samusmatch == 2)
				p.match2tanks = true
				p.matchtanks = true
				else
				p.matchtanks = true
				p.match2tanks = false
				end
			end
		
		
			if (leveltime == 0) and p.samhudmessagetime and p.samhudmessagetime < 229
				p.samhudmessagetime = 0	
				p.samhudmessageget = nil
			
			elseif not p.samhudmessageget and not p.samhudmessagetime 
			and not (gametype == GT_TAG) and not (gametype == GT_HIDEANDSEEK) and not (gametype == GT_RACE)
			
			local samhmrng = P_RandomRange(1,41)
			
				if not (p.lastsamhmrng and (p.lastsamhmrng  == (samhmrng))) 
				
					if YouAreSamus(s)
						if samhmrng == 1 and (gametype == GT_CTF)
						p.samhudmessage1 = "Use charge shots and mighty blasts"
						p.samhudmessage2 = "to make players drop their flag!"
						elseif samhmrng == 1
						p.samhudmessage1 = "Missiles are more potent than your beams,"
						p.samhudmessage2 = "but they are also useful for beam combos!"
						elseif samhmrng == 2
						p.samhudmessage1 = "Many blasts are explosive! Be aware of"
						p.samhudmessage2 = "walls and floors around you and enemies."
						elseif samhmrng == 3
						p.samhudmessage1 = "If someone gets the Hyper Beam, attack"
						p.samhudmessage2 = "them immediately to limit their ammo!"
						elseif samhmrng == 4
						p.samhudmessage1 = "Shinesparks annihilate players, but actually"
						p.samhudmessage2 = "landing a hit takes serious precision."
						elseif samhmrng == 5
						p.samhudmessage1 = "Charge your beam and morph to drop 8"
						p.samhudmessage2 = "bombs around you! Devastating up-close."
						elseif samhmrng == 6
						p.samhudmessage1 = "Weapon Panels and ammo restore"
						p.samhudmessage2 = "your missile count. Stock up!"
						elseif samhmrng == 7 and (gametype == GT_CTF)
						p.samhudmessage1 = "You can't use movement upgrades"
						p.samhudmessage2 = "holding a flag! Stand your ground!"
						elseif samhmrng == 7
						p.samhudmessage1 = "Only charge shots and other powerful"
						p.samhudmessage2 = "attacks make players drop emeralds."
						elseif samhmrng == 8
						p.samhudmessage1 = "1 powerbomb can change the game."
						p.samhudmessage2 = "Be wary around morphed players." 
						elseif samhmrng == 9
						p.samhudmessage1 = "Players that flash or glow are ready to"
						p.samhudmessage2 = "unleash devastating attacks. Watch out." 
						elseif samhmrng == 10
						p.samhudmessage1 = "Morph during a Wave Combo to become the"
						p.samhudmessage2 = "Death Ball, obliterating all by mere touch." 
						elseif samhmrng == 11
						p.samhudmessage1 = "Samus gains score from damage dealt."
						p.samhudmessage2 = "More powerful attacks = more score!" 
						elseif samhmrng == 12
						p.samhudmessage1 = "Samus gains score from damage, but kills"
						p.samhudmessage2 = "gain you 50 points extra on top!"
						elseif samhmrng == 13
						p.samhudmessage1 = "Fire the Ice Beam Combo into the"
						p.samhudmessage2 = "the sky to make it hail ice bombs!"
						elseif samhmrng == 14
						p.samhudmessage1 = "Hit a charged Spazer Beam at point-blank"
						p.samhudmessage2 = "range to send foes flying far away!"
						elseif samhmrng == 15
						p.samhudmessage1 = "The Plasma&Wave combo can be"
						p.samhudmessage2 = "canceled at any time with FIRE."
						elseif samhmrng == 16
						p.samhudmessage1 = "The Wave Combo lets you fire up"
						p.samhudmessage2 = "to 4 charged wave shots at any time!"
						elseif samhmrng == 17
						p.samhudmessage1 = "Try to memorize a stage's layout"
						p.samhudmessage2 = "to take advantage of monitor hotspots!"
						elseif samhmrng == 18
						p.samhudmessage1 = "The Plasma Charge Shot can snake"
						p.samhudmessage2 = "across floors when shot from above!"
						elseif samhmrng == 19
						p.samhudmessage1 = "Alternate Ice shots with Missiles"
						p.samhudmessage2 = "for unmatched explosive damage!"
						elseif samhmrng == 20
						p.samhudmessage1 = "Super Missiles knock foes back with huge"
						p.samhudmessage2 = "explosions. Use them to snatch monitors!"
						elseif samhmrng == 21
						p.samhudmessage1 = "Need to run? Use Super Missiles or "
						p.samhudmessage2 = "Morph Ball bombs to keep foes away!"
						elseif samhmrng == 22
						p.samhudmessage1 = "Reflect projectiles with the invincible"
						p.samhudmessage2 = "Screw Attack! Ignore even Power Bombs!"
						elseif samhmrng == 23
						p.samhudmessage1 = "Direct Screw Attack hits are deadly,"
						p.samhudmessage2 = "but the move leaves you wide open."
						elseif samhmrng == 24
						p.samhudmessage1 = "If you run out of Hyperbeam ammo,"
						p.samhudmessage2 = "you'll sacrifice energy to keep firing!"
						elseif samhmrng == 25
						p.samhudmessage1 = "Your default Power Beam is weak, but"
						p.samhudmessage2 = "foes killed by it drop extra health!"
						elseif samhmrng == 26
						p.samhudmessage1 = "While charging any beam, ammo"
						p.samhudmessage2 = "and health pickups magnetize toward you!"
						elseif samhmrng == 27
						p.samhudmessage1 = "Walljumps can get you to high-up"
						p.samhudmessage2 = "monitors earlier and escape from attacks!"
						elseif samhmrng == 28
						p.samhudmessage1 = "Just too good? Enable challenger mode"
						p.samhudmessage2 = "in the ship and win to gain rewards!"
						elseif samhmrng == 29 and ((p == server or p == admin))
						p.samhudmessage1 = "Want to switch things up? Go into"
						p.samhudmessage2 = "the ship for specific match options!"
						elseif samhmrng == 30 and ((p == server or p == admin)) and not (gametype == GT_HUNTERS)
						p.samhudmessage1 = "Samus supports Match as a novelty."
						p.samhudmessage2 = "Try HUNTERS for the proper balance!"
						elseif samhmrng == 30
						p.samhudmessage1 = "When you fall far behind in points,"
						p.samhudmessage2 = "you gain many perks. Never give up!"				
						elseif samhmrng == 31
						p.samhudmessage1 = "Fire explosives mid-jump to increase"
						p.samhudmessage2 = "evasion AND accuracy at the same time!"
						elseif samhmrng == 32
						p.samhudmessage1 = "Use the speedbooster often to keep"
						p.samhudmessage2 = "up with foes and reach monitors first!"
						elseif samhmrng == 33
						p.samhudmessage1 = "Getting hit by most attacks won't give"
						p.samhudmessage2 = "temporary invincibility. Keep moving!"
						elseif samhmrng == 34
						p.samhudmessage1 = "Charge shots are slower, but easier"
						p.samhudmessage2 = "to land and grant more points+damage!"
						elseif samhmrng == 35
						p.samhudmessage1 = "The Boost Ball is particularly useful"
						p.samhudmessage2 = "around springs and when underwater."
						p.samhudmessage1 = "The Gravity Suit's features shine"
						p.samhudmessage2 = "especially in lava or water stages!"
						elseif samhmrng == 37
						p.samhudmessage1 = "Killing players resets their energy"
						p.samhudmessage2 = "tanks and drops you health or ammo!"
						elseif samhmrng == 38
						p.samhudmessage1 = "You'll lose the Hyperbeam if you"
						p.samhudmessage2 = "die. Use it's devastating shots wisely."
						elseif samhmrng == 39
						p.samhudmessage1 = "Run if you see the Crystal Flash!"
						p.samhudmessage2 = "They'll recover too fast to take down."
						elseif samhmrng == 40
						p.samhudmessage1 = "Don't flee from the Hyper Beam, chase"
						p.samhudmessage2 = "them down before them get more ammo!"
						elseif samhmrng == 41
						p.samhudmessage1 = "You can always scan and charge"
						p.samhudmessage2 = "beams in Matches! Use them lots!"
						elseif samhmrng
						p.samhudmessage1 = "Powering up is crucial to victory!"
						p.samhudmessage2 = "Hunt monitors to gain a massive edge."
						end
						p.samhudmessagetime = 230						

					else

						if samhmrng < 6
						p.samhudmessage1 = "Samus players always drop rings"
						p.samhudmessage2 = "and weapons when hit! Be aggressive!"
						elseif samhmrng < 13
						p.samhudmessage1 = "Only Samus' stronger blasts flinch"
						p.samhudmessage2 = "you. Pierce the small shots fearlessly!"
						elseif samhmrng < 19
						p.samhudmessage1 = "Samus is weak before powered up."
						p.samhudmessage2 = "Take her down as early as possible!"
						elseif samhmrng < 23
						p.samhudmessage1 = "Stay away from floors and walls to"
						p.samhudmessage2 = "minimize Samus' chance of hitting you!"
						elseif samhmrng < 28
						p.samhudmessage1 = "The auto ring deals constant damage"
						p.samhudmessage2 = "to Samus. Use it well for crazy points!"
						elseif samhmrng < 34
						p.samhudmessage1 = "Samus will beeline for strong powerups."
						p.samhudmessage2 = "Predict her movements for easy hits!"
						elseif samhmrng
						p.samhudmessage1 = "The scatter ring deals more damage"
						p.samhudmessage2 = "to Samus the more shots land."
						end
						p.samhudmessagetime = 230						
					end
				end
				if p.samhudmessagetime
				p.samhudmessageget = true
				p.lastsamhmrng = samhmrng
				end
			end
			
			--My inefficient way of calculating who's on top in a Match.
			--Basically, if you're over 750 points behind, you get a buff to help you catch up!
			--An increase in ammo/energy recovery, slightly increased damage, better odds at rare items,
			--and a 1 in 30 chance to use the Crystal Flash upon death! So never give up, the odds are rigged in your favor! 
			--You can tell when you have this buff by the lights on the top of your visor.
			--This feature should help counteract bad RNG, crappy player skill, and late joins a little.

			if leveltime == 2 and players[0] and not players[0].sam_topscorewinnerset --Reset
				players[0].sam_topscore = 0
				players[0].sam_topscoreman = nil
				SetSamusColor(s, p)
				for ns in players.iterate
					if ns.sam_score
						if ns.sam_score != nil and (ns.sam_score >= players[0].sam_topscore)
						players[0].sam_topscore = ns.sam_score
						players[0].sam_topscoreman  = ns
						players[0].sam_topscorewinnerset  = true
						end
					end
				end			
			elseif (leveltime % 35 == 0 or leveltime == 10498 or leveltime == 20998)
			and (leveltime > 2) and players[0]
			and not (players[0].sam_topscorewinnerset)
				if players[0].sam_currenttopscore == nil
				players[0].sam_currenttopscore = 0
				end
				for ns in players.iterate
					if ns.score != nil
					ns.sam_score = ns.score
					end
					if ns.sam_score != nil
						if ns.sam_score and (ns.sam_score >= players[0].sam_currenttopscore)
						players[0].sam_currenttopscore = ns.sam_score
						players[0].sam_currenttopscoreman  = ns
						
						elseif not ns.buffbottomtier and players[0].sam_currenttopscore
						and (ns.sam_score <= players[0].sam_currenttopscore-750) and not (gametype == GT_CTF)
						
						ns.buffbottomtier = true
						
						elseif ns.buffbottomtier and players[0].sam_currenttopscore
						and (ns.sam_score > players[0].sam_currenttopscore-750)
						
						ns.buffbottomtier = false
						end
					end
				end
			end
			if players[0] and players[0].sam_topscorewinnerset
				if players[0].sam_topscore and players[0].sam_topscoreman
					if YouAreSamus(s) and p.samchallenger and (p == players[0].sam_topscoreman)
							if (p.samchallenger < 4)
							S_StartSound(nil, sfx_nxbump)
							print("\x82"+p.name+" has gained a rank \x86"+(p.samchallenger)+"\x82 hunter's commendation for winning as a challenger!")
							p.samchallenger = $+1
							players[0].sam_topscorewinnerset = false
							players[0].sam_topscorelastwinner = p
								if (p.samchallenger >= 4)
								S_StartSound(nil, sfx_bgxpld)
								S_StartSound(nil, sfx_bejet1)	
									for ns in players.iterate
									P_SamFP(ns, PAL_RECYCLE, 4)
									end
								print("\x89"+p.name+" has gained MAX rank for winning in challenger mode! Do not let them get the emeralds at all costs!")
								CONS_Printf(p, "\x82"+"Congratulations, you're at max commendations! At this rank, you can use the exclusive\x82 Hyper Beam Combo!")
								end

							else
							
							S_StartSound(nil, sfx_kc5a)
							print("\x81"+p.name+" is still a max-rank challenger! Break their winning streak!")
							p.samchallenger = $+1
							players[0].sam_topscorewinnerset = false
							players[0].sam_topscorelastwinner = p
							end
					elseif (p != players[0].sam_topscoreman) and (p.samchallenger and (p.samchallenger > 1))
						p.samchallenger = 1
						S_StartSound(s, sfx_kc59, p)
						CONS_Printf(p, "\x85"+"Your commendations have been reset!")
					end
				end
				for ns in players.iterate
					ns.sam_score = 0
					if ns != players[0].sam_topscorelastwinner
						if (ns != p) and (ns.samchallenger and (ns.samchallenger > 1))
						ns.samchallenger = 1
						S_StartSound(nil, sfx_kc59, ns)
						CONS_Printf(ns, "\x85"+"Your commendations have been reset!")
						end
					end
				end
			end
			if leveltime == 4
				if players[0]
				players[0].sam_topscorewinnerset = false
				end
				for ns in players.iterate
					ns.sam_score = 0
				end
				if players[0]
				players[0].sam_currenttopscore = 0
				end
			end
			if (p.spectator)
				if ((p.sam_energytanksstart != nil) and (p.sam_energytanksstart > p.sam_energytanksmax))
				p.sam_energytanksmax = p.sam_energytanksstart
				p.sam_energytanks = p.sam_energytanksmax
					if (leveltime < 350) and not (p.sam_energytanksmax < 2)
					p.sam_energy = (p.sam_energytanksmax*50)-1
					else
					p.sam_energy = 99+(p.sam_energytanksmax*50)
					end
				p.sam_chargebeam = true
				end
			end
		else	
			if p.matchtanks or p.match2tanks
			or (p.cooptanks == nil and p.matchtanks == nil)
				p.sam_energytanksmax = 0
				p.sam_energytanks = 0
				p.matchtanks = false
				p.match2tanks = false
				p.cooptanks = true
			end			
		--Spectator check end.
		end
		
		if p.scannedmons and s.sam_scanpowerup
			if ( (gametype == GT_COOP) and not (table.maxn(p.scannedmons)) )
			or ( (gametype != GT_COOP) and not (table.maxn(p.scannedplays)) )		
			or p.sam_saveselect
				if not p.sam_hyperbeampanic
					s.samradaranim = 9
					s.samradaranimd = false
					p.scannedmons = nil
					p.scannedplays = nil
					S_StopSoundByID(s, sfx_sscan1)
					S_StartSound(s, sfx_samtm7)
					s.sam_scanpowerup = 0
				end
			end
		end
		if p.samescapehud
			p.samescapeseconds = p.samescapehud/35
			p.samescapemseconds = G_TicsToCentiseconds(p.samescapehud)
			if multiplayer or s.health
				p.samescapehud = $-1
			end
			if p.starpostnum or (p.exiting and not multiplayer) or mapheaderinfo[gamemap].lvlttl != "Black Core"
				for ns in players.iterate
					P_SamFP(ns, PAL_RECYCLE, 8)
					ns.samescape = 0
					ns.samescapehud = 0
				end
			end
		end
		
		--Escape run in Black Core!

		if p.samescape
			if (gamemap == 25) and mapheaderinfo[gamemap].lvlttl == "Black Core"
				if multiplayer or (s.health)
					p.samescape = $-1
				end
				if (p.samescape % 105 == 0)
					S_StartSound(nil, sfx_alarm)
				end
				local escaperng = P_RandomRange(1, 10)
				if (escaperng < 3)
				P_StartQuake(6*FRACUNIT, 1)
				s.escb =  P_SpawnMobj(s.x+(P_RandomRange(-2000, 2000))*FRACUNIT, s.y+(P_RandomRange(-2000, 2000))*FRACUNIT, s.z+(P_RandomRange((s.floorz/FRACUNIT), (s.ceilingz/FRACUNIT)))*FRACUNIT, MT_CYBRAKDEMON_MISSILE)
					if s.escb and s.escb.valid
						s.escb.target = s
						P_ExplodeMissile(s.escb)
					end
				elseif (escaperng < 6)
					P_StartQuake(4*FRACUNIT, 1)
					s.escb = P_SpawnMobj(s.x+(P_RandomRange(-2000, 2000))*FRACUNIT, s.y+(P_RandomRange(-2000, 2000))*FRACUNIT, s.floorz, MT_CYBRAKDEMON_VILE_EXPLOSION)
					if s.escb and s.escb.valid
						s.escb.target = s
					end
				else
					P_StartQuake(2*FRACUNIT, 1)
					s.escb = P_SpawnMobj(s.x+(P_RandomRange(-3000, 3000))*FRACUNIT, s.y+(P_RandomRange(-3000, 3000))*FRACUNIT, s.ceilingz-25*FRACUNIT, MT_FALLINGROCK)
					if s.escb and s.escb.valid
						s.escb.flags = $ & ~MF_PAIN
						s.escb.scale = P_RandomRange(1, 3)*FRACUNIT
						s.escb.target = s
					end
				end

				s.escb = P_SpawnMobj(s.x+(P_RandomRange(-6000, 6000))*FRACUNIT, s.y+(P_RandomRange(-7000, 7000))*FRACUNIT, s.z+(P_RandomRange(-1500, 1500))*FRACUNIT, MT_SONIC3KBOSSEXPLODE)
				s.escb.scale = P_RandomRange(1, 4)*FRACUNIT
				
				if s.escb and s.escb.valid and s.escb.z < s.escb.floorz or s.escb.z > s.escb.ceilingz
					s.escb.z = s.escb.ceilingz
				end
				
				s.escb = P_SpawnMobj(s.x+(P_RandomRange(-4000, 4000))*FRACUNIT, s.y+(P_RandomRange(-4000, 4000))*FRACUNIT, s.z+(P_RandomRange(-900, 900))*FRACUNIT, MT_BOSSEXPLODE)
				s.escb.scale = P_RandomRange(1, 4)*FRACUNIT
				
				if s.escb and s.escb.valid and s.escb.z < s.escb.floorz or s.escb.z > s.escb.ceilingz
					s.escb.z = s.escb.ceilingz
				end
				
				s.escb = P_SpawnMobj(s.x+(P_RandomRange(-2000, 2000))*FRACUNIT, s.y+(P_RandomRange(-2000, 2000))*FRACUNIT, s.z+(P_RandomRange((s.floorz/FRACUNIT), (s.ceilingz/FRACUNIT)))*FRACUNIT, MT_SONIC3KBOSSEXPLODE)
				s.escb.scale = P_RandomRange(1, 4)*FRACUNIT

				for ns in players.iterate
					if (p.samescape == 1) and ns.mo and not (ns.exiting and not multiplayer)
						P_SamFP(ns, PAL_NUKE, 26)
						p.samescape = 3281
						ns.samescapehud = 0
						P_KillMobj(ns.mo, ns.mo, ns.mo)
					end
					if ns.starpostnum or (ns.exiting and not multiplayer)
						P_SamFP(ns, PAL_RECYCLE, 8)
						p.samescapehud = 0
						p.samescape = 0
						ns.samescape = 0
						ns.samescapehud = 0
					end
				end
			else --Shouldn't have this!!
				p.samescapehud = 0
				p.samescape = 0		
			end
		end

		if s.sam_sprst and not (p.powers[pw_carry] == CR_NIGHTSMODE) --We just spawned! Time to do a whole lot of crap!
			
			if gamemap == 100 and (p.disablevote != nil or p.ui) --For Subarashii
				for m in mobjs.iterate(mobj)
					if m and m.valid and ((m.flags & MF_SHOOTABLE) and (m.flags & MF_SOLID)) and not m.player
						m.flags = $|MF_NOCLIP & ~MF_SHOOTABLE & ~MF_MONITOR
					end
				end
			elseif ((gamemap == 807) or (gamemap == 806)) --DSZ4+ACZ4 respectively.
				if (mapheaderinfo[gamemap].lvlttl == "Arid Canyon" or mapheaderinfo[gamemap].lvlttl == "Deep Sea") and s.player
				local p = s.player
				local renderer = 1
				local shaders = 1
					if p == consoleplayer --This part doesn't need to sync.
						renderer = CV_FindVar("renderer").value
						shaders = CV_FindVar("gr_shaders").value
						if renderer == 2
						and not shaders
						SAM_COM_BufInsertText(p, "gr_shaders on")
						CONS_Printf(p, "\x80"+"OpenGL shaders turned\x83 ON.\x80 Use\x82 'gr_shaders'\x80 console command to switch if desired.")
						end
					end
					if (p.sam_energy <= 0)
					and (gametype == GT_COOP) --Refresh ammo on these maps
						if p.sammissiles != nil
						and (p.sammissiles < 50)
							p.sammissiles = 50
						end
						if p.samsupermissiles != nil
						and (p.samsupermissiles < 8)
							p.samsupermissiles = 8
						end
						if not p.sampw
							p.sampw = 1
						end
					end
					if (gamemap == 807)
						if p.sam_hidegore
							for m in mobjs.iterate(mobjs)
								if (consoleplayer and p and p == consoleplayer) and (m.type == MT_DARKPZREMAINS)
								m.flags2 = MF2_DONTDRAW
								end
							end
						else
						s.sam_acz4msg = true
						end
					end
				end
			end
			if not multiplayer and not netgame --Reloads your old stuff in single player.
				ActivateHyperMode(p, 7, s, true)
			//	if not p.sam_changedmap
			//	p.sam_dontupdateenergy = true
			//	ProfileSamusSave(s, p, p.sam_saveselected, false, true, false)
			//	SetSamusColor(s, p)
			//	end
			p.sam_changedmap = false	
			end
			p.automobf = nil
			if p.buffbottomtier --Never spawn as bottom tier
				if G_GametypeHasSpectators() and p.sam_scanvisor and (leveltime > 99)
					DoAreaScan(s, p, false) --Get a free scan upon spawn.
				end
			p.buffbottomtier = false
			end
			p.camerascale = FRACUNIT
			if (gametype == GT_COOP)
				if p.sam_emeraldflags and not p.sam_saveselect 
					if ((p == server) or not netgame)
					and p.sam_saveselected != nil --only with a savefile selected.
						emeralds = max(0, min(127, p.sam_emeraldflags))
					end
					p.sam_emeraldflags = 0
				end				
				if not (s.sam_difficultymsg and s.sam_difficultymsgtype == 3)
					if p.fusionmode
						s.sam_difficultytrns = V_90TRANS
						s.sam_difficultymsg = 110
						s.sam_difficultymsgtype = 2
					elseif p.crazysamusmobs
						s.sam_difficultytrns = V_90TRANS
						s.sam_difficultymsg = 110
						s.sam_difficultymsgtype = 1
					end
				end
			end
			p.viewrollangle = 0
			
			if p.superbrakdeath --Brak, PZboss, or Metroid Boss hints
				if (gamemap == 28) -- SuperBrak Hints
					if p.superbrakdeath == 1
						p.samhudmessage1 = "It's time for your acquired items to shine!"
						p.samhudmessage2 = "Blast Brak down with Beam Combos and ammo tanks!"
					elseif p.superbrakdeath == 2
						p.samhudmessage1 = "You'll need to use the speedboosters to stay ahead."
						p.samhudmessage2 = "(Type 'autospeedbooster 1' in the console to automate this.)"
					elseif p.superbrakdeath == 3
						p.samhudmessage1 = "The Spazer and Ice Beam are ineffective on crystals!"
						p.samhudmessage2 = "Your missiles get reflected. Use them for beam combos."
					elseif p.superbrakdeath == 4
						p.samhudmessage1 = "When out of usable ammo, use the charged spazer beam for"
						p.samhudmessage2 = "easy hits. Be on the lookout for any extra ammo Brak drops."
					elseif p.superbrakdeath == 5
						p.samhudmessage1 = "Super Missiles do 2x damage, and Power Bombs 3x!"
						p.samhudmessage2 = "Aim Supers at the floor for their explosive radius!"
					else
						p.samhudmessage1 = "Surely there's a way to take the Hyperbeam back...."
						p.samhudmessage2 = nil
					end
					
					p.samhudmessagetime = 230
					p.samhudmessageflags = V_90TRANS 
					
				elseif (gamemap == 806) --Omega Metroid hints
					if p.superbrakdeath == 1
						p.samhudmessage1 = "The Omega Metroid's stomach is the only vulnerable"
						p.samhudmessage2 = "spot! Concentrate and aim carefully."
					elseif p.superbrakdeath == 2
						p.samhudmessage1 = "Your shots can get cut down by it's Sonic Slash."
						p.samhudmessage2 = "Time your shots when it cannot use that!"
					elseif p.superbrakdeath == 3
						p.samhudmessage1 = "Missiles are extremely effective here. If"
						p.samhudmessage2 = "you run out entirely, try spazer charge shots!"
					elseif p.superbrakdeath == 4
						p.samhudmessage1 = "The Sonic Slash's spread makes blind sprinting"
						p.samhudmessage2 = "dangerous. Slip through the cracks in bursts."
					elseif p.superbrakdeath == 5
						p.samhudmessage1 = "If you get hit by the Sonic Slash, it will always"
						p.samhudmessage2 = "follow up with a ram. Get out of the way!"
					else
						p.samhudmessage1 = "The Blizzard Burst and Gattling Vortex are"
						p.samhudmessage2 = "easier combos for taking the Omega down!"
					end
					
					p.samhudmessagetime = 230
					p.samhudmessageflags = V_90TRANS 
					
				elseif (gamemap == 807) --Dark Samus hints
					if p.superbrakdeath == 1
						p.samhudmessage1 = "The Phazon Dive mimmicks your Screw Attack."
						p.samhudmessage2 = "Answer it with the real deal!"
					elseif p.superbrakdeath == 2
						p.samhudmessage1 = "When Dark Samus teleports, she'll always"
						p.samhudmessage2 = "appear behind you. Strafe and turn around!"
					elseif p.superbrakdeath == 3
						p.samhudmessage1 = "Use the Morph Ball to slip under the Wave"
						p.samhudmessage2 = "Homing Blasts and Phazon Vortex attacks."
					elseif p.superbrakdeath == 4
						p.samhudmessage1 = "Save your Power Bombs or Blizzard Burst for"
						p.samhudmessage2 = "the clone phase to instantly get rid of them!"
					elseif p.superbrakdeath == 5
						p.samhudmessage1 = "Charge the Spazer Beam to catch Dark Samus,"
						p.samhudmessage2 = "and don't get pressured into bad spots!"
					else
						p.samhudmessage1 = "Dark Samus' Hyper Beam has slow tracking, but"
						p.samhudmessage2 = "will devastate you. Keep moving!"
					end

					p.samhudmessagetime = 230
					p.samhudmessageflags = V_90TRANS 	
				else
					p.superbrakdeath = nil
				end
			end
			if p.samshieldhp == nil or p.samshieldhp and (p.samshieldhp < 3)
				if p.samshieldhp == nil
						p.samshieldhp = 3
				end
				if not p.powers[pw_shield] and not (p.samshieldhp and p.samshieldhp == 3)
					p.samshieldhp = 3
				else
					if not p.samshieldhptype
						p.samshieldhptype = p.powers[pw_shield]
						p.samshieldhp = 3
					elseif (p.powers[pw_shield] != p.samshieldhptype)
						p.samshieldhptype = p.powers[pw_shield]
						p.samshieldhp = 3
					end
				end
			end
			if netgame and leveltime < 2
				p.samnetcon = 0
			end
			
			s.samemwarning = 0
			
			if not p.firstsamusmap and (gametype == GT_COOP) --Our starting map when we're loading into a new save file.
				p.firstsamusmap = gamemap
			end
			if p.samescapehud and ( (((YouAreSamus(s)) == false) and not multiplayer) or not (gamemap == 25) )
				p.samescapehud = 0
			end
			if p.samescape and ( (((YouAreSamus(s)) == false) and not multiplayer) or not (gamemap == 25) )
				p.samescape = 0
				p.samescapehud = 0 
			end
			if p.sam_restricthyper --If the restricted Hyper Beam setting is on, the Hyper Beam can't be used if a boss is around.
				if not (p.sam_restricthyper.valid and p.sam_restricthyper.health)
					p.sam_restricthyper = nil
				end
			end
			if not SamOncePerMap2 and (p.samworldevents or (gametype == GT_HUNTERS)) and mapheaderinfo[gamemap]
				if ((gametype == GT_COOP) or (gamemap == 532) or (gamemap == 539))
				  	if mapheaderinfo[gamemap].lvlttl == "Greenflower"
						for m in mobjs.iterate(mobj)
							if (gamemap == 01)  --Weather change GFZ1 + dialogue
								if m.valid and (m.type == MT_GFZFLOWER1) and (m.x == 1920*FRACUNIT) and (m.y == 7744*FRACUNIT)
								p.samweather = 1
								m.state = S_NULL
								end
								for z in players.iterate
									if z.samworldevents --Go to Deep Zone 4 next!
										if YouAreSamus(z.mo) 
										SamusDialogue(z.mo, "The gunship has taken heavy damage. Idle", "thrusters online. Self-repair engaged.", "SAMDEF", 1, 1, 50)	
										end							
									end
								end
							elseif (gamemap == 02)  --Weather change GFZ2+dialogue
								if m.valid and (m.type == MT_GFZFLOWER1) and (m.x == -4032*FRACUNIT) and (m.y == -512*FRACUNIT)
								p.samweather = 1
								m.state = S_NULL
								end
								for z in players.iterate
									if z.samworldevents and YouAreSamus(z.mo)
									SamusDialogue(z.mo, "The world's overrun by rampaging robots. ", "What purpose are they meant to serve?", "SAMDEF", 2, 5, 42)
									end
								end
							elseif (gamemap == 03) --Weather change GFZ3
								if m.valid and (m.type == MT_GFZFLOWER1) and (m.x == 3840*FRACUNIT) and (m.y == 2752*FRACUNIT)
								p.samweather = 1
								m.state = S_NULL
								end
							end
						end
					elseif mapheaderinfo[gamemap].lvlttl == "Techno Hill"
						if (gamemap == 04) --dialogue THZ1
							for z in players.iterate
								if z.samworldevents 
								and YouAreSamus(z.mo) and not leveltime 
								SamusDialogue(z.mo, "All this machinery appears to be in service to that scientist. But why wildly", "wreak havoc across the planet? What could such untargeted aggression", "SAMPRE", 2, 6, 42, "small", "small", "possibly achieve? It's uncharacteristic for intelligent life...")
								end
							end
						elseif (gamemap == 05) --dialogue THZ2
							for z in players.iterate
								if z.samworldevents 
								and YouAreSamus(z.mo) and not leveltime 
									SamusDialogue(s, "Target signal found in current area. ", "Signal waning, lifeforms left recently.", "SAMDEF", 1, 7, 90, nil, nil)					
								end
							end
						end
					elseif mapheaderinfo[gamemap].lvlttl == "Deep Sea"						
						if (gamemap == 07) --dialogue DSZ1
							for z in players.iterate
								if z.samworldevents 
								and YouAreSamus(z.mo) and not leveltime
									SamusDialogue(z.mo, "Target appears present in current zone.", "Continuing scan of surrounding area...", "SAMDEF", 1, 0, 42)
								end
							end								
						elseif (gamemap == 08) --Dialogue and Zone Change DSZ2
							for z in players.iterate
								z.mo.dsz2msgprep = true
									if z.samworldevents
									G_SetCustomExitVars(806) --Go to Deep Zone 4 next!
										if YouAreSamus(z.mo) and not leveltime
											SamusDialogue(z.mo, "Scanning...\x85warning! Caution advised. ", "\x82 Approaching multiple Metroid life signals. Maximum firepower recommended.", "SAMDEF", 1, 2, 90, nil, "small")
										end							
									end
							end	
						elseif (gamemap == 806) --Deep Sea Zone 4 enemies turned to husks by Metroids.
							for m in mobjs.iterate(mobj)
								if m and m.valid
								 	if (m.type == MT_EGGSHIELD) or (m.type == MT_CRUSHCLAW) or (m.type == MT_EGGSHIELD)
										m.state = S_NULL
									elseif (m.type == MT_BLUECRAWLA) or (m.type == MT_FACESTABBERSTATUE) 
									or (m.type == MT_GFZFISH) or ((m.type >= MT_SKIM) and (m.type <= MT_DRAGONTAIL))
										m.colorized = true
										m.health = 1
										m.color = SKINCOLOR_BEIGE
										m.flags = MF_SHOOTABLE|MF_SOLID|MF_SCENERY|MF_NOGRAVITY|MF_NOCLIPHEIGHT
										m.tics = -1
										m.metroidstatue = true
									end
								end
							end						
						end
					elseif mapheaderinfo[gamemap].lvlttl == "Castle Eggman"
						if (gamemap == 10) --Samus' self-reflection on fighting an Omega Metroid for the first time "canonically"
							for z in players.iterate
								if z.samworldevents 
								and YouAreSamus(z.mo) and not leveltime
									
									SamusDialogue(z.mo, "Was that truly a Metroid? How did", "they propagate that kind of mutation?", "SAMDEF", 2, 9, 42)
								end
							end					
						elseif (gamemap == 11)
						
						--[SAMUS DIALOGUE EXAMPLE PART 1] Example of how to start a custom dialogue chain.
						--Uncomment this, then CTRL+F: "DIALOGUE EXAMPLE PART 2" and uncomment that for this to appear in-game(Castle Eggman act 2)
						
						/*	SamusDialogue(s, "I am sick of these motherfucking ", "guards in this motherfucking castle.", "SAMSAD", 2, nil, nil)
							s.samusdialogue.customnextbox = 3 */
						
						end
					elseif mapheaderinfo[gamemap].lvlttl == "Arid Canyon"					
						if (gamemap == 13) --Nighttime ACZ1 events.
							for m in mobjs.iterate(mobj)
								if m.valid and (m.type == MT_REDSPRING) and (m.x == 288*FRACUNIT) and (m.y == -12064*FRACUNIT)
								m.state = S_NULL
								p.samweather = 1
								end
							end
							for z in players.iterate
								if z.samworldevents 
								and YouAreSamus(z.mo) and not leveltime 
									if not z.samspbs and not z.sam_spiderball 
									and not z.sam_screwattack and not z.sam_spacejump
									SamusDialogue(z.mo, "Closing in on target signal.", "Signal moving at 20 miles per hour.", "SAMDEF", 1, 13, 42)
									else
									SamusDialogue(z.mo, "Closing in on target signal.", "Signal moving at 20 miles per hour.", "SAMDEF", 1, 14, 42)
									end
								end
							end						
						elseif (gamemap == 14) --Hide dead Fang gore in ACZ4?
							for m in mobjs.iterate(mobj)
								if m.valid and (m.type == MT_REDHORIZ) and (m.x == 7584*FRACUNIT) and (m.y == -8944*FRACUNIT)
								m.state = S_NULL
								p.samweather = 1
								end
								for z in players.iterate
									if z.samworldevents --Go to Arid Canyon 4 next!
									G_SetCustomExitVars(807)
										if YouAreSamus(z.mo) and not leveltime 
											if p.sams_itemcompletion != nil and (p.sams_itemcompletion < 4) 
											SamusDialogue(z.mo, "Target signal has stopped moving.", "\x85Warning! Metroid scan confirms mutation.", "SAMDEF", 1, 10, 62)
											elseif p.sams_itemcompletion != nil and (p.sams_itemcompletion > 85)
											SamusDialogue(z.mo, "Signal has entered nearby mines.", "\x85Warning! Metroid scan confirms mutation.", "SAMDEF", 1, 15, 62)
											else
											SamusDialogue(z.mo, "Signal has entered nearby mines.", "\x85Warning! Metroid scan confirms mutation.", "SAMDEF", 1, 11, 62)
											end
										end
									end
								end
							end
						elseif (gamemap == 807) --Hide dead Fang gore in ACZ4, a reference to the corpse in front of Kraid's Lair?
							if p.sam_hidegore
								for m in mobjs.iterate(mobjs)
									if (consoleplayer and p and p == consoleplayer) and (m.type == MT_DARKPZREMAINS)
									m.flags2 = MF2_DONTDRAW
									end
								end
							end
						end
					elseif mapheaderinfo[gamemap].lvlttl == "Red Volcano"
						if (gamemap == 16)
							if p.samworldevents and not p.sam_scanvisor and not s.sam_rvzmsg
							s.sam_rvzmsg = 0						
								for m in mobjs.iterate(mobjs)
									if (m.type == MT_SCANVISOR)
									s.sam_rvzmsg = $+1
									end
								end
								if not s.sam_rvzmsg
								P_SpawnMobj(4384*FRACUNIT, 17888*FRACUNIT, 544*FRACUNIT, MT_SCANVISOR)
									for z in players.iterate
										if z.mo
										z.mo.sam_rvzmsg = true
										end
									end
								end
								s.sam_rvzmsg = true
							end
							for z in players.iterate
								if z.samworldevents 
								and YouAreSamus(z.mo) and not leveltime
									SamusDialogue(z.mo, "Ship auto-repairs are 96% complete.", "Sending mission status report to HQ.", "SAMDEF", 1, 17, 42)
								end
							end						
						end
					elseif mapheaderinfo[gamemap].lvlttl == "Egg Rock"
						if (gamemap == 22)
							for z in players.iterate
								if z.samworldevents 
								and YouAreSamus(z.mo) and not leveltime 
									if z.sam_hyperbeam
									SamusDialogue(z.mo, "I won't be stopped now.", nil, "SAMDEF", 2, 22, 44)
									elseif p.sams_itemcompletion != nil and (p.sams_itemcompletion < 4)
									SamusDialogue(z.mo, "Sabotaging the core could work, but", " it'll take a miracle to escape alive...", "SAMSAD", 2, 0, 44)
									else
									SamusDialogue(z.mo, "To truly destroy the station, I'll", "need extreme firepower. Is this enough?", "SAMSAD", 2, 0, 44)
									end
								end
							end
						elseif (gamemap == 23)
						s.discomsgprep = true
							for z in players.iterate
								if z.samworldevents 
								and YouAreSamus(z.mo) and not leveltime 					
									if z.sam_fullypoweredsuit
									SamusDialogue(z.mo, "This is far too easy... surely they", "must be aware what they're up against?", "SAMDEF", 2, 0, 44)
									elseif p.sams_itemcompletion != nil and (p.sams_itemcompletion < 4)
									SamusDialogue(z.mo, "This is utter insanity. I've gone in", "way over my head here. ...yet...", "SAMSAD", 2, 23, 44)
									else
									SamusDialogue(z.mo, "Nearing the core. Last opportunity", "for powering up is here.", "SAMPRE", 2, 0, 44)
									end
								end
							end						
						end					
					elseif mapheaderinfo[gamemap].lvlttl == "Black Core"					
						if (gamemap == 25)
						  for m in mobjs.iterate(mobj)
							if m.valid and (m.type == MT_METALSONIC_RACE)
							and not (m.flags2 & MF2_DONTDRAW) and ((m.x == 3072*FRACUNIT) and (m.y == 2496*FRACUNIT) and (m.z == 256*FRACUNIT))
							p.samweather = 1
								if not p.samworldevents 
									p.samweather = 2
									p.samweather = 2
									p.samescapehud = nil
									p.samescapeseconds = nil
									p.samescapemseconds = nil
								end
								if not (p.samweather and (p.samweather == 2)) and not (m.flags2 & MF2_DONTDRAW)
									m.msp = P_SpawnMobj(3000*FRACUNIT, 2433*FRACUNIT, 300*FRACUNIT, MT_SAMSUPERMISSILE)
									P_ExplodeMissile(m.msp)
									m.msp = P_SpawnMobj(3142*FRACUNIT, 2433*FRACUNIT, 320*FRACUNIT, MT_SAMSUPERMISSILE)
									P_ExplodeMissile(m.msp)
									m.msp = P_SpawnMobj(-9440*FRACUNIT, -1600*FRACUNIT, 0, MT_SPRINGSHELL)
									m.msp.angle = ANGLE_270
									m.msp = P_SpawnMobj(-9973*FRACUNIT, -7040*FRACUNIT, 600*FRACUNIT, MT_SNAILER)
									m.msp.eflags = $|MFE_VERTICALFLIP
									m.flags = $|MF_NOCLIP
									m.flags2 = $|MF2_DONTDRAW
								end
							end
						  end
						elseif gamemap == 26
							s.bcz4prep = true
						end
					else
						if (gamemap == 532) and mapheaderinfo[gamemap].lvlttl == "Jade Valley"	
						  	for m in mobjs.iterate(mobj)
								if m.valid and (m.type == MT_RING) and ((m.x >= -4192*FRACUNIT) and (m.y == -768*FRACUNIT) and (m.z >= 928*FRACUNIT))
								m.state = S_NULL
								p.samweather = 1
								end
						  	end
						end 
					end
				end
				if p.samweather and (p.samweather == 1)
					if (gamemap == 13) --Nighttime ACZ1
						for sector in sectors.iterate
							sector.lightlevel = ($-(sector.lightlevel/4))
						end
						P_SetupLevelSky(6, true)
					elseif (gamemap == 14) --Sunrise ACZ2
						for sector in sectors.iterate
							if (sector.lightlevel > 192)
							sector.lightlevel = ($-(sector.lightlevel/9))
							else
							sector.lightlevel = ($-(sector.lightlevel/10))
							end
						end
						P_SetupLevelSky(15, true)
					elseif (gamemap == 25) --Escape Sequence Black Core
						for ns in players.iterate
						ns.spawnsship = false
						ns.samescapehud = 3281
						end

						for sector in sectors.iterate
							if sector.lightlevel and (sector.lightlevel > 31) 
							and not (sector.floorheight <= -1919*FRACUNIT)
							sector.lightlevel = ($-(sector.lightlevel/9))
							end
							if (sector.floorheight == 448*FRACUNIT) and (sector.ceilingheight == 448*FRACUNIT)
							sector.tag = 0
							sector.floorheight = 256*FRACUNIT
							sector.ceilingheight = 640*FRACUNIT
							end
						end
						if players[0]
						players[0].samescape = 3281
						elseif players[1]
						players[1].samescape = 3281
						end
						P_SwitchWeather(PRECIP_STORM_NORAIN)
						
					elseif (gamemap == 532) and mapheaderinfo[gamemap].lvlttl == "Jade Valley" --Metroid-styled Jade Valley.
					
						for m in mobjs.iterate(all)
							if m and m.valid and m.type == MT_GFZFLOWER1
							P_SpawnMobj(m.x, m.y, m.z, MT_BLUECRYSTAL)
							m.state = S_NULL
							end
							if m and m.valid and m.type == MT_GFZFLOWER2
							P_SpawnMobj(m.x, m.y, m.z, MT_STALAGMITE8)
							m.state = S_NULL
							end
							if m and m.valid and (m.type == MT_GFZFLOWER3) // or m.type == MT_BSZTULIP_PURPLE
							P_SpawnMobj(m.x, m.y, m.z, MT_CEZBUSH1)
							m.state = S_NULL
							end
							if m and m.valid and m.type == MT_BERRYBUSH
							P_SpawnMobj(m.x, m.y, m.z, MT_BSZBUSH_CYAN)
							m.state = S_NULL
							end
							if m and m.valid and m.type == MT_BUSH
							P_SpawnMobj(m.x, m.y, m.z, MT_BSZBUSH_RED)
							m.state = S_NULL
							end
						end
						for sector in sectors.iterate
							sector.lightlevel = ($-(sector.lightlevel/3))
							if sector.tag == 5
								sector.lightlevel = 224
							end
							if sector.tag == 4 or sector.tag == 3 or sector.tag == 1
								sector.lightlevel = 255
							end
							if sector.lightlevel < 140
								sector.lightlevel = 224
							end
							if sector.ceilingpic == "FWATER1"
								sector.floorpic = "LWATER1"
								sector.ceilingpic = "LWATER1"
							end
							if sector.ceilingpic == "GFZFLR01"
								sector.ceilingpic = "RCZFLR3"
							end
							if sector.floorpic == "GFZFLR01"
								sector.floorpic = "RCZFLR3"
							end
							if sector.floorpic == "GFZFLR02"
								sector.floorpic = "JNGGRS02"
							end
							if sector.floorpic == "GFZFLR10"
								sector.floorpic = "JNGGRS10"
							end
							if sector.ceilingpic == "GFZFLR02"
								sector.ceilingpic = "JNGGRS02"
							end
							if sector.ceilingpic == "GFZFLR10"
								sector.ceilingpic = "JNGGRS10"
							end
						end
						sectors[4].ceilingheight = 476*FRACUNIT
						sectors[61].ceilingheight = 400*FRACUNIT
						sectors[61].floorpic = "JNGGRS02"
						sectors[69].ceilingheight = 300*FRACUNIT
						sectors[69].floorheight = 12*FRACUNIT
						sectors[86].ceilingheight = 350*FRACUNIT
						sectors[87].ceilingheight = 350*FRACUNIT
						sectors[89].ceilingheight = 350*FRACUNIT						
						sectors[3].special = 0
						sectors[57].special = 13
						for side in sides.iterate //Texture check
							if side.toptexture and (side.toptexture == "#40A0FFJ")
							side.toptexture = "#00E0C0"
							side.bottomtexture = "#00FFFF"
							end
							if side.bottomtexture and side.bottomtexture >= R_TextureNumForName("GFZROCK") and side.bottomtexture <= R_TextureNumForName("GFZCHEK2")
							side.bottomtexture = R_TextureNumForName("SHALE1")
							end
							if side.midtexture and side.midtexture == R_TextureNumForName("FWBY_04")
							side.midtexture = R_TextureNumForName("LFALL1")
							end
							if side.midtexture and side.midtexture >= R_TextureNumForName("OFALL1") and side.midtexture <= R_TextureNumForName("GOOPW16")  --3532
							side.midtexture = R_TextureNumForName("LFALL1") --3548 
							end							
							if side.toptexture and ((side.toptexture >= R_TextureNumForName("GFZROCK")) and (side.toptexture <= R_TextureNumForName("GFZCHEK2")))  --1481, 1483, 1486
							side.toptexture = R_TextureNumForName("SHALE1") --Was this function always here or was I just an idiot?  	GFZROCK 
							end
							if side.midtexture and ((side.midtexture > R_TextureNumForName("GFZROCK")) and (side.midtexture <= R_TextureNumForName("GFZCHEK2")))
							side.midtexture = R_TextureNumForName("SHALE1")
							end							
							if side.midtexture and (side.midtexture == R_TextureNumForName("GFZROCK"))
							side.midtexture = R_TextureNumForName("RCZFLR5") --120
							end
							if (side.bottomtexture == R_TextureNumForName("GHZWALL7"))
							side.bottomtexture = R_TextureNumForName("RCZWLL5")
							end
							if (side.bottomtexture == R_TextureNumForName("GHZWALL7"))
							side.bottomtexture = R_TextureNumForName("RCZWLL5")
							end
							if (side.bottomtexture == R_TextureNumForName("GHZWALL6"))
							side.bottomtexture = R_TextureNumForName("RCZWLL3")
							end
							if (side.midtexture == R_TextureNumForName("GHZWALL6"))
							side.midtexture = R_TextureNumForName("SHALE1")
							end							
						end				
						P_SwitchWeather(PRECIP_STORM_NORAIN)
						P_SetupLevelSky(22, true)					
					else
						for sector in sectors.iterate --GFZ1 weather change
							sector.lightlevel = ($-31)
							if sector.ceilingpic == "GFS_CLD1"
							or sector.ceilingpic == "GFS_CLD2"
							or sector.ceilingpic == "GFS_CLD3"
							sector.ceilingpic = "GFS_CLDD"
							end
						end
						P_SwitchWeather(PRECIP_STORM)
						P_SetupLevelSky(20, true)
					end
				end
				SamOncePerMap2 = true
				p.samweather = 0
			end	
			if YouAreSamus(s)
				s.spawnsamuscolors = 6
				s.missiletrns = 0
				s.supermissiletrns = 0
				s.pwbtrns = 0
				p.sams_morphcd = 2
				p.sams_usecd = 2
				s.scale = 90000 --Samus is sized 1.4x vanilla Sonic. She should be bigger, tbh, but gameplay gets weird.
				s.samusscaled = true
				s.destscale = s.scale
				
				if p.sam_firstperson --remember between levels what your FPS preference was.
				or not CV_FindVar("chasecam").value	
					if not twodlevel and not (s.flags2 & MF2_TWOD)
					and not p.sammorphed
						SamCam(p, false)
						p.sam_firstperson = true
						p.sam_prevcam = nil
					end
				end
			end
			
			p.samtoken = 0
			p.samustimeshow = 0
			
			if (p.sam_energy and (p.sam_energy < 50) and not p.samusrespawnfunc)
				p.sam_energy = 50
			end
			
			SetSamusColor(s, p) 
			
			if (gametype != GT_COOP)
			p.sam_fullypoweredsuit = false
			p.fusionmode = false
			p.speedrunoverride = false
			p.sams_completiontime = 0
			p.sam_gameover = 0
			s.samspawnprotect = 150
			s.testhyperbeam = nil
				--Is that hyper dude still running around?
					for z in players.iterate
						if z.hyperbeam
						s.testhyperbeam = true
						end
					end
					if s.testhyperbeam
					s.testhyperbeam = nil
					p.sam_hyperbeampanic = 84
						if z.mo
							DoAreaScan(p, z.mo)
						end
						if not p.sammissiles or (p.sammissiles < 10)
						p.sammissiles = 10
						end
						if not p.nosammusic
							SAM_COM_BufInsertText(p, "tunes -none; wait 1; tunes SAMUSH")
						end	
					end
			
					if (gametype == GT_RACE)
					p.sam_energytanksmax = 0
					p.sam_energy = 99					
					end
			elseif not p.gameoverready and p.lives
			p.gameoverready = true
			end
			
			if p.sammorphed
			and not ((s.samspinpad) or (p.readyreadyready))
			MorphballReturn(s)
			end
			if (p.sammissiles != nil)
				if p.buffbottomtier
				and (p.sammissiles < 20)
					p.sammissiles = 20
				elseif (p.sammissiles < 5)
					p.sammissiles = 5
				end
			end
			if p.samsupermissiles != nil and p.buffbottomtier
				if (p.samsupermissiles < 2) --Damn, the noobs have it made.
					p.samsupermissiles = 2
				end
			end
			if p.sam_energytanksmax == nil
				p.sam_energytanksmax = 0
			end
			s.sam_ldclmb = 0
			if G_GametypeHasSpectators() and not (gametype == GT_RACE) and p.sam_energytanksmax != nil
			
				if not (p.samusmatch and (p.samusmatch == 1))
				p.sam_energytanksmax = p.sam_energytanksstart
					if (leveltime and (leveltime < 350)) and not (p.sam_energytanksmax != nil and (p.sam_energytanksmax < 2))
					p.sam_energy = (p.sam_energytanksmax*50)-1
					else
					p.sam_energy = 99+(p.sam_energytanksmax*50)
					end
				end
					if (leveltime < 350) and not (p.sam_energytanksmax !=nil and (p.sam_energytanksmax < 2))
					p.sam_energy = (p.sam_energytanksmax*50)-1
					else
					p.sam_energy = 99+(p.sam_energytanksmax*50)
					end
			p.sam_energytanks = p.sam_energytanksmax
				if p.sam_hyperbeam
				p.sam_hyperbeam = false
				end
			elseif p.matchtanks
				p.sam_energytanksmax = 0
				p.sam_energytanks = 0
			end
			if (p.sam_gametype == nil) --Our first gametype, do nothing!
				p.sam_gametype = gametype
			elseif (p.sam_gametype != gametype) --Start with nothing in these gamemodes.
			and not ( (G_GametypeHasSpectators() and p.samusmatch) or (gametype == GT_RACE) )
				p.samspbs = false
				p.sambsbl = false
				p.sam_spiderball = false
				p.sam_screwattack = false
				p.sam_gravitysuit = 0
				SetSamusColor(s, p) 
				p.sam_highjump = false
				p.sam_bombs = false
				p.sam_spacejump = false
				p.sam_spazerbeam = false
				p.sam_wavebeam = false
				p.sam_icebeam = false
				p.sam_plasmabeam = false
				p.sam_powerbeamcombo = false
				p.sam_spazerbeamcombo = false
				p.sam_wavebeamcombo = false
				p.sam_icebeamcombo = false
				p.sam_plasmabeamcombo = false
				p.sam_hyperbeam = false
				p.sammissilesmax = 5
				p.sampwmax = 0
				p.samsupermissilesmax = 0
				p.sam_fullypoweredsuit = false
				if not G_GametypeHasSpectators()
					if not (gametype == GT_RACE)
						p.sam_chargebeam = false
						p.sammissilesmax = 0
					end
					p.sam_ammodigitizer = 1
					p.sam_autotank = nil
					p.sam_scanvisor = false
					p.sam_energytanksmax = 0
					p.sam_energytanks = 0
				end
				
				SamResetBeam(p)
					if (leveltime < 350) and not (p.sam_energytanksmax != nil and (p.sam_energytanksmax < 2))
						p.sam_energy = (p.sam_energytanksmax*50)-1
					else
						p.sam_energy = 99+(p.sam_energytanksmax*50)
					end
					if (gametype != GT_HUNTERS)
						hud.enable("stagetitle")
					end
				p.sam_gametype = gametype		
			end
			if (p.sam_energy <= 0)
				if p.samusrespawnfunc
					p.sam_energy = p.samusrespawnfunc
				else
					p.sam_energy = 99+(p.sam_energytanksmax*50)
				end							
			end
		s.samusjustspawned = true
		SamSpiderCharge(p, s, 103)
		
		if (gametype != GT_COOP) or (gamemap == 25)
		p.sam_fpsmsg = 0
		end
		p.sams_morphcd = 0
		p.bsblboost = 0
		p.bsblboostattack = 0
		p.samspbscharge = 0
		p.sam_shinespark = 0
		CancelScrewAttack(s,p)
		p.samshieldhp = 3
		p.sam_shinesparkfly = 0
		p.samspbschargefull = false
		p.samushasatimer = false
		p.morphanim = false
		p.sam_beamswitch = 2
		p.sams_supermissileready = 0
		p.sams_mshotcd = 10
		p.sam_powerbeammeltdown = 0
		p.sam_spazerbeammeltdown = 0
		p.sam_icebeammeltdown = 0
		p.sam_wavebeammeltdown = 0
		p.sam_plasmabeammeltdown = 0
		p.sams_beamcombocharge = 0
		p.sam_hyperbeampanic = 0
		
			if p.samusrespawnfunc
				P_SamFP(p, PAL_WHITE, 6)
				S_StartSound(s, sfx_samspa)
				P_PlayRinglossSound(s)
				p.samusrespawnfunc = nil
			else
				--Save your ammo and reload if you die!
				if not p.starposttime and (gametype == GT_COOP) and gamemap
					if (p.sam_lastplaymap == nil or gamemap == p.sam_lastplaymap)
					and p.samuschosefile
						if p.sam_lastmissiles and p.sammissiles != nil and p.sam_lastmissiles > p.sammissiles
							p.sammissiles = p.sam_lastmissiles
						end
						if p.sam_lastsupermissiles and p.samsupermissiles != nil and p.sam_lastsupermissiles > p.samsupermissiles
							p.samsupermissiles = p.sam_lastsupermissiles
						end
						if p.sam_lastpw and p.sampw != nil and p.sam_lastpw > p.sampw
							p.sampw = p.sam_lastpw
						end						
					end
					p.sam_lastplaymap = gamemap
					p.sam_lastmissiles = p.sammissiles
					p.sam_lastsupermissiles = p.samsupermissiles
					p.sam_lastpw = p.sampw
				end			
			end
			
			if YouAreSamus(s)
				p.powers[pw_shield] = SH_NONE
			end
			
			if (p.sam_spazerbeamequip and not p.sam_spazerbeam)
			or (p.sam_wavebeamequip and not p.sam_wavebeam)
			or (p.sam_icebeamequip and not p.sam_icebeam)
			or (p.sam_plasmabeamequip and not p.sam_plasmabeam)
			or (p.sam_hyperbeamequip and not p.sam_hyperbeam)
				SamResetBeam(p)
			end
			if not p.sam_power_grip --Remove object incase.
				SAM_RemoveLDgrp(s)
			end
		SetSamusColor(s, p)
		s.sam_sprst = nil
		end

	--Enter zee ship.
		if (p.sam_st and not p.sam_st.valid)
		p.sam_st = nil
		p.sam_intoship = 0
			if p.sam_firstperson
			SamCam(p, false)
				if s.valid
				p.visorclosein = 15
				end
			end
		p.samentercam = false
			if (s.flags & MF_NOCLIPHEIGHT) or (p.pflags & PF_INVIS)
			s.flags = $|MF_SHOOTABLE & ~MF_NOGRAVITY & ~MF_NOCLIP & ~MF_NOCLIPHEIGHT & ~MF_BOUNCE
			p.pflags = $ & ~PF_INVIS & ~PF_FORCESTRAFE
			end
		p.sam_exitship = 0
		p.accelstart = skins[s.skin].accelstart
		end
		if p.newsamfile and not p.sam_saveselect
			p.newsamfile = false
		end
		if p.sam_shipjingle
			if p.sam_shipjingle == 1
				SAM_COM_BufInsertText(p, "tunes -default")
				P_RestoreMusic(p)
			end
			p.sam_shipjingle = $-1
		end
		
		--Show increased numbers for the HUD!
		
		if (p.sammissiles > p.micatchup) 
			p.micatchupflash = 20
			p.micatchupnum = ((p.sammissiles)-(p.micatchup))
		end
		if (p.samsupermissiles > p.smcatchup)
			p.smcatchupflash = 20
			p.smcatchupnum = ((p.samsupermissiles)-(p.smcatchup))
		end
		if (p.sampw > p.pbcatchup)
			p.pbcatchupflash = 20
		end
		if (p.encatchup != nil) and (p.sam_energy < p.encatchup)
			p.encatchupflash = 8
		end
		p.encatchup = p.sam_energy
		p.micatchup = p.sammissiles
		p.smcatchup = p.samsupermissiles
		p.pbcatchup = p.sampw

		--Show collection stats and total time!
		if p.sams_completiontime != nil
			if not paused and leveltime
				p.sams_completiontime = $+1
			end
		end
		if p.samustimeshow and p.samustimeshow == 245 and p.sams_completiontime
		or (leveltime % 50 == 0) and not (p.samustimeshow and p.samustimeshow < 245)
			p.sams_completiontimehours = G_TicsToHours(p.sams_completiontime)
			p.sams_completiontimeminutes = G_TicsToMinutes(p.sams_completiontime, false)
			p.sams_completiontimeseconds = G_TicsToSeconds(p.sams_completiontime)
			
			local itemcounter = 0 --Start from zero
			
			if p.sambsbl
			itemcounter = $+1 end
			if p.samspbs
			itemcounter = $+2 end 
			if p.sam_screwattack
			itemcounter = $+2 end
			if p.sam_gravitysuit
				if p.sam_gravitysuit == 2  
					itemcounter = $+4
				else
					itemcounter = $+2
				end
			end
			if p.sam_highjump
			itemcounter = $+1 end
			if p.sam_power_grip
			itemcounter = $+1 end
			if p.sam_bombs
			itemcounter = $+1 end
			if p.sam_spacejump
			itemcounter = $+2 end
			if p.sam_spazerbeam
			itemcounter = $+2 end
			if p.sam_wavebeam
			itemcounter = $+2 end
			if p.sam_icebeam
			itemcounter = $+2 end
			if p.sam_plasmabeam
			itemcounter = $+2 end 
			if p.sam_hyperbeam
			itemcounter = $+10 end 
			if p.sammissilesmax 
				if (p.sammissilesmax > 100)
				itemcounter = $+20
				else
				itemcounter = $+(p.sammissilesmax/5)
				end
			end
			if p.samsupermissilesmax 
				if (p.samsupermissilesmax > 10)
				itemcounter = $+10
				else
				itemcounter = $+p.samsupermissilesmax
				end
			end 
			if p.sam_energytanksmax
				itemcounter = $+p.sam_energytanksmax
			end
			if p.sam_chargebeam
			itemcounter = $+1 end
			if p.sampwmax
				if (p.sampwmax > 5)
					itemcounter = $+15
				else
					itemcounter = $+p.sampwmax*3
				end
			end
			if p.sam_spazerbeamcombo
			itemcounter = $+2 end
			if p.sam_wavebeamcombo
			itemcounter = $+2 end
			if p.sam_icebeamcombo
			itemcounter = $+2 end
			if p.sam_plasmabeamcombo
			itemcounter = $+2 end
			if p.sam_powerbeamcombo
			itemcounter = $+2 end

			p.sams_itemcompletion = itemcounter --And our item completion rate iiiis...?

		elseif p.sams_completiontimehours == nil
			p.sams_completiontimehours = 0
			p.sams_completiontimeminutes = 0
			p.sams_completiontimeseconds = 0
			p.sams_itemcompletion = 0
		end
		if p.samustimeshow
		p.samustimeshow = $-1
		end
		if p.samitemgettimer
		p.samitemgettimer = $-1
		end
		if p.encatchupflash
		p.encatchupflash = $-1
		end
		if p.micatchupflash
		p.micatchupflash = $-1
		end
		if p.smcatchupflash
		p.smcatchupflash = $-1
		end
		if p.pbcatchupflash
		p.pbcatchupflash = $-1
		end
		if p.sam_energyflash
		p.sam_energyflash = $-1
		end
		if p.sams_beamanim
		p.sams_beamanim = $-1
		end
		if p.sams_morphcd
		p.sams_morphcd = $-1
		end
		if p.sam_spbsgrace
			p.sam_spbsgrace = $-1
			if (p.sam_spbsgrace > 16)
			p.sam_spbsgrace = 16
			end
		end
	/*	if p.sam_negatewallc
			p.sam_negatewallc = $-1
			if (p.sam_negatewallc > 5)
			p.sam_negatewallc = 5
			end
		end */		
		if p.beampain
			p.beampain = $-1
			s.tempsamdye = SKINCOLOR_SUPERSILVER1
			s.tempsamdyetr = TR_TRANS30
		end	
		if p.sam_darkpz
			if p.sam_darkpz == 1
				SAM_COM_BufInsertText(p, "tunes TNSION")
			else
				SAM_COM_BufInsertText(p, "tunes DARKPZ")
			end
			p.sam_darkpz = nil
		end
		if p.beampulse
			p.beampulse = $-1
		else
			p.beampulse = 68
		end
		if p.sams_notyet
			p.sams_notyet = $-1
		end
		if p.wbball
			p.wbball = $-1
		end
		if p.visorclosein and not p.sam_saveselect
			p.visorclosein = $-1
		end
		if p.gltimer --There was a better way, but here we are.
			if (p.gltimer > 75) or (p.gltimer < 19)
				p.gltrans = V_AUTOFADEOUT
			elseif (p.gltimer > 69)
				p.gltrans = V_10TRANS
			elseif (p.gltimer > 63)
				p.gltrans = V_20TRANS
			elseif (p.gltimer > 56)
				p.gltrans = V_30TRANS
			elseif (p.gltimer > 50)
				p.gltrans = V_40TRANS
			elseif (p.gltimer > 42)
				p.gltrans = V_50TRANS
			elseif (p.gltimer > 35)
				p.gltrans = V_40TRANS
			elseif (p.gltimer > 29)
				p.gltrans = V_30TRANS
			elseif (p.gltimer > 23)
				p.gltrans = V_20TRANS
			elseif (p.gltimer >= 17)
				p.gltrans = V_10TRANS
			end

			p.gltimer = $-1
		else
			p.gltimer = 79
		end
		if p.lavaflash	
		p.lavaflash = $-1
			if not p.powers[pw_flashing] and p.lavaflash and not p.powers[pw_nocontrol] and not (p.pflags & PF_FORCESTRAFE)
				if (p.lavaflash % 2 == 0)
				s.flags2 = $|MF2_DONTDRAW
				elseif (s.flags2 & MF2_DONTDRAW)
				s.flags2 = $ & ~MF2_DONTDRAW
				end
			end
		end
		--Want Samus to stop her HUD functions for a while? Use this.
		if p.heysamusstopyourhudforthismanytics
			p.heysamusstopyourhudforthismanytics = $-1
		end
		
	//	s timers. Work regardless of whether you're Samus.
			
		--[SAMUS DIALOGUE EXAMPLE PART 2] UNCOMMENT THIS AND THE FIRST PART TO ACTIVATE. 
		/*	if s.samusdialogue and s.samusdialogue.customnextboxready
			s.samusdialogue.active = false
				if s.samusdialogue.customnextbox == 3
				SamusDialogue(s, "I will run for president! Residents ", "of space, let us build SamusCare! ", "SAMPRE", 2, 0, nil)
				s.samusdialogue.customnextbox = 4
				elseif s.samusdialogue.customnextbox == 4
				SamusDialogue(s, "It's free healthcare for all! ...unless", "you're a teleporting guard dick. ", "SAMDEF", 2, 0, nil)
				s.samusdialogue.customnextbox = 5
				elseif s.samusdialogue.customnextbox == 5
				s.samusdialogue.noscale = false
				SamusDialogue(s, "Samus...just use the plasma beam. ", nil, "SAMDEF", 1, 0, nil)
				s.samusdialogue.customnextbox = 6
				elseif s.samusdialogue.customnextbox == 6
				s.samusdialogue.noscale = false
					if p.sams_itemcompletion != nil and (p.sams_itemcompletion <= 3) and not p.sam_plasmabeam
					SamusDialogue(s, "Shut up. ", "I'm doing a 3% run. ", "SAMPRE", 2, 0, nil)
					else
					SamusDialogue(s, "Shut up!!", "I'm bad at this, okay?", "SAMSAD", 2, 0, nil)
					end
					s.samusdialogue.customnextbox = 0 //Setting this 0 ends the chain.
				end
			end */
			if p.sam_lockmorphball --Lock the morph ball until it's obtained? (For custom levels)
			p.sams_morphcd = 3
				if p.sammorphed
					MorphballReturn(s, true)
				end
			end
			if s.sam_refusewalljump
				s.sam_refusewalljump = $-1
			end
			if s.sam_dustcd 
				s.sam_dustcd = $-1
			end
			if p.samusmusicadd2
				p.samusmusicadd2 = $-1
				if not p.samusmusicadd2
					p.samusmusicadd2 = nil
				end
			elseif not p.samusmusicadded
				if (SAM_COM_BufInsertText(p, "addfile samusmusic.pk3; tunes -none"))
					p.samusmusicadded = true
						if p.sam_saveselect
						SAM_COM_BufInsertText(p, "tunes SAMMU1")
					--	S_ChangeMusic("SAMMU1", true, p, 0, 0, 699, 699)
						else
						P_RestoreMusic(p)
						end
					p.sam_autoaddmusic = true						
				end
			end
			if s.spawnsamuscolors --Force colors for a while.
				s.spawnsamuscolors = $-1
				SetSamusColor(s, p)
				S_StopSoundByID(s, sfx_cgot) --Don't play this for a bit.
				if not s.spawnsamuscolors
				SamusTutorial(s, "beams")
				SamusTutorial(s, "monitorhunt")
				SamusTutorial(s, "lockon")					
					if (p.pflags & PF_GODMODE)
					p.pflags = $ & ~PF_INVIS & ~PF_GODMODE & ~PF_FORCESTRAFE
					end
					if (gametype != GT_COOP)
					SamusTutorial(s, "scanvisor")
					SamusTutorial(s, "chargebeam")
						if G_GametypeHasSpectators() and not p.sam_hyperbeam
							for z in players.iterate
								if z.mo and z.sam_hyperbeam
								DoAreaScan(s, p)
								end
							end
						end			
					end
				end
			end		
			if s.sam_difficultymsg
				s.sam_difficultymsg = $-1
				if (s.sam_difficultymsg < 31)
				and (s.sam_difficultymsg % 3 == 0)
					if s.sam_difficultytrns != V_90TRANS
					s.sam_difficultytrns = $+FRACUNIT
					end
				elseif (s.sam_difficultymsg % 3 == 0)
					if s.sam_difficultytrns != V_10TRANS
					s.sam_difficultytrns = $-FRACUNIT
					end
				end
			end
			--More s timers
			if s.sam_forcemorphslope
				s.sam_forcemorphslope = $-1
			end
			if s.sam_afterboost
				--Gradual cancel
				if p.samspbschargefull != true
					if not p.sam_walljumping
					and not s.sam_screwattackfling
					and not p.sam_screwattacking
					s.sam_afterboost = $-1
					end
				end
				--Instant cancel.
				if p.powers[pw_carry]
				or P_PlayerInPain(p)
				or p.sammorphed
				or not YouAreSamus(s)
				s.sam_afterboost = 0
				end
			end
			if s.sam_spbshack
				s.sam_spbshack = $-1
			end
			if s.sam_stunimmune
				s.sam_stunimmune = $-1
			end
			if s.sam_scansymbol
				s.sam_scansymbol = $-1
			end
			if s.sam_spbsstart
				if (s.sam_spbsstart % 8 == 0) and (p.speed > 30*FRACUNIT)
					local ghs = P_SpawnMobjFromMobj(s, 0,0,-12*s.scale, MT_THOK)
					ghs.sprite = SPR_MXP4
					ghs.frame = V|FF_FULLBRIGHT|TR_TRANS60
					ghs.blendmode = AST_ADD
					ghs.scale = s.scale/5
					ghs.momz = (-7*FRACUNIT)*P_MobjFlip(ghs)
					ghs.tics = 30
					ghs.fuse = 18
					ghs.scalespeed = $*2
					ghs.destscale = $*99	
				end
				s.sam_spbsstart = $-1
			end
			if s.sam_reflectface
				s.sam_reflectface = $-1
			end
			if s.sam_forcemorphslopeattach
				s.sam_forcemorphslopeattach = $-1
			end
			if s.sam_supertilt
				s.sam_supertilt = $-1
			end
			if s.sam_stopmult
				s.sam_stopmult = $-1
			end
			if s.sam_sidejump --Walljump tracker. > 0 is right, < 0 is left.
				if P_IsObjectOnGround(s)
				or p.sam_screwattacking
				or P_PlayerInPain(p)
				or p.powers[pw_carry]
				or p.sammorphed
					s.sam_sidejump = 0
				elseif (p.pflags & PF_JUMPED) and (p.sam_walljumpsound and p.sam_walljumpsound > 3)
					P_TryMove(s, s.x+(s.momx/2), s.y+(s.momy/2), true)
					p.drawangle = R_PointToAngle2(s.x, s.y, s.x+s.momx, s.y+s.momy)
				end
			end			
			if s.sam_flashfade
				if not s.sam_flashfadecolor
				s.sam_flashfadecolor = SKINCOLOR_SUPERSILVER1
				end
				s.sam_flashfade = $-1
			end
			if p.sam_dosave
				if not G_IsSpecialStage(gamemap)
				and not (gamemap == 812)
				and not (p.powers[pw_carry] == CR_NIGHTS)
				ProfileSamusSave(s, p, p.sam_saveselected, true, false)
				end
			p.sam_dosave = nil
			SetSamusColor(s, p)
			end
/*
if not s.sam_waveoverload and (p.cmd.buttons & BT_CUSTOM3)
InflictWaveOverLoad(s, 55, nil)
end */
			if s.sam_waveoverload
				if s.waveoverfire
				s.waveoverfire = nil
				end
				s.sam_waveoverload = $-1
				
				p.sams_morphcd = 5
				p.sams_powerbombready = 0
				p.sams_supermissileready = 0

				local ghs = P_SpawnGhostMobj(s)
				ghs.tics = 11
				ghs.blendmode = AST_ADD
				ghs.scalespeed = $/10*P_RandomRange(5,8)
				ghs.destscale = $*50
				ghs.colorized = true
				ghs.momx = P_RandomRange(-3,3)*FRACUNIT ghs.momy = P_RandomRange(-3,3)*FRACUNIT
				ghs.momz = P_RandomRange(0,2)*FRACUNIT*P_MobjFlip(s)
				if leveltime % 4 == 0
					A_SAMPARTICLE(s, MT_MPR1, 4, 4, -2, 6, SKINCOLOR_GOLD, P_RandomRange(9355, 30355))
					A_SAMPARTICLE(s, MT_MPR1, 5, 3, -3, 6, SKINCOLOR_MAGENTA, P_RandomRange(35355, 65354))					
					ghs.color = P_RandomRange(SKINCOLOR_SUPERPURPLE1, SKINCOLOR_SUPERPURPLE5)
				else
					ghs.color = SKINCOLOR_GRAVITYSUIT
				end
			
				if (leveltime % 30 == 0)
				S_StartSound(s, sfx_wvbc)
					ghs = P_SpawnGhostMobj(s)
					ghs.tics = 20
					ghs.scalespeed = $*4
					ghs.destscale = $*50
					ghs.color = SKINCOLOR_SUPERGOLD5
					ghs.colorized = true
					ghs = P_SpawnMobj(s.x, s.y, s.z, MT_SAMUSZAP)
					if ghs and ghs.valid
						ghs.scale = s.scale*2
				 		ghs.color = SKINCOLOR_SUPERGOLD4
				 		ghs.morning = 4
					end
				end
		
		
				if p.sams_shotcd > 10
					p.sams_shotcd = 10
					p.sams_mshotcd = 10
				elseif not p.sams_shotcd  --Spam attacks
					if (s.sam_waveoverload % 5 == 0)
						local rng = P_RandomRange(1,8)				
						if rng == 6 and p.sammissiles and (p.sammissiles >= 50)	and not p.sammorphed and (s.sam_waveoverload < 22) --Sudden beamcombo!
							P_FlashPal(p, PAL_NUKE, 3)
							p.sams_chargetime = 42
							p.sams_shotcd = 0
							p.cmd.buttons = $|BT_FIRENORMAL|BT_ATTACK & ~BT_CUSTOM2
							p.sams_firecd = 1
							s.sam_waveoverload = 0
						else --Swap beams!
							p.sams_shotcd = 0
							p.cmd.buttons = $|BT_WEAPONPREV|BT_WEAPONNEXT
							p.sam_beamswitch = 0
							A_SAMBEAMSWITCH(p)
							p.sam_beamswitch = 0
						end
					else
						if p.sammorphed
							s.waveoverfire = nil
							s.waveoverbomb = true
							p.sams_shotcd = 6
							p.sams_mshotcd = 6
							p.sams_morphbombcd = $+31
							p.sams_powerbombready = 0
						elseif p.sammissiles and not P_RandomRange(0,2)
							p.sammissiles = $-1*p.samadrain
							SamusBeamFire(s, MT_SAMMISSILE)
							S_StartSound(s, sfx_mssh2)
							S_StartSound(s, sfx_mssh2)
							p.sams_shotcd = 2
							p.sams_mshotcd = 7
							if p.sammissiles < 0
							p.sammissiles = 0
							end
						else
							s.waveoverfire = true
							p.sams_shotcd = 0
							p.sams_mshotcd = 2							
						end
						p.sams_firecdjlg = 1
						p.sams_firecd = 1
						p.sams_chargetime = 0
					end
				end
				if not s.sam_waveoverload
					s.waveoverfire = false
					s.waveoverbomb = false
					s.samusmission = nil
				end
			end
			
			if s.samspawnprotect
				s.samspawnprotect = $-1
			end
			if s.spiderbanim
			s.spiderbanim = $-1
			end
			if s.sam_automobed
			s.sam_automobed = $-1
			end
			if s.sam_unstuck
			s.sam_unstuck = $-1
			end
			if s.sam_emstress --If this goes over 200, you drop your emeralds.
			s.sam_emstress = $-1
			end
			--Where are or will you be getting hit from?
			if s.sam_combowarn
			s.sam_combowarn = $-1		
				if s.sam_combowarntarget and s.sam_combowarntarget.valid
					s.sam_combowarnang = R_PointToAngle2(s.x,s.y, s.sam_combowarntarget.x, s.sam_combowarntarget.y)-(p.cmd.angleturn << 16)
				else
					s.sam_combowarn = 0
					s.sam_combowarntarget = nil
					s.sam_combowarnang = nil
				end
				if not s.sam_combowarn
				s.sam_combowarntarget = nil
				s.sam_combowarnang = nil
				end
			end
			
			if s.sam_hitwarn
			s.sam_hitwarn = $-1
				if not s.sam_hitwarn
					s.sam_hitwarnang = nil
					s.sam_hitwarntarget = nil					
				elseif s.sam_hitwarntarget and s.sam_hitwarntarget.valid --Only calculate once here.
					s.sam_hitwarnang = R_PointToAngle2(s.x,s.y, s.sam_hitwarntarget.x, s.sam_hitwarntarget.y)-(p.cmd.angleturn << 16)
					s.sam_hitwarntarget = nil
				end
			end
			
			if p.sam_forceanalogoff --These...STUPID....
			p.sam_forceanalogoff = $-1
				if not p.sam_forceanalogoff
				p.pflags = $|PF_ANALOGMODE
				end
			end
			
			if p.sam_forcedirectcharoff
			p.sam_forcedirectcharoff = $-1
				if not p.sam_forcedirectcharoff
			//	p.pflags = $|PF_ANALOGMODE & ~PF_DIRECTIONCHAR
				SAM_COM_BufInsertText(p, "configanalog 1; directionchar 0")
				end
			end
			
			if s.sam_scanpowerup
				if not p.sam_hyperbeampanic
				and not (s.wavebuffering or s.crystalflashtime)
				s.sam_scanpowerup = $-1
				end
				if not s.sam_scanpowerup
				s.samradaranim = 9
				s.samradaranimd = false
				p.scannedmons = nil
				p.scannedplays = nil
				S_StopSoundByID(s, sfx_sscan1)
				S_StartSound(s, sfx_samtm7)
				s.sam_scanpowerup = 0
				end
			end
			if s.metroidsnack
			s.metroidsnack = $-1
				if not s.metroidsnack
				p.viewrollangle = 0 --reset this
				end
			end
			if s.samradaranim
				if s.samradaranimd
					if (s.samradaranim < 9)
					s.samradaranim = $+1
					else
					s.samradaranim = 0
					end
				elseif not s.samradaranimd
				s.samradaranim = $-1
				end
			end
			
			if s.samvisordrop
			s.samvisordrop = $-1
				if s.samvisordrop >= 17
				p.samvisordropdist = p.samvisordropdist2/2
				else
				p.samvisordropdist = (p.samvisordropdist2*s.samvisordrop)/12
				end
			elseif p.samvisordropdist
			p.samvisordropdist = 0
			end	
			
			if s.pbshotsmoke
			s.pbshotsmoke = $-1
				if (s.pbshotsmoke > 240)
				s.pbshotsmoker = 250
				s.pbshotsmoke = 0
				end
				if s.pbshotsmoker2
				s.pbshotsmoker2 = $-1
				end
			end
			
			if s.sammorphedghs
			s.sammorphedghs = $-1
				s.ghs = P_SpawnMobj(s.x, s.y, s.z+(((s.height/5)-(s.sammorphedghs*10*FRACUNIT))*P_MobjFlip(s)), MT_THOK)
				s.ghs.skin = s.skin
				s.ghs.color = s.color
				s.ghs.flags = $|MF_NOCLIPHEIGHT
				s.ghs.eflags = s.eflags
				s.ghs.sprite = SPR_PLAY
				s.ghs.sprite2 = SPR2_ROLL
				s.ghs.frame = (D-s.sammorphedghs)
				if s.ghs.frame <= A
				s.ghs.frame = A
				end
				s.ghs.frame = $|(TR_TRANS30+(s.sammorphedghs*FRACUNIT))
				s.ghs.tics = 7-s.sammorphedghs
				s.ghs.momx = s.momx*10/14 s.ghs.momy = s.momy*10/14 s.ghs.momz = s.momz*10/14
				s.ghs.angle = p.drawangle
				s.ghs.scale = ((s.scale*s.sammorphedghs)*5)/4
			end
		--Crystal Flash power
		
			if s.crystalflashtime
			s.crystalflashtime = $-1
				if YouAreSamus(s)
				CrystalRebirth(s, p)
				end
				if not s.crystalflashtime or not YouAreSamus(s)
					s.crystalflashtime = 0
					s.crystalflashanim = 0
					s.rollangle = 0
					A_samquake(s, 70*FRACUNIT, 14, 1)
					P_SamFP(p, PAL_RECYCLE, 6)
					S_StartSound(s, sfx_rocks3)
					S_StartSound(s, sfx_wvcm2)
					S_StartSound(s, sfx_samtm7)
					
					if s.nocftunechange
						s.nocftunechange = false
					else
						for z in players.iterate
							if s.crystalflashtune and s.crystalflashtune == 2
							SAM_COM_BufInsertText(z, "tunes omega")
							elseif s.crystalflashtune
							SAM_COM_BufInsertText(z, "tunes darkpz")
							else
							SAM_COM_BufInsertText(z, "tunes -default")
							end
						end
					end
					
				end
			end
			if s.crystalflashanim
			s.crystalflashanim = $-1
				p.camerascale = FRACUNIT*3
				s.thesamcam = 1
			end	
			
		--Samus' dialogue thinker here.
			if s.samusdialogue and s.samusdialogue.active
			RunSamusDialogue(s, p)
			end
			if s.samusmission and s.samusmission.active
			RunSamusMission(s, p)
			end
			if p.samustutorial and p.samustutorial.active
			RunSamusTutorial(s,p)			
			end
			if p.sam_saveselect
			RunSamusSaveMenu(s, p)
			end			
			if s.bmreflwarning
			s.bmreflwarning = $-1
			end
			if s.pzpaintime
			s.pzpaintime = $-1
			end
			if s.powerbombjump
			s.powerbombjump = $-1
			end
			if s.samunmorphed
			s.samunmorphed = $-1
			end
			if s.wallnopejump
			s.wallnopejump = $-1
			end
			if s.sampickuptime
			s.sampickuptime = $-1
				if (s.sampickuptime < 40)
					if (s.sampickuptime % 4 == 0) and not (s.sampickuptrns != nil and s.sampickuptrns >= 9*FRACUNIT)
					s.sampickuptrns = s.sampickuptrns and $+FRACUNIT or FRACUNIT
					end
				//	s.sampickupoffset = $+14000
				elseif s.sampickupoffset
				s.sampickupoffset = $-FRACUNIT
				end			
			end
			if s.sampickuptime2
			s.sampickuptime2 = $-1
				if (s.sampickuptime2 < 40)
					if (s.sampickuptime2 % 4 == 0) and not (s.sampickuptrns2 != nil and s.sampickuptrns2 >= 9*FRACUNIT)
					s.sampickuptrns2 = s.sampickuptrns2 and $+FRACUNIT or FRACUNIT
					end
				//	s.sampickupoffset2 = $+14000
				elseif s.sampickupoffset2
				s.sampickupoffset2 = $-FRACUNIT
				end
				if not s.sampickuptime -- Migrate down to the bottom...
				s.sampickupname = s.sampickupname2
				s.sampickupscale = s.sampickupscale2
				s.sampickuptime = s.sampickuptime2
				s.sampickupoffset = s.sampickupoffset2
				s.sampickuptrns = s.sampickuptrns2
				s.sampickupcolor = s.sampickupcolor2
				s.sampickuptime2 = 0
				end
			end
			if s.sampickuptime3
			s.sampickuptime3 = $-1
				if (s.sampickuptime3 < 40)
					if (s.sampickuptime3 % 4 == 0) and not (s.sampickuptrns3 != nil and s.sampickuptrns3 >= 9*FRACUNIT)
					s.sampickuptrns3 = s.sampickuptrns3 and $+FRACUNIT or FRACUNIT
					end
				elseif s.sampickupoffset3
				s.sampickupoffset3 = $-FRACUNIT
				end
				if not s.sampickuptime2 -- Migrate down to the bottom...
				s.sampickupname2 = s.sampickupname3
				s.sampickupscale2 = s.sampickupscale3
				s.sampickuptime2 = s.sampickuptime3
				s.sampickupoffset2 = s.sampickupoffset3
				s.sampickuptrns2 = s.sampickuptrns3
				s.sampickupcolor2 = s.sampickupcolor3
				s.sampickuptime3 = 0
				end				
			end				
			if s.saniccdflinch
			s.saniccdflinch = $-1
			end
			if s.omegarushpanic
			s.omegarushpanic = $-1
			end
			if s.sam_wavestunned
			s.sam_wavestunned = $-1
				if s.sam_wavestunned % 20 == 0
				s.momx = $*10/15
				s.momy = $*10/15
				s.momz = $*10/13
				A_samquake(s, 50*FRACUNIT, 7, 1)
				S_StartSound(s, sfx_mtrds)
				end
			end
			if s.sam_systemreboot
			s.sam_systemreboot = $-1
				if s.sam_systemreboot == 3 and not s.myorammer 
				p.viewrollangle = 0
				end
			end
			if s.myorammer --Omega knockback mechanic
				if ((p.speed < 16*FRACUNIT) or P_IsObjectOnGround(s))
					if not s.myowait --Give it an extra tic!
					s.myowait = true
					else
					s.myoangle = 0
					s.rollangle = 0
					p.viewrollangle = 0
					p.powers[pw_flashing] = 0
					p.losstime = 50*FRACUNIT
					P_DamageMobj(s)
					A_samquake(s, 80*FRACUNIT, 10, 1)
					P_SamFP(p, PAL_NUKE, 6)
					S_StartSound(s, sfx_bkpoof) S_StartSound(s, sfx_dmpain) S_StartSound(s, sfx_rocks2) S_StartSound(s, sfx_pstop)
					s.myorammer = nil
					s.myowait = nil
					end
					
				else
					if s.myoangle == nil
					s.myoangle = 0
					elseif not p.sam_sensitive
					s.myoangle = $+ANG20
					end
					
					A_BossScream(s, 0, MT_EXPLODE)
					s.rollangle = s.myoangle
					p.viewrollangle = s.myoangle
				end
			end
			
			if s.sams_shineframe
				if leveltime % 2 == 0		
				s.tempsamdye = SKINCOLOR_RED
				else
				s.tempsamdye = SKINCOLOR_ORANGE
				end
				
				s.tempsamdyetr = TR_TRANS10
				
				if not s.health or P_IsObjectOnGround(s)
				s.sams_shineframe = 0
				else
				s.sams_shineframe = $-1
					if not p.sammorphed
					s.sprite2 = SPR2_FLT
					s.frame = A
					end
				end
			end	
			
			if s.samspinpadconfirmed
				s.samspinpadconfirmed = $-1
				p.skidtime = 0
			end
			if s.samspinpad
			s.samspinpad = $-1
				if (s.samspinpad > 47) and ((p.powers[pw_flashing] > 7) and (p.powers[pw_flashing] < 10))

					s.samspinpad = 47
					if not p.powers[pw_nocontrol]
						A_CustomPower(s, 21, 32774)
					end
					p.bsblboost = 0
					p.sams_beamcombocharge = 0
					p.pflags = $ & ~PF_JUMPED
					s.samspinpadconfirmed = 2
					if not p.sammorphed
						SamMorphSwitch(p, s)
						S_StartSound(s, sfx_smorph)
						S_StartSound(s, sfx_zoom)
					end
					P_SpawnPlayerMissile(s, MT_SAMMONBREAKER)
					s.momx = $*20/19
					s.momy = $*20/19
					CancelScrewAttack(s,p)
				end
				if (s.samspinpad < 48) and not s.samspinpadconfirmed
					s.samspinpad = 0
				end
				if (s.samspinpad < 48)
				s.delayedghost = 1
				s.samspinpadconfirmed = 2
				p.pflags = $|PF_SPINNING
				p.sams_notyet = 3
				p.charflags = $|SF_RUNONWATER
					if (p.pflags & PF_JUMPED) 
					s.samspinpadconfirmed = 0
					s.samspinpad = 0
					p.charflags = $ & ~SF_RUNONWATER
					end
					if not p.sammorphed
					s.samspinpadconfirmed = 0
					s.samspinpad = 0
					p.charflags = $ & ~SF_RUNONWATER
					p.pflags = $ & ~PF_SPINNING
					elseif (s.eflags & MFE_TOUCHWATER) and not (s.eflags & MFE_UNDERWATER)
					P_SetObjectMomZ(s, 8*FRACUNIT, false)
					s.momx = $*5/6
					s.momy = $*5/6
					S_StartSound(s, sfx_wslap)
					end
					if s.samspinpad == 1 or p.bsblboost or (p.speed < 2*FRACUNIT)
					s.samspinpadconfirmed = 0
					s.samspinpad = 0
					p.pflags = $ & ~PF_SPINNING
					end
				end
			end			
	//	end
		if p.sams_morphbombcd
		p.sams_morphbombcd = $-1
		end
		if p.sams_shotcd
		p.sams_shotcd = $-1
		end
		if p.sams_hbdenid
		p.sams_hbdenid = $-1
			if p.sams_hbdenid == 30
			p.sams_hbdenid = 0
			end
		end
		if p.sams_mshotcd
		p.sams_mshotcd = $-1
		end
		if p.sams_chargetimecd
		p.sams_chargetimecd = $-1
		end
		if p.sams_firecdjlg
		p.sams_firecdjlg = $-1
		end
		if p.sam_hithud
		p.sam_hithud = $-1
		end
		if p.sam_crithud
		p.sam_crithud = $-1
		end
		if p.sam_resisthud
		p.sam_resisthud = $-1
		end
		if p.sam_walljumpsound
		p.sam_walljumpsound = $-1
		end
		if p.sam_shipready
		p.sam_shipready = $-1
		end
		if p.sams_c1cdjlg
		p.sams_c1cdjlg = $-1
		end
		if p.sam_c1ready
		p.sam_c1ready = $-1
			if P_IsObjectOnGround(s)
				p.sam_c1ready = 0
			end
		end
		if p.sam_j1ready
		p.sam_j1ready = $-1
			if P_IsObjectOnGround(s)
				p.sam_j1ready = 0
			end
		end
		if p.sams_c2cdjlg
			p.sams_c2cdjlg = $-1
		end
		if p.sams_normfcdjlg
			p.sams_normfcdjlg = $-1
		end
		if (p.samdoubleammo and G_GametypeHasSpectators() ) or p.fusionmode
			p.samadrain = 2
		else
			p.samadrain = 1
		end
		if p.bsblboost
			p.bsblboost = $-1
		end
		if p.bsblboostattack
		p.bsblboostattack = $-1
			if (p.bsblboostattack > 6)	
				if not (s.bsblflame and s.bsblflame.valid)
					s.bsblflame = P_SpawnMobj(s.x, s.y,s.z-s.height/11, MT_OVERLAY)
				end
				s.bsblflame.target = s
				s.bsblflame.state = S_THOKGLOW
				s.bsblflame.tics = 3
				s.bsblflame.blendmode = AST_ADD
				if p.samvisorcolor and (p.sam_gravitysuit and p.sam_gravitysuit == 2)
					s.bsblflame.color = p.samvisorcolor
				else
					s.bsblflame.color = SKINCOLOR_EMERALD
				end	
				if s.bsblflame and s.bsblflame.valid
					s.tempsamdye = s.bsblflame.color
					if (p.bsblboostattack > 16)
					s.bsblflame.frame = A|FF_FULLBRIGHT|TR_TRANS10
					s.tempsamdyetr = TR_TRANS10
					elseif (p.bsblboostattack > 14)
					s.bsblflame.frame = A|FF_FULLBRIGHT|TR_TRANS40
					s.tempsamdyetr = TR_TRANS40
					elseif (p.bsblboostattack > 12)
					s.bsblflame.frame = A|FF_FULLBRIGHT|TR_TRANS70
					s.tempsamdyetr = TR_TRANS70
					elseif (p.bsblboostattack > 6)
					s.bsblflame.frame = A|FF_FULLBRIGHT|TR_TRANS90
					s.tempsamdyetr = TR_TRANS90
					end
				end 
			end
		end
		if p.sam_spacejumpedboost
		p.sam_spacejumpedboost = $-1
		end
		if p.sam_shinespark
		p.sam_shinespark = $-1
		end
		if p.samhands
		p.samhands = $-1
		end
		if p.sams_screwattackcd
		p.sams_screwattackcd = $-1
		end
		if p.hbimmune and not (p.powers[pw_flashing] and (p.hbimmune < 3))
		p.hbimmune = $-1
		end
		if p.samnoflag
		p.samnoflag = $-1
		end
		if s.thesamcam and not s.crystalflashanim
			p.camerascale = FRACUNIT
			s.thesamcam = 0
		end
		if not p.samusnumdecided
			if players[0] and players[0] == p
			p.samusnum = 0
			p.samusnumdecided = true
			else
				if p.samusnum == nil
				p.samusnum = 1
				end
				if p.samusnum == 1	
					if players[tonumber(p.samusnum)] == p
					p.samusnumdecided = true
					else
					p.samusnum = $*2
					p.samusnummult = 1
					end
				elseif (p.samusnum > 1) and p.samusnummult
					if players[tonumber(p.samusnummult)] == p
					p.samusnumdecided = true
					elseif not (p.samusnummult and (p.samusnummult > 30))
					p.samusnum = $*2
					p.samusnummult = $+1
					end
				end
			end
		end
		if p.sam_powerbeammeltdown
		p.sam_powerbeammeltdown = $-1
			if p.sam_powerbeammeltdown == 1
			S_StartSound(s, sfx_bcrs)

			end
			if p.sam_fullypoweredsuit or s.crystalflashtime
			p.sams_mshotcd = 10
			p.sam_powerbeammeltdown = 0
			end
		elseif s.pbshotsmoker
		s.pbshotsmoker = $-1
			if p.sam_powerbeamequip and YouAreSamus(s)
			and not p.sammorphed
				if not s.pbshotsmoker2
					if (s.eflags & MFE_VERTICALFLIP)
					s.ghs = P_SpawnMobj(s.x+FixedMul(s.radius*5/2, cos(s.angle-(ANG2*2))), s.y+FixedMul(s.radius*5/2, sin(s.angle-(ANG2*2))), s.z+(s.height*4/11), MT_SAMCHARGEGFX)
					else
					s.ghs = P_SpawnMobj(s.x+FixedMul(s.radius*5/2, cos(s.angle-(ANG2*2))), s.y+FixedMul(s.radius*5/2, sin(s.angle-(ANG2*2))), s.z+(s.height*2/3), MT_SAMCHARGEGFX)
					end
					s.ghs.z = $+(FixedMul(FixedMul(50*FRACUNIT, s.scale), sin(p.aiming)))
					s.ghs.scale = s.scale/(P_RandomRange(12,24))
					s.ghs.color = SKINCOLOR_SILVER
					s.ghs.flags2 = $|MF2_SHADOW
					s.ghs.momx = s.momx/2
					s.ghs.momy = s.momy/2
					s.ghs.eflags = s.eflags
					s.ghs.target = s
				end
			else
			s.pbshotsmoker = 0
			s.pbshotsmoke = 0
			s.pbshotsmoker2 = 0
			end
		end
		if p.sam_spazerbeammeltdown
		p.sam_spazerbeammeltdown = $-1
			if p.sam_spazerbeammeltdown== 1
			S_StartSound(s, sfx_bcrs)

			end
			if p.sam_fullypoweredsuit or s.crystalflashtime
			p.sams_mshotcd = 10
			p.sam_spazerbeammeltdown = 0
			end
		end
		if p.sam_icebeammeltdown
		p.sam_icebeammeltdown = $-1
			if p.sam_icebeammeltdown== 1
			S_StartSound(s, sfx_bcrs)
			end
			if p.sam_fullypoweredsuit or s.crystalflashtime
			p.sams_mshotcd = 10
			p.sam_icebeammeltdown = 0
			end
		end
		if p.sam_wavebeammeltdown
		p.sam_wavebeammeltdown = $-1
			if p.sam_wavebeammeltdown== 1
			S_StartSound(s, sfx_bcrs)

			end
			if p.sam_fullypoweredsuit or s.crystalflashtime
			p.sams_mshotcd = 10
			p.sam_wavebeammeltdown = 0
			end
		end
		if p.sam_plasmabeammeltdown
		p.sam_plasmabeammeltdown = $-1
			if p.sam_plasmabeammeltdown== 1
			S_StartSound(s, sfx_bcrs)

			end
			if p.sam_fullypoweredsuit or s.crystalflashtime
			p.sams_mshotcd = 10
			p.sam_plasmabeammeltdown = 0
			end
		end
		if s.spiderballstillgood
		s.spiderballstillgood = $-1
		end
		if s.spiderballdoublecheck
		s.spiderballdoublecheck = $-1
		end
		if s.sam_deathball
			if not (s.wavebuffering or s.crystalflashtime)
			or not p.sammorphed
			s.sam_deathball = nil
			end
		end
		if p.samnoflagmessage
		p.samnoflagmessage = $-1
			if p.samnoflagmessage == 104
			S_StartSound(s, sfx_s3k80)
			CONS_Printf(p, "\x85"+"Cannot pick up flags for 10 seconds after exiting ship.")
			p.samnoflagmessage = 103
			end
		end
		if p.dragdownball
		p.dragdownball = $-1
			if p.sammorphed and (p.pflags & PF_JUMPED)
				if s.bombjump or P_IsObjectOnGround(s)
				p.dragdownball = 0
				end
				if (p.dragdownball > 25)
					if (p.pflags & PF_STARTJUMP)
					s.delayedghost = 1
					else
					p.dragdownball = 16
					end				
				elseif (p.dragdownball < 24) and (p.dragdownball > 20)
				P_SetObjectMomZ(s, -65355, true)
				elseif (p.dragdownball < 10) and (p.dragdownball > 7)
				P_SetObjectMomZ(s, -9500, true)
				elseif (p.dragdownball < 10)
				P_SetObjectMomZ(s, -15355, true)
				end
			end
		end
		if p.sam_walljumping
		p.sam_walljumping = $-1
			if p.sam_walljumping > 7
				if (p.speed > 8*s.scale) and p.sam_walljumpsound
				s.state = S_PLAY_FLOAT
				s.sprite2 = SPR2_FLT_
				s.frame = A
					if not (p.pflags & PF_ANALOGMODE)
					p.drawangle = R_PointToAngle2(s.x, s.y, s.x+s.momx, s.y+s.momy)
					end
				end
			elseif p.sam_walljumping == 7 and not p.sam_shinesparkfly
				if (s.state == S_PLAY_FLOAT or s.state == S_PLAY_FLOAT_RUN)
				s.state = S_PLAY_ROLL
				end
			elseif (p.sam_walljumping == 1) and (p.panim == PA_ROLL)
				s.state = S_PLAY_FALL
				p.sam_walljumping = 0
			end
		end
		if p.sam_shinesparkfly
			if (p.sam_shinesparkfly < 300)
				p.sam_shinesparkfly = $+1
			else
				p.sam_shinesparkfly = 0
			end
		end
		if p.sam_beamswitch
			if (p.sam_beamswitch == 11)
			S_StartSound(s, sfx_besw)
			end
		p.sam_beamswitch = $-1
			if (p.sam_beamswitch > 9)
			p.sam_beamcolor = SKINCOLOR_GREY
			elseif (p.sam_beamswitch > 3)
			p.sam_beamcolor = SKINCOLOR_BLACK
			elseif (p.sam_beamswitch >= 1)
			p.sam_beamcolor = SKINCOLOR_GREY
			end
		end
		if p.prescrewcolor and not p.sam_screwattacking
			s.color = p.prescrewcolor
			p.prescrewcolor = nil
		end
		if p.sams_supermissileready
		and not (((p.cmd.buttons & BT_FIRENORMAL) and p.swapfire) or ((p.cmd.buttons & BT_ATTACK) and not p.swapfire))
			p.sams_supermissileready = $-1
		end
		if p.sams_powerbombready
		and not ((((p.cmd.buttons & BT_FIRENORMAL) and not p.swapfire) or ((p.cmd.buttons & BT_ATTACK) and p.swapfire)) or ((p.cmd.buttons & BT_CUSTOM2) and not p.c1c2disable))
			p.sams_powerbombready = $-1
		end
		if (p.samspbscharge <= 130)
			p.samspbschargefull = false
		end
		if p.sam_hyperbeampanic
			if not p.sam_hyperbeam
				if (leveltime % 3 == 0) --Flash colors
					if p.sam_hyperbeampanic == 84
					p.sam_hyperbeampanic = SKINCOLOR_SUPERRED1
					elseif (p.sam_hyperbeampanic == SKINCOLOR_SUPERRED1)
					p.sam_hyperbeampanic = SKINCOLOR_SUPERSKY5
					elseif (p.sam_hyperbeampanic == SKINCOLOR_SUPERSKY5)
					p.sam_hyperbeampanic = 84
					end
					if not s.sam_scanpowerup
					DoAreaScan(s, p, hyper)
					s.sam_scanpowerup = 3
					s.samradaranim = 1
					s.samradaranimd = true
					S_StartSound(s, sfx_sscan2)	
					end
				end
				
				
			elseif p.sam_hyperbeam
				if (leveltime % 35 == 0) --Scan every second.
					for z in players.iterate
						if z.mo and z.mo.health
						and not z.sam_hyperbeampanic
							z.sam_hyperbeampanic = 84
								if not z.sam_hyperbeam
									if z.mo
									DoAreaScan(z.mo, z)
									end
							
								z.samhudmessagetime = 230
								z.samhudmessage1 = "\x80"+p.name+"\x81 captured the Hyper Beam! Kill them"
								z.samhudmessage2 = "before they gather more ammo and destroy everyone!"
								else
							
									z.mo.sam_scanpowerup = 0
									z.scannedmons = nil
									z.scannedplays = nil
							
								z.samhudmessagetime = 230
								z.samhudmessage1 = "\x81"+"You are hunted!\x82 Use the Hyper Beam to wreak"
								z.samhudmessage2 = "\x82"+"as much havoc as possible before dying!"
								end
								P_SamFP(z, PAL_NUKE, 12)
								if not z.nosammusic
								SAM_COM_BufInsertText(z, "tunes -none; tunes SAMUSH")
								end
						end
					end
				end
			
			 	if s.sam_lasthyperbeamammo and (s.sam_lasthyperbeamammo < p.sam_hyperbeamammo)
				local newammo = (p.sam_hyperbeamammo-s.sam_lasthyperbeamammo)
					if newammo > 25
						S_StartSound(nil, sfx_litng4)
						S_StartSound(nil, sfx_litng3)
						S_StartSound(nil, sfx_litng2)
						S_StartSound(nil, sfx_litng1)
						S_StartSound(nil, sfx_s3k6f)
					else
						S_StartSound(nil, (P_RandomRange(sfx_rocks1, sfx_rocks4)))
					end
					for z in players.iterate
						if z.mo
							if newammo > 24
							P_SamFP(z, PAL_NUKE, 3)
							A_samquake(z.mo, 55*FRACUNIT, 35, 1)
							else
							A_samquake(z.mo, (newammo*3)*FRACUNIT, newammo*2, 1)
							end
						end
					end
				end
				s.sam_lasthyperbeamammo = p.sam_hyperbeamammo
			else
				s.sam_scanpowerup = 3
			end
		end
		if p.sam_hyperbeam 
			if p.sam_hyperbeampanic
			local ghs
				if (s.eflags & MFE_VERTICALFLIP)
					ghs = P_SpawnMobj(s.x, s.y, s.z-s.height*10/12, MT_THOK)
				else
					ghs = P_SpawnMobj(s.x, s.y, s.z+s.height*10/12, MT_THOK)
				end
				  ghs.scale = s.scale
				  ghs.destscale = s.scale/(P_RandomRange(1,5))
				  ghs.momx = s.momx
				  ghs.momy = s.momy
				  ghs.momz = s.momz+((P_RandomRange(30, 100))*FRACUNIT)*P_MobjFlip(s)
				  ghs.eflags = s.eflags
				  ghs.frame = $|FF_FULLBRIGHT
				  ghs.tics = 8
				  ghs.color = P_RandomRange(26, 30)
			end
			if not p.sam_hyperbeamtime
			p.sam_hyperbeamtime = 18
			p.hyperbeamco = SKINCOLOR_SKY
			else
			p.sam_hyperbeamtime = $-1
				if (p.sam_hyperbeamtime > 16)
					p.hyperbeamco = SKINCOLOR_SKY
				elseif (p.sam_hyperbeamtime > 14)
					p.hyperbeamco = SKINCOLOR_BLUE
				elseif (p.sam_hyperbeamtime > 12)
					p.hyperbeamco = SKINCOLOR_MAGENTA
				elseif (p.sam_hyperbeamtime > 10)
					p.hyperbeamco = SKINCOLOR_PINK
				elseif (p.sam_hyperbeamtime > 8)
					p.hyperbeamco = SKINCOLOR_CLOUDY
				elseif (p.sam_hyperbeamtime > 6)
					p.hyperbeamco = SKINCOLOR_GOLD
				elseif (p.sam_hyperbeamtime > 4)
					p.hyperbeamco = SKINCOLOR_SUPERGOLD4
				elseif (p.sam_hyperbeamtime > 2)
					p.hyperbeamco = SKINCOLOR_SUPERRED2
				else
					p.hyperbeamco = SKINCOLOR_EMERALD
				end			
			end
		end
		if (p.cmd.buttons & BT_JUMP)
			if (p.pflags & PF_JUMPED)
				p.sams_jumpcd = $+1
			else
				p.sams_jumpcd = $+2
			end
		elseif p.sams_jumpcd
			p.sams_jumpcd = 0
		end
		if p.hyperbrakmsg
		p.hyperbrakmsg = $-1
		end
		if (p.cmd.buttons & BT_CUSTOM1) and not p.c1c2disable
			p.sams_c1cd = $+1
		elseif p.sams_c1cd
			if s.sambsblchargelvl
			s.sambsblchargelvl = 0
			end
			p.sams_c1cdjlg = 2
			p.sams_c1cd = 0
		end
		if (p.cmd.buttons & BT_CUSTOM2) and not p.c1c2disable
			p.sams_c2cd = $+1
		elseif p.sams_c2cd
			p.sams_c2cdjlg = 2
			p.sams_c2cd = 0
		end	
		if (p.cmd.buttons & BT_USE)
			p.sams_usecd = $+1
		elseif p.sams_usecd
			p.sams_usecd = 0
		end
		if (((p.cmd.buttons & BT_FIRENORMAL) and not p.swapfire) or ((p.cmd.buttons & BT_ATTACK) and p.swapfire))
			p.sams_normfcd = $+1
		elseif p.sams_normfcd
			p.sams_normfcdjlg = 2
			p.sams_normfcd = 0
		end
		if (((p.cmd.buttons & BT_FIRENORMAL) and p.swapfire) or ((p.cmd.buttons & BT_ATTACK) and not p.swapfire))
			p.sams_firecd = $+1
		elseif p.sams_firecd
			if s.sambsblchargelvl
			s.sambsblchargelvl = 0
			end
			p.sams_firecdjlg = 2
			p.sams_firecd = 0
		end
		if p.sams_weappress and not (p.cmd.buttons & BT_WEAPONPREV) and not (p.cmd.buttons & BT_WEAPONNEXT)
		p.sams_weappress = false
		end
		if YouAreSamus(s)
			if not p.samusenergyhud
			p.samusenergyhud = true
			end
		elseif p.samusenergyhud and not YouAreSamus(s)
		p.samusenergyhud = false
			if not p.sam_specialstage
			p.rings = 1
			end
		end
		--The Wave Beam Combo! Includes Death Ball bonus similar to Metroid Prime 2: Echoes' multiplayer mode
		if s.wavebuffering
		or (s.crystalflashtime and p.sammorphed)
			if s.wavebuffering
		  		if (gametype != GT_COOP) and not (leveltime % 8 == 0)
		  			for z in players.iterate
						if z.mo and z.mo.sam_scanpowerup
						and not (z.sam_hyperbeampanic and not z.sam_hyperbeam)
						and not z.mo.wavebuffering
						
		  				z.mo.sam_scanpowerup = 0
						z.scannedmons = nil
						z.scannedplays = nil
						S_StartSound(z.mo, sfx_dmpain)
						S_StartSound(z.mo, sfx_cmol)
						z.mo.sam_wavestunned = 90
						z.mo.sam_scanning = 0
						z.mo.waittoscan = 0
						z.mo.samusjustscanned = true
						S_StopSoundByID(z.mo, sfx_sscan1)
						P_SamFP(z, PAL_NUKE, 4)
						z.mo.samusmission = nil
						SamusMission(z.mo, "WARNING!! SCAN SENSORS JAMMED!", nil, nil, 1, 0, false)
						end
					end
				end
				if not p.sammorphed
				p.sams_shotcd = 3
				p.sams_mshotcd = 3
				end
				if not s.wavebufferang
				s.wavebufferang = 0
				end
				if (s.wavebuffering % 2 == 0)
				S_StartSound(s, sfx_cmol)
				end
				
			s.wavebuffering = $+1
			s.wavebufferang = $ + 14*FRACUNIT
				
					if (s.wavebuffering == 2)
					p.waveguard1 = P_SpawnMobj(s.x + cos(FixedAngle(s.wavebufferang))*120, s.y + sin(FixedAngle(s.wavebufferang))*120, s.z + s.height/2, MT_WVCHARGESHOT)
					p.waveguard1.target = s
					p.waveguard1.scale = s.scale
					p.waveguard1.eflags = s.eflags
					p.waveguard1.flags2 = s.flags2
					p.waveguard1.bufferguard = true

					p.waveguard2 = P_SpawnMobj(s.x+cos(FixedAngle(s.wavebufferang))*-120+s.momx, s.y+sin(FixedAngle(s.wavebufferang))*-120+s.momy, s.z+s.height/2+s.momz, MT_WVCHARGESHOT)
					p.waveguard2.target = s
					p.waveguard2.scale = s.scale
					p.waveguard2.eflags = s.eflags
					p.waveguard2.flags2 = s.flags2
					p.waveguard2.bufferguard = true

					p.waveguard3 = P_SpawnMobj(s.x + cos(FixedAngle(s.wavebufferang))*120, s.y + sin(FixedAngle(s.wavebufferang))*120, s.z + s.height/2, MT_WVCHARGESHOT)
					p.waveguard3.target = s
					p.waveguard3.scale = s.scale
					p.waveguard3.eflags = s.eflags
					p.waveguard3.flags2 = s.flags2
					p.waveguard3.bufferguard = true

					p.waveguard4 = P_SpawnMobj(s.x+cos(FixedAngle(s.wavebufferang))*-120+s.momx, s.y+sin(FixedAngle(s.wavebufferang))*-120+s.momy, s.z+s.height/2+s.momz, MT_WVCHARGESHOT)
					p.waveguard4.target = s
					p.waveguard4.scale = s.scale
					p.waveguard4.eflags = s.eflags
					p.waveguard4.flags2 = s.flags2
					p.waveguard4.bufferguard = true
					end
			end


			if p.sammorphed
				if not s.sam_deathball
					s.dsp = P_SpawnMobj(s.x, s.y, s.z+s.height, MT_THOK)
					s.dsp.height = 40*FRACUNIT
					s.dsp.eflags = s.eflags
					s.dsp.target = s
					s.dsp.sprite = SPR_MPR9
					s.dsp.frame = 26|FF_FULLBRIGHT|TR_TRANS10
					s.dsp.scale = 160000
					s.dsp.deathbolt = true
					if G_GametypeHasSpectators()
					S_StartSound(nil, sfx_litng4)
					S_StartSound(nil, sfx_litng2)
					S_StartSound(nil, sfx_wvcm2)
					else
					S_StartSound(s, sfx_litng4)
					S_StartSound(s, sfx_litng2)
					S_StartSound(s, sfx_wvcm2)
					end
				end
				A_samquake(s, p.speed/15, 1, (p.speed*40)/FRACUNIT, true)
				P_SpawnGhostMobj(s)
				P_SpawnPlayerMissile(s, MT_SAMMONBREAKER)
				s.sam_deathball = true
				if (s.eflags & MFE_JUSTHITFLOOR)
					S_StartSound(s, sfx_wvbc)
					for d = 1,((p.speed/FRACUNIT)/5)
						local t = P_SpawnMobj(s.x, s.y, s.z+s.height/2, MT_MINECARTSPARK)
						t.scale = s.scale/15
						t.fuse = 18
						P_InstaThrust(t, P_RandomRange(1,359)*ANG1, P_RandomRange(12,24)*FRACUNIT)
						t.momz = P_RandomRange(2,7)*FRACUNIT
						if SAM_displayplayer(p)
						t.flags2 = $|MF2_DONTDRAW --Save performance
						end	
					end	
				end
				if (leveltime % 4 == 0)
				S_StartSound(s, sfx_cmol)
				local ghs
					if (s.eflags & MFE_VERTICALFLIP)
						ghs = P_SpawnMobj(s.x, s.y, s.z+s.height, MT_SAMUSZAP)
					else
						ghs = P_SpawnMobj(s.x, s.y, s.z-s.height/10, MT_SAMUSZAP)
					end
				  	ghs.scale = s.scale
					ghs.blendmode = AST_ADD
				  	ghs.momx = s.momx
				  	ghs.momy = s.momy
				 	ghs.momz = s.momz
				 	ghs.eflags = s.eflags
				  	ghs.color = SKINCOLOR_GREY
				  	ghs.morning = 4
				end
				if p.waveguard1 != nil  and p.waveguard1.valid p.waveguard1.flags = $|MF_NOCLIP p.waveguard1.flags2 = $|MF2_DONTDRAW end
				if p.waveguard2 != nil and p.waveguard2.valid p.waveguard2.flags = $|MF_NOCLIP p.waveguard2.flags2 = $|MF2_DONTDRAW end
				if p.waveguard3 != nil and p.waveguard3.valid p.waveguard3.flags = $|MF_NOCLIP p.waveguard3.flags2 = $|MF2_DONTDRAW end
				if p.waveguard4 != nil and p.waveguard4.valid p.waveguard4.flags = $|MF_NOCLIP p.waveguard4.flags2 = $|MF2_DONTDRAW end
			elseif s.wavebuffering
				P_SpawnPlayerMissile(s, MT_SAMMONBREAKER)
				s.sam_deathball = false
				if p.waveguard1 != nil and p.waveguard1.valid p.waveguard1.flags = $ & ~MF_NOCLIP p.waveguard1.flags2 = $ & ~MF2_DONTDRAW end
				if p.waveguard2 != nil and p.waveguard2.valid p.waveguard2.flags = $ & ~MF_NOCLIP p.waveguard2.flags2 = $ & ~MF2_DONTDRAW  end
				if p.waveguard3 != nil and p.waveguard3.valid p.waveguard3.flags = $ & ~MF_NOCLIP p.waveguard3.flags2 = $ & ~MF2_DONTDRAW  end
				if p.waveguard4 != nil and p.waveguard4.valid p.waveguard4.flags = $ & ~MF_NOCLIP p.waveguard4.flags2 = $ & ~MF2_DONTDRAW  end
			end
			
		  	if s.wavebuffering
			
				if p.waveguard1 != nil  and p.waveguard1.valid
				P_MoveOrigin(p.waveguard1, s.x+cos(FixedAngle(s.wavebufferang))*120+s.momx, s.y+sin(FixedAngle(s.wavebufferang))*120+s.momy, s.z+s.momz+s.height/2)
				end
				if p.waveguard2 != nil and p.waveguard2.valid
				P_MoveOrigin(p.waveguard2, s.x+cos(FixedAngle(s.wavebufferang))*-120+s.momx, s.y+sin(FixedAngle(s.wavebufferang))*-120+s.momy, s.z+s.height/2+s.momz)
				end
				if p.waveguard3 != nil and p.waveguard3.valid
				P_MoveOrigin(p.waveguard3, s.x+sin(FixedAngle(s.wavebufferang))*120+s.momx, s.y+cos(FixedAngle(s.wavebufferang))*120+s.momy, s.z+s.height/2+s.momz)
				end
				if p.waveguard4 != nil and p.waveguard4.valid
				P_MoveOrigin(p.waveguard4, s.x+sin(FixedAngle(s.wavebufferang))*-120+s.momx, s.y+cos(FixedAngle(s.wavebufferang))*-120+s.momy, s.z+s.height/2+s.momz)
				end

				if ( ((p.cmd.buttons & BT_CUSTOM2) and not p.c1c2disable and (p.sams_c2cd and not (p.sams_c2cd > 1))) or ((((p.cmd.buttons & BT_FIRENORMAL) and not p.swapfire) or ((p.cmd.buttons & BT_ATTACK) and p.swapfire)) and (p.sams_normfcd and not (p.sams_normfcd > 1))) )
				and not p.sammorphed
					if p.waveguard1 and p.waveguard1.valid
					p.waveguard1.state = S_NULL
					SamusBeamFire(s, MT_WVCHARGESHOT)
					S_StartSoundAtVolume(s, sfx_wvchsh, 125)
					S_StartSound(s, sfx_wvchsh)
						if p.plshot
						p.plshot.scale = $*4/3
						p.plshot.color = SKINCOLOR_MAGENTA
						end

				elseif p.waveguard2 and p.waveguard2.valid
				p.waveguard2.state = S_NULL
				SamusBeamFire(s, MT_WVCHARGESHOT)
				S_StartSoundAtVolume(s, sfx_wvchsh, 125)
				S_StartSound(s, sfx_wvcm)
					if p.plshot
					p.plshot.scale = $*4/3
					p.plshot.color = SKINCOLOR_MAGENTA
					end

				elseif p.waveguard3 and p.waveguard3.valid
				p.waveguard3.state = S_NULL
				SamusBeamFire(s, MT_WVCHARGESHOT)
				S_StartSound(s, sfx_wvcm)
				S_StartSoundAtVolume(s, sfx_wvchsh, 125)
					if p.plshot
					p.plshot.scale = $*4/3
					p.plshot.color = SKINCOLOR_MAGENTA
					end

				elseif p.waveguard4 and p.waveguard4.valid
				p.waveguard4.state = S_NULL
				SamusBeamFire(s, MT_WVCHARGESHOT)
				S_StartSound(s, sfx_wvcm)
				S_StartSoundAtVolume(s, sfx_wvchsh, 125)
					if p.plshot
					p.plshot.scale = $*4/3
					p.plshot.color = SKINCOLOR_MAGENTA
					end
				s.sam_deathball = false
				s.wavebuffering = 0
				p.sam_wavebeammeltdown = 700
				end				
			end
			--Check again, 'cause it could've stopped.
			if s.wavebuffering
					if ( ( (((p.cmd.buttons & BT_FIRENORMAL) and p.swapfire) or ((p.cmd.buttons & BT_ATTACK) and not p.swapfire)) and (p.sams_firecd and (p.sams_firecd < 3)) ) and not p.sammorphed)
					or (p.sammorphed and ((((p.cmd.buttons & BT_FIRENORMAL) and not p.swapfire) or ((p.cmd.buttons & BT_ATTACK) and p.swapfire)) or p.cmd.buttons & BT_CUSTOM2 and not p.c1c2disable))
					or (s.wavebuffering > 580) or not YouAreSamus(s)
					or (p.waveguard1 == nil and not p.waveguard2 and not p.waveguard3 and not p.waveguard4)
					s.wavebuffering = 0
					P_SamFP(p, PAL_MIXUP, 3)
					S_StartSound(s, sfx_wvcm2) S_StartSound(s, sfx_wvcm2)
					p.sam_wavebeammeltdown = 1050
					
						if s.sam_deathball and p.sam_shinesparkfly
							local sdb = P_SpawnMobj(s.x, s.y, s.z, MT_SAMSUPERMISSILE)					
							if sdb and sdb.valid and sdb.health						
							sdb.target = s
							sdb.scale = 2*FRACUNIT
 							P_ExplodeMissile(sdb)
							end
						end
						if p.waveguard1 and p.waveguard1.valid
						p.waveguard1.flags = $ & ~MF_NOCLIP
						p.waveguard1.flags2 = $ & ~MF2_DONTDRAW
						p.waveguard1.bufferguard = false
						P_Thrust(p.waveguard1, (R_PointToAngle2(p.waveguard1.x, p.waveguard1.y, s.x, s.y)), -60*s.scale)
						p.waveguard1.momz = 0
						end
						if p.waveguard2  and p.waveguard2.valid
						p.waveguard2.flags = $ & ~MF_NOCLIP
						p.waveguard2.flags2 = $ & ~MF2_DONTDRAW
						p.waveguard2.bufferguard = false
						P_Thrust(p.waveguard2, (R_PointToAngle2(p.waveguard2.x, p.waveguard2.y, s.x, s.y)), -60*s.scale)
						p.waveguard2.momz = 0
						end
						if p.waveguard3  and p.waveguard3.valid
						p.waveguard3.flags = $ & ~MF_NOCLIP
						p.waveguard3.flags2 = $ & ~MF2_DONTDRAW
						p.waveguard3.bufferguard = false
						P_Thrust(p.waveguard3, (R_PointToAngle2(p.waveguard3.x, p.waveguard3.y, s.x, s.y)), -60*s.scale)
						p.waveguard3.momz = 0
						end
						if p.waveguard4  and p.waveguard4.valid
						p.waveguard4.flags = $ & ~MF_NOCLIP
						p.waveguard4.flags2 = $ & ~MF2_DONTDRAW
						p.waveguard4.bufferguard = false
						P_Thrust(p.waveguard4, (R_PointToAngle2(p.waveguard4.x, p.waveguard4.y, s.x, s.y)), -60*s.scale)
						p.waveguard4.momz = 0
						end
			   		end
				end
		   end
		elseif s.wavebuffering and s.sam_deathball
			s.sam_deathball = false
		end

//Plasma Beam Combo, Gattling Vortex!

		if s.gattlingvortex
			if not s.gattlingtime
			s.gattlingtime = 1
			else
			s.gattlingtime = $+1
			end
			if s.holdgattle and not ( ((p.cmd.buttons & BT_FIRENORMAL) and p.swapfire) or ((p.cmd.buttons & BT_ATTACK) and not p.swapfire))
			s.holdgattle = nil
			end
				if (s.gattlingvortex > 24)
				s.gattlingvortex = 1
					if G_GametypeHasSpectators() 
						for z in players.iterate
							if z.mo and z.mo.health --Show where it's coming from.
							and not (z.mo == s)
								z.mo.sam_combowarn = 26
								z.mo.sam_combowarntarget = s
								z.mo.sam_combowarnang = R_PointToAngle2(z.mo.x, z.mo.y, s.x, s.y)
							end
						end
					end
				end
				if (leveltime % 8 == 0) and not (s.gattlingtime < 31)
					if p.sammissiles and (p.sammissiles >= 1*p.samadrain)
					p.sammissiles = $-1*p.samadrain
					end
				end
				if (s.gattlingvortex % 4 == 0) and not (p.powers[pw_flashing] and (s.state == S_PLAY_PAIN))
				S_StartSound(s, sfx_plcm)
				p.samxbeam = p.samxbeam+125355
				SamusBeamFire(s, MT_PLSHOT, nil, s.gattlingvortex)
					if p.sams_beamanim
					p.sams_beamanim = 10
					end
				end
				if (P_IsObjectOnGround(s) and (p.speed < 20000))
						if sam_gravitysuit
						s.state = S_SAMUFIRE
						s.frame = A
						else
						s.state = S_SAMUFIRE
						s.frame = F
						end
				end
			p.sams_shotcd = 3
			p.sams_mshotcd = 3
			s.gattlingvortex = $+1
			
			if p.sammorphed or p.sam_shinesparkfly or p.sam_screwattacking or (p.cmd.buttons & (BT_WEAPONNEXT|BT_WEAPONPREV|BT_WEAPONMASK))
			or not s.health or ((s.gattlingtime > 35) and not (p.sammissiles and (p.sammissiles >= 1*p.samadrain))) or not YouAreSamus(s) 
			or ( ((p.cmd.buttons & BT_FIRENORMAL) and p.swapfire) or ((p.cmd.buttons & BT_ATTACK) and not p.swapfire)) and not s.holdgattle
			s.gattlingvortex = 0
			P_SamFP(p, PAL_NUKE, 3)
			S_StartSound(s, sfx_fizzle)
				if s.gattlingtime and (s.gattlingtime < 35)
				p.sam_plasmabeammeltdown = 175
				elseif s.gattlingtime and (s.gattlingtime < 350)
				p.sam_plasmabeammeltdown = s.gattlingtime*5
				else
				p.sam_plasmabeammeltdown = 1750
				end
			s.gattlingtime = 0
			end
		end
		if p.exiting
			if s.state == S_ZEROSUITANIM0
				if s.animfade == nil
					s.animfade = 20
				elseif s.animfade
					s.animfade = $-1
					
					local ghs = P_SpawnMobj(s.x, s.y,s.z-s.height/11, MT_OVERLAY)
					ghs.target = s
					ghs.sprite = SPR_ZSSA
					if (s.animfade > 12)
						ghs.frame = H|TR_TRANS10 
					elseif (s.animfade > 8)
						ghs.frame = H|TR_TRANS30 
					elseif (s.animfade > 4)
						ghs.frame = H|TR_TRANS50
					elseif s.animfade
						ghs.frame = H|TR_TRANS70
					end
					ghs.tics = 2
				end
			end
			if s.samuszerorun //Zero Suit Samus cameo
				if s.health and YouAreSamus(s)
				MorphballReturn(s)
				s.z = s.floorz
				p.pflags = $ & ~PF_JUMPED & ~PF_SPINNING & ~PF_STARTDASH
				s.flags = $|MF_NOGRAVITY
				s.eflags = $ & ~MFE_JUSTHITFLOOR
				p.sammorphed = false
				p.sams_morphcd = 35
				p.sams_beamcombocharge = 0
				p.sams_shotcd = 22
				p.samspbschargefull = false
				p.samspbscharge = 0
				p.sam_shinesparkstop = 0
				p.sambsblchargefull = false
				s.samunmorphed = 4
				s.samslopespin = 0
				s.sambsblchargelvl = 0
				p.bsblboost = 0
				p.bsblboostattack = 0
				s.samspinpad = 0
				s.scale = 90000
				P_SamFP(p, PAL_MIXUP, 5)
				S_StartSound(nil, sfx_nxbump)
				S_StartSound(s, sfx_nxbump)
				S_StartSound(s, sfx_s3k66)
				local ghs = P_SpawnMobj(s.x-1, s.y-1, s.z+s.height, MT_THOK)
				ghs.flags = $|MF_NOCLIPHEIGHT|MF_NOCLIP
				ghs.momz = -9*FRACUNIT
				ghs.color = 27
				ghs.tics = 36
				ghs.scalespeed = $*4
				ghs.destscale = s.scale*99
					
				A_SAMUSEXPLOSIONS(s, 20, MT_SPARK, 65*FRACUNIT, 6000)
				A_SAMUSEXPLOSIONS(s, 10, MT_IVSP, 75*FRACUNIT, 199000)
				A_SAMUSEXPLOSIONS(s, 17, MT_IVSP, 5*FRACUNIT, 900)
				A_SAMUSEXPLOSIONS(s, 18, MT_SPARK, 5*FRACUNIT, 100)
				A_SAMUSEXPLOSIONS(s, 23, MT_SPARK, 30*FRACUNIT, 8000)
				A_SAMUSEXPLOSIONS(s, 16, MT_SPARK, 54*FRACUNIT, 15000)
				A_SAMUSEXPLOSIONS(s, 12, MT_SPARK, 67*FRACUNIT, 20000)
				s.momx = 0
				s.momy = 0
				s.momz = 0
				s.state = S_ZEROSUITANIM0
				SamCam(p, true)
				p.sammorphed = false
				end
			s.samuszerorun = nil
			end		
			if p.samtoken --This takes place rapidly every frame.
			local gems = emeralds
				if not (emeralds & EMERALD1)
				emeralds = $|EMERALD1
				elseif not (emeralds & EMERALD2)
				emeralds = $|EMERALD2
				elseif not (emeralds & EMERALD3)
				emeralds = $|EMERALD3
				elseif not (emeralds & EMERALD4)
				emeralds = $|EMERALD4
				elseif not (emeralds & EMERALD5)
				emeralds = $|EMERALD5
				elseif not (emeralds & EMERALD6)
				emeralds = $|EMERALD6
				elseif not (emeralds & EMERALD7)
				emeralds = $|EMERALD7
				end
				if gems ~= emeralds
					S_StartSound(nil, sfx_cgot)
					CONS_Printf(p, "You received a\x83 Chaos Emerald")
				end
				p.samtoken = $-1
			end
		end

		if not s.health and YouAreSamus(s)	
			p.sam_energy = 0
			p.sam_hyperbeamammo = 0
			P_SetObjectMomZ(s, 500)
			s.flags = $|MF_NOGRAVITY
			if p.sammorphed
				p.morphanim = false
				MorphballReturn(s)
				s.state = S_PLAY_DEAD
			end
			if not p.lives and netgame and (gametype == GT_COOP) --Revive mechanic
				if (p.deadtimer > 277)
				p.deadtimer = 276
				end
				if not (p.samnetcon and (p.samnetcon > 2))
					if (p.cmd.buttons & BT_CUSTOM3)
						if p.samnetcon == nil
						p.samnetcon = 1
						else 
						p.samnetcon = $+1
						end
					
						p.lives = 3
						P_SamFP(p, PAL_MIXUP, 10)
						CONS_Printf(p, "\x82"+"Used up your stage revive!")
						p.playerstate = PST_REBORN
						p.samnetrestart = 3
						return
					end
				end
			end
			if (s.state == S_PLAY_DEAD)
				if not (s.metroiddust or s.plasmadust)
				s.state = S_SAMDEAD1
				P_SamFP(p, PAL_NUKE, 11)
				P_PlayerEmeraldBurst(p)
				P_PlayerFlagBurst(p)
				P_PlayerWeaponAmmoBurst(p)
				end
				if p.sam_hyperbeampanic and p.sam_hyperbeam
					for z in players.iterate
						if not z.nosammusic
						SAM_COM_BufInsertText(z, "tunes -default")
						end
					z.sam_hyperbeampanic = 0
					end
				end
			end
			if (s.state == S_SAMDEAD1)
			P_SpawnMobj(s.x+((P_RandomRange(FixedMul(-55, s.scale), FixedMul(55, s.scale)))*FRACUNIT), 
			s.y+((P_RandomRange(FixedMul(-55, s.scale), FixedMul(55, s.scale)))*FRACUNIT), s.z+((P_RandomRange(FixedMul(1, s.scale), FixedMul(49, s.scale)))*FRACUNIT), MT_BOSSEXPLODE)
			elseif (s.state == S_SAMDEAD2)
			and not (s.metroiddust or s.plasmadust)
			A_SAMPARTICLE(s, MT_MPR2, 4, 7, 10, 7, s.color)
			A_SAMPARTICLE(s, MT_MPR3, 4, 6, 10, 6, s.color)
			elseif (s.state == S_SAMDEAD3)
			and not (s.metroiddust or s.plasmadust)
				if not (s.ghs7 and s.ghs7.valid)
				s.ghs7 = P_SpawnGhostMobj(s)
				s.ghs7.fuse = 60
				s.ghs7.frame = D|FF_FULLBRIGHT|TR_TRANS90
				s.ghs7.scalespeed = $/2
				s.ghs7.destscale = 99*FRACUNIT
				end
			end
		end
		if not SamOncePerMap1
			if s.sam_sprstship
			and not ((gametype == GT_TAG) or (gametype == GT_HIDEANDSEEK) or (p.powers[pw_carry] == CR_NIGHTSMODE))
			p.spawnsship = true
				ActivateHyperMode(p, 7, s) --Third arguments puts in player.mo and checks for boss/metal.
				if s.subsector and s.subsector.sector
					if ((s.subsector.sector.ceilingpic == "PIT" and s.subsector.sector.floorpic == "PIT" 
					and s.subsector.sector.lightlevel < 64 and not (sectors[200] or sectors[205] or sectors[210] or sectors[208]))
					or (gamemap == 103 and s.subsector.sector.lightlevel == 0) or (gamemap == 40 and s.subsector.sector.floorheight == -1024*FRACUNIT))
					s.samhudgimmick = true
						if (gamemap == 40 and s.subsector.sector.floorheight == -1024*FRACUNIT)
						s.saminfhealth = true
						end
					end
				end
				if s.sam_sprstship
				
					if not p.nogunshipspawn and ((p.spawnsship and multiplayer) or (p.spawnsship and YouAreSamus(s))) and not (s.samhudgimmick)
					and not G_IsSpecialStage(gamemap) and not (gamemap == 812)
						if players[0] and p == players[0] and not (players[1] and players[1].samusshipspawn and players[1].samusshipspawn.valid)
							if not (p.samusshipspawn and p.samusshipspawn.valid)
							p.samusshipspawn = P_SpawnMobj(s.x+FixedMul(-65000, cos(s.angle)), s.y+FixedMul (-65000, sin (s.angle)), s.z, MT_SAMUSSHIP)
							p.samusshipspawn.tracer = s
							p.samusshipspawn.angle = s.angle
								if mapheaderinfo[gamemap].lvlttl == "Greenflower" and (gamemap == 01) and (gametype == GT_COOP)
								p.samusshipspawn.dying = true
								end
								for z in players.iterate --GUYS! I'm ALREADY here!!
									if z.mo and z.mo.sam_sprstship
									z.mo.sam_sprstship = false
									z.spawnsship = false
									end
								end
							end
						
						elseif players[0] and players[0].mo == nil and p == players[1]
						and not (players[0] and players[0].samusshipspawn and players[0].samusshipspawn.valid)
							if not (p.samusshipspawn and p.samusshipspawn.valid)
							p.samusshipspawn = P_SpawnMobj(s.x+FixedMul(-65000, cos(s.angle)), s.y+FixedMul (-65000, sin (s.angle)), s.z, MT_SAMUSSHIP)
							p.samusshipspawn.tracer = s
							p.samusshipspawn.angle = s.angle
								if mapheaderinfo[gamemap].lvlttl == "Greenflower" and (gamemap == 01) and (gametype == GT_COOP)
									p.samusshipspawn.dying = true
								end							
								for z in players.iterate --GUYS! I'm ALREADY here!!
									if z.mo and z.mo.sam_sprstship
									z.mo.sam_sprstship = false
									z.spawnsship = false
									end
								end							
							end
						end
					end
				end
				
				p.spawnsship = false
				s.sam_sprstship = false
			end
			SamOncePerMap1 = true
		end
		if s.sam_acz4msg
			if ((s.subsector.sector.tag == 2) or (s.subsector.sector == sectors[9]))
			SamusMission(s, "\x85"+"Gruesome violence incoming.", "\x85"+"Use \x82"+"samusmatch hidegore"+"\x85 in", "\x85"+"the console to hide scene.", 3, 0, true)
			s.sam_acz4msg = false
			end
		end
--THIS IS WHERE THE ACTION BEGINS. s.skin == "samus" is not a thing, btw, use YouAreSamus.

		if (s.health and YouAreSamus(s)) and not (p.powers[pw_carry] == CR_NIGHTSMODE)
		p.weapondelay = 9
		p.currentweapon = 0 
			if p.runspeed != 2199594
				p.charflags = $ & ~SF_NOSKID 
				p.runspeed = 2199594
			end SAM_GRPcd(p, s)
			if (p.sammissilesmax > 250)
				p.sammissilesmax = 250
			end
			if (p.sammissiles > p.sammissilesmax)
				p.sammissiles = p.sammissilesmax
			end
			if (p.samsupermissilesmax > 50)
				p.samsupermissilesmax = 50
			end
			if (p.samsupermissiles > p.samsupermissilesmax)
				p.samsupermissiles = p.samsupermissilesmax
			end
			if (p.sampwmax > 30)
				p.sampwmax = 30
			end
			if (p.sampw > p.sampwmax)
				p.sampw = p.sampwmax
			end
			--Last missile changes.
			if s.samlastmissiles == nil or s.samlastmissiles != p.sammissiles
				s.missiletrns = V_10TRANS-FRACUNIT
			end
			if s.samlastsupers == nil or  s.samlastsupers != p.samsupermissiles
				s.supermissiletrns = V_10TRANS-FRACUNIT
			end
			if s.samlastpw == nil or  s.samlastpw != p.sampw
				s.pwbtrns = V_10TRANS-FRACUNIT
			end
			if s.sam_geton 
				s.sam_geton = $-1
			end		
			
			if (leveltime % 25 == 0)
				if (s.missiletrns < 5*FRACUNIT)
					s.missiletrns = s.missiletrns and $+FRACUNIT or V_10TRANS
				end
				if (s.supermissiletrns < 5*FRACUNIT)
					s.supermissiletrns = s.supermissiletrns and $+FRACUNIT or V_10TRANS
				end
				if (s.pwbtrns < 5*FRACUNIT)
					s.pwbtrns = s.pwbtrns and $+FRACUNIT or V_10TRANS
				end
			end	
			
			s.samlastmissiles = p.sammissiles
			s.samlastsupers = p.samsupermissiles
			s.samlastpw = p.sampw
			
			if p.sam_autotank
				if (p.sam_energy < p.sam_autotank)
				and not p.sam_intoship and not p.sam_saveselect
					s.sam_autotimer = s.sam_autotimer and $+1 or 1
					if (s.sam_autotimer > 40)
						p.sam_energy = $+1 --auto restore energy with an autotank
						s.sam_autotimer = 0
					end
				end
			end
			
			if p.samnetrestart
				S_StartSound(s, sfx_samjin)
				S_StartSound(s, sfx_samspa)
				p.samnetrestart = 0
			end

			if (P_PlayerTouchingSectorSpecial(p, 3, 6) or P_PlayerTouchingSectorSpecial(p, 3, 5)) and P_IsObjectOnGround(s) and ((s.samspinpad and s.samspinpad < 42) or not s.samspinpad)
				if P_PlayerTouchingSectorSpecial(p, 3, 5) and not p.sammorphed
					s.samspinpadconfirmed = 12
					s.samspeedpad = 12
 					p.bsblboost = 0
				else
					s.samspinpad = 50
 					p.bsblboost = 0
				end
			end

--Switch skins between Power Suit Samus and Varia/Gravity Suit Samus whenever!

			if not p.sam_gravitysuit
				if s.skin == "samus"
					SAM_COM_BufInsertText(p, "skin samus; skin basesamus")		
					R_SetPlayerSkin(p, "basesamus")
					s.skin = "basesamus"
				end
			elseif s.skin == "basesamus" --Switch skins!
				SAM_COM_BufInsertText(p, "skin basesamus; skin samus")	
				R_SetPlayerSkin(p, "samus")
				s.skin = "samus"
			end
			if p.sammorphed and p.sammorphanimcd != nil
				if (p.sammorphanimcd <= 8)
				s.state = S_SAMUMB4
				elseif ((p.sammorphanimcd > 8) and (p.sammorphanimcd <= 16))
				s.state = S_SAMUMB3
				elseif ((p.sammorphanimcd > 16) and (p.sammorphanimcd <= 24))
				s.state = S_SAMUMB2
				elseif (p.sammorphanimcd > 24)
				s.state = S_SAMUMB1
				end
			elseif (p.pflags & PF_SLIDING)
				s.state = S_PLAY_FALL
			end

//Check the lightning in your sector.			

			if s.bcz4prep
				if (gamemap == 26)
				and mapheaderinfo[gamemap].lvlttl == "Black Core"
					p.sambeamlight = V_70TRANS
					if p.sam_fullypoweredsuit 
					and p.samworldevents
					G_SetCustomExitVars(28) --Get prepped to go to the TRUE Final Boss.
					end
				end
			elseif p.sam_firstperson and s.subsector and s.subsector.sector
				for fof in s.subsector.sector.ffloors()
					if fof and (fof.flags & FF_EXISTS) 
						if not (fof.flags & FF_NOSHADE) and not (fof.flags & FF_DOUBLESHADOW)
						and (s.z < fof.bottomheight)
						p.sambeamfoflight = fof.toplightlevel

						elseif (fof.flags & FF_CUTSPRITES) and ((s.z >= fof.bottomheight) and (s.z+(s.height/2) < fof.topheight))
						p.sambeamfoflight = fof.toplightlevel+20
						end
					end
				end
				if p.sambeamfoflight
				p.sambeamlightsec = p.sambeamfoflight
				else
				p.sambeamlightsec = s.subsector.sector.lightlevel
				end
				p.sambeamfoflight = nil
				if (s.eflags & MFE_UNDERWATER)
					if (P_PlayerTouchingSectorSpecial(s.player, 1, 3))
					p.sambeamlightcolor = SKINCOLOR_ORANGE
					p.sambeamlight2 = V_40TRANS
					elseif (P_PlayerTouchingSectorSpecial(s.player, 1, 2)) or (s.eflags & MFE_GOOWATER)
					p.sambeamlightcolor = SKINCOLOR_MAGENTA
					p.sambeamlight2 = V_60TRANS
					else
					p.sambeamlightcolor = SKINCOLOR_BLUE
					p.sambeamlight2 = V_70TRANS
					end
				end
				if (p.sambeamlightsec > 224) or p.sambeamlightsec == nil
				p.sambeamlight = nil
				elseif (p.sambeamlightsec > 224)
				p.sambeamlight = V_90TRANS
				elseif (p.sambeamlightsec > 192)
				p.sambeamlight = V_80TRANS
				elseif (p.sambeamlightsec > 160)
				p.sambeamlight = V_70TRANS
				elseif (p.sambeamlightsec > 140)
				p.sambeamlight = V_60TRANS
				elseif (p.sambeamlightsec > 120)
				p.sambeamlight = V_50TRANS
				elseif (p.sambeamlightsec > 100)
				p.sambeamlight = V_40TRANS
				elseif (p.sambeamlightsec > 80)
				p.sambeamlight = V_30TRANS
				else
				p.sambeamlight = V_20TRANS
				end
			end
			if s.dsz2msgprep
				if s.subsector and s.subsector.sector				
					if ((s.subsector.sector.tag == 403) or s.subsector.sector == sectors[1866])		
					SamusMission(s, "\x80  Hold\x82 CUSTOM1\x80 or\x82 FIRE", "\x80 while in Morph Ball to", "\x80      spin in place", 3, 0, true)
					s.dsz2msgprep = false
					end
				end						
			elseif s.sam_rvzmsg
				if s.subsector and s.subsector.sector				
					if ((s.subsector.sector.tag == 55) or s.subsector.sector == sectors[145])
					SamusMission(s, "\x84"+"UNIQUE ARTIFACT SIGNATURE", "\x84     DETECTED IN ZONE", nil, 3, 0, false)
					s.sam_rvzmsg = false
					end
				end
			end

			if p.sammorphed and p.sam_spiderball
			
				if s.spiderbcharge == nil
					s.spiderbcharge = 103
				end
				if (s.spiderbcharge < 103)
					if p.powers[pw_carry] or P_IsObjectOnGround(s)
					SamSpiderCharge(p, s, 103)
					s.spiderbanim = 7
						if p.sammorphed
						S_StartSound(s, sfx_s3k7c)
						local ghs
							if (s.eflags & MFE_VERTICALFLIP)
							ghs = P_SpawnMobj(s.x, s.y, s.z+s.height, MT_SAMUSZAP)
							else
							ghs = P_SpawnMobj(s.x, s.y, s.z-s.height/10, MT_SAMUSZAP)
							end
							ghs.blendmode = AST_ADD
							ghs.scale = s.scale
							ghs.momx = s.momx
							ghs.momy = s.momy
							ghs.momz = s.momz
							ghs.eflags = s.eflags
							ghs.color = SKINCOLOR_MOSS
							ghs.morning = 4
						end
					end
				end
				
//New Spider Ball wall-climbing code!

				if s.spiderballing
					p.camerascale = 196071 //261427
					s.thesamcam = 1
					if (p.pflags & PF_ANALOGMODE)
					p.sam_forceanalogoff = 2
					p.pflags = $ & ~PF_ANALOGMODE
					end
					if s.spideronce
						for d = 1,30 --Inch closer to the wall if possible.
						P_TryMove(s, s.x+FixedMul(FRACUNIT, cos(s.angle)), s.y+FixedMul(FRACUNIT, sin (s.angle)), true)
						end
						SamusTutorial(s, "spiderball", true)
					end
					if p.sambsblchargefull
					p.camerascale = 261427
					s.thesamcam = 1
					end
					--Shiny stuff is all the rage
					local ghs = P_SpawnMobjFromMobj(s,0,0, (s.height/2)+s.momz, MT_THOK)
					ghs.sprite = SPR_OILF
					ghs.blendmode = AST_ADD
					ghs.tics = 2
					ghs.frame = A|TR_TRANS90|FF_FULLBRIGHT
					if (leveltime % 2 == 0)
					ghs.scale = $*20/19
					ghs.frame = A|TR_TRANS80|FF_FULLBRIGHT
					ghs.colorized = true
						if p.samvisorcolor and (p.sam_gravitysuit and p.sam_gravitysuit == 2)
							ghs.color = p.samvisorcolor
						else
							ghs.color = SKINCOLOR_GREEN
						end							
					end		
					if not s.spiderthok
					s.spiderthok = P_SpawnMobj(s.x, s.y, s.z-s.height/3, MT_THOK)
					s.spiderthok.sprite = SPR_YGTL
					s.spiderthok.tics = 2
					s.spiderthok.renderflags = $|RF_FULLBRIGHT
					s.spiderthok.scale = s.scale*10/8
					s.spiderthok.destscale = s.scale*4
					s.spiderthok.momz = s.momz
					P_Thrust(s.spiderthok, s.angle, 1)
					
						if p.samvisorcolor and (p.sam_gravitysuit and p.sam_gravitysuit == 2) and s.spiderthok and s.spiderthok.valid
						s.spiderthok.color = p.samvisorcolor
						elseif s.spiderthok and s.spiderthok.valid
						s.spiderthok.color = SKINCOLOR_GREEN
						end
						
						if p.samvisorcolor and (p.samvisorcolor == SKINCOLOR_RED or p.samvisorcolor == SKINCOLOR_CRIMSON)
							if s.spiderthok and s.spiderthok.valid and (s.spiderbcharge and s.spiderbcharge < 35)
							s.spiderthok.color = SKINCOLOR_BLACK
							end
						else
							if s.spiderthok and s.spiderthok.valid and (s.spiderbcharge and s.spiderbcharge < 35)
							s.spiderthok.color = SKINCOLOR_RED
							end
						end
					elseif s.spiderthok and s.spiderthok.valid
					s.spiderthok = nil
					end
					
					if not s.spiderbsound
						s.spiderbsound = 44
						S_StartSound(s, sfx_spdrbl)
					else
						s.spiderbsound = $-1
						if s.spiderbsound == 1
						S_StartSound(s, sfx_spdrbl)
						s.spiderbsound = 44 
						end
					end
					
					local ghs = P_SPMAngle(s, MT_SAMWALLJUMP, s.angle)
						if ghs and ghs.valid			
						ghs.z = $-(s.scale*8*P_MobjFlip(s))			
						ghs = P_SPMAngle(s, MT_SAMWALLJUMP, s.angle)
						end
					P_SPMAngle(s, MT_SAMWALLJUMP, s.angle)
					if s.spideronce
						if ghs and ghs.valid
						P_Thrust(ghs, s.angle, 16*FRACUNIT)
						end
						P_InstaThrust(s, s.angle, 19*FRACUNIT)
					s.spideronce = $-1
					end
					s.angle = s.spiderballing
					p.drawangle = s.spiderballing
					P_InstaThrust(s, 0, 0)
					if not s.bombjump
						if p.cmd.forwardmove
							if s.spiderbcharge
							s.spiderbcharge = $-1
							end
							if (leveltime % 3 == 0)
							P_SpawnMobj(s.x, s.y, s.z-s.height/2, MT_SPARK)
							end
							P_SetObjectMomZ(s, FixedDiv(p.cmd.forwardmove*FRACUNIT, 4*FRACUNIT), false)
							p.speed = 28*FRACUNIT
						else
							if (leveltime % 6 == 0) and s.spiderbcharge
							s.spiderbcharge = $-1
							end
							s.momz = 0
						end
					end
					if s.spiderbwait
					s.spiderbwait = $-1
					end

					if P_IsObjectOnGround(s) or not s.spiderbcharge
					s.spiderballing = 0
					s.spiderbsound = 0
					P_Thrust(s, s.angle, -3*FRACUNIT)
					s.momz = $/2
					S_StopSoundByID(s, sfx_spdrbl)

					elseif not s.spiderballstillgood
					s.spiderballing = 0
					s.spiderbsound = 0
					P_InstaThrust(s, s.angle, 8*FRACUNIT)
					s.momz = 0
					S_StopSoundByID(s, sfx_spdrbl)
					
					elseif s.bombjump or p.powers[pw_flashing] or (p.powers[pw_carry] == CR_ZOOMTUBE)
					p.pflags = $|PF_JUMPED
					s.spiderballing = 0
					s.spiderbsound = 0
					S_StopSoundByID(s, sfx_spdrbl)
					
					elseif (p.cmd.buttons & BT_USE) or not p.sammorphed
					s.spiderballing = 0
					s.spiderbsound = 0
					P_Thrust(s, s.angle, -9*FRACUNIT)
					s.momz = 2
					p.pflags = $|PF_JUMPED
					S_StartSound(s, sfx_sballf)
					p.sams_morphcd = 35
					S_StopSoundByID(s, sfx_spdrbl)
					
					elseif (p.cmd.buttons & BT_JUMP) and not s.spiderbwait and not (p.sams_jumpcd > 2) or p.bsblboost
					s.spiderballing = 0
					s.spiderbsound = 0
					s.angle = $+ANGLE_180
					p.pflags = $|PF_JUMPED
					p.sams_jumpcd = 10
					P_InstaThrust(s, s.angle, 14*FRACUNIT)
					S_StopSoundByID(s, sfx_spdrbl)
					
						if p.sam_highjump and not p.bsblboost
						S_StartSound(s, sfx_sballj)
						P_SetObjectMomZ(s, 9*FRACUNIT, false)
						else
						P_SetObjectMomZ(s, FRACUNIT, false)
						
							if p.bsblboost
								if s.spiderbcharge < 103							
								SamSpiderCharge(p, s, 103)
								s.spiderbanim = 7
								end
							P_InstaThrust(s, s.angle, 80*FRACUNIT)
							P_SetObjectMomZ(s, 7*FRACUNIT, false)
							local ghs = P_SpawnMobj(s.x, s.y, s.z-((s.height/10)*P_MobjFlip(s)), MT_SAMUSZAP)
							ghs.blendmode = AST_ADD							
							ghs.scale = s.scale
							ghs.eflags = s.eflags
							ghs.momx = s.momx ghs.momy = s.momy ghs.momz = s.momz
							ghs.color = SKINCOLOR_MOSS
							ghs.morning = 4
							S_StartSound(s, sfx_s3k7c)
							end
							
							p.bsblboostangle = s.angle
						end
					end
				else
					S_StopSoundByID(s, sfx_spdrbl)
				end			
			
			
			
			
				if s.spiderbcharge and not ( ((p.pflags & PF_STARTJUMP) and ((s.momz > 1*P_MobjFlip(s)) and not (s.eflags & MFE_VERTICALFLIP)) ) ) and not p.gotflag and not p.powers[pw_nocontrol]
					if ( (p.sam_highjump and (p.cmd.buttons & BT_JUMP) and not (p.sams_jumpcd > 3)) or ((p.sams_jumpcd >= 1) and (p.sams_jumpcd < 5) and not p.sam_highjump) ) 
					and not p.powers[pw_flashing] and not s.bombjump and not s.spiderballing
					or s.spiderballdoublecheck
						if (p.pflags & PF_ANALOGMODE) and P_IsObjectOnGround(s)
						and not p.sam_highjump
							P_SetObjectMomZ(s, 2*FRACUNIT, true)
						elseif (p.pflags & PF_DIRECTIONCHAR)
							p.drawangle = s.angle
						end
						
							s.spiderthok = P_SpawnMobj(s.x, s.y, s.z-s.height/3, MT_THOK) --Don't turn into a local variable.
							s.spiderthok.sprite = SPR_YGTL
							s.spiderthok.tics = 1
							s.spiderthok.scale = s.scale*10/8
							s.spiderthok.destscale = s.scale*4
							s.spiderthok.momz = s.momz
							s.spiderthok.momx = s.momx+1
							s.spiderthok.momy = s.momy+1
						
							if p.samvisorcolor and (p.sam_gravitysuit and p.sam_gravitysuit == 2) and s.spiderthok and s.spiderthok.valid
							s.spiderthok.color = p.samvisorcolor
							elseif s.spiderthok and s.spiderthok.valid
							s.spiderthok.color = SKINCOLOR_GREEN
							end
							
							s.spiderthok.tics = 2
							local ghs = P_SPMAngle(s, MT_SAMWALLJUMP, s.angle)
							P_SPMAngle(s, MT_SAMWALLJUMP, s.angle)
							
							if ghs and ghs.valid
							P_Thrust(ghs, s.angle, 20*FRACUNIT)
							end
						s.spideronce = 4
						
						if s.spiderballgo and not s.spiderballdoublecheck
							s.spiderballdoublecheck = 2
							s.spiderballgo = 0
							local ghs = P_SPMAngle(s, MT_SAMWALLJUMP, s.angle)
							P_SPMAngle(s, MT_SAMWALLJUMP, s.angle)
								if ghs and ghs.valid
								P_Thrust(ghs, s.angle, 20*FRACUNIT)
								end
							s.spideronce = 4

						elseif s.spiderballgo and s.spiderballdoublecheck
						
							P_InstaThrust(s, s.angle, 19*FRACUNIT)
							p.bsblboost = 0 
							p.sams_jumpcd = 3
							p.pflags = $|PF_JUMPDOWN & ~PF_JUMPED
							P_SetObjectMomZ(s, 5*FRACUNIT, false)
							S_StartSound(s, sfx_spdrbl)
						
							if (s.angle == 0)
							s.spiderballing = 1
							else
							s.spiderballing = s.angle
							end
							
							s.spiderbwait = 10
							s.spiderballdoublecheck = 0
							s.spiderballgo = false
						end
					elseif p.powers[pw_flashing] and (p.cmd.buttons & BT_JUMP) and not (p.sams_jumpcd > 1)
						S_StartSound(s, sfx_sdenid)
					end
				end
			end

			if s.samhudgimmick or (p.dialog != nil and p.dialog == true)
				p.sams_shotcd = 3
				p.sams_mshotcd = 3
				p.sams_morphcd = 3
			end

			if p.sams_beamcombocharge
				p.sams_beamcombocharge = $+1
				s.sam_stopmult = 2
				if (p.sams_beamcombocharge == 2)
					SamusTutorial(s, "beamcombo", true)
					P_SamFP(p, PAL_MIXUP, 2)
					A_samquake(p.mo, 9*FRACUNIT, 7, 4000*FRACUNIT)
					local ghs = SamusWeaponReady(s, p)
					if ghs and ghs.valid
					ghs.dontstateme = true
					ghs.sprite = SPR_MBOM
					ghs.frame = R|FF_FULLBRIGHT|TR_TRANS50
					ghs.blendmode = AST_ADD
					ghs.tics = 99 ghs.fuse = 16
					ghs.scale = $/8 ghs.scalespeed = $*2
					ghs.color = p.sam_beamcolor
					ghs.destscale = $*99
					end					
				end
				if (p.sams_beamcombocharge % 2 == 0)
				p.samxbeam = p.samxbeam+125355
				p.samybeam = p.samybeam+105355		
				end
				if (p.sams_beamcombocharge % 4 == 0)
				local ghs
					if (s.eflags & MFE_VERTICALFLIP)
					ghs = P_SpawnMobj(s.x, s.y, s.z+s.height*10/13, MT_SAMUSZAP)
					else
					ghs = P_SpawnMobj(s.x, s.y, s.z+s.height/4, MT_SAMUSZAP)
					end
					if ghs and ghs.valid
					ghs.blendmode = AST_ADD
					ghs.scale = s.scale*30/41
					ghs.momx = s.momx/2
					ghs.momy = s.momy/2
					ghs.momz = s.momz/2
					ghs.eflags = s.eflags
					ghs.color = p.sam_beamcolor
					ghs.morning = 4
					end
				end
				if (p.sams_beamcombocharge < 4)
					A_SAMPARTICLE(s, MT_MPR5, 35, 4, -4, 4, p.sam_beamcolor, P_RandomRange(75355, 98355))
					local ghs = P_SpawnMobj(s.x, s.y, s.z+24*FRACUNIT, MT_PBEXPANDW)
					if ghs and ghs.valid
						ghs.scale = $*3
						ghs.scalespeed = $*6
						ghs.destscale = $*25
						ghs.momx = s.momx
						ghs.momy = s.momy
						ghs.momz = s.momz
					end
				end
				p.sam_shinespark = 0
				p.sam_shinesparkstop = 0
				p.sams_supermissileready = 0
				p.sams_screwattackcd = 2
				A_SAMPARTICLE(s, MT_MPR1, 10, 3, 1, 15, p.sam_beamcolor, 35000, true)
				A_SAMPARTICLE(s, MT_MPR1, 20, 6, 1, 45, SKINCOLOR_WHITE, 23000, true)
				A_SAMPARTICLE(s, MT_MPR1, 15, 7, 1, 9, SKINCOLOR_RED, 29000, true)

				if p.sams_beamcombocharge and (p.sams_beamcombocharge >= 35)
					if p.sam_powerbeamequip and p.sam_powerbeamcombo
						if p.sammissiles and (p.sammissiles >= 10*p.samadrain)
						p.sammissiles = $-10*p.samadrain
						else
						p.sammissiles = 0
						end
					A_samquake(s, 10*FRACUNIT, 5, 3000*FRACUNIT)
					SamusBeamFire(s, MT_SAMSUPERMISSILE)		
					P_SamFP(p, PAL_MIXUP, 4)
					S_StartSound(s, sfx_smsh)
					S_StartSound(s, sfx_smsh)
					p.sam_powerbeammeltdown = 665

					elseif p.sam_spazerbeamequip and p.sam_spazerbeam and p.sam_spazerbeamcombo
						if p.sammissiles and (p.sammissiles >= 15*p.samadrain)
						p.sammissiles = $-15*p.samadrain
						else
						p.sammissiles = 0
						end
					A_samquake(s, 12*FRACUNIT, 8, 5000*FRACUNIT)
					SamusBeamFire(s, MT_SPCHARGECOMBO)
					P_SamFP(p, PAL_RECYCLE, 9)
					S_StartSound(s, sfx_spcm)
					S_StartSound(s, sfx_spcm)
					S_StartSound(s, sfx_spcm)
					S_StartSound(s, sfx_spcm)
					p.sam_spazerbeammeltdown = 900

					elseif p.sam_wavebeamequip and p.sam_wavebeam and p.sam_wavebeamcombo
						if p.sammissiles and (p.sammissiles >= 15*p.samadrain)
						p.sammissiles = $-15*p.samadrain
						else
						p.sammissiles = 0
						end
					A_samquake(s, 12*FRACUNIT, 4, 1500*FRACUNIT)
					s.wavebuffering = 1
					P_SamFP(p, PAL_MIXUP, 4)
					s.sam_flashfade = 9
					s.sam_flashfadecolor = SKINCOLOR_SUPERGOLD5
					S_StartSound(s, sfx_wvcm)
					S_StartSound(s, sfx_wvcm2)
					p.sams_beamanim = 15

					elseif p.sam_icebeamequip and p.sam_icebeam and p.sam_icebeamcombo
							if p.sammissiles and (p.sammissiles >= 15*p.samadrain)
							p.sammissiles = $-15*p.samadrain
							else
							p.sammissiles = 0
							end
						A_samquake(s, 8*FRACUNIT, 6, 3500*FRACUNIT)	
						SamusBeamFire(s, MT_ICCHARGECOMBO)
						P_SamFP(p, PAL_MIXUP, 7)
						s.sam_flashfade = 11
						s.sam_flashfadecolor = SKINCOLOR_SKY						
						S_StartSound(s, sfx_iccm)
						S_StartSound(s, sfx_iccm)
						p.sam_icebeammeltdown = 1400

					elseif p.sam_plasmabeamequip and p.sam_plasmabeam and p.sam_plasmabeamcombo
						if p.sammissiles and (p.sammissiles >= 5*p.samadrain)
						p.sammissiles = $-5*p.samadrain
						else
						p.sammissiles = 0
						end
					A_samquake(s, 4*FRACUNIT, 5, 3800*FRACUNIT)
					s.gattlingvortex = 1
					s.holdgattle = true
					P_SamFP(p, PAL_NUKE, 8)
					s.sam_flashfade = 16
					s.sam_flashfadecolor = SKINCOLOR_KETCHUP						
					S_StartSound(s, sfx_plchsh)


					elseif p.sam_hyperbeamequip and p.samchallenger and (p.samchallenger > 3)
						if p.sammissiles and (p.sammissiles >= 10*p.samadrain)
						p.sammissiles = $-(10*p.samadrain)
						else
						p.sammissiles = 0
						end
						if p.sam_hyperbeamammo and (p.sam_hyperbeamammo >= 15*p.samadrain)
						p.sam_hyperbeamammo = $-(15*p.samadrain)
						P_SamFP(p, PAL_WHITE, 2)
						s.sam_flashfade = 9
						s.sam_flashfadecolor = SKINCOLOR_SUPERSKY1
						elseif p.sam_energy and (p.sam_energy > 25)
						p.sam_energy = $-25
						P_SamFP(p, PAL_NUKE, 6)
						s.sam_flashfade = 19
						s.sam_flashfadecolor = SKINCOLOR_CRIMSON							
						P_Thrust(s, s.angle, -16*FRACUNIT)
						P_PlayRinglossSound(s)
						else
						p.sam_hyperbeamammo = 0
						end

						p.sams_shotcd = 3
						p.sams_mshotcd = 3
						A_samquake(s, 18*FRACUNIT, 12, 7500*FRACUNIT)
						SamusBeamFire(s, MT_HBSHOT) SamusTutorial(s, "hyperbeam", true)				
							if p.plshot and p.plshot.valid
							p.plshot.color = SKINCOLOR_SUPERGOLD5
							p.plshot.truehyper = true
							end
							if (p.pflags & PF_JUMPED)
							P_SetObjectMomZ(s, 2*FRACUNIT, true)
							end
						S_StartSound(s, sfx_hbsh)
						P_Thrust(s, s.angle, -16*FRACUNIT)

						S_StartSound(s, sfx_spcm)
						if (p.sam_hyperbeamammo < 0) --Don't go negative.
						p.sam_hyperbeamammo = 0
						end				
					end

				p.sams_beamcombocharge = 0
				end
			end

			if (p.cmd.buttons & BT_WEAPONMASK)
				if (p.cmd.buttons & BT_WEAPONMASK) != 7 and not p.holdingswitch 
				and not p.powers[pw_nocontrol]
				and not p.sam_beamswitch and not p.sams_chargetime
				p.sam_wantstoswitch = (p.cmd.buttons & BT_WEAPONMASK)
				p.sams_c2cd = 0
				p.sams_firecd = 0
				p.sams_normfcd = 0
				p.sams_powerbombready = 0
				p.sams_supermissileready = 0
				p.sams_mshotcd = 10
				A_SAMBEAMSWITCH(p)
				p.holdingswitch = true
				end
			elseif p.holdingswitch
				p.holdingswitch = false
			end

			if not p.sam_fullypoweredsuit and (gametype == GT_COOP)
				if p.sams_itemcompletion and (p.sams_itemcompletion >= 100)
				and not p.exiting and not p.sam_saveselect and (leveltime > 3)
					p.sam_fullypoweredsuit = true
					p.sam_fpsmsg = 370
				end
			elseif p.sam_fpsmsg and (gametype == GT_COOP) and not p.exiting and not p.sam_saveselect and (leveltime > 3)
			p.sam_fpsmsg = $-1
				if (p.sam_fpsmsg == 293)
					S_StartSound(nil, sfx_samusp)
					print("\x83 "+p.name+"\x83 obtained their\x82 fully powered suit")
						for z in players.iterate
							if not z.nosammusic
							SAM_COM_BufInsertText(z, "tunes -none") 
							end
						end
				end
				if (p.sam_fpsmsg < 293)
					A_SAMPARTICLE(s, MT_MPR1, 50, 1, 1, 5, SKINCOLOR_MAGENTA, 40000, true)
					A_SAMPARTICLE(s, MT_MPR1, 30, 1, 1, 6, SKINCOLOR_ORANGE, 25000, true)
				end
				if (p.sam_fpsmsg == 1)
						for z in players.iterate
							if not z.nosammusic
							SAM_COM_BufInsertText(z, "tunes SAMUSP 0 1.25") 
							end
						end
					CONS_Printf(p, "\x82 -Ammo and Energy completely refilled!")
					A_samquake(s, 11*FRACUNIT, 50, 1000*FRACUNIT)
					P_SamFP(p, PAL_MIXUP, 10)
					p.sam_energy = 99+(p.sam_energytanksmax*50)
					p.sam_hyperbeamammo = 99
					p.sammissiles = p.sammissilesmax
					p.sampw = p.sampwmax
					p.samsupermissiles = p.samsupermissilesmax
					p.sam_shinespark = 80
					p.sam_spacejumped = false
					s.pwbomb = P_SpawnMobj(s.x, s.y, s.z, MT_SAMPOWERBOMB)
					s.pwbomb.target = s
					s.pwbomb.gravitysuitjustgot = true
					S_StartSound(s, sfx_pwbmb)
					if ((p.cmd.buttons & BT_CUSTOM1) and not p.c1c2disable or p.autospeedbooster) and not p.samspbschargefull
					p.runspeed = 2199594
					StartSamSPBS(s,p)
					end
					p.sam_fpsmsg = 0
				end
				
			end


			if p.sammatchchars and (p.sammatchchars  == 2) and G_GametypeHasSpectators() 
				if not p.sams_morphcd
				CONS_Printf(p, "\x85"+"The server has set the battle restriction to Sonic chars only. Switch to any other character to play!")
				end
			p.powers[pw_nocontrol] = 3
			p.sams_supermissileready = 0
			p.sams_shotcd = 3
			p.sams_mshotcd = 3
			p.sams_morphcd = 35
			end
			if YouAreSamus(s)
				if (((s.scale <= FRACUNIT+5500) and (s.scale >= FRACUNIT-5500)) and ((s.destscale <= FRACUNIT+5500) and (s.destscale >= FRACUNIT-5500)))
				or ((s.scale == 42001) and not p.sammorphed)
					if not s.samusscaled
						if p.sam_firstperson --remember between levels what your FPS preference was.
						and not twodlevel
						and not (s.flags2 & MF2_TWOD)
						SamCam(p, false)
						end
					end				
					s.scale = 90000
					s.destscale = s.scale
					p.visorclosein = 15
				
					SamusTutorial(s, "beams")
					SamusTutorial(s, "monitorhunt")
					SamusTutorial(s, "lockon")	
				
					if leveltime and p.jointime
						S_StartSound(s, sfx_samspa, p)
					end
					s.samusscaled = true
					s.samusjustspawned = true
					p.powers[pw_shield] = SH_NONE
				end
				if p.rings
					if p.samxtralife == nil or not p.xtralife
						p.samxtralife = p.xtralife
						p.xtralife = 9
					end
					if s.saminfhealth
						p.sam_energy = $+1
					end
					if s.samusjustspawned or (p.rings == 1)
					p.rings = 2
					s.samusjustspawned = false
					s.prevsamhealth = p.rings
						if (P_PlayerTouchingSectorSpecial(p, 1, 10) or (P_PlayerTouchingSectorSpecial(p, 1, 9) and P_IsObjectOnGround(s))) and (p.sam_energy > 1)
							p.sam_energy = $-1
							p.sam_energyflash = 1
						end
					elseif s.prevsamhealth and p.rings
					and not (s.prevsamhealth == p.rings)
						if (p.rings != (s.prevsamhealth+100)) --1-ups from score do not heal you.
						GiveSamusEnergy(p, p.rings-s.prevsamhealth, 1)
						end
						s.prevsamhealth = p.rings
					else
						s.prevsamhealth = p.rings
					end
					if (p.rings > 9850) --What are we doing??
						p.rings = 8001
						s.prevsamhealth = p.rings
					end
				end
				if p.objectplacer
					p.sams_shotcd = 3
					p.sams_mshotcd = 3
				end				

				if not (s.gravitylights and s.gravitylights.valid)
					s.gravitylights = P_SpawnMobj(s.x, s.y, s.z, MT_OVERLAY)
					s.gravitylights.target = s
					s.gravitylights.skin = "samus"
					s.gravitylights.sprite2 = s.sprite2|FF_SPR2SUPER
					s.gravitylights.tics = 2
					s.gravitylights.rollangle = s.rollangle
					s.gravitylights.spritexscale = s.spritexscale
					s.gravitylights.spriteyscale = s.spriteyscale
					s.gravitylights.spritexoffset = s.spritexoffset
					s.gravitylights.spriteyoffset = s.spriteyoffset
					
					if p.samvisorcolor and (p.sam_gravitysuit == 2)
						s.gravitylights.color = p.samvisorcolor
					else
						s.gravitylights.color = SKINCOLOR_EMERALD
					end
				end
			end

			if (p.sam_energy != nil)
				p.sam_energylimit = 99+(p.sam_energytanks*50)
					if (p.sam_energy <= 0)
					p.sam_energy = 0
					p.sam_energytanks = 0
					p.sam_hudenergy = 0
					elseif (p.sam_energy <= 99)
					p.sam_energytanks = 0
						if not (p.sam_energy > p.sam_energylimit) 
						p.sam_hudenergy = p.sam_energy
						end
					elseif (p.sam_energy < 149)
					p.sam_energytanks = 1
						if not (p.sam_energy > p.sam_energylimit)
						p.sam_hudenergy = p.sam_energy-100
						end
					elseif (p.sam_energy <= 199)
					p.sam_energytanks = 2
						if not (p.sam_energy > p.sam_energylimit)
						p.sam_hudenergy = p.sam_energy-100
						end
					elseif (p.sam_energy <= 249)
					p.sam_energytanks = 3
						if not (p.sam_energy > p.sam_energylimit)
						p.sam_hudenergy = p.sam_energy-200
						end
					elseif (p.sam_energy <= 299)
					p.sam_energytanks = 4
						if not (p.sam_energy > p.sam_energylimit)
						p.sam_hudenergy = p.sam_energy-200
						end
					elseif (p.sam_energy <= 349)
					p.sam_energytanks = 5
						if not (p.sam_energy > p.sam_energylimit)
						p.sam_hudenergy = p.sam_energy-300
						end
					elseif (p.sam_energy <= 399)
					p.sam_energytanks = 6
						if not (p.sam_energy > p.sam_energylimit)
						p.sam_hudenergy = p.sam_energy-300
						end
					elseif (p.sam_energy <= 449)
					p.sam_energytanks = 7
						if not (p.sam_energy > p.sam_energylimit)
						p.sam_hudenergy = p.sam_energy-400
						end
					elseif (p.sam_energy <= 499)
					p.sam_energytanks = 8
						if not (p.sam_energy > p.sam_energylimit)
						p.sam_hudenergy = p.sam_energy-400
						end
					elseif (p.sam_energy <= 549)
					p.sam_energytanks = 9
						if not (p.sam_energy > p.sam_energylimit)
						p.sam_hudenergy = p.sam_energy-500
						end
					elseif (p.sam_energy <= 599)
					p.sam_energytanks = 10
						if not (p.sam_energy > p.sam_energylimit)
						p.sam_hudenergy = p.sam_energy-500
						end
					elseif (p.sam_energy <= 649)
					p.sam_energytanks = 11
						if not (p.sam_energy > p.sam_energylimit)
						p.sam_hudenergy = p.sam_energy-600
						end
					elseif (p.sam_energy <= 699)
					p.sam_energytanks = 12
						if not (p.sam_energy > p.sam_energylimit)
						p.sam_hudenergy = p.sam_energy-600
						end
					end
					if (p.sam_energy <= 29)
						if not p.samemecd
						p.samemecd = 33
							if not p.exiting
							S_StartSound(s, sfx_sameme)
							end
						else
						p.samemecd = $-1
						end
					end
				if (p.sam_energytanksmax > 12) 
				p.sam_energytanksmax = 12
				end
				if (p.sam_energytanks > p.sam_energytanksmax)
				p.sam_energytanks = p.sam_energytanksmax
				end
				if (p.sam_energy > (p.sam_energytanksmax*50)+99) 
					if p.sam_hyperbeam
						if G_GametypeHasSpectators() and (p.sam_energy == (p.sam_energytanksmax*50)+100) and (players[3] or players[4] or players[5])
						p.sam_hyperbeamammo = $+2
						p.sams_hbdenid = 33
						else
						p.sam_hyperbeamammo = $+(p.sam_energy-(p.sam_energytanks*50)-99) 
						p.sams_hbdenid = 33
						end
					end
				p.sam_energy = 99+(p.sam_energytanksmax*50)
				end
				if (p.sam_hyperbeamammo > 99)
				p.sam_hyperbeamammo = 99
				end
			end

			if G_GametypeHasSpectators() and p.powers[pw_emeralds] and not s.samemwarning
				s.samemeralds = 0
				if (p.powers[pw_emeralds] & EMERALD1)
					s.samemeralds = $+1
				end
				if (p.powers[pw_emeralds] & EMERALD2)
					s.samemeralds = $+1
				end	
				if (p.powers[pw_emeralds] & EMERALD3)
					s.samemeralds = $+1
				end	
				if (p.powers[pw_emeralds] & EMERALD4)
					s.samemeralds = $+1
				end	
				if (p.powers[pw_emeralds] & EMERALD5)
					s.samemeralds = $+1
				end	
				if (p.powers[pw_emeralds] & EMERALD6)
					s.samemeralds = $+1
				end
				if (p.powers[pw_emeralds] & EMERALD7)
					s.samemeralds = $+1
				end
				if (s.samemeralds > 4)	
				s.samemwarning = 700
				S_StartSound(nil, sfx_ding)
				print("\x87"+p.name+" is collecting emeralds. Be careful.")
					for ns in players.iterate			
						ns.samhudmessagetime = 230
						ns.samhudmessage1 = "\x80"+p.name+"\x87 is collecting emeralds."
						ns.samhudmessage2 = "Be careful!"
					end
				end
			elseif All7Emeralds(emeralds) and not p.sam_hyperbeam 
			and not p.exiting
			and not p.sam_saveselect
				P_SamFP(p, PAL_MIXUP, 10)
				S_StartSound(s, sfx_spwup)
				CONS_Printf(p, "\x82You've obtained the Hyper Beam! Charge it with energy to unleash the ultimate weapon!")
				p.sam_hyperbeam = true SamusTutorial(s, "hyperbeam")
				p.sam_hyperbeamammo = 0
				p.samitemgettimer = 83
				p.samitemcolor = SKINCOLOR_CLOUDY
				p.samitemgetname = "Hyper Beam"
				p.samitemscale = 88000
			end
			if s.samemwarning and (s.samemeralds != nil and s.samemeralds < 5)
				s.samemwarning = $-1
			end
			if (All7Emeralds(p.powers[pw_emeralds]) or (G_GametypeHasSpectators() and (s.samemeralds == 7 or (p.powers[pw_emeralds] == 0) and (p.powers[pw_invulnerability] == 700 and p.powers[pw_sneakers] == 700))) )	
			and not p.exiting and not p.sam_saveselect
			p.powers[pw_emeralds] = 0
			s.samemeralds = 0
				if not p.sam_hyperbeam
				S_StartSound(nil, sfx_mtnk)
				S_StartSound(nil, sfx_s3k9f)
				p.sam_hyperbeam = true SamusTutorial(s, "hyperbeam")
				p.sam_hyperbeamammo = 0
				p.samitemgettimer = 83
				p.samitemcolor = SKINCOLOR_CLOUDY
					if p.samchallenger and (p.samchallenger > 3)
					p.samitemgetname = "True Hyper Beam"
					p.samitemscale = 102000
					else
					p.samitemgetname = "Hyper Beam"
					p.samitemscale = 88000
					end
					
					if G_GametypeHasSpectators()
						//Don't let 2.2 screw over the Hyper Beam mechanic in match....
						p.powers[pw_invulnerability] = 0
						p.powers[pw_sneakers] = 0
						P_RestoreMusic(p)
						P_StartQuake(11*FRACUNIT, 28)
						
						if p.samchallenger and (p.samchallenger > 3)
							if p.sammissilesmax != nil and p.sammissilesmax < 30				
							p.sammissilesmax = 30
							end
							if p.sammissiles != nil and p.sammissiles < 30				
							p.sammissiles = 30
							end
							p.samqchallenger = true
							print("\x85 =!!Emergency!!= The fully powered Hyper Beam has been captured! Stop "+p.name+"\x85 at all costs!")
						else
							print("\x85 =!!Emergency!!= The Hyper Beam has been captured! Stop "+p.name+"\x85 immediately!")
						end
						
						p.sam_energy = 99+(p.sam_energytanksmax*50)
						if (p.sam_energytanksmax > p.sam_energytanksstart) --Convert extra e-tanks into hyperbeam ammo
						p.sam_hyperbeamammo = 30+((p.sam_energytanksmax-p.sam_energytanksstart)*10)
						else
						p.sam_hyperbeamammo = 30
						end
						p.sam_energytanksmax = 2
						p.sam_energy = 199
						p.sam_energytanks = p.sam_energytanksmax
						
						for z in players.iterate
						z.sam_hyperbeampanic = 84
							if not z.sam_hyperbeam
								if z.mo
								DoAreaScan(z.mo, z)
								end
							
							z.samhudmessagetime = 230
							z.samhudmessage1 = "\x80"+p.name+"\x81 captured the Hyper Beam! Kill them"
							z.samhudmessage2 = "before they gather more ammo and destroy everyone!"
							else
							
								z.mo.sam_scanpowerup = 0
								z.scannedmons = nil
								z.scannedplays = nil
							
							z.samhudmessagetime = 230
							z.samhudmessage1 = "\x81"+"You are hunted!\x82 Use the Hyper Beam to wreak"
							z.samhudmessage2 = "\x82"+"as much havoc as possible before dying!"
							end
							P_SamFP(z, PAL_NUKE, 12)
							if not z.nosammusic
							SAM_COM_BufInsertText(z, "tunes -none; tunes SAMUSH")
							end
						end
						
					else
					P_SamFP(p, PAL_MIXUP, 10)
					CONS_Printf(p, "\x82You've obtained the Hyper Beam! Charge it with energy to unleash the ultimate weapon!")
					end
				elseif p.sam_hyperbeam and not p.sam_saveselect
				p.sam_hyperbeamammo = 99
				P_SamFP(p, PAL_MIXUP, 9)
				S_StartSound(s, sfx_mtnk)
				p.sam_energy = 99+(p.sam_energytanksmax*50)
				CONS_Printf(p, "\x82"+"Hyper Beam ammo maxed out!")
				end
			end
			if not p.samcheat
			or (p.samcheat < 9)
				if (p.cmd.buttons & BT_CUSTOM3) and (p.cmd.buttons & BT_CUSTOM2)
					if not netgame
					p.samspbs = true
					p.sambsbl = true
					p.sam_spiderball = true
					p.sam_highjump = true
					p.sam_bombs = true
					p.sam_spacejump = true
					p.sam_chargebeam = true
					p.sam_spazerbeam = true
					p.sam_wavebeam = true
					p.sam_icebeam = true
					p.sam_plasmabeam = true
					p.sam_powerbeamcombo = true
					p.sam_spazerbeamcombo = true
					p.sam_wavebeamcombo = true
					p.sam_icebeamcombo = true
					p.sam_plasmabeamcombo = true
					p.sam_gravitysuit = 2
					p.sam_screwattack = true
					p.sam_power_grip = true
					p.sam_gravitysuitjustgot = 75
					p.sammissiles = 100
					p.sammissilesmax = 100
					p.samsupermissiles = 10
					p.samsupermissilesmax = 10
					p.sampw = 5
					p.sampwmax = 5
					p.sam_energytanksmax = 12
					p.sam_energy = (p.sam_energytanksmax*50)-1
					p.samcheat = 30
					P_SamFP(p, PAL_MIXUP, 9)
					S_StartSound(s, sfx_mtnk)
					end
				elseif (p.cmd.buttons & BT_CUSTOM3) and not (p.cmd.buttons & BT_CUSTOM2)
					if not netgame
					p.samspbs = true
					p.sambsbl = true
					p.sam_bombs = true
					p.sam_spacejump = true
					p.sam_chargebeam = true
					p.sam_power_grip = true
					p.sam_powerbeamcombo = true
					p.sam_gravitysuit = 1
					p.sam_gravitysuitjustgot = 75
					p.sammissiles = 15
					p.sammissilesmax = 15
					p.samsupermissiles = 0
					p.samsupermissilesmax = 2
					p.sam_energytanksmax = 2
					p.sam_energy = (p.sam_energytanksmax*50)-1
					p.samcheat = 30
					P_SamFP(p, PAL_MIXUP, 9)
					S_StartSound(s, sfx_mtnk)
					end
				end
			p.samcheat = p.samcheat and $+1 or 1 --Cannot activate the cheat from here on out.
			end
			


			if p.powers[pw_automaticring] or p.powers[pw_scatterring] or p.powers[pw_bouncering]
			or p.powers[pw_explosionring] or p.powers[pw_railring] or p.powers[pw_grenadering]
				if p.sam_hyperbeam and (p.samchallenger and (p.samchallenger > 3))
				p.sammissiles = $+5*p.sam_ammodigitizer
				end
				if p.sam_hyperbeam and not (p.sam_hyperbeamammo >= 99)
				or (p.sammissiles >= p.sammissilesmax)
				P_GivePlayerRings(p, 1)
				else 
						if (players[6] or players[7] or players[8])
						p.sammissiles = $+2*p.sam_ammodigitizer
						else
						p.sammissiles = $+1*p.sam_ammodigitizer 
						end
						if p.buffbottomtier
						p.sammissiles = $+1*p.sam_ammodigitizer
						end
				end
				
				p.powers[pw_automaticring] = 0 p.powers[pw_scatterring] = 0 p.powers[pw_bouncering] = 0
				p.powers[pw_explosionring] = 0 p.powers[pw_railring] = 0 p.powers[pw_grenadering] = 0
			end
			if p.powers[pw_infinityring] or p.ringweapons
				if p.sam_hyperbeam and not (p.sam_hyperbeamammo >= 99)
				or (p.sammissiles >= p.sammissilesmax)
					P_GivePlayerRings(p, 1)
				elseif p.powers[pw_infinityring]
					p.sammissiles = $+3*p.sam_ammodigitizer
				else
					p.sammissiles = $+2*p.sam_ammodigitizer
				end
				if (p.sammissiles < 6) and (players[6] or players[7] or players[8])
				p.sammissiles = $+1*p.sam_ammodigitizer	
				end
				if p.buffbottomtier --You get extra benefits to catch up.
				
					p.sammissiles = $+2*p.sam_ammodigitizer
					
					if p.sampwmax and not P_RandomRange(0,10) and not p.nosuperpowert --Lucky dog.
						if not p.sampw
						p.sampw = 1
						end
					elseif not p.sampwmax and not P_RandomRange(0,10) and not p.nosuperpowert --Lucky dog2
		
						P_SamFP(p, PAL_MIXUP, 9)
						S_StartSound(s, sfx_spwup)
						CONS_Printf(p, "You've obtained a \x84Power Bomb!\x80 Hold C2/NormalFire as the Morph Ball, then release!")
						SamusTutorial(s, "pw")	
						p.sampwmax = $+1
						p.sampw = $+1
						NewSamPickup(s, "PTNKD0", 55355)	
						ItemGetPopup(p, 80, SKINCOLOR_RED, "Power Bombs", 88355)	
								
					elseif p.samsupermissilesmax 
					
						if not p.samsupermissiles
						p.samsupermissiles = 1
						end
						
					else --Perhaps you not having Super Missiles is the reason you've fallen this far behind.
						--Likely the player joined late.
						P_SamFP(p, PAL_MIXUP, 9)
						CONS_Printf(p, "You've obtained \x84Super Missiles!\x80 Hold FIRE until it clicks, then release!")
						S_StartSound(s, sfx_spwup)
						SamusTutorial(s, "supermissiles")
						p.samsupermissilesmax = $+2
						p.samsupermissiles = $+2
						NewSamPickup(s, "STNKD0", 55355)	
						ItemGetPopup(p, 80, SKINCOLOR_CRIMSON, "Super Missiles", 105355)
					end
				end
				p.powers[pw_infinityring] = 0
				p.ringweapons = 0
			end


			if p.powers[pw_shield] 
				if (p.powers[pw_shield] == SH_PITY)
				   if not (G_GametypeHasSpectators() and p.boxammospawns)
					local samusrng = P_RandomRange(1, 20)
					if (samusrng < 11)
						if (p.sammissilesmax < 25)
						P_SamFP(p, PAL_RECYCLE, 7)
						S_StartSound(s, sfx_mtnk)
						CONS_Printf(p, "You've obtained a bonus Missile Tank! Press FIRE to blast away!")
						p.sammissilesmax = $+5
						p.sammissiles = $+3
						p.samitemgettimer = 80
						p.samitemcolor = SKINCOLOR_CRIMSON
						p.samitemgetname = "Missiles"
						p.samitemscale = 70000
						elseif not (p.sammissiles == p.sammissilesmax)
						S_StartSound(s, sfx_mpku)
						p.sammissiles = $+2
						end
					elseif (samusrng < 12)
						if not p.samsupermissilesmax
						P_SamFP(p, PAL_RECYCLE, 9)
						S_StartSound(s, sfx_spwup)
						CONS_Printf(p, "You've obtained a bonus Super Missile Tank! Hold C2/NormalFire, then release!")
						p.samsupermissilesmax = $+2
						p.samsupermissiles = $+1
						p.samitemgettimer = 80
						p.samitemcolor = SKINCOLOR_CRIMSON
						p.samitemgetname = "Super Missiles"
						p.samitemscale = 95000
						elseif not (p.samsupermissiles == p.samsupermissilesmax)
						S_StartSound(s, sfx_mpku)
						p.samsupermissiles = $+1
						end
					elseif (samusrng < 13)
						if not (p.sampw == p.sampwmax)
						S_StartSound(s, sfx_mpku)
						p.samsupermissiles = $+1
						end
					elseif (samusrng < 14) and not p.sam_spacejump
						P_SamFP(p, PAL_RECYCLE, 9)
						CONS_Printf(p, "You've obtained a Bonus Space Jump! You can jump once more in midair.")
						S_StartSound(s, sfx_spwup)
						p.sam_spacejump = true
						p.samitemgettimer = 80
						p.samitemcolor = SKINCOLOR_GREEN
						p.samitemgetname = "Space Jump"
						p.samitemscale = 81000
					elseif (samusrng < 16) and not p.sam_spazerbeam
						if not p.sam_spazerbeam
						P_SamFP(p, PAL_RECYCLE, 9)
						CONS_Printf(p, "You've obtained a Bonus Spazer Beam! The beam widens as it travels.")
						S_StartSound(s, sfx_spwup)
						p.sam_spazerbeam = true
						p.samitemgettimer = 80
						p.samitemcolor = SKINCOLOR_BLACK
						p.samitemgetname = "Spazer Beam"
						p.samitemscale = 81000
						end
					elseif (samusrng < 17) and not p.sam_highjump
						if not p.sam_highjump
						P_SamFP(p, PAL_RECYCLE, 9)
						CONS_Printf(p, "You've obtained a Bonus Hi-Jump! You jump higher, AND unlock the spring ball!")
						S_StartSound(s, sfx_spwup)
						p.sam_highjump = true
						p.samitemgettimer = 80
						p.samitemcolor = SKINCOLOR_GREEN
						p.samitemgetname = "High Jump"
						p.samitemscale = 70000
						end
					elseif (samusrng < 19) and not p.sam_wavebeam
						if not p.sam_wavebeam
						P_SamFP(p, PAL_RECYCLE, 9)
						CONS_Printf(p, "You've obtained a Bonus Wave Beam! The tricky beam bounces once.")
						S_StartSound(s, sfx_spwup)
						p.sam_wavebeam = true
						p.samitemgettimer = 80
						p.samitemcolor = SKINCOLOR_BLACK
						p.samitemgetname = "Wave Beam"
						p.samitemscale = 77000
						end
					elseif (samusrng < 20) and not p.sam_icebeam
						if not p.sam_icebeam
						P_SamFP(p, PAL_RECYCLE, 9)
						CONS_Printf(p, "You've obtained a Bonus Ice Beam! This explosive beam freezes foes.")
						S_StartSound(s, sfx_spwup)
						p.sam_icebeam = true
						p.samitemgettimer = 80
						p.samitemcolor = SKINCOLOR_BLACK
						p.samitemgetname = "Ice Beam"
						p.samitemscale = 74000
						end
					elseif (samusrng >= 20) and not p.samspbs
						if not p.samspbs
						P_SamFP(p, PAL_RECYCLE, 9)
						CONS_Printf(p, "You've obtained a Bonus Speed Booster! Hold C1 to Dash.")
						S_StartSound(s, sfx_spwup)
						p.samspbs = true
						p.samitemgettimer = 80
						p.samitemcolor = SKINCOLOR_GREEN
						p.samitemgetname = "Speed Booster"
						p.samitemscale = 99000
						end
					end
				       end
					p.powers[pw_shield] = SH_NONE

				elseif (p.powers[pw_shield] == SH_JUMP)
					if not p.sam_spacejump
					P_SamFP(p, PAL_MIXUP, 9)
					CONS_Printf(p, "You've obtained the Space Jump! Now you can jump once more in midair.")
					S_StartSound(s, sfx_spwup)
					p.sam_spacejump = true
					p.samitemgettimer = 80
					p.samitemcolor = SKINCOLOR_GREEN
					p.samitemgetname = "Space Jump"
					p.samitemscale = 79000
					elseif not p.sam_icebeam
					P_SamFP(p, PAL_MIXUP, 9)
					CONS_Printf(p, "You've obtained the Ice Beam! This explosive beam freezes foes.")
					S_StartSound(s, sfx_spwup)
					p.sam_icebeam = true
					p.samitemgettimer = 80
					p.samitemcolor = SKINCOLOR_BLACK
					p.samitemgetname = "Ice Beam"
					p.samitemscale = 74000
					end
					p.powers[pw_shield] = SH_NONE

				elseif (p.powers[pw_shield] == SH_ELEMENTAL)
					if not p.sam_spazerbeam
					P_SamFP(p, PAL_MIXUP, 9)
					CONS_Printf(p, "You've obtained the Spazer Beam! This beam spreads as it travels")
					S_StartSound(s, sfx_spwup)
					p.sam_spazerbeam = true
					p.samitemgettimer = 80
					p.samitemcolor = SKINCOLOR_BLACK
					p.samitemgetname = "Spazer Beam"
					p.samitemscale = 83000
					end
					p.powers[pw_shield] = SH_NONE

				elseif (p.powers[pw_shield] == SH_ATTRACT)
					if not p.sam_wavebeam
					P_SamFP(p, PAL_MIXUP, 9)
					CONS_Printf(p, "You've obtained the Wave Beam! This tricky beam bounces once and paralyzes foes.")
					S_StartSound(s, sfx_spwup)
					p.sam_wavebeam = true
					end
					p.powers[pw_shield] = SH_NONE

				elseif (p.powers[pw_shield] == SH_BOMB)
					if gamemap == 13 and p.samsupermissilesmax
					p.samsupermissiles = $+1
					else
					P_SamFP(p, PAL_MIXUP, 7)
					CONS_Printf(p, "You've obtained a Super Missile Tank! Shoot them by holding the FIRE button, then release!")
					S_StartSound(s, sfx_mtnk)
					p.samsupermissilesmax = $+2
					p.samsupermissiles = $+2
					p.samitemgettimer = 80
					p.samitemcolor = SKINCOLOR_CRIMSON
					p.samitemgetname = "Super Missiles"
					p.samitemscale = 99000
					p.powers[pw_shield] = SH_NONE
					end
				elseif ((p.powers[pw_shield] == SH_FORCE) or (p.powers[pw_shield] == SH_FORCE|1))
					if not p.sam_chargebeam
					P_SamFP(p, PAL_MIXUP, 9)
					CONS_Printf(p, "You've obtained the Charge Beam! Charge your arm cannon by holding C2/NormalFire!")
					S_StartSound(s, sfx_spwup)
					p.sam_chargebeam = true
					p.samitemgettimer = 80
					p.samitemcolor = SKINCOLOR_BLACK
					p.samitemgetname = "Charge Beam"
					p.samitemscale = 82000
					elseif not p.sam_bombs
					P_SamFP(p, PAL_MIXUP, 9)
					CONS_Printf(p, "You've obtained Bombs! Drop them in Morph Ball mode with C2/NormalFire!")
					S_StartSound(s, sfx_spwup)
					p.sam_bombs = true
					p.samitemgettimer = 80
					p.samitemcolor = SKINCOLOR_BROWN
					p.samitemgetname = "Morph Bombs"
					p.samitemscale = 83000
					else
					P_SamFP(p, PAL_MIXUP, 7)
					S_StartSound(s, sfx_spwup)
					p.sammissilesmax = $+5
					p.sammissiles = $+5
					p.samitemgettimer = 80
					p.samitemcolor = SKINCOLOR_CRIMSON
					p.samitemgetname = "Missiles"
					p.samitemscale = 70000
					end
					p.powers[pw_shield] = SH_NONE

				elseif (p.powers[pw_shield] & SH_FIREFLOWER)
					if not p.sam_plasmabeam
					P_SamFP(p, PAL_MIXUP, 9)
					CONS_Printf(p, "You've obtained the Plasma Beam! This powerful mid-range beam pierces enemies!")
					S_StartSound(s, sfx_spwup)
					p.sam_plasmabeam = true
					p.samitemgettimer = 80
					p.samitemcolor = SKINCOLOR_BLACK
					p.samitemgetname = "Plasma Beam"
					p.samitemscale = 84000

					elseif not p.samsupermissilesmax
						P_SamFP(p, PAL_MIXUP, 9)
						S_StartSound(s, sfx_spwup)
						CONS_Printf(p, "You've obtained Super Missiles! Hold FIRE, then release!")
						p.samsupermissilesmax = $+2
						p.samsupermissiles = $+2
						p.samitemgettimer = 80
						p.samitemcolor = SKINCOLOR_CRIMSON
						p.samitemgetname = "Super Missiles"
						p.samitemscale = 99000
					elseif not p.sam_bombs
					P_SamFP(p, PAL_MIXUP, 9)
					CONS_Printf(p, "You've obtained Bombs! Drop them in Morph Ball mode with C2/NormalFire!")
					S_StartSound(s, sfx_spwup)
					p.sam_bombs = true
						p.samitemgettimer = 80
						p.samitemcolor = SKINCOLOR_BROWN
						p.samitemgetname = "Morph Bombs"
						p.samitemscale = 81000
					else
						P_SamFP(p, PAL_MIXUP, 7)
						S_StartSound(s, sfx_mtnk)
						p.samsupermissilesmax = $+2
						p.samsupermissiles = $+2
						p.samitemgettimer = 80
						p.samitemcolor = SKINCOLOR_CRIMSON
						p.samitemgetname = "Super Missiles"
						p.samitemscale = 99000

					end
					s.color = p.skincolor
					p.powers[pw_shield] = SH_NONE
				else
					p.powers[pw_shield] = SH_NONE
				end
			end
			if (s.eflags & MFE_UNDERWATER)
				p.powers[pw_underwater] = 990
			end
			if not p.samushasatimer 
				p.powers[pw_spacetime] = 0
			end
			if (s.state == S_PLAY_PAIN) and not (p.pflags & PF_SLIDING)
				p.sams_shotcd = 2
				p.sams_mshotcd = 2
				p.sam_shinespark = 0
				p.sam_shinesparkfly = 0
				p.sam_shinesparkstop = 0
			end

			if not p.sammorphed and ((s.state == S_PLAY_SPINDASH) or (((p.powers[pw_carry] == CR_ZOOMTUBE) or (p.powers[pw_carry] == CR_MACESPIN)) and not p.sam_screwattacking))
			and not p.sams_notyet and not s.samslopespin and not (s.sprite == SPR_ZSSA)		
				SamMorphSwitch(p, s)
				S_StartSound(s, sfx_smorph)
				CancelScrewAttack(s,p)
			elseif s.samslopespin and (p.pflags & PF_SPINNING)
				p.pflags = $ & ~PF_SPINNING
				if p.panim == PA_ROLL
					s.state = S_PLAY_FALL
				end
			end

			if (p.cmd.buttons & BT_USE) and not p.sammorphed
			and not ((p.sams_usecd or 1) > 1) and not p.sam_shipready 
			and not p.sams_beamcombocharge and not s.samhudgimmick and not (s.sprite == SPR_ZSSA)
			
				if (P_IsObjectOnGround(s)
				or (p.pflags & PF_JUMPED)
				or ((p.panim == PA_WALK or p.panim == PA_RUN) and not P_IsObjectOnGround(s))
				or ((s.state == S_PLAY_SPRING or s.state == S_PLAY_FALL) and not P_IsObjectOnGround(s) and not (p.pflags & PF_JUMPED)) )
				
				and not p.sams_morphcd and not p.exiting
				and not ((p.powers[pw_carry] or p.powers[pw_nocontrol]) and (p.powers[pw_carry] != CR_BRAKGOOP)) --You CAN morph in Brak goo.
				and (s.scale == 90000) --Sorry, this gets too complicated with varying sizes.
				
					SamMorphSwitch(p, s)
					S_StartSound(s, sfx_smorph)
					
					--This is the manual morph ball switch, so add more stuff!
					s.morphtrailwait = 7					
					
					p.samspbschargefull = false
					p.samspbscharge = 0
					p.sams_morphcd = 35
					s.momx = ($*5)/6
					s.momy = ($*5)/6
					s.momz = ($*5)/9
					p.pflags = $ & ~ PF_STARTJUMP
					p.sams_c2cd = 0
					s.scale = 42001	
					
					if (s.state == S_PLAY_SPRING)
					and not P_IsObjectOnGround(s)
					and not (p.pflags & PF_JUMPED)
						if (s.momz*P_MobjFlip(s) > 0)
							s.momz = $*4/5
						end
					end	
					if s.metroidsnack --Don't escape TOO easily
						s.momx = $/5
						s.momy = $/5
					end
					
					--Multibomb technique if morphing with a charged beam!
					if (p.sams_chargetime > 41) and p.sam_bombs
						S_StartSound(s, sfx_smobo)
						local ghs = P_SpawnMobj(s.x, s.y, s.z+s.height/2, MT_PBEXPAND)
						ghs.scale = s.scale*2
						ghs.tics = $+3
						ghs.scalespeed = $*4 ghs.destscale = FRACUNIT*99
						ghs.color = SKINCOLOR_SUPERGOLD2
						ghs.colorized = true
						ghs.blendmode = AST_ADD
						p.camerascale = 196071
						s.thesamcam = 1
						for d = 1,8 --Even I couldn't stand the old code for this one...
							ghs = P_SpawnMobj(s.x+FixedMul(-140*FRACUNIT, cos(s.angle+(ANGLE_45*d))), s.y+FixedMul(-140*FRACUNIT, sin (s.angle+(ANGLE_45*d))), s.z+FixedMul(FRACUNIT*20, (P_MobjFlip(s)*s.scale)), MT_SAMMORPHBOMB)
							if ghs and ghs.valid
							ghs.flags = $|MF_GRENADEBOUNCE|MF_BOUNCE & ~MF_NOGRAVITY
							ghs.target = s
							ghs.momz = $-3*FRACUNIT+(s.momz/2)
							ghs.momx = $+(s.momx/2) ghs.momy = $+(s.momy/2)
							P_Thrust(ghs, s.angle+(ANGLE_45*d), -6*FRACUNIT)
								if (ghs.floorz > s.floorz+140*FRACUNIT) --Oh, geez, don't clip up there.
									ghs.state = S_NULL
								end
							end
							
							ghs = P_SpawnMobj(s.x+FixedMul(-80*FRACUNIT, cos(ANGLE_45*d)), s.y+FixedMul(-80*FRACUNIT, sin (ANGLE_45*d)), s.z+FixedMul(FRACUNIT*20, (P_MobjFlip(s)*s.scale)), MT_THOK)
							ghs.sprite = SPR_MBOM	
							ghs.frame = A|TR_TRANS60|FF_FULLBRIGHT
							ghs.destscale = FRACUNIT*99
						end
					end
					
				elseif not p.sam_screwattacking and not p.sam_shinesparkfly
					S_StartSound(s, sfx_sdenid)
				end
			end
			if (p.pflags & PF_ANALOGMODE) and P_IsObjectOnGround(s)
				s.wallnopejump = 2
			end
			
--========================
--MAIN MORPH BALL CODE
--========================

			if p.sammorphed --if p.sam_morphed is wrong.
				
				p.powers[pw_noautobrake] = 2 --No autobrake with morphball. Preferably none in general, but enjoy your bad taste...
				if p.sam_screwattacking
					CancelScrewAttack(s,p)
				end
				
				if s.sam_automob --Cancel lock on
					p.aiming = 0
					s.sam_automob = nil
					
					if (p.automobf and p.automobf.valid)
						p.automobf.color = SKINCOLOR_SILVER
						p.automobf.destscale = $*10
						p.automobf.scalespeed = $*2
						p.automobf.tics = 8
						p.automobf.cancellock = true
						p.automobf.frame = (Z+1)|TR_TRANS80|FF_FULLBRIGHT
						p.automobf = nil
					end
					s.sam_faillock = false
					s.sam_lockedon = false
					s.sam_lockseek = 0
					s.sam_sighttime = 0
					s.sam_lockclosest = nil
					s.sam_lockchosen = nil
				end

				--Do the rolling morphtrail
					
				if ( ((p.speed >= 40*s.scale) and P_IsObjectOnGround(s))
				or (p.speed > 74*s.scale)
				or (s.momz > 12*s.scale) or (s.momz < -12*s.scale)
				or ((p.pflags & PF_STARTJUMP) and (s.momz*P_MobjFlip(s) > 0)) )
				and not (p.powers[pw_carry] and p.powers[pw_carry] != CR_ZOOMTUBE)
					if s.prevz2 == nil
						s.prevz2 = s.z
					end
					if (p.sam_gravitysuit == 2)
						SamMorphTrail(p, s, p.samvisorcolor)
					else
						SamMorphTrail(p, s, SKINCOLOR_EMERALD)
					end
					s.prevz2 = s.z
					s.morphtrailwait = s.morphtrailwait and $-1 or 0
				else
					s.morphtrailwait = 2
				end
					
				if (p.pflags & (PF_STARTDASH|PF_SPINNING))
				and not s.samspinpad and not p.sugoigolfmode
				and not (p.charability2 == CA2_SPINDASH)
					p.pflags = $ & ~PF_STARTDASH & ~PF_SPINNING
				end
				if (p.gotflag or not p.sam_highjump)
				and not p.powers[pw_carry]
					p.pflags = $|PF_JUMPDOWN
				end

				p.sampers = 2 --Adaptive multiplier to equal your speed in morph ball, since you're scaled around half size.
				
				if (s.sam_forcemorphslopeattach or s.sam_forcemorphslope)
				and not ((p.pflags & PF_JUMPED) or (p.cmd.buttons & BT_JUMP)) --waluigi
				and not (s.eflags & MFE_VERTICALFLIP)
					if not P_IsObjectOnGround(s)
						if p.powers[pw_justlaunched] and (s.momz > FRACUNIT) and s.sam_forcemorphslopeattach
							s.sam_mang = nil
							s.sam_mspd = nil
							s.sam_forcemorphslopeattach = 0
							s.sam_forcemorphslope = 0
							if not p.bsblboost
								s.momx = $*11/12
								s.momy = $*11/12
							end
							s.momz = $*3/4						
						elseif not (s.momz > 0)
						and not ((s.z-(p.speed+(15*FRACUNIT))) > s.floorz) --The game's trying to kick us off. Force us back on.
							s.z = s.floorz 
							P_SetObjectMomZ(s, -2*FRACUNIT, true)
						end
					else
						if s.sam_forcemorphslopeattach --Kill SRB2's attempts to glitch us.
						and s.sam_mspd and (p.speed < s.sam_mspd)
						and s.sam_mang != nil
						and not p.bsblboost
							s.z = s.floorz+4*FRACUNIT
							P_InstaThrust(s, s.sam_mang, s.sam_mspd)
						end
					end
					--Remember what we were doing
					if (p.speed > 33*FRACUNIT)
					and s.standingslope and not (s.standingslope.flags & SL_NOPHYSICS)
						s.sam_mang = R_PointToAngle2(s.x, s.y, s.x+s.momx, s.y+s.momy)
						s.sam_mspd = p.speed
					else
						s.sam_mang = nil
						s.sam_mspd = nil					
					end				
				end
				if (p.pflags & PF_SPINNING)
					if not (p.pflags & PF_STARTDASH) --Break stuff.`
					P_SpawnPlayerMissile(s, MT_SAMMONBREAKER)
						if s.minormorphboost 
							if s.minormorphboost != 1
								s.momx = $*2
								s.momy = $*2
								P_InstaThrust(s, s.angle, s.minormorphboost) --Go really, really fast.
							else
								s.momx = $*5/4
								s.momy = $*5/4
							end
						s.minormorphboost = 0
						end
					end
				else
					PrimeRunPhysics(s, p)
				end

//Make a little smokecloud and bounce when dropping from a height

				if P_IsObjectOnGround(s)
					//Specially designed to raise/lower spin-only platforms.
					if not p.speed --Boostball chargeup on the spot, kind of like a spindash
					and ( (((p.cmd.buttons & BT_FIRENORMAL) and p.swapfire) or ((p.cmd.buttons & BT_ATTACK) and not p.swapfire)) or (p.cmd.buttons & BT_CUSTOM1) and not p.c1c2disable )
					and not (p.cmd.forwardmove or p.cmd.sidemove)
						p.pflags = $|PF_STARTDASH|PF_SPINNING
						p.speed = 32*FRACUNIT
						p.drawangle = s.angle
					end
					if s.sam_falldmg
						s.sam_falldmg = 0 --Stop, pls.
					end
					if s.bombjump and (s.bombjump != 28)
						s.bombjump = 0
					end
					if (s.eflags & MFE_JUSTHITFLOOR) and s.morphbo
					and ( ((s.morphbo > 8*FRACUNIT) and (P_MobjFlip(s) == -1)) or ((s.morphbo < -8*FRACUNIT) and (P_MobjFlip(s) == 1)) ) 
					and not p.bsblboost and not (P_PlayerTouchingSectorSpecial(p, 3, 6) or P_PlayerTouchingSectorSpecial(p, 3, 5))
					and not (s.morphedmachactive and s.smmachbal)
						if ( s.standingslope and ( (s.standingslope.zdelta > 6000) or (s.standingslope.zdelta < -6000) ) )
							P_Thrust(s, s.standingslope.xydirection, (((s.morphbo/FRACUNIT)*2)*P_MobjFlip(s)*-1)*(s.standingslope.zdelta)*(P_MobjFlip(s)))
							p.pflags = $|PF_SPINNING
							s.samslopespin = (((s.morphbo/FRACUNIT)/2)*P_MobjFlip(s))*-1
						else
							if (s.eflags & MFE_UNDERWATER)
								s.momz = (s.morphbo*-1)/12
							else
								s.momz = (s.morphbo*-1)/6
								p.dragdownball = 17
							end
						end

						s.morphbo = 0
						S_StartSound(s, sfx_sballf)
						
						local ghs = P_SpawnMobj(s.x-40*FRACUNIT, s.y+40*FRACUNIT, s.z, MT_PARTICLE)
							ghs.fuse = 10
							ghs.momz = (P_RandomRange(1, 10))*25000
						ghs = P_SpawnMobj(s.x+40*FRACUNIT, s.y-40*FRACUNIT, s.z, MT_PARTICLE)
							ghs.fuse = 10
							ghs.momz = (P_RandomRange(1, 10))*25000
						ghs = P_SpawnMobj(s.x-40*FRACUNIT, s.y-40*FRACUNIT, s.z, MT_PARTICLE)
							ghs.fuse = 10
							ghs.momz = (P_RandomRange(1, 10))*25000
						ghs = P_SpawnMobj(s.x+40*FRACUNIT, s.y+40*FRACUNIT, s.z, MT_PARTICLE)
							ghs.fuse = 10
							ghs.momz = (P_RandomRange(1, 10))*25000
						
					end

					if S_SoundPlaying(s, sfx_sballr)
						if s.rpsnd == nil
							s.rpsnd = 0
						end
						if (s.rpsnd > 7) and (p.speed > 20*FRACUNIT)
							S_StartSound(s, sfx_sballr)
							s.rpsnd = 0
						else
							s.rpsnd = $+1
						end

					elseif (p.speed > 20*FRACUNIT)
						S_StartSound(s, sfx_sballr)
					end
					
				else --if NOT on p_objectonground
					s.morphbo = s.momz
					
					if (p.pflags & PF_SPINNING) and not p.powers[pw_nocontrol]
						P_SetObjectMomZ(s, 5000, true)
					end
					if s.bombjump
						if (s.bombjump > 25)
						P_SetObjectMomZ(s, -12000, true)
							if (s.bombjump == 28) and s.bombjumpangle != nil and s.bombjumpdist != nil
								if (s.z <= s.floorz+20*FRACUNIT)
									P_Thrust(s, s.bombjumpangle, s.bombjumpdist/4)
								else
									P_Thrust(s, s.bombjumpangle, s.bombjumpdist/6) 
								end
							end
							local ghs = P_SpawnGhostMobj(s)
							ghs.blendmode = AST_ADD
							ghs.destscale = ghs.scalespeed+1
						elseif (s.bombjump > 21)
							P_SetObjectMomZ(s, -165000, true)
						elseif (s.bombjump > 4)
							P_SetObjectMomZ(s, -4000, true)
						else
							p.pflags = $|PF_JUMPED
							P_SetObjectMomZ(s, -9000, true)
						end
						if s.powerbombjump
						s.powerbombjump = 0
							if p.sammorphed --Do a superjump!
								s.momx = $*2
								s.momy = $*2
								s.momz = $*10/6
							end
						end
						s.bombjump = $-1
					elseif s.rpsnd
						s.rpsnd = $+1
					end
				end
				
				if p.sammorphanimcd != nil
					if (p.sammorphanimcd <= 0)
						p.sammorphanimcd = 32
					end
					if s.bombjump
						p.sammorphanimcd = $-5
					elseif (p.speed < FRACUNIT)
						--this is fine.
					elseif (p.speed < 3*FRACUNIT)
						p.sammorphanimcd = $-1
					elseif (p.speed < 10*FRACUNIT)
						p.sammorphanimcd = $-2
					elseif (p.speed < 15*FRACUNIT)
						p.sammorphanimcd = $-3
					elseif (p.speed < 20*FRACUNIT)
						p.sammorphanimcd = $-4
					elseif (p.speed < 25*FRACUNIT)
						p.sammorphanimcd = $-5
					elseif (p.speed < 30*FRACUNIT)
						p.sammorphanimcd = $-6
					elseif (p.speed < 35*FRACUNIT)
						p.sammorphanimcd = $-7
					elseif (p.speed >= 35*FRACUNIT)
						p.sammorphanimcd = $-8
					end
				end

				if (s.state >= S_SAMUMB1) and (s.state <= S_SAMUMB4)
					p.morphanim = true
				else
					p.morphanim = false
				end
				
				if P_IsObjectOnGround(s) and not ((p.sam_spiderball and not p.sam_highjump) and p.sams_jumpcd)
				or s.bombjump and s.bombjump > 8 and s.bombjump < 24
				or p.dragdownball and (p.dragdownball > 24)
				or p.thrustfactor == 20
					if not p.sam_spiderball and not (s.samspinpad and s.samspinpad > 35)
						s.flags = $|MF_BOUNCE
					end
					if not s.sam_mbspeedf
					s.friction = 61777
					end
					if (s.bombjump and s.bombjump > 8 and s.bombjump < 24) or (p.dragdownball and (p.dragdownball > 24) and not (p.dragdownball < 25) and (p.pflags & PF_STARTJUMP) and (p.speed < 13*FRACUNIT))
						p.thrustfactor = 20
					elseif (p.sam_gravitysuit and p.sam_gravitysuit == 2) and (s.eflags & MFE_UNDERWATER)
						p.normalspeed = 117*FRACUNIT
						p.acceleration = 46
					else
						p.normalspeed = 58*FRACUNIT
						p.acceleration = 41
					end
					if not (s.bombjump and s.bombjump > 8 and s.bombjump < 24)
					and not (p.dragdownball and (p.dragdownball > 24) and (p.pflags & PF_STARTJUMP))
						if (s.eflags & MFE_UNDERWATER)
							p.thrustfactor = 4
						else
							p.thrustfactor = 3
						end
					end
				else
				s.flags = $ & ~MF_BOUNCE
					if not s.sam_mbspeedf
					s.friction = 59392
					end
					if (p.sam_gravitysuit and p.sam_gravitysuit == 2) and (s.eflags & MFE_UNDERWATER)
					p.normalspeed = 40*FRACUNIT
					p.acceleration = 50
					else
					p.normalspeed = 18*FRACUNIT
					p.acceleration = 32
					end
					if not p.morphanim
					p.morphanim = true
					s.state = S_SAMUMB1
					end
					if (p.pflags & PF_JUMPED) and (p.speed > 16*FRACUNIT)
					s.momx = FixedDiv(FixedMul($, 100), 101)
					s.momy = FixedDiv(FixedMul($, 100), 101)
					end
				end

				if (p.powers[pw_carry] == CR_MACESPIN)
					p.sams_c1cd = 0
					p.sams_shotcd = 5
					p.sambsblchargefull = false
				end

				if (p.speed < FRACUNIT)
					s.tics = $+1
				end

				if (p.sams_normfcdjlg or p.sams_c2cdjlg) and not (p.sams_morphbombcd and (p.sams_morphbombcd > 70))
				and not p.sams_shotcd and not p.powers[pw_nocontrol] and not p.exiting
				or s.waveoverbomb
					if not p.sams_powerbombready
							if p.sam_bombs
							p.sams_shotcd = 3
								if not s.crystalflashtime
								p.sams_morphbombcd = $+31
								end
							s.waveoverbomb = false
							s.mbb = P_SpawnPlayerMissile(s, MT_SAMMORPHBOMB)
							SamusTutorial(s, "bombs", true)
							if s.mbb
							P_TryMove (s.mbb, s.x+FixedMul(-35355, cos(s.angle)), s.y+FixedMul(-35355, sin (s.angle)), true)
							end
							s.sambsblchargelvl = 0
							p.sams_firecd = 10
							p.sams_c1cd = 10
							S_StartSound(s, sfx_mbbmb)
							else
							S_StartSound(s, sfx_sdenid)
							end

					elseif p.sampw and (p.sampw >= 1*p.samadrain) and not p.sams_morphcd 
						p.sams_shotcd = 20
						p.sampw = $-1*p.samadrain
						s.sambsblchargelvl = 0
						p.sams_powerbombready = 0
						SamusTutorial(s, "pw", true)
						P_SpawnPlayerMissile(s, MT_SAMMBOP, MF2_SUPERFIRE)
						S_StartSound(s, sfx_mbop)
					end
				end

				if ( (s.scale != 42001) or (p.powers[pw_carry] and not p.powers[pw_carry] == CR_MACESPIN)	) and not (leveltime < 4 and s.samspinpad)
				and not p.sams_notyet
					p.sammorphed = false
					p.sams_morphcd = 35
					p.morphanim = false
					p.normalspeed = skins[s.skin].normalspeed
					p.acceleration = skins[s.skin].acceleration
					if p.sam_firstperson
						p.visorclosein = 15
						SamCam(p, false)
					end
					s.friction = 59392
					S_StartSound(s, sfx_morphb)
					s.flags = $ & ~MF_BOUNCE
					if P_IsObjectOnGround(s)
						s.state = S_PLAY_RUN
					else
						s.state = S_PLAY_FALL
					end
				end

				if not (( (p.panim == PA_ROLL) and not (p.sam_walljumping or p.sam_screwattacking) ) or p.morphanim)
				and not p.sams_notyet
					s.state = S_SAMUMB1
				end

				if (((p.cmd.buttons & BT_USE) and not (p.sams_usecd and (p.sams_usecd > 1)))
				or (s.valid and not s.health) or (p.exiting and not s.exitedmorph) or (leveltime == 1))
					if not p.sams_morphcd and not s.spiderballing and not (p.powers[pw_carry] == CR_MACESPIN)
						if not (s.ceilingz < s.z+((66*FRACUNIT)*(P_MobjFlip(s))) )
						and not (p.powers[pw_carry] == CR_ZOOMTUBE)
							p.morphanim = false
							MorphballReturn(s)
						else
							S_StartSound(s, sfx_sdenid)
						end
						if p.exiting
							s.exitedmorph = true
						end
					end
				end
				if s.samslopespin
				s.samslopespin = $-1
					if P_IsObjectOnGround(s)
						p.pflags = $|PF_SPINNING
						P_SpawnPlayerMissile(s, MT_SAMMONBREAKER)
					end
					if (p.speed > 35*FRACUNIT) and ( s.standingslope == nil or ( s.standingslope and ( (s.standingslope.zdelta < 3000) and (s.standingslope.zdelta > -3000) ) ) )
						s.momx = $*80/81
						s.momy = $*80/81
					end
					if ( s.samslopespin == 1 or (p.speed < 35*FRACUNIT) and (p.cmd.forwardmove or p.cmd.sidemove) ) and not s.samspinpad 
						s.samslopespin = 0
						p.pflags = $ & ~PF_SPINNING
					end
				end				
				if s.standingslope and not (s.standingslope.flags & SL_NOPHYSICS)
					if not s.samspinpad
						if s.prevsz == nil
							s.prevsz = s.z
						end
						if s.prevssp == nil
							s.prevssp = p.speed
						end
						s.ansl = AngleFixed((s.standingslope.xydirection)-s.angle) 


						if ( ((s.z < s.prevsz-233333) and not (s.eflags & MFE_VERTICALFLIP)) or ((s.eflags & MFE_VERTICALFLIP) and (s.z > s.prevsz+233333)) )
						and ( (s.standingslope.zdelta > 7000) or (s.standingslope.zdelta < -7000) )
							if (p.speed > 33*FRACUNIT)
							p.pflags = $|PF_SPINNING
							s.samslopespin = ((p.speed/FRACUNIT)/3)
								if s.sam_forcemorphslope
								s.sam_forcemorphslopeattach = 5
								s.sam_forcemorphslope = $+1
								end
							end
						end
						if not (p.pflags & PF_SPINNING) and not p.sam_spiderball
							if (p.speed > 17*FRACUNIT) 
							P_Thrust(s, s.standingslope.xydirection, 3*(s.standingslope.zdelta)*(P_MobjFlip(s)) )
							elseif (p.speed < 6*FRACUNIT)
							P_Thrust(s, s.standingslope.xydirection, 1*(s.standingslope.zdelta)*(P_MobjFlip(s)))
							else
							P_Thrust(s, s.standingslope.xydirection, 2*(s.standingslope.zdelta)*(P_MobjFlip(s))) 
							end
						end
						if (p.speed < 9*FRACUNIT) and (s.prevssp and (s.prevssp > 21*FRACUNIT))
						and ( (s.standingslope.zdelta > 30000) or (s.standingslope.zdelta < -30000) ) and not (p.cmd.forwardmove < 0)
						s.z = s.z+((10*FRACUNIT)*(P_MobjFlip(s)))
							if (s.prevssp > 69*FRACUNIT)
							P_SetObjectMomZ(s, (s.prevssp*10/16), true)
							P_Thrust(s, s.standingslope.xydirection, (s.prevssp/2))
							elseif (s.prevssp > 49*FRACUNIT)
							P_SetObjectMomZ(s, (s.prevssp/2), true)
							P_Thrust(s, s.standingslope.xydirection, (s.prevssp*10/13))
							else
							P_SetObjectMomZ(s, (s.prevssp*10/13), true)
							P_Thrust(s, s.standingslope.xydirection, (s.prevssp*10/11))
							end
							p.bsblboost = 0
							p.bsblboostattack = 0

						end
					end
					s.prevssp = p.speed
					s.prevsz = s.z
				elseif s.prevsz
					s.prevsz = nil
					s.prevssp = nil
				end
				if p.powers[pw_carry]
					p.sam_framejump = 0
					if s.discomsgprep and (p.powers[pw_carry] == CR_ZOOMTUBE)
						if s.tracer and s.tracer.valid
						and (s.tracer.type == MT_TUBEWAYPOINT)
						and (s.tracer.y == 675282944)
							s.discomsgprep = false
							SamusDialogue(s, "What.", nil, "SAMDEF", 2, 0, 200)
						end
					elseif (p.powers[pw_carry] == CR_MINECART)
					or (p.powers[pw_carry] == CR_ROLLOUT)
					or (p.powers[pw_carry] == CR_PLAYER)
					or (p.powers[pw_carry] == CR_GENERIC)
					or (p.powers[pw_carry] == CR_ROPEHANG)
						p.panim = PA_WALK
						p.sams_notyet = 3
						s.state = S_PLAY_STND
						MorphballReturn(s)
					end
				end
				s.sam_mbspeedf = nil
			else --if not p.sammorphed
				if ((p.sam_powerbeamequip and p.sam_powerbeammeltdown) or (p.sam_spazerbeamequip and p.sam_spazerbeammeltdown) or (p.sam_wavebeamequip and p.sam_wavebeammeltdown)
				or (p.sam_icebeamequip and p.sam_icebeammeltdown) or (p.sam_plasmabeamequip and p.sam_plasmabeammeltdown))
				p.sams_shotcd = 3
				p.sams_mshotcd = 3
				local ghs
					if (s.eflags & MFE_VERTICALFLIP)
					ghs = P_SpawnMobj(s.x+FixedMul(s.radius*5/2, cos(s.angle-(ANG2*2))), s.y+FixedMul(s.radius*5/2, sin(s.angle-(ANG2*2))), s.z+(s.height*4/11), MT_SAMCHARGEGFX)
					else
					ghs = P_SpawnMobj(s.x+FixedMul(s.radius*5/2, cos(s.angle-(ANG2*2))), s.y+FixedMul(s.radius*5/2, sin(s.angle-(ANG2*2))), s.z+(s.height*2/3), MT_SAMCHARGEGFX)
					end
					ghs.z = $+(FixedMul(FixedMul(50*FRACUNIT, s.scale), sin(p.aiming)))
					ghs.scale = s.scale/(P_RandomRange(10,20))
					ghs.color = SKINCOLOR_BLACK
					ghs.momx = s.momx/2
					ghs.momy = s.momy/2
					ghs.eflags = s.eflags
					ghs.target = s
				end		
			

				if (p.speed < 12*FRACUNIT)
				p.samboblenght = 3*FRACUNIT
				p.samboblenght2 = 7*FRACUNIT
				--Scan Visor feature
					if (p.sam_scanvisor or (gametype != GT_COOP))
					and (p.speed <= 0 and ((s.state <= S_PLAY_WAIT) or (s.state == S_PLAY_EDGE))) //and p.sam_scanvisor
					and not p.sammorphed and not p.sam_intoship and P_IsObjectOnGround(s)
					and not (p.pflags & PF_FORCESTRAFE) and not p.powers[pw_nocontrol] and not p.bot
					and not s.samscananim and not (gametype == GT_HIDEANDSEEK) and not (s.sam_automob and s.sam_automob.valid)
						if not (s.sam_scanpowerup and (s.waittoscan != nil))
						and not p.sam_noautoscan
						s.waittoscan = s.waittoscan and $+1 or 1
						end
						if (s.waittoscan == nil or (s.waittoscan < 500))
						and ((p.cmd.buttons & BT_TOSSFLAG)
						or ((p.cmd.buttons & BT_CUSTOM1) and not p.sam_noautoscan or (p.cmd.buttons & BT_CUSTOM1) and p.sam_noautoscan == 1)
						and not (p.sams_c1cd and p.sams_c1cd > 2))
							s.waittoscan = 500
							s.samusjustscanned = false
						elseif s.samusjustscanned
							s.waittoscan = 0
							if p.cmd.forwardmove or p.cmd.sidemove or (p.cmd.buttons & BT_JUMP) or (p.cmd.buttons & BT_SPIN)
							s.samusjustscanned = false
							end
						elseif s.waittoscan and (s.waittoscan > 230)				
							s.sam_scanning = s.sam_scanning and $+1 or 1
							s.sam_scanang = s.sam_scanang and $+(ANG1*4) or (ANG1*4)
							
							if leveltime % 9 == 0
								local ghs = P_SpawnMobjFromMobj(s,0,0,(s.height*5/6), MT_THOK) --waa1
								ghs.sprite = SPR_MXP4
								ghs.frame = X|FF_FULLBRIGHT|TR_TRANS50
								ghs.blendmode = AST_ADD
								if p.samvisorcolor != nil and (p.sam_gravitysuit and p.sam_gravitysuit == 2)
									ghs.color = p.samvisorcolor
									ghs.colorized = true
								end
								ghs.scale = s.scale/18
								ghs.momz = (-4*FRACUNIT)*P_MobjFlip(ghs)
								ghs.tics = 30
								ghs.fuse = 10
								ghs.scalespeed = $*2
								ghs.destscale = $*99
								P_InstaThrust(ghs, s.angle, 7*FRACUNIT)
								if SAM_displayplayer(p) and not CV_FindVar("chasecam").value
									ghs.flags2 = $|MF2_DONTDRAW
								end
							end
							
							if s.sam_scanning == 1 or s.sam_scanning == 55
								S_StartSound(s, sfx_sscan1)
								SamusTutorial(s, "scanvisor", true)
								
							elseif (s.sam_scanning > 75)
								if p.sam_hyperbeam and p.sam_hyperbeampanic
								
								s.sam_scanpowerup = 0
								p.scannedmons = nil
								p.scannedplays = nil
								s.sam_scanning = 0
								s.waittoscan = 0
								s.samusjustscanned = true
								S_StopSoundByID(s, sfx_sscan1)
								S_StartSound(s, sfx_cmol)
								s.samusmission = nil
								SamusMission(s, "   ENERGY OVERLOAD!", "SCANNING IMPOSSIBLE!", nil, 3, 0, false)
								
								else
								DoAreaScan(s, p, true)
								end
							end
						end
						if not s.sam_scanning
							s.sam_scansymbol = s.sam_scansymbol and $+2 or 2
							if (s.sam_scansymbol > 49)
							s.sam_scansymbol = 49
							end
						end
					elseif s.waittoscan
					s.sam_scanning = 0
					s.sam_scansymbol = 0
					s.waittoscan = 0
					S_StopSoundByID(s, sfx_sscan1)
					end
				elseif (p.speed < 25*FRACUNIT) or ((p.samspbscharge > 70) and (p.samspbscharge < 138))
					p.samboblenght = 7*FRACUNIT
					p.samboblenght2 = 8*FRACUNIT
				elseif p.samspbschargefull
					p.samboblenght = 3*FRACUNIT
					p.samboblenght2 = 3*FRACUNIT
				else
					p.samboblenght = 3*FRACUNIT
					p.samboblenght2 = 4*FRACUNIT
				end
				--More beam code HUD stuff.
				if ((p.speed < FRACUNIT) or not P_IsObjectOnGround(s))
				and not (p.sam_screwattacking or s.sam_ldclmb or p.sam_shinesparkfly or p.beampain or (p.sam_walljumping and p.sam_walljumping >= 1) or p.sams_beamcombocharge)
					if ((p.samxbeam >= -110000) and (p.samxbeam <= 110000))
					p.samxbeam = 0 
						if p.samxbeamleft
						p.samxbeamleft = false
						else
						p.samxbeamleft = true
						end
					end
					if (p.samxbeam < -100000)
					p.samxbeam = $+100000
					end
					if (p.samxbeam > 100000)
					p.samxbeam = $-100000
					end
					if (p.samybeam <= 200000)
					p.samybeam = 0 
					end
					if (p.samybeam > 100000)
					p.samybeam = $-100000
					end
				else
					if p.sam_screwattacking or p.sam_shinesparkfly or p.sam_walljumping or p.beampain
						if p.sam_screwattacking or p.sam_shinesparkfly
						P_SpawnPlayerMissile(s, MT_SAMMONBREAKER)
						end
					elseif p.samxbeamleft
						if (p.samxbeam > -(p.samboblenght))
							if ((p.samspbscharge > 10) and (p.samspbscharge < 138))
							p.samxbeam = $-50000-(p.samspbscharge*440)
							elseif p.samspbschargefull
							p.samxbeam = $-75000
							else
							p.samxbeam = $-50000
							end
						elseif ((p.samxbeam > -(p.samboblenght2)) and (p.samxbeam <= -(p.samboblenght)))
							if ((p.samspbscharge > 10) and (p.samspbscharge < 138))
							p.samxbeam = $-25000-(p.samspbscharge*440)
							elseif p.samspbschargefull
							p.samxbeam = $-37500
							else
							p.samxbeam = $-25000
							end
						else
						p.samxbeamleft = false
						end
					else
						if (p.samxbeam < (p.samboblenght))
							if ((p.samspbscharge > 10) and (p.samspbscharge < 138))
							p.samxbeam = $+50000+(p.samspbscharge*440)
							elseif p.samspbschargefull
							p.samxbeam = $+75000
							else
							p.samxbeam = $+50000

							end
						elseif ((p.samxbeam < (p.samboblenght2)) and (p.samxbeam >= (p.samboblenght)))
							if ((p.samspbscharge > 10) and (p.samspbscharge < 138))
							p.samxbeam = $+25000+(p.samspbscharge*440)
							elseif p.samspbschargefull
							p.samxbeam = $+37500
							else
							p.samxbeam = $+25000

							end
						else
						p.samxbeamleft = true
						end
					end

					if (p.sam_screwattacking or p.sam_shinesparkfly or p.sam_walljumping or p.beampain)
						p.samybeamdest = 1200000
						if p.sam_spacejumpedboost and p.sam_walljumping
							if (p.sam_spacejumpedboost > 4)
							p.samxbeam = $-80000
							elseif (p.sam_spacejumpedboost > 1)
							p.samxbeam = $-30000
							else
							p.samxbeam = $+10000
							end
						end
					elseif (p.samxbeam < 0)
						if (p.speed < 25*FRACUNIT) or ((p.samspbscharge > 10) and (p.samspbscharge < 138))
						p.samybeamdest = (10*FRACUNIT)-(p.samxbeam*-1)
						else
						p.samybeamdest = (10*FRACUNIT)-(p.samxbeam*-2)
						end
					else
						if (p.speed < 25*FRACUNIT) or ((p.samspbscharge > 10) and (p.samspbscharge < 138))
						p.samybeamdest = (10*FRACUNIT)-(p.samxbeam)
						else
						p.samybeamdest = (10*FRACUNIT)-(p.samxbeam*2)
						end
					end
						if (p.samybeamdest < p.samybeam)
							if ((p.samspbscharge > 10) and (p.samspbscharge < 138))
							p.samybeam = $-50000-(p.samspbscharge*510)
							elseif p.samspbschargefull
							p.samybeam = $-75000

							else
							p.samybeam = $-50000
							end
						else
							if ((p.samspbscharge > 10) and (p.samspbscharge < 138))
							p.samybeam = $+50000+(p.samspbscharge*510)
							elseif p.samspbschargefull
							p.samybeam = $+75000
							else
							p.samybeam = $+50000
							end
						end
				end
				if (p.samybeam < 0)
				p.samybeam = 0 
				end
				
				--Visor transition reset
				
				if p.visorclosein
					p.samvisoraimytrue1 = 0
					p.samvisoraimytrue2 = 0
					p.samvisoraimxtrue = 0
					p.samvisordropdist = 0
					p.samvisordropdist2 = 0
					p.samangshift = 0
					s.sam_beamroll = 0
					s.samvisordrop = 0
					s.lastsamusangle = s.angle		
				else
				
					if s.sam_beamroll == nil
					s.sam_beamroll = 0
					end
					local mult = 5
					local stilt = 0
					if s.sam_supertilt
					stilt = 1
					end
					if not P_IsObjectOnGround(s)
						mult = 11
						if (p.speed > 40*s.scale) and not p.sam_spacejumpedboost
							mult = 9
						end
					else
						if (p.speed < 30*FRACUNIT)
							mult = 34-(p.speed/FRACUNIT)
						elseif (p.speed < 46*FRACUNIT)
							if (p.speed < 36*FRACUNIT)
							mult = 9-stilt	
							else
							mult = 8-stilt
							end
						elseif p.samspbschargefull
							mult = 4-stilt
						end
					end
					if s.sam_forcebeamroll --Ignores multiplier, does forced amount.
						s.sam_forcebeamroll = $-1
						if s.sam_forcebeamnum
							s.sam_beamroll = $-s.sam_forcebeamnum
							if s.sam_beamroll > 179
								s.sam_beamroll = 179
							elseif s.sam_beamroll < -179
								s.sam_beamroll = -179			
							end
						end
					end
					if (s.sam_stopmult or p.sams_supermissileready) and not s.sam_forcebeamroll
						if s.sam_beamroll
						s.sam_beamroll = ($*mult)/(mult+1)
						else
						s.sam_beamroll = 0
						end
					elseif (p.cmd.sidemove > 15) and not (p.sam_noscreentilt and p.sam_noscreentilt == 2)
						s.sam_beamroll = $-(p.cmd.sidemove/mult)
						if s.sam_beamroll < -179
							s.sam_beamroll = -179
						elseif s.sam_beamroll > 0
							s.sam_beamroll = $-(p.cmd.sidemove/mult)
						end
					elseif (p.cmd.sidemove < -15) and not (p.sam_noscreentilt and p.sam_noscreentilt == 2)
						s.sam_beamroll = $+(p.cmd.sidemove/-mult)
						if s.sam_beamroll > 179
							s.sam_beamroll = 179
						elseif s.sam_beamroll < 0 
							s.sam_beamroll = $+(p.cmd.sidemove/-mult)
						end
					elseif s.sam_beamroll
						s.sam_beamroll = ($*mult)/(mult+1)
					end
					if s.sam_beamroll
						if ((p.speed < 10*s.scale) or not P_IsObjectOnGround(s))
						s.sam_beamroll = $*30/31
						end
						if not s.sam_systemreboot and not s.metroidsnack and not s.myorammer
							if p.sammorphed or CV_FindVar("chasecam").value or p.sam_noscreentilt
							p.viewrollangle = 0
							else
							p.viewrollangle = ((s.sam_beamroll*-1)*ANG1)/20 --Visual field change!
							end
						end
					elseif p.viewrollangle
						if not s.sam_systemreboot and not s.metroidsnack and not s.myorammer
						p.viewrollangle = 0
						end					
					end
					
					--Add Visor bobbing as well
					if not P_IsObjectOnGround(s) and s.momz --Momz, speed, and aiming direction affect this all seperately!
					 	p.samvisordropdist2 = ((s.momz/5)*P_MobjFlip(s))
						
						--Top Visor Parts
						if (p.samvisoraimy < 8*FRACUNIT) and (s.momz*P_MobjFlip(s) > 0)	
							p.samvisoraimy = $+ ((s.momz*P_MobjFlip(s))/32)
						elseif (p.samvisoraimy > -4*FRACUNIT) and (s.momz*P_MobjFlip(s) < 0)
						p.samvisoraimy = $+ ((s.momz*P_MobjFlip(s))/32)
							if p.samvisoraimy < -4*FRACUNIT
							p.samvisoraimy = -4*FRACUNIT
							end
						end
				
						--Bottom Visor Parts
						if (p.samvisoraimy2 < 4*FRACUNIT) and (s.momz*P_MobjFlip(s) > 0)	
						p.samvisoraimy2 = $+ ((s.momz*P_MobjFlip(s))/32)
							if p.samvisoraimy2 > 4*FRACUNIT
							p.samvisoraimy2 = 4*FRACUNIT
							end
						elseif (p.samvisoraimy2 > -10*FRACUNIT) and (s.momz*P_MobjFlip(s) < 0)
						p.samvisoraimy2 = $+ ((s.momz*P_MobjFlip(s))/32)
						end
						
						--Spacejump when running off a ledge like in Hunters?
						--Be careful with stuff like this....
						if (p.cmd.buttons & BT_JUMP) and not (p.sams_jumpcd > 2)
						and p.sam_spacejump and not p.sam_spacejumped
						and not (p.pflags & (PF_JUMPED|PF_STARTJUMP|PF_SPINNING|PF_SLIDING|PF_JUMPSTASIS))
						and ((p.panim <= PA_DASH) or (p.panim == PA_FALL))
						and not (s.eflags & MFE_SPRUNG)
						and not p.sam_framejump
						and not p.sam_screwattacking
						and not p.sam_shinespark
						and not p.powers[pw_carry]
						and not p.exiting and not p.beampain
						and not p.sam_shinesparkfly and not s.crystalflashanim
						and not p.sam_intoship	
						
							p.pflags = $|PF_JUMPED & ~ PF_JUMPDOWN & ~PF_STARTJUMP
						end
						
					--Reset!
					else
						if (s.eflags & MFE_JUSTHITFLOOR) --landing+dust
							if ((s.sam_lastmomz or 0)*P_MobjFlip(s) < (-6*s.scale))
							and not s.sam_dustcd --landing sound+dust?
								local extraspd = min(27*FRACUNIT, abs(s.sam_lastmomz))
								for d = 1,16
									local ghs = P_SpawnMobjFromMobj(s, 
									FixedMul(cos(ANGLE_22h*d), 23*FRACUNIT),
									FixedMul(sin(ANGLE_22h*d), 23*FRACUNIT),
									-4*FRACUNIT, MT_SPINDUST)
									if (s.eflags & MFE_UNDERWATER)
										ghs.state = S_SPINDUST_BUBBLE1
										ghs.fuse = 30
									else
										ghs.state = S_SPINDUST1
										ghs.scale = $*3/4
										ghs.fuse = 50
									end
									ghs.scalespeed = $/2
									ghs.destscale = $*99
									if d % 2 == 0
										P_InstaThrust(ghs, ANGLE_22h*d, (7*FRACUNIT)+extraspd )
									else
										P_InstaThrust(ghs, ANGLE_22h*d, (20*FRACUNIT)+extraspd )
									end
									ghs.momx = $+(s.momx*5/4)
									ghs.momy = $+(s.momy*5/4)
								end
							/*	for i = -2,2
									local ghs = P_SpawnMobjFromMobj(s,
									FixedMul( cos(s.angle+(ANG1*19*d)), 23*FRACUNIT) ,
									FixedMul( sin(s.angle+(ANG1*19*d)), 23*FRACUNIT) ,
									6*FRACUNIT, MT_SPINDUST)
									ghs.destscale = $*99
									ghs.fuse = 55
									P_InstaThrust(ghs, s.angle+(ANG1*17*d), 40*FRACUNIT+extraspd)
									ghs.momx = $+s.momx
									ghs.momy = $+s.momy
									if (CV_FindVar("chasecam").value == 1)
									or (p != displayplayer)
										ghs.flags2 = $|MF2_DONTDRAW
									end
								end */
								s.sam_dustcd = 5
								s.sam_lastmomz = 0
								S_StartSound(s, sfx_samlnd)
							end
						
							if p.samvisordropdist2
								s.samvisordrop = 18
								s.sam_supertilt = 18
							end
						elseif (s.state == S_PLAY_WAIT) --Beam idle sway			
								if not p.beamswaychosen 
								
									if not p.beamswaydistxend or p.beamswaydistxend < 0
									p.beamswaydistxend = 12*FRACUNIT
									else
									p.beamswaydistxend = P_RandomRange(-2,-1)*FRACUNIT
									end
									
										if p.beamswaydistxend < 0
										p.beamswaydistxdir = -1
										else
										p.beamswaydistxdir = 1
										end
										if not p.beamswaydistx
										p.beamswaydistx = 0
										end
									
									if not p.beamswaydistyend
									p.beamswaydistyend = P_RandomRange(3,4)*FRACUNIT
									else
									p.beamswaydistyend = 0
									end
								
										if not p.beamswaydisty
										p.beamswaydisty = 0
										end
										if (p.beamswaydistyend > p.beamswaydisty)
										p.beamswaydistydir = 1
										else
										p.beamswaydistydir = -1
										end
								
									
									p.beamswaychosen = true
									p.beamswaytimer = 0
								
								else
									p.beamswaytimer = $+1
									if (p.beamswaydistxend < 0) and (p.beamswaydistx > p.beamswaydistxend)
									p.beamswaydistx = $-p.beamswaytimer*60
										if p.beamswaydistx > 0
										p.beamswaydistx = $-p.beamswaytimer*30
										end	
									elseif (p.beamswaydistxend > 0) and (p.beamswaydistx < p.beamswaydistxend)
									p.beamswaydistx = $+p.beamswaytimer*60
										if p.beamswaydistx < 0 or p.beamswaydistydir == -1
										p.beamswaydistx = $+p.beamswaytimer*60
										end
									end

									if p.beamswaydistydir == 1 and (p.beamswaydisty <= p.beamswaydistyend)
									p.beamswaydisty = $+p.beamswaytimer*90
									elseif p.beamswaydistydir == -1 and (p.beamswaydisty >= p.beamswaydistyend)
									p.beamswaydisty = $-p.beamswaytimer*90
									else
									p.beamswaychosen = false
									end	
									
								end
						elseif p.beamswaydisty or p.beamswaydistx
							p.beamswaydisty = 0
							p.beamswaydistx = 0
						end
						
						if (p.samvisoraimy < 0)
							if (p.samvisoraimy > -25001)
							p.samvisoraimy = 0
							else
							p.samvisoraimy = $+25000
							end
						elseif (p.samvisoraimy > 0)
							if (p.samvisoraimy < 25001)
							p.samvisoraimy = 0
							else
							p.samvisoraimy = $-25000
							end
						end
						if (p.samvisoraimy2 < 0)
							if (p.samvisoraimy2 > -25001)
							p.samvisoraimy2 = 0
							else
							p.samvisoraimy2 = $+25000
							end
						elseif (p.samvisoraimy2 > 0)
							if (p.samvisoraimy2 < 25001)
							p.samvisoraimy2 = 0
							else
							p.samvisoraimy2 = $-25000
							end			
						end
					end				
				
					if not s.samangshiftcd
						if p.samangshift > 145000
						p.samangshift = $-145000
						elseif p.samangshift < -145000
						p.samangshift = $+145000
						else
						p.samangshift = 0
						end
					end			
					if s.lastsamusangle != nil
					local diff = (s.lastsamusangle - s.angle)			
						if diff
							if (diff > ANGLE_180)
								diff = InvAngle(InvAngle(diff)/1200)
 							else
								diff = $/1200
							end
							p.samangshift = p.samangshift and $+diff or diff
							s.samangshiftcd = 3
						elseif s.samangshiftcd
						s.samangshiftcd = $-1
            			end
						if p.samangshift > 36*FRACUNIT
						p.samangshift = 36*FRACUNIT
						elseif p.samangshift < -36*FRACUNIT
						p.samangshift = -36*FRACUNIT
						end
						p.samangshift = p.samangshift and $+diff or diff
					end
					p.samvisoraimy3 = p.aiming/1000
					local yvisor2 = (p.samvisoraimy2/2)+(p.samybeam/3)+p.samvisoraimy3+p.samvisordropdist
					local yvisor1 = (p.samvisoraimy/2)+(p.samybeam/2)+p.samvisoraimy3+p.samvisordropdist
					p.samvisoraimxtrue = (p.samangshift/2)+p.samxbeam/3
					s.lastsamusangle = s.angle
						if yvisor1 > 16*FRACUNIT
						p.samvisoraimytrue1 = 16*FRACUNIT
						elseif yvisor1 < -4*FRACUNIT
						p.samvisoraimytrue1 = -4*FRACUNIT
						else
						p.samvisoraimytrue1 = yvisor1
						end
						if yvisor2 > 4*FRACUNIT
						p.samvisoraimytrue2 = 4*FRACUNIT
						elseif yvisor2 < -16*FRACUNIT
						p.samvisoraimytrue2 = -16*FRACUNIT
						else
						p.samvisoraimytrue2 = yvisor2
						end		


					
					if s.crystalflashtime and not p.gotflag
					p.jumpfactor = 98500
					elseif p.sam_highjump and not p.gotflag
					p.jumpfactor = 68500
					else
					p.jumpfactor = skins[s.skin].jumpfactor		
					end

					if ( (p.sams_beamanim == 14) or p.sams_chargetime) and not p.sammorphed
						if (P_IsObjectOnGround(s) and (p.speed < 20000))
						s.state = S_SAMUFIRE
						end
					end
				end
				
				PrimeRunPhysics(s, p) --Start of primerun 

				--Frame jump system, aka coyote time frames. Metroid Prime Hunters had something like this too.
				--Also adds fall "damage". Deals no actual damage, but stuns.

				if not P_IsObjectOnGround(s)
					if (s.momz*P_MobjFlip(s) < s.scale*2)
					and not (twodlevel or (s.flags2 & MF2_TWOD)) 
					and not (s.eflags & MFE_UNDERWATER)
						local falldmg = (s.momz*P_MobjFlip(s))/s.scale
						s.sam_falldmg = s.sam_falldmg and $+falldmg or falldmg 
					elseif s.sam_falldmg
						s.sam_falldmg = 0
					end
					if (p.cmd.buttons & BT_JUMP)
					and ((p.panim == PA_FALL) and ((p.sam_spacejump and p.sam_spacejumped) or not p.sam_spacejump)
					or (P_PlayerInPain(p) and not p.sam_spacejump)
					or p.sammorphed and p.sam_highjump and not s.spiderballing)
						s.sam_bufferjump = s.sam_bufferjump and $+1 or 1
					elseif s.sam_bufferjump
						s.sam_bufferjump = 0
					end
					if p.sam_framejump
						p.sam_framejump = $-1
						if (p.cmd.buttons & BT_JUMP) and (p.sams_jumpcd <= 2)
							if ((p.panim == PA_WALK) or (p.panim == PA_RUN) or (p.panim == PA_FALL))
							and not p.sammorphed
							and not (p.pflags & (PF_JUMPED|PF_SPINNING|PF_THOKKED))
							and not (s.eflags & MFE_SPRUNG)
							and not p.powers[pw_carry]
								P_DoJump(p, true)
								p.pflags = $ & ~PF_SPINNING & ~PF_THOKKED & ~PF_STARTDASH
								p.sam_framejump = 0
							end
						end
					end
				else
					if s.sam_falldmg and (s.sam_falldmg < -920) --OHW, MY KNEES
						if not p.sammorphed and not p.sam_screwattacking					
							if not (p.samspbschargefull and (p.speed > 20*s.scale))	
								s.momx = $/4
								s.momy = $/4
								p.visorclosein = 16
								if (p.powers[pw_nocontrol] < 11)
									p.powers[pw_nocontrol] = 11
								end
							else --Speedbooster landing
								p.visorclosein = 9
								s.momx = $*2/3
								s.momy = $*2/3				
							end

							p.beampain = 6
							S_StartSound(s, sfx_samfld)
							P_SamFP(p, PAL_NUKE, 2)
							P_NukeEnemies(s, s, 0)
							A_samquake(s, 55*FRACUNIT, 12, 60*FRACUNIT)
							S_StartSound(s, sfx_sballf)

							for d = -4,4
								local ghs = P_SpawnMobj(s.x, s.y, s.z, MT_PARTICLE)
								P_Thrust(ghs, s.angle+ANGLE_45*d, 10*FRACUNIT) 
								ghs.fuse = 10
							end
						end
					elseif s.sam_bufferjump
						if (s.sam_bufferjump < 6) and (p.cmd.buttons & BT_JUMP) and (p.pflags & PF_JUMPDOWN)
						and not p.powers[pw_carry] and not (p.pflags & PF_JUMPED)
							 p.pflags = $ & ~PF_JUMPDOWN
						end
						s.sam_bufferjump = 0
					end
					s.sam_falldmg = 0
					p.sam_framejump = 5
				end				
			end
			
--Morph Ball Check gone.

			if ((p.cmd.buttons & BT_WEAPONNEXT) or (p.cmd.buttons & BT_WEAPONPREV)) and not p.sams_weappress
			and not p.powers[pw_nocontrol] and not p.sam_beamswitch
			and not p.sams_chargetime
				p.sams_c2cd = 0
				p.sams_firecd = 0
				p.sams_normfcd = 0
				p.sams_powerbombready = 0
				p.sams_supermissileready = 0
				p.sams_mshotcd = 10
				A_SAMBEAMSWITCH(p)
			end

			if not p.sam_beamswitch
				if s.crystalflashtime 
						p.sam_beamcolor = SKINCOLOR_SUPERSKY1
				elseif p.sam_powerbeamequip
						if p.sam_powerbeammeltdown
						p.sam_beamcolor = SKINCOLOR_BLACK
						else
						p.sam_beamcolor = SKINCOLOR_POWERBEAM
						end
				elseif p.sam_spazerbeamequip
						if p.sam_spazerbeammeltdown
						p.sam_beamcolor = SKINCOLOR_BLACK
						else
						p.sam_beamcolor = SKINCOLOR_GREEN
						end
				elseif p.sam_wavebeamequip
						if p.sam_wavebeammeltdown
						p.sam_beamcolor = SKINCOLOR_BLACK
						else
						p.sam_beamcolor = SKINCOLOR_MAGENTA
						end
				elseif p.sam_icebeamequip
						if p.sam_icebeammeltdown
						p.sam_beamcolor = SKINCOLOR_BLACK
						else
						p.sam_beamcolor = SKINCOLOR_SKY
						end
				elseif p.sam_plasmabeamequip
						if p.sam_plasmabeammeltdown
						p.sam_beamcolor = SKINCOLOR_BLACK
						else
						p.sam_beamcolor = SKINCOLOR_RED
						end
				elseif p.sam_hyperbeamequip
				p.sam_beamcolor = SKINCOLOR_WHITE
				end
			end

//Here are the new Beam Combos!

			if (p.sams_chargetime > 41) and p.sam_chargebeam and (p.sammissiles and p.sammissiles >= 5*p.samadrain) and not p.sams_shotcd 
			and not p.powers[pw_nocontrol] and not p.exiting and not p.sammorphed
				if ( ( (((p.cmd.buttons & BT_FIRENORMAL) and p.swapfire) or ((p.cmd.buttons & BT_ATTACK) and not p.swapfire)) ) and (p.sams_firecd and (p.sams_firecd < 5)) )
					if (p.sam_powerbeamequip and p.sam_powerbeamcombo) or (p.sam_wavebeamequip and p.sam_wavebeamcombo) or (p.sam_spazerbeamequip and p.sam_spazerbeamcombo)
					or (p.sam_icebeamequip and p.sam_icebeamcombo) or (p.sam_plasmabeamequip and p.sam_plasmabeamcombo)

						if p.sam_powerbeamequip and (p.sammissiles and p.sammissiles >= 10*p.samadrain)
						or p.sam_spazerbeamequip and (p.sammissiles and p.sammissiles >= 15*p.samadrain)
						or p.sam_wavebeamequip and (p.sammissiles and p.sammissiles >= 15*p.samadrain)
						or p.sam_icebeamequip and (p.sammissiles and p.sammissiles >= 15*p.samadrain)
						or p.sam_plasmabeamequip and (p.sammissiles and p.sammissiles >= 5*p.samadrain)
							if G_GametypeHasSpectators() and not p.sam_wavebeamequip
							print(p.name+"\x85"+" is charging a beam combo!")
								for z in players.iterate
									if z.mo and z.mo.health --Show where it's coming from.
									and not (z.mo == s)
									z.mo.sam_combowarn = 40
									z.mo.sam_combowarntarget = s
									z.mo.sam_combowarnang = R_PointToAngle2(z.mo.x, z.mo.y, s.x, s.y)
									end
								end
							end

						p.sams_shotcd = 35
						p.sams_mshotcd = 35
						p.sams_chargetimecd = 35
						p.sams_chargetime = 0
						S_StartSound(s, sfx_cmcha)
						p.sams_beamcombocharge = 1
						elseif not (p.sams_firecd and p.sams_firecd > 2)
						p.sams_firecd = 10
						S_StartSound(s, sfx_sdenid)
						end
					elseif not (p.sams_firecd and p.sams_firecd > 2)
					p.sams_firecd = 10
					S_StartSound(s, sfx_sdenid)
					end
				end
			end


			if ( ( ((p.cmd.buttons & BT_CUSTOM2) and not p.c1c2disable and not (p.sams_c2cd and (p.sams_c2cd > 1))) or ((((p.cmd.buttons & BT_FIRENORMAL) and not p.swapfire) or ((p.cmd.buttons & BT_ATTACK) and p.swapfire)) and not (p.sams_normfcd and (p.sams_normfcd > 1))) )
			or ((p.sams_normfcdjlg and p.sams_chargetime > 41) or (p.sams_c2cdjlg and p.sams_chargetime > 41)) 
			or ((p.sams_chargetime > 3) and (p.sams_chargetime < 45)) and p.sam_chargebeam and not ((p.cmd.buttons & BT_FIRENORMAL) or (p.cmd.buttons & BT_ATTACK) or (p.cmd.buttons & BT_CUSTOM2) and not p.c1c2disable)  )
			and not p.sams_shotcd and not p.sammorphed and not p.powers[pw_nocontrol] and not p.exiting
			or s.waveoverfire
			
				S_StopSoundByID(s, sfx_pbcha)
				S_StopSoundByID(s, sfx_pbchao)
				s.waveoverfire = nil
					
				if not ( (p.sams_chargetime > 41) and p.sam_chargebeam)
				and not (s.crystalflashtime and not p.sam_hyperbeamequip)
					if p.sam_powerbeamequip
						p.sams_shotcd = 3
						p.sams_mshotcd = 4
						SamusBeamFire(s, MT_PBSHOT)
						SamusTutorial(s, "beams", true) 
						s.pbshotsmoke = s.pbshotsmoke and $+12 or 12
							if s.pbshotsmoker
								if (s.pbshotsmoker < 300)
								s.pbshotsmoker = $+12
								end
							s.pbshotsmoker2 = 12
							end
						S_StartSound(s, sfx_pbsh)
					elseif p.sam_spazerbeamequip
						p.sams_shotcd = 8
						p.sams_mshotcd = 11
						SamusBeamFire(s, MT_SPSHOT, 1)
						S_StartSound(s, sfx_spsh)
					elseif p.sam_wavebeamequip
						p.sams_shotcd = 14
						p.sams_mshotcd = 13
						SamusBeamFire(s, MT_WVSHOT, 2)
						S_StartSoundAtVolume(s, sfx_wvsh, 210)
							if p.plshot and p.plshot.valid
							p.plshot.scale = $*3/4
							p.plshot.color = SKINCOLOR_MAGENTA
							end
							if p.plshot2 and p.plshot2.valid
							p.plshot2.scale = $*3/4
							p.plshot2.color = SKINCOLOR_MAGENTA
							end
					elseif p.sam_icebeamequip
						p.sams_shotcd = 20
						p.sams_mshotcd = 8
						S_StartSound(s, sfx_icchsh)
						SamusBeamFire(s, MT_ICSHOT)
							if p.plshot and p.plshot.valid
							p.plshot.scale = $*5/3
							p.plshot.color = SKINCOLOR_CLOUDY
							end
					elseif p.sam_plasmabeamequip
						p.sams_shotcd = 13
						p.sams_mshotcd = 13
						SamusBeamFire(s, MT_PLSHOT)
							if p.plshot and p.plshot.valid
							p.plshot.scale = $*5/6
							p.plshot.color = SKINCOLOR_RED
							end
						S_StartSoundAtVolume(s, sfx_plsh, 230)
					elseif p.sam_hyperbeamequip
						if (p.sam_hyperbeamammo or (p.sam_energy and (p.sam_energy > 25)))
						and ( (p.sam_hyperbeamammo >= 10*p.samadrain) or (p.sam_fullypoweredsuit and (p.sam_hyperbeamammo >= 5*p.samadrain)) or (p.sam_energy and (p.sam_energy > 25) and G_GametypeHasSpectators()) ) 
						and not p.gotflag and not (p.sam_restricthyper and not p.hypernetgo)
						p.sams_shotcd = 13
						p.sams_mshotcd = 14
						SamusBeamFire(s, MT_HBSHOT) SamusTutorial(s, "hyperbeam", true)
							if p.plshot and p.plshot.valid
							p.plshot.color = SKINCOLOR_PINK
							end
						if p.sam_fullypoweredsuit and (p.sam_hyperbeamammo and (p.sam_hyperbeamammo >= 5*p.samadrain))
						p.sam_hyperbeamammo = $-5*p.samadrain
						elseif (p.sam_hyperbeamammo and (p.sam_hyperbeamammo >= 10))
						p.sam_hyperbeamammo = $-10*p.samadrain
						elseif (p.sam_energy and (p.sam_energy > 25))
						p.sam_energy = $-25
						A_samquake(s, 10*FRACUNIT, 5, 2500*FRACUNIT)
						P_SamFP(p, PAL_NUKE, 6)
						P_Thrust(s, s.angle, -16*FRACUNIT)
						P_PlayRinglossSound(s)
						end
						
						
						if G_GametypeHasSpectators()
							for z in players.iterate
								if z.mo and not z.sam_hyperbeam
								and (FixedHypot(FixedHypot(s.x-z.mo.x, s.y-z.mo.y), s.z-z.mo.z) < 5000*FRACUNIT)
								z.mo.omegarushpanic = 20
								end
							end
						end	
						
						if (p.sam_hyperbeamammo and (p.sam_hyperbeamammo >= 5*p.samadrain))
							if G_GametypeHasSpectators() and not (players[3] or players[4] or players[5])
							p.sam_hyperbeamammo = $-5*p.samadrain
							end
						end
						S_StartSound(s, sfx_hbsh)
						else
							if p.sam_restricthyper and not p.hypernetgo
							 CONS_Printf(p, "\x85"+"Hyper Beam is restricted in netgame boss battles.")
							end
							if p.gotflag
							 CONS_Printf(p, "\x85"+"Hyper Beam is restricted when carrying the flag.")
							end
						p.sams_hbdenid = 13
						p.sams_shotcd = 13
						S_StartSound(s, sfx_sdenid)
						end
						
						if (p.sam_hyperbeamammo < 0) --Don't go negative.
						p.sam_hyperbeamammo = 0
						end		
						
					end

				else
					
					if p.sam_powerbeamequip
						p.sams_shotcd = 5
						p.sams_mshotcd = 10
						p.sams_chargetimecd = 32
						p.sams_chargetime = 0
						SamusBeamFire(s, MT_PBCHARGESHOT)
						S_StartSound(s, sfx_pbchaf)
					elseif p.sam_spazerbeamequip
						p.sams_shotcd = 10
						p.sams_mshotcd = 12
						p.sams_chargetimecd = 24
						p.sams_chargetime = 0
						SamusBeamFire(s, MT_SPCHARGESHOT, 3)
						if not s.crystalflashtime
						P_SamFP(p, PAL_RECYCLE, 2)
						end
						S_StartSound(s, sfx_spchsh)
					elseif p.sam_wavebeamequip
						p.sams_shotcd = 14
						p.sams_mshotcd = 13
						p.sams_chargetimecd = 32
						p.sams_chargetime = 0
						SamusBeamFire(s, MT_WVCHARGESHOT)
						S_StartSoundAtVolume(s, sfx_wvchsh, 125)
						S_StartSound(s, sfx_wvchsh)
							if p.plshot and p.plshot.valid
							p.plshot.scale = $*4/3
							p.plshot.color = SKINCOLOR_MAGENTA
							end
					elseif p.sam_icebeamequip
						p.sams_shotcd = 21
						p.sams_mshotcd = 9
						p.sams_chargetimecd = 30
						p.sams_chargetime = 0
						S_StartSound(s, sfx_icsh)
						SamusBeamFire(s, MT_ICCHARGESHOT)

					elseif p.sam_plasmabeamequip
						p.sams_shotcd = 18
						p.sams_mshotcd = 18
						p.sams_chargetimecd = 32
						p.sams_chargetime = 0
						SamusBeamFire(s, MT_PLCHARGESHOT)
						if not s.crystalflashtime
						P_SamFP(p, PAL_NUKE, 3)
						end
						S_StartSound(s, sfx_plchsh)
					end
					if s.crystalflashtime // p.sams_shotcd < 13
					p.sams_shotcd = 13
					end
				end
			end

			if p.sam_highjump and not p.sammorphed
				if P_IsObjectOnGround(s) --Reset High Jump charge
				
					p.sam_highjumpcharge = 4
				
				elseif (p.pflags & PF_STARTJUMP) and (p.pflags & PF_JUMPED) and p.sam_highjumpcharge --High Jump effects
				
					if p.sam_highjumpcharge == 4 and (s.z < s.floorz+50*s.scale)
						local ghs = SamShinySplat(s,SKINCOLOR_SUPERSILVER5, 6)
						ghs.scale = $/10
						ghs.destscale = $*99
						ghs.sprite = SPR_MPR9
						ghs.frame = A|TR_TRANS80|FF_FULLBRIGHT
					end
					
					local ghs = P_SpawnMobjFromMobj(s,0,0,0, MT_SAMWINDLINE)
					ghs.scale = 35000
					
					if leveltime % 2 == 0
						ghs.momx = s.momx ghs.momy = s.momy
						ghs.momz = (s.momz*2)/3
					else
						ghs.momx = s.momx/2 ghs.momy = s.momy/2
						ghs.momz = s.momz/2
					end
					P_InstaThrust(ghs, (s.angle+ANGLE_45)+(ANGLE_90*p.sam_highjumpcharge), 18*FRACUNIT)
					ghs.flags2 = $|MF2_SHADOW
					ghs.rollangle = ANGLE_90
					ghs.blendmode = AST_ADD
					ghs.destscale = $*99
					ghs.fuse = 15	
					--And afterimages to boot, like Zero Mission
					ghs = P_SpawnGhostMobj(s)
					ghs.blendmode = AST_ADD
					ghs.destscale = ghs.scalespeed
					if SAM_displayplayer(p) and not CV_FindVar("chasecam").value
						ghs.flags2 = $|MF2_DONTDRAW
					end	
					
					p.sam_highjumpcharge = $-1					
				end
			end

			if p.sams_firecdjlg
			and not p.sams_mshotcd and not p.powers[pw_nocontrol] and not p.exiting and not p.sammorphed and not (p.sams_chargetime and p.sams_chargetime > 10)
				if not p.sams_supermissileready and p.sammissiles and (p.sammissiles >= 1*p.samadrain)
					if p.sam_hyperbeamequip and p.samchallenger and (p.samchallenger > 3)
					and p.sammissiles and (p.sammissiles >= 10*p.samadrain)
					and (p.sam_hyperbeamammo or (p.sam_energy and (p.sam_energy > 25)))
					and ( (p.sam_hyperbeamammo >= 15*p.samadrain) or (p.sam_energy and (p.sam_energy > 25) and G_GametypeHasSpectators()) ) 
					and not p.gotflag and not (p.sam_restricthyper and not p.hypernetgo) and not p.sams_beamcombocharge
						p.sams_shotcd = 22
						p.sams_mshotcd = 22
						S_StartSound(s, sfx_cmcha)
						p.sams_beamcombocharge = 14

					else
						if p.sam_icebeamequip
						p.sams_shotcd = 13
						else
						p.sams_shotcd = 14					
						end
						p.sams_mshotcd = 23
						p.sammissiles = $-1*p.samadrain
						SamusBeamFire(s, MT_SAMMISSILE)
						SamusTutorial(s, "missiles", true)
						S_StartSound(s, sfx_mssh2)
						S_StartSound(s, sfx_mssh2)
						if s.crystalflashtime
						p.sams_shotcd = 4
						p.sams_mshotcd = 4
						end
					end

				elseif p.sams_supermissileready and p.samsupermissiles and (p.samsupermissiles >= 1*p.samadrain) 

					p.sams_shotcd = 20
					p.sams_mshotcd = 40
					p.samsupermissiles = $-1*p.samadrain
					A_samquake(s, 10*FRACUNIT, 5, 3500*FRACUNIT)
					SamusTutorial(s, "supermissiles", true)
					SamusBeamFire(s, MT_SAMSUPERMISSILE)
					S_StartSound(s, sfx_smsh)
					S_StartSound(s, sfx_smsh)
				else
					if not p.samhudmessagetime and (p.samadrain and (p.samadrain > 1))
					p.samhudmessagetime = 35
					p.samhudmessage1 = "\x85"+"Double Ammo is on. You'll need twice the ammunition."
					p.samhudmessage2 = nil
					end
					S_StartSound(s, sfx_sdenid)
					p.sams_mshotcd = 12
					p.sams_shotcd = 4
				end

			end

			if ((((p.cmd.buttons & BT_FIRENORMAL) and p.swapfire) or ((p.cmd.buttons & BT_ATTACK) and not p.swapfire)) and (p.sams_firecd and (p.sams_firecd > 25)))
			and not p.sams_supermissileready and not p.sammorphed and not p.sams_firecdjlg
			and p.samsupermissiles and (p.samsupermissiles >= 1*p.samadrain)
			and not p.sams_shotcd and not p.sams_mshotcd
			and not p.powers[pw_nocontrol] and not p.exiting
			and not (p.sams_chargetime and p.sams_chargetime > 10)
				p.sams_supermissileready = 2 --Initial effect.
				S_StartSound(s, sfx_mread)
				local ghs = SamusWeaponReady(s, p)
				if ghs and ghs.valid
				ghs.dontstateme = true
				ghs.color = SKINCOLOR_WHITE
				ghs.sprite = SPR_MBOM
				ghs.frame = R|TR_TRANS90
				ghs.blendmode = AST_ADD
				ghs.tics = 99 ghs.fuse = 8
				ghs.scale = $/5 ghs.scalespeed = $*2
				ghs.destscale = $*99
				end

				
			elseif p.sammorphed and p.sampw and (p.sampw >= 1*p.samadrain) and ( ((p.cmd.buttons & BT_CUSTOM2) and not p.c1c2disable and (p.sams_c2cd and (p.sams_c2cd > 24)))
			or ((((p.cmd.buttons & BT_FIRENORMAL) and not p.swapfire) or ((p.cmd.buttons & BT_ATTACK) and p.swapfire)) and (p.sams_normfcd and (p.sams_normfcd > 24))) ) and not p.sams_morphcd
			and not p.sams_powerbombready and not p.powers[pw_nocontrol] and not p.exiting
				p.sams_powerbombready = 2
				S_StartSound(s, sfx_mread)
				local ghs = P_SpawnMobjFromMobj(s, 0, 0, s.height, MT_THOK)
				ghs.color = SKINCOLOR_CRIMSON
				ghs.sprite = SPR_MBOM
				ghs.frame = R|TR_TRANS60|FF_FULLBRIGHT
				ghs.blendmode = AST_ADD
				ghs.fuse = 9 ghs.tics = 99
				ghs.scalespeed = $*4 ghs.destscale = $*99
				ghs.momx = s.momx ghs.momy = s.momy
				P_SetObjectMomZ(ghs, FRACUNIT*11)
				ghs.momz = $+s.momz
			end

			if p.sams_supermissileready and not (s.samret2fire and s.samret2fire.valid)
				s.samret2fire = SamusWeaponReady(s, p)
				if s.samret2fire and s.samret2fire.valid
				s.samret2fire.sprite = SPR_MPR9
				s.samret2fire.frame = Z|FF_FULLBRIGHT|TR_TRANS30
				s.samret2fire.tics = 12
				s.samret2fire.scale = s.scale/3
				s.samret2fire.destscale = $*5
				s.samret2fire.target = s
				end
			elseif p.sams_powerbombready and p.sammorphed and p.sampw
				if not (s.samret2fire and s.samret2fire.valid)
				s.samret2fire = P_SpawnMobjFromMobj(s, FixedMul(s.radius*5/2, cos(s.angle-(ANG2*2))), FixedMul(s.radius*5/2, sin(s.angle-(ANG2*2))), s.height/10, MT_SAMCHARGEGFX)
				s.samret2fire.state = S_SAMCHARGEGFX4
				s.samret2fire.sprite = SPR_MPR9
				s.samret2fire.frame = Z|FF_FULLBRIGHT|TR_TRANS30
				s.samret2fire.tics = 12
				s.samret2fire.scale = $*10/9
				s.samret2fire.destscale = $*6
				s.samret2fire.color = SKINCOLOR_SUPERGOLD4
				s.samret2fire.ret2bomb = true
				s.samret2fire.target = s
				end
				
				local rng = P_RandomRange(-50,50)
				local ghs = P_SpawnMobjFromMobj(s,
				rng*FRACUNIT,
				P_RandomRange(-50,50)*FRACUNIT,
				8*FRACUNIT, MT_SAMWINDLINE)
				ghs.scale = 35000+(rng*310)
				if leveltime % 2 == 0
					P_SetObjectMomZ(ghs, 20*ghs.scale)
					ghs.flags2 = $|MF2_SHADOW
					ghs.momx = s.momx ghs.momy = s.momy
				else
					ghs.momx = s.momx/2 ghs.momy = s.momy/2
					P_SetObjectMomZ(ghs, 16*ghs.scale+(rng*500))
				end
				ghs.rollangle = ANGLE_90
				ghs.blendmode = AST_ADD
				ghs.destscale = $*99
				ghs.fuse = (51+rng)/17
				
				
				local pcolor = SKINCOLOR_EMERALD					
				if p.samvisorcolor and (p.sam_gravitysuit and p.sam_gravitysuit == 2)
					pcolor = p.samvisorcolor
				end
			end

			if not p.sam_chargebeam and G_GametypeHasSpectators()
			p.sam_chargebeam = true
				if (p.samusmatch == 2)
				p.samspbs = true
				p.sambsbl = true
				p.sam_spiderball = true
				p.sam_gravitysuit = 2
				p.sam_highjump = true
				p.sam_bombs = true
				p.sam_spacejump = true
				p.sam_spazerbeam = true
				p.sam_wavebeam = true
				p.sam_icebeam = true
				p.sam_plasmabeam = true
				p.sam_screwattack = true
				p.sam_power_grip = true
				if p.sam_beamcombosrestrict
						p.sam_powerbeamcombo = false
						p.sam_spazerbeamcombo = false
						p.sam_wavebeamcombo = false
						p.sam_icebeamcombo = false
						p.sam_plasmabeamcombo = false
				else
						p.sam_powerbeamcombo = true
						p.sam_spazerbeamcombo = true
						p.sam_wavebeamcombo = true
						p.sam_icebeamcombo = true
						p.sam_plasmabeamcombo = true
				end
				p.sam_hyperbeam = false
				p.sam_fullypoweredsuit = false
				p.sammissilesmax = 15
				p.sammissiles = 10
				p.sampw = 0
				p.sampwmax = 1
				p.sampw = 0
				p.samsupermissilesmax = 2
				p.samsupermissiles = 0
				end
			end
			
			--Charge Beam charging up!
			
			if ( 
			
			   (p.cmd.buttons & BT_CUSTOM2) and not p.c1c2disable and p.sams_c2cd and (p.sams_c2cd > 17)
			   
			or (p.cmd.buttons & BT_FIRENORMAL) and not p.swapfire and p.sams_normfcd and (p.sams_normfcd > 17)
			
			or (p.cmd.buttons & BT_ATTACK) and p.swapfire and p.sams_normfcd and (p.sams_normfcd > 17)
			
			or (p.sams_chargetime > 4) and (p.sam_walljumping and p.sam_walljumpsound or s.sam_ldclmb and (s.sam_ldclmb > 20))
			
			)
			
			and p.sam_chargebeam
			and not p.sams_shotcd
		--	and not (p.sam_walljumpsound and (p.sam_walljumpsound > 2)) 
			and not p.exiting
			and not p.sammorphed
			and not p.sams_chargetimecd
			and not p.sam_hyperbeamequip
			
				p.sams_chargetime = $+1
				s.sam_stopmult = 2
				if (p.sams_chargetime == 40)
				p.sams_firecd = 0
				SamusTutorial(s, "chargebeam", true)
				end
				if (p.sams_chargetime > 9) and not s.sams_chargedrawcd
					if AttractSamPickUps(s, 1800*FRACUNIT, 1)
						s.sams_chargedrawcd = 0
					else
						s.sams_chargedrawcd = 9 --Save resources, pls.
					end
				elseif s.sams_chargedrawcd
					s.sams_chargedrawcd = $-1
				end
				if (p.sams_chargetime % 2 == 0)
				local ghs
					if (s.eflags & MFE_VERTICALFLIP)
					ghs = P_SpawnMobj(s.x+FixedMul(s.radius*5/2, cos(s.angle-(ANG2*2))), s.y+FixedMul(s.radius*5/2, sin(s.angle-(ANG2*2))), s.z+(s.height*4/11), MT_SAMCHARGEGFX)
					else
					ghs = P_SpawnMobj(s.x+FixedMul(s.radius*5/2, cos(s.angle-(ANG2*2))), s.y+FixedMul(s.radius*5/2, sin(s.angle-(ANG2*2))), s.z+(s.height*2/3), MT_SAMCHARGEGFX)
					end
					ghs.z = $+(FixedMul(FixedMul(50*FRACUNIT, s.scale), sin(p.aiming)))
					ghs.scale = s.scale/(P_RandomRange(10,20))
					if p.sam_powerbeamequip
						ghs.color = SKINCOLOR_VARIASUIT
					else
						ghs.color = p.sam_beamcolor
					end
					ghs.momx = s.momx/2
					ghs.momy = s.momy/2
					ghs.eflags = s.eflags
					ghs.blendmode = AST_ADD
					ghs.target = s
				end
				if (p.sams_chargetime == 1)
					S_StartSound(s, sfx_pbcha)
				end
				if (p.sams_chargetime % 50 == 0)
					S_StartSound(s, sfx_pbchao)
				end			
			elseif p.sams_chargetime
				p.sams_chargetimecd = 26
				p.sams_chargetime = 0 
				S_StopSoundByID(s, sfx_pbcha)
				S_StopSoundByID(s, sfx_pbchao)
			end

			if p.sam_gravitysuit
				if (p.sam_gravitysuit == 2)
					if p.forceskincolor3 and s.color == p.skincolor
					and not p.sam_screwattacking and not p.sam_gravitysuitjustgot
					and not p.forceskincolor3 == p.skincolor
						s.color = p.forceskincolor3
					end
					
					if p.fusionmode
					p.gravityrd = 6
					else
					p.gravityrd = 10
					end
				else
					if p.forceskincolor2 and s.color == p.skincolor
					and not p.sam_screwattacking and not p.sam_gravitysuitjustgot
					and not p.forceskincolor2 == p.skincolor
						s.color = p.forceskincolor2
					end
					
					if p.fusionmode --defense values. Higher number = More Defense.
					p.gravityrd = 5
					else
					p.gravityrd = 8
					end
				end
				if s.exposedtime
				s.exposedtime = 0
				end
				if not p.sam_gravitysuitjustgot
				SetSamusColor(s, p)					
				elseif not p.sam_saveselect
					if not S_SoundPlaying(s, sfx_ssuitu)
					S_StartSound(s, sfx_ssuitu)
					end
					if (p.sam_gravitysuitjustgot == 1)
						s.pwbomb = P_SpawnMobj(s.x, s.y, s.z, MT_SAMPOWERBOMB) --Create a big boom!
						s.pwbomb.target = s
						s.pwbomb.eflags = s.eflags
						s.pwbomb.gravitysuitjustgot = true
						S_StartSound(s, sfx_pwbmb)
					end
					p.sam_gravitysuitjustgot = $-1
					local pcolor = SKINCOLOR_VARIASUIT
					if (p.sam_gravitysuit == 1)
					A_SAMPARTICLE(s, MT_MPR1, 25, 2, 2, 3, SKINCOLOR_ORANGE, 40000, true)
					else
					A_SAMPARTICLE(s, MT_MPR1, 25, 2, 2, 3, SKINCOLOR_MAGENTA, 40000, true)
					pcolor = p.samvisorcolor
					end
					if P_IsObjectOnGround(s) or (s.z < (s.z+s.floorz)+50*FRACUNIT) --So shiny, wow!
						for d = 1,8
						local ghs = SamShinySplat(s, pcolor, 2)
						ghs.scale = $*5
						ghs.momx = s.momx*8/7 ghs.momy = s.momy*8/7
						ghs.sprite = SPR_BBTR
						ghs.frame = C|TR_TRANS20|FF_FULLBRIGHT
						ghs.flags2 = $|MF2_SPLAT
						ghs.rollangle = (leveltime*ANG10)+(ANGLE_45*d)
						end
					end					
				end
			else
				if p.forceskincolor1 and s.color == p.skincolor and not p.sam_screwattacking and not p.sam_gravitysuitjustgot
				and not p.forceskincolor1 == p.skincolor and not G_GametypeHasTeams()
				s.color = p.forceskincolor1
				end
				if p.fusionmode --defense values
				p.gravityrd = 4
				else
				p.gravityrd = 7
				end
				if s.burnuptime
					if (s.burnuptime % ((TICRATE + TICRATE/2)-1)== 0)
						p.rings = max(1, $-s.prevringlost)
						s.prevringlost = $+2
						if p.sam_energy and p.sam_energy > 5
						p.sam_energy = $-5
						else
						P_DamageMobj(s)
						end
					end
				end
			end
--if NOT p.sammorphed
			if not p.sammorphed
				p.sampers = 1

				if p.sam_screwattack
					if (p.pflags & PF_JUMPED)
					and ( ((p.cmd.buttons & BT_CUSTOM1) and not p.c1c2disable and not (p.sams_c1cd > 1))
					or ((p.cmd.buttons & BT_JUMP) and not (p.sams_jumpcd > 1)) )
					and not (p.sam_screwattacking or s.sam_screwattackfling)
					and not p.sams_morphcd and not p.powers[pw_nocontrol] and not p.exiting and not p.gotflag
					and not p.sams_screwattackcd
					local doscrewattack
						if not p.sam_j1ready
							if ( (((p.cmd.buttons & BT_JUMP) and not (p.sams_jumpcd  > 1)) and not p.sam_spacejump) 
							or (((p.cmd.buttons & BT_JUMP) and not (p.sams_jumpcd  > 1)) and p.sam_spacejump and p.sam_spacejumped and not (p.sam_spacejumpedboost and p.sam_spacejumpedboost > 4)) )
								p.sam_j1ready = 11
								p.sam_c1ready = 0
							end
						elseif p.sam_j1ready
							if ((p.cmd.buttons & BT_JUMP) and not (p.sams_jumpcd  > 1))
				  				if ((s.eflags & MFE_UNDERWATER) and not (p.sam_gravitysuit and p.sam_gravitysuit == 2))
								or p.sam_gravitysuitjustgot
									S_StartSound(nil, sfx_sdenid, p)
				  				else
									doscrewattack = true
				  				end
							end
						end

						if not p.sam_c1ready
							if ((p.cmd.buttons & BT_CUSTOM1) and not p.c1c2disable and not (p.sams_c1cd > 1))
								p.sam_c1ready = 11
								p.sam_j1ready = 0
								if s.sam_spbshack and not p.autospeedbooster --waa5
									s.sam_spbshack = 11
								end
							end
						elseif p.sam_c1ready
							if ((p.cmd.buttons & BT_CUSTOM1) and not p.c1c2disable and not (p.sams_c1cd > 1))
				  				if ((s.eflags & MFE_UNDERWATER) and not (p.sam_gravitysuit and p.sam_gravitysuit == 2))
								or p.sam_gravitysuitjustgot
									S_StartSound(nil, sfx_sdenid, p)
				  				else
									doscrewattack = true
				  				end
							end
						end
						if doscrewattack
						p.sam_c1ready = 0
						p.sam_j1ready = 0
						p.sam_screwattacking = 1
						p.sams_jumpcd = 99
						s.sam_bufferjump = 0
						SamusTutorial(s, "screwattack", true)
						p.pflags = $ & ~PF_JUMPED
						p.prescrewcolor = s.color
						s.state = S_PLAY_ROLL
						P_SamFP(p, PAL_MIXUP, 2)
						S_StartSound(s, sfx_satkc)
									
						local ghs = P_SpawnMobjFromMobj(s, 0, 0, 0, MT_THOK)
						ghs.sprite = SPR_MXP4
						ghs.frame = W|FF_FULLBRIGHT|TR_TRANS10
						P_SetObjectMomZ(ghs, FRACUNIT, true)
						ghs.blendmode = AST_ADD
						ghs.tics = 39
						ghs.fuse = 14
						ghs.color = SKINCOLOR_SUPERGOLD5
							ghs.colorized = true
							ghs.scalespeed = $*3
							ghs.destscale = $*99
							ghs.dispoffset = 3
							SAM_RemoveLDgrp(s)
						end
					end

					if (p.sam_screwattacking or s.sam_screwattackfling) and p.prescrewcolor
						s.tempsamdye = s.color
						s.tempsamdyetr = TR_TRANS10
						s.colorized = true
						s.color = P_RandomRange(SKINCOLOR_SUPERGOLD1, SKINCOLOR_SUPERPERIDOT5)
						p.noprimerun = 9
						s.sam_supertilt = 3
						p.pflags = $|PF_JUMPDOWN
						if not ((p.sam_screwattacking > 26) and s.crystalflashtime and (s.crystalflashtime % 2 == 0))
						p.sam_screwattacking = $+1
							if s.sam_waveoverload and (p.sam_screwattacking < 25)
							p.sam_screwattacking = 25
								local angshift = P_RandomRange(27,80)
								if leveltime % 2 == 0
									angshift = $*-1
								end
								s.angle = $+(angshift*ANG1)
							end
						end
						local ghs = P_SpawnGhostMobj(s)
						ghs.blendmode = AST_ADD
						ghs.destscale = ghs.scalespeed+1
						A_SAMPARTICLE(s, MT_MPR1, 30, 3, -3, 5, SKINCOLOR_SUPERGOLD4, 35000, true)
						A_SAMPARTICLE(s, MT_MPR1, 50, 4, -4, 6, SKINCOLOR_WHITE, 23000, true)
						p.sams_morphcd = 30
						p.sams_firecd = 0
						p.sams_shotcd = 35
						p.sams_mshotcd = 35
						if p.samspbschargefull or (p.samspbscharge > 140)
						or s.sam_screwattackspbs or (s.sam_screwattackfling and s.sam_screwattackfling.speed > 60)
						or (s.sam_spbshack and not p.autospeedbooster)
							s.sam_afterboost = 7
							if s.sam_spbshack
							s.sam_spbshack = 11
							end
							s.sam_screwattackspbs = true
							p.samspbschargefull = false
						end
						p.samspbscharge = 0
						p.sam_spacejumped = true
						p.sams_supermissileready = 0
							if not (p.panim == PA_ROLL)
							s.state = S_PLAY_ROLL
							end			
						if (leveltime % 4 == 0)
						and ((p.sam_screwattacking < 44) or s.sam_screwattackfling)
							local ghs
							if (s.eflags & MFE_VERTICALFLIP)
							ghs = P_SpawnMobj(s.x, s.y, s.z+s.height*5/6, MT_SAMUSZAP)
							else
							ghs = P_SpawnMobj(s.x, s.y, s.z+s.height/6, MT_SAMUSZAP)
							end
						   if ghs and ghs.valid
						   ghs.blendmode = AST_ADD
						   ghs.scale = s.scale*30/41
						   ghs.momx = s.momx
						   ghs.momy = s.momy
						   ghs.momz = s.momz
						   ghs.eflags = s.eflags
						   ghs.color = s.color
						   ghs.morning = 4
								if (p.sam_screwattacking > 24) and ghs.valid
								ghs = P_SpawnMobj(s.x, s.y, s.z+((s.height/6)*P_MobjFlip(s)), MT_SAMUSZAP)
								ghs.color = s.color
								ghs.morning = 4
								end
						   end
						end

					--Wallfling Screw Attack Launch!

						if s.sam_screwattackfling
						local yu = s.sam_screwattackfling
							yu.timer = $+1
							p.powers[pw_noautobrake] = 2
							p.sam_screwattacking = 30
							s.spriteyscale = FRACUNIT
							s.spritexscale = FRACUNIT						
							s.sam_flashfade = 4
							s.sam_flashfadecolor = SKINCOLOR_SUPERGOLD3
							if (yu.timer < 4)
								local ghs = ScrewAttackParticles(s,p)
								if ghs and ghs.valid
								ghs.flags2 = $|MF2_SHADOW
								end
								if yu.speedboosted
									P_Thrust(s, yu.angle, 15000)
								end
							end
							S_StopSoundByID(s, sfx_swljp)
							if not yu.chainsaw
								S_StartSound(s, sfx_satkrl)
							end							
							if yu.speedboosted
								SpeedBoosterGhosts(s, p)
								if leveltime % 2 == 0
								s.color = SKINCOLOR_SKY
								end
							end
							if not P_IsObjectOnGround(s)
							yu.momz = s.momz
								if (p.cmd.buttons & BT_SPIN) and not (p.sams_usecd > 99000)
									s.spriteyscale = 74555
									s.spritexscale = 50010
									
									if (p.sams_usecd == 1) and not yu.diddive
										yu.walljumpavailable = false
										yu.diddive = true 
										S_StartSound(s, sfx_satkf)
										
										local ghs = P_SpawnMobjFromMobj(s, 0, 0, 0, MT_THOK)
										ghs.fuse = 6
										ghs.scalespeed = $*3
										ghs.destscale = FRACUNIT*99
										ghs.sprite = SPR_ICXP
										ghs.frame = E|TR_TRANS20|FF_FULLBRIGHT
										ghs.color = SKINCOLOR_RED

										if (s.momz*P_MobjFlip(s) > 0)
											P_SetObjectMomZ(s, -8*FRACUNIT, false)
										else
											P_SetObjectMomZ(s, -8*FRACUNIT, true)
										end
										if (p.speed < 50*s.scale)
											P_InstaThrust(s, s.angle, 50*FRACUNIT)
										else
											if p.speed > 70*s.scale
												P_Thrust(s, yu.angle, 27000)
											else
												P_Thrust(s, yu.angle, FRACUNIT*4)
											end
										end
										
										for d = 1,3
										local ghs = P_SpawnMobjFromMobj(s, 0, 0, 0, MT_PBEXPANDW)
											if ghs
											ghs.scale = $*d
											ghs.blendmode = AST_ADD
											ghs.colorized = true
											ghs.color = SKINCOLOR_RED+1
											ghs.scalespeed = ($*3)*d
											ghs.destscale = $*99
											ghs.momx = s.momx*d ghs.momy = ghs.momy*d
											ghs.momz = 7*FRACUNIT
											end
										end
									end
									local rng = P_RandomRange(-220,220)
									local ghs = P_SpawnMobjFromMobj(s,
									rng*FRACUNIT,
									P_RandomRange(-220,220)*FRACUNIT,
									8*FRACUNIT, MT_SAMWINDLINE)
									ghs.scale = 35000+(rng*210)
									if leveltime % 2 == 0
										P_SetObjectMomZ(ghs, 20*ghs.scale)
										ghs.flags2 = $|MF2_SHADOW
										ghs.momx = s.momx ghs.momy = s.momy
									else
										ghs.momx = s.momx/2 ghs.momy = s.momy/2
										P_SetObjectMomZ(ghs, 16*ghs.scale+(rng*300))
									end
									ghs.rollangle = ANGLE_90
									ghs.blendmode = AST_ADD
									ghs.destscale = $*99
									ghs.fuse = (51+rng)/23							
									if leveltime % 2 == 0
										s.color = SKINCOLOR_RED
									end

									P_SetObjectMomZ(s, -41000, true)
									P_Thrust(s, s.angle, 19000)
										
								elseif (p.cmd.buttons & BT_JUMP)
								
									if yu.walljumpavailable
										if (s.eflags & MFE_UNDERWATER)
										P_SetObjectMomZ(s, 3800, true)
										else
										P_SetObjectMomZ(s, 10000, true)
										end
									
										if leveltime % 6 == 0
										local ghs = P_SpawnMobjFromMobj(s, 0, 0, s.momz*-1, MT_THOK)
										ghs.scale = 1
										ghs.spritexscale = 40000
										ghs.fuse = 9
										ghs.tics = 20
										ghs.sprite = SPR_HBXP
										ghs.frame = C|TR_TRANS70|FF_FULLBRIGHT
										ghs.destscale = FRACUNIT*99
										ghs.blendmode = AST_ADD
										ghs.color = s.color
										P_SetObjectMomZ(ghs, -5*FRACUNIT)
										end
										
										if not (p.sams_jumpcd > 2)
										samwallcheck(s, p)
										end
										
									end
									
									s.momx = FixedDiv(FixedMul($, 70), 71)
									s.momy = FixedDiv(FixedMul($, 70), 71)
								end
								
								--Do regardless of button input...
								if (p.speed < yu.speed*FRACUNIT)
									if yu.speedboosted
										P_Thrust(s, s.angle, FRACUNIT/4)
									end
									s.momx = FixedDiv(FixedMul($, 80), 79)
									s.momy = FixedDiv(FixedMul($, 80), 79)
								end
								
								
							else --if P_IsObjectOnGround(s) on the ground checks.
							
								p.pflags = $|PF_JUMPDOWN
								
								--Cancel conditions
								if not ((p.cmd.buttons & BT_SPIN) or yu.chainsaw)
								or (p.speed < 22*s.scale)
								
									p.speed = 5
									if yu.chainsaw or (yu.momz*P_MobjFlip(s) < -25*FRACUNIT)
										ScrewAttackBonk(s,p)
									else
										CancelScrewAttack(s,p)
										S_StartSound(s, sfx_skid)
										if (p.speed > 39*s.scale) and (p.samspbscharge < 50)
										 p.samspbscharge = 50
										end
									end
									
								else --Chainsaw! Time to cut some grass. 
									if (p.cmd.buttons & BT_SPIN)
										s.spriteyscale = 74555
										s.spritexscale = 50010
										yu.diddive = false
										yu.timer = $+1000
										yu.walljumpavailable = false
										if (p.speed < 92*FRACUNIT)
											if (s.eflags & MFE_JUSTHITFLOOR) 
												local smomz = yu.momz
												if smomz < 0
												smomz = $*-1
												end
												ScrewAttackLandingGFX(s,p)
												P_SamFP(p, PAL_RECYCLE, 2)
												P_NukeEnemies(s, s, 0)
												P_RadiusAttack(s, s, 290*s.scale)
												S_StartSound(s, sfx_s3k4e)
												if (p.speed < smomz) --Go faster the harder you land!
													P_InstaThrust(s, s.angle, (smomz*7/6))
												else
													P_Thrust(s, s.angle, 6*FRACUNIT)
												end
											else
												s.friction = FRACUNIT
												P_Thrust(s, s.angle, 235355)
											end
										end										
										if not noquake --No quaking!		
											for z in players.iterate
												if z.mo
													if (FixedHypot(s.x-z.mo.x, s.y-z.mo.y) < 1000*FRACUNIT)
														if quakestrength
														A_samquake(z.mo, quakestrength*FRACUNIT, 1, 8)
														else
														A_samquake(z.mo, 22*FRACUNIT, 1, 8)
														end
													end
												end
											end
										end
										local rng = P_RandomRange(-35,35)
										local ghs = P_SpawnMobjFromMobj(s, rng*FRACUNIT,rng*FRACUNIT,0, MT_METROIDDROOL)
										if ghs and ghs.valid
											ghs.state = S_ROCKCRUMBLEA
											ghs.scale = FRACUNIT*2/(P_RandomRange(2,6))
											ghs.momx = s.momx ghs.momy = s.momy
											P_Thrust(ghs, s.angle+(rng*ANG1), -30*FRACUNIT)
											ghs.tics = 130
											ghs.flags = $|MF_NOCLIPTHING & ~MF_NOGRAVITY
											ghs.momz = FRACUNIT+(rng*(ghs.scale/20))
											ghs.omgrock = true
										end
										local ghs = ScrewAttackParticles(s,p)
										if ghs and ghs.valid
										ghs.flags2 = $|MF2_SHADOW
										end
										
										if (yu.timer > 50000) --Okay, stop!
										
											p.speed = 5
											P_SetObjectMomZ(s, 6*FRACUNIT, true)
											ScrewAttackBonk(s,p)
											
										elseif not yu.chainsaw
										
										yu.chainsaw = true --We're a chainsaw now!
										p.sams_usecd = 99000
										
										elseif not (p.sams_usecd > 2) --Morph from the chainsaw roll?
											CancelScrewAttack(s,p)
											SamMorphSwitch(p, s)
											S_StartSound(s, sfx_smorph)
											S_StartSound(s, sfx_zoom)
											if p.sambsbl
												if (p.pflags & PF_DIRECTIONCHAR)
													p.drawangle = s.angle
												end
												p.sams_c1cdjlg = 0
												p.sams_firecdjlg = 0
												s.bsblboostcspeed = p.speed
												P_InstaThrust(s, s.angle, 90*FRACUNIT)
												p.bsblboostangle = s.angle
												p.bsblboost = 9
												p.bsblboostattack = 18
												if s.samslopespin
													s.samslopespin = 1
												end
											end
										end
									elseif yu.chainsaw and (p.speed > 15*s.scale)
										s.momx = FixedDiv(FixedMul($, 5), 6)
										s.momy = FixedDiv(FixedMul($, 5), 6)									
									end
									if (p.cmd.buttons & BT_JUMP)
									and not (p.sams_jumpcd > 2)
									and P_IsObjectOnGround(s)
										p.speed = 120*s.scale
										CancelScrewAttack(s,p)
										P_DoJump(p, true)
										SpaceJumpGFX(s, SKINCOLOR_SKY, 2, L|TR_TRANS20)
										p.sam_walljumping = 13
										p.pflags = $|PF_JUMPED
										s.state = S_PLAY_ROLL
										if (p.speed > 40*s.scale)
											P_InstaThrust(s, R_PointToAngle2(s.x, s.y, s.x+s.momx, s.y+s.momy), 40*FRACUNIT)
										end
										p.sam_spacejumped = false
										p.sams_jumpcd = 10
										p.sam_c1ready = 11
										p.sam_j1ready = 0
									end
								end
							end
							--Aww, no fun allowed.
							if (s.eflags & MFE_UNDERWATER)
							and not (p.sam_gravitysuit and p.sam_gravitysuit == 2)
								CancelScrewAttack(s,p)
								s.momx = $/2
								s.momy = $/2
								s.momz = $/2
								s.state = S_PLAY_SPRING
							end
							
					--Regular Screw Attack Launch!	
					
						elseif (p.sam_screwattacking < 25) --Screw Attack startup. Cancel start a walljump from here.
						
						
							p.pflags = $ & ~PF_JUMPED
							if p.sam_screwattacking < 3
								s.momx = $/9
								s.momy = $/9
							else
								s.momx = $*20/21
								s.momy = $*20/21
							end
							P_SetObjectMomZ(s, 55000)
							local maxfade = 7
							if not ((p.sam_screwattacking/4) > maxfade)
							maxfade = p.sam_screwattacking/4
							end
							s.sam_flashfade = maxfade
							s.sam_flashfadecolor = SKINCOLOR_SUPERGOLD5						
							if ((p.cmd.buttons & BT_JUMP) and not (p.sams_jumpcd > 2))
								samwallcheck(s, p)
							end
							
						elseif (p.sam_screwattacking == 25) --Magic initiation frame
						
							p.sam_shinesparkdirect = s.angle
							p.sam_satkoriginalz = s.z
							S_StartSound(s, sfx_satkf)
							P_SamFP(p, PAL_MIXUP, 3)	
						
							if (p.cmd.buttons & BT_JUMP) or (((s.flags2 & MF2_TWOD) or twodlevel) and (p.cmd.forwardmove) > 0)
								if (s.eflags & MFE_VERTICALFLIP)
									p.aiming = -3600*FRACUNIT
								else
									p.aiming = 3600*FRACUNIT
								end
								p.samscrewedbefore = true
							elseif (p.cmd.buttons & BT_USE) or (((s.flags2 & MF2_TWOD) or twodlevel) and (p.cmd.forwardmove) < 0)
								if (s.eflags & MFE_VERTICALFLIP) 
									p.aiming = 3600*FRACUNIT
								else
									p.aiming = -3600*FRACUNIT
								end
								p.samscrewedbefore = true
							end
						
							s.momz = $+FixedMul(FixedMul(45*FRACUNIT, s.scale), sin(p.aiming))
							P_Thrust(s, s.angle, FixedMul(FixedMul(45*FRACUNIT, s.scale), cos(p.aiming)))
							p.sam_screwattacking = 26
							if not (gametyperules & GTR_POINTLIMIT)
							P_RadiusAttack(s, s, 180*FRACUNIT)
							end
							
						elseif (p.sam_screwattacking < 46) --In the middle of our big attack
						
							s.angle = p.sam_shinesparkdirect
							P_SetObjectMomZ(s, 12000, true)
							s.sam_flashfade = 4
							s.sam_flashfadecolor = SKINCOLOR_SUPERGOLD3

							if ((s.z > p.sam_satkoriginalz+185*FRACUNIT) and not (s.eflags & MFE_VERTICALFLIP)) or ((s.z < p.sam_satkoriginalz-185*FRACUNIT) and (s.eflags & MFE_VERTICALFLIP))
							and not s.crystalflashtime
							p.sam_screwattacking = 99
							end

						ScrewAttackParticles(s,p)
						
						elseif (p.sam_screwattacking >= 46) or p.gotflag --Okay, timer's up, cancel the Screw Attack.
							s.momx = $/5
							s.momy = $/5
							if (s.momz*P_MobjFlip(s) > -2*FRACUNIT)
								s.momz = $/17
							else
								s.momz = $/5
							end
							if not (p.powers[pw_carry] == CR_MACESPIN) and not p.sammorphed
							s.state = S_PLAY_FALL
							end
							CancelScrewAttack(s,p)
						end
						--Global situations in which to cancel.
						if p.powers[pw_carry]
						or (p.panim == PA_PAIN) or (s.state == S_PLAY_RIDE)
							CancelScrewAttack(s,p)
						elseif ((p.sam_screwattacking > 26) and not s.sam_screwattackfling) or p.gotflag
							ScrewAttackBonk(s,p)
						end
					end
				elseif p.sam_screwattacking
					CancelScrewAttack(s,p) --Don't be screwattacking without the powerup now...
				end
				
			--LOCK ON CODE
			
				if p.sam_lockonpower and not (p.sam_lockontoggle and s.sam_automob and s.sam_automob.valid)
				and ( (((p.cmd.buttons & BT_CUSTOM1) and not (p.sams_c1cd and p.sams_c1cd > 1)) and p.sam_lockonpower == 1)
				or ((p.cmd.buttons & BT_CUSTOM3) and not p.sams_c3cd) )
					if not p.powers[pw_nocontrol] and not p.exiting and not p.sam_screwattacking and not p.beampain
					and not p.sam_shinesparkfly and not s.crystalflashanim and not p.sam_intoship
						s.sam_lockseek = 3
						s.sam_lockchosen = nil
						s.sam_automob = nil
						s.sam_lockedon = false
						for d = 1,12
						s.sam_lock = P_SpawnMobj(s.x+FixedMul((600*FRACUNIT)*d, cos(s.angle)),s.y+FixedMul((600*FRACUNIT)*d, sin(s.angle)), s.z, MT_SAMUSLOCKSEEK)
							if s.sam_lock and s.sam_lock.valid
							s.sam_lock.tics = 1
							s.sam_lock.target = s
							s.sam_lock.flags2 = $|MF2_DONTDRAW
							end					
						end
					end
				elseif s.sam_lockseek
				s.sam_lockseek = $-1
					if s.sam_lockchosen
						if s.sam_lockchosen.valid and P_CheckSight(s, s.sam_lockchosen)
							s.sam_automob = s.sam_lockchosen
							s.sam_lockchosen = nil
							s.sam_lockedon = true
							s.sam_lockseek = 3
							s.sam_sighttime = 0
							s.sam_faillock = false
							SamusTutorial(s, "lockon", true)
							if p.sam_lockontoggle
								p.sams_c1cd = 4
								p.sams_c3cd = true
							end
						end
					end
					--Here's our target, let's do behaviour.
					if s.sam_automob and s.sam_automob.valid
					local t = s.sam_automob	
					
						if t.player
						t.sam_automobed = 3
						end
						
						--Cursor
						if not (p.automobf and p.automobf.valid)
						p.automobf = P_SpawnMobj(t.x, t.y, t.z+t.height/6, MT_SAMUSLOCKGFX)
						p.automobf.scale = (t.height/24)*3
						p.automobf.scalespeed = (t.height/300)*4
						p.automobf.color = SKINCOLOR_SKY
						p.automobf.colorized = true
						S_StartSound(s, sfx_samtm8)
							if s.sam_automob.player
							S_StartSound(s, sfx_samtm8, s.sam_automob.player)
							end
						end
						
						if (p.automobf and p.automobf.valid)
						p.automobf.sprite = SPR_MPR1
							if p.automobf.scale != t.height/24
							p.automobf.frame = (Z+1)|TR_TRANS60|FF_FULLBRIGHT
							p.automobf.destscale = t.height/24
							else
							p.automobf.frame = (Z+1)|TR_TRANS40|FF_FULLBRIGHT
							end
						p.automobf.rollangle = $+ANG20
						p.automobf.tics = 3
						P_MoveOrigin(p.automobf, t.x, t.y, t.z)
						s.sam_beamroll = 0
						end
						
					--Aiming adjustment.
						if p.sam_lockontoggle
							if ( (((p.cmd.buttons & BT_CUSTOM1) and not (p.sams_c1cd and p.sams_c1cd > 2)) and p.sam_lockonpower == 1)
							or ((p.cmd.buttons & BT_CUSTOM3) and not p.sams_c3cd) )
							s.sam_faillock = true
							else
							s.sam_lockseek = 3
							end
						else
							if ((p.cmd.buttons & BT_CUSTOM1) and p.sam_lockonpower == 1)
							or (p.cmd.buttons & BT_CUSTOM3) --Continue lock on
							s.sam_lockseek = 3
							end
						end
						
						--Vertical aiming

						p.aiming = R_PointToAngle2(0, 0, R_PointToDist2(s.x, s.y, t.x, t.y), ((t.z+(t.height/5)) - s.z)	)					
						
						
						--The higher 2020 is, the more the aim vertically overcompensates. 
						if t.superbrak
						p.aiming = $+1500*FRACUNIT
						end
						
						if p.aiming > 11500*FRACUNIT or p.aiming < -11500*FRACUNIT
						or p.sam_screwattacking 
						s.sam_faillock = true
						else
						
							if (p.pflags & PF_ANALOGMODE)
							and not (p.pflags & PF_DIRECTIONCHAR)
								if not p.sam_forcedirectcharoff --old analogmode fix. console: directionchar 0, configanalog 1
							//	p.pflags = $ & ~PF_DIRECTIONCHAR & ~PF_ANALOGMODE
								SAM_COM_BufInsertText(p, "configanalog 1; directionchar 1")
								end
								p.sam_forcedirectcharoff = 2
							end
							if p.sam_forcedirectcharoff --Refresh infinitely.
							p.sam_forcedirectcharoff = 2
							end							
							s.angle = R_PointToAngle2(s.x, s.y, t.x, t.y)
							p.drawangle = s.angle
						end
						
					--Fail conditions	
						if (leveltime % 5 == 0) --periodic checks.
						local distcheck = FixedHypot(t.x-s.x, t.y-s.y)
							--Don't stay out of sight too long.
							if not P_CheckSight(s,t)
							s.sam_sighttime = s.sam_sighttime and $+2 or 2
							elseif s.sam_sighttime
							s.sam_sighttime = $-1
							end
							
							if (distcheck < 40*FRACUNIT or distcheck > 9000*FRACUNIT)
							or (s.state == S_PLAY_PAIN) and not (p.pflags & PF_SLIDING)
							or p.powers[pw_nocontrol] or p.sammorphed
							or p.sam_shinesparkfly or s.crystalflashanim
							or s.sam_wavestunned or s.sam_systemreboot
							or s.sam_sighttime and s.sam_sighttime >= 17
							or ((t.flags & MF_MONITOR) and (t.flags2 & MF2_DONTDRAW))
							or (t.flags2 & MF2_FRET) and (t.flags & MF_BOSS) and not t.superbrak
							s.sam_faillock = true
							end
						end
					end
					if not s.sam_lockseek
					or s.sam_faillock
					or s.sam_automob and not s.sam_automob.valid
					or s.sam_automob and not s.sam_automob.health
						if s.sam_automob
						p.aiming = 0
						s.sam_automob = nil
						end
						if (p.automobf and p.automobf.valid)
						p.automobf.color = SKINCOLOR_SILVER
						p.automobf.destscale = $*10
						p.automobf.scalespeed = $*2
						p.automobf.tics = 8
						p.automobf.cancellock = true
						p.automobf.frame = (Z+1)|TR_TRANS80|FF_FULLBRIGHT
						p.automobf = nil
						end
					s.sam_faillock = false
					s.sam_lockedon = false
					s.sam_lockseek = 0
					s.sam_sighttime = 0
					s.sam_lockclosest = nil
					s.sam_lockchosen = nil
					end
				end
				
				if p.sam_screwattacking and (s.state == S_PLAY_WALK)
					CancelScrewAttack(s,p)
				end
				
				if (p.pflags & PF_JUMPED) --Walljump check
				and ((p.cmd.buttons & BT_JUMP) and not (p.sams_jumpcd > 2))
					samwallcheck(s,p)
				end
				if p.speed >= 38*FRACUNIT and not p.sammorphed and not s.morphedmachactive
					s.smmachball = p.speed
				end

				if ( ((p.cmd.buttons & BT_CUSTOM1) and not p.c1c2disable or p.autospeedbooster) and p.samspbs) and not p.gotflag
					if (  (p.speed > FixedMul(24*FRACUNIT, s.scale)) and not ((s.eflags & MFE_UNDERWATER) and not (p.sam_gravitysuit and p.sam_gravitysuit == 2))
					or ((twodlevel or (s.flags2 & MF2_TWOD)) and (p.speed > FixedMul(13*FRACUNIT, s.scale)) and not ((s.eflags & MFE_UNDERWATER) and not (p.sam_gravitysuit and p.sam_gravitysuit == 2)))  )
					and not p.powers[pw_carry]
					p.samspbscharge = $+1
						if (s.wavebuffering or s.crystalflashtime) and (p.samspbscharge < 50)
							p.samspbscharge = 50
						end
						if s.samspinpadconfirmed and not p.sampbschargefull and not p.sammorphed and not s.samspinpad
							p.samspbscharge = 140
							StartSamSPBS(s,p)
						end
						if (p.samspbscharge > 70) and (p.samspbscharge < 138)
						local ghs
							if (s.eflags & MFE_VERTICALFLIP)
								ghs = P_SpawnMobj(s.x+FixedMul(-25*FRACUNIT, cos(s.angle+ANG10+ANG2)), s.y+FixedMul(-25*FRACUNIT, sin (s.angle+ANG10+ANG2)), s.z+( ( FixedMul(s.height/52, s.scale) ) ), MT_SAMCHARGEGFX)
							elseif s.scale == 90000
								ghs = P_SpawnMobj(s.x+FixedMul(-25*FRACUNIT, cos(s.angle+ANG10+ANG2)), s.y+FixedMul(-25*FRACUNIT, sin (s.angle+ANG10+ANG2)), s.z+( ( FixedMul(s.height*20/29, s.scale) ) ), MT_SAMCHARGEGFX)
							else
								ghs = P_SpawnMobj(s.x+FixedMul(-25*FRACUNIT, cos(s.angle+ANG10+ANG2)), s.y+FixedMul(-25*FRACUNIT, sin (s.angle+ANG10+ANG2)), s.z+(s.height*20/21), MT_SAMCHARGEGFX)
							end
							if ghs and ghs.valid
								ghs.scale = s.scale/(P_RandomRange(10,20))
								ghs.color = SKINCOLOR_SKY
								ghs.momx = s.momx/2
								ghs.momy = s.momy/2
							end
							local ghs = nil
							if (s.eflags & MFE_VERTICALFLIP)
								ghs = P_SpawnMobj(s.x+FixedMul(-25*FRACUNIT, cos(s.angle-ANG10-ANG2)), s.y+FixedMul(-25*FRACUNIT, sin (s.angle-ANG10-ANG2)), s.z+( ( FixedMul(s.height/52, s.scale) ) ), MT_SAMCHARGEGFX)
							elseif s.scale == 90000
								ghs = P_SpawnMobj(s.x+FixedMul(-25*FRACUNIT, cos(s.angle-ANG10-ANG2)), s.y+FixedMul(-25*FRACUNIT, sin (s.angle-ANG10-ANG2)), s.z+( ( FixedMul(s.height*20/29, s.scale) ) ), MT_SAMCHARGEGFX)
							else
								ghs = P_SpawnMobj(s.x+FixedMul(-25*FRACUNIT, cos(s.angle-ANG10-ANG2)), s.y+FixedMul(-25*FRACUNIT, sin (s.angle-ANG10-ANG2)), s.z+(s.height*20/21), MT_SAMCHARGEGFX)
							end
							if ghs and ghs.valid
								ghs.scale = s.scale/(P_RandomRange(10,20))
								ghs.color = SKINCOLOR_SKY
								ghs.momx = s.momx/2
								ghs.momy = s.momy/2
							end
						end
					elseif not s.samspinpadconfirmed
						if p.sampbschargefull or (p.samspbscharge > 139)
							local forwardmove = p.cmd.forwardmove
							local sidemove = p.cmd.sidemove
							local direct = 0
							if (sidemove < 0)
								sidemove = $*-1
							end
							if (forwardmove < 0)
								forwardmove = $*-1
							end
							if forwardmove > sidemove
								direct = forwardmove
							else
								direct = sidemove --Decide how hard we're pressing the stick! From 0 to 50!
							end
							if (direct > 10) or ((p.pflags & PF_JUMPED) and not p.autospeedbooster)
								s.sam_afterboost = 7
								if not p.autospeedbooster
									s.sam_spbshack = 11 --And again alternate controls screw my life.
								end
								if (p.pflags & PF_JUMPED)
									if not p.autospeedbooster and not (p.cmd.buttons & BT_CUSTOM1)
									p.sam_spbsgrace = p.sam_spbsgrace and $+1 or 1
									else
									p.sam_spbsgrace = p.sam_spbsgrace and $+3 or 3
									end
								else
									p.sam_spbsgrace = p.sam_spbsgrace and $+8 or 8
								end
								if (p.sam_spbsgrace > 15)
									p.samspbscharge = 0
									p.sampbschargefull = false
									p.sam_spbsgrace = 0
								end
							else
								p.sam_spbsgrace = p.sam_spbsgrace and $+8 or 8
								if (p.sam_spbsgrace > 15)
									p.samspbscharge = 0
									p.sampbschargefull = false
									p.sam_spbsgrace = 0
								end								
							end
						else
							p.samspbscharge = 0
							p.sampbschargefull = false
						end
					end

					if not p.samspbschargefull
						if s.sam_unstuck
							if P_IsObjectOnGround(s)
							p.accelstart = 175
							p.thrustfactor = 7
							else
							p.accelstart = 192
								if (s.eflags & MFE_UNDERWATER) and (p.sam_gravitysuit and p.sam_gravitysuit == 2)
								p.thrustfactor = 13
								else
								p.thrustfactor = 11
								end
							end
							s.sam_unstuck = $-1
						elseif ((s.eflags & MFE_UNDERWATER) and (p.sam_gravitysuit and p.sam_gravitysuit == 2))
						p.normalspeed = 62*FRACUNIT
							if p.sam_spacejumpedboost
							p.accelstart = 192
							p.thrustfactor = 12
							else
							p.accelstart = 175
							p.thrustfactor = 7
							end
						elseif not (s.eflags & MFE_UNDERWATER)
						p.normalspeed = 32*FRACUNIT
							if p.sam_spacejumpedboost
							p.accelstart = 192
							p.thrustfactor = 11
							else
							p.accelstart = 150
							p.thrustfactor = 5
							end
						else
						p.normalspeed = skins[s.skin].normalspeed
							if p.sam_spacejumpedboost
							p.thrustfactor = 11
							p.accelstart = 192
							else
							p.thrustfactor = skins[s.skin].thrustfactor
							p.accelstart = skins[s.skin].accelstart
							end
						end
					elseif p.samspbschargefull and (p.samspbscharge > 70)
						if not p.sammorphed and not s.morphedmachactive
						s.smmachball = p.speed
						s.smmachballspbs = true
						end
						if not p.autospeedbooster
						s.sam_spbshack = 11 --waa8
						end
						s.sam_afterboost = 7
						SpeedBoosterGhosts(s, p)
						
						if s.speedghost
							s.tempsamdye = s.speedghost.color
							s.tempsamdyetr = TR_TRANS70
						end
						
						if s.morphedmachactive --morphine
							p.normalspeed = s.morphedmachspeed*14/9
							p.thrustfactor = 13
						else
							if not (p.pflags & PF_ANALOGMODE)
							p.powers[pw_noautobrake] = 3
							end
							if ((s.eflags & MFE_UNDERWATER) and (p.sam_gravitysuit and p.sam_gravitysuit == 2)) and not p.powers[pw_flashing]
								p.normalspeed = 88*FRACUNIT
								p.accelstart = 192
								if p.sam_spacejumpedboost
								p.thrustfactor = 13
								else
								p.thrustfactor = 9
								end
							elseif (s.eflags & MFE_UNDERWATER) or (p.powers[pw_flashing] and not s.samspinpadconfirmed)
								p.samspbscharge = 0
								p.sampbschargefull = false
								s.momx = $/2
								s.momy = $/2
							else
								p.normalspeed = 43*FRACUNIT
								p.accelstart = 192
								if p.sam_spacejumpedboost
								p.thrustfactor = 11
								else
								p.thrustfactor = 6
								end
							end
						end	
						CheckForShineSpark(s, p) --Check for Shinespark potential!
					end

					if (p.samspbscharge > 70)
						if (p.samspbscharge == 71)
							s.spbssfx = P_SpawnMobj(s.x, s.y, s.z, MT_THOK) --Don't make this a local value.
							s.spbssfx.flags2 = $|MF2_DONTDRAW
							s.spbssfx.tics = 90
							S_StartSound(s.spbssfx, sfx_sprnc)
							p.samspbscharge = 86
						end
						if (p.samspbscharge > 130) and not p.samspbschargefull
						P_SamFP(p, PAL_RECYCLE, 2)
						StartSamSPBS(s,p) --Local function referal
						end
						if s.spbssfx and s.spbssfx.valid
						P_MoveOrigin(s.spbssfx, s.x, s.y, s.z+s.height/2)
						end
					elseif s.spbssfx and s.spbssfx.valid
						s.spbssfx.state = S_NULL
					end
				elseif ((s.eflags & MFE_UNDERWATER) and (p.sam_gravitysuit and p.sam_gravitysuit == 2))
					p.normalspeed = 45*FRACUNIT
						if p.sam_spacejumpedboost
						p.thrustfactor = 12
						p.accelstart = 192
						else
						p.thrustfactor = 6
						p.accelstart = 143
						end
						if p.samspbscharge
						p.samspbschargefull = false
						p.samspbscharge = 0
						end
				else
					p.normalspeed = skins[s.skin].normalspeed
					if p.sam_spacejumpedboost
						p.thrustfactor = 11
						p.accelstart = 192
					else
						p.thrustfactor = skins[s.skin].thrustfactor
						p.accelstart = skins[s.skin].accelstart
					end
					if p.samspbscharge
						p.samspbschargefull = false
						p.samspbscharge = 0
					end
				end


				if p.sam_spacejump and ((p.cmd.buttons & BT_JUMP) and not (p.sams_jumpcd > 2))
					if P_PlayerInPain(p) and not s.spazershocked
						if (s.eflags & MFE_UNDERWATER)
						P_SetObjectMomZ(s, 5*FRACUNIT)
						else
						P_SetObjectMomZ(s, 11*FRACUNIT)
						end
						p.sam_spacejumped = true
						p.pflags = $|PF_JUMPED
						p.sam_spacejumpedboost = 12
						s.state = S_PLAY_FALL
						S_StartSound(s, sfx_sjmp)
					end
						
				end
				if p.sam_spacejumped
					if p.sam_spacejumpedboost
					local ghs
					local rng = P_RandomRange(10,20)
						if (s.eflags & MFE_VERTICALFLIP)
							ghs = P_SpawnMobj(s.x+FixedMul(-25*FRACUNIT, cos(s.angle-ANG10-ANG2)), s.y+FixedMul(-25*FRACUNIT, sin (s.angle-ANG10-ANG2)), s.z+( ( FixedMul(s.height/52, s.scale) ) ), MT_SAMCHARGEGFX)
						else
							ghs = P_SpawnMobj(s.x+FixedMul(-25*FRACUNIT, cos(s.angle-ANG10-ANG2)), s.y+FixedMul(-25*FRACUNIT, sin (s.angle-ANG10-ANG2)), s.z+s.height, MT_SAMCHARGEGFX)
						end 
						ghs.scale = s.scale/rng
						ghs.color = SKINCOLOR_SKY
						ghs.momx = s.momx/2
						ghs.momy = s.momy/2
						if (s.eflags & MFE_VERTICALFLIP)
							ghs = P_SpawnMobj(s.x+FixedMul(-25*FRACUNIT, cos(s.angle+ANG10+ANG2)), s.y+FixedMul(-25*FRACUNIT, sin (s.angle+ANG10+ANG2)), s.z+( ( FixedMul(s.height/52, s.scale) ) ), MT_SAMCHARGEGFX)
						else
							ghs = P_SpawnMobj(s.x+FixedMul(-25*FRACUNIT, cos(s.angle+ANG10+ANG2)), s.y+FixedMul(-25*FRACUNIT, sin (s.angle+ANG10+ANG2)), s.z+s.height, MT_SAMCHARGEGFX)
						end  
						ghs.scale = s.scale/rng
						ghs.color = SKINCOLOR_SKY
						ghs.momx = s.momx/2
						ghs.momy = s.momy/2
					end
					if P_IsObjectOnGround(s) or p.powers[pw_carry] or (s.eflags & MFE_JUSTHITFLOOR)
					p.sam_spacejumped = false
					end
				end
				
				if (p.powers[pw_carry] == CR_ZOOMTUBE)
					if (p.pflags & PF_SPINNING) and not (s.sprite == SPR_ZSSA)
					SamMorphSwitch(p, s)
					S_StartSound(s, sfx_smorph)
					S_StartSound(s, sfx_zoom)
					end
				end


			elseif p.sammorphed and p.sambsbl and not p.gotflag
				if (p.realtime == 0 and (gametype == GT_RACE or gametype == GT_COMPETITION))
				or ( ( (p.cmd.buttons & BT_CUSTOM1) and not p.c1c2disable and (p.sams_c1cd and (p.sams_c1cd >= 11)) ) or ( (((p.cmd.buttons & BT_FIRENORMAL) and p.swapfire) or ((p.cmd.buttons & BT_ATTACK) and not p.swapfire)) and (p.sams_firecd and (p.sams_firecd >= 11)) ) )
				and not p.sams_shotcd and not p.powers[pw_nocontrol] and not p.exiting
					if (gametype == GT_RACE or gametype == GT_COMPETITION) and not p.powers[pw_nocontrol] and (p.realtime < 9)
						if (p.cmd.buttons & BT_CUSTOM1) 
							p.sams_c1cd = 500
							p.sambsblchargefull = true
						elseif ( ((p.cmd.buttons & BT_FIRENORMAL) and p.swapfire) or ((p.cmd.buttons & BT_ATTACK) and not p.swapfire) )
							p.sams_firecd = 500
							p.sambsblchargefull = true
						end
					end
					s.sambsblchargelvl = s.sambsblchargelvl and $+1 or 1
					s.bsblglowticket = 2
					
				elseif p.sambsblchargefull and not p.sams_c1cdjlg and not p.sams_firecdjlg
					p.sambsblchargefull = false
					s.sambsblchargelvl = 0
				end



				if (p.sams_c1cdjlg or p.sams_firecdjlg) and p.sambsblchargefull
				and not p.sams_shotcd and not p.powers[pw_nocontrol] and not p.exiting
				and not (p.powers[pw_carry] == CR_ZOOMTUBE)
					if (p.pflags & PF_DIRECTIONCHAR)
					p.drawangle = s.angle
					end
					p.sams_c1cdjlg = 0
					p.sams_firecdjlg = 0
					S_StartSound(s, sfx_zoom)
					s.bsblboostcspeed = p.speed
					P_InstaThrust(s, s.angle, 75*FRACUNIT)
					p.bsblboostangle = s.angle
					p.bsblboost = 9
					p.bsblboostattack = 18
					p.cmd.forwardmove = 1
						if s.samslopespin
						s.samslopespin = 1
						end
						if (twodlevel or (s.flags2 & MF2_TWOD)) and s.z == s.floorz
						s.z = s.z+FRACUNIT
						end
				end
				if p.bsblboostattack
				AttractSamPickUps(s, 400*FRACUNIT, 3)
				p.charflags = $|SF_RUNONWATER
				P_SpawnPlayerMissile(s, MT_SAMMONBREAKER)
				elseif (p.charflags & SF_RUNONWATER) and not s.samspinpad
				p.charflags = $ & ~SF_RUNONWATER
				end
						
				if p.bsblboost and p.bsblboostangle and not (p.powers[pw_flashing] == 9) 
				s.flags = $ & !MF_BOUNCE
					if (p.powers[pw_carry] == CR_BRAKGOOP)
						p.powers[pw_carry] = 0 --ignore this stuff.
						p.powers[pw_nocontrol] = 0			
					elseif (p.powers[pw_carry] == CR_ZOOMTUBE)
					p.bsblboost = 0
					return
					end
					if ((s.eflags & MFE_UNDERWATER) and not (p.sam_gravitysuit and p.sam_gravitysuit == 2))
					P_InstaThrust(s, p.bsblboostangle, 45*FRACUNIT)
					else
						if s.bsblboostcspeed and (s.bsblboostcspeed > 76*FRACUNIT)
						P_InstaThrust(s, p.bsblboostangle, s.bsblboostcspeed)
						else
						P_InstaThrust(s, p.bsblboostangle, 75*FRACUNIT)
						end
					end
					local ghs = P_SpawnMobjFromMobj(s, s.momx, s.momy, s.momz, MT_THOK)
					ghs.angle = p.bsblboostangle //R_PointToAngle2(s.x, s.y, s.x+s.momx, s.y+s.momy)
					ghs.sprite = SPR_BBTR
					ghs.blendmode = AST_ADD
					ghs.colorized = true
					if leveltime % 2 == 0
						ghs.color = SKINCOLOR_SUPERGOLD3
						ghs.scale = $*12/14
						ghs.tics = 2
						ghs.frame = A|FF_FULLBRIGHT|TR_TRANS70
					else
						ghs.color = SKINCOLOR_SUPERORANGE5
						ghs.scale = $*12/15
						ghs.tics = 9
						ghs.fuse = 6
						ghs.frame = A|FF_FULLBRIGHT|TR_TRANS40
					end
				p.sams_c1cd = 0
				p.sams_firecd = 0
					if not ((p.pflags & PF_STARTJUMP) and (p.pflags & PF_JUMPED) and (s.momz*P_MobjFlip(s) > 0) and (p.cmd.buttons & BT_JUMP))
					P_SetObjectMomZ(s, -FRACUNIT, true)
					end
				end
			end

			if p.sam_shinespark and (p.samspbs or s.crystalflashtime)
				if (p.sam_shinespark % 16 == 0)
					local ghs = P_SpawnMobj(s.x, s.y, s.z, MT_PBEXPANDI)
					if ghs and ghs.valid
						ghs.color = SKINCOLOR_AZURE
						ghs.scale = s.scale+5*FRACUNIT
						ghs.momz = s.momz*10/11
						ghs.momy = s.momy*10/11
						ghs.momx = s.momx*10/11
						ghs.scalespeed = $*11
						ghs.destscale = $*45
					end
				end

				if ( ( (p.sam_shinespark % 2 == 0)
				or p.sam_shinespark == 1) )
				and not s.crystalflashtime
					s.flags2 = $ & ~MF2_DONTDRAW
						s.speedghost = P_SpawnMobjFromMobj(s, 0, 0, 0, MT_OVERLAY)
						s.speedghost.target = s
						s.speedghost.skin = s.skin
						s.speedghost.sprite = s.sprite
						s.speedghost.sprite2 = s.sprite2
						s.speedghost.frame = s.frame|TR_TRANS50|FF_FULLBRIGHT
						s.speedghost.angle = s.angle
						s.speedghost.tics = 2
						s.speedghost.scale = s.scale
						s.speedghost.flags2 = s.flags2
						s.speedghost.eflags = s.eflags
						if s.crystalflashtime
						s.speedghost.color = SKINCOLOR_SUPERSILVER1
						else
						s.speedghost.color = SKINCOLOR_SUPERSKY3
						end
						s.speedghost.colorized = true
						
						if (s.eflags & MFE_VERTICALFLIP)
						s.speedghost = P_SpawnMobj(s.x, s.y, s.z-s.height*5/13, MT_THOK)
						else
						s.speedghost = P_SpawnMobj(s.x, s.y, s.z, MT_THOK)
						end
						s.speedghost.skin = s.skin
						s.speedghost.sprite = s.sprite
						s.speedghost.sprite2 = s.sprite2
						s.speedghost.frame = s.frame|TR_TRANS40|FF_FULLBRIGHT
						s.speedghost.angle = s.angle
						s.speedghost.tics = 4
						s.speedghost.scale = s.scale
						s.speedghost.flags2 = s.flags2
						s.speedghost.eflags = s.eflags
						s.speedghost.colorized = true
						if s.speedghost and s.speedghost.valid and ( ((P_RandomRange(1, 2)) == 2) or s.crystalflashtime)
							s.speedghost.color = SKINCOLOR_WHITE
						elseif s.speedghost and s.speedghost.valid
							s.speedghost.color = SKINCOLOR_SUPERSKY3
						end
						
					if s.crystalflashtime
					A_SAMPARTICLE(s, MT_MPR1, 30, 3, -2, 3, SKINCOLOR_SUPERRED2, 36000, true)
					else
					A_SAMPARTICLE(s, MT_MPR1, 30, 3, -2, 3, SKINCOLOR_BLUE, 36000, true)
					end

				elseif not (p.sam_shinespark % 2 == 0)
					s.flags2 = $|MF2_DONTDRAW
				end

				if (p.sam_energy > 29)
				and not ((p.pflags & PF_SLIDING) or (s.state == S_PLAY_PAIN))
				and not p.gotflag
				and not ((s.eflags & MFE_UNDERWATER) and not (p.sam_gravitysuit and p.sam_gravitysuit == 2))
					if ((p.cmd.buttons & BT_JUMP) and (p.pflags & PF_JUMPED))
					and not ((p.cmd.sidemove) or (p.cmd.forwardmove))
					and not ( ((p.sams_jumpcd > 9) and (p.pflags & PF_ANALOGMODE)) or ((p.sams_jumpcd > 1) and not (p.pflags & PF_ANALOGMODE)) )
					p.samspbscharge = 0
					p.sampbschargefull = false
					p.sam_shinespark = 0
					p.sam_shinesparkfly = 1
					S_StartSound(s, sfx_spbsc)
					local ghs = P_SpawnMobj(s.x, s.y, s.z+((s.height*3/5)*P_MobjFlip(s)), MT_PBEXPAND)
					ghs.scale = s.scale*2
					ghs.scalespeed = $*9
					ghs.destscale = s.scale*25
					if s.plexpand and s.plexpand.valid
					s.plexpand.state = S_NULL
					end
					end
				end
			end
			if p.sam_shinesparkfly
			and not ((p.pflags & PF_SLIDING) or (s.state == S_PLAY_PAIN))
					p.sams_morphcd = 35
					p.sams_jumpcd = 10
					p.sams_firecd = 0
					p.sams_shotcd = 22
					p.sams_mshotcd = 22
					p.samspbschargefull = false
					p.sam_spacejumped = true
					p.samspbscharge = 0
					p.bsblboost = 0
					p.bsblboostattack = 0
					p.sambsblchargefull = false
					p.sams_supermissileready = 0

				if (p.sam_shinesparkfly < 18)
				p.pflags = $ & ~PF_JUMPED
				s.momx = 0
				s.momy = 0
				P_SetObjectMomZ(s, 155000)
				
				s.sam_flashfade = p.sam_shinesparkfly/4
				s.sam_flashfadecolor = SKINCOLOR_SAPPHIRE
					if (twodlevel or (s.flags2 & MF2_TWOD))
						p.sam_shinesparkdirect = s.angle
						if (p.cmd.forwardmove > 0) or not (p.cmd.sidemove)
						p.sam_shinesparkdirectz = 28*FRACUNIT
						else
						p.sam_shinesparkdirectz = 0
						end
						if (p.cmd.sidemove)
						p.sam_shinesparkdirectx = 80*FRACUNIT
						else
						p.sam_shinesparkdirectx = 0
						end
					else
						if ((p.cmd.forwardmove > 0) or ((p.pflags & PF_ANALOGMODE) and ((p.cmd.forwardmove) or (p.cmd.sidemove)) and not (p.cmd.buttons & BT_USE)))
						p.sam_shinesparkdirect = s.angle
						if p.sammorphed
						p.sam_shinesparkdirectx = FixedMul(FRACUNIT*140, s.scale)
						else
						p.sam_shinesparkdirectx = FixedMul(FRACUNIT*66, s.scale)
						end
						p.sam_shinesparkdirectz = 0 
						elseif (p.cmd.forwardmove < 0) or (p.cmd.buttons & BT_USE)
						p.sam_shinesparkdirect = s.angle
						if p.sammorphed
						p.sam_shinesparkdirectx = FixedMul(FRACUNIT*120, s.scale)
						else
						p.sam_shinesparkdirectx = FixedMul(FRACUNIT*57, s.scale)
						end
						p.sam_shinesparkdirectz = 25*FRACUNIT
						else
						p.sam_shinesparkdirect = s.angle
						p.sam_shinesparkdirectx = 0
						p.sam_shinesparkdirectz = 38*FRACUNIT
						end
					end
				elseif (p.sam_shinesparkfly == 18)
					P_SetObjectMomZ(s, p.sam_shinesparkdirectz*p.sampers)
					s.angle = p.sam_shinesparkdirect
					P_InstaThrust(s, p.sam_shinesparkdirect, p.sam_shinesparkdirectx)
					P_SpawnGhostMobj(s)
					p.pflags = $|PF_JUMPED & ~PF_STARTJUMP
					P_SamFP(p, PAL_MIXUP, 3)
					s.sam_flashfade = 11
					s.sam_flashfadecolor = SKINCOLOR_RED				
					S_StartSound(s, sfx_s3k4e)
					S_StartSound(s, sfx_sprnsp)
				elseif (p.sam_shinesparkfly > 18)
				--Point Blank explosion range		
					if not G_GametypeHasSpectators() 
						if (p.sam_shinesparkfly < 21)
						P_RadiusAttack(s, s, 280*FRACUNIT)
						else
						P_RadiusAttack(s, s, 100*FRACUNIT)
						end
					elseif (p.sam_shinesparkfly < 20)
						for z in players.iterate
							if z and z.mo and (FixedHypot(FixedHypot((s.x+s.momx)-z.mo.x, (s.y+s.momy)-z.mo.y), s.z-z.mo.z) < 170*FRACUNIT)
							and not ((G_GametypeHasTeams() and (p.ctfteam == z.ctfteam)) or (z.mo == s))
							P_DamageMobj(z.mo, s, s)
							s.angle = R_PointToAngle2(s.x, s.y, z.mo.x, z.mo.y)
							p.sam_shinesparkstop = 5
							end
						end
					end
					if leveltime % 2 == 0		
					s.tempsamdye = SKINCOLOR_RED
					else
					s.tempsamdye = SKINCOLOR_ORANGE
					end
					s.tempsamdyetr = TR_TRANS10
					if ( ( (p.speed < FixedMul(39*FRACUNIT, s.scale) ) and (p.sam_shinesparkdirectx != 0) and not p.sammorphed) or ( p.sammorphed and (p.speed < 60*FRACUNIT ) and (p.sam_shinesparkdirectx != 0)) )
					or ( ( (p.sam_shinesparkdirectz < 0) and (s.momz > FixedMul(p.sam_shinesparkdirectz, s.scale)+FixedMul(11*FRACUNIT, s.scale)) ) or ( (p.sam_shinesparkdirectz > 0) and (s.momz < FixedMul(p.sam_shinesparkdirectz, s.scale)-FixedMul(11*FRACUNIT, s.scale)) ) )
					or (p.sam_energy <= 29) or (p.powers[pw_carry])
						if p.sam_shinesparkstop == nil
						p.sam_shinesparkstop = 0
						end
						if (p.sam_shinesparkstop > 3)
							p.sam_spacejumped = false
							p.sam_shinesparkfly = 0
							p.sam_shinespark = 0
							s.flags = $ & ~MF_BOUNCE
							P_RadiusAttack(s, s, 300*FRACUNIT)
							s.momx = 0
							s.momy = 0
							p.sam_shinesparkstop = 0
							s.state = S_PLAY_FALL
							P_SetObjectMomZ(s, 4*FRACUNIT*p.sampers)
							P_SamFP(p, PAL_NUKE, 2)
							S_StartSound(s, sfx_sprnh)
							if s.sam_deathball
							local sdb = P_SpawnMobj(s.x, s.y, s.z, MT_SAMSUPERMISSILE)
									if sdb and sdb.valid and sdb.health
									sdb.target = s
									sdb.scale = 2*FRACUNIT
 									P_ExplodeMissile(sdb)
									end
							end
							if not p.powers[pw_flashing] 
							p.powers[pw_flashing] = 30 
							end	
						else
							if (s.z > s.floorz+20*FRACUNIT)
							p.sam_shinesparkstop = $+5
							else
							p.sam_shinesparkstop = $+1
							end
						end
					end
				end
				if ((p.sam_shinesparkfly > 18) and not (leveltime % 2 == 0))
				AttractSamPickUps(s, 750*FRACUNIT, 4)
				P_SetObjectMomZ(s, p.sam_shinesparkdirectz*p.sampers)
				s.angle = p.sam_shinesparkdirect
				p.powers[pw_nocontrol] = 2
				P_InstaThrust(s, p.sam_shinesparkdirect, p.sam_shinesparkdirectx)
				P_SpawnGhostMobj(s)
					if not p.sam_shinesparkdirectx
					local ghs = P_SpawnMobj(s.x+FixedMul(-25*FRACUNIT, cos(s.angle+ANG10+ANG2)), s.y+FixedMul(-25*FRACUNIT, sin (s.angle+ANG10+ANG2)), s.z+( ( FixedMul(s.height*5/7, s.scale) ) * (P_MobjFlip(s)) ), MT_SAMCHARGEGFX)
					ghs.scale = s.scale/(P_RandomRange(10,20))
					ghs.color = SKINCOLOR_SKY
					ghs.momx = s.momx/2
					ghs.momy = s.momy/2
					ghs = P_SpawnMobj(s.x+FixedMul(-25*FRACUNIT, cos(s.angle-ANG10-ANG2)), s.y+FixedMul(-25*FRACUNIT, sin (s.angle-ANG10-ANG2)), s.z+( ( FixedMul(s.height*5/7, s.scale) ) * (P_MobjFlip(s)) ), MT_SAMCHARGEGFX)
					ghs.scale = s.scale/(P_RandomRange(10,20))
					ghs.color = SKINCOLOR_SKY
					ghs.momx = s.momx/2
					ghs.momy = s.momy/2
					else
						if not p.sammorphed
						s.sams_shineframe = 5
						s.tics = 3
						end							
						local ghs = P_SpawnMobj(s.x, s.y, s.z+((s.height/2)*P_MobjFlip(s)), MT_PBEXPANDW)
						ghs.scale = s.scale*2
						ghs.scalespeed = $*(P_RandomRange(2,10))
						ghs.destscale = s.scale*25
					end
					if not (p.sam_energy <= 29) and not (p.exiting or (p.pflags & PF_FINISHED))
						if p.fusionmode
							if (leveltime % 5 == 0) --In Fusion Mode your energy drains slower. An actually positive effect!
							p.sam_energy = $-1
							end
						else
						p.sam_energy = $-1
						end
					end
					if (p.speed >= FixedMul(33*FRACUNIT, s.scale)) and (s.z == s.floorz)
						if p.sammorphed and not s.morphedmachactive
						P_InstaThrust(s, s.angle, 75*FRACUNIT)
						s.smmachball = p.speed
						s.smmachballspbs = true
						else
						s.state = S_PLAY_WALK
						p.normalspeed = 75*FRACUNIT						
						end
						p.sam_shinesparkfly = 0
						p.sam_shinesparkstop = 0
						p.sam_shinespark = 0
						p.samspbscharge = 250
						p.sampbschargefull = true
					end
				end
			end
		--Adjust thrustfactor on slopes so you don't get easily stuck on them like hedgehogs. Samus was around when traction was invented.
				if s.standingslope and (p.speed <= 5*FRACUNIT) and P_IsObjectOnGround(s)
				and ( (s.standingslope.zdelta > 26000) or (s.standingslope.zdelta < -26000) )
				and (p.cmd.forwardmove or p.cmd.sidemove) and not p.sammorphed and (p.thrustfactor < 11)
					p.thrustfactor = 11
						if ( (s.standingslope.zdelta > 60000) or (s.standingslope.zdelta < -60000) )
						p.thrustfactor = 15
						end
				end
				
				if p.powers[pw_carry] == CR_DUSTDEVIL
					s.samdustdevil = true //SRB2 doesn't properly scale tornado jumps, alongside many other things. Gotta do it manually.
				elseif s.samdustdevil
					if not P_IsObjectOnGround(s) and not p.sammorphed
					p.pflags = $|PF_JUMPED
					P_SetObjectMomZ(s, 3*FRACUNIT, true)
						if (p.cmd.forwardmove) or (p.cmd.sidemove) -- Allow a bit more speed build up too.
						s.momx = $*10/9 s.momy = $*10/9
						end				
					end
					s.samdustdevil = false
				end				
	--Recreate the Mach/Mock Ball from Super Metroid! Morphine!
			if s.smmachball 
				if (s.smmachball < 38*FRACUNIT)
				s.smmachballwait = 0
				s.smmachball = 0
				s.smmachballspbs = false
				
				elseif p.sammorphed 
				and (((s.eflags & MFE_JUSTHITFLOOR) 
				or (((s.z < s.floorz+30*FRACUNIT) and not (s.eflags & MFE_VERTICALFLIP)) or ((s.z > s.ceilingz-30*FRACUNIT) and (s.eflags & MFE_VERTICALFLIP)))
				and (s.momz*P_MobjFlip(s) < 2*FRACUNIT) and not P_IsObjectOnGround(s)))	
				
				s.morphedmachspeed = s.smmachball
				s.morphedmachactive = true
				s.smmachball = 0
				s.smmachballwait = 0
				s.initialmockbspeed = p.speed
				s.smmachballpw = 0
				P_Thrust(s, s.angle, 20*FRACUNIT) --initial boost!
				P_NukeEnemies(s, s, 0)
				S_StartSound(s, sfx_sballf)
					for d = -4,4
						local ghs = P_SpawnMobj(s.x, s.y, s.z, MT_PARTICLE)
						P_Thrust(ghs, s.angle+ANGLE_45*d, 10*FRACUNIT) 
						ghs.fuse = 10
					end
				end
				
				if not s.smmachballwait
					s.smmachballwait = s.smmachballwait and $+1 or 1
				elseif s.smmachballwait > 3 --We'll give you a 4 tic window! Slightly higher than Super Metroid's.
					s.smmachballwait = 0
					s.smmachball = 0
					s.smmachballspbs = false
				elseif s.smmachballwait
					s.smmachballwait = $+1		
				end
			elseif s.smmachballwait
				s.smmachballwait = 0
			end
			if s.morphedmachspeed and s.morphedmachactive					
			
				if p.samspbs and s.smmachballspbs
					if CheckForShineSpark(s, p)
						s.morphedmachspeed = 0
						s.morphedmachactive = false
					end
				end
				
				p.normalspeed = s.morphedmachspeed*14/9
				
				if p.samspbs and s.smmachballspbs
					p.samspbscharge = 250
					p.sampbschargefull = true
				end

				if (p.speed < (s.initialmockbspeed/2)) or (p.pflags & PF_STARTJUMP) or P_PlayerInPain(p)
				or p.powers[pw_carry] or p.bsblboost // or (p.cmd.sidemove == 0 and p.cmd.forwardmove == 0) 
					s.morphedmachspeed = 0			
					s.smmachballpw = 0
				else 
					if (p.speed > 38*FRACUNIT)
						if s.smmachballpw
							s.smmachballpw = $-1
							s.omegarushpanic = 2
							local ghs = P_SpawnMobj(s.x, s.y, s.z+((s.height/2)*P_MobjFlip(s)), MT_PBEXPANDW)
							ghs.scale = s.scale*P_RandomRange(2,4)
							ghs.scalespeed = $*(P_RandomRange(1,20))
							ghs.destscale = $*25
						end
						
						if p.samspbs and s.smmachballspbs
						
							SpeedBoosterGhosts(s, p)
						
						else
						
							local ghs = P_SpawnGhostMobj(s)
							ghs.tics = 3
							if leveltime % 2 == 0
								ghs.color = SKINCOLOR_WAVE
							else
								ghs.color = SKINCOLOR_GREY		
							end						
							ghs.colorized = true
						end
						
					end
				end
			end
			
			if p.sam_power_grip	
				if not (s.sam_pwgrp and s.sam_pwgrp.valid)
					s.sam_pwgrp = P_SpawnMobjFromMobj(s, 0, 0, 0, MT_PWGRPLD)
				end
				if not (s.sam_pwgrpS and s.sam_pwgrpS.valid)
					s.sam_pwgrpS = P_SpawnMobj(s.x, s.y, s.z, MT_PWGRPLD)
				end
				
				if s.sam_pwgrp and s.sam_pwgrp.valid
				and s.sam_pwgrpS and s.sam_pwgrpS.valid
				
					s.sam_pwgrp.flags2 = $|MF2_DONTDRAW
					s.sam_pwgrp.target = s
					s.sam_pwgrp.scale = s.scale
					s.sam_pwgrp.tics = 3 
					s.sam_pwgrp.fuse = 3
				
					if (s.eflags & MFE_VERTICALFLIP)
						P_SetOrigin(s.sam_pwgrp, s.x, s.y, s.z-(s.height*5/3))
						s.sam_pwgrp.eflags = $|MFE_VERTICALFLIP
					else
						P_SetOrigin(s.sam_pwgrp, s.x, s.y, s.z+(s.height*5/3))
						s.sam_pwgrp.eflags = $ & ~MFE_VERTICALFLIP
					end
					
					s.sam_pwgrpS.flags2 = $|MF2_DONTDRAW
					s.sam_pwgrpS.target = s
					s.sam_pwgrpS.scale = FRACUNIT
					s.sam_pwgrpS.tics = 3
					s.sam_pwgrpS.fuse = 3
				
					if (s.eflags & MFE_VERTICALFLIP)
						P_SetOrigin(s.sam_pwgrpS, s.x, s.y, s.z-(s.height*5/4))
						s.sam_pwgrpS.eflags = $|MFE_VERTICALFLIP
					else
						P_SetOrigin(s.sam_pwgrpS, s.x, s.y, s.z+(s.height*5/4))
						s.sam_pwgrpS.eflags = $ & ~MFE_VERTICALFLIP
					end

					if (s.momz*P_MobjFlip(s) < -(s.scale*2/3))
					or ((p.cmd.buttons & BT_JUMP) and (p.sams_jumpcd == nil or p.sams_jumpcd < 11) and not ((p.pflags & PF_STARTJUMP) and s.momz*P_MobjFlip(s) > 0) )
					
						if ((p.cmd.forwardmove > 38) or (p.pflags & PF_ANALOGMODE) and (abs(p.cmd.sidemove) > 25 or abs(p.cmd.forwardmove) > 25) )
						and (p.panim != PA_PAIN)
						and not (P_IsObjectOnGround(s) or s.sam_ldclmb or p.powers[pw_carry] or p.sam_shinespark)
						and not (p.pflags & (PF_SPINNING|PF_STARTDASH|PF_SLIDING|PF_FULLSTASIS|PF_JUMPSTASIS))
						and not (s.sam_automob and s.sam_automob.valid)
						and not p.exiting
						and not p.beampain
						and not p.sam_shinesparkfly 
						and not s.crystalflashanim 
						and not p.sam_intoship
						and not p.sam_screwattacking
						and not s.sam_screwattackfling

						local ledgeang = (p.cmd.angleturn<<16)+(R_PointToAngle2(0, 0, p.cmd.forwardmove<<16, -(p.cmd.sidemove<<16)))	
					
							if ((s.flags2 & MF2_TWOD) or twodlevel) 
								if (p.cmd.sidemove > 0) 
									ledgeang = 0
								else
									ledgeang = ANGLE_180
								end
							end
							
							P_TryMove(s.sam_pwgrp, s.x+FixedMul(28*s.scale, cos(ledgeang) ), s.y+FixedMul(28*s.scale, sin (ledgeang) ), true)
							if (s.eflags & MFE_VERTICALFLIP)
								if ( (s.sam_pwgrp.z+31*s.scale) > s.sam_pwgrp.ceilingz)
									s.sam_pwgrp.z = s.sam_pwgrp.ceilingz
								else
									s.sam_pwgrp.z = $+(31*s.scale)
								end
							else
								if ( (s.sam_pwgrp.z-31*s.scale) < s.sam_pwgrp.floorz)
									s.sam_pwgrp.z = s.sam_pwgrp.floorz
								else
									s.sam_pwgrp.z = $-(31*s.scale)
								end
							end
							
							P_TryMove(s.sam_pwgrpS, s.x+FixedMul(36*FRACUNIT, cos(ledgeang) ), s.y+FixedMul(36*FRACUNIT, sin (ledgeang) ), true)
							if (s.eflags & MFE_VERTICALFLIP)
								if ( (s.sam_pwgrpS.z+56*FRACUNIT) > s.sam_pwgrpS.ceilingz)
									s.sam_pwgrpS.z = s.sam_pwgrpS.ceilingz
								else
									s.sam_pwgrpS.z = $+(56*FRACUNIT)
								end
							else
								if ( (s.sam_pwgrpS.z-56*FRACUNIT) < s.sam_pwgrpS.floorz)
									s.sam_pwgrpS.z = s.sam_pwgrpS.floorz
								else
									s.sam_pwgrpS.z = $-(56*FRACUNIT)
								end
							end
						end
					end
				end
			end

			--We'll have to make a manual system nowadays to see if players changed their chasecam every frame.
			
			local chaseforce = nil
			local samchasecam = CV_FindVar("chasecam").value
			
			if p.sammorphed --Main times when we want to stay in 3rd person unless the player switches manually.
			or s.sam_screwattackfling			
			or twodlevel or (s.flags2 & MF2_TWOD) or p.threedimensionalexplosion
				chaseforce = true
				p.sam_prevcam = nil
			else
				if p.sam_prevcam == nil --Wait 1 frame before deciding what camera to switch to.
					p.sam_prevcam = samchasecam
				else
					if p.sam_prevcam != samchasecam --Do we need to change camera??
						if samchasecam == 0 and p.sam_prevcam != 0
							p.sam_firstperson = false
							SamCam(p, false)
						elseif not p.sam_prevcam
							if not p.sam_firstperson
								p.sam_firstperson = true
								p.visorclosein = 15
							end
							SamCam(p, true)
						end
						p.sam_prevcam = nil --We're done here.
					else
						p.sam_prevcam = samchasecam
					end
				end
			end
			
			if p.sam_spacejumpedsound
				p.sam_spacejumpedsound = $-1
				if p.sam_spacejumpedsound == 1 and not p.sam_walljumpsound
				S_StartSound(s, sfx_sjmp)
				end
			end
			if p.sugoigolfmode
				p.pflags = $|PF_JUMPSTASIS
				p.charability2 = CA2_SPINDASH
			end
			if (p.cmd.buttons & BT_CUSTOM3)
				p.sams_c3cd = true
			elseif p.sams_c3cd
				p.sams_c3cd = false
			end
			
		elseif not YouAreSamus(s) and s.valid --Now the non-Samus stuff.
		
			if s.samusscaled --s.otherchar s.NotSamus. Usually right as you switch from Samus to another char.
				p.sams_beamcombocharge = 0
				s.samusscaled = false
				if p.sam_firstperson --Un-first person when another character.
					if not (gametyperules&GTR_FIRSTPERSON)
						SamCam(p, true)
						p.sam_prevcam = nil
					end
				end
				if (p.samxtralife != nil)
					p.xtralife = p.samxtralife
					p.samxtralife = nil
				end
				p.viewrollangle = 0
				if (s.scale > FRACUNIT) --If beeg, be not beeg
					s.scale = FRACUNIT
				end
				if s.gravitylights and s.gravitylights.valid --Disable Gravity Suit lights.
					s.gravitylights.state = S_NULL
				end
				if ((p.skincolor >= SKINCOLOR_SUPERSILVER1) and (p.skincolor <= SKINCOLOR_SUPERTAN5))
					p.skincolor = SKINCOLOR_BLUE --Fixing a weird bug where you're stuck with colors...
					s.color = SKINCOLOR_BLUE
				end
			end
			
			--Don't carry morph ball stuff to other characters.
			if p.sammorphed
			p.sammorphed = false
			p.morphanim = false
			p.sams_beamcombocharge = 0
			p.viewrollangle = 0
				if s.scale == 42001 --if smol, be not smol
					s.scale = FRACUNIT
				end
				if s.friction == 61777
					s.flags = $ & ~MF_BOUNCE
					s.state = S_PLAY_FALL
				end
			end
			
			--No switching to other characters in the Hunters gamemode. Play Match for that instead.
			if (gametype == GT_HUNTERS)
				s.momx = 0 s.momy = 0
				s.state = S_PLAY_STND
				p.pflags = $ & ~PF_JUMPED
				R_SetPlayerSkin(p, "samus")
				s.skin = "samus"
				SAM_COM_BufInsertText(p, "skin samus")
				P_SamFP(p, PAL_NUKE, 4)
				S_StartSound(s, sfx_samspa, p)
				if not p.weapondelay
					CONS_Printf(p, "\x85"+"Hunters is a Samus-focused gamemode. Character crossplay is available in Match!")
				end
				
				p.powers[pw_nocontrol] = 9
				p.weapondelay = 9
				p.tossdelay = 9
			end
		end	
end, MT_PLAYER)

--Space Jump Ability!
addHook("AbilitySpecial", function(p)
	if p.mo
local s = p.mo
		if (s.health and YouAreSamus(s)) and p.sam_spacejump 

			if not (p.sam_spacejumped or p.sammorphed or p.powers[pw_nocontrol] or p.exiting or p.sam_walljumpsound or (s.sam_ldclmb and s.sam_ldclmb > 20))
			local ghs
				if (s.eflags & MFE_UNDERWATER)
				P_SetObjectMomZ(s, 4*FRACUNIT)
				else
				P_SetObjectMomZ(s, 8*FRACUNIT)
				end

				if (P_MobjFlip(s) == 1)
				ghs = P_SpawnMobj(s.x, s.y, s.z+15*FRACUNIT, MT_SPACEJUMPWAVE)
				else
				ghs = P_SpawnMobj(s.x, s.y, s.z+47*FRACUNIT, MT_SPACEJUMPWAVE)
				end
				
				ghs.scale = s.scale/3
				ghs.destscale = $*22
				ghs.scalespeed = $/2
				ghs.blendmode = AST_ADD
				ghs.color = SKINCOLOR_CLOUDY
				ghs.momx = s.momx/2 ghs.momy = s.momy/2 ghs.momz = s.momz/2

				if (P_MobjFlip(s) == 1)
				ghs = P_SpawnMobj(s.x, s.y, s.z+17*FRACUNIT, MT_SPACEJUMPWAVE)
				else
				ghs = P_SpawnMobj(s.x, s.y, s.z+45*FRACUNIT, MT_SPACEJUMPWAVE)
				end

				ghs.scale = s.scale/5
				ghs.destscale = $*22
				ghs.scalespeed = $*10/25
				ghs.color = SKINCOLOR_GREY
				ghs.momx = s.momx/2 ghs.momy = s.momy/2
				ghs.momz = s.momz/2-((3*FRACUNIT)*P_MobjFlip(s))

				p.sam_spacejumped = true
				p.sam_walljumping = 13
				p.sam_j1ready = 0
				s.state = S_PLAY_ROLL
				p.sam_spacejumpedboost = 6
				p.sam_spacejumpedsound = 4
			end
		end		
	end
end, MT_PLAYER)

addHook("MobjThinker", function(s) --Display only for player using the lockon
	if displayplayer and s ~= displayplayer.automobf
	and not (s.flags2 & MF2_DONTDRAW)
	and not s.cancellock
    	s.flags2 = $|MF2_DONTDRAW
	end
end, MT_SAMUSLOCKGFX)

addHook("JumpSpecial", function(p)
	if p.mo
local s = p.mo
		if (s.health and YouAreSamus(s)) and p.sammorphed

			if (p.powers[pw_carry] == CR_MACESPIN) --Mace Spin fix!
			and not p.sam_highjump and not p.powers[pw_nocontrol]
			P_DoJump(p, false)
			p.pflags = $|PF_JUMPED
			p.powers[pw_carry] = $ & ~CR_MACESPIN
			P_SetObjectMomZ(s, 595000, true)
			p.dragdownball = 30
			s.momx = $*10/6
			s.momy = $*10/6
			s.momz = $*10/7
			p.powers[pw_flashing] = 16
			return true

			elseif p.sam_highjump --Spring Ball!
			and (P_IsObjectOnGround(s) or ((p.pflags & PF_ANALOGMODE) and (leveltime % 5 == 0)
			and (p.speed > 18*FRACUNIT) and (s.z < s.floorz+3)))
			and not p.powers[pw_nocontrol] and not p.exiting and not (p.powers[pw_carry] == CR_NIGHTSMODE)
			and not (p.pflags & (PF_JUMPED|PF_JUMPDOWN|PF_JUMPSTASIS|PF_FULLSTASIS)) 
			
			P_DoJump(p, false)
			SpaceJumpGFX(s, SKINCOLOR_SKY, 2, L|TR_TRANS20)
			s.sam_bufferjump = 0
			
			if p.bsblboost and (p.bsblboost > 3)
			P_SetObjectMomZ(s, 260000, true)
			else
			P_SetObjectMomZ(s, 195000, true)
			end
			
			if s.standingslope and not (s.standingslope.flags & SL_NOPHYSICS) and not s.samspinpad and ( (s.standingslope.zdelta > 4000) or (s.standingslope.zdelta < -4000)) and not 
			( ((s.prevsz != nil and (s.z < s.prevsz-233333)) and not (s.eflags & MFE_VERTICALFLIP)) or ((s.eflags & MFE_VERTICALFLIP) and (s.prevsz != nil and (s.z > s.prevsz+233333))) ) 
			P_SetObjectMomZ(s, 99000, true)
			end
			p.dragdownball = 30
			S_StartSound(s, sfx_sballj)
			return true
			end
		end		
	end
end, MT_PLAYER)