hud.disable("nightslink")
hud.disable("nightsdrill")
hud.disable("nightsrings")
hud.disable("nightsscore")
hud.disable("nightstime")
--!2191 mr gonna fix that :/
--rn it completely useless
--05.02.24: yay they finally merged that mr and it will work in 2.2.14 :)
hud.disable("nightsrecords")

addHook("PlayerThink", function()
	if gamemap ~= 29 then
		hud.disable("nightslink")
		hud.disable("nightsdrill")
		hud.disable("nightsrings")
		hud.disable("nightsscore")
		hud.disable("nightstime")
		hud.disable("nightsrecords")
	else
		hud.enable("nightslink")
		hud.enable("nightsdrill")
		hud.enable("nightsrings")
		hud.enable("nightsscore")
		hud.enable("nightstime")
		hud.enable("nightsrecords")
	end
	--!2191 mr gonna fix that :/
	--rn it completely useless
	--05.02.24: yay they finally merged that mr and it will work in 2.2.14 :)
end)

local time = {
	timelost = 0,
}
---Hud drawing hooks yipee
--the GET X RINGS stays for 5 seconds
--also this draws the timer
--draw TIME LEFT 8 px higher than the sonic
--rings is 40 px right

local countrings = function()
	local count = 0
	for p in players.iterate() do
		if p.rings then
			count = $ + p.rings
		end
	end
	
	return count
end

hud.add(function(v, p, cam)
	if gamemap > 49 and gamemap < 58
	and leveltime < 175 then
		local getrings = v.cachePatch("GETALL")
		v.draw(111, 95, getrings)
        v.drawNum(161, 95, mapheaderinfo[gamemap].ssspheres)
	end

	if gamemap > 49 and gamemap < 58 then
        v.drawString(160, 159, "TIME LEFT", V_OLDSPACING, "center")
		if p.mo.eflags & MFE_UNDERWATER and not paused then
			time.timelost = time.timelost + 5 --this is in TICS
		end
		if sstimer - p.realtime - time.timelost > 0 then
        	v.drawNum(172, 175, (sstimer - p.realtime - time.timelost)/TICRATE)
		else
			local timeup = v.cachePatch("TIMEUP")
			v.drawNum(172, 175, 0)
			if countrings() ~= tonumber(mapheaderinfo[gamemap].ssspheres)
				v.draw(130, 95, timeup)
			end
		end
		
		local rings = v.cachePatch("SBORINGS")
		v.draw(16, 40, rings, V_SNAPTOLEFT|V_SNAPTOTOP)
        v.drawNum(96, 40, p.rings, V_SNAPTOLEFT|V_SNAPTOTOP, "right")
        v.drawNum(96, 56, mapheaderinfo[gamemap].ssspheres, V_SNAPTOLEFT|V_SNAPTOTOP, "right")

        local score = v.cachePatch("SBOSCORE")
        v.draw(16, 8, score, V_SNAPTOLEFT|V_SNAPTOTOP)
		v.drawNum(120, 8, p.score, V_SNAPTOLEFT|V_SNAPTOTOP, "right")

        local timepic = v.cachePatch("SBOTIME")
		local colon = v.cachePatch("SBOCOLON")
		local seconds = G_TicsToSeconds(p.realtime)
		local minutes = G_TicsToMinutes(p.realtime)
        v.draw(16, 24, timepic, V_SNAPTOLEFT|V_SNAPTOTOP)
        v.drawPaddedNum(96, 24, seconds, 2, V_SNAPTOLEFT|V_SNAPTOTOP, "right")
		v.draw(72, 24, colon, V_SNAPTOLEFT|V_SNAPTOTOP)
		v.drawNum(72, 24, minutes, V_SNAPTOLEFT|V_SNAPTOTOP, "right")
	end
end, "game")

addHook("MapLoad", function(map)
	time.timelost = 0
end)

addHook("MobjDamage", function(mo)
	if mo ~= consoleplayer.mo then return end
	time.timelost = $ + 175
end, MT_PLAYER)