]> git.lizzy.rs Git - dragonfireclient.git/blob - src/nodemetadata.cpp
Drop luaentity_common.h which is not included anywhere
[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 /*
78         NodeMetadataList
79 */
80
81 void NodeMetadataList::serialize(std::ostream &os) const
82 {
83         /*
84                 Version 0 is a placeholder for "nothing to see here; go away."
85         */
86
87         if(m_data.empty()){
88                 writeU8(os, 0); // version
89                 return;
90         }
91
92         writeU8(os, 1); // version
93
94         u16 count = m_data.size();
95         writeU16(os, count);
96
97         for(std::map<v3s16, NodeMetadata*>::const_iterator
98                         i = m_data.begin();
99                         i != m_data.end(); ++i)
100         {
101                 v3s16 p = i->first;
102                 NodeMetadata *data = i->second;
103
104                 u16 p16 = p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE + p.Y * MAP_BLOCKSIZE + p.X;
105                 writeU16(os, p16);
106
107                 data->serialize(os);
108         }
109 }
110
111 void NodeMetadataList::deSerialize(std::istream &is, IItemDefManager *item_def_mgr)
112 {
113         clear();
114
115         u8 version = readU8(is);
116
117         if (version == 0) {
118                 // Nothing
119                 return;
120         }
121
122         if (version != 1) {
123                 std::string err_str = std::string(FUNCTION_NAME)
124                         + ": version " + itos(version) + " not supported";
125                 infostream << err_str << std::endl;
126                 throw SerializationError(err_str);
127         }
128
129         u16 count = readU16(is);
130
131         for (u16 i=0; i < count; i++) {
132                 u16 p16 = readU16(is);
133
134                 v3s16 p;
135                 p.Z = p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
136                 p16 &= MAP_BLOCKSIZE * MAP_BLOCKSIZE - 1;
137                 p.Y = p16 / MAP_BLOCKSIZE;
138                 p16 &= MAP_BLOCKSIZE - 1;
139                 p.X = p16;
140
141                 if (m_data.find(p) != m_data.end()) {
142                         warningstream<<"NodeMetadataList::deSerialize(): "
143                                         <<"already set data at position"
144                                         <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
145                                         <<std::endl;
146                         continue;
147                 }
148
149                 NodeMetadata *data = new NodeMetadata(item_def_mgr);
150                 data->deSerialize(is);
151                 m_data[p] = data;
152         }
153 }
154
155 NodeMetadataList::~NodeMetadataList()
156 {
157         clear();
158 }
159
160 std::vector<v3s16> NodeMetadataList::getAllKeys()
161 {
162         std::vector<v3s16> keys;
163
164         std::map<v3s16, NodeMetadata *>::const_iterator it;
165         for (it = m_data.begin(); it != m_data.end(); ++it)
166                 keys.push_back(it->first);
167
168         return keys;
169 }
170
171 NodeMetadata *NodeMetadataList::get(v3s16 p)
172 {
173         std::map<v3s16, NodeMetadata *>::const_iterator n = m_data.find(p);
174         if (n == m_data.end())
175                 return NULL;
176         return n->second;
177 }
178
179 void NodeMetadataList::remove(v3s16 p)
180 {
181         NodeMetadata *olddata = get(p);
182         if (olddata) {
183                 delete olddata;
184                 m_data.erase(p);
185         }
186 }
187
188 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
189 {
190         remove(p);
191         m_data.insert(std::make_pair(p, d));
192 }
193
194 void NodeMetadataList::clear()
195 {
196         std::map<v3s16, NodeMetadata*>::iterator it;
197         for (it = m_data.begin(); it != m_data.end(); ++it) {
198                 delete it->second;
199         }
200         m_data.clear();
201 }
202
203 std::string NodeMetadata::getString(const std::string &name,
204         unsigned short recursion) const
205 {
206         StringMap::const_iterator it = m_stringvars.find(name);
207         if (it == m_stringvars.end())
208                 return "";
209
210         return resolveString(it->second, recursion);
211 }
212
213 void NodeMetadata::setString(const std::string &name, const std::string &var)
214 {
215         if (var.empty()) {
216                 m_stringvars.erase(name);
217         } else {
218                 m_stringvars[name] = var;
219         }
220 }
221
222 std::string NodeMetadata::resolveString(const std::string &str,
223         unsigned short recursion) const
224 {
225         if (recursion > 1) {
226                 return str;
227         }
228         if (str.substr(0, 2) == "${" && str[str.length() - 1] == '}') {
229                 return getString(str.substr(2, str.length() - 3), recursion + 1);
230         }
231         return str;
232 }
233