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(IGameDef *gamedef):
40 NodeMetadata::~NodeMetadata()
44 NodeMetadata* NodeMetadata::deSerialize(std::istream &is, IGameDef *gamedef)
48 is.read((char*)buf, 2);
49 s16 id = readS16(buf);
52 std::string data = deSerializeString(is);
54 // Find factory function
55 core::map<u16, Factory>::Node *n;
59 // If factory is not found, just return.
60 infostream<<"WARNING: NodeMetadata: No factory for typeId="
65 // Try to load the metadata. If it fails, just return.
68 std::istringstream iss(data, std::ios_base::binary);
70 Factory f = n->getValue();
71 NodeMetadata *meta = (*f)(iss, gamedef);
74 catch(SerializationError &e)
76 infostream<<"WARNING: NodeMetadata: ignoring SerializationError"<<std::endl;
81 void NodeMetadata::serialize(std::ostream &os)
84 writeU16(buf, typeId());
85 os.write((char*)buf, 2);
87 std::ostringstream oss(std::ios_base::binary);
89 os<<serializeString(oss.str());
92 void NodeMetadata::registerType(u16 id, Factory f)
94 core::map<u16, Factory>::Node *n;
98 m_types.insert(id, f);
105 void NodeMetadataList::serialize(std::ostream &os)
110 writeU16(buf, version);
111 os.write((char*)buf, 2);
113 u16 count = m_data.size();
114 writeU16(buf, count);
115 os.write((char*)buf, 2);
117 for(core::map<v3s16, NodeMetadata*>::Iterator
118 i = m_data.getIterator();
119 i.atEnd()==false; i++)
121 v3s16 p = i.getNode()->getKey();
122 NodeMetadata *data = i.getNode()->getValue();
124 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
126 os.write((char*)buf, 2);
132 void NodeMetadataList::deSerialize(std::istream &is, IGameDef *gamedef)
138 is.read((char*)buf, 2);
139 u16 version = readU16(buf);
143 infostream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
145 throw SerializationError("NodeMetadataList::deSerialize");
148 is.read((char*)buf, 2);
149 u16 count = readU16(buf);
151 for(u16 i=0; i<count; i++)
153 is.read((char*)buf, 2);
154 u16 p16 = readU16(buf);
157 p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
158 p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
159 p.Y += p16 / MAP_BLOCKSIZE;
160 p16 -= p.Y * MAP_BLOCKSIZE;
163 NodeMetadata *data = NodeMetadata::deSerialize(is, gamedef);
170 infostream<<"WARNING: NodeMetadataList::deSerialize(): "
171 <<"already set data at position"
172 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
178 m_data.insert(p, data);
182 NodeMetadataList::~NodeMetadataList()
184 for(core::map<v3s16, NodeMetadata*>::Iterator
185 i = m_data.getIterator();
186 i.atEnd()==false; i++)
188 delete i.getNode()->getValue();
192 NodeMetadata* NodeMetadataList::get(v3s16 p)
194 core::map<v3s16, NodeMetadata*>::Node *n;
198 return n->getValue();
201 void NodeMetadataList::remove(v3s16 p)
203 NodeMetadata *olddata = get(p);
211 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
217 bool NodeMetadataList::step(float dtime)
219 bool something_changed = false;
220 for(core::map<v3s16, NodeMetadata*>::Iterator
221 i = m_data.getIterator();
222 i.atEnd()==false; i++)
224 v3s16 p = i.getNode()->getKey();
225 NodeMetadata *meta = i.getNode()->getValue();
226 bool changed = meta->step(dtime);
228 something_changed = true;
230 return something_changed;