freeslot("sfx_hurry", "sfx_tover", "sfx_ringtk", "sfx_ringtm", "sfx_cointk")

sfxinfo[sfx_hurry] = {
	singular = false,
	caption = "Hurry up!"
}

sfxinfo[sfx_tover] = {
	singular = false,
	caption = "Time over!"
}

sfxinfo[sfx_ringtk] = {
	singular = false,
	caption = "Got Token"
}

sfxinfo[sfx_ringtm] = {
	singular = false,
	caption = "Got Token"
}

sfxinfo[sfx_cointk] = {
	singular = false,
	caption = "Got Token"
}

//General variables. Used to help the script flow better.
local textstuff = false
local textstuff2 = false
local texttimer = 0
local textnoise = false

local wtvar = 9
local etvar = 10

//Timing Constants. Used to streamline the conditionals of the time-up script.
local warningtime = wtvar * 60*TICRATE
local warningsfxlength = 2242*TICRATE/1000 //Length of the warning sound
local exitingtime = etvar * 60*TICRATE

//Misc constants/variables.
local nightmare = false
local autoexit = false
local ttr = false
local checkxy = {}

local function HurryText(v)
    if textstuff
        v.drawString(160, 100, "Hurry Up!", V_HUDTRANS|V_PERPLAYER, "center")
    end
end

local function TimeOverText(v)
    if textstuff2
        v.drawString(160, 100, "Time Over!", V_HUDTRANS|V_PERPLAYER, "center")
    end
end

hud.add(HurryText) 
hud.add(TimeOverText) 

COM_AddCommand("ic_tokenstorings", function(player, arg)
	if player != server
		CONS_Printf(player, "You must be a server host to use this.")
	else
		local bool = ttr and "on." or "off."
		if arg
			if arg == "1" or arg == "true" or arg == "on"
				if ttr == true then CONS_Printf(player, "Tokens already give 50 rings upon collection with all 7 emeralds.") return end
				ttr = true
				S_StartSound(nil, sfx_ringtk)
				print("Tokens will now give 50 rings upon collection with all 7 emeralds.")
			elseif arg == "0" or arg == "false" or arg == "off"
				if ttr == false then CONS_Printf(player, "Tokens already follow their vanilla behavior upon collection with all 7 emeralds.") return end
				ttr = false
				S_StartSound(nil, sfx_oneup)
				print("Tokens will now follow their vanilla behavior upon collection with all 7 emeralds.")
			else
				CONS_Printf(player, "ic_tokenstorings [on/off] - Makes Emerald Tokens give 50 rings upon collection with all 7 Chaos Emeralds. Currently ".. bool)
			end
		else
			CONS_Printf(player, "ic_tokenstorings [on/off] - Makes Emerald Tokens give 50 rings upon collection with all 7 Chaos Emeralds. Currently ".. bool)
		end
	end
end, 0)

COM_AddCommand("ic_autoexit", function(player, arg)
	if player != server
		CONS_Printf(player, "You must be a server host to use this.")
	else
		local bool = autoexit and "on." or "off."
		if arg
			if arg == "1" or arg == "true" or arg == "on"
				if autoexit == true then CONS_Printf(player, "Autoexit is already on.") return end
				autoexit = true
				S_StartSound(nil, sfx_addfil)
				print("The level will now automatically end after " ..etvar.. " minutes.")
			elseif arg == "0" or arg == "false" or arg == "off"
				if autoexit == false then CONS_Printf(player, "Autoexit is already off.") return end
				autoexit = false
				S_StartSound(nil, sfx_notadd)
				print("The level will no longer automatically end after enough time passes.")
			else
				CONS_Printf(player, "ic_autoexit [on/off] - Makes the level automatically end after a set amount of time. Currently ".. bool)
			end
		else
			CONS_Printf(player, "ic_autoexit [on/off] - Makes the level automatically end after a set amount of time. Currently ".. bool)
		end
	end
end, 0)

//Command used to set the time limit.
COM_AddCommand("ic_autoexittime", function(player, arg)
	if player != server
		CONS_Printf(player,"You must be hosting a server to use this.")
		return
	end
	if arg
		if ((tonumber(arg) <= 4) or (tonumber(arg) >= 16))
			CONS_Printf(player,"Please enter a time limit between 5 and 15 minutes.")
		elseif arg == none or string.lower(arg) == "help"
			CONS_Printf(player,"ic_autoexit [5 - 15] - This command will let you set the amount of time before the level automatically ends. Currently set to " ..etvar.." minutes.")
		else
			S_StartSound(nil, sfx_s3k3a)
			print("Exit time set to " ..arg.." minutes.")
			etvar = arg
			wtvar = arg - 1
		end
	elseif arg == none or string.lower(arg) == "help"
		CONS_Printf(player,"ic_autoexit [5 - 15] - This command will let you set the amount of time before the level automatically ends. Currently set to " ..etvar.." minutes.")
	end
end)


//Time Over.
addHook("ThinkFrame", function()
	if G_IsSpecialStage(gamemap) return end
	if exitingtime and exitingtime ~= etvar
		exitingtime = etvar * 60*TICRATE
	end
	if warningtime and warningtime ~= wtvar
		warningtime = wtvar * 60*TICRATE
	end
	if not titlemapinaction
		if not autoexit return end
		if leveltime == warningtime //If the timer hits warningtime, play a noise.
			S_StartSound(nil, sfx_hurry)
		end
		if leveltime > warningtime //If the timer hits warningtime, also display text.
		and leveltime < warningtime + warningsfxlength //To make the text flash appropriately, warningsfxlength determines how long it flashes.
			if textstuff == nil
				textstuff = true
			else
				if texttimer == 0
					textstuff = true
				elseif texttimer == -5
					textstuff = false
				end
				texttimer = textstuff and ($ - 1) or ($ + 1)
			end
		end
		if leveltime >= exitingtime //If the timer hits exitingtime, end the level and play a noise.
			for player in players.iterate do
				P_DoPlayerExit(player)
			end
			if textnoise == false
				S_StartSound(nil, sfx_tover)
				textnoise = true
			end
			textstuff2 = true
			texttimer = 0
		end
	end
end)

//Command used for emeralds.
COM_AddCommand("ic_emeralds", function(player, arg)
	if player != server
		CONS_Printf(player,"You must be a server host to use this.")
		return
	end
	if arg
		if arg == "1" or arg == "true" or arg == "on"
			if emeralds then CONS_Printf(player, "You already have all of the emeralds!") return end
			emeralds = $ | EMERALD1|EMERALD2|EMERALD3|EMERALD4|EMERALD5|EMERALD6|EMERALD7
			S_StartSound(nil,sfx_cgot)
			print("You now have all 7 Chaos Emeralds.")
		elseif arg == "0" or arg == "false" or arg == "off"
			if not emeralds then CONS_Printf(player, "You don't have any of the emeralds!") return end
			emeralds = 0
			S_StartSound(nil,sfx_s3kaf)
			print("The Emeralds have warped back to the Special Stages...")
		elseif arg == none
			CONS_Printf(player, "ic_emeralds [on/off] [true/false] - This command will give you all 7 Chaos Emeralds, and can be used to take them away as well.")
		end
	elseif arg == none
		CONS_Printf(player, "ic_emeralds [on/off] [true/false] - This command will give you all 7 Chaos Emeralds, and can be used to take them away as well.")
	end
end, 0)

//Command that disables checkpoints.
COM_AddCommand("ic_checkpointless", function(player, arg)
	if player != server
		CONS_Printf(player, "You must be a server host to use this.")
	else
		local bool = nightmare and "on." or "off."
		if arg
			if arg == "1" or arg == "true" or arg == "on"
				if nightmare == true then CONS_Printf(player, "Checkpoints are already disabled.") return end
				nightmare = true
				S_StartSound(nil, sfx_kc67)
				print("All of the checkpoints have suddenly vanished!")
			elseif arg == "0" or arg == "false" or arg == "off"
				if nightmare == false then CONS_Printf(player, "Checkpoints are already enabled.") return end
				nightmare = false
				S_StartSound(nil, sfx_kc5e)
				print("All of the checkpoints have returned to their original locations.")
				if checkxy[1]
					for i = 1, #checkxy
						local starpost = P_SpawnMobj(checkxy[i][1], checkxy[i][2], checkxy[i][3], MT_STARPOST)
						starpost.angle = checkxy[i][4]
					end
					checkxy = {}
				end
			else
				CONS_Printf(player, "ic_checkpointless [on/off] - Disables all checkpoints placed in the current level. Currently ".. bool)
			end
		else
			CONS_Printf(player, "ic_checkpointless [on/off] - Disables all checkpoints placed in the current level. Currently ".. bool)
		end
	end
end, 0)

//Command used for giving the player a random character.
COM_AddCommand("ic_randomchar", function(player, arg)
	if player
	    local skintable = {}
		for skin in skins.iterate do
			if skin.name != player.mo.skin
				table.insert(skintable, skin.name)
			end
		end
		if not player.mo.storedflags
			player.mo.storedflags = false
		end
		S_StartSound(player.mo, sfx_s3kb5)
		R_SetPlayerSkin(player, skintable[P_RandomRange(1, #skintable)])
		if not netgame
			player.mo.color = skins[player.mo.skin].prefcolor
		end
		player.mo.storedcarry = player.powers[pw_carry]
		player.mo.storedflags = player.pflags
		P_ResetPlayer(player)
		player.pflags = player.mo.storedflags
		player.powers[pw_carry] = player.mo.storedcarry
    end
end, 0)

//MobjThinker that handles the Thanos-snapping of checkpoints.
addHook("MobjThinker", function(mo)
	if mo and mo.valid
	and nightmare
		table.insert(checkxy, {mo.x, mo.y, mo.z, mo.angle});
		P_RemoveMobj(mo)
	end
end, MT_STARPOST)

//Give the player 50 Rings if they pick up an Emerald Token with all 7 emeralds. Will do nothing for Super Sonic, and will play a unique sound for Modern Sonic and Mario.
addHook("PlayerThink", function(player)
if player.mo and player.mo.valid
if player.mo.skin == "supersonic" return end
if not ttr return end
if player.mo.ringtokengrabbed
	P_GivePlayerRings(player, 50)
	P_AddPlayerScore(player, 1000)
	if player.mo.skin == "modernsonic"
		S_StartSound(player.mo, sfx_ringtm)
	elseif player.mo.skin == "mario"
		S_StartSound(player.mo, sfx_cointk)
	else
		S_StartSound(player.mo, sfx_ringtk)
	end
	player.mo.ringtokengrabbed = false
		end
	end
end)

addHook("TouchSpecial", function(emeraldtoken, player)
	if not ttr return end
	if All7Emeralds(emeralds)
		if not emeraldtoken.grabbed then
			player.ringtokengrabbed = true
			emeraldtoken.grabbed = true
			P_KillMobj(emeraldtoken)
			return true
		end
	elseif not All7Emeralds(emeralds)
		return false
	end
end, MT_TOKEN)

addHook("NetVars", function(network)
	nightmare = network($)
	autoexit = network($)
	ttr = network($)
	checkxy = network($)
	etvar = network($)
	wtvar = network($)
end)

addHook("MapLoad", do
	textstuff = nil
	textstuff2 = nil
	texttimer = 0
	textnoise = false
end)