Beispiel #1
0
        /// <summary>Tells if the part can be stacked in the inventory.</summary>
        /// <param name="avPart">The part proto to check.</param>
        /// <returns><c>true</c> if it can stack.</returns>
        public static bool CheckItemStackable(AvailablePart avPart)
        {
            var module = avPart.partPrefab.GetComponent <ModuleKISItem>();
            var allModulesCompatible = avPart.partPrefab.Modules.Cast <PartModule>()
                                       .All(m => KISAddonConfig.stackableModules.Contains(m.moduleName));
            var hasNoResources = PartNodeUtils.GetModuleNode(avPart.partConfig, "RESOURCE") != null;

            return(module != null && module.stackable ||
                   KISAddonConfig.stackableList.Contains(avPart.name.Replace('.', '_')) ||
                   allModulesCompatible && hasNoResources);
        }
Beispiel #2
0
        /// <summary>Creates a new item, given a saved state.</summary>
        /// <remarks>
        /// It's intentionally private. The items must be restored thru the factory methods.
        /// </remarks>
        /// <seealso cref="RestoreItemFromNode"/>
        KIS_Item(AvailablePart availablePart, ConfigNode itemNode,
                 ModuleKISInventory inventory, int quantity)
        {
            this.availablePart = availablePart;
            this.inventory     = inventory;
            this.quantity      = quantity;
            SetPrefabModule();
            this.stackable = CheckItemStackable(availablePart);
            this.partNode  = new ConfigNode();
            itemNode.GetNode("PART").CopyTo(partNode);
            ConfigAccessor.ReadFieldsFromNode(
                itemNode, GetType(), this, group: StdPersistentGroups.PartPersistant);
            this.itemVolume = KISAPI.PartUtils.GetPartVolume(availablePart, partNode: partNode);

            // COMPATIBILITY: Set/restore the dry cost and mass.
            // TODO(ihsoft): This code is only needed for the pre-1.17 KIS version saves. Drop it one day.
            if (this.itemDryMass < float.Epsilon || this.itemDryCost < float.Epsilon)
            {
                this._itemDryMass = KISAPI.PartUtils.GetPartDryMass(availablePart, partNode: partNode);
                this._itemDryCost = KISAPI.PartUtils.GetPartDryCost(availablePart, partNode: partNode);
                DebugEx.Warning("Calculated values for a pre 1.17 version save: dryMass={0}, dryCost={1}",
                                this.itemDryMass, this.itemDryCost);
            }

            // COMPATIBILITY: Set/restore the resources cost and mass.
            // TODO(ihsoft): This code is only needed for the pre-1.17 KIS version saves. Drop it one day.
            var resourceNodes = PartNodeUtils.GetModuleNodes(partNode, "RESOURCE");

            if (resourceNodes.Any() &&
                (this.itemResourceMass < float.Epsilon || this.itemResourceCost < float.Epsilon))
            {
                var oldResourceMass = this.itemResourceMass;
                foreach (var resourceNode in resourceNodes)
                {
                    var resource = new ProtoPartResourceSnapshot(resourceNode);
                    this._resourceMass += (float)resource.amount * resource.definition.density;
                    this._resourceCost += (float)resource.amount * resource.definition.unitCost;
                }
                DebugEx.Warning("Calculated values for a pre 1.17 version save:"
                                + " oldResourceMass={0}, newResourceMass={1}, resourceCost={2}",
                                oldResourceMass, this.itemResourceMass, this.itemResourceCost);
            }
        }
Beispiel #3
0
        /// <summary>Gets the part's variant.</summary>
        /// <param name="avPart">The part proto to get the variant for.</param>
        /// <param name="partNode">The part's persistent state.</param>
        /// <returns>The part's variant or <c>null</c> if none defined.</returns>
        public static PartVariant GetCurrentPartVariant(AvailablePart avPart, ConfigNode partNode)
        {
            var variantsModule = PartNodeUtils.GetModuleNode <ModulePartVariants>(partNode);

            if (variantsModule == null)
            {
                return(null);
            }
            var selectedVariantName = variantsModule.GetValue("selectedVariant");

            if (selectedVariantName == null)
            {
                // Use the very first variant, if any.
                return(avPart.partPrefab.variants.variantList.Count > 0
          ? avPart.partPrefab.variants.variantList[0]
          : null);
            }
            return(avPart.partPrefab.variants.variantList
                   .FirstOrDefault(v => v.Name == selectedVariantName));
        }
Beispiel #4
0
 /// <summary>Gets <c>TweakScale</c> module config.</summary>
 /// <param name="partNode">
 /// The config to extract the module config from. It can be <c>null</c>.
 /// </param>
 /// <returns>The <c>TweakScale</c> module or <c>null</c>.</returns>
 public ConfigNode GetTweakScaleModule(ConfigNode partNode)
 {
     return(partNode != null?PartNodeUtils.GetModuleNode(partNode, "TweakScale") : null);
 }