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
35 static const u8 rot_to_wallmounted[] = {
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)
49 content_t id = CONTENT_IGNORE;
50 ndef->getId(name, id);
56 void MapNode::setLight(enum LightBank bank, u8 a_light, INodeDefManager *nodemgr)
58 // If node doesn't contain light data, ignore this
59 if(nodemgr->get(*this).param_type != CPT_LIGHT)
61 if(bank == LIGHTBANK_DAY)
64 param1 |= a_light & 0x0f;
66 else if(bank == LIGHTBANK_NIGHT)
69 param1 |= (a_light & 0x0f)<<4;
75 u8 MapNode::getLight(enum LightBank bank, INodeDefManager *nodemgr) const
77 // Select the brightest of [light source, propagated light]
78 const ContentFeatures &f = nodemgr->get(*this);
80 if(f.param_type == CPT_LIGHT)
82 if(bank == LIGHTBANK_DAY)
83 light = param1 & 0x0f;
84 else if(bank == LIGHTBANK_NIGHT)
85 light = (param1>>4)&0x0f;
89 if(f.light_source > light)
90 light = f.light_source;
94 bool MapNode::getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const
96 // Select the brightest of [light source, propagated light]
97 const ContentFeatures &f = nodemgr->get(*this);
98 if(f.param_type == CPT_LIGHT)
100 lightday = param1 & 0x0f;
101 lightnight = (param1>>4)&0x0f;
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;
115 u8 MapNode::getFaceDir(INodeDefManager *nodemgr) const
117 const ContentFeatures &f = nodemgr->get(*this);
118 if(f.param_type_2 == CPT2_FACEDIR)
119 return getParam2() & 0x1F;
123 u8 MapNode::getWallMounted(INodeDefManager *nodemgr) const
125 const ContentFeatures &f = nodemgr->get(*this);
126 if(f.param_type_2 == CPT2_WALLMOUNTED)
127 return getParam2() & 0x07;
131 v3s16 MapNode::getWallMountedDir(INodeDefManager *nodemgr) const
133 switch(getWallMounted(nodemgr))
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);
144 void MapNode::rotateAlongYAxis(INodeDefManager *nodemgr, Rotation rot) {
145 ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
147 if (cpt2 == CPT2_FACEDIR) {
148 u8 newrot = param2 & 3;
150 param2 |= (newrot + rot) & 3;
151 } else if (cpt2 == CPT2_WALLMOUNTED) {
152 u8 wmountface = (param2 & 7);
156 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
158 param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
162 static std::vector<aabb3f> transformNodeBox(const MapNode &n,
163 const NodeBox &nodebox, INodeDefManager *nodemgr)
165 std::vector<aabb3f> boxes;
166 if(nodebox.type == NODEBOX_FIXED)
168 const std::vector<aabb3f> &fixed = nodebox.fixed;
169 int facedir = n.getFaceDir(nodemgr);
170 u8 axisdir = facedir>>2;
172 for(std::vector<aabb3f>::const_iterator
174 i != fixed.end(); i++)
182 box.MinEdge.rotateXZBy(-90);
183 box.MaxEdge.rotateXZBy(-90);
185 else if(facedir == 2)
187 box.MinEdge.rotateXZBy(180);
188 box.MaxEdge.rotateXZBy(180);
190 else if(facedir == 3)
192 box.MinEdge.rotateXZBy(90);
193 box.MaxEdge.rotateXZBy(90);
197 box.MinEdge.rotateYZBy(90);
198 box.MaxEdge.rotateYZBy(90);
201 box.MinEdge.rotateXYBy(90);
202 box.MaxEdge.rotateXYBy(90);
204 else if(facedir == 2)
206 box.MinEdge.rotateXYBy(180);
207 box.MaxEdge.rotateXYBy(180);
209 else if(facedir == 3)
211 box.MinEdge.rotateXYBy(-90);
212 box.MaxEdge.rotateXYBy(-90);
216 box.MinEdge.rotateYZBy(-90);
217 box.MaxEdge.rotateYZBy(-90);
220 box.MinEdge.rotateXYBy(-90);
221 box.MaxEdge.rotateXYBy(-90);
223 else if(facedir == 2)
225 box.MinEdge.rotateXYBy(180);
226 box.MaxEdge.rotateXYBy(180);
228 else if(facedir == 3)
230 box.MinEdge.rotateXYBy(90);
231 box.MaxEdge.rotateXYBy(90);
235 box.MinEdge.rotateXYBy(-90);
236 box.MaxEdge.rotateXYBy(-90);
239 box.MinEdge.rotateYZBy(90);
240 box.MaxEdge.rotateYZBy(90);
242 else if(facedir == 2)
244 box.MinEdge.rotateYZBy(180);
245 box.MaxEdge.rotateYZBy(180);
247 else if(facedir == 3)
249 box.MinEdge.rotateYZBy(-90);
250 box.MaxEdge.rotateYZBy(-90);
254 box.MinEdge.rotateXYBy(90);
255 box.MaxEdge.rotateXYBy(90);
258 box.MinEdge.rotateYZBy(-90);
259 box.MaxEdge.rotateYZBy(-90);
261 else if(facedir == 2)
263 box.MinEdge.rotateYZBy(180);
264 box.MaxEdge.rotateYZBy(180);
266 else if(facedir == 3)
268 box.MinEdge.rotateYZBy(90);
269 box.MaxEdge.rotateYZBy(90);
273 box.MinEdge.rotateXYBy(-180);
274 box.MaxEdge.rotateXYBy(-180);
277 box.MinEdge.rotateXZBy(90);
278 box.MaxEdge.rotateXZBy(90);
280 else if(facedir == 2)
282 box.MinEdge.rotateXZBy(180);
283 box.MaxEdge.rotateXZBy(180);
285 else if(facedir == 3)
287 box.MinEdge.rotateXZBy(-90);
288 box.MaxEdge.rotateXZBy(-90);
295 boxes.push_back(box);
298 else if(nodebox.type == NODEBOX_WALLMOUNTED)
300 v3s16 dir = n.getWallMountedDir(nodemgr);
303 if(dir == v3s16(0,1,0))
305 boxes.push_back(nodebox.wall_top);
308 else if(dir == v3s16(0,-1,0))
310 boxes.push_back(nodebox.wall_bottom);
317 nodebox.wall_side.MinEdge,
318 nodebox.wall_side.MaxEdge
321 for(s32 i=0; i<2; i++)
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);
333 aabb3f box = aabb3f(vertices[0]);
334 box.addInternalPoint(vertices[1]);
335 boxes.push_back(box);
338 else // NODEBOX_REGULAR
340 boxes.push_back(aabb3f(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2));
345 std::vector<aabb3f> MapNode::getNodeBoxes(INodeDefManager *nodemgr) const
347 const ContentFeatures &f = nodemgr->get(*this);
348 return transformNodeBox(*this, f.node_box, nodemgr);
351 std::vector<aabb3f> MapNode::getSelectionBoxes(INodeDefManager *nodemgr) const
353 const ContentFeatures &f = nodemgr->get(*this);
354 return transformNodeBox(*this, f.selection_box, nodemgr);
357 u32 MapNode::serializedLength(u8 version)
359 if(!ser_ver_supported(version))
360 throw VersionMismatchException("ERROR: MapNode format not supported");
364 else if(version <= 9)
366 else if(version <= 23)
371 void MapNode::serialize(u8 *dest, u8 version)
373 if(!ser_ver_supported(version))
374 throw VersionMismatchException("ERROR: MapNode format not supported");
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.
379 throw SerializationError("MapNode::serialize: serialization to "
380 "version < 24 not possible");
382 writeU16(dest+0, param0);
383 writeU8(dest+2, param1);
384 writeU8(dest+3, param2);
386 void MapNode::deSerialize(u8 *source, u8 version)
388 if(!ser_ver_supported(version))
389 throw VersionMismatchException("ERROR: MapNode format not supported");
393 deSerialize_pre22(source, version);
398 param0 = readU16(source+0);
399 param1 = readU8(source+2);
400 param2 = readU8(source+3);
403 param0 = readU8(source+0);
404 param1 = readU8(source+1);
405 param2 = readU8(source+2);
407 param0 |= ((param2&0xF0)<<4);
412 void MapNode::serializeBulk(std::ostream &os, int version,
413 const MapNode *nodes, u32 nodecount,
414 u8 content_width, u8 params_width, bool compressed)
416 if(!ser_ver_supported(version))
417 throw VersionMismatchException("ERROR: MapNode format not supported");
419 assert(content_width == 2);
420 assert(params_width == 2);
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.
425 throw SerializationError("MapNode::serializeBulk: serialization to "
426 "version < 24 not possible");
428 SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
431 for(u32 i=0; i<nodecount; i++)
432 writeU16(&databuf[i*2], nodes[i].param0);
435 u32 start1 = content_width * nodecount;
436 for(u32 i=0; i<nodecount; i++)
437 writeU8(&databuf[start1 + i], nodes[i].param1);
440 u32 start2 = (content_width + 1) * nodecount;
441 for(u32 i=0; i<nodecount; i++)
442 writeU8(&databuf[start2 + i], nodes[i].param2);
445 Compress data to output stream
450 compressZlib(databuf, os);
454 os.write((const char*) &databuf[0], databuf.getSize());
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)
463 if(!ser_ver_supported(version))
464 throw VersionMismatchException("ERROR: MapNode format not supported");
466 assert(version >= 22);
467 assert(content_width == 1 || content_width == 2);
468 assert(params_width == 2);
470 // Uncompress or read data
471 u32 len = nodecount * (content_width + params_width);
472 SharedBuffer<u8> databuf(len);
475 std::ostringstream os(std::ios_base::binary);
476 decompressZlib(is, os);
477 std::string s = os.str();
479 throw SerializationError("deSerializeBulkNodes: "
480 "decompress resulted in invalid size");
481 memcpy(&databuf[0], s.c_str(), len);
485 is.read((char*) &databuf[0], len);
486 if(is.eof() || is.fail())
487 throw SerializationError("deSerializeBulkNodes: "
488 "failed to read bulk node data");
491 // Deserialize content
492 if(content_width == 1)
494 for(u32 i=0; i<nodecount; i++)
495 nodes[i].param0 = readU8(&databuf[i]);
497 else if(content_width == 2)
499 for(u32 i=0; i<nodecount; i++)
500 nodes[i].param0 = readU16(&databuf[i*2]);
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]);
508 // Deserialize param2
509 u32 start2 = (content_width + 1) * nodecount;
510 if(content_width == 1)
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;
521 else if(content_width == 2)
523 for(u32 i=0; i<nodecount; i++)
524 nodes[i].param2 = readU8(&databuf[start2 + i]);
531 void MapNode::deSerialize_pre22(u8 *source, u8 version)
537 else if(version <= 9)
549 param0 |= (param2&0xf0)>>4;
554 // Convert special values from old version to new
557 // In these versions, CONTENT_IGNORE and CONTENT_AIR
559 // Version 19 is fucked up with sometimes the old values and sometimes not
561 param0 = CONTENT_IGNORE;
562 else if(param0 == 254)
563 param0 = CONTENT_AIR;
566 // Translate to our known version
567 *this = mapnode_translate_to_internal(*this, version);