-- Kaboom Box

-- ABOUT: FL Chan's tertiary ability.

local kaboombox = nil

if srb2p return end

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
										end
									end
								end
							end
						end
						
						if (player.mo.boomboxspawned == 1) and player.spintapping // If the above is true and you press spin... (I'm surprised this works here)
							if not player.weapondelay
								P_NukeEnemies(kaboombox, player.mo, 1*FRACUNIT) -- Let's send out a quick signal
								player.mo.state = S_KABOOMBOX_ACTIVATE -- Plays the activation animation
								player.weapondelay = TICRATE*2 -- Cooldown for both spawning another one and Mic Drop
							end
						end
					end
				end
			end
		end
				
		if (player.mo.state == S_KABOOMBOX_SETUP 
		or player.mo.state == S_KABOOMBOX_ACTIVATE 
		or player.mo.state == S_KABOOMBOX_FIN) // If the player is setting up, activating, or finished with activaing the Kaboom Box...
			player.pflags = $|PF_FULLSTASIS
		end
	end
end)

-- NOTE: Kaboom Box internal functions
addHook("MobjThinker", function(kaboombox)
	-- Defines the player
	local p = players[0]
	local mo = p.mo
	-- Flipping
	if (mo.eflags & MFE_VERTICALFLIP)
		kaboombox.eflags = $1|MFE_VERTICALFLIP
		kaboombox.flags2 = $1|MF2_OBJECTFLIP
	end
	-- "Spring" functionality
	if mo.valid and mo.flchanupgraded and kaboombox.valid
		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
	-- Exploding
	if kaboombox.state == S_BOOMBOX_EXPL1
		if mo.flchanupgraded
			P_NukeEnemies(kaboombox, mo, 1600*FRACUNIT) -- Makes an even larger sound blast
		else
			P_NukeEnemies(kaboombox, mo, 800*FRACUNIT) -- Makes a large sound blast
		end
	end
	-- Floor breaking
	if kaboombox.state == S_BOOMBOX_EXPL1 
	or kaboombox.state == S_BOOMBOX_EXPL2 
	or kaboombox.state == S_BOOMBOX_EXPL3 // If the Kaboom Box explodes...
		if P_IsObjectOnGround(kaboombox)
			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
	-- Despawned
	if kaboombox.state == S_AUTODETONATE // If the Kaboom Box isn't around anymore or if it's automatically detonated...
		p.mo.boomboxspawned = 0 -- Allows us to spawn another one, or use Mic Drop again
	end
end, MT_KABOOMBOX)

addHook("MobjRemoved", function(kaboombox)
	-- Defines the player
	local p = players[0]
	local mo = p.mo
	-- Despawns the Kaboom Box
	mo.boomboxspawned = 0
end, MT_KABOOMBOX)

-- 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)