--BLUE BALL RINGS
--By Callmore and fucked with by Ivo and Eldog, then further fucked by Nimsay

if (ringblueballs_initialized) then return end -- Check if the script has already been run

freeslot("MT_RINGBALL", "sfx_s3k33")

local wait = 70
--wait = network(wait)
local timer = 0

local function RingBallSpecial(mt, mt2)
	--print("FUCKINGSPECIAL")
	if not (mt2.valid and mt2.player and mt2.player.valid) then return true end
	if not mt.pickedup then
		mt.pickedup = {}
	end
	if mt.pickedup[mt2.player] then
		return true
	end
	mt.pickedup[mt2.player] = true
	mt2.player.kartstuff[k_driftboost] = $+7
	S_StartSoundAtVolume(nil, sfx_s3k33, 160, mt2.player)
	--print("FUCKINGSPEED")
	return true
end

addHook("TouchSpecial", RingBallSpecial, MT_RINGBALL)
--print("FUCKINGBALLS")

local function RingBallThink(mo)
    --Thinker for ivo's blue balls
	if not mo.pickedup then return end
    local newFlags = 0
    if not splitscreen then
        if mo.pickedup[displayplayers[0]] then
            newFlags = MF2_DONTDRAW
        end
        mo.flags2 = ($&~MF2_DONTDRAW)|newFlags
    else
        for p, n in displayplayers.iterate do
            if not mo.pickedup[p] then
                newFlags = MFE_DRAWONLYFORP1 << n
            end
            if newFlags then
                local flgs = MFE_DRAWONLYFORP1|MFE_DRAWONLYFORP2|MFE_DRAWONLYFORP3|MFE_DRAWONLYFORP4
                mo.eflags = ($&~flgs)|newFlags
            else
                mo.flags2 = $|MF2_DONTDRAW
            end
        end
    end
	timer = $+1
	--print(timer)
	if timer == wait then
		mo.flags2 = $&~MF2_DONTDRAW
		mo.pickedup = 0
		timer = 0
	end
end

addHook("MobjThinker", RingBallThink, MT_RINGBALL)

rawset(_G, "ringblueballs_initialized", true) -- The script ran, don't run it again