Light = 0.89

turnOn = function()
	Lighting.Ambient = Light
	if Light < 0.794 and Light > 0.793 then
		POWER.Destroy()
		Trigger.AfterDelay(100, function()
			PlayMusic()
		end)
	end
	if Light < 1.01 then
		Light = Light + 0.001
		Trigger.AfterDelay(8, function()
			turnOn()
		end)
	else
		Trigger.AfterDelay(5500, function()
			turnOff()
		end)
	end
end

turnOff = function()
	Lighting.Ambient = Light
	if Light < 0.794 and Light > 0.793 then
		GettingDark = true
		POWER = Actor.Create("dummypower", true, { Location = CPos.New(0,0), Owner = Neutral })
		Trigger.AfterDelay(100, function()
			PlayMusic()
		end)
	end
	if Light > 0.55 then
		Light = Light - 0.001
		Trigger.AfterDelay(8, function()
			turnOff()
		end)
	else
		Trigger.AfterDelay(3500, function()
			GettingDark = false
			turnOn()
		end)
	end
end

PlayMusic = function()
	if GettingDark then
		Media.PlaySound("SilentNight.wav")
	else
		Media.PlaySound("JingleBells.wav")
	end
end

SetObjectiveandMakeTrees = function()
	if Player.GetPlayer("Multi0") ~= nil then
		BurnTree1 = Multi0.AddPrimaryObjective("Burn down the enemys Xmass-Tree")
	end
	if Player.GetPlayer("Multi1") ~= nil then
		BurnTree1 = Multi1.AddPrimaryObjective("Burn down the enemys Xmass-Tree")
	end
	if Player.GetPlayer("Multi2") ~= nil then
		BurnTree1 = Multi2.AddPrimaryObjective("Burn down the enemys Xmass-Tree")
	end
	if Player.GetPlayer("Multi3") ~= nil then
		BurnTree2 = Multi3.AddPrimaryObjective("Burn down the enemys Xmass-Tree")
	end
	if Player.GetPlayer("Multi4") ~= nil then
		BurnTree2 = Multi4.AddPrimaryObjective("Burn down the enemys Xmass-Tree")
	end
	if Player.GetPlayer("Multi5") ~= nil then
		BurnTree2 = Multi5.AddPrimaryObjective("Burn down the enemys Xmass-Tree")
	end
	Bottomtree = Actor.Create("ctree", true, { Location = CPos.New(8,87), Owner = Multi3 })
	Toptree = Actor.Create("ctree", true, { Location = CPos.New(54,11), Owner = Multi0 })
	Trigger.OnDamaged(Toptree, function(tree, attacker)
		if not ColorisRed then
			attackingplayer = attacker.Owner
			CounterColor = attackingplayer.Color
			Trigger.AfterDelay(DateTime.Seconds(1), function()
				if not ColorisRed then
					CounterColor = HSLColor.White
				end
			end)
		end
		TopTreeDamaged()
	end)
	Trigger.OnDamaged(Bottomtree, function(tree, attacker)
		if not ColorisRed then
			attackingplayer = attacker.Owner
			CounterColor = attackingplayer.Color
			Trigger.AfterDelay(DateTime.Seconds(1), function()
				if not ColorisRed then
					CounterColor = HSLColor.White
				end
			end)
		end
		BottomTreeDamaged()
	end)
	Trigger.OnKilled(Toptree, Team2Won)
	Trigger.OnKilled(Bottomtree, Team1Won)
end

TopTreeDamaged = function()
	if not TopTreeWasDamaged then
		TopTreeWasDamaged = true
		Media.DisplayMessage("" .. Multi1.Name .. " and " .. Multi2.Name .. " our christmastree is under fire take care we will loose the game if he gets destroyed!", Multi0.Name, Multi0.Color)
		Camera.Position = Toptree.CenterPosition
		Beacon.New(Multi0, Toptree.CenterPosition, 500, true)
	end
	if TopTreehealth < 50 and not toptrigger50 then
		toptrigger50 = true
		Media.DisplayMessage("The northern christmastree is under heavy fire!", "Santa Clause", HSLColor.Red)
	end
end

BottomTreeDamaged = function()
	if not BottomTreeWasDamaged then
		BottomTreeWasDamaged = true
		Media.DisplayMessage("" .. Multi4.Name .. " and " .. Multi5.Name .. " our christmastree is under fire take care we will loose the game if he gets destroyed!", Multi3.Name, Multi3.Color)
		Camera.Position = Bottomtree.CenterPosition
		Beacon.New(Multi3, Bottomtree.CenterPosition, 500, true)
	end
	if BottomTreehealth < 50 and not bottomtrigger50 then
		bottomtrigger50 = true
		Media.DisplayMessage("The southern christmastree is under heavy fire!", "Santa Clause", HSLColor.Red)
	end
end

Team1Won = function()
	Team1WonTheGame= true
	if not Team2WonTheGame then
		Cam1 = Actor.Create("merry", true, { Location = (CPos.New(39,47)), Owner = Neutral })
		Cam2 = Actor.Create("xmass", true, { Location = (CPos.New(39,52)), Owner = Neutral })
		Cam1 = Actor.Create("camera", true, { Location = (CPos.New(39,50)), Owner = Multi0 })
		Cam2 = Actor.Create("camera", true, { Location = (CPos.New(39,50)), Owner = Multi3 })
		Camera.Position = Cam1.CenterPosition
		Media.DisplayMessage("Congratulations\n" .. Multi0.Name .. ", " .. Multi1.Name .. " and " .. Multi2.Name .. "\nBurned the enemys christmastree!", "Santa Clause", HSLColor.Red)
		Trigger.AfterDelay(DateTime.Seconds(5), function()
			if Player.GetPlayer("Multi0") ~= nil then
				Multi0.MarkCompletedObjective(BurnTree1)
			end
			if Player.GetPlayer("Multi1") ~= nil then
				Multi1.MarkCompletedObjective(BurnTree1)
			end
			if Player.GetPlayer("Multi2") ~= nil then
				Multi2.MarkCompletedObjective(BurnTree1)
			end
			if Player.GetPlayer("Multi3") ~= nil then
				Multi3.MarkFailedObjective(BurnTree2)
			end
			if Player.GetPlayer("Multi4") ~= nil then
				Multi4.MarkFailedObjective(BurnTree2)
			end
			if Player.GetPlayer("Multi5") ~= nil then
				Multi5.MarkFailedObjective(BurnTree2)
			end
		end)
	end
end
Team2Won = function()
	Team2WonTheGame= true
	if not Team1WonTheGame then
		Cam1 = Actor.Create("merry", true, { Location = (CPos.New(39,47)), Owner = Neutral })
		Cam2 = Actor.Create("xmass", true, { Location = (CPos.New(39,52)), Owner = Neutral })
		Cam1 = Actor.Create("camera", true, { Location = (CPos.New(39,50)), Owner = Multi0 })
		Cam2 = Actor.Create("camera", true, { Location = (CPos.New(39,50)), Owner = Multi3 })
		Camera.Position = Cam1.CenterPosition
		Media.DisplayMessage("Congratulations\n" .. Multi3.Name .. ", " .. Multi4.Name .. " and " .. Multi5.Name .. "\nBurned the enemys christmastree!", "Santa Clause", HSLColor.Red)
		Trigger.AfterDelay(DateTime.Seconds(5), function()
			if Player.GetPlayer("Multi5") ~= nil then
				Multi5.MarkCompletedObjective(BurnTree2)
			end
			if Player.GetPlayer("Multi4") ~= nil then
				Multi4.MarkCompletedObjective(BurnTree2)
			end
			if Player.GetPlayer("Multi3") ~= nil then
				Multi3.MarkCompletedObjective(BurnTree2)
			end
			if Player.GetPlayer("Multi2") ~= nil then
				Multi2.MarkFailedObjective(BurnTree1)
			end
			if Player.GetPlayer("Multi1") ~= nil then
				Multi1.MarkFailedObjective(BurnTree1)
			end
			if Player.GetPlayer("Multi0") ~= nil then
				Multi0.MarkFailedObjective(BurnTree1)
			end
		end)
	end
end

SetupPlayers = function()
	Neutral = Player.GetPlayer("Neutral")
	Multi0 = Player.GetPlayer("Multi0")
	if Multi0 == nil then
		Multi0 = Player.GetPlayer("Multi1")
		if Multi0 == nil then
			Multi0 = Player.GetPlayer("Multi2")
			if Multi0 == nil then
				Multi0 = Player.GetPlayer("Neutral")
			end
		end
	end
	Multi1 = Player.GetPlayer("Multi1")
	if Multi1 == nil then
		Multi1 = Player.GetPlayer("Multi0")
		if Multi1 == nil then
			Multi1 = Player.GetPlayer("Multi2")
			if Multi1 == nil then
				Multi1 = Player.GetPlayer("Neutral")
			end
		end
	end
	Multi2 = Player.GetPlayer("Multi2")
	if Multi2 == nil then
		Multi2 = Player.GetPlayer("Multi0")
		if Multi2 == nil then
			Multi2 = Player.GetPlayer("Multi1")
			if Multi2 == nil then
				Multi2 = Player.GetPlayer("Neutral")
			end
		end
	end
	Multi3  = Player.GetPlayer("Multi3")
	if Multi3  == nil then
		Multi3  = Player.GetPlayer("Multi4")
		if Multi3  == nil then
			Multi3  = Player.GetPlayer("Multi5")
			if Multi3  == nil then
				Multi3  = Player.GetPlayer("Neutral")
			end
		end
	end
	Multi4  = Player.GetPlayer("Multi4")
	if Multi4  == nil then
		Multi4  = Player.GetPlayer("Multi3")
		if Multi4  == nil then
			Multi4  = Player.GetPlayer("Multi5")
			if Multi4  == nil then
				Multi4  = Player.GetPlayer("Neutral")
			end
		end
	end
	Multi5 = Player.GetPlayer("Multi5")
	if Multi5 == nil then
		Multi5 = Player.GetPlayer("Multi3")
		if Multi5 == nil then
			Multi5 = Player.GetPlayer("Multi4 ")
			if Multi5 == nil then
				Multi5 = Player.GetPlayer("Neutral")
			end
		end
	end
end


function WorldLoaded()
	SetupPlayers()
	Media.StopMusic()
	turnOn()
	SetObjectiveandMakeTrees()
	Trigger.AfterDelay(DateTime.Seconds(5), function()
		Media.DisplayMessage("Ho Ho Ho Merry X-Mass Its Presenttime!", "Santa Clause", HSLColor.Red)
		PlayMusic()
	end)
	Trigger.AfterDelay(DateTime.Seconds(10), function()
		Media.DisplayMessage("You can open the presents around your tree with harvesters!", "Santa Clause", HSLColor.Red)
	end)
	Trigger.AfterDelay(DateTime.Seconds(15), function()
		Media.DisplayMessage("Protect the Xmass-Tree on your side, you will loose if he gets burned ! ! !", "Santa Clause", HSLColor.Red)
	end)
end

CounterColor = HSLColor.White

Tick = function()
	if not Team1WonTheGame and not Team2WonTheGame then
		if Toptree.Health < 100000 or Bottomtree.Health < 100000 and not ColorisRed then
			ColorisRed = true
			CounterColor = HSLColor.Red
		end
		TopTreehealth = Toptree.Health / 5000
		BottomTreehealth = Bottomtree.Health / 5000
		UserInterface.SetMissionText("   Top X-masstree health: ".. TopTreehealth .. " %\n\n\nBottom X-masstree health: " .. BottomTreehealth .. " %", CounterColor)
	else
		UserInterface.SetMissionText("", CounterColor)
	end
end
