3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "irrlichttypes_extrabloated.h"
23 #include "main.h" // For g_settings
25 #include "content_mapnode.h" // For mapnode_translate_*_internal
26 #include "serialization.h" // For ser_ver_supported
27 #include "util/serialize.h"
31 static const Rotation wallmounted_to_rot[] = {
32 ROTATE_0, ROTATE_180, ROTATE_90, ROTATE_270
34 static const u8 rot_to_wallmounted[] = {
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)
48 content_t id = CONTENT_IGNORE;
49 ndef->getId(name, id);
55 void MapNode::setLight(enum LightBank bank, u8 a_light, INodeDefManager *nodemgr)
57 // If node doesn't contain light data, ignore this
58 if(nodemgr->get(*this).param_type != CPT_LIGHT)
60 if(bank == LIGHTBANK_DAY)
63 param1 |= a_light & 0x0f;
65 else if(bank == LIGHTBANK_NIGHT)
68 param1 |= (a_light & 0x0f)<<4;
74 u8 MapNode::getLight(enum LightBank bank, INodeDefManager *nodemgr) const
76 // Select the brightest of [light source, propagated light]
77 const ContentFeatures &f = nodemgr->get(*this);
79 if(f.param_type == CPT_LIGHT)
81 if(bank == LIGHTBANK_DAY)
82 light = param1 & 0x0f;
83 else if(bank == LIGHTBANK_NIGHT)
84 light = (param1>>4)&0x0f;
88 if(f.light_source > light)
89 light = f.light_source;
93 bool MapNode::getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const
95 // Select the brightest of [light source, propagated light]
96 const ContentFeatures &f = nodemgr->get(*this);
97 if(f.param_type == CPT_LIGHT)
99 lightday = param1 & 0x0f;
100 lightnight = (param1>>4)&0x0f;
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;
114 u8 MapNode::getFaceDir(INodeDefManager *nodemgr) const
116 const ContentFeatures &f = nodemgr->get(*this);
117 if(f.param_type_2 == CPT2_FACEDIR)
118 return getParam2() & 0x1F;
122 u8 MapNode::getWallMounted(INodeDefManager *nodemgr) const
124 const ContentFeatures &f = nodemgr->get(*this);
125 if(f.param_type_2 == CPT2_WALLMOUNTED)
126 return getParam2() & 0x07;
130 v3s16 MapNode::getWallMountedDir(INodeDefManager *nodemgr) const
132 switch(getWallMounted(nodemgr))
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);
143 void MapNode::rotateAlongYAxis(INodeDefManager *nodemgr, Rotation rot) {
144 ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
146 if (cpt2 == CPT2_FACEDIR) {
147 u8 newrot = param2 & 3;
149 param2 |= (newrot + rot) & 3;
150 } else if (cpt2 == CPT2_WALLMOUNTED) {
151 u8 wmountface = (param2 & 7);
155 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
157 param2 |= rot_to_wallmounted[(oldrot + rot) & 3];
161 static std::vector<aabb3f> transformNodeBox(const MapNode &n,
162 const NodeBox &nodebox, INodeDefManager *nodemgr)
164 std::vector<aabb3f> boxes;
165 if(nodebox.type == NODEBOX_FIXED)
167 const std::vector<aabb3f> &fixed = nodebox.fixed;
168 int facedir = n.getFaceDir(nodemgr);
169 u8 axisdir = facedir>>2;
171 for(std::vector<aabb3f>::const_iterator
173 i != fixed.end(); i++)
181 box.MinEdge.rotateXZBy(-90);
182 box.MaxEdge.rotateXZBy(-90);
184 else if(facedir == 2)
186 box.MinEdge.rotateXZBy(180);
187 box.MaxEdge.rotateXZBy(180);
189 else if(facedir == 3)
191 box.MinEdge.rotateXZBy(90);
192 box.MaxEdge.rotateXZBy(90);
196 box.MinEdge.rotateYZBy(90);
197 box.MaxEdge.rotateYZBy(90);
200 box.MinEdge.rotateXYBy(90);
201 box.MaxEdge.rotateXYBy(90);
203 else if(facedir == 2)
205 box.MinEdge.rotateXYBy(180);
206 box.MaxEdge.rotateXYBy(180);
208 else if(facedir == 3)
210 box.MinEdge.rotateXYBy(-90);
211 box.MaxEdge.rotateXYBy(-90);
215 box.MinEdge.rotateYZBy(-90);
216 box.MaxEdge.rotateYZBy(-90);
219 box.MinEdge.rotateXYBy(-90);
220 box.MaxEdge.rotateXYBy(-90);
222 else if(facedir == 2)
224 box.MinEdge.rotateXYBy(180);
225 box.MaxEdge.rotateXYBy(180);
227 else if(facedir == 3)
229 box.MinEdge.rotateXYBy(90);
230 box.MaxEdge.rotateXYBy(90);
234 box.MinEdge.rotateXYBy(-90);
235 box.MaxEdge.rotateXYBy(-90);
238 box.MinEdge.rotateYZBy(90);
239 box.MaxEdge.rotateYZBy(90);
241 else if(facedir == 2)
243 box.MinEdge.rotateYZBy(180);
244 box.MaxEdge.rotateYZBy(180);
246 else if(facedir == 3)
248 box.MinEdge.rotateYZBy(-90);
249 box.MaxEdge.rotateYZBy(-90);
253 box.MinEdge.rotateXYBy(90);
254 box.MaxEdge.rotateXYBy(90);
257 box.MinEdge.rotateYZBy(-90);
258 box.MaxEdge.rotateYZBy(-90);
260 else if(facedir == 2)
262 box.MinEdge.rotateYZBy(180);
263 box.MaxEdge.rotateYZBy(180);
265 else if(facedir == 3)
267 box.MinEdge.rotateYZBy(90);
268 box.MaxEdge.rotateYZBy(90);
272 box.MinEdge.rotateXYBy(-180);
273 box.MaxEdge.rotateXYBy(-180);
276 box.MinEdge.rotateXZBy(90);
277 box.MaxEdge.rotateXZBy(90);
279 else if(facedir == 2)
281 box.MinEdge.rotateXZBy(180);
282 box.MaxEdge.rotateXZBy(180);
284 else if(facedir == 3)
286 box.MinEdge.rotateXZBy(-90);
287 box.MaxEdge.rotateXZBy(-90);
294 boxes.push_back(box);
297 else if(nodebox.type == NODEBOX_WALLMOUNTED)
299 v3s16 dir = n.getWallMountedDir(nodemgr);
302 if(dir == v3s16(0,1,0))
304 boxes.push_back(nodebox.wall_top);
307 else if(dir == v3s16(0,-1,0))
309 boxes.push_back(nodebox.wall_bottom);
316 nodebox.wall_side.MinEdge,
317 nodebox.wall_side.MaxEdge
320 for(s32 i=0; i<2; i++)
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);
332 aabb3f box = aabb3f(vertices[0]);
333 box.addInternalPoint(vertices[1]);
334 boxes.push_back(box);
337 else // NODEBOX_REGULAR
339 boxes.push_back(aabb3f(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2));
344 std::vector<aabb3f> MapNode::getNodeBoxes(INodeDefManager *nodemgr) const
346 const ContentFeatures &f = nodemgr->get(*this);
347 return transformNodeBox(*this, f.node_box, nodemgr);
350 std::vector<aabb3f> MapNode::getSelectionBoxes(INodeDefManager *nodemgr) const
352 const ContentFeatures &f = nodemgr->get(*this);
353 return transformNodeBox(*this, f.selection_box, nodemgr);
356 u32 MapNode::serializedLength(u8 version)
358 if(!ser_ver_supported(version))
359 throw VersionMismatchException("ERROR: MapNode format not supported");
363 else if(version <= 9)
365 else if(version <= 23)
370 void MapNode::serialize(u8 *dest, u8 version)
372 if(!ser_ver_supported(version))
373 throw VersionMismatchException("ERROR: MapNode format not supported");
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.
378 throw SerializationError("MapNode::serialize: serialization to "
379 "version < 24 not possible");
381 writeU16(dest+0, param0);
382 writeU8(dest+2, param1);
383 writeU8(dest+3, param2);
385 void MapNode::deSerialize(u8 *source, u8 version)
387 if(!ser_ver_supported(version))
388 throw VersionMismatchException("ERROR: MapNode format not supported");
392 deSerialize_pre22(source, version);
397 param0 = readU16(source+0);
398 param1 = readU8(source+2);
399 param2 = readU8(source+3);
402 param0 = readU8(source+0);
403 param1 = readU8(source+1);
404 param2 = readU8(source+2);
406 param0 |= ((param2&0xF0)<<4);
411 void MapNode::serializeBulk(std::ostream &os, int version,
412 const MapNode *nodes, u32 nodecount,
413 u8 content_width, u8 params_width, bool compressed)
415 if(!ser_ver_supported(version))
416 throw VersionMismatchException("ERROR: MapNode format not supported");
418 assert(content_width == 2);
419 assert(params_width == 2);
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.
424 throw SerializationError("MapNode::serializeBulk: serialization to "
425 "version < 24 not possible");
427 SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
430 for(u32 i=0; i<nodecount; i++)
431 writeU16(&databuf[i*2], nodes[i].param0);
434 u32 start1 = content_width * nodecount;
435 for(u32 i=0; i<nodecount; i++)
436 writeU8(&databuf[start1 + i], nodes[i].param1);
439 u32 start2 = (content_width + 1) * nodecount;
440 for(u32 i=0; i<nodecount; i++)
441 writeU8(&databuf[start2 + i], nodes[i].param2);
444 Compress data to output stream
449 compressZlib(databuf, os);
453 os.write((const char*) &databuf[0], databuf.getSize());
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)
462 if(!ser_ver_supported(version))
463 throw VersionMismatchException("ERROR: MapNode format not supported");
465 assert(version >= 22);
466 assert(content_width == 1 || content_width == 2);
467 assert(params_width == 2);
469 // Uncompress or read data
470 u32 len = nodecount * (content_width + params_width);
471 SharedBuffer<u8> databuf(len);
474 std::ostringstream os(std::ios_base::binary);
475 decompressZlib(is, os);
476 std::string s = os.str();
478 throw SerializationError("deSerializeBulkNodes: "
479 "decompress resulted in invalid size");
480 memcpy(&databuf[0], s.c_str(), len);
484 is.read((char*) &databuf[0], len);
485 if(is.eof() || is.fail())
486 throw SerializationError("deSerializeBulkNodes: "
487 "failed to read bulk node data");
490 // Deserialize content
491 if(content_width == 1)
493 for(u32 i=0; i<nodecount; i++)
494 nodes[i].param0 = readU8(&databuf[i]);
496 else if(content_width == 2)
498 for(u32 i=0; i<nodecount; i++)
499 nodes[i].param0 = readU16(&databuf[i*2]);
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]);
507 // Deserialize param2
508 u32 start2 = (content_width + 1) * nodecount;
509 if(content_width == 1)
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;
520 else if(content_width == 2)
522 for(u32 i=0; i<nodecount; i++)
523 nodes[i].param2 = readU8(&databuf[start2 + i]);
530 void MapNode::deSerialize_pre22(u8 *source, u8 version)
536 else if(version <= 9)
548 param0 |= (param2&0xf0)>>4;
553 // Convert special values from old version to new
556 // In these versions, CONTENT_IGNORE and CONTENT_AIR
558 // Version 19 is fucked up with sometimes the old values and sometimes not
560 param0 = CONTENT_IGNORE;
561 else if(param0 == 254)
562 param0 = CONTENT_AIR;
565 // Translate to our known version
566 *this = mapnode_translate_to_internal(*this, version);