players = {}
wfSpawns = {}
plrSpawns = {}
livesConditions = {}
playersLives = {}
numLives = 0

Tick = function()
	if playersLives[1] > 0 then
		M0Text = "" .. players[1].Name .. ": " .. playersLives[1] .. " Lives   " .. players[1].UnitsKilled .. " Kills\n"
	else
		M0Text = ""
	end
	if playersLives[2] > 0 then
		M1Text = "" .. players[2].Name .. ": " .. playersLives[2] .. " Lives   " .. players[2].UnitsKilled .. " Kills\n"
	else
		M1Text = ""
	end
	if playersLives[3] > 0 then
		M2Text = "" .. players[3].Name .. ": " .. playersLives[3] .. " Lives   " .. players[3].UnitsKilled .. " Kills\n"
	else
		M2Text = ""
	end
	if playersLives[4] > 0 then
		M3Text = "" .. players[4].Name .. ": " .. playersLives[4] .. " Lives   " .. players[4].UnitsKilled .. " Kills\n"
	else
		M3Text = ""
	end
	if playersLives[5] > 0 then
		M4Text = "" .. players[5].Name .. ": " .. playersLives[5] .. " Lives   " .. players[5].UnitsKilled .. " Kills\n"
	else
		M4Text = ""
	end
	if playersLives[6] > 0 then
		M5Text = "" .. players[6].Name .. ": " .. playersLives[6] .. " Lives   " .. players[6].UnitsKilled .. " Kills\n"
	else
		M5Text = ""
	end
	UserInterface.SetMissionText("\n\n" .. M0Text .. M1Text .. M2Text .. M3Text .. M4Text .. M5Text, HSLColor.Gold)
end

InitializeGame = function()
	Creeps = Player.GetPlayer("Creeps")
	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")
	wfSpawns[1] = pl1wf
	wfSpawns[2] = pl2wf
	wfSpawns[3] = pl3wf
	wfSpawns[4] = pl4wf
	wfSpawns[5] = pl5wf
	wfSpawns[6] = pl6wf
	plrSpawns[1] = spwn1
	plrSpawns[2] = spwn2
	plrSpawns[3] = spwn3
	plrSpawns[4] = spwn4
	plrSpawns[5] = spwn5
	plrSpawns[6] = spwn6
	livesConditions[1] = "livescntr1"
	livesConditions[2] = "livescntr2"
	livesConditions[3] = "livescntr3"
	livesConditions[4] = "livescntr4"
	livesConditions[5] = "livescntr5"
	livesConditions[6] = "livescntr6"
	livesConditions[7] = "livescntr7"
	livesConditions[8] = "livescntr8"
	livesConditions[9] = "livescntr9"
	livesConditions[10] = "livescntr10"
	livesConditions[11] = "livescntr11"
	livesConditions[12] = "livescntr12"
	livesConditions[13] = "livescntr13"
	livesConditions[14] = "livescntr14"
	livesConditions[15] = "livescntr15"
	livesConditions[16] = "livescntr16"
	livesConditions[17] = "livescntr17"
	livesConditions[18] = "livescntr18"
	livesConditions[19] = "livescntr19"
	livesConditions[20] = "livescntr20"
	if players[1] == nil then
		players[1] = Player.GetPlayer("Creeps")
		local actor = Actor.Create("mcv", true, { Owner = Creeps, Location = pl1wf.Location })
	end
	if players[2] == nil then
		players[2] = Player.GetPlayer("Creeps")
		local actor = Actor.Create("mcv", true, { Owner = Creeps, Location = pl2wf.Location })
	end
	if players[3] == nil then
		players[3] = Player.GetPlayer("Creeps")
		local actor = Actor.Create("mcv", true, { Owner = Creeps, Location = pl3wf.Location })
	end
	if players[4] == nil then
		players[4] = Player.GetPlayer("Creeps")
		local actor = Actor.Create("mcv", true, { Owner = Creeps, Location = pl4wf.Location })
	end
	if players[5] == nil then
		players[5] = Player.GetPlayer("Creeps")
		local actor = Actor.Create("mcv", true, { Owner = Creeps, Location = pl5wf.Location })
	end
	if players[6] == nil then
		players[6] = Player.GetPlayer("Creeps")
		local actor = Actor.Create("mcv", true, { Owner = Creeps, Location = pl6wf.Location })
	end
end

InitializePlayers = function()
	for i = 1, #players do
		playersLives[i] = numLives
		local actor = Actor.Create("weap", true, { Owner = players[i], Location = wfSpawns[i].Location })
		local plrMcv = players[i].GetActorsByType("mcv")[1]
		if players[i] ~= Player.GetPlayer("Creeps") then
			actor = Actor.Create("jeep2", true, { Owner = players[i], Location = plrMcv.Location})
			actor.GrantCondition(livesConditions[numLives], 0)
			plrMcv.Destroy()
			Trigger.OnKilled(actor, function ()
				SpawnNewJeep(i)
			end)
		else
			playersLives[i] = 0
		end
	end
end

SpawnNewJeep = function(playerNum)
	DestroyAllPowersAndMines(players[playerNum])
	local doISpawn = false
	doIspawn = UpdateLives(playerNum)
	if doIspawn == true then
		local spawnPoint = FindSafeSpawn()
		local newJeep = Actor.Create("jeep2", true, { Owner = players[playerNum], Location = spawnPoint.Location})
		newJeep.GrantCondition(livesConditions[playersLives[playerNum]], 0)
		Beacon.New(players[playerNum], newJeep.CenterPosition, 120, true)
		Media.FloatingText("" .. players[playerNum].Name .. "", newJeep.CenterPosition, 60, players[playerNum].Color)
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			if not newJeep.IsDead then
				PlayerLive = numLives - players[playerNum].UnitsLost
				Media.FloatingText("" .. PlayerLive .. "  lives", newJeep.CenterPosition, 60, players[playerNum].Color)
			end
		end)
	Trigger.AfterDelay(5, function()
		if Multi0.IsLocalPlayer and newJeep.Owner == Multi0
		then
			SetCamera(Multi0)
		elseif Multi1.IsLocalPlayer and newJeep.Owner == Multi1 then
			SetCamera(Multi1)
		elseif Multi2.IsLocalPlayer and newJeep.Owner == Multi2 then
			SetCamera(Multi2)
		elseif Multi3.IsLocalPlayer and newJeep.Owner == Multi3 then
			SetCamera(Multi3)
		elseif Multi4.IsLocalPlayer and newJeep.Owner == Multi4 then
			SetCamera(Multi4)
		elseif Multi5.IsLocalPlayer and newJeep.Owner == Multi5 then
			SetCamera(Multi5)
		end
	end)
		Trigger.OnKilled(newJeep, function ()
			Trigger.AfterDelay(55, function()
				SpawnNewJeep(playerNum)
			end)
		end)
	end
end
SetCamera = function(Multi)
	local Apower = Multi.GetActorsByType("jeep2")[1]
	Camera.Position = Apower.CenterPosition
end
FindSafeSpawn = function()
	local jeepsInVicinity = {}
	local safeSpawns = {}
	local minJeeps = 1000
	local safeSpawnNum = 1
	local safeSpawnTracker = 1
	local safeSpawn = gotohere
	for i = 1, #plrSpawns do
		jeepsInVicinity = Map.ActorsInCircle(plrSpawns[i].CenterPosition, WDist.New(6144), IsJeep)
		if #jeepsInVicinity <= minJeeps then
			safeSpawnNum = i
			if #jeepsInVicinity == minJeeps then
				safeSpawns[safeSpawnTracker] = plrSpawns[i]
				safeSpawnTracker = safeSpawnTracker + 1
			end
			minJeeps = #jeepsInVicinity
		end
	end
	safeSpawn = plrSpawns[safeSpawnNum]
	if #safeSpawns > 1 then
		local randomSpawn = Utils.RandomInteger(1, #safeSpawns)
		safeSpawn = safeSpawns[randomSpawn]
	end
	return safeSpawn
end

IsJeep = function(actor)
	return actor.Type == "jeep2"
end

UpdateLives = function(playerNum)
	playersLives[playerNum] = playersLives[playerNum] - 1
	if playersLives[playerNum] > 0 then
		return true
	else
		players[playerNum].GetActorsByType("weap")[1].Destroy()
		return false
	end
end

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

Light = 0

turnOn = function()
	Lighting.Ambient = Light
	if Light < 1 then
		Light = Light + 0.009
		Trigger.AfterDelay(1, function()
			turnOn()
		end)
	end
end
turnOff = function()
	Lighting.Ambient = Light
	if Light > 0.05 then
		Light = Light - 0.035
		Trigger.AfterDelay(1, function()
			turnOff()
		end)
	else
		Trigger.AfterDelay(23, function()
			ShowFightSign()
		end)
		Trigger.AfterDelay(102, function()
			turnOn()
		end)
	end
end

SetupPlayers = function()
	France = Player.GetPlayer("france")
	Multi0 = Player.GetPlayer("Multi0")
	if Multi0 == nil then
		Multi0 = Player.GetPlayer("Creeps")
	end
	Multi1 = Player.GetPlayer("Multi1")
	if Multi1 == nil then
		Multi1 = Player.GetPlayer("Creeps")
	end
	Multi2 = Player.GetPlayer("Multi2")
	if Multi2 == nil then
		Multi2 = Player.GetPlayer("Creeps")
	end
	Multi3 = Player.GetPlayer("Multi3")
	if Multi3 == nil then
		Multi3 = Player.GetPlayer("Creeps")
	end
	Multi4 = Player.GetPlayer("Multi4")
	if Multi4 == nil then
		Multi4 = Player.GetPlayer("Creeps")
	end
	Multi5 = Player.GetPlayer("Multi5")
	if Multi5 == nil then
		Multi5 = Player.GetPlayer("Creeps")
	end
	AllPlayers = { Multi0, Multi1, Multi2, Multi3, Multi4, Multi5 }
end

WorldLoaded = function()
	SetupPlayers()
	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
	end
	InitializeGame()
--	Trigger.AfterDelay(1, function()
		InitializePlayers()
--	end)
	turnOn()
	Trigger.AfterDelay(DateTime.Seconds(2), function()

	end)
end