コード例 #1
0
 /// <summary>
 /// adds teh given exression to this expressions dependant list
 /// </summary>
 /// <param name="dependant"></param>
 public void AddDependee(ChainedExpression dependant)
 {
     if (!DependantExpressions.Contains(dependant) && dependant._isDependant)
     {
         DependantExpressions.Add(dependant);
     }
 }
コード例 #2
0
 /// <summary>
 /// adds this to the given ChainedExpressions dependants list.
 /// </summary>
 /// <param name="dependee"></param>
 private void MakeThisDependant(ChainedExpression dependee)
 {
     if (!dependee.DependantExpressions.Contains(this) && _isDependant)
     {
         dependee.DependantExpressions.Add(this);
     }
 }
コード例 #3
0
        /// <summary>
        /// extra custom functinos go in here.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="args"></param>
        private void NCalcPulsarFunctions(string name, FunctionArgs args)
        {
            if (name == "Ability")
            {
                int index = 0;
                try
                {
                    index = (int)args.Parameters[0].Evaluate();

                    ChainedExpression result = _design.ComponentDesignAbilities[index].Formula;
                    if (result.Result == null)
                    {
                        result.Evaluate();
                    }
                    MakeThisDependant(result);
                    args.Result = result.Result;
                }
                catch (InvalidCastException e) { throw new Exception("Parameter must be an intiger. " + e); }
                catch (IndexOutOfRangeException e) { throw new Exception("This component does not have an ComponentAbilitySD at index " + index + ". " + e); }
            }
            if (name == "SetAbilityValue") //I might remove this..
            {
                int index = 0;
                try
                {
                    index = (int)args.Parameters[0].Evaluate();

                    ChainedExpression expression = _design.ComponentDesignAbilities[index].Formula;
                    expression.SetResult = args.Parameters[1].Evaluate();
                }
                catch (InvalidCastException e) { throw new Exception("Parameter must be an intiger. " + e); }
                catch (IndexOutOfRangeException e) { throw new Exception("This component does not have an ComponentAbilitySD at index " + index + ". " + e); }
            }

            if (name == "EnumDict")
            {
                string  typeAsString = (string)args.Parameters[0].Evaluate();
                Type    type         = Type.GetType(typeAsString);
                Type    dictType     = typeof(Dictionary <,>).MakeGenericType(type, typeof(double));
                dynamic dict         = Activator.CreateInstance(dictType);

                Type    enumDictType  = typeof(Dictionary <,>).MakeGenericType(typeof(string), type);
                dynamic enumConstants = Activator.CreateInstance(enumDictType);
                foreach (dynamic value in Enum.GetValues(type))
                {
                    enumConstants.Add(Enum.GetName(type, value), value);
                }

                foreach (var kvp in _designAbility.GuidDictionary)
                {
                    dynamic key = enumConstants[(string)kvp.Key];
                    dict.Add(key, kvp.Value.DResult);
                }
                args.Result = dict;
            }

            if (name == "TechData")
            {
                Guid   techGuid = Guid.Parse((string)args.EvaluateParameters()[0]);
                TechSD techSD   = _staticDataStore.Techs[techGuid];
                args.Result = TechProcessor.DataFormula(_factionTechDB, techSD);
            }

            //Returns the tech level for the given guid
            if (name == "TechLevel")
            {
                Guid techGuid = Guid.Parse((string)args.EvaluateParameters()[0]);
                if (_factionTechDB.ResearchedTechs.ContainsKey(techGuid))
                {
                    args.Result = _factionTechDB.ResearchedTechs[techGuid];
                }
                else
                {
                    args.Result = 0;
                }
            }
            //currently not used, but an future experiment to pass the CargoTypeSD as a parameter
            if (name == "CargoType")
            {
                Guid        typeGuid = Guid.Parse((string)args.EvaluateParameters()[0]);
                CargoTypeSD typeSD   = _staticDataStore.CargoTypes[typeGuid];
                args.Result = typeSD;
            }
            //used for datablob args for when a guid is required as a parameter
            if (name == "GuidString")
            {
                Guid typeGuid = Guid.Parse((string)args.EvaluateParameters()[0]);
                args.Result = typeGuid;
            }

            //This sets the DatablobArgs. it's up to the user to ensure the right number of args for a specific datablob
            //The datablob will be the one defined in designAbility.DataBlobType
            //TODO document blobs and what args they take!!
            if (name == "DataBlobArgs")
            {
                if (_designAbility.DataBlobType == null)
                {
                    throw new Exception("This Ability does not have a DataBlob defined! define a datablob for this ability!");
                }
                //_designAbility.DataBlobArgs = new List<double>();
                List <object> argList = new List <object>();
                foreach (var argParam in args.Parameters)
                {
                    ChainedExpression argExpression = new ChainedExpression(argParam, _designAbility, _factionTechDB, _staticDataStore);
                    _isDependant = false;
                    argExpression.Evaluate();
                    argList.Add(argExpression.Result);
                }
                _designAbility.DataBlobArgs = argList.ToArray();
                args.Result = argList;
            }
        }
コード例 #4
0
        /// <summary>
        /// extra custom functinos go in here.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="args"></param>
        private void NCalcPulsarFunctions(string name, FunctionArgs args)
        {
            string key   = "Unknown Key";
            int    index = -1;
            Guid   techGuid;
            Guid   typeGuid;

            switch (name)
            {
            case "Ability":
                try
                {
                    //TODO: get rid of this once json data is rewritten to use names instead of indexes
                    if (args.Parameters[0].Evaluate() is int)
                    {
                        index = (int)args.Parameters[0].Evaluate();
                        ChainedExpression result = _designer.ComponentDesignAttributeList[index].Formula;
                        if (result.Result == null)
                        {
                            result.Evaluate();
                        }
                        MakeThisDependant(result);
                        args.Result = result.Result;
                    }
                    else
                    {
                        key = (string)args.Parameters[0].Evaluate();

                        ChainedExpression result = _designer.ComponentDesignAttributes[key].Formula;
                        if (result.Result == null)
                        {
                            result.Evaluate();
                        }
                        MakeThisDependant(result);
                        args.Result = result.Result;
                    }
                }
                //TODO: maybe log this catch and throw the component out. (instead of throwing)
                catch (KeyNotFoundException e)
                {
                    throw new Exception("Cannot find an ability named " + key + ", in " + _designer.Name + " " + e);
                }

                //TODO: the two catches below will be unnesiary once ComponentDesignAttributeList is gone.
                catch (InvalidCastException e)
                {
                    throw new Exception("Parameter must be an intiger. " + e);
                }
                catch (IndexOutOfRangeException e)
                {
                    throw new Exception("This component does not have an ComponentAbilitySD at index " + index + ". " + e);
                }
                break;

            case "SetAbilityValue":     //I might remove this..
                try
                {
                    //TODO: get rid of this once json data is rewritten to use names instead of indexes
                    if (args.Parameters[0].Evaluate() is int)
                    {
                        index = (int)args.Parameters[0].Evaluate();
                        ChainedExpression expression = _designer.ComponentDesignAttributeList[index].Formula;
                        expression.SetResult = args.Parameters[1].Evaluate();
                    }
                    else
                    {
                        key = (string)args.Parameters[0].Evaluate();

                        ChainedExpression expression = _designer.ComponentDesignAttributes[key].Formula;
                        expression.SetResult = args.Parameters[1].Evaluate();
                    }
                }
                //TODO: maybe log this catch and throw the component out. (instead of throwing)
                catch (KeyNotFoundException e)
                {
                    throw new Exception("Cannot find an ability named " + key + ". " + e);
                }

                //TODO: the two catches below will be unnesiary once ComponentDesignAttributeList is gone.
                catch (InvalidCastException e)
                {
                    throw new Exception("Parameter must be an intiger. " + e);
                }
                catch (IndexOutOfRangeException e)
                {
                    throw new Exception("This component does not have an ComponentAbilitySD at index " + index + ". " + e);
                }

                break;

            case "EnumDict":
                string typeAsString = (string)args.Parameters[0].Evaluate();
                Type   type         = Type.GetType(typeAsString);
                if (type == null)
                {
                    throw new Exception("Type not found: " + typeAsString + " Check spelling and namespaces");
                }

                Type    dictType = typeof(Dictionary <,>).MakeGenericType(type, typeof(double));
                dynamic dict     = Activator.CreateInstance(dictType);

                Type    enumDictType  = typeof(Dictionary <,>).MakeGenericType(typeof(string), type);
                dynamic enumConstants = Activator.CreateInstance(enumDictType);
                foreach (dynamic value in Enum.GetValues(type))
                {
                    enumConstants.Add(Enum.GetName(type, value), value);
                }

                foreach (var kvp in _designAttribute.GuidDictionary)
                {
                    dynamic keyd = enumConstants[(string)kvp.Key];
                    dict.Add(keyd, kvp.Value.DResult);
                }

                args.Result = dict;
                break;

            case "TechData":
                techGuid = Guid.Parse((string)args.EvaluateParameters()[0]);
                TechSD techSD = _staticDataStore.Techs[techGuid];
                args.Result = ResearchProcessor.DataFormula(_factionTechDB, techSD);
                break;

            //Returns the tech level for the given guid
            case "TechLevel":
                techGuid = Guid.Parse((string)args.EvaluateParameters()[0]);
                if (_factionTechDB.ResearchedTechs.ContainsKey(techGuid))
                {
                    args.Result = _factionTechDB.ResearchedTechs[techGuid];
                }
                else
                {
                    args.Result = 0;
                }
                break;

            //currently not used, but an future experiment to pass the CargoTypeSD as a parameter
            case "CargoType":
                typeGuid = Guid.Parse((string)args.EvaluateParameters()[0]);
                CargoTypeSD typeSD = _staticDataStore.CargoTypes[typeGuid];
                args.Result = typeSD;
                break;

            //used for datablob args for when a guid is required as a parameter
            case "GuidString":
                typeGuid    = Guid.Parse((string)args.EvaluateParameters()[0]);
                args.Result = typeGuid;
                break;

            //This sets the DatablobArgs. it's up to the user to ensure the right number of args for a specific datablob
            //The datablob will be the one defined in designAbility.AttributeType
            //TODO document blobs and what args they take!!
            case "AtbConstrArgs":
                if (_designAttribute.AttributeType == null)
                {
                    throw new Exception(_designAttribute.Name + " does not have a type defined! define an AttributeType for this Attribute!");
                }
                //_designAbility.AtbConstrArgs = new List<double>();
                List <object> argList = new List <object>();
                foreach (var argParam in args.Parameters)
                {
                    ChainedExpression argExpression = new ChainedExpression(argParam, _designAttribute, _factionTechDB, _staticDataStore);
                    _isDependant = false;
                    argExpression.Evaluate();
                    argList.Add(argExpression.Result);
                }

                _designAttribute.AtbConstrArgs = argList.ToArray();
                args.Result = argList;
                break;
            }
        }
コード例 #5
0
        public ComponentDesignAttribute(ComponentDesigner parentComponent, ComponentTemplateAttributeSD templateAtb, FactionTechDB factionTech)
        {
            ParentComponent = parentComponent;
            _templateSD     = templateAtb;
            var staticData = StaticRefLib.StaticData;

            if (_templateSD.AttributeFormula != null)
            {
                Formula = new ChainedExpression(_templateSD.AttributeFormula, this, factionTech, staticData);
            }

            if (!string.IsNullOrEmpty(_templateSD.DescriptionFormula))
            {
                DescriptionFormula = new ChainedExpression(_templateSD.DescriptionFormula, this, factionTech, staticData);
            }

            if (_templateSD.GuidDictionary != null)
            {
                GuidDictionary = new Dictionary <object, ChainedExpression>();
                if (GuiHint == GuiHint.GuiTechSelectionList)
                {
                    foreach (var kvp in _templateSD.GuidDictionary)
                    {
                        if (factionTech.ResearchedTechs.ContainsKey(Guid.Parse(kvp.Key.ToString())))
                        {
                            TechSD techSD = staticData.Techs[Guid.Parse(kvp.Key.ToString())];
                            GuidDictionary.Add(kvp.Key, new ChainedExpression(ResearchProcessor.DataFormula(factionTech, techSD).ToString(), this, factionTech, staticData));
                        }
                    }
                }
                else
                {
                    foreach (var kvp in _templateSD.GuidDictionary)
                    {
                        GuidDictionary.Add(kvp.Key, new ChainedExpression(kvp.Value, this, factionTech, staticData));
                    }
                }
            }
            if (GuiHint == GuiHint.GuiSelectionMaxMin)
            {
                MaxValueFormula  = new ChainedExpression(_templateSD.MaxFormula, this, factionTech, staticData);
                MinValueFormula  = new ChainedExpression(_templateSD.MinFormula, this, factionTech, staticData);
                StepValueFormula = new ChainedExpression(_templateSD.StepFormula, this, factionTech, staticData);
            }
            if (_templateSD.AttributeType != null)
            {
                AttributeType = Type.GetType(_templateSD.AttributeType);
                if (AttributeType == null)
                {
                    throw new Exception("Attribute Type Error. Attribute type not found: " + _templateSD.AttributeType + ". Try checking the namespace.");
                }
            }

            if (GuiHint == GuiHint.GuiEnumSelectionList)
            {
                MaxValueFormula  = new ChainedExpression(_templateSD.MaxFormula, this, factionTech, staticData);
                MinValueFormula  = new ChainedExpression(_templateSD.MinFormula, this, factionTech, staticData);
                StepValueFormula = new ChainedExpression(_templateSD.StepFormula, this, factionTech, staticData);
                SetMax();
                SetMin();
                SetStep();
                EnumType = Type.GetType(_templateSD.EnumTypeName);
                if (EnumType == null)
                {
                    throw new Exception("EnumTypeName not found: " + _templateSD.EnumTypeName);
                }
                ListSelection = (int)Value;
                //string[] names = Enum.GetNames(EnumType);
            }

            if (_templateSD.GuiIsEnabledFormula != null)
            {
                IsEnabledFormula = new ChainedExpression(_templateSD.GuiIsEnabledFormula, this, factionTech, staticData);
                var ghint = GuiHint.GuiTextDisplay | GuiHint.GuiDisplayBool;
            }

            if (GuiHint == GuiHint.GuiOrdnanceSelectionList)
            {
            }
        }