-- Server stuff.
Intro = {
	Map = 72,
	Event = 100
}

Title = {
	-- Note that all of these maps can be by name if you prefer.
	-- Intro and Start maps are by number for existing mod compatibility.
	Map = '0',
	Event = nil
}

Start = {
	Map = 13,
	Pos = {10,7},
	Event = 200,
	Health = 3
}

players = {}

function GAME:Init()
	if net.offline
		self:NetJoin(0) -- hack
	end
end

function GAME:NetJoin(uid)
	players[uid] = {
		id = uid,
		spawned = false,
		intro = net.offline
	}
	if players[uid].intro
		net.send_map(uid,stage.load(Intro.Map))
		net.tsc_nod(uid)
		tsc.start(players[uid],Intro.Event,true)
	else
		-- TODO: Load saved game here.
		net.send_map(v.id,stage.load(Start.Map))
		tsc.start(v,Start.Event,true)
	end
end

function GAME:NetLeave(uid)
	local stage = players[uid].stage
	if stage
		stage.players[uid] = nil
	end
	players[uid] = nil
end

function GAME:Think()
	for k,v in pairs(stages)
		if not next(v.players)
			console.debug("Unloaded map "..k)
			stages[k] = nil
		else
			for k,v in pairs(v.ents)
				NPC.think(v)
			end
		end
	end
	for k,v in pairs(players)
		tsc.run(v)
		if v.intro and not v.tsc
			v.intro = nil
			net.send_title(v.id)
			net.send_map(v.id,stage.load(Title.Map))
			if Title.Event
				tsc.start(v,Title.Event,true)
			end
		end
	end
end
