--=============================ENEMY SYSTEMS=======================================
--Hooks for enemy damage, Hypermode/Fusion mode enemies, custom bosses, 
--and general Samus-specific enemy reactions are mostly clumped here. 
--=================================================================================

local SamOncePerMap5 = true
addHook("MapChange", function(gamemap)
	SamOncePerMap5 = false
end)

local function SetHyperMode(p, m, allversion)
if CBW_Chaos and (gametype == GT_CHAOS)
return
end
 if ( ( players[tonumber(0)] and (players[tonumber(0)].crazysamusmobs or players[tonumber(0)].fusionmode) )
 or ( players[tonumber(1)] and (players[tonumber(1)].crazysamusmobs or players[tonumber(1)].fusionmode) ) )
	if m.valid and ( allversion == nil or (m.flags and (m.flags & (MF_ENEMY|MF_BOSS))) )  // and m.health
		if not (m.smontracker and m.smontracker.valid and (m.smontracker.bolter or m.smontracker.floater or m.smontracker.machinegunner or m.smontracker.rammer or m.smontracker.flamer or m.smontracker.leecher))
			if not m.thebabyspawn
						if m.type == MT_YELLOWSHELL or m.type == MT_SPRINGSHELL or m.type == MT_GFZFISH or m.type == MT_BUGGLE
						or m.type == MT_EGGGUARD or m.type == MT_GSNAPPER or m.type == MT_HANGSTER

						m.smontracker = P_SpawnMobj(m.x, m.y, m.z, MT_SMONTRACKER)
						m.smontracker.target = m
						m.smontracker.bolter = true
						end

						if m.type == MT_REDCRAWLA or m.type == MT_VULTURE or m.type == MT_UNIDUS or m.type == MT_POINTY

						m.smontracker = P_SpawnMobj(m.x, m.y, m.z, MT_SMONTRACKER)
						m.smontracker.target = m
						m.smontracker.floater = true
						end
		

						if m.type == MT_SNAILER or m.type == MT_CRAWLACOMMANDER or m.type == MT_POPUPTURRET or m.type == MT_JETTGUNNER
						or m.type == MT_DRAGONBOMBER or m.type == MT_CACOLANTERN

						m.smontracker = P_SpawnMobj(m.x, m.y, m.z, MT_SMONTRACKER)
						m.smontracker.target = m
						m.smontracker.machinegunner = true
						end


						if m.type == MT_FACESTABBER  or m.type == MT_REDBUZZ or m.type == MT_JETTBOMBER or m.type == MT_GSNAPPER 
						or m.type == MT_SKIM or m.type == MT_JETJAW
						m.smontracker = P_SpawnMobj(m.x, m.y, m.z, MT_SMONTRACKER)
						m.smontracker.target = m
						m.smontracker.rammer = true
						end

						if m.type == MT_BLUECRAWLA or m.type == MT_MINUS or m.type == MT_ROBOHOOD or m.type == MT_PYREFLY
						or m.type == MT_POPHAT

						m.smontracker = P_SpawnMobj(m.x, m.y, m.z, MT_SMONTRACKER)
						m.smontracker.target = m
						m.smontracker.flamer = true
						end
						
						if m.type == MT_PTERABYTE or m.type == MT_CANARIVORE or m.type == MT_CRUSHSTACEAN or m.type == MT_BANPYURA or m.type == MT_JETJAW
						m.smontracker2 = P_SpawnMobj(m.x, m.y, m.z, MT_SMONTRACKER)
						m.smontracker2.target = m
						m.smontracker2.thebaby = true --Dem memes, tho
						end

						if m.type == MT_GOLDBUZZ

						m.smontracker = P_SpawnMobj(m.x, m.y, m.z, MT_SMONTRACKER)
						m.smontracker.target = m
						m.smontracker.leecher = true
						end
						
						if m.type == MT_PENGUINATOR or m.type == MT_HIVEELEMENTAL or m.type == MT_BUMBLEBORE or m.type == MT_SPINBOBERT
						m.smontracker = P_SpawnMobj(m.x, m.y, m.z, MT_SMONTRACKER)
						m.smontracker.target = m
						m.smontracker.thetank = true
						end
						
						if m.montargetedyet and not allversion --Someone purposely wants this re-activated, so....
						m.montargetedyet = false
						end
			end
		end
	end
 end
end

rawset(_G, "ActivateHyperMode", function(p, allmobs, bosscheck, force) --For starting Hyper Mode. Set allmobs to 7 to check all mobs in the map! Set bosscheck to check for boss
	if allmobs and tonumber(allmobs) != nil and allmobs == 7 --Is it 7? Then update ALL mobjs!
	and (force or not SamOncePerMap5) and not (gamemap == 806)
		for m in mobjs.iterate(mobjs)  
		
			SetHyperMode(p, m, true)
			
			if bosscheck and p.mo and m.valid and 
			( (m.type == MT_SAMUSSHIP) or (m.flags & MF_BOSS) or (m.type == MT_METALSONIC_RACE) )
			local s = p.mo
			
				if YouAreSamus(s)
					m.smontracker = P_SpawnMobj(m.x, m.y, m.z, MT_SMONTRACKER)
					m.smontracker.target = m
				end
				
				if ( (FixedHypot(m.x-s.x, m.y-s.y)) <= 6000*FRACUNIT) or (m.type == MT_SAMUSSHIP)
					for z in players.iterate
						z.spawnsship = false
						if netgame and (m.flags & MF_BOSS)
							z.sam_restricthyper = m
						else
							z.sam_restricthyper = nil
						end
						if z.mo
							z.mo.sam_sprstship = false
						end
					end
				end
			end
		end
		SamOncePerMap5 = true
	elseif allmobs and not allmobs == 7 and allmobs.valid --Update an individual mobj of your choosing.	
		SetHyperMode(p, allmobs)
	elseif (gamemap == 806)
		for z in players.iterate
			z.spawnsship = false
			if z.mo
				z.mo.sam_sprstship = false
			end
		end	
	end
end)
rawset(_G, "samcriticalfx", function(s, ang, i) --Mobj, angle to face, inflictor push(for screw attack/shinespark/etc) 
	for d = 1,16
	s.ghs = P_SpawnMobj(s.x, s.y, s.z+s.height/2, MT_SONIC3KBOSSEXPLODE)
	s.ghs.colorized = true
	s.ghs.tics = 1
	s.ghs.blendmode = AST_ADD
	s.ghs.color = P_RandomRange(SKINCOLOR_SUPERSILVER1, SKINCOLOR_SUPERGOLD3)
	s.ghs.flags = $|MF_NOCLIP|MF_NOCLIPHEIGHT
	s.ghs.flags2 = $|MF2_SHADOW
	if ang
	P_InstaThrust(s.ghs, ( (ang)+ANGLE_90 ) + ((ANGLE_180)*d) , (2*s.scale)*d)	
	elseif s.target and s.target.valid
	P_InstaThrust(s.ghs, ( (R_PointToAngle2(s.x, s.y, s.target.x, s.target.y))+ANGLE_90 ) + ((ANGLE_180)*d) , (2*s.scale)*d)
	else
	P_InstaThrust(s.ghs, (s.angle+ANGLE_90)+((ANGLE_180)*d), (2*s.scale)*d)
	end
		if (d > 8)
		P_SetObjectMomZ(s.ghs, (-1*s.scale)*d)
		else
		P_SetObjectMomZ(s.ghs, (2*s.scale)*d)
		end
	s.ghs.scale = s.scale*2
		if i
		s.ghs.momx = $+i.momx
		s.ghs.momy = $+i.momy	
		end
	end
	
	s.ghs = P_SpawnMobj(s.x, s.y, s.z, MT_THOK)
	s.ghs.flags2 = $|MF2_DONTDRAW
	s.ghs.tics = 44
	S_StartSound(s.ghs, sfx_smcrit)
end)

addHook("MobjThinker", function(s)
	if s.valid
		if s.superbrak
			if (s.health < 54) and not s.hdrop9
			s.hdrop9 = true
			s.health = 49
			end
			if (s.health < 36) and not s.hdrop1
			local samusrnd = P_RandomRange(1, 100)
			s.samspawneditem = P_SpawnMobj(s.x+(20*FRACUNIT)+(samusrnd*FRACUNIT), s.y+(samusrnd*FRACUNIT), s.z+(samusrnd*FRACUNIT/3), MT_MPKU)
			s.samspawneditem = P_SpawnMobj(s.x-(samusrnd*FRACUNIT), s.y-(samusrnd*FRACUNIT), s.z, MT_BMPKU)
			s.samspawneditem = P_SpawnMobj(s.x, s.y-(samusrnd*FRACUNIT), s.z+(samusrnd*FRACUNIT/2), MT_MPKU)
			s.samspawneditem = P_SpawnMobj(s.x+(samusrnd*FRACUNIT), s.y+(33*FRACUNIT), s.z+(samusrnd*FRACUNIT/4), MT_MPKU)
			s.hdrop1 = true
			S_StartSound(nil, sfx_behurt)
				for nsa in players.iterate
				P_SamFP(nsa, PAL_RECYCLE, 6)
					if not nsa.fusionmode and not nsa.crazysamusmobs
					s.health = 18 --Take a massive dive in health for our casual player
					elseif not nsa.crazysamusmobs
					s.health = 27 --Take a small dive in health for our player
					end
				end
			end
			if (s.health < 25) and not s.hdrop5
			local samusrnd = P_RandomRange(1, 100)
			s.samspawneditem = P_SpawnMobj(s.x+(20*FRACUNIT)+(samusrnd*FRACUNIT), s.y+(samusrnd*FRACUNIT), s.z+(samusrnd*FRACUNIT/3), MT_MPKU)
			s.samspawneditem = P_SpawnMobj(s.x-(samusrnd*FRACUNIT), s.y-(samusrnd*FRACUNIT), s.z, MT_BMPKU)
			s.samspawneditem = P_SpawnMobj(s.x, s.y-(samusrnd*FRACUNIT), s.z+(samusrnd*FRACUNIT/2), MT_BMPKU)
			s.samspawneditem = P_SpawnMobj(s.x+(samusrnd*FRACUNIT), s.y+(33*FRACUNIT), s.z+(samusrnd*FRACUNIT/4), MT_SPKU)
			s.hdrop5 = true
			s.sbexplodey = 68
			S_StartSound(nil, sfx_behurt)
				for nsa in players.iterate
				P_SamFP(nsa, PAL_RECYCLE, 6)
				end
			end
			if (s.health < 17) and not s.hdrop2
			local samusrnd = P_RandomRange(1, 100)
			s.samspawneditem = P_SpawnMobj(s.x+(20*FRACUNIT)+(samusrnd*FRACUNIT), s.y+(samusrnd*FRACUNIT), s.z+(samusrnd*FRACUNIT/3), MT_SPKU)
			s.samspawneditem = P_SpawnMobj(s.x-(samusrnd*FRACUNIT), s.y-(samusrnd*FRACUNIT), s.z, MT_PBOM)
			s.samspawneditem = P_SpawnMobj(s.x, s.y-(samusrnd*FRACUNIT), s.z+(samusrnd*FRACUNIT/2), MT_BMPKU)
			s.samspawneditem = P_SpawnMobj(s.x+(samusrnd*FRACUNIT), s.y+(33*FRACUNIT), s.z+(samusrnd*FRACUNIT/4), MT_PBOM)
			s.hdrop2 = true
			s.sbexplodey = 99
			S_StartSound(nil, sfx_begrnd)
			S_StartSound(nil, sfx_beragh)
				for nsa in players.iterate
				P_SamFP(nsa, PAL_RECYCLE, 6)
				end
			end
			if s.sbexplodey
				s.sbexplodey = $-1
			end
			if ((s.health < 12) or s.sbexplodey) and (leveltime % 7 == 0)
			local samusrnd = P_RandomRange(-300, 300)
			s.samspawneditem = P_SpawnMobj(s.x+(samusrnd*FRACUNIT), s.y+(samusrnd*FRACUNIT), s.z+s.height/2+((samusrnd*FRACUNIT)/2), MT_BOSSEXPLODE)
			s.samspawneditem.scale = ((P_RandomRange(1,5))*FRACUNIT)
			end
		//	if s.radius < 380*FRACUNIT 
		//	s.radius = 380*FRACUNIT
		//	end
			if ( (s.health < 6) or ((s.health < 8) and s.spazershocked) )
			and not s.hyperbeampanic
			s.health = 36
			s.state = S_BLACKEGG_PAIN1
			s.flags = $1|MF_NOGRAVITY & ~MF_SHOOTABLE
			s.flags = $1|MF_NOCLIPHEIGHT
			s.flags2 = $1|MF2_FRET
			s.fretmax = 100
			s.hyperbeampanic = 84
			S_StartSound(nil, sfx_behurt)
			S_StartSound(nil, sfx_nrga) S_StartSound(nil, sfx_nrga)
			print("\x83"+"The damaged Brak mech is leaking stolen hyper energy!")
				for nsa in players.iterate
				P_SamFP(nsa, PAL_RECYCLE, 9)
				nsa.hyperbrakmsg = 99
				end
			s.satanspawn = P_SpawnMobj(s.x, s.y, s.z+s.z+s.height/6, MT_METALSONIC_BATTLE)
			s.satanspawn.health = 6
			s.satanspawn.scale = 75355
			s.satanspawn.superbraksmetal = true
			s.satanspawn.flags = $1 & ~MF_SPECIAL
			s.satanspawn = P_SpawnMobj(s.x+1000*FRACUNIT, s.y, s.z+s.z+s.height/14, MT_NRGD)
			s.satanspawn.hyperbeamammo = true
			s.satanspawn.scale = s.scale
			s.satanspawn = P_SpawnMobj(s.x-1000*FRACUNIT, s.y, s.z+s.height/12, MT_NRGD)
			s.satanspawn.hyperbeamammo = true
			s.satanspawn.scale = s.scale
			s.satanspawn = P_SpawnMobj(s.x, s.y+1000*FRACUNIT, s.z+s.height/13, MT_NRGD)
			s.satanspawn.hyperbeamammo = true
			s.satanspawn.scale = s.scale
			s.satanspawn = P_SpawnMobj(s.x, s.y-1000*FRACUNIT, s.z+s.height/11, MT_NRGD)
			s.satanspawn.hyperbeamammo = true
			s.satanspawn.scale = s.scale
				for d = 1,3
				P_SpawnMobj(s.x, s.y-(500*d*FRACUNIT), s.z+s.height/15, MT_NRGD)
				P_SpawnMobj(s.x, s.y+(500*d*FRACUNIT), s.z+s.height/14, MT_NRGD)
				P_SpawnMobj(s.x-(500*d*FRACUNIT), s.y, s.z+s.height/16, MT_NRGB)
				P_SpawnMobj(s.x+(500*d*FRACUNIT), s.y, s.z+s.height/11, MT_NRGA)
				end
			s.health = 29
			end

			if (s.z < s.floorz-300*FRACUNIT) or (s.z > s.ceilingz+300*FRACUNIT)
			P_MoveOrigin(s, s.x, s.y, s.floorz+20*FRACUNIT)
			s.flags = $1 & ~MF_NOCLIPHEIGHT
			end

			if (s.health < 1)
			s.health = 0
			s.flags = $1 & ~MF_NOCLIPHEIGHT
				if (s.state == S_CYBRAKDEMON_DIE8) and not s.getouttaherep
					for ns in players.iterate
					P_DoPlayerExit(ns)
					end
					s.getouttaherep = true
					s.superbrak = 0
				end
			return
			end
			if s.jumpspam == nil
			s.jumpspam = 0
			elseif s.jumpspam
			s.jumpspam = $-1
			end
			if s.target and not (s.target.player or s.target.health)
			s.target = nil
			end
			if s.fretmax2 == nil or (s.fretmax2 != 81 and not (s.flags2 & MF2_FRET))
			s.fretmax2 = 81
			elseif s.fretmax2 and (s.flags2 & MF2_FRET)
			s.fretmax2 = $-1
				if not s.fretmax2 and (s.flags2 & MF2_FRET)
				s.fretmax2 = 81
				s.flags = $|MF_SHOOTABLE
				s.flags2 = $ & ~MF2_FRET
				end
			end			
			if s.fretmax == nil or (s.fretmax != 100 and not (s.flags2 & MF2_FRET))
			s.fretmax = 100 
			end
			if s.fretmax and (s.flags2 & MF2_FRET)
				s.fretmax = $-1
				if (s.fretmax == 35) and not s.superbrakquickfret and not s.spazershocked
					local brakrngc = P_RandomRange(1,75)
					if (brakrngc < 51) and P_IsObjectOnGround(s)
						A_FaceTarget(s)
						if (brakrngc < 26)
						s.sidejumpangle = s.angle+ANGLE_90
						else
						s.sidejumpangle = s.angle-ANGLE_90
						end
						s.superbrakjump = 35
						s.jumpspam = $1+70
						s.superbrakjumpdirect = brakrngc
						S_StartSound(nil, sfx_beflap)
						S_StartSound(nil, sfx_beflap)
						s.state = S_BLACKEGG_JUMP2
						P_SetObjectMomZ(s, 20*FRACUNIT, false)
					elseif P_IsObjectOnGround(s)
						s.superbrakbigjump = 60
						S_StartSound(nil, sfx_belnch) 
						S_StartSound(nil, sfx_belnch)
						s.jumpspam = $1+80
						s.state = S_BLACKEGG_JUMP2
						P_SetObjectMomZ(s, 30*FRACUNIT, false)
					end
				end
				if s.superbrakquickfret 
					if (s.fretmax > 22) and not s.spazershocked
					s.fretmax = 22
					s.superbrakjump = 0
					s.superbrakbigjump = 0
					s.state = S_BLACKEGG_PAIN1 
					S_StartSound(nil, sfx_behurt)
					S_StartSound(nil, sfx_smexp)
					if s.superbrakquickfret2
					S_StartSound(nil, sfx_bedie2)
					end
					P_StartQuake(25*FRACUNIT, 30)
					S_StartSound(nil, sfx_befall)
					S_StartSound(nil, sfx_rocks2)
					S_StartSound(nil, sfx_s3k9b)
					S_StartSound(nil, sfx_brakrx)
					S_StartSound(nil, sfx_bgxpld)
					A_SAMUSEXPLOSIONS(s, 10, MT_SPCHARGESHOT, 10*FRACUNIT, 1, SKINCOLOR_SKY, "brakdust")
					end
				s.superbrakquickfret2 = false
				s.superbrakquickfret = false
				end
				if s.fretmax == 1
				s.fretmax = 0
				s.flags = $1|MF_SHOOTABLE
				s.flags2 = $1 & ~MF2_FRET
				end
			end
			if (s.state == S_CYBRAKDEMON_VILE_ATTACK2) and not s.vilesbextend
			s.tics = $+18
			s.vilesbextend = true
			elseif (s.state != S_CYBRAKDEMON_VILE_ATTACK2) and s.vilesbextend
			s.vilesbextend = false
			end
			if (s.tics == 2) and (s.state == S_CYBRAKDEMON_WALK2) and not s.superbrakjump and not s.superbrakbigjump and not s.spazershocked
			s.flags = $1 & ~MF_NOCLIPHEIGHT
			local brakrnga = P_RandomRange(1,100)
				if ((brakrnga < 19) and (s.health < 49) or ((brakrnga > 85) and (brakrnga < 95)) and (s.health < 20)) and not (s.jumpspam > 150) and P_IsObjectOnGround(s)
				A_FaceTarget(s)
					if (brakrnga < 10)
					s.sidejumpangle = s.angle+ANGLE_90
					else
					s.sidejumpangle = s.angle-ANGLE_90
					end
				s.superbrakjump = 35
				s.jumpspam = $1+70
				s.superbrakjumpdirect = brakrnga
				S_StartSound(nil, sfx_beflap)
				S_StartSound(nil, sfx_beflap)
				s.state = S_BLACKEGG_JUMP2
				P_SetObjectMomZ(s, 20*FRACUNIT, false)

				elseif (brakrnga < 24) and (s.health < 49) and not (s.jumpspam > 150)
				s.state = S_CYBRAKDEMON_NAPALM_ATTACK1
				s.jumpspam = $1+20

				elseif (brakrnga < 27) and (s.health < 45) and not (s.jumpspam > 150) //Do viletarget and napalm attack early!
				s.state = S_CYBRAKDEMON_VILE_ATTACK1
				s.jumpspam = $1+115

				elseif ((brakrnga < 37) and (s.health < 49) or ((brakrnga > 74) and (brakrnga < 83)) and (s.health < 20)) and not (s.jumpspam > 150) and P_IsObjectOnGround(s)
				s.superbrakbigjump = 60
				S_StartSound(nil, sfx_belnch) 
				S_StartSound(nil, sfx_belnch)
				s.jumpspam = $1+110
				s.state = S_BLACKEGG_JUMP2
				P_SetObjectMomZ(s, 30*FRACUNIT, false)
				elseif (brakrnga < 32) and (s.health < 47)
				s.state = S_BLACKEGG_MISSILE1
				s.tics = 2

				elseif (brakrnga < 46) and (s.health < 49) and s.target
				A_LobShot(s, MT_CANNONBALL, 8)
				S_StartSound(nil, sfx_cannon)
				end
			end
			if s.health and ((s.state == S_BLACKEGG_DESTROYPLAT3) or (s.state == S_BLACKEGG_JUMP2) or (s.state == S_BLACKEGG_MISSILE3) or (s.state == S_BLACKEGG_PAIN35)) and (s.tics < 3)
				if (s.state == S_BLACKEGG_PAIN35)
				s.flags = $1|MF_SHOOTABLE & ~MF_BOUNCE & ~MF_NOCLIPHEIGHT & ~MF_NOGRAVITY
				end
				s.state = S_CYBRAKDEMON_WALK1
			end
//Giant stomp+shockwave!
			if s.superbrakbigjump
			s.superbrakbigjump = $-1
			s.state = S_BLACKEGG_JUMP2
			s.tics = 5
			A_FaceTarget(s)
				if (s.superbrakbigjump > 40)
				P_SetObjectMomZ(s, ((s.superbrakbigjump*FRACUNIT)/3), false)
				P_SpawnGhostMobj(s)
				elseif (s.superbrakbigjump < 10)
				P_SetObjectMomZ(s, -20*FRACUNIT, false)
				elseif (s.superbrakbigjump < 18)
				P_SetObjectMomZ(s, -9*FRACUNIT, false)
				elseif (s.superbrakbigjump < 25)
				P_SetObjectMomZ(s, -1*FRACUNIT, false)
				end
				if s.target
					if ((FixedHypot(s.target.x-s.x, s.target.y-s.y)) > 1800*FRACUNIT) and ((s.superbrakbigjump > 20) and (s.superbrakbigjump < 40))
					P_InstaThrust(s, s.angle, 66*FRACUNIT)
					else
					P_InstaThrust(s, s.angle, 33*FRACUNIT)
					end
				else
				P_InstaThrust(s, 0, 0)
				end
				if (s.superbrakbigjump == 1) or ((s.superbrakbigjump < 40) and P_IsObjectOnGround(s))
				s.state = S_BLACKEGG_DESTROYPLAT3
				s.superbrakbigjump = 0
				P_StartQuake(25*FRACUNIT, 30)
				S_StartSound(nil, sfx_befall)
				S_StartSound(nil, sfx_rocks2)
				S_StartSound(nil, sfx_s3k9b)
				S_StartSound(nil, sfx_brakrx)
				S_StartSound(nil, sfx_bgxpld)

				A_SAMUSEXPLOSIONS(s, 10, MT_SPCHARGESHOT, 10*FRACUNIT, 1, SKINCOLOR_SKY, "brakdust")
				A_SAMUSEXPLOSIONS(s, 6, MT_BOSSEXPLODE, 25*FRACUNIT, 90)
				A_SAMUSEXPLOSIONS(s, 4, MT_EXPLODE, 41*FRACUNIT, 90)
				P_InstaThrust(s, 0, 0)
				end
			end
//Sidehop while firing!
			if s.superbrakjump
			s.superbrakjump = $-1
			s.state = S_BLACKEGG_JUMP2
			s.tics = 5 
			A_FaceTarget(s)
			P_InstaThrust(s, s.sidejumpangle, 60*FRACUNIT)
				if (s.superbrakjump < 30) and (s.superbrakjump > 4) and (s.superbrakjump % 2 == 0)
					if s.target
					S_StartSound(nil, sfx_befire)
					P_SpawnMissile(s, s.target, MT_BLACKEGGMAN_GOOPFIRE)
					end
				end
				if (s.superbrakjump > 24)
				P_SetObjectMomZ(s, ((s.superbrakjump*FRACUNIT)/2), false)
				P_SpawnGhostMobj(s)
				elseif (s.superbrakjump < 9)
				P_SetObjectMomZ(s, -18*FRACUNIT, false)
				elseif (s.superbrakjump < 19)
				P_SetObjectMomZ(s, -8*FRACUNIT, false) 
				elseif (s.superbrakjump < 25)
				P_SetObjectMomZ(s, -1*FRACUNIT, false)
				end
				if (s.superbrakjump == 1) or ((s.superbrakjump < 13) and P_IsObjectOnGround(s))
				s.state = S_BLACKEGG_DESTROYPLAT3
				s.superbrakjump = 0
				s.thruster = false
				P_StartQuake(11*FRACUNIT, 6)
				S_StartSound(nil, sfx_befall)
				A_SAMUSEXPLOSIONS(s, 15, MT_BOSSEXPLODE, 15*FRACUNIT, 90)
				A_SAMUSEXPLOSIONS(s, 20, MT_EXPLODE, 21*FRACUNIT, 90)
				P_InstaThrust(s, 0, 0)
				end
			end
			if (leveltime % 7 == 0)
			s.satanrock = P_SpawnMobj(s.x+(P_RandomRange(-7000, 7000)*FRACUNIT), s.y+(P_RandomRange(-7000, 7000)*FRACUNIT), s.ceilingz-80*FRACUNIT, MT_FALLINGROCK)
			s.satanrock.scale = $1*(P_RandomRange(1, 6))
			s.satanrock.flags = $1|MF_NOGRAVITY|MF_MISSILE
			s.satanrock.target = s
			s.satanrock.satansrock = true
			end
		if s.braktime
		local brakrngr = P_RandomRange(1,10)
			
			if not netgame and not s.eggmercyonce and s.target and s.target.player and YouAreSamus(s.target) and s.target.player.sam_energy and (s.target.player.sam_energy < 166)
			P_SpawnMobj(s.x, s.y-500*FRACUNIT, s.z+s.height/15, MT_NRGD)
			P_SpawnMobj(s.x, s.y+500*FRACUNIT, s.z+s.height/14, MT_NRGD)
			P_SpawnMobj(s.x-500*FRACUNIT, s.y, s.z+s.height/11, MT_NRGD)
			P_SpawnMobj(s.x+500*FRACUNIT, s.y, s.z+s.height/13, MT_BMPKU)
			s.eggmercyonce = true
			end

			if s.target and s.target.player and (YouAreSamus(s.target)) and s.target.player.sam_energy and (s.target.player.sam_energy < 99)
			s.satanspawn = P_SpawnMobj(s.x+200*FRACUNIT, s.y-200*FRACUNIT, s.z+s.height/12, MT_NRGB)
			s.satanspawn = P_SpawnMobj(s.x-200*FRACUNIT, s.y+200*FRACUNIT, s.z+s.height/13, MT_NRGA)
			s.satanspawn = P_SpawnMobj(s.x, s.y, s.z+s.height/11, MT_NRGA)
			end

			if s.hyperbeampanic and (leveltime % 2 == 0)
			s.satanspawn = P_SpawnMobj(s.x+1000*FRACUNIT, s.y, s.z+s.z+s.height/10, MT_NRGD)
			s.satanspawn.hyperbeamammo = true
			s.satanspawn.scale = s.scale
			s.satanspawn = P_SpawnMobj(s.x-1000*FRACUNIT, s.y, s.z+s.height/12, MT_NRGD)
			s.satanspawn.hyperbeamammo = true
			s.satanspawn.scale = s.scale
			s.satanspawn = P_SpawnMobj(s.x, s.y+1000*FRACUNIT, s.z+s.height/13, MT_NRGD)
			s.satanspawn.hyperbeamammo = true
			s.satanspawn.scale = s.scale
			s.satanspawn = P_SpawnMobj(s.x, s.y-1000*FRACUNIT, s.z+s.height/11, MT_NRGD)
			s.satanspawn.hyperbeamammo = true
			s.satanspawn.scale = s.scale
			end
			if (brakrngr > 7)
			s.satanspawn = P_SpawnMobj(s.x+400*FRACUNIT, s.y, s.z+s.z+s.height/10, MT_SPRINGSHELL)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)
			s.satanspawn = P_SpawnMobj(s.x-400*FRACUNIT, s.y, s.z+s.height/12, MT_EGGGUARD)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)
			s.satanspawn = P_SpawnMobj(s.x, s.y+400*FRACUNIT, s.z+s.height/2, MT_JETTGUNNER)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)
			s.satanspawn = P_SpawnMobj(s.x, s.y-400*FRACUNIT, s.z+s.height/3, MT_JETTGUNNER)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)

			elseif (brakrngr > 5)
			s.satanspawn = P_SpawnMobj(s.x+400*FRACUNIT, s.y, s.z+s.z+s.height/10, MT_FACESTABBER)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)
			s.satanspawn = P_SpawnMobj(s.x-400*FRACUNIT, s.y, s.z, MT_MINUS)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)
			s.satanspawn = P_SpawnMobj(s.x, s.y+400*FRACUNIT, s.z+32*FRACUNIT, MT_UNIDUS)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)
			s.satanspawn = P_SpawnMobj(s.x, s.y-400*FRACUNIT, s.z+s.height/11, MT_ROBOHOOD)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)

			elseif (brakrngr > 3)
			s.satanspawn = P_SpawnMobj(s.x+400*FRACUNIT, s.y, s.z+s.z+s.height/6, MT_GFZFISH)
			s.satanspawn.brakandco = true
			s.satanspawn.angle = 0
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)
			s.satanspawn = P_SpawnMobj(s.x-400*FRACUNIT, s.y, s.z+s.height/2, MT_JETTBOMBER)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)
			s.satanspawn = P_SpawnMobj(s.x, s.y+400*FRACUNIT, s.z+s.height/3, MT_JETTBOMBER)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)
			s.satanspawn = P_SpawnMobj(s.x, s.y-400*FRACUNIT, s.z+s.height/11, MT_GSNAPPER)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)

			elseif (brakrngr > 2)
			s.satanspawn = P_SpawnMobj(s.x+1000*FRACUNIT, s.y, s.z+s.z+s.height/8, MT_GSNAPPER)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)
			s.satanspawn = P_SpawnMobj(s.x-1000*FRACUNIT, s.y, s.z+s.height/8, MT_GSNAPPER)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)
			s.satanspawn = P_SpawnMobj(s.x, s.y+1000*FRACUNIT, s.z+s.height/8, MT_GSNAPPER)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)
			s.satanspawn = P_SpawnMobj(s.x, s.y-1000*FRACUNIT, s.z+s.height/8, MT_GSNAPPER)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)
			elseif (brakrngr > 1)
			s.satanspawn = P_SpawnMobj(s.x+1000*FRACUNIT, s.y, s.z+s.z+s.height/8, MT_VULTURE)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)
			s.satanspawn = P_SpawnMobj(s.x-1000*FRACUNIT, s.y, s.z+s.height/8, MT_VULTURE)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)
			s.satanspawn = P_SpawnMobj(s.x, s.y+1000*FRACUNIT, s.z+s.height/8, MT_VULTURE)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)
			s.satanspawn = P_SpawnMobj(s.x, s.y-1000*FRACUNIT, s.z+s.height/8, MT_VULTURE)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)
			else
			s.satanspawn = P_SpawnMobj(s.x+1000*FRACUNIT, s.y, s.z+s.z+s.height/5, MT_DETON)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)
			s.satanspawn = P_SpawnMobj(s.x-1000*FRACUNIT, s.y, s.z+s.height/2, MT_DETON)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)
			s.satanspawn = P_SpawnMobj(s.x, s.y+1000*FRACUNIT, s.z+s.height/3, MT_DETON)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)
			s.satanspawn = P_SpawnMobj(s.x, s.y-1000*FRACUNIT, s.z+s.height/4, MT_DETON)
			s.satanspawn.brakandco = true
			s.satanspawn.npcsp = P_SpawnMobj(s.satanspawn.x, s.satanspawn.y, s.satanspawn.z+s.satanspawn.height/2, MT_EXPLODE)
			end
			if s.target and s.target.player and (s.target.player.crazysamusmobs or s.target.player.fusionmode)
			ActivateHyperMode(s.target.player, 7)
			end
		s.braktime = false
		end

		elseif s.superbrak == nil and not s.superbraktransform
			if s.target and s.target.player 
			and ((gamemap == 28) and mapheaderinfo[gamemap].lvlttl == "Black Core" or s.forcesuperbraktransform)
			//and s.target.player.sam_fullypoweredsuit
				for m in mobjs.iterate(mobj)
					if not (m and (m.type == MT_BSZBUSH_CYAN))
					s.superbraktransform = 1
						if not s.target.player.samworldevents
							s.superbraktransform = 2 //Cancel transformation if World events is off
						elseif m.player
						m.z = 512*FRACUNIT
						end
					end
					P_SpawnMobj(2112*FRACUNIT, -1216*FRACUNIT, 0, MT_BSZBUSH_CYAN) //Should be ok considering how few mobs there are.
				end
				if not s.superbraktransform
					s.superbraktransform = 2
				end
			elseif s.target and s.target.player and not s.target.player.sams_itemcompletion and YouAreSamus(s.target)
				for ns in players.iterate
					if ns.mo
					ns.mo.samuszerorun = true
					end
				end
				s.superbraktransform = 2
			elseif s.target and s.target.player and ((gamemap != 28) or not s.target.player.samworldevents or not s.target.player.sam_fullypoweredsuit)
				s.superbraktransform = 0
				s.superbrak = 0
			end
			
		elseif s.superbraktransform and s.superbraktransform == 1 and not s.superbrak
			s.superbraktransform = 0
			s.superbrak = 1
			s.nosamuspushpls = true -- W-wait, I'm superbrak! Don't just push me around!
			s.destscale = FRACUNIT*4
			s.health = 56
			s.superbraktransform = 0
			S_StartSound(nil, sfx_beragh)
			S_StartSound(nil, sfx_beragh)
			S_StartSound(nil, sfx_bechrg)
			A_LinedefExecute(s, LE_PINCHPHASE, nil)
			P_SetMobjStateNF(s, S_BLACKEGG_PAIN1)
			s.tics = 20
			P_SwitchWeather(PRECIP_STORM_NORAIN)
				for ns in players.iterate
					if ns.mo and ns.mo.valid
						if ns.sam_hyperbeam
							SamusDialogue(ns.mo, "\x85 Warning! Hyper Beam energy drained!", nil, "SAMSAD", 1, 500)
							CONS_Printf(ns, "\x85"+"You were drained of your Hyper Beam energy!")
						end
					ns.mo.z = ns.mo.floorz
					P_DoPlayerPain(ns, s, s)
					P_PlayRinglossSound(ns.mo)
					ns.sammissiles = ns.sammissilesmax
					ns.sampw = ns.sampwmax
					ns.samsupermissiles = ns.samsupermissilesmax
					ns.sam_energy = 99+(ns.sam_energytanksmax*50)
					ns.sam_hyperbeamammo = 0
					ns.mo.hypernope = P_SpawnPointMissile(ns.mo, ns.mo.x, ns.mo.y, ns.mo.z+ns.mo.height, MT_ENERGYBALL, s.x, s.y, s.z+s.height/2)
					ns.mo.hypernope.flags = $1|MF_MISSILE & ~MF_PAIN
					ns.mo.rdeathmsg = true
					P_SamFP(ns, PAL_NUKE, 12)
					P_StartQuake(15*FRACUNIT, 32)
					S_StartSound(ns.mo, sfx_gravch)
					ns.mo.hypernope.target = s
					end
				end
				if (gamemap == 28)
					for sector in sectors.iterate
						if sector.floorheight == 608*FRACUNIT
							sector.floorheight = 512*FRACUNIT
						end
						if (sector == sectors[3] or sector == sectors[79] or sector == sectors[8]) //Most efficient method.
							if (sector == sectors[8])
							sector.ceilingheight = 480*FRACUNIT
							end
							sector.floorheight = 480*FRACUNIT
							sector.floorpic = "ERZRCKF1"
							sector.special = 0
							if sector == sectors[8]
								sector.lightlevel = 142
							else
								sector.lightlevel = 192
							end
						end
						if sector.special == 18 
							sector.special = 16
						end
						--No death pits, damnit.
						if (GetSecSpecial(sector.special, 1) > 4)
						or sector.floorpic == "ERZRCKF1"
						or sector.floorheight == 480*FRACUNIT
							sector.special = 0
						end
					end
				end
		elseif s.superbraktransform and s.superbraktransform == 2 //No superbrak, then.
			s.superbrak = 0
			s.superbraktransform = 0
		end		
	end
end, MT_CYBRAKDEMON)

--1 = bronze(zero mission)
--2 = silver(hyper)
--3 = gold(fusion)
--Achievement1 is any%, achievement2 is 100%, achievement 3 is 5% clear.
local function AwardAchievements(p, superbrak)
	if (gamemap == 28) and mapheaderinfo[gamemap].lvlttl == "Black Core" --Only in Black Core, not custom campaign Braks!
		if p and p.mo and p.mo.valid and YouAreSamus(p.mo)
			if p.sam_saveselected  // and not p.sam_achievementlock
				if p.fusionmode
				
					if (p.sams_itemcompletion <= 5) or not (p.sams_itemcompletion)
						if not p.sam_achievement3 or (p.sam_achievement3 < 3)
							if p.samworldevents
							p.sam_achievement3 = 3
							CONS_Printf(p, "\x82"+"Unlocked new 5% items achievement!")
							end
						end								
					elseif ((p.sams_itemcompletion >= 100) or p.sam_fullypoweredsuit) and superbrak
						if not p.sam_achievement2 or (p.sam_achievement2 < 3)
							if p.samworldevents
							p.sam_achievement2 = 3
							CONS_Printf(p, "\x82"+"Unlocked new 100% items achievement!")
							end
						end						
					end	
					--Always get clear achievement
					if not p.sam_achievement1 or (p.sam_achievement1 < 3)
						if p.samworldevents
						p.sam_achievement1 = 3
						CONS_Printf(p, "\x82"+"Unlocked new CLEAR achievement!")
						end
					end
					
				elseif p.crazysamusmobs
					if (p.sams_itemcompletion <= 5) or not (p.sams_itemcompletion)
						if not p.sam_achievement3 or (p.sam_achievement3 < 2)
							if p.samworldevents
							p.sam_achievement3 = 2
							CONS_Printf(p, "\x82"+"Unlocked new 5% items achievement!")
							end
						end								
					elseif ((p.sams_itemcompletion >= 100) or p.sam_fullypoweredsuit) and superbrak
						if not p.sam_achievement2 or (p.sam_achievement2 < 2)
							if p.samworldevents
							p.sam_achievement2 = 2
							CONS_Printf(p, "\x82"+"Unlocked new 100% items achievement!")
							end
						end						
					end	
					--Always get clear achievement
					if not p.sam_achievement1 or (p.sam_achievement1 < 2)
						if p.samworldevents
						p.sam_achievement1 = 2
						CONS_Printf(p, "\x82"+"Unlocked new CLEAR achievement!")
						end
					end
				
				else
				
					if (p.sams_itemcompletion <= 5) or not (p.sams_itemcompletion)
						if not p.sam_achievement3
							if p.samworldevents
							p.sam_achievement3 = 1
							CONS_Printf(p, "\x82"+"Unlocked new 5% items achievement!")
							end
						end								
					elseif ((p.sams_itemcompletion >= 100) or p.sam_fullypoweredsuit) and superbrak
						if not p.sam_achievement2
							if p.samworldevents
							p.sam_achievement2 = 1
							CONS_Printf(p, "\x82"+"Unlocked new 100% items achievement!")
							end
						end						
					end			
				end
				
				--Always get clear achievement
				if not p.sam_achievement1
					if p.samworldevents
					p.sam_achievement1 = 1
					CONS_Printf(p, "\x82"+"Unlocked new CLEAR achievement!")
					end
				end
				ProfileSamusSave(p.mo, p, p.sam_saveselected, true, false)
			end
			if p.mo.health and not p.sams_itemcompletion and p.samworldevents
				p.mo.samuszerorun = true --Beat the game at 0%? You are truly the ultimate player!
			end
		end
	end
end

addHook("MobjDeath", function(s, i, so)
	if not s.superbrak
		for ns in players.iterate
		AwardAchievements(ns)
		end
	elseif s.valid and s.superbrak
	s.flags = $1|MF_BOUNCE & ~MF_NOGRAVITY
	P_StartQuake(14*FRACUNIT, 50)
	S_StartSound(nil, sfx_begrnd)
	S_StartSound(nil, sfx_becrsh)
	S_StartSound(nil, sfx_bkpoof)
	S_StartSound(nil, sfx_bkpoof)
		for ns in players.iterate
		P_SamFP(ns, PAL_MIXUP, 5)
		AwardAchievements(ns, true)
		end
		s.lastsurprise = P_SpawnMobj(s.x, s.y, s.z+s.height/2, MT_EGGMOBILE)
		s.lastsurprise.health = 2
		s.lastsurprise.superbraksmetal = true
		s.lastsurprise.flags = $1|MF_BOUNCE & ~MF_SPECIAL
		s.lastsurprise.meleeblast = P_SpawnMobj(s.x, s.y, s.z, MT_SPAZERSHOCK)
		s.lastsurprise.meleeblast.rocket = s.lastsurprise
		s.lastsurprise.meleeblast.mojo = true
		s.lastsurprise.spazershocked = true
		s.lastsurprise.spcombod = true
		S_StartSound(s.lastsurprise.meleeblast, sfx_smexp)
		S_StartSound(s.lastsurprise.meleeblast, sfx_smexp)
		s.lastsurprise.meleeblast.angle = s.angle
	end
end, MT_CYBRAKDEMON)

addHook("ShouldDamage", function(s, i, so)
	if s.valid and s.superbrak 
		if ((i and (i.flags & MF_ENEMY)) or (so and (so.flags & MF_ENEMY)))
		return false
		end
		if not s.hyperbeampanic and (s.health < 6)
		return false
		end
	end
end, MT_CYBRAKDEMON)

addHook("MobjDamage", function(s, i, so)
	if s.valid and s.superbrak
		s.braktime = true
		if so and so.player 	
			if (so.player.sam_energy and so.player.sam_energy < 300) 
			P_SpawnMobj(s.x, s.y, s.z+s.height/18, MT_NRGB) -- Mercy health
			end
			if (so.player.sammissiles != nil and so.player.sammissiles < 11)
			P_SpawnMobj(s.x+20*FRACUNIT, s.y, s.z+s.height/17, MT_BMPKU) -- Mercy ammo		
			end
		end
	end
end, MT_CYBRAKDEMON)

addHook("MobjThinker", function(s)
	if s.valid and s.target and s.target.frozenmobj and s.health
		P_KillMobj(s)
	end
end, MT_UNIBALL)
addHook("MobjThinker", function(s)
	if s.valid and s.target and s.target.frozenmobj and s.health
		P_KillMobj(s)
	end
end, MT_POINTYBALL)

addHook("MobjThinker", function(s)
	if s.valid and s.target and s.target.superbrak
		if s and s.valid and s.target.hyperbeampanic
		s.state = S_NULL
		return
		end
		s.scale = s.target.scale*10/7
		s.angle = s.target.angle
		if not s.shieldspawned
		s.shield1 = P_SpawnMobj(s.x, s.y, s.z+s.target.height+220*FRACUNIT, MT_POPUPTURRET)
		s.shield1.samusHP = 2  s.shield1.samusHPmax = 2
		s.shield1.scale = 230712
		s.shield1.flags = $1|MF_SHOOTABLE|MF_NOGRAVITY|MF_NOCLIPHEIGHT
		s.shield1.height = 200*FRACUNIT
		s.shield1.radius = 200*FRACUNIT
		s.shield1.brakandco = true
		s.shield2 = P_SpawnMobj(s.x + cos(FixedAngle(s.angle+ANGLE_90))*120, s.y + sin(FixedAngle(s.angle+ANGLE_90))*120, s.z + s.height/2, MT_POPUPTURRET)
		s.shield2.samusHP = 2  s.shield2.samusHPmax = 2
		s.shield2.scale = 230712
		s.shield2.flags = $1|MF_SHOOTABLE|MF_NOGRAVITY|MF_NOCLIPHEIGHT
		s.shield2.height = 200*FRACUNIT
		s.shield2.radius = 200*FRACUNIT
		s.shield2.brakandco = true
		s.shield3 = P_SpawnMobj(s.x + sin(FixedAngle(s.angle+ANGLE_90))*120, s.y + cos(FixedAngle(s.angle+ANGLE_90))*120, s.z + s.height/2, MT_POPUPTURRET)
		s.shield3.samusHP = 2 s.shield3.samusHPmax = 2
		s.shield3.scale = 230712
		s.shield3.flags = $1|MF_SHOOTABLE|MF_NOGRAVITY|MF_NOCLIPHEIGHT
		s.shield3.height = 200*FRACUNIT
		s.shield3.radius = 200*FRACUNIT
		s.shield3.brakandco = true
		s.shieldspawned = true
		end
		if s.shield1 and s.shield1.valid and s.shield1.health
			s.shield1.sprite = SPR_RCRY
			s.shield1.frame = B|FF_FULLBRIGHT
			s.shield1.tics = 3
			P_MoveOrigin(s.shield1, s.x, s.y, s.z+s.target.height+220*FRACUNIT)
		end
		if s.shield2 and s.shield2.valid and s.shield2.health
			s.shield2.sprite = SPR_RCRY
			s.shield2.frame = B|FF_FULLBRIGHT
			s.shield2.tics = 3
			P_MoveOrigin(s.shield2, s.x+FixedMul(440*FRACUNIT, cos(s.angle-ANGLE_90)), s.y+FixedMul (440*FRACUNIT, sin (s.angle-ANGLE_90)), s.z + s.height*10/15)
		end
		if s.shield3 and s.shield3.valid and s.shield3.health
			s.shield3.sprite = SPR_RCRY
			s.shield3.frame = B|FF_FULLBRIGHT
			s.shield3.tics = 3
			P_MoveOrigin(s.shield3, s.x+FixedMul(440*FRACUNIT, cos(s.angle+ANGLE_90)), s.y+FixedMul (440*FRACUNIT, sin (s.angle+ANGLE_90)), s.z + s.height*10/15)
		end
		if not (s.shield1 and s.shield1.valid) and not (s.shield2 and s.shield2.valid) and not (s.shield3 and s.shield3.valid)
		or (s.target and (s.target.health < 12))
			if s and s.valid and s.target and s.target.superbrak and s.health
			s.target.flags = $1|MF_SHOOTABLE
			P_KillMobj(s)
			end
		end
		if s and not s.health 
			if (s.shield1 and s.shield1.valid) s.shield1.state = S_NULL end
			if (s.shield2 and s.shield2.valid) s.shield2.state = S_NULL end	
			if (s.shield3 and s.shield3.valid) s.shield3.state = S_NULL end
		end
		if s and s.valid and s.health and s.target and s.target.valid and not s.target.hyperbeampanic
		s.target.flags = $1 & ~MF_SHOOTABLE
		end
	end
end, MT_CYBRAKDEMON_ELECTRIC_BARRIER)

addHook("MobjCollide", function(s, i)
	if s.valid and (s.scale == 230712) and i and (i.type == MT_SPSHOT)
		if not i.shielded
		SamusBeamReflect(i, s)
		i.shielded = true
		S_StartSound(s, sfx_bewar4)
		S_StartSound(s, sfx_s3k42)
			if (i.target and i.target.player and YouAreSamus(i.target))
			i.target.player.sam_resisthud = 18
			end
		end
		return false
	end
end, MT_POPUPTURRET)

addHook("MobjThinker", function(s)
	if s.valid and s.satansrock
		if not (s.z < s.floorz+5*FRACUNIT)
		s.fuse = 150
		s.momz = (-10*FRACUNIT)/(s.scale/FRACUNIT)-145355
		elseif s and s.valid and s.target and not s.boom
		s.boom = P_SpawnXYZMissile(s.target, s, MT_CYBRAKDEMON_NAPALM_BOMB_SMALL, s.x, s.y, s.z)
			if s.boom and s.boom.valid and s.boom.health
			P_ExplodeMissile(s.boom)
			end
		end
	end
end, MT_FALLINGROCK)

addHook("MobjThinker", function(s)
	if s.valid and s.health and (s.flags & MF_NOGRAVITY) and s.tracer
		if s.ogzsam == nil
		s.ogzsam = s.z
		end
		if s.ogzsam and not s.spazershocked
			if not (s.eflags & MFE_VERTICALFLIP) and (s.z < s.ogzsam-(5*FRACUNIT))
			s.z = $+55355
			elseif (s.eflags & MFE_VERTICALFLIP) and (s.z > s.ogzsam+(5*FRACUNIT))
			s.z = $-55355
			end
		end
	end
end, MT_EGGMOBILE2)
		
addHook("MobjSpawn", function(m) --All enemy spawns gain an HP & weakness system for Samus
	if m.flags and ((m.flags & MF_ENEMY) or (m.flags & MF_BOSS)
	or ((m.type == MT_DETON or m.type == MT_EGGSHIELD or m.type == MT_EGGGUARD or m.type == MT_MINUS)))
		A_SAMUSMOBHP(m)
	end
end)

addHook("PlayerSpawn", function(p)
	if p and ((p == server or p == admin) ) and (p.crazysamusmobs or p.fusionmode) and not (gametype == GT_MATCH or gametype == GT_CTF or gametype == GT_HUNTERS)
		for z in mobjs.iterate(mobjs)
			if z.flags and ((z.flags & MF_ENEMY) or (z.flags & MF_BOSS) or ((z.type == MT_DETON or z.type == MT_EGGSHIELD or z.type == MT_EGGGUARD or z.type == MT_MINUS)))
			A_SAMUSMOBHP(z, p) --p is the player, z are mobjs in this case
			end
		end
	end	
end)

addHook("MobjDeath", function(m, i, so)
	if m.valid and m.flags
		if (m.flags & MF_BOSS)
			if not (m.type == MT_PZBOSS)
			and (netgame or (so and YouAreSamus(so)))
				if mobjinfo[m.type].doomednum == 2119 and mobjinfo[m.type].speed == 5 //Dark Knight fix
					for sector in sectors.iterate
						sector.special = 8192
					end
				end
				if not m.superbraksmetal
					for z in players.iterate
						if not netgame or z.speedrunoverride
						z.samustimeshow = 245
						end
					end
				end
				
				local samusrnd = P_RandomRange(1, 100)
				
				--Just Metal Sonic exclusive.
				if (m.type == MT_METALSONIC_BATTLE)
					m.samspawneditem = P_SpawnMobj(m.x+(66*FRACUNIT)+(samusrnd*FRACUNIT), m.y+(samusrnd*FRACUNIT), m.z+(samusrnd*FRACUNIT/3), MT_NRGD)
					m.samspawneditem = P_SpawnMobj(m.x-(66*FRACUNIT)+(samusrnd*FRACUNIT), m.y-(samusrnd*FRACUNIT), m.z+(samusrnd*FRACUNIT/3), MT_NRGD)
				end
				
				--What item do you need? LEt's give you a good reward.
				if (m.type == MT_CYBRAKDEMON)
					m.samspawneditem = P_SpawnMobj(m.x+(66*FRACUNIT)+(samusrnd*FRACUNIT), m.y+(samusrnd*FRACUNIT), m.z+(samusrnd*FRACUNIT/3), MT_NRGD)
					m.samspawneditem = P_SpawnMobj(m.x-(66*FRACUNIT)+(samusrnd*FRACUNIT), m.y-(samusrnd*FRACUNIT), m.z+(samusrnd*FRACUNIT/3), MT_NRGD)
				elseif (so and so.player and so.player.sammissilesmax and (so.player.sammissilesmax > 100) and so.player.samsupermissilesmax != nil and so.player.samsupermissilesmax < 10)
					m.samspawneditem = P_SpawnMobj(m.x, m.y, m.floorz, MT_STNK)
				elseif (so and so.player and so.player.samsupermissilesmax and (so.player.samsupermissilesmax > 9) and so.player.sampwmax != nil and (so.player.sampwmax < 5))
					m.samspawneditem = P_SpawnMobj(m.x, m.y, m.floorz, MT_PTNK)
				elseif (so and so.player and so.player.sams_itemcompletion != nil and (so.player.sams_itemcompletion >= 90) and not so.player.sam_screwattack)
					m.samspawneditem = P_SpawnMobj(m.x, m.y, m.z, MT_SATK)
				elseif (so and so.player and so.player.sams_itemcompletion != nil and (so.player.sams_itemcompletion >= 94) and not (so.player.sam_gravitysuit and so.player.sam_gravitysuit == 2))
					m.samspawneditem = P_SpawnMobj(m.x, m.y, m.z, MT_GVST)
				elseif (so and so.player and so.player.sams_itemcompletion != nil and (so.player.sams_itemcompletion >= 98) and not (so.player.sam_energytanksmax and so.player.sam_energytanksmax >= 12))
					m.samspawneditem = P_SpawnMobj(m.x, m.y, m.floorz, MT_ENGTNK)
				elseif (samusrnd < 15)
					m.samspawneditem = P_SpawnMobj(m.x, m.y, m.floorz, MT_MHBM)
				elseif (samusrnd < 30)
					m.samspawneditem = P_SpawnMobj(m.x, m.y, m.floorz, MT_CHBM)
				elseif (samusrnd < 50)
					m.samspawneditem = P_SpawnMobj(m.x, m.y, m.z, MT_VRST)
				elseif (samusrnd < 70)
					m.samspawneditem = P_SpawnMobj(m.x, m.y, m.floorz, MT_MTNK)
				elseif (samusrnd < 80)
					m.samspawneditem = P_SpawnMobj(m.x, m.y, m.floorz, MT_WVBM)
				elseif (samusrnd < 90)
					m.samspawneditem = P_SpawnMobj(m.x, m.y, m.floorz, MT_SPBM)
				elseif (samusrnd < 95)
					m.samspawneditem = P_SpawnMobj(m.x, m.y, m.floorz, MT_SPBL)
				elseif (samusrnd < 100)
					m.samspawneditem = P_SpawnMobj(m.x, m.y, m.floorz, MT_STNK)
				elseif (samusrnd == 100)
					m.samspawneditem = P_SpawnMobj(m.x, m.y, m.floorz, MT_PTNK)
				end
				
				--Spawn these on ALL bosses.
				m.samspawneditem = P_SpawnMobj(m.x+(20*FRACUNIT)+(samusrnd*FRACUNIT), m.y+(samusrnd*FRACUNIT), m.z+(samusrnd*FRACUNIT/3), MT_NRGA)
				m.samspawneditem = P_SpawnMobj(m.x-(samusrnd*FRACUNIT), m.y-(samusrnd*FRACUNIT), m.z, MT_SPKU)
				m.samspawneditem = P_SpawnMobj(m.x, m.y-(samusrnd*FRACUNIT), m.z+(samusrnd*FRACUNIT/2), MT_NRGB)
				m.samspawneditem = P_SpawnMobj(m.x+(samusrnd*FRACUNIT), m.y+(33*FRACUNIT), m.z+(samusrnd*FRACUNIT/4), MT_BMPKU)
				m.samspawneditem = P_SpawnMobj(m.x, m.y, m.floorz+(33*FRACUNIT), MT_EXPLODE)
				m.samspawneditem = P_SpawnMobj(m.x, m.y, m.floorz, MT_EXPLODE)
				S_StartSound(m, sfx_s3k44)
			end
			
		elseif ((m.flags & MF_ENEMY) or ((m.type == MT_EGGGUARD) or (m.type == MT_MINUS)))
			if (so and so.valid and so.player and YouAreSamus(so))
			local sami = so.player
				if not (i and i.valid and (i.hypered or i.type == MT_HBSHOT))
				sami.crystalflashchance = sami.crystalflashchance and $+1 or 1
				end
				if (i and i.valid and (i.type == MT_PBSHOT)) and sami.crystalflashchance --Gain extra for powerbeam kills
				sami.crystalflashchance = $+2
				end
				if not (sami.mobjscan1 == states[m.info.spawnstate].sprite) and not (sami.mobjscan2 == states[m.info.spawnstate].sprite) and not (sami.mobjscan3 == states[m.info.spawnstate].sprite)
				sami.mobjscan3 = sami.mobjscan2
				sami.mobjscan3weak = sami.mobjscan2weak
				sami.mobjscan3resist = sami.mobjscan2resist
				sami.mobjscan3missileresist = sami.mobjscan2missileresist
				sami.mobjscan2 = sami.mobjscan1 
				sami.mobjscan2weak = sami.mobjscan1weak
				sami.mobjscan2resist = sami.mobjscan1resist
				sami.mobjscan2missileresist = sami.mobjscan1missileresist
					if (states[mobjinfo[m.type].spawnstate].sprite == 0)
					sami.mobjscan1 = states[m.info.missilestate].sprite // print("Scanned enemy graphic[MINUSso]: "+(sprnames[m.sprite])+"A1")
					else
					sami.mobjscan1 = states[m.info.spawnstate].sprite // print("Scanned enemy graphic[so]: "+(sprnames[m.sprite])+"A1")
					end

					if m.spazweak
					sami.mobjscan1weak = "WEAPSPBM"
					elseif m.waveweak
					sami.mobjscan1weak = "WEAPWVBM"
					elseif m.iceweak
					sami.mobjscan1weak = "WEAPICBM"
					elseif m.plasmaweak
					sami.mobjscan1weak = "WEAPPLBM"
					else
					sami.mobjscan1weak = nil
					end
					if m.missileresist
					sami.mobjscan1missileresist = "MPKUA0"
					else
					sami.mobjscan1missileresist = nil
					end
					if m.spazresist
					sami.mobjscan1resist = "WEAPSPBM"
					elseif m.waveresist
					sami.mobjscan1resist = "WEAPWVBM"
					elseif m.iceresist
					sami.mobjscan1resist = "WEAPICBM"
					elseif m.plasmaresist
					sami.mobjscan1resist = "WEAPPLBM"
					else
					sami.mobjscan1resist = nil
					end

				end
			end
		elseif (i and i.valid and i.player and YouAreSamus(i))
		local sami = i.player
			if not (sami.mobjscan1 == states[m.info.spawnstate].sprite) and not (sami.mobjscan2 == states[m.info.spawnstate].sprite) and not (sami.mobjscan3 == states[m.info.spawnstate].sprite)
			sami.mobjscan3 = sami.mobjscan2
			sami.mobjscan3weak = sami.mobjscan2weak
			sami.mobjscan3resist = sami.mobjscan2resist
			sami.mobjscan3missileresist = sami.mobjscan2missileresist
			sami.mobjscan2 = sami.mobjscan1 
			sami.mobjscan2weak = sami.mobjscan1weak
			sami.mobjscan2resist = sami.mobjscan1resist
			sami.mobjscan2missileresist = sami.mobjscan1missileresist
				if (states[mobjinfo[m.type].spawnstate].sprite == 0)
				sami.mobjscan1 = states[m.info.missilestate].sprite //print("Scanned enemy graphic[MINUSi]: "+(sprnames[m.sprite])+"A1")
				else
				sami.mobjscan1 = states[m.info.spawnstate].sprite //print("Scanned enemy graphic[i]: "+(sprnames[m.sprite])+"A1")
				end

				if m.spazweak
				sami.mobjscan1weak = "WEAPSPBM"
				elseif m.waveweak
				sami.mobjscan1weak = "WEAPWVBM"
				elseif m.iceweak
				sami.mobjscan1weak = "WEAPICBM"
				elseif m.plasmaweak
				sami.mobjscan1weak = "WEAPPLBM"
				else
				sami.mobjscan1weak = nil
				end
				if m.missileresist
				sami.mobjscan1missileresist = "MPKUA0"
				else
				sami.mobjscan1missileresist = nil
				end
				if m.spazresist
				sami.mobjscan1resist = "WEAPSPBM"
				elseif m.waveresist
				sami.mobjscan1resist = "WEAPWVBM"
				elseif m.iceresist
				sami.mobjscan1resist = "WEAPICBM"
				elseif m.plasmaresist
				sami.mobjscan1resist = "WEAPPLBM"
				else
				sami.mobjscan1resist = nil
				end
			end
		end
	end

--Merging some MobjDeath hooks...

	if not m.valid
	or not m.flags
		return
	end
	if m.brakandco
		if so and so.player and YouAreSamus(so)
			if (so.player.score >= 100)
				so.player.score = $-100
			else
				so.player.score = 0
			end
			P_ResetScore(so.player)
			return
		end
		
//Drop RNG when enemies die by Samus' hand. 

	elseif (m.flags & MF_ENEMY)
	and (so and so.player and (YouAreSamus(so) or (multiplayer and gametype == GT_COOP)))
		if not (i and (i.type == MT_HBSHOT or (i.type == MT_SPAZERSHOCK and i.hypered)) ) --Downside from hyperbeam? No pickups.
		and not (m.type == MT_PZBOSS)
			local samusrng = P_RandomRange(1, 200)		
				if so and so.crystalflashtime and so.player and YouAreSamus(so)
					P_ResetScore(so.player)
				end
	--NOTE: Fix holdon later, it stays untouchable forever atm.			
				if m.dropmonforsam and (samusrng < 30) //Drop monitor loot from regular foes in stingy-asf levels
					if (samusrng < 2)
					m.droppedsammon = P_SpawnMobj(m.x, m.y, m.z, MT_INVULN_BOX)
					elseif (samusrng < 4)
					m.droppedsammon = P_SpawnMobj(m.x, m.y, m.z, MT_ARMAGEDDON_BOX)
					elseif (samusrng < 6)
					m.droppedsammon = P_SpawnMobj(m.x, m.y, m.z, MT_PITY_BOX)
					elseif (samusrng < 8)
					m.droppedsammon = P_SpawnMobj(m.x, m.y, m.z, MT_ELEMENTAL_BOX)
					elseif (samusrng < 11)
					m.droppedsammon = P_SpawnMobj(m.x, m.y, m.z, MT_WHIRLWIND_BOX)
					elseif (samusrng < 14)
					m.droppedsammon = P_SpawnMobj(m.x, m.y, m.z, MT_FORCE_BOX)
					elseif (samusrng < 17)
					m.droppedsammon = P_SpawnMobj(m.x, m.y, m.z, MT_ATTRACT_BOX )
					elseif (samusrng < 21)
					m.droppedsammon = P_SpawnMobj(m.x, m.y, m.z, MT_SNEAKERS_BOX)
					elseif (samusrng < 22)
					m.droppedsammon = P_SpawnMobj(m.x, m.y, m.z, MT_1UP_BOX)
					else
					m.droppedsammon = P_SpawnMobj(m.x, m.y, m.z, MT_SCORE1K_BOX)
					end
					if m.droppedsammon and m.droppedsammon.valid
						local ghs = P_SpawnMobj(m.x, m.y, m.z, MT_SAMSPAWNITEMSONIC)
						ghs.holdontime = 5
						ghs.ogmonitor = m.droppedsammon
						m.droppedsammon.holdon = true
						m.droppedsammon.samgotme = 0
						m.droppedsammon.samgotmehost = false
					end

				elseif (samusrng < 23)
					m.droppedsamitem = P_SpawnMobj(m.x, m.y, m.z+((m.height*2/3)*(P_MobjFlip(m))), MT_NRGA)
				elseif (samusrng < 28)
					m.droppedsamitem = P_SpawnMobj(m.x, m.y, m.z+((m.height*2/3)*(P_MobjFlip(m))), MT_NRGB)
				elseif (samusrng < 41)
						if so and so.player and (so.player.sammissilesmax and (so.player.sammissilesmax > 50))
						and not so.player.samsupermissilesmax
						m.droppedsamitem = P_SpawnMobj(m.x, m.y, m.z, MT_STNK)
								if m and ((m.flags2 & MF2_OBJECTFLIP) or (m.eflags & MFE_VERTICALFLIP))
								m.droppedsamitem.flags2 = $1|MF2_OBJECTFLIP
								end
						else
						m.droppedsamitem = P_SpawnMobj(m.x, m.y, m.z+((m.height*2/3)*(P_MobjFlip(m))), MT_MPKU)
						end
				elseif (samusrng < 43)
					if so and so.player and so.player.samsupermissilesmax
						m.droppedsamitem = P_SpawnMobj(m.x, m.y, m.z+((m.height*2/3)*(P_MobjFlip(m))), MT_SPKU)
					end
				elseif (samusrng < 44)
					if so and so.player and so.player.sampwmax
						m.droppedsamitem = P_SpawnMobj(m.x, m.y, m.z+((m.height*2/3)*(P_MobjFlip(m))), MT_PBOM)
					end
				elseif (samusrng < 46) // Pity Missile Tank
						if so and so.player and (so.player.sammissilesmax and (so.player.sammissilesmax < 15))
						m.droppedsamitem = P_SpawnMobj(m.x, m.y, m.z, MT_MTNK)
							if m and ((m.flags2 & MF2_OBJECTFLIP) or (m.eflags & MFE_VERTICALFLIP))
							m.droppedsamitem.flags2 = $1|MF2_OBJECTFLIP
							end
						end
				elseif (samusrng < 49) // Pity Space Jump
						if so and so.player and (so.player.sammissilesmax and (so.player.sammissilesmax > 40))
						and (so.player.samsupermissilesmax and so.player.samsupermissilesmax > 2)
						and not so.player.sam_spacejump and not so.player.samspbs
						m.droppedsamitem = P_SpawnMobj(m.x, m.y, m.z, MT_SJMP)
							if m and ((m.flags2 & MF2_OBJECTFLIP) or (m.eflags & MFE_VERTICALFLIP))
							m.droppedsamitem.flags2 = $1|MF2_OBJECTFLIP
							end
						end
				elseif (samusrng < 53) // Pity ChargeBeam
					if so and so.player and (so.player.sammissilesmax and (so.player.sammissilesmax > 15))
						if so.player.samsupermissilesmax and not so.player.sam_chargebeam
						and (so.player.sam_icebeam or (so.player.sam_wavebeam and so.player.sam_spazerbeam))
						m.droppedsamitem = P_SpawnMobj(m.x, m.y, m.z, MT_CHBM)
							if m and ((m.flags2 & MF2_OBJECTFLIP) or (m.eflags & MFE_VERTICALFLIP))
							m.droppedsamitem.flags2 = $1|MF2_OBJECTFLIP
							end
						end
					end
				elseif (samusrng < 110) // Mercy energy drop B
					if so and so.player and so.player.sam_energy and (so.player.sam_energy < (99+(so.player.sam_energytanksmax*50))/5)
						m.droppedsamitem = P_SpawnMobj(m.x, m.y, m.z+((m.height*2/3)*(P_MobjFlip(m))), MT_NRGB)
					end
				elseif (samusrng < 141) // Mercy energy drop D, because C does not exist.
					if so and so.player and so.player.sam_energy
					and ((so.player.sam_energy < (99+(so.player.sam_energytanksmax*50))/7) or (so.player.sam_energy < 40))
						m.droppedsamitem = P_SpawnMobj(m.x, m.y, m.z+((m.height*2/3)*(P_MobjFlip(m))), MT_NRGD) 
					end
				elseif (samusrng < 160) // Mercy missile drops
					if so and so.player
						if (so.player.sammissiles != nil and so.player.sammissilesmax and (so.player.sammissiles < so.player.sammissilesmax/2))
						m.droppedsamitem = P_SpawnMobj(m.x, m.y, m.z+((m.height*2/3)*(P_MobjFlip(m))), MT_MPKU)
						elseif so.player.sam_energy and (so.player.sam_energy < (99+(so.player.sam_energytanksmax*50))/5)
						m.droppedsamitem = P_SpawnMobj(m.x, m.y, m.z+((m.height*2/3)*(P_MobjFlip(m))), MT_NRGA)
						end
					end				
				elseif (samusrng < 190) // Mercy big missile drops
					if so and so.player
						if (so.player.sammissiles != nil and so.player.sammissilesmax and (so.player.sammissiles < so.player.sammissilesmax/3))
						m.droppedsamitem = P_SpawnMobj(m.x, m.y, m.z+((m.height*2/3)*(P_MobjFlip(m))), MT_BMPKU)
						elseif so.player.sam_energy and (so.player.sam_energy < (99+(so.player.sam_energytanksmax*50))/5)
						m.droppedsamitem = P_SpawnMobj(m.x, m.y, m.z+((m.height*2/3)*(P_MobjFlip(m))), MT_NRGA)
						end
					end
				elseif (samusrng < 198) // Mercy super missile drops
					if so and so.player and (so.player.samsupermissiles != nil and so.player.samsupermissilesmax and (so.player.samsupermissiles < so.player.samsupermissilesmax/3))
						m.droppedsamitem = P_SpawnMobj(m.x, m.y, m.z+((m.height*2/3)*(P_MobjFlip(m))), MT_SPKU)
					end
				elseif (samusrng < 200) // Mercy powerbomb drops
					if so and so.player and (so.player.sampw != nil and so.player.sampwmax and (so.player.sampw < so.player.sampwmax/2))
						m.droppedsamitem = P_SpawnMobj(m.x, m.y, m.z+((m.height*2/3)*(P_MobjFlip(m))), MT_PBOM)
					end
				end
				if so and so.crystalflashtime and YouAreSamus(so)
					if not P_RandomRange(0,10)
					m.droppedsamitem2 = P_SpawnMobj(m.x, m.y, m.z+((m.height*2)*(P_MobjFlip(m))), MT_PBOM)
					else
					m.droppedsamitem2 = P_SpawnMobj(m.x, m.y+50*FRACUNIT, m.z+((m.height*2)*(P_MobjFlip(m))), MT_SPKU)
					m.droppedsamitem2 = P_SpawnMobj(m.x-50*FRACUNIT, m.y+50*FRACUNIT, m.z+((m.height/3)*(P_MobjFlip(m))), MT_BMPKU)
					end
				end
				if m and m.thetank and samusrng --These drop extra goodies! Lucky you.
					if (samusrng < 50)
					m.droppedsamitem = P_SpawnMobj(m.x, m.y, m.z+((m.height*2/3)*(P_MobjFlip(m))), MT_BMPKU)
					elseif (samusrng < 120)
					m.droppedsamitem = P_SpawnMobj(m.x, m.y, m.z+((m.height*2/3)*(P_MobjFlip(m))), MT_NRGD)
					elseif (samusrng < 160)
					m.droppedsamitem = P_SpawnMobj(m.x, m.y, m.z+((m.height*2/3)*(P_MobjFlip(m))), MT_SPKU)
					elseif (samusrng < 180)
					m.droppedsamitem = P_SpawnMobj(m.x, m.y, m.z+((m.height*2/3)*(P_MobjFlip(m))), MT_PBOM)
					end
				end
				if m and m.valid and (m.eflags & MFE_VERTICALFLIP)
					if m.droppedsamitem and m.droppedsamitem.valid
					m.droppedsamitem.eflags = $1|MFE_VERTICALFLIP
					end
					if m.droppedsammon and m.droppedsammon.valid
					m.droppedsammon.eflags = $1|MFE_VERTICALFLIP					
					end
				end
		end
	end
end)

addHook("MobjRemoved", function(m)
	if not P_RandomRange(0,2)
		if not P_RandomRange(0,2)
		P_SpawnMobj(m.x, m.y, m.z+m.height/2, MT_SPKU)
		else
		P_SpawnMobj(m.x, m.y, m.z+m.height/2, MT_BMPKU)
		end
	end
end, MT_TNTBARREL)

addHook("MobjThinker", function(m)
	if m and m.valid and m.samreflector and m.target and m.target.valid
	local t = m.target
		if t.samreflecttime
		t.samreflecttime = $-1
		m.momx = t.momx
		m.momy = t.momy
		m.momz = t.momz
		P_MoveOrigin(m, t.x, t.y, t.z-t.height/4)
		elseif m and m.valid
		m.state = S_NULL
		end
	elseif m and m.valid and m.samreflector
		m.state = S_NULL
	end
end, MT_MSSHIELD_FRONT)

addHook("MobjDamage", function(m, i, so, dmg) --Custom boss counters to make life harder for Samus.
	if m and m.valid
	and m.flags and ((m.flags & MF_BOSS) or m.thetank) and m.health and (m.health > 1)
	and not ((m.type == MT_METALSONIC_BATTLE) or (m.type == MT_PZBOSS))
		if m.thetank
			if (m.health > 1) and not (so and so.player and so.player.hypermysticsonic)
			and not (i and (i.type == MT_HBSHOT) or (i.type == MT_ICCHARGECOMBO) or (i.type == MT_SPCHARGECOMBO))
			and ((i and i.player and not YouAreSamus(i)) or not (dmg and dmg > 1))
			
				if i and i.player and not YouAreSamus(i)
					i.momx = $*-1
					i.momy = $*-1
					i.momz = $*-1+(FRACUNIT*4*P_MobjFlip(i))
					i.state = S_PLAY_SPRING
					i.player.pflags = $ & ~PF_SPINNING & ~PF_JUMPED
					if not i.player.powers[pw_flashing]
					i.player.powers[pw_flashing] = 24
					end
				end
			
				for d = 1,5 --to encourage players to use more varied weaponry. 
					if P_RandomRange(0,1) 
					m.moseyflash = P_SpawnMobj(m.x, m.y, m.z+m.height/2, MT_SONIC3KBOSSEXPLODE)
					S_StartSound(m, sfx_cybdth)
					else
					S_StartSound(m, sfx_dmpain)
					m.moseyflash = P_SpawnMobj(m.x, m.y, m.z+m.height/2, MT_SMOKE)
					end
					m.moseyflash.scale = m.scale
					m.moseyflash.eflags = m.eflags
					m.moseyflash.destscale = m.scale*3
					m.moseyflash.momx = (P_RandomRange(-8, 8)*m.scale)
					m.moseyflash.momy = (P_RandomRange(-8, 8)*m.scale)
					P_SetObjectMomZ(m.moseyflash, (P_RandomRange(1, 3)*FRACUNIT))
				end
				m.moseyflash = P_SpawnMobj(m.x, m.y, m.z, MT_SMONTRACKER)
				m.moseyflash.target = m
				m.moseyflash.flashingt = 30
				m.samusHP = 7 m.samusHPmax = 7
				m.health = $-1
					if i and (i.flags & MF_MISSILE) and i.health
					P_ExplodeMissile(i)
					end
				return true
			else
				if (i and (i.type == MT_HBSHOT) or (i.type == MT_ICCHARGECOMBO) or (i.type == MT_SPCHARGECOMBO))
				m.health = 1 //You're not surviving these.
				end
				return nil
			end
		end
		if so and (YouAreSamus(so))
		and not (so.player and (so.player.bsblboostattack or so.player.sam_screwattacking or so.player.sam_shinesparkfly))
		and not so.sam_deathball
		and not ((gamemap == 40) and m.floorz == -1024*FRACUNIT)
		  if (m.type == MT_FANG)  --Little snowflake gets special treatment.
		  	for d = 0,3
				if m.target
				m.fbomb = P_SpawnMissile(m, m.target, MT_FBOMB)
				else
				m.fbomb = P_SpawnMissile(m, m, MT_FBOMB)
				end
				if m.fbomb and m.fbomb.valid
					if d == 0 //Lay a big scary trap.
					P_SpawnMobj(m.x, m.y, m.z, MT_EXPLODE)
					m.fbomb.momx = FRACUNIT/100 m.fbomb.momy = 0 m.fbomb.momz = FRACUNIT/100
					m.fbomb.z = m.fbomb.floorz+FRACUNIT
					m.fbomb.destscale = m.scale*2
					m.fbomb.flags = $|MF_NOGRAVITY
					m.fbomb.kamibomb = true
					else
					m.fbomb.momz = 6*FRACUNIT+(d*3*FRACUNIT)
					P_InstaThrust(m.fbomb, R_PointToAngle2(m.x, m.y, so.x, so.y), 8*FRACUNIT*d)
					end
					
					m.fbomb.fuse = 900
				end
			end
		  else
			local sambossrng = P_RandomRange(1, 5)
			if (sambossrng < 3) or (i.type == MT_HBSHOT) or m.superbrak or not netgame
				if (m.type == MT_EGGMOBILE4) or (m.type == MT_EGGMOBILE3) or ((m.type == MT_EGGMOBILE2) and m.health < 4)
				m.mmmmm = P_SpawnXYZMissile(m, so, MT_ENERGYBALL, m.x, m.y, m.z+210*FRACUNIT)
				m.mmmmm.flags = $1|MF_MISSILE & ~MF_PAIN
				m.mmmmm.samcounter = so
				m.mmmmm = P_SpawnXYZMissile(m, so, MT_ENERGYBALL, m.x+260*FRACUNIT, m.y+260*FRACUNIT, m.z+160*FRACUNIT)
				m.mmmmm.flags = $1|MF_MISSILE & ~MF_PAIN
				m.mmmmm.samcounter = so
				m.mmmmm = P_SpawnXYZMissile(m, so, MT_ENERGYBALL, m.x-260*FRACUNIT, m.y-260*FRACUNIT, m.z+160*FRACUNIT)
				m.mmmmm.flags = $1|MF_MISSILE & ~MF_PAIN
				m.mmmmm.samcounter = so
				 if ((m.type == MT_EGGMOBILE3) and (m.health > 4)) or ((m.type == MT_EGGMOBILE4) and (m.health > 2)) or ((m.type == MT_EGGMOBILE2) and m.health < 4)
				 m.mmmmm = P_SpawnMissile(m, so, MT_MSSHIELD_FRONT)
				 m.mmmmm.flags = $1|MF_NOCLIPHEIGHT|MF_NOCLIP
				 m.mmmmm.target = m
				 S_StartSound(m, sfx_s3k40)
				 m.mmmmm.samreflector = true
				 m.mmmmm.scale = m.height/34
					if (m.type == MT_EGGMOBILE4)
					m.samreflecttime = 200
					else
					m.samreflecttime = 110			
					end
				 end
				elseif m and m.valid and so
				m.mmmmm = P_SpawnMissile(m, so, MT_ENERGYBALL)
					if m.mmmmm and m.mmmmm.valid
					m.mmmmm.flags = MF_NOGRAVITY|MF_MISSILE & ~MF_PAIN
					m.mmmmm.samcounter = so
						if m.mmmmm and m.mmmmm.valid and so and so.valid and so.player and not so.player.cvirgin
						m.mmmmm.momx = $/2
						m.mmmmm.momy = $/2
						m.mmmmm.momz = $/2
						so.player.cvirgin = true
						end						
					end
					if m.superbrak and m.mmmmm and m.mmmmm.valid
					m.mmmmm.momx = $1/3
					m.mmmmm.momy = $1/3
					m.mmmmm.momz = $1/3
					end
				end
				return false
			else
				m.mmmmm = P_SpawnMissile(m, so, MT_MSSHIELD_FRONT)
				m.mmmmm.flags = $1|MF_NOCLIPHEIGHT|MF_NOCLIP
				m.mmmmm.target = m
				S_StartSound(m, sfx_s3k40)
				m.mmmmm.samreflector = true
				m.mmmmm.scale = m.height/34
				if (m.type == MT_EGGMOBILE4) or (m.type == MT_EGGMOBILE3)
				m.samreflecttime = 220
				else
				m.samreflecttime = 110
				end
				return false
			end
		  end			
		end
	end
end)

addHook("MobjThinker", function(s)
	if (s.valid and s.health > 3)
	 	if (s.state < S_EGGMOBILE4_RAISE1)
			if not s.samreflecttime or (s.samreflecttime < 5)
			s.mmmmm = P_SpawnMissile(s, s, MT_MSSHIELD_FRONT)
			s.mmmmm.flags = $1|MF_NOCLIPHEIGHT|MF_NOCLIP
			s.mmmmm.target = s
			s.mmmmm.samreflector = true
			s.mmmmm.scale = s.height/34
			s.samreflecttime = 122
			end
		elseif s.samreflecttime
		s.samreflecttime = 0
			if s.mmmmm and s.mmmmm.valid
			P_KillMobj(s.mmmmm)
			end
		end
	end
end, MT_EGGMOBILE4)

rawset(_G, "NoPushList", function (m)
	if not ((m.flags & MF_BOSS) and not ((m.type == MT_EGGMOBILE) or (m.type == MT_CYBRAKDEMON)))
	and not (m.type == MT_SNAILER) and not (m.type == MT_GFZFISH) and not (m.type == MT_POPUPTURRET)
	and not (m.type == MT_CANARIVORE) and not m.nosamuspushpls //Give an enemy NoSamusPushpls to become immune to pushing
	return false
	else
	return true
	end
end)

local function CritGhost(m, color)
	m.ghs = P_SpawnGhostMobj(m)
	m.ghs.color = color
	m.ghs.destscale = 99*FRACUNIT
	m.ghs.scalespeed = $*3
	m.ghs.colorized = true
end

addHook("ShouldDamage", function(m, i, so)
	if not (i or so)
		return
	end
	if (m.flags & MF_MONITOR) and so
		--Damnit, monitors, don't die to Samus' projectiles. Or let projectile characters like Fang screw everything over either.
		if not ((m.type == MT_EGGMAN_BOX) or (m.type == MT_EGGMAN_GOLDBOX))
			if YouAreSamus(so)
				return false --Samus never damages boxes. Only the systematic drops
			elseif so.player
				if so.skin == "duke" --Duke does Duke things
				return 			
				--Check if the player got the box before	
	  			elseif ( G_GametypeHasSpectators() or not
	  			(m.samgotme != nil and not (players[tonumber(0)] and so.player == players[tonumber(0)]) and (so.player.samusnum and (m.samgotme & (so.player.samusnum)) or m.samgotme == so.player.samusnum) )
	  			and not (players[tonumber(0)] and so.player == players[tonumber(0)] and m.samgotmehost) and not (mobjinfo[m.type].doomednum == 429) )
				return
				else
				return false
				end
			end
		end
		
	elseif (m.flags & (MF_ENEMY|MF_BOSS)) and i
	
			if m.missileresist and (i.type == MT_SAMMISSILE)
				if (so and so.player and YouAreSamus(so))
				so.player.sam_resisthud = 18
				end
			return false
			elseif m.iceresist and (i.type == MT_ICSHOT)
				if (so and so.player and YouAreSamus(so))
				so.player.sam_resisthud = 18
				end
			return false
			end
		
			if (m.flags & MF_ENEMY)
				if (i.type == MT_SPCHARGECOMBO)
					return false
				elseif ( (i.type == MT_SMONTRACKER) and i.target and (i.target.flags & MF_ENEMY)) or i.craymobfire
					return false
				end
				if m.dodgeret
				and (m.health and (i.flags & MF_MISSILE) and ((m.flags & MF_NOGRAVITY) or (P_IsObjectOnGround(m) and not (m.flags & MF_NOGRAVITY))) )
			
 				and not (i.type == MT_SAMSUPERMISSILE or i.type == MT_SAMPOWERBOMB or i.type == MT_HBSHOT or (i.type == MT_WVSHOT and m.waveweak)) 
				and not (m.eflags & MFE_SPRUNG)
					P_SpawnMobj(m.x, m.y, m.z, MT_EXPLODE)
					if not (m.target and m.target.valid and m.target.health) and i.target and i.target.player
						m.target = i.target
						A_FaceTarget(m)
					end
					if not (m.flags & MF_NOGRAVITY)
						P_SetObjectMomZ(m, 5*FRACUNIT, true)
					end
					if P_RandomRange(0,1) == 1
						P_InstaThrust(m, m.angle+ANGLE_90, 12*FRACUNIT)
					else
						P_InstaThrust(m, m.angle-ANGLE_90, 12*FRACUNIT)
					end
					if (m.type == MT_REDBUZZ)
						P_SpawnGhostMobj(m)
						m.z = $+120*FRACUNIT		
						S_StartSound(m, sfx_s3k41)
						A_SAMUSEXPLOSIONS(m, 22, MT_IVSP, false, 4000)
						A_SAMUSEXPLOSIONS(m, 16, MT_SUPERSPARK, false, 2200)
					end
					m.dodgecd = 75
					m.dodgeret = false
					if i.valid and i.health and (i.flags & MF_MISSILE)
						P_ExplodeMissile(i)
					end
				return false
 				end	
			end	
		
	elseif m.samreflecttime and ((i and YouAreSamus(i)) or (so and YouAreSamus(so)) or i and i.sambossreflect)
	and not (i and i.player and (i.player.sam_screwattacking or i.player.sam_shinesparkfly or i.player.bsblboostattack or i.sam_deathball))
		return false
	end
	
--Part two of the ShouldDamage hook	
	if m.samusHP and i and m.health and not (m.flags & MF2_FRET)
		if (so and so.player and YouAreSamus(so))
		so.player.sam_hitmult = 0
		so.player.sam_hithud = 18
			if not (m.type == MT_PZBOSS or m.type == MT_METROIDPART)
				if ((i.type == MT_HBSHOT) or ((i.type == MT_SPAZERSHOCK) and (m.hypered or i.hypered)))
					so.player.sam_hitmult = 6
				elseif m.superbrak and i and ((i.type == MT_SPAZERSHOCK or i.type == MT_SPCHARGECOMBO) and not m.hypered)
					so.player.sam_hitmult = 4
				elseif (i.type == MT_SAMSUPERMISSILE or (i.type == MT_SPAZERSHOCK and not m.hypered)
				or (i.player and (i.sam_deathball or i.player.sam_screwattacking or i.player.sam_shinesparkfly))
				or i.type == MT_SPCHARGECOMBO)
					so.player.sam_hitmult = 2
				elseif ((i.type == MT_SAMMORPHPBOMB) or (i.type == MT_SAMPOWERBOMB))
					if not i.phase
					so.player.sam_hitmult = 3
					end
				end
			end
		end
		if (i.type == MT_PBSHOT)
			if not (m.flags2 & MF2_FRET)
				if so and so.player --Nothing resists the power beam, but it does take longer to chew through.
					if not so.player.fusionmode
						if not m.powerbeamsamusHP
						m.powerbeamsamusHP = m.powerbeamsamusHP and $+1 or 1
						else
						m.samusHP = $-1
						end
					else
						if not (m.powerbeamsamusHP and m.powerbeamsamusHP >= 3) --Fusion Mode wants you to play smarter than this.
						m.powerbeamsamusHP = m.powerbeamsamusHP and $+1 or 1
						else
						m.samusHP = $-1
						end				
					end
				else
					m.samusHP = $-1
				end
				if m.samusHP
				S_StartSound(m, sfx_dmpain)
					if not NoPushList(m)
					P_Thrust(m, i.angle, 3*FRACUNIT)
					end
				end
			end
		elseif (i.type == MT_SPSHOT)
			if m.spazweak
			m.samusHP = $1-8
			CritGhost(m, SKINCOLOR_EMERALD)
			if so and so.valid
			samcriticalfx(m, R_PointToAngle2(m.x, m.y, so.x, so.y))
			else
			samcriticalfx(m)
			end
			S_StartSound(m, sfx_s3k81)
			A_SAMUSEXPLOSIONS(m, 4, MT_MPR4, false, 2000, SKINCOLOR_EMERALD)
			A_SAMUSEXPLOSIONS(m, 10, MT_MPR4, false, 1500, SKINCOLOR_GREEN)
			A_SAMUSEXPLOSIONS(m, 12, MT_SMOKE, false, 9000, SKINCOLOR_BLACK)
			A_SAMUSEXPLOSIONS(m, 7, MT_MPR1, false, 1000, SKINCOLOR_YELLOW)
				if (so and so.player and YouAreSamus(so))
				so.player.sam_crithud = 18
				end
			elseif m.spazresist
			m.samusHP = $-1
				if (so and so.player and YouAreSamus(so))
				so.player.sam_resisthud = 18
				end
			else
			m.samusHP = $1-2
			end
			if m.samusHP and (m.samusHP > 0)
			S_StartSound(m, sfx_dmpain)
				if not NoPushList(m)
				P_Thrust(m, i.angle, 13*FRACUNIT)
					if not (m.flags & MF_BOSS) and not m.spazresist
						if m.wavestunner and m.wavestunner.valid and m.wavestunner.spazerstunner
						m.wavestunner.state = S_NULL --Remove the last one
						end
						m.wavestunner = P_SpawnMobj(m.x, m.y, m.z+m.height/2, MT_WAVESTUNNER)
						m.wavestunner.target = m
						m.wavestunner.spazerstunner = 18
						m.wavestunner.spazerstunang = i.angle
						P_SpawnGhostMobj(m)
					end				
				end
			end
		elseif (i.type == MT_WVSHOT)
			if i.charged
			return
			end
			if m.waveweak
			CritGhost(m, SKINCOLOR_SUPERGOLD2)
				if so and so.valid
				samcriticalfx(m, R_PointToAngle2(m.x, m.y, so.x, so.y))
				else
				samcriticalfx(m)
				end
			m.samusHP = $1-8
			S_StartSound(m, sfx_s3k81)
			A_SAMUSEXPLOSIONS(m, 11, MT_SMOKE, false, 1000, SKINCOLOR_BLACK)
			A_SAMUSEXPLOSIONS(m, 7, MT_MPR1, false, 1000, SKINCOLOR_YELLOW)
				if (so and so.player and YouAreSamus(so))
				so.player.sam_crithud = 18
				end
			elseif m.waveresist
			m.samusHP = $-1
				if (so and so.player and YouAreSamus(so))
				so.player.sam_resisthud = 18
				end
			else
			m.samusHP = $1-2
			end
			if m.samusHP and (m.samusHP > 0)
			S_StartSound(m, sfx_dmpain)
				if not (m.flags & MF_BOSS) and not m.waveresist
				m.wavestunner = P_SpawnMobj(m.x, m.y, m.z+m.height/2, MT_WAVESTUNNER)
				m.wavestunner.target = m
				P_InstaThrust(m, 0, 0)
				end
			end
		elseif (i.type == MT_ICSHOT)
			if m.iceweak
			CritGhost(m, SKINCOLOR_ICY)
			m.samusHP = $1-9
			S_StartSound(m, sfx_s3k81)
			if so and so.valid
				samcriticalfx(m, R_PointToAngle2(m.x, m.y, so.x, so.y))
			else
				samcriticalfx(m)
			end
			A_SAMUSEXPLOSIONS(m, 7, MT_MPR8, false, 1000, SKINCOLOR_SKY)
			A_SAMUSEXPLOSIONS(m, 4, MT_MPR8, false, 2000, SKINCOLOR_WHITE)
			A_SAMUSEXPLOSIONS(m, 12, MT_SMOKE, false, 1000, SKINCOLOR_BLACK)
			A_SAMUSEXPLOSIONS(m, 8, MT_MPR1, false, 1200, SKINCOLOR_YELLOW)
				if (so and so.player and YouAreSamus(so))
					so.player.sam_crithud = 18
				end
			elseif m.iceresist
			m.samusHP = $-1
				if (so and so.player and YouAreSamus(so))
					so.player.sam_resisthud = 18
				end
			else
				m.samusHP = $1-3
			end
			if m.samusHP and (m.samusHP > 0)
			S_StartSound(m, sfx_dmpain)
				if not NoPushList(m)
				P_Thrust(m, i.angle, 3*FRACUNIT)
				end
			end
		elseif (i.type == MT_PLSHOT)
			if i.gattlingvortex
				if m.type == MT_METROIDPART
				m.samusHP = $1-5 --Not as effective against a certain Metroid boss
				else
				m.samusHP = $-12
				end
			elseif m.plasmaweak
			CritGhost(m, SKINCOLOR_CARBON)
			if so and so.valid
			samcriticalfx(m, R_PointToAngle2(m.x, m.y, so.x, so.y))
			else
			samcriticalfx(m)
			end
			m.samusHP = $-11
			A_SAMUSEXPLOSIONS(m, 14, MT_SMOKE, false, 1000, SKINCOLOR_BLACK)
			A_SAMUSEXPLOSIONS(m, 10, MT_MPR5, false, 90000, SKINCOLOR_RED, "nii")
			A_SAMUSEXPLOSIONS(m, 7, MT_MPR1, false, 1000, SKINCOLOR_YELLOW)
			S_StartSound(m, sfx_s3k81)
				if (so and so.player and YouAreSamus(so))
				so.player.sam_crithud = 18
				end
			elseif m.plasmaresist
			m.samusHP = $-1
				if (so and so.player and YouAreSamus(so))
				so.player.sam_resisthud = 18
				end
			else
				if m and (m.type <= MT_GFZFISH)
				and (so and so.player and YouAreSamus(so) and not so.player.fusionmode)
				m.samusHP = 0 --instakill from Plasma Shots
				else
				m.samusHP = $1-4
				end
			end
			if m.samusHP and (m.samusHP > 0)
			S_StartSound(m, sfx_dmpain)
				if not NoPushList(m)
				P_Thrust(m, i.angle, 25*FRACUNIT)
				P_SetObjectMomZ(m, 4*FRACUNIT, true)
				end
				P_ExplodeMissile(i)
			end
		else
			return --Don't continue if it's none of these.
		end

		if m.samusHP and (m.flags & (MF_ENEMY|MF_BOSS)) and m.health
			m.moseyflash = P_SpawnMobj(m.x, m.y, m.z+m.height/2, MT_SMOKE)
			m.moseyflash.scale = m.scale
			m.moseyflash.eflags = m.eflags
			m.moseyflash.destscale = m.scale*3
			m.moseyflash.momx = (P_RandomRange(-3, 3)*FRACUNIT)
			m.moseyflash.momy = (P_RandomRange(-3, 3)*FRACUNIT)
			P_SetObjectMomZ(m.moseyflash, (P_RandomRange(1, 3)*FRACUNIT))
			if ((m.type == MT_METROIDPART) and m.omega)
				m.omega.painfyellow = 7 --Use your own script.
				if i 
					if i.target and i.target.player and i.target.health
						m.omega.target = i.target --Switch target
					end
					if(i.type == MT_SAMMISSILE) and (m.samusHP > 5) --Make missiles extra effective
						m.samusHP = 5
					end
				end
			elseif (m.type == MT_PZBOSS)
				m.painfyellow = 7 --Use your own script.
				if i and i.target and i.target.player and i.target.health
				m.target = i.target --Switch target
				end	
			else
				m.moseyflash = P_SpawnMobj(m.x, m.y, m.z, MT_SMONTRACKER)
				m.moseyflash.target = m
				m.moseyflash.flashingt = 6
				if not m.target and (m.flags & MF_ENEMY)
					if i and i.target and i.target.valid and i.target.health and not (m.type == MT_GFZFISH)
					m.angle =  R_PointToAngle2(m.x, m.y, i.target.x, i.target.y)
					end
				end
			end
		end

		if m.valid and (m.samusHP <= 0)
			if m.health
				if ((m.flags & MF_BOSS) and not (m.type == MT_METALSONIC_BATTLE) and not ((m.type == MT_CYBRAKDEMON) or m.superstrongfoe) )
				m.samusHP = 6 m.samusHPmax = 6
					if i and (i.type == MT_PLSHOT)
					P_ExplodeMissile(i)
					end
				elseif (m.type == MT_CYBRAKDEMON) or m.superstrongfoe
				m.samusHP = 9 m.samusHPmax = 9
					if i and (i.type == MT_PLSHOT)
					P_ExplodeMissile(i)
					end
				else
					m.samusHP = 4 m.samusHPmax = 4
				end
			end
			return
		else
			return false
		end	
	end
end)