From b4522bf14e2552217611b578b1847298c0ed5ac5 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Sun, 26 May 2013 14:34:26 +0000 Subject: ItemGrid: Added the RemoveOneItem() function git-svn-id: http://mc-server.googlecode.com/svn/trunk@1513 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/ItemGrid.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ source/ItemGrid.h | 6 ++++++ 2 files changed, 53 insertions(+) diff --git a/source/ItemGrid.cpp b/source/ItemGrid.cpp index 0495105f2..5a432e80f 100644 --- a/source/ItemGrid.cpp +++ b/source/ItemGrid.cpp @@ -355,6 +355,53 @@ int cItemGrid::ChangeSlotCount(int a_X, int a_Y, int a_AddToCount) +cItem cItemGrid::RemoveOneItem(int a_SlotNum) +{ + if ((a_SlotNum < 0) || (a_SlotNum >= m_NumSlots)) + { + LOGWARNING("%s: Invalid slot number %d out of %d slots, ignoring the call, returning empty item", + __FUNCTION__, a_SlotNum, m_NumSlots + ); + return cItem(); + } + + // If the slot is empty, return an empty item + if (m_Slots[a_SlotNum].IsEmpty()) + { + return cItem(); + } + + // Make a copy of the item in slot, set count to 1 and remove one from the slot + cItem res = m_Slots[a_SlotNum]; + res.m_ItemCount = 1; + m_Slots[a_SlotNum].m_ItemCount -= 1; + + // Emptying the slot correctly if appropriate + if (m_Slots[a_SlotNum].m_ItemCount == 0) + { + m_Slots[a_SlotNum].Empty(); + } + + // Notify everyone of the change + TriggerListeners(a_SlotNum); + + // Return the stored one item + return res; +} + + + + + +cItem cItemGrid::RemoveOneItem(int a_X, int a_Y) +{ + return RemoveOneItem(GetSlotNum(a_X, a_Y)); +} + + + + + int cItemGrid::HowManyItems(const cItem & a_Item) { int res = 0; diff --git a/source/ItemGrid.h b/source/ItemGrid.h index 22d64f076..82898537c 100644 --- a/source/ItemGrid.h +++ b/source/ItemGrid.h @@ -98,6 +98,12 @@ public: */ int ChangeSlotCount(int a_X, int a_Y, int a_AddToCount); + /// Removes one item from the specified slot, and returns it. If the slot was empty, returns an empty item + cItem RemoveOneItem(int a_SlotNum); + + /// Removes one item from the specified slot, and returns it. If the slot was empty, returns an empty item + cItem RemoveOneItem(int a_X, int a_Y); + /// Returns the number of items of type a_Item that are stored int HowManyItems(const cItem & a_Item); -- cgit v1.2.3