

PlayersWin = function()
	GameIsOver = true
	Media.PlaySoundNotification( allplayers, "AlertBuzzer")
	RateOfIncrease = 0.1
	BrightnessChangeSpeed = 3
	BrightestLight = 1.1
	if NoturnOn then
		turnOn()
	end
	
--	if Difficulty == "easy" then
		Media.DisplayMessage("Good job! The enemy is destoryed.", "Materia", TimerColor)
--	end
--	if Difficulty == "normal" then
--		Media.DisplayMessage("Wow well done guys you accomplished this on Normal with 0% enemy unit income, not bad but are you prepared for Hard ?", "Materia", TimerColor)
--	end
--	if Difficulty == "hard" then
--		if WaveInterval ~= 0 then
--			Media.DisplayMessage("I'm really impressed, you made this on hard with 0% enemy units, increase the income for a bigger challange.", "Materia", TimerColor)
--		end
--		if WaveInterval == 0 then
--			Media.DisplayMessage("What the ... , how did you do this ? i have to add a new level of difficulty for you guys, please upload this replay somewhere oO.", "Materia", TimerColor)
--		end
--	end
	Trigger.AfterDelay(DateTime.Seconds(4), function()
		if M0True then
			Multi0.MarkCompletedObjective(KillEnemysBase0)
		end
		if M1True then
			Multi1.MarkCompletedObjective(KillEnemysBase1)
		end
		if M2True then
			Multi2.MarkCompletedObjective(KillEnemysBase2)
		end
		if M3True then
			Multi3.MarkCompletedObjective(KillEnemysBase3)
		end
		if M4True then
			Multi4.MarkCompletedObjective(KillEnemysBase4)
		end
		Enemys.MarkFailedObjective(KillPlayers)
	end)
end

PlayerConquestConditions = function()
	DefenseMaker()
	AAMaker()
	KillPlayers = Enemys.AddPrimaryObjective("Destroy all the Players.")
	if M0True then
		RaBaseM0()
		KillEnemysBase0 = Multi0.AddPrimaryObjective("Destroy all enemy buildings.")
		if Multi0.Faction == "allies" then
			newMulti0fact = Actor.Create("factgdi", true, { Location = CPos.New(13,24), Owner = Multi0 })
		elseif Multi0.Faction == "soviet" then
			newMulti0fact = Actor.Create("factnod", true, { Location = CPos.New(13,24), Owner = Multi0 })
		else
			newMulti0fact = Actor.Create("fact", true, { Location = CPos.New(13,24), Owner = Multi0 })
		end
		Trigger.OnKilledOrCaptured(newMulti0fact, Player1Loose)
	end
	if M1True then
		RaBaseM1()
		KillEnemysBase1 = Multi1.AddPrimaryObjective("Destroy all enemy buildings.")
		if Multi1.Faction == "allies" then
			newMulti1fact = Actor.Create("factgdi", true, { Location = CPos.New(13,39), Owner = Multi1 })
		elseif Multi1.Faction == "soviet" then
			newMulti1fact = Actor.Create("factnod", true, { Location = CPos.New(13,39), Owner = Multi1 })
		else
			newMulti1fact = Actor.Create("fact", true, { Location = CPos.New(13,39), Owner = Multi1 })
		end
		Trigger.OnKilledOrCaptured(newMulti1fact, Player2Loose)
	end
	if M2True then
		RaBaseM2()
		KillEnemysBase2 = Multi2.AddPrimaryObjective("Destroy all enemys buildings.")
		if Multi2.Faction == "allies" then
			newMulti2fact = Actor.Create("factgdi", true, { Location = CPos.New(13,50), Owner = Multi2 })
		elseif Multi2.Faction == "soviet" then
			newMulti2fact = Actor.Create("factnod", true, { Location = CPos.New(13,50), Owner = Multi2 })
		else
			newMulti2fact = Actor.Create("fact", true, { Location = CPos.New(13,50), Owner = Multi2 })
		end
		Trigger.OnKilledOrCaptured(newMulti2fact, Player3Loose)
	end
	if M3True then
		RaBaseM3()
		KillEnemysBase3 = Multi3.AddPrimaryObjective("Destroy all enemys buildings.")
		if Multi3.Faction == "allies" then
			newMulti3fact = Actor.Create("factgdi", true, { Location = CPos.New(13,66), Owner = Multi3 })
		elseif Multi3.Faction == "soviet" then
			newMulti3fact = Actor.Create("factnod", true, { Location = CPos.New(13,66), Owner = Multi3 })
		else
			newMulti3fact = Actor.Create("fact", true, { Location = CPos.New(13,66), Owner = Multi3 })
		end
		Trigger.OnKilledOrCaptured(newMulti3fact, Player4Loose)
	end
	if M4True then
		RaBaseM4()
		KillEnemysBase4 = Multi4.AddPrimaryObjective("Destroy all enemy buildings.")
		if Multi4.Faction == "allies" then
			newMulti4fact = Actor.Create("factgdi", true, { Location = CPos.New(13,91), Owner = Multi4 })
		elseif Multi4.Faction == "soviet" then
			newMulti4fact = Actor.Create("factnod", true, { Location = CPos.New(13,91), Owner = Multi4 })
		else
			newMulti4fact = Actor.Create("fact", true, { Location = CPos.New(13,91), Owner = Multi4 })
		end
		Trigger.OnKilledOrCaptured(newMulti4fact, Player5Loose)
	end
	AllEnemyBuildings = buildingM0 + buildingM1 + buildingM2 + buildingM3 + buildingM4
end


Player1Loose = function()
	Multi0.MarkFailedObjective(KillEnemysBase0)
	Media.DisplayMessage("" .. Multi0.Name .. "'s main base has been destroyed!", "Materia", TimerColor)
	M0Dead = true
	M0True = false
	Trigger.AfterDelay(DateTime.Seconds(3), function()
--		Media.DisplayMessage("" .. Multi0.Name .. " Is defeated!", "Materia", TimerColor)
		allp1actors = Multi0.GetActors()
		for i,v in pairs(allp1actors) do
			if v.HasProperty("Health") then
				v.Stop()
				v.Kill()
			end
		end
	end)
end
Player2Loose = function()
	Multi1.MarkFailedObjective(KillEnemysBase1)
	Media.DisplayMessage("" .. Multi1.Name .. "'s main base has been destroyed!", "Materia", TimerColor)
	M1Dead = true
	M1True = false
	Trigger.AfterDelay(DateTime.Seconds(3), function()
--		Media.DisplayMessage("" .. Multi1.Name .. " Is defeated!", "Materia", TimerColor)
		allp2actors = Multi1.GetActors()
		for i,v in pairs(allp2actors) do
			if v.HasProperty("Health") then
				v.Stop()
				v.Kill()
			end
		end
	end)
end
Player3Loose = function()
	Multi2.MarkFailedObjective(KillEnemysBase2)
	Media.DisplayMessage("" .. Multi2.Name .. "'s main base has been destroyed!", "Materia", TimerColor)
	M2Dead = true
	M2True = false
	Trigger.AfterDelay(DateTime.Seconds(3), function()
--		Media.DisplayMessage("" .. Multi2.Name .. " Is defeated!", "Materia", TimerColor)
		allp3actors = Multi2.GetActors()
		for i,v in pairs(allp3actors) do
			if v.HasProperty("Health") then
				v.Stop()
				v.Kill()
			end
		end
	end)
end
Player4Loose = function()
	Multi3.MarkFailedObjective(KillEnemysBase3)
	Media.DisplayMessage("" .. Multi3.Name .. "'s main base has been destroyed!", "Materia", TimerColor)
	M3Dead = true
	M3True = false
	Trigger.AfterDelay(DateTime.Seconds(3), function()
--		Media.DisplayMessage("" .. Multi3.Name .. " Is defeated!", "Materia", TimerColor)
		allp4actors = Multi3.GetActors()
		for i,v in pairs(allp4actors) do
			if v.HasProperty("Health") then
				v.Stop()
				v.Kill()
			end
		end
	end)
end
Player5Loose = function()
    Multi4.MarkFailedObjective(KillEnemysBase4)
    Media.DisplayMessage("" .. Multi4.Name .. "'s main base has been destroyed!", "Materia", TimerColor)
    M4Dead = true
    M4True = false
    Trigger.AfterDelay(DateTime.Seconds(3), function()
        --        Media.DisplayMessage("" .. Multi4.Name .. " Is defeated!", "Materia", TimerColor)
        allp5actors = Multi4.GetActors()
        for i, v in pairs(allp5actors) do
            if v.HasProperty("Health") then
                v.Stop()
                v.Kill()
            end
        end
    end)
end

playingPlayers = 0

SetupPlayers = function()
	France = Player.GetPlayer("france")
	Enemys = Player.GetPlayer("Enemys")
    Neutral = Player.GetPlayer("Neutral")
    Materia = Player.GetPlayer("Materia")
    Carnivores = Player.GetPlayer("Carnivores")
	Herbivores = Player.GetPlayer("Herbivores")
	Multi0 = Player.GetPlayer("Multi0")
	Multi1 = Player.GetPlayer("Multi1")
	Multi2 = Player.GetPlayer("Multi2")
	Multi3 = Player.GetPlayer("Multi3")
	Multi4 = Player.GetPlayer("Multi4")
	Humans = { Multi0, Multi1, Multi2, Multi3, Multi4 }
    if Multi0 ~= nil then
	    playingPlayers = playingPlayers + 1
		M0True = true
		fact0 = "mcv"
		allactors = Multi0.GetActors()
		for i,v in pairs(allactors) do
			if v.Type == "mcvgdi" then
				fact0 = "factgdi"
				v.Destroy()
			elseif v.Type == "mcvnod" then
				fact0 = "factnod"
				v.Destroy()
			elseif v.Type == "mcv" then
				fact0 = "fact"
				v.Destroy()
			end
		end
	else
		factKilled0 = true
		weapKilled0 = true
		apwrKilled0 = true
		fixKilled0 = true
		domeKilled0 = true
		hpadKilled0 = true
		barrKilled0 = true
		afldKilled0 = true
		tentKilled0 = true
		kennKilled0 = true
		weapcncKilled0 = true
		pyleKilled0 = true
		afldcncKilled0 = true
		handKilled0 = true
		hpadnodKilled0 = true
		bioKilled0 = true
		anthillKilled0 = true
	end
    if Multi1 ~= nil then
	    playingPlayers = playingPlayers + 1
		M1True = true
		fact1 = "mcv"
		allactors = Multi1.GetActors()
		for i,v in pairs(allactors) do
			if v.Type == "mcvgdi" then
				fact1 = "factgdi"
				v.Destroy()
			elseif v.Type == "mcvnod" then
				fact1 = "factnod"
				v.Destroy()
			elseif v.Type == "mcv" then
				fact1 = "fact"
				v.Destroy()
			end
		end
	else
		factKilled1 = true
		weapKilled1 = true
		apwrKilled1 = true
		fixKilled1 = true
		domeKilled1 = true
		hpadKilled1 = true
		barrKilled1 = true
		afldKilled1 = true
		tentKilled1 = true
		kennKilled1 = true
		weapcncKilled1 = true
		pyleKilled1 = true
		afldcncKilled1 = true
		handKilled1 = true
		hpadnodKilled1 = true
		bioKilled1 = true
		anthillKilled1 = true
	end
    if Multi2 ~= nil then
	    playingPlayers = playingPlayers + 1
		M2True = true
		fact2 = "mcv"
		allactors = Multi2.GetActors()
		for i,v in pairs(allactors) do
			if v.Type == "mcvgdi" then
				fact2 = "factgdi"
				v.Destroy()
			elseif v.Type == "mcvnod" then
				fact2 = "factnod"
				v.Destroy()
			elseif v.Type == "mcv" then
				fact2 = "fact"
				v.Destroy()
			end
		end
	else
		factKilled2 = true
		weapKilled2 = true
		apwrKilled2 = true
		fixKilled2 = true
		domeKilled2 = true
		hpadKilled2 = true
		barrKilled2 = true
		afldKilled2 = true
		tentKilled2 = true
		kennKilled2 = true
		weapcncKilled2 = true
		pyleKilled2 = true
		afldcncKilled2 = true
		handKilled2 = true
		hpadnodKilled2 = true
		bioKilled2 = true
		anthillKilled2 = true
	end
    if Multi3 ~= nil then
	    playingPlayers = playingPlayers + 1
		M3True = true
		fact3 = "mcv"
		allactors = Multi3.GetActors()
		for i,v in pairs(allactors) do
			if v.Type == "mcvgdi" then
				fact3 = "factgdi"
				v.Destroy()
			elseif v.Type == "mcvnod" then
				fact3 = "factnod"
				v.Destroy()
			elseif v.Type == "mcv" then
				fact3 = "fact"
				v.Destroy()
			end
		end
	else
		factKilled3 = true
		weapKilled3 = true
		apwrKilled3 = true
		fixKilled3 = true
		domeKilled3 = true
		hpadKilled3 = true
		barrKilled3 = true
		afldKilled3 = true
		tentKilled3 = true
		kennKilled3 = true
		weapcncKilled3 = true
		pyleKilled3 = true
		afldcncKilled3 = true
		handKilled3 = true
		hpadnodKilled3 = true
		bioKilled3 = true
		anthillKilled3 = true
	end
    if Multi4 ~= nil then
	    playingPlayers = playingPlayers + 1
		M4True = true
		fact4 = "mcv"
		allactors = Multi4.GetActors()
		for i,v in pairs(allactors) do
			if v.Type == "mcvgdi" then
				fact4 = "factgdi"
				v.Destroy()
			elseif v.Type == "mcvnod" then
				fact4 = "factnod"
				v.Destroy()
			elseif v.Type == "mcv" then
				fact4 = "fact"
				v.Destroy()
			end
		end
	else
		factKilled4 = true
		weapKilled4 = true
		apwrKilled4 = true
		fixKilled4 = true
		domeKilled4 = true
		hpadKilled4 = true
		barrKilled4 = true
		afldKilled4 = true
		tentKilled4 = true
		kennKilled4 = true
		weapcncKilled4 = true
		pyleKilled4 = true
		afldcncKilled4 = true
		handKilled4 = true
		hpadnodKilled4 = true
		bioKilled4 = true
		anthillKilled4 = true
	end
end

SneakPlayersCheck = function()
    return Player.GetPlayers(function(player)
        return true
    end);
end

CheckSneakAttacks = function()
    Utils.Do(SneakPlayersCheck(), function(thesePlayers)
        if thesePlayers.HasPrerequisites({ "sneakattacks_on" }) then
            SneakAttacks = true
            SneakMessage = "enabled"
        else
            SneakAttacks = false
            SneakMessage = "disabled"
        end
    end)
end

CheckDinos = function()
    Utils.Do(SneakPlayersCheck(), function(thesePlayers)
        if thesePlayers.HasPrerequisites({ "dinos_on" }) then
            Dinos = true
        else
            Dinos = false
        end
    end)
end