Example #1
0
 /// <summary>
 /// Other mods need to call this to start processing a ProtoVessel.
 /// Will write this into an API in future version.
 /// </summary>
 /// <param name="vessel"></param>
 public void AddInterestedVessel(ProtoVessel vessel)
 {
     if (!InterestedVessels.Contains(vessel))
     {
         InterestedVessel iVessel = new InterestedVessel(vessel.vesselRef, vessel);
         InterestedVessels.Add(vessel, iVessel);
         CacheResources.CreatecachedVesselResources(vessel);
     }
 }
 /// <summary>
 /// Looks for a CachedResources entry for the passed in ProtoVessel. If one doesn't exist it will create one.
 /// Then it will search the CachedResources for the ProtoVessel for the passed in resourceName and return the amount and maxAmount available
 /// on the ProtoVessel.
 /// </summary>
 /// <param name="vessel"></param>
 /// <param name="resourceName"></param>
 /// <param name="amount"></param>
 /// <param name="maxAmount"></param>
 public static void GetResourceTotals(ProtoVessel vessel, string resourceName, out double amount, out double maxAmount)
 {
     amount    = 0d;
     maxAmount = 0d;
     if (UnloadedResources.InterestedVessels == null)
     {
         UnloadedResources.InterestedVessels = new DictionaryValueList <ProtoVessel, InterestedVessel>();
     }
     if (!UnloadedResources.InterestedVessels.Contains(vessel))
     {
         CacheResources.CreatecachedVesselResources(vessel);
     }
     //If there are no cachedResources for the vessel create one.
     if (UnloadedResources.InterestedVessels[vessel].CachedResources == null)
     {
         CacheResources.CreatecachedVesselResources(vessel);
     }
     //If BackgroundProcessing is installed and our cache hasn't been refreshed in 3 mins. Then refresh it.
     if (UnloadedResources.BackgroundProcessingInstalled &&
         (Time.time - UnloadedResources.InterestedVessels[vessel].TimeLastRefresh > 180))
     {
         CacheResources.CreatecachedVesselResources(vessel);
     }
     //Double check, not really necessary. Now find the resource amounts if in the vessel.
     if (UnloadedResources.InterestedVessels.Contains(vessel))
     {
         List <CacheResources.CacheResource> vslresources = UnloadedResources.InterestedVessels[vessel].CachedResources;
         for (int i = 0; i < vslresources.Count; i++)
         {
             if (vslresources[i].resourceName == resourceName)
             {
                 amount    = vslresources[i].amount;
                 maxAmount = vslresources[i].maxAmount;
                 if (vslresources[i].timeWarpOverflow.totalAmount > 0 && TimeWarp.fetch != null && TimeWarp.CurrentRateIndex > CacheResources.timeWarpStep) //If we have timewarp Overflow check that first.
                 {
                     amount    += vslresources[i].timeWarpOverflow.totalAmount;
                     maxAmount += vslresources[i].timeWarpOverflow.totalAmount;
                 }
             }
         }
     }
 }
        /// <summary>
        /// Request Resource processing on a ProtoVessel.
        /// If the ProtoVessel is not known or resources not cached for the ProtoVessel will automatically add them to the Cached data.
        /// </summary>
        /// <param name="vessel">ProtoVessel reference</param>
        /// <param name="resourceName">Name of the Resource we want to process</param>
        /// <param name="amount">The amount of the resource we want to process</param>
        /// <param name="amountReceived">returns the amount processed for the request in this variable</param>
        /// <param name="pushing">default of false (which means take resource). If true will push (put resource)</param>
        public static void RequestResource(ProtoVessel vessel, string resourceName, double amount, out double amountReceived, bool pushing = false)
        {
            amountReceived = 0d;
            if (UnloadedResources.InterestedVessels == null)
            {
                UnloadedResources.InterestedVessels = new DictionaryValueList <ProtoVessel, InterestedVessel>();
            }
            //If there are no cachedResources for the vessel create one.
            if (!UnloadedResources.InterestedVessels.Contains(vessel))
            {
                CacheResources.CreatecachedVesselResources(vessel);
            }

            //Double check, not really necessary. Now find the resource amounts if in the vessel.
            if (UnloadedResources.InterestedVessels.Contains(vessel))
            {
                List <CacheResources.CacheResource> vslresources = UnloadedResources.InterestedVessels[vessel].CachedResources;
                for (int i = 0; i < vslresources.Count; i++)
                {
                    CacheResources.CacheResource cacheResource = vslresources[i];
                    if (cacheResource.resourceName == resourceName)
                    {
                        if (!pushing)  //We are taking resource
                        {
                            if (cacheResource.amount > 0 || cacheResource.timeWarpOverflow.totalAmount > 0)
                            {
                                if (cacheResource.timeWarpOverflow.totalAmount > 0 && TimeWarp.fetch != null && TimeWarp.CurrentRateIndex > CacheResources.timeWarpStep) //If we have timewarp Overflow check that first.
                                {
                                    double amountTaken = 0;
                                    cacheResource.timeWarpOverflow.Take(amount, out amountTaken);
                                    amountReceived += amountTaken;
                                    amount         -= amountTaken;
                                    if (amount <= 0) //Did we get all we need already? If so return.
                                    {
                                        return;
                                    }
                                }
                                //TimewarpOverflow didn't have enough or didn't have what we need. so now the partResrouceSnapshot
                                Dictionary <string, ProtoPartResourceSnapshot> .Enumerator ppRSenumerator = cacheResource.protoPartResourceSnapshot.GetDictEnumerator();
                                while (ppRSenumerator.MoveNext())
                                {
                                    ProtoPartResourceSnapshot partResourceSnapshot = ppRSenumerator.Current.Value;

                                    if (partResourceSnapshot.amount > 0)
                                    {
                                        if (partResourceSnapshot.amount <= amount) //Not enough but take what it has
                                        {
                                            amountReceived             += partResourceSnapshot.amount;
                                            amount                     -= partResourceSnapshot.amount;
                                            cacheResource.amount       -= partResourceSnapshot.amount;
                                            partResourceSnapshot.amount = 0;
                                        }
                                        else //this part has more than we need.
                                        {
                                            amountReceived              += amount;
                                            cacheResource.amount        -= amount;
                                            partResourceSnapshot.amount -= amount;
                                            amount = 0;
                                        }
                                    }
                                    if (amount <= 0) //Did we get all we wanted? if so return.
                                    {
                                        ppRSenumerator.Dispose();
                                        return;
                                    }
                                }
                                ppRSenumerator.Dispose();
                            }
                        }
                        else  //We are putting a resource
                        {
                            //Get how much space there is in this part.
                            double spaceAvailable = cacheResource.maxAmount - cacheResource.amount;
                            if (spaceAvailable > 0) //If we have space put some in.
                            {
                                Dictionary <string, ProtoPartResourceSnapshot> .Enumerator ppRSenumerator = cacheResource.protoPartResourceSnapshot.GetDictEnumerator();
                                while (ppRSenumerator.MoveNext())
                                {
                                    ProtoPartResourceSnapshot partResourceSnapshot = ppRSenumerator.Current.Value;
                                    double partspaceAvailable = partResourceSnapshot.maxAmount - partResourceSnapshot.amount;
                                    if (partspaceAvailable > 0)
                                    {
                                        if (amount > partspaceAvailable) //If we can't fit it all in this part. Put what we can.
                                        {
                                            partResourceSnapshot.amount = partResourceSnapshot.maxAmount;
                                            cacheResource.amount       += partspaceAvailable;
                                            amount         -= partspaceAvailable;
                                            amountReceived += partspaceAvailable;
                                        }
                                        else //If we can fit it all in this part, put it in.
                                        {
                                            partResourceSnapshot.amount += amount;
                                            cacheResource.amount        += amount;
                                            amountReceived += amount;
                                            amount          = 0;
                                        }
                                        if (amount <= 0) //Did we get all we wanted? if so return.
                                        {
                                            return;
                                        }
                                    }
                                }
                            }
                            //If we get here we had more than can fit in the parts... But if TimeWarp is too high, we put it in the overflow.
                            if (TimeWarp.fetch != null && amount > 0)
                            {
                                if (TimeWarp.CurrentRateIndex > CacheResources.timeWarpStep) //But only if timewarp rate is high enough.
                                {
                                    cacheResource.timeWarpOverflow.Add(amount);
                                    amountReceived += amount;
                                    amount          = 0;
                                    return;
                                }
                            }
                        }
                    }
                } //End For loop all vessel resources.
            }
        }