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