RescueWarning = { CPos.New(108,1), CPos.New(108,2), CPos.New(108,3), CPos.New(108,4), CPos.New(108,5), CPos.New(108,6), CPos.New(108,7), CPos.New(108,8), CPos.New(108,9), CPos.New(108,10), CPos.New(108,11), CPos.New(108,12), CPos.New(108,13), CPos.New(108,14), CPos.New(108,15)   }
Engineer = "e6"
ChangeStance = true
Sniper = "sniper"
General = "gnrl"
Delphi = "delphi"
FlameType = "e3"
InfantryType = "e1"
Chan = "chan"
Medic = "medi"
Mechanic = "mech"
RescueGuards = { "e1", "e1", "e6", "e4" }
RescueGuardPathMulti0 = { RescueSpawn.Location, RescueRally.Location }
Arrows = { CPos.New(184,13) }

FlareType = "flare"
Startingheli = "tran.insertion"
Extractionheli = "tran.extraction"
InsertionPathMulti0 = { InsertionEntryMulti0.Location, ZombieSpawn8.Location }
StartingReinforcements = { "e7.noautotarget", "e1", "e1", "e1", "sniper" }
ExtractionPathMulti0 = { InsertExtractionHeli.Location, Extraction.Location }
--Zombie Reinforements
ZombieEntryPoints = { RoadSpawn6, RoadSpawn9, RoadSpawn10, RoadSpawn15 }
SpawnPoints = { ZombieSpawn9, ZombieRally16, ZombieRally5B, ZombieRally6 }
ZombieEntryPoints2 = { RoadSpawn3, RoadSpawn16, RoadSpawn13, RoadSpawn14 }
SpawnPoints2 = { Rally3, Rally4, Rally5 }
ZombieEntryPoints3 = { RoadSpawn17, RoadSpawn18, RoadSpawn2, RoadSpawn1 }
SpawnPoints3 = { Rally8, Rally6, Rally9 }

ZombieTeam1 = {  "c1", "c1", "c1" }
ZombieTeam2 = {  "c3", "c6", "c2" }
ZombieTeam3 = {  "c7", "c3", "c2", "c3" }
ZombieTeam4 = {  "c7", "c2", "c3" } 
ZombieTeam5 = {  "c4", "c8", "c1", "c3" }
ZombieTeam6 = {  "c6", "c3", "c1" }
ZombieTeam7 = {  "c1", "c8", "c9" } 
ZombieTeam8 = {  "c1", "c3", "c3", "c3", "c2", "c9" } 

Wave = 0
Waves =
{
	{ delay = 500, units = { ZombieTeam1 } },
	{ delay = 500, units = { ZombieTeam7 } },
	{ delay = 500, units = { ZombieTeam4 } },
	{ delay = 500, units = { ZombieTeam5 } },
	{ delay = 500, units = { ZombieTeam8 } }
}

Wave1 = 0
Waves1 =
{
	{ delay = 100, units = { ZombieTeam1 } },
	{ delay = 100, units = { ZombieTeam2 } },
	{ delay = 100, units = { ZombieTeam4 } },
	{ delay = 100, units = { ZombieTeam6 } },
	{ delay = 100, units = { ZombieTeam8 } }
}

ZombieWave = 0
ZombieWaves =
{
	{ delay = 100, units = { ZombieTeam1 } },
	{ delay = 100, units = { ZombieTeam2 } },
	{ delay = 100, units = { ZombieTeam3 } },
	{ delay = 100, units = { ZombieTeam4 } },
	{ delay = 100, units = { ZombieTeam5 } },
	{ delay = 100, units = { ZombieTeam6 } },
	{ delay = 100, units = { ZombieTeam7 } },
	{ delay = 100, units = { ZombieTeam8 } }
}

SendInsertionHelicopterMulti0 = function()
	local passengers = Reinforcements.ReinforceWithTransport(Multi0, Startingheli,
		StartingReinforcements, InsertionPathMulti0, { RoadSpawn11.Location })[2]
	local Player1 = passengers[1]
	Trigger.OnKilled(Player1, MissionFailed)
	Player1.Stance = "HoldFire"
end
--Wave1
SendUnits = function(entryCell, unitTypes, targetCell)
	local units = Reinforcements.Reinforce(BadGuy, unitTypes, { entryCell }, 40, function(a)
		if not a.HasProperty("AttackMove") then
			Trigger.OnIdle(a, function(a)
				a.Move(targetCell)
			end)
			return
		end

		Trigger.OnIdle(a, function(a)
			if a.Location ~= targetCell then
				a.AttackMove(targetCell)
			else
				a.Hunt()
			end
		end)
	end)
end

SendWave = function()
	Wave = Wave + 1
	local wave = Waves[Wave]
	Trigger.AfterDelay(wave.delay, function()
		Utils.Do(wave.units, function(units)
			local entry = Utils.Random(ZombieEntryPoints).Location
			local target = Utils.Random(SpawnPoints).Location
			SendUnits(entry, units, target)
		end)
		if (Wave < #Waves) then
			local delay = Utils.RandomInteger(DateTime.Seconds(10), DateTime.Seconds(10))
			Trigger.AfterDelay(delay, SendWave)
		end
	end)
end

HospitalGuardsKilled = function()
	medic = Actor.Create(Medic, true, { Location = MedicSpawn.Location, Owner = Multi0 })
	engi = Actor.Create(Engineer, true, { Location = MedicSpawn.Location, Owner = Multi0 })
	medic.Move(HospitalRally.Location)
	engi.Move(HospitalRally.Location)
	Multi0.MarkCompletedObjective(alliedObjective0)
	Media.PlaySpeechNotification(Multi0, "TargetRescued")
	Trigger.AfterDelay(DateTime.Seconds(2), function()
		Media.DisplayMessage("Repair The Bridge")
	end)
end
--Wave2
ZombieSpawn = function()
	Trigger.AfterDelay(DateTime.Seconds(3), function()
		Reinforcements.Reinforce(BadGuy, { "c1", "c4", "c4", "c3"  }, { RoadSpawn12.Location, Rally2.Location })
	end)
end

RescueObjective = function()
	Village()
	Reinforcements.Reinforce(BadGuy, { "c9", "c9", "c9", "c9", "c9", "c9", }, { Rally7.Location, Rally7.Location })
end

SendVillageUnits = function(entryCell, unitTypes, targetCell)
	local units = Reinforcements.Reinforce(BadGuy, unitTypes, { entryCell }, 40, function(a)
		if not a.HasProperty("AttackMove") then
			Trigger.OnIdle(a, function(a)
				a.Move(targetCell)
			end)
			return
		end

		Trigger.OnIdle(a, function(a)
			if a.Location ~= targetCell then
				a.AttackMove(targetCell)
			else
				a.Hunt()
			end
		end)
	end)
end

Village = function()
	Wave1 = Wave1 + 1
	local wave1 = Waves1[Wave1]
	Trigger.AfterDelay(wave1.delay, function()
		Utils.Do(wave1.units, function(units)
			local entry = Utils.Random(ZombieEntryPoints2).Location
			local target = Utils.Random(SpawnPoints2).Location
			SendVillageUnits(entry, units, target)
		end)
		if (Wave1 < #Waves1) then
			local delay = Utils.RandomInteger(DateTime.Seconds(10), DateTime.Seconds(20))
			Trigger.AfterDelay(delay, Village)
		else
			Trigger.AfterDelay(DateTime.Seconds(1), Rescue)
		end
	end)
end

Rescue = function()
	Media.PlaySpeechNotification(Multi0, "TargetRescued")
	general = Actor.Create(General, true, { Location = RescueSpawn.Location, Owner = Multi0 })
	chan = Actor.Create(Chan, true, { Location = RescueSpawn.Location, Owner = Multi0 })
	delphi = Actor.Create(Delphi, true, { Location = RescueSpawn.Location, Owner = Multi0 })
	Reinforcements.Reinforce(Multi0, { "e1", "e1", "e3" }, { RescueSpawn.Location, RescueRally.Location })
	general.Move(RescueRally.Location)
	chan.Move(RescueRally.Location)
	delphi.Move(RescueRally.Location)
	Trigger.AfterDelay(DateTime.Seconds(3), function()
		ScienceTeam()
		Actor.Create(FlareType, true, { Owner = Multi0, Location = Extraction.Location })
		EscapeObjective = Multi0.AddPrimaryObjective("Move To Extraction Point")
		EscapeObjective1 = Multi0.AddPrimaryObjective("Extract")
		Trigger.AfterDelay(DateTime.Seconds(5), function()
			Media.PlaySpeechNotification(Multi0, "SignalFlareWest")
			ZombieAttack()
			RussiaBase()
		end)
	end)
end
--Wave3
ZombieUnits = function(entryCell, unitTypes, targetCell)
	local units = Reinforcements.Reinforce(BadGuy, unitTypes, { entryCell }, 40, function(a)
		if not a.HasProperty("AttackMove") then
			Trigger.OnIdle(a, function(a)
				a.Move(targetCell)
			end)
			return
		end

		Trigger.OnIdle(a, function(a)
			if a.Location ~= targetCell then
				a.AttackMove(targetCell)
			else
				a.Hunt()
			end
		end)
	end)
end

ZombieAttack = function()
	ZombieWave = ZombieWave + 1
	local zombiewaves = ZombieWaves[ZombieWave]
	Trigger.AfterDelay(zombiewaves.delay, function()
		Utils.Do(zombiewaves.units, function(units)
			local entry = Utils.Random(ZombieEntryPoints3).Location
			local target = Utils.Random(SpawnPoints3).Location
			ZombieUnits(entry, units, target)
		end)

		if (ZombieWave < #ZombieWaves) then
			local delay = Utils.RandomInteger(DateTime.Seconds(10), DateTime.Seconds(10))
			Trigger.AfterDelay(delay, ZombieAttack)
		else
			Trigger.AfterDelay(DateTime.Seconds(1), SendExtractionHelicopter)
		end
	end)
end

RussiaBase = function()
	Reinforcements.Reinforce(BadGuy, { "e1", "e1", "e1" }, { Rally7.Location, Rally7.Location })
	Reinforcements.Reinforce(BadGuy, { "e1", "e1", "e1" }, { Rally7.Location, Rally7.Location })
	Reinforcements.Reinforce(BadGuy, { "e1", "e1", "e4", "e1", "e4" }, { Rally6.Location, Rally8.Location })
	Reinforcements.Reinforce(BadGuy, { "e1", "e1", "e1", "e1", "e1" }, { Rally6.Location, Rally9.Location })
	Reinforcements.Reinforce(BadGuy, { "3tnk", "3tnk", "v2rl" }, { Rally8.Location, Rally6.Location })
	Reinforcements.Reinforce(BadGuy, { "ftrk", "ftrk", "e2", }, { Rally8.Location, Rally6.Location })
	Reinforcements.Reinforce(BadGuy, { "e1", "e1", "e1" }, { Rally7.Location, Rally7.Location })
end


--extraction
SendExtractionHelicopter = function()
	heli = Reinforcements.ReinforceWithTransport(Government, Extractionheli, nil, ExtractionPathMulti0)[1]
	Trigger.AfterDelay(DateTime.Seconds(15), function()
		EvacuateHelicopter()
	end)
	Trigger.OnKilled(heli, RescueFailed)
	Trigger.OnRemovedFromWorld(heli, HelicopterGone)
end

EvacuateHelicopter = function()
	heli.Move(RoadSpawn18.Location)
	Trigger.OnIdle(heli, heli.Destroy)
end

HelicopterGone = function()
	if not heli.IsDead then
		Media.PlaySpeechNotification(Multi0, "TargetRescued")
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			Multi0.MarkCompletedObjective(alliedObjective1)
			Multi0.MarkCompletedObjective(alliedObjective2)
			Multi0.MarkCompletedObjective(EscapeObjective)
			Multi0.MarkCompletedObjective(EscapeObjective1)
		end)
	end
end

--Objective Fails/Completions
HospitalFail = function()
	Media.DisplayMessage("Hospital Destroyed, Mission Failed")
		Trigger.AfterDelay(DateTime.Seconds(3), function()
		Multi0.MarkFailedObjective(alliedObjective1)
	end)
end

ScienceTeam = function()
	SciTeam = { general, chan, delphi }
	Trigger.OnAllKilled(SciTeam, RescueFailed)
end

RescueFailed = function()
	Media.DisplayMessage("Scientists Have Died")
	Trigger.AfterDelay(DateTime.Seconds(3), function()
		Multi0.MarkFailedObjective(EscapeObjective)
		Multi0.MarkFailedObjective(EscapeObjective1)
	end)
end

MissionAccomplished = function()
	Trigger.AfterDelay(DateTime.Seconds(1), function()
	Media.PlaySpeechNotification(Multi0, "MissionAccomplished")
	end)
end

MissionFailed = function()
	Multi0.MarkFailedObjective(alliedObjective0)
	Multi0.MarkFailedObjective(alliedObjective1)
	Multi0.MarkFailedObjective(alliedObjective2)
end

--fix heli trigger to stay idle

WorldLoaded = function()
	BadGuy = Player.GetPlayer("BadGuy")
	Multi0 = Player.GetPlayer("Multi0")
	Russia = Player.GetPlayer("Russia")
	Government = Player.GetPlayer("Government")
	players = { }
	for i = 0, 7, 1 do
		local player = Player.GetPlayer("Multi" ..i)
		players[i] = player
	end

	Camera.Position = ZombieSpawn8.CenterPosition

--Objectives

	Trigger.OnPlayerLost(Multi0, MissionFailed)

	Trigger.OnPlayerWon(Multi0, MissionAccomplished)

	Trigger.OnObjectiveAdded(Multi0, function(p, id)
		Trigger.AfterDelay(DateTime.Seconds(5), function()
			Media.DisplayMessage(p.GetObjectiveDescription(id), "New " .. string.lower(p.GetObjectiveType(id)) .. " objective")
		end)
	end)

	BadGuy.AddPrimaryObjective("Eat Brains")
	alliedObjective0 = Multi0.AddPrimaryObjective("Rescue Medic Team")
	alliedObjective1 = Multi0.AddPrimaryObjective("Rescue Scientists")
	alliedObjective2 = Multi0.AddPrimaryObjective("Tanya Must Survive")

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

	Trigger.OnPlayerLost(Multi0, function()
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			Media.PlaySpeechNotification(Multi0, "MissionFailed")
		end)
	end)
	Trigger.OnPlayerWon(Multi0, function()
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			Media.PlaySpeechNotification(Multi0, "MissionAccomplished")
		end)
	end)
	SendWave()

	SendInsertionHelicopterMulti0()

	HospitalGuards = { Guard1, Guard2, Guard3, Guard4 }
	Trigger.OnAllKilled(HospitalGuards, HospitalGuardsKilled)

	Trigger.OnKilled(Church, RescueFailed)

	Trigger.OnKilled(Hospital, HospitalFail)
--Background Music
	Trigger.AfterDelay(DateTime.Seconds(0), function()
		Media.PlaySound("RavenholmTrack1.aud")
	end)
	Trigger.AfterDelay(DateTime.Seconds(110), function()
		Media.PlaySound("RavenholmTrack1.aud")
	end)
	Trigger.AfterDelay(DateTime.Seconds(220), function()
		Media.PlaySound("RavenholmTrack1.aud")
	end)
	Trigger.AfterDelay(DateTime.Seconds(330), function()
		Media.PlaySound("RavenholmTrack1.aud")
	end)
	Trigger.AfterDelay(DateTime.Seconds(440), function()
		Media.PlaySound("RavenholmTrack1.aud")
	end)
	Trigger.AfterDelay(DateTime.Seconds(550), function()
		Media.PlaySound("RavenholmTrack1.aud")
	end)
	Trigger.AfterDelay(DateTime.Seconds(660), function()
		Media.PlaySound("RavenholmTrack1.aud")
	end)
	Trigger.AfterDelay(DateTime.Seconds(770), function()
		Media.PlaySound("RavenholmTrack1.aud")
	end)
	Trigger.AfterDelay(DateTime.Seconds(880), function()
		Media.PlaySound("RavenholmTrack1.aud")
	end)
	Trigger.AfterDelay(DateTime.Seconds(990), function()
		Media.PlaySound("RavenholmTrack1.aud")
	end)
	Trigger.AfterDelay(DateTime.Seconds(1100), function()
		Media.PlaySound("RavenholmTrack1.aud")
	end)
	Trigger.AfterDelay(DateTime.Seconds(1210), function()
		Media.PlaySound("RavenholmTrack1.aud")
	end)
	Trigger.AfterDelay(DateTime.Seconds(1320), function()
		Media.PlaySound("RavenholmTrack1.aud")
	end)
	Trigger.AfterDelay(DateTime.Seconds(1430), function()
		Media.PlaySound("RavenholmTrack1.aud")
	end)
	Trigger.AfterDelay(DateTime.Seconds(1540), function()
		Media.PlaySound("RavenholmTrack1.aud")
	end)
	Trigger.AfterDelay(DateTime.Seconds(1650), function()
		Media.PlaySound("RavenholmTrack1.aud")
	end)
	Trigger.AfterDelay(DateTime.Seconds(1760), function()
		Media.PlaySound("RavenholmTrack1.aud")
	end)

--Zombie Triggers

	--rescueWarning

	Trigger.OnEnteredFootprint(RescueWarning, function(a, id)
		if a.Type == "e7.noautotarget" then
			Trigger.RemoveFootprintTrigger(id)
			Trigger.AfterDelay(10, function()
				RescueObjective()
			end)
		end
	end)
end


		Tick = function()
			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
		end