--Spawn a bunch of enemies. Only TYPE is required.
local function SuperBrakArmy(s, TYPE, XYDIST, ZHEIGHT, NUM)
	XYDIST = (XYDIST) or 1000<<16
	ZHEIGHT = (ZHEIGHT) or s.height/3
	NUM = (NUM) or 4
	local FOE = (type(TYPE) != "table") and TYPE or nil
	local ANG,ADDANG = s.angle+ANGLE_45, (360/NUM)*ANG1
	local ghs
	for i = 1,NUM
		ghs = P_SpawnMobj(
		s.x+FixedMul(XYDIST, cos(ANG)),
		s.y+FixedMul(XYDIST, sin(ANG)),
		s.z+s.height/6, (FOE) or TYPE[i] )
		if ghs and ghs.valid
			ghs.brakandco = true
			local ghs = P_SpawnMobjFromMobj(ghs,0,0,0, MT_EXPLODE) --Some smoke to give an alarm for the appearance.
			if ghs and ghs.valid
				ghs.renderflags = RF_SEMIBRIGHT|RF_NOCOLORMAPS
				ghs.scale = $*2
				ghs.destscale = 99<<16
				ghs.scalespeed = FRACUNIT/5
			end	
		end ANG = $+ADDANG 
	end
end

--Hyper (and later Super) Brak!
addHook("MobjThinker", function(s) if not s.valid return end
	local t = s.target local p = t and t.player
	
	if not s.superbrak --We're not Super Brak yet!
	
		if not s.superbraktransform --Start a check to see if we SHOULD be Super Brak.
			if s.superbrak == nil
				if (gamemap==28) or s.forcesuperbraktransform
					if mapheaderinfo[gamemap].lvlttl == "Black Core" or s.forcesuperbraktransform
						if not s.SUPERBRAKSTART
							local TRANSFORM = (SAMUSCAMPAIGN == true) and 1 or 2				
							if TRANSFORM == 1 --1 means transform. 2 means failed attempt.
								for z in players.iterate
									if z.valid and z.mo
										z.mo.z = 512<<16
									end
								end							
							end
							s.SUPERBRAKSTART = true
							s.superbraktransform = TRANSFORM
						end
					end
				else
					for z in players.iterate
						if z.mo and not z.sams_itemcompletion and YouAreSamus(z.mo)
							z.mo.samuszerorun = true
						end
					end
					s.SUPERBRAKSTART = true
					if s.superbraktransform == 2 //No superbrak, then.
					or (gamemap != 28) -- or not p.sam_fullypoweredsuit)
						s.superbrak,s.superbraktransform = 0,0
					end
				end
			end
		end
		--Transformation is happening!
		if s.superbraktransform == 1
			s.superbraktransform = 0
			s.superbrak = 1 --Confirmed SuperBrak!
			s.nosamuspushpls = true -- W-wait, I'm superbrak! Don't just push me around!
			s.destscale = 4*FRACUNIT --beeg
			s.health = 56 --Tanky AF. But that's why you collected those Beam Combos!
			S_StartSound(nil, sfx_beragh)
			S_StartSound(nil, sfx_beragh)
			S_StartSound(nil, sfx_bechrg)
			P_SetMobjStateNF(s, S_BLACKEGG_PAIN1) s.tics = 20
			P_StartQuake(15<<16, 32)
			A_LinedefExecute(s, LE_PINCHPHASE, nil)
			for z in players.iterate
				if z.valid
					z.sammissiles = z.sammissilesmax
					z.sampw = z.sampwmax
					z.samsupermissiles = z.samsupermissilesmax
					z.sam_energy = 99+(z.sam_energytanksmax*50)
					z.sam_hyperbeamammo = 0
					P_SamFP(z, PAL_NUKE, 12)
					if z.mo and z.mo.valid
						if z.sam_hyperbeam and YouAreSamus(z.mo)
							SamusDialogue(z.mo, "\x85 Warning! Hyper Beam energy drained!", nil, "SAMSAD", 1, 500)
							CONS_Printf(z, "\x85"+"You were drained of your Hyper Beam energy!")
						end
						z.mo.z = z.mo.floorz
						P_DoPlayerPain(z, s, s)
						P_PlayRinglossSound(z.mo)
						z.mo.hypernope = P_SpawnPointMissile(z.mo, z.mo.x, z.mo.y, z.mo.z+z.mo.height, MT_ENERGYBALL, s.x, s.y, s.z+s.height/2)
						z.mo.hypernope.flags = $|MF_MISSILE & ~MF_PAIN
						z.mo.hypernope.target = s
						z.mo.rdeathmsg = true
						S_StartSound(z.mo, sfx_gravch)
					end
				end
			end
			if (gamemap == 28) --Make sure we're altering the right map.
				P_SwitchWeather(PRECIP_STORM_NORAIN)			
				for SEC in sectors.iterate
					if SEC.floorheight == 608<<16
						SEC.floorheight = 512<<16
					end
					if SEC == sectors[3] or SEC == sectors[79] or SEC == sectors[8]
						if SEC == sectors[8]
							SEC.ceilingheight = 480<<16
						end
						SEC.floorheight = 480<<16
						SEC.floorpic = "ERZRCKF1" --Raise the death pits.
						SEC.special = 0
						SEC.lightlevel = (SEC == sectors[8]) and 142 or 192
					end
					if SEC.special
						if SEC.special == 18 
							SEC.special = 16
						elseif (GetSecSpecial(SEC.special, 1) > 4) --No death pits, damnit.
						or SEC.floorpic == "ERZRCKF1"
						or SEC.floorheight == 480<<16
							SEC.special = 0
						end
					end
				end
			end
		end
	end
	if not s.superbrak return end --
	
	if (s.health < 54)
		if not s.hdrop9
			s.hdrop9 = true
			s.health = 49
		end
		if (s.health < 36) 
			if not s.hdrop1
				local samusrnd = P_RandomRange(1, 100)
				local ghs = P_SpawnMobj(s.x+(20<<16)+(samusrnd<<16), s.y+(samusrnd<<16), s.z+(samusrnd<<16/3), MT_MPKU)
				ghs = P_SpawnMobj(s.x-(samusrnd<<16), s.y-(samusrnd<<16), s.z, MT_BMPKU)
				ghs = P_SpawnMobj(s.x, s.y-(samusrnd<<16), s.z+(samusrnd<<16/2), MT_MPKU)
				ghs = P_SpawnMobj(s.x+(samusrnd<<16), s.y+(33<<16), s.z+(samusrnd<<16/4), MT_MPKU)
				s.hdrop1 = true
				S_StartSound(nil, sfx_behurt)
				for nsa in players.iterate
					P_SamFP(nsa, PAL_RECYCLE, 6)
				end
				if SAMUSDIFFICULTY == 0
					s.health = 18 --Take a massive dive in health for a more casual player
				elseif SAMUSDIFFICULTY == 1
					s.health = 27 --Take a small dive in health for our player
				end
			end
			if (s.health < 25) 
				if not s.hdrop5
					local samusrnd = P_RandomRange(1, 100)
					local ghs = P_SpawnMobj(s.x+(20<<16)+(samusrnd<<16), s.y+(samusrnd<<16), s.z+(samusrnd<<16/3), MT_MPKU)
					ghs = P_SpawnMobj(s.x-(samusrnd<<16), s.y-(samusrnd<<16), s.z, MT_BMPKU)
					ghs = P_SpawnMobj(s.x, s.y-(samusrnd<<16), s.z+(samusrnd<<16/2), MT_BMPKU)
					ghs = P_SpawnMobj(s.x+(samusrnd<<16), s.y+(33<<16), s.z+(samusrnd<<16/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) 
					if not s.hdrop2
						local samusrnd = P_RandomRange(1, 100)
						local ghs = P_SpawnMobj(s.x+(20<<16)+(samusrnd<<16), s.y+(samusrnd<<16), s.z+(samusrnd<<16/3), MT_SPKU)
						ghs = P_SpawnMobj(s.x-(samusrnd<<16), s.y-(samusrnd<<16), s.z, MT_PBOM)
						ghs = P_SpawnMobj(s.x, s.y-(samusrnd<<16), s.z+(samusrnd<<16/2), MT_BMPKU)
						ghs = P_SpawnMobj(s.x+(samusrnd<<16), s.y+(33<<16), s.z+(samusrnd<<16/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.health < 1)
						s.health = 0
						s.flags = $ & ~MF_NOCLIPHEIGHT
						if (s.state == S_CYBRAKDEMON_DIE8) and not s.getouttaherep
							for z in players.iterate
								P_DoPlayerExit(z)
							end
							s.getouttaherep = true
							s.superbrak = 0
						end
						return --
					end						
				end
			end
		end
	end
	if s.sbexplodey
		s.sbexplodey = $-1
	end
	if (leveltime % 7 == 0)
		if (s.health < 12) or s.sbexplodey
			local samusrnd = P_RandomRange(-300, 300)
			local ghs = P_SpawnMobj(s.x+(samusrnd<<16), s.y+(samusrnd<<16), s.z+s.height/2+((samusrnd<<16)/2), MT_BOSSEXPLODE)
			ghs.scale = ((P_RandomRange(1,5))<<16)
		end
	end
//	if s.radius < 380<<16 
//	s.radius = 380<<16
//	end
	if (s.health < 6) 
	or (s.health < 8) and s.spazershocked
		if not s.hyperbeampanic
			s.health = 36
			s.state = S_BLACKEGG_PAIN1
			s.flags = ($|MF_NOGRAVITY|MF_NOCLIPHEIGHT) & ~MF_SHOOTABLE
			s.flags2 = $|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 z in players.iterate
				P_SamFP(z, PAL_RECYCLE, 9)
				z.hyperbrakmsg = 99
			end
			local ghs = P_SpawnMobj(s.x, s.y, s.z+s.z+s.height/6, MT_METALSONIC_BATTLE)
			ghs.health = 6
			ghs.scale = 75355
			ghs.superbraksmetal = true
			ghs.flags = $ & ~MF_SPECIAL
			ghs = P_SpawnMobj(s.x+1000<<16, s.y, s.z+s.z+s.height/14, MT_NRGD)
			ghs.hyperbeamammo = true
			ghs.scale = s.scale
			ghs = P_SpawnMobj(s.x-1000<<16, s.y, s.z+s.height/12, MT_NRGD)
			ghs.hyperbeamammo = true
			ghs.scale = s.scale
			ghs = P_SpawnMobj(s.x, s.y+1000<<16, s.z+s.height/13, MT_NRGD)
			ghs.hyperbeamammo = true
			ghs.scale = s.scale
			ghs = P_SpawnMobj(s.x, s.y-1000<<16, s.z+s.height/11, MT_NRGD)
			ghs.hyperbeamammo = true
			ghs.scale = s.scale
			for d = 1,3
				P_SpawnMobj(s.x, s.y-(500*d<<16), s.z+s.height/15, MT_NRGD)
				P_SpawnMobj(s.x, s.y+(500*d<<16), s.z+s.height/14, MT_NRGD)
				P_SpawnMobj(s.x-(500*d<<16), s.y, s.z+s.height/16, MT_NRGB)
				P_SpawnMobj(s.x+(500*d<<16), s.y, s.z+s.height/11, MT_NRGA)
			end
			s.health = 29
		end
	end

	if (s.z < s.floorz-300<<16) or (s.z > s.ceilingz+300<<16)
		P_MoveOrigin(s, s.x, s.y, s.floorz+20<<16)
		s.flags = $ & ~MF_NOCLIPHEIGHT
	end
	if s.jumpspam == nil
		s.jumpspam = 0
	elseif s.jumpspam
		s.jumpspam = $-1
	end
	if t and not (t.player or t.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 = $+70
				s.superbrakjumpdirect = brakrngc
				S_StartSound(nil, sfx_beflap)
				S_StartSound(nil, sfx_beflap)
				s.state = S_BLACKEGG_JUMP2
				P_SetObjectMomZ(s, 20<<16, false)
			elseif P_IsObjectOnGround(s)
				s.superbrakbigjump = 60
				S_StartSound(nil, sfx_belnch) 
				S_StartSound(nil, sfx_belnch)
				s.jumpspam = $+80
				s.state = S_BLACKEGG_JUMP2
				P_SetObjectMomZ(s, 30<<16, 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<<16, 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<<16, 1, SKINCOLOR_SKY, "brakdust")
			end
			s.superbrakquickfret2 = false
			s.superbrakquickfret = false
		end
		if s.fretmax == 1
			s.fretmax = 0
			s.flags = $|MF_SHOOTABLE
			s.flags2 = $ & ~MF2_FRET
		end
	end
	if (s.state == S_CYBRAKDEMON_VILE_ATTACK2)
		if not s.vilesbextend
			s.tics = $+18
			s.vilesbextend = true
		end
	elseif 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 = $ & ~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)
			s.sidejumpangle = s.angle+((brakrnga < 10) and ANGLE_90 or -ANGLE_90)
			s.superbrakjump = 35
			s.jumpspam = $+70
			s.superbrakjumpdirect = brakrnga
			S_StartSound(nil, sfx_beflap)
			S_StartSound(nil, sfx_beflap)
			s.state = S_BLACKEGG_JUMP2
			P_SetObjectMomZ(s, 20<<16, false)

		elseif (brakrnga < 24) and (s.health < 49) and not (s.jumpspam > 150)
			s.state = S_CYBRAKDEMON_NAPALM_ATTACK1
			s.jumpspam = $+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 = $+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 = $+110
			s.state = S_BLACKEGG_JUMP2
			P_SetObjectMomZ(s, 30<<16, false)
		elseif (brakrnga < 32) and (s.health < 47)
			s.state = S_BLACKEGG_MISSILE1
			s.tics = 2

		elseif (brakrnga < 46) and (s.health < 49) and t
			A_LobShot(s, MT_CANNONBALL, 8)
			S_StartSound(nil, sfx_cannon)
		end
	end
	if (s.tics < 3)
		if (s.state == S_BLACKEGG_DESTROYPLAT3) or (s.state == S_BLACKEGG_JUMP2) 
		or (s.state == S_BLACKEGG_MISSILE3) or (s.state == S_BLACKEGG_PAIN35)
			if s.health
				if (s.state == S_BLACKEGG_PAIN35)
					s.flags = $|MF_SHOOTABLE & ~(MF_BOUNCE|MF_NOCLIPHEIGHT|MF_NOGRAVITY)
				end
				s.state = S_CYBRAKDEMON_WALK1
			end
		end
	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<<16)/3), false)
			P_SpawnGhostMobj(s)
		elseif (s.superbrakbigjump < 25)
			if (s.superbrakbigjump < 10)
				P_SetObjectMomZ(s, -20<<16, false)			
			elseif (s.superbrakbigjump < 18)
				P_SetObjectMomZ(s, -9<<16, false)
			else
				P_SetObjectMomZ(s, -1<<16, false)
			end
		end
		if t
			local SPEED = 33<<16
			if (s.superbrakbigjump > 20) and (s.superbrakbigjump < 40) and (FixedHypot(t.x-s.x, t.y-s.y) > 1800<<16) 
				SPEED = 66<<16
			end
			P_InstaThrust(s, s.angle, SPEED)
		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<<16, 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<<16, 1, SKINCOLOR_SKY, "brakdust")
			A_SAMUSEXPLOSIONS(s, 6, MT_BOSSEXPLODE, 25<<16, 90)
			A_SAMUSEXPLOSIONS(s, 4, MT_EXPLODE, 41<<16, 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<<16)
		if (s.superbrakjump < 30) and (s.superbrakjump > 4) and (s.superbrakjump % 2 == 0)
			if t
				S_StartSound(nil, sfx_befire)
				P_SpawnMissile(s, t, MT_BLACKEGGMAN_GOOPFIRE)
			end
		end
		if (s.superbrakjump > 24)
			P_SetObjectMomZ(s, ((s.superbrakjump<<16)/2), false)
			P_SpawnGhostMobj(s)
		elseif (s.superbrakjump < 9)
			P_SetObjectMomZ(s, -18<<16, false)
		elseif (s.superbrakjump < 19)
			P_SetObjectMomZ(s, -8<<16, false) 
		elseif (s.superbrakjump < 25)
			P_SetObjectMomZ(s, -1<<16, 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<<16, 6)
			S_StartSound(nil, sfx_befall)
			A_SAMUSEXPLOSIONS(s, 15, MT_BOSSEXPLODE, 15<<16, 90)
			A_SAMUSEXPLOSIONS(s, 20, MT_EXPLODE, 21<<16, 90)
			P_InstaThrust(s, 0, 0)
		end
	end
	if (leveltime % 8 == 0) --Falling rocks!
		local ghs = P_SpawnMobj(P_RandomRange(-4010, 4010)<<16, P_RandomRange(-4010, 4010)<<16, 1900<<16, MT_FALLINGROCK)
		ghs.scale = $*P_RandomRange(1, 5)
		ghs.flags = $|MF_NOGRAVITY|MF_MISSILE|MF_NOBLOCKMAP & ~(MF_SOLID|MF_PAIN)
		ghs.radius = 1
		ghs.target = s
		ghs.satansrock = true
		if (leveltime % 24 == 0) --Skybox rock.
			local ghs = P_SpawnMobj(P_RandomRange(6560, 7773)<<16, P_RandomRange(2200, 1100)<<16, 270<<16, MT_FALLINGROCK)
			ghs.scale = ($/3)*P_RandomRange(1,4)
			ghs.flags = $|MF_NOGRAVITY|MF_NOBLOCKMAP|MF_NOCLIPHEIGHT|MF_NOCLIP & ~(MF_SOLID|MF_PAIN)
			ghs.fuse = 250
			ghs.momz = -(s.scale*2/3)
			ghs.target = s
		end
	end
	--Brak Time starts an barrage of energy or enemies!
	if s.braktime
		local p = (t) and t.player
		local brakRNGr = P_RandomRange(1,10)
		local ANG = s.angle+(brakRNGr*ANG1*6)
		
		if p and YouAreSamus(t)
			local ENERGY = (t.player.sam_energy) or 0
			if (ENERGY < 166)
				if not netgame and not s.eggmercyonce
					P_SpawnMobj(s.x, s.y-500<<16, s.z+s.height/15, MT_NRGD)
					P_SpawnMobj(s.x, s.y+500<<16, s.z+s.height/14, MT_NRGD)
					P_SpawnMobj(s.x-500<<16, s.y, s.z+s.height/11, MT_NRGD)
					P_SpawnMobj(s.x+500<<16, s.y, s.z+s.height/13, MT_BMPKU)
					s.eggmercyonce = true
				end
				if (ENERGY < 99)
					local ghs = P_SpawnMobj(s.x+200<<16, s.y-200<<16, s.z+s.height/12, MT_NRGB)
					ghs = P_SpawnMobj(s.x-200<<16, s.y+200<<16, s.z+s.height/13, MT_NRGA)
					ghs = P_SpawnMobj(s.x, s.y, s.z+s.height/11, MT_NRGA)
				end
			end
		end
		
		if s.hyperbeampanic and (leveltime%2==0)
			for i = 1,4
				local Eorb = P_SpawnMobjFromMobj(s, 
				FixedMul(1000<<16, cos(ANG)), FixedMul(1000<<16, sin(ANG)), 
				s.z+s.z+(s.height/P_RandomRange(10,13)), MT_NRGD)
				if Eorb and Eorb.valid
					local ghs = P_SpawnMobjFromMobj(Eorb,0,0,0, MT_IVSP) --Some smoke to give an alarm for the appearance.
					if ghs and ghs.valid
						ghs.renderflags = RF_SEMIBRIGHT|RF_NOCOLORMAPS
						ghs.fuse = 32
						ghs.dispoffset = 8
						ghs.scale = $*2
					end
					Eorb.hyperbeamammo = true
					Eorb.scale = s.scale
				end ANG = $+ANGLE_90
			end
		end
		if (brakRNGr > 7)
			SuperBrakArmy(s, {MT_SPRINGSHELL, MT_EGGGUARD, MT_JETTGUNNER,MT_JETTGUNNER}, 600<<16, s.height/2, 4)
		elseif (brakRNGr > 5)
			SuperBrakArmy(s, {MT_FACESTABBER, MT_UNIDUS, MT_ROBOHOOD}, 600<<16, s.height/10, 3)
			local ghs = P_SpawnMobj(s.x-400<<16, s.y, s.z, MT_MINUS)
			ghs.brakandco = true
		elseif (brakRNGr > 3)
			SuperBrakArmy(s, {MT_GFZFISH, MT_JETTBOMBER, MT_JETTBOMBER, MT_GSNAPPER}, 600<<16, s.height/2, 4)
		elseif (brakRNGr > 2)
			SuperBrakArmy(s, MT_GSNAPPER, 1000<<16, s.height/6, 4)
		elseif (brakRNGr > 1)
			SuperBrakArmy(s, MT_VULTURE, 980<<16, s.height/6, 4)
		else
			SuperBrakArmy(s, MT_DETON, 1200<<16, s.height/3, 4)
		end
		if p and (SAMUSDIFFICULTY != 0)
			ActivateHyperMode(p, 7)
		end
		s.braktime = false
	end
end, MT_CYBRAKDEMON)

--Achievement1 is any%, achievement2 is 100%, achievement 3 is 5% clear.
local function AwardAchievements(p, SUPERBRAK) --Only in Black Core, not custom campaign Braks!
	if not (gamemap==28 and SUPERBRAK) and not (gamemap==27) 
	or mapheaderinfo[gamemap].lvlttl != "Black Core"
	or not (p and p.valid and p.mo and YouAreSamus(p.mo))
		return --Didn't meet the right requirements.
	end local s = p.mo
	
	if SAMUSCAMPAIGN == false --No achievements awarded in SRB2's vanilla campaign!
		ProfileSamusSave(s, p, p.sam_saveselected, true, false) return --
	end
	
	local ITEMRATE = p.sams_itemcompletion or 0
	if p.sam_saveselected  -- and not p.sam_achievementlock
		local TIER = (SAMUSDIFFICULTY==2) and 3 or (SAMUSDIFFICULTY!=0) and 2 or 1 --Tier 3 is gold, 2 is silver, 1 is bronze!
		if (ITEMRATE <= 5)
			if not p.sam_achievement3 or (p.sam_achievement3 < TIER) --5% or lower! Wow!
				p.sam_achievement3 = TIER
				CONS_Printf(p, "\x82"+"Unlocked new 5% items achievement!")
			end								
		elseif ((ITEMRATE >= 100) or p.sam_fullypoweredsuit) and SUPERBRAK --100% complete!
			if not p.sam_achievement2 or (p.sam_achievement2 < TIER)
				p.sam_achievement2 = TIER
				CONS_Printf(p, "\x82"+"Unlocked new 100% items achievement!")
			end						
		end	
		if not p.sam_achievement1 or (p.sam_achievement1 < TIER) 	--Always get clear achievement
			p.sam_achievement1 = TIER
			CONS_Printf(p, "\x82"+"Unlocked new CLEAR achievement!")
		end
		ProfileSamusSave(s, p, p.sam_saveselected, true, false)
	end
	if not ITEMRATE and s.health
		s.samuszerorun = true --Beat the game at 0%? You are truly the ultimate player!
	end	
end

addHook("MobjDeath", function(s, i, so)
	if not s.superbrak
		for z in players.iterate
			AwardAchievements(z)
		end
	elseif s.valid
		s.flags = $|MF_BOUNCE & ~MF_NOGRAVITY
		P_StartQuake(14<<16, 50)
		S_StartSound(nil, sfx_begrnd)
		S_StartSound(nil, sfx_becrsh)
		S_StartSound(nil, sfx_bkpoof)
		S_StartSound(nil, sfx_bkpoof)
		for z in players.iterate
			P_SamFP(z, PAL_MIXUP, 5)
			AwardAchievements(z, 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 = $|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) --Don't take damage from your own goons.
		or so and (so.flags & MF_ENEMY)
			return false --
		end
		if not s.hyperbeampanic and (s.health < 6) --Fix to not accidentally skip phase.
			return false --
		end
	end
end, MT_CYBRAKDEMON)

addHook("MobjDamage", function(s, i, so)
	if s.valid and s.superbrak and not s.braktime
		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<<16, s.y, s.z+s.height/17, MT_BMPKU) -- Mercy ammo		
			end
		end
	end
end, MT_CYBRAKDEMON)

local function PTMakeShield(s) 
	if not (s and s.valid) return end --
	s.samusHP = 2  
	s.samusHPmax = 2
	s.scale = 230712
	s.renderflags = RF_FULLBRIGHT|RF_NOCOLORMAPS
	s.flags = $|MF_SHOOTABLE|MF_NOGRAVITY|MF_NOCLIPHEIGHT
	s.height = 200<<16
	s.radius = 200<<16
	s.brakandco = true
	s.IsAShield = true
end
addHook("MobjThinker", function(s) if not s.valid return end
	local t = s.target
	if t and t.superbrak --This behaviour is only applied to SUPER Brak.
		if not t.hyperbeampanic
			s.scale = t.scale*10/7
			s.angle = t.angle
			if not s.shieldspawned
				s.shield1 = P_SpawnMobj(s.x, s.y, s.z+t.height+220<<16, MT_POPUPTURRET)
				PTMakeShield(s.shield1)
				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)
				PTMakeShield(s.shield2)
				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)
				PTMakeShield(s.shield3)
				
				s.shieldspawned = true
			end
		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 t and (t.health < 12)
			if s and s.valid and t and t.superbrak and s.health
				t.flags = $|MF_SHOOTABLE
				t.flags2 = $ & ~MF2_FRET
				P_KillMobj(s) 
			end
		else --At least SOME are valid then.
			for i = -1,1
				local SH = i==-1 and s.shield3 or i==0 and s.shield1 or s.shield2
				if SH and SH.valid and SH.health
					SH.sprite,SH.frame = SPR_RCRY,B
					SH.tics,SH.fuse = 5,5
					if i == 0
						P_MoveOrigin(SH, t.x, t.y, t.z+t.height+220<<16)
					else
						P_MoveOrigin(SH, 
						t.x+FixedMul(440<<16, cos(t.angle+(ANGLE_90*i))), 
						t.y+FixedMul(440<<16, sin(t.angle+(ANGLE_90*i))), 
						t.z + t.height*10/15)
					end
				end
			end		
		end
		if s and s.valid
			if not s.health
				for i = 1,3
					local SH = i==3 and s.shield3 or i==2 and s.shield2 or s.shield1
					if SH and SH.valid SH.state = S_NULL end
				end
			elseif t and t.valid and not t.hyperbeampanic
				t.flags = $ & ~MF_SHOOTABLE
			end
		end
	end
end, MT_CYBRAKDEMON_ELECTRIC_BARRIER)


--What is THIS, then? Disappear, damn you.
local function FlameSpawn(s)
	if (gamemap==28) and s.valid and not s.fuse
		s.fuse = 350
		local RNG = P_RandomRange(2,8)
		if RNG == 7
			s.state = S_MINIFIR1
			s.blendmode = AST_ADD
		else
			s.scale = 24000*RNG
		end
	end
end
addHook("MobjSpawn", FlameSpawn, MT_CYBRAKDEMON_FLAMEREST)
addHook("MobjSpawn", FlameSpawn, MT_CYBRAKDEMON_NAPALM_FLAMES)
addHook("MobjSpawn", FlameSpawn, MT_SPINFIRE)

--The turrets are actually the switches in this fight!
addHook("MobjCollide", function(s, i)
	if s.valid and s.IsAShield 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) local t = i.target
			if t and t.player and YouAreSamus(t)
				t.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<<16)
			s.fuse = 150
			s.momz = (-10<<16)/(s.scale/FRACUNIT)-145355
		elseif s.target and not s.Landed
			s.Landed = true
			A_samquake(s, 40<<16, 4, 250*FRACUNIT)
			local ghs = P_SpawnXYZMissile(s.target, s, MT_CYBRAKDEMON_NAPALM_BOMB_SMALL, s.x, s.y, s.z)
			if ghs and ghs.valid
				ghs.fuse,s.fuse = 60,60
				P_ExplodeMissile(ghs)
			end
			if (s and s.valid) s.state = S_NULL return end --
		end
	end
end, MT_FALLINGROCK)