--BLUE BALLS
--By Callmore and fucked with by Ivo and Eldog

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

freeslot("MT_IVOBALL", "sfx_ivobal")

local function BlueBallSpecial(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_ivobal, 160, mt2.player)
	--print("FUCKINGSPEED")
	return true
end

addHook("TouchSpecial", BlueBallSpecial, MT_IVOBALL)
--print("FUCKINGBALLS")

local function BlueBallThink(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
end

addHook("MobjThinker", BlueBallThink, MT_IVOBALL)

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