From 7a26f0e8b2777a3eea51e096ab43becfb1a80974 Mon Sep 17 00:00:00 2001 From: steveman0 Date: Wed, 15 Jun 2016 23:17:20 -0700 Subject: [PATCH 1/6] Item List bug fixes Fixed bugs discovered through implementation in my Stockpiler multi-item support update. Added null reference checks for robustness. --- .../Collections/ItemBaseCollectionUtils.cs | 268 +++++++++++++++++- 1 file changed, 266 insertions(+), 2 deletions(-) diff --git a/Utilities/Collections/ItemBaseCollectionUtils.cs b/Utilities/Collections/ItemBaseCollectionUtils.cs index 97faa1c..1d601ed 100644 --- a/Utilities/Collections/ItemBaseCollectionUtils.cs +++ b/Utilities/Collections/ItemBaseCollectionUtils.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using UnityEngine; namespace FortressCraft.Community.Utilities { @@ -61,6 +62,269 @@ public static Int32 GetItemCount(this IEnumerable items, ItemBase rest var cube = restraints.As(); return items.GetItemCount(cube.mCubeType, cube.mCubeValue); - } - } + } + + + /// + /// Adds an ItemBase to a list, consolidating stacks - obeys list storage capacity by returning excess items + /// + /// The item to add to the list + /// The list to transfer it to + /// If true returns partial stack when insufficient stack size found + /// The list's storage capacity + /// The remaining items that do not fit in the list + public static ItemBase AddListItem(this ItemBase item, ref List targetlist, bool returnpartialstack, int storagecapacity = int.MaxValue) + { + if (item == null) + { + Debug.LogError("AddListItem attempted to add a null item!"); + return null; + } + List workingtarget = targetlist.ToList(); + int remainder = 0; + int listcount = workingtarget.GetItemCount(); + int itemcount = item.GetAmount(); + int newtotal = listcount + itemcount; + ItemBase returnitem = null; + if (newtotal > storagecapacity) + { + remainder = newtotal - storagecapacity; + itemcount -= remainder; + item.SetAmount(itemcount); + returnitem = NewInstance(item); + returnitem.SetAmount(remainder); + } + int listlength = workingtarget.Count; + for (int index = 0; index <= listlength; index++) + { + if (index != listlength) + { + if (workingtarget[index].IsStackAndSame(item)) + { + workingtarget[index].IncrementStack(itemcount); + item = null; + break; + } + } + else + { + workingtarget.Add(NewInstance(item)); + item = null; + break; + } + } + targetlist = workingtarget; + return returnitem; + } + + /// + /// Deduct an item from an item list by example + /// + /// The example item to attempt to remove from the list + /// The list to take the item from + /// If true returns partial stack when insufficient stack size found + /// The ItemBase object or null if unavailable + public static ItemBase RemoveListItem(this ItemBase item, ref List sourcelist, bool returnpartialstack) + { + if (item == null) + { + Debug.LogError("RemoveListItem attempted to remove a null item from a list"); + return null; + } + List workingsource = sourcelist.ToList(); + if (item.IsStack()) + { + int itemcount = item.GetAmount(); + int listitemcount; + + for (int index = 0; index < workingsource.Count; index++) + { + ItemBase i = workingsource[index]; + listitemcount = i.GetAmount(); + if (i.Compare(item) && listitemcount > itemcount) + { + i.DecrementStack(itemcount); + sourcelist = workingsource; + return NewInstance(item); + } + else if (i.Compare(item) && listitemcount == itemcount) + { + workingsource.Remove(i); + sourcelist = workingsource; + return NewInstance(item); + } + else if (returnpartialstack) + { + item.SetAmount(listitemcount); + sourcelist = workingsource; + return NewInstance(item); + } + } + } + else + for (int index = 0; index < workingsource.Count; index++) + { + ItemBase i = workingsource[index]; + if (i.Compare(item)) + { + workingsource.Remove(i); + sourcelist = workingsource; + return i; + } + } + sourcelist = workingsource; + return null; + } + + /// + /// Moves specified amount of any items from one list to another obeying target storage capacity. + /// + /// Source list of items + /// Target list of items + /// Quantity of items to move + /// Storage capacity of target item list + /// Moves only the first item found + /// A list of items to server as a whitelist or blacklist for transferring + /// True if whitelist otherwise treat as a blacklist + public static void MoveItems(ref List sourcelist, ref List targetlist, int amount = 1, int StorageCapacity = int.MaxValue, bool takefirstitem = false, IEnumerable whiteblacklist = null, bool iswhitelist = true) + { + int listcount; + int freespace; + List workingsource = sourcelist.ToList(); + List workingtarget = targetlist.ToList(); + List filter = new List(); + if (whiteblacklist != null) + filter = whiteblacklist.ToList(); + int filtercount = filter.Count; + bool matchfound = false; + + if (amount <= 0) + { + return; + } + for (int index = 0; index < workingsource.Count; index++) + { + listcount = workingsource[index].GetAmount(); + freespace = StorageCapacity - workingtarget.GetItemCount(); + ItemBase i = workingsource[index]; + + if (filtercount != 0) + { + for (int index2 = 0; index2 < filtercount; index2++) + { + ItemBase j = filter[index2]; + if (j == null) + { + Debug.LogError("MoveItems attempted to filter by null item"); + return; + } + matchfound = (i.Compare(j)); + if (matchfound) + break; + } + //XOR to skip the continue otherwise white/black list violation so go to next item + if (matchfound ^ iswhitelist) + continue; + } + + if (i.IsStack()) + { + if (listcount > amount) + { + if (amount > freespace) + { + AddListItem(NewInstance(i).SetAmount(freespace), ref workingtarget, false); + i.DecrementStack(freespace); + sourcelist = workingsource; + targetlist = workingtarget; + return; + } + else + { + AddListItem(NewInstance(i).SetAmount(amount), ref workingtarget, false); + i.DecrementStack(amount); + sourcelist = workingsource; + targetlist = workingtarget; + return; + } + } + else if (listcount == amount) + { + if (amount > freespace) + { + AddListItem(NewInstance(i).SetAmount(freespace), ref workingtarget, false); + workingsource.Remove(i); + sourcelist = workingsource; + targetlist = workingtarget; + return; + } + else + { + AddListItem(NewInstance(i).SetAmount(amount), ref workingtarget, false); + workingsource.Remove(i); + sourcelist = workingsource; + targetlist = workingtarget; + return; + } + } + else + { + if (listcount > freespace) + { + AddListItem(NewInstance(i).SetAmount(freespace), ref workingtarget, false); + workingsource.Remove(i); + amount -= listcount; + } + else + { + AddListItem(NewInstance(i).SetAmount(listcount), ref workingtarget, false); + workingsource.Remove(i); + amount -= listcount; + } + } + } + else + { + if (freespace > 0) + { + AddListItem(NewInstance(i), ref workingtarget, false); + workingsource.Remove(i); + amount--; + } + else + { + sourcelist = workingsource; + targetlist = workingtarget; + return; + } + } + if (takefirstitem) + break; + } + sourcelist = workingsource; + targetlist = workingtarget; + } + + /// + /// Moves specified amount of any items from one list to another obeying target storage capacity. + /// + /// Source item to move + /// Target list of items + /// Quantity of items to move (for stacks) + /// Storage capacity of target item list + /// Moves only the first item found + /// A list of items to server as a whitelist or blacklist for transferring + /// True if whitelist otherwise treat as a blacklist + public static void MoveItems(this ItemBase sourceitem, ref List targetlist, int amount = 1, int StorageCapacity = int.MaxValue, bool takefirstitem = false, IEnumerable whiteblacklist = null, bool iswhitelist = true) + { + if (sourceitem == null) + { + Debug.LogError("MoveItems attempted to move a null item!"); + return; + } + List sourcelist = new List(); + sourcelist.Add(sourceitem); + MoveItems(ref sourcelist, ref targetlist, amount, StorageCapacity, takefirstitem, whiteblacklist, iswhitelist); + } + } } From cf922332850537f496acfc2b5b75af60385cd7e8 Mon Sep 17 00:00:00 2001 From: steveman0 Date: Wed, 15 Jun 2016 23:19:36 -0700 Subject: [PATCH 2/6] MachineInventory finalized Implemented safe read by null checking deserialized items. Added numerous logic functions for the inventory. Added a drop item function that can be easily called when the machine is deleted. --- Utilities/MachineInventory.cs | 363 ++++++++++++++++++++++++++++++++++ 1 file changed, 363 insertions(+) create mode 100644 Utilities/MachineInventory.cs diff --git a/Utilities/MachineInventory.cs b/Utilities/MachineInventory.cs new file mode 100644 index 0000000..2fdc0ae --- /dev/null +++ b/Utilities/MachineInventory.cs @@ -0,0 +1,363 @@ +using System.Collections.Generic; +using System.IO; +using UnityEngine; + +namespace FortressCraft.Community.Utilities +{ + /// + /// Generic machine inventory class with list inventory support + /// + /// Origianl code by steveman0 + public class MachineInventory + { + /// + /// For associating the machine owner of the inventory + /// + public MachineEntity Machine; + + /// + /// The total item capacity of the storage + /// + public int StorageCapacity; + + /// + /// The list of items in the inventory + /// + public List Inventory; + + /// + /// Generic machine inventory class with list inventory support + /// + /// For associating the owner machine + /// The storage capacity of the inventory + public MachineInventory(MachineEntity machineentity, int storagecapacity) + { + this.Machine = machineentity; + this.StorageCapacity = storagecapacity; + this.Inventory = new List(); + } + + /// + /// Add a single item type to the inventory + /// + /// The item to add + /// Amount of items added if given a stack + /// Returns the remainder that doesn't fit or null if successful + public ItemBase AddItem(ItemBase item, int amount = 1) + { + return ItemBaseUtil.AddListItem(item, ref this.Inventory, true, this.StorageCapacity); + } + + /// + /// Add items from a source inventory or item list + /// + /// The source inventory or list of items + /// The number of items to transfer + public void AddItem(ref List items, int amount = 1) + { + ItemBaseUtil.MoveItems(ref items, ref this.Inventory, amount, this.StorageCapacity, false); + } + + /// + /// Transfers items to machine inventory if they are on the provided whitelist + /// + /// Source inventory or list of items + /// List of items types allowed in the transfer + /// Number of items to add + public void AddWhiteList(ref List items, IEnumerable whitelist, int amount = 1) + { + ItemBaseUtil.MoveItems(ref items, ref this.Inventory, amount, this.StorageCapacity, false, whitelist, true); + } + + /// + /// Transfers items to machine inventory if they are on the provided whitelist + /// + /// Source inventory or list of items + /// Item type allowed in the transfer + /// Number of items to add + public void AddWhiteList(ref List items, ItemBase whitelist, int amount = 1) + { + ItemBase[] WhiteList = new[] { whitelist }; + ItemBaseUtil.MoveItems(ref items, ref this.Inventory, amount, this.StorageCapacity, false, WhiteList, true); + } + + /// + /// Transfers items to machine inventory if they are not on the provided blacklist + /// + /// Source inventory or list of items + /// List of items forbidden from transfer + /// Number of items to add + public void AddBlackList(ref List items, IEnumerable blacklist, int amount = 1) + { + ItemBaseUtil.MoveItems(ref items, ref this.Inventory, amount, this.StorageCapacity, false, blacklist, false); + } + + + + /// + /// Transfers items to machine inventory if they are not on the provided blacklist + /// + /// Source inventory or list of items + /// Item forbidden from transfer + /// Number of items to add + public void AddBlackList(ref List items, ItemBase blacklist, int amount = 1) + { + ItemBase[] BlackList = new[] { blacklist }; + ItemBaseUtil.MoveItems(ref items, ref this.Inventory, amount, this.StorageCapacity, false, BlackList, false); + } + + /// + /// Fills the inventory to capacity with source items + /// + /// Source items to fill the inventory + public void Fill(ref List items) + { + ItemBaseUtil.MoveItems(ref items, ref this.Inventory, this.SpareCapacity(), this.StorageCapacity); + } + + /// + /// Fills the inventory to capacity with source items + /// + /// Source items to fill the inventory + /// Item type allowed in the transfer + public void FillWhiteList(ref List items, IEnumerable whitelist) + { + ItemBaseUtil.MoveItems(ref items, ref this.Inventory, this.SpareCapacity(), this.StorageCapacity, false, whitelist, true); + } + + /// + /// Fills the inventory to capacity with source items + /// + /// Source items to fill the inventory + /// Item type allowed in the transfer + public void FillWhiteList(ref List items, ItemBase whitelist) + { + ItemBase[] WhiteList = new[] { whitelist }; + ItemBaseUtil.MoveItems(ref items, ref this.Inventory, this.SpareCapacity(), this.StorageCapacity, false, WhiteList, true); + } + + /// + /// Fills the inventory to capacity with source items + /// + /// Source items to fill the inventory + /// Item forbidden from transfer + public void FillBlackList(ref List items, IEnumerable blacklist) + { + ItemBaseUtil.MoveItems(ref items, ref this.Inventory, this.SpareCapacity(), this.StorageCapacity, false, blacklist, false); + } + + /// + /// Fills the inventory to capacity with source items + /// + /// Source items to fill the inventory + /// Item forbidden from transfer + public void FillBlackList(ref List items, ItemBase blacklist) + { + ItemBase[] BlackList = new[] { blacklist }; + ItemBaseUtil.MoveItems(ref items, ref this.Inventory, this.SpareCapacity(), this.StorageCapacity, false, BlackList, false); + } + + /// + /// Empty the inventory of items + /// + /// Target inventory or list + /// Maximum number of items to take + public void Empty(ref List items, int amount) + { + ItemBaseUtil.MoveItems(ref this.Inventory, ref items, amount); + } + + /// + /// Return item from inventory by example (obeys stack size) + /// + /// Example item to find in inventory + /// Returns the item or null if unavailable or insufficient stack size + public ItemBase RemoveItem(ItemBase item) + { + return ItemBaseUtil.RemoveListItem(item, ref this.Inventory, false); + } + + /// + /// Return item from inventory by example including partial item stack + /// + /// Example item to find in inventory + /// Returns the item or partial stack (null if item not found) + public ItemBase RemovePartialStack(ItemBase item) + { + return ItemBaseUtil.RemoveListItem(item, ref this.Inventory, true); + } + + /// + /// Remove any single item type from the inventory + /// + /// Amount to remove (for stacks) + /// The ItemBase removed from inventory + public ItemBase RemoveAnySingle(int amount = 1) + { + List output = new List(); + ItemBaseUtil.MoveItems(ref this.Inventory, ref output, amount, amount, true); + if (output.Count == 0) + return null; + return output[0]; + } + + /// + /// Remove items from inventory if items are on the whitelist + /// + /// The target inventory or list to store the items + /// The list of items allowed to transfer + /// Storage capacity of target inventory + /// Amount of items to move in this transfer + public void RemoveWhiteList(ref List items, IEnumerable whitelist, int storagecapacity = int.MaxValue, int amount = 1) + { + ItemBaseUtil.MoveItems(ref this.Inventory, ref items, amount, storagecapacity, false, whitelist, true); + } + + /// + /// Remove items from inventory if items are on the whitelist + /// + /// The target inventory or list to store the items + /// Item allowed to transfer + /// Storage capacity of target inventory + /// Amount of items to move in this transfer + public void RemoveWhiteList(ref List items, ItemBase whitelist, int storagecapacity = int.MaxValue, int amount = 1) + { + ItemBase[] WhiteList = new [] { whitelist }; + ItemBaseUtil.MoveItems(ref this.Inventory, ref items, amount, storagecapacity, false, WhiteList, true); + } + + /// + /// Remove items from inventory if items are not on the blacklist + /// + /// The target inventory or list to store the items + /// The list of items forbidden from transfer + /// Storage capacity of target inventory + /// Amount of items to move in this transfer + public void RemoveBlackList(ref List items, IEnumerable blacklist, int storagecapacity = int.MaxValue, int amount = 1) + { + ItemBaseUtil.MoveItems(ref this.Inventory, ref items, amount, storagecapacity, false, blacklist, false); + } + + /// + /// Remove items from inventory if items are not on the blacklist + /// + /// The target inventory or list to store the items + /// Item forbidden from transfer + /// Storage capacity of target inventory + /// Amount of items to move in this transfer + public void RemoveBlackList(ref List items, ItemBase blacklist, int storagecapacity = int.MaxValue, int amount = 1) + { + ItemBase[] BlackList = new[] { blacklist }; + ItemBaseUtil.MoveItems(ref this.Inventory, ref items, amount, storagecapacity, false, BlackList, false); + } + + /// + /// Returns the spare capacity of the inventory + /// + /// The spare capacity of the inventory + public int SpareCapacity() + { + return this.StorageCapacity - this.Inventory.GetItemCount(); + } + + /// + /// Returns the current number of items in the inventory + /// + /// Current number of items in inventory + public int ItemCount() + { + return this.Inventory.GetItemCount(); + } + + /// + /// Helper logic for checking if the inventory has space + /// + /// + public bool HasSpareCapcity() + { + return this.SpareCapacity() > 0; + } + + /// + /// Helper logic for checking if the inventory is empty + /// + /// + public bool IsEmpty() + { + return this.ItemCount() == 0; + } + + /// + /// Helper logic for checking if the inventory is full + /// + /// + public bool IsFull() + { + return this.ItemCount() >= this.StorageCapacity; + } + + /// + /// Item drop code for emptying the inventory on the ground on machine delete + /// + public void DropOnDelete() + { + if (!WorldScript.mbIsServer) + return; + System.Random random = new System.Random(); + for (int index = 0; index < this.Inventory.Count; ++index) + { + if (this.Inventory[index] != null) + { + Vector3 velocity = new Vector3((float)random.NextDouble() - 0.5f, (float)random.NextDouble() - 0.5f, (float)random.NextDouble() - 0.5f); + ItemManager.instance.DropItem(this.Inventory[index], this.Machine.mnX, this.Machine.mnY, this.Machine.mnZ, velocity); + } + } + this.Inventory = null; + } + + /// + /// Generic serialization function for writing the inventory to disk + /// + /// + public void WriteInventory(BinaryWriter writer) + { + int listcount = this.Inventory.Count; + int version = 0; + + writer.Write(version); + writer.Write(listcount); + for (int index = 0; index < listcount; ++index) + { + ItemFile.SerialiseItem(this.Inventory[index], writer); + } + } + + /// + /// Generic serialization function for reading the inventory from disk + /// + /// + public void ReadInventory(BinaryReader reader) + { + int listcount; + int version = reader.ReadInt32(); + + switch (version) + { + case 0: + listcount = reader.ReadInt32(); + for (int index = 0; index < listcount; ++index) + { + ItemBase item = ItemFile.DeserialiseItem(reader); + if (item != null) + this.Inventory.Add(item); + else + Debug.LogError("Machine inventory tried to read in a null item! Corrupt save?"); + } + break; + default: + Debug.LogError("Attempted to read Machine inventory version that does not exist!"); + break; + } + } + } +} From c422c4ac7a999824aeef425824ad3e709ad037a8 Mon Sep 17 00:00:00 2001 From: steveman0 Date: Wed, 15 Jun 2016 23:20:59 -0700 Subject: [PATCH 3/6] SetAmount revised Made it return the item for ease in handing off to other functions. --- Utilities/ItemBaseUtil.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Utilities/ItemBaseUtil.cs b/Utilities/ItemBaseUtil.cs index 4b6f2bb..308af4a 100644 --- a/Utilities/ItemBaseUtil.cs +++ b/Utilities/ItemBaseUtil.cs @@ -183,14 +183,15 @@ public static void DecrementStack(this ItemBase item, Int32 amount = 1) /// /// The Item Stack /// The amount of Items - public static void SetAmount(this ItemBase item, Int32 amount) + public static ItemBase SetAmount(this ItemBase item, Int32 amount) { if (!item.IsStack()) - return; + return item; if (item.mType == ItemType.ItemCubeStack) item.As().mnAmount = amount; if (item.mType == ItemType.ItemStack) item.As().mnAmount = amount; + return item; } /// From 6dfb25b4610116b1fd00693eb100854b6606aca9 Mon Sep 17 00:00:00 2001 From: steveman0 Date: Wed, 22 Jun 2016 09:42:01 -0700 Subject: [PATCH 4/6] Update 6/22 From 6e9af308a78d362abf9a887c013e9a5c968e248f Mon Sep 17 00:00:00 2001 From: steveman0 Date: Wed, 22 Jun 2016 09:43:37 -0700 Subject: [PATCH 5/6] Update 6/22 --- .../Collections/ItemBaseCollectionUtils.cs | 136 +++++++++--------- 1 file changed, 68 insertions(+), 68 deletions(-) diff --git a/Utilities/Collections/ItemBaseCollectionUtils.cs b/Utilities/Collections/ItemBaseCollectionUtils.cs index 1d601ed..46f69a5 100644 --- a/Utilities/Collections/ItemBaseCollectionUtils.cs +++ b/Utilities/Collections/ItemBaseCollectionUtils.cs @@ -1,67 +1,67 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using UnityEngine; - -namespace FortressCraft.Community.Utilities -{ - /// - /// Extension Methods and Helpers for the ItemBase Class - /// - public static partial class ItemBaseUtil - { - /// - /// Gets the amount of items and cubes in any type of Enumerable ItemBase - /// - /// The list of items to get the total count from - /// The amount of Items and Cubes - public static Int32 GetItemCount(this IEnumerable items) - { - return items.Sum(itemBase => itemBase.GetAmount()); - } - - /// - /// Gets the amount of items in any type of Enumerable ItemBase - /// - /// The list of items to get the total count from - /// The unique id of the item to count - /// The amount of Items - public static Int32 GetItemCount(this IEnumerable items, Int32 itemId) - { - return items.Where(item => item.mnItemID == itemId).Sum(item => item.GetAmount()); - } - - /// - /// Gets the amount of cubes in any type of Enumerable ItemBase - /// - /// - /// - /// - /// The amount of Cubes - public static Int32 GetItemCount(this IEnumerable items, UInt16 cubeId, UInt16 cubeValue) - { - return items.Where(item => - { - var cube = item.As(); - if (cube == null) - return false; - return cube.mCubeType == cubeId && cube.mCubeValue == cubeValue; - }).Sum(item => item.GetAmount()); - } - - /// - /// Gets the amount of cubes OR items from any Enumerable ItemBase, based off of an ItemBase - /// - /// The list of items to get the total count from - /// The ItemBase which to restrain the Count to - /// The amount of Cubes or Items - public static Int32 GetItemCount(this IEnumerable items, ItemBase restraints) - { - if (restraints.mType != ItemType.ItemCubeStack) - return items.GetItemCount(restraints.mnItemID); - - var cube = restraints.As(); - return items.GetItemCount(cube.mCubeType, cube.mCubeValue); +using System; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + +namespace FortressCraft.Community.Utilities +{ + /// + /// Extension Methods and Helpers for the ItemBase Class + /// + public static partial class ItemBaseUtil + { + /// + /// Gets the amount of items and cubes in any type of Enumerable ItemBase + /// + /// The list of items to get the total count from + /// The amount of Items and Cubes + public static Int32 GetItemCount(this IEnumerable items) + { + return items.Sum(itemBase => itemBase.GetAmount()); + } + + /// + /// Gets the amount of items in any type of Enumerable ItemBase + /// + /// The list of items to get the total count from + /// The unique id of the item to count + /// The amount of Items + public static Int32 GetItemCount(this IEnumerable items, Int32 itemId) + { + return items.Where(item => item.mnItemID == itemId).Sum(item => item.GetAmount()); + } + + /// + /// Gets the amount of cubes in any type of Enumerable ItemBase + /// + /// + /// + /// + /// The amount of Cubes + public static Int32 GetItemCount(this IEnumerable items, UInt16 cubeId, UInt16 cubeValue) + { + return items.Where(item => + { + var cube = item.As(); + if (cube == null) + return false; + return cube.mCubeType == cubeId && cube.mCubeValue == cubeValue; + }).Sum(item => item.GetAmount()); + } + + /// + /// Gets the amount of cubes OR items from any Enumerable ItemBase, based off of an ItemBase + /// + /// The list of items to get the total count from + /// The ItemBase which to restrain the Count to + /// The amount of Cubes or Items + public static Int32 GetItemCount(this IEnumerable items, ItemBase restraints) + { + if (restraints.mType != ItemType.ItemCubeStack) + return items.GetItemCount(restraints.mnItemID); + + var cube = restraints.As(); + return items.GetItemCount(cube.mCubeType, cube.mCubeValue); } @@ -97,9 +97,9 @@ public static ItemBase AddListItem(this ItemBase item, ref List target int listlength = workingtarget.Count; for (int index = 0; index <= listlength; index++) { - if (index != listlength) + if (index != listlength && item.IsStack()) { - if (workingtarget[index].IsStackAndSame(item)) + if (workingtarget[index].Compare(item)) { workingtarget[index].IncrementStack(itemcount); item = null; @@ -326,5 +326,5 @@ public static void MoveItems(this ItemBase sourceitem, ref List target sourcelist.Add(sourceitem); MoveItems(ref sourcelist, ref targetlist, amount, StorageCapacity, takefirstitem, whiteblacklist, iswhitelist); } - } -} + } +} From d683b9f0988e4b4d15d8b116ec6ad233dc675e0a Mon Sep 17 00:00:00 2001 From: steveman0 Date: Wed, 22 Jun 2016 09:44:05 -0700 Subject: [PATCH 6/6] MachineInventory update for Mobs --- Utilities/MachineInventory.cs | 46 +++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/Utilities/MachineInventory.cs b/Utilities/MachineInventory.cs index 2fdc0ae..7d106ca 100644 --- a/Utilities/MachineInventory.cs +++ b/Utilities/MachineInventory.cs @@ -15,6 +15,11 @@ public class MachineInventory /// public MachineEntity Machine; + /// + /// For associating the mob owner of the inventory + /// + public MobEntity Mob; + /// /// The total item capacity of the storage /// @@ -37,6 +42,18 @@ public MachineInventory(MachineEntity machineentity, int storagecapacity) this.Inventory = new List(); } + /// + /// Generic machine inventory class with list inventory support + /// + /// For associating the owner mob + /// The storage capacity of the inventory + public MachineInventory(MobEntity mobentity, int storagecapacity) + { + this.Mob = mobentity; + this.StorageCapacity = storagecapacity; + this.Inventory = new List(); + } + /// /// Add a single item type to the inventory /// @@ -301,6 +318,11 @@ public bool IsFull() /// public void DropOnDelete() { + if (this.Machine == null) + { + Debug.LogWarning("Tried to drop machine inventory when not associated with a machine!"); + return; + } if (!WorldScript.mbIsServer) return; System.Random random = new System.Random(); @@ -315,6 +337,30 @@ public void DropOnDelete() this.Inventory = null; } + /// + /// Item drop code for emptying the inventory on the ground on mob delete + /// + public void DropOnMobDelete() + { + if (this.Mob == null) + { + Debug.LogWarning("Tried to drop mob inventory when not associated with a mob!"); + return; + } + if (!WorldScript.mbIsServer) + return; + System.Random random = new System.Random(); + for (int index = 0; index < this.Inventory.Count; ++index) + { + if (this.Inventory[index] != null) + { + Vector3 velocity = new Vector3((float)random.NextDouble() - 0.5f, (float)random.NextDouble() - 0.5f, (float)random.NextDouble() - 0.5f); + ItemManager.instance.DropItem(this.Inventory[index], this.Mob.mnX, this.Mob.mnY, this.Mob.mnZ, velocity); + } + } + this.Inventory = null; + } + /// /// Generic serialization function for writing the inventory to disk ///