public static ItemAmountRow[] EnforceMaxStackSize(IEnumerable <ItemAmountRow> itemRows) { var list = new List <ItemAmountRow>(); foreach (var row in itemRows) { uint stackCount = row.amount; while (stackCount > 0) { var row2 = new ItemAmountRow(row.item, stackCount); if (stackCount > row.item.maxStackSize) { stackCount -= row.item.maxStackSize; row2.SetAmount(row.item.maxStackSize); } else { stackCount = 0; } list.Add(row2); } } return(list.ToArray()); }
public static ItemAmountRow[] ItemsToRows(IList <InventoryItemBase> itemsToAdd) { var list = new List <ItemAmountRow>(itemsToAdd.Count); for (int i = 0; i < itemsToAdd.Count; i++) { if (itemsToAdd[i] == null) { continue; } uint stackCount = itemsToAdd[i].currentStackSize; while (stackCount > 0) { var row = new ItemAmountRow(itemsToAdd[i], stackCount); if (stackCount > itemsToAdd[i].maxStackSize) { stackCount -= itemsToAdd[i].maxStackSize; row.SetAmount(itemsToAdd[i].maxStackSize); } else { stackCount = 0; } list.Add(row); } } return(list.ToArray()); }
/// <summary> /// Try adding a list of items. /// </summary> /// <param name="itemsToAdd"></param> /// <returns>All items that couldn't be added. If returnValue.Length == 0 the action was sucesful.</returns> public ItemAmountRow[] TryAdd(IList <ItemAmountRow> itemsToAdd) { Assert.IsFalse(itemsToAdd.Any(o => o.item == null), "Given array contains an empty item! (NULL)"); itemsToAdd = itemsToAdd.OrderByDescending(o => o.item.layoutSize).ToArray(); // Order from large to small to make sure large items can always be placed. itemsToAdd = InventoryItemUtility.EnforceMaxStackSize(itemsToAdd); var unAddedItems = new List <ItemAmountRow>(itemsToAdd.Count); unAddedItems.AddRange(itemsToAdd); for (int j = 0; j < itemsToAdd.Count; j++) { var currency = itemsToAdd[j].item as CurrencyInventoryItem; if (currency != null) { var l = GetBestCollectionForCurrency(currency); if (l == null) { break; } unAddedItems[j] = new ItemAmountRow(null, 0); continue; } bool added = false; var collection = GetBestCollectionForItem(itemsToAdd[j].item); if (collection == null) { break; } for (int i = 0; i < collection.collection.Length; i++) { if (collection.collection[i].itemID == itemsToAdd[j].item.ID) { if (collection.collection[i].amount + itemsToAdd[j].amount <= itemsToAdd[j].item.maxStackSize) { // Doesn't exceed stack size. collection.collection[i].amount += itemsToAdd[j].amount; unAddedItems[j] = new ItemAmountRow(null, 0); added = true; break; } // Exceeds stack size, try to add as much as possible. uint canAddAmount = itemsToAdd[j].item.maxStackSize - collection.collection[i].amount; unAddedItems[j].SetAmount(unAddedItems[j].amount - canAddAmount); collection.collection[i].amount += canAddAmount; } } if (added == false) { for (uint i = 0; i < collection.collection.Length; i++) { if (collection.collection[i].itemID == null) { if (CanSetItem(i, itemsToAdd[j].item, collection)) { var t = collection.collection[i]; t.itemID = itemsToAdd[j].item.ID; t.amount = itemsToAdd[j].item.currentStackSize; SetItem(collection, i, t); unAddedItems[j] = new ItemAmountRow(null, 0); break; } } } } } unAddedItems.RemoveAll(o => o.item == null || o.amount == 0); return(unAddedItems.ToArray()); }
public static bool CanAddItem(ItemAmountRow row) { return(CanAddItems(new[] { row })); }