-- Death states
freeslot(
	"S_PLAY_HONEY_DIE_FIRE",  	-- fire
	"S_PLAY_HONEY_DIE_ELEC",	-- electricity
	"S_PLAY_HONEY_DIE_NUKE",	--nuke
	"S_PLAY_HONEY_DIE_NUKE_GIB", -- gibs (no, not *those*)
	"MT_HONEY_NUKE_GIB", -- look its just clothing ffs
	"SPR2_ADIE",	-- extra Death sprites
	"SPR2_GIB_",	-- nuked gibs (refer to above)
	--30/12/20, heya, bloops back!
	--today we'll hyper.
	"SPR_HMST",
	"S_HONEY_HYPERSTAR"
)

states[S_PLAY_HONEY_DIE_FIRE] = {SPR_PLAY, SPR2_ADIE, -1, nil, 0, 0, S_NULL}
states[S_PLAY_HONEY_DIE_NUKE] = {SPR_PLAY, SPR2_ADIE, -1, nil, 0, 0, S_NULL}
states[S_PLAY_HONEY_DIE_ELEC] = {SPR_PLAY, SPR2_ADIE, 2, nil, 0, 0, S_PLAY_HONEY_DIE_ELEC}
states[S_PLAY_HONEY_DIE_NUKE_GIB] = {SPR_PLAY, SPR2_GIB_, -1, nil, 0, 0, S_NULL}
states[S_HONEY_HYPERSTAR] = {SPR_HMST, A|FF_ANIMATE, 8, nil, 3, 2, S_HONEY_HYPERSTAR}

mobjinfo[MT_HONEY_NUKE_GIB] = {
	spawnstate = S_NULL,
	spawnhealth = 0,
    deathstate = S_NULL,
    speed = FRACUNIT,
    radius = 2*FRACUNIT,
    height = 2*FRACUNIT,
	flags = MF_SCENERY
}


addHook("ThinkFrame",do
	for p in players.iterate do
		if p.mo and p.mo.valid and p.mo.skin == "honey"
			if p.playerstate != PST_LIVE
				if p.mo.deathtype == 1
					p.mo.state = S_PLAY_HONEY_DIE_FIRE
					p.mo.frame = C
					p.mo.deathtype = 0
				elseif p.mo.deathtype == 2
					p.mo.state = S_PLAY_HONEY_DIE_ELEC
					p.mo.deathtype = 0
				elseif p.mo.deathtype == 3
					p.mo.state = S_PLAY_HONEY_DIE_NUKE
					p.mo.frame = D
					--dont reset it immediately
				end
				local r = p.mo.radius/p.mo.scale
				local xy = (P_RandomRange(r, -r)*FRACUNIT)
				local z = (P_RandomKey(p.mo.height/p.mo.scale)*FRACUNIT)

				if p.mo.state == S_PLAY_HONEY_DIE_FIRE --burned cat, smokey
					P_SpawnMobjFromMobj(p.mo, xy, xy, z, MT_SMOKE)
				elseif p.mo.state == S_PLAY_HONEY_DIE_ELEC --zappy cat, still static
					if p.mo.frame == C --to explain, they both use the same spr2 set, this *would* advance into the fire frame, this prevents so.
						p.mo.state = S_PLAY_HONEY_DIE_ELEC
					end

					if leveltime % 2 == 0
						local zap = P_SpawnMobjFromMobj(p.mo, xy, xy, z, MT_WATERZAP)
						zap.colorized = true
						zap.color = SKINCOLOR_GOLD
					end
				elseif p.mo.state == S_PLAY_HONEY_DIE_NUKE
					if p.mo.deathtype == 3 --only runs once before it gets reset.
						local clothframes = {A, B, B, C, D, E, E, F, F, G, G, H, H, I, I, I, I, I, I, J, J, K, K}
						for i = 1, 23
							local cloth = P_SpawnMobjFromMobj(p.mo, 0, 0, p.mo.height/2, MT_HONEY_NUKE_GIB)
							cloth.fuse = 10*TICRATE
							cloth.color = p.mo.color
							cloth.skin = "honey"
							cloth.state = S_PLAY_HONEY_DIE_NUKE_GIB
							cloth.frame = clothframes[i]
							P_InstaThrust(cloth, FixedAngle(P_RandomRange(1,359)*FRACUNIT), P_RandomRange(1,6)*p.mo.scale)
							P_SetObjectMomZ(cloth, P_RandomRange(3,12)*FRACUNIT, false)
						end
						p.mo.deathtype = 0
					end
				end
			else
				if p.honey.hypermode == 12*TICRATE
				or p.powers[pw_super] == 2
					for i = 0, 11
						local star = P_SpawnMobjFromMobj(p.mo,-(p.mo.radius*2/p.mo.scale)*cos(p.mo.angle+(ANG30*i)), -(p.mo.radius*2/p.mo.scale)*sin(p.mo.angle+(ANG30*i)), p.mo.height/2, MT_HONEY_NUKE_GIB) --general purpose FX
						star.state = S_HONEY_HYPERSTAR
						star.scale = (i % 3 == 0) and p.mo.scale or p.mo.scale/4*3
						star.flags = $ | (MF_NOCLIP|MF_NOCLIPHEIGHT|MF_NOGRAVITY)
						star.angle = ANG30*i
						if gametyperules & GTR_TEAMS
							star.color = p.mo.color
						end
						table.insert(p.honey.hyperstars, i, star)
					end
				end
				if (p.honey.hypermode < 12*TICRATE
				and p.honey.hypermode > 0)
				or p.powers[pw_super] > 1
					--todo, fix candy
					if leveltime % 10 == 0
						local ghosts = p.nohairdye and 1 or 2
						for i = 1, ghosts
							p.honey.hyperghost[i] = P_SpawnGhostMobj(p.mo)
							p.honey.hyperghost[i].fuse = 10
							p.honey.hyperghost[i].destscale = $*3/2
							//p.honey.hyperghost[i].colorized = true
							p.honey.hyperghost[i].dispoffset = i
						end
					end
					//if p.honey.hyperghost[1] and p.honey.hyperghost[1].valid --this should occur during runtime
						//p.honey.hyperghost[1].color = p.superobj.color
					//end
					if p.honey.hyperghost[2] and p.honey.hyperghost[2].valid
					and p.superobj and p.superobj.valid
						p.honey.hyperghost[2].color = p.superobj.color
						p.honey.hyperghost[2].colorized = true
						p.honey.hyperghost[2].sprite2 = $ |FF_SPR2SUPER
					end
					for i = 1, 2
						if p.honey.hyperghost[i] and p.honey.hyperghost[i].valid
							P_TeleportMove(p.honey.hyperghost[i], p.mo.x, p.mo.y, p.mo.z-11*FRACUNIT)
							p.honey.hyperghost[i].angle = p.drawangle
						end
					end
					p.honey.hyperstarangle = $ + 15
					if p.honey.hyperstarangle >= 360
						p.honey.hyperstarangle = 0
					end
					local ang = FixedAngle(p.honey.hyperstarangle*FRACUNIT)
					for i = 0, 11
						if p.honey.hyperstars[i] and p.honey.hyperstars[i].valid
							local destz = p.mo.z + (p.mo.height/2) + (10*sin(p.honey.hyperstars[i].angle))
							if P_MobjFlip(p.mo) == -1
								destz = p.mo.z + (10*sin(p.honey.hyperstars[i].angle)) + p.mo.height - (p.mo.height/2) 
							end
							p.honey.hyperstars[i].angle = $ + ANG1*3
							P_TeleportMove(p.honey.hyperstars[i],p.mo.x + -(p.mo.radius*2/p.mo.scale)*cos(ang+(ANG30*i)), p.mo.y + -(p.mo.radius*2/p.mo.scale)*sin(ang+(ANG30*i)), destz)-- + p.honey.hyperstars[i].momz)
							p.honey.hyperstars[i].fuse = 3
						end
					end
				end
			end
		end
	end
end)



addHook("MobjDeath", function(h, i, s, d) --these arguments can be renamed as needed, though i and s are unused.
    if h and h.valid
    and h.player and h.player.valid
    and h.skin == "honey"
		if d == DMG_FIRE
			h.deathtype = 1
		elseif d == DMG_ELECTRIC
			h.deathtype = 2
		elseif d == DMG_NUKE
			h.deathtype = 3
		end
    end
end, MT_PLAYER)

addHook("MobjThinker", function(cloth)
	if cloth and cloth.valid
		if not P_IsObjectOnGround(cloth)
			cloth.rollangle = $ + FixedAngle(abs(cloth.momz))
		end
	end
end, MT_HONEY_NUKE_GIB)