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