freeslot("MT_SUPERMISSILE", "SPR_HOMI", "S_SUPERMISSILE", "S_SUPERMISSILE2", "S_SUPERMISSILE3", "S_SUPERMISSILE4", "S_SUPERMISSILE5", "S_SUPERMISSILE6", "S_SUPERMISSILE7", "S_SUPERMISSILE8", "S_SUPERMISSILE9", "S_SUPERMISSILE10", "S_SUPERMISSILE11", "S_SUPERMISSILE12", "S_SUPERMISSILE13", "S_SUPERMISSILE14")

// Finds a player other than the object's target that's nearby, and sets it to the object's tracer
// Var1 = distance limit
// Var2 = if set, get the nearest player - if not, just take the first one in range
function A_FindOtherPlayer(mo, distlimit, nearest)
    local distcheck = distlimit
    for p in players.iterate do
        if not (p.mo and p.mo.valid and p.mo.health) then continue end
        if p.mo == mo.target then continue end
		if p.mo.ctfteam == 1 and mo.target.ctfteam == 1 then continue end
		if p.mo.ctfteam == 2 and mo.target.ctfteam == 2 then continue end
        local newdist = P_AproxDistance(P_AproxDistance(p.mo.x-mo.x, p.mo.y-mo.y), p.mo.z-mo.z)
        if newdist <= distcheck then
            mo.tracer = p.mo
            distcheck = newdist
            if not nearest then return end
        end
    end
end

// Player.rtimer = reaction timer
// Player.supermissile = Homing missile timer
addHook("ThinkFrame", function()
	for player in players.iterate do
		
		if not player.supermissile then
			player.supermissile = 0
		end
		
		player.supermissile = $1 + 1
		
		
		if (player.cmd.buttons & BT_ATTACK)
		and player.currentweapon == WEP_BOUNCE
		and player.supermissile >= 75
		and player.rings >= 0 
			P_SpawnPlayerMissile(player.mo, MT_SUPERMISSILE, MF2_BOUNCERING)
			player.supermissile = 1
			player.weapondelay = TICRATE*2
		end

end
end)

// addHook("ShouldDamage", function(target, inflictor, source, damage)
//	if  not (target.player.powers[pw_invulnerability] or target.player.powers[pw_flashing])
//		
//		if (target.player and G_GametypeHasTeams())
//			if (target.player.ctfteam == source.player.ctfteam)
//				return false
//			end
//		end
//		
//		if (inflictor and inflictor.type == MT_SUPERMISSILE)
//			print(source.player.name.."'s homing ring hit "..target.player.name..".")
//			end
//			P_PlayRinglossSound(target.player.mo)
//	//	end
//	end
// end, MT_PLAYER)
addHook("ShouldDamage", function(target, inflictor, source, damage)
			if target.message == nil
			   target.message = 0
			end	
	
			if target.message == true then			
			print(source.player.name.."'s homing ring decreased "..target.player.name.." 10 rings.")
			target.player.rings = $1 - 5
			end
			
	if target.player.powers[pw_super] and not (target.player.powers[pw_invulnerability] or target.player.powers[pw_flashing])
	
		
		if (target.player and G_GametypeHasTeams())
			if (target.player.ctfteam == source.player.ctfteam)
				return false
			end
		end
		if (inflictor and inflictor.type == MT_SUPERMISSILE)
		    target.message = true
		end
		P_PlayRinglossSound(target.player.mo)
	end
end, MT_PLAYER)