ringBearer = nil
theOneRing = nil
continueTheRingCycle = true
gondorCapitol = capitolminastirith
rohanCapitol  = capitoledoras
eriadorCapitol = capitolfornost
eldarCapitol = capitolmithlond
dwarfCapitol = capitolerebor
daleCapitol = capitoldaletown
mordorCapitol = baraddurtower
isengardCapitol = capitolangernost
rhunCapitol = capitolmistrand
haradCapitol = capitolpezarzan
umbarCapitol = capitolhavensumbar
moriaCapitol = capitolgundabad
settlementNames = {}
settlementNames[1] = "settlement"
settlementNames[2] = "erebor"
settlementNames[3] = "mtdoom"
settlementNames[4] = "minastirithtower"
settlementNames[5] = "baraddurtower"
settlementNames[6] = "orthanctower"
allSettlers = {}
allSettlers[1] = "gondor_settler"
allSettlers[2] = "rohan_settler"
allSettlers[3] = "eriador_settler"
allSettlers[4] = "dale_settler"
allSettlers[5] = "dwarf_settler"
allSettlers[6] = "eldar_settler"
allSettlers[7] = "mordor_settler"
allSettlers[8] = "isengard_settler"
allSettlers[9] = "rhun_settler"
allSettlers[10] = "harad_settler"
allSettlers[11] = "moria_settler"
allSettlers[12] = "umbar_settler"
goodCapitols = 6
evilCapitols = 6
factionKeeperGondor = factgondor
factionKeeperRohan = factrohan
factionKeeperEriador = facteriador
factionKeeperEldar = facteldar
factionKeeperDwarf = factdwarf
factionKeeperDale = factdale
factionKeeperMordor = factmordor
factionKeeperIsengard = factisengard
factionKeeperHarad = factharad
factionKeeperRhun = factrhun
factionKeeperMoria = factmoria
factionKeeperUmbar = factumbar
worldAmbient = 1
worldBlue = 1
worldRed = 1

DestroyTheRing = function()
	Trigger.OnEnteredProximityTrigger(mtdoom.CenterPosition, WDist.FromCells(3), function(a, id)
		if a.Type == "theonering.destroy" then
			local allActors = Player.GetPlayer("Mordor").GetActors()
			for i = 1, #allActors do
				if allActors[i].Type == "settlement" or allActors[i].Type == "erebor" or allActors[i].Type == "mtdoom" or allActors[i].Type == "orthanctower" or allActors[i].Type == "baraddurtower" or allActors[i].Type == "nazgul" or allActors[i].Type == "witchking" then
					allActors[i].Kill()
				elseif allActors[i].Type ~= "player" then
					allActors[i].Owner = Player.GetPlayer("Creeps")
				end
			end
			Media.PlaySound("saurondie.aud")
			Lighting.Flash("LightningStrike", 25)
			Media.DisplayMessage("The Ring has been cast into the fires of Mt. Doom! While the Shadow has passed, his servants and worshippers still remain.")
			a.Kill()
			continueTheRingCycle = false
		end
	end)
end

ReturnTheRing = function()
	Trigger.OnEnteredProximityTrigger(baraddur.CenterPosition, WDist.FromCells(3), function(a, id)
		if a.Type == "theonering.destroy" then
			Player.GetPlayer("Mordor").Cash = Player.GetPlayer("Mordor").Cash + 7500
			a.Kill()
			Media.DisplayMessage("The Ring has been returned to Barad Dur! There is little hope left for the Free Peoples of Middle Earth!")
			baraddur.GrantCondition("ring_returned", 0)
			Media.PlaySound("sauronlaugh.aud")
			Lighting.Flash("DarkStrike", 10)
			DarkenTheWorld()
			continueTheRingCycle = false
		end
	end)
end

DarkenTheWorld = function()
	if worldAmbient > 0.6 then
		Lighting.Ambient = worldAmbient
		Lighting.Blue = worldBlue
		Lighting.Red = worldRed
		worldAmbient = worldAmbient - 0.01
		worldBlue = worldBlue + 0.01
		worldRed = worldRed + 0.001
		Trigger.AfterDelay(5, function()
			DarkenTheWorld()
		end)
	end
end

LightenTheWorld = function()
	if worldAmbient < 1 then
		Lighting.Ambient = worldAmbient
		Lighting.Blue = worldBlue
		Lighting.Red = worldRed
		worldAmbient = worldAmbient + 0.01
		worldBlue = worldBlue - 0.01
		worldRed = worldRed - 0.001
		Trigger.AfterDelay(5, function()
			LightenTheWorld()
		end)
	end
end

Ringbearer = function()
	local text = ""
	text = "" .. ringBearer.TooltipName .. ""
	Trigger.OnKilled(ringBearer, function()
		if continueTheRingCycle == true then
			Media.DisplayMessage("" .. text .. " the Ringbearer has been killed and the One Ring has been dropped!")
			local actor = Actor.Create("theonering", true, { Owner = Player.GetPlayer("Neutral"), Location = ringBearer.Location })
			theOneRing = actor
			RingPicker()
		end
	end)
end

RingPicker = function()
	local text = ""
	Trigger.OnKilled(theOneRing, function(self, picker)
		text = "" .. picker.TooltipName .. ""
		Media.DisplayMessage("" .. text .. " has found the Ring!")
		if picker.AcceptsCondition("ring_corruption") == true then
			picker.GrantCondition("ring_corruption", 24000)
		end
		picker.GrantCondition("has_the_ring")
		ringBearer = picker
		Ringbearer()
	end)
end

CapitolLostGood = function(player, capitolNum)
	Trigger.AfterDelay(2, function()
		local playerSettlements = player.GetActorsByTypes(settlementNames)
		local radius = WDist.FromCells(2)
		local townName = ""
		local text = ""
		if #playerSettlements > 0 then
			local randnum = Utils.RandomInteger(1, #playerSettlements)
			if player.InternalName == "Gondor" then
				gondorCapitol = playerSettlements[randnum]
				gondorCapitol.GrantCondition("capitol")
				local town = Map.ActorsInCircle(gondorCapitol.CenterPosition, radius, function(actor)
					return actor.HasProperty("AcceptsCondition") and actor.AcceptsCondition("im_a_nametag")
				end)[1]
				townName = town.TooltipName
				Trigger.OnKilled(gondorCapitol, function()
					CapitolLostGood(Player.GetPlayer("Gondor", 1))
				end)
			elseif player.InternalName == "Rohan" then
				rohanCapitol = playerSettlements[randnum]
				rohanCapitol.GrantCondition("capitol")
				local town = Map.ActorsInCircle(rohanCapitol.CenterPosition, radius, function(actor)
					return actor.HasProperty("AcceptsCondition") and actor.AcceptsCondition("im_a_nametag")
				end)[1]
				townName = town.TooltipName
				Trigger.OnKilled(rohanCapitol, function()
					CapitolLostGood(Player.GetPlayer("Rohan", 1))
				end)
			elseif player.InternalName == "Eriador" then
				eriadorCapitol = playerSettlements[randnum]
				eriadorCapitol.GrantCondition("capitol")
				local town = Map.ActorsInCircle(eriadorCapitol.CenterPosition, radius, function(actor)
					return actor.HasProperty("AcceptsCondition") and actor.AcceptsCondition("im_a_nametag")
				end)[1]
				townName = town.TooltipName
				Trigger.OnKilled(eriadorCapitol, function()
					CapitolLostGood(Player.GetPlayer("Eriador", 1))
				end)
			elseif player.InternalName == "Dwarves" then
				dwarfCapitol = playerSettlements[randnum]
				dwarfCapitol.GrantCondition("capitol")
				local town = Map.ActorsInCircle(dwarfCapitol.CenterPosition, radius, function(actor)
					return actor.HasProperty("AcceptsCondition") and actor.AcceptsCondition("im_a_nametag")
				end)[1]
				townName = town.TooltipName
				Trigger.OnKilled(dwarfCapitol, function()
					CapitolLostGood(Player.GetPlayer("Dwarves", 1))
				end)
			elseif player.InternalName == "Eldar" then
				eldarCapitol = playerSettlements[randnum]
				eldarCapitol.GrantCondition("capitol")
				local town = Map.ActorsInCircle(eldarCapitol.CenterPosition, radius, function(actor)
					return actor.HasProperty("AcceptsCondition") and actor.AcceptsCondition("im_a_nametag")
				end)[1]
				townName = town.TooltipName
				Trigger.OnKilled(eldarCapitol, function()
					CapitolLostGood(Player.GetPlayer("Eldar", 1))
				end)
			elseif player.InternalName == "Dale" then
				daleCapitol = playerSettlements[randnum]
				daleCapitol.GrantCondition("capitol")
				local town = Map.ActorsInCircle(daleCapitol.CenterPosition, radius, function(actor)
					return actor.HasProperty("AcceptsCondition") and actor.AcceptsCondition("im_a_nametag")
				end)[1]
				townName = town.TooltipName
				Trigger.OnKilled(daleCapitol, function()
					CapitolLostGood(Player.GetPlayer("Dale", 1))
				end)
			end
			text = "" .. player.InternalName .. ""
			Media.DisplayMessage("" .. text .. "'s new capitol is " .. townName .. "")
		else
			text = "" .. player.InternalName .. ""
			Media.DisplayMessage("" .. text .. " has lost all their settlements. Their remaining Settlers have become Refugees. Their remaining armies and heroes will continue on fighting.")
			goodCapitols = goodCapitols - 1
			local allPlayerSettlers = player.GetActorsByTypes(allSettlers)
			for i = 1, #allPlayerSettlers do
				allPlayerSettlers[i].GrantCondition("refugeed")
			end
			if goodCapitols < 1 then
				Media.DisplayMessage("All resistance to Sauron's might has been wiped from Middle Earth. A new age of Darkness has has fallen upon all the land.")
				Trigger.AfterDelay(100, function()
					factionKeeperGondor.Destroy()
					factionKeeperRohan.Destroy()
					factionKeeperEriador.Destroy()
					factionKeeperEldar.Destroy()
					factionKeeperDwarf.Destroy()
					factionKeeperDale.Destroy()
				end)
			end
		end
	end)
end

CapitolLostEvil = function(player, capitolNum)
	Trigger.AfterDelay(2, function()
		local playerSettlements = player.GetActorsByTypes(settlementNames)
		local radius = WDist.FromCells(2)
		local townName = ""
		local text = ""
		if #playerSettlements > 0 then
			local randnum = Utils.RandomInteger(1, #playerSettlements)
			if player.InternalName == "Mordor" then
				mordorCapitol = playerSettlements[randnum]
				mordorCapitol.GrantCondition("capitol")
				local town = Map.ActorsInCircle(mordorCapitol.CenterPosition, radius, function(actor)
					return actor.HasProperty("AcceptsCondition") and actor.AcceptsCondition("im_a_nametag")
				end)[1]
				townName = town.TooltipName
				Trigger.OnKilled(mordorCapitol, function()
					CapitolLostEvil(Player.GetPlayer("Mordor", 1))
				end)
			elseif player.InternalName == "Isengard" then
				isengardCapitol = playerSettlements[randnum]
				isengardCapitol.GrantCondition("capitol")
				local town = Map.ActorsInCircle(isengardCapitol.CenterPosition, radius, function(actor)
					return actor.HasProperty("AcceptsCondition") and actor.AcceptsCondition("im_a_nametag")
				end)[1]
				townName = town.TooltipName
				Trigger.OnKilled(isengardCapitol, function()
					CapitolLostEvil(Player.GetPlayer("Isengard", 1))
				end)
			elseif player.InternalName == "Rhun" then
				rhunCapitol = playerSettlements[randnum]
				rhunCapitol.GrantCondition("capitol")
				local town = Map.ActorsInCircle(rhunCapitol.CenterPosition, radius, function(actor)
					return actor.HasProperty("AcceptsCondition") and actor.AcceptsCondition("im_a_nametag")
				end)[1]
				townName = town.TooltipName
				Trigger.OnKilled(rhunCapitol, function()
					CapitolLostEvil(Player.GetPlayer("Rhun", 1))
				end)
			elseif player.InternalName == "Moria" then
				moriaCapitol = playerSettlements[randnum]
				moriaCapitol.GrantCondition("capitol")
				local town = Map.ActorsInCircle(moriaCapitol.CenterPosition, radius, function(actor)
					return actor.HasProperty("AcceptsCondition") and actor.AcceptsCondition("im_a_nametag")
				end)[1]
				townName = town.TooltipName
				Trigger.OnKilled(moriaCapitol, function()
					CapitolLostEvil(Player.GetPlayer("Moria", 1))
				end)
			elseif player.InternalName == "Harad" then
				haradCapitol = playerSettlements[randnum]
				haradCapitol.GrantCondition("capitol")
				local town = Map.ActorsInCircle(haradCapitol.CenterPosition, radius, function(actor)
					return actor.HasProperty("AcceptsCondition") and actor.AcceptsCondition("im_a_nametag")
				end)[1]
				townName = town.TooltipName
				Trigger.OnKilled(haradCapitol, function()
					CapitolLostEvil(Player.GetPlayer("Harad", 1))
				end)
			elseif player.InternalName == "Umbar" then
				umbarCapitol = playerSettlements[randnum]
				umbarCapitol.GrantCondition("capitol")
				local town = Map.ActorsInCircle(umbarCapitol.CenterPosition, radius, function(actor)
					return actor.HasProperty("AcceptsCondition") and actor.AcceptsCondition("im_a_nametag")
				end)[1]
				townName = town.TooltipName
				Trigger.OnKilled(umbarCapitol, function()
					CapitolLostEvil(Player.GetPlayer("Umbar", 1))
				end)
			end
			text = "" .. player.InternalName .. ""
			Media.DisplayMessage("" .. text .. "'s new capitol is " .. townName .. "")
		else
			text = "" .. player.InternalName .. ""
			Media.DisplayMessage("" .. text .. " has lost all their settlements. Their remaining Settlers have become Refugees. Their remaining armies and heroes will continue on fighting.")
			local allPlayerSettlers = player.GetActorsByTypes(allSettlers)
			for i = 1, #allPlayerSettlers do
				allPlayerSettlers[i].GrantCondition("refugeed")
			end
			evilCapitols = evilCapitols - 1
			if player.InternalName == "Mordor" then
				LightenTheWorld()
			end
			if evilCapitols < 1 then
				Media.DisplayMessage("All Sauron's forces in Middle Earth have been defeated! Peace reigns in Middle Earth and the age of Men has begun!")
				Trigger.AfterDelay(100, function()
					factionKeeperMordor.Destroy()
					factionKeeperIsengard.Destroy()
					factionKeeperRhun.Destroy()
					factionKeeperUmbar.Destroy()
					factionKeeperHarad.Destroy()
					factionKeeperMoria.Destroy()
				end)
			end
		end
	end)
end

GoodCapitols = function ()
	local playerSettlements = Player.GetPlayer("Gondor").GetActorsByTypes(settlementNames)
	if #playerSettlements > 0 then
		Trigger.OnKilled(gondorCapitol, function()
			CapitolLostGood(Player.GetPlayer("Gondor", 1))
		end)
	end
	playerSettlements = Player.GetPlayer("Rohan").GetActorsByTypes(settlementNames)
	if #playerSettlements > 0 then
		Trigger.OnKilled(rohanCapitol, function()
			CapitolLostGood(Player.GetPlayer("Rohan", 2))
		end)
	end
	playerSettlements = Player.GetPlayer("Eriador").GetActorsByTypes(settlementNames)
	if #playerSettlements > 0 then
		Trigger.OnKilled(eriadorCapitol, function()
			CapitolLostGood(Player.GetPlayer("Eriador", 3))
		end)
	end
	playerSettlements = Player.GetPlayer("Eldar").GetActorsByTypes(settlementNames)
	if #playerSettlements > 0 then
		Trigger.OnKilled(eldarCapitol, function()
			CapitolLostGood(Player.GetPlayer("Eldar", 4))
		end)
	end
	playerSettlements = Player.GetPlayer("Dwarves").GetActorsByTypes(settlementNames)
	if #playerSettlements > 0 then
		Trigger.OnKilled(dwarfCapitol, function()
			CapitolLostGood(Player.GetPlayer("Dwarves", 5))
		end)
	end
	playerSettlements = Player.GetPlayer("Dale").GetActorsByTypes(settlementNames)
	if #playerSettlements > 0 then
		Trigger.OnKilled(daleCapitol, function()
			CapitolLostGood(Player.GetPlayer("Dale", 6))
		end)
	end
end

EvilCapitols = function ()
	local playerSettlements = Player.GetPlayer("Mordor").GetActorsByTypes(settlementNames)
	if #playerSettlements > 0 then
		Trigger.OnKilled(mordorCapitol, function()
			CapitolLostEvil(Player.GetPlayer("Mordor", 1))
		end)
	end
	playerSettlements = Player.GetPlayer("Isengard").GetActorsByTypes(settlementNames)
	if #playerSettlements > 0 then
		Trigger.OnKilled(isengardCapitol, function()
			CapitolLostEvil(Player.GetPlayer("Isengard", 2))
		end)
	end
	playerSettlements = Player.GetPlayer("Rhun").GetActorsByTypes(settlementNames)
	if #playerSettlements > 0 then
		Trigger.OnKilled(rhunCapitol, function()
			CapitolLostEvil(Player.GetPlayer("Rhun", 3))
		end)
	end
	playerSettlements = Player.GetPlayer("Harad").GetActorsByTypes(settlementNames)
	if #playerSettlements > 0 then
		Trigger.OnKilled(haradCapitol, function()
			CapitolLostEvil(Player.GetPlayer("Harad", 4))
		end)
	end
	playerSettlements = Player.GetPlayer("Umbar").GetActorsByTypes(settlementNames)
	if #playerSettlements > 0 then
		Trigger.OnKilled(umbarCapitol, function()
			CapitolLostEvil(Player.GetPlayer("Umbar", 5))
		end)
	end
	playerSettlements = Player.GetPlayer("Moria").GetActorsByTypes(settlementNames)
	if #playerSettlements > 0 then
		Trigger.OnKilled(moriaCapitol, function()
			CapitolLostEvil(Player.GetPlayer("Moria", 6))
		end)
	end
end

WorldLoaded = function()
	ringBearer = Player.GetPlayer("Eriador").GetActorsByType("frodosam")[1]
	Ringbearer()
	DestroyTheRing()
	ReturnTheRing()
	gondorCapitol.GrantCondition("capitol")
	rohanCapitol.GrantCondition("capitol")
	eriadorCapitol.GrantCondition("capitol")
	eldarCapitol.GrantCondition("capitol")
	dwarfCapitol.GrantCondition("capitol")
	daleCapitol.GrantCondition("capitol")
	mordorCapitol.GrantCondition("capitol")
	isengardCapitol.GrantCondition("capitol")
	rhunCapitol.GrantCondition("capitol")
	haradCapitol.GrantCondition("capitol")
	moriaCapitol.GrantCondition("capitol")
	umbarCapitol.GrantCondition("capitol")
	GoodCapitols()
	EvilCapitols()
	Trigger.OnKilled(factionKeeperMordor, function()
		LightenTheWorld()
	end)
end