]> git.lizzy.rs Git - minetest.git/blob - src/mapnode.cpp
Fix a crash (assert) when client set serial version < 24 in INIT
[minetest.git] / src / mapnode.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "irrlichttypes_extrabloated.h"
21 #include "mapnode.h"
22 #include "porting.h"
23 #include "main.h" // For g_settings
24 #include "nodedef.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::setLight(enum LightBank bank, u8 a_light, INodeDefManager *nodemgr)
59 {
60         // If node doesn't contain light data, ignore this
61         if(nodemgr->get(*this).param_type != CPT_LIGHT)
62                 return;
63         if(bank == LIGHTBANK_DAY)
64         {
65                 param1 &= 0xf0;
66                 param1 |= a_light & 0x0f;
67         }
68         else if(bank == LIGHTBANK_NIGHT)
69         {
70                 param1 &= 0x0f;
71                 param1 |= (a_light & 0x0f)<<4;
72         }
73         else
74                 assert(0);
75 }
76
77 u8 MapNode::getLight(enum LightBank bank, INodeDefManager *nodemgr) const
78 {
79         // Select the brightest of [light source, propagated light]
80         const ContentFeatures &f = nodemgr->get(*this);
81
82         u8 light;
83         if(f.param_type == CPT_LIGHT)
84                 light = bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f;
85         else
86                 light = 0;
87
88         return MYMAX(f.light_source, light);
89 }
90
91 u8 MapNode::getLightNoChecks(enum LightBank bank, const ContentFeatures *f)
92 {
93         return MYMAX(f->light_source,
94                      bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f);
95 }
96
97 bool MapNode::getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const
98 {
99         // Select the brightest of [light source, propagated light]
100         const ContentFeatures &f = nodemgr->get(*this);
101         if(f.param_type == CPT_LIGHT)
102         {
103                 lightday = param1 & 0x0f;
104                 lightnight = (param1>>4)&0x0f;
105         }
106         else
107         {
108                 lightday = 0;
109                 lightnight = 0;
110         }
111         if(f.light_source > lightday)
112                 lightday = f.light_source;
113         if(f.light_source > lightnight)
114                 lightnight = f.light_source;
115         return f.param_type == CPT_LIGHT || f.light_source != 0;
116 }
117
118 u8 MapNode::getFaceDir(INodeDefManager *nodemgr) const
119 {
120         const ContentFeatures &f = nodemgr->get(*this);
121         if(f.param_type_2 == CPT2_FACEDIR)
122                 return (getParam2() & 0x1F) % 24;
123         return 0;
124 }
125
126 u8 MapNode::getWallMounted(INodeDefManager *nodemgr) const
127 {
128         const ContentFeatures &f = nodemgr->get(*this);
129         if(f.param_type_2 == CPT2_WALLMOUNTED)
130                 return getParam2() & 0x07;
131         return 0;
132 }
133
134 v3s16 MapNode::getWallMountedDir(INodeDefManager *nodemgr) const
135 {
136         switch(getWallMounted(nodemgr))
137         {
138         case 0: default: return v3s16(0,1,0);
139         case 1: return v3s16(0,-1,0);
140         case 2: return v3s16(1,0,0);
141         case 3: return v3s16(-1,0,0);
142         case 4: return v3s16(0,0,1);
143         case 5: return v3s16(0,0,-1);
144         }
145 }
146
147 void MapNode::rotateAlongYAxis(INodeDefManager *nodemgr, Rotation rot) {
148         ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
149
150         if (cpt2 == CPT2_FACEDIR) {
151                 u8 newrot = param2 & 3;
152                 param2 &= ~3;
153                 param2 |= (newrot + rot) & 3;
154         } else if (cpt2 == CPT2_WALLMOUNTED) {
155                 u8 wmountface = (param2 & 7);
156                 if (wmountface <= 1)
157                         return;
158
159                 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
160                 param2 &= ~7;
161                 param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
162         }
163 }
164
165 static std::vector<aabb3f> transformNodeBox(const MapNode &n,
166                 const NodeBox &nodebox, INodeDefManager *nodemgr)
167 {
168         std::vector<aabb3f> boxes;
169         if(nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED)
170         {
171                 const std::vector<aabb3f> &fixed = nodebox.fixed;
172                 int facedir = n.getFaceDir(nodemgr);
173                 u8 axisdir = facedir>>2;
174                 facedir&=0x03;
175                 for(std::vector<aabb3f>::const_iterator
176                                 i = fixed.begin();
177                                 i != fixed.end(); i++)
178                 {
179                         aabb3f box = *i;
180
181                         if (nodebox.type == NODEBOX_LEVELED) {
182                                 box.MaxEdge.Y = -BS/2 + BS*((float)1/LEVELED_MAX) * n.getLevel(nodemgr);
183                         }
184
185                         switch (axisdir)
186                         {
187                         case 0:
188                                 if(facedir == 1)
189                                 {
190                                         box.MinEdge.rotateXZBy(-90);
191                                         box.MaxEdge.rotateXZBy(-90);
192                                 }
193                                 else if(facedir == 2)
194                                 {
195                                         box.MinEdge.rotateXZBy(180);
196                                         box.MaxEdge.rotateXZBy(180);
197                                 }
198                                 else if(facedir == 3)
199                                 {
200                                         box.MinEdge.rotateXZBy(90);
201                                         box.MaxEdge.rotateXZBy(90);
202                                 }
203                                 break;
204                         case 1: // z+
205                                 box.MinEdge.rotateYZBy(90);
206                                 box.MaxEdge.rotateYZBy(90);
207                                 if(facedir == 1)
208                                 {
209                                         box.MinEdge.rotateXYBy(90);
210                                         box.MaxEdge.rotateXYBy(90);
211                                 }
212                                 else if(facedir == 2)
213                                 {
214                                         box.MinEdge.rotateXYBy(180);
215                                         box.MaxEdge.rotateXYBy(180);
216                                 }
217                                 else if(facedir == 3)
218                                 {
219                                         box.MinEdge.rotateXYBy(-90);
220                                         box.MaxEdge.rotateXYBy(-90);
221                                 }
222                                 break;
223                         case 2: //z-
224                                 box.MinEdge.rotateYZBy(-90);
225                                 box.MaxEdge.rotateYZBy(-90);
226                                 if(facedir == 1)
227                                 {
228                                         box.MinEdge.rotateXYBy(-90);
229                                         box.MaxEdge.rotateXYBy(-90);
230                                 }
231                                 else if(facedir == 2)
232                                 {
233                                         box.MinEdge.rotateXYBy(180);
234                                         box.MaxEdge.rotateXYBy(180);
235                                 }
236                                 else if(facedir == 3)
237                                 {
238                                         box.MinEdge.rotateXYBy(90);
239                                         box.MaxEdge.rotateXYBy(90);
240                                 }
241                                 break;
242                         case 3:  //x+
243                                 box.MinEdge.rotateXYBy(-90);
244                                 box.MaxEdge.rotateXYBy(-90);
245                                 if(facedir == 1)
246                                 {
247                                         box.MinEdge.rotateYZBy(90);
248                                         box.MaxEdge.rotateYZBy(90);
249                                 }
250                                 else if(facedir == 2)
251                                 {
252                                         box.MinEdge.rotateYZBy(180);
253                                         box.MaxEdge.rotateYZBy(180);
254                                 }
255                                 else if(facedir == 3)
256                                 {
257                                         box.MinEdge.rotateYZBy(-90);
258                                         box.MaxEdge.rotateYZBy(-90);
259                                 }
260                                 break;
261                         case 4:  //x-
262                                 box.MinEdge.rotateXYBy(90);
263                                 box.MaxEdge.rotateXYBy(90);
264                                 if(facedir == 1)
265                                 {
266                                         box.MinEdge.rotateYZBy(-90);
267                                         box.MaxEdge.rotateYZBy(-90);
268                                 }
269                                 else if(facedir == 2)
270                                 {
271                                         box.MinEdge.rotateYZBy(180);
272                                         box.MaxEdge.rotateYZBy(180);
273                                 }
274                                 else if(facedir == 3)
275                                 {
276                                         box.MinEdge.rotateYZBy(90);
277                                         box.MaxEdge.rotateYZBy(90);
278                                 }
279                                 break;
280                         case 5:
281                                 box.MinEdge.rotateXYBy(-180);
282                                 box.MaxEdge.rotateXYBy(-180);
283                                 if(facedir == 1)
284                                 {
285                                         box.MinEdge.rotateXZBy(90);
286                                         box.MaxEdge.rotateXZBy(90);
287                                 }
288                                 else if(facedir == 2)
289                                 {
290                                         box.MinEdge.rotateXZBy(180);
291                                         box.MaxEdge.rotateXZBy(180);
292                                 }
293                                 else if(facedir == 3)
294                                 {
295                                         box.MinEdge.rotateXZBy(-90);
296                                         box.MaxEdge.rotateXZBy(-90);
297                                 }
298                                 break;
299                         default:
300                                 break;
301                         }
302                         box.repair();
303                         boxes.push_back(box);
304                 }
305         }
306         else if(nodebox.type == NODEBOX_WALLMOUNTED)
307         {
308                 v3s16 dir = n.getWallMountedDir(nodemgr);
309
310                 // top
311                 if(dir == v3s16(0,1,0))
312                 {
313                         boxes.push_back(nodebox.wall_top);
314                 }
315                 // bottom
316                 else if(dir == v3s16(0,-1,0))
317                 {
318                         boxes.push_back(nodebox.wall_bottom);
319                 }
320                 // side
321                 else
322                 {
323                         v3f vertices[2] =
324                         {
325                                 nodebox.wall_side.MinEdge,
326                                 nodebox.wall_side.MaxEdge
327                         };
328
329                         for(s32 i=0; i<2; i++)
330                         {
331                                 if(dir == v3s16(-1,0,0))
332                                         vertices[i].rotateXZBy(0);
333                                 if(dir == v3s16(1,0,0))
334                                         vertices[i].rotateXZBy(180);
335                                 if(dir == v3s16(0,0,-1))
336                                         vertices[i].rotateXZBy(90);
337                                 if(dir == v3s16(0,0,1))
338                                         vertices[i].rotateXZBy(-90);
339                         }
340
341                         aabb3f box = aabb3f(vertices[0]);
342                         box.addInternalPoint(vertices[1]);
343                         boxes.push_back(box);
344                 }
345         }
346         else // NODEBOX_REGULAR
347         {
348                 boxes.push_back(aabb3f(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2));
349         }
350         return boxes;
351 }
352
353 std::vector<aabb3f> MapNode::getNodeBoxes(INodeDefManager *nodemgr) const
354 {
355         const ContentFeatures &f = nodemgr->get(*this);
356         return transformNodeBox(*this, f.node_box, nodemgr);
357 }
358
359 std::vector<aabb3f> MapNode::getCollisionBoxes(INodeDefManager *nodemgr) const
360 {
361         const ContentFeatures &f = nodemgr->get(*this);
362         if (f.collision_box.fixed.empty())
363                 return transformNodeBox(*this, f.node_box, nodemgr);
364         else
365                 return transformNodeBox(*this, f.collision_box, nodemgr);
366 }
367
368 std::vector<aabb3f> MapNode::getSelectionBoxes(INodeDefManager *nodemgr) const
369 {
370         const ContentFeatures &f = nodemgr->get(*this);
371         return transformNodeBox(*this, f.selection_box, nodemgr);
372 }
373
374 u8 MapNode::getMaxLevel(INodeDefManager *nodemgr) const
375 {
376         const ContentFeatures &f = nodemgr->get(*this);
377         // todo: after update in all games leave only if (f.param_type_2 ==
378         if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
379                 return LIQUID_LEVEL_MAX;
380         if(f.leveled || f.param_type_2 == CPT2_LEVELED)
381                 return LEVELED_MAX;
382         return 0;
383 }
384
385 u8 MapNode::getLevel(INodeDefManager *nodemgr) const
386 {
387         const ContentFeatures &f = nodemgr->get(*this);
388         // todo: after update in all games leave only if (f.param_type_2 ==
389         if(f.liquid_type == LIQUID_SOURCE)
390                 return LIQUID_LEVEL_SOURCE;
391         if (f.param_type_2 == CPT2_FLOWINGLIQUID)
392                 return getParam2() & LIQUID_LEVEL_MASK;
393         if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
394                 return getParam2() & LIQUID_LEVEL_MASK;
395         if(f.leveled || f.param_type_2 == CPT2_LEVELED) {
396                  u8 level = getParam2() & LEVELED_MASK;
397                  if(level)
398                         return level;
399                  if(f.leveled > LEVELED_MAX)
400                         return LEVELED_MAX;
401                  return f.leveled; //default
402         }
403         return 0;
404 }
405
406 u8 MapNode::setLevel(INodeDefManager *nodemgr, s8 level)
407 {
408         u8 rest = 0;
409         if (level < 1) {
410                 setContent(CONTENT_AIR);
411                 return 0;
412         }
413         const ContentFeatures &f = nodemgr->get(*this);
414         if (f.param_type_2 == CPT2_FLOWINGLIQUID
415                 || f.liquid_type == LIQUID_FLOWING
416                 || f.liquid_type == LIQUID_SOURCE) {
417                 if (level >= LIQUID_LEVEL_SOURCE) {
418                         rest = level - LIQUID_LEVEL_SOURCE;
419                         setContent(nodemgr->getId(f.liquid_alternative_source));
420                 } else {
421                         setContent(nodemgr->getId(f.liquid_alternative_flowing));
422                         setParam2(level & LIQUID_LEVEL_MASK);
423                 }
424         } else if (f.leveled || f.param_type_2 == CPT2_LEVELED) {
425                 if (level > LEVELED_MAX) {
426                         rest = level - LEVELED_MAX;
427                         level = LEVELED_MAX;
428                 }
429                 setParam2(level & LEVELED_MASK);
430         }
431         return rest;
432 }
433
434 u8 MapNode::addLevel(INodeDefManager *nodemgr, s8 add)
435 {
436         s8 level = getLevel(nodemgr);
437         if (add == 0) level = 1;
438         level += add;
439         return setLevel(nodemgr, level);
440 }
441
442 u32 MapNode::serializedLength(u8 version)
443 {
444         if(!ser_ver_supported(version))
445                 throw VersionMismatchException("ERROR: MapNode format not supported");
446
447         if(version == 0)
448                 return 1;
449         else if(version <= 9)
450                 return 2;
451         else if(version <= 23)
452                 return 3;
453         else
454                 return 4;
455 }
456 void MapNode::serialize(u8 *dest, u8 version)
457 {
458         if(!ser_ver_supported(version))
459                 throw VersionMismatchException("ERROR: MapNode format not supported");
460
461         // Can't do this anymore; we have 16-bit dynamically allocated node IDs
462         // in memory; conversion just won't work in this direction.
463         if(version < 24)
464                 throw SerializationError("MapNode::serialize: serialization to "
465                                 "version < 24 not possible");
466
467         writeU16(dest+0, param0);
468         writeU8(dest+2, param1);
469         writeU8(dest+3, param2);
470 }
471 void MapNode::deSerialize(u8 *source, u8 version)
472 {
473         if(!ser_ver_supported(version))
474                 throw VersionMismatchException("ERROR: MapNode format not supported");
475
476         if(version <= 21)
477         {
478                 deSerialize_pre22(source, version);
479                 return;
480         }
481
482         if(version >= 24){
483                 param0 = readU16(source+0);
484                 param1 = readU8(source+2);
485                 param2 = readU8(source+3);
486         }else{
487                 param0 = readU8(source+0);
488                 param1 = readU8(source+1);
489                 param2 = readU8(source+2);
490                 if(param0 > 0x7F){
491                         param0 |= ((param2&0xF0)<<4);
492                         param2 &= 0x0F;
493                 }
494         }
495 }
496 void MapNode::serializeBulk(std::ostream &os, int version,
497                 const MapNode *nodes, u32 nodecount,
498                 u8 content_width, u8 params_width, bool compressed)
499 {
500         if(!ser_ver_supported(version))
501                 throw VersionMismatchException("ERROR: MapNode format not supported");
502
503         assert(content_width == 2);
504         assert(params_width == 2);
505
506         // Can't do this anymore; we have 16-bit dynamically allocated node IDs
507         // in memory; conversion just won't work in this direction.
508         if(version < 24)
509                 throw SerializationError("MapNode::serializeBulk: serialization to "
510                                 "version < 24 not possible");
511
512         SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
513
514         // Serialize content
515         for(u32 i=0; i<nodecount; i++)
516                 writeU16(&databuf[i*2], nodes[i].param0);
517
518         // Serialize param1
519         u32 start1 = content_width * nodecount;
520         for(u32 i=0; i<nodecount; i++)
521                 writeU8(&databuf[start1 + i], nodes[i].param1);
522
523         // Serialize param2
524         u32 start2 = (content_width + 1) * nodecount;
525         for(u32 i=0; i<nodecount; i++)
526                 writeU8(&databuf[start2 + i], nodes[i].param2);
527
528         /*
529                 Compress data to output stream
530         */
531
532         if(compressed)
533         {
534                 compressZlib(databuf, os);
535         }
536         else
537         {
538                 os.write((const char*) &databuf[0], databuf.getSize());
539         }
540 }
541
542 // Deserialize bulk node data
543 void MapNode::deSerializeBulk(std::istream &is, int version,
544                 MapNode *nodes, u32 nodecount,
545                 u8 content_width, u8 params_width, bool compressed)
546 {
547         if(!ser_ver_supported(version))
548                 throw VersionMismatchException("ERROR: MapNode format not supported");
549
550         assert(version >= 22);
551         assert(content_width == 1 || content_width == 2);
552         assert(params_width == 2);
553
554         // Uncompress or read data
555         u32 len = nodecount * (content_width + params_width);
556         SharedBuffer<u8> databuf(len);
557         if(compressed)
558         {
559                 std::ostringstream os(std::ios_base::binary);
560                 decompressZlib(is, os);
561                 std::string s = os.str();
562                 if(s.size() != len)
563                         throw SerializationError("deSerializeBulkNodes: "
564                                         "decompress resulted in invalid size");
565                 memcpy(&databuf[0], s.c_str(), len);
566         }
567         else
568         {
569                 is.read((char*) &databuf[0], len);
570                 if(is.eof() || is.fail())
571                         throw SerializationError("deSerializeBulkNodes: "
572                                         "failed to read bulk node data");
573         }
574
575         // Deserialize content
576         if(content_width == 1)
577         {
578                 for(u32 i=0; i<nodecount; i++)
579                         nodes[i].param0 = readU8(&databuf[i]);
580         }
581         else if(content_width == 2)
582         {
583                 for(u32 i=0; i<nodecount; i++)
584                         nodes[i].param0 = readU16(&databuf[i*2]);
585         }
586
587         // Deserialize param1
588         u32 start1 = content_width * nodecount;
589         for(u32 i=0; i<nodecount; i++)
590                 nodes[i].param1 = readU8(&databuf[start1 + i]);
591
592         // Deserialize param2
593         u32 start2 = (content_width + 1) * nodecount;
594         if(content_width == 1)
595         {
596                 for(u32 i=0; i<nodecount; i++) {
597                         nodes[i].param2 = readU8(&databuf[start2 + i]);
598                         if(nodes[i].param0 > 0x7F){
599                                 nodes[i].param0 <<= 4;
600                                 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
601                                 nodes[i].param2 &= 0x0F;
602                         }
603                 }
604         }
605         else if(content_width == 2)
606         {
607                 for(u32 i=0; i<nodecount; i++)
608                         nodes[i].param2 = readU8(&databuf[start2 + i]);
609         }
610 }
611
612 /*
613         Legacy serialization
614 */
615 void MapNode::deSerialize_pre22(u8 *source, u8 version)
616 {
617         if(version <= 1)
618         {
619                 param0 = source[0];
620         }
621         else if(version <= 9)
622         {
623                 param0 = source[0];
624                 param1 = source[1];
625         }
626         else
627         {
628                 param0 = source[0];
629                 param1 = source[1];
630                 param2 = source[2];
631                 if(param0 > 0x7f){
632                         param0 <<= 4;
633                         param0 |= (param2&0xf0)>>4;
634                         param2 &= 0x0f;
635                 }
636         }
637
638         // Convert special values from old version to new
639         if(version <= 19)
640         {
641                 // In these versions, CONTENT_IGNORE and CONTENT_AIR
642                 // are 255 and 254
643                 // Version 19 is fucked up with sometimes the old values and sometimes not
644                 if(param0 == 255)
645                         param0 = CONTENT_IGNORE;
646                 else if(param0 == 254)
647                         param0 = CONTENT_AIR;
648         }
649
650         // Translate to our known version
651         *this = mapnode_translate_to_internal(*this, version);
652 }