/*
local clanrunning = 0 //has the round started?? run respective logic.
local clanstart = 0 //because i suck at coding and all my shit is segmented.
local fuckfuckfuck = 0 //spectator count. not used but its nice to have anyways incase it is needed.
local livelovered = 0 //red team player count.
local liveloveblue = 0 //blue team player count.
local bigboobies = 0 //should we force spec the player??
*/

local mt_stupidfuckingtable = { //to look up item pickups we should delete cause they cant just hAVE THEIR OWN FUCKING FLAG FUCK FUCK
	[MT_BOUNCERING] = true,
	[MT_RAILRING] = true,
	[MT_INFINITYRING] = true,
	[MT_AUTOMATICRING] = true,
	[MT_EXPLOSIONRING] = true,
	[MT_SCATTERRING] = true,
	[MT_GRENADERING] = true,
	[MT_BOUNCEPICKUP] = true,
	[MT_RAILPICKUP] = true,
	[MT_AUTOPICKUP] = true,
	[MT_EXPLODEPICKUP] = true,
	[MT_SCATTERPICKUP] = true,
	[MT_GRENADEPICKUP] = true,
	[MT_COIN] = true,
	[MT_FLINGCOIN] = true,
	[MT_FIREFLOWER] = true,
	[MT_RING] = true,
	[MT_FLINGRING] = true,
	[MT_REDTEAMRING] = true,
	[MT_BLUETEAMRING] = true,
}

/*
todo:
FFA Clan???
CTF Clan?????? probably not lol.
listen out for feedback
*/

local assasindonkey = CV_RegisterVar({
	name = "clanplayers",
	defaultvalue = 2,
	PossibleValue = {MIN = 2, MAX = 32},
	flags = CV_NETVAR,
})

addHook("PreThinkFrame", function()
	if gametype ~= GT_CLAN then return end
	if not server.clanrunning then
		for ihatemychudlife in players.iterate do
			if ihatemychudlife.mo and not ihatemychudlife.spectator then
				ihatemychudlife.powers[pw_flashing] = 106
			end
		end
	end
	if leveltime < 15 * TICRATE then
		server.clanstart = 0
		server.clanrunning = 0
	end
	if leveltime == 12 * TICRATE then
		chatprint("CLAN ARENA IS STARTING IN 3...")
	end
	if leveltime == 13 * TICRATE then
		chatprint("CLAN ARENA IS STARTING IN 2...")
	end
	if leveltime == 14 * TICRATE then
		chatprint("CLAN ARENA IS STARTING IN 1...")
	end
	if leveltime == 526 and not server.clanrunning then //15 seconds with 1 tic buffer
		chatprint("\x85" .. "you guys are fucking stupid :P, clan arena will immediately begin when both teams have at least 1 player.")
	end
	if leveltime >= 15 * TICRATE and not server.clanrunning then
		fuckfuckfuck = 0 //reset variables in case of waiting for players
		livelovered = 0
		liveloveblue = 0
		for iwantboobiesheldinmyface in players.iterate do //wait for the documentary to come out where the person making this thinks i shut myself out from society while making this
			if iwantboobiesheldinmyface.spectator then
				fuckfuckfuck = $ + 1
				iwantboobiesheldinmyface.bigboobies = 1
			elseif iwantboobiesheldinmyface.ctfteam == 1 then
				livelovered = $ + 1
				iwantboobiesheldinmyface.bigboobies = 0
			elseif iwantboobiesheldinmyface.ctfteam == 2 then
				liveloveblue = $ + 1
				iwantboobiesheldinmyface.bigboobies = 0
			end
		end
		if livelovered >= 1 and liveloveblue >= 1 and (livelovered + liveloveblue >= assasindonkey.value) then
			server.clanstart = 1
			chatprint("\x83" .. "CLAN ARENA IS STARTING NOW")
		end
	end
	if server.clanstart and leveltime >= 15 * TICRATE then
		server.clanstart = 0
		server.clanrunning = 1
		for mo in mobjs.iterate() do
			if mo.player then continue end
			if mo.flags & MF_SCENERY then continue end
			if mo.flags & MF_NOTHINK then continue end
			if mt_stupidfuckingtable[mo.type] == true then
				mo.flags2 = $|MF2_DONTRESPAWN
				P_RemoveMobj(mo)
				continue
			end
			if mo.flags & MF_MONITOR then
				P_RemoveMobj(mo)
				continue
			end
		end
		for iwantboobiesheldinmyface in players.iterate do
			iwantboobiesheldinmyface.powers[pw_shield] = SH_FORCE|1
			P_SpawnShieldOrb(iwantboobiesheldinmyface)
			iwantboobiesheldinmyface.powers[pw_flashing] = 0
			iwantboobiesheldinmyface.powers[pw_invulnerability] = 0
			iwantboobiesheldinmyface.powers[pw_sneakers] = 0
			iwantboobiesheldinmyface.rings = 200
			iwantboobiesheldinmyface.ringweapons = 63
			iwantboobiesheldinmyface.powers[pw_infinityring] = 800
			iwantboobiesheldinmyface.powers[pw_automaticring] = 200
			iwantboobiesheldinmyface.powers[pw_bouncering] = 100
			iwantboobiesheldinmyface.powers[pw_scatterring] = 50
			iwantboobiesheldinmyface.powers[pw_grenadering] = 100
			iwantboobiesheldinmyface.powers[pw_railring] = 50
			iwantboobiesheldinmyface.powers[pw_explosionring] = 50
		end
	end
	if server.clanrunning and leveltime >= 15 * TICRATE then
		fuckfuckfuck = 0 //reset variables so we can check when a team is EXTERMINATED
		livelovered = 0
		liveloveblue = 0
		for iwantboobiesheldinmyface in players.iterate do
			if iwantboobiesheldinmyface.spectator or iwantboobiesheldinmyface.playerstate == PST_DEAD then
				if iwantboobiesheldinmyface.playerstate == PST_DEAD and not iwantboobiesheldinmyface.spectator then
					chatprintf(iwantboobiesheldinmyface, "you died lmao, you can still chat if you want, ghost all you want i dont give a shit")
				end
				iwantboobiesheldinmyface.bigboobies = 1
			end
			if iwantboobiesheldinmyface.bigboobies == 1 and not iwantboobiesheldinmyface.spectator then
				iwantboobiesheldinmyface.ctfteam = 0
				iwantboobiesheldinmyface.spectator = true
			end
			if iwantboobiesheldinmyface.bigboobies == 1 and iwantboobiesheldinmyface.spectator then
				iwantboobiesheldinmyface.cmd.buttons = $ & ~BT_ATTACK
			end
			if iwantboobiesheldinmyface.spectator then //now that spectators are handled, recount the players.
				fuckfuckfuck = $ + 1
				iwantboobiesheldinmyface.bigboobies = 1
			elseif iwantboobiesheldinmyface.ctfteam == 1 then
				livelovered = $ + 1
				iwantboobiesheldinmyface.bigboobies = 0
			elseif iwantboobiesheldinmyface.ctfteam == 2 then
				liveloveblue = $ + 1
				iwantboobiesheldinmyface.bigboobies = 0
			end
		end
		if liveloveblue == 0 then
			G_ExitLevel()
			redscore = $ + 2500
			chatprint("\x85" .. "RED TEAM HAS WON THE MATCH.")
			chatprint("\x8B" .. "Thanks for playing the Clan Arena Beta!")
			server.clanstart = 0
			server.clanrunning = 0
		end
		if livelovered == 0 then
			G_ExitLevel()
			bluescore = $ + 2500
			chatprint("\x84" .. "BLUE TEAM HAS WON THE MATCH.")
			chatprint("\x8B" .. "Thanks for playing the Clan Arena Beta!")
			server.clanstart = 0
			server.clanrunning = 0
		end
	end
end)

//this shit is so depraved.
//if anybody seriously wants to use this code please just fucking dm me its not worth trying to translate what the fuck the code says xddddd