-- Kaboom Box --

-- ABOUT: FL Chan's tertiary ability.

if srb2p return end

local kaboombox = nil

addHook("ThinkFrame", function()
	for player in players.iterate do
		if player.mo.skin ~= "flchan" then
			continue
		end
		
		if not player.exiting then
			if not (P_PlayerInPain(player) or (player.playerstate == PST_DEAD)) then
				if ((player.powers[pw_carry] == CR_NONE) and not (player.pflags == PF_SPINNNING)) then
					if not (player.mo.state == S_PLAY_FLOAT 
					or player.mo.state == S_PLAY_FLOAT_RUN 
					or player.mo.state == S_PLAY_DASH 
					or player.powers[pw_tailsfly]) then
						if not (player.cmd.buttons & BT_CUSTOM1) then // Then if Custom 1 isn't pressed...
							player.boomboxspawnready = true
							player.boomboxspawning = false
						elseif player.boomboxspawnready then
							player.boomboxspawning = true
							player.boomboxspawnready = false
						else
							player.boomboxspawning = false
						end
						
						if player.boomboxspawning then
							if P_IsObjectOnGround(player.mo) then
								if (player.speed == 0) then // Then if the player ain't going anywhere...
									if not ((player.mo.boomboxspawned == 1) or (player.mo.flchanhugging == 1)) then
										if not player.weapondelay then // Then if there's no cooldown active...
											player.mo.state = S_KABOOMBOX_SETUP -- Plays the set up animation
											player.mo.boomboxspawned = 1 -- Registers the Kaboom Box as spawned so that you can't spawn another one or use Mic Drop until it's gone
											S_StartSound(player.mo, sfx_nbmper) -- Plays a bumper sound effect
											kaboombox = P_SpawnMobj(player.mo.x + FixedMul(50*FRACUNIT, cos(player.mo.angle)), player.mo.y + FixedMul(50*FRACUNIT, sin(player.mo.angle)), player.mo.z, MT_KABOOMBOX) -- Spawns the Kaboom Box relative to the player's angle
											player.weapondelay = TICRATE*1
											if (player.mo.eflags & MFE_VERTICALFLIP) then
												kaboombox.eflags = $1|MFE_VERTICALFLIP
												kaboombox.flags2 = $1|MF2_OBJECTFLIP
											end
										end
									end
								end
							end
						end
						
						if (player.mo.boomboxspawned == 1) and player.spintapping then // If the aboe is true and you press spin... (I'm surprised this works here)
							if not player.weapondelay then
								player.mo.state = S_KABOOMBOX_ACTIVATE -- Plays the activation animation
								player.weapondelay = TICRATE*2 -- Cooldown for both spawning another one and Mic Drop
								if (All7Emeralds(emeralds)) then
									P_NukeEnemies(kaboombox, player.mo, 1600*FRACUNIT) -- Makes an even larger sound blast
								else
									P_NukeEnemies(kaboombox, player.mo, 800*FRACUNIT) -- Makes a large sound blast
								end
							end
						end
						
						if not (kaboombox.valid) or (kaboombox.state == S_AUTODETONATE) then // If the Kaboom Box isn't around anymore or if it's automatically detonated...
							player.mo.boomboxspawned = 0 -- Allows us to spawn another one, or use Mic Drop again
						end
					end
				end
			end
		end
		
		if (All7Emeralds(emeralds)) then
			mobjinfo[MT_KABOOMBOX].mass = 32*FRACUNIT -- Launches you like a red spring
		else
			mobjinfo[MT_KABOOMBOX].mass = 20*FRACUNIT -- Launches you like a yellow spring
		end
		
		if (player.mo.state == S_KABOOMBOX_SETUP 
		or player.mo.state == S_KABOOMBOX_ACTIVATE 
		or player.mo.state == S_KABOOMBOX_FIN) then // If the player is setting up, activating, or finished with activaing the Kaboom Box...
			player.pflags = $|PF_FULLSTASIS
		end
		
		if (kaboombox.state == S_BOOMBOX_EXPL1 
		or kaboombox.state == S_BOOMBOX_EXPL2 
		or kaboombox.state == S_BOOMBOX_EXPL3) then // If the Kaboom Box explodes...
			if P_IsObjectOnGround(kaboombox) then
				for x = kaboombox.x, kaboombox.x + kaboombox.radius, kaboombox.radius
					for y = kaboombox.y, kaboombox.y + kaboombox.radius, kaboombox.radius
						local ss = R_PointInSubsector(x,y)
						for fof in ss.sector.ffloors()
							if (fof.flags & FF_BUSTUP) and (fof.flags & FF_EXISTS)
								EV_CrumbleChain(ss.sector, fof)
							end
						end
					end
				end
			end
		end
	end
end)

-- ABOUT: Fail message support for Henry Stickmin.
addHook("MobjDeath", function(mo, inflictor, source, damagetype)
	if mo.skin == "henry"
	and mo.valid and not (mo.player.powers[pw_carry] == CR_NIGHTSMODE)
		local player = mo.player
		if inflictor and inflictor.type == MT_KABOOMBOX
			player.failwhy = "Seems like whatever company made that"
			player.failwhy2 = "boombox took the name too literally."
			player.failwhy3 = nil
			player.failwhy4 = nil
			player.failwhy5 = nil
			player.failwhy6 = nil
			player.failwhy7 = nil
			player.failwhy8 = nil
			player.failwhy9 = nil
			player.failwhy10 = nil
		end
	end
end, MT_PLAYER)