private void OrderBribeVillage_TicketRaised(object sender, TicketRaisedEventArgs e)
    {
        AICommanderWithObjective commanderObjective = this.aiBehaviorTree.AICommander as AICommanderWithObjective;

        if (commanderObjective != null)
        {
            EvaluableMessage_VillageAction evaluableMessage_VillageAction = this.aiBehaviorTree.AICommander.AIPlayer.Blackboard.FindFirst <EvaluableMessage_VillageAction>(BlackboardLayerID.Empire, (EvaluableMessage_VillageAction match) => match.RegionIndex == commanderObjective.RegionIndex && match.VillageGUID == commanderObjective.SubObjectiveGuid && match.AccountTag == AILayer_AccountManager.MilitaryAccountName);
            if (evaluableMessage_VillageAction == null)
            {
                if (this.ticket.PostOrderResponse != PostOrderResponse.Processed)
                {
                    evaluableMessage_VillageAction.SetFailedToObtain();
                    this.aiBehaviorTree.ErrorCode = 31;
                }
                else
                {
                    evaluableMessage_VillageAction.SetObtained();
                }
            }
        }
        this.ticket = null;
    }
    protected override State Execute(AIBehaviorTree aiBehaviorTree, params object[] parameters)
    {
        global::Empire empire = aiBehaviorTree.AICommander.Empire;

        if (empire == null || !(empire is MajorEmpire))
        {
            aiBehaviorTree.ErrorCode = 10;
            return(State.Failure);
        }
        Army army;

        if (base.GetArmyUnlessLocked(aiBehaviorTree, "$Army", out army) > AIArmyMission.AIArmyMissionErrorCode.None)
        {
            return(State.Failure);
        }
        GameServer             gameServer             = (Services.GetService <ISessionService>().Session as global::Session).GameServer as GameServer;
        AILayer_ArmyManagement ailayer_ArmyManagement = null;
        AIPlayer_MajorEmpire   aiplayer_MajorEmpire;

        if (gameServer.AIScheduler != null && gameServer.AIScheduler.TryGetMajorEmpireAIPlayer(empire as MajorEmpire, out aiplayer_MajorEmpire))
        {
            AIEntity entity = aiplayer_MajorEmpire.GetEntity <AIEntity_Empire>();
            if (entity != null)
            {
                ailayer_ArmyManagement = entity.GetLayer <AILayer_ArmyManagement>();
            }
        }
        if (ailayer_ArmyManagement == null)
        {
            return(State.Failure);
        }
        int         num         = int.MaxValue;
        Army        value       = null;
        AICommander aicommander = null;

        foreach (AICommander aicommander2 in ailayer_ArmyManagement.AICommanders)
        {
            if (aicommander2 is AICommander_WarWithObjective || aicommander2 is AICommander_Victory)
            {
                foreach (AICommanderMission aicommanderMission in aicommander2.Missions)
                {
                    IGameEntity gameEntity = null;
                    if (aicommanderMission.AIDataArmyGUID.IsValid && this.gameEntityRepositoryService.TryGetValue(aicommanderMission.AIDataArmyGUID, out gameEntity) && gameEntity is Army)
                    {
                        Army army2    = gameEntity as Army;
                        int  distance = this.worldPositionningService.GetDistance(army.WorldPosition, army2.WorldPosition);
                        if (distance < num)
                        {
                            num         = distance;
                            value       = army2;
                            aicommander = aicommander2;
                        }
                    }
                }
            }
        }
        if ((float)num > Mathf.Max(army.GetPropertyValue(SimulationProperties.MaximumMovement) * 1.5f, 6f))
        {
            return(State.Failure);
        }
        if (aicommander != null && aicommander is AICommanderWithObjective)
        {
            AICommanderWithObjective aicommanderWithObjective = aicommander as AICommanderWithObjective;
            IWorldPositionable       worldPositionable        = null;
            if (aicommanderWithObjective.SubObjectiveGuid.IsValid)
            {
                IGameEntity gameEntity2 = null;
                if (this.gameEntityRepositoryService.TryGetValue(aicommanderWithObjective.SubObjectiveGuid, out gameEntity2) && gameEntity2 is IWorldPositionable)
                {
                    worldPositionable = (gameEntity2 as IWorldPositionable);
                }
            }
            Region region = this.worldPositionningService.GetRegion(aicommanderWithObjective.RegionIndex);
            if (region.City != null)
            {
                worldPositionable = region.City;
            }
            if (worldPositionable == null)
            {
                if (aiBehaviorTree.Variables.ContainsKey(this.Output_MainTargetVarName))
                {
                    aiBehaviorTree.Variables.Remove(this.Output_MainTargetVarName);
                }
                return(State.Failure);
            }
            if (aiBehaviorTree.Variables.ContainsKey(this.Output_MainTargetVarName))
            {
                aiBehaviorTree.Variables[this.Output_MainTargetVarName] = worldPositionable;
            }
            else
            {
                aiBehaviorTree.Variables.Add(this.Output_MainTargetVarName, worldPositionable);
            }
        }
        if (aiBehaviorTree.Variables.ContainsKey(this.Output_TargetVarName))
        {
            aiBehaviorTree.Variables[this.Output_TargetVarName] = value;
        }
        else
        {
            aiBehaviorTree.Variables.Add(this.Output_TargetVarName, value);
        }
        return(State.Success);
    }
    protected override State Execute(AIBehaviorTree aiBehaviorTree, params object[] parameters)
    {
        this.aiBehaviorTree = aiBehaviorTree;
        if (this.ticket != null)
        {
            return(State.Running);
        }
        Army army;

        if (base.GetArmyUnlessLocked(aiBehaviorTree, "$Army", out army) != AIArmyMission.AIArmyMissionErrorCode.None)
        {
            return(State.Failure);
        }
        if (!aiBehaviorTree.Variables.ContainsKey(this.TargetVarName))
        {
            aiBehaviorTree.LogError("${0} not set", new object[]
            {
                this.TargetVarName
            });
            return(State.Failure);
        }
        IGameEntity target = aiBehaviorTree.Variables[this.TargetVarName] as IGameEntity;

        if (!(target is IWorldPositionable))
        {
            return(State.Failure);
        }
        IGameService service = Services.GetService <IGameService>();

        Diagnostics.Assert(service != null);
        if (!service.Game.Services.GetService <IGameEntityRepositoryService>().Contains(target.GUID))
        {
            return(State.Success);
        }
        AICommanderWithObjective commanderObjective = aiBehaviorTree.AICommander as AICommanderWithObjective;

        if (commanderObjective == null)
        {
            return(State.Failure);
        }
        EvaluableMessage_VillageAction evaluableMessage_VillageAction = aiBehaviorTree.AICommander.AIPlayer.Blackboard.FindFirst <EvaluableMessage_VillageAction>(BlackboardLayerID.Empire, (EvaluableMessage_VillageAction match) => match.RegionIndex == commanderObjective.RegionIndex && match.VillageGUID == target.GUID && match.AccountTag == AILayer_AccountManager.MilitaryAccountName);

        if (evaluableMessage_VillageAction == null || evaluableMessage_VillageAction.EvaluationState == EvaluableMessage.EvaluableMessageState.Cancel)
        {
            this.aiBehaviorTree.ErrorCode = 31;
            return(State.Failure);
        }
        if (evaluableMessage_VillageAction.EvaluationState == EvaluableMessage.EvaluableMessageState.Obtained)
        {
            return(State.Success);
        }
        if (evaluableMessage_VillageAction.ChosenBuyEvaluation == null || evaluableMessage_VillageAction.EvaluationState == EvaluableMessage.EvaluableMessageState.Cancel)
        {
            return(State.Failure);
        }
        IEncounterRepositoryService service2 = service.Game.Services.GetService <IEncounterRepositoryService>();

        if (service2 != null)
        {
            IEnumerable <Encounter> enumerable = service2;
            if (enumerable != null && enumerable.Any((Encounter encounter) => encounter.IsGarrisonInEncounter(army.GUID, false) || encounter.IsGarrisonInEncounter(target.GUID, false)))
            {
                return(State.Running);
            }
        }
        Village village = target as Village;

        if (village == null)
        {
            aiBehaviorTree.ErrorCode = 2;
            return(State.Failure);
        }
        Diagnostics.Assert(AIScheduler.Services != null);
        if (service.Game.Services.GetService <IWorldPositionningService>().GetDistance(army.WorldPosition, (target as IWorldPositionable).WorldPosition) != 1)
        {
            aiBehaviorTree.ErrorCode = 12;
            return(State.Failure);
        }
        if (village.HasBeenPacified || village.HasBeenConverted || village.HasBeenInfected)
        {
            return(State.Failure);
        }
        OrderBribeVillage order = new OrderBribeVillage(army.Empire.Index, army.GUID, (target as IWorldPositionable).WorldPosition, ArmyAction_Bribe.ReadOnlyName);

        aiBehaviorTree.AICommander.Empire.PlayerControllers.AI.PostOrder(order, out this.ticket, new EventHandler <TicketRaisedEventArgs>(this.OrderBribeVillage_TicketRaised));
        return(State.Running);
    }
Esempio n. 4
0
    protected override void RefreshObjectives(StaticString context, StaticString pass)
    {
        base.RefreshObjectives(context, pass);
        base.GatherObjectives(AICommanderMissionDefinition.AICommanderCategory.Defense.ToString(), ref this.globalObjectiveMessages);
        base.ValidateMessages(ref this.globalObjectiveMessages);
        AILayer_War layer = base.AIEntity.GetLayer <AILayer_War>();

        base.GlobalPriority.Reset();
        AILayer_Strategy layer2 = base.AIEntity.GetLayer <AILayer_Strategy>();

        base.GlobalPriority.Add(layer2.StrategicNetwork.GetAgentValue("InternalMilitary"), "Strategic Network 'InternalMilitary'", new object[0]);
        AILayer_ArmyManagement layer3 = base.AIEntity.GetLayer <AILayer_ArmyManagement>();
        float worldColonizationRatio  = this.worldAtlasHelper.GetWorldColonizationRatio(base.AIEntity.Empire);
        bool  flag     = layer.WantWarWithSomoeone() || layer.NumberOfWar > 0;
        City  mainCity = this.departmentOfTheInterior.MainCity;
        bool  flag2    = false;

        if (this.departmentOfTheInterior.NonInfectedCities.Count < 4)
        {
            List <IGarrison> list = new List <IGarrison>();
            list.AddRange(this.departmentOfDefense.Armies.ToList <Army>().FindAll((Army match) => !match.IsSeafaring && !match.IsSettler && match.UnitsCount > 3).Cast <IGarrison>());
            if (list.Count > 1)
            {
                flag2 = true;
            }
        }
        for (int i = 0; i < this.departmentOfTheInterior.Cities.Count; i++)
        {
            City city = this.departmentOfTheInterior.Cities[i];
            if (this.IsObjectiveValid(AICommanderMissionDefinition.AICommanderCategory.Defense.ToString(), city.Region.Index, true))
            {
                GlobalObjectiveMessage globalObjectiveMessage = this.globalObjectiveMessages.Find((GlobalObjectiveMessage match) => match.RegionIndex == city.Region.Index);
                if (globalObjectiveMessage == null)
                {
                    globalObjectiveMessage = base.GenerateObjective(city.Region.Index);
                    globalObjectiveMessage.LocalPriority = new HeuristicValue(0f);
                    this.globalObjectiveMessages.Add(globalObjectiveMessage);
                }
                globalObjectiveMessage.TimeOut = 1;
                globalObjectiveMessage.LocalPriority.Reset();
                if (flag2 && city == mainCity)
                {
                    bool flag3 = !AILayer_Military.AreaIsSave(city.WorldPosition, 15, this.departmentOfForeignAffairs, false, false);
                    if (!flag3)
                    {
                        foreach (Region region in this.worldPositionningService.GetNeighbourRegions(city.Region, false, false))
                        {
                            if (region.IsLand && region.Owner is MajorEmpire)
                            {
                                DiplomaticRelation diplomaticRelation = this.departmentOfForeignAffairs.GetDiplomaticRelation(region.Owner);
                                if (diplomaticRelation.State.Name == DiplomaticRelationState.Names.War || diplomaticRelation.State.Name == DiplomaticRelationState.Names.ColdWar || diplomaticRelation.State.Name == DiplomaticRelationState.Names.Truce)
                                {
                                    flag3 = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (flag3)
                    {
                        globalObjectiveMessage.LocalPriority  = new HeuristicValue(1f);
                        globalObjectiveMessage.GlobalPriority = new HeuristicValue(1f);
                    }
                }
                else
                {
                    globalObjectiveMessage.GlobalPriority = base.GlobalPriority;
                    AICommanderWithObjective aicommanderWithObjective = layer3.FindCommander(globalObjectiveMessage);
                    if (aicommanderWithObjective != null && aicommanderWithObjective is AICommander_Defense)
                    {
                        AICommander_Defense aicommander_Defense = aicommanderWithObjective as AICommander_Defense;
                        globalObjectiveMessage.LocalPriority.Add(AILayer_Military.GetCityDefenseLocalPriority(city, this.unitRatioBoost, aicommander_Defense.ComputeCurrentUnitInDefense()), "CityDefenseLocalPriority", new object[0]);
                    }
                    else
                    {
                        globalObjectiveMessage.LocalPriority.Add(AILayer_Military.GetCityDefenseLocalPriority(city, this.unitRatioBoost, -1), "CityDefenseLocalPriority", new object[0]);
                    }
                    HeuristicValue heuristicValue = new HeuristicValue(0f);
                    heuristicValue.Add(worldColonizationRatio, "colonization ratio", new object[0]);
                    heuristicValue.Multiply(0.2f, "(constant)", new object[0]);
                    globalObjectiveMessage.LocalPriority.Boost(heuristicValue, "Colonization boost", new object[0]);
                    if (flag)
                    {
                        globalObjectiveMessage.LocalPriority.Boost(0.3f, "Want war", new object[0]);
                    }
                    AIData_City aidata_City;
                    if (this.aiDataRepositoryAIHelper.TryGetAIData <AIData_City>(city.GUID, out aidata_City) && aidata_City.IsAtWarBorder)
                    {
                        globalObjectiveMessage.LocalPriority.Boost(0.3f, "War border", new object[0]);
                    }
                    if ((float)this.endTurnService.Turn < this.unitInGarrisonTurnLimit)
                    {
                        globalObjectiveMessage.LocalPriority.Boost(-0.3f, "turn under 'unitInGarrisonTurnLimit' ({0})", new object[]
                        {
                            this.unitInGarrisonTurnLimit
                        });
                    }
                }
            }
        }
        MajorEmpire majorEmpire = base.AIEntity.Empire as MajorEmpire;

        if (majorEmpire == null || majorEmpire.ConvertedVillages.Count == 0)
        {
            return;
        }
        if (mainCity == null)
        {
            return;
        }
        float num = AILayer_Military.GetCityDefenseLocalPriority(mainCity, this.unitRatioBoost, AICommanderMission_Garrison.SimulatedUnitsCount);

        num *= this.villageDefenseRatioDeboost;
        num *= base.GlobalPriority;
        for (int k = 0; k < this.VillageDOFPriority.Count; k++)
        {
            this.VillageDOFPriority[k].Reset();
        }
        float num2 = 0f;

        for (int l = 0; l < majorEmpire.ConvertedVillages.Count; l++)
        {
            Village village = majorEmpire.ConvertedVillages[l];
            AILayer_Military.VillageDefensePriority villageDefensePriority = this.VillageDOFPriority.Find((AILayer_Military.VillageDefensePriority match) => match.Village.GUID == village.GUID);
            if (villageDefensePriority == null)
            {
                villageDefensePriority = new AILayer_Military.VillageDefensePriority();
                villageDefensePriority.Reset();
                villageDefensePriority.Village = village;
                this.VillageDOFPriority.Add(villageDefensePriority);
            }
            villageDefensePriority.ToDelete          = false;
            villageDefensePriority.FirstUnitPriority = num;
            float num3 = (float)this.worldPositionningService.GetDistance(village.WorldPosition, mainCity.WorldPosition);
            villageDefensePriority.DistanceToMainCity = num3;
            if (num3 > num2)
            {
                num2 = num3;
            }
        }
        for (int m = this.VillageDOFPriority.Count - 1; m >= 0; m--)
        {
            AILayer_Military.VillageDefensePriority villageDefensePriority2 = this.VillageDOFPriority[m];
            if (villageDefensePriority2.ToDelete)
            {
                this.VillageDOFPriority.Remove(villageDefensePriority2);
            }
            else
            {
                float num4 = villageDefensePriority2.DistanceToMainCity / num2;
                if (majorEmpire.ConvertedVillages.Count > 1)
                {
                    villageDefensePriority2.FirstUnitPriority = AILayer.Boost(villageDefensePriority2.FirstUnitPriority, num4 * -0.1f);
                }
            }
        }
    }
Esempio n. 5
0
    protected override State Execute(AIBehaviorTree aiBehaviorTree, params object[] parameters)
    {
        this.aiBehaviorTree = aiBehaviorTree;
        State result;

        if (this.ticket != null)
        {
            result = State.Running;
        }
        else
        {
            Army army;
            if (base.GetArmyUnlessLocked(aiBehaviorTree, "$Army", out army) > AIArmyMission.AIArmyMissionErrorCode.None)
            {
                result = State.Failure;
            }
            else if (!aiBehaviorTree.Variables.ContainsKey(this.TargetVarName))
            {
                aiBehaviorTree.LogError("${0} not set", new object[]
                {
                    this.TargetVarName
                });
                result = State.Failure;
            }
            else
            {
                IGameEntity target = aiBehaviorTree.Variables[this.TargetVarName] as IGameEntity;
                if (!(target is IWorldPositionable))
                {
                    result = State.Failure;
                }
                else
                {
                    IGameService service = Services.GetService <IGameService>();
                    Diagnostics.Assert(service != null);
                    if (!service.Game.Services.GetService <IGameEntityRepositoryService>().Contains(target.GUID))
                    {
                        result = State.Success;
                    }
                    else
                    {
                        AICommanderWithObjective commanderObjective = aiBehaviorTree.AICommander as AICommanderWithObjective;
                        if (commanderObjective == null)
                        {
                            result = State.Failure;
                        }
                        else if (!(target is Village))
                        {
                            aiBehaviorTree.ErrorCode = 2;
                            result = State.Failure;
                        }
                        else
                        {
                            Village village = target as Village;
                            if (village.HasBeenConverted)
                            {
                                if (village.HasBeenConvertedByIndex == aiBehaviorTree.AICommander.Empire.Index)
                                {
                                    return(State.Success);
                                }
                                this.aiBehaviorTree.ErrorCode = 32;
                                return(State.Failure);
                            }
                            else
                            {
                                EvaluableMessage_VillageAction evaluableMessage_VillageAction = aiBehaviorTree.AICommander.AIPlayer.Blackboard.FindFirst <EvaluableMessage_VillageAction>(BlackboardLayerID.Empire, (EvaluableMessage_VillageAction match) => match.RegionIndex == commanderObjective.RegionIndex && match.VillageGUID == target.GUID && match.AccountTag == AILayer_AccountManager.ConversionAccountName);
                                if (evaluableMessage_VillageAction == null || evaluableMessage_VillageAction.EvaluationState == EvaluableMessage.EvaluableMessageState.Obtained)
                                {
                                    float num;
                                    army.Empire.GetAgency <DepartmentOfTheTreasury>().TryGetResourceStockValue(army.Empire.SimulationObject, DepartmentOfTheTreasury.Resources.EmpirePoint, out num, false);
                                    if (AILayer_Village.GetVillageConversionCost(army.Empire as MajorEmpire, village) > num)
                                    {
                                        if (evaluableMessage_VillageAction == null)
                                        {
                                            this.aiBehaviorTree.ErrorCode = 32;
                                            return(State.Failure);
                                        }
                                        return(State.Success);
                                    }
                                }
                                else
                                {
                                    if (evaluableMessage_VillageAction.EvaluationState == EvaluableMessage.EvaluableMessageState.Cancel)
                                    {
                                        this.aiBehaviorTree.ErrorCode = 32;
                                        return(State.Failure);
                                    }
                                    if (evaluableMessage_VillageAction.ChosenBuyEvaluation == null || evaluableMessage_VillageAction.ChosenBuyEvaluation.State != BuyEvaluation.EvaluationState.Purchased || evaluableMessage_VillageAction.EvaluationState != EvaluableMessage.EvaluableMessageState.Validate)
                                    {
                                        return(State.Failure);
                                    }
                                }
                                IEncounterRepositoryService service2 = service.Game.Services.GetService <IEncounterRepositoryService>();
                                if (service2 != null)
                                {
                                    IEnumerable <Encounter> enumerable = service2;
                                    if (enumerable != null && enumerable.Any((Encounter encounter) => encounter.IsGarrisonInEncounter(army.GUID, false) || encounter.IsGarrisonInEncounter(target.GUID, false)))
                                    {
                                        return(State.Running);
                                    }
                                }
                                Diagnostics.Assert(AIScheduler.Services != null);
                                if (service.Game.Services.GetService <IWorldPositionningService>().GetDistance(army.WorldPosition, village.WorldPosition) != 1)
                                {
                                    aiBehaviorTree.ErrorCode = 12;
                                    result = State.Failure;
                                }
                                else
                                {
                                    OrderConvertVillage order = new OrderConvertVillage(army.Empire.Index, army.GUID, village.WorldPosition);
                                    aiBehaviorTree.AICommander.Empire.PlayerControllers.AI.PostOrder(order, out this.ticket, new EventHandler <TicketRaisedEventArgs>(this.OrderConvertVillage_TicketRaised));
                                    result = State.Running;
                                }
                            }
                        }
                    }
                }
            }
        }
        return(result);
    }