]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapnode.cpp
Fix build since: "Remove referenced schematics from Decorations on clear"
[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 "content_mapnode.h" // For mapnode_translate_*_internal
25 #include "serialization.h" // For ser_ver_supported
26 #include "util/serialize.h"
27 #include "log.h"
28 #include "util/numeric.h"
29 #include <string>
30 #include <sstream>
31
32 static const Rotation wallmounted_to_rot[] = {
33         ROTATE_0, ROTATE_180, ROTATE_90, ROTATE_270
34 };
35
36 static const u8 rot_to_wallmounted[] = {
37         2, 4, 3, 5
38 };
39
40
41 /*
42         MapNode
43 */
44
45 // Create directly from a nodename
46 // If name is unknown, sets CONTENT_IGNORE
47 MapNode::MapNode(INodeDefManager *ndef, const std::string &name,
48                 u8 a_param1, u8 a_param2)
49 {
50         content_t id = CONTENT_IGNORE;
51         ndef->getId(name, id);
52         param0 = id;
53         param1 = a_param1;
54         param2 = a_param2;
55 }
56
57 void MapNode::setLight(enum LightBank bank, u8 a_light, INodeDefManager *nodemgr)
58 {
59         // If node doesn't contain light data, ignore this
60         if(nodemgr->get(*this).param_type != CPT_LIGHT)
61                 return;
62         if(bank == LIGHTBANK_DAY)
63         {
64                 param1 &= 0xf0;
65                 param1 |= a_light & 0x0f;
66         }
67         else if(bank == LIGHTBANK_NIGHT)
68         {
69                 param1 &= 0x0f;
70                 param1 |= (a_light & 0x0f)<<4;
71         }
72         else
73                 assert("Invalid light bank" == NULL);
74 }
75
76 bool MapNode::isLightDayNightEq(INodeDefManager *nodemgr) const
77 {
78         const ContentFeatures &f = nodemgr->get(*this);
79         bool isEqual;
80
81         if (f.param_type == CPT_LIGHT) {
82                 u8 day   = MYMAX(f.light_source, param1 & 0x0f);
83                 u8 night = MYMAX(f.light_source, (param1 >> 4) & 0x0f);
84                 isEqual = day == night;
85         } else {
86                 isEqual = true;
87         }
88
89         return isEqual;
90 }
91
92 u8 MapNode::getLight(enum LightBank bank, INodeDefManager *nodemgr) const
93 {
94         // Select the brightest of [light source, propagated light]
95         const ContentFeatures &f = nodemgr->get(*this);
96
97         u8 light;
98         if(f.param_type == CPT_LIGHT)
99                 light = bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f;
100         else
101                 light = 0;
102
103         return MYMAX(f.light_source, light);
104 }
105
106 u8 MapNode::getLightNoChecks(enum LightBank bank, const ContentFeatures *f) const
107 {
108         return MYMAX(f->light_source,
109                      bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f);
110 }
111
112 bool MapNode::getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const
113 {
114         // Select the brightest of [light source, propagated light]
115         const ContentFeatures &f = nodemgr->get(*this);
116         if(f.param_type == CPT_LIGHT)
117         {
118                 lightday = param1 & 0x0f;
119                 lightnight = (param1>>4)&0x0f;
120         }
121         else
122         {
123                 lightday = 0;
124                 lightnight = 0;
125         }
126         if(f.light_source > lightday)
127                 lightday = f.light_source;
128         if(f.light_source > lightnight)
129                 lightnight = f.light_source;
130         return f.param_type == CPT_LIGHT || f.light_source != 0;
131 }
132
133 u8 MapNode::getFaceDir(INodeDefManager *nodemgr) const
134 {
135         const ContentFeatures &f = nodemgr->get(*this);
136         if(f.param_type_2 == CPT2_FACEDIR)
137                 return (getParam2() & 0x1F) % 24;
138         return 0;
139 }
140
141 u8 MapNode::getWallMounted(INodeDefManager *nodemgr) const
142 {
143         const ContentFeatures &f = nodemgr->get(*this);
144         if(f.param_type_2 == CPT2_WALLMOUNTED)
145                 return getParam2() & 0x07;
146         return 0;
147 }
148
149 v3s16 MapNode::getWallMountedDir(INodeDefManager *nodemgr) const
150 {
151         switch(getWallMounted(nodemgr))
152         {
153         case 0: default: return v3s16(0,1,0);
154         case 1: return v3s16(0,-1,0);
155         case 2: return v3s16(1,0,0);
156         case 3: return v3s16(-1,0,0);
157         case 4: return v3s16(0,0,1);
158         case 5: return v3s16(0,0,-1);
159         }
160 }
161
162 void MapNode::rotateAlongYAxis(INodeDefManager *nodemgr, Rotation rot) {
163         ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
164
165         if (cpt2 == CPT2_FACEDIR) {
166                 u8 newrot = param2 & 3;
167                 param2 &= ~3;
168                 param2 |= (newrot + rot) & 3;
169         } else if (cpt2 == CPT2_WALLMOUNTED) {
170                 u8 wmountface = (param2 & 7);
171                 if (wmountface <= 1)
172                         return;
173
174                 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
175                 param2 &= ~7;
176                 param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
177         }
178 }
179
180 static std::vector<aabb3f> transformNodeBox(const MapNode &n,
181                 const NodeBox &nodebox, INodeDefManager *nodemgr)
182 {
183         std::vector<aabb3f> boxes;
184         if(nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED)
185         {
186                 const std::vector<aabb3f> &fixed = nodebox.fixed;
187                 int facedir = n.getFaceDir(nodemgr);
188                 u8 axisdir = facedir>>2;
189                 facedir&=0x03;
190                 for(std::vector<aabb3f>::const_iterator
191                                 i = fixed.begin();
192                                 i != fixed.end(); i++)
193                 {
194                         aabb3f box = *i;
195
196                         if (nodebox.type == NODEBOX_LEVELED) {
197                                 box.MaxEdge.Y = -BS/2 + BS*((float)1/LEVELED_MAX) * n.getLevel(nodemgr);
198                         }
199
200                         switch (axisdir)
201                         {
202                         case 0:
203                                 if(facedir == 1)
204                                 {
205                                         box.MinEdge.rotateXZBy(-90);
206                                         box.MaxEdge.rotateXZBy(-90);
207                                 }
208                                 else if(facedir == 2)
209                                 {
210                                         box.MinEdge.rotateXZBy(180);
211                                         box.MaxEdge.rotateXZBy(180);
212                                 }
213                                 else if(facedir == 3)
214                                 {
215                                         box.MinEdge.rotateXZBy(90);
216                                         box.MaxEdge.rotateXZBy(90);
217                                 }
218                                 break;
219                         case 1: // z+
220                                 box.MinEdge.rotateYZBy(90);
221                                 box.MaxEdge.rotateYZBy(90);
222                                 if(facedir == 1)
223                                 {
224                                         box.MinEdge.rotateXYBy(90);
225                                         box.MaxEdge.rotateXYBy(90);
226                                 }
227                                 else if(facedir == 2)
228                                 {
229                                         box.MinEdge.rotateXYBy(180);
230                                         box.MaxEdge.rotateXYBy(180);
231                                 }
232                                 else if(facedir == 3)
233                                 {
234                                         box.MinEdge.rotateXYBy(-90);
235                                         box.MaxEdge.rotateXYBy(-90);
236                                 }
237                                 break;
238                         case 2: //z-
239                                 box.MinEdge.rotateYZBy(-90);
240                                 box.MaxEdge.rotateYZBy(-90);
241                                 if(facedir == 1)
242                                 {
243                                         box.MinEdge.rotateXYBy(-90);
244                                         box.MaxEdge.rotateXYBy(-90);
245                                 }
246                                 else if(facedir == 2)
247                                 {
248                                         box.MinEdge.rotateXYBy(180);
249                                         box.MaxEdge.rotateXYBy(180);
250                                 }
251                                 else if(facedir == 3)
252                                 {
253                                         box.MinEdge.rotateXYBy(90);
254                                         box.MaxEdge.rotateXYBy(90);
255                                 }
256                                 break;
257                         case 3:  //x+
258                                 box.MinEdge.rotateXYBy(-90);
259                                 box.MaxEdge.rotateXYBy(-90);
260                                 if(facedir == 1)
261                                 {
262                                         box.MinEdge.rotateYZBy(90);
263                                         box.MaxEdge.rotateYZBy(90);
264                                 }
265                                 else if(facedir == 2)
266                                 {
267                                         box.MinEdge.rotateYZBy(180);
268                                         box.MaxEdge.rotateYZBy(180);
269                                 }
270                                 else if(facedir == 3)
271                                 {
272                                         box.MinEdge.rotateYZBy(-90);
273                                         box.MaxEdge.rotateYZBy(-90);
274                                 }
275                                 break;
276                         case 4:  //x-
277                                 box.MinEdge.rotateXYBy(90);
278                                 box.MaxEdge.rotateXYBy(90);
279                                 if(facedir == 1)
280                                 {
281                                         box.MinEdge.rotateYZBy(-90);
282                                         box.MaxEdge.rotateYZBy(-90);
283                                 }
284                                 else if(facedir == 2)
285                                 {
286                                         box.MinEdge.rotateYZBy(180);
287                                         box.MaxEdge.rotateYZBy(180);
288                                 }
289                                 else if(facedir == 3)
290                                 {
291                                         box.MinEdge.rotateYZBy(90);
292                                         box.MaxEdge.rotateYZBy(90);
293                                 }
294                                 break;
295                         case 5:
296                                 box.MinEdge.rotateXYBy(-180);
297                                 box.MaxEdge.rotateXYBy(-180);
298                                 if(facedir == 1)
299                                 {
300                                         box.MinEdge.rotateXZBy(90);
301                                         box.MaxEdge.rotateXZBy(90);
302                                 }
303                                 else if(facedir == 2)
304                                 {
305                                         box.MinEdge.rotateXZBy(180);
306                                         box.MaxEdge.rotateXZBy(180);
307                                 }
308                                 else if(facedir == 3)
309                                 {
310                                         box.MinEdge.rotateXZBy(-90);
311                                         box.MaxEdge.rotateXZBy(-90);
312                                 }
313                                 break;
314                         default:
315                                 break;
316                         }
317                         box.repair();
318                         boxes.push_back(box);
319                 }
320         }
321         else if(nodebox.type == NODEBOX_WALLMOUNTED)
322         {
323                 v3s16 dir = n.getWallMountedDir(nodemgr);
324
325                 // top
326                 if(dir == v3s16(0,1,0))
327                 {
328                         boxes.push_back(nodebox.wall_top);
329                 }
330                 // bottom
331                 else if(dir == v3s16(0,-1,0))
332                 {
333                         boxes.push_back(nodebox.wall_bottom);
334                 }
335                 // side
336                 else
337                 {
338                         v3f vertices[2] =
339                         {
340                                 nodebox.wall_side.MinEdge,
341                                 nodebox.wall_side.MaxEdge
342                         };
343
344                         for(s32 i=0; i<2; i++)
345                         {
346                                 if(dir == v3s16(-1,0,0))
347                                         vertices[i].rotateXZBy(0);
348                                 if(dir == v3s16(1,0,0))
349                                         vertices[i].rotateXZBy(180);
350                                 if(dir == v3s16(0,0,-1))
351                                         vertices[i].rotateXZBy(90);
352                                 if(dir == v3s16(0,0,1))
353                                         vertices[i].rotateXZBy(-90);
354                         }
355
356                         aabb3f box = aabb3f(vertices[0]);
357                         box.addInternalPoint(vertices[1]);
358                         boxes.push_back(box);
359                 }
360         }
361         else // NODEBOX_REGULAR
362         {
363                 boxes.push_back(aabb3f(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2));
364         }
365         return boxes;
366 }
367
368 std::vector<aabb3f> MapNode::getNodeBoxes(INodeDefManager *nodemgr) const
369 {
370         const ContentFeatures &f = nodemgr->get(*this);
371         return transformNodeBox(*this, f.node_box, nodemgr);
372 }
373
374 std::vector<aabb3f> MapNode::getCollisionBoxes(INodeDefManager *nodemgr) const
375 {
376         const ContentFeatures &f = nodemgr->get(*this);
377         if (f.collision_box.fixed.empty())
378                 return transformNodeBox(*this, f.node_box, nodemgr);
379         else
380                 return transformNodeBox(*this, f.collision_box, nodemgr);
381 }
382
383 std::vector<aabb3f> MapNode::getSelectionBoxes(INodeDefManager *nodemgr) const
384 {
385         const ContentFeatures &f = nodemgr->get(*this);
386         return transformNodeBox(*this, f.selection_box, nodemgr);
387 }
388
389 u8 MapNode::getMaxLevel(INodeDefManager *nodemgr) const
390 {
391         const ContentFeatures &f = nodemgr->get(*this);
392         // todo: after update in all games leave only if (f.param_type_2 ==
393         if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
394                 return LIQUID_LEVEL_MAX;
395         if(f.leveled || f.param_type_2 == CPT2_LEVELED)
396                 return LEVELED_MAX;
397         return 0;
398 }
399
400 u8 MapNode::getLevel(INodeDefManager *nodemgr) const
401 {
402         const ContentFeatures &f = nodemgr->get(*this);
403         // todo: after update in all games leave only if (f.param_type_2 ==
404         if(f.liquid_type == LIQUID_SOURCE)
405                 return LIQUID_LEVEL_SOURCE;
406         if (f.param_type_2 == CPT2_FLOWINGLIQUID)
407                 return getParam2() & LIQUID_LEVEL_MASK;
408         if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
409                 return getParam2() & LIQUID_LEVEL_MASK;
410         if(f.leveled || f.param_type_2 == CPT2_LEVELED) {
411                  u8 level = getParam2() & LEVELED_MASK;
412                  if(level)
413                         return level;
414                  if(f.leveled > LEVELED_MAX)
415                         return LEVELED_MAX;
416                  return f.leveled; //default
417         }
418         return 0;
419 }
420
421 u8 MapNode::setLevel(INodeDefManager *nodemgr, s8 level)
422 {
423         u8 rest = 0;
424         if (level < 1) {
425                 setContent(CONTENT_AIR);
426                 return 0;
427         }
428         const ContentFeatures &f = nodemgr->get(*this);
429         if (f.param_type_2 == CPT2_FLOWINGLIQUID
430                 || f.liquid_type == LIQUID_FLOWING
431                 || f.liquid_type == LIQUID_SOURCE) {
432                 if (level >= LIQUID_LEVEL_SOURCE) {
433                         rest = level - LIQUID_LEVEL_SOURCE;
434                         setContent(nodemgr->getId(f.liquid_alternative_source));
435                 } else {
436                         setContent(nodemgr->getId(f.liquid_alternative_flowing));
437                         setParam2(level & LIQUID_LEVEL_MASK);
438                 }
439         } else if (f.leveled || f.param_type_2 == CPT2_LEVELED) {
440                 if (level > LEVELED_MAX) {
441                         rest = level - LEVELED_MAX;
442                         level = LEVELED_MAX;
443                 }
444                 setParam2(level & LEVELED_MASK);
445         }
446         return rest;
447 }
448
449 u8 MapNode::addLevel(INodeDefManager *nodemgr, s8 add)
450 {
451         s8 level = getLevel(nodemgr);
452         if (add == 0) level = 1;
453         level += add;
454         return setLevel(nodemgr, level);
455 }
456
457 u32 MapNode::serializedLength(u8 version)
458 {
459         if(!ser_ver_supported(version))
460                 throw VersionMismatchException("ERROR: MapNode format not supported");
461
462         if(version == 0)
463                 return 1;
464         else if(version <= 9)
465                 return 2;
466         else if(version <= 23)
467                 return 3;
468         else
469                 return 4;
470 }
471 void MapNode::serialize(u8 *dest, u8 version)
472 {
473         if(!ser_ver_supported(version))
474                 throw VersionMismatchException("ERROR: MapNode format not supported");
475
476         // Can't do this anymore; we have 16-bit dynamically allocated node IDs
477         // in memory; conversion just won't work in this direction.
478         if(version < 24)
479                 throw SerializationError("MapNode::serialize: serialization to "
480                                 "version < 24 not possible");
481
482         writeU16(dest+0, param0);
483         writeU8(dest+2, param1);
484         writeU8(dest+3, param2);
485 }
486 void MapNode::deSerialize(u8 *source, u8 version)
487 {
488         if(!ser_ver_supported(version))
489                 throw VersionMismatchException("ERROR: MapNode format not supported");
490
491         if(version <= 21)
492         {
493                 deSerialize_pre22(source, version);
494                 return;
495         }
496
497         if(version >= 24){
498                 param0 = readU16(source+0);
499                 param1 = readU8(source+2);
500                 param2 = readU8(source+3);
501         }else{
502                 param0 = readU8(source+0);
503                 param1 = readU8(source+1);
504                 param2 = readU8(source+2);
505                 if(param0 > 0x7F){
506                         param0 |= ((param2&0xF0)<<4);
507                         param2 &= 0x0F;
508                 }
509         }
510 }
511 void MapNode::serializeBulk(std::ostream &os, int version,
512                 const MapNode *nodes, u32 nodecount,
513                 u8 content_width, u8 params_width, bool compressed)
514 {
515         if(!ser_ver_supported(version))
516                 throw VersionMismatchException("ERROR: MapNode format not supported");
517
518         sanity_check(content_width == 2);
519         sanity_check(params_width == 2);
520
521         // Can't do this anymore; we have 16-bit dynamically allocated node IDs
522         // in memory; conversion just won't work in this direction.
523         if(version < 24)
524                 throw SerializationError("MapNode::serializeBulk: serialization to "
525                                 "version < 24 not possible");
526
527         SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
528
529         // Serialize content
530         for(u32 i=0; i<nodecount; i++)
531                 writeU16(&databuf[i*2], nodes[i].param0);
532
533         // Serialize param1
534         u32 start1 = content_width * nodecount;
535         for(u32 i=0; i<nodecount; i++)
536                 writeU8(&databuf[start1 + i], nodes[i].param1);
537
538         // Serialize param2
539         u32 start2 = (content_width + 1) * nodecount;
540         for(u32 i=0; i<nodecount; i++)
541                 writeU8(&databuf[start2 + i], nodes[i].param2);
542
543         /*
544                 Compress data to output stream
545         */
546
547         if(compressed)
548         {
549                 compressZlib(databuf, os);
550         }
551         else
552         {
553                 os.write((const char*) &databuf[0], databuf.getSize());
554         }
555 }
556
557 // Deserialize bulk node data
558 void MapNode::deSerializeBulk(std::istream &is, int version,
559                 MapNode *nodes, u32 nodecount,
560                 u8 content_width, u8 params_width, bool compressed)
561 {
562         if(!ser_ver_supported(version))
563                 throw VersionMismatchException("ERROR: MapNode format not supported");
564
565         if (version < 22
566                         || (content_width != 1 && content_width != 2)
567                         || params_width != 2)
568                 FATAL_ERROR("Deserialize bulk node data error");
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 }