Falling sand and gravel
[oweals/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
35 NodeMetadata::NodeMetadata(IGameDef *gamedef):
36         m_gamedef(gamedef)
37 {
38 }
39
40 NodeMetadata::~NodeMetadata()
41 {
42 }
43
44 NodeMetadata* NodeMetadata::deSerialize(std::istream &is, IGameDef *gamedef)
45 {
46         // Read id
47         u8 buf[2];
48         is.read((char*)buf, 2);
49         s16 id = readS16(buf);
50         
51         // Read data
52         std::string data = deSerializeString(is);
53         
54         // Find factory function
55         core::map<u16, Factory>::Node *n;
56         n = m_types.find(id);
57         if(n == NULL)
58         {
59                 // If factory is not found, just return.
60                 infostream<<"WARNING: NodeMetadata: No factory for typeId="
61                                 <<id<<std::endl;
62                 return NULL;
63         }
64         
65         // Try to load the metadata. If it fails, just return.
66         try
67         {
68                 std::istringstream iss(data, std::ios_base::binary);
69                 
70                 Factory f = n->getValue();
71                 NodeMetadata *meta = (*f)(iss, gamedef);
72                 return meta;
73         }
74         catch(SerializationError &e)
75         {
76                 infostream<<"WARNING: NodeMetadata: ignoring SerializationError"<<std::endl;
77                 return NULL;
78         }
79 }
80
81 void NodeMetadata::serialize(std::ostream &os)
82 {
83         u8 buf[2];
84         writeU16(buf, typeId());
85         os.write((char*)buf, 2);
86         
87         std::ostringstream oss(std::ios_base::binary);
88         serializeBody(oss);
89         os<<serializeString(oss.str());
90 }
91
92 void NodeMetadata::registerType(u16 id, Factory f)
93 {
94         core::map<u16, Factory>::Node *n;
95         n = m_types.find(id);
96         if(n)
97                 return;
98         m_types.insert(id, f);
99 }
100
101 /*
102         NodeMetadataList
103 */
104
105 void NodeMetadataList::serialize(std::ostream &os)
106 {
107         u8 buf[6];
108         
109         u16 version = 1;
110         writeU16(buf, version);
111         os.write((char*)buf, 2);
112
113         u16 count = m_data.size();
114         writeU16(buf, count);
115         os.write((char*)buf, 2);
116
117         for(core::map<v3s16, NodeMetadata*>::Iterator
118                         i = m_data.getIterator();
119                         i.atEnd()==false; i++)
120         {
121                 v3s16 p = i.getNode()->getKey();
122                 NodeMetadata *data = i.getNode()->getValue();
123                 
124                 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
125                 writeU16(buf, p16);
126                 os.write((char*)buf, 2);
127
128                 data->serialize(os);
129         }
130         
131 }
132 void NodeMetadataList::deSerialize(std::istream &is, IGameDef *gamedef)
133 {
134         m_data.clear();
135
136         u8 buf[6];
137         
138         is.read((char*)buf, 2);
139         u16 version = readU16(buf);
140
141         if(version > 1)
142         {
143                 infostream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
144                                 <<std::endl;
145                 throw SerializationError("NodeMetadataList::deSerialize");
146         }
147         
148         is.read((char*)buf, 2);
149         u16 count = readU16(buf);
150         
151         for(u16 i=0; i<count; i++)
152         {
153                 is.read((char*)buf, 2);
154                 u16 p16 = readU16(buf);
155
156                 v3s16 p(0,0,0);
157                 p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
158                 p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
159                 p.Y += p16 / MAP_BLOCKSIZE;
160                 p16 -= p.Y * MAP_BLOCKSIZE;
161                 p.X += p16;
162                 
163                 NodeMetadata *data = NodeMetadata::deSerialize(is, gamedef);
164
165                 if(data == NULL)
166                         continue;
167                 
168                 if(m_data.find(p))
169                 {
170                         infostream<<"WARNING: NodeMetadataList::deSerialize(): "
171                                         <<"already set data at position"
172                                         <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
173                                         <<std::endl;
174                         delete data;
175                         continue;
176                 }
177
178                 m_data.insert(p, data);
179         }
180 }
181         
182 NodeMetadataList::~NodeMetadataList()
183 {
184         for(core::map<v3s16, NodeMetadata*>::Iterator
185                         i = m_data.getIterator();
186                         i.atEnd()==false; i++)
187         {
188                 delete i.getNode()->getValue();
189         }
190 }
191
192 NodeMetadata* NodeMetadataList::get(v3s16 p)
193 {
194         core::map<v3s16, NodeMetadata*>::Node *n;
195         n = m_data.find(p);
196         if(n == NULL)
197                 return NULL;
198         return n->getValue();
199 }
200
201 void NodeMetadataList::remove(v3s16 p)
202 {
203         NodeMetadata *olddata = get(p);
204         if(olddata)
205         {
206                 delete olddata;
207                 m_data.remove(p);
208         }
209 }
210
211 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
212 {
213         remove(p);
214         m_data.insert(p, d);
215 }
216
217 bool NodeMetadataList::step(float dtime)
218 {
219         bool something_changed = false;
220         for(core::map<v3s16, NodeMetadata*>::Iterator
221                         i = m_data.getIterator();
222                         i.atEnd()==false; i++)
223         {
224                 v3s16 p = i.getNode()->getKey();
225                 NodeMetadata *meta = i.getNode()->getValue();
226                 bool changed = meta->step(dtime);
227                 if(changed)
228                         something_changed = true;
229         }
230         return something_changed;
231 }
232