-- its here lmao

freeslot("sfx_sprwrn")

-------------------------------------------------------
-- SPIRAL ITEM HELL CODE
-------------------------------------------------------

local springrain = false
local bananrain = false
local orbirain = false
local sinkrain = false

local STARTTIME = 6*TICRATE + (3*TICRATE/4)

addHook("NetVars", function(net)
	springrain = net(springrain)
	bananrain = net(bananrain)
	orbirain = net(orbirain)
	sinkrain = net(sinkrain)
end)

addHook("LinedefExecute", function(line, mobj, sec)
	if not mobj.valid then return end
	if not mobj.player and not mobj.player.valid then return end
	mobj.player.spiral_funny = 80
	-- playa
	local player = mobj.player
	-- we are not in first. fuck off
	if player.kartstuff[k_position] ~= 1 then return end
	-- lapper
	local reallaps = player.laps + 2
	if reallaps == 4 then
		P_StartQuake(FRACUNIT*8, TICRATE*2)
		springrain = true -- spring rain!
	end
	if reallaps == 8 then
		P_StartQuake(FRACUNIT*8, TICRATE*2)
		bananrain = true -- BANANA
	end
	if reallaps == 16 then
		P_LinedefExecute(2006, mobj, sec)
		P_StartQuake(FRACUNIT*16, TICRATE*2)
		COM_BufInsertText(server, "csay THE WALLS ARE FALLING!")
		S_StartSound(nil, sfx_sprwrn)
		springrain = false -- disable spring rain...
		orbirain = true -- but enable orbi rain!!!
	end
	if reallaps == 20 then
		P_StartQuake(FRACUNIT*32, TICRATE*2)
		COM_BufInsertText(server, "csay THE SINKS ARE FALLING!")
		S_StartSound(nil, sfx_sprwrn)
		sinkrain = true -- we ball
	end
	
end, "SPRCHAOS")

local function RandomFrac(pos)
	local ret = P_SignedRandom()*12
	ret = ret * FRACUNIT
	return pos + ret
end

addHook("PostThinkFrame", function()
	-- reset
	if leveltime == 2 then
		springrain = false
		bananrain = false
		orbirain = false
		sinkrain = false
	end
	
	-- NO
	if not mapheaderinfo[gamemap].spiral_r then return end
	server.pplexiting = 0
	
	-- get our playas
	for player in players.iterate do
		
		-- no.
		if player.spectator then continue end
		-- we dont exist
		if not player.mo and not player.mo.valid then continue end
		-- we already finished. why even bother
		if player.exiting then 
			server.pplexiting = $ + 1
			continue 
		end
		-- only do shit for the podium players, dimbass :fatcat:
		if player.kartstuff[k_position] > 3 + server.pplexiting then continue end
	
		-- originally spring rain was supposed to launch more springs than just gray ones
		-- be grateful i didnt put that in lmao
		-- this doesnt spawn as much because of issues related to them
		if springrain then
			if leveltime%TICRATE == 1
				local spring = P_SpawnMobj(RandomFrac(player.mo.x), RandomFrac(player.mo.y), player.mo.z+FRACUNIT*500, MT_ARIDTOAD) 
				spring.fuse = TICRATE*12
			end
		end
		
		-- oooo banana
		-- bananas die after 2 seconds
		if bananrain then
			if leveltime%10 == 1
				local banana = P_SpawnMobj(RandomFrac(player.mo.x), RandomFrac(player.mo.y), player.mo.z+FRACUNIT*500, MT_BANANA) 
				banana.fuse = TICRATE*4
			end
		end
		
		-- Orbinauts are a special case. They're noisy, annoying and generally stupid
		-- Sounds for orbeez are disabled on this map for a reason. So many can spawn
		if orbirain then
			if leveltime%10 == 1
				local orbi = P_SpawnMobj(RandomFrac(player.mo.x), RandomFrac(player.mo.y), player.mo.z+FRACUNIT*500, MT_ORBINAUT) 
				orbi.fuse = TICRATE*4
			end
		end
		
		-- let that sink in
		-- no fuse shit for sinks. theyre sinks
		if sinkrain then
			if leveltime%10 == 1
				P_SpawnMobj(RandomFrac(player.mo.x), RandomFrac(player.mo.y), player.mo.z+FRACUNIT*500, MT_SINK) 
			end
			
			if leveltime%10 == 1
				local spring = P_SpawnMobj(RandomFrac(player.mo.x), RandomFrac(player.mo.y), player.mo.z+FRACUNIT*500, MT_ARIDTOAD) 
				spring.fuse = TICRATE*4
			end
		end
	
	end
end)

addHook("MobjThinker", function(mobj)
	-- dont activate when not on spiral
	if not mapheaderinfo[gamemap].spiral_r then return end
	S_StopSound(mobj)
end, MT_ORBINAUT)

addHook("MusicChange", function(oldmusic, newmusic, flags, looping)
	if not mapheaderinfo[gamemap].tmmstart then return end
	local muslump = "TMMSTR"
	if encoremode then muslump = "TMMSTE" end
	if oldmusic == muslump and newmusic ~= muslump then
		return muslump
	end
	
	if newmusic == "kstart"
	or newmusic == "estart" then
		return muslump -- You're GONNA hear this fucking announcement.
	end
end)

