]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapnode.cpp
0479d2e55e9e00a3f670c507c1ea8e7c9c055f1f
[dragonfireclient.git] / src / mapnode.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 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 "irrlichttypes_extrabloated.h"
21 #include "mapnode.h"
22 #include "porting.h"
23 #include "main.h" // For g_settings
24 #include "nodedef.h"
25 #include "content_mapnode.h" // For mapnode_translate_*_internal
26 #include "serialization.h" // For ser_ver_supported
27 #include "util/serialize.h"
28 #include <string>
29 #include <sstream>
30
31 /*
32         MapNode
33 */
34
35 // Create directly from a nodename
36 // If name is unknown, sets CONTENT_IGNORE
37 MapNode::MapNode(INodeDefManager *ndef, const std::string &name,
38                 u8 a_param1, u8 a_param2)
39 {
40         content_t id = CONTENT_IGNORE;
41         ndef->getId(name, id);
42         param0 = id;
43         param1 = a_param1;
44         param2 = a_param2;
45 }
46
47 void MapNode::setLight(enum LightBank bank, u8 a_light, INodeDefManager *nodemgr)
48 {
49         // If node doesn't contain light data, ignore this
50         if(nodemgr->get(*this).param_type != CPT_LIGHT)
51                 return;
52         if(bank == LIGHTBANK_DAY)
53         {
54                 param1 &= 0xf0;
55                 param1 |= a_light & 0x0f;
56         }
57         else if(bank == LIGHTBANK_NIGHT)
58         {
59                 param1 &= 0x0f;
60                 param1 |= (a_light & 0x0f)<<4;
61         }
62         else
63                 assert(0);
64 }
65
66 u8 MapNode::getLight(enum LightBank bank, INodeDefManager *nodemgr) const
67 {
68         // Select the brightest of [light source, propagated light]
69         const ContentFeatures &f = nodemgr->get(*this);
70         u8 light = 0;
71         if(f.param_type == CPT_LIGHT)
72         {
73                 if(bank == LIGHTBANK_DAY)
74                         light = param1 & 0x0f;
75                 else if(bank == LIGHTBANK_NIGHT)
76                         light = (param1>>4)&0x0f;
77                 else
78                         assert(0);
79         }
80         if(f.light_source > light)
81                 light = f.light_source;
82         return light;
83 }
84
85 bool MapNode::getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const
86 {
87         // Select the brightest of [light source, propagated light]
88         const ContentFeatures &f = nodemgr->get(*this);
89         if(f.param_type == CPT_LIGHT)
90         {
91                 lightday = param1 & 0x0f;
92                 lightnight = (param1>>4)&0x0f;
93         }
94         else
95         {
96                 lightday = 0;
97                 lightnight = 0;
98         }
99         if(f.light_source > lightday)
100                 lightday = f.light_source;
101         if(f.light_source > lightnight)
102                 lightnight = f.light_source;
103         return f.param_type == CPT_LIGHT || f.light_source != 0;
104 }
105
106 u8 MapNode::getFaceDir(INodeDefManager *nodemgr) const
107 {
108         const ContentFeatures &f = nodemgr->get(*this);
109         if(f.param_type_2 == CPT2_FACEDIR)
110                 return getParam2() & 0x03;
111         return 0;
112 }
113
114 u8 MapNode::getWallMounted(INodeDefManager *nodemgr) const
115 {
116         const ContentFeatures &f = nodemgr->get(*this);
117         if(f.param_type_2 == CPT2_WALLMOUNTED)
118                 return getParam2() & 0x07;
119         return 0;
120 }
121
122 v3s16 MapNode::getWallMountedDir(INodeDefManager *nodemgr) const
123 {
124         switch(getWallMounted(nodemgr))
125         {
126         case 0: default: return v3s16(0,1,0);
127         case 1: return v3s16(0,-1,0);
128         case 2: return v3s16(1,0,0);
129         case 3: return v3s16(-1,0,0);
130         case 4: return v3s16(0,0,1);
131         case 5: return v3s16(0,0,-1);
132         }
133 }
134
135 static std::vector<aabb3f> transformNodeBox(const MapNode &n,
136                 const NodeBox &nodebox, INodeDefManager *nodemgr)
137 {
138         std::vector<aabb3f> boxes;
139         if(nodebox.type == NODEBOX_FIXED)
140         {
141                 const std::vector<aabb3f> &fixed = nodebox.fixed;
142                 int facedir = n.getFaceDir(nodemgr);
143                 for(std::vector<aabb3f>::const_iterator
144                                 i = fixed.begin();
145                                 i != fixed.end(); i++)
146                 {
147                         aabb3f box = *i;
148                         if(facedir == 1)
149                         {
150                                 box.MinEdge.rotateXZBy(-90);
151                                 box.MaxEdge.rotateXZBy(-90);
152                                 box.repair();
153                         }
154                         else if(facedir == 2)
155                         {
156                                 box.MinEdge.rotateXZBy(180);
157                                 box.MaxEdge.rotateXZBy(180);
158                                 box.repair();
159                         }
160                         else if(facedir == 3)
161                         {
162                                 box.MinEdge.rotateXZBy(90);
163                                 box.MaxEdge.rotateXZBy(90);
164                                 box.repair();
165                         }
166                         boxes.push_back(box);
167                 }
168         }
169         else if(nodebox.type == NODEBOX_WALLMOUNTED)
170         {
171                 v3s16 dir = n.getWallMountedDir(nodemgr);
172
173                 // top
174                 if(dir == v3s16(0,1,0))
175                 {
176                         boxes.push_back(nodebox.wall_top);
177                 }
178                 // bottom
179                 else if(dir == v3s16(0,-1,0))
180                 {
181                         boxes.push_back(nodebox.wall_bottom);
182                 }
183                 // side
184                 else
185                 {
186                         v3f vertices[2] =
187                         {
188                                 nodebox.wall_side.MinEdge,
189                                 nodebox.wall_side.MaxEdge
190                         };
191
192                         for(s32 i=0; i<2; i++)
193                         {
194                                 if(dir == v3s16(-1,0,0))
195                                         vertices[i].rotateXZBy(0);
196                                 if(dir == v3s16(1,0,0))
197                                         vertices[i].rotateXZBy(180);
198                                 if(dir == v3s16(0,0,-1))
199                                         vertices[i].rotateXZBy(90);
200                                 if(dir == v3s16(0,0,1))
201                                         vertices[i].rotateXZBy(-90);
202                         }
203
204                         aabb3f box = aabb3f(vertices[0]);
205                         box.addInternalPoint(vertices[1]);
206                         boxes.push_back(box);
207                 }
208         }
209         else // NODEBOX_REGULAR
210         {
211                 boxes.push_back(aabb3f(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2));
212         }
213         return boxes;
214 }
215
216 std::vector<aabb3f> MapNode::getNodeBoxes(INodeDefManager *nodemgr) const
217 {
218         const ContentFeatures &f = nodemgr->get(*this);
219         return transformNodeBox(*this, f.node_box, nodemgr);
220 }
221
222 std::vector<aabb3f> MapNode::getSelectionBoxes(INodeDefManager *nodemgr) const
223 {
224         const ContentFeatures &f = nodemgr->get(*this);
225         return transformNodeBox(*this, f.selection_box, nodemgr);
226 }
227
228 u32 MapNode::serializedLength(u8 version)
229 {
230         if(!ser_ver_supported(version))
231                 throw VersionMismatchException("ERROR: MapNode format not supported");
232                 
233         if(version == 0)
234                 return 1;
235         else if(version <= 9)
236                 return 2;
237         else
238                 return 3;
239 }
240 void MapNode::serialize(u8 *dest, u8 version)
241 {
242         if(!ser_ver_supported(version))
243                 throw VersionMismatchException("ERROR: MapNode format not supported");
244
245         if(version <= 21)
246         {
247                 serialize_pre22(dest, version);
248                 return;
249         }
250
251         if(version >= 24){
252                 writeU16(dest+0, param0);
253                 writeU8(dest+2, param1);
254                 writeU8(dest+3, param2);
255         }
256         else{
257                 writeU8(dest+0, (param0&0xFF));
258                 writeU8(dest+1, param1);
259                 if (param0 > 0x7F){
260                         writeU8(dest+2, ((param2&0x0F) | ((param0&0x0F00)>>4)));
261                 }
262                 else{
263                         writeU8(dest+2, param2);
264                 }
265         }
266 }
267 void MapNode::deSerialize(u8 *source, u8 version)
268 {
269         if(!ser_ver_supported(version))
270                 throw VersionMismatchException("ERROR: MapNode format not supported");
271                 
272         if(version <= 21)
273         {
274                 deSerialize_pre22(source, version);
275                 return;
276         }
277
278         if(version >= 24){
279                 param0 = readU16(source+0);
280                 param1 = readU8(source+2);
281                 param2 = readU8(source+3);
282         }
283         else{
284                 param0 = readU8(source+0);
285                 param1 = readU8(source+1);
286                 param2 = readU8(source+2);
287                 if(param0 > 0x7F){
288                         param0 |= ((param2&0xF0)<<4);
289                         param2 &= 0x0F;
290                 }
291         }
292 }
293 void MapNode::serializeBulk(std::ostream &os, int version,
294                 const MapNode *nodes, u32 nodecount,
295                 u8 content_width, u8 params_width, bool compressed)
296 {
297         if(!ser_ver_supported(version))
298                 throw VersionMismatchException("ERROR: MapNode format not supported");
299
300         assert(version >= 22);
301         assert(content_width == 1 || content_width == 2);
302         assert(params_width == 2);
303
304         SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
305
306         // Serialize content
307         if(content_width == 1)
308         {
309                 for(u32 i=0; i<nodecount; i++)
310                         writeU8(&databuf[i], (nodes[i].param0&0x00FF));
311         }else if(content_width == 2)
312         {
313                 for(u32 i=0; i<nodecount; i++)
314                         writeU16(&databuf[i*2], nodes[i].param0);
315         }
316
317         // Serialize param1
318         u32 start1 = content_width * nodecount;
319         for(u32 i=0; i<nodecount; i++)
320                 writeU8(&databuf[start1 + i], nodes[i].param1);
321
322         // Serialize param2
323         u32 start2 = (content_width + 1) * nodecount;
324         if(content_width == 1)
325         {
326                 for(u32 i=0; i<nodecount; i++) {
327                         if(nodes[i].param0 > 0x7F){
328                                 writeU8(&databuf[start2 + i], ((nodes[i].param2&0x0F) | ((nodes[i].param0&0x0F00)>>4)));
329                         }
330                         else{
331                                 writeU8(&databuf[start2 + i], nodes[i].param2);
332                         }
333                 }
334         }else if(content_width == 2)
335         {
336                 for(u32 i=0; i<nodecount; i++)
337                         writeU8(&databuf[start2 + i], nodes[i].param2);
338         }
339
340         /*
341                 Compress data to output stream
342         */
343
344         if(compressed)
345         {
346                 compressZlib(databuf, os);
347         }
348         else
349         {
350                 os.write((const char*) &databuf[0], databuf.getSize());
351         }
352 }
353
354 // Deserialize bulk node data
355 void MapNode::deSerializeBulk(std::istream &is, int version,
356                 MapNode *nodes, u32 nodecount,
357                 u8 content_width, u8 params_width, bool compressed)
358 {
359         if(!ser_ver_supported(version))
360                 throw VersionMismatchException("ERROR: MapNode format not supported");
361
362         assert(version >= 22);
363         assert(content_width == 1 || content_width == 2);
364         assert(params_width == 2);
365
366         // Uncompress or read data
367         u32 len = nodecount * (content_width + params_width);
368         SharedBuffer<u8> databuf(len);
369         if(compressed)
370         {
371                 std::ostringstream os(std::ios_base::binary);
372                 decompressZlib(is, os);
373                 std::string s = os.str();
374                 if(s.size() != len)
375                         throw SerializationError("deSerializeBulkNodes: "
376                                         "decompress resulted in invalid size");
377                 memcpy(&databuf[0], s.c_str(), len);
378         }
379         else
380         {
381                 is.read((char*) &databuf[0], len);
382                 if(is.eof() || is.fail())
383                         throw SerializationError("deSerializeBulkNodes: "
384                                         "failed to read bulk node data");
385         }
386
387         // Deserialize content
388         if(content_width == 1)
389         {
390                 for(u32 i=0; i<nodecount; i++)
391                         nodes[i].param0 = readU8(&databuf[i]);
392         }
393         else if(content_width == 2)
394         {
395                 for(u32 i=0; i<nodecount; i++)
396                         nodes[i].param0 = readU16(&databuf[i*2]);
397         }
398
399         // Deserialize param1
400         u32 start1 = content_width * nodecount;
401         for(u32 i=0; i<nodecount; i++)
402                 nodes[i].param1 = readU8(&databuf[start1 + i]);
403
404         // Deserialize param2
405         u32 start2 = (content_width + 1) * nodecount;
406         if(content_width == 1)
407         {
408                 for(u32 i=0; i<nodecount; i++) {
409                         nodes[i].param2 = readU8(&databuf[start2 + i]);
410                         if(nodes[i].param0 > 0x7F){
411                                 nodes[i].param0 |= ((nodes[i].param2&0xF0)<<4);
412                                 nodes[i].param2 &= 0x0F;
413                         }
414                 }
415         }
416         else if(content_width == 2)
417         {
418                 for(u32 i=0; i<nodecount; i++)
419                         nodes[i].param2 = readU8(&databuf[start2 + i]);
420         }
421 }
422
423 /*
424         Legacy serialization
425 */
426 void MapNode::serialize_pre22(u8 *dest, u8 version)
427 {
428         // Translate to wanted version
429         MapNode n_foreign = mapnode_translate_from_internal(*this, version);
430
431         u8 actual_param0 = n_foreign.param0;
432
433         // Convert special values from new version to old
434         if(version <= 18)
435         {
436                 // In these versions, CONTENT_IGNORE and CONTENT_AIR
437                 // are 255 and 254
438                 if(actual_param0 == CONTENT_IGNORE)
439                         actual_param0 = 255;
440                 else if(actual_param0 == CONTENT_AIR)
441                         actual_param0 = 254;
442         }
443
444         if(version == 0)
445         {
446                 dest[0] = actual_param0;
447         }
448         else if(version <= 9)
449         {
450                 dest[0] = actual_param0;
451                 dest[1] = n_foreign.param1;
452         }
453         else
454         {
455                 dest[0] = actual_param0;
456                 dest[1] = n_foreign.param1;
457                 dest[2] = n_foreign.param2;
458         }
459 }
460 void MapNode::deSerialize_pre22(u8 *source, u8 version)
461 {
462         if(version <= 1)
463         {
464                 param0 = source[0];
465         }
466         else if(version <= 9)
467         {
468                 param0 = source[0];
469                 param1 = source[1];
470         }
471         else
472         {
473                 param0 = source[0];
474                 param1 = source[1];
475                 param2 = source[2];
476         }
477         
478         // Convert special values from old version to new
479         if(version <= 19)
480         {
481                 // In these versions, CONTENT_IGNORE and CONTENT_AIR
482                 // are 255 and 254
483                 // Version 19 is fucked up with sometimes the old values and sometimes not
484                 if(param0 == 255)
485                         param0 = CONTENT_IGNORE;
486                 else if(param0 == 254)
487                         param0 = CONTENT_AIR;
488         }
489
490         // Translate to our known version
491         *this = mapnode_translate_to_internal(*this, version);
492 }