Beispiel #1
0
        /// <summary>Updates the part's resource.</summary>
        /// <param name="partNode">
        /// The part's config or a persistent state. It can be a top-level node or the <c>PART</c> node.
        /// </param>
        /// <param name="name">The name of the resource.</param>
        /// <param name="amount">The new amount or the delta.</param>
        /// <param name="isAmountRelative">
        /// Tells if the amount must be added to the current item's amount instead of simply replacing it.
        /// </param>
        /// <returns>The new amount or <c>null</c> if the resource was not found.</returns>
        public double?UpdateResource(ConfigNode partNode, string name, double amount,
                                     bool isAmountRelative = false)
        {
            if (partNode.HasNode("PART"))
            {
                partNode = partNode.GetNode("PART");
            }
            var node = partNode.GetNodes("RESOURCE")
                       .FirstOrDefault(r => r.GetValue("name") == name);
            double?setAmount = null;

            if (node != null)
            {
                setAmount = amount;
                if (isAmountRelative)
                {
                    setAmount += ConfigAccessor.GetValueByPath <double>(node, "amount") ?? 0.0;
                }
                ConfigAccessor.SetValueByPath(node, "amount", setAmount.Value);
            }
            else
            {
                DebugEx.Error("Cannot find resource '{0}' in config:\n{1}", name, partNode);
            }
            return(setAmount);
        }
Beispiel #2
0
        /// <summary>Calculates part's dry cost given the config and the variant.</summary>
        /// <param name="avPart">The part's proto.</param>
        /// <param name="variant">
        /// The part's variant. If it's <c>null</c>, then the variant will be attempted to read from
        /// <paramref name="partNode"/>.
        /// </param>
        /// <param name="partNode">
        /// The part's persistent config. It will be looked up for the various cost modifiers.
        /// </param>
        /// <returns>The dry cost of the part.</returns>
        public double GetPartDryCost(
            AvailablePart avPart, PartVariant variant = null, ConfigNode partNode = null)
        {
            // TweakScale compatibility
            if (partNode != null)
            {
                var tweakScale = KISAPI.PartNodeUtils.GetTweakScaleModule(partNode);
                if (tweakScale != null)
                {
                    var tweakedCost = ConfigAccessor.GetValueByPath <double>(tweakScale, "DryCost");
                    if (tweakedCost.HasValue)
                    {
                        // TODO(ihsoft): Get back to this code once TweakScale supports variants.
                        return(tweakedCost.Value);
                    }
                    DebugEx.Error("No dry cost specified in a tweaked part {0}:\n{1}", avPart.name, tweakScale);
                }
            }
            var itemCost = avPart.cost;

            if (variant == null && partNode != null)
            {
                variant = VariantsUtils.GetCurrentPartVariant(avPart, partNode);
            }
            VariantsUtils.ExecuteAtPartVariant(avPart, variant,
                                               p => itemCost += p.GetModuleCosts(avPart.cost));
            return(itemCost);
        }
Beispiel #3
0
 /// <summary>Returns external part's scale given a config.</summary>
 /// <remarks>This is a scale applied on the module by the other mods. I.e. it's a "runtime" scale,
 /// not the one specified in the common part's config.
 /// <para>The only mod supported till now is <c>TweakScale</c>.</para>
 /// </remarks>
 /// <param name="partNode">A config to get values from.</param>
 /// <returns>Multiplier to a model's scale on one axis.</returns>
 public static float GetPartExternalScaleModifier(ConfigNode partNode)
 {
     // TweakScale compatibility.
     foreach (var node in partNode.GetNodes("MODULE"))
     {
         if (node.GetValue("name") == "TweakScale")
         {
             double defaultScale = 1.0f;
             ConfigAccessor.GetValueByPath(node, "defaultScale", ref defaultScale);
             double currentScale = 1.0f;
             ConfigAccessor.GetValueByPath(node, "currentScale", ref currentScale);
             return((float)(currentScale / defaultScale));
         }
     }
     return(1.0f);
 }
Beispiel #4
0
        /// <summary>Creates an item, restored form the save file.</summary>
        /// <param name="itemNode">The item config node to load the data from.</param>
        /// <param name="inventory">The owner inventory of the item.</param>
        /// <returns>The item instance.</returns>
        public static KIS_Item RestoreItemFromNode(ConfigNode itemNode, ModuleKISInventory inventory)
        {
            var           qty      = ConfigAccessor.GetValueByPath <int>(itemNode, "quantity") ?? 0;
            var           partName = itemNode.GetValue("partName");
            AvailablePart avPart   = null;

            if (partName != null)
            {
                avPart = PartLoader.getPartInfoByName(partName);
            }
            if (qty == 0 || partName == null || avPart == null)
            {
                DebugEx.Error("Bad item config:\n{0}", itemNode);
                throw new ArgumentException("Bad item config node", "itemNode");
            }
            return(new KIS_Item(avPart, itemNode, inventory, qty));
        }
Beispiel #5
0
        /// <summary>Gets scale modifier, applied by TweakScale mod.</summary>
        /// <param name="partNode">The part's persistent state config.</param>
        /// <returns>The scale ratio.</returns>
        public double GetTweakScaleSizeModifier(ConfigNode partNode)
        {
            var ratio          = 1.0;
            var tweakScaleNode = GetTweakScaleModule(partNode);

            if (tweakScaleNode != null)
            {
                var defaultScale = ConfigAccessor.GetValueByPath <double>(tweakScaleNode, "defaultScale");
                var currentScale = ConfigAccessor.GetValueByPath <double>(tweakScaleNode, "currentScale");
                if (defaultScale.HasValue && currentScale.HasValue)
                {
                    ratio = currentScale.Value / defaultScale.Value;
                }
                else
                {
                    DebugEx.Error("Bad TweakScale config:\n{0}", tweakScaleNode);
                }
            }
            return(ratio);
        }
 /// <summary>Loads a persisted switch binding.</summary>
 /// <param name="node">The node to get values from.</param>
 /// <seealso cref="PersistentFieldAttribute"/>
 /// <include file="KSPAPI_HelpIndex.xml" path="//item[@name='T:KSPField']"/>
 public virtual void Load(ConfigNode node)
 {
     ConfigAccessor.GetValueByPath(node, "keyCode", ref keyCode);
 }