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