Spies = { }

LeftIXes = 3

CarryallTimer = { }
CarryallTimer["easy"] = DateTime.Minutes(3)
CarryallTimer["normal"] = DateTime.Minutes(3) + DateTime.Seconds(45)
CarryallTimer["hard"] = DateTime.Minutes(4) + DateTime.Seconds(30)

CarryallPath = { CarryallEntry.Location, CarryallRally.Location }
Carryall = { "carryall" }
Harvester = { "harvester.starport" }

Upgrades = { "upgrade.barracks", "upgrade.light", "upgrade.conyard", "upgrade.heavy", "upgrade.hightech" }
Cargos = { "cargo_vehicle", "cargo_vehicle", "cargo_vehicle" }

AlliedDefences = { AllGunt1, AllGunt2, AllPillBox }
SovietUnits = { SovTank1, SovTank2, SovE11, SovE12, SovE13, SovE14, SovE3 }

Reinforcementses =
{
	Harkonnen =
	{
		easy =
		{
			{ "combat_tank_h", "trike", "quad", "quad" },
			{ "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf" },
			{ "sardaukar", "sardaukar", "trooper", "trike", "missile_tank", "missile_tank" },
			{ "trike", "trike", "trike", "trike", "trike" },
			{ "combat_tank_h", "combat_tank_h", "combat_tank_h", "devastator" },
			{ "trooper", "trooper", "trooper", "trooper", "trooper" },
			{ "devastator", "combat_tank_h", "missile_tank", "missile_tank" },
			{ "sardaukar", "sardaukar", "sardaukar", "sardaukar", "sardaukar", "sardaukar", "sardaukar", "sardaukar" },
			{ "combat_tank_h", "siege_tank", "siege_tank" }
		},
		normal =
		{
			{ "combat_tank_h", "combat_tank_h", "trike", "trike", "quad", "quad" },
			{ "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf" },
			{ "sardaukar", "sardaukar", "sardaukar", "trooper", "trooper", "trike", "trike", "missile_tank", "missile_tank" },
			{ "trike", "trike", "trike", "trike", "trike", "trike" },
			{ "combat_tank_h", "combat_tank_h", "combat_tank_h", "combat_tank_h", "devastator" },
			{ "trooper", "trooper", "trooper", "trooper", "trooper", "trooper", "trooper" },
			{ "devastator", "combat_tank_h", "combat_tank_h", "missile_tank", "missile_tank", "missile_tank" },
			{ "sardaukar", "sardaukar", "sardaukar", "sardaukar", "sardaukar", "sardaukar", "sardaukar", "sardaukar", "sardaukar", "sardaukar", "sardaukar" },
			{ "devastator", "siege_tank", "siege_tank" }
		},
		hard =
		{
			{ "combat_tank_h", "combat_tank_h", "combat_tank_h", "trike", "trike", "quad", "quad", "quad" },
			{ "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf" },
			{ "sardaukar", "sardaukar", "sardaukar", "sardaukar", "trooper", "trooper", "trike", "trike", "trike", "missile_tank", "missile_tank", "missile_tank" },
			{ "trike", "trike", "trike", "trike", "trike", "trike", "trike" },
			{ "combat_tank_h", "combat_tank_h", "combat_tank_h", "combat_tank_h", "combat_tank_h", "devastator", "devastator" },
			{ "trooper", "trooper", "trooper", "trooper", "trooper", "trooper", "trooper", "trooper", "trooper" },
			{ "devastator", "devastator", "combat_tank_h", "combat_tank_h", "missile_tank", "missile_tank", "missile_tank", "missile_tank" },
			{ "sardaukar", "sardaukar", "sardaukar", "sardaukar", "sardaukar", "sardaukar", "sardaukar", "sardaukar", "sardaukar", "sardaukar", "sardaukar", "sardaukar", "sardaukar", "sardaukar" },
			{ "devastator", "siege_tank", "siege_tank", "siege_tank" }
		}
	},
	Ordos =
	{
		easy =
		{
			{ "combat_tank_o", "raider", "quad", "quad" },
			{ "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf" },
			{ "stealth_raider", "stealth_raider", "trooper", "raider", "missile_tank", "missile_tank" },
			{ "raider", "raider", "raider", "raider" },
			{ "combat_tank_o", "combat_tank_o", "combat_tank_o", "deviator" },
			{ "trooper", "trooper", "trooper", "trooper", "trooper" },
			{ "deviator", "combat_tank_o", "missile_tank", "missile_tank" },
			{ "stealth_raider", "stealth_raider", "stealth_raider", "stealth_raider", "stealth_raider", "stealth_raider" },
			{ "combat_tank_o", "siege_tank", "siege_tank" }
		},
		normal =
		{
			{ "combat_tank_o", "combat_tank_o", "raider", "raider", "quad", "quad" },
			{ "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf" },
			{ "stealth_raider", "stealth_raider", "stealth_raider", "trooper", "trooper", "raider", "raider", "missile_tank", "missile_tank" },
			{ "raider", "raider", "raider", "raider", "raider" },
			{ "combat_tank_o", "combat_tank_o", "combat_tank_o", "combat_tank_o", "deviator" },
			{ "trooper", "trooper", "trooper", "trooper", "trooper", "trooper", "trooper" },
			{ "deviator", "combat_tank_o", "combat_tank_o", "missile_tank", "missile_tank", "missile_tank" },
			{ "stealth_raider", "stealth_raider", "stealth_raider", "stealth_raider", "stealth_raider", "stealth_raider", "stealth_raider", "stealth_raider" },
			{ "deviator", "siege_tank", "siege_tank" }
		},
		hard =
		{
			{ "combat_tank_o", "combat_tank_o", "combat_tank_o", "raider", "raider", "quad", "quad", "quad" },
			{ "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf" },
			{ "stealth_raider", "stealth_raider", "stealth_raider", "stealth_raider", "trooper", "trooper", "raider", "raider", "raider", "missile_tank", "missile_tank", "missile_tank" },
			{ "raider", "raider", "raider", "raider", "raider", "raider" },
			{ "combat_tank_o", "combat_tank_o", "combat_tank_o", "combat_tank_o", "combat_tank_o", "deviator", "deviator" },
			{ "trooper", "trooper", "trooper", "trooper", "trooper", "trooper", "trooper", "trooper", "trooper" },
			{ "deviator", "deviator", "combat_tank_o", "combat_tank_o", "combat_tank_o", "missile_tank", "missile_tank", "missile_tank", "missile_tank" },
			{ "stealth_raider", "stealth_raider", "stealth_raider", "stealth_raider", "stealth_raider", "stealth_raider", "stealth_raider", "stealth_raider", "stealth_raider", "stealth_raider" },
			{ "deviator", "siege_tank", "siege_tank", "siege_tank" }
		}
	},
	Smugglers =
	{
		easy =
		{
			{ "combat_tank_s", "raider", "raider", "quad", "quad" },
			{ "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf" },
			{ "raider", "raider", "raider", "raider", "trooper", "trooper", "raider", "missile_tank", "missile_tank" },
			{ "raider", "raider", "raider", "raider", "raider" },
			{ "combat_tank_s", "combat_tank_s", "combat_tank_s", "combat_tank_s", "combat_tank_s" },
			{ "trooper", "trooper", "trooper", "trooper", "trooper", "trooper", "trooper", "trooper" },
			{ "combat_tank_s", "combat_tank_s", "missile_tank", "missile_tank", "missile_tank" },
			{ "raider", "raider", "raider", "raider", "raider", "raider", "raider" },
			{ "combat_tank_s", "combat_tank_s", "siege_tank", "siege_tank", "siege_tank" }
		},
		normal =
		{
			{ "combat_tank_s", "raider", "quad", "quad" },
			{ "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf" },
			{ "raider", "raider", "raider", "trooper", "raider", "missile_tank", "missile_tank" },
			{ "raider", "raider", "raider", "raider" },
			{ "combat_tank_s", "combat_tank_s", "combat_tank_s", "combat_tank_s" },
			{ "trooper", "trooper", "trooper", "trooper", "trooper", "trooper", "trooper" },
			{ "combat_tank_s", "combat_tank_s", "missile_tank", "missile_tank" },
			{ "raider", "raider", "raider", "raider", "raider", "raider" },
			{ "combat_tank_s", "combat_tank_s", "siege_tank", "siege_tank" }
		},
		hard =
		{
			{ "combat_tank_s", "raider", "quad" },
			{ "light_inf", "light_inf", "light_inf", "light_inf", "light_inf", "light_inf" },
			{ "raider", "raider", "trooper", "raider", "missile_tank" },
			{ "raider", "raider", "raider" },
			{ "combat_tank_s", "combat_tank_s", "combat_tank_s" },
			{ "trooper", "trooper", "trooper", "trooper", "trooper", "trooper" },
			{ "combat_tank_s", "combat_tank_s", "missile_tank" },
			{ "raider", "raider", "raider", "raider", "raider" },
			{ "combat_tank_s", "siege_tank", "siege_tank" }
		}
	}
}

ReinforcementWaypoints =
{
	{ EntryPoint1.Location, RallyPoint1.Location },
	{ EntryPoint2.Location, RallyPoint2.Location },
	{ EntryPoint3.Location, RallyPoint3.Location },
	{ EntryPoint4.Location, RallyPoint4.Location },
	{ EntryPoint5.Location, RallyPoint5.Location },
	{ EntryPoint6.Location, RallyPoint6.Location },
	{ EntryPoint7.Location, RallyPoint7.Location },
	{ EntryPoint8.Location, RallyPoint8.Location },
	{ EntryPoint9.Location, RallyPoint9.Location }
}

ReinforcementDelays =
{
	easy = { DateTime.Seconds(8), DateTime.Seconds(40), DateTime.Seconds(80), DateTime.Seconds(140), DateTime.Seconds(222), DateTime.Seconds(300), DateTime.Seconds(380), DateTime.Seconds(450), DateTime.Seconds(510), DateTime.Seconds(570) },
	normal = { DateTime.Seconds(16), DateTime.Seconds(35), DateTime.Seconds(70), DateTime.Seconds(125), DateTime.Seconds(211), DateTime.Seconds(280), DateTime.Seconds(360), DateTime.Seconds(425), DateTime.Seconds(465), DateTime.Seconds(505) },
	hard = { DateTime.Seconds(24), DateTime.Seconds(30), DateTime.Seconds(60), DateTime.Seconds(110), DateTime.Seconds(200), DateTime.Seconds(270), DateTime.Seconds(340), DateTime.Seconds(400), DateTime.Seconds(440), DateTime.Seconds(480) }
}

FrigateWaypoints = { FrigateEntry.Location, FrigateRally.Location }

MissionTimer = { }
MissionTimer["easy"] = DateTime.Minutes(20)
MissionTimer["normal"] = DateTime.Minutes(30)
MissionTimer["hard"] = DateTime.Minutes(40)

DialougePlayed = false

IdleHunt = function(unit) if not unit.IsDead then Trigger.OnIdle(unit, unit.Hunt) end end

IXInfiltrated = {
	Atreides = false,
	Harkonnen = false,
	Ordos = false
}

GoToChronoSphere = function(unit)
	Trigger.OnIdle(unit, function()
		unit.Move(CPos.New(125, 70))
		unit.Destroy()
	end)
end

CheckIfIXKilledBeforeInfiltration = function(house, ix)
	Trigger.OnKilled(ix, function()
		if IXInfiltrated[house.Name] == false then
			player.MarkFailedObjective(InfiltrateIXLabs)
		end
	end)
end

SetIXInfiltrated = function(house, ix)
	Trigger.OnInfiltrated(ix, function()
		if IXInfiltrated[house.Name] == false then
			IXInfiltrated[house.Name] = true
			Media.DisplayMessage("".. house.Name .." Ix Research Center has been infiltated.", "EVA")
			
			LeftIXes = LeftIXes - 1
		end
	end)
end

AllIXInfiltated = function()
	if IXInfiltrated["Atreides"] and IXInfiltrated["Harkonnen"] and IXInfiltrated["Ordos"] then
		player.MarkCompletedObjective(InfiltrateIXLabs)

		if DialougePlayed == false then
			DialougePlayed = true

			Trigger.AfterDelay(DateTime.Seconds(4), function()
				Media.DisplayMessage("Good, now you can get your materials.", "Smuggler Leader")
				
				local units = Reinforcements.ReinforceWithTransport(smuggler, "frigate.reinforce", Cargos, FrigateWaypoints, { FrigateWaypoints[1] })[2]
				Utils.Do(units, GoToChronoSphere)
			end)

			Trigger.AfterDelay(DateTime.Seconds(15), function()
				Media.DisplayMessage("What the heck do you think you are doing Smugglers? We found your spy.", "Ordos Mentat")
				Trigger.AfterDelay(DateTime.Seconds(4), function()
					Media.DisplayMessage("We found a spy in our IX Lab too. Smugglers what is going on?", "Harkonnen Mentat")
					Trigger.AfterDelay(DateTime.Seconds(4), function()
						Media.DisplayMessage("Ditto. Also who are those guys, we didn't even notice fighting each other.", "Atreides Mentat")
						Trigger.AfterDelay(DateTime.Seconds(4), function()
							Media.DisplayMessage("Nothing important guys.", "Smuggler Leader")
							Trigger.AfterDelay(DateTime.Seconds(4), function()
								Media.DisplayMessage("Do you really think we'll belive that?", "Harkonnen Mentat")
								ActivateSmugglerAI()
								Trigger.AfterDelay(DateTime.Seconds(4), function()
									Media.DisplayMessage("The Harkonnen are now hostile!", "EVA")
									Media.DisplayMessage("The Ordos are now hostile!", "EVA")

									AttackLocations["Harkonnen"] = { AConyard.Location, OConyard.Location, ChronoSphere.Location, SOutpost.Location, SStarport.Location, SBarracks.Location }
									AttackLocations["Ordos"] = { AConyard.Location, HConyard.Location, ChronoSphere.Location, SOutpost.Location, SStarport.Location, SBarracks.Location }
									Trigger.AfterDelay(DateTime.Seconds(4), function()
										Media.DisplayMessage("Do whatever you want idiots, but this spy is staying with us!", "Atreides Mentat")
										Trigger.AfterDelay(DateTime.Seconds(8), function()
											Media.DisplayMessage("You should guard the ChronoSphere till i repair it, commander.", "Einstein")
											Trigger.AfterDelay(DateTime.Seconds(4), function()
												Media.DisplayMessage("We are gonna help with the fight, don't worry.", "Smuggler Leader")
												SendReinforcements()
											end)
										end)
									end)
								end)
							end)
						end)
					end)
				end)
			end)
		end
	end
end

InitialiseTimer = function()
	if ticked > 0 then
		UserInterface.SetMissionText("ChronoSphere will be repaired in " .. Utils.FormatTime(ticked), TimerColor)
		ticked = ticked - 1
	elseif ticked == 0 then
		FinishTimer()
		ticked = ticked - 1
	end
end

FinishTimer = function()
	for i = 0, 5, 1 do
		local c = TimerColor
		if i % 2 == 0 then
			c = HSLColor.White
		end

		Trigger.AfterDelay(DateTime.Seconds(i), function()
			UserInterface.SetMissionText("ChronoSphere is repaired. Get ready for the time shift!", c)
			player.MarkCompletedObjective(DefendTheChronosphere)
		end)
	end
	Trigger.AfterDelay(DateTime.Seconds(6), function() UserInterface.SetMissionText("") end)
end

GivePlayerCarryall = function()
	Trigger.AfterDelay(CarryallTimer[Difficulty], function()
		Reinforcements.Reinforce(player, Carryall, CarryallPath)
		Media.DisplayMessage("You can have this Carryall to help you get credits quicker.", "Smuggler Leader")
	end)
end

ChronoSphereIsDead = function()
	Trigger.OnKilledOrCaptured(ChronoSphere, function()
		player.MarkFailedObjective(DefendTheChronosphere)
	end)
end

RadarDomeIsDead = function()
	Trigger.OnRemovedFromWorld(AllRadar, function()
		DomeNoLongerExists = true
	end)
end

AlliedBarracksIsDead = function()
	Trigger.OnRemovedFromWorld(AllBarrakcs, function()
		BarracksNoLongerExists = true
	end)
end

SendReinforcement = function(house, transport, unit_number, waypoints)
	local units = Reinforcements.ReinforceWithTransport(house, transport, Reinforcementses[house.Name][Difficulty][unit_number], waypoints, { waypoints[1] })[2]
	Utils.Do(units, IdleHunt)
end

SendReinforcements = function()
	Trigger.AfterDelay(ReinforcementDelays[Difficulty][1], function()
		local Waypoint1 = Utils.Random(ReinforcementWaypoints)
		local Waypoint2 = Utils.Random(Utils.Where(ReinforcementWaypoints, function(v) return v ~= Waypoint1 end))

		SendReinforcement(smuggler, "carryall.reinforce", 1, Waypoint1)
		SendReinforcement(smuggler, "carryall.reinforce", 2, Waypoint2)
	end)
	Trigger.AfterDelay(ReinforcementDelays[Difficulty][2], function()
		local Waypoint3 = Utils.Random(Utils.Where(ReinforcementWaypoints, function(v) return v ~= Waypoint1 and v ~= Waypoint2 and v ~= ReinforcementWaypoints[8] end))
		local Waypoint4 = Utils.Random(Utils.Where(ReinforcementWaypoints, function(v) return v ~= Waypoint1 and v ~= Waypoint2 and v ~= Waypoint3 and v ~= ReinforcementWaypoints[9] end))

		SendReinforcement(harkonnen, "carryall.reinforce", 1, Waypoint3)
		SendReinforcement(ordos, "carryall.reinforce", 1, Waypoint4)
	end)
	Trigger.AfterDelay(ReinforcementDelays[Difficulty][3], function()
		local Waypoint5 = Utils.Random(Utils.Where(ReinforcementWaypoints, function(v) return v ~= ReinforcementWaypoints[8] end))
		local Waypoint6 = Utils.Random(Utils.Where(ReinforcementWaypoints, function(v) return v ~= Waypoint5 and v ~= ReinforcementWaypoints[9] end))

		SendReinforcement(harkonnen, "carryall.reinforce", 2, Waypoint5)
		SendReinforcement(ordos, "carryall.reinforce", 2, Waypoint6)
	end)
	Trigger.AfterDelay(ReinforcementDelays[Difficulty][4], function()
		local Waypoint7 = Utils.Random(ReinforcementWaypoints)
		local Waypoint8 = Utils.Random(Utils.Where(ReinforcementWaypoints, function(v) return v ~= Waypoint7 and v ~= ReinforcementWaypoints[8] end))
		local Waypoint9 = Utils.Random(Utils.Where(ReinforcementWaypoints, function(v) return v ~= Waypoint7 and v ~= Waypoint8 and v ~= ReinforcementWaypoints[9] end))

		SendReinforcement(smuggler, "carryall.reinforce", 3, Waypoint7)
		SendReinforcement(harkonnen, "carryall.reinforce", 3, Waypoint8)
		SendReinforcement(ordos, "carryall.reinforce", 3, Waypoint9)
	end)
	Trigger.AfterDelay(ReinforcementDelays[Difficulty][5], function()
		local Waypoint10 = Utils.Random(ReinforcementWaypoints)
		local Waypoint11 = Utils.Random(Utils.Where(ReinforcementWaypoints, function(v) return v ~= Waypoint10 and v ~= ReinforcementWaypoints[8] end))
		local Waypoint12 = Utils.Random(Utils.Where(ReinforcementWaypoints, function(v) return v ~= Waypoint10 and v ~= Waypoint11 and v ~= ReinforcementWaypoints[9] end))

		SendReinforcement(smuggler, "carryall.reinforce", 4, Waypoint10)
		SendReinforcement(harkonnen, "carryall.reinforce", 4, Waypoint11)
		SendReinforcement(ordos, "carryall.reinforce", 4, Waypoint12)
	end)
	Trigger.AfterDelay(ReinforcementDelays[Difficulty][6], function()
		local Waypoint13 = Utils.Random(ReinforcementWaypoints)
		local Waypoint14 = Utils.Random(Utils.Where(ReinforcementWaypoints, function(v) return v ~= Waypoint13 and v ~= ReinforcementWaypoints[8] end))
		local Waypoint15 = Utils.Random(Utils.Where(ReinforcementWaypoints, function(v) return v ~= Waypoint13 and v ~= Waypoint14 and v ~= ReinforcementWaypoints[9] end))

		SendReinforcement(smuggler, "carryall.reinforce", 5, Waypoint13)
		SendReinforcement(harkonnen, "carryall.reinforce", 5, Waypoint14)
		SendReinforcement(ordos, "carryall.reinforce", 5, Waypoint15)
	end)
	Trigger.AfterDelay(ReinforcementDelays[Difficulty][7], function()
		local Waypoint16 = Utils.Random(ReinforcementWaypoints)
		local Waypoint17 = Utils.Random(Utils.Where(ReinforcementWaypoints, function(v) return v ~= Waypoint16 and v ~= ReinforcementWaypoints[8] end))
		local Waypoint18 = Utils.Random(Utils.Where(ReinforcementWaypoints, function(v) return v ~= Waypoint16 and v ~= Waypoint17 and v ~= ReinforcementWaypoints[9] end))

		SendReinforcement(smuggler, "carryall.reinforce", 6, Waypoint16)
		SendReinforcement(harkonnen, "carryall.reinforce", 6, Waypoint17)
		SendReinforcement(ordos, "carryall.reinforce", 6, Waypoint18)
	end)
	Trigger.AfterDelay(ReinforcementDelays[Difficulty][8], function()
		local Waypoint19 = Utils.Random(ReinforcementWaypoints)
		local Waypoint20 = Utils.Random(Utils.Where(ReinforcementWaypoints, function(v) return v ~= Waypoint19 and v ~= ReinforcementWaypoints[8] end))
		local Waypoint21 = Utils.Random(Utils.Where(ReinforcementWaypoints, function(v) return v ~= Waypoint19 and v ~= Waypoint20 and v ~= ReinforcementWaypoints[9] end))

		SendReinforcement(smuggler, "carryall.reinforce", 7, Waypoint19)
		SendReinforcement(harkonnen, "carryall.reinforce", 7, Waypoint20)
		SendReinforcement(ordos, "carryall.reinforce", 7, Waypoint21)
	end)
	Trigger.AfterDelay(ReinforcementDelays[Difficulty][9], function()
		local Waypoint22 = Utils.Random(ReinforcementWaypoints)
		local Waypoint23 = Utils.Random(Utils.Where(ReinforcementWaypoints, function(v) return v ~= Waypoint22 and v ~= ReinforcementWaypoints[8] end))
		local Waypoint24 = Utils.Random(Utils.Where(ReinforcementWaypoints, function(v) return v ~= Waypoint22 and v ~= Waypoint23 and v ~= ReinforcementWaypoints[9] end))

		SendReinforcement(smuggler, "carryall.reinforce", 8, Waypoint22)
		SendReinforcement(harkonnen, "carryall.reinforce", 8, Waypoint23)
		SendReinforcement(ordos, "carryall.reinforce", 8, Waypoint24)
	end)
	Trigger.AfterDelay(ReinforcementDelays[Difficulty][10], function()
		local Waypoint25 = Utils.Random(ReinforcementWaypoints)
		local Waypoint26 = Utils.Random(Utils.Where(ReinforcementWaypoints, function(v) return v ~= Waypoint25 and v ~= ReinforcementWaypoints[8] end))
		local Waypoint27 = Utils.Random(Utils.Where(ReinforcementWaypoints, function(v) return v ~= Waypoint25 and v ~= Waypoint26 and v ~= ReinforcementWaypoints[9] end))

		SendReinforcement(smuggler, "carryall.reinforce", 9, Waypoint25)
		SendReinforcement(harkonnen, "carryall.reinforce", 9, Waypoint26)
		SendReinforcement(ordos, "carryall.reinforce", 9, Waypoint27)

		SendReinforcements() -- loop, lazy to code enough units for the rest of the game
	end)
end

SetupStartingUnits = function()
	if Difficulty == "easy" then
		SovE13.Destroy()
		SovE3.Destroy()
	elseif Difficulty == "normal" then
		SovE3.Destroy()
		AllEngi.Destroy()
	else
		AllEngi.Destroy()
		AllMedic.Destroy()
	end
end

Tick = function()
	AllIXInfiltated()

	if IXInfiltrated["Atreides"] and IXInfiltrated["Harkonnen"] and IXInfiltrated["Ordos"] then
		Trigger.AfterDelay(DateTime.Seconds(4), function()
			InitialiseTimer()
		end)
	end
	
	Spies = player.GetActorsByType("spy")
	
	if #Spies < LeftIXes and (DomeNoLongerExists or BarracksNoLongerExists) then
		player.MarkFailedObjective(InfiltrateIXLabs)
	end
end

WorldLoaded = function()
	atreides = Player.GetPlayer("Atreides")
	harkonnen = Player.GetPlayer("Harkonnen")
	ordos = Player.GetPlayer("Ordos")
	smuggler = Player.GetPlayer("Smugglers")
	soviets = Player.GetPlayer("USSR")
	player = Player.GetPlayer("Allies")
	
	Difficulty = Map.LobbyOption("difficulty")
	ticked = MissionTimer[Difficulty]
	
	InitObjectives()
	SetupStartingUnits()
	ChronoSphereIsDead()
	RadarDomeIsDead()
	AlliedBarracksIsDead()
	
	GivePlayerCarryall()

	Camera.Position = ChronoSphere.CenterPosition
	
	SetIXInfiltrated(atreides, AResearch)
	SetIXInfiltrated(harkonnen, HResearch)
	SetIXInfiltrated(ordos, OResearch)
	
	CheckIfIXKilledBeforeInfiltration(atreides, AResearch)
	CheckIfIXKilledBeforeInfiltration(harkonnen, HResearch)
	CheckIfIXKilledBeforeInfiltration(ordos, OResearch)
	
	Trigger.AfterDelay(0, ActivateAI)

	atreides.Resources = 15000
	harkonnen.Resources = 15000
	ordos.Resources = 15000

	TimerColor = player.Color

	Utils.Do(Utils.Take(4, Upgrades), function(upgrade)
		Actor.Create(upgrade, true, { Owner = atreides })
		Actor.Create(upgrade, true, { Owner = harkonnen })
		Actor.Create(upgrade, true, { Owner = ordos })
		Actor.Create(upgrade, true, { Owner = smuggler })
	end)
	Actor.Create(Upgrades[5], true, { Owner = atreides })
	
	SovV2.Attack(ChronoSphere)
	Trigger.OnAllKilled(AlliedDefences, function()
		Utils.Do(SovietUnits, function(unit)
			if not unit.IsDead then
				unit.Attack(ChronoSphere)
			end
		end)
	end)
end

InitObjectives = function()
	Trigger.OnObjectiveAdded(player, function(p, id)
		Media.DisplayMessage(p.GetObjectiveDescription(id), "New " .. string.lower(p.GetObjectiveType(id)) .. " objective")
	end)

	InfiltrateIXLabs = player.AddPrimaryObjective("Infiltrate all 3 Ix Research Centres.")
	DefendTheChronosphere = player.AddPrimaryObjective("Keep the ChronoSphere intact.")
	KillAtreidesH = harkonnen.AddPrimaryObjective("Destroy all Atreides forces.")
	KillAtreidesO = ordos.AddPrimaryObjective("Destroy all Atreides forces.")
	KillHarkonnenA = atreides.AddPrimaryObjective("Destroy all Harkonnen forces.")
	KillHarkonnenO = ordos.AddPrimaryObjective("Destroy all Harkonnen forces.")
	KillOrdosA = atreides.AddPrimaryObjective("Destroy all Ordos forces.")
	KillOrdosH = harkonnen.AddPrimaryObjective("Destroy all Ordos forces.")

	Trigger.OnObjectiveCompleted(player, function(p, id)
		Media.DisplayMessage(p.GetObjectiveDescription(id), "Objective completed")
	end)
	Trigger.OnObjectiveFailed(player, function(p, id)
		Media.DisplayMessage(p.GetObjectiveDescription(id), "Objective failed")
	end)

	Trigger.OnPlayerLost(player, function()
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			Media.PlaySpeechNotification(player, "Lose")
		end)
	end)
	Trigger.OnPlayerWon(player, function()
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			Media.PlaySpeechNotification(player, "Win")
		end)
	end)
end
