]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapnode.cpp
786224240a5e302a6f5cb13ed5b359e26f05d3de
[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 "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 <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(0);
74 }
75
76 u8 MapNode::getLight(enum LightBank bank, INodeDefManager *nodemgr) const
77 {
78         // Select the brightest of [light source, propagated light]
79         const ContentFeatures &f = nodemgr->get(*this);
80         u8 light = 0;
81         if(f.param_type == CPT_LIGHT)
82         {
83                 if(bank == LIGHTBANK_DAY)
84                         light = param1 & 0x0f;
85                 else if(bank == LIGHTBANK_NIGHT)
86                         light = (param1>>4)&0x0f;
87                 else
88                         assert(0);
89         }
90         if(f.light_source > light)
91                 light = f.light_source;
92         return light;
93 }
94
95 bool MapNode::getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const
96 {
97         // Select the brightest of [light source, propagated light]
98         const ContentFeatures &f = nodemgr->get(*this);
99         if(f.param_type == CPT_LIGHT)
100         {
101                 lightday = param1 & 0x0f;
102                 lightnight = (param1>>4)&0x0f;
103         }
104         else
105         {
106                 lightday = 0;
107                 lightnight = 0;
108         }
109         if(f.light_source > lightday)
110                 lightday = f.light_source;
111         if(f.light_source > lightnight)
112                 lightnight = f.light_source;
113         return f.param_type == CPT_LIGHT || f.light_source != 0;
114 }
115
116 u8 MapNode::getFaceDir(INodeDefManager *nodemgr) const
117 {
118         const ContentFeatures &f = nodemgr->get(*this);
119         if(f.param_type_2 == CPT2_FACEDIR)
120                 return getParam2() & 0x1F;
121         return 0;
122 }
123
124 u8 MapNode::getWallMounted(INodeDefManager *nodemgr) const
125 {
126         const ContentFeatures &f = nodemgr->get(*this);
127         if(f.param_type_2 == CPT2_WALLMOUNTED)
128                 return getParam2() & 0x07;
129         return 0;
130 }
131
132 v3s16 MapNode::getWallMountedDir(INodeDefManager *nodemgr) const
133 {
134         switch(getWallMounted(nodemgr))
135         {
136         case 0: default: return v3s16(0,1,0);
137         case 1: return v3s16(0,-1,0);
138         case 2: return v3s16(1,0,0);
139         case 3: return v3s16(-1,0,0);
140         case 4: return v3s16(0,0,1);
141         case 5: return v3s16(0,0,-1);
142         }
143 }
144
145 void MapNode::rotateAlongYAxis(INodeDefManager *nodemgr, Rotation rot) {
146         ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
147
148         if (cpt2 == CPT2_FACEDIR) {
149                 u8 newrot = param2 & 3;
150                 param2 &= ~3;
151                 param2 |= (newrot + rot) & 3;
152         } else if (cpt2 == CPT2_WALLMOUNTED) {
153                 u8 wmountface = (param2 & 7);
154                 if (wmountface <= 1)
155                         return;
156                         
157                 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
158                 param2 &= ~7;
159                 param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
160         }
161 }
162
163 static std::vector<aabb3f> transformNodeBox(const MapNode &n,
164                 const NodeBox &nodebox, INodeDefManager *nodemgr)
165 {
166         std::vector<aabb3f> boxes;
167         if(nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED)
168         {
169                 const std::vector<aabb3f> &fixed = nodebox.fixed;
170                 int facedir = n.getFaceDir(nodemgr);
171                 u8 axisdir = facedir>>2;
172                 facedir&=0x03;
173                 for(std::vector<aabb3f>::const_iterator
174                                 i = fixed.begin();
175                                 i != fixed.end(); i++)
176                 {
177                         aabb3f box = *i;
178
179                         if (nodebox.type == NODEBOX_LEVELED) {
180                                 box.MaxEdge.Y = -BS/2 + BS*((float)1/LEVELED_MAX) * n.getLevel(nodemgr);
181                         }
182
183                         switch (axisdir)
184                         {
185                         case 0:
186                                 if(facedir == 1)
187                                 {
188                                         box.MinEdge.rotateXZBy(-90);
189                                         box.MaxEdge.rotateXZBy(-90);
190                                 }
191                                 else if(facedir == 2)
192                                 {
193                                         box.MinEdge.rotateXZBy(180);
194                                         box.MaxEdge.rotateXZBy(180);
195                                 }
196                                 else if(facedir == 3)
197                                 {
198                                         box.MinEdge.rotateXZBy(90);
199                                         box.MaxEdge.rotateXZBy(90);
200                                 }
201                                 break;
202                         case 1: // z+
203                                 box.MinEdge.rotateYZBy(90);
204                                 box.MaxEdge.rotateYZBy(90);
205                                 if(facedir == 1)
206                                 {
207                                         box.MinEdge.rotateXYBy(90);
208                                         box.MaxEdge.rotateXYBy(90);
209                                 }
210                                 else if(facedir == 2)
211                                 {
212                                         box.MinEdge.rotateXYBy(180);
213                                         box.MaxEdge.rotateXYBy(180);
214                                 }
215                                 else if(facedir == 3)
216                                 {
217                                         box.MinEdge.rotateXYBy(-90);
218                                         box.MaxEdge.rotateXYBy(-90);
219                                 }
220                                 break;
221                         case 2: //z-
222                                 box.MinEdge.rotateYZBy(-90);
223                                 box.MaxEdge.rotateYZBy(-90);
224                                 if(facedir == 1)
225                                 {
226                                         box.MinEdge.rotateXYBy(-90);
227                                         box.MaxEdge.rotateXYBy(-90);
228                                 }
229                                 else if(facedir == 2)
230                                 {
231                                         box.MinEdge.rotateXYBy(180);
232                                         box.MaxEdge.rotateXYBy(180);
233                                 }
234                                 else if(facedir == 3)
235                                 {
236                                         box.MinEdge.rotateXYBy(90);
237                                         box.MaxEdge.rotateXYBy(90);
238                                 }
239                                 break;
240                         case 3:  //x+
241                                 box.MinEdge.rotateXYBy(-90);
242                                 box.MaxEdge.rotateXYBy(-90);
243                                 if(facedir == 1)
244                                 {
245                                         box.MinEdge.rotateYZBy(90);
246                                         box.MaxEdge.rotateYZBy(90);
247                                 }
248                                 else if(facedir == 2)
249                                 {
250                                         box.MinEdge.rotateYZBy(180);
251                                         box.MaxEdge.rotateYZBy(180);
252                                 }
253                                 else if(facedir == 3)
254                                 {
255                                         box.MinEdge.rotateYZBy(-90);
256                                         box.MaxEdge.rotateYZBy(-90);
257                                 }
258                                 break;
259                         case 4:  //x-
260                                 box.MinEdge.rotateXYBy(90);
261                                 box.MaxEdge.rotateXYBy(90);
262                                 if(facedir == 1)
263                                 {
264                                         box.MinEdge.rotateYZBy(-90);
265                                         box.MaxEdge.rotateYZBy(-90);
266                                 }
267                                 else if(facedir == 2)
268                                 {
269                                         box.MinEdge.rotateYZBy(180);
270                                         box.MaxEdge.rotateYZBy(180);
271                                 }
272                                 else if(facedir == 3)
273                                 {
274                                         box.MinEdge.rotateYZBy(90);
275                                         box.MaxEdge.rotateYZBy(90);
276                                 }
277                                 break;
278                         case 5:
279                                 box.MinEdge.rotateXYBy(-180);
280                                 box.MaxEdge.rotateXYBy(-180);
281                                 if(facedir == 1)
282                                 {
283                                         box.MinEdge.rotateXZBy(90);
284                                         box.MaxEdge.rotateXZBy(90);
285                                 }
286                                 else if(facedir == 2)
287                                 {
288                                         box.MinEdge.rotateXZBy(180);
289                                         box.MaxEdge.rotateXZBy(180);
290                                 }
291                                 else if(facedir == 3)
292                                 {
293                                         box.MinEdge.rotateXZBy(-90);
294                                         box.MaxEdge.rotateXZBy(-90);
295                                 }
296                                 break;
297                         default:
298                                 break;
299                         }
300                         box.repair();
301                         boxes.push_back(box);
302                 }
303         }
304         else if(nodebox.type == NODEBOX_WALLMOUNTED)
305         {
306                 v3s16 dir = n.getWallMountedDir(nodemgr);
307
308                 // top
309                 if(dir == v3s16(0,1,0))
310                 {
311                         boxes.push_back(nodebox.wall_top);
312                 }
313                 // bottom
314                 else if(dir == v3s16(0,-1,0))
315                 {
316                         boxes.push_back(nodebox.wall_bottom);
317                 }
318                 // side
319                 else
320                 {
321                         v3f vertices[2] =
322                         {
323                                 nodebox.wall_side.MinEdge,
324                                 nodebox.wall_side.MaxEdge
325                         };
326
327                         for(s32 i=0; i<2; i++)
328                         {
329                                 if(dir == v3s16(-1,0,0))
330                                         vertices[i].rotateXZBy(0);
331                                 if(dir == v3s16(1,0,0))
332                                         vertices[i].rotateXZBy(180);
333                                 if(dir == v3s16(0,0,-1))
334                                         vertices[i].rotateXZBy(90);
335                                 if(dir == v3s16(0,0,1))
336                                         vertices[i].rotateXZBy(-90);
337                         }
338
339                         aabb3f box = aabb3f(vertices[0]);
340                         box.addInternalPoint(vertices[1]);
341                         boxes.push_back(box);
342                 }
343         }
344         else // NODEBOX_REGULAR
345         {
346                 boxes.push_back(aabb3f(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2));
347         }
348         return boxes;
349 }
350
351 std::vector<aabb3f> MapNode::getNodeBoxes(INodeDefManager *nodemgr) const
352 {
353         const ContentFeatures &f = nodemgr->get(*this);
354         return transformNodeBox(*this, f.node_box, nodemgr);
355 }
356
357 std::vector<aabb3f> MapNode::getCollisionBoxes(INodeDefManager *nodemgr) const
358 {
359         const ContentFeatures &f = nodemgr->get(*this);
360         if (f.collision_box.fixed.empty())
361                 return transformNodeBox(*this, f.node_box, nodemgr);
362         else
363                 return transformNodeBox(*this, f.collision_box, nodemgr);
364 }
365
366 std::vector<aabb3f> MapNode::getSelectionBoxes(INodeDefManager *nodemgr) const
367 {
368         const ContentFeatures &f = nodemgr->get(*this);
369         return transformNodeBox(*this, f.selection_box, nodemgr);
370 }
371
372 u8 MapNode::getMaxLevel(INodeDefManager *nodemgr) const
373 {
374         const ContentFeatures &f = nodemgr->get(*this);
375         // todo: after update in all games leave only if (f.param_type_2 ==
376         if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
377                 return LIQUID_LEVEL_MAX;
378         if(f.leveled || f.param_type_2 == CPT2_LEVELED)
379                 return LEVELED_MAX;
380         return 0;
381 }
382
383 u8 MapNode::getLevel(INodeDefManager *nodemgr) const
384 {
385         const ContentFeatures &f = nodemgr->get(*this);
386         // todo: after update in all games leave only if (f.param_type_2 ==
387         if(f.liquid_type == LIQUID_SOURCE)
388                 return LIQUID_LEVEL_SOURCE;
389         if (f.param_type_2 == CPT2_FLOWINGLIQUID)
390                 return getParam2() & LIQUID_LEVEL_MASK;
391         if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
392                 return getParam2() & LIQUID_LEVEL_MASK;
393         if(f.leveled || f.param_type_2 == CPT2_LEVELED) {
394                  u8 level = getParam2() & LEVELED_MASK;
395                  if(level)
396                         return level;
397                  if(f.leveled > LEVELED_MAX)
398                         return LEVELED_MAX;
399                  return f.leveled; //default
400         }
401         return 0;
402 }
403
404 u8 MapNode::setLevel(INodeDefManager *nodemgr, s8 level)
405 {
406         u8 rest = 0;
407         if (level < 1) {
408                 setContent(CONTENT_AIR);
409                 return 0;
410         }
411         const ContentFeatures &f = nodemgr->get(*this);
412         if (f.param_type_2 == CPT2_FLOWINGLIQUID
413                 || f.liquid_type == LIQUID_FLOWING
414                 || f.liquid_type == LIQUID_SOURCE) {
415                 if (level >= LIQUID_LEVEL_SOURCE) {
416                         rest = level - LIQUID_LEVEL_SOURCE;
417                         setContent(nodemgr->getId(f.liquid_alternative_source));
418                 } else {
419                         setContent(nodemgr->getId(f.liquid_alternative_flowing));
420                         setParam2(level & LIQUID_LEVEL_MASK);
421                 }
422         } else if (f.leveled || f.param_type_2 == CPT2_LEVELED) {
423                 if (level > LEVELED_MAX) {
424                         rest = level - LEVELED_MAX;
425                         level = LEVELED_MAX;
426                 }
427                 setParam2(level & LEVELED_MASK);
428         }
429         return rest;
430 }
431
432 u8 MapNode::addLevel(INodeDefManager *nodemgr, s8 add)
433 {
434         s8 level = getLevel(nodemgr);
435         if (add == 0) level = 1;
436         level += add;
437         return setLevel(nodemgr, level);
438 }
439
440 void MapNode::freezeMelt(INodeDefManager *ndef) {
441         u8 level_was_max = this->getMaxLevel(ndef);
442         u8 level_was = this->getLevel(ndef);
443         this->setContent(ndef->getId(ndef->get(*this).freezemelt));
444         u8 level_now_max = this->getMaxLevel(ndef);
445         if (level_was_max && level_was_max != level_now_max) {
446                 u8 want = (float)level_now_max / level_was_max * level_was;
447                 if (!want)
448                         want = 1;
449                 if (want != level_was)
450                         this->setLevel(ndef, want);
451                 //errorstream<<"was="<<(int)level_was<<"/"<<(int)level_was_max<<" nowm="<<(int)want<<"/"<<(int)level_now_max<< " => "<<(int)this->getLevel(ndef)<< std::endl;
452         }
453         if (this->getMaxLevel(ndef) && !this->getLevel(ndef))
454                 this->addLevel(ndef);
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         assert(content_width == 2);
519         assert(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         assert(version >= 22);
566         assert(content_width == 1 || content_width == 2);
567         assert(params_width == 2);
568
569         // Uncompress or read data
570         u32 len = nodecount * (content_width + params_width);
571         SharedBuffer<u8> databuf(len);
572         if(compressed)
573         {
574                 std::ostringstream os(std::ios_base::binary);
575                 decompressZlib(is, os);
576                 std::string s = os.str();
577                 if(s.size() != len)
578                         throw SerializationError("deSerializeBulkNodes: "
579                                         "decompress resulted in invalid size");
580                 memcpy(&databuf[0], s.c_str(), len);
581         }
582         else
583         {
584                 is.read((char*) &databuf[0], len);
585                 if(is.eof() || is.fail())
586                         throw SerializationError("deSerializeBulkNodes: "
587                                         "failed to read bulk node data");
588         }
589
590         // Deserialize content
591         if(content_width == 1)
592         {
593                 for(u32 i=0; i<nodecount; i++)
594                         nodes[i].param0 = readU8(&databuf[i]);
595         }
596         else if(content_width == 2)
597         {
598                 for(u32 i=0; i<nodecount; i++)
599                         nodes[i].param0 = readU16(&databuf[i*2]);
600         }
601
602         // Deserialize param1
603         u32 start1 = content_width * nodecount;
604         for(u32 i=0; i<nodecount; i++)
605                 nodes[i].param1 = readU8(&databuf[start1 + i]);
606
607         // Deserialize param2
608         u32 start2 = (content_width + 1) * nodecount;
609         if(content_width == 1)
610         {
611                 for(u32 i=0; i<nodecount; i++) {
612                         nodes[i].param2 = readU8(&databuf[start2 + i]);
613                         if(nodes[i].param0 > 0x7F){
614                                 nodes[i].param0 <<= 4;
615                                 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
616                                 nodes[i].param2 &= 0x0F;
617                         }
618                 }
619         }
620         else if(content_width == 2)
621         {
622                 for(u32 i=0; i<nodecount; i++)
623                         nodes[i].param2 = readU8(&databuf[start2 + i]);
624         }
625 }
626
627 /*
628         Legacy serialization
629 */
630 void MapNode::deSerialize_pre22(u8 *source, u8 version)
631 {
632         if(version <= 1)
633         {
634                 param0 = source[0];
635         }
636         else if(version <= 9)
637         {
638                 param0 = source[0];
639                 param1 = source[1];
640         }
641         else
642         {
643                 param0 = source[0];
644                 param1 = source[1];
645                 param2 = source[2];
646                 if(param0 > 0x7f){
647                         param0 <<= 4;
648                         param0 |= (param2&0xf0)>>4;
649                         param2 &= 0x0f;
650                 }
651         }
652         
653         // Convert special values from old version to new
654         if(version <= 19)
655         {
656                 // In these versions, CONTENT_IGNORE and CONTENT_AIR
657                 // are 255 and 254
658                 // Version 19 is fucked up with sometimes the old values and sometimes not
659                 if(param0 == 255)
660                         param0 = CONTENT_IGNORE;
661                 else if(param0 == 254)
662                         param0 = CONTENT_AIR;
663         }
664
665         // Translate to our known version
666         *this = mapnode_translate_to_internal(*this, version);
667 }