]> git.lizzy.rs Git - minetest.git/blob - src/nodemetadata.cpp
Mode node definition loading from Lua (still not finished), fix metadata creation...
[minetest.git] / src / nodemetadata.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 "utility.h"
22 #include "mapnode.h"
23 #include "exceptions.h"
24 #include "inventory.h"
25 #include <sstream>
26 #include "content_mapnode.h"
27 #include "log.h"
28
29 /*
30         NodeMetadata
31 */
32
33 core::map<u16, NodeMetadata::Factory> NodeMetadata::m_types;
34 core::map<std::string, NodeMetadata::Factory2> NodeMetadata::m_names;
35
36 NodeMetadata::NodeMetadata(IGameDef *gamedef):
37         m_gamedef(gamedef)
38 {
39 }
40
41 NodeMetadata::~NodeMetadata()
42 {
43 }
44
45 NodeMetadata* NodeMetadata::create(const std::string &name, IGameDef *gamedef)
46 {
47         // Find factory function
48         core::map<std::string, Factory2>::Node *n;
49         n = m_names.find(name);
50         if(n == NULL)
51         {
52                 // If factory is not found, just return.
53                 errorstream<<"WARNING: NodeMetadata: No factory for name=\""
54                                 <<name<<"\""<<std::endl;
55                 return NULL;
56         }
57         
58         // Try to load the metadata. If it fails, just return.
59         try
60         {
61                 Factory2 f2 = n->getValue();
62                 NodeMetadata *meta = (*f2)(gamedef);
63                 return meta;
64         }
65         catch(SerializationError &e)
66         {
67                 errorstream<<"NodeMetadata: SerializationError "
68                                 <<"while creating name=\""<<name<<"\""<<std::endl;
69                 return NULL;
70         }
71 }
72
73 NodeMetadata* NodeMetadata::deSerialize(std::istream &is, IGameDef *gamedef)
74 {
75         // Read id
76         u8 buf[2];
77         is.read((char*)buf, 2);
78         s16 id = readS16(buf);
79         
80         // Read data
81         std::string data = deSerializeString(is);
82         
83         // Find factory function
84         core::map<u16, Factory>::Node *n;
85         n = m_types.find(id);
86         if(n == NULL)
87         {
88                 // If factory is not found, just return.
89                 infostream<<"WARNING: NodeMetadata: No factory for typeId="
90                                 <<id<<std::endl;
91                 return NULL;
92         }
93         
94         // Try to load the metadata. If it fails, just return.
95         try
96         {
97                 std::istringstream iss(data, std::ios_base::binary);
98                 
99                 Factory f = n->getValue();
100                 NodeMetadata *meta = (*f)(iss, gamedef);
101                 return meta;
102         }
103         catch(SerializationError &e)
104         {
105                 infostream<<"WARNING: NodeMetadata: ignoring SerializationError"<<std::endl;
106                 return NULL;
107         }
108 }
109
110 void NodeMetadata::serialize(std::ostream &os)
111 {
112         u8 buf[2];
113         writeU16(buf, typeId());
114         os.write((char*)buf, 2);
115         
116         std::ostringstream oss(std::ios_base::binary);
117         serializeBody(oss);
118         os<<serializeString(oss.str());
119 }
120
121 void NodeMetadata::registerType(u16 id, const std::string &name, Factory f,
122                 Factory2 f2)
123 {
124         { // typeId
125                 core::map<u16, Factory>::Node *n;
126                 n = m_types.find(id);
127                 if(!n)
128                         m_types.insert(id, f);
129         }
130         { // typeName
131                 core::map<std::string, Factory2>::Node *n;
132                 n = m_names.find(name);
133                 if(!n)
134                         m_names.insert(name, f2);
135         }
136 }
137
138 /*
139         NodeMetadataList
140 */
141
142 void NodeMetadataList::serialize(std::ostream &os)
143 {
144         u8 buf[6];
145         
146         u16 version = 1;
147         writeU16(buf, version);
148         os.write((char*)buf, 2);
149
150         u16 count = m_data.size();
151         writeU16(buf, count);
152         os.write((char*)buf, 2);
153
154         for(core::map<v3s16, NodeMetadata*>::Iterator
155                         i = m_data.getIterator();
156                         i.atEnd()==false; i++)
157         {
158                 v3s16 p = i.getNode()->getKey();
159                 NodeMetadata *data = i.getNode()->getValue();
160                 
161                 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
162                 writeU16(buf, p16);
163                 os.write((char*)buf, 2);
164
165                 data->serialize(os);
166         }
167         
168 }
169 void NodeMetadataList::deSerialize(std::istream &is, IGameDef *gamedef)
170 {
171         m_data.clear();
172
173         u8 buf[6];
174         
175         is.read((char*)buf, 2);
176         u16 version = readU16(buf);
177
178         if(version > 1)
179         {
180                 infostream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
181                                 <<std::endl;
182                 throw SerializationError("NodeMetadataList::deSerialize");
183         }
184         
185         is.read((char*)buf, 2);
186         u16 count = readU16(buf);
187         
188         for(u16 i=0; i<count; i++)
189         {
190                 is.read((char*)buf, 2);
191                 u16 p16 = readU16(buf);
192
193                 v3s16 p(0,0,0);
194                 p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
195                 p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
196                 p.Y += p16 / MAP_BLOCKSIZE;
197                 p16 -= p.Y * MAP_BLOCKSIZE;
198                 p.X += p16;
199                 
200                 NodeMetadata *data = NodeMetadata::deSerialize(is, gamedef);
201
202                 if(data == NULL)
203                         continue;
204                 
205                 if(m_data.find(p))
206                 {
207                         infostream<<"WARNING: NodeMetadataList::deSerialize(): "
208                                         <<"already set data at position"
209                                         <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
210                                         <<std::endl;
211                         delete data;
212                         continue;
213                 }
214
215                 m_data.insert(p, data);
216         }
217 }
218         
219 NodeMetadataList::~NodeMetadataList()
220 {
221         for(core::map<v3s16, NodeMetadata*>::Iterator
222                         i = m_data.getIterator();
223                         i.atEnd()==false; i++)
224         {
225                 delete i.getNode()->getValue();
226         }
227 }
228
229 NodeMetadata* NodeMetadataList::get(v3s16 p)
230 {
231         core::map<v3s16, NodeMetadata*>::Node *n;
232         n = m_data.find(p);
233         if(n == NULL)
234                 return NULL;
235         return n->getValue();
236 }
237
238 void NodeMetadataList::remove(v3s16 p)
239 {
240         NodeMetadata *olddata = get(p);
241         if(olddata)
242         {
243                 delete olddata;
244                 m_data.remove(p);
245         }
246 }
247
248 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
249 {
250         remove(p);
251         m_data.insert(p, d);
252 }
253
254 bool NodeMetadataList::step(float dtime)
255 {
256         bool something_changed = false;
257         for(core::map<v3s16, NodeMetadata*>::Iterator
258                         i = m_data.getIterator();
259                         i.atEnd()==false; i++)
260         {
261                 v3s16 p = i.getNode()->getKey();
262                 NodeMetadata *meta = i.getNode()->getValue();
263                 bool changed = meta->step(dtime);
264                 if(changed)
265                         something_changed = true;
266         }
267         return something_changed;
268 }
269