Mapnode: Add rotateAlongYAxisFull supporting 24 facedirs
[oweals/minetest.git] / src / mapnode.cpp
1 /*
2 Minetest
3 Copyright (C) 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 "irrlichttypes_extrabloated.h"
21 #include "mapnode.h"
22 #include "porting.h"
23 #include "nodedef.h"
24 #include "content_mapnode.h" // For mapnode_translate_*_internal
25 #include "serialization.h" // For ser_ver_supported
26 #include "util/serialize.h"
27 #include "log.h"
28 #include "util/numeric.h"
29 #include <string>
30 #include <sstream>
31
32 static const Rotation wallmounted_to_rot[] = {
33         ROTATE_0, ROTATE_180, ROTATE_90, ROTATE_270
34 };
35
36 static const u8 rot_to_wallmounted[] = {
37         2, 4, 3, 5
38 };
39
40
41 /*
42         MapNode
43 */
44
45 // Create directly from a nodename
46 // If name is unknown, sets CONTENT_IGNORE
47 MapNode::MapNode(INodeDefManager *ndef, const std::string &name,
48                 u8 a_param1, u8 a_param2)
49 {
50         content_t id = CONTENT_IGNORE;
51         ndef->getId(name, id);
52         param0 = id;
53         param1 = a_param1;
54         param2 = a_param2;
55 }
56
57 void MapNode::setLight(enum LightBank bank, u8 a_light, INodeDefManager *nodemgr)
58 {
59         // If node doesn't contain light data, ignore this
60         if(nodemgr->get(*this).param_type != CPT_LIGHT)
61                 return;
62         if(bank == LIGHTBANK_DAY)
63         {
64                 param1 &= 0xf0;
65                 param1 |= a_light & 0x0f;
66         }
67         else if(bank == LIGHTBANK_NIGHT)
68         {
69                 param1 &= 0x0f;
70                 param1 |= (a_light & 0x0f)<<4;
71         }
72         else
73                 assert("Invalid light bank" == NULL);
74 }
75
76 bool MapNode::isLightDayNightEq(INodeDefManager *nodemgr) const
77 {
78         const ContentFeatures &f = nodemgr->get(*this);
79         bool isEqual;
80
81         if (f.param_type == CPT_LIGHT) {
82                 u8 day   = MYMAX(f.light_source, param1 & 0x0f);
83                 u8 night = MYMAX(f.light_source, (param1 >> 4) & 0x0f);
84                 isEqual = day == night;
85         } else {
86                 isEqual = true;
87         }
88
89         return isEqual;
90 }
91
92 u8 MapNode::getLight(enum LightBank bank, INodeDefManager *nodemgr) const
93 {
94         // Select the brightest of [light source, propagated light]
95         const ContentFeatures &f = nodemgr->get(*this);
96
97         u8 light;
98         if(f.param_type == CPT_LIGHT)
99                 light = bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f;
100         else
101                 light = 0;
102
103         return MYMAX(f.light_source, light);
104 }
105
106 u8 MapNode::getLightNoChecks(enum LightBank bank, const ContentFeatures *f) const
107 {
108         return MYMAX(f->light_source,
109                      bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f);
110 }
111
112 bool MapNode::getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const
113 {
114         // Select the brightest of [light source, propagated light]
115         const ContentFeatures &f = nodemgr->get(*this);
116         if(f.param_type == CPT_LIGHT)
117         {
118                 lightday = param1 & 0x0f;
119                 lightnight = (param1>>4)&0x0f;
120         }
121         else
122         {
123                 lightday = 0;
124                 lightnight = 0;
125         }
126         if(f.light_source > lightday)
127                 lightday = f.light_source;
128         if(f.light_source > lightnight)
129                 lightnight = f.light_source;
130         return f.param_type == CPT_LIGHT || f.light_source != 0;
131 }
132
133 u8 MapNode::getFaceDir(INodeDefManager *nodemgr) const
134 {
135         const ContentFeatures &f = nodemgr->get(*this);
136         if(f.param_type_2 == CPT2_FACEDIR)
137                 return (getParam2() & 0x1F) % 24;
138         return 0;
139 }
140
141 u8 MapNode::getWallMounted(INodeDefManager *nodemgr) const
142 {
143         const ContentFeatures &f = nodemgr->get(*this);
144         if(f.param_type_2 == CPT2_WALLMOUNTED)
145                 return getParam2() & 0x07;
146         return 0;
147 }
148
149 v3s16 MapNode::getWallMountedDir(INodeDefManager *nodemgr) const
150 {
151         switch(getWallMounted(nodemgr))
152         {
153         case 0: default: return v3s16(0,1,0);
154         case 1: return v3s16(0,-1,0);
155         case 2: return v3s16(1,0,0);
156         case 3: return v3s16(-1,0,0);
157         case 4: return v3s16(0,0,1);
158         case 5: return v3s16(0,0,-1);
159         }
160 }
161
162 void MapNode::rotateAlongYAxis(INodeDefManager *nodemgr, Rotation rot)
163 {
164         ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
165
166         if (cpt2 == CPT2_FACEDIR) {
167                 if (param2 >= 4)
168                         return;
169
170                 u8 newrot = param2 & 3;
171                 param2 &= ~3;
172                 param2 |= (newrot + rot) & 3;
173         } else if (cpt2 == CPT2_WALLMOUNTED) {
174                 u8 wmountface = (param2 & 7);
175                 if (wmountface <= 1)
176                         return;
177
178                 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
179                 param2 &= ~7;
180                 param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
181         }
182 }
183
184 void MapNode::rotateAlongYAxisFull(INodeDefManager *nodemgr, Rotation rot)
185 {
186         ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
187
188         if (cpt2 == CPT2_FACEDIR) {
189                 static const u16 rotate_facedir[24 * 4] = {
190                         // Table value = rotated facedir
191                         // Columns: 0, 90, 180, 270 degrees rotation around vertical axis
192                         // Rotation is anticlockwise as seen from above (+Y)
193
194                         0, 1, 2, 3,  // Initial facedir 0 to 3
195                         1, 2, 3, 0,
196                         2, 3, 0, 1,
197                         3, 0, 1, 2,
198
199                         4, 13, 10, 19,  // 4 to 7
200                         5, 14, 11, 16,
201                         6, 15, 8, 17,
202                         7, 12, 9, 18,
203
204                         8, 17, 6, 15,  // 8 to 11
205                         9, 18, 7, 12,
206                         10, 19, 4, 13,
207                         11, 16, 5, 14,
208
209                         12, 9, 18, 7,  // 12 to 15
210                         13, 10, 19, 4,
211                         14, 11, 16, 5,
212                         15, 8, 17, 6,
213
214                         16, 5, 14, 11,  // 16 to 19
215                         17, 6, 15, 8,
216                         18, 7, 12, 9,
217                         19, 4, 13, 10,
218
219                         20, 23, 22, 21,  // 20 to 23
220                         21, 20, 23, 22,
221                         22, 21, 20, 23,
222                         23, 22, 21, 20
223                 };
224                 u16 index = param2 * 4 + rot;
225                 param2 = rotate_facedir[index];
226         } else if (cpt2 == CPT2_WALLMOUNTED) {
227                 u8 wmountface = (param2 & 7);
228                 if (wmountface <= 1)
229                         return;
230
231                 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
232                 param2 &= ~7;
233                 param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
234         }
235 }
236
237 static std::vector<aabb3f> transformNodeBox(const MapNode &n,
238                 const NodeBox &nodebox, INodeDefManager *nodemgr)
239 {
240         std::vector<aabb3f> boxes;
241         if(nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED)
242         {
243                 const std::vector<aabb3f> &fixed = nodebox.fixed;
244                 int facedir = n.getFaceDir(nodemgr);
245                 u8 axisdir = facedir>>2;
246                 facedir&=0x03;
247                 for(std::vector<aabb3f>::const_iterator
248                                 i = fixed.begin();
249                                 i != fixed.end(); ++i)
250                 {
251                         aabb3f box = *i;
252
253                         if (nodebox.type == NODEBOX_LEVELED) {
254                                 box.MaxEdge.Y = -BS/2 + BS*((float)1/LEVELED_MAX) * n.getLevel(nodemgr);
255                         }
256
257                         switch (axisdir)
258                         {
259                         case 0:
260                                 if(facedir == 1)
261                                 {
262                                         box.MinEdge.rotateXZBy(-90);
263                                         box.MaxEdge.rotateXZBy(-90);
264                                 }
265                                 else if(facedir == 2)
266                                 {
267                                         box.MinEdge.rotateXZBy(180);
268                                         box.MaxEdge.rotateXZBy(180);
269                                 }
270                                 else if(facedir == 3)
271                                 {
272                                         box.MinEdge.rotateXZBy(90);
273                                         box.MaxEdge.rotateXZBy(90);
274                                 }
275                                 break;
276                         case 1: // z+
277                                 box.MinEdge.rotateYZBy(90);
278                                 box.MaxEdge.rotateYZBy(90);
279                                 if(facedir == 1)
280                                 {
281                                         box.MinEdge.rotateXYBy(90);
282                                         box.MaxEdge.rotateXYBy(90);
283                                 }
284                                 else if(facedir == 2)
285                                 {
286                                         box.MinEdge.rotateXYBy(180);
287                                         box.MaxEdge.rotateXYBy(180);
288                                 }
289                                 else if(facedir == 3)
290                                 {
291                                         box.MinEdge.rotateXYBy(-90);
292                                         box.MaxEdge.rotateXYBy(-90);
293                                 }
294                                 break;
295                         case 2: //z-
296                                 box.MinEdge.rotateYZBy(-90);
297                                 box.MaxEdge.rotateYZBy(-90);
298                                 if(facedir == 1)
299                                 {
300                                         box.MinEdge.rotateXYBy(-90);
301                                         box.MaxEdge.rotateXYBy(-90);
302                                 }
303                                 else if(facedir == 2)
304                                 {
305                                         box.MinEdge.rotateXYBy(180);
306                                         box.MaxEdge.rotateXYBy(180);
307                                 }
308                                 else if(facedir == 3)
309                                 {
310                                         box.MinEdge.rotateXYBy(90);
311                                         box.MaxEdge.rotateXYBy(90);
312                                 }
313                                 break;
314                         case 3:  //x+
315                                 box.MinEdge.rotateXYBy(-90);
316                                 box.MaxEdge.rotateXYBy(-90);
317                                 if(facedir == 1)
318                                 {
319                                         box.MinEdge.rotateYZBy(90);
320                                         box.MaxEdge.rotateYZBy(90);
321                                 }
322                                 else if(facedir == 2)
323                                 {
324                                         box.MinEdge.rotateYZBy(180);
325                                         box.MaxEdge.rotateYZBy(180);
326                                 }
327                                 else if(facedir == 3)
328                                 {
329                                         box.MinEdge.rotateYZBy(-90);
330                                         box.MaxEdge.rotateYZBy(-90);
331                                 }
332                                 break;
333                         case 4:  //x-
334                                 box.MinEdge.rotateXYBy(90);
335                                 box.MaxEdge.rotateXYBy(90);
336                                 if(facedir == 1)
337                                 {
338                                         box.MinEdge.rotateYZBy(-90);
339                                         box.MaxEdge.rotateYZBy(-90);
340                                 }
341                                 else if(facedir == 2)
342                                 {
343                                         box.MinEdge.rotateYZBy(180);
344                                         box.MaxEdge.rotateYZBy(180);
345                                 }
346                                 else if(facedir == 3)
347                                 {
348                                         box.MinEdge.rotateYZBy(90);
349                                         box.MaxEdge.rotateYZBy(90);
350                                 }
351                                 break;
352                         case 5:
353                                 box.MinEdge.rotateXYBy(-180);
354                                 box.MaxEdge.rotateXYBy(-180);
355                                 if(facedir == 1)
356                                 {
357                                         box.MinEdge.rotateXZBy(90);
358                                         box.MaxEdge.rotateXZBy(90);
359                                 }
360                                 else if(facedir == 2)
361                                 {
362                                         box.MinEdge.rotateXZBy(180);
363                                         box.MaxEdge.rotateXZBy(180);
364                                 }
365                                 else if(facedir == 3)
366                                 {
367                                         box.MinEdge.rotateXZBy(-90);
368                                         box.MaxEdge.rotateXZBy(-90);
369                                 }
370                                 break;
371                         default:
372                                 break;
373                         }
374                         box.repair();
375                         boxes.push_back(box);
376                 }
377         }
378         else if(nodebox.type == NODEBOX_WALLMOUNTED)
379         {
380                 v3s16 dir = n.getWallMountedDir(nodemgr);
381
382                 // top
383                 if(dir == v3s16(0,1,0))
384                 {
385                         boxes.push_back(nodebox.wall_top);
386                 }
387                 // bottom
388                 else if(dir == v3s16(0,-1,0))
389                 {
390                         boxes.push_back(nodebox.wall_bottom);
391                 }
392                 // side
393                 else
394                 {
395                         v3f vertices[2] =
396                         {
397                                 nodebox.wall_side.MinEdge,
398                                 nodebox.wall_side.MaxEdge
399                         };
400
401                         for(s32 i=0; i<2; i++)
402                         {
403                                 if(dir == v3s16(-1,0,0))
404                                         vertices[i].rotateXZBy(0);
405                                 if(dir == v3s16(1,0,0))
406                                         vertices[i].rotateXZBy(180);
407                                 if(dir == v3s16(0,0,-1))
408                                         vertices[i].rotateXZBy(90);
409                                 if(dir == v3s16(0,0,1))
410                                         vertices[i].rotateXZBy(-90);
411                         }
412
413                         aabb3f box = aabb3f(vertices[0]);
414                         box.addInternalPoint(vertices[1]);
415                         boxes.push_back(box);
416                 }
417         }
418         else // NODEBOX_REGULAR
419         {
420                 boxes.push_back(aabb3f(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2));
421         }
422         return boxes;
423 }
424
425 std::vector<aabb3f> MapNode::getNodeBoxes(INodeDefManager *nodemgr) const
426 {
427         const ContentFeatures &f = nodemgr->get(*this);
428         return transformNodeBox(*this, f.node_box, nodemgr);
429 }
430
431 std::vector<aabb3f> MapNode::getCollisionBoxes(INodeDefManager *nodemgr) const
432 {
433         const ContentFeatures &f = nodemgr->get(*this);
434         if (f.collision_box.fixed.empty())
435                 return transformNodeBox(*this, f.node_box, nodemgr);
436         else
437                 return transformNodeBox(*this, f.collision_box, nodemgr);
438 }
439
440 std::vector<aabb3f> MapNode::getSelectionBoxes(INodeDefManager *nodemgr) const
441 {
442         const ContentFeatures &f = nodemgr->get(*this);
443         return transformNodeBox(*this, f.selection_box, nodemgr);
444 }
445
446 u8 MapNode::getMaxLevel(INodeDefManager *nodemgr) const
447 {
448         const ContentFeatures &f = nodemgr->get(*this);
449         // todo: after update in all games leave only if (f.param_type_2 ==
450         if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
451                 return LIQUID_LEVEL_MAX;
452         if(f.leveled || f.param_type_2 == CPT2_LEVELED)
453                 return LEVELED_MAX;
454         return 0;
455 }
456
457 u8 MapNode::getLevel(INodeDefManager *nodemgr) const
458 {
459         const ContentFeatures &f = nodemgr->get(*this);
460         // todo: after update in all games leave only if (f.param_type_2 ==
461         if(f.liquid_type == LIQUID_SOURCE)
462                 return LIQUID_LEVEL_SOURCE;
463         if (f.param_type_2 == CPT2_FLOWINGLIQUID)
464                 return getParam2() & LIQUID_LEVEL_MASK;
465         if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
466                 return getParam2() & LIQUID_LEVEL_MASK;
467         if(f.leveled || f.param_type_2 == CPT2_LEVELED) {
468                  u8 level = getParam2() & LEVELED_MASK;
469                  if(level)
470                         return level;
471                  if(f.leveled > LEVELED_MAX)
472                         return LEVELED_MAX;
473                  return f.leveled; //default
474         }
475         return 0;
476 }
477
478 u8 MapNode::setLevel(INodeDefManager *nodemgr, s8 level)
479 {
480         u8 rest = 0;
481         if (level < 1) {
482                 setContent(CONTENT_AIR);
483                 return 0;
484         }
485         const ContentFeatures &f = nodemgr->get(*this);
486         if (f.param_type_2 == CPT2_FLOWINGLIQUID
487                 || f.liquid_type == LIQUID_FLOWING
488                 || f.liquid_type == LIQUID_SOURCE) {
489                 if (level >= LIQUID_LEVEL_SOURCE) {
490                         rest = level - LIQUID_LEVEL_SOURCE;
491                         setContent(nodemgr->getId(f.liquid_alternative_source));
492                 } else {
493                         setContent(nodemgr->getId(f.liquid_alternative_flowing));
494                         setParam2(level & LIQUID_LEVEL_MASK);
495                 }
496         } else if (f.leveled || f.param_type_2 == CPT2_LEVELED) {
497                 if (level > LEVELED_MAX) {
498                         rest = level - LEVELED_MAX;
499                         level = LEVELED_MAX;
500                 }
501                 setParam2(level & LEVELED_MASK);
502         }
503         return rest;
504 }
505
506 u8 MapNode::addLevel(INodeDefManager *nodemgr, s8 add)
507 {
508         s8 level = getLevel(nodemgr);
509         if (add == 0) level = 1;
510         level += add;
511         return setLevel(nodemgr, level);
512 }
513
514 u32 MapNode::serializedLength(u8 version)
515 {
516         if(!ser_ver_supported(version))
517                 throw VersionMismatchException("ERROR: MapNode format not supported");
518
519         if(version == 0)
520                 return 1;
521         else if(version <= 9)
522                 return 2;
523         else if(version <= 23)
524                 return 3;
525         else
526                 return 4;
527 }
528 void MapNode::serialize(u8 *dest, u8 version)
529 {
530         if(!ser_ver_supported(version))
531                 throw VersionMismatchException("ERROR: MapNode format not supported");
532
533         // Can't do this anymore; we have 16-bit dynamically allocated node IDs
534         // in memory; conversion just won't work in this direction.
535         if(version < 24)
536                 throw SerializationError("MapNode::serialize: serialization to "
537                                 "version < 24 not possible");
538
539         writeU16(dest+0, param0);
540         writeU8(dest+2, param1);
541         writeU8(dest+3, param2);
542 }
543 void MapNode::deSerialize(u8 *source, u8 version)
544 {
545         if(!ser_ver_supported(version))
546                 throw VersionMismatchException("ERROR: MapNode format not supported");
547
548         if(version <= 21)
549         {
550                 deSerialize_pre22(source, version);
551                 return;
552         }
553
554         if(version >= 24){
555                 param0 = readU16(source+0);
556                 param1 = readU8(source+2);
557                 param2 = readU8(source+3);
558         }else{
559                 param0 = readU8(source+0);
560                 param1 = readU8(source+1);
561                 param2 = readU8(source+2);
562                 if(param0 > 0x7F){
563                         param0 |= ((param2&0xF0)<<4);
564                         param2 &= 0x0F;
565                 }
566         }
567 }
568 void MapNode::serializeBulk(std::ostream &os, int version,
569                 const MapNode *nodes, u32 nodecount,
570                 u8 content_width, u8 params_width, bool compressed)
571 {
572         if(!ser_ver_supported(version))
573                 throw VersionMismatchException("ERROR: MapNode format not supported");
574
575         sanity_check(content_width == 2);
576         sanity_check(params_width == 2);
577
578         // Can't do this anymore; we have 16-bit dynamically allocated node IDs
579         // in memory; conversion just won't work in this direction.
580         if(version < 24)
581                 throw SerializationError("MapNode::serializeBulk: serialization to "
582                                 "version < 24 not possible");
583
584         SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
585
586         // Serialize content
587         for(u32 i=0; i<nodecount; i++)
588                 writeU16(&databuf[i*2], nodes[i].param0);
589
590         // Serialize param1
591         u32 start1 = content_width * nodecount;
592         for(u32 i=0; i<nodecount; i++)
593                 writeU8(&databuf[start1 + i], nodes[i].param1);
594
595         // Serialize param2
596         u32 start2 = (content_width + 1) * nodecount;
597         for(u32 i=0; i<nodecount; i++)
598                 writeU8(&databuf[start2 + i], nodes[i].param2);
599
600         /*
601                 Compress data to output stream
602         */
603
604         if(compressed)
605         {
606                 compressZlib(databuf, os);
607         }
608         else
609         {
610                 os.write((const char*) &databuf[0], databuf.getSize());
611         }
612 }
613
614 // Deserialize bulk node data
615 void MapNode::deSerializeBulk(std::istream &is, int version,
616                 MapNode *nodes, u32 nodecount,
617                 u8 content_width, u8 params_width, bool compressed)
618 {
619         if(!ser_ver_supported(version))
620                 throw VersionMismatchException("ERROR: MapNode format not supported");
621
622         if (version < 22
623                         || (content_width != 1 && content_width != 2)
624                         || params_width != 2)
625                 FATAL_ERROR("Deserialize bulk node data error");
626
627         // Uncompress or read data
628         u32 len = nodecount * (content_width + params_width);
629         SharedBuffer<u8> databuf(len);
630         if(compressed)
631         {
632                 std::ostringstream os(std::ios_base::binary);
633                 decompressZlib(is, os);
634                 std::string s = os.str();
635                 if(s.size() != len)
636                         throw SerializationError("deSerializeBulkNodes: "
637                                         "decompress resulted in invalid size");
638                 memcpy(&databuf[0], s.c_str(), len);
639         }
640         else
641         {
642                 is.read((char*) &databuf[0], len);
643                 if(is.eof() || is.fail())
644                         throw SerializationError("deSerializeBulkNodes: "
645                                         "failed to read bulk node data");
646         }
647
648         // Deserialize content
649         if(content_width == 1)
650         {
651                 for(u32 i=0; i<nodecount; i++)
652                         nodes[i].param0 = readU8(&databuf[i]);
653         }
654         else if(content_width == 2)
655         {
656                 for(u32 i=0; i<nodecount; i++)
657                         nodes[i].param0 = readU16(&databuf[i*2]);
658         }
659
660         // Deserialize param1
661         u32 start1 = content_width * nodecount;
662         for(u32 i=0; i<nodecount; i++)
663                 nodes[i].param1 = readU8(&databuf[start1 + i]);
664
665         // Deserialize param2
666         u32 start2 = (content_width + 1) * nodecount;
667         if(content_width == 1)
668         {
669                 for(u32 i=0; i<nodecount; i++) {
670                         nodes[i].param2 = readU8(&databuf[start2 + i]);
671                         if(nodes[i].param0 > 0x7F){
672                                 nodes[i].param0 <<= 4;
673                                 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
674                                 nodes[i].param2 &= 0x0F;
675                         }
676                 }
677         }
678         else if(content_width == 2)
679         {
680                 for(u32 i=0; i<nodecount; i++)
681                         nodes[i].param2 = readU8(&databuf[start2 + i]);
682         }
683 }
684
685 /*
686         Legacy serialization
687 */
688 void MapNode::deSerialize_pre22(u8 *source, u8 version)
689 {
690         if(version <= 1)
691         {
692                 param0 = source[0];
693         }
694         else if(version <= 9)
695         {
696                 param0 = source[0];
697                 param1 = source[1];
698         }
699         else
700         {
701                 param0 = source[0];
702                 param1 = source[1];
703                 param2 = source[2];
704                 if(param0 > 0x7f){
705                         param0 <<= 4;
706                         param0 |= (param2&0xf0)>>4;
707                         param2 &= 0x0f;
708                 }
709         }
710
711         // Convert special values from old version to new
712         if(version <= 19)
713         {
714                 // In these versions, CONTENT_IGNORE and CONTENT_AIR
715                 // are 255 and 254
716                 // Version 19 is fucked up with sometimes the old values and sometimes not
717                 if(param0 == 255)
718                         param0 = CONTENT_IGNORE;
719                 else if(param0 == 254)
720                         param0 = CONTENT_AIR;
721         }
722
723         // Translate to our known version
724         *this = mapnode_translate_to_internal(*this, version);
725 }