-- Dummy Ring Bomb --

-- ABOUT: An edited version of "Bomb Throw For Fang" by Simon_T.

if srb2p return end

-- NOTE: SRB2 is whiny about these, but they're here so that they'll work in other functions.
local monitor
local autoaim

addHook("ThinkFrame", function()
	for player in players.iterate do
		if player.mo.skin ~= "tails" then
			continue
		end
		
		if not (player.exiting) then // If the player isn't exiting the level...
			if not (P_PlayerInPain(player) or (player.playerstate == PST_DEAD)) then // Then, if the player isn't in pain or dead...
				if ((player.powers[pw_carry] == CR_NONE) and not (player.pflags == PF_SPINNING)) then // Then if the player isn't being carried or spinning...
					if not (player.cmd.buttons & BT_CUSTOM1) then // Then if Custom 1 isn't pressed...
						player.cust1tapready = true -- The following makes it act as a singular button press
						player.cust1tapping = false
					elseif player.cust1tapready then
						player.cust1tapping = true
						player.cust1tapready = false
					else
						player.cust1tapping = false
					end
					
					-- NOTE: Conditions in which regular throws/carpet bombs will be disabled.
					if (player.rings < 2) and (gametype and GT_MATCH) then
						player.cust1tapping = false
					end
					if (player.rings < 2) and (gametype and GT_TEAMMATCH) then
						player.cust1tapping = false
					end
					if (player.rings < 2) and (gametype and GT_CTF) then
						player.cust1tapping = false
					end
					if (player.rings < 2) and (gametype and GT_TAG) then
						player.cust1tapping = false
					end
					if (gametype and GT_HIDEANDSEEK) then
						player.cust1tappping = false
					end
					
					if player.cust1tapping and not player.weapondelay then // If the above function is true while the cooldown isn't active...
						monitor = P_SpawnMobj(player.mo.x, player.mo.y, player.mo.z + (player.mo.height/2), MT_DUMMYBOMB1) -- Spawns the monitor for regular throws/carpet bombs
						monitor.target = player.mo -- Monitors will not hurt the user
						S_StartSound(monitor, sfx_s3k51) -- Plays a throwing sound
						if (player.mo.momz > 0 and not player.powers[pw_tailsfly]) then // If you're moving vertically, but not flying...
							monitor.momz = (player.mo.momz)+(7*FRACUNIT) -- Launches the monitor based on your vertical momentum
						else // Otherwise...
							if (player.powers[pw_tailsfly]) then // If the player is flying...
								monitor.momz = 0*FRACUNIT -- The monitor will be dropped
							else
								monitor.momz = 7*FRACUNIT -- The monitor will be launched upwards
							end
						end
						if (player.mo.eflags & MFE_VERTICALFLIP) then // If the player is upside down...
							monitor.eflags = $1|MFE_VERTICALFLIP -- Considers it flipped
							monitor.flags2 = $1|MF2_OBJECTFLIP -- Visually flips it upside down
							if (player.powers[pw_tailsfly]) then
								monitor.momz = 0*FRACUNIT
							else
								monitor.momz = -7*FRACUNIT -- Launches it in the opposite direction
							end
						end
						if not (player.powers[pw_tailsfly]) then // If the player isn't flying...
							P_InstaThrust(monitor, player.mo.angle, ((player.speed)+(7*FRACUNIT))) -- Throws the monitor based on your angle and speed
							S_StartSound(player.mo, sfx_hyah) -- Plays the grunt that Tails makes for regular throws
						end
						if (player.powers[pw_tailsfly]) then
							player.weapondelay = TICRATE*1/4 -- You'll always be able to throw carpet bombs faster
						else
							if (All7Emeralds(emeralds)) then
								player.weapondelay = TICRATE*1 -- Shortens the firing rate
							else
								player.weapondelay = TICRATE*2 -- Gives the normal firing rate
							end
						end
						if (All7Emeralds(emeralds)) then
							mobjinfo[MT_DUMMYBOMB1].reactiontime = -1 -- One ring will be spent for each throw
						else
							mobjinfo[MT_DUMMYBOMB1].reactiontime = -2 -- Two rings will be spent for each throw
						end
						if P_IsObjectOnGround(player.mo) then // If the player is on the ground at the time...
							player.mo.state = S_PLAY_THROW -- Gives Tails a throwing animation
						end
					end
					
					if not (player.cmd.buttons & BT_CUSTOM2) then // If Custom 2 isn't pressed...
						player.cust2tapready = true
						player.cust2tapping = false
					elseif player.cust2tapready then
						player.cust2tapping = true
						player.cust2tapready = false
					else
						player.cust2tapping = false
					end
					
					-- NOTE: Conditions in which snipe throws will be disabled.
					if player.rings < 4 and (gametype and GT_MATCH) then
						player.cust2tapping = false
					end
					if player.rings < 4 and (gametype and GT_TEAMMATCH) then
						player.cust2tapping = false
					end
					if player.rings < 4 and (gametype and GT_CTF) then
						player.cust2tapping = false
					end
					if player.rings < 4 and (gametype and GT_TAG) then
						player.cust2tapping = false
					end
					if (gametype and GT_HIDEANDSEEK) then
						player.cust2tappping = false
					end
					
					if player.cust2tapping and not player.powers[pw_tailsfly] then // If the above is true while the player isn't flying...
						autoaim = P_LookForEnemies(player, false, true) -- Looks for enemies/monitors
					end
					
					if player.cust2tapping and not (player.weapondelay) then
						monitor = P_SpawnMissile(player.mo, autoaim, MT_DUMMYBOMB2) -- Spawns the monitor for snipe throws
						monitor.target = player.mo
						S_StartSound(player.mo, sfx_hupp) -- Plays the grunt that Tails makes for snipe throws
						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)) then
							player.weapondelay = TICRATE*1
							mobjinfo[MT_DUMMYBOMB2].reactiontime = -2 -- Two rings will be spent for each throw
						else
							player.weapondelay = TICRATE*2
							mobjinfo[MT_DUMMYBOMB2].reactiontime = -4 -- Four rings will be spent for each throw
						end
						if P_IsObjectOnGround(player.mo) then
							player.mo.state = S_PLAY_THROW
						end
					end
				end
			end
		end
		
		-- NOTE: This part of the code was taken 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 -- The following will set up variables for floor breaking
				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 breakable floor exists where the monitor lands...
							EV_CrumbleChain(ss.sector, fof) -- Breaks that floor
						end
					end
				end
			end
		end
		
		if (player.mo.state == S_PLAY_THROW or player.mo.state == S_PLAY_THROWFIN) then // If the throwing animation is playing...
			player.pflags = $|PF_FULLSTASIS -- Stabilizes the player
		end
	end
end)