Esempio n. 1
0
 internal void InitializeAvailableResources()
 {
     if (availableResources == null)
     {
         availableResources = SwitchableResourceSet.ForPart(part.name);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Here when the game is first loading up.
        /// </summary>
        /// <param name="node"></param>
        private void OnInitialGameLoading(ConfigNode node)
        {
            Loader.Initialize();

            // Load the resources from config.
            resources = SwitchableResource.Load(node);
            longTitle = SwitchableResource.LongTitleOf(resources);

            // If they haven't specified a resources title in the config,
            // use the auto-generated long one.
            if (string.IsNullOrEmpty(displayName))
            {
                displayName = longTitle;
            }

            // Add the resources to the global map. This is how the ModuleSimpleFuelSwitch will
            // control behavior when the player is working with a ship in the editor.
            SwitchableResourceSet.Add(
                part.name,
                resourcesId,
                displayName,
                selectorFieldName,
                ParseLinkedVariants(linkedVariant),
                isDefault,
                resources,
                part.Resources);

            // Everything else in this class is concerned simply with setting up stuff
            // to display appropriate info in the part info window from the parts pane
            // of the vehicle editor.

            info = FormatInfo(resourcesId, resources);
            if (resources.Length == 0)
            {
                primaryField = longTitle;
            }
            else
            {
                primaryField = LocalizeUtil.Format(
                    "#SimpleFuelSwitch_primaryInfoFormat",
                    selectorFieldName,
                    displayName,
                    ResourcePrimaryInfoFormatter.Format(resourcesId, resources));
            }
        }
        /// <summary>
        /// Add a set of resources for the specified part.
        /// </summary>
        /// <param name="partName"></param>
        /// <param name="resourcesId"></param>
        /// <param name="displayName"></param>
        /// <param name="selectorFieldName"></param>
        /// <param name="isDefault"></param>
        /// <param name="resources"></param>
        public static void Add(
            string partName,
            string resourcesId,
            string displayName,
            string selectorFieldName,
            bool isDefault,
            SwitchableResource[] resources)
        {
            SwitchableResourceSet set = null;

            if (!resourcesByPartName.TryGetValue(partName, out set))
            {
                set = new SwitchableResourceSet(selectorFieldName);
                resourcesByPartName.Add(partName, set);
            }
            set.Add(resourcesId, displayName, resources);
            if (isDefault && (set.defaultResourcesId == null))
            {
                set.defaultResourcesId = resourcesId;
            }
        }
        /// <summary>
        /// Here when the game is first loading up.
        /// </summary>
        /// <param name="node"></param>
        private void OnInitialGameLoading(ConfigNode node)
        {
            // Load the resources from config and add them to the global map. This is how
            // the ModuleSimpleFuelSwitch will control behavior when the player is working
            // with a ship in the editor.
            resources = SwitchableResource.Load(node);
            SwitchableResourceSet.Add(part.name, resourcesId, displayName, selectorFieldName, isDefault, resources);

            // Everything else in this class is concerned simply with setting up stuff
            // to display appropriate info in the part info window from the parts pane
            // of the vehicle editor.

            // Build a long title from the resource names.
            StringBuilder builder = new StringBuilder(resources[0].definition.displayName);

            for (int i = 1; i < resources.Length; ++i)
            {
                builder.Append(" + ");
                builder.Append(resources[i].definition.displayName);
            }
            longTitle = builder.ToString();

            // If they haven't specified a resources title in the config,
            // use the auto-generated long one.
            if (displayName == null)
            {
                displayName = longTitle;
            }

            info         = FormatInfo(resources);
            primaryField = LocalizeUtil.Format(
                "#SimpleFuelSwitch_primaryInfoFormat",
                selectorFieldName,
                displayName,
                FormatPrimaryFieldQuantity(resources));
        }
        /// <summary>
        /// Bring a part's resources into line with the specified resources ID for selection. Returns
        /// the current selection, or null if there's a problem.
        /// </summary>
        /// <param name="part"></param>
        /// <param name="resourcesId"></param>
        /// <param name="resetAmounts"></param>
        public static Selection UpdatePartResourceList(Part part, string resourcesId, bool resetAmounts)
        {
            // First, find what selectable resources *should* be there
            SwitchableResourceSet set = null;

            if (!resourcesByPartName.TryGetValue(part.name, out set))
            {
                return(null);                                                      // no info found for this part
            }
            Selection selection = set[resourcesId];

            if (selection == null)
            {
                return(null);                   // no such selection available for this part
            }
            // Now see if there's anything we should be removing
            bool          isDirty           = false;
            List <string> unwantedResources = null;

            for (int i = 0; i < part.Resources.Count; ++i)
            {
                PartResource resource = part.Resources[i];
                if (set.baseResourceNames.Contains(resource.resourceName))
                {
                    continue;                                                        // included on the base part, so keep it
                }
                SwitchableResource switchableResource = selection.TryFind(resource.resourceName);
                if (switchableResource != null)
                {
                    if (resetAmounts)
                    {
                        // The resource is one that we want to be on the current part. However, the amount
                        // or maxAmount might be off.
                        resource.maxAmount = switchableResource.maxAmount;
                        resource.amount    = switchableResource.amount;
                    }
                    continue;
                }
                // It's unwanted. Add it to the list.
                if (unwantedResources == null)
                {
                    unwantedResources = new List <string>();
                }
                unwantedResources.Add(resource.resourceName);
            }
            if (unwantedResources != null)
            {
                isDirty = true;
                for (int i = 0; i < unwantedResources.Count; ++i)
                {
                    part.Resources.Remove(unwantedResources[i]);
                }
            }

            // Then see whether there are any missing resources we should be adding
            for (int i = 0; i < selection.resources.Length; ++i)
            {
                SwitchableResource resource = selection.resources[i];
                if (!part.Resources.Contains(resource.definition.name))
                {
                    isDirty = true;
                    part.Resources.Add(resource.CreateResourceNode());
                }
            }

            if (isDirty)
            {
                // we made changes, so we need to reset some stuff
                part.SimulationResources.Clear();
                part.ResetSimulation();
            }
            return(selection);
        }