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