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