Light = 0.9
 
turnOn = function()
	Lighting.Ambient = Light
	if Light < 1.01 then
		Light = Light + 0.001
		Trigger.AfterDelay(9, function()
			turnOn()
		end)
	
	else
		Trigger.AfterDelay(7837, function()
			turnOff()
		end)
	end
end

turnOff = function()
	Lighting.Ambient = Light
	if Light > 0.613 then
		Light = Light - 0.001
		Trigger.AfterDelay(8, function()
			turnOff()
		end)	
	
	else
		Trigger.AfterDelay(2873, function()
			turnOn()
		end)
	end
end

PlaySound = function()
	Media.PlaySound("JingleBells.wav")
	Trigger.AfterDelay(DateTime.Seconds(200), PlaySound)
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 = Multi0 })
	Toptree = Actor.Create("ctree", true, { Location = CPos.New(54,11), Owner = Multi3 })
	Trigger.OnDamaged(Toptree, TopTreeDamaged)
	Trigger.OnDamaged(Bottomtree, BottomTreeDamaged)
	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
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
end

Team1Won = function()
	Team1WonTheGame= true
	if not Team2WonTheGame then
		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
		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()
	PlaySound()
	turnOn()
	SetObjectiveandMakeTrees()
	Trigger.AfterDelay(DateTime.Seconds(5), function()
		Media.DisplayMessage("Ho Ho Ho Merry X-Mass Its Presenttime!", "Santa Clause", HSLColor.Red)
	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
