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