From e8143de01bff31f9e153949d7ab5b0df82629541 Mon Sep 17 00:00:00 2001 From: archshift Date: Thu, 19 Jun 2014 01:49:56 -0700 Subject: Nullify deleted pointers. --- src/ItemGrid.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/ItemGrid.cpp') diff --git a/src/ItemGrid.cpp b/src/ItemGrid.cpp index 34a267bab..cd36b1f2a 100644 --- a/src/ItemGrid.cpp +++ b/src/ItemGrid.cpp @@ -28,6 +28,7 @@ cItemGrid::cItemGrid(int a_Width, int a_Height) : cItemGrid::~cItemGrid() { delete[] m_Slots; + m_Slots = NULL; } -- cgit v1.2.3 From 9c32e31edf7c283d939910f23b9f59edba0d2db6 Mon Sep 17 00:00:00 2001 From: Howaner Date: Fri, 18 Jul 2014 01:02:25 +0200 Subject: Items should first added to the first slot, not the latest. --- src/ItemGrid.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/ItemGrid.cpp') diff --git a/src/ItemGrid.cpp b/src/ItemGrid.cpp index cd36b1f2a..395547545 100644 --- a/src/ItemGrid.cpp +++ b/src/ItemGrid.cpp @@ -269,7 +269,7 @@ int cItemGrid::AddItemToSlot(const cItem & a_ItemStack, int a_Slot, int a_Num, i int cItemGrid::AddItem(cItem & a_ItemStack, bool a_AllowNewStacks, int a_PrioritarySlot) { int NumLeft = a_ItemStack.m_ItemCount; - int MaxStack = ItemHandler(a_ItemStack.m_ItemType)->GetMaxStackSize(); + int MaxStack = a_ItemStack.GetMaxStackSize(); // Try prioritarySlot first: if ( @@ -284,7 +284,7 @@ int cItemGrid::AddItem(cItem & a_ItemStack, bool a_AllowNewStacks, int a_Priorit } // Scan existing stacks: - for (int i = m_NumSlots - 1; i >= 0; i--) + for (int i = 0; i < m_NumSlots; i++) { if (m_Slots[i].IsEqual(a_ItemStack)) { @@ -302,7 +302,7 @@ int cItemGrid::AddItem(cItem & a_ItemStack, bool a_AllowNewStacks, int a_Priorit return (a_ItemStack.m_ItemCount - NumLeft); } - for (int i = m_NumSlots - 1; i >= 0; i--) + for (int i = 0; i < m_NumSlots; i++) { if (m_Slots[i].IsEmpty()) { -- cgit v1.2.3 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/ItemGrid.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src/ItemGrid.cpp') 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)) -- cgit v1.2.3