]> git.lizzy.rs Git - minetest.git/blobdiff - src/inventory.cpp
Fixed a temporary solution of server shutting down to an assert(0) when a too large...
[minetest.git] / src / inventory.cpp
index 86e00877c96054eb1e9001599d5da0bfb660192a..f91e2e0b6cd6e433e784d7b1b5ad0a18d757e135 100644 (file)
@@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "debug.h"
 #include <sstream>
 #include "main.h"
+#include "serverobject.h"
 
 /*
        InventoryItem
@@ -90,8 +91,125 @@ InventoryItem* InventoryItem::deSerialize(std::istream &is)
        }
 }
 
+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());
+       return obj;
+}
+
+/*
+       MaterialItem
+*/
+
+bool MaterialItem::isCookable()
+{
+       if(m_content == CONTENT_TREE)
+       {
+               return true;
+       }
+       else if(m_content == CONTENT_COBBLE)
+       {
+               return true;
+       }
+       return false;
+}
+
+InventoryItem *MaterialItem::createCookResult()
+{
+       if(m_content == CONTENT_TREE)
+       {
+               return new CraftItem("lump_of_coal", 1);
+       }
+       else if(m_content == CONTENT_COBBLE)
+       {
+               return new MaterialItem(CONTENT_STONE, 1);
+       }
+       return NULL;
+}
+
+/*
+       CraftItem
+*/
+
+#ifndef SERVER
+video::ITexture * CraftItem::getImage()
+{
+       if(g_texturesource == NULL)
+               return NULL;
+       
+       std::string name;
+
+       if(m_subname == "Stick")
+               name = "stick.png";
+       else if(m_subname == "lump_of_coal")
+               name = "lump_of_coal.png";
+       else if(m_subname == "lump_of_iron")
+               name = "lump_of_iron.png";
+       else if(m_subname == "steel_ingot")
+               name = "steel_ingot.png";
+       else if(m_subname == "rat")
+               name = "rat.png";
+       else
+               name = "cloud.png";
+       
+       // Get such a texture
+       return g_texturesource->getTextureRaw(name);
+}
+#endif
+
+ServerActiveObject* CraftItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
+{
+       // Special cases
+       if(m_subname == "rat")
+       {
+               ServerActiveObject *obj = new RatSAO(env, id, pos);
+               return obj;
+       }
+       // Default
+       else
+       {
+               return InventoryItem::createSAO(env, id, pos);
+       }
+}
+
+u16 CraftItem::getDropCount()
+{
+       // Special cases
+       if(m_subname == "rat")
+               return 1;
+       // Default
+       else
+               return InventoryItem::getDropCount();
+}
+
+bool CraftItem::isCookable()
+{
+       if(m_subname == "lump_of_iron")
+       {
+               return true;
+       }
+       return false;
+}
+
+InventoryItem *CraftItem::createCookResult()
+{
+       if(m_subname == "lump_of_iron")
+       {
+               return new CraftItem("steel_ingot", 1);
+       }
+       return NULL;
+}
+
 /*
        MapBlockObjectItem
+       TODO: Remove
 */
 #ifndef SERVER
 video::ITexture * MapBlockObjectItem::getImage()
@@ -169,6 +287,7 @@ InventoryList::InventoryList(std::string name, u32 size)
        m_name = name;
        m_size = size;
        clearItems();
+       //m_dirty = false;
 }
 
 InventoryList::~InventoryList()
@@ -194,6 +313,8 @@ void InventoryList::clearItems()
        {
                m_items.push_back(NULL);
        }
+
+       //setDirty(true);
 }
 
 void InventoryList::serialize(std::ostream &os)
@@ -287,6 +408,7 @@ InventoryList & InventoryList::operator = (const InventoryList &other)
                        m_items[i] = item->clone();
                }
        }
+       //setDirty(true);
 
        return *this;
 }
@@ -313,6 +435,11 @@ u32 InventoryList::getUsedSlots()
        return num;
 }
 
+u32 InventoryList::getFreeSlots()
+{
+       return getSize() - getUsedSlots();
+}
+
 InventoryItem * InventoryList::getItem(u32 i)
 {
        if(i > m_items.size() - 1)
@@ -326,6 +453,7 @@ InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
 
        InventoryItem *olditem = m_items[i];
        m_items[i] = newitem;
+       //setDirty(true);
        return olditem;
 }
 
@@ -339,6 +467,9 @@ void InventoryList::deleteItem(u32 i)
 
 InventoryItem * InventoryList::addItem(InventoryItem *newitem)
 {
+       if(newitem == NULL)
+               return NULL;
+       
        /*
                First try to find if it could be added to some existing items
        */
@@ -373,6 +504,11 @@ InventoryItem * InventoryList::addItem(InventoryItem *newitem)
 
 InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
 {
+       if(newitem == NULL)
+               return NULL;
+       
+       //setDirty(true);
+       
        // If it is an empty position, it's an easy job.
        InventoryItem *to_item = m_items[i];
        if(to_item == NULL)
@@ -429,6 +565,8 @@ InventoryItem * InventoryList::takeItem(u32 i, u32 count)
 {
        if(count == 0)
                return NULL;
+       
+       //setDirty(true);
 
        InventoryItem *item = m_items[i];
        // If it is an empty position, return NULL