local tokencount = 0
local canexit = true
local boss = false
-- HOOK BELOW BY GOLDEN FROM SRB2 DISCORD AND A DELETED USER <@456226577798135808> 
addHook("MapLoad", function(map)
    for mo in mobjs.iterate() do -- For each mapthing in the map...
        if mo and mo.valid then -- the mobj is there and valid...
            if mo.flags & MF_BOSS then -- If the mobj has the boss flag...
                boss = true -- We think this is a boss level!
                break
            end
        end
    end
    -- But just to make sure..
    for sector in sectors.iterate do -- We iterate through all the sectors.
        if GetSecSpecial(sector.special, 4) == 2 and not (string.upper(mapheaderinfo[map].lvlttl) == "EGG ROCK" and mapheaderinfo[map].actnum == 3 and gamemap == 26) and not (string.upper(mapheaderinfo[map].lvlttl) == "EGG ROCK CORE")  then -- And if one's an exit sector?
            boss = false -- Then this probably isn't a boss level (like ERZ2).
            break
        end
    end
end)

addHook("MapLoad", function(map)
	if (modeattacking == false) return end
	if (multiplayer == true) return end
	if (maptol & TOL_NIGHTS or maptol & TOL_XMAS) return end
	tokencount = 0
	if (string.upper(mapheaderinfo[map].lvlttl) == "EGG ROCK" and mapheaderinfo[map].actnum == 3 and gamemap == 25) or boss == true or (string.upper(mapheaderinfo[map].lvlttl) == "CASTLE EGGMAN" and mapheaderinfo[map].actnum == 1 and gamemap == 10)
		canexit = false
	else canexit = true
	end
	for player in players.iterate
		player.tokencollected = 0
		player.spawnx = player.mo.x
		player.spawny = player.mo.y
		player.spawnz = player.mo.z
		player.spawnangle = player.mo.angle
	end
	for mo in mobjs.iterate() do
		if mo.checked == true break end
		if mo.type == MT_TOKEN and mo.checked == nil
			tokencount = $ + 1
			mo.checked = true
		end
	end
end)

addHook("TouchSpecial", function(special, toucher)
	if (toucher and toucher.valid)
		if (modeattacking == true)
			toucher.player.tokencollected = $ + 1
		end
	end
end, (MT_TOKEN))
   
 addHook("PlayerThink", function(player)
	if player.mo and player.valid and modeattacking == true and boss == false and multiplayer == false
		player.time = player.realtime/TICRATE
		player.timebonus = 0
		if player.time <= 90
			player.timebonus = 25000
		elseif player.time <= 180
			player.timebonus = 15000
		elseif player.time <= 270
			player.timebonus = 10000
		elseif player.time <= 360
			player.timebonus = 6250
		elseif player.time >= 420
			player.timebonus = 0
		end
		if (player.exiting != 0 or player.pflags & PF_FINISHED) and (player.cmd.buttons & BT_CUSTOM1) and canexit == true and modeattacking == true and multiplayer == false
			player.pflags = $ & ~PF_FINISHED
			player.state = PST_REBORN
			player.mo.angle = player.spawnangle
			P_FlashPal(player, PAL_MIXUP, TICRATE/2)
			S_StartSound(nil, 116, player)
			player.exiting = 0
			P_SetOrigin(player.mo,player.spawnx,player.spawny,player.spawnz)
			P_RestoreMusic(player)
		end
	end
end)

local function redo(redo, player)
    if (player.mo and player.valid) and player.powers[pw_carry] != CR_NIGHTSMODE and player.exiting != 0 and canexit == true and (modeattacking == true)
		redo.drawString(-24, 45, "Custom 1 - Go Back to the Start", V_HUDTRANSHALF|V_PERPLAYER, "small")
    end
end
hud.add(redo, "game")

local function rank(rank, player) //taken straight from srb2plus
	if (player.bot == 1) return end 
	if (player.timebonus == nil) return end
	if (modeattacking != true) return end
	if player.mo and player.mo.valid
		if (player.score/2 + player.timebonus + (player.rings*100)) != nil and not (maptol & TOL_NIGHTS or maptol & TOL_XMAS) and (modeattacking == true) and boss == false
			if (player.score/2 + player.timebonus + (player.rings*100)) >= 25000 and player.tokencollected == tokencount
				rank.drawLevelTitle(140, 11, "X", V_HUDTRANS|V_SNAPTOLEFT|V_PERPLAYER|V_SNAPTOTOP|V_PURPLEMAP)
				elseif (player.score/2 + player.timebonus + (player.rings*100)) >= 25000 and player.tokencollected != tokencount
				rank.drawLevelTitle(140, 11, "S", V_HUDTRANS|V_SNAPTOLEFT|V_PERPLAYER|V_SNAPTOTOP|V_AQUAMAP)
				elseif (player.score/2 + player.timebonus + (player.rings*100)) >= 20000
				rank.drawLevelTitle(140, 11, "A", V_HUDTRANS|V_SNAPTOLEFT|V_PERPLAYER|V_SNAPTOTOP|V_REDMAP)
				elseif (player.score/2 + player.timebonus + (player.rings*100)) >= 15000
				rank.drawLevelTitle(140, 11, "B", V_HUDTRANS|V_SNAPTOLEFT|V_PERPLAYER|V_SNAPTOTOP|V_GREENMAP)
				elseif (player.score/2 + player.timebonus + (player.rings*100)) >= 10000 
				rank.drawLevelTitle(140, 11, "C", V_HUDTRANS|V_SNAPTOLEFT|V_PERPLAYER|V_SNAPTOTOP|V_SKYMAP)
				elseif (player.score/2 + player.timebonus + (player.rings*100)) >= 5000
				rank.drawLevelTitle(140, 11, "D", V_HUDTRANS|V_SNAPTOLEFT|V_PERPLAYER|V_SNAPTOTOP|V_GRAYMAP)
				elseif (player.score/2 + player.timebonus + (player.rings*100)) < 5000
				rank.drawLevelTitle(140, 11, "E", V_HUDTRANS|V_SNAPTOLEFT|V_PERPLAYER|V_SNAPTOTOP|V_BROWNMAP)
			end
		end
	end
end
hud.add(rank, "game")