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 NodeMetadata::NodeMetadata(IGameDef *gamedef):
38 NodeMetadata::~NodeMetadata()
42 NodeMetadata* NodeMetadata::create(const std::string &name, IGameDef *gamedef)
44 // Find factory function
45 core::map<std::string, Factory2>::Node *n;
46 n = m_names.find(name);
49 // If factory is not found, just return.
50 errorstream<<"WARNING: NodeMetadata: No factory for name=\""
51 <<name<<"\""<<std::endl;
55 // Try to load the metadata. If it fails, just return.
58 Factory2 f2 = n->getValue();
59 NodeMetadata *meta = (*f2)(gamedef);
62 catch(SerializationError &e)
64 errorstream<<"NodeMetadata: SerializationError "
65 <<"while creating name=\""<<name<<"\""<<std::endl;
70 NodeMetadata* NodeMetadata::deSerialize(std::istream &is, IGameDef *gamedef)
74 is.read((char*)buf, 2);
75 s16 id = readS16(buf);
78 std::string data = deSerializeString(is);
80 // Find factory function
81 core::map<u16, Factory>::Node *n;
85 // If factory is not found, just return.
86 infostream<<"WARNING: NodeMetadata: No factory for typeId="
91 // Try to load the metadata. If it fails, just return.
94 std::istringstream iss(data, std::ios_base::binary);
96 Factory f = n->getValue();
97 NodeMetadata *meta = (*f)(iss, gamedef);
100 catch(SerializationError &e)
102 infostream<<"WARNING: NodeMetadata: ignoring SerializationError"<<std::endl;
107 void NodeMetadata::serialize(std::ostream &os)
110 writeU16(buf, typeId());
111 os.write((char*)buf, 2);
113 std::ostringstream oss(std::ios_base::binary);
115 os<<serializeString(oss.str());
118 void NodeMetadata::registerType(u16 id, const std::string &name, Factory f,
122 core::map<u16, Factory>::Node *n;
123 n = m_types.find(id);
125 m_types.insert(id, f);
128 core::map<std::string, Factory2>::Node *n;
129 n = m_names.find(name);
131 m_names.insert(name, f2);
139 void NodeMetadataList::serialize(std::ostream &os)
144 writeU16(buf, version);
145 os.write((char*)buf, 2);
147 u16 count = m_data.size();
148 writeU16(buf, count);
149 os.write((char*)buf, 2);
151 for(core::map<v3s16, NodeMetadata*>::Iterator
152 i = m_data.getIterator();
153 i.atEnd()==false; i++)
155 v3s16 p = i.getNode()->getKey();
156 NodeMetadata *data = i.getNode()->getValue();
158 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
160 os.write((char*)buf, 2);
166 void NodeMetadataList::deSerialize(std::istream &is, IGameDef *gamedef)
172 is.read((char*)buf, 2);
173 u16 version = readU16(buf);
177 infostream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
179 throw SerializationError("NodeMetadataList::deSerialize");
182 is.read((char*)buf, 2);
183 u16 count = readU16(buf);
185 for(u16 i=0; i<count; i++)
187 is.read((char*)buf, 2);
188 u16 p16 = readU16(buf);
191 p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
192 p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
193 p.Y += p16 / MAP_BLOCKSIZE;
194 p16 -= p.Y * MAP_BLOCKSIZE;
197 NodeMetadata *data = NodeMetadata::deSerialize(is, gamedef);
204 infostream<<"WARNING: NodeMetadataList::deSerialize(): "
205 <<"already set data at position"
206 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
212 m_data.insert(p, data);
216 NodeMetadataList::~NodeMetadataList()
218 for(core::map<v3s16, NodeMetadata*>::Iterator
219 i = m_data.getIterator();
220 i.atEnd()==false; i++)
222 delete i.getNode()->getValue();
226 NodeMetadata* NodeMetadataList::get(v3s16 p)
228 core::map<v3s16, NodeMetadata*>::Node *n;
232 return n->getValue();
235 void NodeMetadataList::remove(v3s16 p)
237 NodeMetadata *olddata = get(p);
245 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
251 bool NodeMetadataList::step(float dtime)
253 bool something_changed = false;
254 for(core::map<v3s16, NodeMetadata*>::Iterator
255 i = m_data.getIterator();
256 i.atEnd()==false; i++)
258 v3s16 p = i.getNode()->getKey();
259 NodeMetadata *meta = i.getNode()->getValue();
260 bool changed = meta->step(dtime);
262 something_changed = true;
264 return something_changed;