]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapnode.cpp
Add wallmounted support for plantlike and plantlike_rooted nodes (#11379)
[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 std::vector<aabb3f> &fixed = nodebox.fixed;
270                 int facedir = n.getFaceDir(nodemgr, true);
271                 u8 axisdir = facedir>>2;
272                 facedir&=0x03;
273                 for (aabb3f box : fixed) {
274                         if (nodebox.type == NODEBOX_LEVELED)
275                                 box.MaxEdge.Y = (-0.5f + n.getLevel(nodemgr) / 64.0f) * BS;
276
277                         switch (axisdir) {
278                         case 0:
279                                 if(facedir == 1)
280                                 {
281                                         box.MinEdge.rotateXZBy(-90);
282                                         box.MaxEdge.rotateXZBy(-90);
283                                 }
284                                 else if(facedir == 2)
285                                 {
286                                         box.MinEdge.rotateXZBy(180);
287                                         box.MaxEdge.rotateXZBy(180);
288                                 }
289                                 else if(facedir == 3)
290                                 {
291                                         box.MinEdge.rotateXZBy(90);
292                                         box.MaxEdge.rotateXZBy(90);
293                                 }
294                                 break;
295                         case 1: // z+
296                                 box.MinEdge.rotateYZBy(90);
297                                 box.MaxEdge.rotateYZBy(90);
298                                 if(facedir == 1)
299                                 {
300                                         box.MinEdge.rotateXYBy(90);
301                                         box.MaxEdge.rotateXYBy(90);
302                                 }
303                                 else if(facedir == 2)
304                                 {
305                                         box.MinEdge.rotateXYBy(180);
306                                         box.MaxEdge.rotateXYBy(180);
307                                 }
308                                 else if(facedir == 3)
309                                 {
310                                         box.MinEdge.rotateXYBy(-90);
311                                         box.MaxEdge.rotateXYBy(-90);
312                                 }
313                                 break;
314                         case 2: //z-
315                                 box.MinEdge.rotateYZBy(-90);
316                                 box.MaxEdge.rotateYZBy(-90);
317                                 if(facedir == 1)
318                                 {
319                                         box.MinEdge.rotateXYBy(-90);
320                                         box.MaxEdge.rotateXYBy(-90);
321                                 }
322                                 else if(facedir == 2)
323                                 {
324                                         box.MinEdge.rotateXYBy(180);
325                                         box.MaxEdge.rotateXYBy(180);
326                                 }
327                                 else if(facedir == 3)
328                                 {
329                                         box.MinEdge.rotateXYBy(90);
330                                         box.MaxEdge.rotateXYBy(90);
331                                 }
332                                 break;
333                         case 3:  //x+
334                                 box.MinEdge.rotateXYBy(-90);
335                                 box.MaxEdge.rotateXYBy(-90);
336                                 if(facedir == 1)
337                                 {
338                                         box.MinEdge.rotateYZBy(90);
339                                         box.MaxEdge.rotateYZBy(90);
340                                 }
341                                 else if(facedir == 2)
342                                 {
343                                         box.MinEdge.rotateYZBy(180);
344                                         box.MaxEdge.rotateYZBy(180);
345                                 }
346                                 else if(facedir == 3)
347                                 {
348                                         box.MinEdge.rotateYZBy(-90);
349                                         box.MaxEdge.rotateYZBy(-90);
350                                 }
351                                 break;
352                         case 4:  //x-
353                                 box.MinEdge.rotateXYBy(90);
354                                 box.MaxEdge.rotateXYBy(90);
355                                 if(facedir == 1)
356                                 {
357                                         box.MinEdge.rotateYZBy(-90);
358                                         box.MaxEdge.rotateYZBy(-90);
359                                 }
360                                 else if(facedir == 2)
361                                 {
362                                         box.MinEdge.rotateYZBy(180);
363                                         box.MaxEdge.rotateYZBy(180);
364                                 }
365                                 else if(facedir == 3)
366                                 {
367                                         box.MinEdge.rotateYZBy(90);
368                                         box.MaxEdge.rotateYZBy(90);
369                                 }
370                                 break;
371                         case 5:
372                                 box.MinEdge.rotateXYBy(-180);
373                                 box.MaxEdge.rotateXYBy(-180);
374                                 if(facedir == 1)
375                                 {
376                                         box.MinEdge.rotateXZBy(90);
377                                         box.MaxEdge.rotateXZBy(90);
378                                 }
379                                 else if(facedir == 2)
380                                 {
381                                         box.MinEdge.rotateXZBy(180);
382                                         box.MaxEdge.rotateXZBy(180);
383                                 }
384                                 else if(facedir == 3)
385                                 {
386                                         box.MinEdge.rotateXZBy(-90);
387                                         box.MaxEdge.rotateXZBy(-90);
388                                 }
389                                 break;
390                         default:
391                                 break;
392                         }
393                         box.repair();
394                         boxes.push_back(box);
395                 }
396         }
397         else if(nodebox.type == NODEBOX_WALLMOUNTED)
398         {
399                 v3s16 dir = n.getWallMountedDir(nodemgr);
400
401                 // top
402                 if(dir == v3s16(0,1,0))
403                 {
404                         boxes.push_back(nodebox.wall_top);
405                 }
406                 // bottom
407                 else if(dir == v3s16(0,-1,0))
408                 {
409                         boxes.push_back(nodebox.wall_bottom);
410                 }
411                 // side
412                 else
413                 {
414                         v3f vertices[2] =
415                         {
416                                 nodebox.wall_side.MinEdge,
417                                 nodebox.wall_side.MaxEdge
418                         };
419
420                         for (v3f &vertex : vertices) {
421                                 if(dir == v3s16(-1,0,0))
422                                         vertex.rotateXZBy(0);
423                                 if(dir == v3s16(1,0,0))
424                                         vertex.rotateXZBy(180);
425                                 if(dir == v3s16(0,0,-1))
426                                         vertex.rotateXZBy(90);
427                                 if(dir == v3s16(0,0,1))
428                                         vertex.rotateXZBy(-90);
429                         }
430
431                         aabb3f box = aabb3f(vertices[0]);
432                         box.addInternalPoint(vertices[1]);
433                         boxes.push_back(box);
434                 }
435         }
436         else if (nodebox.type == NODEBOX_CONNECTED)
437         {
438                 size_t boxes_size = boxes.size();
439                 boxes_size += nodebox.fixed.size();
440                 if (neighbors & 1)
441                         boxes_size += nodebox.connect_top.size();
442                 else
443                         boxes_size += nodebox.disconnected_top.size();
444
445                 if (neighbors & 2)
446                         boxes_size += nodebox.connect_bottom.size();
447                 else
448                         boxes_size += nodebox.disconnected_bottom.size();
449
450                 if (neighbors & 4)
451                         boxes_size += nodebox.connect_front.size();
452                 else
453                         boxes_size += nodebox.disconnected_front.size();
454
455                 if (neighbors & 8)
456                         boxes_size += nodebox.connect_left.size();
457                 else
458                         boxes_size += nodebox.disconnected_left.size();
459
460                 if (neighbors & 16)
461                         boxes_size += nodebox.connect_back.size();
462                 else
463                         boxes_size += nodebox.disconnected_back.size();
464
465                 if (neighbors & 32)
466                         boxes_size += nodebox.connect_right.size();
467                 else
468                         boxes_size += nodebox.disconnected_right.size();
469
470                 if (neighbors == 0)
471                         boxes_size += nodebox.disconnected.size();
472
473                 if (neighbors < 4)
474                         boxes_size += nodebox.disconnected_sides.size();
475
476                 boxes.reserve(boxes_size);
477
478 #define BOXESPUSHBACK(c) \
479                 for (std::vector<aabb3f>::const_iterator \
480                                 it = (c).begin(); \
481                                 it != (c).end(); ++it) \
482                         (boxes).push_back(*it);
483
484                 BOXESPUSHBACK(nodebox.fixed);
485
486                 if (neighbors & 1) {
487                         BOXESPUSHBACK(nodebox.connect_top);
488                 } else {
489                         BOXESPUSHBACK(nodebox.disconnected_top);
490                 }
491
492                 if (neighbors & 2) {
493                         BOXESPUSHBACK(nodebox.connect_bottom);
494                 } else {
495                         BOXESPUSHBACK(nodebox.disconnected_bottom);
496                 }
497
498                 if (neighbors & 4) {
499                         BOXESPUSHBACK(nodebox.connect_front);
500                 } else {
501                         BOXESPUSHBACK(nodebox.disconnected_front);
502                 }
503
504                 if (neighbors & 8) {
505                         BOXESPUSHBACK(nodebox.connect_left);
506                 } else {
507                         BOXESPUSHBACK(nodebox.disconnected_left);
508                 }
509
510                 if (neighbors & 16) {
511                         BOXESPUSHBACK(nodebox.connect_back);
512                 } else {
513                         BOXESPUSHBACK(nodebox.disconnected_back);
514                 }
515
516                 if (neighbors & 32) {
517                         BOXESPUSHBACK(nodebox.connect_right);
518                 } else {
519                         BOXESPUSHBACK(nodebox.disconnected_right);
520                 }
521
522                 if (neighbors == 0) {
523                         BOXESPUSHBACK(nodebox.disconnected);
524                 }
525
526                 if (neighbors < 4) {
527                         BOXESPUSHBACK(nodebox.disconnected_sides);
528                 }
529
530         }
531         else // NODEBOX_REGULAR
532         {
533                 boxes.emplace_back(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2);
534         }
535 }
536
537 static inline void getNeighborConnectingFace(
538         const v3s16 &p, const NodeDefManager *nodedef,
539         Map *map, MapNode n, u8 bitmask, u8 *neighbors)
540 {
541         MapNode n2 = map->getNode(p);
542         if (nodedef->nodeboxConnects(n, n2, bitmask))
543                 *neighbors |= bitmask;
544 }
545
546 u8 MapNode::getNeighbors(v3s16 p, Map *map) const
547 {
548         const NodeDefManager *nodedef = map->getNodeDefManager();
549         u8 neighbors = 0;
550         const ContentFeatures &f = nodedef->get(*this);
551         // locate possible neighboring nodes to connect to
552         if (f.drawtype == NDT_NODEBOX && f.node_box.type == NODEBOX_CONNECTED) {
553                 v3s16 p2 = p;
554
555                 p2.Y++;
556                 getNeighborConnectingFace(p2, nodedef, map, *this, 1, &neighbors);
557
558                 p2 = p;
559                 p2.Y--;
560                 getNeighborConnectingFace(p2, nodedef, map, *this, 2, &neighbors);
561
562                 p2 = p;
563                 p2.Z--;
564                 getNeighborConnectingFace(p2, nodedef, map, *this, 4, &neighbors);
565
566                 p2 = p;
567                 p2.X--;
568                 getNeighborConnectingFace(p2, nodedef, map, *this, 8, &neighbors);
569
570                 p2 = p;
571                 p2.Z++;
572                 getNeighborConnectingFace(p2, nodedef, map, *this, 16, &neighbors);
573
574                 p2 = p;
575                 p2.X++;
576                 getNeighborConnectingFace(p2, nodedef, map, *this, 32, &neighbors);
577         }
578
579         return neighbors;
580 }
581
582 void MapNode::getNodeBoxes(const NodeDefManager *nodemgr,
583         std::vector<aabb3f> *boxes, u8 neighbors) const
584 {
585         const ContentFeatures &f = nodemgr->get(*this);
586         transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
587 }
588
589 void MapNode::getCollisionBoxes(const NodeDefManager *nodemgr,
590         std::vector<aabb3f> *boxes, u8 neighbors) const
591 {
592         const ContentFeatures &f = nodemgr->get(*this);
593         if (f.collision_box.fixed.empty())
594                 transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
595         else
596                 transformNodeBox(*this, f.collision_box, nodemgr, boxes, neighbors);
597 }
598
599 void MapNode::getSelectionBoxes(const NodeDefManager *nodemgr,
600         std::vector<aabb3f> *boxes, u8 neighbors) const
601 {
602         const ContentFeatures &f = nodemgr->get(*this);
603         transformNodeBox(*this, f.selection_box, nodemgr, boxes, neighbors);
604 }
605
606 u8 MapNode::getMaxLevel(const NodeDefManager *nodemgr) const
607 {
608         const ContentFeatures &f = nodemgr->get(*this);
609         // todo: after update in all games leave only if (f.param_type_2 ==
610         if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
611                 return LIQUID_LEVEL_MAX;
612         if(f.leveled || f.param_type_2 == CPT2_LEVELED)
613                 return f.leveled_max;
614         return 0;
615 }
616
617 u8 MapNode::getLevel(const NodeDefManager *nodemgr) const
618 {
619         const ContentFeatures &f = nodemgr->get(*this);
620         // todo: after update in all games leave only if (f.param_type_2 ==
621         if(f.liquid_type == LIQUID_SOURCE)
622                 return LIQUID_LEVEL_SOURCE;
623         if (f.param_type_2 == CPT2_FLOWINGLIQUID)
624                 return getParam2() & LIQUID_LEVEL_MASK;
625         if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
626                 return getParam2() & LIQUID_LEVEL_MASK;
627         if (f.param_type_2 == CPT2_LEVELED) {
628                 u8 level = getParam2() & LEVELED_MASK;
629                 if (level)
630                         return level;
631         }
632         // Return static value from nodedef if param2 isn't used for level
633         if (f.leveled > f.leveled_max)
634                 return f.leveled_max;
635         return f.leveled;
636 }
637
638 s8 MapNode::setLevel(const NodeDefManager *nodemgr, s16 level)
639 {
640         s8 rest = 0;
641         const ContentFeatures &f = nodemgr->get(*this);
642         if (f.param_type_2 == CPT2_FLOWINGLIQUID
643                         || f.liquid_type == LIQUID_FLOWING
644                         || f.liquid_type == LIQUID_SOURCE) {
645                 if (level <= 0) { // liquid can’t exist with zero level
646                         setContent(CONTENT_AIR);
647                         return 0;
648                 }
649                 if (level >= LIQUID_LEVEL_SOURCE) {
650                         rest = level - LIQUID_LEVEL_SOURCE;
651                         setContent(f.liquid_alternative_source_id);
652                         setParam2(0);
653                 } else {
654                         setContent(f.liquid_alternative_flowing_id);
655                         setParam2((level & LIQUID_LEVEL_MASK) | (getParam2() & ~LIQUID_LEVEL_MASK));
656                 }
657         } else if (f.param_type_2 == CPT2_LEVELED) {
658                 if (level < 0) { // zero means default for a leveled nodebox
659                         rest = level;
660                         level = 0;
661                 } else if (level > f.leveled_max) {
662                         rest = level - f.leveled_max;
663                         level = f.leveled_max;
664                 }
665                 setParam2((level & LEVELED_MASK) | (getParam2() & ~LEVELED_MASK));
666         }
667         return rest;
668 }
669
670 s8 MapNode::addLevel(const NodeDefManager *nodemgr, s16 add)
671 {
672         s16 level = getLevel(nodemgr);
673         level += add;
674         return setLevel(nodemgr, level);
675 }
676
677 u32 MapNode::serializedLength(u8 version)
678 {
679         if(!ser_ver_supported(version))
680                 throw VersionMismatchException("ERROR: MapNode format not supported");
681
682         if (version == 0)
683                 return 1;
684
685         if (version <= 9)
686                 return 2;
687
688         if (version <= 23)
689                 return 3;
690
691         return 4;
692 }
693 void MapNode::serialize(u8 *dest, u8 version) const
694 {
695         if(!ser_ver_supported(version))
696                 throw VersionMismatchException("ERROR: MapNode format not supported");
697
698         // Can't do this anymore; we have 16-bit dynamically allocated node IDs
699         // in memory; conversion just won't work in this direction.
700         if(version < 24)
701                 throw SerializationError("MapNode::serialize: serialization to "
702                                 "version < 24 not possible");
703
704         writeU16(dest+0, param0);
705         writeU8(dest+2, param1);
706         writeU8(dest+3, param2);
707 }
708 void MapNode::deSerialize(u8 *source, u8 version)
709 {
710         if(!ser_ver_supported(version))
711                 throw VersionMismatchException("ERROR: MapNode format not supported");
712
713         if(version <= 21)
714         {
715                 deSerialize_pre22(source, version);
716                 return;
717         }
718
719         if(version >= 24){
720                 param0 = readU16(source+0);
721                 param1 = readU8(source+2);
722                 param2 = readU8(source+3);
723         }else{
724                 param0 = readU8(source+0);
725                 param1 = readU8(source+1);
726                 param2 = readU8(source+2);
727                 if(param0 > 0x7F){
728                         param0 |= ((param2&0xF0)<<4);
729                         param2 &= 0x0F;
730                 }
731         }
732 }
733 void MapNode::serializeBulk(std::ostream &os, int version,
734                 const MapNode *nodes, u32 nodecount,
735                 u8 content_width, u8 params_width, int compression_level)
736 {
737         if (!ser_ver_supported(version))
738                 throw VersionMismatchException("ERROR: MapNode format not supported");
739
740         sanity_check(content_width == 2);
741         sanity_check(params_width == 2);
742
743         // Can't do this anymore; we have 16-bit dynamically allocated node IDs
744         // in memory; conversion just won't work in this direction.
745         if (version < 24)
746                 throw SerializationError("MapNode::serializeBulk: serialization to "
747                                 "version < 24 not possible");
748
749         size_t databuf_size = nodecount * (content_width + params_width);
750         u8 *databuf = new u8[databuf_size];
751
752         u32 start1 = content_width * nodecount;
753         u32 start2 = (content_width + 1) * nodecount;
754
755         // Serialize content
756         for (u32 i = 0; i < nodecount; i++) {
757                 writeU16(&databuf[i * 2], nodes[i].param0);
758                 writeU8(&databuf[start1 + i], nodes[i].param1);
759                 writeU8(&databuf[start2 + i], nodes[i].param2);
760         }
761
762         /*
763                 Compress data to output stream
764         */
765
766         compressZlib(databuf, databuf_size, os, compression_level);
767
768         delete [] databuf;
769 }
770
771 // Deserialize bulk node data
772 void MapNode::deSerializeBulk(std::istream &is, int version,
773                 MapNode *nodes, u32 nodecount,
774                 u8 content_width, u8 params_width)
775 {
776         if(!ser_ver_supported(version))
777                 throw VersionMismatchException("ERROR: MapNode format not supported");
778
779         if (version < 22
780                         || (content_width != 1 && content_width != 2)
781                         || params_width != 2)
782                 FATAL_ERROR("Deserialize bulk node data error");
783
784         // Uncompress or read data
785         u32 len = nodecount * (content_width + params_width);
786         std::ostringstream os(std::ios_base::binary);
787         decompressZlib(is, os);
788         std::string s = os.str();
789         if(s.size() != len)
790                 throw SerializationError("deSerializeBulkNodes: "
791                                 "decompress resulted in invalid size");
792         const u8 *databuf = reinterpret_cast<const u8*>(s.c_str());
793
794         // Deserialize content
795         if(content_width == 1)
796         {
797                 for(u32 i=0; i<nodecount; i++)
798                         nodes[i].param0 = readU8(&databuf[i]);
799         }
800         else if(content_width == 2)
801         {
802                 for(u32 i=0; i<nodecount; i++)
803                         nodes[i].param0 = readU16(&databuf[i*2]);
804         }
805
806         // Deserialize param1
807         u32 start1 = content_width * nodecount;
808         for(u32 i=0; i<nodecount; i++)
809                 nodes[i].param1 = readU8(&databuf[start1 + i]);
810
811         // Deserialize param2
812         u32 start2 = (content_width + 1) * nodecount;
813         if(content_width == 1)
814         {
815                 for(u32 i=0; i<nodecount; i++) {
816                         nodes[i].param2 = readU8(&databuf[start2 + i]);
817                         if(nodes[i].param0 > 0x7F){
818                                 nodes[i].param0 <<= 4;
819                                 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
820                                 nodes[i].param2 &= 0x0F;
821                         }
822                 }
823         }
824         else if(content_width == 2)
825         {
826                 for(u32 i=0; i<nodecount; i++)
827                         nodes[i].param2 = readU8(&databuf[start2 + i]);
828         }
829 }
830
831 /*
832         Legacy serialization
833 */
834 void MapNode::deSerialize_pre22(const u8 *source, u8 version)
835 {
836         if(version <= 1)
837         {
838                 param0 = source[0];
839         }
840         else if(version <= 9)
841         {
842                 param0 = source[0];
843                 param1 = source[1];
844         }
845         else
846         {
847                 param0 = source[0];
848                 param1 = source[1];
849                 param2 = source[2];
850                 if(param0 > 0x7f){
851                         param0 <<= 4;
852                         param0 |= (param2&0xf0)>>4;
853                         param2 &= 0x0f;
854                 }
855         }
856
857         // Convert special values from old version to new
858         if(version <= 19)
859         {
860                 // In these versions, CONTENT_IGNORE and CONTENT_AIR
861                 // are 255 and 254
862                 // Version 19 is messed up with sometimes the old values and sometimes not
863                 if(param0 == 255)
864                         param0 = CONTENT_IGNORE;
865                 else if(param0 == 254)
866                         param0 = CONTENT_AIR;
867         }
868
869         // Translate to our known version
870         *this = mapnode_translate_to_internal(*this, version);
871 }