]> git.lizzy.rs Git - dragonfireclient.git/blob - src/nodemetadata.cpp
RemotePlayer/LocalPlayer Player base class proper separation (code cleanup) (patch...
[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_stringvars(),
35         m_inventory(new Inventory(item_def_mgr))
36 {
37 }
38
39 NodeMetadata::~NodeMetadata()
40 {
41         delete m_inventory;
42 }
43
44 void NodeMetadata::serialize(std::ostream &os) const
45 {
46         int num_vars = m_stringvars.size();
47         writeU32(os, num_vars);
48         for (StringMap::const_iterator
49                         it = m_stringvars.begin();
50                         it != m_stringvars.end(); ++it) {
51                 os << serializeString(it->first);
52                 os << serializeLongString(it->second);
53         }
54
55         m_inventory->serialize(os);
56 }
57
58 void NodeMetadata::deSerialize(std::istream &is)
59 {
60         m_stringvars.clear();
61         int num_vars = readU32(is);
62         for(int i=0; i<num_vars; i++){
63                 std::string name = deSerializeString(is);
64                 std::string var = deSerializeLongString(is);
65                 m_stringvars[name] = var;
66         }
67
68         m_inventory->deSerialize(is);
69 }
70
71 void NodeMetadata::clear()
72 {
73         m_stringvars.clear();
74         m_inventory->clear();
75 }
76
77 bool NodeMetadata::empty() const
78 {
79         return m_stringvars.size() == 0 && m_inventory->getLists().size() == 0;
80 }
81
82 /*
83         NodeMetadataList
84 */
85
86 void NodeMetadataList::serialize(std::ostream &os) const
87 {
88         /*
89                 Version 0 is a placeholder for "nothing to see here; go away."
90         */
91
92         u16 count = countNonEmpty();
93         if (count == 0) {
94                 writeU8(os, 0); // version
95                 return;
96         }
97
98         writeU8(os, 1); // version
99         writeU16(os, count);
100
101         for(std::map<v3s16, NodeMetadata*>::const_iterator
102                         i = m_data.begin();
103                         i != m_data.end(); ++i)
104         {
105                 v3s16 p = i->first;
106                 NodeMetadata *data = i->second;
107                 if (data->empty())
108                         continue;
109
110                 u16 p16 = p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE + p.Y * MAP_BLOCKSIZE + p.X;
111                 writeU16(os, p16);
112
113                 data->serialize(os);
114         }
115 }
116
117 void NodeMetadataList::deSerialize(std::istream &is, IItemDefManager *item_def_mgr)
118 {
119         clear();
120
121         u8 version = readU8(is);
122
123         if (version == 0) {
124                 // Nothing
125                 return;
126         }
127
128         if (version != 1) {
129                 std::string err_str = std::string(FUNCTION_NAME)
130                         + ": version " + itos(version) + " not supported";
131                 infostream << err_str << std::endl;
132                 throw SerializationError(err_str);
133         }
134
135         u16 count = readU16(is);
136
137         for (u16 i=0; i < count; i++) {
138                 u16 p16 = readU16(is);
139
140                 v3s16 p;
141                 p.Z = p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
142                 p16 &= MAP_BLOCKSIZE * MAP_BLOCKSIZE - 1;
143                 p.Y = p16 / MAP_BLOCKSIZE;
144                 p16 &= MAP_BLOCKSIZE - 1;
145                 p.X = p16;
146
147                 if (m_data.find(p) != m_data.end()) {
148                         warningstream<<"NodeMetadataList::deSerialize(): "
149                                         <<"already set data at position"
150                                         <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
151                                         <<std::endl;
152                         continue;
153                 }
154
155                 NodeMetadata *data = new NodeMetadata(item_def_mgr);
156                 data->deSerialize(is);
157                 m_data[p] = data;
158         }
159 }
160
161 NodeMetadataList::~NodeMetadataList()
162 {
163         clear();
164 }
165
166 std::vector<v3s16> NodeMetadataList::getAllKeys()
167 {
168         std::vector<v3s16> keys;
169
170         std::map<v3s16, NodeMetadata *>::const_iterator it;
171         for (it = m_data.begin(); it != m_data.end(); ++it)
172                 keys.push_back(it->first);
173
174         return keys;
175 }
176
177 NodeMetadata *NodeMetadataList::get(v3s16 p)
178 {
179         std::map<v3s16, NodeMetadata *>::const_iterator n = m_data.find(p);
180         if (n == m_data.end())
181                 return NULL;
182         return n->second;
183 }
184
185 void NodeMetadataList::remove(v3s16 p)
186 {
187         NodeMetadata *olddata = get(p);
188         if (olddata) {
189                 delete olddata;
190                 m_data.erase(p);
191         }
192 }
193
194 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
195 {
196         remove(p);
197         m_data.insert(std::make_pair(p, d));
198 }
199
200 void NodeMetadataList::clear()
201 {
202         std::map<v3s16, NodeMetadata*>::iterator it;
203         for (it = m_data.begin(); it != m_data.end(); ++it) {
204                 delete it->second;
205         }
206         m_data.clear();
207 }
208
209 int NodeMetadataList::countNonEmpty() const
210 {
211         int n = 0;
212         std::map<v3s16, NodeMetadata*>::const_iterator it;
213         for (it = m_data.begin(); it != m_data.end(); ++it) {
214                 if (!it->second->empty())
215                         n++;
216         }
217         return n;
218 }
219
220 std::string NodeMetadata::getString(const std::string &name,
221         unsigned short recursion) const
222 {
223         StringMap::const_iterator it = m_stringvars.find(name);
224         if (it == m_stringvars.end())
225                 return "";
226
227         return resolveString(it->second, recursion);
228 }
229
230 void NodeMetadata::setString(const std::string &name, const std::string &var)
231 {
232         if (var.empty()) {
233                 m_stringvars.erase(name);
234         } else {
235                 m_stringvars[name] = var;
236         }
237 }
238
239 std::string NodeMetadata::resolveString(const std::string &str,
240         unsigned short recursion) const
241 {
242         if (recursion > 1) {
243                 return str;
244         }
245         if (str.substr(0, 2) == "${" && str[str.length() - 1] == '}') {
246                 return getString(str.substr(2, str.length() - 3), recursion + 1);
247         }
248         return str;
249 }
250