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