]> git.lizzy.rs Git - dragonfireclient.git/blob - src/nodemetadata.cpp
Pass clang-format on various cpp/header files (#5559)
[dragonfireclient.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 "constants.h" // MAP_BLOCKSIZE
27 #include <sstream>
28
29 /*
30         NodeMetadata
31 */
32
33 NodeMetadata::NodeMetadata(IItemDefManager *item_def_mgr):
34         m_inventory(new Inventory(item_def_mgr))
35 {}
36
37 NodeMetadata::~NodeMetadata()
38 {
39         delete m_inventory;
40 }
41
42 void NodeMetadata::serialize(std::ostream &os) const
43 {
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);
51         }
52
53         m_inventory->serialize(os);
54 }
55
56 void NodeMetadata::deSerialize(std::istream &is)
57 {
58         m_stringvars.clear();
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;
64         }
65
66         m_inventory->deSerialize(is);
67 }
68
69 void NodeMetadata::clear()
70 {
71         Metadata::clear();
72         m_inventory->clear();
73 }
74
75 bool NodeMetadata::empty() const
76 {
77         return Metadata::empty() && m_inventory->getLists().size() == 0;
78 }
79
80 /*
81         NodeMetadataList
82 */
83
84 void NodeMetadataList::serialize(std::ostream &os) const
85 {
86         /*
87                 Version 0 is a placeholder for "nothing to see here; go away."
88         */
89
90         u16 count = countNonEmpty();
91         if (count == 0) {
92                 writeU8(os, 0); // version
93                 return;
94         }
95
96         writeU8(os, 1); // version
97         writeU16(os, count);
98
99         for(std::map<v3s16, NodeMetadata*>::const_iterator
100                         i = m_data.begin();
101                         i != m_data.end(); ++i)
102         {
103                 v3s16 p = i->first;
104                 NodeMetadata *data = i->second;
105                 if (data->empty())
106                         continue;
107
108                 u16 p16 = p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE + p.Y * MAP_BLOCKSIZE + p.X;
109                 writeU16(os, p16);
110
111                 data->serialize(os);
112         }
113 }
114
115 void NodeMetadataList::deSerialize(std::istream &is, IItemDefManager *item_def_mgr)
116 {
117         clear();
118
119         u8 version = readU8(is);
120
121         if (version == 0) {
122                 // Nothing
123                 return;
124         }
125
126         if (version != 1) {
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);
131         }
132
133         u16 count = readU16(is);
134
135         for (u16 i=0; i < count; i++) {
136                 u16 p16 = readU16(is);
137
138                 v3s16 p;
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;
143                 p.X = p16;
144
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."
149                                         <<std::endl;
150                         continue;
151                 }
152
153                 NodeMetadata *data = new NodeMetadata(item_def_mgr);
154                 data->deSerialize(is);
155                 m_data[p] = data;
156         }
157 }
158
159 NodeMetadataList::~NodeMetadataList()
160 {
161         clear();
162 }
163
164 std::vector<v3s16> NodeMetadataList::getAllKeys()
165 {
166         std::vector<v3s16> keys;
167
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);
171
172         return keys;
173 }
174
175 NodeMetadata *NodeMetadataList::get(v3s16 p)
176 {
177         std::map<v3s16, NodeMetadata *>::const_iterator n = m_data.find(p);
178         if (n == m_data.end())
179                 return NULL;
180         return n->second;
181 }
182
183 void NodeMetadataList::remove(v3s16 p)
184 {
185         NodeMetadata *olddata = get(p);
186         if (olddata) {
187                 delete olddata;
188                 m_data.erase(p);
189         }
190 }
191
192 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
193 {
194         remove(p);
195         m_data.insert(std::make_pair(p, d));
196 }
197
198 void NodeMetadataList::clear()
199 {
200         std::map<v3s16, NodeMetadata*>::iterator it;
201         for (it = m_data.begin(); it != m_data.end(); ++it) {
202                 delete it->second;
203         }
204         m_data.clear();
205 }
206
207 int NodeMetadataList::countNonEmpty() const
208 {
209         int n = 0;
210         std::map<v3s16, NodeMetadata*>::const_iterator it;
211         for (it = m_data.begin(); it != m_data.end(); ++it) {
212                 if (!it->second->empty())
213                         n++;
214         }
215         return n;
216 }