Fix wallmounted rotations along Y axis for 90 and 270 degree cases
[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)
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                         switch (axisdir)
178                         {
179                         case 0:
180                                 if(facedir == 1)
181                                 {
182                                         box.MinEdge.rotateXZBy(-90);
183                                         box.MaxEdge.rotateXZBy(-90);
184                                 }
185                                 else if(facedir == 2)
186                                 {
187                                         box.MinEdge.rotateXZBy(180);
188                                         box.MaxEdge.rotateXZBy(180);
189                                 }
190                                 else if(facedir == 3)
191                                 {
192                                         box.MinEdge.rotateXZBy(90);
193                                         box.MaxEdge.rotateXZBy(90);
194                                 }
195                                 break;
196                         case 1: // z+
197                                 box.MinEdge.rotateYZBy(90);
198                                 box.MaxEdge.rotateYZBy(90);
199                                 if(facedir == 1)
200                                 {
201                                         box.MinEdge.rotateXYBy(90);
202                                         box.MaxEdge.rotateXYBy(90);
203                                 }
204                                 else if(facedir == 2)
205                                 {
206                                         box.MinEdge.rotateXYBy(180);
207                                         box.MaxEdge.rotateXYBy(180);
208                                 }
209                                 else if(facedir == 3)
210                                 {
211                                         box.MinEdge.rotateXYBy(-90);
212                                         box.MaxEdge.rotateXYBy(-90);
213                                 }
214                                 break;
215                         case 2: //z-
216                                 box.MinEdge.rotateYZBy(-90);
217                                 box.MaxEdge.rotateYZBy(-90);
218                                 if(facedir == 1)
219                                 {
220                                         box.MinEdge.rotateXYBy(-90);
221                                         box.MaxEdge.rotateXYBy(-90);
222                                 }
223                                 else if(facedir == 2)
224                                 {
225                                         box.MinEdge.rotateXYBy(180);
226                                         box.MaxEdge.rotateXYBy(180);
227                                 }
228                                 else if(facedir == 3)
229                                 {
230                                         box.MinEdge.rotateXYBy(90);
231                                         box.MaxEdge.rotateXYBy(90);
232                                 }
233                                 break;
234                         case 3:  //x+
235                                 box.MinEdge.rotateXYBy(-90);
236                                 box.MaxEdge.rotateXYBy(-90);
237                                 if(facedir == 1)
238                                 {
239                                         box.MinEdge.rotateYZBy(90);
240                                         box.MaxEdge.rotateYZBy(90);
241                                 }
242                                 else if(facedir == 2)
243                                 {
244                                         box.MinEdge.rotateYZBy(180);
245                                         box.MaxEdge.rotateYZBy(180);
246                                 }
247                                 else if(facedir == 3)
248                                 {
249                                         box.MinEdge.rotateYZBy(-90);
250                                         box.MaxEdge.rotateYZBy(-90);
251                                 }
252                                 break;
253                         case 4:  //x-
254                                 box.MinEdge.rotateXYBy(90);
255                                 box.MaxEdge.rotateXYBy(90);
256                                 if(facedir == 1)
257                                 {
258                                         box.MinEdge.rotateYZBy(-90);
259                                         box.MaxEdge.rotateYZBy(-90);
260                                 }
261                                 else if(facedir == 2)
262                                 {
263                                         box.MinEdge.rotateYZBy(180);
264                                         box.MaxEdge.rotateYZBy(180);
265                                 }
266                                 else if(facedir == 3)
267                                 {
268                                         box.MinEdge.rotateYZBy(90);
269                                         box.MaxEdge.rotateYZBy(90);
270                                 }
271                                 break;
272                         case 5:
273                                 box.MinEdge.rotateXYBy(-180);
274                                 box.MaxEdge.rotateXYBy(-180);
275                                 if(facedir == 1)
276                                 {
277                                         box.MinEdge.rotateXZBy(90);
278                                         box.MaxEdge.rotateXZBy(90);
279                                 }
280                                 else if(facedir == 2)
281                                 {
282                                         box.MinEdge.rotateXZBy(180);
283                                         box.MaxEdge.rotateXZBy(180);
284                                 }
285                                 else if(facedir == 3)
286                                 {
287                                         box.MinEdge.rotateXZBy(-90);
288                                         box.MaxEdge.rotateXZBy(-90);
289                                 }
290                                 break;
291                         default:
292                                 break;
293                         }
294                         box.repair();
295                         boxes.push_back(box);
296                 }
297         }
298         else if(nodebox.type == NODEBOX_WALLMOUNTED)
299         {
300                 v3s16 dir = n.getWallMountedDir(nodemgr);
301
302                 // top
303                 if(dir == v3s16(0,1,0))
304                 {
305                         boxes.push_back(nodebox.wall_top);
306                 }
307                 // bottom
308                 else if(dir == v3s16(0,-1,0))
309                 {
310                         boxes.push_back(nodebox.wall_bottom);
311                 }
312                 // side
313                 else
314                 {
315                         v3f vertices[2] =
316                         {
317                                 nodebox.wall_side.MinEdge,
318                                 nodebox.wall_side.MaxEdge
319                         };
320
321                         for(s32 i=0; i<2; i++)
322                         {
323                                 if(dir == v3s16(-1,0,0))
324                                         vertices[i].rotateXZBy(0);
325                                 if(dir == v3s16(1,0,0))
326                                         vertices[i].rotateXZBy(180);
327                                 if(dir == v3s16(0,0,-1))
328                                         vertices[i].rotateXZBy(90);
329                                 if(dir == v3s16(0,0,1))
330                                         vertices[i].rotateXZBy(-90);
331                         }
332
333                         aabb3f box = aabb3f(vertices[0]);
334                         box.addInternalPoint(vertices[1]);
335                         boxes.push_back(box);
336                 }
337         }
338         else // NODEBOX_REGULAR
339         {
340                 boxes.push_back(aabb3f(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2));
341         }
342         return boxes;
343 }
344
345 std::vector<aabb3f> MapNode::getNodeBoxes(INodeDefManager *nodemgr) const
346 {
347         const ContentFeatures &f = nodemgr->get(*this);
348         return transformNodeBox(*this, f.node_box, nodemgr);
349 }
350
351 std::vector<aabb3f> MapNode::getSelectionBoxes(INodeDefManager *nodemgr) const
352 {
353         const ContentFeatures &f = nodemgr->get(*this);
354         return transformNodeBox(*this, f.selection_box, nodemgr);
355 }
356
357 u32 MapNode::serializedLength(u8 version)
358 {
359         if(!ser_ver_supported(version))
360                 throw VersionMismatchException("ERROR: MapNode format not supported");
361                 
362         if(version == 0)
363                 return 1;
364         else if(version <= 9)
365                 return 2;
366         else if(version <= 23)
367                 return 3;
368         else
369                 return 4;
370 }
371 void MapNode::serialize(u8 *dest, u8 version)
372 {
373         if(!ser_ver_supported(version))
374                 throw VersionMismatchException("ERROR: MapNode format not supported");
375         
376         // Can't do this anymore; we have 16-bit dynamically allocated node IDs
377         // in memory; conversion just won't work in this direction.
378         if(version < 24)
379                 throw SerializationError("MapNode::serialize: serialization to "
380                                 "version < 24 not possible");
381                 
382         writeU16(dest+0, param0);
383         writeU8(dest+2, param1);
384         writeU8(dest+3, param2);
385 }
386 void MapNode::deSerialize(u8 *source, u8 version)
387 {
388         if(!ser_ver_supported(version))
389                 throw VersionMismatchException("ERROR: MapNode format not supported");
390                 
391         if(version <= 21)
392         {
393                 deSerialize_pre22(source, version);
394                 return;
395         }
396
397         if(version >= 24){
398                 param0 = readU16(source+0);
399                 param1 = readU8(source+2);
400                 param2 = readU8(source+3);
401         }
402         else{
403                 param0 = readU8(source+0);
404                 param1 = readU8(source+1);
405                 param2 = readU8(source+2);
406                 if(param0 > 0x7F){
407                         param0 |= ((param2&0xF0)<<4);
408                         param2 &= 0x0F;
409                 }
410         }
411 }
412 void MapNode::serializeBulk(std::ostream &os, int version,
413                 const MapNode *nodes, u32 nodecount,
414                 u8 content_width, u8 params_width, bool compressed)
415 {
416         if(!ser_ver_supported(version))
417                 throw VersionMismatchException("ERROR: MapNode format not supported");
418
419         assert(content_width == 2);
420         assert(params_width == 2);
421
422         // Can't do this anymore; we have 16-bit dynamically allocated node IDs
423         // in memory; conversion just won't work in this direction.
424         if(version < 24)
425                 throw SerializationError("MapNode::serializeBulk: serialization to "
426                                 "version < 24 not possible");
427
428         SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
429
430         // Serialize content
431         for(u32 i=0; i<nodecount; i++)
432                 writeU16(&databuf[i*2], nodes[i].param0);
433
434         // Serialize param1
435         u32 start1 = content_width * nodecount;
436         for(u32 i=0; i<nodecount; i++)
437                 writeU8(&databuf[start1 + i], nodes[i].param1);
438
439         // Serialize param2
440         u32 start2 = (content_width + 1) * nodecount;
441         for(u32 i=0; i<nodecount; i++)
442                 writeU8(&databuf[start2 + i], nodes[i].param2);
443
444         /*
445                 Compress data to output stream
446         */
447
448         if(compressed)
449         {
450                 compressZlib(databuf, os);
451         }
452         else
453         {
454                 os.write((const char*) &databuf[0], databuf.getSize());
455         }
456 }
457
458 // Deserialize bulk node data
459 void MapNode::deSerializeBulk(std::istream &is, int version,
460                 MapNode *nodes, u32 nodecount,
461                 u8 content_width, u8 params_width, bool compressed)
462 {
463         if(!ser_ver_supported(version))
464                 throw VersionMismatchException("ERROR: MapNode format not supported");
465
466         assert(version >= 22);
467         assert(content_width == 1 || content_width == 2);
468         assert(params_width == 2);
469
470         // Uncompress or read data
471         u32 len = nodecount * (content_width + params_width);
472         SharedBuffer<u8> databuf(len);
473         if(compressed)
474         {
475                 std::ostringstream os(std::ios_base::binary);
476                 decompressZlib(is, os);
477                 std::string s = os.str();
478                 if(s.size() != len)
479                         throw SerializationError("deSerializeBulkNodes: "
480                                         "decompress resulted in invalid size");
481                 memcpy(&databuf[0], s.c_str(), len);
482         }
483         else
484         {
485                 is.read((char*) &databuf[0], len);
486                 if(is.eof() || is.fail())
487                         throw SerializationError("deSerializeBulkNodes: "
488                                         "failed to read bulk node data");
489         }
490
491         // Deserialize content
492         if(content_width == 1)
493         {
494                 for(u32 i=0; i<nodecount; i++)
495                         nodes[i].param0 = readU8(&databuf[i]);
496         }
497         else if(content_width == 2)
498         {
499                 for(u32 i=0; i<nodecount; i++)
500                         nodes[i].param0 = readU16(&databuf[i*2]);
501         }
502
503         // Deserialize param1
504         u32 start1 = content_width * nodecount;
505         for(u32 i=0; i<nodecount; i++)
506                 nodes[i].param1 = readU8(&databuf[start1 + i]);
507
508         // Deserialize param2
509         u32 start2 = (content_width + 1) * nodecount;
510         if(content_width == 1)
511         {
512                 for(u32 i=0; i<nodecount; i++) {
513                         nodes[i].param2 = readU8(&databuf[start2 + i]);
514                         if(nodes[i].param0 > 0x7F){
515                                 nodes[i].param0 <<= 4;
516                                 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
517                                 nodes[i].param2 &= 0x0F;
518                         }
519                 }
520         }
521         else if(content_width == 2)
522         {
523                 for(u32 i=0; i<nodecount; i++)
524                         nodes[i].param2 = readU8(&databuf[start2 + i]);
525         }
526 }
527
528 /*
529         Legacy serialization
530 */
531 void MapNode::deSerialize_pre22(u8 *source, u8 version)
532 {
533         if(version <= 1)
534         {
535                 param0 = source[0];
536         }
537         else if(version <= 9)
538         {
539                 param0 = source[0];
540                 param1 = source[1];
541         }
542         else
543         {
544                 param0 = source[0];
545                 param1 = source[1];
546                 param2 = source[2];
547                 if(param0 > 0x7f){
548                         param0 <<= 4;
549                         param0 |= (param2&0xf0)>>4;
550                         param2 &= 0x0f;
551                 }
552         }
553         
554         // Convert special values from old version to new
555         if(version <= 19)
556         {
557                 // In these versions, CONTENT_IGNORE and CONTENT_AIR
558                 // are 255 and 254
559                 // Version 19 is fucked up with sometimes the old values and sometimes not
560                 if(param0 == 255)
561                         param0 = CONTENT_IGNORE;
562                 else if(param0 == 254)
563                         param0 = CONTENT_AIR;
564         }
565
566         // Translate to our known version
567         *this = mapnode_translate_to_internal(*this, version);
568 }