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