]> git.lizzy.rs Git - minetest.git/blobdiff - src/inventory.h
inventorycube: use all three specified textures; also moved mesh creation / modificat...
[minetest.git] / src / inventory.h
index 29b97ccad95b6e2be7dc5ddf343b03846e728069..93a29764c9a01a1caa16ac22b59136510489aadd 100644 (file)
@@ -31,6 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 class ServerActiveObject;
 class ServerEnvironment;
+struct PointedThing;
 class ITextureSource;
 class IGameDef;
 
@@ -41,6 +42,8 @@ class InventoryItem
        virtual ~InventoryItem();
        
        static InventoryItem* deSerialize(std::istream &is, IGameDef *gamedef);
+       static InventoryItem* deSerialize(const std::string &str,
+                       IGameDef *gamedef);
        
        virtual const char* getName() const = 0;
        // Shall write the name and the parameters
@@ -51,47 +54,47 @@ class InventoryItem
        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 ""; }
        // Returns the string used for inventory
        virtual std::string getItemString();
-       // Creates an object from the item, to be placed in the world.
-       virtual ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
-       // Gets amount of items that dropping one SAO will decrement
-       virtual u16 getDropCount() const { return getCount(); }
 
        /*
                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; }
+
+       u16 freeSpace() const
        {
-               m_count = count;
-       }
-       // This should return something else for stackable items
-       virtual u16 freeSpace() const
-       {
-               return 0;
+               u16 max = getStackMax();
+               if(m_count > max)
+                       return 0;
+               return max - m_count;
        }
+
        void add(u16 count)
        {
-               assert(m_count + count <= QUANTITY_ITEM_MAX_COUNT);
                m_count += count;
        }
        void remove(u16 count)
@@ -104,18 +107,36 @@ class InventoryItem
                Other properties
        */
 
+       // Maximum size of a stack
+       virtual u16 getStackMax() const {return 1;}
+       // Whether it can be used
+       virtual bool isUsable() const {return false;}
        // Whether it can be cooked
        virtual bool isCookable() const {return false;}
-       // Time of cooking
-       virtual float getCookTime(){return 3.0;}
        // Result of cooking (can randomize)
        virtual InventoryItem *createCookResult() const {return NULL;}
-       
+       // Time of cooking
+       virtual float getCookTime() const {return 3.0;}
+       // Whether it can be burned (<0 = cannot be burned)
+       virtual float getBurnTime() const {return -1;}
+       // Gets amount of items that dropping one ItemSAO will decrement
+       // -1 means as many as possible
+       virtual s16 getDropCount() const { return -1; }
+       // Whether this item can point to liquids
+       virtual bool areLiquidsPointable() const { return false; }
+
+       // Creates an object from the item and places it in the world.
+       // If return value is true, item should be removed.
+       virtual bool dropOrPlace(ServerEnvironment *env,
+                       ServerActiveObject *dropper,
+                       v3f pos, bool place, s16 count);
+
        // Eat, press, activate, whatever.
-       // Called when item is right-clicked when lying on ground.
+       // Called when item is left-clicked while in hand.
        // If returns true, item shall be deleted.
        virtual bool use(ServerEnvironment *env,
-                       ServerActiveObject *user){return false;}
+                       ServerActiveObject *user,
+                       const PointedThing& pointed){return false;}
 
 protected:
        IGameDef *m_gamedef;
@@ -125,11 +146,9 @@ class InventoryItem
 class MaterialItem : public InventoryItem
 {
 public:
-       MaterialItem(IGameDef *gamedef, content_t content, u16 count):
-               InventoryItem(gamedef, count)
-       {
-               m_content = content;
-       }
+       MaterialItem(IGameDef *gamedef, std::string nodename, u16 count);
+       // Legacy constructor
+       MaterialItem(IGameDef *gamedef, content_t content, u16 count);
        /*
                Implementation interface
        */
@@ -139,19 +158,18 @@ class MaterialItem : public InventoryItem
        }
        virtual void serialize(std::ostream &os) const
        {
-               //os.imbue(std::locale("C"));
-               os<<"MaterialItem2";
-               os<<" ";
-               os<<(unsigned int)m_content;
-               os<<" ";
+               os<<"node";
+               os<<" \"";
+               os<<m_nodename;
+               os<<"\" ";
                os<<m_count;
        }
        virtual InventoryItem* clone()
        {
-               return new MaterialItem(m_gamedef, m_content, m_count);
+               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()
        {
@@ -165,30 +183,48 @@ class MaterialItem : public InventoryItem
                if(std::string(other->getName()) != "MaterialItem")
                        return false;
                MaterialItem *m = (MaterialItem*)other;
-               if(m->getMaterial() != m_content)
+               if(m->m_nodename != m_nodename)
                        return false;
                return true;
        }
-       u16 freeSpace() const
+       virtual bool isSubsetOf(const InventoryItem *other) const
        {
-               if(m_count > QUANTITY_ITEM_MAX_COUNT)
-                       return 0;
-               return QUANTITY_ITEM_MAX_COUNT - m_count;
+               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 getStackMax() const
+       {
+               return QUANTITY_ITEM_MAX_COUNT;
+       }
+
        /*
                Other properties
        */
        bool isCookable() const;
        InventoryItem *createCookResult() const;
+       float getCookTime() const;
+       float getBurnTime() const;
        /*
-               Special methods
+               Special properties (not part of virtual interface)
        */
-       content_t getMaterial()
-       {
-               return m_content;
-       }
+       std::string getNodeName() const
+       { return m_nodename; }
+       content_t getMaterial() const;
 private:
-       content_t m_content;
+       std::string m_nodename;
 };
 
 /*
@@ -213,10 +249,10 @@ class CraftItem : public InventoryItem
        }
        virtual void serialize(std::ostream &os) const
        {
-               os<<getName();
-               os<<" ";
+               os<<"craft";
+               os<<" \"";
                os<<m_subname;
-               os<<" ";
+               os<<"\" ";
                os<<m_count;
        }
        virtual InventoryItem* clone()
@@ -224,7 +260,7 @@ class CraftItem : public InventoryItem
                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()
        {
@@ -233,9 +269,6 @@ class CraftItem : public InventoryItem
                return os.str();
        }
 
-       ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
-       u16 getDropCount() const;
-
        virtual bool addableTo(const InventoryItem *other) const
        {
                if(std::string(other->getName()) != "CraftItem")
@@ -245,22 +278,44 @@ class CraftItem : public InventoryItem
                        return false;
                return true;
        }
-       u16 freeSpace() const
+       virtual bool isSubsetOf(const InventoryItem *other) const
        {
-               if(m_count > QUANTITY_ITEM_MAX_COUNT)
-                       return 0;
-               return QUANTITY_ITEM_MAX_COUNT - m_count;
+               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;
        }
 
        /*
                Other properties
        */
 
+       u16 getStackMax() const;
+       bool isUsable() const;
        bool isCookable() const;
        InventoryItem *createCookResult() const;
+       float getCookTime() const;
+       float getBurnTime() const;
+       s16 getDropCount() const;
+       bool areLiquidsPointable() const;
+
+       bool dropOrPlace(ServerEnvironment *env,
+                       ServerActiveObject *dropper,
+                       v3f pos, bool place, s16 count);
+       bool use(ServerEnvironment *env,
+                       ServerActiveObject *user,
+                       const PointedThing& pointed);
 
-       bool use(ServerEnvironment *env, ServerActiveObject *user);
-       
        /*
                Special methods
        */
@@ -290,10 +345,10 @@ class ToolItem : public InventoryItem
        }
        virtual void serialize(std::ostream &os) const
        {
-               os<<getName();
-               os<<" ";
+               os<<"tool";
+               os<<" \"";
                os<<m_toolname;
-               os<<" ";
+               os<<"\" ";
                os<<m_wear;
        }
        virtual InventoryItem* clone()
@@ -303,30 +358,33 @@ class ToolItem : public InventoryItem
 
        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
        */
@@ -493,6 +551,7 @@ class InventoryManager
 };
 
 #define IACTION_MOVE 0
+#define IACTION_DROP 1
 
 struct InventoryAction
 {
@@ -500,7 +559,8 @@ struct InventoryAction
        
        virtual u16 getType() const = 0;
        virtual void serialize(std::ostream &os) const = 0;
-       virtual void apply(InventoryContext *c, InventoryManager *mgr) = 0;
+       virtual void apply(InventoryContext *c, InventoryManager *mgr,
+                       ServerEnvironment *env) = 0;
 };
 
 struct IMoveAction : public InventoryAction
@@ -520,27 +580,8 @@ struct IMoveAction : public InventoryAction
                from_i = -1;
                to_i = -1;
        }
-       IMoveAction(std::istream &is)
-       {
-               std::string ts;
-
-               std::getline(is, ts, ' ');
-               count = stoi(ts);
-
-               std::getline(is, from_inv, ' ');
-
-               std::getline(is, from_list, ' ');
-
-               std::getline(is, ts, ' ');
-               from_i = stoi(ts);
-
-               std::getline(is, to_inv, ' ');
-
-               std::getline(is, to_list, ' ');
-
-               std::getline(is, ts, ' ');
-               to_i = stoi(ts);
-       }
+       
+       IMoveAction(std::istream &is);
 
        u16 getType() const
        {
@@ -559,7 +600,42 @@ struct IMoveAction : public InventoryAction
                os<<to_i;
        }
 
-       void apply(InventoryContext *c, InventoryManager *mgr);
+       void apply(InventoryContext *c, InventoryManager *mgr,
+                       ServerEnvironment *env);
+};
+
+struct IDropAction : public InventoryAction
+{
+       // count=0 means "everything"
+       u16 count;
+       std::string from_inv;
+       std::string from_list;
+       s16 from_i;
+       
+       IDropAction()
+       {
+               count = 0;
+               from_i = -1;
+       }
+       
+       IDropAction(std::istream &is);
+
+       u16 getType() const
+       {
+               return IACTION_DROP;
+       }
+
+       void serialize(std::ostream &os) const
+       {
+               os<<"Drop ";
+               os<<count<<" ";
+               os<<from_inv<<" ";
+               os<<from_list<<" ";
+               os<<from_i;
+       }
+
+       void apply(InventoryContext *c, InventoryManager *mgr,
+                       ServerEnvironment *env);
 };
 
 /*
@@ -608,5 +684,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