private bool CanTransferToNearMission()
    {
        AIData_Army aidata = this.aiDataRepository.GetAIData <AIData_Army>(base.AIDataArmyGUID);

        if (aidata == null || aidata.Army == null || aidata.Army.IsLocked || aidata.Army.IsInEncounter)
        {
            return(false);
        }
        foreach (AICommanderMission aicommanderMission in this.AICommanderRegroupArmies.Missions)
        {
            AICommanderMission_RegroupArmyAt aicommanderMission_RegroupArmyAt = (AICommanderMission_RegroupArmyAt)aicommanderMission;
            AIData_Army aidata2 = this.aiDataRepository.GetAIData <AIData_Army>(aicommanderMission_RegroupArmyAt.AIDataArmyGUID);
            if (aicommanderMission_RegroupArmyAt != this && aicommanderMission_RegroupArmyAt.AIDataArmyGUID.IsValid && aidata2 != null && aidata2.Army != null && !aidata2.Army.IsLocked && !aidata2.Army.IsInEncounter && aicommanderMission_RegroupArmyAt.targetTransferArmy == null && this.worldPositionningService.GetDistance(aidata.Army.WorldPosition, aidata2.Army.WorldPosition) == 1 && this.pathfindingService.IsTransitionPassable(aidata.Army.WorldPosition, aidata2.Army.WorldPosition, aidata.Army, (PathfindingFlags)0, null))
            {
                if (aidata.Army.GetPropertyValue(SimulationProperties.Movement) <= 0f)
                {
                    if (this.IsArmyBesiegingACity(aidata2.Army.GUID))
                    {
                        continue;
                    }
                    if (aidata2.Army.GetPropertyValue(SimulationProperties.Movement) > 0f && aicommanderMission_RegroupArmyAt.TransferUnits(aidata.GameEntity.GUID))
                    {
                        aicommanderMission_RegroupArmyAt.targetTransferArmy = aidata;
                        aicommanderMission_RegroupArmyAt.Completion         = AICommanderMission.AICommanderMissionCompletion.Success;
                        int num = this.UnitGuids.Length;
                        Array.Resize <GameEntityGUID>(ref this.UnitGuids, this.UnitGuids.Length + aicommanderMission_RegroupArmyAt.UnitGuids.Length);
                        for (int i = 0; i < aicommanderMission_RegroupArmyAt.UnitGuids.Length; i++)
                        {
                            this.UnitGuids[num + i] = aicommanderMission_RegroupArmyAt.UnitGuids[i];
                        }
                    }
                }
                else if (this.TransferUnits(aidata2.GameEntity.GUID))
                {
                    this.targetTransferArmy = aidata2;
                    base.Completion         = AICommanderMission.AICommanderMissionCompletion.Success;
                    int num2 = aicommanderMission_RegroupArmyAt.UnitGuids.Length;
                    Array.Resize <GameEntityGUID>(ref aicommanderMission_RegroupArmyAt.UnitGuids, this.UnitGuids.Length + aicommanderMission_RegroupArmyAt.UnitGuids.Length);
                    for (int j = 0; j < this.UnitGuids.Length; j++)
                    {
                        aicommanderMission_RegroupArmyAt.UnitGuids[num2 + j] = this.UnitGuids[j];
                    }
                }
                return(true);
            }
        }
        return(false);
    }
    private void ResetRecruitementLocks()
    {
        this.unitsGUIDS.Clear();
        foreach (AICommander aicommander in base.AICommanders)
        {
            foreach (AICommanderMission aicommanderMission in aicommander.Missions)
            {
                if (aicommanderMission.Completion == AICommanderMission.AICommanderMissionCompletion.Initializing)
                {
                    AICommanderMission_RegroupArmyAt aicommanderMission_RegroupArmyAt = aicommanderMission as AICommanderMission_RegroupArmyAt;
                    if (aicommanderMission_RegroupArmyAt != null)
                    {
                        this.unitsGUIDS.AddRange(aicommanderMission_RegroupArmyAt.UnitGuids);
                    }
                }
            }
        }
        DepartmentOfDefense agency = this.Empire.GetAgency <DepartmentOfDefense>();

        for (int i = 0; i < agency.Armies.Count; i++)
        {
            this._Internal_ResetRecruitementLocks(agency.Armies[i]);
        }
        if (this.Empire is MajorEmpire)
        {
            DepartmentOfTheInterior agency2 = this.Empire.GetAgency <DepartmentOfTheInterior>();
            if (agency2 != null)
            {
                for (int j = 0; j < agency2.Cities.Count; j++)
                {
                    this._Internal_ResetRecruitementLocks(agency2.Cities[j]);
                }
            }
            List <Village> convertedVillages = ((MajorEmpire)this.Empire).ConvertedVillages;
            for (int k = 0; k < convertedVillages.Count; k++)
            {
                Village village = convertedVillages[k];
                if (village != null)
                {
                    this._Internal_ResetRecruitementLocks(village);
                }
            }
        }
    }
    protected override bool TryComputeArmyMissionParameter()
    {
        if (!base.AIDataArmyGUID.IsValid)
        {
            base.Completion = AICommanderMission.AICommanderMissionCompletion.Fail;
            return(false);
        }
        AIData_Army aidata = this.aiDataRepository.GetAIData <AIData_Army>(base.AIDataArmyGUID);

        if (aidata == null || aidata.Army == null)
        {
            base.Completion = AICommanderMission.AICommanderMissionCompletion.Fail;
            return(false);
        }
        if (aidata.Army.IsLocked || aidata.Army.IsInEncounter)
        {
            return(false);
        }
        if (this.AICommanderRegroupArmies.MissionHasAllUnits(this))
        {
            RequestGarrisonMessage requestGarrisonMessage = null;
            if (this.AICommanderRegroupArmies != null && this.AICommanderRegroupArmies.RequestUnitListMessageID != 0UL)
            {
                requestGarrisonMessage = (base.Commander.AIPlayer.Blackboard.GetMessage(this.AICommanderRegroupArmies.RequestUnitListMessageID) as RequestGarrisonMessage);
            }
            if (requestGarrisonMessage == null)
            {
                RequestGarrisonCampMessage requestGarrisonCampMessage = null;
                if (this.AICommanderRegroupArmies != null && this.AICommanderRegroupArmies.RequestUnitListMessageID != 0UL)
                {
                    requestGarrisonCampMessage = (base.Commander.AIPlayer.Blackboard.GetMessage(this.AICommanderRegroupArmies.RequestUnitListMessageID) as RequestGarrisonCampMessage);
                }
                if (requestGarrisonCampMessage == null)
                {
                    base.Completion = AICommanderMission.AICommanderMissionCompletion.Success;
                    return(false);
                }
                IGameEntity gameEntity;
                if (!this.gameEntityRepositoryService.TryGetValue(requestGarrisonCampMessage.CampGuid, out gameEntity))
                {
                    base.Completion = AICommanderMission.AICommanderMissionCompletion.Fail;
                    return(false);
                }
                Camp camp = gameEntity as Camp;
                if (camp == null)
                {
                    base.Completion = AICommanderMission.AICommanderMissionCompletion.Fail;
                    return(false);
                }
                if (!this.AICommanderRegroupArmies.FinalPosition.IsValid)
                {
                    this.AICommanderRegroupArmies.FinalPosition = camp.GetValidDistrictToTarget(aidata.Army).WorldPosition;
                    requestGarrisonCampMessage.FinalPosition    = this.AICommanderRegroupArmies.FinalPosition;
                }
                Army armyAtPosition = this.worldPositionningService.GetArmyAtPosition(this.AICommanderRegroupArmies.FinalPosition);
                if (armyAtPosition != null && armyAtPosition.GUID != base.AIDataArmyGUID)
                {
                    this.AICommanderRegroupArmies.FinalPosition = camp.GetValidDistrictToTarget(aidata.Army).WorldPosition;
                    requestGarrisonCampMessage.FinalPosition    = this.AICommanderRegroupArmies.FinalPosition;
                }
                bool flag = false;
                for (int i = 0; i < camp.Districts.Count; i++)
                {
                    if (camp.Districts[i].Type != DistrictType.Exploitation && camp.Districts[i].Type != DistrictType.Improvement && this.worldPositionningService.GetDistance(this.AICommanderRegroupArmies.FinalPosition, camp.Districts[i].WorldPosition) <= 1)
                    {
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                {
                    this.AICommanderRegroupArmies.FinalPosition = camp.GetValidDistrictToTarget(aidata.Army).WorldPosition;
                    requestGarrisonCampMessage.FinalPosition    = this.AICommanderRegroupArmies.FinalPosition;
                }
                if (aidata.Army.WorldPosition == this.AICommanderRegroupArmies.FinalPosition)
                {
                    base.Completion = AICommanderMission.AICommanderMissionCompletion.Success;
                    return(false);
                }
            }
            else
            {
                IGameEntity gameEntity2;
                if (!this.gameEntityRepositoryService.TryGetValue(requestGarrisonMessage.CityGuid, out gameEntity2))
                {
                    base.Completion = AICommanderMission.AICommanderMissionCompletion.Fail;
                    return(false);
                }
                City city = gameEntity2 as City;
                if (city == null)
                {
                    base.Completion = AICommanderMission.AICommanderMissionCompletion.Fail;
                    return(false);
                }
                if (city.IsInEncounter)
                {
                    return(false);
                }
                if (city.BesiegingEmpire != null)
                {
                    base.Completion = AICommanderMission.AICommanderMissionCompletion.Fail;
                    return(false);
                }
                if (!this.AICommanderRegroupArmies.FinalPosition.IsValid)
                {
                    this.AICommanderRegroupArmies.FinalPosition = city.GetValidDistrictToTarget(aidata.Army).WorldPosition;
                    requestGarrisonMessage.FinalPosition        = this.AICommanderRegroupArmies.FinalPosition;
                }
                Army armyAtPosition2 = this.worldPositionningService.GetArmyAtPosition(this.AICommanderRegroupArmies.FinalPosition);
                if (armyAtPosition2 != null && armyAtPosition2.GUID != base.AIDataArmyGUID)
                {
                    this.AICommanderRegroupArmies.FinalPosition = city.GetValidDistrictToTarget(aidata.Army).WorldPosition;
                    requestGarrisonMessage.FinalPosition        = this.AICommanderRegroupArmies.FinalPosition;
                }
                bool flag2 = false;
                for (int j = 0; j < city.Districts.Count; j++)
                {
                    if (city.Districts[j].Type != DistrictType.Exploitation && city.Districts[j].Type != DistrictType.Improvement && this.worldPositionningService.GetDistance(this.AICommanderRegroupArmies.FinalPosition, city.Districts[j].WorldPosition) <= 1)
                    {
                        flag2 = true;
                        break;
                    }
                }
                if (!flag2)
                {
                    this.AICommanderRegroupArmies.FinalPosition = city.GetValidDistrictToTarget(aidata.Army).WorldPosition;
                    requestGarrisonMessage.FinalPosition        = this.AICommanderRegroupArmies.FinalPosition;
                }
                if (aidata.Army.WorldPosition == this.AICommanderRegroupArmies.FinalPosition)
                {
                    base.Completion = AICommanderMission.AICommanderMissionCompletion.Success;
                    return(false);
                }
            }
        }
        if (this.targetTransferArmy != null)
        {
            if (this.targetTransferArmy.Army == null)
            {
                this.targetTransferArmy = null;
            }
            else
            {
                if (!this.targetTransferArmy.Army.IsInEncounter && !this.targetTransferArmy.Army.IsLocked)
                {
                    return(false);
                }
                this.targetTransferArmy = null;
            }
        }
        foreach (AICommanderMission aicommanderMission in this.AICommanderRegroupArmies.Missions)
        {
            AICommanderMission_RegroupArmyAt aicommanderMission_RegroupArmyAt = (AICommanderMission_RegroupArmyAt)aicommanderMission;
            if (aicommanderMission_RegroupArmyAt != this && aicommanderMission_RegroupArmyAt.targetTransferArmy == aidata)
            {
                if (!aidata.Army.IsLocked && !aidata.Army.IsInEncounter)
                {
                    return(false);
                }
                aicommanderMission_RegroupArmyAt.targetTransferArmy = null;
            }
        }
        if (this.AICommanderRegroupArmies != null && this.AICommanderRegroupArmies.AIPlayer != null && this.AICommanderRegroupArmies.AIPlayer.AIEntities != null)
        {
            AIEntity_Empire aientity_Empire = this.AICommanderRegroupArmies.AIPlayer.AIEntities.Find((AIEntity match) => match is AIEntity_Empire) as AIEntity_Empire;
            if (aientity_Empire != null)
            {
                AICommanderMission_PrivateersHarass aicommanderMission_PrivateersHarass = aientity_Empire.GetCommanderMissionBasedOnItsArmyRequestArmy(this.AICommanderRegroupArmies.RequestUnitListMessageID) as AICommanderMission_PrivateersHarass;
                if (aicommanderMission_PrivateersHarass != null && aicommanderMission_PrivateersHarass.TargetCity != null && !aidata.Army.IsPrivateers && base.TryCreateArmyMission("ConvertToPrivateers", new List <object>
                {
                    aicommanderMission_PrivateersHarass.TargetCity
                }))
                {
                    base.State = TickableState.NeedTick;
                    RequestUnitListMessage requestUnitListMessage = base.Commander.AIPlayer.Blackboard.GetMessage(this.AICommanderRegroupArmies.RequestUnitListMessageID) as RequestUnitListMessage;
                    if (requestUnitListMessage != null)
                    {
                        requestUnitListMessage.ExecutionState = RequestUnitListMessage.RequestUnitListState.Regrouping;
                    }
                    return(true);
                }
            }
        }
        WorldPosition worldPosition;

        if (this.IsMaster)
        {
            if (this.IsArmyBesiegingACity(base.AIDataArmyGUID) && !aidata.Army.IsPrivateers)
            {
                return(false);
            }
            if (this.CanTransferToNearMission())
            {
                return(false);
            }
            AICommanderMission_RegroupArmyAt aicommanderMission_RegroupArmyAt2 = null;
            int num = int.MaxValue;
            foreach (AICommanderMission aicommanderMission2 in this.AICommanderRegroupArmies.Missions)
            {
                AICommanderMission_RegroupArmyAt aicommanderMission_RegroupArmyAt3 = (AICommanderMission_RegroupArmyAt)aicommanderMission2;
                if (aicommanderMission_RegroupArmyAt3 != this)
                {
                    WorldPosition unitsToRegroupPosition = aicommanderMission_RegroupArmyAt3.GetUnitsToRegroupPosition();
                    if (unitsToRegroupPosition.IsValid)
                    {
                        int distance = this.worldPositionningService.GetDistance(aidata.Army.WorldPosition, unitsToRegroupPosition);
                        if (distance < num)
                        {
                            num = distance;
                            aicommanderMission_RegroupArmyAt2 = aicommanderMission_RegroupArmyAt3;
                        }
                    }
                }
            }
            if (aicommanderMission_RegroupArmyAt2 == null)
            {
                if (!this.AICommanderRegroupArmies.FinalPosition.IsValid)
                {
                    base.Completion = AICommanderMission.AICommanderMissionCompletion.Success;
                    return(false);
                }
                worldPosition = this.AICommanderRegroupArmies.FinalPosition;
            }
            else
            {
                worldPosition = aicommanderMission_RegroupArmyAt2.GetUnitsToRegroupPosition();
            }
        }
        else
        {
            if (this.CanTransferToNearMission())
            {
                return(false);
            }
            AICommanderMission_RegroupArmyAt masterMission = this.AICommanderRegroupArmies.MasterMission;
            if (masterMission == null)
            {
                return(false);
            }
            worldPosition = masterMission.GetUnitsToRegroupPosition();
        }
        base.State = TickableState.NoTick;
        if (aidata.Army.GetPropertyValue(SimulationProperties.Movement) > 0f)
        {
            int num2 = Math.Min(4, Math.Max(1, (this.worldPositionningService.GetDistance(worldPosition, aidata.Army.WorldPosition) - 1) / 2));
            PathfindingContext pathfindingContext = aidata.Army.GenerateContext();
            pathfindingContext.Greedy = true;
            PathfindingResult pathfindingResult = this.pathfindingService.FindPath(pathfindingContext, aidata.Army.WorldPosition, worldPosition, PathfindingManager.RequestMode.Default, null, PathfindingFlags.IgnoreFogOfWar, null);
            if (pathfindingResult != null)
            {
                int           num3           = 0;
                WorldPosition worldPosition2 = WorldPosition.Invalid;
                foreach (WorldPosition worldPosition3 in pathfindingResult.GetCompletePath())
                {
                    if (worldPosition3 != pathfindingResult.Start)
                    {
                        num3++;
                        if ((num3 <= num2 || !worldPosition2.IsValid) && worldPosition3 != worldPosition && this.pathfindingService.IsTileStopable(worldPosition3, aidata.Army, (PathfindingFlags)0, null))
                        {
                            worldPosition2 = worldPosition3;
                        }
                        if (num3 >= num2 && worldPosition2.IsValid && base.TryCreateArmyMission("ReachPosition", new List <object>
                        {
                            worldPosition2
                        }))
                        {
                            base.State = TickableState.NeedTick;
                            this.SetRequestMessageExecutionState(RequestUnitListMessage.RequestUnitListState.Regrouping);
                            return(true);
                        }
                    }
                }
            }
            if (base.Commander.Empire.SimulationObject.Tags.Contains(FactionTrait.FactionTraitMimics1))
            {
                DepartmentOfTheInterior agency = base.Commander.Empire.GetAgency <DepartmentOfTheInterior>();
                if (agency.NonInfectedCities.Count > 0 && base.TryCreateArmyMission("ReachPositionMykara", new List <object>
                {
                    agency.NonInfectedCities[0].WorldPosition
                }))
                {
                    base.State = TickableState.NeedTick;
                    this.SetRequestMessageExecutionState(RequestUnitListMessage.RequestUnitListState.Regrouping);
                    return(true);
                }
            }
        }
        return(false);
    }