public void RemovePart(Part part)
        {
            parts.Remove(part);
            var remove_list = new List <string> ();

            foreach (var resinfo in resources)
            {
                string         resource     = resinfo.Key;
                RMResourceInfo resourceInfo = resinfo.Value;
                for (int i = resourceInfo.containers.Count - 1; i >= 0; i--)
                {
                    var container = resourceInfo.containers[i];
                    if (container.part == part)
                    {
                        resourceInfo.containers.Remove(container);
                    }
                }
                if (resourceInfo.containers.Count == 0)
                {
                    remove_list.Add(resource);
                }
            }
            foreach (string resource in remove_list)
            {
                resources.Remove(resource);
            }
        }
        public void RemoveSet(RMResourceSet set)
        {
            sets.Remove(set);
            var remove_list = new List <string> ();

            foreach (var resinfo in resources)
            {
                string         resource     = resinfo.Key;
                RMResourceInfo resourceInfo = resinfo.Value;
                for (int i = resourceInfo.containers.Count - 1; i >= 0; i--)
                {
                    var container = resourceInfo.containers[i] as ResourceSetContainer;
                    if (container != null && container.set == set)
                    {
                        resourceInfo.containers.Remove(container);
                    }
                }
                if (resourceInfo.containers.Count == 0)
                {
                    remove_list.Add(resource);
                }
            }
            foreach (string resource in remove_list)
            {
                resources.Remove(resource);
            }
        }
 public void SetFlowState(string res, bool state)
 {
     if (resources.ContainsKey(res))
     {
         RMResourceInfo info = resources[res];
         for (int i = info.containers.Count; i-- > 0;)
         {
             info.containers[i].flowState = state;
         }
     }
 }
 public void AddSet(RMResourceSet set)
 {
     sets.Add(set);
     foreach (var resource in set.resources.Keys)
     {
         RMResourceInfo resourceInfo;
         if (!resources.ContainsKey(resource))
         {
             resourceInfo        = new RMResourceInfo();
             resources[resource] = resourceInfo;
         }
         resourceInfo = resources[resource];
         resourceInfo.containers.Add(new ResourceSetContainer(resource, set));
     }
 }
 public bool GetFlowState(string res)
 {
     if (resources.ContainsKey(res))
     {
         RMResourceInfo info = resources[res];
         for (int i = info.containers.Count; i-- > 0;)
         {
             if (info.containers[i].flowState)
             {
                 return(true);
             }
         }
     }
     return(false);
 }
        // Return the vessel's total available amount of the resource.
        // If the vessel has no such resource 0.0 is returned.
        public double ResourceAmount(string resource)
        {
            if (!resources.ContainsKey(resource))
            {
                return(0.0);
            }
            RMResourceInfo resourceInfo = resources[resource];
            double         amount       = 0.0;

            foreach (var container in resourceInfo.containers)
            {
                amount += container.amount;
            }
            return(amount);
        }
        // Return the vessel's total capacity for the resource.
        // If the vessel has no such resource 0.0 is returned.
        public double ResourceCapacity(string resource)
        {
            if (!resources.ContainsKey(resource))
            {
                return(0.0);
            }
            RMResourceInfo resourceInfo = resources[resource];
            double         capacity     = 0.0;

            foreach (var container in resourceInfo.containers)
            {
                capacity += container.maxAmount;
            }
            return(capacity);
        }
 // Completely empty the vessel of any and all resources.
 // However, if resources_to_remove is not null, only those resources
 // specified will be removed.
 public void RemoveAllResources(HashSet <string> resources_to_remove = null)
 {
     foreach (KeyValuePair <string, RMResourceInfo> pair in resources)
     {
         string resource = pair.Key;
         if (resources_to_remove != null && !resources_to_remove.Contains(resource))
         {
             continue;
         }
         RMResourceInfo resourceInfo = pair.Value;
         foreach (var container in resourceInfo.containers)
         {
             container.amount = 0.0;
         }
     }
 }
        public double ResourceMass()
        {
            double mass = 0;

            foreach (KeyValuePair <string, RMResourceInfo> pair in resources)
            {
                string         resource     = pair.Key;
                var            def          = PartResourceLibrary.Instance.GetDefinition(resource);
                float          density      = def.density;
                RMResourceInfo resourceInfo = pair.Value;
                foreach (var container in resourceInfo.containers)
                {
                    mass += density * container.amount;
                }
            }
            return(mass);
        }
        // Transfer a resource into (positive amount) or out of (negative
        // amount) the vessel. No attempt is made to balance the resource
        // across parts: they are filled/emptied on a first-come-first-served
        // basis.
        // If the vessel has no such resource no action is taken.
        // Returns the amount of resource not transfered (0 = all has been
        // transfered).
        public double TransferResource(string resource, double amount)
        {
            if (!resources.ContainsKey(resource))
            {
                return(amount);
            }
            RMResourceInfo resourceInfo = resources[resource];

            if (balanced)
            {
                return(BalancedTransfer(resourceInfo, amount));
            }
            else
            {
                return(UnbalancedTransfer(resourceInfo, amount));
            }
        }
 public void AddPart(Part part)
 {
     if (part.Resources.Count > 0)
     {
         parts.Add(part);
     }
     foreach (PartResource resource in part.Resources)
     {
         RMResourceInfo resourceInfo;
         if (!resources.ContainsKey(resource.resourceName))
         {
             resourceInfo = new RMResourceInfo();
             resources[resource.resourceName] = resourceInfo;
         }
         resourceInfo = resources[resource.resourceName];
         resourceInfo.containers.Add(new PartResourceContainer(resource));
     }
 }
Esempio n. 12
0
        public static void ProcessPart(Part part, Dictionary <string, RMResourceInfo> resources, double massOffset)
        {
            string name = GetPartName(part);

            if (name.Contains("kerbalEVA"))
            {
                // kerbalEVA parts have the name of the kerbal appended to the
                // part name.
                name = "kerbalEVA";
            }
            if (!part_recipes.ContainsKey(name))
            {
                print("ELRecipeDatabase.ProcessPart: no part recipe for " + name);
                return;
            }
            var recipe = part_recipes[name].Bake(part.mass - massOffset);

            for (int i = 0; i < recipe.ingredients.Count; i++)
            {
                var ingredient = recipe.ingredients[i];
                if (!ingredient.isReal)
                {
                    //print ("fake ingredient: " + ingredient.name);
                    continue;
                }
                double density = ResourceDensity(ingredient.name);
                //double ratio = ingredient.ratio;
                if (density > 0)
                {
                    ingredient.ratio /= density;
                }
                //Debug.Log($"ProcessPart: {ingredient.name} {ratio} {density} {ingredient.ratio}");

                RMResourceInfo resourceInfo;
                if (!resources.ContainsKey(ingredient.name))
                {
                    resourceInfo = new RMResourceInfo();
                    resources[ingredient.name] = resourceInfo;
                }
                resourceInfo = resources[ingredient.name];
                resourceInfo.containers.Add(new RecipeResourceContainer(part, ingredient));
            }
        }
 double UnbalancedTransfer(RMResourceInfo resourceInfo, double amount)
 {
     foreach (var container in resourceInfo.containers)
     {
         double adjust = amount;
         double space  = container.maxAmount - container.amount;
         if (adjust < 0 && -adjust > container.amount)
         {
             // Ensure the resource amount never goes negative
             adjust = -container.amount;
         }
         else if (adjust > 0 && adjust > space)
         {
             // ensure the resource amount never excees the maximum
             adjust = space;
         }
         container.amount += adjust;
         amount           -= adjust;
     }
     return(amount);
 }
        double BalancedTransfer(RMResourceInfo resourceInfo, double amount)
        {
            double setTotal = 0;

            if (amount < 0)
            {
                for (int i = 0; i < resourceInfo.containers.Count; i++)
                {
                    var    container = resourceInfo.containers[i];
                    double avail     = container.amount;
                    setTotal += avail;
                }
            }
            else
            {
                for (int i = 0; i < resourceInfo.containers.Count; i++)
                {
                    var    container = resourceInfo.containers[i];
                    double space     = container.maxAmount - container.amount;
                    setTotal += space;
                }
            }
            double adjust = amount;

            if (adjust < 0 && -adjust > setTotal)
            {
                // Ensure the resource amount never goes negative
                adjust = -setTotal;
            }
            else if (adjust > 0 && adjust > setTotal)
            {
                // ensure the resource amount never excees the maximum
                adjust = setTotal;
            }
            amount -= adjust;
            if (setTotal > 0)
            {
                if (adjust < 0)
                {
                    for (int i = 0; i < resourceInfo.containers.Count; i++)
                    {
                        var    container = resourceInfo.containers[i];
                        double avail     = container.amount;
                        double adj       = avail * adjust / setTotal;
                        if (-adj > avail)
                        {
                            adj = -avail;
                        }
                        container.amount += adj;
                    }
                }
                else
                {
                    for (int i = 0; i < resourceInfo.containers.Count; i++)
                    {
                        var    container = resourceInfo.containers[i];
                        double space     = container.maxAmount - container.amount;
                        double adj       = space * adjust / setTotal;
                        if (adj > space)
                        {
                            adj = space;
                        }
                        container.amount += adj;
                    }
                }
            }
            return(amount);
        }