3 Copyright (C) 2010-2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser 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"
21 #include "exceptions.h"
23 #include "inventory.h"
25 #include "util/serialize.h"
26 #include "constants.h" // MAP_BLOCKSIZE
33 NodeMetadata::NodeMetadata(IItemDefManager *item_def_mgr):
34 m_inventory(new Inventory(item_def_mgr))
37 NodeMetadata::~NodeMetadata()
42 void NodeMetadata::serialize(std::ostream &os) const
44 int num_vars = m_stringvars.size();
45 writeU32(os, num_vars);
46 for (StringMap::const_iterator
47 it = m_stringvars.begin();
48 it != m_stringvars.end(); ++it) {
49 os << serializeString(it->first);
50 os << serializeLongString(it->second);
53 m_inventory->serialize(os);
56 void NodeMetadata::deSerialize(std::istream &is)
59 int num_vars = readU32(is);
60 for(int i=0; i<num_vars; i++){
61 std::string name = deSerializeString(is);
62 std::string var = deSerializeLongString(is);
63 m_stringvars[name] = var;
66 m_inventory->deSerialize(is);
69 void NodeMetadata::clear()
75 bool NodeMetadata::empty() const
77 return Metadata::empty() && m_inventory->getLists().size() == 0;
84 void NodeMetadataList::serialize(std::ostream &os) const
87 Version 0 is a placeholder for "nothing to see here; go away."
90 u16 count = countNonEmpty();
92 writeU8(os, 0); // version
96 writeU8(os, 1); // version
99 for(std::map<v3s16, NodeMetadata*>::const_iterator
101 i != m_data.end(); ++i)
104 NodeMetadata *data = i->second;
108 u16 p16 = p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE + p.Y * MAP_BLOCKSIZE + p.X;
115 void NodeMetadataList::deSerialize(std::istream &is, IItemDefManager *item_def_mgr)
119 u8 version = readU8(is);
127 std::string err_str = std::string(FUNCTION_NAME)
128 + ": version " + itos(version) + " not supported";
129 infostream << err_str << std::endl;
130 throw SerializationError(err_str);
133 u16 count = readU16(is);
135 for (u16 i=0; i < count; i++) {
136 u16 p16 = readU16(is);
139 p.Z = p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
140 p16 &= MAP_BLOCKSIZE * MAP_BLOCKSIZE - 1;
141 p.Y = p16 / MAP_BLOCKSIZE;
142 p16 &= MAP_BLOCKSIZE - 1;
145 if (m_data.find(p) != m_data.end()) {
146 warningstream<<"NodeMetadataList::deSerialize(): "
147 <<"already set data at position"
148 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
153 NodeMetadata *data = new NodeMetadata(item_def_mgr);
154 data->deSerialize(is);
159 NodeMetadataList::~NodeMetadataList()
164 std::vector<v3s16> NodeMetadataList::getAllKeys()
166 std::vector<v3s16> keys;
168 std::map<v3s16, NodeMetadata *>::const_iterator it;
169 for (it = m_data.begin(); it != m_data.end(); ++it)
170 keys.push_back(it->first);
175 NodeMetadata *NodeMetadataList::get(v3s16 p)
177 std::map<v3s16, NodeMetadata *>::const_iterator n = m_data.find(p);
178 if (n == m_data.end())
183 void NodeMetadataList::remove(v3s16 p)
185 NodeMetadata *olddata = get(p);
192 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
195 m_data.insert(std::make_pair(p, d));
198 void NodeMetadataList::clear()
200 std::map<v3s16, NodeMetadata*>::iterator it;
201 for (it = m_data.begin(); it != m_data.end(); ++it) {
207 int NodeMetadataList::countNonEmpty() const
210 std::map<v3s16, NodeMetadata*>::const_iterator it;
211 for (it = m_data.begin(); it != m_data.end(); ++it) {
212 if (!it->second->empty())