--"Latpoint-B-Gone" by JugadorXeI
-- addfile maphz.lua; addfile KL_kartmp-v1.3.pk3; wait; kmp_respawnpoints On; nokmppoints_debug On

if latpointbgone == nil then
	rawset(_G, "latpointbgone", false)
end

if latpointbgone == false then

local noLatPointDebug = CV_RegisterVar({
	name = "nokmppoints_debug",
	defaultvalue = "Off",
	flags = CV_NETVAR,
	PossibleValue = CV_YesNo,
})

local function areLatPointsEnabled()
	if kmp_respawnpoints == nil 	then return false end
	if kmp_respawnpoints.value == 0 then return false end

	return true
end

local function isMobjValidPlayer(mobj)
	if mobj == nil 			then return false end
	if mobj.player == nil 	then return false end
	
	return true
end

local function noLatPointHandlerOne(line, mobj, sector)
	if noLatPointDebug.value then
		print("No KMP Points - 'One' Handler Called")
	end
	
	if not isMobjValidPlayer(mobj) then return end
	
	mobj.player.disableLatPoint = true
	mobj.player.latPointToDisable = line.tag
	
	if noLatPointDebug.value then
		print("KMP Respawn Point disabled for checkpoint " +
			mobj.player.latPointToDisable + " for player " + mobj.player.name)
	end
end

local function noLatPointHandlerThis(line, mobj, sector)
	if noLatPointDebug.value then
		print("No KMP Points - 'This' Handler Called")
	end
	
	if not isMobjValidPlayer(mobj) then return end
	
	mobj.player.disableLatPoint = true
	mobj.player.latPointToDisable = mobj.player.starpostnum
	
	if noLatPointDebug.value then
		print("KMP Respawn Point disabled for checkpoint " +
			mobj.player.latPointToDisable + " for player " + mobj.player.name)
	end
end

local function noLatPointHandlerToggle(line, mobj, sector)
	if noLatPointDebug.value then 
		print("No KMP Points - 'Toggle' Handler Called")
	end
	
	if not isMobjValidPlayer(mobj) then return end
	mobj.player.disableLatPoint = true
	
	local latPointStatus = not mobj.player.latPointToggle or false
	
	-- forcing on/off
	if line.flags & ML_BOUNCY then -- force on
		latPointStatus = true
	elseif line.flags & ML_TFERLINE then -- force off
		latPointStatus = false
	end
	
	mobj.player.latPointToggle = latPointStatus
	
	if noLatPointDebug.value then
		print("KMP Respawn Point toggle status: " +
			mobj.player.latPointToggle + " for player " + mobj.player.name)
	end
end

local function noLatPoints(target, inflictor, source, damage)
	if not areLatPointsEnabled() then return end
	if target.player.disableLatPoint == false then return end
	
	-- we're trying to prevent players from getting latpoints in this specified checkpoint
	if target.player.starpostnum == target.player.latPointToDisable then
		target.player.kmp_respawn = nil
		
		target.player.disableLatPoint = false
		target.player.latPointToDisable = 0

		if noLatPointDebug.value then
			print("KMP Respawn Point prevented for " +
			target.player.name + " - Once")
		end
		
	elseif target.player.latPointToggle then
		target.player.kmp_respawn = nil
		
		if noLatPointDebug.value then	
			print("KMP Respawn Point prevented for " +
			target.player.name + " - Toggle")
		end
	end
end

local function mapStartHandler(mapnum)
	-- don't allow residual information to be used in new maps
	for	player in players.iterate do
		player.disableLatPoint 		= false
		player.latPointToDisable 	= -1
		player.latPointToggle 		= false
	end
end

addHook("MobjDeath", noLatPoints, MT_PLAYER)
addHook("MapLoad", mapStartHandler)
addHook("LinedefExecute", noLatPointHandlerThis, "NOLATPNTTHIS")
addHook("LinedefExecute", noLatPointHandlerOne, "NOLATPNTONE")
addHook("LinedefExecute", noLatPointHandlerToggle, "NOLATPNTTOGGLE")

latpointbgone = true

end