players = {}
lives = {}
numLives = 5

Tick = function()
	local plrLoop = 1
	while plrLoop <= #players do
		local jeepz = players[plrLoop].GetActorsByType("jeep2")
		if jeepz[1] == nil then
			UpdateLives(plrLoop)
			DestroyAllPowers(players[plrLoop])
		end
		plrLoop = plrLoop + 1
	end
	plrLoop = 1
	while plrLoop <= #players  do
		local units = players[plrLoop].GetActors()
		local x = 4
		while x <= #units do
			if units[x].Type ~= "jeep2" then
				if units[x].Type ~= "minv" then
					units[x].Teleport(gotohere.Location)
				end
			end
			x = x + 1
		end
		plrLoop = plrLoop + 1
	end
end

UpdateLives = function(playerNumber)
	if lives[playerNumber] > 1 then
		lives[playerNumber] = lives[playerNumber] - 1
		Media.DisplayMessage("Player " .. playerNumber .. " has " .. lives[playerNumber] .. " lives")
		CreateJeep(players[playerNumber])
	elseif lives[playerNumber] == 1 then
		lives[playerNumber] = lives[playerNumber] - 1
		players[playerNumber].GetActorsByType("apwr")[1].Destroy()
	elseif lives[playerNumber] < 1 then
	end
end

DestroyAllPowers = function(player)
	local plractors = player.GetActors()
	local looper = #plractors
	while looper > 2 do
		plractors[looper].Destroy()
		looper = looper - 1
	end
end

CreateJeep = function(player)
	local spawnPointNum = Utils.RandomInteger(1, 6)
	local spawnPoint = spwn1
	if spawnPointNum == 1 then
		spawnPoint = spwn1
	elseif spawnPointNum == 2 then
		spawnPoint = spwn2
	elseif spawnPointNum == 3 then
		spawnPoint = spwn3
	elseif spawnPointNum == 4 then
		spawnPoint = spwn4
	elseif spawnPointNum == 5 then
		spawnPoint = spwn5
	elseif spawnPointNum == 6 then
		spawnPoint = spwn6
	end
	local actor = Actor.Create("jeep2", true, { Owner = player, Location = spawnPoint.Location })
	if player.IsLocalPlayer then
		Camera.Position = actor.CenterPosition
	end
end

InitializeStartingCamera = function(player)
	if player.IsLocalPlayer then
		local startingJeep = player.GetActorsByType("jeep2")[1]
		Camera.Position = startingJeep.CenterPosition
	end
end

InitializePlayers = function()
	players[1] = Player.GetPlayer("Multi0")
	players[2] = Player.GetPlayer("Multi1")
	players[3] = Player.GetPlayer("Multi2")
	players[4] = Player.GetPlayer("Multi3")
	players[5] = Player.GetPlayer("Multi4")
	players[6] = Player.GetPlayer("Multi5")
	local plrsLoopz = 1
	while plrsLoopz <= #players do
		lives[plrsLoopz] = numLives
		local actor = Actor.Create("apwr", true, { Owner = players[plrsLoopz], Location = gotohere.Location })
		InitializeStartingCamera(players[plrsLoopz])
		plrsLoopz = plrsLoopz + 1
	end
end

WorldLoaded = function()
	if Map.LobbyOption("livesID") == "a" then
		numLives = 1
	elseif Map.LobbyOption("livesID") == "g" then
		numLives = 3
	elseif Map.LobbyOption("livesID") == "b" then
		numLives = 5
	elseif Map.LobbyOption("livesID") == "c" then
		numLives = 10
	elseif Map.LobbyOption("livesID") == "d" then
		numLives = 15
	elseif Map.LobbyOption("livesID") == "e" then
		numLives = 20
	elseif Map.LobbyOption("livesID") == "f" then
		numLives = 25
	end
	InitializePlayers()
end