--Because SOMETIMES, SRB2 devs don't give a flying fuck about mods and make half-assed decisions.

rawset(_G, "SAM_COM_BufInsertText", function(p, text)
	if p and p.valid
		COM_BufInsertText(p, text)
	elseif consoleplayer
		COM_BufInsertText(consoleplayer, text)
	end
end)

rawset(_G, "SAM_displayplayer", function(p) --Checks if p is the displayplayer
	if displayplayer and (displayplayer == p)
	or splitscreen and p
		return true
	end return false --
end)

rawset(_G, "SAM_FirstPerson", function(p, var) --checks if p is the displayplayer and whether p.sam_firstperson is true or false.
	if displayplayer and (displayplayer == p)
	or splitscreen and p
		if var1 == false
			if p.sam_firstperson == false
				return true --
			end
		elseif p.sam_firstperson == true
			return true --
		end
	end return false --
end)

--Switch all of these. You MUST switch all of them at once and fill them all in correctly.
local function SAM_CMD_Execute(p, PREVCAM, FIRSTPERSON, CAMERAdotCHASE, VISORCLOSEIN)
--	CONS_Printf(p, "\x83"+"sam_prevcam:\x80 "+p.sam_prevcam +"\x81 PrevCam:\x80 "+PREVCAM+" FirstPerson:\x80 "+FIRSTPERSON+"\x84 camera.chase:\x80 "+CAMERAdotCHASE+"\x82 visorclosein:\x80 "+VISORCLOSEIN+"\x89 time:"+leveltime)

	--Convert everything, since it can come in through a command or regular function.
	--I'll use "lol" as a stand-in for "leave stuff the same", since this situation is ridiculous.
	if PREVCAM != "lol"
		if PREVCAM == "nil" or PREVCAM == nil or PREVCAM == "NIL"
			PREVCAM = nil
		else
			PREVCAM = tonumber($)
		end
		p.sam_prevcam = PREVCAM
	end
	if FIRSTPERSON != "lol"
		if FIRSTPERSON == "false" or FIRSTPERSON == "FALSE" or not FIRSTPERSON or FIRSTPERSON == "nil"
			FIRSTPERSON = false
		else
			FIRSTPERSON = true
		end
		p.sam_firstperson = FIRSTPERSON
	end
	if CAMERAdotCHASE != "lol"
		if CAMERAdotCHASE == "true" or CAMERAdotCHASE == "TRUE" or CAMERAdotCHASE == true
			CAMERAdotCHASE = true
		else
			CAMERAdotCHASE = false
		end
		if SAM_displayplayer(p) and camera
			camera.chase = CAMERAdotCHASE
		end		
	end
	if VISORCLOSEIN != "lol"
		VISORCLOSEIN = tonumber(VISORCLOSEIN) or p.visorclosein
		p.visorclosein = VISORCLOSEIN
	end
end

--Sends info towards netgames. Using it manually is pointless.
COM_AddCommand("MVSAMUS-NETCAMSWITCH", function(p, ...) 
	if (gamestate!=GS_LEVEL) or not p.valid return end --or p.sam_saveselect return end --
	local args = {...}
	SAM_CMD_Execute(p, args[1], args[2], args[3], args[4])
end)

rawset(_G, "SAM_CMD", function(p, ...)
	if (gamestate==GS_LEVEL) and p.valid --and not p.sam_saveselect
		SAM_CMD_Execute(p, ...)
		if netgame --Now do it again, except in a way that forces netgame syncing.
			local ALONE = false
			if p==server and p.jointime<5 --No point if you've just loaded as the server.
				ALONE = true
				for z in players.iterate
					if z and z!=p ALONE=false break end --
				end
			end
			if ALONE != true
				local args,STRING = {...},"MVSAMUS-NETCAMSWITCH"
				for i = 1,#args
					STRING = $+" "+tostring(args[i])
				end
				COM_BufInsertText(p, STRING)
			end
		end
	end
end)