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 = "\n"
	end
	if playersLives[2] > 0 then
		M1Text = "" .. players[2].Name .. ": " .. playersLives[2] .. " Lives   " .. players[2].UnitsKilled .. " Kills\n"
	else
		M1Text = "\n"
	end
	if playersLives[3] > 0 then
		M2Text = "" .. players[3].Name .. ": " .. playersLives[3] .. " Lives   " .. players[3].UnitsKilled .. " Kills\n"
	else
		M2Text = "\n"
	end
	if playersLives[4] > 0 then
		M3Text = "" .. players[4].Name .. ": " .. playersLives[4] .. " Lives   " .. players[4].UnitsKilled .. " Kills\n"
	else
		M3Text = "\n"
	end
	UserInterface.SetMissionText("\n\n" .. M0Text .. M1Text .. M2Text .. M3Text, 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")
	wfSpawns[1] = pl1wf
	wfSpawns[2] = pl2wf
	wfSpawns[3] = pl3wf
	wfSpawns[4] = pl4wf
	plrSpawns[1] = spwn1
	plrSpawns[2] = spwn2
	plrSpawns[3] = spwn3
	plrSpawns[4] = spwn4
	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
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.OnKilled(newJeep, function ()
			SpawnNewJeep(playerNum)
		end)
	end
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()
		Trigger.AfterDelay(DateTime.Seconds(3), function()
			if players[playerNum] == players[1] then
				P1isDead = true
				if P2isDead then
					Multi0.MarkFailedObjective(WinGameP1)
					Multi1.MarkFailedObjective(WinGameP2)
					Multi2.MarkCompletedObjective(WinGameP3)
					Multi3.MarkCompletedObjective(WinGameP4)
				end
			elseif players[playerNum] == players[2] then
				P2isDead = true
				if P1isDead then
					Multi0.MarkFailedObjective(WinGameP1)
					Multi1.MarkFailedObjective(WinGameP2)
					Multi2.MarkCompletedObjective(WinGameP3)
					Multi3.MarkCompletedObjective(WinGameP4)
				end
			elseif players[playerNum] == players[3] then
				P3isDead = true
				if P4isDead then
					Multi2.MarkFailedObjective(WinGameP3)
					Multi3.MarkFailedObjective(WinGameP4)
					Multi0.MarkCompletedObjective(WinGameP1)
					Multi1.MarkCompletedObjective(WinGameP2)
				end
			elseif players[playerNum] == players[4] then
				P4isDead = true
				if P3isDead then
					Multi2.MarkFailedObjective(WinGameP3)
					Multi3.MarkFailedObjective(WinGameP4)
					Multi0.MarkCompletedObjective(WinGameP1)
					Multi1.MarkCompletedObjective(WinGameP2)
				end
			end
		end)
		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("Multi1")
		if Multi0 == nil then
			Multi0 = Player.GetPlayer("Creeps")
		end
	end
	allactors = Multi0.GetActors()
	for i,v in pairs(allactors) do
		if v.Type == "mcv" then
			v.Destroy()
		end
	end
	Multi1 = Player.GetPlayer("Multi1")
	if Multi1 == nil then
		Multi1 = Player.GetPlayer("Multi0")
		if Multi1 == nil then
			Multi1 = Player.GetPlayer("Creeps")
		end
	end
	allactors = Multi1.GetActors()
	for i,v in pairs(allactors) do
		if v.Type == "mcv" then
			v.Destroy()
		end
	end
	Multi2 = Player.GetPlayer("Multi2")
	if Multi2 == nil then
		Multi2 = Player.GetPlayer("Multi3")
		if Multi2 == nil then
			Multi2 = Player.GetPlayer("Creeps")
		end
	end
	allactors = Multi2.GetActors()
	for i,v in pairs(allactors) do
		if v.Type == "mcv" then
			v.Destroy()
		end
	end
	Multi3 = Player.GetPlayer("Multi3")
	if Multi3 == nil then
		Multi3 = Player.GetPlayer("Multi2")
		if Multi3 == nil then
			Multi3 = Player.GetPlayer("Creeps")
		end
	end
	allactors = Multi3.GetActors()
	for i,v in pairs(allactors) do
		if v.Type == "mcv" then
			v.Destroy()
		end
	end
	AllPlayers = { Multi0, Multi1, Multi2, Multi3 }
end
 
MakeBattleGround1 = function()
	Positions = {
		maptile_3 = { CPos.New(30,36), CPos.New(52,1), CPos.New(48,1), CPos.New(48,4), CPos.New(28,36), CPos.New(24,16), CPos.New(1,48), CPos.New(4,48), CPos.New(1,52), CPos.New(5,52), CPos.New(8,52), CPos.New(44,0) } ,
		
		maptile_4 = { CPos.New(44,52), CPos.New(40,52), CPos.New(36,52), CPos.New(32,52), CPos.New(28,52), CPos.New(24,52), CPos.New(20,52), CPos.New(16,52), CPos.New(24,12) } ,
	
		maptile_7 = { CPos.New(36,40), CPos.New(40,36), CPos.New(12,16), CPos.New(16,12), CPos.New(36,4), CPos.New(32,4), CPos.New(28,4), CPos.New(24,4), CPos.New(20,4), CPos.New(16,4), CPos.New(12,4), CPos.New(8,4), CPos.New(44,48), CPos.New(40,48), CPos.New(36,48), CPos.New(28,48), CPos.New(24,48), CPos.New(20,48), CPos.New(16,48), CPos.New(4,36), CPos.New(16,36), CPos.New(12,36), CPos.New(8,32), CPos.New(48,44), CPos.New(4,8), CPos.New(32,12), CPos.New(32,16), CPos.New(32,20), CPos.New(40,8), CPos.New(44,12), CPos.New(8,40), CPos.New(12,44), CPos.New(20,32), CPos.New(20,36), CPos.New(20,40), CPos.New(32,48), CPos.New(48,16), CPos.New(44,20), CPos.New(40,16), CPos.New(36,16) },
	
		maptile_8 = { CPos.New(8,0), CPos.New(28,40), CPos.New(36,0), CPos.New(32,0), CPos.New(28,0), CPos.New(24,0), CPos.New(20,0), CPos.New(16,0), CPos.New(12,0) } ,
	
		maptile_9 = { CPos.New(40,28), CPos.New(0,8), CPos.New(0,12), CPos.New(0,16), CPos.New(0,20), CPos.New(0,24), CPos.New(0,28), CPos.New(0,32), CPos.New(0,36) } ,
	
		maptile_10 = { CPos.New(12,24), CPos.New(52,44), CPos.New(52,40), CPos.New(52,36), CPos.New(52,32), CPos.New(52,28), CPos.New(52,24), CPos.New(52,20), CPos.New(52,16) } ,
	
		maptile_13 = { CPos.New(44, 16), CPos.New(40, 12), CPos.New(36, 12), CPos.New(36, 8), CPos.New(32, 8), CPos.New(20, 44), CPos.New(16, 44), CPos.New(16, 40), CPos.New(12, 40), CPos.New(8, 36), CPos.New(4, 32), CPos.New(4, 28), CPos.New(8, 28), CPos.New(8, 24), CPos.New(4, 24), CPos.New(4, 20), CPos.New(8, 20), CPos.New(48, 20), CPos.New(44, 24), CPos.New(48, 24), CPos.New(44, 28), CPos.New(48, 28), CPos.New(44, 32), CPos.New(48, 32), CPos.New(44, 36), CPos.New(48, 36), CPos.New(44, 40), CPos.New(48, 40), CPos.New(44, 44), CPos.New(8, 8), CPos.New(12, 8), CPos.New(16, 8), CPos.New(8, 12), CPos.New(8, 16), CPos.New(4, 16), CPos.New(4, 12), CPos.New(36, 44), CPos.New(40, 44), CPos.New(20, 8), CPos.New(24, 8), CPos.New(28, 8), CPos.New(24, 44), CPos.New(28, 44), CPos.New(32, 44) } ,
	 }
	newactor = Actor.Create("maptile1", true, { Location = CPos.New(40,40), Owner = France })
	newactor = Actor.Create("maptile1", true, { Location = CPos.New(12,12), Owner = France })
	newactor = Actor.Create("maptile2", true, { Location = CPos.New(24,28), Owner = France })
	newactor = Actor.Create("maptile2", true, { Location = CPos.New(28,24), Owner = France })
	for i,v in pairs(Positions.maptile_3) do
		Actor.Create('maptile3', true, {
			Owner = France,
			Location = v
		})
	end
	for i,v in pairs(Positions.maptile_4) do
		Actor.Create('maptile4', true, {
			Owner = France,
			Location = v
		})
	end
	newactor = Actor.Create("maptile5", true, { Location = CPos.New(32,24), Owner = France })
	newactor = Actor.Create("maptile5", true, { Location = CPos.New(28,28), Owner = France })
	newactor = Actor.Create("maptile5", true, { Location = CPos.New(36,20), Owner = France })
	newactor = Actor.Create("maptile6", true, { Location = CPos.New(8,44), Owner = France })
	newactor = Actor.Create("maptile6", true, { Location = CPos.New(12,48), Owner = France })
	for i,v in pairs(Positions.maptile_7) do
		Actor.Create('maptile7', true, {
			Owner = France,
			Location = v
		})
	end
	for i,v in pairs(Positions.maptile_8) do
		Actor.Create('maptile8', true, {
			Owner = France,
			Location = v
		})
	end
	for i,v in pairs(Positions.maptile_9) do
		Actor.Create('maptile9', true, {
			Owner = France,
			Location = v
		})
	end
	for i,v in pairs(Positions.maptile_10) do
		Actor.Create('maptile10', true, {
			Owner = France,
			Location = v
		})
	end
	newactor = Actor.Create("maptile12", true, { Location = CPos.New(36,28), Owner = France })
	newactor = Actor.Create("maptile12", true, { Location = CPos.New(16,24), Owner = France })
	for i,v in pairs(Positions.maptile_13) do
		Actor.Create('maptile13', true, {
			Owner = France,
			Location = v
		})
	end

	newactor = Actor.Create("maptile15", true, { Location = CPos.New(0,40), Owner = France })
	newactor = Actor.Create("maptile16", true, { Location = CPos.New(48,48), Owner = France })
	newactor = Actor.Create("maptile16", true, { Location = CPos.New(16,16), Owner = France })
	newactor = Actor.Create("maptile17", true, { Location = CPos.New(48,8), Owner = France })
	newactor = Actor.Create("maptile18", true, { Location = CPos.New(0,0), Owner = France })
	newactor = Actor.Create("maptile18", true, { Location = CPos.New(32,32), Owner = France })
	newactor = Actor.Create("maptile19", true, { Location = CPos.New(40,32), Owner = France })
	newactor = Actor.Create("maptile19", true, { Location = CPos.New(28,20), Owner = France })
	newactor = Actor.Create("maptile19", true, { Location = CPos.New(32,40), Owner = France })
	newactor = Actor.Create("maptile21", true, { Location = CPos.New(20,12), Owner = France })
	newactor = Actor.Create("maptile21", true, { Location = CPos.New(12,20), Owner = France })
	newactor = Actor.Create("maptile21", true, { Location = CPos.New(24,32), Owner = France })
	newactor = Actor.Create("maptile24", true, { Location = CPos.New(44,4), Owner = France })
	newactor = Actor.Create("maptile24", true, { Location = CPos.New(40,0), Owner = France })
	newactor = Actor.Create("maptile25", true, { Location = CPos.New(20,24), Owner = France })
	newactor = Actor.Create("maptile25", true, { Location = CPos.New(24,20), Owner = France })
	newactor = Actor.Create("maptile25", true, { Location = CPos.New(16,28), Owner = France })
	newactor = Actor.Create("maptile26", true, { Location = CPos.New(24,36), Owner = France })
	newactor = Actor.Create("maptile26", true, { Location = CPos.New(12,28), Owner = France })
	newactor = Actor.Create("maptile28", true, { Location = CPos.New(28,12), Owner = France })
	newactor = Actor.Create("maptile28", true, { Location = CPos.New(40,20), Owner = France })
end
countHF1 = 0
countVF1 = 0
countHF2 = 0
countVF2 = 0
MakeBattleGround3 = function()
	if countHF1 < 1 then
		newactor = Actor.Create("maptile18", true, { Location = CPos.New(0,0), Owner = France })
		newactor = Actor.Create("maptile17", true, { Location = CPos.New(48,0), Owner = France })
		newactor = Actor.Create("maptile15", true, { Location = CPos.New(0,48), Owner = France })
		newactor = Actor.Create("maptile16", true, { Location = CPos.New(48,48), Owner = France })
		newactor = Actor.Create("maptile3", true, { Location = CPos.New(24,24), Owner = France })
		newactor = Actor.Create("maptile3", true, { Location = CPos.New(24,28), Owner = France })
		newactor = Actor.Create("maptile3", true, { Location = CPos.New(28,24), Owner = France })
		newactor = Actor.Create("maptile3", true, { Location = CPos.New(28,28), Owner = France })
	end
	if countVF2 < 35 then
		newactor = Actor.Create("maptile21", true, { Location = CPos.New(8,8) + CVec.New(countHF2, countVF2), Owner = France })
		newactor = Actor.Create("maptile22", true, { Location = CPos.New(12,8) + CVec.New(countHF2, countVF2), Owner = France })
		newactor = Actor.Create("maptile20", true, { Location = CPos.New(8,12) + CVec.New(countHF2, countVF2), Owner = France })
		newactor = Actor.Create("maptile19", true, { Location = CPos.New(12,12) + CVec.New(countHF2, countVF2), Owner = France })
		countVF2 = countVF2 + 16
	elseif countHF2 < 35 then
		countVF2 = 0
		countHF2 = countHF2 + 16
	end
	if countHF1 < 38 then
		newactor = Actor.Create("maptile8", true, { Location = CPos.New(8,0) + CVec.New(countHF1, countVF1), Owner = France })
		newactor = Actor.Create("maptile7", true, { Location = CPos.New(8,4) + CVec.New(countHF1, countVF1), Owner = France })
		newactor = Actor.Create("maptile7", true, { Location = CPos.New(8,48) + CVec.New(countHF1, countVF1), Owner = France })
		newactor = Actor.Create("maptile13", true, { Location = CPos.New(8,16) + CVec.New(countHF1, countVF1), Owner = France })
		newactor = Actor.Create("maptile13", true, { Location = CPos.New(8,20) + CVec.New(countHF1, countVF1), Owner = France })
		newactor = Actor.Create("maptile13", true, { Location = CPos.New(8,32) + CVec.New(countHF1, countVF1), Owner = France })
		newactor = Actor.Create("maptile13", true, { Location = CPos.New(8,36) + CVec.New(countHF1, countVF1), Owner = France })

		newactor = Actor.Create("maptile13", true, { Location = CPos.New(16,8) + CVec.New(countVF1, countHF1), Owner = France })
		newactor = Actor.Create("maptile13", true, { Location = CPos.New(20,8) + CVec.New(countVF1, countHF1), Owner = France })
		newactor = Actor.Create("maptile13", true, { Location = CPos.New(32,8) + CVec.New(countVF1, countHF1), Owner = France })
		newactor = Actor.Create("maptile13", true, { Location = CPos.New(36,8) + CVec.New(countVF1, countHF1), Owner = France })
		newactor = Actor.Create("maptile4", true, { Location = CPos.New(8,52) + CVec.New(countHF1, countVF1), Owner = France })
		newactor = Actor.Create("maptile9", true, { Location = CPos.New(0,8) + CVec.New(countVF1, countHF1), Owner = France })
		newactor = Actor.Create("maptile10", true, { Location = CPos.New(52,8) + CVec.New(countVF1, countHF1), Owner = France })
		newactor = Actor.Create("maptile7", true, { Location = CPos.New(4,8) + CVec.New(countVF1, countHF1), Owner = France })
		newactor = Actor.Create("maptile7", true, { Location = CPos.New(48,8) + CVec.New(countVF1, countHF1), Owner = France })
		countHF1 = countHF1 + 4
		MakeBattleGround3()
	end
end

	-- elseif countHF1 < 52 then
		-- countVF1 = 0
		-- countHF1 = countHF1 + 4
	-- end
	-- if countVF2 < 40 then
		-- newactor = Actor.Create("maptile13", true, { Location = CPos.New(8,8) + CVec.New(countHF2, countVF2), Owner = France })
		-- newactor = Actor.Create("maptile13", true, { Location = CPos.New(36,8) + CVec.New(countHF2, countVF2), Owner = France })
		-- countVF2 = countVF2 + 4
		-- BattleFloor()
	-- elseif countHF2 < 8 then
		-- countVF2 = 0
		-- countHF2 = countHF2 + 4
		-- BattleFloor()
	-- end
--end

MakeBattleGround2 = function()
	newactor = Actor.Create("maptile17", true, { Location = CPos.New(48,0), Owner = France })

	newactor = Actor.Create("maptile16", true, { Location = CPos.New(48,36), Owner = France })
	newactor = Actor.Create("maptile26", true, { Location = CPos.New(16,32), Owner = France })

	Positions = {
		-- Arrays für maptile7
		maptile_7 = {
			CPos.New(20, 4), CPos.New(24, 4), CPos.New(28, 4), CPos.New(32, 4), CPos.New(36, 4),
			CPos.New(16, 48), CPos.New(20, 48), CPos.New(24, 48), CPos.New(28, 48), CPos.New(32, 48),
			CPos.New(48, 16), CPos.New(12, 16), CPos.New(16, 12), CPos.New(40, 36), CPos.New(36, 40),
			CPos.New(12, 48), CPos.New(4, 44), CPos.New(4, 40), CPos.New(4, 36), CPos.New(4, 32),
			CPos.New(4, 28), CPos.New(4, 24), CPos.New(4, 20), CPos.New(8, 16), CPos.New(16, 16),
			CPos.New(16, 8), CPos.New(36, 44), CPos.New(44, 36), CPos.New(36, 36), CPos.New(48, 32),
			CPos.New(48, 28), CPos.New(48, 24), CPos.New(48, 20), CPos.New(16, 40), CPos.New(20, 40),
			CPos.New(24, 40), CPos.New(28, 40), CPos.New(28, 36), CPos.New(28, 32), CPos.New(32, 28),
			CPos.New(36, 28), CPos.New(40, 28), CPos.New(40, 24), CPos.New(40, 20), CPos.New(40, 16),
			CPos.New(36, 12), CPos.New(32, 12), CPos.New(28, 12), CPos.New(24, 12), CPos.New(24, 16),
			CPos.New(24, 19), CPos.New(20, 23), CPos.New(16, 23), CPos.New(12, 23), CPos.New(12, 27),
			CPos.New(12, 31), CPos.New(12, 36), CPos.New(12, 32), CPos.New(8, 48), 
			CPos.New(40, 4), CPos.New(44, 4), CPos.New(48, 8), CPos.New(48, 12)
		},
		
		-- Arrays für maptile4
		maptile_4 = {
			CPos.New(32, 52), CPos.New(28, 52), CPos.New(24, 52), CPos.New(20, 52), CPos.New(16, 52),
			CPos.New(12, 52), CPos.New(8, 52), CPos.New(32, 16), CPos.New(20, 27)
		},
		
		-- Arrays für maptile8
		maptile_8 = {
			CPos.New(20, 36), CPos.New(20, 0), CPos.New(24, 0), CPos.New(28, 0), CPos.New(32, 0),
			CPos.New(36, 0), CPos.New(40, 0), CPos.New(44, 0), CPos.New(32, 24)
		},
		
		-- Arrays für maptile10
		maptile_10 = {
			CPos.New(28, 20), CPos.New(16, 31), CPos.New(52, 16), CPos.New(52, 20), CPos.New(52, 24),
			CPos.New(52, 28), CPos.New(52, 32), CPos.New(52, 8), CPos.New(52, 12)
		},
		
		-- Arrays für maptile9
		maptile_9 = {
			CPos.New(24, 32), CPos.New(0, 36), CPos.New(0, 32), CPos.New(0, 28), CPos.New(0, 24),
			CPos.New(0, 20), CPos.New(0, 40), CPos.New(0, 44)
		},
		
		-- Arrays für maptile13
		maptile_13 = {
			CPos.New(44, 16), CPos.New(36, 8), CPos.New(32, 8), CPos.New(20, 44), CPos.New(16, 44),
			CPos.New(8, 36), CPos.New(8, 28), CPos.New(8, 24), CPos.New(8, 20), CPos.New(44, 24),
			CPos.New(44, 28), CPos.New(44, 32), CPos.New(20, 8), CPos.New(24, 8), CPos.New(28, 8),
			CPos.New(24, 44), CPos.New(28, 44), CPos.New(32, 44), CPos.New(8, 40), CPos.New(8, 44),
			CPos.New(12, 44), CPos.New(8, 32), CPos.New(12, 20), CPos.New(16, 20), CPos.New(20, 20),
			CPos.New(20, 16), CPos.New(20, 12), CPos.New(40, 8), CPos.New(44, 8), CPos.New(44, 12),
			CPos.New(44, 20), CPos.New(40, 32), CPos.New(36, 32), CPos.New(32, 32), CPos.New(32, 36),
			CPos.New(32, 40), CPos.New(12, 40), CPos.New(40, 12)
			
		},
		-- Arrays für maptile25
		maptile_25 = {
			CPos.New(8, 8), CPos.New(12, 4), CPos.New(16, 0), CPos.New(28, 24)
		},
		
		-- Arrays für maptile1
		maptile_1 = {
			CPos.New(40, 40), CPos.New(12, 12)
		},
		
		-- Arrays für nix01
		nix01 = {
			CPos.New(20, 31), CPos.New(21, 31), CPos.New(22, 31), CPos.New(23, 31), CPos.New(24, 31),
			CPos.New(25, 31), CPos.New(26, 31), CPos.New(27, 31)
		},
		
		-- Arrays für maptile12
		maptile_12 = {
			CPos.New(20, 32), CPos.New(32, 20)
		},
		
		-- Arrays für maptile3
		maptile_3 = {
			CPos.New(1, 1), CPos.New(5, 1), CPos.New(9, 1), CPos.New(12, 1), CPos.New(1, 5),
			CPos.New(5, 5), CPos.New(9, 5), CPos.New(3, 7), CPos.New(3, 8), CPos.New(4, 8),
			CPos.New(48, 44), CPos.New(52, 44), CPos.New(44, 48), CPos.New(48, 48), CPos.New(52, 48),
			CPos.New(40, 52), CPos.New(44, 52), CPos.New(48, 52), CPos.New(52, 52), CPos.New(0, 8)

		},
		maptile_5 = {
			CPos.New(24,23), CPos.New(44,40), CPos.New(40,44), CPos.New(36,48)
		}
	}
	newactor = Actor.Create("maptile15", true, { Location = CPos.New(0,48), Owner = France })
	newactor = Actor.Create("maptile28", true, { Location = CPos.New(36, 16), Owner = France })
	newactor = Actor.Create("maptile18", true, { Location = CPos.New(0, 12), Owner = France })
	newactor = Actor.Create("maptile21", true, { Location = CPos.New(28, 16), Owner = France })
	newactor = Actor.Create("maptile21", true, { Location = CPos.New(16, 27), Owner = France })
	newactor = Actor.Create("maptile19", true, { Location = CPos.New(36, 24), Owner = France })
	newactor = Actor.Create("maptile19", true, { Location = CPos.New(24, 36), Owner = France })
	for i,v in pairs(Positions.maptile_3) do
		Actor.Create('maptile3', true, {
			Owner = France,
			Location = v
		})
	end
	for i,v in pairs(Positions.maptile_25) do
		Actor.Create('maptile25', true, {
			Owner = France,
			Location = v
		})
	end
	for i,v in pairs(Positions.maptile_1) do
		Actor.Create('maptile1', true, {
			Owner = France,
			Location = v
		})
	end
	for i,v in pairs(Positions.maptile_12) do
		Actor.Create('maptile12', true, {
			Owner = France,
			Location = v
		})
	end
	for i,v in pairs(Positions.nix01) do
		Actor.Create('nix01', true, {
			Owner = France,
			Location = v
		})
	end
	for i,v in pairs(Positions.maptile_8) do
		Actor.Create('maptile8', true, {
			Owner = France,
			Location = v
		})
	end	for i,v in pairs(Positions.maptile_4) do
		Actor.Create('maptile4', true, {
			Owner = France,
			Location = v
		})
	end	for i,v in pairs(Positions.maptile_7) do
		Actor.Create('maptile7', true, {
			Owner = France,
			Location = v
		})
	end



	for i,v in pairs(Positions.maptile_10) do
		Actor.Create('maptile10', true, {
			Owner = France,
			Location = v
		})
	end
	for i,v in pairs(Positions.maptile_9) do
		Actor.Create('maptile9', true, {
			Owner = France,
			Location = v
		})
	end
	for i,v in pairs(Positions.maptile_13) do
		Actor.Create('maptile13', true, {
			Owner = France,
			Location = v
		})
	end
	for i,v in pairs(Positions.maptile_5) do
		Actor.Create('maptile5', true, {
			Owner = France,
			Location = v
		})
	end
end

MakeBattleGround31 = function()
	Positions = { 
		maptile_7 = {
		CPos.New(20, 4), CPos.New(24, 4), CPos.New(28, 4), CPos.New(32, 4), CPos.New(36, 4), 
		CPos.New(16, 48), CPos.New(20, 48), CPos.New(24, 48), CPos.New(28, 48), CPos.New(32, 48),
		CPos.New(48, 16), CPos.New(48, 8), CPos.New(44, 4), CPos.New(40, 4), CPos.New(48, 12),
		CPos.New(8, 48), CPos.New(12, 48), CPos.New(4, 44), CPos.New(4, 40), CPos.New(4, 36),
		CPos.New(4, 32), CPos.New(4, 28), CPos.New(4, 24), CPos.New(4, 20), CPos.New(48, 32),
		CPos.New(48, 28), CPos.New(48, 24), CPos.New(48, 20), CPos.New(48, 44), CPos.New(48, 40),
		CPos.New(48, 36), CPos.New(36, 48), CPos.New(40, 48), CPos.New(44, 48), CPos.New(44, 44),
		CPos.New(44, 40), CPos.New(44, 36), CPos.New(44, 32), CPos.New(44, 28), CPos.New(44, 24),
		CPos.New(44, 20), CPos.New(44, 16), CPos.New(44, 12), CPos.New(44, 8), CPos.New(44, 4),
		CPos.New(44, 0), CPos.New(44, 4), CPos.New(44, 12), CPos.New(44, 16), CPos.New(48, 8),
		CPos.New(48, 12), CPos.New(48, 16), CPos.New(48, 20), CPos.New(48, 24), CPos.New(48, 28),
		CPos.New(48, 32), CPos.New(48, 36), CPos.New(48, 40), CPos.New(48, 44), CPos.New(48, 48)
		},
	
		maptile_4 = {
			CPos.New(32, 52), CPos.New(28, 52), CPos.New(24, 52), CPos.New(20, 52), CPos.New(16, 52),
			CPos.New(12, 52), CPos.New(8, 52), CPos.New(36, 52), CPos.New(40, 52), CPos.New(44, 52),
			CPos.New(36, 40), CPos.New(40, 40), CPos.New(44, 40), CPos.New(28, 20), CPos.New(32, 20),
			CPos.New(36, 20), CPos.New(40, 20), CPos.New(44, 20), CPos.New(16, 16), CPos.New(20, 16),
			CPos.New(24, 16), CPos.New(28, 16), CPos.New(32, 16), CPos.New(36, 16), CPos.New(36, 8),
			CPos.New(40, 8), CPos.New(32, 12), CPos.New(44, 12), CPos.New(44, 8), CPos.New(40, 32)
		},
		
		maptile_8 = {
			CPos.New(20, 0), CPos.New(24, 0), CPos.New(28, 0), CPos.New(32, 0), CPos.New(36, 0),
			CPos.New(40, 0), CPos.New(44, 0), CPos.New(48, 0), CPos.New(52, 0), CPos.New(56, 0)
		},
		
		maptile_10 = {
			CPos.New(52, 16), CPos.New(52, 20), CPos.New(52, 24), CPos.New(52, 28), CPos.New(52, 32),
			CPos.New(52, 36), CPos.New(52, 40), CPos.New(52, 44), CPos.New(52, 48), CPos.New(52, 52)
		},
		
		maptile_9 = {
			CPos.New(0, 36), CPos.New(0, 32), CPos.New(0, 28), CPos.New(0, 24), CPos.New(0, 20),
			CPos.New(0, 16), CPos.New(0, 12), CPos.New(0, 8), CPos.New(0, 4), CPos.New(0, 0)
		},
		
		maptile_22 = {
			CPos.New(44, 40), CPos.New(40, 40), CPos.New(40, 44), CPos.New(44, 44), CPos.New(44, 24),
			CPos.New(44, 28), CPos.New(48, 40), CPos.New(48, 44), CPos.New(48, 28), CPos.New(48, 32)
		},
		
		maptile_21 = {
			CPos.New(40, 24), CPos.New(8, 24), CPos.New(8, 8), CPos.New(24, 8), CPos.New(40, 8),
			CPos.New(28, 8), CPos.New(40, 32), CPos.New(40, 44), CPos.New(32, 44), CPos.New(32, 8)
		},
		
		maptile_19 = {
			CPos.New(44, 28), CPos.New(44, 12), CPos.New(32, 12), CPos.New(40, 32), CPos.New(40, 28),
			CPos.New(40, 12), CPos.New(32, 36), CPos.New(28, 28), CPos.New(36, 28), CPos.New(44, 8)
		}
	}
	for i,v in pairs(Positions.maptile_19) do
		Actor.Create('maptile19', true, {
			Owner = France,
			Location = v
		})
	end
	for i,v in pairs(Positions.maptile_21) do
		Actor.Create('maptile21', true, {
			Owner = France,
			Location = v
		})
	end
	for i,v in pairs(Positions.maptile_22) do
		Actor.Create('maptile22', true, {
			Owner = France,
			Location = v
		})
	end
	for i,v in pairs(Positions.maptile_9) do
		Actor.Create('maptile9', true, {
			Owner = France,
			Location = v
		})
	end
	for i,v in pairs(Positions.maptile_10) do
		Actor.Create('maptile10', true, {
			Owner = France,
			Location = v
		})
	end
	for i,v in pairs(Positions.maptile_8) do
		Actor.Create('maptile8', true, {
			Owner = France,
			Location = v
		})
	end
	for i,v in pairs(Positions.maptile_4) do
		Actor.Create('maptile4', true, {
			Owner = France,
			Location = v
		})
	end
	for i,v in pairs(Positions.maptile_7) do
		Actor.Create('maptile7', true, {
			Owner = France,
			Location = v
		})
	end
end
LobbyOptionCheck = 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
	end
	MapNumbers = { 1, 2, 3 }
	Groundnumber = Utils.Random(MapNumbers)
	if Map.LobbyOption("map") == "random" then
		if Groundnumber == 1 then
			MakeBattleGround1()
		end
		if Groundnumber == 2 then
			MakeBattleGround2()
		end
		if Groundnumber == 3 then
			MakeBattleGround3()
		end
	elseif Map.LobbyOption("map") == "map1" then
		MakeBattleGround1()
	elseif Map.LobbyOption("map") == "map2" then
		MakeBattleGround2()
	elseif Map.LobbyOption("map") == "map3" then
		MakeBattleGround3()
	end
end
WorldLoaded = function()
	SetupPlayers()
	LobbyOptionCheck()
	InitializeGame()
--	Trigger.AfterDelay(1, function()
		InitializePlayers()
--	end)
	turnOn()
	WinGameP1 = Multi0.AddPrimaryObjective("KillEnemys!")
	WinGameP2 = Multi1.AddPrimaryObjective("KillEnemys!")
	WinGameP3 = Multi2.AddPrimaryObjective("KillEnemys!")
	WinGameP4 = Multi3.AddPrimaryObjective("KillEnemys!")
	P1isDead = false
	P2isDead = false
	P3isDead = false
	P4isDead = false
	Trigger.AfterDelay(DateTime.Seconds(3), function()

	end)
end