freeslot("sfx_glasb1")
sfxinfo[sfx_glasb1].caption = "glass breaking"
freeslot("sfx_glasb2")
sfxinfo[sfx_glasb2].caption = "glass breaking"
freeslot("sfx_glasc1")
sfxinfo[sfx_glasc1].caption = "glass clink"
freeslot("sfx_glasc2")
sfxinfo[sfx_glasc2].caption = "glass clink"
freeslot("sfx_glasc3")
sfxinfo[sfx_glasc3].caption = "glass clink"
freeslot("sfx_glasc4")
sfxinfo[sfx_glasc4].caption = "glass clink"

rawset(_G, "GLASSSONIC", {})

GLASSSONIC.glass_soundlist = {
	[0] = sfx_glasb1,
	[1] = sfx_glasb2,
	[2] = sfx_glasc1,
	[3] = sfx_glasc2,
	[4] = sfx_glasc3,
	[5] = sfx_glasc4,
}

local glass_soundlist = GLASSSONIC.glass_soundlist

local GLASS_SKIN = "sonic"
	
addHook("PlayerSpawn", function(p)
	if p and p.valid and p.mo and p.mo.valid then
		if p.mo.skin == GLASS_SKIN and SONICCURSES.curses[0] == 1 then
			p.dieimpact = false
			p.gibclean = true
			p.gibnum = 0
			p.explode = true
			p.safeframes = 0
		end
	end
end)

addHook("PostThinkFrame", function()
	for player in players.iterate do
		if player.mo and player.mo.valid
		and player.mo.skin == GLASS_SKIN and SONICCURSES.curses[0] == 1 then
			player.mo.frame = $ | FF_TRANS30
		end
	end
end)

local function toofast(mobj)
	local player = mobj.player
	
	if player and player.valid and SONICCURSES.curses[0] == 1 then
		if mobj.skin == GLASS_SKIN and player.speed > 28*FRACUNIT and player.explode == true then
			if not player.powers[pw_invulnerability] 
			and not player.powers[pw_flashing] then
				P_KillMobj(player.mo)
			end
		end
	end
end

addHook("PlayerThink", function(player)
	if player.mo.valid and player.mo.skin == GLASS_SKIN and SONICCURSES.curses[0] == 1 then
		if P_IsObjectOnGround(player.mo) and player.dieimpact == true and player.explode == true 
		and not player.safeframes then
			if not player.powers[pw_invulnerability] 
			and not player.powers[pw_flashing] then
				P_KillMobj(player.mo)
			end
		end
		
		if not player.safeframes
		and (player.mo.eflags & MFE_JUSTHITFLOOR) 
		and player.mo.lastmomz_glass < -20*FU then
			if not player.powers[pw_invulnerability] 
			and not player.powers[pw_flashing] then
				player.dieimpact = true
			end
		end

		if P_IsObjectInGoop(player.mo) or (player.mo.eflags & MFE_UNDERWATER) then
			player.mo.lastmomz_glass = 0
			player.safeframes = min($ + 1, 10)
		end
	
		if P_IsObjectOnGround(player.mo) and player.safeframes then
			player.safeframes = min($ + 1, 2)
		end
		
		if player.safeframes then
			player.safeframes = max(0, $ - 1)
		end
		
		player.mo.lastmomz_glass = player.mo.momz
	end
end)

local function explodebro(pmo)
	local p = pmo.player
  if SONICCURSES.curses[0] == 1
	if pmo and pmo.valid 
	and p and p.valid then
		p.gibnum = 0
		p.gibclean = false
		if p.explode == true then
			pmo.momx = 0
			pmo.momy = 0
			pmo.momz = 0
			for i=1,7 do
				local glassgib = P_SpawnMobj(p.mo.x, p.mo.y, p.mo.z, MT_GLASSGIB)
				glassgib.target = pmo
				
				-- All of these IF statements could just be a table, but keeping it for legacy sake.
				if p.gibnum == 0 then
					S_StartSound(glassgib, glass_soundlist[P_RandomRange(0, 1)]) 
					glassgib.frame = FF_TRANS30|A
				end
				
				if p.gibnum == 1 then
					glassgib.frame = FF_TRANS30|B
				end
				
				if p.gibnum == 2 then
					glassgib.frame = FF_TRANS30|C
				end
				
				if p.gibnum == 3 then
					glassgib.frame = FF_TRANS30|D
				end
				
				if p.gibnum == 4 then
					glassgib.frame = FF_TRANS30|E
				end
				
				if p.gibnum == 5 then
					glassgib.frame = FF_TRANS30|F
				end
				
				if p.gibnum == 6 then
					glassgib.frame = FF_TRANS30|G
				end
				
				glassgib.color = p.mo.color -- set the gibs color the same as the player object
				
				p.gibnum = p.gibnum + 1
			end
			p.explode = false
		end
	end
	return true
  end
end

local function diebro(mobj)
 if SONICCURSES.curses[0] == 1 then
	if mobj.skin == GLASS_SKIN then
		local player = mobj.player
		
		if player and player.valid then
			if not player.powers[pw_invulnerability] 
			and not player.powers[pw_flashing] then
				P_KillMobj(mobj)
			end
		end
		
		return true
	end
 end
end

addHook("MobjMoveBlocked", toofast, MT_PLAYER)
addHook("MobjDeath", explodebro, MT_PLAYER)
addHook("MobjDamage", diebro, MT_PLAYER)