CTFFLagtypes = {"ctflag"}
CTbs = {"tent","barr","stek","weap","afld","pbox","hbox","tsla","ftur", "gun", "dome", "fix", "mslo","atek","pdox","hpad", "iron", "kenn"}
aiTicker = DateTime.Seconds(4) -- 0:4

function GetNearFlag (flag) -- function for flag trigger and capture
	--Media.DisplayMessage("Set Up Flag!","")
		local flagactors = Map.ActorsInCircle(flag.CenterPosition , WDist.FromCells(10), function(bld)	
			return  bld.Type == CTbs[1] or  bld.Type == CTbs[2] or  bld.Type == CTbs[3] or  bld.Type == CTbs[4] or  bld.Type == CTbs[5] or  bld.Type == CTbs[6] or  bld.Type == CTbs[7] or  bld.Type == CTbs[8] or  bld.Type == CTbs[9] or  bld.Type == CTbs[10] or  bld.Type == CTbs[11]or  bld.Type == CTbs[12] or  bld.Type == CTbs[13] or  bld.Type == CTbs[14] or  bld.Type == CTbs[15]or  bld.Type == CTbs[16] or  bld.Type == CTbs[17] or  bld.Type == CTbs[18]
		end)
	Trigger.OnEnteredProximityTrigger(flag.CenterPosition, WDist.FromCells(2), function(a, id)
		local mine = flag.Owner
		local yours = a.Owner
		if mine ~= yours and  mine.IsAlliedWith(yours) ~= true and yours ~= Player.GetPlayer("Neutral") and yours ~= Player.GetPlayer("Creeps") and Actor.CruiseAltitude(a.Type) == 0 then
			flag.Owner = yours
			if flagactors ~= nil  then
				for i,v in ipairs(flagactors) do
					if v ~= nil then
						v.Owner = yours  -- Desync solved?
					end
				end 
			end
		end
	end)	
end

num = 0
function joinMyTables(t1, t2) -- simple join tables
	for k,v in ipairs(t2) do
		table.insert(t1, v)
		num = num + 1
	end 
	return t1
end

Alliesfaction = {"germany", "france", "england"}
Sovietfaction = {"ukraine", "russia"}
Allfactions = { "ukraine", "russia", "germany", "france", "england" }

Basecreator = function()
	anyplayer = Utils.Random(Allfactions)
	if Multi0 == nil then
		Multi0 = Player.GetPlayer(anyplayer)
	end
	anyplayer = Utils.Random(Allfactions)
	if Multi1 == nil then
		Multi1 = Player.GetPlayer(anyplayer)
	end
	anyplayer = Utils.Random(Allfactions)
	if Multi2 == nil then
		Multi2 = Player.GetPlayer(anyplayer)
	end
	anyplayer = Utils.Random(Allfactions)
	if Multi3 == nil then
		Multi3 = Player.GetPlayer(anyplayer)
	end
	Middleflag = Actor.Create("ctflag", true, { Location = middle.Location, Owner = Neutral })
	newactor = Actor.Create("dome", true, { Location = radarm0.Location, Owner = Multi0 }) newactor.Owner = Neutral
	newactor = Actor.Create("dome", true, { Location = radarm1.Location, Owner = Multi1 }) newactor.Owner = Neutral
	newactor = Actor.Create("dome", true, { Location = radarm2.Location, Owner = Multi2 }) newactor.Owner = Neutral
	newactor = Actor.Create("dome", true, { Location = radarm3.Location, Owner = Multi3 }) newactor.Owner = Neutral
	newactor = Actor.Create("fix", true, { Location = servm0.Location, Owner = Multi0 }) newactor.Owner = Neutral
	newactor = Actor.Create("fix", true, { Location = servm1.Location, Owner = Multi1 }) newactor.Owner = Neutral
	newactor = Actor.Create("fix", true, { Location = servm2.Location, Owner = Multi2 }) newactor.Owner = Neutral
	newactor = Actor.Create("fix", true, { Location = servm3.Location, Owner = Multi3 }) newactor.Owner = Neutral
	newactor = Actor.Create("mslo", true, { Location = nukem0.Location, Owner = Multi0 }) newactor.Owner = Neutral
	newactor = Actor.Create("mslo", true, { Location = nukem1.Location, Owner = Multi1 }) newactor.Owner = Neutral
	newactor = Actor.Create("mslo", true, { Location = nukem2.Location, Owner = Multi2 }) newactor.Owner = Neutral
	newactor = Actor.Create("mslo", true, { Location = nukem3.Location, Owner = Multi3 }) newactor.Owner = Neutral
	newactor = Actor.Create("weap", true, { Location = m0fac1.Location, Owner = Multi0 }) newactor.Owner = Neutral
	newactor = Actor.Create("weap", true, { Location = m0fac2.Location, Owner = Multi0 }) newactor.Owner = Neutral
	newactor = Actor.Create("weap", true, { Location = m0fac3.Location, Owner = Multi0 }) newactor.Owner = Neutral
	newactor = Actor.Create("weap", true, { Location = m0fac4.Location, Owner = Multi0 }) newactor.Owner = Neutral
	newactor = Actor.Create("weap", true, { Location = m1fac1.Location, Owner = Multi1 }) newactor.Owner = Neutral
	newactor = Actor.Create("weap", true, { Location = m1fac4.Location, Owner = Multi1 }) newactor.Owner = Neutral
	newactor = Actor.Create("weap", true, { Location = m1fac2.Location, Owner = Multi1 }) newactor.Owner = Neutral
	newactor = Actor.Create("weap", true, { Location = m1fac3.Location, Owner = Multi1 }) newactor.Owner = Neutral
	newactor = Actor.Create("weap", true, { Location = m2fac1.Location, Owner = Multi2 }) newactor.Owner = Neutral
	newactor = Actor.Create("weap", true, { Location = m2fac2.Location, Owner = Multi2 }) newactor.Owner = Neutral
	newactor = Actor.Create("weap", true, { Location = m2fac3.Location, Owner = Multi2 }) newactor.Owner = Neutral
	newactor = Actor.Create("weap", true, { Location = m2fac4.Location, Owner = Multi2 }) newactor.Owner = Neutral
	newactor = Actor.Create("weap", true, { Location = m3fac1.Location, Owner = Multi3 }) newactor.Owner = Neutral
	newactor = Actor.Create("weap", true, { Location = m3fac2.Location, Owner = Multi3 }) newactor.Owner = Neutral
	newactor = Actor.Create("weap", true, { Location = m3fac3.Location, Owner = Multi3 }) newactor.Owner = Neutral
	newactor = Actor.Create("weap", true, { Location = m3fac4.Location, Owner = Multi3 }) newactor.Owner = Neutral

													--Multi0
	for i=1,4 do
		if Multi0.Faction == Alliesfaction[i] then
			newactor = Actor.Create("pbox", true, { Location = m0pill1.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("pbox", true, { Location = m0pill2.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("pbox", true, { Location = m0pill3.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("gun", true, { Location = m0tsla1.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("gun", true, { Location = m0tsla2.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("gun", true, { Location = m0tsla3.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("hbox", true, { Location = m0camo1.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("hbox", true, { Location = m0camo2.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("hbox", true, { Location = m0camo3.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("hbox", true, { Location = m0camo4.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("hbox", true, { Location = m0camo5.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("atek", true, { Location = techm0.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("pdox", true, { Location = ichrm0.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("hpad", true, { Location = airm0.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("tent", true, { Location = m0barr1.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("tent", true, { Location = m0barr2.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("tent", true, { Location = m0barr3.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("tent", true, { Location = m0barr4.Location, Owner = Multi0 }) newactor.Owner = Neutral


		elseif Multi0.Faction == Sovietfaction[i] then
			newactor = Actor.Create("tsla", true, { Location = m0tsla1.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("tsla", true, { Location = m0tsla2.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("tsla", true, { Location = m0tsla3.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("ftur", true, { Location = m0camo1.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("ftur", true, { Location = m0camo2.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("ftur", true, { Location = m0camo3.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("ftur", true, { Location = m0camo4.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("ftur", true, { Location = m0camo5.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("stek", true, { Location = techm0.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("iron", true, { Location = ichrm0.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("afld", true, { Location = airm0.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("barr", true, { Location = m0barr1.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("barr", true, { Location = m0barr2.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("barr", true, { Location = m0barr3.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("barr", true, { Location = m0barr4.Location, Owner = Multi0 }) newactor.Owner = Neutral
			newactor = Actor.Create("kenn", true, { Location = m0kenn.Location, Owner = Multi0 }) newactor.Owner = Neutral

		end
													--Multi1
		if Multi1.Faction == Alliesfaction[i] then
			newactor1 = Actor.Create("pbox", true, { Location = m1pill1.Location, Owner = Multi1 }) newactor1.Owner = Neutral
			newactor1 = Actor.Create("pbox", true, { Location = m1pill2.Location, Owner = Multi1 }) newactor1.Owner = Neutral
			newactor1 = Actor.Create("pbox", true, { Location = m1pill3.Location, Owner = Multi1 }) newactor1.Owner = Neutral
			newactor1 = Actor.Create("gun", true, { Location = m1tsla1.Location, Owner = Multi1 }) newactor1.Owner = Neutral
			newactor1 = Actor.Create("gun", true, { Location = m1tsla2.Location, Owner = Multi1 }) newactor1.Owner = Neutral
			newactor1 = Actor.Create("gun", true, { Location = m1tsla3.Location, Owner = Multi1 }) newactor1.Owner = Neutral
			newactor1 = Actor.Create("hbox", true, { Location = m1camo1.Location, Owner = Multi1 }) newactor1.Owner = Neutral
			newactor1 = Actor.Create("hbox", true, { Location = m1camo2.Location, Owner = Multi1 }) newactor1.Owner = Neutral
			newactor1 = Actor.Create("hbox", true, { Location = m1camo3.Location, Owner = Multi1 }) newactor1.Owner = Neutral
			newactor1 = Actor.Create("hbox", true, { Location = m1camo4.Location, Owner = Multi1 }) newactor1.Owner = Neutral
			newactor1 = Actor.Create("hbox", true, { Location = m1camo5.Location, Owner = Multi1 }) newactor1.Owner = Neutral
			newactor = Actor.Create("atek", true, { Location = techm1.Location, Owner = Multi1 }) newactor.Owner = Neutral
			newactor = Actor.Create("pdox", true, { Location = ichrm1.Location, Owner = Multi1 }) newactor.Owner = Neutral
			newactor = Actor.Create("hpad", true, { Location = airm1.Location, Owner = Multi1 }) newactor.Owner = Neutral
			newactor = Actor.Create("tent", true, { Location = m1barr1.Location, Owner = Multi1 }) newactor.Owner = Neutral
			newactor = Actor.Create("tent", true, { Location = m1barr2.Location, Owner = Multi1 }) newactor.Owner = Neutral
			newactor = Actor.Create("tent", true, { Location = m1barr3.Location, Owner = Multi1 }) newactor.Owner = Neutral
			newactor = Actor.Create("tent", true, { Location = m1barr4.Location, Owner = Multi1 }) newactor.Owner = Neutral

		elseif Multi1.Faction == Sovietfaction[i] then
			newactor1 = Actor.Create("tsla", true, { Location = m1tsla1.Location, Owner = Multi1 }) newactor1.Owner = Neutral
			newactor1 = Actor.Create("tsla", true, { Location = m1tsla2.Location, Owner = Multi1 }) newactor1.Owner = Neutral
			newactor1 = Actor.Create("tsla", true, { Location = m1tsla3.Location, Owner = Multi1 }) newactor1.Owner = Neutral
			newactor1 = Actor.Create("ftur", true, { Location = m1camo1.Location, Owner = Multi1 }) newactor1.Owner = Neutral
			newactor1 = Actor.Create("ftur", true, { Location = m1camo2.Location, Owner = Multi1 }) newactor1.Owner = Neutral
			newactor1 = Actor.Create("ftur", true, { Location = m1camo3.Location, Owner = Multi1 }) newactor1.Owner = Neutral
			newactor1 = Actor.Create("ftur", true, { Location = m1camo4.Location, Owner = Multi1 }) newactor1.Owner = Neutral
			newactor1 = Actor.Create("ftur", true, { Location = m1camo5.Location, Owner = Multi1 }) newactor1.Owner = Neutral
			newactor = Actor.Create("stek", true, { Location = techm1.Location, Owner = Multi1 }) newactor.Owner = Neutral
			newactor = Actor.Create("iron", true, { Location = ichrm1.Location, Owner = Multi1 }) newactor.Owner = Neutral
			newactor = Actor.Create("afld", true, { Location = airm1.Location, Owner = Multi1 }) newactor.Owner = Neutral
			newactor = Actor.Create("barr", true, { Location = m1barr1.Location, Owner = Multi1 }) newactor.Owner = Neutral
			newactor = Actor.Create("barr", true, { Location = m1barr2.Location, Owner = Multi1 }) newactor.Owner = Neutral
			newactor = Actor.Create("barr", true, { Location = m1barr3.Location, Owner = Multi1 }) newactor.Owner = Neutral
			newactor = Actor.Create("barr", true, { Location = m1barr4.Location, Owner = Multi1 }) newactor.Owner = Neutral
			newactor = Actor.Create("kenn", true, { Location = m1kenn.Location, Owner = Multi1 }) newactor.Owner = Neutral
		end
													--Multi2
		if Multi2.Faction == Alliesfaction[i] then
			newactor2 = Actor.Create("pbox", true, { Location = m2pill1.Location, Owner = Multi2 }) newactor2.Owner = Neutral
			newactor2 = Actor.Create("pbox", true, { Location = m2pill2.Location, Owner = Multi2 }) newactor2.Owner = Neutral
			newactor2 = Actor.Create("pbox", true, { Location = m2pill3.Location, Owner = Multi2 }) newactor2.Owner = Neutral
			newactor2 = Actor.Create("gun", true, { Location = m2tsla1.Location, Owner = Multi2 }) newactor2.Owner = Neutral
			newactor2 = Actor.Create("gun", true, { Location = m2tsla2.Location, Owner = Multi2 }) newactor2.Owner = Neutral
			newactor2 = Actor.Create("gun", true, { Location = m2tsla3.Location, Owner = Multi2 }) newactor2.Owner = Neutral
			newactor2 = Actor.Create("hbox", true, { Location = m2camo1.Location, Owner = Multi2 }) newactor2.Owner = Neutral
			newactor2 = Actor.Create("hbox", true, { Location = m2camo2.Location, Owner = Multi2 }) newactor2.Owner = Neutral
			newactor2 = Actor.Create("hbox", true, { Location = m2camo3.Location, Owner = Multi2 }) newactor2.Owner = Neutral
			newactor2 = Actor.Create("hbox", true, { Location = m2camo4.Location, Owner = Multi2 }) newactor2.Owner = Neutral
			newactor2 = Actor.Create("hbox", true, { Location = m2camo5.Location, Owner = Multi2 }) newactor2.Owner = Neutral
			newactor = Actor.Create("atek", true, { Location = techm2.Location, Owner = Multi2 }) newactor.Owner = Neutral
			newactor = Actor.Create("pdox", true, { Location = ichrm2.Location, Owner = Multi2 }) newactor.Owner = Neutral
			newactor = Actor.Create("hpad", true, { Location = airm2.Location, Owner = Multi2 }) newactor.Owner = Neutral
			newactor = Actor.Create("tent", true, { Location = m2barr1.Location, Owner = Multi2 }) newactor.Owner = Neutral
			newactor = Actor.Create("tent", true, { Location = m2barr2.Location, Owner = Multi2 }) newactor.Owner = Neutral
			newactor = Actor.Create("tent", true, { Location = m2barr3.Location, Owner = Multi2 }) newactor.Owner = Neutral
			newactor = Actor.Create("tent", true, { Location = m2barr4.Location, Owner = Multi2 }) newactor.Owner = Neutral

		elseif Multi2.Faction == Sovietfaction[i] then
			newactor2 = Actor.Create("tsla", true, { Location = m2tsla1.Location, Owner = Multi2 }) newactor2.Owner = Neutral
			newactor2 = Actor.Create("tsla", true, { Location = m2tsla2.Location, Owner = Multi2 }) newactor2.Owner = Neutral
			newactor2 = Actor.Create("tsla", true, { Location = m2tsla3.Location, Owner = Multi2 }) newactor2.Owner = Neutral
			newactor2 = Actor.Create("ftur", true, { Location = m2camo1.Location, Owner = Multi2 }) newactor2.Owner = Neutral
			newactor2 = Actor.Create("ftur", true, { Location = m2camo2.Location, Owner = Multi2 }) newactor2.Owner = Neutral
			newactor2 = Actor.Create("ftur", true, { Location = m2camo3.Location, Owner = Multi2 }) newactor2.Owner = Neutral
			newactor2 = Actor.Create("ftur", true, { Location = m2camo4.Location, Owner = Multi2 }) newactor2.Owner = Neutral
			newactor2 = Actor.Create("ftur", true, { Location = m2camo5.Location, Owner = Multi2 }) newactor2.Owner = Neutral
			newactor = Actor.Create("stek", true, { Location = techm2.Location, Owner = Multi2 }) newactor.Owner = Neutral
			newactor = Actor.Create("iron", true, { Location = ichrm2.Location, Owner = Multi2 }) newactor.Owner = Neutral
			newactor = Actor.Create("afld", true, { Location = airm2.Location, Owner = Multi2 }) newactor.Owner = Neutral
			newactor = Actor.Create("barr", true, { Location = m2barr1.Location, Owner = Multi2 }) newactor.Owner = Neutral
			newactor = Actor.Create("barr", true, { Location = m2barr2.Location, Owner = Multi2 }) newactor.Owner = Neutral
			newactor = Actor.Create("barr", true, { Location = m2barr3.Location, Owner = Multi2 }) newactor.Owner = Neutral
			newactor = Actor.Create("barr", true, { Location = m2barr4.Location, Owner = Multi2 }) newactor.Owner = Neutral
			newactor = Actor.Create("kenn", true, { Location = m2kenn.Location, Owner = Multi2 }) newactor.Owner = Neutral
		end
													--Multi3
		if Multi3.Faction == Alliesfaction[i] then
			newactor3 = Actor.Create("pbox", true, { Location = m3pill1.Location, Owner = Multi3 }) newactor3.Owner = Neutral
			newactor3 = Actor.Create("pbox", true, { Location = m3pill2.Location, Owner = Multi3 }) newactor3.Owner = Neutral
			newactor3 = Actor.Create("pbox", true, { Location = m3pill3.Location, Owner = Multi3 }) newactor3.Owner = Neutral
			newactor3 = Actor.Create("gun", true, { Location = m3tsla1.Location, Owner = Multi3 }) newactor3.Owner = Neutral
			newactor3 = Actor.Create("gun", true, { Location = m3tsla2.Location, Owner = Multi3 }) newactor3.Owner = Neutral
			newactor3 = Actor.Create("gun", true, { Location = m3tsla3.Location, Owner = Multi3 }) newactor3.Owner = Neutral
			newactor3 = Actor.Create("hbox", true, { Location = m3camo1.Location, Owner = Multi3 }) newactor3.Owner = Neutral
			newactor3 = Actor.Create("hbox", true, { Location = m3camo2.Location, Owner = Multi3 }) newactor3.Owner = Neutral
			newactor3 = Actor.Create("hbox", true, { Location = m3camo3.Location, Owner = Multi3 }) newactor3.Owner = Neutral
			newactor3 = Actor.Create("hbox", true, { Location = m3camo4.Location, Owner = Multi3 }) newactor3.Owner = Neutral
			newactor3 = Actor.Create("hbox", true, { Location = m3camo5.Location, Owner = Multi3 }) newactor3.Owner = Neutral
			newactor = Actor.Create("atek", true, { Location = techm3.Location, Owner = Multi3 }) newactor.Owner = Neutral
			newactor = Actor.Create("pdox", true, { Location = ichrm3.Location, Owner = Multi3 }) newactor.Owner = Neutral
			newactor = Actor.Create("hpad", true, { Location = airm3.Location, Owner = Multi3 }) newactor.Owner = Neutral
			newactor = Actor.Create("tent", true, { Location = m3barr1.Location, Owner = Multi3 }) newactor.Owner = Neutral
			newactor = Actor.Create("tent", true, { Location = m3barr2.Location, Owner = Multi3 }) newactor.Owner = Neutral
			newactor = Actor.Create("tent", true, { Location = m3barr3.Location, Owner = Multi3 }) newactor.Owner = Neutral
			newactor = Actor.Create("tent", true, { Location = m3barr4.Location, Owner = Multi3 }) newactor.Owner = Neutral

		elseif Multi3.Faction == Sovietfaction[i] then
			newactor3 = Actor.Create("tsla", true, { Location = m3tsla1.Location, Owner = Multi3 }) newactor3.Owner = Neutral
			newactor3 = Actor.Create("tsla", true, { Location = m3tsla2.Location, Owner = Multi3 }) newactor3.Owner = Neutral
			newactor3 = Actor.Create("tsla", true, { Location = m3tsla3.Location, Owner = Multi3 }) newactor3.Owner = Neutral
			newactor3 = Actor.Create("ftur", true, { Location = m3camo1.Location, Owner = Multi3 }) newactor3.Owner = Neutral
			newactor3 = Actor.Create("ftur", true, { Location = m3camo2.Location, Owner = Multi3 }) newactor3.Owner = Neutral
			newactor3 = Actor.Create("ftur", true, { Location = m3camo3.Location, Owner = Multi3 }) newactor3.Owner = Neutral
			newactor3 = Actor.Create("ftur", true, { Location = m3camo4.Location, Owner = Multi3 }) newactor3.Owner = Neutral
			newactor3 = Actor.Create("ftur", true, { Location = m3camo5.Location, Owner = Multi3 }) newactor3.Owner = Neutral
			newactor = Actor.Create("stek", true, { Location = techm3.Location, Owner = Multi3 }) newactor.Owner = Neutral
			newactor = Actor.Create("iron", true, { Location = ichrm3.Location, Owner = Multi3 }) newactor.Owner = Neutral
			newactor = Actor.Create("afld", true, { Location = airm3.Location, Owner = Multi3 }) newactor.Owner = Neutral
			newactor = Actor.Create("barr", true, { Location = m3barr1.Location, Owner = Multi3 }) newactor.Owner = Neutral
			newactor = Actor.Create("barr", true, { Location = m3barr2.Location, Owner = Multi3 }) newactor.Owner = Neutral
			newactor = Actor.Create("barr", true, { Location = m3barr3.Location, Owner = Multi3 }) newactor.Owner = Neutral
			newactor = Actor.Create("barr", true, { Location = m3barr4.Location, Owner = Multi3 }) newactor.Owner = Neutral
			newactor = Actor.Create("kenn", true, { Location = m3kenn.Location, Owner = Multi3 }) newactor.Owner = Neutral
		end
	end
	newmcv0 = Actor.Create("mcv", true, { Location = m0mcvin.Location, Owner = Multi0 }) 
	newmcv0.Move(m0mcvmove.Location)
	Trigger.OnIdle(newmcv0, function()
		Trigger.ClearAll(newmcv0)
		Trigger.AfterDelay(4, function()
			newmcv0.Move(m0mcvdeploy.Location)
			Trigger.OnIdle(newmcv0, function()
				Trigger.ClearAll(newmcv0)
				Trigger.AfterDelay(4, function()
					newmcv0.Deploy()
					m0e11 = Actor.Create("e1", true, { Location = m0mcvin.Location + CVec.New(0, Utils.RandomInteger(0, 10)), Owner = Multi0 }) m0e11.Move(m0mcvmove.Location)
					m0e12 = Actor.Create("e3", true, { Location = m0mcvin.Location + CVec.New(0, Utils.RandomInteger(0, 10)), Owner = Multi0 }) m0e12.Move(m0mcvmove.Location)
					m0e13 = Actor.Create("e1", true, { Location = m0mcvin.Location + CVec.New(Utils.RandomInteger(0, 10), 0), Owner = Multi0 }) m0e13.Move(m0mcvmove.Location)
					m0e14 = Actor.Create("e3", true, { Location = m0mcvin.Location + CVec.New(Utils.RandomInteger(0, 10), 0), Owner = Multi0 }) m0e14.Move(m0mcvmove.Location)
				end)
			end)
		end)
	end)
	newmcv1 = Actor.Create("mcv", true, { Location = m1mcvin.Location, Owner = Multi1 }) 
	newmcv1.Move(m1mcvmove.Location)
	Trigger.OnIdle(newmcv1, function()
		Trigger.ClearAll(newmcv1)
		Trigger.AfterDelay(4, function()
			newmcv1.Move(m1mcvdeploy.Location)
			Trigger.OnIdle(newmcv1, function()
				Trigger.ClearAll(newmcv1)
				Trigger.AfterDelay(4, function()
					newmcv1.Deploy()
					m1e11 = Actor.Create("e1", true, { Location = m1mcvin.Location + CVec.New(0, Utils.RandomInteger(0, 10)), Owner = Multi1 }) m1e11.Move(m1mcvmove.Location)
					m1e12 = Actor.Create("e3", true, { Location = m1mcvin.Location + CVec.New(0, Utils.RandomInteger(0, 10)), Owner = Multi1 }) m1e12.Move(m1mcvmove.Location)
					m1e13 = Actor.Create("e1", true, { Location = m1mcvin.Location + CVec.New(Utils.RandomInteger(-10, 0), 0), Owner = Multi1 }) m1e13.Move(m1mcvmove.Location)
					m1e14 = Actor.Create("e3", true, { Location = m1mcvin.Location + CVec.New(Utils.RandomInteger(-10, 0), 0), Owner = Multi1 }) m1e14.Move(m1mcvmove.Location)
				end)
			end)
		end)
	end)
	newmcv2 = Actor.Create("mcv", true, { Location = m2mcvin.Location, Owner = Multi2 }) 
	newmcv2.Move(m2mcvmove.Location)
	Trigger.OnIdle(newmcv2, function()
		Trigger.ClearAll(newmcv2)
		Trigger.AfterDelay(4, function()
			newmcv2.Move(m2mcvdeploy.Location)
			Trigger.OnIdle(newmcv2, function()
				Trigger.ClearAll(newmcv2)
				Trigger.AfterDelay(4, function()
					newmcv2.Deploy()
					m2e11 = Actor.Create("e1", true, { Location = m2mcvin.Location + CVec.New(0, Utils.RandomInteger(-10, 0)), Owner = Multi2 }) m2e11.Move(m2mcvmove.Location)
					m2e12 = Actor.Create("e3", true, { Location = m2mcvin.Location + CVec.New(0, Utils.RandomInteger(-10, 0)), Owner = Multi2 }) m2e12.Move(m2mcvmove.Location)
					m2e13 = Actor.Create("e1", true, { Location = m2mcvin.Location + CVec.New(Utils.RandomInteger(0, 10), 0), Owner = Multi2 }) m2e13.Move(m2mcvmove.Location)
					m2e14 = Actor.Create("e3", true, { Location = m2mcvin.Location + CVec.New(Utils.RandomInteger(0, 10), 0), Owner = Multi2 }) m2e14.Move(m2mcvmove.Location)
				end)
			end)
		end)
	end)
	newmcv3 = Actor.Create("mcv", true, { Location = m3mcvin.Location, Owner = Multi3 }) 
	newmcv3.Move(m3mcvmove.Location)
	Trigger.OnIdle(newmcv3, function()
		Trigger.ClearAll(newmcv3)
		Trigger.AfterDelay(4, function()
			newmcv3.Move(m3mcvdeploy.Location)
			Trigger.OnIdle(newmcv3, function()
				Trigger.ClearAll(newmcv3)
				Trigger.AfterDelay(4, function()
					newmcv3.Deploy()
					m3e11 = Actor.Create("e1", true, { Location = m3mcvin.Location + CVec.New(0, Utils.RandomInteger(-10, 0)), Owner = Multi3 }) m3e11.Move(m3mcvmove.Location)
					m3e12 = Actor.Create("e3", true, { Location = m3mcvin.Location + CVec.New(0, Utils.RandomInteger(-10, 0)), Owner = Multi3 }) m3e12.Move(m3mcvmove.Location)
					m3e13 = Actor.Create("e1", true, { Location = m3mcvin.Location + CVec.New(Utils.RandomInteger(-10, 0), 0), Owner = Multi3 }) m3e13.Move(m3mcvmove.Location)
					m3e14 = Actor.Create("e3", true, { Location = m3mcvin.Location + CVec.New(Utils.RandomInteger(-10, 0), 0), Owner = Multi3 }) m3e14.Move(m3mcvmove.Location)
				end)
			end)
		end)
	end)
end

AllInfantry = { "dog", "e1", "e2", "e3", "e4", "e6", "spy", "e7", "medi", "mech", "thf", "shok", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8" }
Middlelandingplaces = { midreinf1, midreinf2, midreinf3, midreinf4, midreinf5 }

Middlereinforcements = function()
	Rndlanding  = Utils.Random(Middlelandingplaces)
	Rndinf1 = Utils.Random(AllInfantry)
	Rndinf2 = Utils.Random(AllInfantry)
	Rndinf3 = Utils.Random(AllInfantry)
	Rndinf4 = Utils.Random(AllInfantry)
	Rndinf5 = Utils.Random(AllInfantry)
	Rndinf6 = Utils.Random(AllInfantry)
	Rndinf7 = Utils.Random(AllInfantry)
	if Middleflag.Owner ~= Neutral then
		Reinforcements.ReinforceWithTransport(Middleflag.Owner, "tran.insertion", {Rndinf1, Rndinf2, Rndinf3, Rndinf4, Rndinf5, Rndinf6, Rndinf7}, {m0mcvin.Location + CVec.New(0, Utils.RandomInteger(0, 119)), Rndlanding.Location + CVec.New( Utils.RandomInteger(-1, 1), Utils.RandomInteger(-1, 1))}, {m3mcvin.Location + CVec.New(0, Utils.RandomInteger(-119, 0))})
	end
	Trigger.AfterDelay(DateTime.Seconds(30), function()
			Middlereinforcements()
	end)
end


Playmusic = false
Thunderstorm = false

turnOn = function()
	Lighting.Ambient = Light
	if Light < 1.1 then
		Light = Light + 0.001
		Trigger.AfterDelay(4, function()
			turnOn()
		end)
	
	else
		Trigger.AfterDelay(3000, function()
			turnOff()
		end)
	end		
end

turnOff = function()
	Raintimer = 0
	Lighting.Ambient = Light
	if Light < 0.75 then 
		if Playmusic == false then
			Rainsound()
			Playmusic = true
		end
	end
	if Light < 0.65 then
		Thunderstorm = true
	end
	if Light > 0.47 then
		Light = Light - 0.001
		Trigger.AfterDelay(4, function()
			turnOff()
		end)	
	
	else
		Trigger.AfterDelay(3000, function()
			turnOn()
			Thunderstorm = false
			Playmusic = false
		end)
	end
end

Raintimer = 0
Rainsound = function()
	if Raintimer < 11 then
		Trigger.AfterDelay(12, function()
			Media.PlaySound("rain.aud")
			Trigger.AfterDelay(DateTime.Seconds(13), function()
				Raintimer = Raintimer + 1
				Rainsound()
			end)
		end)
	end
end

WorldLoaded = function()
	Multi0 = Player.GetPlayer("Multi0")
	Multi1 = Player.GetPlayer("Multi1")
	Multi2 = Player.GetPlayer("Multi2")
	Multi3 = Player.GetPlayer("Multi3")
	Neutral = Player.GetPlayer("Neutral")
	if Map.LobbyOption("difficulty") == "normal" then
		Light = 0.85
		turnOn()
	elseif Map.LobbyOption("difficulty") == "easy" then
		Lighting.Ambient = 1.1
	end
	Basecreator()
	CTFlags = {}
	Middlereinforcements()
	Trigger.AfterDelay(DateTime.Seconds(1), function() -- set up Flags
		local neut = Player.GetPlayer("Neutral")
		for i=1,#CTFFLagtypes do
			local T1 = neut.GetActorsByType(CTFFLagtypes[i])
			if T1 ~= nil then
				joinMyTables(CTFlags, T1)			
			end
		end
	end)
	
	Trigger.AfterDelay(DateTime.Seconds(2), function()	 -- fuse flags
		for k,v in ipairs(CTFlags) do
			GetNearFlag(v)
		end
		return CTFlags
	end)

	Trigger.AfterDelay(DateTime.Seconds(3), function() -- player announcements
		Media.DisplayMessage("capture flags and receive structures")	
	end)
	Trigger.AfterDelay(DateTime.Seconds(8), function()
		Media.DisplayMessage("capture the middleflag and receive reinfordements")
	end)
	Trigger.AfterDelay(DateTime.Seconds(15), function()
		Media.DisplayMessage("hi5")
	end)
	Trigger.AfterDelay(DateTime.Seconds(3), function()	
	
	--AI Enter Closed area Trigger	
	
		local aiflaggers = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(bld)	
				return  bld.Type == CTFFLagtypes[1] or  bld.Type == CTFFLagtypes[2] or  bld.Type == CTFFLagtypes[3] or  bld.Type == CTFFLagtypes[4] or  bld.Type == CTFFLagtypes[5] or  bld.Type == CTFFLagtypes[6]
			end)
			
		for i = 1, #aiflaggers do
			if aiflaggers[i] ~= nil then
				Trigger.OnEnteredProximityTrigger(aiflaggers[i].CenterPosition, WDist.FromCells(8), function(a, id)
					local mine = aiflaggers[i].Owner
					local yours = a.Owner
					if mine ~= yours and mine.IsAlliedWith(yours) == false and yours.IsBot and a.HasProperty("AttackMove")  and Actor.CruiseAltitude(a.Type) == 0 and a.IsDead == false then
						a.Stop()
						a.AttackMove(aiflaggers[i].Location)
					end
				end)
				Trigger.OnExitedProximityTrigger(aiflaggers[i].CenterPosition, WDist.FromCells(5), function(a, id)
					local mine = aiflaggers[i].Owner
					local yours = a.Owner
					if mine ~= yours and mine.IsAlliedWith(yours) == false and yours.IsBot and a.HasProperty("AttackMove")  and Actor.CruiseAltitude(a.Type) == 0 and a.IsDead == false then
						a.Stop()
						a.AttackMove(aiflaggers[i].Location)
					end
				end)
			end
		end
	end)
end


Tick = function() 
		if aiTicker > 0 and aiTicker then
		aiTicker = aiTicker - 1
	elseif aiTicker == 0 then
		Trigger.AfterDelay(DateTime.Seconds(1), function()
			aiTicker = DateTime.Minutes(0) + DateTime.Seconds(5)  -- 0:5
		end)		
	end
	
	--new AI scripz
	if aiTicker == 1 then
		for i=1,8 do
			local ai = Player.GetPlayer("Multi" .. tostring(i-1))
			if ai ~= nil and ai.IsBot then
				local aiunits = ai.GetGroundAttackers() -- get Atack units
				local attackpoint = nil
				local randomn = Utils.RandomInteger(1,100) -- random Roll
				
				local lowtarget = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(bld)	-- getting sight flags
					return  (bld.Type == CTFFLagtypes[6]) and bld.Owner ~= ai and ai.IsAlliedWith(bld.Owner) == false
				end)
				local medtarget = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(bld)	-- getting Infantry flags
					return  (bld.Type == CTFFLagtypes[1] or bld.Type == CTFFLagtypes[2]) and bld.Owner ~= ai and ai.IsAlliedWith(bld.Owner) == false
				end)
				local Hightarget = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(bld)	-- getting Tang flags
					return  (bld.Type == CTFFLagtypes[3] or bld.Type == CTFFLagtypes[4] or  bld.Type == CTFFLagtypes[8]) and bld.Owner ~= ai and ai.IsAlliedWith(bld.Owner) == false
				end)
				local Mosttarget = Map.ActorsInBox(Map.TopLeft, Map.BottomRight, function(bld)	-- getting Heavy flags
					return  (bld.Type == CTFFLagtypes[5] or  bld.Type == CTFFLagtypes[7]) and bld.Owner ~= ai and ai.IsAlliedWith(bld.Owner) == false
				end)
			
				--Media.DisplayMessage("Ai decides to Attack now and Rolled: " .. tostring(randomn),"") -- Delete aafterwards
					if randomn > 73 and #Mosttarget ~= 0 then  -- decide on priority and random roll
						attackpoint = Mosttarget[Utils.RandomInteger(1,#Mosttarget)]
					elseif( randomn > 35 and randomn < 74 ) or (#Mosttarget == 0 and #Hightarget ~= 0)then
						attackpoint = Hightarget[Utils.RandomInteger(1,#Hightarget)]
					elseif (randomn > 13 and randomn < 36) or (#Hightarget == 0 and #medtarget ~= 0) then
						attackpoint = medtarget[Utils.RandomInteger(1,#medtarget)]
					else
						attackpoint = lowtarget[Utils.RandomInteger(1,#lowtarget)]
					end
					

				if attackpoint ~= nil then -- check and force to attack choosen point
					--Media.DisplayMessage("Ai decides to Attack: " .. attackpoint.Type,"") -- Delete aafterwards
					local whos = attackpoint.Owner
					if whos ~= ai and whos.IsAlliedWith(ai) == false then
						for v=1, 40 do
							local unit = aiunits[v]
							if unit ~= nil and unit.IsIdle then
								unit.AttackMove(attackpoint.Location)
							end
						end
					end
				end	
			end
		end
	end
	if Thunderstorm 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,6) .. ".aud")
			end)
		end
		if (Utils.RandomInteger(1, 200) == 10) then
			Media.PlaySound("thunder-ambient.aud")
		end
	end
end

