public UnitModelProductionNeeds(UnitDesign unitDesign, RequestUnitListMessage requestArmy, int count, StaticString seekedUnitPatternCategory)
 {
     this.Count       = count;
     this.RequestArmy = requestArmy;
     this.UnitDesign  = unitDesign;
     this.WantedUnitPatternCategory = seekedUnitPatternCategory;
 }
    private void CreateNewCommanderRegroup(GameEntityGUID[] units, RequestUnitListMessage message)
    {
        AICommander commander = new AICommander_RegroupArmies(units, (message == null) ? 0UL : message.ID)
        {
            AIPlayer = base.AIEntity.AIPlayer,
            Empire   = base.AIEntity.Empire
        };

        this.AddCommander(commander);
    }
 protected override void Fail()
 {
     base.Fail();
     if (this.AICommanderRegroupArmies != null && this.AICommanderRegroupArmies.RequestUnitListMessageID != 0UL)
     {
         RequestUnitListMessage requestUnitListMessage = base.Commander.AIPlayer.Blackboard.GetMessage(this.AICommanderRegroupArmies.RequestUnitListMessageID) as RequestUnitListMessage;
         if (requestUnitListMessage != null)
         {
             requestUnitListMessage.ExecutionState = RequestUnitListMessage.RequestUnitListState.Pending;
         }
     }
 }
 private bool IsCommanderStillRegrouping(RequestUnitListMessage requestUnitList)
 {
     for (int i = 0; i < this.aiCommanders.Count; i++)
     {
         AICommander_RegroupArmies aicommander_RegroupArmies = this.aiCommanders[i] as AICommander_RegroupArmies;
         if (aicommander_RegroupArmies != null && requestUnitList.ID == aicommander_RegroupArmies.RequestUnitListMessageID)
         {
             return(true);
         }
     }
     return(false);
 }
    private void SetRequestMessageExecutionState(RequestUnitListMessage.RequestUnitListState executionState)
    {
        if (this.AICommanderRegroupArmies.RequestUnitListMessageID == GameEntityGUID.Zero)
        {
            return;
        }
        RequestUnitListMessage requestUnitListMessage = base.Commander.AIPlayer.Blackboard.GetMessage(this.AICommanderRegroupArmies.RequestUnitListMessageID) as RequestUnitListMessage;

        if (requestUnitListMessage != null)
        {
            requestUnitListMessage.ExecutionState = executionState;
        }
    }
 public override void Release()
 {
     base.Release();
     if (this.AICommanderRegroupArmies != null && this.AICommanderRegroupArmies.RequestUnitListMessageID != 0UL)
     {
         RequestUnitListMessage requestUnitListMessage = base.Commander.AIPlayer.Blackboard.GetMessage(this.AICommanderRegroupArmies.RequestUnitListMessageID) as RequestUnitListMessage;
         if (requestUnitListMessage != null)
         {
             requestUnitListMessage.ExecutionState = RequestUnitListMessage.RequestUnitListState.Pending;
         }
     }
     this.majorEmpireAIEntity = null;
 }
 protected override void Pending()
 {
     base.Pending();
     if (this.currentTicket != null)
     {
         return;
     }
     if (this.AICommanderRegroupArmies != null && this.AICommanderRegroupArmies.RequestUnitListMessageID != 0UL)
     {
         RequestUnitListMessage requestUnitListMessage = base.Commander.AIPlayer.Blackboard.GetMessage(this.AICommanderRegroupArmies.RequestUnitListMessageID) as RequestUnitListMessage;
         if (requestUnitListMessage != null)
         {
             requestUnitListMessage.ExecutionState = RequestUnitListMessage.RequestUnitListState.Pending;
         }
     }
     base.State = TickableState.NoTick;
 }
    private void ResetRequestArmyMessages()
    {
        List <RequestUnitListMessage> list = new List <RequestUnitListMessage>(base.AIEntity.AIPlayer.Blackboard.GetMessages <RequestUnitListMessage>(BlackboardLayerID.Empire));
        int i = 0;

        while (i < list.Count)
        {
            RequestUnitListMessage requestUnitListMessage = list[i];
            if (requestUnitListMessage.ExecutionState == RequestUnitListMessage.RequestUnitListState.Regrouping)
            {
                if (!this.IsCommanderStillRegrouping(requestUnitListMessage))
                {
                    requestUnitListMessage.ExecutionState = RequestUnitListMessage.RequestUnitListState.Pending;
                    goto IL_72;
                }
                requestUnitListMessage.ExecutionState = RequestUnitListMessage.RequestUnitListState.Regrouping;
            }
            else if (requestUnitListMessage.State != BlackboardMessage.StateValue.Message_Canceled && requestUnitListMessage.State != BlackboardMessage.StateValue.Message_Failed)
            {
                goto IL_72;
            }
IL_69:
            i++;
            continue;
IL_72:
            if (requestUnitListMessage.ArmyPattern == null || requestUnitListMessage.ArmyPattern.UnitPatternCategoryList.Count == 0)
            {
                base.AIEntity.AIPlayer.Blackboard.CancelMessage(requestUnitListMessage);
                goto IL_69;
            }
            if (requestUnitListMessage is RequestArmyMessage)
            {
                (requestUnitListMessage as RequestArmyMessage).HeroGUID = GameEntityGUID.Zero;
            }
            for (int j = 0; j < requestUnitListMessage.ArmyPattern.UnitPatternCategoryList.Count; j++)
            {
                if (StaticString.IsNullOrEmpty(requestUnitListMessage.ArmyPattern.UnitPatternCategoryList[j].Category))
                {
                    base.AIEntity.AIPlayer.Blackboard.CancelMessage(requestUnitListMessage);
                    break;
                }
            }
            goto IL_69;
        }
    }
 private void LaunchProductionNeeds(DepartmentOfDefense departmentOfDefense, RequestUnitListMessage requestUnitListMessage)
 {
     Predicate <AILayer_ArmyRecruitment.UnitModelProductionNeeds> < > 9__1;
     for (int i = 0; i < requestUnitListMessage.ArmyPattern.UnitPatternCategoryList.Count; i++)
     {
         ArmyPattern.UnitPatternCategory unitPatternCategory = requestUnitListMessage.ArmyPattern.UnitPatternCategoryList[i];
         UnitDesign bestUnitDesign = this.intelligenceAIHelper.GetBestUnitDesignForNeededCategory(departmentOfDefense, unitPatternCategory);
         if (bestUnitDesign != null)
         {
             int num = this.countByUnitModel.FindIndex((AILayer_ArmyRecruitment.UnitModelProductionNeeds match) => match.UnitDesign != null && match.UnitDesign.Model == bestUnitDesign.Model && match.RequestArmy.ID == requestUnitListMessage.ID);
             if (num < 0)
             {
                 num = ~num;
                 this.countByUnitModel.Insert(num, new AILayer_ArmyRecruitment.UnitModelProductionNeeds(bestUnitDesign, requestUnitListMessage, 1, unitPatternCategory.Category));
             }
             else
             {
                 this.countByUnitModel[num].Count++;
             }
         }
         else
         {
             List <AILayer_ArmyRecruitment.UnitModelProductionNeeds>      list = this.countByUnitModel;
             Predicate <AILayer_ArmyRecruitment.UnitModelProductionNeeds> match2;
             if ((match2 = < > 9__1) == null)
             {
                 match2 = (< > 9__1 = ((AILayer_ArmyRecruitment.UnitModelProductionNeeds match) => match.UnitDesign == null && match.RequestArmy.ID == requestUnitListMessage.ID));
             }
             int num2 = list.FindIndex(match2);
             if (num2 < 0)
             {
                 num2 = ~num2;
                 this.countByUnitModel.Insert(num2, new AILayer_ArmyRecruitment.UnitModelProductionNeeds(null, requestUnitListMessage, 1, unitPatternCategory.Category));
             }
             else
             {
                 this.countByUnitModel[num2].Count++;
             }
         }
     }
 }
    protected override bool TryGetArmyData()
    {
        if (this.createFirstArmyTicket != null)
        {
            return(false);
        }
        if (base.AIDataArmyGUID.IsValid)
        {
            return(true);
        }
        if (this.currentTicket != null)
        {
            return(false);
        }
        IGameEntity gameEntity;

        if (!this.gameEntityRepositoryService.TryGetValue(this.SourceGuid, out gameEntity))
        {
            base.Completion = AICommanderMission.AICommanderMissionCompletion.Fail;
            return(false);
        }
        for (int i = this.UnitGuids.Length - 1; i >= 0; i--)
        {
            AIData_Unit aidata_Unit;
            this.aiDataRepository.TryGetAIData <AIData_Unit>(this.UnitGuids[i], out aidata_Unit);
            if (aidata_Unit == null)
            {
                Diagnostics.LogWarning("[AICommanderMission_RegroupArmyAt] no AIData for Unit {0}", new object[]
                {
                    this.UnitGuids[i].ToString()
                });
            }
            else
            {
                if (aidata_Unit.IsUnitLocked() && !aidata_Unit.IsUnitLockedByMe(base.InternalGUID))
                {
                    WorldPosition worldPosition = WorldPosition.Invalid;
                    if (aidata_Unit.Unit.Garrison is IWorldPositionable)
                    {
                        worldPosition = (aidata_Unit.Unit.Garrison as IWorldPositionable).WorldPosition;
                    }
                    Diagnostics.LogWarning(string.Format("{1} {2} LOCKING: Unit shouldn't be in use, current unit state: {0}", aidata_Unit.GetLockingStateString(), base.Commander.Empire, worldPosition));
                    base.Completion = AICommanderMission.AICommanderMissionCompletion.Fail;
                    return(false);
                }
                if (aidata_Unit.Unit.UnitDesign.Tags.Contains(DownloadableContent9.TagSolitary))
                {
                    Diagnostics.LogWarning("You cannot regroup Colossus or Solitary unit with something else.");
                    base.Completion = AICommanderMission.AICommanderMissionCompletion.Fail;
                    return(false);
                }
            }
        }
        IGarrison garrison = gameEntity as IGarrison;

        if (garrison == null)
        {
            base.Completion = AICommanderMission.AICommanderMissionCompletion.Fail;
            return(false);
        }
        if (this.AICommanderRegroupArmies != null && this.AICommanderRegroupArmies.RequestUnitListMessageID != 0UL)
        {
            RequestUnitListMessage requestUnitListMessage = base.Commander.AIPlayer.Blackboard.GetMessage(this.AICommanderRegroupArmies.RequestUnitListMessageID) as RequestUnitListMessage;
            if (requestUnitListMessage == null)
            {
                base.Completion = AICommanderMission.AICommanderMissionCompletion.Fail;
                return(false);
            }
            requestUnitListMessage.ExecutionState = RequestUnitListMessage.RequestUnitListState.Regrouping;
        }
        bool flag = false;

        for (int j = 0; j < this.UnitGuids.Length; j++)
        {
            if (!garrison.ContainsUnit(this.UnitGuids[j]))
            {
                flag = true;
                break;
            }
        }
        if (flag)
        {
            List <GameEntityGUID> list = new List <GameEntityGUID>();
            for (int k = 0; k < this.UnitGuids.Length; k++)
            {
                if (garrison.ContainsUnit(this.UnitGuids[k]))
                {
                    list.Add(this.UnitGuids[k]);
                }
            }
            this.UnitGuids = list.ToArray();
        }
        if (this.UnitGuids.Length == 0)
        {
            base.Completion = AICommanderMission.AICommanderMissionCompletion.Fail;
            return(false);
        }
        WorldPosition armyPosition = WorldPosition.Invalid;
        Army          army         = gameEntity as Army;

        if (army != null)
        {
            AIData_Army aidata = this.aiDataRepository.GetAIData <AIData_Army>(gameEntity.GUID);
            Diagnostics.Assert(aidata != null);
            if (army.GetPropertyValue(SimulationProperties.Movement) <= 0f)
            {
                return(false);
            }
            if (aidata.CommanderMission != null)
            {
                aidata.CommanderMission.Interrupt();
            }
            if (aidata.Army.IsInEncounter || aidata.Army.IsLocked)
            {
                return(false);
            }
            if (aidata.Army.StandardUnits.Count != this.UnitGuids.Length)
            {
                armyPosition = AILayer_ArmyRecruitment.GetValidArmySpawningPosition(aidata.Army, this.worldPositionningService, this.pathfindingService);
                int num = (int)base.Commander.Empire.GetPropertyValue(SimulationProperties.ArmyUnitSlot);
                if (aidata.Army.StandardUnits.Count > num)
                {
                    Array.Resize <GameEntityGUID>(ref this.UnitGuids, num);
                }
            }
            else
            {
                if (!aidata.AssignCommanderMission(this))
                {
                    base.Completion = AICommanderMission.AICommanderMissionCompletion.Fail;
                    return(false);
                }
                base.AIDataArmyGUID = gameEntity.GUID;
                return(true);
            }
        }
        else if (gameEntity is Village)
        {
            Village village = gameEntity as Village;
            if (village.IsInEncounter)
            {
                return(false);
            }
            WorldPosition    worldPosition2   = village.WorldPosition;
            WorldOrientation worldOrientation = WorldOrientation.East;
            if (this.AICommanderRegroupArmies.FinalPosition.IsValid)
            {
                worldOrientation = this.worldPositionningService.GetOrientation(worldPosition2, this.AICommanderRegroupArmies.FinalPosition);
            }
            for (int l = 0; l < 6; l++)
            {
                WorldPosition neighbourTile = this.worldPositionningService.GetNeighbourTile(worldPosition2, worldOrientation, 1);
                if (DepartmentOfDefense.CheckWhetherTargetPositionIsValidForUseAsArmySpawnLocation(neighbourTile, PathfindingMovementCapacity.Ground | PathfindingMovementCapacity.Water) && this.pathfindingService.IsTransitionPassable(village.WorldPosition, neighbourTile, this.pathfindingContext, PathfindingFlags.IgnoreFogOfWar, null) && this.pathfindingService.IsTilePassable(neighbourTile, this.pathfindingContext, (PathfindingFlags)0, null) && this.pathfindingService.IsTileStopable(neighbourTile, this.pathfindingContext, (PathfindingFlags)0, null))
                {
                    if (this.worldPositionningService.GetArmyAtPosition(neighbourTile) == null)
                    {
                        armyPosition = neighbourTile;
                        break;
                    }
                }
                else
                {
                    worldOrientation = worldOrientation.Rotate(1);
                }
            }
        }
        else if (gameEntity is City)
        {
            City city = gameEntity as City;
            if (city.IsInEncounter)
            {
                return(false);
            }
            if (!DepartmentOfTheInterior.TryGetWorldPositionForNewArmyFromCity(city, this.pathfindingService, this.pathfindingContext, out armyPosition))
            {
                return(false);
            }
        }
        else
        {
            Diagnostics.LogError(string.Format("[AICommanderMission_RegroupArmyAt] The garrison {0} is not valid.", garrison.GUID));
        }
        if (armyPosition.IsValid)
        {
            if (army != null && army.WorldPath != null && army.WorldPath.Destination != army.WorldPosition)
            {
                OrderGoTo orderGoTo = new OrderGoTo(army.Empire.Index, army.GUID, army.WorldPosition);
                orderGoTo.Flags = (PathfindingFlags)0;
                base.Commander.Empire.PlayerControllers.AI.PostOrder(orderGoTo, out this.currentTicket, new EventHandler <TicketRaisedEventArgs>(this.MoveOrder_TicketRaised));
                if (this.currentTicket != null)
                {
                    return(false);
                }
                if (!this.orderExecuted)
                {
                    Diagnostics.LogError("[AICommanderMission_RegroupArmyAt] new cancelation move order was not executed, from {0} to {1}", new object[]
                    {
                        army.WorldPosition,
                        army.WorldPosition
                    });
                    return(false);
                }
            }
            for (int m = 0; m < this.UnitGuids.Length; m++)
            {
                AIData_Unit aidata_Unit2;
                this.aiDataRepository.TryGetAIData <AIData_Unit>(this.UnitGuids[m], out aidata_Unit2);
                if (aidata_Unit2 == null)
                {
                    Diagnostics.LogWarning("[AICommanderMission_RegroupArmyAt] no AIData for Unit {0}", new object[]
                    {
                        this.UnitGuids[m].ToString()
                    });
                }
                else if (!aidata_Unit2.IsUnitLockedByMe(base.InternalGUID))
                {
                    aidata_Unit2.TryLockUnit(base.InternalGUID, base.GetType().ToString(), AIData_Unit.AIDataReservationExtraTag.Regrouping, base.Commander.GetPriority(this));
                }
            }
            OrderTransferGarrisonToNewArmy order = new OrderTransferGarrisonToNewArmy(base.Commander.Empire.Index, this.SourceGuid, this.UnitGuids, armyPosition, "Regroup", false, true, true);
            base.Commander.Empire.PlayerControllers.Server.PostOrder(order, out this.createFirstArmyTicket, new EventHandler <TicketRaisedEventArgs>(this.OrderTransferUnitToNewArmyTicketRaised));
        }
        return(false);
    }
    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);
    }
    protected override void CreateLocalNeeds(StaticString context, StaticString pass)
    {
        AILayer_Military layer = base.AIEntity.GetLayer <AILayer_Military>();

        base.CreateLocalNeeds(context, pass);
        this.VerifyAndUpdateRecruitementLocks();
        Diagnostics.Assert(base.AIEntity != null && base.AIEntity.AIPlayer != null && base.AIEntity.AIPlayer.Blackboard != null);
        IEnumerable <RequestUnitListMessage> messages = base.AIEntity.AIPlayer.Blackboard.GetMessages <RequestUnitListMessage>(BlackboardLayerID.Empire);

        Diagnostics.Assert(messages != null);
        List <RequestUnitListMessage> list = new List <RequestUnitListMessage>(messages);

        Diagnostics.Assert(this.Empire != null);
        list.RemoveAll((RequestUnitListMessage match) => match.EmpireTarget != this.Empire.Index);
        this.ResetRequestArmyMessages();
        this.countByUnitModel.Clear();
        list.Sort((RequestUnitListMessage left, RequestUnitListMessage right) => - 1 * left.Priority.CompareTo(right.Priority));
        this.intelligenceAIHelper.FillAvailableUnitDesignList(this.Empire);
        Diagnostics.Assert(this.intelligenceAIHelper != null);
        for (int i = 0; i < list.Count; i++)
        {
            RequestUnitListMessage requestUnitListMessage = list[i];
            if (float.IsNaN(requestUnitListMessage.Priority) || float.IsInfinity(requestUnitListMessage.Priority))
            {
                AILayer.LogWarning("[SCORING] Skipping RequestUnitListMessage {0} with a priority of {1}", new object[]
                {
                    requestUnitListMessage.CommanderCategory,
                    requestUnitListMessage.Priority
                });
            }
            else if (AILayer_ArmyRecruitment.IsCommanderMissionNotInteresting(requestUnitListMessage.CommanderCategory))
            {
                requestUnitListMessage.State   = BlackboardMessage.StateValue.Message_Canceled;
                requestUnitListMessage.TimeOut = 0;
            }
            else
            {
                BlackboardMessage.StateValue state = requestUnitListMessage.State;
                if (state != BlackboardMessage.StateValue.Message_None)
                {
                    if (state == BlackboardMessage.StateValue.Message_Success || state == BlackboardMessage.StateValue.Message_Canceled)
                    {
                        goto IL_1F3;
                    }
                    if (state != BlackboardMessage.StateValue.Message_InProgress)
                    {
                        AILayer.LogError("[AILayer_ArmyRecruitment] Unknow state for RequestArmyMessage {0}", new object[]
                        {
                            requestUnitListMessage.State
                        });
                        goto IL_1F3;
                    }
                }
                if (requestUnitListMessage.ExecutionState == RequestUnitListMessage.RequestUnitListState.Pending)
                {
                    Intelligence.BestRecruitementCombination bestRecruitementCombination;
                    if (requestUnitListMessage is RequestGarrisonMessage)
                    {
                        bestRecruitementCombination = null;
                    }
                    else if (requestUnitListMessage is RequestGarrisonCampMessage)
                    {
                        bestRecruitementCombination = null;
                    }
                    else
                    {
                        bestRecruitementCombination = this.intelligenceAIHelper.FillArmyPattern(this.Empire.Index, requestUnitListMessage, layer);
                    }
                    if (bestRecruitementCombination != null)
                    {
                        this.RecruitArmiesUnits(requestUnitListMessage, bestRecruitementCombination);
                    }
                }
            }
            IL_1F3 :;
        }
        this.ResetRecruitementLocks();
        this.RegroupSmallFreeArmies();
        this.CleanupRequestUnitMessages();
    }
    private void RecruitArmiesUnits(RequestUnitListMessage requestUnitListMessage, Intelligence.BestRecruitementCombination bestRecruits)
    {
        IAIDataRepositoryAIHelper service = AIScheduler.Services.GetService <IAIDataRepositoryAIHelper>();

        this.unitsGUIDS.Clear();
        for (int i = 0; i < bestRecruits.CombinationOfArmiesUnits.Count; i++)
        {
            AIData_GameEntity aidata_GameEntity = bestRecruits.CombinationOfArmiesUnits[i];
            if (aidata_GameEntity is AIData_Army)
            {
                AIData_Army aidata_Army = aidata_GameEntity as AIData_Army;
                if (aidata_Army.Army == null)
                {
                    AILayer.LogWarning(string.Format("[AILayer_ArmyRecruitment.UnitAssignation] Army was not found, was it destroyed?", new object[0]));
                }
                else
                {
                    if (aidata_Army.CommanderMission != null)
                    {
                        aidata_Army.CommanderMission.Interrupt();
                    }
                    for (int j = 0; j < aidata_Army.Army.StandardUnits.Count; j++)
                    {
                        Unit        unit = aidata_Army.Army.StandardUnits[j];
                        AIData_Unit aidata_Unit;
                        if (service.TryGetAIData <AIData_Unit>(unit.GUID, out aidata_Unit))
                        {
                            this.unitsGUIDS.Add(unit.GUID);
                            aidata_Unit.ClearLock();
                            aidata_Unit.TryLockUnit(base.InternalGUID, base.GetType().ToString(), AIData_Unit.AIDataReservationExtraTag.ArmyRecruitment, requestUnitListMessage.Priority);
                        }
                    }
                }
            }
            else if (aidata_GameEntity is AIData_Unit)
            {
                AIData_Unit aidata_Unit2 = aidata_GameEntity as AIData_Unit;
                Unit        unit2        = aidata_Unit2.Unit;
                if (unit2 == null || unit2.Garrison == null)
                {
                    AILayer.LogWarning(string.Format("[AILayer_ArmyRecruitment.UnitAssignation] Unit was not found, was it destroyed?", new object[0]));
                }
                else
                {
                    this.unitsGUIDS.Add(unit2.GUID);
                    if (aidata_Unit2.IsUnitLocked() && !aidata_Unit2.IsUnitLockedByMe(base.InternalGUID))
                    {
                        IGarrison garrison = unit2.Garrison;
                        if (garrison is Army)
                        {
                            Army        army   = garrison as Army;
                            AIData_Army aidata = service.GetAIData <AIData_Army>(army.GUID);
                            if (aidata == null)
                            {
                                AILayer.LogWarning(string.Format("[AILayer_ArmyRecruitment.UnitAssignation] Army was not found, was it destroyed?", new object[0]));
                            }
                            else if (aidata.CommanderMission != null)
                            {
                                aidata.CommanderMission.Interrupt();
                            }
                            else
                            {
                                AILayer.LogWarning(string.Format("[AILayer_ArmyRecruitment.UnitAssignation] Although unit is locked, Army commander mission was not found.", new object[0]));
                                aidata_Unit2.ClearLock();
                            }
                        }
                        else if (garrison is City)
                        {
                            aidata_Unit2.ClearLock();
                        }
                        else
                        {
                            aidata_Unit2.ClearLock();
                        }
                    }
                    aidata_Unit2.TryLockUnit(base.InternalGUID, base.GetType().ToString(), AIData_Unit.AIDataReservationExtraTag.ArmyRecruitment, requestUnitListMessage.Priority);
                }
            }
            else
            {
                AILayer.LogError("[AILayer_ArmyRecruitment.UnitAssignation] Unknown GameEntity Type {0}", new object[]
                {
                    aidata_GameEntity.GetType().ToString()
                });
            }
        }
        if (this.unitsGUIDS.Count != 0)
        {
            this.CreateNewCommanderRegroup(this.unitsGUIDS.ToArray(), requestUnitListMessage);
            requestUnitListMessage.ExecutionState = RequestUnitListMessage.RequestUnitListState.Regrouping;
        }
    }