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