freeslot(
	"MT_YELLOW_LUM"
)

--Yellow Lums

local lums = 0
local maxlums = 0
local lumtable = {}
local LUMS_EMBLEM = 16
local LUMS_TIME_EMBLEM = 17

--Reset Lums on map load

addHook("MapLoad", do
	if not mapheaderinfo[gamemap].lumslua then return end
	lums = 0
	lumtable = {}
end)

--Netgame sync

addHook("NetVars", function(net)
	if not mapheaderinfo[gamemap].lumslua then return end
	lums = net($)
end)

--Add Lum spawn point to the table

addHook("TouchSpecial", function(special, toucher)
	if not mapheaderinfo[gamemap].lumslua then return end
	if toucher.player.bot == 0
		table.insert(lumtable, #special.spawnpoint)
		lums = $ + 1
	end
end, MT_YELLOW_LUM)

--Remove collected lums on map retry

addHook("MapThingSpawn", function(mobj, mapthing)
	if not mapheaderinfo[gamemap].lumslua then return end
	for _, lum in pairs(lumtable)
		if #mapthing == lum
			P_RemoveMobj(mobj)
			return true
		end
	end
	mobj.renderflags = $ + RF_NOCOLORMAPS
end, MT_YELLOW_LUM)

addHook("MapLoad", function(mapnum)
	if not mapheaderinfo[gamemap].lumslua then return end
	maxlums = 0
	for thing in mapthings.iterate
		if thing.type == mobjinfo[MT_YELLOW_LUM].doomednum
			maxlums = $ + 1
		end
	end
end)

--Award emblems

addHook("PlayerThink", function(player)
	if not mapheaderinfo[gamemap].lumslua then return end
	if modifiedgame then return end
	if lums >= maxlums
	and maxlums > 0
		for thing in mapthings.iterate
			if thing.type == mobjinfo[MT_EMBLEM].doomednum
				if thing.angle == LUMS_EMBLEM
				and multiplayer == false
					P_TeleportMove(thing.mobj, player.mo.x, player.mo.y, player.mo.z)
				end
				if thing.angle == LUMS_TIME_EMBLEM
				and leveltime <= tonumber(mapheaderinfo[gamemap].lumstime)*TICRATE
				and modeattacking == true
					P_TeleportMove(thing.mobj, player.mo.x, player.mo.y, player.mo.z)
				end
			end
		end
	end
end)

--Empty Lum table on map change

addHook("MapChange", function(mapnum)
	if not mapheaderinfo[gamemap].lumslua then return end
	lums = 0
	lumtable = {}
end)

--Draw Lum counter

hud.add(function(v,player)
	if not mapheaderinfo[gamemap].lumslua then return end
	v.draw(16,160,v.cachePatch("RAYLUMS"),V_SNAPTOLEFT|V_SNAPTOTOP|V_PERPLAYER)
	v.drawNum(100,160,lums,V_SNAPTOLEFT|V_SNAPTOTOP|V_PERPLAYER)
	v.drawNum(150,160,maxlums,V_SNAPTOLEFT|V_SNAPTOTOP|V_PERPLAYER)
end,"scores")