Translated using Weblate (Italian)
[oweals/minetest.git] / src / nodemetadata.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "nodemetadata.h"
21 #include "exceptions.h"
22 #include "gamedef.h"
23 #include "inventory.h"
24 #include "log.h"
25 #include "util/serialize.h"
26 #include "util/basic_macros.h"
27 #include "constants.h" // MAP_BLOCKSIZE
28 #include <sstream>
29
30 /*
31         NodeMetadata
32 */
33
34 NodeMetadata::NodeMetadata(IItemDefManager *item_def_mgr):
35         m_inventory(new Inventory(item_def_mgr))
36 {}
37
38 NodeMetadata::~NodeMetadata()
39 {
40         delete m_inventory;
41 }
42
43 void NodeMetadata::serialize(std::ostream &os, u8 version, bool disk) const
44 {
45         int num_vars = disk ? m_stringvars.size() : countNonPrivate();
46         writeU32(os, num_vars);
47         for (const auto &sv : m_stringvars) {
48                 bool priv = isPrivate(sv.first);
49                 if (!disk && priv)
50                         continue;
51
52                 os << serializeString(sv.first);
53                 os << serializeLongString(sv.second);
54                 if (version >= 2)
55                         writeU8(os, (priv) ? 1 : 0);
56         }
57
58         m_inventory->serialize(os);
59 }
60
61 void NodeMetadata::deSerialize(std::istream &is, u8 version)
62 {
63         clear();
64         int num_vars = readU32(is);
65         for(int i=0; i<num_vars; i++){
66                 std::string name = deSerializeString(is);
67                 std::string var = deSerializeLongString(is);
68                 m_stringvars[name] = var;
69                 if (version >= 2) {
70                         if (readU8(is) == 1)
71                                 markPrivate(name, true);
72                 }
73         }
74
75         m_inventory->deSerialize(is);
76 }
77
78 void NodeMetadata::clear()
79 {
80         Metadata::clear();
81         m_privatevars.clear();
82         m_inventory->clear();
83 }
84
85 bool NodeMetadata::empty() const
86 {
87         return Metadata::empty() && m_inventory->getLists().empty();
88 }
89
90
91 void NodeMetadata::markPrivate(const std::string &name, bool set)
92 {
93         if (set)
94                 m_privatevars.insert(name);
95         else
96                 m_privatevars.erase(name);
97 }
98
99 int NodeMetadata::countNonPrivate() const
100 {
101         // m_privatevars can contain names not actually present
102         // DON'T: return m_stringvars.size() - m_privatevars.size();
103         int n = 0;
104         for (const auto &sv : m_stringvars) {
105                 if (!isPrivate(sv.first))
106                         n++;
107         }
108         return n;
109 }
110
111 /*
112         NodeMetadataList
113 */
114
115 void NodeMetadataList::serialize(std::ostream &os, u8 blockver, bool disk,
116         bool absolute_pos) const
117 {
118         /*
119                 Version 0 is a placeholder for "nothing to see here; go away."
120         */
121
122         u16 count = countNonEmpty();
123         if (count == 0) {
124                 writeU8(os, 0); // version
125                 return;
126         }
127
128         u8 version = (blockver > 27) ? 2 : 1;
129         writeU8(os, version);
130         writeU16(os, count);
131
132         for (NodeMetadataMap::const_iterator
133                         i = m_data.begin();
134                         i != m_data.end(); ++i) {
135                 v3s16 p = i->first;
136                 NodeMetadata *data = i->second;
137                 if (data->empty())
138                         continue;
139
140                 if (absolute_pos) {
141                         writeS16(os, p.X);
142                         writeS16(os, p.Y);
143                         writeS16(os, p.Z);
144                 } else {
145                         // Serialize positions within a mapblock
146                         u16 p16 = (p.Z * MAP_BLOCKSIZE + p.Y) * MAP_BLOCKSIZE + p.X;
147                         writeU16(os, p16);
148                 }
149                 data->serialize(os, version, disk);
150         }
151 }
152
153 void NodeMetadataList::deSerialize(std::istream &is,
154         IItemDefManager *item_def_mgr, bool absolute_pos)
155 {
156         clear();
157
158         u8 version = readU8(is);
159
160         if (version == 0) {
161                 // Nothing
162                 return;
163         }
164
165         if (version > 2) {
166                 std::string err_str = std::string(FUNCTION_NAME)
167                         + ": version " + itos(version) + " not supported";
168                 infostream << err_str << std::endl;
169                 throw SerializationError(err_str);
170         }
171
172         u16 count = readU16(is);
173
174         for (u16 i = 0; i < count; i++) {
175                 v3s16 p;
176                 if (absolute_pos) {
177                         p.X = readS16(is);
178                         p.Y = readS16(is);
179                         p.Z = readS16(is);
180                 } else {
181                         u16 p16 = readU16(is);
182                         p.X = p16 & (MAP_BLOCKSIZE - 1);
183                         p16 /= MAP_BLOCKSIZE;
184                         p.Y = p16 & (MAP_BLOCKSIZE - 1);
185                         p16 /= MAP_BLOCKSIZE;
186                         p.Z = p16;
187                 }
188                 if (m_data.find(p) != m_data.end()) {
189                         warningstream << "NodeMetadataList::deSerialize(): "
190                                         << "already set data at position " << PP(p)
191                                         << ": Ignoring." << std::endl;
192                         continue;
193                 }
194
195                 NodeMetadata *data = new NodeMetadata(item_def_mgr);
196                 data->deSerialize(is, version);
197                 m_data[p] = data;
198         }
199 }
200
201 NodeMetadataList::~NodeMetadataList()
202 {
203         clear();
204 }
205
206 std::vector<v3s16> NodeMetadataList::getAllKeys()
207 {
208         std::vector<v3s16> keys;
209
210         NodeMetadataMap::const_iterator it;
211         for (it = m_data.begin(); it != m_data.end(); ++it)
212                 keys.push_back(it->first);
213
214         return keys;
215 }
216
217 NodeMetadata *NodeMetadataList::get(v3s16 p)
218 {
219         NodeMetadataMap::const_iterator n = m_data.find(p);
220         if (n == m_data.end())
221                 return NULL;
222         return n->second;
223 }
224
225 void NodeMetadataList::remove(v3s16 p)
226 {
227         NodeMetadata *olddata = get(p);
228         if (olddata) {
229                 if (m_is_metadata_owner)
230                         delete olddata;
231                 m_data.erase(p);
232         }
233 }
234
235 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
236 {
237         remove(p);
238         m_data.insert(std::make_pair(p, d));
239 }
240
241 void NodeMetadataList::clear()
242 {
243         if (m_is_metadata_owner) {
244                 NodeMetadataMap::const_iterator it;
245                 for (it = m_data.begin(); it != m_data.end(); ++it)
246                         delete it->second;
247         }
248         m_data.clear();
249 }
250
251 int NodeMetadataList::countNonEmpty() const
252 {
253         int n = 0;
254         NodeMetadataMap::const_iterator it;
255         for (it = m_data.begin(); it != m_data.end(); ++it) {
256                 if (!it->second->empty())
257                         n++;
258         }
259         return n;
260 }