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