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