Falling sand and gravel
[oweals/minetest.git] / src / inventory.h
index c202d5533125c860dbf15030c2da54840f592ec3..d6049f52fd9ca0fb943da77b8fd0c5453413dfeb 100644 (file)
@@ -1,6 +1,6 @@
 /*
 Minetest-c55
-Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -17,10 +17,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-/*
-(c) 2010 Perttu Ahola <celeron55@gmail.com>
-*/
-
 #ifndef INVENTORY_HEADER
 #define INVENTORY_HEADER
 
@@ -29,34 +25,37 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <string>
 #include "common_irrlicht.h"
 #include "debug.h"
-#include "main.h" // For g_materials
 #include "mapnode.h" // For content_t
 
 #define QUANTITY_ITEM_MAX_COUNT 99
 
 class ServerActiveObject;
 class ServerEnvironment;
+class ITextureSource;
+class IGameDef;
 
 class InventoryItem
 {
 public:
-       InventoryItem(u16 count);
+       InventoryItem(IGameDef *gamedef, u16 count);
        virtual ~InventoryItem();
        
-       static InventoryItem* deSerialize(std::istream &is);
+       static InventoryItem* deSerialize(std::istream &is, IGameDef *gamedef);
        
        virtual const char* getName() const = 0;
        // Shall write the name and the parameters
        virtual void serialize(std::ostream &os) const = 0;
        // Shall make an exact clone of the item
        virtual InventoryItem* clone() = 0;
-#ifndef SERVER
        // Return the name of the image for this item
-       virtual std::string getBasename() const { return ""; }
+       virtual std::string getImageBasename() const { return ""; }
+#ifndef SERVER
        // Shall return an image of the item (or NULL)
-       virtual video::ITexture * getImage() const { return NULL; }
+       virtual video::ITexture * getImage() const
+               { return NULL; }
        // Shall return an image of the item without embellishments (or NULL)
-       virtual video::ITexture * getImageRaw() const { return getImage(); }
+       virtual video::ITexture * getImageRaw() const
+               { return getImage(); }
 #endif
        // Shall return a text to show in the GUI
        virtual std::string getText() { return ""; }
@@ -71,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);
@@ -107,10 +107,12 @@ public:
 
        // 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;}
        
        // Eat, press, activate, whatever.
        // Called when item is right-clicked when lying on ground.
@@ -119,17 +121,16 @@ public:
                        ServerActiveObject *user){return false;}
 
 protected:
+       IGameDef *m_gamedef;
        u16 m_count;
 };
 
 class MaterialItem : public InventoryItem
 {
 public:
-       MaterialItem(content_t content, u16 count):
-               InventoryItem(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,16 +140,15 @@ public:
        }
        virtual void serialize(std::ostream &os) const
        {
-               //os.imbue(std::locale("C"));
-               os<<"MaterialItem2";
-               os<<" ";
-               os<<(unsigned int)m_content;
-               os<<" ";
+               os<<"NodeItem";
+               os<<" \"";
+               os<<m_nodename;
+               os<<"\" ";
                os<<m_count;
        }
        virtual InventoryItem* clone()
        {
-               return new MaterialItem(m_content, m_count);
+               return new MaterialItem(m_gamedef, m_nodename, m_count);
        }
 #ifndef SERVER
        video::ITexture * getImage() const;
@@ -165,10 +165,28 @@ public:
                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;
+       }
+       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)
@@ -180,15 +198,16 @@ public:
        */
        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;
 };
 
 /*
@@ -199,8 +218,8 @@ private:
 class CraftItem : public InventoryItem
 {
 public:
-       CraftItem(std::string subname, u16 count):
-               InventoryItem(count)
+       CraftItem(IGameDef *gamedef, std::string subname, u16 count):
+               InventoryItem(gamedef, count)
        {
                m_subname = subname;
        }
@@ -214,14 +233,14 @@ public:
        virtual void serialize(std::ostream &os) const
        {
                os<<getName();
-               os<<" ";
+               os<<" \"";
                os<<m_subname;
-               os<<" ";
+               os<<"\" ";
                os<<m_count;
        }
        virtual InventoryItem* clone()
        {
-               return new CraftItem(m_subname, m_count);
+               return new CraftItem(m_gamedef, m_subname, m_count);
        }
 #ifndef SERVER
        video::ITexture * getImage() const;
@@ -245,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)
@@ -258,6 +295,8 @@ public:
 
        bool isCookable() const;
        InventoryItem *createCookResult() const;
+       float getCookTime() const;
+       float getBurnTime() const;
 
        bool use(ServerEnvironment *env, ServerActiveObject *user);
        
@@ -275,8 +314,8 @@ private:
 class ToolItem : public InventoryItem
 {
 public:
-       ToolItem(std::string toolname, u16 wear):
-               InventoryItem(1)
+       ToolItem(IGameDef *gamedef, std::string toolname, u16 wear):
+               InventoryItem(gamedef, 1)
        {
                m_toolname = toolname;
                m_wear = wear;
@@ -291,96 +330,45 @@ public:
        virtual void serialize(std::ostream &os) const
        {
                os<<getName();
-               os<<" ";
+               os<<" \"";
                os<<m_toolname;
-               os<<" ";
+               os<<"\" ";
                os<<m_wear;
        }
        virtual InventoryItem* clone()
        {
-               return new ToolItem(m_toolname, m_wear);
+               return new ToolItem(m_gamedef, m_toolname, m_wear);
        }
+
+       std::string getImageBasename() const;
 #ifndef SERVER
-       std::string getBasename() const {
-               if(m_toolname == "WPick")
-                       return "tool_woodpick.png";
-               else if(m_toolname == "STPick")
-                       return "tool_stonepick.png";
-               else if(m_toolname == "SteelPick")
-                       return "tool_steelpick.png";
-               else if(m_toolname == "MesePick")
-                       return "tool_mesepick.png";
-               else if(m_toolname == "WShovel")
-                       return "tool_woodshovel.png";
-               else if(m_toolname == "STShovel")
-                       return "tool_stoneshovel.png";
-               else if(m_toolname == "SteelShovel")
-                       return "tool_steelshovel.png";
-               else if(m_toolname == "WAxe")
-                       return "tool_woodaxe.png";
-               else if(m_toolname == "STAxe")
-                       return "tool_stoneaxe.png";
-               else if(m_toolname == "SteelAxe")
-                       return "tool_steelaxe.png";
-               else if(m_toolname == "WSword")
-                       return "tool_woodsword.png";
-               else if(m_toolname == "STSword")
-                       return "tool_stonesword.png";
-               else if(m_toolname == "SteelSword")
-                       return "tool_steelsword.png";
-               else
-                       return "cloud.png";
-}
-       
-       video::ITexture * getImage() const
-       {
-               if(g_texturesource == NULL)
-                       return NULL;
-               
-               std::string basename = getBasename();
-               
-               /*
-                       Calculate a progress value with sane amount of
-                       maximum states
-               */
-               u32 maxprogress = 30;
-               u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
-               
-               float value_f = (float)toolprogress / (float)maxprogress;
-               std::ostringstream os;
-               os<<basename<<"^[progressbar"<<value_f;
+       video::ITexture * getImage() const;
+       video::ITexture * getImageRaw() const;
+#endif
 
-               return g_texturesource->getTextureRaw(os.str());
+       std::string getText()
+       {
+               return "";
        }
 
-       video::ITexture * getImageRaw() const
+       virtual bool isSubsetOf(const InventoryItem *other) const
        {
-               if(g_texturesource == NULL)
-                       return NULL;
-               
-               return g_texturesource->getTextureRaw(getBasename());
+               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;
        }
-#endif
-       std::string getText()
+       virtual bool removeOther(const InventoryItem *other)
        {
-               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();*/
+               if(!other->isSubsetOf(this))
+                       return false;
+               ToolItem *m = (ToolItem*)other;
+               m_wear -= m->m_wear;
+               return true;
        }
+
        /*
                Special methods
        */
@@ -418,7 +406,7 @@ public:
        ~InventoryList();
        void clearItems();
        void serialize(std::ostream &os) const;
-       void deSerialize(std::istream &is);
+       void deSerialize(std::istream &is, IGameDef *gamedef);
 
        InventoryList(const InventoryList &other);
        InventoryList & operator = (const InventoryList &other);
@@ -488,7 +476,7 @@ public:
        Inventory & operator = (const Inventory &other);
        
        void serialize(std::ostream &os) const;
-       void deSerialize(std::istream &is);
+       void deSerialize(std::istream &is, IGameDef *gamedef);
 
        InventoryList * addList(const std::string &name, u32 size);
        InventoryList * getList(const std::string &name);
@@ -574,27 +562,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
        {
@@ -662,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