freeslot("sfx_yuedth", "MT_YUEDTH")

--BOOM! You're ded. Much more satisfying than the classic pop!

addHook("MobjDeath", function(s, i, so)
	if s.valid and (s.flags & MF_ENEMY) and not (s.flags & MF_BOSS)
		if not ((s.type > MT_BUGGLE) and (s.type < MT_SPINBOBERT_FIRE2))
		and not ((s.type > MT_COIN) and (s.type <= MT_SHLEEP)) --Hey, these are NOT robots!
		and not s.spawnedyuexplosion --Makes the mod stackable.
		s.spawnedyuexplosion = true
		s.flags2 = $|MF2_DONTDRAW
		
		local sh = P_SpawnMobjFromMobj(s, 0, 0, 0, MT_THOK) --This invisible object makes the sound for us.
		sh.tics = 50
		sh.fuse = 50
		sh.flags2 = $|MF2_DONTDRAW
		S_StartSound(sh, sfx_yuedth)
		
		local ghs = P_SpawnMobjFromMobj(s, 0, 0, s.height/3, MT_YUEDTH)
		local height = s.height/32 --Bigger explosion the bigger you are!
		if height < s.scale
			height = s.scale
		end
		
			ghs.sprite = SPR_GPLZ
			ghs.frame = A
			ghs.blendmode = AST_ADD
			ghs.scale = height
			ghs.scalespeed = $*2
			ghs.destscale = $*99
			ghs.target = s
			ghs.fuse = 25
			ghs.tics = 40
			if i and i.player and i.player == displayplayer and i.skin == "adventuresonic" --Ehhh, fine then
			P_YuQuake(i.player, 10*FRACUNIT, 3)
			end
		
		end
	end
end)

addHook("MobjThinker", function(s)
	if s.valid
		if not s.fuse --This shouldn't happen...
			s.fuse = 25
			return
		else
			if ((s.frame & ~FF_FULLBRIGHT & ~FF_TRANSMASK) < P) --I really couldn't just use a single custom state?
			s.frame = ($+1)|FF_FULLBRIGHT
			end
			if (s.fuse < 18) --Fade out!
			s.frame = $|(TR_TRANS90-((s.fuse/2)*FRACUNIT))
			else
			s.frame = $|TR_TRANS30
			end
		end
		if s.valid and s.target and s.target.valid --Stop popping sounds.
			S_StopSoundByID(s.target, sfx_pop)
			return
		end
	end
end, MT_YUEDTH)