Improved MaterialItem (stores nodename)

This commit is contained in:
Perttu Ahola 2011-11-16 14:36:33 +02:00
parent df8346ef4d
commit 7a29b14a20
4 changed files with 74 additions and 21 deletions

View 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
*/

View 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;
};
/*

View 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)
{

View 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;