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;
34 core::map<std::string, NodeMetadata::Factory2> NodeMetadata::m_names;
36 NodeMetadata::NodeMetadata(IGameDef *gamedef):
41 NodeMetadata::~NodeMetadata()
45 NodeMetadata* NodeMetadata::create(const std::string &name, IGameDef *gamedef)
47 // Find factory function
48 core::map<std::string, Factory2>::Node *n;
49 n = m_names.find(name);
52 // If factory is not found, just return.
53 errorstream<<"WARNING: NodeMetadata: No factory for name=\""
54 <<name<<"\""<<std::endl;
58 // Try to load the metadata. If it fails, just return.
61 Factory2 f2 = n->getValue();
62 NodeMetadata *meta = (*f2)(gamedef);
65 catch(SerializationError &e)
67 errorstream<<"NodeMetadata: SerializationError "
68 <<"while creating name=\""<<name<<"\""<<std::endl;
73 NodeMetadata* NodeMetadata::deSerialize(std::istream &is, IGameDef *gamedef)
77 is.read((char*)buf, 2);
78 s16 id = readS16(buf);
81 std::string data = deSerializeString(is);
83 // Find factory function
84 core::map<u16, Factory>::Node *n;
88 // If factory is not found, just return.
89 infostream<<"WARNING: NodeMetadata: No factory for typeId="
94 // Try to load the metadata. If it fails, just return.
97 std::istringstream iss(data, std::ios_base::binary);
99 Factory f = n->getValue();
100 NodeMetadata *meta = (*f)(iss, gamedef);
103 catch(SerializationError &e)
105 infostream<<"WARNING: NodeMetadata: ignoring SerializationError"<<std::endl;
110 void NodeMetadata::serialize(std::ostream &os)
113 writeU16(buf, typeId());
114 os.write((char*)buf, 2);
116 std::ostringstream oss(std::ios_base::binary);
118 os<<serializeString(oss.str());
121 void NodeMetadata::registerType(u16 id, const std::string &name, Factory f,
125 core::map<u16, Factory>::Node *n;
126 n = m_types.find(id);
128 m_types.insert(id, f);
131 core::map<std::string, Factory2>::Node *n;
132 n = m_names.find(name);
134 m_names.insert(name, f2);
142 void NodeMetadataList::serialize(std::ostream &os)
147 writeU16(buf, version);
148 os.write((char*)buf, 2);
150 u16 count = m_data.size();
151 writeU16(buf, count);
152 os.write((char*)buf, 2);
154 for(core::map<v3s16, NodeMetadata*>::Iterator
155 i = m_data.getIterator();
156 i.atEnd()==false; i++)
158 v3s16 p = i.getNode()->getKey();
159 NodeMetadata *data = i.getNode()->getValue();
161 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
163 os.write((char*)buf, 2);
169 void NodeMetadataList::deSerialize(std::istream &is, IGameDef *gamedef)
175 is.read((char*)buf, 2);
176 u16 version = readU16(buf);
180 infostream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
182 throw SerializationError("NodeMetadataList::deSerialize");
185 is.read((char*)buf, 2);
186 u16 count = readU16(buf);
188 for(u16 i=0; i<count; i++)
190 is.read((char*)buf, 2);
191 u16 p16 = readU16(buf);
194 p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
195 p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
196 p.Y += p16 / MAP_BLOCKSIZE;
197 p16 -= p.Y * MAP_BLOCKSIZE;
200 NodeMetadata *data = NodeMetadata::deSerialize(is, gamedef);
207 infostream<<"WARNING: NodeMetadataList::deSerialize(): "
208 <<"already set data at position"
209 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
215 m_data.insert(p, data);
219 NodeMetadataList::~NodeMetadataList()
221 for(core::map<v3s16, NodeMetadata*>::Iterator
222 i = m_data.getIterator();
223 i.atEnd()==false; i++)
225 delete i.getNode()->getValue();
229 NodeMetadata* NodeMetadataList::get(v3s16 p)
231 core::map<v3s16, NodeMetadata*>::Node *n;
235 return n->getValue();
238 void NodeMetadataList::remove(v3s16 p)
240 NodeMetadata *olddata = get(p);
248 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
254 bool NodeMetadataList::step(float dtime)
256 bool something_changed = false;
257 for(core::map<v3s16, NodeMetadata*>::Iterator
258 i = m_data.getIterator();
259 i.atEnd()==false; i++)
261 v3s16 p = i.getNode()->getKey();
262 NodeMetadata *meta = i.getNode()->getValue();
263 bool changed = meta->step(dtime);
265 something_changed = true;
267 return something_changed;