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


GetandsetFlags = function()
	CTFlags = {}
	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)
end

CTFFLagtypes = { "ctflag", "ctflagii", "ctflagiii", "ctflagiv", "ctflagra" }
CTbs = { "oilb" }

function GetNearFlag (flag)
		local flagactors = Map.ActorsInCircle(flag.CenterPosition , WDist.FromCells(3), function(bld)	
			return  bld.Type == CTbs[1]
		end)
	Trigger.OnEnteredProximityTrigger(flag.CenterPosition, WDist.FromCells(3), function(a, id)
		local mine = flag.Owner
		local yours = a.Owner

		if flag.Type == "ctflagra" then
			if mine ~= yours and  mine.IsAlliedWith(yours) ~= true and yours ~= Player.GetPlayer("Neutral") and yours ~= Player.GetPlayer("Creeps") and Actor.CruiseAltitude(a.Type) == 0 and a.Type ~= "1tnk.husk" and a.Type ~= "2tnk.husk" and a.Type ~= "3tnk.husk" and a.Type ~= "4tnk.husk" and a.Type ~= "harv.fullhusk" and a.Type ~= "harv.emptyhusk" and a.Type ~= "mcv.husk" and a.Type ~= "mgg.husk" and a.Type ~= "camera" and a.Type ~= "camera.paradrop" and a.Type ~= "camera.spyplane" and a.MaxHealth ~= 17500 then
				flag.Owner = yours
			end
		end
		if flag.Type == "ctflag" then
			if mine ~= yours and  mine.IsAlliedWith(yours) ~= true and yours ~= Player.GetPlayer("Neutral") and yours ~= Player.GetPlayer("Creeps") and Actor.CruiseAltitude(a.Type) == 0 and a.Type ~= "1tnk.husk" and a.Type ~= "2tnk.husk" and a.Type ~= "3tnk.husk" and a.Type ~= "4tnk.husk" and a.Type ~= "harv.fullhusk" and a.Type ~= "harv.emptyhusk" and a.Type ~= "mcv.husk" and a.Type ~= "mgg.husk" and a.Type ~= "camera" and a.Type ~= "camera.paradrop" and a.Type ~= "camera.spyplane" and a.MaxHealth ~= 17500 then
				if yours == Multi0 or yours == Multi1 then
					flag.Owner = Multi0
					if flagactors ~= nil  then
						for i,v in ipairs(flagactors) do
							if v ~= nil then
								v.Owner = Multi0
							end
						end
					end
				else
					flag.Owner = EnemysTop
					if flagactors ~= nil  then
						for i,v in ipairs(flagactors) do
							if v ~= nil then
								v.Owner = EnemysTop
							end
						end
					end
				end
			end
		end
		if flag.Type == "ctflagii" then
			if mine ~= yours and  mine.IsAlliedWith(yours) ~= true and yours ~= Player.GetPlayer("Neutral") and yours ~= Player.GetPlayer("Creeps") and Actor.CruiseAltitude(a.Type) == 0 and a.Type ~= "1tnk.husk" and a.Type ~= "2tnk.husk" and a.Type ~= "3tnk.husk" and a.Type ~= "4tnk.husk" and a.Type ~= "harv.fullhusk" and a.Type ~= "harv.emptyhusk" and a.Type ~= "mcv.husk" and a.Type ~= "mgg.husk" and a.Type ~= "camera" and a.Type ~= "camera.paradrop" and a.Type ~= "camera.spyplane" and a.MaxHealth ~= 17500 then
				if yours == Multi0 or yours == Multi1 then
					flag.Owner = Multi1
					if flagactors ~= nil  then
						for i,v in ipairs(flagactors) do
							if v ~= nil then
								v.Owner = Multi1
							end
						end
					end
				else
					flag.Owner = EnemysTop
					if flagactors ~= nil  then
						for i,v in ipairs(flagactors) do
							if v ~= nil then
								v.Owner = EnemysTop
							end
						end
					end
				end
			end
		end
		if flag.Type == "ctflagiii" then
			if mine ~= yours and  mine.IsAlliedWith(yours) ~= true and yours ~= Player.GetPlayer("Neutral") and yours ~= Player.GetPlayer("Creeps") and Actor.CruiseAltitude(a.Type) == 0 and a.Type ~= "1tnk.husk" and a.Type ~= "2tnk.husk" and a.Type ~= "3tnk.husk" and a.Type ~= "4tnk.husk" and a.Type ~= "harv.fullhusk" and a.Type ~= "harv.emptyhusk" and a.Type ~= "mcv.husk" and a.Type ~= "mgg.husk" and a.Type ~= "camera" and a.Type ~= "camera.paradrop" and a.Type ~= "camera.spyplane" and a.MaxHealth ~= 17500 then
				if yours == Multi2 or yours == Multi3 then
					flag.Owner = Multi2
					if flagactors ~= nil  then
						for i,v in ipairs(flagactors) do
							if v ~= nil then
								v.Owner = Multi2
							end
						end
					end
				else
					flag.Owner = EnemysBottom
					if flagactors ~= nil  then
						for i,v in ipairs(flagactors) do
							if v ~= nil then
								v.Owner = EnemysBottom
							end
						end
					end
				end
			end
		end
		if flag.Type == "ctflagiv" then
			if mine ~= yours and  mine.IsAlliedWith(yours) ~= true and yours ~= Player.GetPlayer("Neutral") and yours ~= Player.GetPlayer("Creeps") and Actor.CruiseAltitude(a.Type) == 0 and a.Type ~= "1tnk.husk" and a.Type ~= "2tnk.husk" and a.Type ~= "3tnk.husk" and a.Type ~= "4tnk.husk" and a.Type ~= "harv.fullhusk" and a.Type ~= "harv.emptyhusk" and a.Type ~= "mcv.husk" and a.Type ~= "mgg.husk" and a.Type ~= "camera" and a.Type ~= "camera.paradrop" and a.Type ~= "camera.spyplane" and a.MaxHealth ~= 17500 then
				if yours == Multi2 or yours == Multi3 then
					flag.Owner = Multi3
					if flagactors ~= nil  then
						for i,v in ipairs(flagactors) do
							if v ~= nil then
								v.Owner = Multi3
							end
						end
					end
				else
					flag.Owner = EnemysBottom
					if flagactors ~= nil  then
						for i,v in ipairs(flagactors) do
							if v ~= nil then
								v.Owner = EnemysBottom
							end
						end
					end
				end
			end
		end
	end)	
end


