local function SingleFreeSlot(...)
	for _,slot in ipairs({...})
		if not rawget(_G, slot) freeslot(slot) end --doth not exist? Then add.
	end
end
SingleFreeSlot("sfx_yuedth", "MT_YUEDTH", "SPR_GPLZ")

mobjinfo[MT_YUEDTH] = {
spawnstate = S_THOK,
deathsound = sfx_yusfxz,
flags = MF_NOCLIP|MF_NOCLIPHEIGHT|MF_NOCLIPTHING|MF_NOBLOCKMAP|MF_SCENERY|MF_NOGRAVITY
}

local SAEXPLOSIONS = true

--Hmm...I can't think a better way than a global MobjDeath hook. Oh well, it should be okay!
addHook("MobjDeath", function(s, i, so)
	if SAEXPLOSIONS == false return end --
	if s.valid and (s.flags & MF_ENEMY) and not (s.flags & (MF_BOSS|MF_NOSECTOR|MF_MONITOR))
	
		local type = s.type

		if not ((type > MT_BUGGLE) and (type < MT_SPINBOBERT_FIRE2))
		and not ((type > MT_COIN) and (type <= MT_SHLEEP)) --Hey, these are NOT robots!
		and not (type == MT_FANG)
		
			if not s.spawnedyuexplosion --Makes the mod stackable and modifyable. You can add it to a MobjSpawn hook for example.
			and not s.player --...duh.
			
				s.spawnedyuexplosion = true
				s.flags2 = $|MF2_DONTDRAW
				s.spritexscale,s.spriteyscale = 1,1
				
				local sh = P_SpawnMobjFromMobj(s,0,0,0,MT_THOK) --This invisible object makes the sound for us.
				sh.tics,sh.fuse = 60,60 sh.flags2 = MF2_DONTDRAW sh.spriteyscale = 1
				
				S_StartSound(sh, sfx_yuedth)
			
				local ghs = P_SpawnMobjFromMobj(s, 0, 0, s.height/3, MT_YUEDTH)
				ghs.scale = max(s.scale, s.height/32) --A bigger explosion the bigger you are!
				ghs.target = s
				ghs.scalespeed = FRACUNIT/6
				ghs.destscale = 99<<16
				for z in players.iterate
					if displayplayer and (z == displayplayer) and z.mo and z.mo.valid
						local DIST = FixedHypot(FixedHypot(s.x-z.mo.x, s.y-z.mo.y), s.z-z.mo.z)
						if (DIST < 1400<<16)
						and not ((z.mo.state >= S_PLAY_SUPER_TRANS1) and (z.mo.state <= S_PLAY_SUPER_TRANS6))
						
							local QUAKEPOWER = max(24<<16-(DIST/80), 8<<16)
							if z.mo == i or z.mo == so
								if (z.speed+abs(z.mo.momz) < 35*z.mo.scale) and not z.homing
									QUAKEPOWER = $/2
								end
							else
								QUAKEPOWER = $*3/4
							end
							
							P_StartQuake(QUAKEPOWER, (QUAKEPOWER>18<<16) and 4 or 3)
						end
					end
				end
			end
		end
	end
end)

addHook("MobjSpawn", function(s)
	s.renderflags = RF_FULLBRIGHT|RF_NOCOLORMAPS
	s.fuse,s.tics = 25,40 --We're merely using the S_THOK state, so we need to put some custom properties first.
	s.sprite = SPR_GPLZ
	s.frame = A|FF_TRANS30
	s.blendmode = AST_ADD
	s.dispoffset = 99
end, MT_YUEDTH)

addHook("MobjThinker", function(s) 
	if not s.valid or not s.fuse return end --
	if ((s.frame & FF_FRAMEMASK) < P)
		s.frame = $+1
		local t = s.target
		if t and t.valid --Stop an enemy's generic "pop" sounds.
			S_StopSoundByID(t, sfx_pop)
		end
	end
	if (s.fuse < 18)
		local TRANS = min(FF_TRANS90, 10<<16 - (s.fuse/2)<<16 ) --Fading away!
		s.frame = ($ & ~FF_TRANSMASK)|TRANS 
	end
end, MT_YUEDTH)

COM_AddCommand("SAExplosions", function() 
	SAEXPLOSIONS = (not SAEXPLOSIONS)
	print("SA-explosions are now set to\x82 "+SAEXPLOSIONS+"\x80"+"!")
end, COM_ADMIN)

addHook("NetVars", function(network) --Sync emblem count and SAemblems value.
	SAEXPLOSIONS = network($)
end)