Falling sand and gravel
[oweals/minetest.git] / src / inventory.h
index 32e1cd93e7f87043fbf4d5091c818b59fcc095f1..d6049f52fd9ca0fb943da77b8fd0c5453413dfeb 100644 (file)
@@ -51,11 +51,11 @@ public:
        virtual std::string getImageBasename() const { return ""; }
 #ifndef SERVER
        // Shall return an image of the item (or NULL)
-       virtual video::ITexture * getImage(ITextureSource *tsrc) const
+       virtual video::ITexture * getImage() const
                { return NULL; }
        // Shall return an image of the item without embellishments (or NULL)
-       virtual video::ITexture * getImageRaw(ITextureSource *tsrc) const
-               { return getImage(tsrc); }
+       virtual video::ITexture * getImageRaw() const
+               { return getImage(); }
 #endif
        // Shall return a text to show in the GUI
        virtual std::string getText() { return ""; }
@@ -70,25 +70,26 @@ public:
                Quantity methods
        */
 
-       // Shall return true if the item can be add()ed to the other
+       // Return true if the item can be add()ed to the other
        virtual bool addableTo(const InventoryItem *other) const
-       {
-               return false;
-       }
+       { return false; }
+       // Return true if the other item contains this item
+       virtual bool isSubsetOf(const InventoryItem *other) const
+       { return false; }
+       // Remove the other item from this one if possible and return true
+       // Return false if not possible
+       virtual bool removeOther(const InventoryItem *other)
+       { return false; }
        
        u16 getCount() const
-       {
-               return m_count;
-       }
+       { return m_count; }
        void setCount(u16 count)
-       {
-               m_count = count;
-       }
+       { m_count = count; }
+
        // This should return something else for stackable items
        virtual u16 freeSpace() const
-       {
-               return 0;
-       }
+       { return 0; }
+
        void add(u16 count)
        {
                assert(m_count + count <= QUANTITY_ITEM_MAX_COUNT);
@@ -139,29 +140,18 @@ public:
        }
        virtual void serialize(std::ostream &os) const
        {
-               std::string nodename = m_nodename;
-               if(nodename == "")
-                       nodename = "unknown_block";
-                       
-               os<<"MaterialItem3";
+               os<<"NodeItem";
                os<<" \"";
-               os<<nodename;
+               os<<m_nodename;
                os<<"\" ";
                os<<m_count;
-
-               // Old
-               /*os<<"MaterialItem2";
-               os<<" ";
-               os<<(unsigned int)m_content;
-               os<<" ";
-               os<<m_count;*/
        }
        virtual InventoryItem* clone()
        {
                return new MaterialItem(m_gamedef, m_nodename, m_count);
        }
 #ifndef SERVER
-       video::ITexture * getImage(ITextureSource *tsrc) const;
+       video::ITexture * getImage() const;
 #endif
        std::string getText()
        {
@@ -179,6 +169,24 @@ public:
                        return false;
                return true;
        }
+       virtual bool isSubsetOf(const InventoryItem *other) const
+       {
+               if(std::string(other->getName()) != "MaterialItem")
+                       return false;
+               MaterialItem *m = (MaterialItem*)other;
+               if(m->m_nodename != m_nodename)
+                       return false;
+               return m_count <= m->m_count;
+       }
+       virtual bool removeOther(const InventoryItem *other)
+       {
+               if(!other->isSubsetOf(this))
+                       return false;
+               MaterialItem *m = (MaterialItem*)other;
+               m_count += m->m_count;
+               return true;
+       }
+
        u16 freeSpace() const
        {
                if(m_count > QUANTITY_ITEM_MAX_COUNT)
@@ -225,9 +233,9 @@ public:
        virtual void serialize(std::ostream &os) const
        {
                os<<getName();
-               os<<" ";
+               os<<" \"";
                os<<m_subname;
-               os<<" ";
+               os<<"\" ";
                os<<m_count;
        }
        virtual InventoryItem* clone()
@@ -235,7 +243,7 @@ public:
                return new CraftItem(m_gamedef, m_subname, m_count);
        }
 #ifndef SERVER
-       video::ITexture * getImage(ITextureSource *tsrc) const;
+       video::ITexture * getImage() const;
 #endif
        std::string getText()
        {
@@ -256,6 +264,24 @@ public:
                        return false;
                return true;
        }
+       virtual bool isSubsetOf(const InventoryItem *other) const
+       {
+               if(std::string(other->getName()) != "CraftItem")
+                       return false;
+               CraftItem *m = (CraftItem*)other;
+               if(m->m_subname != m_subname)
+                       return false;
+               return m_count <= m->m_count;
+       }
+       virtual bool removeOther(const InventoryItem *other)
+       {
+               if(!other->isSubsetOf(this))
+                       return false;
+               CraftItem *m = (CraftItem*)other;
+               m_count += m->m_count;
+               return true;
+       }
+
        u16 freeSpace() const
        {
                if(m_count > QUANTITY_ITEM_MAX_COUNT)
@@ -304,9 +330,9 @@ public:
        virtual void serialize(std::ostream &os) const
        {
                os<<getName();
-               os<<" ";
+               os<<" \"";
                os<<m_toolname;
-               os<<" ";
+               os<<"\" ";
                os<<m_wear;
        }
        virtual InventoryItem* clone()
@@ -316,30 +342,33 @@ public:
 
        std::string getImageBasename() const;
 #ifndef SERVER
-       video::ITexture * getImage(ITextureSource *tsrc) const;
-       video::ITexture * getImageRaw(ITextureSource *tsrc) const;
+       video::ITexture * getImage() const;
+       video::ITexture * getImageRaw() const;
 #endif
 
        std::string getText()
        {
                return "";
-               
-               /*std::ostringstream os;
-               u16 f = 4;
-               u16 d = 65535/f;
-               u16 i;
-               for(i=0; i<(65535-m_wear)/d; i++)
-                       os<<'X';
-               for(; i<f; i++)
-                       os<<'-';
-               return os.str();*/
-               
-               /*std::ostringstream os;
-               os<<m_toolname;
-               os<<" ";
-               os<<(m_wear/655);
-               return os.str();*/
        }
+
+       virtual bool isSubsetOf(const InventoryItem *other) const
+       {
+               if(std::string(other->getName()) != "ToolItem")
+                       return false;
+               ToolItem *m = (ToolItem*)other;
+               if(m->m_toolname != m_toolname)
+                       return false;
+               return m_wear <= m->m_wear;
+       }
+       virtual bool removeOther(const InventoryItem *other)
+       {
+               if(!other->isSubsetOf(this))
+                       return false;
+               ToolItem *m = (ToolItem*)other;
+               m_wear -= m->m_wear;
+               return true;
+       }
+
        /*
                Special methods
        */
@@ -602,5 +631,12 @@ struct ItemSpec
 */
 bool checkItemCombination(const InventoryItem * const*items, const ItemSpec *specs);
 
+/*
+       items: a pointer to an array of 9 pointers to items
+       specs: a pointer to an array of 9 pointers to items
+*/
+bool checkItemCombination(const InventoryItem * const * items,
+               const InventoryItem * const * specs);
+
 #endif