
BlueLight = 1
GreenLight = 1
WarStarts = false
SetPeaceTime = Map.LobbyOption("peacetime")

Warningalert = function()
	Lighting.Blue = BlueLight
	Lighting.Green = GreenLight
	if not WarStarts then
		if Lighting.Blue == 1 then
			GettingRed = true
--			Media.PlaySoundNotification( allplayers, "AlertBuzzer")
		end
		if BlueLight >= 0.2 and GettingRed then
			BlueLight = BlueLight - 0.05
			GreenLight = GreenLight - 0.05
		elseif BlueLight <= 1 then
			BlueLight = BlueLight + 0.05
			GreenLight = GreenLight + 0.05
			GettingRed = false
		end
		Trigger.AfterDelay(1, function()
			Warningalert()
		end)
	end
	if WarStarts and GreenLight <= 1 then
		BlueLight = BlueLight + 0.1
		GreenLight = GreenLight + 0.1
		Trigger.AfterDelay(1, function()
			Warningalert()
		end)
	end
end

PeaceContractEnds = function()
	WarStarts = true
	Barb = Creeps.GetActorsByType("abarb")
	for p,fences in pairs(Barb) do
		fences.Kill()
	end
	Trigger.AfterDelay(5, function()
		AA = Creeps.GetActorsByType("aagun")
		for p,aaguns in pairs(AA) do
			aaguns.Kill()
		end
	end)
	Trigger.AfterDelay(10, function()
		Gap = Creeps.GetActorsByType("agap")
		for p,generators in pairs(Gap) do
			generators.Kill()
		end
	end)
	Trigger.AfterDelay(30, function()
		if Multi0 ~= nil then Wartime = Actor.Create("wartime", true, { Location = (CPos.New(0,0)), Owner = Multi0 }) end
		if Multi1 ~= nil then Wartime = Actor.Create("wartime", true, { Location = (CPos.New(0,0)), Owner = Multi1 }) end
		if Multi2 ~= nil then Wartime = Actor.Create("wartime", true, { Location = (CPos.New(0,0)), Owner = Multi2 }) end
		if Multi3 ~= nil then Wartime = Actor.Create("wartime", true, { Location = (CPos.New(0,0)), Owner = Multi3 }) end
		if Multi4 ~= nil then Wartime = Actor.Create("wartime", true, { Location = (CPos.New(0,0)), Owner = Multi4 }) end
		if Multi5 ~= nil then Wartime = Actor.Create("wartime", true, { Location = (CPos.New(0,0)), Owner = Multi5 }) end
		if Multi6 ~= nil then Wartime = Actor.Create("wartime", true, { Location = (CPos.New(0,0)), Owner = Multi6 }) end
		if Multi7 ~= nil then Wartime = Actor.Create("wartime", true, { Location = (CPos.New(0,0)), Owner = Multi7 }) end
	end)
end



TimeConditions = function()
	if SetPeaceTime == "5min" then
		PeaceTime = DateTime.Minutes(5)
	end
	if SetPeaceTime == "10min" then
		PeaceTime = DateTime.Minutes(10)
	end
	if SetPeaceTime == "15min" then
		PeaceTime = DateTime.Minutes(15)
		Trigger.AfterDelay(DateTime.Minutes(10), function()
			local Mines = Neutral.GetActorsByType("mine")
			for p,mine in pairs(Mines) do
				mine.GrantCondition("charging", 3000)
			end
		end)
	end
	if SetPeaceTime == "20min" then
		PeaceTime = DateTime.Minutes(20)
		Trigger.AfterDelay(DateTime.Minutes(10), function()
			local Mines = Neutral.GetActorsByType("mine")
			for p,mine in pairs(Mines) do
				mine.GrantCondition("charging", 6000)
			end
		end)
	end
	if SetPeaceTime == "30min" then
		PeaceTime = DateTime.Minutes(30)
		Trigger.AfterDelay(DateTime.Minutes(10), function()
			local Mines = Neutral.GetActorsByType("mine")
			for p,mine in pairs(Mines) do
				mine.GrantCondition("charging", 3000)
			end
		end)
		Trigger.AfterDelay(DateTime.Minutes(20), function()
			local Mines2 = Neutral.GetActorsByType("mine")
			for p,mine2 in pairs(Mines2) do
				mine2.GrantCondition("charging2", 6000)
			end
		end)
	end
	if SetPeaceTime == "40min" then
		PeaceTime = DateTime.Minutes(40)
		Trigger.AfterDelay(DateTime.Minutes(10), function()
			local Mines = Neutral.GetActorsByType("mine")
			for p,mine in pairs(Mines) do
				mine.GrantCondition("charging", 3000)
			end
		end)
		Trigger.AfterDelay(DateTime.Minutes(20), function()
			local Mines2 = Neutral.GetActorsByType("mine")
			for p,mine2 in pairs(Mines2) do
				mine2.GrantCondition("charging2", 6000)
			end
		end)
		Trigger.AfterDelay(DateTime.Minutes(30), function()
			local Mines3 = Neutral.GetActorsByType("mine")
			for p,mine3 in pairs(Mines3) do
				mine3.GrantCondition("charging3", 6000)
			end
		end)
	end
	if SetPeaceTime == "50min" then
		PeaceTime = DateTime.Minutes(50)
		Trigger.AfterDelay(DateTime.Minutes(10), function()
			local Mines = Neutral.GetActorsByType("mine")
			for p,mine in pairs(Mines) do
				mine.GrantCondition("charging", 3000)
			end
		end)
		Trigger.AfterDelay(DateTime.Minutes(20), function()
			local Mines2 = Neutral.GetActorsByType("mine")
			for p,mine2 in pairs(Mines2) do
				mine2.GrantCondition("charging2", 6000)
			end
		end)
		Trigger.AfterDelay(DateTime.Minutes(30), function()
			local Mines3 = Neutral.GetActorsByType("mine")
			for p,mine3 in pairs(Mines3) do
				mine3.GrantCondition("charging3", 6000)
			end
		end)
		Trigger.AfterDelay(DateTime.Minutes(40), function()
			local Mines4 = Neutral.GetActorsByType("mine")
			for p,mine4 in pairs(Mines4) do
				mine4.GrantCondition("charging4", 6000)
			end
		end)
	end
	if SetPeaceTime == "60min" then
		PeaceTime = DateTime.Minutes(60)
		Trigger.AfterDelay(DateTime.Minutes(10), function()
			local Mines = Neutral.GetActorsByType("mine")
			for p,mine in pairs(Mines) do
				mine.GrantCondition("charging", 3000)
			end
		end)
		Trigger.AfterDelay(DateTime.Minutes(20), function()
			local Mines2 = Neutral.GetActorsByType("mine")
			for p,mine2 in pairs(Mines2) do
				mine2.GrantCondition("charging2", 6000)
			end
		end)
		Trigger.AfterDelay(DateTime.Minutes(30), function()
			local Mines3 = Neutral.GetActorsByType("mine")
			for p,mine3 in pairs(Mines3) do
				mine3.GrantCondition("charging3", 6000)
			end
		end)
		Trigger.AfterDelay(DateTime.Minutes(40), function()
			local Mines4 = Neutral.GetActorsByType("mine")
			for p,mine4 in pairs(Mines4) do
				mine4.GrantCondition("charging4", 6000)
			end
		end)
		Trigger.AfterDelay(DateTime.Minutes(50), function()
			local Mines5 = Neutral.GetActorsByType("mine")
			for p,mine5 in pairs(Mines5) do
				mine5.GrantCondition("charging5", 6000)
			end
		end)
	end
end

SetupPlayers = function()
	Neutral = Player.GetPlayer("Neutral")
	Creeps = Player.GetPlayer("Creeps")
	Multi0 = Player.GetPlayer("Multi0")
	Multi1 = Player.GetPlayer("Multi1")
	Multi2 = Player.GetPlayer("Multi2")
	Multi3 = Player.GetPlayer("Multi3")
	Multi4 = Player.GetPlayer("Multi4")
	Multi5 = Player.GetPlayer("Multi5")
	Multi6 = Player.GetPlayer("Multi6")
	Multi7 = Player.GetPlayer("Multi7")
end


function WorldLoaded()
	SetupPlayers()
	TimeConditions()
	Media.DisplayMessage("Peace will be warranted by the borders, please dont even try to remove or cross them.\nThe peace contract ends  in " .. Utils.FormatTime(PeaceTime) .. " minutes", "UN", White)
	WarningTime = PeaceTime - DateTime.Minutes(1)
	Trigger.AfterDelay(WarningTime, function()
		Trigger.AfterDelay(DateTime.Seconds(55), function()
			Media.PlaySound("purge.aud")
			Warningalert()
		end)
	end)
	Trigger.AfterDelay(PeaceTime, function()
		PeaceContractEnds()
	end)
end

Red = HSLColor.Red
White = HSLColor.White

Tick = function()
	if PeaceTime > DateTime.Minutes(1) then
		TimerColor = White
	else TimerColor = Red end

	if PeaceTime == DateTime.Minutes(5) and SetPeaceTime ~= "5min" then
		Media.PlaySpeechNotification( allplayers, "WarningFiveMinutesRemaining")
	end
	if PeaceTime < DateTime.Seconds(300) and PeaceTime > DateTime.Seconds(298) and SetPeaceTime ~= "5min" then
		TimerColor = Red
	end
	if PeaceTime == DateTime.Minutes(4) then
		Media.PlaySpeechNotification( allplayers, "WarningFourMinutesRemaining")
	end
	if PeaceTime < DateTime.Seconds(240) and PeaceTime > DateTime.Seconds(238) then
		TimerColor = Red
	end
	if PeaceTime == DateTime.Minutes(3) then
		Media.PlaySpeechNotification( allplayers, "WarningThreeMinutesRemaining")
	end
	if PeaceTime < DateTime.Seconds(180) and PeaceTime > DateTime.Seconds(178) then
		TimerColor = Red
	end
	if PeaceTime == DateTime.Minutes(2) then
		Media.PlaySpeechNotification( allplayers, "WarningTwoMinutesRemaining")
	end
	if PeaceTime < DateTime.Seconds(120) and PeaceTime > DateTime.Seconds(118) then
		TimerColor = Red
	end
	if PeaceTime == DateTime.Minutes(1) then
		Media.PlaySpeechNotification( allplayers, "WarningOneMinuteRemaining")
	end
	if PeaceTime >= 0 then
		UserInterface.SetMissionText("Borders will be removed in: " .. Utils.FormatTime(PeaceTime) .. " minutes", TimerColor)
		PeaceTime = PeaceTime - 1
	else
		UserInterface.SetMissionText(" ", TimerColor)
	end
end


