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