-- Mic Drop --

if srb2p return end

-- These are here so that they work in other functions
local mic
local shockwave

addHook("ThinkFrame", function()
	for player in players.iterate do
		if player.mo.skin ~= "flchan" then
			continue
		end
	
		if not player.exiting then // If you're not exiting the level...
			if not (P_PlayerInPain(player) or (player.playerstate == PST_DEAD)) then // If you're not in pain or not dead...
				if ((player.powers[pw_carry] == CR_NONE) and not (player.pflags == PF_SPINNING)) then // If you're not be carried and not spinning of any kind...
					if not (player.powers[pw_tailsfly]) then // If you're not flying (or in FL Chan's case, swimming)
						if not (player.cmd.buttons & BT_USE) then // If Spin is pressed
							player.spintapready = true
							player.spintapping = false
						elseif player.spintapready then
							player.spintapping = true
							player.spintapready = false
						else
							player.spintapping = false
						end
						
						if player.spintapping and not player.weapondelay then // If the above is true when not delayed...
							if not (player.mo.boomboxspawned == 1) then // If there's not a boombox around...
								mic = P_SpawnMobj(player.mo.x, player.mo.y, player.mo.z + (player.mo.height/2), MT_DROPPEDMIC) -- Spawns the mic itself
								S_StartSound(mic, sfx_brakrl) -- Plays a launching sound
								mic.momz = -30*FRACUNIT -- Drops the mic quickly
								mic.target = player.mo -- Mic Drop can't hurt the user (or "users" if you're playing as Metal & FL)
								if not P_IsObjectOnGround(player.mo) then // If you're not on the ground at the time...
									player.mo.state = S_MICDROP_MIDAIR -- Plays the midair animation
									if (All7Emeralds(emeralds) or All7Emeralds(pw_emeralds)) then
										player.mo.momz = 20*FRACUNIT -- Gives you the boosted extra jump
										player.weapondelay = TICRATE*3 -- Delays the attack even more
									else
										player.mo.momz = 10*FRACUNIT -- Gives you the normal extra jump
										player.weapondelay = TICRATE*2 -- Delays the attack more so that you can't abuse the extra jump
									end
								else // If you are on the ground at the time...
									player.mo.state = S_MICDROP -- Plays the grounded animation
									player.weapondelay = TICRATE*1 -- You'll always be able to attack at a fast rate
								end
								if (player.mo.eflags & MFE_VERTICALFLIP) then // If you're upside down...
									mic.eflags = $1|MFE_VERTICALFLIP -- Flips the mic upside
									mic.flags2 = $1|MF2_OBJECTFLIP
									mic.momz = 30*FRACUNIT
									if not P_IsObjectOnGround(player.mo) then
										if (All7Emeralds(emeralds) or All7Emeralds(pw_emeralds)) then
											player.mo.momz = -20*FRACUNIT
										else
											player.mo.momz = -10*FRACUNIT
										end
									end
								end
							end
						end
					end
				end
			end
		end
		
		-- This piece of code is from SSN Mighty since he's reusable
		if P_IsObjectOnGround(mic) then // If the mic is on the ground...
			for x = mic.x, mic.x + mic.radius, mic.radius
				for y = mic.y, mic.y + mic.radius, mic.radius
					local ss = R_PointInSubsector(x,y)
					for fof in ss.sector.ffloors()
						if (fof.flags & FF_BUSTUP) and (fof.flags & FF_EXISTS) // If a bustable fof exists below the mic...
							EV_CrumbleChain(ss.sector, fof) -- Break that floor
						end
					end
				end
			end
		end
		
		if (mic.state == S_XPLD1) then // If the mic explodes...
			if (All7Emeralds(emeralds) or All7Emeralds(pw_emeralds)) then
				P_Earthquake(mic, player.mo, 400*FRACUNIT) -- Gives the boosted radial damage
				shockwave = P_SpawnMobj(player.mo.x, player.mo.y, player.mo.z, MT_SHOCKWAVE2) -- For later use
			else
				P_Earthquake(mic, player.mo, 200*FRACUNIT) -- Gives the normal radial damage
				shockwave = P_SpawnMobj(player.mo.x, player.mo.y, player.mo.z, MT_SHOCKWAVE1) -- For later use
			end
		end
		
		if ((player.mo.state == S_MICDROP) or (player.mo.state == S_MICDROP_FIN)) then // If you're either one of these two states...
			player.pflags = $|PF_FULLSTASIS -- Stops all control
		end
	end
end)

-- With the extra jump in the way, she can't use shield actives (Tbh, I had to put this in because Persona hadn't been updated to add SF_NOSHIELDABILITY)
addHook("ShieldSpecial", function(player)
	if player.mo.skin == "flchan" then
		return true
	end
end)

-- The following allows the shockwave to kill spikes (I used Honey as a template)
local killspikes = function(thing, tmthing)
	if not (thing and thing.valid and tmthing and tmthing.valid) return end
	if tmthing == shockwave then -- This is what we needed the shockwave object for
		P_KillMobj(thing, tmthing)
		return true
	end
end

addHook("MobjCollide", function(thing, tmthing) killspikes(thing, tmthing) end, MT_SPIKE)
addHook("MobjCollide", function(thing, tmthing) killspikes(thing, tmthing) end, MT_WALLSPIKE)