public void SetItem(CollectionPriority lookup, uint i, Tuple t) { Assert.IsTrue(i < lookup.collection.Length, "Index out of bounds."); lookup.collection[i] = t; if (t.itemID != null) { Assert.IsTrue(t.itemID.Value < ItemManager.database.items.Length, "Given item with ID " + t.itemID + " does not exist in the current database! Make sure you're only adding items that are defined in the database."); var item = ItemManager.database.items[t.itemID.Value]; if (item.layoutSize > 1) { for (int col = 0; col < item.layoutSizeCols; col++) { for (int row = 0; row < item.layoutSizeRows; row++) { if (row == 0 && col == 0) { continue; } var iTemp = (uint)(i + col + (row * lookup.collectionRef.colsCount)); if (iTemp >= lookup.collection.Length) { continue; } lookup.collection[iTemp].blockedBy = i; } } } } }
public void EndTest(CollectionPriority lookup) { lookup.collection = new Tuple[testTuples.Length]; testTuples.CopyTo(lookup.collection, 0); testTuples = new Tuple[0]; }
public void RemoveSlot(CollectionPriority lookup, uint slot) { lookup.collection[slot].Reset(); foreach (var source in lookup.collection.Where(o => o.blockedBy == slot)) { source.Reset(); } }
public void LoadFrom(params ItemCollectionPriority <ItemCollectionBase>[] collectionsToLoadFrom) { _collections.Clear(); foreach (var col in collectionsToLoadFrom) { var lookup = new CollectionPriority(null, col.priority, col.collection); LoadCollectionTuples(lookup); _collections.Add(lookup); } }
public void StartTest(CollectionPriority lookup) { // Copy all data so that it can be restored later. testTuples = new Tuple[lookup.collection.Length]; for (int i = 0; i < testTuples.Length; i++) { testTuples[i] = new Tuple(lookup.collection[i].itemID, lookup.collection[i].amount) { blockedBy = lookup.collection[i].blockedBy }; } }
/// <summary> /// Finds the first empty slot in this collection /// </summary> /// <returns>The index of the first empty slot, and -1 if no slot found (when collection is full)</returns> public int FindFirstEmptySlotForItem(CollectionPriority lookup, InventoryItemBase item) { for (uint i = 0; i < lookup.collection.Length; i++) { if (lookup.collection[i].itemID == null) { if (CanSetItem(i, item, lookup)) { return((int)i); } } } return(-1); }
public bool CanRemoveSlots(CollectionPriority lookup, uint amount) { if (lookup.collection.Length - (int)amount < 0) { return(false); } uint oldSize = (uint)lookup.collection.Length; uint newSize = oldSize - amount; for (uint i = newSize; i < oldSize; i++) { if (lookup.collection[i].itemID != null || lookup.collection[i].blockedBy.HasValue) { return(false); } } return(true); }
public CollectionPriority GetBestCollectionForItem(InventoryItemBase item) { CollectionPriority best = null; foreach (var lookup in _collections) { if (CanAddItem(lookup, item)) { if (best == null) { best = lookup; } else if (lookup.priority > best.priority) { best = lookup; } } } return(best); }
private void LoadCollectionTuples(CollectionPriority lookup) { var items = lookup.collectionRef.items.Select(o => o.item).ToArray(); lookup.collection = new Tuple[items.Length]; for (int i = 0; i < lookup.collection.Length; i++) { lookup.collection[i] = new Tuple(null, 0); } for (uint i = 0; i < lookup.collection.Length; i++) { // Only set those with an item to avoid overwriting old changes. if (items[i] != null) { var t = lookup.collection[i]; t.itemID = items[i].ID; t.amount = items[i].currentStackSize; SetItem(lookup, i, t); } } }
public Tuple[] GetStacksSmallestToLargest(CollectionPriority lookup, uint itemID) { return(lookup.collection.Where(o => o.itemID == itemID).OrderBy(o => o.amount).ToArray()); }
public bool CanSetItem(uint toSlot, InventoryItemBase item, CollectionPriority collection) { if (item == null) { return(true); } if (collection.collectionRef.ignoreItemLayoutSizes == false) { #region Blocked by other object if (collection.collection[toSlot].blockedBy.HasValue) { return(false); } for (int col = 0; col < item.layoutSizeCols; col++) { for (int row = 0; row < item.layoutSizeRows; row++) { if (row == 0 && col == 0) { continue; } var checkSlot = (uint)(toSlot + col + (row * collection.collectionRef.colsCount)); if (checkSlot >= collection.collectionRef.items.Length) { return(false); // Out of bounds // break; } if (collection.collection[checkSlot].itemID.HasValue) { return(false); } if (collection.collection[checkSlot].blockedBy.HasValue) { return(false); } } } #endregion #region Layout out of bounds var col2 = (int)(toSlot % collection.collectionRef.colsCount); var row2 = Mathf.FloorToInt((float)toSlot / collection.collectionRef.colsCount); if (row2 + item.layoutSizeRows - 1 >= collection.collectionRef.rowsCount) { return(false); } if (col2 + item.layoutSizeCols - 1 >= collection.collectionRef.colsCount) { return(false); } #endregion } if (collection.collectionRef.canPutItemsInCollection == false) { return(false); } if (collection.collectionRef.restrictByWeight && collection.collectionRef.GetWeight() + item.weight * item.currentStackSize > collection.collectionRef.restrictMaxWeight) { DevdogLogger.LogVerbose("Can't add item to collection, adding item would exceed max weight", collection.collectionRef); return(false); // To much weight } if (collection.collectionRef.VerifyFilters(item) == false) { DevdogLogger.LogVerbose("Can't add item to collection, restriction filter does not allow: #" + item.ID + " " + item.GetType(), collection.collectionRef); return(false); } return(true); }
public uint CanAddItemCount(CollectionPriority lookup, InventoryItemBase itemToAdd, uint earlyBailAmount) { if (lookup.collectionRef.canPutItemsInCollection == false) { DevdogLogger.LogVerbose("Can't put item in this collection - " + lookup.collectionRef.collectionName, lookup.collectionRef); return(0); } if (lookup.collectionRef.useReferences) { return(0); } if (lookup.collectionRef.VerifyFilters(itemToAdd) == false) { // Debug.Log("Item can't be added to this collection, blocked by filters - " + lookup.collectionRef.collectionName); return(0); } if (lookup.collectionRef.VerifyCustomConditionals(itemToAdd) == false) { // Debug.Log("Item can't be added to this collection, blocked by custom conditionals - " + lookup.collectionRef.collectionName); return(0); } int weightLimit = 99999; if (lookup.collectionRef.restrictByWeight && itemToAdd.weight > 0.0f) // avoid dividing by 0.0f { float weightSpace = lookup.collectionRef.restrictMaxWeight - GetWeight(lookup); weightLimit = Mathf.FloorToInt(weightSpace / itemToAdd.weight); } StartTest(lookup); float amount = 0f; for (uint i = 0; i < lookup.collection.Length; i++) { if (amount >= earlyBailAmount) { break; // Got enough to pass. } var item = lookup.collection[i]; if (item.itemID != null && item.itemID == itemToAdd.ID) { amount += Mathf.Clamp((int)itemToAdd.maxStackSize - (int)item.amount, 0, 999999); } else if (item.itemID == null) { if (CanSetItem(i, itemToAdd, lookup)) { var t = lookup.collection[i]; t.itemID = itemToAdd.ID; t.amount = itemToAdd.currentStackSize; SetItem(lookup, i, t); // Set state -> Test is active, which will revert changes. amount += itemToAdd.maxStackSize; } } } EndTest(lookup); return((uint)Mathf.Min(amount, weightLimit)); }
public bool CanAddItem(CollectionPriority lookup, InventoryItemBase item) { return(CanAddItemCount(lookup, item, item.currentStackSize) >= item.currentStackSize); }
/// <summary> /// Get the total weight of all items inside this collection /// </summary> /// <returns></returns> public float GetWeight(CollectionPriority lookup) { return(lookup.collection.Sum(o => o.itemID == null ? 0.0f : ItemManager.database.items[o.itemID.Value].weight * o.amount)); }