public static InterpreterContext.InterpreterSession CreateSession(SimulationObjectWrapper simObject, IEnumerable <QuestVariable> variables)
 {
     InterpreterContext.InterpreterSession result = new InterpreterContext.InterpreterSession(simObject);
     if (variables != null)
     {
         foreach (QuestVariable questVariable in variables)
         {
             if (questVariable.Object is float)
             {
                 result.Context.Register(questVariable.Name, (float)Convert.ChangeType(questVariable.Object, typeof(float)));
             }
             else if (questVariable.Object is IList)
             {
                 IList list = (IList)questVariable.Object;
                 if (list.Count == 1)
                 {
                     object obj = list[0];
                     if (obj is float)
                     {
                         result.Context.Register(questVariable.Name, (float)Convert.ChangeType(obj, typeof(float)));
                     }
                 }
             }
         }
     }
     return(result);
 }
        public override bool Check(SimulationObject simulationObject)
        {
            if (simulationObject == null)
            {
                throw new ArgumentNullException("simulationObject");
            }
            bool result;

            using (InterpreterContext.InterpreterSession interpreterSession = new InterpreterContext.InterpreterSession(simulationObject))
            {
                result = this.Check(interpreterSession.Context);
            }
            return(result);
        }
示例#3
0
    private bool CheckPrerequisites(QuestBehaviour questBehaviour, Dictionary <StaticString, IEnumerable <SimulationObjectWrapper> > targets)
    {
        if (this.Prerequisites == null || this.Prerequisites.Length == 0)
        {
            return(true);
        }
        if (this.Prerequisites.Length > 1)
        {
            Diagnostics.LogWarning("TODO: FIX THIS CODE TO SUPPORT MORE THAN 1 PREREQUISITE OF TYPE QUESTBEHAVIOURPREREQUISITES. Concerned quest is '{0}'.", new object[]
            {
                questBehaviour.Quest.Name
            });
        }
        if (global::GameManager.Preferences.QuestVerboseMode)
        {
            Diagnostics.Log("ELCP: {0} quest {1} begins prereq check", new object[]
            {
                questBehaviour.Initiator,
                questBehaviour.Quest.Name
            });
        }
        int i = 0;

        while (i < this.Prerequisites.Length)
        {
            QuestBehaviourPrerequisites           questBehaviourPrerequisites = this.Prerequisites[i];
            IEnumerable <SimulationObjectWrapper> enumerable = null;
            if (questBehaviourPrerequisites.Target == null)
            {
                enumerable = new SimulationObjectWrapper[1];
            }
            else
            {
                SimulationObjectWrapper targetSimulationObjectWrapper = questBehaviourPrerequisites.GetTargetSimulationObjectWrapper(questBehaviour);
                if (targetSimulationObjectWrapper != null && targetSimulationObjectWrapper.SimulationObject != null)
                {
                    enumerable = new SimulationObjectWrapper[]
                    {
                        targetSimulationObjectWrapper
                    };
                }
                else
                {
                    targets.TryGetValue(questBehaviourPrerequisites.Target, out enumerable);
                }
            }
            if (enumerable != null)
            {
                bool flag = false;
                foreach (SimulationObjectWrapper simulationObjectWrapper in enumerable)
                {
                    if (global::GameManager.Preferences.QuestVerboseMode)
                    {
                        Diagnostics.Log("ELCP: {0} simulationObjectWrapper {1}", new object[]
                        {
                            questBehaviour.Initiator,
                            (simulationObjectWrapper is Garrison) ? (simulationObjectWrapper as Garrison).LocalizedName : simulationObjectWrapper.GetType().ToString()
                        });
                    }
                    flag = false;
                    using (InterpreterContext.InterpreterSession interpreterSession = new InterpreterContext.InterpreterSession(simulationObjectWrapper))
                    {
                        foreach (QuestVariable questVariable in questBehaviour.QuestVariables)
                        {
                            if (questVariable.Object is float)
                            {
                                interpreterSession.Context.Register(questVariable.Name, questVariable.Object);
                            }
                        }
                        int j = 0;
                        while (j < questBehaviourPrerequisites.Prerequisites.Length)
                        {
                            if (!questBehaviourPrerequisites.Prerequisites[j].Check(interpreterSession.Context))
                            {
                                if (global::GameManager.Preferences.QuestVerboseMode)
                                {
                                    Diagnostics.Log("ELCP: context check failed for {0} {1}", new object[]
                                    {
                                        j,
                                        questBehaviourPrerequisites.ToString()
                                    });
                                }
                                if (questBehaviourPrerequisites.AnyTarget)
                                {
                                    flag = true;
                                    break;
                                }
                                return(false);
                            }
                            else
                            {
                                j++;
                            }
                        }
                    }
                    if (!flag && questBehaviourPrerequisites.AnyTarget)
                    {
                        return(true);
                    }
                }
                if (flag)
                {
                    return(false);
                }
                i++;
                continue;
            }
            if (global::GameManager.Preferences.QuestVerboseMode)
            {
                Diagnostics.LogWarning("[Quest] Could not find prerequisite target {0} in quest {1}", new object[]
                {
                    questBehaviourPrerequisites.Target,
                    questBehaviour.Quest.Name
                });
            }
            return(false);
        }
        return(true);
    }
示例#4
0
    private void FindHeroSpecialtyELCP(Unit hero)
    {
        List <UnitSkill> list = new List <UnitSkill>();

        DepartmentOfEducation.FillAvailableUnitSkills(hero, ref list);
        using (InterpreterContext.InterpreterSession interpreterSession = new InterpreterContext.InterpreterSession(hero))
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Prerequisites != null && list[i].Prerequisites.Length != 0 && hero.IsSkillUnlocked(list[i].Name))
                {
                    int num = hero.GetSkillLevel(list[i].Name) + 1;
                    foreach (IAIParameter <InterpreterContext> iaiparameter in this.constructibleEvaluationAIHelper.GetAIParameters(list[i]))
                    {
                        int num2 = Array.IndexOf <string>(AILayer_HeroAssignation.HeroAssignationTypeNames, iaiparameter.Name.ToString());
                        if (num2 >= 0)
                        {
                            for (int j = 0; j < num; j++)
                            {
                                float num3 = iaiparameter.GetValue(interpreterSession.Context);
                                if (j > 0)
                                {
                                    num3 *= 0.5f;
                                }
                                this.LongTermSpecialtyFitness[num2] = AILayer.Boost(this.LongTermSpecialtyFitness[num2], num3);
                            }
                        }
                    }
                }
            }
            List <UnitAbilityReference> list2 = new List <UnitAbilityReference>();
            if (hero.UnitDesign.UnitBodyDefinition.UnitAbilities != null)
            {
                list2.AddRange(hero.UnitDesign.UnitBodyDefinition.UnitAbilities.ToList <UnitAbilityReference>());
            }
            if ((hero.UnitDesign as UnitProfile).ProfileAbilityReferences != null)
            {
                list2.AddRange((hero.UnitDesign as UnitProfile).ProfileAbilityReferences.ToList <UnitAbilityReference>());
            }
            list2.Add(new UnitAbilityReference(UnitAbility.ReadonlyLastStand, 0));
            using (IEnumerator <KeyValuePair <StaticString, int> > enumerator2 = hero.EnumerateUnitAbilities().GetEnumerator())
            {
                while (enumerator2.MoveNext())
                {
                    KeyValuePair <StaticString, int> keyValuePair = enumerator2.Current;
                    int num4 = list2.FindIndex((UnitAbilityReference r) => r.Name == keyValuePair.Key);
                    if (num4 >= 0)
                    {
                        float       a    = Mathf.Max(1f, (float)(list2[num4].Level + 1));
                        float       b    = Mathf.Max(1f, (float)(keyValuePair.Value + 1));
                        float       num5 = Mathf.Min(a, b);
                        UnitAbility element;
                        if (this.unitAbilityDatabase.TryGetValue(keyValuePair.Key, out element))
                        {
                            foreach (IAIParameter <InterpreterContext> iaiparameter2 in this.constructibleEvaluationAIHelper.GetAIParameters(element))
                            {
                                int num6 = Array.IndexOf <string>(AILayer_HeroAssignation.HeroAssignationTypeNames, iaiparameter2.Name.ToString());
                                if (num6 >= 0)
                                {
                                    int num7 = 0;
                                    while ((float)num7 < num5)
                                    {
                                        float num8 = iaiparameter2.GetValue(interpreterSession.Context);
                                        if (num7 > 0)
                                        {
                                            num8 *= 0.5f;
                                        }
                                        this.LongTermSpecialtyFitness[num6] = AILayer.Boost(this.LongTermSpecialtyFitness[num6], num8);
                                        num7++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
示例#5
0
    private void FindHeroSpecialty(Unit hero)
    {
        Diagnostics.Log("======================================================================================");
        Diagnostics.Log("ELCP {0} FindHeroSpecialty", new object[]
        {
            hero.Name
        });
        float            num  = 0f;
        List <UnitSkill> list = new List <UnitSkill>();

        DepartmentOfEducation.FillAvailableUnitSkills(hero, ref list);
        using (InterpreterContext.InterpreterSession interpreterSession = new InterpreterContext.InterpreterSession(hero))
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Prerequisites != null && list[i].Prerequisites.Length != 0)
                {
                    Diagnostics.Log("checking skill {0}, num {1}", new object[]
                    {
                        list[i].Name,
                        num
                    });
                    num += 1f;
                    float num2 = 1f;
                    if (hero.IsSkillUnlocked(list[i].Name))
                    {
                        int num3 = hero.GetSkillLevel(list[i].Name) + 1;
                        num  += (float)num3;
                        num2 += (float)num3;
                        Diagnostics.Log("skill unlocked, levelfactor {0}", new object[]
                        {
                            num2
                        });
                    }
                    foreach (IAIParameter <InterpreterContext> iaiparameter in this.constructibleEvaluationAIHelper.GetAIParameters(list[i]))
                    {
                        int num4 = Array.IndexOf <string>(AILayer_HeroAssignation.HeroAssignationTypeNames, iaiparameter.Name.ToString());
                        if (num4 >= 0)
                        {
                            this.LongTermSpecialtyFitness[num4] += iaiparameter.GetValue(interpreterSession.Context) * num2;
                            Diagnostics.Log("ltspecialiaty {0} + {1} to {2}", new object[]
                            {
                                num4,
                                iaiparameter.GetValue(interpreterSession.Context) * num2,
                                this.LongTermSpecialtyFitness[num4]
                            });
                        }
                    }
                }
            }
            foreach (KeyValuePair <StaticString, int> keyValuePair in hero.EnumerateUnitAbilities())
            {
                float num5 = Mathf.Max(1f, (float)(keyValuePair.Value + 1));
                Diagnostics.Log("checking ability {0} {1}/{2}, num {3}", new object[]
                {
                    keyValuePair.Key,
                    keyValuePair.Value,
                    num5,
                    num
                });
                bool        flag = false;
                UnitAbility element;
                if (this.unitAbilityDatabase.TryGetValue(keyValuePair.Key, out element))
                {
                    foreach (IAIParameter <InterpreterContext> iaiparameter2 in this.constructibleEvaluationAIHelper.GetAIParameters(element))
                    {
                        int num6 = Array.IndexOf <string>(AILayer_HeroAssignation.HeroAssignationTypeNames, iaiparameter2.Name.ToString());
                        if (num6 >= 0)
                        {
                            this.LongTermSpecialtyFitness[num6] += iaiparameter2.GetValue(interpreterSession.Context) * num5;
                            Diagnostics.Log("ltspecialiaty {0} + {1} to {2}", new object[]
                            {
                                num6,
                                iaiparameter2.GetValue(interpreterSession.Context) * num5,
                                this.LongTermSpecialtyFitness[num6]
                            });
                            flag = true;
                        }
                    }
                }
                if (flag)
                {
                    num += num5;
                }
            }
        }
        Diagnostics.Log("+++++++++++++++++++");
        for (int j = 0; j < this.LongTermSpecialtyFitness.Length; j++)
        {
            Diagnostics.Log("final specialty {0}: {1}/{2} = {3}", new object[]
            {
                j,
                this.LongTermSpecialtyFitness[j],
                num,
                this.LongTermSpecialtyFitness[j] / num
            });
            this.LongTermSpecialtyFitness[j] /= num;
        }
    }
    public override State CheckCondition(QuestBehaviour questBehaviour, GameEvent gameEvent, params object[] parameters)
    {
        if (!StaticString.IsNullOrEmpty(this.StepName))
        {
            int num = -1;
            if (!string.IsNullOrEmpty(this.InterpretedValue))
            {
                if (this.compiledExpression == null)
                {
                    this.compiledExpression = Interpreter.InfixTransform(this.InterpretedValue);
                }
                SimulationObject simulationObject;
                if (this.EmpireIndex >= 0)
                {
                    simulationObject = (Services.GetService <IGameService>().Game as global::Game).Empires[this.EmpireIndex].SimulationObject;
                }
                else if (gameEvent != null)
                {
                    simulationObject = gameEvent.Empire.SimulationObject;
                }
                else
                {
                    simulationObject = questBehaviour.Initiator.SimulationObject;
                }
                using (InterpreterContext.InterpreterSession interpreterSession = new InterpreterContext.InterpreterSession(simulationObject))
                {
                    foreach (QuestVariable questVariable in questBehaviour.QuestVariables)
                    {
                        if (questVariable.Object is float)
                        {
                            interpreterSession.Context.Register(questVariable.Name, (float)Convert.ChangeType(questVariable.Object, typeof(float)));
                        }
                    }
                    object obj = Interpreter.Execute(this.compiledExpression, interpreterSession.Context);
                    if (obj is float)
                    {
                        num = Mathf.FloorToInt((float)Convert.ChangeType(obj, typeof(float)));
                    }
                    else
                    {
                        if (!(obj is int))
                        {
                            if (global::GameManager.Preferences.QuestVerboseMode)
                            {
                                Diagnostics.LogWarning("[Quest] IsStepProgressionComplete expression or variable '{0}' returned {1} wich is not a number (in quest {2} of empire {3}).", new object[]
                                {
                                    string.IsNullOrEmpty(this.InterpretedValue) ? this.InterpretedVarName : this.InterpretedValue,
                                    obj ?? "null",
                                    questBehaviour.Quest.QuestDefinition.Name,
                                    questBehaviour.Initiator.Index
                                });
                            }
                            return(State.Failure);
                        }
                        num = (int)Convert.ChangeType(obj, typeof(int));
                    }
                    QuestVariable questVariable2 = questBehaviour.QuestVariables.FirstOrDefault((QuestVariable match) => match.Name == this.StepName);
                    if (questVariable2 != null)
                    {
                        QuestRegisterVariable questRegisterVariable = questVariable2.Object as QuestRegisterVariable;
                        if (questRegisterVariable != null)
                        {
                            questRegisterVariable.Value = num;
                            questBehaviour.Push(new QuestInstruction_UpdateRegisterVariable(this.StepName, questRegisterVariable.Value));
                        }
                        if (questBehaviour.Quest.QuestDefinition.IsGlobal)
                        {
                            StaticString personnalStepName = this.StepName + Quest.PersonnalProgressStepSuffix;
                            questVariable2 = questBehaviour.QuestVariables.FirstOrDefault((QuestVariable match) => match.Name == personnalStepName);
                            if (questVariable2 != null)
                            {
                                questRegisterVariable = (questVariable2.Object as QuestRegisterVariable);
                                if (questRegisterVariable != null)
                                {
                                    questRegisterVariable.Value = num;
                                    questBehaviour.Push(new QuestInstruction_UpdateRegisterVariable(personnalStepName, questRegisterVariable.Value));
                                }
                            }
                        }
                    }
                    goto IL_2DB;
                }
            }
            num = questBehaviour.Quest.GetStepProgressionValueByName(this.StepName);
IL_2DB:
            QuestStepProgressionRange stepProgressionRangeByName = questBehaviour.Quest.GetStepProgressionRangeByName(this.StepName);
            if (stepProgressionRangeByName == null)
            {
                Diagnostics.LogError("[Quest] ConditionCheck_IsStepProgressionComplete: step '{0}' doesn't have a progression range. (in quest {1})", new object[]
                {
                    this.StepName,
                    questBehaviour.Quest.Name
                });
                return(State.Failure);
            }
            if (stepProgressionRangeByName.EndValueVarName != string.Empty)
            {
                questBehaviour.UpdateProgressionVariables();
                stepProgressionRangeByName = questBehaviour.Quest.GetStepProgressionRangeByName(this.StepName);
            }
            if (num >= stepProgressionRangeByName.EndValue)
            {
                return(State.Success);
            }
        }
        return(State.Failure);
    }