/// <summary> /// Adds the given inventory item to the storage, placing it with other items of the item, or /// placing it into an existing item if needed. /// </summary> /// <param name="stack"> The stack to add to the storage. </param> /// <returns> The items that could not be added to the storage. </returns> public InventoryStack AddToStorage(InventoryStack stack) { // first look for any existing stacks that have room for (var i = 0; i < _slots.Length && !stack.IsEmpty; i++) { var slot = _slots[i]; if (!slot.IsEmpty && slot.Model == stack.Model && slot.ExtraCapacity > 0) { stack = Fill(i, stack); } } // fill any remaining empty slots with remaining quantities if (!stack.IsEmpty) { for (var i = 0; i < _slots.Length && !stack.IsEmpty; i++) { stack = Put(i, stack); } } // return however many are left return(stack); }
/// <summary> Fills the designated slot with the items of the same time. </summary> /// <param name="cursor"> The position in which to fill the stack. </param> /// <param name="stack"> The stack to add to the storage. </param> /// <returns> The remaining items which could not be added. </returns> public InventoryStack Fill(Cursor cursor, InventoryStack stack) { if (cursor.Parent != this) { return(stack); } return(Fill(cursor.SlotNumber, stack)); }
/// <summary> Writes a given stack to a slot. </summary> /// <param name="slotNumber"> The slot number to which the value should be written. </param> /// <param name="value"> The value of the slot. </param> private void Write(int slotNumber, InventoryStack value) { if (value.IsEmpty) { value = InventoryStack.Empty; } _slots[slotNumber] = value; // increment the change count ForceSnapshot(); }
/// <summary> Fills the designated slot with the items of the same time. </summary> /// <param name="slotNumber"> Zero-based index of the slot to fill. </param> /// <param name="stack"> The stack to add to the storage. </param> /// <returns> The remaining items which could not be added. </returns> private InventoryStack Fill(int slotNumber, InventoryStack stack) { var slot = _slots[slotNumber]; int numberToAdd = Math.Min(slot.ExtraCapacity, stack.Quantity); // add that much to the current slot Write(slotNumber, slot.Plus(numberToAdd)); // reduce the current stack by that much return(stack.Plus(-numberToAdd)); }
/// <summary> Gets the quantity to display for the given stack. </summary> /// <param name="stack"> The stack for which the count should be retrieved. </param> /// <returns> The display count for the stack. </returns> public int GetDisplayCount(InventoryStack stack) { var model = stack.Model; var weapon = model as FireableWeapon; if (weapon != null) { return(GetDisplayFor(weapon)); } else { return(stack.Quantity); } }
/// <summary> Puts items of the designated into the designated empty slot. </summary> /// <param name="slotNumber"> The position in which to put the stack. </param> /// <param name="stack"> The stack to add to the storage. </param> /// <returns> The remaining items that could not be placed in the designated slot. </returns> private InventoryStack Put(int slotNumber, InventoryStack stack) { if (!_slots[slotNumber].IsEmpty) { return(stack); } int numberToAdd = Math.Min(stack.Model.StackAmount, stack.Quantity); // add that much to the current slot Write(slotNumber, new InventoryStack(stack.Model, numberToAdd)); // reduce the current stack by that much stack = stack.Plus(-numberToAdd); return(stack); }