summaryrefslogtreecommitdiff
path: root/src/inventory.cpp
diff options
context:
space:
mode:
authorest31 <MTest31@outlook.com>2015-06-20 12:55:48 +0200
committerest31 <MTest31@outlook.com>2015-06-23 20:18:41 +0200
commit2c1fd29884adec17564d39c0f7792633cbc55f9a (patch)
tree73bdee04a50963ac5990f08b96ee30f069aa8ce7 /src/inventory.cpp
parentbc55ef337cc83a5c31d4fdafba352a7a26921900 (diff)
downloadminetest-2c1fd29884adec17564d39c0f7792633cbc55f9a.tar.gz
minetest-2c1fd29884adec17564d39c0f7792633cbc55f9a.tar.bz2
minetest-2c1fd29884adec17564d39c0f7792633cbc55f9a.zip
Add MoveSomewhere inventory action
Improve shift+click experience
Diffstat (limited to 'src/inventory.cpp')
-rw-r--r--src/inventory.cpp47
1 files changed, 42 insertions, 5 deletions
diff --git a/src/inventory.cpp b/src/inventory.cpp
index 7941b3a2a..ff2c15b65 100644
--- a/src/inventory.cpp
+++ b/src/inventory.cpp
@@ -782,11 +782,48 @@ 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)
+void InventoryList::moveItemSomewhere(u32 i, InventoryList *dest, u32 count)
{
- if(this == dest && i == dest_i)
+ // 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;
+ u32 dest_size = dest->getSize();
+ // First try all the non-empty slots
+ for (u32 dest_i = 0; dest_i < dest_size; dest_i++) {
+ if (!m_items[dest_i].empty()) {
+ item1 = dest->addItem(dest_i, item1);
+ if (item1.empty()) return;
+ }
+ }
+
+ // Then try all the empty ones
+ for (u32 dest_i = 0; dest_i < dest_size; dest_i++) {
+ if (m_items[dest_i].empty()) {
+ item1 = dest->addItem(dest_i, item1);
+ if (item1.empty()) return;
+ }
+ }
+
+ // If we reach this, the item was not fully added
+ // Add the remaining part back to the source item
+ addItem(i, item1);
+}
+
+u32 InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i,
+ u32 count, bool swap_if_needed)
+{
+ if(this == dest && i == dest_i)
+ return count;
+
// Take item from source list
ItemStack item1;
if(count == 0)
@@ -795,7 +832,7 @@ void InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i, u32 count)
item1 = takeItem(i, count);
if(item1.empty())
- return;
+ return 0;
// Try to add the item to destination list
u32 oldcount = item1.count;
@@ -813,8 +850,7 @@ void InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i, u32 count)
// If olditem is returned, nothing was added.
// Swap the items
- if(nothing_added)
- {
+ if (nothing_added && swap_if_needed) {
// Take item from source list
item1 = changeItem(i, ItemStack());
// Adding was not possible, swap the items.
@@ -823,6 +859,7 @@ void InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i, u32 count)
changeItem(i, item2);
}
}
+ return (oldcount - item1.count);
}
/*