
EnemyunitsConditions = function()
	EnemyInfUnit = { "e1", "e3", "dog" }
	InfDelayTimemin = 1
	InfDelayTimemax = 9
	EnemyInfantryIncome()
	Trigger.AfterDelay(DateTime.Minutes(1), function()
		WaveInterrupter()
		EnemyInfUnit = { "e1", "e1", "e2", "e3", "medi", "dog" }
		InfDelayTimemin = 3
		InfDelayTimemax = 6
		if Difficulty ~= "easy" then
			EnemyMonsterUnit = { "PVICE", "BSPI" }
			MonsterDelayTimemin = 20
			MonsterDelayTimemax = 25
			EnemyMonsterIncome()
		end
	end)
	Trigger.AfterDelay(DateTime.Minutes(2), function()
		PlayNewTrack()
		EnemyInfUnit = { "e1", "e2", "e3", "e4", "medi", "medi", "dog" }
		InfDelayTimemin = 3
		InfDelayTimemax = 8
		EnemyVehUnit = { "apc", "1tnk", "jeep", "ftrk" }
		VehDelayTimemin = 10
		VehDelayTimemax = 20
		EnemyVehicleIncome()
		EnemyMonsterUnit = { "PVICE", "IMP", "BSPI" }
	end)
	Trigger.AfterDelay(DateTime.Minutes(3), function()
		EnemyInfUnit = { "e1", "e2", "e3", "e4", "medi", "medi", "dog", "mech" }
		InfDelayTimemin = 4
		InfDelayTimemax = 7
		EnemyVehUnit = { "apc", "1tnk", "1tnk", "jeep", "ftrk" }
		VehDelayTimemin = 6
		VehDelayTimemax = 12
		MonsterDelayTimemin = 15
		MonsterDelayTimemax = 20
	end)
	Trigger.AfterDelay(DateTime.Minutes(4), function()
		EnemyMonsterUnit = { "PVICE", "IMP", "REVE", "BSPI" }
		VehDelayTimemin = 5
		VehDelayTimemax = 11

	end)
	Trigger.AfterDelay(DateTime.Minutes(5), function()
		EnemyTnkUnit = { "2tnk", "3tnk" }
		TnkDelayTimemin = 18
		TnkDelayTimemax = 23
		EnemyTnkIncome()
		EnemyInfUnit = { "e1", "e1", "e1", "e2", "e3", "e4", "e2", "e3", "e4", "medi", "medi", "medi", "dog", "mech", "mech" }
		EnemyArtyUnit = { "v2rl", "arty" }
		ArtyDelayTimemin = 20
		ArtyDelayTimemax = 30
		EnemyArtyIncome()
		EnemyHeliUnit = { "hind", "hind" }
		HeliDelayTimemin = 20
		HeliDelayTimemax = 30
		EnemyHeliIncome()
		if Difficulty == "hard" then
			EnemyUFOUnit = { "ufo" }
			UFODelayTimemin = 20
			UFODelayTimemax = 30
			EnemyUFOIncome()
			HeliDelayTimemin = 15
			HeliDelayTimemax = 25
			MonsterDelayTimemin = 10
			MonsterDelayTimemax = 18
		end
	end)
	Trigger.AfterDelay(DateTime.Minutes(6), function()
		EnemyInfUnit = { "e1", "e1", "e1", "e2", "e3", "e4", "e2", "e3", "e4", "medi", "medi", "dog", "mech", "mech", "shok" }
		EnemyHeliUnit = { "heli", "hind" }

		EnemyMonsterUnit = { "PVICE", "IMP", "REVE", "BSPI", "PINKY" }
		TnkDelayTimemin = 12
		TnkDelayTimemax = 20
		ArtyDelayTimemin = 15
		ArtyDelayTimemax = 25
	end)
	Trigger.AfterDelay(DateTime.Minutes(7), function()
		EnemyInfUnit = { "e1", "e1", "e1", "e1", "e2", "e3", "e4", "medi", "medi", "dog", "mech", "mech", "shok" }
		EnemyTnkUnit = { "2tnk", "3tnk", "ctnk", "ttnk", "4tnk" }
		EnemyMonsterUnit = { "PVICE", "IMP", "REVE", "BSPI", "PINKY", "MANC" }
		EnemyTnkUnit = { "2tnk", "3tnk", "ctnk", "4tnk" }
	end)
	Trigger.AfterDelay(DateTime.Minutes(8), function()
		EnemyInfUnit = { "e1", "e1", "e1", "e1", "e2", "e3", "e2", "e3", "e4", "medi", "medi", "medi", "dog", "mech", "mech", "mech", "shok", "shok", "e7" }
		InfDelayTimemin = 3
		InfDelayTimemax = 6
		EnemyTnkUnit = { "2tnk", "3tnk", "ctnk", "ttnk", "4tnk", "4tnk" }
		if Difficulty ~= "easy" then
			Demotruckincome()
		end
	end)
	Trigger.AfterDelay(DateTime.Minutes(9), function()
		EnemyVehUnit = { "apc", "1tnk", "1tnk", "jeep", "ftrk", "ftrk", "ftrk", "ftrk" }
		VehDelayTimemin = 4
		VehDelayTimemax = 5
	end)
	Trigger.AfterDelay(DateTime.Minutes(10), function()
	Media.PlayMusic("benzin")
	Demotruckincome()
	end)
end

StartingUnitsCheck = function()
	Utils.Do(Humans, function(player)
		Dog = player.GetActorsByType("dog")
		for p,doggy in pairs(Dog) do
			if doggy.IsInWorld then
				WaveInterval = 0
			end
		end
		E1 = player.GetActorsByType("e1")
		for p,rifleman in pairs(E1) do
			if rifleman.IsInWorld then
				WaveInterval = 1
			end
		end
		E2 = player.GetActorsByType("e2")
		for p,nader in pairs(E2) do
			if nader.IsInWorld then
				WaveInterval = 2
			end
		end
		E3 = player.GetActorsByType("e3")
		for p,rocketman in pairs(E3) do
			if rocketman.IsInWorld then
				WaveInterval = 3
			end
		end
		Medi = player.GetActorsByType("medi")
		for p,medic in pairs(Medi) do
			if medic.IsInWorld then
				WaveInterval = 4
			end
		end
		Mech = player.GetActorsByType("mech")
		for p,mechanic in pairs(Mech) do
			if mechanic.IsInWorld then
				WaveInterval = 5
			end
		end
		E6 = player.GetActorsByType("e6")
		for p,engie in pairs(E6) do
			if engie.IsInWorld then
				WaveInterval = 6
			end
		end
		UnitPercent = 10 - WaveInterval
	end)
end

WaveInterrupter = function()
	PeaceTime = true
	Trigger.AfterDelay(DateTime.Seconds(WaveInterval), function()
		PeaceTime = false
	end)
	Trigger.AfterDelay(DateTime.Seconds(10), function()
		WaveInterrupter()
	end)
end

-- 8,24  8,39  8,50  8,65  8,90

EnemyInfantryIncome = function()
	Attackpath0 = { CPos.New(8,22) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,91) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath1 = { CPos.New(8,39) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,22) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,91) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath2 = { CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,22) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,91) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath3 = { CPos.New(8,65) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,91) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,22) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath4 = { CPos.New(8,91) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,22) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	RandomUnit = Utils.Random(EnemyInfUnit)
	if Multi0 ~= nil and not PeaceTime then
		EnemyUnit0 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,7) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy0 = { EnemyUnit0 }
		GroupPatrol(Enemy0, Attackpath0, DateTime.Seconds(0))
	end
	if Multi1 ~= nil and not PeaceTime then
		EnemyUnit1 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,33) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy1 = { EnemyUnit1 }
		GroupPatrol(Enemy1, Attackpath1, DateTime.Seconds(0))
	end
	if Multi2 ~= nil and not PeaceTime then
		EnemyUnit2 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,44) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy2 = { EnemyUnit2 }
		GroupPatrol(Enemy2, Attackpath2, DateTime.Seconds(0))
	end
	if Multi3 ~= nil and not PeaceTime then
		EnemyUnit3 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,59) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy3 = { EnemyUnit3 }
		GroupPatrol(Enemy3, Attackpath3, DateTime.Seconds(0))
	end
	if Multi4 ~= nil and not PeaceTime then
		EnemyUnit4 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,93) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy4 = { EnemyUnit4 }
		GroupPatrol(Enemy4, Attackpath4, DateTime.Seconds(0))
	end
	Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(InfDelayTimemin, InfDelayTimemax)), function()
		EnemyInfantryIncome()
	end)
end

EnemyVehicleIncome = function()
	Attackpath0 = { CPos.New(8,22) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,91) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath1 = { CPos.New(8,39) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,22) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,91) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath2 = { CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,22) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,91) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath3 = { CPos.New(8,65) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,91) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,22) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath4 = { CPos.New(8,91) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,22) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	RandomUnit = Utils.Random(EnemyVehUnit)
	if Multi0 ~= nil and not PeaceTime then
		EnemyUnit0 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,7) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy0 = { EnemyUnit0 }
		GroupPatrol(Enemy0, Attackpath0, DateTime.Seconds(0))
	end
	if Multi1 ~= nil and not PeaceTime then
		EnemyUnit1 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,33) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy1 = { EnemyUnit1 }
		GroupPatrol(Enemy1, Attackpath1, DateTime.Seconds(0))
	end
	if Multi2 ~= nil and not PeaceTime then
		EnemyUnit2 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,44) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy2 = { EnemyUnit2 }
		GroupPatrol(Enemy2, Attackpath2, DateTime.Seconds(0))
	end
	if Multi3 ~= nil and not PeaceTime then
		EnemyUnit3 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,59) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy3 = { EnemyUnit3 }
		GroupPatrol(Enemy3, Attackpath3, DateTime.Seconds(0))
	end
	if Multi4 ~= nil and not PeaceTime then
		EnemyUnit4 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,93) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy4 = { EnemyUnit4 }
		GroupPatrol(Enemy4, Attackpath4, DateTime.Seconds(0))
	end
	Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(VehDelayTimemin, VehDelayTimemax)), function()
		EnemyVehicleIncome()
	end)
end

EnemyTnkIncome = function()
	Attackpath0 = { CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath1 = { CPos.New(8,39) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath2 = { CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath3 = { CPos.New(8,65) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath4 = { CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	RandomUnit = Utils.Random(EnemyTnkUnit)
	if Multi0 ~= nil and not PeaceTime then
		EnemyUnit0 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,7) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy0 = { EnemyUnit0 }
		GroupPatrol(Enemy0, Attackpath0, DateTime.Seconds(0))
	end
	if Multi1 ~= nil and not PeaceTime then
		EnemyUnit1 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,33) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy1 = { EnemyUnit1 }
		GroupPatrol(Enemy1, Attackpath1, DateTime.Seconds(0))
	end
	if Multi2 ~= nil and not PeaceTime then
		EnemyUnit2 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,44) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy2 = { EnemyUnit2 }
		GroupPatrol(Enemy2, Attackpath2, DateTime.Seconds(0))
	end
	if Multi3 ~= nil and not PeaceTime then
		EnemyUnit3 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,59) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy3 = { EnemyUnit3 }
		GroupPatrol(Enemy3, Attackpath3, DateTime.Seconds(0))
	end
	if Multi4 ~= nil and not PeaceTime then
		EnemyUnit4 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,93) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy4 = { EnemyUnit4 }
		GroupPatrol(Enemy4, Attackpath4, DateTime.Seconds(0))
	end
	Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(TnkDelayTimemin, TnkDelayTimemax)), function()
		EnemyTnkIncome()
	end)
end

EnemyArtyIncome = function()
	Attackpath0 = { CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath1 = { CPos.New(8,39) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath2 = { CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath3 = { CPos.New(8,65) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath4 = { CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	RandomUnit = Utils.Random(EnemyArtyUnit)
	if Multi0 ~= nil and not PeaceTime then
		EnemyUnit0 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,7) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy0 = { EnemyUnit0 }
		GroupPatrol(Enemy0, Attackpath0, DateTime.Seconds(0))
	end
	if Multi1 ~= nil and not PeaceTime then
		EnemyUnit1 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,33) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy1 = { EnemyUnit1 }
		GroupPatrol(Enemy1, Attackpath1, DateTime.Seconds(0))
	end
	if Multi2 ~= nil and not PeaceTime then
		EnemyUnit2 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,44) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy2 = { EnemyUnit2 }
		GroupPatrol(Enemy2, Attackpath2, DateTime.Seconds(0))
	end
	if Multi3 ~= nil and not PeaceTime then
		EnemyUnit3 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,59) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy3 = { EnemyUnit3 }
		GroupPatrol(Enemy3, Attackpath3, DateTime.Seconds(0))
	end
	if Multi4 ~= nil and not PeaceTime then
		EnemyUnit4 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,93) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy4 = { EnemyUnit4 }
		GroupPatrol(Enemy4, Attackpath4, DateTime.Seconds(0))
	end
	Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(ArtyDelayTimemin, ArtyDelayTimemax)), function()
		EnemyArtyIncome()
	end)
end

EnemyHeliIncome = function()
	Attackpath0 = { CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath1 = { CPos.New(8,39) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath2 = { CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath3 = { CPos.New(8,65) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath4 = { CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	RandomUnit = Utils.Random(EnemyHeliUnit)
	if Multi0 ~= nil and not PeaceTime then
		EnemyUnit0 = Actor.Create(RandomUnit, true, { Location = CPos.New(152,7) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy0 = { EnemyUnit0 }
		GroupPatrol(Enemy0, Attackpath0, DateTime.Seconds(0))
	end
	if Multi1 ~= nil and not PeaceTime then
		EnemyUnit1 = Actor.Create(RandomUnit, true, { Location = CPos.New(152,33) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy1 = { EnemyUnit1 }
		GroupPatrol(Enemy1, Attackpath1, DateTime.Seconds(0))
	end
	if Multi2 ~= nil and not PeaceTime then
		EnemyUnit2 = Actor.Create(RandomUnit, true, { Location = CPos.New(152,44) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy2 = { EnemyUnit2 }
		GroupPatrol(Enemy2, Attackpath2, DateTime.Seconds(0))
	end
	if Multi3 ~= nil and not PeaceTime then
		EnemyUnit3 = Actor.Create(RandomUnit, true, { Location = CPos.New(152,59) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy3 = { EnemyUnit3 }
		GroupPatrol(Enemy3, Attackpath3, DateTime.Seconds(0))
	end
	if Multi4 ~= nil and not PeaceTime then
		EnemyUnit4 = Actor.Create(RandomUnit, true, { Location = CPos.New(152,93) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy4 = { EnemyUnit4 }
		GroupPatrol(Enemy4, Attackpath4, DateTime.Seconds(0))
	end
	Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(HeliDelayTimemin, HeliDelayTimemax)), function()
		EnemyHeliIncome()
	end)
end

EnemyMonsterIncome = function()
	Attackpath0 = { CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath1 = { CPos.New(8,39) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath2 = { CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath3 = { CPos.New(8,65) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath4 = { CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	RandomUnit = Utils.Random(EnemyMonsterUnit)
	if Multi0 ~= nil and not PeaceTime then
		EnemyUnit0 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,7) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy0 = { EnemyUnit0 }
		GroupPatrol(Enemy0, Attackpath0, DateTime.Seconds(0))
	end
	if Multi1 ~= nil and not PeaceTime then
		EnemyUnit1 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,33) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy1 = { EnemyUnit1 }
		GroupPatrol(Enemy1, Attackpath1, DateTime.Seconds(0))
	end
	if Multi2 ~= nil and not PeaceTime then
		EnemyUnit2 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,44) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy2 = { EnemyUnit2 }
		GroupPatrol(Enemy2, Attackpath2, DateTime.Seconds(0))
	end
	if Multi3 ~= nil and not PeaceTime then
		EnemyUnit3 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,59) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy3 = { EnemyUnit3 }
		GroupPatrol(Enemy3, Attackpath3, DateTime.Seconds(0))
	end
	if Multi4 ~= nil and not PeaceTime then
		EnemyUnit4 = Actor.Create(RandomUnit, true, { Location = CPos.New(150,93) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy4 = { EnemyUnit4 }
		GroupPatrol(Enemy4, Attackpath4, DateTime.Seconds(0))
	end
	Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(MonsterDelayTimemin, MonsterDelayTimemax)), function()
		EnemyMonsterIncome()
	end)
end

EnemyUFOIncome = function()
	Attackpath0 = { CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath1 = { CPos.New(8,39) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath2 = { CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath3 = { CPos.New(8,65) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath4 = { CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	RandomUnit = Utils.Random(EnemyUFOUnit)
	if Multi0 ~= nil and not PeaceTime then
		EnemyUnit0 = Actor.Create(RandomUnit, true, { Location = CPos.New(152,7) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy0 = { EnemyUnit0 }
		GroupPatrol(Enemy0, Attackpath0, DateTime.Seconds(0))
	end
	if Multi1 ~= nil and not PeaceTime then
		EnemyUnit1 = Actor.Create(RandomUnit, true, { Location = CPos.New(152,33) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy1 = { EnemyUnit1 }
		GroupPatrol(Enemy1, Attackpath1, DateTime.Seconds(0))
	end
	if Multi2 ~= nil and not PeaceTime then
		EnemyUnit2 = Actor.Create(RandomUnit, true, { Location = CPos.New(152,44) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy2 = { EnemyUnit2 }
		GroupPatrol(Enemy2, Attackpath2, DateTime.Seconds(0))
	end
	if Multi3 ~= nil and not PeaceTime then
		EnemyUnit3 = Actor.Create(RandomUnit, true, { Location = CPos.New(152,59) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy3 = { EnemyUnit3 }
		GroupPatrol(Enemy3, Attackpath3, DateTime.Seconds(0))
	end
	if Multi4 ~= nil and not PeaceTime then
		EnemyUnit4 = Actor.Create(RandomUnit, true, { Location = CPos.New(152,93) + CVec.New(0, Utils.RandomInteger(-3, 3)), Owner = Enemys })
		Enemy4 = { EnemyUnit4 }
		GroupPatrol(Enemy4, Attackpath4, DateTime.Seconds(0))
	end
	Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(UFODelayTimemin, UFODelayTimemax)), function()
		EnemyUFOIncome()
	end)
end

Demotruckincome = function()
	Attackpath0 = { CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath1 = { CPos.New(8,39) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath2 = { CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath3 = { CPos.New(8,65) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }

	Attackpath4 = { CPos.New(8,90) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,24) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)), CPos.New(8,50) + CVec.New(Utils.RandomInteger(-3, 3), Utils.RandomInteger(-3, 3)) }
	Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(45, 90)), function()
		if Multi0 ~= nil and not PeaceTime then
			Demo0 = Actor.Create("dtrk", true, { Location = CPos.New(150,7), Owner = Enemys })
			Dtruk0 = { Demo0 }
			MovePatrol(Dtruk0, Attackpath0, DateTime.Seconds(0))
		end
		if Multi1 ~= nil and not PeaceTime then
			Demo1 = Actor.Create("dtrk", true, { Location = CPos.New(150,33), Owner = Enemys })
			Dtruk1 = { Demo1 }
			MovePatrol(Dtruk1, Attackpath1, DateTime.Seconds(0))
		end
		if Multi2 ~= nil and not PeaceTime then
			Demo2 = Actor.Create("dtrk", true, { Location = CPos.New(150,44), Owner = Enemys })
			Dtruk2 = { Demo2 }
			MovePatrol(Dtruk2, Attackpath2, DateTime.Seconds(0))
		end
		if Multi3 ~= nil and not PeaceTime then
			Demo3 = Actor.Create("dtrk", true, { Location = CPos.New(150,59), Owner = Enemys })
			Dtruk3 = { Demo3 }
			MovePatrol(Dtruk3, Attackpath3, DateTime.Seconds(0))
		end
		if Multi4 ~= nil and not PeaceTime then
			Demo4 = Actor.Create("dtrk", true, { Location = CPos.New(150,93), Owner = Enemys })
			Dtruk4 = { Demo4 }
			MovePatrol(Dtruk4, Attackpath4, DateTime.Seconds(0))
		end
		Demotruckincome()
	end)
end


GroupPatrol = function(units, waypoints, delay) 
	local i = 1
	local stop = false
	Utils.Do(units, function(unit)
		Trigger.OnIdle(unit, function()
			if stop then
				return
			end
			if unit.Location == waypoints[i] then
				local bool = Utils.All(units, function(actor) return actor.IsIdle end)
				if bool then
					stop = true
					i = i + 1
					if i > #waypoints then
						i = 1
					end
					Trigger.AfterDelay(delay, function() stop = false end)
				end
			else
				unit.AttackMove(waypoints[i])
			end
		end)
	end)
end

MovePatrol = function(units, waypoints, delay) 
	local i = 1
	local stop = false
	Utils.Do(units, function(unit)
		Trigger.OnIdle(unit, function()
			if stop then
				return
			end
			if unit.Location == waypoints[i] then
				local bool = Utils.All(units, function(actor) return actor.IsIdle end)
				if bool then
					stop = true
					i = i + 1
					if i > #waypoints then
						i = 1
					end
					Trigger.AfterDelay(delay, function() stop = false end)
				end
			else
				unit.Move(waypoints[i])
			end
		end)
	end)
end


RateOfIncrease = 0.001

turnOn = function()
	Lighting.Ambient = Light
	if Light < BrightestLight then
		Light = Light + RateOfIncrease
		Trigger.AfterDelay(BrightnessChangeSpeed, function()
			turnOn()
		end)
	
	else
		NoturnOn = true
		Trigger.AfterDelay(DayDuration, function()
			turnOff()
		end)
	end
end

turnOff = function()
	Lighting.Ambient = Light
	if Light < 0.66 then
		Media.PlayMusic("quake")
	end
	if Light < 0.62 then
		Thunderstorm = true
	end
	if Light > DarkestLight then
		Light = Light - 0.001
		Trigger.AfterDelay(BrightnessChangeSpeed, function()
			turnOff()
		end)	
	
	else
		Trigger.AfterDelay(NightDuration, function()
			NoturnOn = false
			Thunderstorm = false
			turnOn()
			Trigger.AfterDelay(DateTime.Seconds(15), function()
				PlayNewTrack()		
			end)
		end)
	end
end

Crossover = { "drown1.wav", "drown2.wav", "drown3.wav" }
MediumMusic = { "doomsgate", "amigo" }
HardMusic = { "quake", "benzin", "doomsgate", "amigo" }
RepeatTimes = 0

PlayNewTrack = function()
	RandomCrossover = Utils.Random(Crossover)
	Media.PlaySound(RandomCrossover)
	Trigger.AfterDelay(DateTime.Seconds(1), function()
		Media.StopMusic()
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			if Difficulty == "easy" then
				if RepeatTimes == 0 then
					Media.PlayMusic("doomsgate")
					RepeatTimes = RepeatTimes + 1
				else
					Media.PlayMusic()
				end
			end
			if Difficulty == "normal" then
				if RepeatTimes == 0 then
					RandomMediumSound = Utils.Random(MediumMusic)
					Media.PlayMusic(RandomMediumSound)
					RepeatTimes = RepeatTimes + 1
				elseif RepeatTimes == 1 then
					RandomHardSound = Utils.Random(HardMusic)
					Media.PlayMusic(RandomHardSound)
					RepeatTimes = RepeatTimes + 1
				else
					Media.PlayMusic()
				end
			end
			if Difficulty == "hard" then
				RandomHardSound = Utils.Random(HardMusic)
				Media.PlayMusic(RandomHardSound)
			else
				Trigger.AfterDelay(DateTime.Minutes(3), function()
					PlayNewTrack()
				end)
			end
		end)
	end)
end

Difficulty = Map .LobbyOption("difficulty")

DifficultiesCondition = function()
	if Difficulty == "easy" then
		TimerColor = HSLColor.Red
		NameColor = HSLColor.LawnGreen
		Light = 1
		Media.DisplayMessage("Difficulty: Easy\n" .. UnitPercent .. "0% Enemy units income.\n\n\n\n\nHF newbie\nYou can adjust the enemys strength in the lobby settings\n40% is easier and 100% the full enemy strength.", "Materia", NameColor)
		Trigger.AfterDelay(DateTime.Seconds(3), function()
			Media.DisplayMessage("Defend our bases and give your best in destroying the enemys strctures", "Materia", NameColor)
			Trigger.AfterDelay(DateTime.Seconds(5), function()
				Media.DisplayMessage("You need no engie to capture the oilderricks just get a unit next to the flag", "Materia", NameColor)
				Trigger.AfterDelay(DateTime.Seconds(5), function()
					Media.DisplayMessage("You can build buildings at the flags", "Materia", NameColor)
					Trigger.AfterDelay(DateTime.Seconds(5), function()
						Media.DisplayMessage("All superweapon and other chargedurations are halfed!", "Materia", NameColor)
						Trigger.AfterDelay(DateTime.Seconds(5), function()
							Media.DisplayMessage("Allies have the Cobra and Soviets a Nukesub as new techunits, you maybe need them to defeat the enemy!", "Materia", NameColor)
							Trigger.AfterDelay(DateTime.Seconds(5), function()
								Media.DisplayMessage("Good luck and have fun", "Materia", NameColor)
							end)
						end)
					end)
				end)
			end)
		end)
	end

	if Difficulty == "normal" then
		TimerColor = HSLColor.White
		NameColor = HSLColor.Orange
		Light = 0.9
		BrightnessChangeSpeed = 8
		BrightestLight = 1.01
		DarkestLight = 0.69
		DayDuration = 7000
		NightDuration = 1500
		turnOn()
		Media.DisplayMessage("Difficulty: Medium\n" .. UnitPercent .. "0% Enemy units income.\n\n\n\n\nGL\nHF", "Materia", NameColor)
		Trigger.AfterDelay(DateTime.Seconds(3), function()
			Media.DisplayMessage("Commanders defend The Mainbases against the enemy forces.\nBuild enough defenses to save our base", "Materia", NameColor)
		end)
	end
	if Difficulty == "hard" then
		TimerColor = HSLColor.Gray
		NameColor = HSLColor.Red
		Light = 0.77
		BrightnessChangeSpeed = 5
		BrightestLight = 0.93
		DarkestLight = 0.52
		DayDuration = 5000
		NightDuration = 3000
		turnOn()
		Media.DisplayMessage("Difficulty: Hard\n" .. UnitPercent .. "0% Enemy units income.\n\n\n\n\nGL\nHF", "Materia", NameColor)
		Trigger.AfterDelay(DateTime.Seconds(3), function()
			Media.DisplayMessage("I guess no description is needed on your skilllevel ;D", "Materia", NameColor)
		end)
	end
end

WorldLoaded = function()
	SetupPlayers()
	StartingUnitsCheck()
	PlayerConquestConditions()
	EnemysBaseMaker()
	EnemyunitsConditions()
	GetandsetFlags()
	DifficultiesCondition()
end

Tick = function()
	UserInterface.SetMissionText("" .. (DestroyedBuildings) .. " / " .. (EnemyBuildingCounter) .. " Enemy buildings destroyed", TimerColor)
	if DestroyedBuildings == EnemyBuildingCounter then
		if not Goodwon then
			Goodwon = true
			PlayersWin()
		end
	end
	if Thunderstorm then
		if (Utils.RandomInteger(1, 200) == 10) then
			local delay = Utils.RandomInteger(1, 10)
			Lighting.Flash("LightningStrike", delay)
			Trigger.AfterDelay(delay, function()
				Media.PlaySound("thunder" .. Utils.RandomInteger(1,6) .. ".aud")
			end)
		end
		if (Utils.RandomInteger(1, 200) == 10) then
			Media.PlaySound("thunder-ambient.aud")
		end
	end
end
