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