summaryrefslogtreecommitdiff
path: root/src/inventory.cpp
diff options
context:
space:
mode:
authorKahrl <kahrl@gmx.net>2012-01-22 00:49:02 +0100
committerPerttu Ahola <celeron55@gmail.com>2012-01-22 17:31:20 +0200
commitb4dd5d3bd7d2152fdf02e0e7422b1305caf151f2 (patch)
treeb47e57a55e6a2787464e49ffe172592b956c5a96 /src/inventory.cpp
parent1efdc36b22532807d21a0beac94524e3eacfe7bc (diff)
downloadminetest-b4dd5d3bd7d2152fdf02e0e7422b1305caf151f2.tar.gz
minetest-b4dd5d3bd7d2152fdf02e0e7422b1305caf151f2.tar.bz2
minetest-b4dd5d3bd7d2152fdf02e0e7422b1305caf151f2.zip
Client-side prediction of inventory changes, and some inventory menu fixes
Diffstat (limited to 'src/inventory.cpp')
-rw-r--r--src/inventory.cpp43
1 files changed, 43 insertions, 0 deletions
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
*/