From 51b91befbd26b630cd2cecaec081edd03edfb5f3 Mon Sep 17 00:00:00 2001 From: Howaner Date: Fri, 18 Jul 2014 23:11:59 +0200 Subject: Added RemoveItem() function to the player inventory. --- src/Inventory.cpp | 18 ++++++++++++++++++ src/Inventory.h | 3 +++ src/ItemGrid.cpp | 33 +++++++++++++++++++++++++++++++++ src/ItemGrid.h | 3 +++ 4 files changed, 57 insertions(+) (limited to 'src') diff --git a/src/Inventory.cpp b/src/Inventory.cpp index 38d3c886d..5f7c24b60 100644 --- a/src/Inventory.cpp +++ b/src/Inventory.cpp @@ -151,6 +151,24 @@ int cInventory::AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks, bool a +int cInventory::RemoveItem(const cItem & a_ItemStack) +{ + int RemovedItems = m_HotbarSlots.RemoveItem(a_ItemStack); + + if (RemovedItems < a_ItemStack.m_ItemCount) + { + cItem Temp(a_ItemStack); + Temp.m_ItemCount -= RemovedItems; + RemovedItems += m_InventorySlots.RemoveItem(Temp); + } + + return RemovedItems; +} + + + + + bool cInventory::RemoveOneEquippedItem(void) { if (m_HotbarSlots.GetSlot(m_EquippedSlotNum).IsEmpty()) diff --git a/src/Inventory.h b/src/Inventory.h index e25fc4a8a..6e32536b3 100644 --- a/src/Inventory.h +++ b/src/Inventory.h @@ -86,6 +86,9 @@ public: */ int AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks, bool a_tryToFillEquippedFirst); + /** Remove the specified item with the specified amount from the inventory. Returns the amount from the items were was removed. */ + int RemoveItem(const cItem & a_ItemStack); + /** Removes one item out of the currently equipped item stack, returns true if successful, false if empty-handed */ bool RemoveOneEquippedItem(void); diff --git a/src/ItemGrid.cpp b/src/ItemGrid.cpp index cd36b1f2a..38829cbf8 100644 --- a/src/ItemGrid.cpp +++ b/src/ItemGrid.cpp @@ -345,6 +345,39 @@ int cItemGrid::AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks, int a_P +int cItemGrid::RemoveItem(const cItem & a_ItemStack) +{ + int NumLeft = a_ItemStack.m_ItemCount; + + for (int i = 0; i < m_NumSlots; i++) + { + if (NumLeft <= 0) + { + break; + } + + if (m_Slots[i].IsEqual(a_ItemStack)) + { + int NumToRemove = std::min(NumLeft, (int)m_Slots[i].m_ItemCount); + NumLeft -= NumToRemove; + m_Slots[i].m_ItemCount -= NumToRemove; + + if (m_Slots[i].m_ItemCount <= 0) + { + m_Slots[i].Empty(); + } + + TriggerListeners(i); + } + } + + return (a_ItemStack.m_ItemCount - NumLeft); +} + + + + + int cItemGrid::ChangeSlotCount(int a_SlotNum, int a_AddToCount) { if ((a_SlotNum < 0) || (a_SlotNum >= m_NumSlots)) diff --git a/src/ItemGrid.h b/src/ItemGrid.h index c34d5e9e2..6731b0065 100644 --- a/src/ItemGrid.h +++ b/src/ItemGrid.h @@ -96,6 +96,9 @@ public: Returns the total number of items that fit. */ int AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks = true, int a_PrioritarySlot = -1); + + /** Remove the specified item with the specified amount from the inventory. Returns the amount from the items were was removed. */ + int RemoveItem(const cItem & a_ItemStack); /** Adds (or subtracts, if a_AddToCount is negative) to the count of items in the specified slot. If the slot is empty, ignores the call. -- cgit v1.2.3