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

--BLUE BALLS
--By Callmore and fucked with by Ivo
--refucked by callmore and ang
freeslot("MT_IVOBALL", "MT_PATROLIVOBALL", "MT_AIRIVOBALL", "sfx_ivobal")

rawset(_G, "blueballmode", CV_RegisterVar({
	name = "blueballmode",
	defaultvalue = "Respawn",
	flags = CV_NETVAR,
	possiblevalue = {Off = 0, Once = 1, Respawn = 2}
}))

--0 = Delete all balls
--1 = Balls can be used once
--2 = Balls will repsawn after PICKED_UP_TICS

--Try not to change this mid-map, you bing.

--This is a constant that defines how long a blue ball should disapear for
-- It is in ticrate, one tic is equal to 1/35th of a second

local PICKED_UP_TICS = TICRATE*10

local function BlueBallSpecial(mt, mt2)
	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] = PICKED_UP_TICS
	mt2.player.kartstuff[k_driftboost] = $+6
	S_StartSoundAtVolume(nil, sfx_ivobal, 160, mt2.player)
	--print("FUCKINGSPEED")
	return true
end

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

local function BlueBallThink(mo)
    --Thinker for ivo's blue balls
	if not(blueballmode.value) then --no fun allowed
		P_RemoveMobj(mo)
		return
	end
	if not mo.pickedup then return end
    local newFlags = 0
	if blueballmode.value == 2
		for i, k in pairs(mo.pickedup) do
			if mo.pickedup[i] then
				mo.pickedup[i] = $-1
			end
		end
	end
    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)
addHook("MobjThinker", BlueBallThink, MT_PATROLIVOBALL)
addHook("MobjThinker", BlueBallThink, MT_AIRIVOBALL)

local function mmSawbladeThink(mo)
	local xdir = sin(mo.angle - ANGLE_90)
	local ydir = cos(mo.angle + ANGLE_90)
	
	local didMove
	if mo.currentmovement then
		didMove = P_TryMove(mo, mo.x + (xdir * mo.info.speed), mo.y + (ydir * mo.info.speed))
	else
		didMove = P_TryMove(mo, mo.x - (xdir * mo.info.speed), mo.y - (ydir * mo.info.speed))
	end
	if not didMove then
		--print("Flipping sawblade")
		--mo.currentmovement = not $
		mo.angle = $+ANGLE_180
	end

end

addHook("MobjThinker", mmSawbladeThink, MT_PATROLIVOBALL)


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