]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapnode.cpp
Mgv7, mgflat, mgfractal: Tunnel generation code optimisation
[dragonfireclient.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                 static const u8 rotate_facedir[24 * 4] = {
168                         // Table value = rotated facedir
169                         // Columns: 0, 90, 180, 270 degrees rotation around vertical axis
170                         // Rotation is anticlockwise as seen from above (+Y)
171
172                         0, 1, 2, 3,  // Initial facedir 0 to 3
173                         1, 2, 3, 0,
174                         2, 3, 0, 1,
175                         3, 0, 1, 2,
176
177                         4, 13, 10, 19,  // 4 to 7
178                         5, 14, 11, 16,
179                         6, 15, 8, 17,
180                         7, 12, 9, 18,
181
182                         8, 17, 6, 15,  // 8 to 11
183                         9, 18, 7, 12,
184                         10, 19, 4, 13,
185                         11, 16, 5, 14,
186
187                         12, 9, 18, 7,  // 12 to 15
188                         13, 10, 19, 4,
189                         14, 11, 16, 5,
190                         15, 8, 17, 6,
191
192                         16, 5, 14, 11,  // 16 to 19
193                         17, 6, 15, 8,
194                         18, 7, 12, 9,
195                         19, 4, 13, 10,
196
197                         20, 23, 22, 21,  // 20 to 23
198                         21, 20, 23, 22,
199                         22, 21, 20, 23,
200                         23, 22, 21, 20
201                 };
202                 u8 facedir = (param2 & 31) % 24;
203                 u8 index = facedir * 4 + rot;
204                 param2 &= ~31;
205                 param2 |= rotate_facedir[index];
206         } else if (cpt2 == CPT2_WALLMOUNTED) {
207                 u8 wmountface = (param2 & 7);
208                 if (wmountface <= 1)
209                         return;
210
211                 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
212                 param2 &= ~7;
213                 param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
214         }
215 }
216
217 void transformNodeBox(const MapNode &n, const NodeBox &nodebox,
218                 INodeDefManager *nodemgr, std::vector<aabb3f> *p_boxes, u8 neighbors = 0)
219 {
220         std::vector<aabb3f> &boxes = *p_boxes;
221
222         if (nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED) {
223                 const std::vector<aabb3f> &fixed = nodebox.fixed;
224                 int facedir = n.getFaceDir(nodemgr);
225                 u8 axisdir = facedir>>2;
226                 facedir&=0x03;
227                 for(std::vector<aabb3f>::const_iterator
228                                 i = fixed.begin();
229                                 i != fixed.end(); ++i)
230                 {
231                         aabb3f box = *i;
232
233                         if (nodebox.type == NODEBOX_LEVELED) {
234                                 box.MaxEdge.Y = -BS/2 + BS*((float)1/LEVELED_MAX) * n.getLevel(nodemgr);
235                         }
236
237                         switch (axisdir)
238                         {
239                         case 0:
240                                 if(facedir == 1)
241                                 {
242                                         box.MinEdge.rotateXZBy(-90);
243                                         box.MaxEdge.rotateXZBy(-90);
244                                 }
245                                 else if(facedir == 2)
246                                 {
247                                         box.MinEdge.rotateXZBy(180);
248                                         box.MaxEdge.rotateXZBy(180);
249                                 }
250                                 else if(facedir == 3)
251                                 {
252                                         box.MinEdge.rotateXZBy(90);
253                                         box.MaxEdge.rotateXZBy(90);
254                                 }
255                                 break;
256                         case 1: // z+
257                                 box.MinEdge.rotateYZBy(90);
258                                 box.MaxEdge.rotateYZBy(90);
259                                 if(facedir == 1)
260                                 {
261                                         box.MinEdge.rotateXYBy(90);
262                                         box.MaxEdge.rotateXYBy(90);
263                                 }
264                                 else if(facedir == 2)
265                                 {
266                                         box.MinEdge.rotateXYBy(180);
267                                         box.MaxEdge.rotateXYBy(180);
268                                 }
269                                 else if(facedir == 3)
270                                 {
271                                         box.MinEdge.rotateXYBy(-90);
272                                         box.MaxEdge.rotateXYBy(-90);
273                                 }
274                                 break;
275                         case 2: //z-
276                                 box.MinEdge.rotateYZBy(-90);
277                                 box.MaxEdge.rotateYZBy(-90);
278                                 if(facedir == 1)
279                                 {
280                                         box.MinEdge.rotateXYBy(-90);
281                                         box.MaxEdge.rotateXYBy(-90);
282                                 }
283                                 else if(facedir == 2)
284                                 {
285                                         box.MinEdge.rotateXYBy(180);
286                                         box.MaxEdge.rotateXYBy(180);
287                                 }
288                                 else if(facedir == 3)
289                                 {
290                                         box.MinEdge.rotateXYBy(90);
291                                         box.MaxEdge.rotateXYBy(90);
292                                 }
293                                 break;
294                         case 3:  //x+
295                                 box.MinEdge.rotateXYBy(-90);
296                                 box.MaxEdge.rotateXYBy(-90);
297                                 if(facedir == 1)
298                                 {
299                                         box.MinEdge.rotateYZBy(90);
300                                         box.MaxEdge.rotateYZBy(90);
301                                 }
302                                 else if(facedir == 2)
303                                 {
304                                         box.MinEdge.rotateYZBy(180);
305                                         box.MaxEdge.rotateYZBy(180);
306                                 }
307                                 else if(facedir == 3)
308                                 {
309                                         box.MinEdge.rotateYZBy(-90);
310                                         box.MaxEdge.rotateYZBy(-90);
311                                 }
312                                 break;
313                         case 4:  //x-
314                                 box.MinEdge.rotateXYBy(90);
315                                 box.MaxEdge.rotateXYBy(90);
316                                 if(facedir == 1)
317                                 {
318                                         box.MinEdge.rotateYZBy(-90);
319                                         box.MaxEdge.rotateYZBy(-90);
320                                 }
321                                 else if(facedir == 2)
322                                 {
323                                         box.MinEdge.rotateYZBy(180);
324                                         box.MaxEdge.rotateYZBy(180);
325                                 }
326                                 else if(facedir == 3)
327                                 {
328                                         box.MinEdge.rotateYZBy(90);
329                                         box.MaxEdge.rotateYZBy(90);
330                                 }
331                                 break;
332                         case 5:
333                                 box.MinEdge.rotateXYBy(-180);
334                                 box.MaxEdge.rotateXYBy(-180);
335                                 if(facedir == 1)
336                                 {
337                                         box.MinEdge.rotateXZBy(90);
338                                         box.MaxEdge.rotateXZBy(90);
339                                 }
340                                 else if(facedir == 2)
341                                 {
342                                         box.MinEdge.rotateXZBy(180);
343                                         box.MaxEdge.rotateXZBy(180);
344                                 }
345                                 else if(facedir == 3)
346                                 {
347                                         box.MinEdge.rotateXZBy(-90);
348                                         box.MaxEdge.rotateXZBy(-90);
349                                 }
350                                 break;
351                         default:
352                                 break;
353                         }
354                         box.repair();
355                         boxes.push_back(box);
356                 }
357         }
358         else if(nodebox.type == NODEBOX_WALLMOUNTED)
359         {
360                 v3s16 dir = n.getWallMountedDir(nodemgr);
361
362                 // top
363                 if(dir == v3s16(0,1,0))
364                 {
365                         boxes.push_back(nodebox.wall_top);
366                 }
367                 // bottom
368                 else if(dir == v3s16(0,-1,0))
369                 {
370                         boxes.push_back(nodebox.wall_bottom);
371                 }
372                 // side
373                 else
374                 {
375                         v3f vertices[2] =
376                         {
377                                 nodebox.wall_side.MinEdge,
378                                 nodebox.wall_side.MaxEdge
379                         };
380
381                         for(s32 i=0; i<2; i++)
382                         {
383                                 if(dir == v3s16(-1,0,0))
384                                         vertices[i].rotateXZBy(0);
385                                 if(dir == v3s16(1,0,0))
386                                         vertices[i].rotateXZBy(180);
387                                 if(dir == v3s16(0,0,-1))
388                                         vertices[i].rotateXZBy(90);
389                                 if(dir == v3s16(0,0,1))
390                                         vertices[i].rotateXZBy(-90);
391                         }
392
393                         aabb3f box = aabb3f(vertices[0]);
394                         box.addInternalPoint(vertices[1]);
395                         boxes.push_back(box);
396                 }
397         }
398         else if (nodebox.type == NODEBOX_CONNECTED)
399         {
400                 size_t boxes_size = boxes.size();
401                 boxes_size += nodebox.fixed.size();
402                 if (neighbors & 1)
403                         boxes_size += nodebox.connect_top.size();
404                 if (neighbors & 2)
405                         boxes_size += nodebox.connect_bottom.size();
406                 if (neighbors & 4)
407                         boxes_size += nodebox.connect_front.size();
408                 if (neighbors & 8)
409                         boxes_size += nodebox.connect_left.size();
410                 if (neighbors & 16)
411                         boxes_size += nodebox.connect_back.size();
412                 if (neighbors & 32)
413                         boxes_size += nodebox.connect_right.size();
414                 boxes.reserve(boxes_size);
415
416 #define BOXESPUSHBACK(c) do { \
417                 for (std::vector<aabb3f>::const_iterator \
418                                 it = (c).begin(); \
419                                 it != (c).end(); ++it) \
420                         (boxes).push_back(*it); \
421                 } while (0)
422
423                 BOXESPUSHBACK(nodebox.fixed);
424
425                 if (neighbors & 1)
426                         BOXESPUSHBACK(nodebox.connect_top);
427                 if (neighbors & 2)
428                         BOXESPUSHBACK(nodebox.connect_bottom);
429                 if (neighbors & 4)
430                         BOXESPUSHBACK(nodebox.connect_front);
431                 if (neighbors & 8)
432                         BOXESPUSHBACK(nodebox.connect_left);
433                 if (neighbors & 16)
434                         BOXESPUSHBACK(nodebox.connect_back);
435                 if (neighbors & 32)
436                         BOXESPUSHBACK(nodebox.connect_right);
437         }
438         else // NODEBOX_REGULAR
439         {
440                 boxes.push_back(aabb3f(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2));
441         }
442 }
443
444 void MapNode::getNodeBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes, u8 neighbors)
445 {
446         const ContentFeatures &f = nodemgr->get(*this);
447         transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
448 }
449
450 void MapNode::getCollisionBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes, u8 neighbors)
451 {
452         const ContentFeatures &f = nodemgr->get(*this);
453         if (f.collision_box.fixed.empty())
454                 transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
455         else
456                 transformNodeBox(*this, f.collision_box, nodemgr, boxes, neighbors);
457 }
458
459 void MapNode::getSelectionBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes, u8 neighbors)
460 {
461         const ContentFeatures &f = nodemgr->get(*this);
462         transformNodeBox(*this, f.selection_box, nodemgr, boxes, neighbors);
463 }
464
465 u8 MapNode::getMaxLevel(INodeDefManager *nodemgr) const
466 {
467         const ContentFeatures &f = nodemgr->get(*this);
468         // todo: after update in all games leave only if (f.param_type_2 ==
469         if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
470                 return LIQUID_LEVEL_MAX;
471         if(f.leveled || f.param_type_2 == CPT2_LEVELED)
472                 return LEVELED_MAX;
473         return 0;
474 }
475
476 u8 MapNode::getLevel(INodeDefManager *nodemgr) const
477 {
478         const ContentFeatures &f = nodemgr->get(*this);
479         // todo: after update in all games leave only if (f.param_type_2 ==
480         if(f.liquid_type == LIQUID_SOURCE)
481                 return LIQUID_LEVEL_SOURCE;
482         if (f.param_type_2 == CPT2_FLOWINGLIQUID)
483                 return getParam2() & LIQUID_LEVEL_MASK;
484         if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
485                 return getParam2() & LIQUID_LEVEL_MASK;
486         if(f.leveled || f.param_type_2 == CPT2_LEVELED) {
487                  u8 level = getParam2() & LEVELED_MASK;
488                  if(level)
489                         return level;
490                  if(f.leveled > LEVELED_MAX)
491                         return LEVELED_MAX;
492                  return f.leveled; //default
493         }
494         return 0;
495 }
496
497 u8 MapNode::setLevel(INodeDefManager *nodemgr, s8 level)
498 {
499         u8 rest = 0;
500         if (level < 1) {
501                 setContent(CONTENT_AIR);
502                 return 0;
503         }
504         const ContentFeatures &f = nodemgr->get(*this);
505         if (f.param_type_2 == CPT2_FLOWINGLIQUID
506                 || f.liquid_type == LIQUID_FLOWING
507                 || f.liquid_type == LIQUID_SOURCE) {
508                 if (level >= LIQUID_LEVEL_SOURCE) {
509                         rest = level - LIQUID_LEVEL_SOURCE;
510                         setContent(nodemgr->getId(f.liquid_alternative_source));
511                 } else {
512                         setContent(nodemgr->getId(f.liquid_alternative_flowing));
513                         setParam2(level & LIQUID_LEVEL_MASK);
514                 }
515         } else if (f.leveled || f.param_type_2 == CPT2_LEVELED) {
516                 if (level > LEVELED_MAX) {
517                         rest = level - LEVELED_MAX;
518                         level = LEVELED_MAX;
519                 }
520                 setParam2(level & LEVELED_MASK);
521         }
522         return rest;
523 }
524
525 u8 MapNode::addLevel(INodeDefManager *nodemgr, s8 add)
526 {
527         s8 level = getLevel(nodemgr);
528         if (add == 0) level = 1;
529         level += add;
530         return setLevel(nodemgr, level);
531 }
532
533 u32 MapNode::serializedLength(u8 version)
534 {
535         if(!ser_ver_supported(version))
536                 throw VersionMismatchException("ERROR: MapNode format not supported");
537
538         if(version == 0)
539                 return 1;
540         else if(version <= 9)
541                 return 2;
542         else if(version <= 23)
543                 return 3;
544         else
545                 return 4;
546 }
547 void MapNode::serialize(u8 *dest, u8 version)
548 {
549         if(!ser_ver_supported(version))
550                 throw VersionMismatchException("ERROR: MapNode format not supported");
551
552         // Can't do this anymore; we have 16-bit dynamically allocated node IDs
553         // in memory; conversion just won't work in this direction.
554         if(version < 24)
555                 throw SerializationError("MapNode::serialize: serialization to "
556                                 "version < 24 not possible");
557
558         writeU16(dest+0, param0);
559         writeU8(dest+2, param1);
560         writeU8(dest+3, param2);
561 }
562 void MapNode::deSerialize(u8 *source, u8 version)
563 {
564         if(!ser_ver_supported(version))
565                 throw VersionMismatchException("ERROR: MapNode format not supported");
566
567         if(version <= 21)
568         {
569                 deSerialize_pre22(source, version);
570                 return;
571         }
572
573         if(version >= 24){
574                 param0 = readU16(source+0);
575                 param1 = readU8(source+2);
576                 param2 = readU8(source+3);
577         }else{
578                 param0 = readU8(source+0);
579                 param1 = readU8(source+1);
580                 param2 = readU8(source+2);
581                 if(param0 > 0x7F){
582                         param0 |= ((param2&0xF0)<<4);
583                         param2 &= 0x0F;
584                 }
585         }
586 }
587 void MapNode::serializeBulk(std::ostream &os, int version,
588                 const MapNode *nodes, u32 nodecount,
589                 u8 content_width, u8 params_width, bool compressed)
590 {
591         if(!ser_ver_supported(version))
592                 throw VersionMismatchException("ERROR: MapNode format not supported");
593
594         sanity_check(content_width == 2);
595         sanity_check(params_width == 2);
596
597         // Can't do this anymore; we have 16-bit dynamically allocated node IDs
598         // in memory; conversion just won't work in this direction.
599         if(version < 24)
600                 throw SerializationError("MapNode::serializeBulk: serialization to "
601                                 "version < 24 not possible");
602
603         SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
604
605         // Serialize content
606         for(u32 i=0; i<nodecount; i++)
607                 writeU16(&databuf[i*2], nodes[i].param0);
608
609         // Serialize param1
610         u32 start1 = content_width * nodecount;
611         for(u32 i=0; i<nodecount; i++)
612                 writeU8(&databuf[start1 + i], nodes[i].param1);
613
614         // Serialize param2
615         u32 start2 = (content_width + 1) * nodecount;
616         for(u32 i=0; i<nodecount; i++)
617                 writeU8(&databuf[start2 + i], nodes[i].param2);
618
619         /*
620                 Compress data to output stream
621         */
622
623         if(compressed)
624         {
625                 compressZlib(databuf, os);
626         }
627         else
628         {
629                 os.write((const char*) &databuf[0], databuf.getSize());
630         }
631 }
632
633 // Deserialize bulk node data
634 void MapNode::deSerializeBulk(std::istream &is, int version,
635                 MapNode *nodes, u32 nodecount,
636                 u8 content_width, u8 params_width, bool compressed)
637 {
638         if(!ser_ver_supported(version))
639                 throw VersionMismatchException("ERROR: MapNode format not supported");
640
641         if (version < 22
642                         || (content_width != 1 && content_width != 2)
643                         || params_width != 2)
644                 FATAL_ERROR("Deserialize bulk node data error");
645
646         // Uncompress or read data
647         u32 len = nodecount * (content_width + params_width);
648         SharedBuffer<u8> databuf(len);
649         if(compressed)
650         {
651                 std::ostringstream os(std::ios_base::binary);
652                 decompressZlib(is, os);
653                 std::string s = os.str();
654                 if(s.size() != len)
655                         throw SerializationError("deSerializeBulkNodes: "
656                                         "decompress resulted in invalid size");
657                 memcpy(&databuf[0], s.c_str(), len);
658         }
659         else
660         {
661                 is.read((char*) &databuf[0], len);
662                 if(is.eof() || is.fail())
663                         throw SerializationError("deSerializeBulkNodes: "
664                                         "failed to read bulk node data");
665         }
666
667         // Deserialize content
668         if(content_width == 1)
669         {
670                 for(u32 i=0; i<nodecount; i++)
671                         nodes[i].param0 = readU8(&databuf[i]);
672         }
673         else if(content_width == 2)
674         {
675                 for(u32 i=0; i<nodecount; i++)
676                         nodes[i].param0 = readU16(&databuf[i*2]);
677         }
678
679         // Deserialize param1
680         u32 start1 = content_width * nodecount;
681         for(u32 i=0; i<nodecount; i++)
682                 nodes[i].param1 = readU8(&databuf[start1 + i]);
683
684         // Deserialize param2
685         u32 start2 = (content_width + 1) * nodecount;
686         if(content_width == 1)
687         {
688                 for(u32 i=0; i<nodecount; i++) {
689                         nodes[i].param2 = readU8(&databuf[start2 + i]);
690                         if(nodes[i].param0 > 0x7F){
691                                 nodes[i].param0 <<= 4;
692                                 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
693                                 nodes[i].param2 &= 0x0F;
694                         }
695                 }
696         }
697         else if(content_width == 2)
698         {
699                 for(u32 i=0; i<nodecount; i++)
700                         nodes[i].param2 = readU8(&databuf[start2 + i]);
701         }
702 }
703
704 /*
705         Legacy serialization
706 */
707 void MapNode::deSerialize_pre22(u8 *source, u8 version)
708 {
709         if(version <= 1)
710         {
711                 param0 = source[0];
712         }
713         else if(version <= 9)
714         {
715                 param0 = source[0];
716                 param1 = source[1];
717         }
718         else
719         {
720                 param0 = source[0];
721                 param1 = source[1];
722                 param2 = source[2];
723                 if(param0 > 0x7f){
724                         param0 <<= 4;
725                         param0 |= (param2&0xf0)>>4;
726                         param2 &= 0x0f;
727                 }
728         }
729
730         // Convert special values from old version to new
731         if(version <= 19)
732         {
733                 // In these versions, CONTENT_IGNORE and CONTENT_AIR
734                 // are 255 and 254
735                 // Version 19 is fucked up with sometimes the old values and sometimes not
736                 if(param0 == 255)
737                         param0 = CONTENT_IGNORE;
738                 else if(param0 == 254)
739                         param0 = CONTENT_AIR;
740         }
741
742         // Translate to our known version
743         *this = mapnode_translate_to_internal(*this, version);
744 }