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