Improved MaterialItem (stores nodename)
authorPerttu Ahola <celeron55@gmail.com>
Wed, 16 Nov 2011 12:36:33 +0000 (14:36 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Tue, 29 Nov 2011 17:13:47 +0000 (19:13 +0200)
src/inventory.cpp
src/inventory.h
src/nodedef.cpp
src/nodedef.h

index 98bf57738ef0edc02a42d9836361c8951d671a60..b0b128418e515fa9bb05d01f5789e404e42cde77 100644 (file)
@@ -32,6 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "nodedef.h"
 #include "tooldef.h"
 #include "gamedef.h"
+#include "strfnd.h"
 
 /*
        InventoryItem
@@ -95,6 +96,16 @@ InventoryItem* InventoryItem::deSerialize(std::istream &is, IGameDef *gamedef)
                        throw SerializationError("Too large material number");
                return new MaterialItem(gamedef, material, count);
        }
+       else if(name == "MaterialItem3")
+       {
+               std::string all;
+               std::getline(is, all, '\n');
+               Strfnd fnd(all);
+               fnd.next("\"");
+               std::string nodename = fnd.next("\"");
+               u16 count = stoi(trim(fnd.next("")));
+               return new MaterialItem(gamedef, nodename, count);
+       }
        else if(name == "MBOItem")
        {
                std::string inventorystring;
@@ -149,24 +160,42 @@ ServerActiveObject* InventoryItem::createSAO(ServerEnvironment *env, u16 id, v3f
        MaterialItem
 */
 
+MaterialItem::MaterialItem(IGameDef *gamedef, std::string nodename, u16 count):
+       InventoryItem(gamedef, count)
+{
+       if(nodename == "")
+               nodename = "unknown_block";
+       m_nodename = nodename;
+}
+// Legacy constructor
+MaterialItem::MaterialItem(IGameDef *gamedef, content_t content, u16 count):
+       InventoryItem(gamedef, count)
+{
+       INodeDefManager *ndef = m_gamedef->ndef();
+       std::string nodename = ndef->get(content).name;
+       if(nodename == "")
+               nodename = "unknown_block";
+       m_nodename = nodename;
+}
+
 #ifndef SERVER
 video::ITexture * MaterialItem::getImage(ITextureSource *tsrc) const
 {
-       return m_gamedef->getNodeDefManager()->get(m_content).inventory_texture;
+       return m_gamedef->getNodeDefManager()->get(m_nodename).inventory_texture;
 }
 #endif
 
 bool MaterialItem::isCookable() const
 {
        INodeDefManager *ndef = m_gamedef->ndef();
-       const ContentFeatures &f = ndef->get(m_content);
+       const ContentFeatures &f = ndef->get(m_nodename);
        return (f.cookresult_item != "");
 }
 
 InventoryItem *MaterialItem::createCookResult() const
 {
        INodeDefManager *ndef = m_gamedef->ndef();
-       const ContentFeatures &f = ndef->get(m_content);
+       const ContentFeatures &f = ndef->get(m_nodename);
        std::istringstream is(f.cookresult_item, std::ios::binary);
        return InventoryItem::deSerialize(is, m_gamedef);
 }
@@ -174,17 +203,25 @@ InventoryItem *MaterialItem::createCookResult() const
 float MaterialItem::getCookTime() const
 {
        INodeDefManager *ndef = m_gamedef->ndef();
-       const ContentFeatures &f = ndef->get(m_content);
+       const ContentFeatures &f = ndef->get(m_nodename);
        return f.furnace_cooktime;
 }
 
 float MaterialItem::getBurnTime() const
 {
        INodeDefManager *ndef = m_gamedef->ndef();
-       const ContentFeatures &f = ndef->get(m_content);
+       const ContentFeatures &f = ndef->get(m_nodename);
        return f.furnace_burntime;
 }
 
+content_t MaterialItem::getMaterial() const
+{
+       INodeDefManager *ndef = m_gamedef->ndef();
+       content_t id = CONTENT_IGNORE;
+       ndef->getId(m_nodename, id);
+       return id;
+}
+
 /*
        ToolItem
 */
index b9de48f9af641de8189b54ac13cf0463cc27a23b..32e1cd93e7f87043fbf4d5091c818b59fcc095f1 100644 (file)
@@ -127,11 +127,9 @@ protected:
 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
        */
@@ -141,16 +139,26 @@ public:
        }
        virtual void serialize(std::ostream &os) const
        {
-               //os.imbue(std::locale("C"));
-               os<<"MaterialItem2";
+               std::string nodename = m_nodename;
+               if(nodename == "")
+                       nodename = "unknown_block";
+                       
+               os<<"MaterialItem3";
+               os<<" \"";
+               os<<nodename;
+               os<<"\" ";
+               os<<m_count;
+
+               // Old
+               /*os<<"MaterialItem2";
                os<<" ";
                os<<(unsigned int)m_content;
                os<<" ";
-               os<<m_count;
+               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;
@@ -167,7 +175,7 @@ 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;
        }
@@ -185,14 +193,13 @@ public:
        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;
 };
 
 /*
index baaaa1048ae987e8be1abc249d67b9dbb84d74d2..565e3cb3413af7a0cf300c0b80b6ca284526ae26 100644 (file)
@@ -406,6 +406,12 @@ public:
        {
                return m_name_id_mapping.getId(name, result);
        }
+       virtual const ContentFeatures& get(const std::string &name) const
+       {
+               content_t id = CONTENT_IGNORE;
+               getId(name, id);
+               return get(id);
+       }
        // IWritableNodeDefManager
        virtual void set(content_t c, const ContentFeatures &def)
        {
index 055ed7a4b46b571d52539388aa9232b665152077..54722f5af5ee08bf5b4a312882450ccd0ee7632b 100644 (file)
@@ -254,6 +254,7 @@ public:
        virtual const ContentFeatures& get(content_t c) const=0;
        virtual const ContentFeatures& get(const MapNode &n) const=0;
        virtual bool getId(const std::string &name, content_t &result) const=0;
+       virtual const ContentFeatures& get(const std::string &name) const=0;
        
        virtual void serialize(std::ostream &os)=0;
 };
@@ -268,6 +269,8 @@ public:
        virtual const ContentFeatures& get(content_t c) const=0;
        virtual const ContentFeatures& get(const MapNode &n) const=0;
        virtual bool getId(const std::string &name, content_t &result) const=0;
+       // If not found, returns the features of CONTENT_IGNORE
+       virtual const ContentFeatures& get(const std::string &name) const=0;
                
        // Register node definition
        virtual void set(content_t c, const ContentFeatures &def)=0;