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"
32 core::map<u16, NodeMetadata::Factory> NodeMetadata::m_types;
34 NodeMetadata::NodeMetadata()
38 NodeMetadata::~NodeMetadata()
42 NodeMetadata* NodeMetadata::deSerialize(std::istream &is)
46 is.read((char*)buf, 2);
47 s16 id = readS16(buf);
50 std::string data = deSerializeString(is);
52 // Find factory function
53 core::map<u16, Factory>::Node *n;
57 // If factory is not found, just return.
58 dstream<<"WARNING: NodeMetadata: No factory for typeId="
63 // Try to load the metadata. If it fails, just return.
66 std::istringstream iss(data, std::ios_base::binary);
68 Factory f = n->getValue();
69 NodeMetadata *meta = (*f)(iss);
72 catch(SerializationError &e)
74 dstream<<"WARNING: NodeMetadata: ignoring SerializationError"<<std::endl;
79 void NodeMetadata::serialize(std::ostream &os)
82 writeU16(buf, typeId());
83 os.write((char*)buf, 2);
85 std::ostringstream oss(std::ios_base::binary);
87 os<<serializeString(oss.str());
90 void NodeMetadata::registerType(u16 id, Factory f)
92 core::map<u16, Factory>::Node *n;
96 m_types.insert(id, f);
103 void NodeMetadataList::serialize(std::ostream &os)
108 writeU16(buf, version);
109 os.write((char*)buf, 2);
111 u16 count = m_data.size();
112 writeU16(buf, count);
113 os.write((char*)buf, 2);
115 for(core::map<v3s16, NodeMetadata*>::Iterator
116 i = m_data.getIterator();
117 i.atEnd()==false; i++)
119 v3s16 p = i.getNode()->getKey();
120 NodeMetadata *data = i.getNode()->getValue();
122 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
124 os.write((char*)buf, 2);
130 void NodeMetadataList::deSerialize(std::istream &is)
136 is.read((char*)buf, 2);
137 u16 version = readU16(buf);
141 dstream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
143 throw SerializationError("NodeMetadataList::deSerialize");
146 is.read((char*)buf, 2);
147 u16 count = readU16(buf);
149 for(u16 i=0; i<count; i++)
151 is.read((char*)buf, 2);
152 u16 p16 = readU16(buf);
155 p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
156 p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
157 p.Y += p16 / MAP_BLOCKSIZE;
158 p16 -= p.Y * MAP_BLOCKSIZE;
161 NodeMetadata *data = NodeMetadata::deSerialize(is);
168 dstream<<"WARNING: NodeMetadataList::deSerialize(): "
169 <<"already set data at position"
170 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
176 m_data.insert(p, data);
180 NodeMetadataList::~NodeMetadataList()
182 for(core::map<v3s16, NodeMetadata*>::Iterator
183 i = m_data.getIterator();
184 i.atEnd()==false; i++)
186 delete i.getNode()->getValue();
190 NodeMetadata* NodeMetadataList::get(v3s16 p)
192 core::map<v3s16, NodeMetadata*>::Node *n;
196 return n->getValue();
199 void NodeMetadataList::remove(v3s16 p)
201 NodeMetadata *olddata = get(p);
209 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
215 bool NodeMetadataList::step(float dtime)
217 bool something_changed = false;
218 for(core::map<v3s16, NodeMetadata*>::Iterator
219 i = m_data.getIterator();
220 i.atEnd()==false; i++)
222 v3s16 p = i.getNode()->getKey();
223 NodeMetadata *meta = i.getNode()->getValue();
224 bool changed = meta->step(dtime);
226 something_changed = true;
228 return something_changed;