Noise: Prevent unittest crash caused by division by zero
[oweals/minetest.git] / src / inventory.cpp
index 6d5b499168ac69c2f5d140659c3b37a1f8e7e1e3..4da380a249b06f58700b3b895b1080c63093c851 100644 (file)
@@ -46,12 +46,11 @@ static content_t content_translate_from_19_to_internal(content_t c_from)
 }
 
 ItemStack::ItemStack(const std::string &name_, u16 count_,
-               u16 wear_, IItemDefManager *itemdef)
+               u16 wear_, IItemDefManager *itemdef) :
+       name(itemdef->getAlias(name_)),
+       count(count_),
+       wear(wear_)
 {
-       name = itemdef->getAlias(name_);
-       count = count_;
-       wear = wear_;
-
        if (name.empty() || count == 0)
                clear();
        else if (itemdef->get(name).type == ITEM_TOOL)
@@ -255,11 +254,8 @@ std::string ItemStack::getItemString() const
 }
 
 
-ItemStack ItemStack::addItem(const ItemStack &newitem_,
-               IItemDefManager *itemdef)
+ItemStack ItemStack::addItem(ItemStack newitem, IItemDefManager *itemdef)
 {
-       ItemStack newitem = newitem_;
-
        // If the item is empty or the position invalid, bail out
        if(newitem.empty())
        {
@@ -268,8 +264,17 @@ ItemStack ItemStack::addItem(const ItemStack &newitem_,
        // If this is an empty item, it's an easy job.
        else if(empty())
        {
+               const u16 stackMax = newitem.getStackMax(itemdef);
+
                *this = newitem;
-               newitem.clear();
+
+               // If the item fits fully, delete it
+               if (count <= stackMax) {
+                       newitem.clear();
+               } else { // Else the item does not fit fully. Return the rest.
+                       count = stackMax;
+                       newitem.remove(count);
+               }
        }
        // If item name or metadata differs, bail out
        else if (name != newitem.name
@@ -295,11 +300,10 @@ ItemStack ItemStack::addItem(const ItemStack &newitem_,
        return newitem;
 }
 
-bool ItemStack::itemFits(const ItemStack &newitem_,
+bool ItemStack::itemFits(ItemStack newitem,
                ItemStack *restitem,
                IItemDefManager *itemdef) const
 {
-       ItemStack newitem = newitem_;
 
        // If the item is empty or the position invalid, bail out
        if(newitem.empty())
@@ -309,7 +313,14 @@ bool ItemStack::itemFits(const ItemStack &newitem_,
        // If this is an empty item, it's an easy job.
        else if(empty())
        {
-               newitem.clear();
+               const u16 stackMax = newitem.getStackMax(itemdef);
+
+               // If the item fits fully, delete it
+               if (newitem.count <= stackMax) {
+                       newitem.clear();
+               } else { // Else the item does not fit fully. Return the rest.
+                       newitem.remove(stackMax);
+               }
        }
        // If item name or metadata differs, bail out
        else if (name != newitem.name
@@ -323,7 +334,6 @@ bool ItemStack::itemFits(const ItemStack &newitem_,
                newitem.clear();
        }
        // Else the item does not fit fully. Return the rest.
-       // the rest.
        else
        {
                u16 freespace = freeSpace(itemdef);
@@ -370,14 +380,12 @@ ItemStack ItemStack::peekItem(u32 peekcount) const
        Inventory
 */
 
-InventoryList::InventoryList(std::string name, u32 size, IItemDefManager *itemdef)
+InventoryList::InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef):
+       m_name(name),
+       m_size(size),
+       m_itemdef(itemdef)
 {
-       m_name = name;
-       m_size = size;
-       m_width = 0;
-       m_itemdef = itemdef;
        clearItems();
-       //m_dirty = false;
 }
 
 InventoryList::~InventoryList()
@@ -661,7 +669,7 @@ bool InventoryList::roomForItem(const ItemStack &item_) const
        return false;
 }
 
-bool InventoryList::containsItem(const ItemStack &item) const
+bool InventoryList::containsItem(const ItemStack &item, bool match_meta) const
 {
        u32 count = item.count;
        if(count == 0)
@@ -672,9 +680,9 @@ bool InventoryList::containsItem(const ItemStack &item) const
        {
                if(count == 0)
                        break;
-               if(i->name == item.name)
-               {
-                       if(i->count >= count)
+               if (i->name == item.name
+                               && (!match_meta || (i->metadata == item.metadata))) {
+                       if (i->count >= count)
                                return true;
                        else
                                count -= i->count;
@@ -712,14 +720,6 @@ ItemStack InventoryList::takeItem(u32 i, u32 takecount)
        return taken;
 }
 
-ItemStack InventoryList::peekItem(u32 i, u32 peekcount) const
-{
-       if(i >= m_items.size())
-               return ItemStack();
-
-       return m_items[i].peekItem(peekcount);
-}
-
 void InventoryList::moveItemSomewhere(u32 i, InventoryList *dest, u32 count)
 {
        // Take item from source list