--Golden Shine: Most Cell Games code is already handled in LUA_XPACK, but I'll add Area Destruction Lua here for convenience

--Destroys all tagged sectors with graphics that look better and also DON'T lag the crap out of SRB2.
local function DBZbreak(line, s)
	if not (line.frontsector and line.frontsector.valid) or not (s and s.valid)
		return --
	end
	local sec = line.frontsector
	local TAG = (line) and line.tag or 0
	
	if not (sec.ceilingheight == ((-9999)<<16)+15 )
		for fof in sec.ffloors()
			if fof and fof.valid
				fof.topheight = ((-9999)<<16)+15
				fof.bottomheight = ((-9999)<<16)+15
				fof.flags = 0
			end
		end
		for i = 1,2
			local slope = (i==1) and sec.f_slope or sec.c_slope
			if slope 
				slope.zdelta = 0
				slope.zangle = 0
				if slope.o
					slope.o = {
					x = 0,
					y = 0,
					z = (-9999<<16)+15
					}
				end
			end
		end
		sec.ceilingheight = ((-9999)<<16)+15
		sec.floorheight = ((-9999)<<16)+15
		sec.tag,sec.special = 0,0
		P_NukeEnemies(s, s, 840<<16)
		
		local ANG,DIST,ghs = 0,32*s.scale, nil
		for i = 1,29
			ghs = P_SpawnMobj(s.x, s.y, s.z+(s.momz/3)+((i%2) and -32<<16 or 64<<16), MT_THOK)
			ghs.state = S_ROCKCRUMBLEA
			ghs.renderflags = RF_SEMIBRIGHT
			ghs.flags = MF_NOCLIPTHING|MF_BOUNCE|MF_GRENADEBOUNCE|MF_SOLID
			ghs.fuse = 99
			ghs.scale = P_RandomRange(2, 10)<<16
			ghs.momx,ghs.momy = (i%2) and s.momx/2 or s.momx*2/3, (i%2) and s.momy/2 or s.momy*2/3
			ghs.momz = (s.momz/3)+P_RandomRange(-18,30)<<16
			P_Thrust(ghs, ANG, P_RandomRange(16, 160)<<16)
			ghs.height = 1
			ghs.shadowscale = 75000
			ANG = $+ANG1*15
		end
		for i = 1,13
			ghs = P_SpawnMobj(s.x+FixedMul(DIST, cos(ANG)), s.y+FixedMul(DIST, sin(ANG)), s.z, MT_THOK)
			ghs.state = (i%3==0) and S_SPINDUST2 or S_SPINDUST1
			ghs.renderflags = RF_SEMIBRIGHT|RF_NOCOLORMAPS
			ghs.flags = MF_NOBLOCKMAP|MF_NOCLIPHEIGHT|MF_NOCLIPTHING|MF_NOCLIP|MF_SCENERY|MF_NOGRAVITY
			ghs.scale = P_RandomRange(5, 19)<<16
			ghs.destscale = 99<<16
			ghs.fuse = 66
			ghs.momx,ghs.momy = (i%2) and s.momx or s.momx*2/3, (i%2) and s.momy or s.momy*2/3
			ghs.momz = s.momz/2
			P_Thrust(ghs, ANG, P_RandomRange(16, 89)<<16 )
			ANG = $+ANG30
		end
		DIST = 264*s.scale
		for i = 1,10
			ghs = P_SpawnMobj(s.x+FixedMul(DIST, cos(ANG)), s.y+FixedMul(DIST, sin(ANG)), s.z, MT_THOK)
			ghs.state = S_WATERZAP
			ghs.renderflags = RF_FULLBRIGHT|RF_NOCOLORMAPS
			ghs.flags = MF_NOBLOCKMAP|MF_NOCLIPHEIGHT|MF_NOCLIPTHING|MF_NOCLIP|MF_SCENERY|MF_NOGRAVITY
			ghs.scale = 10<<16
			ghs.destscale = 99<<16
			ghs.fuse = 99
			ghs.momx,ghs.momy,ghs.momz = s.momx/2,s.momy/2,s.momz/4
			P_Thrust(ghs, ANG, P_RandomRange(11, 60)<<16)
			ANG = $+ANG1*36
		end
		DIST = 96*s.scale
		for i = 1,5
			ghs = P_SpawnMobj(s.x+FixedMul(DIST, cos(ANG)), s.y+FixedMul(DIST, sin(ANG)), s.z+(s.height/2), MT_THOK)
			ghs.state = S_TNTDUST_1
			ghs.flags = MF_NOBLOCKMAP|MF_NOCLIPHEIGHT|MF_NOCLIPTHING|MF_NOCLIP|MF_NOGRAVITY
			ghs.fuse = 1500
			ghs.scalespeed = $*((i%2) and 2 or 1)
			ghs.destscale = 99<<16
			ghs.shadowscale = FRACUNIT
			ghs.momx,ghs.momy = s.momx/2,s.momy/2
			P_Thrust(ghs, ANG, 46<<16)
			ANG = $+(72*ANG1)
		end
		local XSOUND = {sfx_bkpoof, sfx_brakrx, sfx_kc40, sfx_kc41}
		if not (leveltime%5==0) and (skins["xsonicserious"]) and (skins["xsonic"]) --There's a chance people didn't add X Sonic...
			XSOUND = {sfx_xssfxt, (leveltime%2) and sfx_xssfxp or sfx_brakrx, sfx_xssfxs, (leveltime%8==0) and sfx_kc40 or sfx_kc41}
		end
		for i = 1,2
			P_TryMove(s, s.x-s.momx, s.y-s.momy, true)
		end
		s.momx,s.momy = $*2/3,$*2/3
		for z in players.iterate
			if z and z.valid and (z==displayplayer)
				if not (z.mo and z.mo.valid) or z.mo==s
					P_StartQuake(142<<16, 24)
					P_FlashPal(z, PAL_NUKE, 7)
					for i = 1,#XSOUND
						S_StartSound(nil, XSOUND[i], z)
					end
				else
					local DIST = FixedHypot(s.x-z.mo.x, s.y-z.mo.y)
					if (DIST < 22000<<16) --Max dist.
						if (DIST < 7500<<16)
							P_StartQuake(111<<16, 24)
							P_FlashPal(z, PAL_NUKE, 5)
							for i = 1,#XSOUND
								S_StartSoundAtVolume(nil, XSOUND[i], 230, z)
							end
						else
							P_StartQuake(80<<16, 24)
							for i = 1,#XSOUND
								S_StartSoundAtVolume(nil, XSOUND[i], 170, z)
							end
						end
					end
				end
			end
		end		
	end
end
addHook("LinedefExecute", DBZbreak, "DBZBRK") --Destroy all floors.