]> git.lizzy.rs Git - minetest.git/blob - src/mapnode.cpp
Fix detach inventory serialisation (#8331)
[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 // Create directly from a nodename
48 // If name is unknown, sets CONTENT_IGNORE
49 MapNode::MapNode(const NodeDefManager *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(LightBank bank, u8 a_light, const ContentFeatures &f) noexcept
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(LightBank bank, u8 a_light, const NodeDefManager *nodemgr)
88 {
89         setLight(bank, a_light, nodemgr->get(*this));
90 }
91
92 bool MapNode::isLightDayNightEq(const NodeDefManager *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(LightBank bank, const NodeDefManager *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(LightBank bank, const ContentFeatures &f) const noexcept
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(LightBank bank, const ContentFeatures *f) const noexcept
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,
136         const NodeDefManager *nodemgr) const
137 {
138         // Select the brightest of [light source, propagated light]
139         const ContentFeatures &f = nodemgr->get(*this);
140         if(f.param_type == CPT_LIGHT)
141         {
142                 lightday = param1 & 0x0f;
143                 lightnight = (param1>>4)&0x0f;
144         }
145         else
146         {
147                 lightday = 0;
148                 lightnight = 0;
149         }
150         if(f.light_source > lightday)
151                 lightday = f.light_source;
152         if(f.light_source > lightnight)
153                 lightnight = f.light_source;
154         return f.param_type == CPT_LIGHT || f.light_source != 0;
155 }
156
157 u8 MapNode::getFaceDir(const NodeDefManager *nodemgr,
158         bool allow_wallmounted) const
159 {
160         const ContentFeatures &f = nodemgr->get(*this);
161         if (f.param_type_2 == CPT2_FACEDIR ||
162                         f.param_type_2 == CPT2_COLORED_FACEDIR)
163                 return (getParam2() & 0x1F) % 24;
164         if (allow_wallmounted && (f.param_type_2 == CPT2_WALLMOUNTED ||
165                         f.param_type_2 == CPT2_COLORED_WALLMOUNTED))
166                 return wallmounted_to_facedir[getParam2() & 0x07];
167         return 0;
168 }
169
170 u8 MapNode::getWallMounted(const NodeDefManager *nodemgr) const
171 {
172         const ContentFeatures &f = nodemgr->get(*this);
173         if (f.param_type_2 == CPT2_WALLMOUNTED ||
174                         f.param_type_2 == CPT2_COLORED_WALLMOUNTED)
175                 return getParam2() & 0x07;
176         return 0;
177 }
178
179 v3s16 MapNode::getWallMountedDir(const NodeDefManager *nodemgr) const
180 {
181         switch(getWallMounted(nodemgr))
182         {
183         case 0: default: return v3s16(0,1,0);
184         case 1: return v3s16(0,-1,0);
185         case 2: return v3s16(1,0,0);
186         case 3: return v3s16(-1,0,0);
187         case 4: return v3s16(0,0,1);
188         case 5: return v3s16(0,0,-1);
189         }
190 }
191
192 void MapNode::rotateAlongYAxis(const NodeDefManager *nodemgr, Rotation rot)
193 {
194         ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
195
196         if (cpt2 == CPT2_FACEDIR || cpt2 == CPT2_COLORED_FACEDIR) {
197                 static const u8 rotate_facedir[24 * 4] = {
198                         // Table value = rotated facedir
199                         // Columns: 0, 90, 180, 270 degrees rotation around vertical axis
200                         // Rotation is anticlockwise as seen from above (+Y)
201
202                         0, 1, 2, 3,  // Initial facedir 0 to 3
203                         1, 2, 3, 0,
204                         2, 3, 0, 1,
205                         3, 0, 1, 2,
206
207                         4, 13, 10, 19,  // 4 to 7
208                         5, 14, 11, 16,
209                         6, 15, 8, 17,
210                         7, 12, 9, 18,
211
212                         8, 17, 6, 15,  // 8 to 11
213                         9, 18, 7, 12,
214                         10, 19, 4, 13,
215                         11, 16, 5, 14,
216
217                         12, 9, 18, 7,  // 12 to 15
218                         13, 10, 19, 4,
219                         14, 11, 16, 5,
220                         15, 8, 17, 6,
221
222                         16, 5, 14, 11,  // 16 to 19
223                         17, 6, 15, 8,
224                         18, 7, 12, 9,
225                         19, 4, 13, 10,
226
227                         20, 23, 22, 21,  // 20 to 23
228                         21, 20, 23, 22,
229                         22, 21, 20, 23,
230                         23, 22, 21, 20
231                 };
232                 u8 facedir = (param2 & 31) % 24;
233                 u8 index = facedir * 4 + rot;
234                 param2 &= ~31;
235                 param2 |= rotate_facedir[index];
236         } else if (cpt2 == CPT2_WALLMOUNTED ||
237                         cpt2 == CPT2_COLORED_WALLMOUNTED) {
238                 u8 wmountface = (param2 & 7);
239                 if (wmountface <= 1)
240                         return;
241
242                 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
243                 param2 &= ~7;
244                 param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
245         }
246 }
247
248 void transformNodeBox(const MapNode &n, const NodeBox &nodebox,
249         const NodeDefManager *nodemgr, std::vector<aabb3f> *p_boxes,
250         u8 neighbors = 0)
251 {
252         std::vector<aabb3f> &boxes = *p_boxes;
253
254         if (nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED) {
255                 const std::vector<aabb3f> &fixed = nodebox.fixed;
256                 int facedir = n.getFaceDir(nodemgr, true);
257                 u8 axisdir = facedir>>2;
258                 facedir&=0x03;
259                 for (aabb3f box : fixed) {
260                         if (nodebox.type == NODEBOX_LEVELED)
261                                 box.MaxEdge.Y = (-0.5f + n.getLevel(nodemgr) / 64.0f) * BS;
262
263                         switch (axisdir) {
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 (v3f &vertex : vertices) {
407                                 if(dir == v3s16(-1,0,0))
408                                         vertex.rotateXZBy(0);
409                                 if(dir == v3s16(1,0,0))
410                                         vertex.rotateXZBy(180);
411                                 if(dir == v3s16(0,0,-1))
412                                         vertex.rotateXZBy(90);
413                                 if(dir == v3s16(0,0,1))
414                                         vertex.rotateXZBy(-90);
415                         }
416
417                         aabb3f box = aabb3f(vertices[0]);
418                         box.addInternalPoint(vertices[1]);
419                         boxes.push_back(box);
420                 }
421         }
422         else if (nodebox.type == NODEBOX_CONNECTED)
423         {
424                 size_t boxes_size = boxes.size();
425                 boxes_size += nodebox.fixed.size();
426                 if (neighbors & 1)
427                         boxes_size += nodebox.connect_top.size();
428                 else
429                         boxes_size += nodebox.disconnected_top.size();
430
431                 if (neighbors & 2)
432                         boxes_size += nodebox.connect_bottom.size();
433                 else
434                         boxes_size += nodebox.disconnected_bottom.size();
435
436                 if (neighbors & 4)
437                         boxes_size += nodebox.connect_front.size();
438                 else
439                         boxes_size += nodebox.disconnected_front.size();
440
441                 if (neighbors & 8)
442                         boxes_size += nodebox.connect_left.size();
443                 else
444                         boxes_size += nodebox.disconnected_left.size();
445
446                 if (neighbors & 16)
447                         boxes_size += nodebox.connect_back.size();
448                 else
449                         boxes_size += nodebox.disconnected_back.size();
450
451                 if (neighbors & 32)
452                         boxes_size += nodebox.connect_right.size();
453                 else
454                         boxes_size += nodebox.disconnected_right.size();
455
456                 if (neighbors == 0)
457                         boxes_size += nodebox.disconnected.size();
458
459                 if (neighbors < 4)
460                         boxes_size += nodebox.disconnected_sides.size();
461
462                 boxes.reserve(boxes_size);
463
464 #define BOXESPUSHBACK(c) \
465                 for (std::vector<aabb3f>::const_iterator \
466                                 it = (c).begin(); \
467                                 it != (c).end(); ++it) \
468                         (boxes).push_back(*it);
469
470                 BOXESPUSHBACK(nodebox.fixed);
471
472                 if (neighbors & 1) {
473                         BOXESPUSHBACK(nodebox.connect_top);
474                 } else {
475                         BOXESPUSHBACK(nodebox.disconnected_top);
476                 }
477
478                 if (neighbors & 2) {
479                         BOXESPUSHBACK(nodebox.connect_bottom);
480                 } else {
481                         BOXESPUSHBACK(nodebox.disconnected_bottom);
482                 }
483
484                 if (neighbors & 4) {
485                         BOXESPUSHBACK(nodebox.connect_front);
486                 } else {
487                         BOXESPUSHBACK(nodebox.disconnected_front);
488                 }
489
490                 if (neighbors & 8) {
491                         BOXESPUSHBACK(nodebox.connect_left);
492                 } else {
493                         BOXESPUSHBACK(nodebox.disconnected_left);
494                 }
495
496                 if (neighbors & 16) {
497                         BOXESPUSHBACK(nodebox.connect_back);
498                 } else {
499                         BOXESPUSHBACK(nodebox.disconnected_back);
500                 }
501
502                 if (neighbors & 32) {
503                         BOXESPUSHBACK(nodebox.connect_right);
504                 } else {
505                         BOXESPUSHBACK(nodebox.disconnected_right);
506                 }
507
508                 if (neighbors == 0) {
509                         BOXESPUSHBACK(nodebox.disconnected);
510                 }
511
512                 if (neighbors < 4) {
513                         BOXESPUSHBACK(nodebox.disconnected_sides);
514                 }
515
516         }
517         else // NODEBOX_REGULAR
518         {
519                 boxes.emplace_back(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2);
520         }
521 }
522
523 static inline void getNeighborConnectingFace(
524         const v3s16 &p, const NodeDefManager *nodedef,
525         Map *map, MapNode n, u8 bitmask, u8 *neighbors)
526 {
527         MapNode n2 = map->getNodeNoEx(p);
528         if (nodedef->nodeboxConnects(n, n2, bitmask))
529                 *neighbors |= bitmask;
530 }
531
532 u8 MapNode::getNeighbors(v3s16 p, Map *map) const
533 {
534         const NodeDefManager *nodedef = map->getNodeDefManager();
535         u8 neighbors = 0;
536         const ContentFeatures &f = nodedef->get(*this);
537         // locate possible neighboring nodes to connect to
538         if (f.drawtype == NDT_NODEBOX && f.node_box.type == NODEBOX_CONNECTED) {
539                 v3s16 p2 = p;
540
541                 p2.Y++;
542                 getNeighborConnectingFace(p2, nodedef, map, *this, 1, &neighbors);
543
544                 p2 = p;
545                 p2.Y--;
546                 getNeighborConnectingFace(p2, nodedef, map, *this, 2, &neighbors);
547
548                 p2 = p;
549                 p2.Z--;
550                 getNeighborConnectingFace(p2, nodedef, map, *this, 4, &neighbors);
551
552                 p2 = p;
553                 p2.X--;
554                 getNeighborConnectingFace(p2, nodedef, map, *this, 8, &neighbors);
555
556                 p2 = p;
557                 p2.Z++;
558                 getNeighborConnectingFace(p2, nodedef, map, *this, 16, &neighbors);
559
560                 p2 = p;
561                 p2.X++;
562                 getNeighborConnectingFace(p2, nodedef, map, *this, 32, &neighbors);
563         }
564
565         return neighbors;
566 }
567
568 void MapNode::getNodeBoxes(const NodeDefManager *nodemgr,
569         std::vector<aabb3f> *boxes, u8 neighbors) const
570 {
571         const ContentFeatures &f = nodemgr->get(*this);
572         transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
573 }
574
575 void MapNode::getCollisionBoxes(const NodeDefManager *nodemgr,
576         std::vector<aabb3f> *boxes, u8 neighbors) const
577 {
578         const ContentFeatures &f = nodemgr->get(*this);
579         if (f.collision_box.fixed.empty())
580                 transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
581         else
582                 transformNodeBox(*this, f.collision_box, nodemgr, boxes, neighbors);
583 }
584
585 void MapNode::getSelectionBoxes(const NodeDefManager *nodemgr,
586         std::vector<aabb3f> *boxes, u8 neighbors) const
587 {
588         const ContentFeatures &f = nodemgr->get(*this);
589         transformNodeBox(*this, f.selection_box, nodemgr, boxes, neighbors);
590 }
591
592 u8 MapNode::getMaxLevel(const NodeDefManager *nodemgr) const
593 {
594         const ContentFeatures &f = nodemgr->get(*this);
595         // todo: after update in all games leave only if (f.param_type_2 ==
596         if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
597                 return LIQUID_LEVEL_MAX;
598         if(f.leveled || f.param_type_2 == CPT2_LEVELED)
599                 return LEVELED_MAX;
600         return 0;
601 }
602
603 u8 MapNode::getLevel(const NodeDefManager *nodemgr) const
604 {
605         const ContentFeatures &f = nodemgr->get(*this);
606         // todo: after update in all games leave only if (f.param_type_2 ==
607         if(f.liquid_type == LIQUID_SOURCE)
608                 return LIQUID_LEVEL_SOURCE;
609         if (f.param_type_2 == CPT2_FLOWINGLIQUID)
610                 return getParam2() & LIQUID_LEVEL_MASK;
611         if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
612                 return getParam2() & LIQUID_LEVEL_MASK;
613         if (f.param_type_2 == CPT2_LEVELED) {
614                 u8 level = getParam2() & LEVELED_MASK;
615                 if (level)
616                         return level;
617         }
618         if (f.leveled > LEVELED_MAX)
619                 return LEVELED_MAX;
620         return f.leveled;
621 }
622
623 u8 MapNode::setLevel(const NodeDefManager *nodemgr, s8 level)
624 {
625         u8 rest = 0;
626         const ContentFeatures &f = nodemgr->get(*this);
627         if (f.param_type_2 == CPT2_FLOWINGLIQUID
628                         || f.liquid_type == LIQUID_FLOWING
629                         || f.liquid_type == LIQUID_SOURCE) {
630                 if (level <= 0) { // liquid can’t exist with zero level
631                         setContent(CONTENT_AIR);
632                         return 0;
633                 }
634                 if (level >= LIQUID_LEVEL_SOURCE) {
635                         rest = level - LIQUID_LEVEL_SOURCE;
636                         setContent(nodemgr->getId(f.liquid_alternative_source));
637                         setParam2(0);
638                 } else {
639                         setContent(nodemgr->getId(f.liquid_alternative_flowing));
640                         setParam2((level & LIQUID_LEVEL_MASK) | (getParam2() & ~LIQUID_LEVEL_MASK));
641                 }
642         } else if (f.param_type_2 == CPT2_LEVELED) {
643                 if (level < 0) { // zero means default for a leveled nodebox
644                         rest = level;
645                         level = 0;
646                 } else if (level > LEVELED_MAX) {
647                         rest = level - LEVELED_MAX;
648                         level = LEVELED_MAX;
649                 }
650                 setParam2((level & LEVELED_MASK) | (getParam2() & ~LEVELED_MASK));
651         }
652         return rest;
653 }
654
655 u8 MapNode::addLevel(const NodeDefManager *nodemgr, s8 add)
656 {
657         s8 level = getLevel(nodemgr);
658         level += add;
659         return setLevel(nodemgr, level);
660 }
661
662 u32 MapNode::serializedLength(u8 version)
663 {
664         if(!ser_ver_supported(version))
665                 throw VersionMismatchException("ERROR: MapNode format not supported");
666
667         if (version == 0)
668                 return 1;
669
670         if (version <= 9)
671                 return 2;
672
673         if (version <= 23)
674                 return 3;
675
676         return 4;
677 }
678 void MapNode::serialize(u8 *dest, u8 version) const
679 {
680         if(!ser_ver_supported(version))
681                 throw VersionMismatchException("ERROR: MapNode format not supported");
682
683         // Can't do this anymore; we have 16-bit dynamically allocated node IDs
684         // in memory; conversion just won't work in this direction.
685         if(version < 24)
686                 throw SerializationError("MapNode::serialize: serialization to "
687                                 "version < 24 not possible");
688
689         writeU16(dest+0, param0);
690         writeU8(dest+2, param1);
691         writeU8(dest+3, param2);
692 }
693 void MapNode::deSerialize(u8 *source, u8 version)
694 {
695         if(!ser_ver_supported(version))
696                 throw VersionMismatchException("ERROR: MapNode format not supported");
697
698         if(version <= 21)
699         {
700                 deSerialize_pre22(source, version);
701                 return;
702         }
703
704         if(version >= 24){
705                 param0 = readU16(source+0);
706                 param1 = readU8(source+2);
707                 param2 = readU8(source+3);
708         }else{
709                 param0 = readU8(source+0);
710                 param1 = readU8(source+1);
711                 param2 = readU8(source+2);
712                 if(param0 > 0x7F){
713                         param0 |= ((param2&0xF0)<<4);
714                         param2 &= 0x0F;
715                 }
716         }
717 }
718 void MapNode::serializeBulk(std::ostream &os, int version,
719                 const MapNode *nodes, u32 nodecount,
720                 u8 content_width, u8 params_width, bool compressed)
721 {
722         if (!ser_ver_supported(version))
723                 throw VersionMismatchException("ERROR: MapNode format not supported");
724
725         sanity_check(content_width == 2);
726         sanity_check(params_width == 2);
727
728         // Can't do this anymore; we have 16-bit dynamically allocated node IDs
729         // in memory; conversion just won't work in this direction.
730         if (version < 24)
731                 throw SerializationError("MapNode::serializeBulk: serialization to "
732                                 "version < 24 not possible");
733
734         size_t databuf_size = nodecount * (content_width + params_width);
735         u8 *databuf = new u8[databuf_size];
736
737         u32 start1 = content_width * nodecount;
738         u32 start2 = (content_width + 1) * nodecount;
739
740         // Serialize content
741         for (u32 i = 0; i < nodecount; i++) {
742                 writeU16(&databuf[i * 2], nodes[i].param0);
743                 writeU8(&databuf[start1 + i], nodes[i].param1);
744                 writeU8(&databuf[start2 + i], nodes[i].param2);
745         }
746
747         /*
748                 Compress data to output stream
749         */
750
751         if (compressed)
752                 compressZlib(databuf, databuf_size, os);
753         else
754                 os.write((const char*) &databuf[0], databuf_size);
755
756         delete [] databuf;
757 }
758
759 // Deserialize bulk node data
760 void MapNode::deSerializeBulk(std::istream &is, int version,
761                 MapNode *nodes, u32 nodecount,
762                 u8 content_width, u8 params_width, bool compressed)
763 {
764         if(!ser_ver_supported(version))
765                 throw VersionMismatchException("ERROR: MapNode format not supported");
766
767         if (version < 22
768                         || (content_width != 1 && content_width != 2)
769                         || params_width != 2)
770                 FATAL_ERROR("Deserialize bulk node data error");
771
772         // Uncompress or read data
773         u32 len = nodecount * (content_width + params_width);
774         SharedBuffer<u8> databuf(len);
775         if(compressed)
776         {
777                 std::ostringstream os(std::ios_base::binary);
778                 decompressZlib(is, os);
779                 std::string s = os.str();
780                 if(s.size() != len)
781                         throw SerializationError("deSerializeBulkNodes: "
782                                         "decompress resulted in invalid size");
783                 memcpy(&databuf[0], s.c_str(), len);
784         }
785         else
786         {
787                 is.read((char*) &databuf[0], len);
788                 if(is.eof() || is.fail())
789                         throw SerializationError("deSerializeBulkNodes: "
790                                         "failed to read bulk node data");
791         }
792
793         // Deserialize content
794         if(content_width == 1)
795         {
796                 for(u32 i=0; i<nodecount; i++)
797                         nodes[i].param0 = readU8(&databuf[i]);
798         }
799         else if(content_width == 2)
800         {
801                 for(u32 i=0; i<nodecount; i++)
802                         nodes[i].param0 = readU16(&databuf[i*2]);
803         }
804
805         // Deserialize param1
806         u32 start1 = content_width * nodecount;
807         for(u32 i=0; i<nodecount; i++)
808                 nodes[i].param1 = readU8(&databuf[start1 + i]);
809
810         // Deserialize param2
811         u32 start2 = (content_width + 1) * nodecount;
812         if(content_width == 1)
813         {
814                 for(u32 i=0; i<nodecount; i++) {
815                         nodes[i].param2 = readU8(&databuf[start2 + i]);
816                         if(nodes[i].param0 > 0x7F){
817                                 nodes[i].param0 <<= 4;
818                                 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
819                                 nodes[i].param2 &= 0x0F;
820                         }
821                 }
822         }
823         else if(content_width == 2)
824         {
825                 for(u32 i=0; i<nodecount; i++)
826                         nodes[i].param2 = readU8(&databuf[start2 + i]);
827         }
828 }
829
830 /*
831         Legacy serialization
832 */
833 void MapNode::deSerialize_pre22(const u8 *source, u8 version)
834 {
835         if(version <= 1)
836         {
837                 param0 = source[0];
838         }
839         else if(version <= 9)
840         {
841                 param0 = source[0];
842                 param1 = source[1];
843         }
844         else
845         {
846                 param0 = source[0];
847                 param1 = source[1];
848                 param2 = source[2];
849                 if(param0 > 0x7f){
850                         param0 <<= 4;
851                         param0 |= (param2&0xf0)>>4;
852                         param2 &= 0x0f;
853                 }
854         }
855
856         // Convert special values from old version to new
857         if(version <= 19)
858         {
859                 // In these versions, CONTENT_IGNORE and CONTENT_AIR
860                 // are 255 and 254
861                 // Version 19 is fucked up with sometimes the old values and sometimes not
862                 if(param0 == 255)
863                         param0 = CONTENT_IGNORE;
864                 else if(param0 == 254)
865                         param0 = CONTENT_AIR;
866         }
867
868         // Translate to our known version
869         *this = mapnode_translate_to_internal(*this, version);
870 }