
FirstFlagMessage = { "'s first flag has been captured he has still 2 more!", " lost the first of his 3 flags all fine for now!", " one of yor flags has been captured but you still got 2 remaining flags now!", " better take care of your flags 2 more captured and you will die!", " do you see what he's doing with your flag? Better make something", " its all fine you still got 2 more flags ...", " better dont be so sloppy one of your flags is lost!", " 1/3 flags are destroyed!", " 1/3 has been captured!", " 1/3 flags has been captured!", " 1 of 3 flags is lost!", " 1/3 flags are destroyed!", " 1/3 has been captured!", " 1/3 flags has been captured!", " 1 of 3 flags is lost!", " 1/3 flags are destroyed!", " 1/3 has been captured!", " 1/3 flags has been captured!", " 1 of 3 flags is lost!" }
SecondFlagMessage = { " The RED ALERT is for you, only 1 flag left!", " what are you doing? dont let them steal your flags only 1 remaining flag now!", " dont get frighten but you only have 1 flag over now!", " you seem to be a bit lame today or why do you let the other player steal your flags?", " 2 of 3 flags burned!", " 2/3 flags captured!", " has only 1 remaining flag!", " let them steal one more flag and you loose!", " 2 of 3 flags burned!", " 2/3 flags captured!", " has only 1 remaining flag!", " let them steal one more flag and you loose!", " do you smell the death?", " 2 of 3 flags burned!", " 2/3 flags got captured!", " has only 1 remaining flag!", " better you dont let them steal your last flag!", " 2 of 3 flags burned!", " 2/3 flags captured!", " has only 1 remaining flag!" }
Year = 2022
Mode = Map.LobbyOption("ctf")
Season = Map.LobbyOption("season")
Teleporter = Map.LobbyOption("teleporter")
White = HSLColor.White
RedLight = 1
BlueLight = 1
GreenLight = 1
AlertStarts = false
AlreadyAlert = false
messageshown = false
RedCounter = 0
Warningalert = function()
	Lighting.Blue = BlueLight
	Lighting.Green = GreenLight
	if not AlertStarts then
		GettingRed = true
		Media.PlaySoundNotification( allplayers, "AlertBuzzer")
	end
	if BlueLight >= 0.35 and GettingRed then
		AlertStarts = true
		BlueLight = BlueLight - 0.05
		GreenLight = GreenLight - 0.05
		RedCounter = RedCounter + 1
		Trigger.AfterDelay(1, function()
			Warningalert()
		end)
	elseif RedCounter >= 1 then
		BlueLight = BlueLight + 0.05
		GreenLight = GreenLight + 0.05
		GettingRed = false
		RedCounter = RedCounter - 1
		Trigger.AfterDelay(1, function()
			Warningalert()
		end)
	end
end

--																 Flagfunctions

GetandsetFlagsRepeater = function()
	Trigger.AfterDelay(DateTime.Seconds(3), function()
		FuseFlag1Cell()
		FuseFlag2Cells()
		if Mode ~= "temprasphere" then
			FuseFlag7Cells()
		end
		GetandsetFlagsRepeater()
	end)
end

FuseFlag1Cell = function()
	Utils.Do(Humans, function(player)
		local flagactors = player.GetActorsByType("ctflag")
		for p,flag in pairs(flagactors) do
			if not flag.HasTag("isfused1") then
				flag.AddTag("isfused1")
				Trigger.OnEnteredProximityTrigger(flag.CenterPosition, WDist.FromCells(1), function(a, id)
					local mine = flag.Owner
					local yours = a.Owner
					if a.HasTag("flagholdingunit") and mine == yours then
						a.Stop()
						a.Kill()
					elseif mine ~= yours and a.Type == "tflag" and mine.IsAlliedWith(yours) == true then
						NewPositionFlag = Actor.Create("ctflag", true, { Location = a.Location + CVec.New(Utils.RandomInteger(-1, 2), 1), Owner = yours })
						a.Destroy()
					elseif mine ~= yours and a.Type ~= "mcv" and a.Type ~= "truk" and a.Type ~= "ctfmcv" and a.Type ~= "dpctflag" and a.HasProperty("GrantCondition") and a.AcceptsCondition("flagholder") and mine ~= Neutral and not a.HasTag("flagholdingunit") then
						if Mode ~= "temprasphere" and mine.IsAlliedWith(yours) == true then
							return
						else
							Trigger.RemoveProximityTrigger(id)
							if mine.IsLocalPlayer then
								SetCamera(mine)
							end
							Beacon.New(mine, a.CenterPosition, 100, true)
							a.AddTag("flagholdingunit")
							a.GrantCondition("flagholder", 0)
							a.GrantCondition("hold" .. mine.InternalName .. "flag", 0)
							if Mode ~= "hawkie" then
								if DateTime.GameTime < DateTime.Minutes(5) then
									a.GrantCondition("snailspeed", 0)
								elseif DateTime.GameTime < DateTime.Minutes(7) then
									a.GrantCondition("turtlespeed", 0)
								else
									a.GrantCondition("frogspeed", 0)
								end
							end
							flag.Destroy()
							Trigger.OnKilled(a, function()
								if not mine.HasNoRequiredUnits() then
									Flag = Actor.Create("tflag", true, { Location = a.Location, Owner = mine })
								end
							end)
						end
					end
				end)
			end
		end
	end)
end

FuseFlag2Cells = function()
	Utils.Do(Humans, function(player)
		local flagactors = player.GetActorsByType("ctflag")
		for p,flag in pairs(flagactors) do
			if not flag.HasTag("isfused2") then
				flag.AddTag("isfused2")
				Trigger.OnEnteredProximityTrigger(flag.CenterPosition, WDist.FromCells(2), function(a, id)
					local mine = flag.Owner
					local yours = a.Owner
					if flag.IsDead then
						Trigger.RemoveProximityTrigger(id)
						return
					end
					if Mode ~= "temprasphere" then
						if a.Type == "sbag" or a.Type == "fenc" then
							yours.Cash = yours.Cash + 10
							Media.FloatingText("10 $", a.CenterPosition, 40, yours.Color)
							a.Destroy()
						end
					end
					if a.Type == "tflag" and mine.IsAlliedWith(yours) ~= true then
						if Mode ~= "hawkie" then
							Media.DisplayMessage("" .. mine.Name .. " captured " .. yours.Name .. "s flag!", "God", White)
						end
						DeadplayersFlag = Actor.Create("dpctflag", true, { Location = a.Location + CVec.New(Utils.RandomInteger(-1, 2), Utils.RandomInteger(0, 2)), Owner = yours })
						Media.FloatingText("+ " .. (mine.Experience/2) .. " Exp Bonus", flag.CenterPosition, 85, mine.Color)
						mine.Experience = mine.Experience + mine.Experience/2
						a.Destroy()
						Trigger.AfterDelay(DateTime.Seconds(1), function()
							if Mode == "hawkie" then
								Lives = yours.GetActorsByTypes({"live1", "live2"})
								for i,v in pairs(Lives) do
									if v.Type == "live1" then
										RandomMessage = Utils.Random(FirstFlagMessage)
										Newflag = Actor.Create("ctflag", true, { Location = v.Location + CVec.New(1, 3), Owner = yours })
										Media.DisplayMessage("" .. yours.Name .. "" .. RandomMessage .. "", "God", White)
										v.Destroy()
										return
									elseif v.Type == "live2" then
										RandomMessage = Utils.Random(SecondFlagMessage)
										Newflag = Actor.Create("ctflag", true, { Location = v.Location + CVec.New(-1, 3), Owner = yours })
										Media.DisplayMessage("" .. yours.Name .. "" .. RandomMessage .. "", "God", White)
										v.Destroy()
										if not AlreadyAlert then
											AlreadyAlert = true
											Warningalert()
										end
										Trigger.AfterDelay(DateTime.Seconds(5), function()
											AlreadyAlert = false
											AlertStarts = false
										end)
										return
									end
								end
							end
							allactors = yours.GetActors()
							for i,v in pairs(allactors) do
								if v.HasProperty("Health") and v.Type ~= "dpctflag" then
									v.Stop()
									if Mode ~= "temprasphere" then
										if not messageshown then
											Media.DisplayMessage("All your Base belong to me " .. yours.Name .. " !", mine.Name, mine.Color)
											messageshown = true
										end
										Trigger.AfterDelay(DateTime.Seconds(1), function()
											messageshown = false
										end)
										v.Owner = mine
									else
										v.Kill()
									end
								end
							end
						end)
					end
				end)
			end
		end
	end)
end

FuseFlag7Cells = function()
	Utils.Do(Humans, function(player)
		local flagactors = player.GetActorsByType("ctflag")
		for p,flag in pairs(flagactors) do
			if not flag.HasTag("isfused3") then
				flag.AddTag("isfused3")
				Trigger.OnEnteredProximityTrigger(flag.CenterPosition, WDist.FromCells(7), function(a, id)
					if flag.IsDead then
						Trigger.RemoveProximityTrigger(id)
						return
					elseif a.Type == "brik" and a.Owner ~= Neutral then
						local yours = a.Owner
						yours.Cash = yours.Cash + 15
						Media.FloatingText("15 $", a.CenterPosition, 40, yours.Color)
						a.Destroy()
					end
				end)
			end
		end
	end)
end
SetCamera = function(Multi)
	local PlayerFlag = Multi.GetActorsByType("ctflag")[1]
	if PlayerFlag ~= nil then
		Camera.Position = PlayerFlag.CenterPosition
	end
end
StartingUnitsCheck = function()
	Utils.Do(Humans, function(player)
		MCV = player.GetActorsByType("mcv")
		for p,mobcon in pairs(MCV) do
			if Mode ~= "hawkie" then
				CTFMCV = Actor.Create("ctfmcv", true, { Location = mobcon.Location, Owner = mobcon.Owner, Facing = Angle.South })
				CTFMCV.GrantCondition("frogspeed", 0)
			else
				CTFMCV = Actor.Create("ct3fmcv", true, { Location = mobcon.Location, Owner = mobcon.Owner, Facing = Angle.South })
			end
			CTFMCV.GrantCondition("flagholder", 0)
			CTFMCV.GrantCondition("hold" .. CTFMCV.Owner.InternalName .. "flag", 0)
			if CTFMCV.Owner.IsBot then
				CTFMCV.Deploy()
			end
			mobcon.Destroy()
		end
	end)
end

ATWNotifications = function()
	if Mode ~= "off" then
		Trigger.AfterDelay(DateTime.Seconds(36), function()
			Media.DisplayMessage("Around the World mode is activated.\nThe arrows show you where your units can go to the other side of the World!", "God", White)
		end)
	end
end

FlagNotifications = function()
	Trigger.AfterDelay(DateTime.Seconds(44), function()
		Media.DisplayMessage("\n\n   This map features ERCC refinery 1.3 by Widow", "God", White)
	end)
	if Mode ~= "noctf" then
			GetandsetFlagsRepeater()
			StartingUnitsCheck()
	end
	Trigger.AfterDelay(DateTime.Seconds(3), function()
		if Mode ~= "noctf" then
			GetandsetFlagsRepeater()
			StartingUnitsCheck()
			if Mode == "materia" then
				Media.DisplayMessage("Capture The Flag\nFFA", "God", White)
			elseif Mode == "temprasphere" then
				Media.DisplayMessage("Capture The Flag\nTeamgame", "God", White)
			end
		end
		Trigger.AfterDelay(DateTime.Seconds(3), function()
			if Mode == "hawkie" then
				Media.DisplayMessage("3flags mode\nEach player has 3 flags but only 1 can be stolen at a time.", "God", White)

			elseif Mode ~= "noctf" then
				Media.DisplayMessage("Capture the enemys flag with any vehicle and bring it to your own flag", "God", White)
				if Mode == "temprasphere" then
					Trigger.AfterDelay(DateTime.Seconds(2), function()
						Media.DisplayMessage("to defeat him!", "God", White)
					end)
				elseif Mode ~= "noctf" then
					Trigger.AfterDelay(DateTime.Seconds(2), function()
						Media.DisplayMessage("to get all his stuff!", "God", White)
					end)
				end
			end
			Trigger.AfterDelay(DateTime.Seconds(7), function()
				if Mode == "hawkie" then
					Media.DisplayMessage("there are no speedlimitations for the flagholding unit except of chronotanks\n\nMCV and supply trucks cant steal flags.", "God", White)
				elseif Mode ~= "noctf" then
					Media.DisplayMessage("The speed of vehicles wich carry the flag will be reduced!\n\nIf it is destroyed it will throw the flag to the ground!", "God", White)
				end
				Trigger.AfterDelay(DateTime.Seconds(7), function()
					if Mode ~= "hawkie" and Mode ~= "noctf" then Media.DisplayMessage("Iron curtain and chronosphere doesnt work for the flagholding unit!", "God", White) end
					Trigger.AfterDelay(DateTime.Seconds(6), function()
						if Mode == "materia" then
							Media.DisplayMessage("No concrete walls can be build in the rangecircle around the flag.", "God", White)
						elseif Mode == "temprasphere" then
							Media.DisplayMessage("Teammates can carry your flag\nThere are no buildrestrictions for walls.", "God", White)
						elseif Mode == "hawkie" then
							Media.DisplayMessage("Concrete walls can not be build around your flag in a 7 cell radius and teammates cant take your flag.", "God", White)
						end
					end)
				end)
			end)
		end)
	end)
end


--																 Weatherfunctions

InitialiseWeather = function()
	if Season == "spring" then
		Month = 3
		Light = 0.91
		RedLight = 0.95
		GreenLight = 1.07
		BlueLight = 1.07
	elseif Season == "summer" then
		Month = 6
		Light = 0.93
		RedLight = 1.09
		GreenLight = 0.94
		BlueLight = 0.97
	elseif Season == "fall" or Season == "defmode" then
		Month = 9
		Light = 0.89
		RedLight = 1.05
		GreenLight = 0.93
		BlueLight = 1.04
	elseif Season == "winter"then
		StartingWinter()
		Month = 12
		Light = 0.88
		RedLight = 1.03
		GreenLight = 1.07
		BlueLight = 1.08
	end
	ColorChangeSpeed = 28
	MonthConditions()
	ColorChanger()
	if Month > 7 then
		turnOff()
	else
		turnOn()
	end
end
Season = Map.LobbyOption("season")
RateOfIncrease = 0.001

AllTrees = {"t03", "t10", "t11", "t12", "t13", "t14", "t15", "t17", "tc02", "tc05"}
AllNeedleTrees = {"t01", "t02", "t05", "t06", "t07", "t08", "t16", "tc01", "tc03", "tc04"}
AllBuildings = {"v01", "v02", "v03", "v04", "v05", "v06", "v07", "v08", "v09", "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18"}
AllTNumbers = {"01", "02", "03", "04", "05", "06", "07", "08", "10", "11", "12", "13", "14", "15", "16", "17", "18"}
AllTCNumbers = {"01", "02", "03", "04", "05"}
AllVNumbers = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18"}
AllWinterStufft = {"t01winter", "t02winter", "t03winter", "t05winter", "t06winter", "t07winter", "t08winter", "t10winter", "t11winter", "t12winter", "t13winter", "t14winter", "t15winter", "t16winter", "t17winter"}
AllWinterStufftc = {"tc01winter", "tc02winter", "tc03winter", "tc04winter", "tc05winter"}
AllWinterStuffv = {"v01winter", "v02winter", "v03winter", "v04winter", "v05winter", "v06winter", "v07winter", "v08winter", "v09winter", "v10winter", "v11winter", "v12winter", "v13winter", "v14winter", "v15winter", "v16winter", "v17winter", "v18winter"}

AprilTrees = function()
	for i=1,#AllTrees do
		if i ~= nil then
			local Trees = Neutral.GetActorsByType(AllTrees[i])
			for p,tree in pairs(Trees) do
				Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(1, 210)), function()
					if not tree.IsDead and tree.IsInWorld and tree.HasProperty("GrantCondition") and tree.AcceptsCondition("spring2") then
						tree.GrantCondition("spring2", 0)
					end
				end)
			end
		end
	end
end

MaiTrees = function()
	for i=1,#AllTrees do
		if i ~= nil then
			local Trees = Neutral.GetActorsByType(AllTrees[i])
			for p,tree in pairs(Trees) do
				Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(1, 380)), function()
					if not tree.IsDead and tree.IsInWorld and tree.HasProperty("GrantCondition") and tree.AcceptsCondition("summer") then
						tree.GrantCondition("summer", 0)
					end
				end)
			end
		end
	end
end

SeptemberTrees = function()
	for i=1,#AllTrees do
		if i ~= nil then
			local Trees = Neutral.GetActorsByType(AllTrees[i])
			for p,tree in pairs(Trees) do
				Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(1, 290)), function()
					if not tree.IsDead and tree.IsInWorld and tree.HasProperty("GrantCondition") and tree.AcceptsCondition("fall") then
						tree.GrantCondition("fall", 0)
					end
				end)
			end
		end
	end
end

OktoberTrees = function()
	for i=1,#AllTrees do
		if i ~= nil then
			local Trees = Neutral.GetActorsByType(AllTrees[i])
			for p,tree in pairs(Trees) do
				Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(1, 320)), function()
					if not tree.IsDead and tree.IsInWorld and tree.HasProperty("GrantCondition") and tree.AcceptsCondition("fall2") then
						tree.GrantCondition("fall2", 0)
					end
				end)
			end
		end
	end
end

NovemberTrees = function()
	for i=1,#AllTrees do
		if i ~= nil then
			local Trees = Neutral.GetActorsByType(AllTrees[i])
			for p,tree in pairs(Trees) do
				Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(1, 30)), function()
					if not tree.IsDead and tree.IsInWorld then
						Newtree = Actor.Create(tree.Type .. "winter" , true, { Location = tree.Location, Owner = Neutral })
						tree.Destroy()
					end
				end)
			end
		end
	end
end

StartingWinter = function()
	for i=1,#AllTrees do
		if i ~= nil then
			local Trees = Neutral.GetActorsByType(AllTrees[i])
			for p,tree in pairs(Trees) do
				Newtree = Actor.Create(tree.Type .. "winter" , true, { Location = tree.Location, Owner = Neutral })
				tree.Destroy()
			end
		end
	end
	for i=1,#AllNeedleTrees do
		if i ~= nil then
			local Trees = Neutral.GetActorsByType(AllNeedleTrees[i])
			for p,tree in pairs(Trees) do
				Newtree = Actor.Create(tree.Type .. "winter" , true, { Location = tree.Location, Owner = Neutral })
				tree.Destroy()
			end
		end
	end
	for i=1,#AllBuildings do
		if i ~= nil then
			local Trees = Neutral.GetActorsByType(AllBuildings[i])
			for p,tree in pairs(Trees) do
				Newtree = Actor.Create(tree.Type .. "winter" , true, { Location = tree.Location, Owner = Neutral })
				tree.Destroy()
			end
		end
	end
end

NeedleTrees = function()
	for i=1,#AllNeedleTrees do
		if i ~= nil then
			local Trees = Neutral.GetActorsByType(AllNeedleTrees[i])
			for p,tree in pairs(Trees) do
				Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(1, 30)), function()
					if not tree.IsDead and tree.IsInWorld then
						Newtree = Actor.Create(tree.Type .. "winter" , true, { Location = tree.Location, Owner = Neutral })
						tree.Destroy()
					end
				end)
			end
		end
	end
	for i=1,#AllBuildings do
		if i ~= nil then
			local Trees = Neutral.GetActorsByType(AllBuildings[i])
			for p,tree in pairs(Trees) do
				Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(1, 30)), function()
					if not tree.IsDead and tree.IsInWorld then
						Newtree = Actor.Create(tree.Type .. "winter" , true, { Location = tree.Location, Owner = Neutral })
						tree.Destroy()
					end
				end)
			end
		end
	end
end

ChangeSpringTrees = function()
	for u=1,#AllWinterStufft do
		if u ~= nil then
			local Trees = Neutral.GetActorsByType(AllWinterStufft[u])
			for p,tree in pairs(Trees) do
				for i=1,#AllTNumbers do
					if tree.Type == "t0" .. i .. "winter" then
						Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(1, 25)), function()
							if not tree.IsDead and tree.IsInWorld then
								Newtree = Actor.Create("t0" .. i .. "" , true, { Location = tree.Location, Owner = Neutral })
								if Newtree.HasProperty("GrantCondition") and Newtree.AcceptsCondition("spring") then
									Newtree.GrantCondition("spring", 0)
								end
								tree.Destroy()
							end
						end)
					elseif tree.Type == "t" .. i .. "winter" then
						Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(1, 25)), function()
							if not tree.IsDead and tree.IsInWorld then
								Newtree = Actor.Create("t" .. i .. "" , true, { Location = tree.Location, Owner = Neutral })
								if Newtree.HasProperty("GrantCondition") and Newtree.AcceptsCondition("spring") then
									Newtree.GrantCondition("spring", 0)
								end
								tree.Destroy()
							end
						end)
					end
				end
			end
		end
	end
	for u=1,#AllWinterStufftc do
		if u ~= nil then
			local Trees = Neutral.GetActorsByType(AllWinterStufftc[u])
			for p,tree in pairs(Trees) do
				for i=1,#AllTCNumbers do
					if tree.Type == "tc0" .. i .. "winter" then
						Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(1, 25)), function()
							if not tree.IsDead and tree.IsInWorld then
								Newtree = Actor.Create("tc0" .. i .. "" , true, { Location = tree.Location, Owner = Neutral })
								if Newtree.HasProperty("GrantCondition") and Newtree.AcceptsCondition("spring") then
									Newtree.GrantCondition("spring", 0)
								end
								tree.Destroy()
							end
						end)
					end
				end
			end
		end
	end
	for u=1,#AllWinterStuffv do
		if u ~= nil then
			local Trees = Neutral.GetActorsByType(AllWinterStuffv[u])
			for p,tree in pairs(Trees) do
				for i=1,#AllVNumbers do
					if tree.Type == "v" .. i .. "winter" then
						Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(1, 25)), function()
							if not tree.IsDead and tree.IsInWorld then
								Newtree = Actor.Create("v" .. i .. "" , true, { Location = tree.Location, Owner = Neutral })
								tree.Destroy()
							end
						end)
					elseif tree.Type == "v0" .. i .. "winter" then
						Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(1, 25)), function()
							if not tree.IsDead and tree.IsInWorld then
								Newtree = Actor.Create("v0" .. i .. "" , true, { Location = tree.Location, Owner = Neutral })
								tree.Destroy()
							end
						end)
					end
				end
			end
		end
	end
end


StartingSpringTrees = function()
	for i=1,#AllTrees do
		if i ~= nil then
			local Trees = Neutral.GetActorsByType(AllTrees[i])
			for p,tree in pairs(Trees) do
				if not tree.IsDead and tree.IsInWorld and tree.HasProperty("GrantCondition") and tree.AcceptsCondition("spring") then
					tree.GrantCondition("spring", 0)
				end
			end
		end
	end
end

MonthConditions = function()
	if Month == 1 then
		BrightnessChangeSpeed = 7
		ColorChangeSpeed = 16
		BrightestLight = 0.92
		DarkestLight = 0.55
		DayDuration = 5000
		NightDuration = 6000
		DesiredRed = 1
		DesiredGreen = 1
		DesiredBlue = 1
		MonthName = "Januar"
		ThunderRiskfactor = { 1, 2, 3 }
		TimerColor = HSLColor.Cyan
	elseif Month == 2 then
		Firstyear = true
		BrightnessChangeSpeed = 8
		ColorChangeSpeed = 18
		BrightestLight = 0.94
		DarkestLight = 0.58
		DayDuration = 5400
		NightDuration = 5500
		DesiredRed = 1.15
		DesiredGreen = 0.91
		DesiredBlue = 1.18
		MonthName = "Februar"
		ThunderRiskfactor = { 1, 2, 3 }
		TimerColor = HSLColor.Blue
--												Spring
	elseif Month == 3 then
		if not Firstyear then
			Firstyear = true
			StartingSpringTrees()
		else
			ChangeSpringTrees()
		end
		BrightnessChangeSpeed = 9
		ColorChangeSpeed = 20
		BrightestLight = 0.96
		DarkestLight = 0.60
		DayDuration = 5800
		NightDuration = 5000
		DesiredRed = 0.97
		DesiredGreen = 1.10
		DesiredBlue = 1.05
		MonthName = "Maerz"
		ThunderRiskfactor = { 1, 2, 3 }
		TimerColor = HSLColor.LawnGreen
	elseif Month == 4 then
		BrightnessChangeSpeed = 10
		ColorChangeSpeed = 22
		BrightestLight = 0.98
		DarkestLight = 0.62
		DayDuration = 6300
		NightDuration = 4900
		DesiredRed = 0.97
		DesiredGreen = 1.10
		DesiredBlue = 1.05
		MonthName = "April"
		AprilTrees()
		ThunderRiskfactor = { 1, 2, 3, 4, 5, 6, 11, 12, 13 }
		TimerColor = HSLColor.Green
	elseif Month == 5 then
		BrightnessChangeSpeed = 11
		ColorChangeSpeed = 25
		BrightestLight = 1.0
		DarkestLight = 0.65
		DayDuration = 6800
		NightDuration = 4300
		DesiredRed = 0.97
		DesiredGreen = 1.10
		DesiredBlue = 1.05
		MonthName = "Mai"
		MaiTrees()
		ThunderRiskfactor = { 1, 2, 3, 4, 5, 6, 7, 8, 11, 12, 13 }
		TimerColor = HSLColor.LimeGreen
--												Summer
	elseif Month == 6 then
		BrightnessChangeSpeed = 12
		ColorChangeSpeed = 28
		BrightestLight = 1.03
		DarkestLight = 0.68
		DayDuration = 7300
		NightDuration = 3700
		DesiredRed = 1.15
		DesiredGreen = 1.15
		DesiredBlue = 0.95
		MonthName = "Juni"
		ThunderRiskfactor = { 1, 2, 3, 4, 5, 6, 7, 12, 13 }
		TimerColor = HSLColor.LightYellow
	elseif Month == 7 then
		BrightnessChangeSpeed = 12
		ColorChangeSpeed = 26
		BrightestLight = 1.03
		DarkestLight = 0.68
		DayDuration = 7300
		NightDuration = 3700
		DesiredRed = 1.17
		DesiredGreen = 1.17
		DesiredBlue = 0.94
		MonthName = "Juli"
		ThunderRiskfactor = { 1, 2, 3, 4, 5, 6, 7, 12, 13 }
		TimerColor = HSLColor.Gold
	elseif Month == 8 then
		BrightnessChangeSpeed = 11
		ColorChangeSpeed = 24
		BrightestLight = 1.0
		DarkestLight = 0.65
		DayDuration = 6800
		NightDuration = 4300
		DesiredRed = 1.13
		DesiredGreen = 1.1
		DesiredBlue = 0.93
		MonthName = "August"
		ThunderRiskfactor = { 1, 2, 3, 4, 5, 6, 7, 8, 11, 12, 13 }
		TimerColor = HSLColor.DarkRed
--												Fall
	elseif Month == 9 then
		BrightnessChangeSpeed = 10
		ColorChangeSpeed = 22
		BrightestLight = 0.98
		DarkestLight = 0.62
		DayDuration = 6300
		NightDuration = 4900
		DesiredRed = 1.08
		DesiredGreen = 1.03
		DesiredBlue = 0.94
		MonthName = "September"
		SeptemberTrees()
		ThunderRiskfactor = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 }
		TimerColor = HSLColor.DarkOrange
	elseif Month == 10 then
		BrightnessChangeSpeed = 9
		ColorChangeSpeed = 20
		BrightestLight = 0.96
		DarkestLight = 0.60
		DayDuration = 5800
		NightDuration = 5000
		DesiredRed = 1.13
		DesiredGreen = 1
		DesiredBlue = 0.97
		MonthName = "Oktober"
		OktoberTrees()
		ThunderRiskfactor = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13 }
		TimerColor = HSLColor.DarkGray
	elseif Month == 11 then
		BrightnessChangeSpeed = 8
		ColorChangeSpeed = 18
		BrightestLight = 0.94
		DarkestLight = 0.58
		DayDuration = 5400
		NightDuration = 5500
		DesiredRed = 1.14
		DesiredGreen = 0.97
		DesiredBlue = 1.04
		MonthName = "November"
		NovemberTrees()
		ThunderRiskfactor = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }
		TimerColor = HSLColor.DarkGray
--								Winter
	elseif Month == 12 then
		BrightnessChangeSpeed = 7
		ColorChangeSpeed = 15
		BrightestLight = 0.92
		DarkestLight = 0.55
		DayDuration = 5000
		NightDuration = 6000
		DesiredRed = 1.15
		DesiredGreen = 0.93
		DesiredBlue = 1.12
		MonthName = "Dezember"
		ThunderRiskfactor = { 1, 2, 3 }
		TimerColor = HSLColor.White
	end
	Trigger.AfterDelay(5, function()
		Month = Month + 1
		if Month == 13 then
			Month = 1
			Year = Year + 1  -- Happy new year :D
		end
	end)
end

ColorChanger = function()
	if RedLight < DesiredRed then
		RedLight = RedLight + 0.001
	elseif RedLight > DesiredRed then
		RedLight = RedLight - 0.001
	end
	if GreenLight < DesiredGreen then
		GreenLight = GreenLight + 0.001
	elseif GreenLight > DesiredGreen then
		GreenLight = GreenLight - 0.001
	end
	if BlueLight < DesiredBlue then
		BlueLight = BlueLight + 0.001
	elseif BlueLight > DesiredBlue then
		BlueLight = BlueLight - 0.001
	end
	Lighting.Red = RedLight
	Lighting.Green = GreenLight
	Lighting.Blue = BlueLight
	Trigger.AfterDelay(ColorChangeSpeed, function()
		ColorChanger()
	end)
end

turnOn = function()
	Lighting.Ambient = Light
	if Light < 0.794 and Light > 0.793 then
		POWER.Destroy()
		Trigger.AfterDelay(100, function()
			Media.PlayMusic()
		end)
	end
	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()
	if Light < 0.734 and Light > 0.733 then
		DesiredRed = 0.93
		DesiredGreen = 1.07
		DesiredBlue = 1.13
	end
	if Light < 0.734 and Light > 0.733 then
		GettingDark = true
		POWER = Actor.Create("dummypower", true, { Location = CPos.New(0,0), Owner = Neutral })
		Trigger.AfterDelay(350, function()
			Media.StopMusic()
		end)
	end
	if Light < 0.691 and Light > 0.690 then
		ThunderManager()
	end
	if Light > DarkestLight then
		Light = Light - RateOfIncrease
		Trigger.AfterDelay(BrightnessChangeSpeed, function()
			turnOff()
		end)
	else
		Trigger.AfterDelay(NightDuration, function()
			if Month == 12 then
				NeedleTrees()
			end
			NoturnOn = false
			Thunderstorm = false
			MonthConditions()
			turnOn()
		end)
	end
	Lighting.Ambient = Light
end


--										Teleporterfunctions


Ynumber = 0
SetLandteleporters = function()
	Trigger.OnEnteredFootprint({CPos.New(1,5 + Ynumber )}, function(a, id)
		if not a.HasProperty("Move") or a.Type == "SATELLITE" or a.Type == "SATELLITE.Husk" or a.Type == "stegosaurusegg" then
			return
		else
			if not a.IsDead and a.IsInWorld then
				a.Stop()
				a.Teleport(CPos.New(153, 1) + CVec.New(Utils.RandomInteger(0, 5), 0))
			end
		end
	end)
	Trigger.OnEnteredFootprint({CPos.New(163,5 + Ynumber )}, function(a, id)
		if not a.HasProperty("Move") then
			return
		else
			if not a.IsDead and a.IsInWorld then
				a.Stop()
				a.Teleport(CPos.New(5, 1) + CVec.New(Utils.RandomInteger(0, 5), 0))
			end
		end
	end)
	if Ynumber < 5 then
		Ynumber = Ynumber + 1
		SetLandteleporters()
	end
end

Y2number = 0

SetWaterteleporters = function()
	Trigger.OnEnteredFootprint({CPos.New(1,49 + Y2number )}, function(a, id)
		if not a.HasProperty("Move") then
			return
		else
			if not a.IsDead and a.IsInWorld then
				a.Stop()
				a.Teleport(CPos.New(163, 33) + CVec.New(0, Utils.RandomInteger(0, 5)))
			end
		end
	end)
	Trigger.OnEnteredFootprint({CPos.New(1,80 + Y2number )}, function(a, id)
		if not a.HasProperty("Move") then
			return
		else
			if not a.IsDead and a.IsInWorld then
				a.Stop()
				a.Teleport(CPos.New(163, 72) + CVec.New(0, Utils.RandomInteger(0, 5)))
			end
		end
	end)
	Trigger.OnEnteredFootprint({CPos.New(163,41 + Y2number )}, function(a, id)
		if not a.HasProperty("Move") then
			return
		else
			if not a.IsDead and a.IsInWorld then
				a.Stop()
				a.Teleport(CPos.New(1, 41) + CVec.New(0, Utils.RandomInteger(0, 5)))
			end
		end
	end)
	Trigger.OnEnteredFootprint({CPos.New(163,80 + Y2number )}, function(a, id)
		if not a.HasProperty("Move") then
			return
		else
			if not a.IsDead and a.IsInWorld then
				a.Stop()
				a.Teleport(CPos.New(1, 72) + CVec.New(0, Utils.RandomInteger(0, 5)))
			end
		end
	end)
	if Y2number < 6 then
		Y2number = Y2number + 1
		SetWaterteleporters()
	end
end

Russiamove = { CPos.New(153, 1), CPos.New(154, 1), CPos.New(155, 1), CPos.New(156, 1), CPos.New(157, 1) }
Canadamove = { CPos.New(5, 1), CPos.New(6, 1), CPos.New(7, 1), CPos.New(8, 1), CPos.New(9, 1) }

SetNorthmovetriggers = function()
	larrow = Actor.Create("larrow", true, { Location = CPos.New(5,6), Owner = Neutral })
	rarrow = Actor.Create("rarrow", true, { Location = CPos.New(159,6), Owner = Neutral })
	Trigger.OnEnteredFootprint(Russiamove, function(a, id)
		if a.HasProperty("Move") then
			a.Move(CPos.New(153, 7) + CVec.New(Utils.RandomInteger(1, 6), 0))
		end
	end)
	Trigger.OnEnteredFootprint(Canadamove, function(a, id)
		if a.HasProperty("Move") then
			a.Move(CPos.New(5, 7) + CVec.New(Utils.RandomInteger(1, 6), 0))
		end
	end)
end

NWPacificmove = { CPos.New(1, 41), CPos.New(1, 42), CPos.New(1, 43), CPos.New(1, 44), CPos.New(1, 45), CPos.New(1, 46), CPos.New(1, 47) }
SWPacificmove = { CPos.New(1, 72), CPos.New(1, 73), CPos.New(1, 74), CPos.New(1, 75), CPos.New(1, 76), CPos.New(1, 77), CPos.New(1, 78) }
NEPacificmove = { CPos.New(163, 33), CPos.New(163, 34), CPos.New(163, 35), CPos.New(163, 36), CPos.New(163, 37), CPos.New(163, 38), CPos.New(163, 39) }
SEPacificmove = { CPos.New(163, 72), CPos.New(163, 73), CPos.New(163, 74), CPos.New(163, 75), CPos.New(163, 76), CPos.New(163, 77), CPos.New(163, 78) }

SetPacificmovetriggers = function()
	lp1arrow = Actor.Create("larrow", true, { Location = CPos.New(5,52), Owner = Neutral })
	rp2arrow = Actor.Create("rarrow", true, { Location = CPos.New(159,82), Owner = Neutral })
	lp3arrow = Actor.Create("larrow", true, { Location = CPos.New(5,82), Owner = Neutral })
	rp4arrow = Actor.Create("rarrow", true, { Location = CPos.New(159,44), Owner = Neutral })
	Trigger.OnEnteredFootprint(NWPacificmove, function(a, id)
		if a.HasProperty("Move") then
			a.Move(CPos.New(3, 43) + CVec.New(Utils.RandomInteger(0, 2), Utils.RandomInteger(0, 6)))
		end
	end)
	Trigger.OnEnteredFootprint(SWPacificmove, function(a, id)
		if a.HasProperty("Move") then
			a.Move(CPos.New(6, 72) + CVec.New(Utils.RandomInteger(0, 2), Utils.RandomInteger(0, 6)))
		end
	end)
	Trigger.OnEnteredFootprint(NEPacificmove, function(a, id)
		if a.HasProperty("Move") then
			a.Move(CPos.New(159, 35) + CVec.New(Utils.RandomInteger(0, 2), Utils.RandomInteger(0, 6)))
		end
	end)
	Trigger.OnEnteredFootprint(SEPacificmove, function(a, id)
		if a.HasProperty("Move") then
			a.Move(CPos.New(159, 77) + CVec.New(Utils.RandomInteger(0, 2), Utils.RandomInteger(0, 6)))
		end
	end)
end

ThunderManager = function()
	ThunderstormRisk = Utils.Random(ThunderRiskfactor)
	if ThunderstormRisk == 11 then
		SmoothThunderstorm = true
		Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(45, 65)), function()
			SmoothThunderstorm = false
		end)
	elseif ThunderstormRisk == 12 then
		SmoothThunderstorm = true
		Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(15, 30)), function()
			SmoothThunderstorm = false
			NormalThunderstorm = true
			Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(30, 60)), function()
				NormalThunderstorm = false
				SmoothThunderstorm = true
				Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(15, 30)), function()
					SmoothThunderstorm = false
				end)
			end)
		end)
	elseif ThunderstormRisk == 13 then
		SmoothThunderstorm = true
		Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(15, 30)), function()
			SmoothThunderstorm = false
			NormalThunderstorm = true
			Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(20, 30)), function()
				NormalThunderstorm = false
				HardThunderstorm = true
				Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(30, 60)), function()
					HardThunderstorm = false
					NormalThunderstorm = true
					Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(15, 30)), function()
						NormalThunderstorm = false
						SmoothThunderstorm = true
						Trigger.AfterDelay(DateTime.Seconds(Utils.RandomInteger(15, 30)), function()
							SmoothThunderstorm = false
						end)
					end)
				end)
			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")
	Multi8 = Player.GetPlayer("Multi8")
	Multi9 = Player.GetPlayer("Multi9")
	Humans = { Multi0, Multi1, Multi2, Multi3, Multi4, Multi5, Multi6, Multi7, Multi8, Multi9 }
end

WorldLoaded = function()
	SetupPlayers()
	FlagNotifications()
	if Season == "noweather" then
	else
		InitialiseWeather()
	end
	ATWNotifications()
	if Teleporter == "land" then
		SetLandteleporters()
		SetNorthmovetriggers()
	elseif Teleporter == "water" then
		SetWaterteleporters()
		SetPacificmovetriggers()
	elseif Teleporter == "wandl" then
		SetLandteleporters()
		SetNorthmovetriggers()
		SetWaterteleporters()
		SetPacificmovetriggers()
	end
end

Tick = function()
	if Season == "defmode" then
		if SmoothThunderstorm then
			WeatherInfo = "SmoothThunderstorm"
		elseif NormalThunderstorm then
			WeatherInfo = "NormalThunderstorm"
		elseif HardThunderstorm then
			WeatherInfo = "HardThunderstorm"
		else
			WeatherInfo = "NoStorm"
		end
			UserInterface.SetMissionText("" .. MonthName .. " " .. (Year) .. "\nRed:" .. RedLight .. "     Green:" .. GreenLight .. "     Blue:" .. BlueLight .. "\nDesiredRed:" .. DesiredRed .. "     DesiredGreen:" .. DesiredGreen .. "     DesiredBlue:" .. DesiredBlue .. "\nLight:" .. Light .. "\n\n" .. WeatherInfo .. "", TimerColor)
	elseif Season ~= "noweather" then
		UserInterface.SetMissionText("" .. MonthName .. "   " .. (Year) .. "", TimerColor)

	end
	if SmoothThunderstorm then
		if (Utils.RandomInteger(1, 300) == 10) then
			local delay = Utils.RandomInteger(1, 10)
			Lighting.Flash("LightningStrike", delay)
			Trigger.AfterDelay(delay, function()
				Media.PlaySound("thunder" .. Utils.RandomInteger(1,10) .. ".aud")
			end)
		end
		if (Utils.RandomInteger(1, 250) == 10) then
			Media.PlaySound("thunder-ambient.aud")
		end
	elseif NormalThunderstorm then
		if (Utils.RandomInteger(1, 250) == 10) then
			local delay = Utils.RandomInteger(1, 10)
			Lighting.Flash("LightningStrike", delay)
			Trigger.AfterDelay(delay, function()
				Media.PlaySound("thunder" .. Utils.RandomInteger(1,10) .. ".aud")
			end)
		end
		if (Utils.RandomInteger(1, 225) == 10) then
			Media.PlaySound("thunder-ambient.aud")
		end
	elseif HardThunderstorm 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,10) .. ".aud")
			end)
		end
		if (Utils.RandomInteger(1, 200) == 10) then
			Media.PlaySound("thunder-ambient.aud")
		end
	end
end
