示例#1
0
    public bool IsTaggedFreeForExploration()
    {
        if (AIScheduler.Services == null)
        {
            return(false);
        }
        if (this.GetArmyLockState() != AIData_Army.AIDataArmyLockState.Free)
        {
            return(false);
        }
        if (this.Army.HasSeafaringUnits())
        {
            return(false);
        }
        IAIDataRepositoryAIHelper service = AIScheduler.Services.GetService <IAIDataRepositoryAIHelper>();

        if (this.Army != null)
        {
            for (int i = 0; i < this.Army.StandardUnits.Count; i++)
            {
                AIData_Unit aidata_Unit;
                if (service.TryGetAIData <AIData_Unit>(this.Army.StandardUnits[i].GUID, out aidata_Unit) && aidata_Unit.ReservationExtraTag != AIData_Unit.AIDataReservationExtraTag.FreeForExploration)
                {
                    return(false);
                }
            }
        }
        return(true);
    }
    public AIArmyMission.AIArmyMissionErrorCode GetArmyUnlessLocked(AIBehaviorTree aiBehaviorTree, string variableName, out Army army)
    {
        army = null;
        if (!aiBehaviorTree.Variables.ContainsKey(variableName))
        {
            aiBehaviorTree.LogError(variableName + " not set", new object[0]);
            aiBehaviorTree.ErrorCode = 34;
            return(AIArmyMission.AIArmyMissionErrorCode.InvalidArmy);
        }
        army = (aiBehaviorTree.Variables[variableName] as Army);
        IAIDataRepositoryAIHelper service = AIScheduler.Services.GetService <IAIDataRepositoryAIHelper>();

        Diagnostics.Assert(service != null);
        AIData_Army aidata_Army;

        if (!service.TryGetAIData <AIData_Army>(army.GUID, out aidata_Army))
        {
            aiBehaviorTree.LogError("ArmyGUID {0} not found in AIDataRepository", new object[]
            {
                army.GUID
            });
            aiBehaviorTree.ErrorCode = 35;
            return(AIArmyMission.AIArmyMissionErrorCode.MissingArmyAIData);
        }
        if (army.IsLocked || army.IsInEncounter)
        {
            aiBehaviorTree.Log(variableName + " locked or in encounter", new object[0]);
            aiBehaviorTree.ErrorCode = 17;
            return(AIArmyMission.AIArmyMissionErrorCode.TargetLocked);
        }
        return(AIArmyMission.AIArmyMissionErrorCode.None);
    }
示例#3
0
    public AIData_Army.AIDataArmyLockState GetArmyLockState()
    {
        if (AIScheduler.Services == null)
        {
            return(AIData_Army.AIDataArmyLockState.Free);
        }
        IAIDataRepositoryAIHelper service = AIScheduler.Services.GetService <IAIDataRepositoryAIHelper>();

        if (this.Army != null)
        {
            GameEntityGUID x = GameEntityGUID.Zero;
            for (int i = 0; i < this.Army.StandardUnits.Count; i++)
            {
                AIData_Unit aidata_Unit;
                if (service.TryGetAIData <AIData_Unit>(this.Army.StandardUnits[i].GUID, out aidata_Unit) && aidata_Unit.IsUnitLocked())
                {
                    if (x == GameEntityGUID.Zero)
                    {
                        x = aidata_Unit.ReservingGUID;
                    }
                    else if (x != aidata_Unit.ReservingGUID)
                    {
                        return(AIData_Army.AIDataArmyLockState.Hybrid);
                    }
                }
            }
            if (x != GameEntityGUID.Zero)
            {
                return(AIData_Army.AIDataArmyLockState.Locked);
            }
        }
        return(AIData_Army.AIDataArmyLockState.Free);
    }
    private void VerifyAndUpdateRecruitementLocks()
    {
        IAIDataRepositoryAIHelper service = AIScheduler.Services.GetService <IAIDataRepositoryAIHelper>();
        DepartmentOfDefense       agency  = this.Empire.GetAgency <DepartmentOfDefense>();

        for (int i = 0; i < agency.Armies.Count; i++)
        {
            AIData_Unit aidata_Unit;
            for (int j = 0; j < agency.Armies[i].StandardUnits.Count; j++)
            {
                if (service.TryGetAIData <AIData_Unit>(agency.Armies[i].StandardUnits[j].GUID, out aidata_Unit) && aidata_Unit.IsUnitLocked())
                {
                    if (aidata_Unit.IsUnitLockedByMe(base.InternalGUID))
                    {
                        aidata_Unit.TryUnLockUnit(base.InternalGUID);
                    }
                    else
                    {
                        AICommanderMission commanderMissionWithGUID = base.AIEntity.GetCommanderMissionWithGUID(aidata_Unit.ReservingGUID);
                        if (commanderMissionWithGUID == null)
                        {
                            aidata_Unit.ClearLock();
                        }
                        else
                        {
                            aidata_Unit.ReservationPriority = commanderMissionWithGUID.Commander.GetPriority(commanderMissionWithGUID);
                        }
                    }
                }
            }
            if (agency.Armies[i].Hero != null && service.TryGetAIData <AIData_Unit>(agency.Armies[i].Hero.GUID, out aidata_Unit) && aidata_Unit.IsUnitLocked())
            {
                aidata_Unit.ClearLock();
            }
        }
    }
    private State ArmyExecute(Army targetArmy, AIBehaviorTree aiBehaviorTree, params object[] parameters)
    {
        IAIDataRepositoryAIHelper service = AIScheduler.Services.GetService <IAIDataRepositoryAIHelper>();

        Diagnostics.Assert(service != null);
        AIData_Army aidata_Army;

        if (!service.TryGetAIData <AIData_Army>(targetArmy.GUID, out aidata_Army))
        {
            aiBehaviorTree.LogError("ArmyGUID {0} not found in AIDataRepository", new object[]
            {
                targetArmy.GUID
            });
            return(State.Failure);
        }
        bool flag = targetArmy.IsLocked;

        if (!flag)
        {
            IEncounterRepositoryService service2 = Services.GetService <IGameService>().Game.Services.GetService <IEncounterRepositoryService>();
            if (service2 != null)
            {
                IEnumerable <Encounter> enumerable = service2;
                if (enumerable != null)
                {
                    flag = enumerable.Any((Encounter encounter) => encounter.IsGarrisonInEncounter(targetArmy.GUID, false));
                }
            }
        }
        if (this.Inverted)
        {
            if (!flag)
            {
                return(State.Success);
            }
            aiBehaviorTree.ErrorCode = 17;
            return(State.Failure);
        }
        else
        {
            if (flag)
            {
                return(State.Success);
            }
            aiBehaviorTree.ErrorCode = 16;
            return(State.Failure);
        }
    }
示例#6
0
    public bool AssignCommanderMission(AICommanderMission commanderMissionOwner)
    {
        if (this.CommanderMission != null)
        {
            Diagnostics.LogWarning(string.Format("LOCKING: {0} tries to recruit Army assigned to {1}", commanderMissionOwner.ToString(), this.CommanderMission.ToString()));
            return(false);
        }
        this.CommanderMission = commanderMissionOwner;
        if (this.ArmyMission != null)
        {
            this.UnassignArmyMission();
        }
        IAIDataRepositoryAIHelper service = AIScheduler.Services.GetService <IAIDataRepositoryAIHelper>();

        if (this.Army != null)
        {
            for (int i = 0; i < this.Army.StandardUnits.Count; i++)
            {
                AIData_Unit aidata_Unit;
                if (service.TryGetAIData <AIData_Unit>(this.Army.StandardUnits[i].GUID, out aidata_Unit))
                {
                    if (aidata_Unit.IsUnitLocked())
                    {
                        if (!aidata_Unit.IsUnitLockedByMe(commanderMissionOwner.InternalGUID) && Amplitude.Unity.Framework.Application.Preferences.EnableModdingTools)
                        {
                            Diagnostics.LogWarning(string.Format("{3} {4} LOCKING: Error when assigning commander mission {0} Guid {1}, current infroamtion {2}", new object[]
                            {
                                commanderMissionOwner.GetType().ToString(),
                                commanderMissionOwner.InternalGUID,
                                aidata_Unit.GetLockingStateString(),
                                this.Army.Empire,
                                this.Army.LocalizedName
                            }));
                        }
                        aidata_Unit.ClearLock();
                    }
                    aidata_Unit.TryLockUnit(commanderMissionOwner.InternalGUID, commanderMissionOwner.GetType().ToString(), AIData_Unit.AIDataReservationExtraTag.GenericCommander, commanderMissionOwner.Commander.GetPriority(commanderMissionOwner));
                }
            }
        }
        return(true);
    }
示例#7
0
 public void UnassignCommanderMission()
 {
     if (this.CommanderMission != null)
     {
         IAIDataRepositoryAIHelper service = AIScheduler.Services.GetService <IAIDataRepositoryAIHelper>();
         if (this.Army != null)
         {
             for (int i = 0; i < this.Army.StandardUnits.Count; i++)
             {
                 AIData_Unit aidata_Unit;
                 if (service.TryGetAIData <AIData_Unit>(this.Army.StandardUnits[i].GUID, out aidata_Unit) && aidata_Unit.IsUnitLockedByMe(this.CommanderMission.InternalGUID))
                 {
                     aidata_Unit.TryUnLockUnit(this.CommanderMission.InternalGUID);
                 }
             }
         }
     }
     this.CommanderMission = null;
     this.UnassignArmyMission();
 }
    protected override State Execute(AIBehaviorTree aiBehaviorTree, params object[] parameters)
    {
        DepartmentOfScience agency = aiBehaviorTree.AICommander.Empire.GetAgency <DepartmentOfScience>();

        if (agency != null && !agency.CanPillage())
        {
            return(State.Failure);
        }
        if (!this.downloadableContentService.IsShared(DownloadableContent11.ReadOnlyName))
        {
            return(State.Failure);
        }
        Army army;

        if (base.GetArmyUnlessLocked(aiBehaviorTree, "$Army", out army) != AIArmyMission.AIArmyMissionErrorCode.None)
        {
            return(State.Failure);
        }
        if (!aiBehaviorTree.Variables.ContainsKey(this.TargetListVarName))
        {
            return(State.Failure);
        }
        if (!string.IsNullOrEmpty(this.OpportunityMaximumTurnName) && aiBehaviorTree.Variables.ContainsKey(this.OpportunityMaximumTurnName))
        {
            this.OpportunityMaximumTurn = (float)aiBehaviorTree.Variables[this.OpportunityMaximumTurnName];
        }
        List <IWorldPositionable> list = aiBehaviorTree.Variables[this.TargetListVarName] as List <IWorldPositionable>;

        if (list == null)
        {
            aiBehaviorTree.ErrorCode = 10;
            return(State.Failure);
        }
        if (list.Count == 0)
        {
            aiBehaviorTree.ErrorCode = 10;
            return(State.Failure);
        }
        AIEntity_Empire entity          = aiBehaviorTree.AICommander.AIPlayer.GetEntity <AIEntity_Empire>();
        float           num             = 0.1f;
        float           propertyValue   = army.GetPropertyValue(SimulationProperties.MaximumMovement);
        PointOfInterest pointOfInterest = null;

        for (int i = 0; i < list.Count; i++)
        {
            PointOfInterest pointOfInterest2 = list[i] as PointOfInterest;
            if (pointOfInterest2 != null && pointOfInterest2.Region.City != null && pointOfInterest2.PointOfInterestImprovement != null && DepartmentOfDefense.CanStartPillage(army, pointOfInterest2, false))
            {
                float num2 = 0.5f;
                if (entity != null)
                {
                    num2 = this.ComputePillageInterest(pointOfInterest2, entity, army) * 0.5f;
                }
                if (num2 >= 0f)
                {
                    float num3 = (float)this.worldPositionningService.GetDistance(army.WorldPosition, pointOfInterest2.WorldPosition) / propertyValue;
                    if (num3 <= this.MaximumTurnDistance)
                    {
                        float num4 = 0.5f - num3 / this.MaximumTurnDistance;
                        num2 = AILayer.Boost(num2, num4 * 0.5f);
                        float propertyValue2 = pointOfInterest2.GetPropertyValue(SimulationProperties.PillageDefense);
                        float propertyValue3 = pointOfInterest2.GetPropertyValue(SimulationProperties.MaximumPillageDefense);
                        num2 = AILayer.Boost(num2, (1f - propertyValue2 / propertyValue3) * 0.2f);
                        if (num2 > num)
                        {
                            num             = num2;
                            pointOfInterest = pointOfInterest2;
                        }
                    }
                }
            }
        }
        if (pointOfInterest != null)
        {
            if (this.OpportunityMaximumTurn > 0f)
            {
                int num5 = 0;
                int num6 = 0;
                if (aiBehaviorTree.Variables.ContainsKey(this.OpportunityMainTargetPosition))
                {
                    WorldPosition mainTargetPosition = (WorldPosition)aiBehaviorTree.Variables[this.OpportunityMainTargetPosition];
                    if (!AIBehaviorTreeNode_Decorator_EvaluateOpportunity.IsDetourWorthCheckingFast(this.worldPositionningService, army, pointOfInterest.WorldPosition, mainTargetPosition, out num6, out num5))
                    {
                        return(State.Failure);
                    }
                }
                int remainingTurnToPillage        = DepartmentOfDefense.GetRemainingTurnToPillage(army, pointOfInterest);
                IAIDataRepositoryAIHelper service = AIScheduler.Services.GetService <IAIDataRepositoryAIHelper>();
                Diagnostics.Assert(service != null);
                float       num7 = 1f;
                AIData_Army aidata_Army;
                if (service.TryGetAIData <AIData_Army>(army.GUID, out aidata_Army))
                {
                    num7 = aiBehaviorTree.AICommander.GetPillageModifier(aidata_Army.CommanderMission);
                }
                if ((float)(num5 - num6 + remainingTurnToPillage) > this.OpportunityMaximumTurn * num7)
                {
                    return(State.Failure);
                }
            }
            if (aiBehaviorTree.Variables.ContainsKey(this.Output_TargetVarName))
            {
                aiBehaviorTree.Variables[this.Output_TargetVarName] = pointOfInterest;
            }
            else
            {
                aiBehaviorTree.Variables.Add(this.Output_TargetVarName, pointOfInterest);
            }
            return(State.Success);
        }
        return(State.Failure);
    }
    private void RegroupSmallFreeArmies()
    {
        List <Army> list = new List <Army>();
        IAIDataRepositoryAIHelper service = AIScheduler.Services.GetService <IAIDataRepositoryAIHelper>();

        this.smallEntitiesToRegroup.Clear();
        this.tempEntitiesToRegroup.Clear();
        int num = (int)this.Empire.GetPropertyValue(SimulationProperties.ArmyUnitSlot);
        DepartmentOfDefense agency = this.Empire.GetAgency <DepartmentOfDefense>();

        for (int i = 0; i < agency.Armies.Count; i++)
        {
            Army        army   = agency.Armies[i];
            AIData_Army aidata = service.GetAIData <AIData_Army>(army.GUID);
            if (aidata != null && !aidata.IsSolitary && !aidata.Army.IsSeafaring && !aidata.Army.HasCatspaw && !(aidata.Army is KaijuArmy))
            {
                if (aidata.CommanderMission != null)
                {
                    if (aidata.CommanderMission.Commander == null)
                    {
                        AILayer.LogError("[AILayer_ArmyRecruitment] Commander Mission without a commander");
                        goto IL_131;
                    }
                    if (!AILayer_ArmyRecruitment.IsCommanderMissionNotInteresting(aidata.CommanderMission.Commander.Category))
                    {
                        goto IL_131;
                    }
                }
                else if (!aidata.IsTaggedFreeForExploration())
                {
                    goto IL_131;
                }
                if (army.StandardUnits.Count < num)
                {
                    if (!this.IsMercArmy(aidata.Army))
                    {
                        this.smallEntitiesToRegroup.Add(aidata);
                    }
                    else
                    {
                        list.Add(aidata.Army);
                    }
                }
            }
            IL_131 :;
        }
        if (this.Empire is MajorEmpire)
        {
            DepartmentOfTheInterior agency2 = this.Empire.GetAgency <DepartmentOfTheInterior>();
            if (agency2 != null)
            {
                for (int j = 0; j < agency2.Cities.Count; j++)
                {
                    City garrison = agency2.Cities[j];
                    this._Internal_RegroupSmallFreeArmies(garrison);
                }
            }
            List <Village> convertedVillages = ((MajorEmpire)this.Empire).ConvertedVillages;
            for (int k = 0; k < convertedVillages.Count; k++)
            {
                Village village = convertedVillages[k];
                if (village != null)
                {
                    this._Internal_RegroupSmallFreeArmies(village);
                }
            }
        }
        while (this.smallEntitiesToRegroup.Count > 1)
        {
            AIData_GameEntity aidata_GameEntity = this.smallEntitiesToRegroup[0];
            this.smallEntitiesToRegroup.RemoveAt(0);
            int num2;
            if (aidata_GameEntity is AIData_Army)
            {
                num2 = (aidata_GameEntity as AIData_Army).Army.StandardUnits.Count;
            }
            else
            {
                if (!(aidata_GameEntity is AIData_Unit))
                {
                    continue;
                }
                num2 = 1;
            }
            this.tempEntitiesToRegroup.Clear();
            this.tempEntitiesToRegroup.Add(aidata_GameEntity);
            int num3 = this.smallEntitiesToRegroup.Count - 1;
            while (num3 >= 0 && num2 < num)
            {
                AIData_GameEntity item = this.smallEntitiesToRegroup[num3];
                int num4;
                if (aidata_GameEntity is AIData_Army)
                {
                    num4 = (aidata_GameEntity as AIData_Army).Army.StandardUnits.Count;
                    goto IL_29E;
                }
                if (aidata_GameEntity is AIData_Unit)
                {
                    num4 = 1;
                    goto IL_29E;
                }
IL_296:
                num3--;
                continue;
IL_29E:
                if (num2 + num4 <= num)
                {
                    num2 += num4;
                    this.tempEntitiesToRegroup.Add(item);
                    this.smallEntitiesToRegroup.RemoveAt(num3);
                    goto IL_296;
                }
                goto IL_296;
            }
            if (this.tempEntitiesToRegroup.Count > 1)
            {
                this.unitsGUIDS.Clear();
                for (int l = 0; l < this.tempEntitiesToRegroup.Count; l++)
                {
                    AIData_GameEntity aidata_GameEntity2 = this.tempEntitiesToRegroup[l];
                    if (aidata_GameEntity2 is AIData_Army)
                    {
                        AIData_Army aidata_Army = aidata_GameEntity2 as AIData_Army;
                        if (aidata_Army.CommanderMission != null)
                        {
                            aidata_Army.CommanderMission.Interrupt();
                        }
                        for (int m = 0; m < aidata_Army.Army.StandardUnits.Count; m++)
                        {
                            AIData_Unit aidata_Unit;
                            if (service.TryGetAIData <AIData_Unit>(aidata_Army.Army.StandardUnits[m].GUID, out aidata_Unit))
                            {
                                aidata_Unit.ReservationExtraTag = AIData_Unit.AIDataReservationExtraTag.None;
                                this.unitsGUIDS.Add(aidata_Unit.Unit.GUID);
                            }
                        }
                    }
                    else if (aidata_GameEntity2 is AIData_Unit)
                    {
                        AIData_Unit aidata_Unit2 = aidata_GameEntity2 as AIData_Unit;
                        aidata_Unit2.ReservationExtraTag = AIData_Unit.AIDataReservationExtraTag.None;
                        this.unitsGUIDS.Add(aidata_Unit2.Unit.GUID);
                    }
                }
                if (this.unitsGUIDS.Count != 0)
                {
                    this.CreateNewCommanderRegroup(this.unitsGUIDS.ToArray(), null);
                }
            }
        }
        if (list.Count > 1)
        {
            int num5 = 0;
            List <GameEntityGUID> list2 = new List <GameEntityGUID>();
            for (int n = 0; n < list.Count; n++)
            {
                num5++;
                foreach (Unit unit in list[n].StandardUnits)
                {
                    AIData_Unit aidata_Unit3;
                    if (service.TryGetAIData <AIData_Unit>(unit.GUID, out aidata_Unit3))
                    {
                        aidata_Unit3.ReservationExtraTag = AIData_Unit.AIDataReservationExtraTag.None;
                        list2.Add(aidata_Unit3.Unit.GUID);
                        if (list2.Count >= num)
                        {
                            this.CreateNewCommanderRegroup(list2.ToArray(), null);
                            list2.Clear();
                            num5 = 0;
                            break;
                        }
                    }
                }
            }
            if (num5 > 1)
            {
                this.CreateNewCommanderRegroup(list2.ToArray(), null);
            }
        }
    }
    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;
        }
    }