]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapnode.cpp
Weather support
[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::getSelectionBoxes(INodeDefManager *nodemgr) const
358 {
359         const ContentFeatures &f = nodemgr->get(*this);
360         return transformNodeBox(*this, f.selection_box, nodemgr);
361 }
362
363 u8 MapNode::getMaxLevel(INodeDefManager *nodemgr) const
364 {
365         const ContentFeatures &f = nodemgr->get(*this);
366         // todo: after update in all games leave only if (f.param_type_2 ==
367         if(        f.liquid_type == LIQUID_SOURCE 
368                 || f.liquid_type == LIQUID_FLOWING 
369                 || f.param_type_2 == CPT2_FLOWINGLIQUID)
370                 return LIQUID_LEVEL_MAX;
371         if(f.leveled || f.param_type_2 == CPT2_LEVELED)
372                 return LEVELED_MAX;
373         return 0;
374 }
375
376 u8 MapNode::getLevel(INodeDefManager *nodemgr) const
377 {
378         const ContentFeatures &f = nodemgr->get(*this);
379         // todo: after update in all games leave only if (f.param_type_2 ==
380         if(f.liquid_type == LIQUID_SOURCE)
381                 return LIQUID_LEVEL_SOURCE;
382         if (f.param_type_2 == CPT2_FLOWINGLIQUID)
383                 return getParam2() & LIQUID_LEVEL_MASK;
384         if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
385                 return getParam2() & LIQUID_LEVEL_MASK;
386         if(f.leveled || f.param_type_2 == CPT2_LEVELED) {
387                  u8 level = getParam2() & LEVELED_MASK;
388                  if(level) return level;
389                  if(f.leveled > LEVELED_MAX) return LEVELED_MAX;
390                  return f.leveled; //default
391         }
392         return 0;
393 }
394
395 u8 MapNode::addLevel(INodeDefManager *nodemgr, s8 add)
396 {
397         s8 level = getLevel(nodemgr);
398         u8 rest = 0;
399         if (add == 0) level = 1;
400         level += add;
401         if (level < 1) {
402                 setContent(CONTENT_AIR);
403                 return 0;
404         }
405         const ContentFeatures &f = nodemgr->get(*this);
406         if (       f.param_type_2 == CPT2_FLOWINGLIQUID
407                 || f.liquid_type == LIQUID_FLOWING
408                 || f.liquid_type == LIQUID_SOURCE) {
409                 if (level >= LIQUID_LEVEL_MAX) {
410                         rest = level - LIQUID_LEVEL_MAX;
411                         setContent(nodemgr->getId(f.liquid_alternative_source));
412                 } else {
413                         setContent(nodemgr->getId(f.liquid_alternative_flowing));
414                         setParam2(level & LIQUID_LEVEL_MASK);
415                 }
416         } else if (f.leveled || f.param_type_2 == CPT2_LEVELED) {
417                 if (level > LEVELED_MAX) {
418                         rest = level - LEVELED_MAX;
419                         level = LEVELED_MAX;
420                 }
421                 setParam2(level & LEVELED_MASK);
422         }
423         return rest;
424 }
425
426 u32 MapNode::serializedLength(u8 version)
427 {
428         if(!ser_ver_supported(version))
429                 throw VersionMismatchException("ERROR: MapNode format not supported");
430                 
431         if(version == 0)
432                 return 1;
433         else if(version <= 9)
434                 return 2;
435         else if(version <= 23)
436                 return 3;
437         else
438                 return 4;
439 }
440 void MapNode::serialize(u8 *dest, u8 version)
441 {
442         if(!ser_ver_supported(version))
443                 throw VersionMismatchException("ERROR: MapNode format not supported");
444         
445         // Can't do this anymore; we have 16-bit dynamically allocated node IDs
446         // in memory; conversion just won't work in this direction.
447         if(version < 24)
448                 throw SerializationError("MapNode::serialize: serialization to "
449                                 "version < 24 not possible");
450                 
451         writeU16(dest+0, param0);
452         writeU8(dest+2, param1);
453         writeU8(dest+3, param2);
454 }
455 void MapNode::deSerialize(u8 *source, u8 version)
456 {
457         if(!ser_ver_supported(version))
458                 throw VersionMismatchException("ERROR: MapNode format not supported");
459                 
460         if(version <= 21)
461         {
462                 deSerialize_pre22(source, version);
463                 return;
464         }
465
466         if(version >= 24){
467                 param0 = readU16(source+0);
468                 param1 = readU8(source+2);
469                 param2 = readU8(source+3);
470         }
471         else{
472                 param0 = readU8(source+0);
473                 param1 = readU8(source+1);
474                 param2 = readU8(source+2);
475                 if(param0 > 0x7F){
476                         param0 |= ((param2&0xF0)<<4);
477                         param2 &= 0x0F;
478                 }
479         }
480 }
481 void MapNode::serializeBulk(std::ostream &os, int version,
482                 const MapNode *nodes, u32 nodecount,
483                 u8 content_width, u8 params_width, bool compressed)
484 {
485         if(!ser_ver_supported(version))
486                 throw VersionMismatchException("ERROR: MapNode format not supported");
487
488         assert(content_width == 2);
489         assert(params_width == 2);
490
491         // Can't do this anymore; we have 16-bit dynamically allocated node IDs
492         // in memory; conversion just won't work in this direction.
493         if(version < 24)
494                 throw SerializationError("MapNode::serializeBulk: serialization to "
495                                 "version < 24 not possible");
496
497         SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
498
499         // Serialize content
500         for(u32 i=0; i<nodecount; i++)
501                 writeU16(&databuf[i*2], nodes[i].param0);
502
503         // Serialize param1
504         u32 start1 = content_width * nodecount;
505         for(u32 i=0; i<nodecount; i++)
506                 writeU8(&databuf[start1 + i], nodes[i].param1);
507
508         // Serialize param2
509         u32 start2 = (content_width + 1) * nodecount;
510         for(u32 i=0; i<nodecount; i++)
511                 writeU8(&databuf[start2 + i], nodes[i].param2);
512
513         /*
514                 Compress data to output stream
515         */
516
517         if(compressed)
518         {
519                 compressZlib(databuf, os);
520         }
521         else
522         {
523                 os.write((const char*) &databuf[0], databuf.getSize());
524         }
525 }
526
527 // Deserialize bulk node data
528 void MapNode::deSerializeBulk(std::istream &is, int version,
529                 MapNode *nodes, u32 nodecount,
530                 u8 content_width, u8 params_width, bool compressed)
531 {
532         if(!ser_ver_supported(version))
533                 throw VersionMismatchException("ERROR: MapNode format not supported");
534
535         assert(version >= 22);
536         assert(content_width == 1 || content_width == 2);
537         assert(params_width == 2);
538
539         // Uncompress or read data
540         u32 len = nodecount * (content_width + params_width);
541         SharedBuffer<u8> databuf(len);
542         if(compressed)
543         {
544                 std::ostringstream os(std::ios_base::binary);
545                 decompressZlib(is, os);
546                 std::string s = os.str();
547                 if(s.size() != len)
548                         throw SerializationError("deSerializeBulkNodes: "
549                                         "decompress resulted in invalid size");
550                 memcpy(&databuf[0], s.c_str(), len);
551         }
552         else
553         {
554                 is.read((char*) &databuf[0], len);
555                 if(is.eof() || is.fail())
556                         throw SerializationError("deSerializeBulkNodes: "
557                                         "failed to read bulk node data");
558         }
559
560         // Deserialize content
561         if(content_width == 1)
562         {
563                 for(u32 i=0; i<nodecount; i++)
564                         nodes[i].param0 = readU8(&databuf[i]);
565         }
566         else if(content_width == 2)
567         {
568                 for(u32 i=0; i<nodecount; i++)
569                         nodes[i].param0 = readU16(&databuf[i*2]);
570         }
571
572         // Deserialize param1
573         u32 start1 = content_width * nodecount;
574         for(u32 i=0; i<nodecount; i++)
575                 nodes[i].param1 = readU8(&databuf[start1 + i]);
576
577         // Deserialize param2
578         u32 start2 = (content_width + 1) * nodecount;
579         if(content_width == 1)
580         {
581                 for(u32 i=0; i<nodecount; i++) {
582                         nodes[i].param2 = readU8(&databuf[start2 + i]);
583                         if(nodes[i].param0 > 0x7F){
584                                 nodes[i].param0 <<= 4;
585                                 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
586                                 nodes[i].param2 &= 0x0F;
587                         }
588                 }
589         }
590         else if(content_width == 2)
591         {
592                 for(u32 i=0; i<nodecount; i++)
593                         nodes[i].param2 = readU8(&databuf[start2 + i]);
594         }
595 }
596
597 /*
598         Legacy serialization
599 */
600 void MapNode::deSerialize_pre22(u8 *source, u8 version)
601 {
602         if(version <= 1)
603         {
604                 param0 = source[0];
605         }
606         else if(version <= 9)
607         {
608                 param0 = source[0];
609                 param1 = source[1];
610         }
611         else
612         {
613                 param0 = source[0];
614                 param1 = source[1];
615                 param2 = source[2];
616                 if(param0 > 0x7f){
617                         param0 <<= 4;
618                         param0 |= (param2&0xf0)>>4;
619                         param2 &= 0x0f;
620                 }
621         }
622         
623         // Convert special values from old version to new
624         if(version <= 19)
625         {
626                 // In these versions, CONTENT_IGNORE and CONTENT_AIR
627                 // are 255 and 254
628                 // Version 19 is fucked up with sometimes the old values and sometimes not
629                 if(param0 == 255)
630                         param0 = CONTENT_IGNORE;
631                 else if(param0 == 254)
632                         param0 = CONTENT_AIR;
633         }
634
635         // Translate to our known version
636         *this = mapnode_translate_to_internal(*this, version);
637 }