-- Dummy Ring Bomb (A modified version of "Bomb Throw for Fang" by Simon_T) --

if srb2p return end

-- These are here so that they work in other functions
local autoaim
local monitor

addHook("ThinkFrame", function()
	for player in players.iterate do
		if player.mo.skin ~= "tails" 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 dead...
				if ((player.powers[pw_carry] == CR_NONE) and not (player.pflags == PF_SPINNING)) then // If you're not being carried and not spinning of any kind...
					if not (player.cmd.buttons & BT_CUSTOM1) then // If Custom 1 is pressed...
						player.cust1tapready = true
						player.cust1tapping = false
					elseif player.cust1tapready then
						player.cust1tapping = true
						player.cust1tapready = false
					else
						player.cust1tapping = false
					end
					
					if player.cust1tapping and not (player.weapondelay) then // If the above is true when not delayed...
						monitor = P_SpawnMobj(player.mo.x, player.mo.y, player.mo.z + (player.mo.height/2), MT_DUMMYBOMB1) -- Spawns the monitor for the regular throw
						monitor.target = player.mo -- Monitors can't hurt Tails
						S_StartSound(monitor, sfx_s3k51) -- Plays a throwing sound
						if (player.mo.momz > 0) and not (player.powers[pw_tailsfly]) then // If you're jumping and not flying...
							monitor.momz = (player.mo.momz)+(7*FRACUNIT) -- The monitor gets a height boost
						else // If that's not the case...
							if (player.powers[pw_tailsfly]) then // If you're flying...
								monitor.momz = 0*FRACUNIT -- Drops the monitor
							else // If you aren't flying...
								monitor.momz = 7*FRACUNIT -- Lobs the monitor
							end
						end
						if (player.mo.eflags & MFE_VERTICALFLIP) then // If you're upside down...
							monitor.eflags = $1|MFE_VERTICALFLIP -- Flips the monitor upside down (I don't know if I can do that for the dummy rings due to how they spawn)
							monitor.flags2 = $1|MF2_OBJECTFLIP
							if (player.powers[pw_tailsfly]) then
								monitor.momz = 0*FRACUNIT
							else
								monitor.momz = -7*FRACUNIT
							end
						end
						if not (player.powers[pw_tailsfly]) then // If you're not flying...
							P_InstaThrust(monitor, player.mo.angle, ((player.speed)+(7*FRACUNIT))) -- The monitor is thrown in an arc affected by your speed
							S_StartSound(player.mo, sfx_hyah)
						end
						if (player.powers[pw_tailsfly]) then
							player.weapondelay = TICRATE*1/4 -- You'll always be ale to fire off monitors faster
						else
							if (All7Emeralds(emeralds) or All7Emeralds(pw_emeralds)) then
								player.weapondelay = TICRATE*1 -- You can throw monitors at a slightly faster rate
							else
								player.weapondelay = TICRATE*2 -- You'll have to wait 2 seconds between attacks
							end
						end
						if P_IsObjectOnGround(player.mo) then // If you're on the ground...
							player.mo.state = S_PLAY_THROW -- Plays a throwing animation (I don't know how to control the overlay, so he'll be missing his tails when in this state)
						end
					end
					
					if not (player.cmd.buttons & BT_CUSTOM2) then // If Custom 2 is pressed...
						player.cust2tapready = true
						player.cust2tapping = false
					elseif player.cust2tapready then
						player.cust2tapping = true
						player.cust2tapready = false
					else
						player.cust2tapping = false
					end
					
					if player.cust2tapping and not (player.powers[pw_tailsfly]) then // If the above is true when not flying...
						autoaim = P_LookForEnemies(player, false, true) -- Looks for enemies
					end
					
					if player.cust2tapping and not (player.weapondelay) then
						monitor = P_SpawnMissile(player.mo, autoaim, MT_DUMMYBOMB2) -- Spawns the monitor for the aimed throw
						monitor.target = player.mo
						S_StartSound(player.mo, sfx_hupp)
						S_StartSound(player.mo, sfx_s3k51)
						if (player.mo.eflags & MFE_VERTICALFLIP) then
							monitor.eflags = $1|MFE_VERTICALFLIP
							monitor.flags2 = $1|MF2_OBJECTFLIP
						end
						if (All7Emeralds(emeralds) or All7Emeralds(pw_emeralds)) then
							player.weapondelay = TICRATE*1
						else
							player.weapondelay = TICRATE*2
						end
						if P_IsObjectOnGround(player.mo) then
							player.mo.state = S_PLAY_THROW
						end
					end
				end
			end
		end
		
		-- This piece of code is from SSN Mighty since he's reusable 
		if P_IsObjectOnGround(monitor) then // If the monitor is on the ground...
			for x = monitor.x, monitor.x + monitor.radius, monitor.radius
				for y = monitor.y, monitor.y + monitor.radius, monitor.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 D.R.B...
							EV_CrumbleChain(ss.sector, fof) -- Break that floor
						end
					end
				end
			end
		end
		
		if ((player.mo.state == S_PLAY_THROW) or (player.mo.state == S_PLAY_THROWFIN)) then
			player.pflags = $|PF_FULLSTASIS
		end
	end
end)