--------------------------------------------------------------
--															--
-- Global Level Time Limit									--
--															--
-- Use Lua.hmt_timelimit to a number to specify the			--
-- amount of minutes the map will last for.					--
--															--
-- Lua.hmt_timelimit_seconds works the same way but for		--
-- seconds.													--
--															--
--------------------------------------------------------------

freeslot("sfx_tlhury") -- Hurry up!
freeslot("sfx_hmexpl")
freeslot("sfx_hmtart")

if (hmt_timelimitinit) then return end -- Do not run twice.

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

-- cool stuff
local timelimit_minutes = 0
local timelimit_seconds = 0

addHook("NetVars", function(net)
	timelimit_minutes = net(timelimit_minutes)
	timelimit_seconds = net(timelimit_seconds)
end)

local premadevalues = {
	[962] = {10, 0} -- KanzakiPak's "Climb the Stairs" (10 minutes and 0 seconds)
}

local function TimeLimitStart()
	-- reset
	timelimit_minutes = 0
	timelimit_seconds = 0
	-- we have a premade value!
	if premadevalues[gamemap] then
		timelimit_minutes = premadevalues[gamemap][1]
		timelimit_seconds = premadevalues[gamemap][2]
	end
	-- timelimit in minutes
	if mapheaderinfo[gamemap].hmt_timelimit then
		timelimit_minutes = tonumber(mapheaderinfo[gamemap].hmt_timelimit)
	end
	-- timelimit in seconds
	if mapheaderinfo[gamemap].hmt_timelimit_seconds then
		timelimit_seconds = tonumber(mapheaderinfo[gamemap].hmt_timelimit_seconds)
	end
	
	if timelimit_minutes > 0 or timelimit_seconds > 0 then
		S_StartSound(nil, sfx_hmtart)
		hmt_alert("\x85".."ALERT! ".."\x83".."The level will end in "..tostring(timelimit_minutes).." minutes and "..tostring(timelimit_seconds).." seconds. Good luck!")
	end
end

local function TimeLimitThinker()
	-- we have it
	if leveltime < starttime then return end
	if timelimit_minutes > 0 or timelimit_seconds > 0 then
		local sec = (leveltime - starttime) / TICRATE
		-- remind people
		if (leveltime - starttime) == (((timelimit_minutes*60) + timelimit_seconds) - 60)*TICRATE then
			hmt_alert("\x85".."ALERT! ".."\x83".."The level will end in 60 seconds. Good luck!")
		end
		if (leveltime - starttime) == (((timelimit_minutes*60) + timelimit_seconds) - 10)*TICRATE then
			hmt_alert("\x85".."ALERT! ".."\x83".."The level will end in 10 seconds. Hurry up!")
			S_StartSound(nil, sfx_tlhury)
		end
		-- we up?
		if sec >= (timelimit_minutes*60) + timelimit_seconds then
			COM_BufInsertText(server, "exitlevel")
			COM_BufInsertText(consoleplayer, "tunes racent")
			-- it would be so awesome
			for p in players.iterate do
				if p.exiting then continue end
				-- boom
				P_DamageMobj(p.mo, nil, nil, 10000)
				p.lives = 0
				p.pflags = $ | PF_TIMEOVER
				local boom = P_SpawnMobj(p.mo.x, p.mo.y, p.mo.z, MT_FZEROBOOM)
				boom.scale = p.mo.scale
				boom.angle = p.mo.angle
			end
			S_StartSound(nil, sfx_hmexpl)
			hmt_alert("Times up!")
		end
	end
end

addHook("ThinkFrame", function()
	if leveltime == 2 then
		TimeLimitStart()
	end
	if leveltime > 2 then
		TimeLimitThinker()
	end
end)

rawset(_G, "hmt_timelimitinit", 1) -- We're done!