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