-- Server network messages
net.recv = {}

function GAME:NetRecv(uid,msg)
	local id = msg:bin(1)
	if net.recv[id]
		net.recv[id](uid,msg:sub(2))
	else
		console.print("WARNING: Unhandled message type "..(NETMSG[id] or id).." recieved!")
	end
end

net.recv[NETMSG.TITLE] = function(uid,msg)
	-- TODO: Load saved game here.
	local player = players[uid]
	if not player.title
		return
	end
	player.title = false
	net.send_map(player.id,stage.load(Start.Map))
	tsc.start(player,Start.Event,true)
end

net.recv[NETMSG.TSC_TEXT] = function(uid,msg)
	local player = players[uid]
	if player.tsc and player.tsc.wait == "text"
		player.tsc.wait = nil
	end
end

net.recv[NETMSG.TSC_NOD] = function(uid,msg)
	local player = players[uid]
	if player.intro
		tsc.cmd.END(player)
		return
	end
	if player.tsc and player.tsc.wait == "nod"
		player.tsc.wait = nil
	end
end

function net.send_console(text)
	net.send(0,string.num(NETMSG.CHAT) .. text)
end

function net.send_map(uid,map)
	local player = players[uid]
	if player.stage
		player.stage.players[player.id] = nil
	end
	player.stage = stages[map]
	player.tsc = nil
	player.ents = {}
	net.send(player.id,string.num(NETMSG.MAP) .. map,NET.GUARANTEED|NET.ORDERED)
	if stages[map]
		stages[map].players[player.id] = player
		for i,v in ipairs(stages[map].ents)
			net.ent_spawn(uid,v)
		end
	end
end

function net.send_title(uid)
	local player = players[uid]
	player.title = true
	net.send(player.id,string.num(NETMSG.TITLE),NET.GUARANTEED)
end

function net.ent_spawn(uid,e)
	local net_flags = 0
	local important_flags = e.flags & (NPCF.BLOCKING|NPCF.BLOCK_SHOTS|NPCF.TOPBOUNCE|NPCF.SHOOTABLE|NPCF.SOLID|NPCF.RIDE|NPCF.TOUCH_EVENT|NPCF.CHECK_EVENT)
	if important_flags ~= 0
		if e.type ~= 0 or important_flags & (~(NPCF.TOUCH_EVENT|NPCF.CHECK_EVENT)) ~= 0
			net_flags = NET.GUARANTEED
		end
	end
	local flags = e.flags & (~NPCF.SERVER_ONLY)
	players[uid].ents[e.id] = {
		net_flags = net_flags,
		x = math.floor(e.x), y = math.floor(e.y),
		mx = 0, my = 0,
		type = e.type,
		flags = flags,
		anim = e.anim
	}
	net.send(uid,string.num(NETMSG.ENT_SPAWN) .. string.num(e.id) .. string.num(e.type,2) .. string.num(math.floor(e.x),4) .. string.num(math.floor(e.y),4) .. string.num(flags,4) .. string.num(e.anim),net_flags)
end

function net.ent_anim(uid,e)
	local pe = players[uid].ents[e.id]
	pe.x = math.floor(e.x)
	pe.y = math.floor(e.y)
	pe.anim = e.anim
	pe.flags = (pe.flags & (~NPCF.FLIP)) | (e.flags & NPCF.FLIP)
	net.send(uid,string.num(NETMSG.ENT_ANIM) .. string.num(e.id) .. string.num(e.x,4) .. string.num(e.y,4) .. string.num(e.anim) .. string.num(e.flags & NPCF.FLIP ~= 0),pe.net_flags)
end

function net.ent_move(uid,e)
	local pe = players[uid].ents[e.id]
	pe.x = math.floor(e.x)
	pe.y = math.floor(e.y)
	pe.mx = e.mx
	pe.my = e.my
	pe.anim = e.anim
	net.send(uid,string.num(NETMSG.ENT_MOVE) .. string.num(e.id) .. string.num(e.x,4) .. string.num(e.y,4) .. string.num(e.mx) .. string.num(e.my) .. string.num(e.anim),pe.net_flags)
end

function net.tsc_end(uid)
	net.send(uid,string.num(NETMSG.TSC_END),NET.GUARANTEED)
end

function net.tsc_keylock(uid)
	if not players[uid].spawned
		return
	end
	local keylock = 0
	if players[uid].tsc
		keylock = players[uid].tsc.keylock or 0
	end
	net.send(uid,string.num(NETMSG.TSC_KEY) .. string.num(keylock),NET.GUARANTEED)
end

function net.tsc_fai(uid,dir)
	net.send(uid,string.num(NETMSG.TSC_FAI) .. string.num(dir),NET.GUARANTEED)
end

function net.tsc_fao(uid,dir)
	net.send(uid,string.num(NETMSG.TSC_FAO) .. string.num(dir))
end

function net.tsc_msg(uid)
	local msg
	if not players[uid].tsc or not players[uid].tsc.msg
		msg = 0
	else
		msg = players[uid].tsc.msg
	end
	net.send(uid,string.num(NETMSG.TSC_MSG) .. string.num(msg),NET.GUARANTEED)
end

function net.tsc_text(uid,text)
	local tur = 0
	if players[uid].tsc and players[uid].tsc.tur
		tur = 1 -- tur or cat is 1
		if players[uid].tsc.tur == "sat"
			tur = 2 -- sat is 2
		end
	end
	net.send(uid,string.num(NETMSG.TSC_TEXT) .. string.num(tur) .. text,NET.GUARANTEED|NET.ORDERED)
end

function net.tsc_nod(uid)
	net.send(uid,string.num(NETMSG.TSC_NOD),NET.GUARANTEED|NET.ORDERED)
end

function net.tsc_clr(uid)
	net.send(uid,string.num(NETMSG.TSC_CLR),NET.GUARANTEED|NET.ORDERED)
end
