--===========================THINKFRAMES================================
--PreThinkFrame, PostThinkFrame, and maybe a ThinkFrame.
--Also dumping special hooks in here, like MapLoad, AbilitySpecial and such.
--======================================================================

--Make sure your Suit Lights are in the proper frames!
local function UpdateSuitLights(p,s)
	if not (s.gravitylights and s.gravitylights.valid)
		s.gravitylights = P_SpawnMobj(s.x, s.y, s.z, MT_OVERLAY)
	else
		P_MoveOrigin(s.gravitylights, s.x, s.y, s.z)
	end
	if s.gravitylights and s.gravitylights.valid
		local LIGHTS = s.gravitylights
		LIGHTS.skin = "samus" --Skin must always be Samus! BaseSamus has no Super sprites!
		if (s.sprite == SPR_SAMU)
			LIGHTS.sprite = SPR_YGTM
		else --if p.sam_gravitysuit and not (s.sprite == SPR_SAMU)// and not (s.sprite == SPR_ZSSA)
			LIGHTS.sprite = SPR_PLAY
			LIGHTS.sprite2 = s.sprite2|FF_SPR2SUPER
		end
		LIGHTS.target = s
		LIGHTS.skin = "samus"
		LIGHTS.sprite2 = s.sprite2|FF_SPR2SUPER		
		if p.gltrans --Set frame!
			local TRANS = (p.gltrans) or 0
			if s.orangespring
				LIGHTS.frame = (s.frame & ~FF_FRAMEMASK)|TRANS
				if p.sammorphed
					MorphballReturn(s)
				end
			else
				LIGHTS.frame = s.frame|TRANS
			end
		else
			LIGHTS.frame = (s.frame & FF_FRAMEMASK)|FF_TRANS70 --FAILSAFE!
		end		
		LIGHTS.tics,LIGHTS.fuse = 2,2	
		LIGHTS.renderflags = RF_FULLBRIGHT|RF_NOCOLORMAPS
		LIGHTS.flags = $|MF_NOBLOCKMAP|MF_NOCLIPTHING|MF_NOCLIP|MF_NOCLIPHEIGHT|MF_NOGRAVITY
		LIGHTS.flags2 = s.flags2
		LIGHTS.eflags = (s.eflags & MFE_VERTICALFLIP) and $|MFE_VERTICALFLIP or $ & ~MFE_VERTICALFLIP
		LIGHTS.rollangle = s.rollangle
		LIGHTS.spritexscale,LIGHTS.spriteyscale = s.spritexscale,s.spriteyscale
		LIGHTS.spritexoffset,LIGHTS.spriteyoffset = s.spritexoffset,s.spriteyoffset
		LIGHTS.momx,LIGHTS.momy,LIGHTS.momz = s.momx,s.momy,s.momz
		LIGHTS.angle = p.drawangle
		LIGHTS.dispoffset = 4
		LIGHTS.mirrored = s.mirrored
		LIGHTS.samtracer = p --We're tracing the player instead of the mo...?	
		
		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
		or (s.sprite != SPR_PLAY) and (s.sprite != SPR_SAMU)
		or not YouAreSamus(s)
			LIGHTS.sprite = SPR_NULL
		else
			if s.sam_scanning and (s.sam_scanning % 2 == 0)
				LIGHTS.color = SKINCOLOR_SUPERSKY3
				LIGHTS.frame = s.frame
			else
				LIGHTS.color = ((p.samvisorcolor) and p.sam_gravitysuit==2) and p.samvisorcolor or SKINCOLOR_EMERALD
			end
			if not (s.flags & MF2_DONTDRAW) and not p.sammorphed 
				if p==displayplayer and not CV_FindVar("chasecam").value and not splitscreen
					LIGHTS.flags2 = $|MF2_DONTDRAW
				end
			end
			A_CapeChase(LIGHTS,0,0)
		end
	end
end

addHook("PostThinkFrame", do for p in players.iterate if not p.valid continue end --
	if (p == server) and not p.samusloadconfig2 --Kind of important to load ASAP....
		AdminSamusSave(p, false, true)
		p.samusloadconfig2 = true
	end
	if p.mo and YouAreSamus(p.mo)
		local s = p.mo
		if p.sammorphed
			if 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
				p.morphanim = ((s.state >= S_SAMUMB1) and (s.state <= S_SAMUMB4))	
				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
				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<<16)
					p.sammorphanimcd = $-1
				elseif (p.speed < 10<<16)
					p.sammorphanimcd = $-2
				elseif (p.speed < 15<<16)
					p.sammorphanimcd = $-3
				elseif (p.speed < 20<<16)
					p.sammorphanimcd = $-4
				elseif (p.speed < 25<<16)
					p.sammorphanimcd = $-5
				elseif (p.speed < 30<<16)
					p.sammorphanimcd = $-6
				elseif (p.speed < 35<<16)
					p.sammorphanimcd = $-7
				elseif (p.speed >= 35<<16)
					p.sammorphanimcd = $-8
				end				
			end
		--	if s.sprite == SPR_PLAY
		--		s.sprite = SPR_SAMU
		--		s.frame = ($ & ~FF_FRAMEMASK) | ( (leveltime%4==0) and E or (leveltime%3==0) and D or (leveltime%2==0) and C or B )
		--	end
		elseif s.sams_shineframe
			s.sprite2,s.frame = SPR2_FLT, $ & ~FF_FRAMEMASK
		elseif s.state == S_SAMUFIRE
			s.frame = ($ & ~FF_FRAMEMASK) | ((not p.sam_gravitysuit) and F or A) 
		end
		p.weapondelay = max($, 9)
		p.currentweapon = 0		
		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_THOK)
				local GLOW = s.bsblglow
				GLOW.blendmode = AST_ADD
				GLOW.dispoffset = 50
				GLOW.renderflags = RF_FULLBRIGHT|RF_NOCOLORMAPS
				GLOW.state = S_THOKGLOW
				GLOW.spriteyoffset = -4<<16
				GLOW.color = SKINCOLOR_SUPERGOLD4
			end
			local TRANS = (s.sambsblchargelvl < 4) and FF_TRANS90 or (s.sambsblchargelvl < 8) and FF_TRANS80
			or (s.sambsblchargelvl < 12) and FF_TRANS70 or FF_TRANS50
			
			local GLOW = s.bsblglow
			GLOW.target,GLOW.tracer = s,s
			GLOW.momz = s.momz
			if (leveltime%2==0) or (s.sambsblchargelvl < 12)
				GLOW.spritexscale,GLOW.spriteyscale = s.spritexscale,s.spriteyscale
			else
				GLOW.spritexscale,GLOW.spriteyscale = $+500,$+500
				TRANS = min(FF_TRANS90, $+FRACUNIT)
			end
			GLOW.frame = A|TRANS
			GLOW.scale = s.scale
			GLOW.tics,GLOW.fuse = 3,3		
			if (p.sams_c1cd >= 24) or (p.sams_firecd > 24)
				p.sambsblchargefull = true
			elseif p.sams_c1cd == 11 or p.sams_firecd == 11
				S_StartSound(s, sfx_spbsc)
			end
			A_CapeChase(GLOW, 1, 0)
		end
		if p.powers[pw_shield]
			MetroidVanguard.PowerUpConvert(p,s)
		end

		UpdateSuitLights(p,s) --"Gravity" Lights

		if SAM_displayplayer(p)
			if not CV_FindVar("chasecam").value
				if not p.sammorphed 
				and not (s.eflags & MFE_VERTICALFLIP)
					if not ((s.z+s.height+s.momz+(66<<16)) <= s.ceilingz)
						p.viewz = $+(16<<16)
					else
						p.viewz = s.z+p.viewheight+p.bob
					end
				end
			end
		end
		if s.gravitylights and s.gravitylights.valid
			s.gravitylights.angle = p.drawangle
			if (s.gravitylights.z > s.z+250<<16) --WHAT THE HELL, MYSTIC REALM?! Get back down here!!
				s.gravitylights.z = s.z
			end
		end
	end
end end)

local function P_MobjFlip(s) --Preventing MobjFlip crash by overriding the function entirely.
	if s and s.valid and ((s.eflags & MFE_VERTICALFLIP) or (s.flags2 & MF2_OBJECTFLIP))
		return -1 --
	end return 1 --
end

--Space Jump Ability!
addHook("AbilitySpecial", function(p)
	if not p.sam_spacejump return end
	local s = p.mo
	
	if s and s.valid and YouAreSamus(s)
		--None of these can be active!
		if p.sam_spacejumped 
		or p.sammorphed
		or p.sam_walljumpsound
		or s.sam_ldclmb and (s.sam_ldclmb > 20)
		or not s.health		
		or p.powers[pw_nocontrol]
		or p.exiting
			return --
		end
		
		P_SetObjectMomZ(s, (s.eflags & MFE_UNDERWATER) and 4<<16 or 8<<16)
		
		local ZOFFSET = (P_MobjFlip(s)==1) and 47<<16 or 15<<16
		--Spawn 2 jumpwaves.
		local ghs = P_SpawnMobj(s.x, s.y, s.z+ZOFFSET, MT_SPACEJUMPWAVE)
		ghs.scale = s.scale/3
		ghs.destscale = $*22
		ghs.scalespeed = 9730
		ghs.blendmode = AST_ADD
		ghs.color = SKINCOLOR_CLOUDY
		ghs.momx,ghs.momy,ghs.momz = s.momx/2,s.momy/2,-s.momz/3
		
		if ZOFFSET == (47<<16) ZOFFSET = 45<<16 end
		ghs = P_SpawnMobj(s.x, s.y, s.z+ZOFFSET, MT_SPACEJUMPWAVE)
		ghs.scale = s.scale/5
		ghs.destscale = $*22
		ghs.scalespeed = 7184
		ghs.color = SKINCOLOR_GREY
		ghs.momx,ghs.momy = s.momx/2,s.momy/2
		ghs.momz = -s.momz/3 - (3<<16*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 --A synced variable since checking for sounds is a recipe for desyncs!
	end
end)

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

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

		if p.sam_highjump --Spring Ball!
			local ALT = (P_IsObjectOnGround(s)) and true or false
			if ALT != true and ((p.pflags & PF_ANALOGMODE) and (leveltime % 5 == 0))
				if p.speed > 18<<16
					if (s.eflags & MFE_VERTICALFLIP)
						if (s.z+s.height >= s.ceilingz-99)
							ALT = true
						end
					elseif (s.z < s.floorz+3)
						ALT = true
					end	
				end
			end
			if ALT == true
				if 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|FF_TRANS20)
					s.sam_bufferjump = 0
					
					local BOOST = (p.bsblboost) or 0
					P_SetObjectMomZ(s, (BOOST>3) and 260000 or 195000, true)
					
					--The Spring Ball brings beneficial slope-jump adjustments compared to regular jumps.
					if s.standingslope and (abs(s.standingslope.zdelta) > 4000) 
					and not (s.standingslope.flags & SL_NOPHYSICS)
					and not s.samspinpad
						if s.prevsz != nil
							if not (s.eflags & MFE_VERTICALFLIP)
								if (s.z < s.prevsz-233333)
									P_SetObjectMomZ(s, 99000, true)
								end
							elseif (s.z > s.prevsz+233333)
								P_SetObjectMomZ(s, 99000, true)
							end
						end
						--Extra-jumping speed from slopes in all directions
						if (p.cmd.forwardmove > 25) and (p.speed < 32*s.scale) 
							local MAXTHRUST = max(s.scale/3, abs(s.standingslope.zdelta))
							MAXTHRUST = FixedDiv(FixedMul($, p.cmd.forwardmove), 50)
							for i = 1,10
								if (p.speed > 32*s.scale) break end
								P_Thrust(s, s.angle, MAXTHRUST)
							end
						end
					end
					p.dragdownball = 30
					S_StartSound(s, sfx_sballj)
					return true --
				end
			end
		elseif (p.powers[pw_carry] == CR_MACESPIN) --Mace Spin fix!
		and not p.powers[pw_nocontrol]
		
			P_DoJump(p, false)
			p.pflags = $|PF_JUMPED & ~(PF_NOJUMPDAMAGE|PF_THOKKED)
			p.powers[pw_carry] = 0
			P_SetObjectMomZ(s, 595000, true)
			p.dragdownball = 30
			s.momx,s.momy,s.momz = $*10/6,$*10/6,$*10/7
			p.powers[pw_flashing] = 16
--			return true --			
		end
		return true --
	end
end)

local SAM = MetroidVanguard
local HOST = SAM.HOST

addHook("MapLoad", function(gamemap)
	if G_GametypeHasSpectators() and not (gamemap == 543)
		if HOST and HOST.valid
			SAM.UpdateScoreBracket(HOST, true)
		end
		if HOST and HOST.valid
		and HOST.sammatchmonitors
			local TOTAL = 0
			for m in mobjs.iterate(mobjs)
				if m.valid and (m.flags & MF_MONITOR) and m.health
					TOTAL = $+1
				end
			end
			if not (TOTAL > 9) --Map needs a minimum of 10 monitors to be considered playable. Otherwise we add extra.
				local MATCHMON1,MATCHMON2,MATCHMON3,MATCHMON4,MATCHMON5,MATCHMON6,MATCHMON7,MATCHMON8,MATCHMONr = 0,0,0,0,0,0,0,0,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.valid 
					and ((m.type == MT_RING) or (m.type == MT_COIN))
					and (m.z <= m.floorz+24<<16) and m.health		
						if MATCHMON1 == 0 and (P_RandomRange(1, 5)) == 2
							MATCHMON1 = P_SpawnMobj(m.x, m.y, m.z, MT_WHIRLWIND_BOX)
							m.flags2 = $|MF2_DONTRESPAWN 
							P_RemoveMobj(m)
						elseif MATCHMON2 == 0
							if MATCHMON1 != 0 and (FixedHypot(m.x-MATCHMON1.x, m.y-MATCHMON1.y) > (P_RandomRange(55, 1500)<<16))
								MATCHMON2 = P_SpawnMobj(m.x, m.y, m.z, MT_RING_BOX)
								m.flags2 = $|MF2_DONTRESPAWN
								P_RemoveMobj(m)
							end
						elseif MATCHMON3 == 0
							if MATCHMON2!=0 and (FixedHypot(m.x-MATCHMON2.x, m.y-MATCHMON2.y) > (P_RandomRange(111, 2000)<<16))
								MATCHMON3 = P_SpawnMobj(m.x, m.y, m.z, MT_ATTRACT_BOX )
								m.flags2 = $|MF2_DONTRESPAWN
								P_RemoveMobj(m)
							end
						elseif MATCHMON4 == 0
							if MATCHMON3!=0 and (FixedHypot(m.x-MATCHMON3.x, m.y-MATCHMON3.y) > (P_RandomRange(200, 2000)<<16))
								MATCHMON4 = P_SpawnMobj(m.x, m.y, m.z, MT_RING_BOX)
								m.flags2 = $|MF2_DONTRESPAWN
								P_RemoveMobj(m)
							end
						elseif MATCHMON5 == 0
							if MATCHMON4!=0 and (FixedHypot(m.x-MATCHMON4.x, m.y-MATCHMON4.y) > (P_RandomRange(900, 2500)<<16))
								MATCHMON5 = P_SpawnMobj(m.x, m.y, m.z, MT_ELEMENTAL_BOX)
								m.flags2 = $|MF2_DONTRESPAWN 
								P_RemoveMobj(m)
							end
						elseif MATCHMON6 == 0 
							if MATCHMON5!=0 and (FixedHypot(m.x-MATCHMON5.x, m.y-MATCHMON5.y) > (P_RandomRange(1400, 3500)<<16))
								MATCHMON6 = P_SpawnMobj(m.x, m.y, m.z, MT_RING_BOX)
								m.flags2 = $|MF2_DONTRESPAWN 
								P_RemoveMobj(m)
							end 
						elseif MATCHMON7 == 0
							if MATCHMON6!=0 and (FixedHypot(m.x-MATCHMON6.x, m.y-MATCHMON6.y) > (P_RandomRange(2500, 4500)<<16))
								MATCHMON7 = P_SpawnMobj(m.x, m.y, m.z, MT_SNEAKERS_BOX)
								m.flags2 = $|MF2_DONTRESPAWN
								P_RemoveMobj(m)
							end
						elseif MATCHMON8 == 0 
							if MATCHMON1!=0 and (FixedHypot(m.x-MATCHMON1.x, m.y-MATCHMON1.y) > (P_RandomRange(5000, 9000)<<16))
								MATCHMON8 = P_SpawnMobj(m.x, m.y, m.z, MT_RING_BOX)
								m.flags2 = $|MF2_DONTRESPAWN
								P_RemoveMobj(m) break --
							end 
						end
					end
				end
			end
		end
	end
	local SKIP = false
	if not (netgame) and (gametype == GT_COOP) and players[0]
		local p = players[0]
		 if (p.samuschosefile == nil or p.sam_saveselect)
			if p.mo and YouAreSamus(p.mo) 
				SKIP = true
			end
		end
	end
	if SKIP != true
		if HOST and HOST.valid and (HOST.sammatchmonitors or HOST.boxammospawns) //Turn boxes into ammo?
			local BOXAMMO = (HOST.boxammospawns) and true or false
			for m in mobjs.iterate(mobjs)
				if m.valid and (m.flags & MF_MONITOR) and m.health
					if not (m.type == MT_EGGMAN_BOX)
					and not (m.type == MT_EGGMAN_GOLDBOX)
						if BOXAMMO
							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)
						elseif not (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
		end
	end
	for p in players.iterate	
		p.samxbeam,p.samybeam = 0,0
		if gamemap == 812
			if not p.sam_lastlvl --Backups.
				p.sam_lastlvl = (spstage_start) and spstage_start or (bootmap) and bootmap or 1
			end
		else
			p.sam_lastlvl = gamemap	
		end
		if gamemap != 807 and gamemap != 806 and gamemap != 28
			p.superbrakdeath = nil
		end
		if p.camerascale == 196071 or p.camerascale == 261427 --Fix some special bugs.
			p.camerascale = FRACUNIT
		end
		if p.samhudmessagetime and (p.samhudmessagetime < 229)
			p.samhudmessagetime = 0	
			p.samhudmessageget = nil
		end
		local SPHERES = mapheaderinfo[gamemap].ssspheres or 0
		if (SPHERES > 1) or (maptol & TOL_NIGHTS) or 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] and p != players[0]
				p.sam_energytanksmax = players[0].sam_energytanksmax
			elseif HOST and HOST.valid and p != HOST
				p.sam_energytanksmax = HOST.sam_energytanksmax
			else
				p.sam_energytanksmax = 2
			end
		end	
		if G_GametypeHasSpectators()
			SamResetBeam(p)
			SAM.MatchSpawnWeapons(p,s)
		else
			p.sam_topscore = nil
			p.sam_topscoreman = nil
			if (gametype == GT_RACE)
				SamResetBeam(p)
				SAM.MatchSpawnWeapons(p,s)			
			end
		end
	end
	
	if SKIP != true
		SetMetroidVisuals(s)	
		if (gametype == GT_COOP) and not (gamemap == 28) --Bad monitor variety?
			local montype1,montype2,montype3,montype4,montype5,montype6,montype7,montype8,montype9 = 1,1,1,1,1,1,1,1,1
			for m in mobjs.iterate(mobjs)
				if m.valid and (m.flags & MF_MONITOR)
					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
			local MONITORTYPES = 0+montype1+montype2+montype3+montype4+montype5+montype6+montype7+montype8+montype9
			if (MONITORTYPES > 4) --Yeah, this level doesn't seem to have many monitors. Let enemies drop some.
				for m in mobjs.iterate(mobjs)
					if m.valid and m.flags != nil and (m.flags & MF_ENEMY)
					and not (m.flags & MF_BOSS) and m.health
						m.dropmonforsam = MONITORTYPES
					end
				end
			end	
		end
	end
end)

SAM.MatchSpawnWeapons = function(p,s)
	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 = (G_GametypeHasSpectators()) and true or 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 = true
		p.sammissilesmax = 10
		p.sammissiles = 5
		p.sampwmax = 0
		p.sampw = 0
		p.samsupermissilesmax = 2
		p.samsupermissiles = 0

	elseif p.samusmatch == 9 --Start with Weaponry, but nothing else. (shooter style match)

		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 = true
		
		p.sam_powerbeamcombo = (p.sam_beamcombosrestrict) and true or 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 = 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