#include "mapnode.h"
#include "exceptions.h"
#include "inventory.h"
+#include <sstream>
+#include "content_mapnode.h"
+#include "log.h"
/*
NodeMetadata
*/
-core::map<u16, NodeMetadata::Factory> NodeMetadata::m_types;
-
-NodeMetadata::NodeMetadata()
+NodeMetadata::NodeMetadata(IGameDef *gamedef):
+ m_gamedef(gamedef)
{
}
{
}
-NodeMetadata* NodeMetadata::deSerialize(std::istream &is)
+NodeMetadata* NodeMetadata::create(const std::string &name, IGameDef *gamedef)
+{
+ // Find factory function
+ core::map<std::string, Factory2>::Node *n;
+ n = m_names.find(name);
+ if(n == NULL)
+ {
+ // If factory is not found, just return.
+ errorstream<<"WARNING: NodeMetadata: No factory for name=\""
+ <<name<<"\""<<std::endl;
+ return NULL;
+ }
+
+ // Try to load the metadata. If it fails, just return.
+ try
+ {
+ Factory2 f2 = n->getValue();
+ NodeMetadata *meta = (*f2)(gamedef);
+ return meta;
+ }
+ catch(SerializationError &e)
+ {
+ errorstream<<"NodeMetadata: SerializationError "
+ <<"while creating name=\""<<name<<"\""<<std::endl;
+ return NULL;
+ }
+}
+
+NodeMetadata* NodeMetadata::deSerialize(std::istream &is, IGameDef *gamedef)
{
+ // Read id
u8 buf[2];
is.read((char*)buf, 2);
s16 id = readS16(buf);
-
+
+ // Read data
+ std::string data = deSerializeString(is);
+
+ // Find factory function
core::map<u16, Factory>::Node *n;
n = m_types.find(id);
if(n == NULL)
{
- dstream<<"NodeMetadata(): No factory for typeId="<<id<<std::endl;
- throw SerializationError("Unknown metadata id");
+ // If factory is not found, just return.
+ infostream<<"WARNING: NodeMetadata: No factory for typeId="
+ <<id<<std::endl;
+ return NULL;
}
- Factory f = n->getValue();
- NodeMetadata *meta = (*f)(is);
- return meta;
+ // Try to load the metadata. If it fails, just return.
+ try
+ {
+ std::istringstream iss(data, std::ios_base::binary);
+
+ Factory f = n->getValue();
+ NodeMetadata *meta = (*f)(iss, gamedef);
+ return meta;
+ }
+ catch(SerializationError &e)
+ {
+ infostream<<"WARNING: NodeMetadata: ignoring SerializationError"<<std::endl;
+ return NULL;
+ }
}
void NodeMetadata::serialize(std::ostream &os)
u8 buf[2];
writeU16(buf, typeId());
os.write((char*)buf, 2);
-
- serializeBody(os);
-}
-
-void NodeMetadata::registerType(u16 id, Factory f)
-{
- core::map<u16, Factory>::Node *n;
- n = m_types.find(id);
- if(n)
- return;
- m_types.insert(id, f);
-}
-
-/*
- SignNodeMetadata
-*/
-
-SignNodeMetadata::SignNodeMetadata(std::string text):
- m_text(text)
-{
- NodeMetadata::registerType(typeId(), create);
-}
-u16 SignNodeMetadata::typeId() const
-{
- return CONTENT_SIGN_WALL;
-}
-NodeMetadata* SignNodeMetadata::create(std::istream &is)
-{
- std::string text = deSerializeString(is);
- return new SignNodeMetadata(text);
-}
-NodeMetadata* SignNodeMetadata::clone()
-{
- return new SignNodeMetadata(m_text);
-}
-void SignNodeMetadata::serializeBody(std::ostream &os)
-{
- os<<serializeString(m_text);
-}
-std::string SignNodeMetadata::infoText()
-{
- return std::string("\"")+m_text+"\"";
-}
-
-/*
- ChestNodeMetadata
-*/
-
-ChestNodeMetadata::ChestNodeMetadata()
-{
- NodeMetadata::registerType(typeId(), create);
- m_inventory = new Inventory();
- m_inventory->addList("0", 8*4);
-}
-ChestNodeMetadata::~ChestNodeMetadata()
-{
- delete m_inventory;
+ std::ostringstream oss(std::ios_base::binary);
+ serializeBody(oss);
+ os<<serializeString(oss.str());
}
-u16 ChestNodeMetadata::typeId() const
-{
- return CONTENT_CHEST;
-}
-NodeMetadata* ChestNodeMetadata::create(std::istream &is)
-{
- ChestNodeMetadata *d = new ChestNodeMetadata();
- d->m_inventory->deSerialize(is);
- return d;
-}
-NodeMetadata* ChestNodeMetadata::clone()
-{
- ChestNodeMetadata *d = new ChestNodeMetadata();
- *d->m_inventory = *m_inventory;
- return d;
-}
-void ChestNodeMetadata::serializeBody(std::ostream &os)
-{
- m_inventory->serialize(os);
-}
-std::string ChestNodeMetadata::infoText()
+
+void NodeMetadata::registerType(u16 id, const std::string &name, Factory f,
+ Factory2 f2)
{
- return "Chest";
+ { // typeId
+ core::map<u16, Factory>::Node *n;
+ n = m_types.find(id);
+ if(!n)
+ m_types.insert(id, f);
+ }
+ { // typeName
+ core::map<std::string, Factory2>::Node *n;
+ n = m_names.find(name);
+ if(!n)
+ m_names.insert(name, f2);
+ }
}
-/*Inventory* ChestNodeMetadata::getInventory()
-{
- return m_inventory;
-}*/
/*
- NodeMetadatalist
+ NodeMetadataList
*/
void NodeMetadataList::serialize(std::ostream &os)
{
u8 buf[6];
+ u16 version = 1;
+ writeU16(buf, version);
+ os.write((char*)buf, 2);
+
u16 count = m_data.size();
writeU16(buf, count);
os.write((char*)buf, 2);
}
}
-void NodeMetadataList::deSerialize(std::istream &is)
+void NodeMetadataList::deSerialize(std::istream &is, IGameDef *gamedef)
{
m_data.clear();
u8 buf[6];
+ is.read((char*)buf, 2);
+ u16 version = readU16(buf);
+
+ if(version > 1)
+ {
+ infostream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
+ <<std::endl;
+ throw SerializationError("NodeMetadataList::deSerialize");
+ }
+
is.read((char*)buf, 2);
u16 count = readU16(buf);
p16 -= p.Y * MAP_BLOCKSIZE;
p.X += p16;
+ NodeMetadata *data = NodeMetadata::deSerialize(is, gamedef);
+
+ if(data == NULL)
+ continue;
+
if(m_data.find(p))
{
- dstream<<"ERROR: NodeMetadataList::deSerialize(): "
+ infostream<<"WARNING: NodeMetadataList::deSerialize(): "
<<"already set data at position"
- <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
+ <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
<<std::endl;
- throw SerializationError("NodeMetadataList::deSerialize()");
+ delete data;
+ continue;
}
- NodeMetadata *data = NodeMetadata::deSerialize(is);
-
m_data.insert(p, data);
}
}
m_data.insert(p, d);
}
+bool NodeMetadataList::step(float dtime)
+{
+ bool something_changed = false;
+ for(core::map<v3s16, NodeMetadata*>::Iterator
+ i = m_data.getIterator();
+ i.atEnd()==false; i++)
+ {
+ v3s16 p = i.getNode()->getKey();
+ NodeMetadata *meta = i.getNode()->getValue();
+ bool changed = meta->step(dtime);
+ if(changed)
+ something_changed = true;
+ }
+ return something_changed;
+}
+