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