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"
30 core::map<u16, NodeMetadata::Factory> NodeMetadata::m_types;
32 NodeMetadata::NodeMetadata()
36 NodeMetadata::~NodeMetadata()
40 NodeMetadata* NodeMetadata::deSerialize(std::istream &is)
43 is.read((char*)buf, 2);
44 s16 id = readS16(buf);
46 core::map<u16, Factory>::Node *n;
50 dstream<<"NodeMetadata(): No factory for typeId="<<id<<std::endl;
51 throw SerializationError("Unknown metadata id");
54 Factory f = n->getValue();
55 NodeMetadata *meta = (*f)(is);
59 void NodeMetadata::serialize(std::ostream &os)
62 writeU16(buf, typeId());
63 os.write((char*)buf, 2);
68 void NodeMetadata::registerType(u16 id, Factory f)
70 core::map<u16, Factory>::Node *n;
74 m_types.insert(id, f);
81 SignNodeMetadata::SignNodeMetadata(std::string text):
84 NodeMetadata::registerType(typeId(), create);
86 u16 SignNodeMetadata::typeId() const
88 return CONTENT_SIGN_WALL;
90 NodeMetadata* SignNodeMetadata::create(std::istream &is)
92 std::string text = deSerializeString(is);
93 return new SignNodeMetadata(text);
95 NodeMetadata* SignNodeMetadata::clone()
97 return new SignNodeMetadata(m_text);
99 void SignNodeMetadata::serializeBody(std::ostream &os)
101 os<<serializeString(m_text);
103 std::string SignNodeMetadata::infoText()
105 return std::string("\"")+m_text+"\"";
112 ChestNodeMetadata::ChestNodeMetadata()
114 NodeMetadata::registerType(typeId(), create);
116 m_inventory = new Inventory();
117 m_inventory->addList("0", 8*4);
119 ChestNodeMetadata::~ChestNodeMetadata()
123 u16 ChestNodeMetadata::typeId() const
125 return CONTENT_CHEST;
127 NodeMetadata* ChestNodeMetadata::create(std::istream &is)
129 ChestNodeMetadata *d = new ChestNodeMetadata();
130 d->m_inventory->deSerialize(is);
133 NodeMetadata* ChestNodeMetadata::clone()
135 ChestNodeMetadata *d = new ChestNodeMetadata();
136 *d->m_inventory = *m_inventory;
139 void ChestNodeMetadata::serializeBody(std::ostream &os)
141 m_inventory->serialize(os);
143 std::string ChestNodeMetadata::infoText()
147 /*Inventory* ChestNodeMetadata::getInventory()
156 void NodeMetadataList::serialize(std::ostream &os)
160 u16 count = m_data.size();
161 writeU16(buf, count);
162 os.write((char*)buf, 2);
164 for(core::map<v3s16, NodeMetadata*>::Iterator
165 i = m_data.getIterator();
166 i.atEnd()==false; i++)
168 v3s16 p = i.getNode()->getKey();
169 NodeMetadata *data = i.getNode()->getValue();
171 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
173 os.write((char*)buf, 2);
179 void NodeMetadataList::deSerialize(std::istream &is)
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;
202 dstream<<"ERROR: NodeMetadataList::deSerialize(): "
203 <<"already set data at position"
204 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
206 throw SerializationError("NodeMetadataList::deSerialize()");
209 NodeMetadata *data = NodeMetadata::deSerialize(is);
211 m_data.insert(p, data);
215 NodeMetadataList::~NodeMetadataList()
217 for(core::map<v3s16, NodeMetadata*>::Iterator
218 i = m_data.getIterator();
219 i.atEnd()==false; i++)
221 delete i.getNode()->getValue();
225 NodeMetadata* NodeMetadataList::get(v3s16 p)
227 core::map<v3s16, NodeMetadata*>::Node *n;
231 return n->getValue();
234 void NodeMetadataList::remove(v3s16 p)
236 NodeMetadata *olddata = get(p);
244 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)