3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "nodemetadata.h"
23 #include "exceptions.h"
24 #include "inventory.h"
26 #include "content_mapnode.h"
33 core::map<u16, NodeMetadata::Factory> NodeMetadata::m_types;
35 NodeMetadata::NodeMetadata()
39 NodeMetadata::~NodeMetadata()
43 NodeMetadata* NodeMetadata::deSerialize(std::istream &is)
47 is.read((char*)buf, 2);
48 s16 id = readS16(buf);
51 std::string data = deSerializeString(is);
53 // Find factory function
54 core::map<u16, Factory>::Node *n;
58 // If factory is not found, just return.
59 infostream<<"WARNING: NodeMetadata: No factory for typeId="
64 // Try to load the metadata. If it fails, just return.
67 std::istringstream iss(data, std::ios_base::binary);
69 Factory f = n->getValue();
70 NodeMetadata *meta = (*f)(iss);
73 catch(SerializationError &e)
75 infostream<<"WARNING: NodeMetadata: ignoring SerializationError"<<std::endl;
80 void NodeMetadata::serialize(std::ostream &os)
83 writeU16(buf, typeId());
84 os.write((char*)buf, 2);
86 std::ostringstream oss(std::ios_base::binary);
88 os<<serializeString(oss.str());
91 void NodeMetadata::registerType(u16 id, Factory f)
93 core::map<u16, Factory>::Node *n;
97 m_types.insert(id, f);
104 void NodeMetadataList::serialize(std::ostream &os)
109 writeU16(buf, version);
110 os.write((char*)buf, 2);
112 u16 count = m_data.size();
113 writeU16(buf, count);
114 os.write((char*)buf, 2);
116 for(core::map<v3s16, NodeMetadata*>::Iterator
117 i = m_data.getIterator();
118 i.atEnd()==false; i++)
120 v3s16 p = i.getNode()->getKey();
121 NodeMetadata *data = i.getNode()->getValue();
123 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
125 os.write((char*)buf, 2);
131 void NodeMetadataList::deSerialize(std::istream &is)
137 is.read((char*)buf, 2);
138 u16 version = readU16(buf);
142 infostream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
144 throw SerializationError("NodeMetadataList::deSerialize");
147 is.read((char*)buf, 2);
148 u16 count = readU16(buf);
150 for(u16 i=0; i<count; i++)
152 is.read((char*)buf, 2);
153 u16 p16 = readU16(buf);
156 p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
157 p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
158 p.Y += p16 / MAP_BLOCKSIZE;
159 p16 -= p.Y * MAP_BLOCKSIZE;
162 NodeMetadata *data = NodeMetadata::deSerialize(is);
169 infostream<<"WARNING: NodeMetadataList::deSerialize(): "
170 <<"already set data at position"
171 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
177 m_data.insert(p, data);
181 NodeMetadataList::~NodeMetadataList()
183 for(core::map<v3s16, NodeMetadata*>::Iterator
184 i = m_data.getIterator();
185 i.atEnd()==false; i++)
187 delete i.getNode()->getValue();
191 NodeMetadata* NodeMetadataList::get(v3s16 p)
193 core::map<v3s16, NodeMetadata*>::Node *n;
197 return n->getValue();
200 void NodeMetadataList::remove(v3s16 p)
202 NodeMetadata *olddata = get(p);
210 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
216 bool NodeMetadataList::step(float dtime)
218 bool something_changed = false;
219 for(core::map<v3s16, NodeMetadata*>::Iterator
220 i = m_data.getIterator();
221 i.atEnd()==false; i++)
223 v3s16 p = i.getNode()->getKey();
224 NodeMetadata *meta = i.getNode()->getValue();
225 bool changed = meta->step(dtime);
227 something_changed = true;
229 return something_changed;