public static PartResource AddResource(this Part part, PartResourceDefinition info, float maxAmount, float amount)
        {
            PartResource resource = new PartResource(part);

            resource.SetInfo(info);
            resource.maxAmount   = maxAmount;
            resource.amount      = amount;
            resource.flowState   = true;
            resource.isTweakable = info.isTweakable;
            resource.isVisible   = info.isVisible;
            resource.hideFlow    = false;
            resource.flowMode    = PartResource.FlowMode.Both;
            part.Resources.dict.Add(info.name.GetHashCode(), resource);

            PartResource simulationResource = new PartResource(resource)
            {
                simulationResource = true
            };

            part.SimulationResources.dict.Add(info.name.GetHashCode(), simulationResource);

            GameEvents.onPartResourceListChange.Fire(part);

            return(resource);
        }
        private void EvaAddResource(Part part, double rate, string name, bool full)
        {
            try
            {
                double       max      = rate * globalSettings.EvaDefaultResourceAmount;
                PartResource resource = part.gameObject.AddComponent <PartResource>();
                resource.SetInfo(PartResourceLibrary.Instance.resourceDefinitions[name]);
                resource.maxAmount = max;
                resource.flowState = true;
                resource.flowMode  = PartResource.FlowMode.Both;
                resource.part      = part;

                if (full)
                {
                    resource.amount = max;
                }
                else
                {
                    resource.amount = 0;
                }

                part.Resources.list.Add(resource);
            }
            catch (Exception ex)
            {
                this.LogError("Failed to add resource " + name + " to EVA: " + ex.Message + "\n" + ex.StackTrace);
            }
        }
Example #3
0
        private PartResource EnsureAblatorResource(string name)
        {
            PartResource res = part.Resources[name];

            if (res == null)
            {
                PartResourceDefinition resDef = PartResourceLibrary.Instance.GetDefinition(name);
                if (resDef == null)
                {
                    Debug.LogError($"[ROHeatshields] Resource {name} not found!");
                    return(null);
                }

                res = new PartResource(part);
                res.resourceName = name;
                res.SetInfo(resDef);
                res._flowState  = true;
                res.isTweakable = resDef.isTweakable;
                res.isVisible   = resDef.isVisible;
                res.hideFlow    = false;
                res._flowMode   = PartResource.FlowMode.Both;
                part.Resources.dict.Add(resDef.id, res);
            }

            return(res);
        }
        private void EvaAddResource(Part part, double rate, string name, bool full)
        {
            try
            {
                double       max      = rate * settings.EvaDefaultResourceAmount;
                PartResource resource = part.gameObject.AddComponent <PartResource>();
                resource.SetInfo(PartResourceLibrary.Instance.resourceDefinitions[name]);
                resource.maxAmount = max;

                if (full)
                {
                    resource.amount = max;
                }
                else
                {
                    resource.amount = 0;
                }

                part.Resources.list.Add(resource);
            }
            catch (Exception ex)
            {
                Debug.LogError("TAC Life Support (AddLifeSupport) [" + this.GetInstanceID().ToString("X") + "][" + Time.time
                               + "]: Failed to add resource " + name + " to EVA: " + ex.Message + "\n" + ex.StackTrace);
            }
        }
        private void addLSResources(string EvaNAME)
        {
            try
            {
                PartLoader.getPartInfoByName(EvaNAME).partPrefab.AddModule("IFILifeSupportEVA");
                IFIDebug.IFIMess(" IFI Preload ++Module++  Module Added");
            }
            catch (Exception ex) { IFIDebug.IFIMess("EVA IFI Exception +addmodule+ " + ex.Message); }


            double       MaxLS       = Rate_Per_Kerbal * 60 * 60 * 4;
            PartResource LifeSupport = PartLoader.getPartInfoByName(EvaNAME).partPrefab.gameObject.AddComponent <PartResource>();

            LifeSupport.SetInfo(PartResourceLibrary.Instance.resourceDefinitions["LifeSupport"]);
            LifeSupport.maxAmount = MaxLS;
            LifeSupport.flowState = true;
            LifeSupport.flowMode  = PartResource.FlowMode.Both;
            LifeSupport.part      = PartLoader.getPartInfoByName(EvaNAME).partPrefab;
            LifeSupport.amount    = MaxLS;


            try
            {
                PartLoader.getPartInfoByName(EvaNAME).partPrefab.Resources.list.Add(LifeSupport);;
                IFIDebug.IFIMess(" IFI Preload ++LS resource++");
            }
            catch (Exception ex) { IFIDebug.IFIMess("  IFI Preload Exception +addresource+" + ex.Message); }

            LifeSupport = PartLoader.getPartInfoByName(EvaNAME).partPrefab.gameObject.AddComponent <PartResource>();
            LifeSupport.SetInfo(PartResourceLibrary.Instance.resourceDefinitions["ElectricCharge"]);
            LifeSupport.maxAmount = MaxLS * 1.5;
            LifeSupport.flowState = true;
            LifeSupport.flowMode  = PartResource.FlowMode.Both;
            LifeSupport.part      = PartLoader.getPartInfoByName(EvaNAME).partPrefab;
            LifeSupport.amount    = MaxLS * 1.5;;
            try
            {
                PartLoader.getPartInfoByName(EvaNAME).partPrefab.Resources.list.Add(LifeSupport);;
                IFIDebug.IFIMess(" IFI Preload ++ ELect resource++");
            }
            catch (Exception ex) { IFIDebug.IFIMess("  IFI Preload Exception +addresource+" + ex.Message); }
        }
Example #6
0
        // create a new resource in a part
        public static void SetupResource(Part part, string resource_name, double amount, double max_amount)
        {
            PartResource res = part.gameObject.AddComponent <PartResource>();

            res.SetInfo(PartResourceLibrary.Instance.resourceDefinitions[resource_name]);
            res.amount    = amount;
            res.maxAmount = max_amount;
            res.flowMode  = PartResource.FlowMode.Both;
            res.flowState = true;
            res.part      = part;
            part.Resources.list.Add(res);
        }
        public static PartResource AddResource(this Part part, PartResourceDefinition info, float maxAmount, float amount)
        {
            PartResource resource = new PartResource(part);
            resource.SetInfo(info);
            resource.maxAmount = maxAmount;
            resource.amount = amount;
            resource.flowState = true;
            resource.isTweakable = info.isTweakable;
            resource.isVisible = info.isVisible;
            resource.hideFlow = false;
            resource.flowMode = PartResource.FlowMode.Both;
            part.Resources.dict.Add(info.name.GetHashCode(), resource);

            return resource;
        }
Example #8
0
        public static void AddResource(string resourceName, float amount, float maxAmount, Part part)
        {
            PartResourceDefinitionList definitions = PartResourceLibrary.Instance.resourceDefinitions;
            PartResource newResource = new PartResource();

            //First, does the resource definition exist?
            if (definitions.Contains(resourceName))
            {
                newResource.part = part;
                newResource.SetInfo(definitions[resourceName]);
                newResource.amount    = amount;
                newResource.maxAmount = maxAmount;

                part.Resources.list.Add(newResource);
            }
        }
Example #9
0
        private void addResourceFromConfig(Part evaPart, ConfigNode evaResourceNode)
        {
            string resourceName;

            if (evaResourceNode.TryGetValue("name", out resourceName))
            {
                this.LogDebug("Adding resource '{0}'", resourceName);

                PartResourceDefinition resourceInfo =
                    PartResourceLibrary.Instance.GetDefinition(resourceName);

                if (resourceInfo == null)
                {
                    this.LogWarning("Skipping resource {0}: definition not present in library.", resourceName);

                    return;
                }

                this.LogDebug("Resource '{0}' is in library.", resourceName);

                if (evaPart.GetComponents <PartResource>().Any(r => r.resourceName == resourceName))
                {
                    this.LogWarning("Skipping resource {0}: already present in kerbalEVA.", resourceName);

                    return;
                }

                this.LogDebug("Resource '{0}' is not present.", resourceName);

                PartResource resource = evaPart.gameObject.AddComponent <EVAPartResource>();

                this.LogDebug("Resource '{0}' component built.", resourceName);

                resource.SetInfo(resourceInfo);
                ((EVAPartResource)resource).Load(evaResourceNode);

                this.Log("Added resource {0} to {1}", resource.resourceName, evaPart);

                this.LogDebug("Resource '{0}' loaded.", resourceName);
            }
            else
            {
                this.Log("Skipping malformed EVA_RESOURCE node: missing 'name' field.");
                return;
            }
        }
        public static PartResource AddResource(this Part part, string name, double amt, double maxAmt)
        {
            var resDef = PartResourceLibrary.Instance.GetDefinition(name);

            PartResource partResource = new PartResource(part);

            partResource.resourceName = name;
            partResource.SetInfo(resDef);

            partResource.amount     = amt;
            partResource.maxAmount  = maxAmt;
            partResource._flowState = true;

            partResource.isTweakable = resDef.isTweakable;
            partResource.isVisible   = resDef.isVisible;
            partResource.hideFlow    = false;
            partResource._flowMode   = PartResource.FlowMode.Both;

            part.Resources.dict.Add(resDef.id, partResource);

            return(partResource);
        }
Example #11
0
        void AddTank(double value)
        {
            //Debug.LogWarning ("[MFT] Adding tank from API " + name + " amount: " + value);
            // The following is for unmanaged resource; if such a resource is defined then we probably shouldn't be here....
            ModuleFuelTanks.UnmanagedResource unmanagedResource = null;
            double unmanagedAmount    = 0;
            double unmanagedMaxAmount = 0;

            if (module != null && module.unmanagedResources != null)
            {
                module.unmanagedResources.TryGetValue(name, out unmanagedResource);
            }
            if (unmanagedResource != null)
            {
                unmanagedAmount    = unmanagedResource.amount;
                unmanagedMaxAmount = unmanagedResource.maxAmount;
            }

            var resDef = PartResourceLibrary.Instance.GetDefinition(name);
            var res    = new PartResource(part);

            res.resourceName = name;
            res.SetInfo(resDef);
            res.amount      = value + unmanagedAmount;
            res.maxAmount   = value + unmanagedMaxAmount;
            res._flowState  = true;
            res.isTweakable = resDef.isTweakable;
            res.isVisible   = resDef.isVisible;
            res.hideFlow    = false;
            res._flowMode   = PartResource.FlowMode.Both;
            part.Resources.dict.Add(resDef.id, res);
            //Debug.Log ($"[MFT] AddTank {res.resourceName} {res.amount} {res.maxAmount} {res.flowState} {res.isTweakable} {res.isVisible} {res.hideFlow} {res.flowMode}");

            ConfigNode node = new ConfigNode("RESOURCE");

            node.AddValue("name", name);
            node.AddValue("amount", value + unmanagedAmount);
            node.AddValue("maxAmount", value + unmanagedMaxAmount);
#if DEBUG
            MonoBehaviour.print(node.ToString());
#endif

            module.RaiseResourceListChanged();

            // Update symmetry counterparts.
            if (HighLogic.LoadedSceneIsEditor && propagate)
            {
                foreach (Part sym in part.symmetryCounterparts)
                {
                    sym.Resources.dict.Add(resDef.id, new PartResource(res));
                }
            }
            if (HighLogic.LoadedSceneIsEditor && propagate)
            {
                foreach (Part sym in part.symmetryCounterparts)
                {
                    sym.Resources.dict.Add(resDef.id, new PartResource(res));
                    RaiseResourceListChanged(sym);
                }
            }
        }
Example #12
0
        private void UpdateTankSetup(bool forceFull)
        {
            List <PartResource> partResources = part.Resources.list;

            int[] resourceIndices = Enumerable.Repeat <int>(-1, CurrentTankType.resources.Count).ToArray();
            bool  tmp             = false;

            for (int i = 0; i < partResources.Count; i++)
            {
                string resourceName = partResources[i].resourceName;
                tmp = false;

                for (int j = 0; j < CurrentTankType.resources.Count; j++)
                {
                    if (resourceName == CurrentTankType.resources[j].ResourceName)
                    {
                        resourceIndices[j] = i;
                        tmp = true;
                        break;
                    }
                }

                if (tmp)
                {
                    continue;
                }

                if (IsManagedResource(resourceName))
                {
                    DestroyImmediate(partResources[i]);
                    partResources.RemoveAt(i);
                    i--;
                }
            }

            for (int i = 0; i < CurrentTankType.resources.Count; i++)
            {
                TankResource resource       = CurrentTankType[i];
                float        resourceAmount = resource.unitsPerVolume * TankVolume;
                PartResource partResource   = null;
                if (resourceIndices[i] < 0)
                {
                    partResource = part.gameObject.AddComponent <PartResource>();
                    partResource.SetInfo(resource.resourceDefinition);
                    partResource.maxAmount   = resourceAmount;
                    partResource.amount      = resourceAmount;
                    partResource.flowState   = true;
                    partResource.isTweakable = resource.resourceDefinition.isTweakable;
                    partResource.hideFlow    = false;
                    partResource.flowMode    = PartResource.FlowMode.Both;
                    partResources.Add(partResource);
                }
                else
                {
                    partResource           = part.Resources[resourceIndices[i]];
                    partResource.maxAmount = resourceAmount;
                    if (forceFull)
                    {
                        partResource.amount = resourceAmount;
                    }
                    else
                    {
                        if (partResource.amount > resourceAmount)
                        {
                            partResource.amount = resourceAmount;
                        }
                    }
                }
            }

            part.Resources.UpdateList();
        }