From b4dd5d3bd7d2152fdf02e0e7422b1305caf151f2 Mon Sep 17 00:00:00 2001 From: Kahrl Date: Sun, 22 Jan 2012 00:49:02 +0100 Subject: Client-side prediction of inventory changes, and some inventory menu fixes --- src/inventory.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'src/inventory.cpp') diff --git a/src/inventory.cpp b/src/inventory.cpp index ebd0b9c23..66101b831 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -733,6 +733,49 @@ ItemStack InventoryList::peekItem(u32 i, u32 peekcount) const return m_items[i].peekItem(peekcount); } +void InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i, u32 count) +{ + if(this == dest && i == dest_i) + return; + + // Take item from source list + ItemStack item1; + if(count == 0) + item1 = changeItem(i, ItemStack()); + else + item1 = takeItem(i, count); + + if(item1.empty()) + return; + + // Try to add the item to destination list + u32 oldcount = item1.count; + item1 = dest->addItem(dest_i, item1); + + // If something is returned, the item was not fully added + if(!item1.empty()) + { + // If olditem is returned, nothing was added. + bool nothing_added = (item1.count == oldcount); + + // If something else is returned, part of the item was left unadded. + // Add the other part back to the source item + addItem(i, item1); + + // If olditem is returned, nothing was added. + // Swap the items + if(nothing_added) + { + // Take item from source list + item1 = changeItem(i, ItemStack()); + // Adding was not possible, swap the items. + ItemStack item2 = dest->changeItem(dest_i, item1); + // Put item from destination list to the source list + changeItem(i, item2); + } + } +} + /* Inventory */ -- cgit v1.2.3