---------------------------------------------------------------------------------------------
function createQuestOpenGate()

    StartSimpleJob("controlOpenGate")
	
	SpawnScoutTroops()

    Logic.SetShareExplorationWithPlayerFlag(1, 8, 1)

    end
---------------------------------------------------------------------------------------------
function controlOpenGate()
		
		if IsDead("gateKeeper") then
			Defeat()
			return true
		end
	
	   if IsNear("gateKeeper","keyKeeper",500) == true then
		UAScoutTroops1:ClearCommandQueue()
		for troopID in UAScoutTroops1:Iterator() do
			DestroyEntity(troopID)
		end
		UAScoutTroops1:Destroy()
		UAScoutTroops2:ClearCommandQueue()
		for troopID in UAScoutTroops2:Iterator() do
			DestroyEntity(troopID)
		end
		UAScoutTroops2:Destroy()

        ReplaceEntity("gate",Entities.XD_DarkWallStraightGate)

        createBriefingOpenGate()

        return true

        end
	
	if Counter.Tick2("ScoutMovement",5) then
		if not moveflag then
			Move("gateKeeper","keyKeeper",500)
		else
			Logic.GroupStand(GetID("gateKeeper"))
			Counter["ScoutMovement"].TickCount = 2
		end
		moveflag = not moveflag
	end
	
	return false
		
	end
	
function SpawnScoutTroops()
	if randomgatekeeper == 1 then
		for i=1,3,1 do
			local spawnpos = GetPosition("pat"..i)
			AI.Entity_CreateFormation(2, Entities.CU_Evil_LeaderBearman1, nil, 16, spawnpos.X, spawnpos.Y, 0, 1, 3, 16)
			AI.Entity_CreateFormation(2, Entities.CU_Evil_LeaderSkirmisher1, nil, 16, spawnpos.X, spawnpos.Y, 0, 1, 3, 16)
		end
	elseif randomgatekeeper == 2 then
		for i=6,7,1 do
			local spawnpos = GetPosition("pat"..i)
			AI.Entity_CreateFormation(2, Entities.CU_Evil_LeaderBearman1, nil, 16, spawnpos.X, spawnpos.Y, 0, 1, 3, 16)
			AI.Entity_CreateFormation(2, Entities.CU_Evil_LeaderSkirmisher1, nil, 16, spawnpos.X, spawnpos.Y, 0, 1, 3, 16)
		end
			spawnpos = GetPosition("CUTSCENE_MOVE_START_DARIO")
			AI.Entity_CreateFormation(2, Entities.CU_Evil_LeaderBearman1, nil, 16, spawnpos.X, spawnpos.Y, 0, 1, 3, 16)
			AI.Entity_CreateFormation(2, Entities.CU_Evil_LeaderSkirmisher1, nil, 16, spawnpos.X, spawnpos.Y, 0, 1, 3, 16)
	else
			local spawnpos = GetPosition("pat7")
			AI.Entity_CreateFormation(2, Entities.CU_Evil_LeaderBearman1, nil, 16, spawnpos.X, spawnpos.Y, 0, 1, 3, 16)
			AI.Entity_CreateFormation(2, Entities.CU_Evil_LeaderSkirmisher1, nil, 16, spawnpos.X, spawnpos.Y, 0, 1, 3, 16)
			spawnpos = GetPosition("CUTSCENE_MOVE_START_DARIO")
			AI.Entity_CreateFormation(2, Entities.CU_Evil_LeaderBearman1, nil, 16, spawnpos.X, spawnpos.Y, 0, 1, 3, 16)
			AI.Entity_CreateFormation(2, Entities.CU_Evil_LeaderSkirmisher1, nil, 16, spawnpos.X, spawnpos.Y, 0, 1, 3, 16)
			spawnpos = GetPosition("scouttroopspos")
			AI.Entity_CreateFormation(2, Entities.CU_Evil_LeaderBearman1, nil, 16, spawnpos.X, spawnpos.Y, 0, 1, 3, 16)
			AI.Entity_CreateFormation(2, Entities.CU_Evil_LeaderSkirmisher1, nil, 16, spawnpos.X, spawnpos.Y, 0, 1, 3, 16)
	end
	HunterTroops(randomgatekeeper)
end

function HunterTroops(_NotToSpawnFrom)
		UAScoutTroops1 = LazyUnlimitedArmy:New({
			Player = 2,
			Area = 3500,
			TransitAttackMove = true,
			Formation = UnlimitedArmy.Formations.Chaotic,
			AIActive = true,
			HiResJob = true,
		},8,21)
		 UAScoutTroops2 = LazyUnlimitedArmy:New({
			Player = 2,
			Area = 3500,
			TransitAttackMove = true,
			Formation = UnlimitedArmy.Formations.Chaotic,
			AIActive = true,
			HiResJob = true,
		},9,21)
	local spawnpointstable = {GetPosition("armyCamp2"),GetPosition("P2_Nephilim_Camp2_SpawnPoint"),GetPosition("armyCamp3")}
	table.remove(spawnpointstable,_NotToSpawnFrom)
	UAScoutTroops1.SpawnPoint = spawnpointstable[1]
	UAScoutTroops1.SpawnCounter = 0
	UAScoutTroops2.SpawnCounter = 0
	UAScoutTroops2.SpawnPoint = spawnpointstable[2]
	UAScoutTroops1:AddCommandLuaFunc(HandleScoutTroops,true)
	UAScoutTroops2:AddCommandLuaFunc(HandleScoutTroops,true)
end

function HandleScoutTroops(self)
	if self.SpawnCounter > 0 then
		self.SpawnCounter = self.SpawnCounter - 1
	else
		self:CreateLeaderForArmy(Entities.CU_Evil_LeaderBearman1,16,self.SpawnPoint)
		self:CreateLeaderForArmy(Entities.CU_Evil_LeaderSkirmisher1,16,self.SpawnPoint)
		self.SpawnCounter = 30
	end
	
	if self:IsIdle() then
		return true, UnlimitedArmy.CreateCommandMove(GetPosition("gateKeeper"),false)
	end
	return false
end