Commented out debug statements again
[oweals/minetest.git] / src / inventory.cpp
index c413cc52f6d2be436293d28ce4b67a51f433f1cf..f31e19f77fb545bb9e0378347090c63c720a6e93 100644 (file)
@@ -122,16 +122,20 @@ InventoryItem* InventoryItem::deSerialize(std::istream &is)
        }
 }
 
+std::string InventoryItem::getItemString() {
+       // Get item string
+       std::ostringstream os(std::ios_base::binary);
+       serialize(os);
+       return os.str();
+}
+
 ServerActiveObject* InventoryItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
 {
        /*
                Create an ItemSAO
        */
-       // Get item string
-       std::ostringstream os(std::ios_base::binary);
-       serialize(os);
        // Create object
-       ServerActiveObject *obj = new ItemSAO(env, 0, pos, os.str());
+       ServerActiveObject *obj = new ItemSAO(env, 0, pos, getItemString());
        return obj;
 }
 
@@ -200,12 +204,17 @@ bool CraftItem::use(ServerEnvironment *env, Player *player)
 {
        if(item_craft_is_eatable(m_subname))
        {
+               u16 result_count = getCount() - 1; // Eat one at a time
                s16 hp_change = item_craft_eat_hp_change(m_subname);
                if(player->hp + hp_change > 20)
                        player->hp = 20;
                else
                        player->hp += hp_change;
-               return true;
+               
+               if(result_count < 1)
+                       return true;
+               else
+                       setCount(result_count);
        }
        return false;
 }
@@ -320,7 +329,7 @@ void InventoryList::clearItems()
        //setDirty(true);
 }
 
-void InventoryList::serialize(std::ostream &os)
+void InventoryList::serialize(std::ostream &os) const
 {
        //os.imbue(std::locale("C"));
        
@@ -549,7 +558,7 @@ InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
        }
 }
 
-bool InventoryList::itemFits(u32 i, InventoryItem *newitem)
+bool InventoryList::itemFits(const u32 i, const InventoryItem *newitem)
 {
        // If it is an empty position, it's an easy job.
        const InventoryItem *to_item = getItem(i);
@@ -558,11 +567,11 @@ bool InventoryList::itemFits(u32 i, InventoryItem *newitem)
                return true;
        }
        
-       // If not addable, return the item
+       // If not addable, fail
        if(newitem->addableTo(to_item) == false)
                return false;
        
-       // If the item fits fully in the slot, add counter and delete it
+       // If the item fits fully in the slot, pass
        if(newitem->getCount() <= to_item->freeSpace())
        {
                return true;
@@ -571,6 +580,26 @@ bool InventoryList::itemFits(u32 i, InventoryItem *newitem)
        return false;
 }
 
+bool InventoryList::roomForItem(const InventoryItem *item)
+{
+       for(u32 i=0; i<m_items.size(); i++)
+               if(itemFits(i, item))
+                       return true;
+       return false;
+}
+
+bool InventoryList::roomForCookedItem(const InventoryItem *item)
+{
+       if(!item)
+               return false;
+       const InventoryItem *cook = item->createCookResult();
+       if(!cook)
+               return false;
+       bool room = roomForItem(cook);
+       delete cook;
+       return room;
+}
+
 InventoryItem * InventoryList::takeItem(u32 i, u32 count)
 {
        if(count == 0)
@@ -661,7 +690,7 @@ Inventory & Inventory::operator = (const Inventory &other)
        return *this;
 }
 
-void Inventory::serialize(std::ostream &os)
+void Inventory::serialize(std::ostream &os) const
 {
        for(u32 i=0; i<m_lists.size(); i++)
        {