Exemplo n.º 1
0
 protected void ChangeParts(Condition currentCondition)
 {
     foreach (ChangeInventoryPartsHandler handler in _handlers)
     {
         if (handler.TriggerCondition == currentCondition) //if it's the right condition, do the thing
         {
             foreach (IntermediateInventoryPart iip in handler.InventoryParts)
             {
                 foreach (InventoryPart ip in iip.ToInventoryParts())
                 {
                     if (handler.Adding)
                     {
                         ip.TrackerModule.TimesRecovered = Math.Max(0, ip.TrackerModule.TimesRecovered);
                         ScrapYard.ScrapYard.Instance.TheInventory.AddPart(ip);
                     }
                     else
                     {
                         ComparisonStrength compareStrength = ip.TrackerModule.TimesRecovered < 0 ? ComparisonStrength.MODULES : ComparisonStrength.TRACKER; //if negative, just care about modules. If 0+ then be strict
                         ScrapYard.ScrapYard.Instance.TheInventory.RemovePart(ip, compareStrength);
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Finds a part in the inventory for the given part
 /// </summary>
 /// <param name="part">The part to search for</param>
 /// <param name="strictness">The strictness to use when searching for the part. Defaults to MODULES.</param>
 /// <returns>A ConfigNode representing the InventoryPart, or null if none found.</returns>
 public static ConfigNode FindInventoryPart(ConfigNode part, ComparisonStrength strictness = ComparisonStrength.MODULES)
 {
     if (!Available)
     {
         return(null);
     }
     return(invokeMethod("FindInventoryPart_Node", part, strictness.ToString()) as ConfigNode);
 }
Exemplo n.º 3
0
 /// <summary>
 /// Removes a part from the Inventory using the given strictness for finding the part
 /// </summary>
 /// <param name="part">The part to remove</param>
 /// <param name="strictness">The strictenss to use when searching for the part. Defaults to MODULES</param>
 /// <returns>True if removed, false otherwise.</returns>
 public static bool RemovePartFromInventory(ConfigNode part, ComparisonStrength strictness = ComparisonStrength.MODULES)
 {
     if (!Available)
     {
         return(false);
     }
     return((bool)invokeMethod("RemovePartFromInventory_Node", part, strictness.ToString()));
 }
Exemplo n.º 4
0
        /// <summary>
        /// Finds an InventoryPart for a given part
        /// </summary>
        /// <param name="sourcePart">The part to search for</param>
        /// <param name="strictness">The strictness to use when searching for the part</param>
        /// <returns>The ConfigNode for the InventoryPart, or null if not found</returns>
        public ConfigNode FindInventoryPart_Node(ConfigNode sourcePart, string strictness)
        {
            if (!ScrapYard.Instance.TheInventory.InventoryEnabled)
            {
                return(null);
            }

            ComparisonStrength actualStrictness = parseStrictnessString(strictness);

            return(ScrapYard.Instance.TheInventory.FindPart(new InventoryPart(sourcePart), actualStrictness)?.State);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Removes a single part from the inventory.
        /// </summary>
        /// <param name="sourcePart">The part to remove.</param>
        /// <param name="strictness">The strictness to use when searching for the appropriate part.</param>
        /// <returns>True if removed, false otherwise.</returns>
        public bool RemovePartFromInventory_Node(ConfigNode sourcePart, string strictness)
        {
            if (!ScrapYard.Instance.TheInventory.InventoryEnabled)
            {
                return(false);
            }

            ComparisonStrength actualStrictness = parseStrictnessString(strictness);

            return(ScrapYard.Instance.TheInventory.RemovePart(new InventoryPart(sourcePart), actualStrictness) != null);
        }
Exemplo n.º 6
0
 private ComparisonStrength parseStrictnessString(string strictness)
 {
     try
     {
         ComparisonStrength actualStrictness = (ComparisonStrength)Enum.Parse(typeof(ComparisonStrength), strictness);
         return(actualStrictness);
     }
     catch
     {
         return(ComparisonStrength.MODULES);
     }
 }
Exemplo n.º 7
0
 /// <summary>
 /// Finds a part in the inventory for the given InventoryPart and a strictness of comparison
 /// </summary>
 /// <param name="part">The source part to find a match for</param>
 /// <param name="strength">The strictness of the comparison. Defaults to MODULES.</param>
 /// <returns>The InventoryPart or null if not found.</returns>
 public InventoryPart FindPart(InventoryPart part, ComparisonStrength strength = ComparisonStrength.MODULES)
 {
     if (!InventoryEnabled)
     {
         return(null);
     }
     if (strength == ComparisonStrength.STRICT)
     {
         InventoryPart found = internalInventory.FirstOrDefault(ip => ip == part);
         if (found != null)
         {
             return(found);
         }
     }
     return(internalInventory.FirstOrDefault(ip => ip.IsSameAs(part, strength)));
 }
Exemplo n.º 8
0
        /// <summary>
        /// Removes a part from the inventory given an InventoryPart to compare and the strictness of comparison
        /// </summary>
        /// <param name="part">The source part to find a match for</param>
        /// <param name="strength">The strictness of the comparison. Defaults to MODULES</param>
        /// <returns>The removed InventoryPart, or null if none found</returns>
        public InventoryPart RemovePart(InventoryPart part, ComparisonStrength strength = ComparisonStrength.MODULES)
        {
            if (!InventoryEnabled)
            {
                return(null);
            }
            InventoryPart found = FindPart(part, strength);

            if (found != null && internalInventory.Remove(found))
            {
                if (!disableEvents)
                {
                    ScrapYardEvents.OnSYInventoryChanged.Fire(found, false);
                }
                return(found);
            }
            return(null);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Takes a List of part ConfigNodes and returns the ConfigNodes that are present in the inventory.
        /// Assumes the default strictness.
        /// </summary>
        /// <param name="sourceParts">Source list of parts</param>
        /// <returns>List of part ConfigNodes that are in the inventory</returns>
        public IList <ConfigNode> GetPartsInInventory_ConfigNodes(IEnumerable <ConfigNode> sourceParts, string strictness)
        {
            if (!ScrapYard.Instance.TheInventory.InventoryEnabled)
            {
                return(new List <ConfigNode>());
            }
            ComparisonStrength actualStrictness = parseStrictnessString(strictness);
            List <ConfigNode>  inInventory      = new List <ConfigNode>();
            PartInventory      InventoryCopy    = ScrapYard.Instance.TheInventory.Copy();

            foreach (ConfigNode part in sourceParts)
            {
                InventoryPart inputPart = new InventoryPart(part);
                if (InventoryCopy.RemovePart(inputPart, actualStrictness) != null)
                {
                    inInventory.Add(part);
                }
            }
            return(inInventory);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Finds all parts in the inventory for the given InventoryPart and the provided strictness
        /// </summary>
        /// <param name="part">The source part to find a match for</param>
        /// <param name="strength">The strictness of the comparison. Defaults to MODULES.</param>
        /// <returns>An IEnumerable of InventoryParts that match</returns>
        public IEnumerable <InventoryPart> FindParts(InventoryPart part, ComparisonStrength strength = ComparisonStrength.MODULES)
        {
            if (!InventoryEnabled)
            {
                return(null);
            }

            List <InventoryPart> foundParts = new List <InventoryPart>();
            PartInventory        copy       = Copy();
            InventoryPart        found      = null;

            do
            {
                found = copy.RemovePart(part, strength);
                if (found != null)
                {
                    foundParts.Add(found);
                }
            } while (found != null);

            return(foundParts);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Checks to see if the passed InventoryPart is identical to this one, for a given strictness of "identical"
        /// </summary>
        /// <param name="comparedPart">The part to compare to</param>
        /// <param name="strictness">The strength of the comparison (just name? modules? everything?)</param>
        /// <returns>True if mathing, false otherwise</returns>
        public bool IsSameAs(InventoryPart comparedPart, ComparisonStrength strictness)
        {
            //Test that the name is the same
            if (Name != comparedPart.Name)
            {
                return(false);
            }
            if (strictness == ComparisonStrength.NAME) //If we're just comparing name then we're done
            {
                return(true);
            }

            //Verify the costs are within 1 funds
            if (Math.Abs(DryCost - comparedPart.DryCost) > 1.0)
            {
                return(false);
            }
            if (strictness == ComparisonStrength.COSTS)
            {
                return(true);
            }

            if (strictness == ComparisonStrength.STRICT) //Strict comparison, the ids must be the same
            {                                            //Compare IDs now so we can avoid the full module comparison if they don't have the same ID
                if (comparedPart.ID != ID)
                {
                    return(false);
                }
            }

            //Test to ensure the number of saved modules are identical
            if (savedModules.Count == comparedPart.savedModules.Count)
            {
                //Compare the saved modules to ensure they are identical
                for (int index = 0; index < savedModules.Count; ++index)
                {
                    if (!savedModules[index].IsIdenticalTo(comparedPart.savedModules[index]))
                    {
                        return(false);
                    }
                }
                //If everything has passed, they are considered equal
            }
            else
            {
                return(false);
            }
            if (strictness == ComparisonStrength.MODULES)
            {
                return(true);
            }

            //Tracker comparison, the times used must match
            if (TrackerModule.TimesRecovered != comparedPart.TrackerModule.TimesRecovered)
            {
                return(false);
            }
            if (TrackerModule.Inventoried != comparedPart.TrackerModule.Inventoried)
            {
                return(false);
            }
            if (strictness == ComparisonStrength.TRACKER)
            {
                return(true);
            }

            //Everything must match, they are the same
            return(true);
        }
Exemplo n.º 12
0
 /// <summary>
 /// Takes a List of Parts and returns the Parts that are present in the inventory.
 /// </summary>
 /// <param name="sourceParts">Source list of parts</param>
 /// <param name="strictness">How strict of a comparison to use. Defaults to MODULES</param>
 /// <returns>List of Parts that are in the inventory</returns>
 public static IList <Part> GetPartsInInventory(IEnumerable <Part> sourceParts, ComparisonStrength strictness = ComparisonStrength.MODULES)
 {
     if (!Available)
     {
         return(null);
     }
     return((IList <Part>)invokeMethod("GetPartsInInventory_Parts", sourceParts, strictness.ToString()));
     //Why do a ToString on an enum instead of casting to int? Because if the internal enum changes then the intended strictness is kept.
 }