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"
25 #include "serialization.h" // For ser_ver_supported
26 #include "util/serialize.h"
28 #include "util/numeric.h"
32 static const Rotation wallmounted_to_rot[] = {
33 ROTATE_0, ROTATE_180, ROTATE_90, ROTATE_270
36 static const u8 rot_to_wallmounted[] = {
45 // Create directly from a nodename
46 // If name is unknown, sets CONTENT_IGNORE
47 MapNode::MapNode(INodeDefManager *ndef, const std::string &name,
48 u8 a_param1, u8 a_param2)
50 content_t id = CONTENT_IGNORE;
51 ndef->getId(name, id);
57 void MapNode::getColor(const ContentFeatures &f, video::SColor *color) const
60 *color = (*f.palette)[param2];
66 void MapNode::setLight(enum LightBank bank, u8 a_light, const ContentFeatures &f)
68 // If node doesn't contain light data, ignore this
69 if(f.param_type != CPT_LIGHT)
71 if(bank == LIGHTBANK_DAY)
74 param1 |= a_light & 0x0f;
76 else if(bank == LIGHTBANK_NIGHT)
79 param1 |= (a_light & 0x0f)<<4;
82 assert("Invalid light bank" == NULL);
85 void MapNode::setLight(enum LightBank bank, u8 a_light, INodeDefManager *nodemgr)
87 setLight(bank, a_light, nodemgr->get(*this));
90 bool MapNode::isLightDayNightEq(INodeDefManager *nodemgr) const
92 const ContentFeatures &f = nodemgr->get(*this);
95 if (f.param_type == CPT_LIGHT) {
96 u8 day = MYMAX(f.light_source, param1 & 0x0f);
97 u8 night = MYMAX(f.light_source, (param1 >> 4) & 0x0f);
98 isEqual = day == night;
106 u8 MapNode::getLight(enum LightBank bank, INodeDefManager *nodemgr) const
108 // Select the brightest of [light source, propagated light]
109 const ContentFeatures &f = nodemgr->get(*this);
112 if(f.param_type == CPT_LIGHT)
113 light = bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f;
117 return MYMAX(f.light_source, light);
120 u8 MapNode::getLightRaw(enum LightBank bank, const ContentFeatures &f) const
122 if(f.param_type == CPT_LIGHT)
123 return bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f;
127 u8 MapNode::getLightNoChecks(enum LightBank bank, const ContentFeatures *f) const
129 return MYMAX(f->light_source,
130 bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f);
133 bool MapNode::getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const
135 // Select the brightest of [light source, propagated light]
136 const ContentFeatures &f = nodemgr->get(*this);
137 if(f.param_type == CPT_LIGHT)
139 lightday = param1 & 0x0f;
140 lightnight = (param1>>4)&0x0f;
147 if(f.light_source > lightday)
148 lightday = f.light_source;
149 if(f.light_source > lightnight)
150 lightnight = f.light_source;
151 return f.param_type == CPT_LIGHT || f.light_source != 0;
154 u8 MapNode::getFaceDir(INodeDefManager *nodemgr) const
156 const ContentFeatures &f = nodemgr->get(*this);
157 if (f.param_type_2 == CPT2_FACEDIR ||
158 f.param_type_2 == CPT2_COLORED_FACEDIR)
159 return (getParam2() & 0x1F) % 24;
163 u8 MapNode::getWallMounted(INodeDefManager *nodemgr) const
165 const ContentFeatures &f = nodemgr->get(*this);
166 if (f.param_type_2 == CPT2_WALLMOUNTED ||
167 f.param_type_2 == CPT2_COLORED_WALLMOUNTED)
168 return getParam2() & 0x07;
172 v3s16 MapNode::getWallMountedDir(INodeDefManager *nodemgr) const
174 switch(getWallMounted(nodemgr))
176 case 0: default: return v3s16(0,1,0);
177 case 1: return v3s16(0,-1,0);
178 case 2: return v3s16(1,0,0);
179 case 3: return v3s16(-1,0,0);
180 case 4: return v3s16(0,0,1);
181 case 5: return v3s16(0,0,-1);
185 void MapNode::rotateAlongYAxis(INodeDefManager *nodemgr, Rotation rot)
187 ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
189 if (cpt2 == CPT2_FACEDIR || cpt2 == CPT2_COLORED_FACEDIR) {
190 static const u8 rotate_facedir[24 * 4] = {
191 // Table value = rotated facedir
192 // Columns: 0, 90, 180, 270 degrees rotation around vertical axis
193 // Rotation is anticlockwise as seen from above (+Y)
195 0, 1, 2, 3, // Initial facedir 0 to 3
200 4, 13, 10, 19, // 4 to 7
205 8, 17, 6, 15, // 8 to 11
210 12, 9, 18, 7, // 12 to 15
215 16, 5, 14, 11, // 16 to 19
220 20, 23, 22, 21, // 20 to 23
225 u8 facedir = (param2 & 31) % 24;
226 u8 index = facedir * 4 + rot;
228 param2 |= rotate_facedir[index];
229 } else if (cpt2 == CPT2_WALLMOUNTED ||
230 cpt2 == CPT2_COLORED_WALLMOUNTED) {
231 u8 wmountface = (param2 & 7);
235 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
237 param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
241 void transformNodeBox(const MapNode &n, const NodeBox &nodebox,
242 INodeDefManager *nodemgr, std::vector<aabb3f> *p_boxes, u8 neighbors = 0)
244 std::vector<aabb3f> &boxes = *p_boxes;
246 if (nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED) {
247 const std::vector<aabb3f> &fixed = nodebox.fixed;
248 int facedir = n.getFaceDir(nodemgr);
249 u8 axisdir = facedir>>2;
251 for(std::vector<aabb3f>::const_iterator
253 i != fixed.end(); ++i)
257 if (nodebox.type == NODEBOX_LEVELED) {
258 box.MaxEdge.Y = -BS/2 + BS*((float)1/LEVELED_MAX) * n.getLevel(nodemgr);
266 box.MinEdge.rotateXZBy(-90);
267 box.MaxEdge.rotateXZBy(-90);
269 else if(facedir == 2)
271 box.MinEdge.rotateXZBy(180);
272 box.MaxEdge.rotateXZBy(180);
274 else if(facedir == 3)
276 box.MinEdge.rotateXZBy(90);
277 box.MaxEdge.rotateXZBy(90);
281 box.MinEdge.rotateYZBy(90);
282 box.MaxEdge.rotateYZBy(90);
285 box.MinEdge.rotateXYBy(90);
286 box.MaxEdge.rotateXYBy(90);
288 else if(facedir == 2)
290 box.MinEdge.rotateXYBy(180);
291 box.MaxEdge.rotateXYBy(180);
293 else if(facedir == 3)
295 box.MinEdge.rotateXYBy(-90);
296 box.MaxEdge.rotateXYBy(-90);
300 box.MinEdge.rotateYZBy(-90);
301 box.MaxEdge.rotateYZBy(-90);
304 box.MinEdge.rotateXYBy(-90);
305 box.MaxEdge.rotateXYBy(-90);
307 else if(facedir == 2)
309 box.MinEdge.rotateXYBy(180);
310 box.MaxEdge.rotateXYBy(180);
312 else if(facedir == 3)
314 box.MinEdge.rotateXYBy(90);
315 box.MaxEdge.rotateXYBy(90);
319 box.MinEdge.rotateXYBy(-90);
320 box.MaxEdge.rotateXYBy(-90);
323 box.MinEdge.rotateYZBy(90);
324 box.MaxEdge.rotateYZBy(90);
326 else if(facedir == 2)
328 box.MinEdge.rotateYZBy(180);
329 box.MaxEdge.rotateYZBy(180);
331 else if(facedir == 3)
333 box.MinEdge.rotateYZBy(-90);
334 box.MaxEdge.rotateYZBy(-90);
338 box.MinEdge.rotateXYBy(90);
339 box.MaxEdge.rotateXYBy(90);
342 box.MinEdge.rotateYZBy(-90);
343 box.MaxEdge.rotateYZBy(-90);
345 else if(facedir == 2)
347 box.MinEdge.rotateYZBy(180);
348 box.MaxEdge.rotateYZBy(180);
350 else if(facedir == 3)
352 box.MinEdge.rotateYZBy(90);
353 box.MaxEdge.rotateYZBy(90);
357 box.MinEdge.rotateXYBy(-180);
358 box.MaxEdge.rotateXYBy(-180);
361 box.MinEdge.rotateXZBy(90);
362 box.MaxEdge.rotateXZBy(90);
364 else if(facedir == 2)
366 box.MinEdge.rotateXZBy(180);
367 box.MaxEdge.rotateXZBy(180);
369 else if(facedir == 3)
371 box.MinEdge.rotateXZBy(-90);
372 box.MaxEdge.rotateXZBy(-90);
379 boxes.push_back(box);
382 else if(nodebox.type == NODEBOX_WALLMOUNTED)
384 v3s16 dir = n.getWallMountedDir(nodemgr);
387 if(dir == v3s16(0,1,0))
389 boxes.push_back(nodebox.wall_top);
392 else if(dir == v3s16(0,-1,0))
394 boxes.push_back(nodebox.wall_bottom);
401 nodebox.wall_side.MinEdge,
402 nodebox.wall_side.MaxEdge
405 for(s32 i=0; i<2; i++)
407 if(dir == v3s16(-1,0,0))
408 vertices[i].rotateXZBy(0);
409 if(dir == v3s16(1,0,0))
410 vertices[i].rotateXZBy(180);
411 if(dir == v3s16(0,0,-1))
412 vertices[i].rotateXZBy(90);
413 if(dir == v3s16(0,0,1))
414 vertices[i].rotateXZBy(-90);
417 aabb3f box = aabb3f(vertices[0]);
418 box.addInternalPoint(vertices[1]);
419 boxes.push_back(box);
422 else if (nodebox.type == NODEBOX_CONNECTED)
424 size_t boxes_size = boxes.size();
425 boxes_size += nodebox.fixed.size();
427 boxes_size += nodebox.connect_top.size();
429 boxes_size += nodebox.connect_bottom.size();
431 boxes_size += nodebox.connect_front.size();
433 boxes_size += nodebox.connect_left.size();
435 boxes_size += nodebox.connect_back.size();
437 boxes_size += nodebox.connect_right.size();
438 boxes.reserve(boxes_size);
440 #define BOXESPUSHBACK(c) do { \
441 for (std::vector<aabb3f>::const_iterator \
443 it != (c).end(); ++it) \
444 (boxes).push_back(*it); \
447 BOXESPUSHBACK(nodebox.fixed);
450 BOXESPUSHBACK(nodebox.connect_top);
452 BOXESPUSHBACK(nodebox.connect_bottom);
454 BOXESPUSHBACK(nodebox.connect_front);
456 BOXESPUSHBACK(nodebox.connect_left);
458 BOXESPUSHBACK(nodebox.connect_back);
460 BOXESPUSHBACK(nodebox.connect_right);
462 else // NODEBOX_REGULAR
464 boxes.push_back(aabb3f(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2));
468 static inline void getNeighborConnectingFace(
469 v3s16 p, INodeDefManager *nodedef,
470 Map *map, MapNode n, u8 bitmask, u8 *neighbors)
472 MapNode n2 = map->getNodeNoEx(p);
473 if (nodedef->nodeboxConnects(n, n2, bitmask))
474 *neighbors |= bitmask;
477 u8 MapNode::getNeighbors(v3s16 p, Map *map)
479 INodeDefManager *nodedef=map->getNodeDefManager();
481 const ContentFeatures &f = nodedef->get(*this);
482 // locate possible neighboring nodes to connect to
483 if (f.drawtype == NDT_NODEBOX && f.node_box.type == NODEBOX_CONNECTED) {
487 getNeighborConnectingFace(p2, nodedef, map, *this, 1, &neighbors);
491 getNeighborConnectingFace(p2, nodedef, map, *this, 2, &neighbors);
495 getNeighborConnectingFace(p2, nodedef, map, *this, 4, &neighbors);
499 getNeighborConnectingFace(p2, nodedef, map, *this, 8, &neighbors);
503 getNeighborConnectingFace(p2, nodedef, map, *this, 16, &neighbors);
507 getNeighborConnectingFace(p2, nodedef, map, *this, 32, &neighbors);
513 void MapNode::getNodeBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes, u8 neighbors)
515 const ContentFeatures &f = nodemgr->get(*this);
516 transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
519 void MapNode::getCollisionBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes, u8 neighbors)
521 const ContentFeatures &f = nodemgr->get(*this);
522 if (f.collision_box.fixed.empty())
523 transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
525 transformNodeBox(*this, f.collision_box, nodemgr, boxes, neighbors);
528 void MapNode::getSelectionBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes, u8 neighbors)
530 const ContentFeatures &f = nodemgr->get(*this);
531 transformNodeBox(*this, f.selection_box, nodemgr, boxes, neighbors);
534 u8 MapNode::getMaxLevel(INodeDefManager *nodemgr) const
536 const ContentFeatures &f = nodemgr->get(*this);
537 // todo: after update in all games leave only if (f.param_type_2 ==
538 if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
539 return LIQUID_LEVEL_MAX;
540 if(f.leveled || f.param_type_2 == CPT2_LEVELED)
545 u8 MapNode::getLevel(INodeDefManager *nodemgr) const
547 const ContentFeatures &f = nodemgr->get(*this);
548 // todo: after update in all games leave only if (f.param_type_2 ==
549 if(f.liquid_type == LIQUID_SOURCE)
550 return LIQUID_LEVEL_SOURCE;
551 if (f.param_type_2 == CPT2_FLOWINGLIQUID)
552 return getParam2() & LIQUID_LEVEL_MASK;
553 if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
554 return getParam2() & LIQUID_LEVEL_MASK;
555 if(f.leveled || f.param_type_2 == CPT2_LEVELED) {
556 u8 level = getParam2() & LEVELED_MASK;
559 if(f.leveled > LEVELED_MAX)
561 return f.leveled; //default
566 u8 MapNode::setLevel(INodeDefManager *nodemgr, s8 level)
570 setContent(CONTENT_AIR);
573 const ContentFeatures &f = nodemgr->get(*this);
574 if (f.param_type_2 == CPT2_FLOWINGLIQUID
575 || f.liquid_type == LIQUID_FLOWING
576 || f.liquid_type == LIQUID_SOURCE) {
577 if (level >= LIQUID_LEVEL_SOURCE) {
578 rest = level - LIQUID_LEVEL_SOURCE;
579 setContent(nodemgr->getId(f.liquid_alternative_source));
581 setContent(nodemgr->getId(f.liquid_alternative_flowing));
582 setParam2(level & LIQUID_LEVEL_MASK);
584 } else if (f.leveled || f.param_type_2 == CPT2_LEVELED) {
585 if (level > LEVELED_MAX) {
586 rest = level - LEVELED_MAX;
589 setParam2(level & LEVELED_MASK);
594 u8 MapNode::addLevel(INodeDefManager *nodemgr, s8 add)
596 s8 level = getLevel(nodemgr);
597 if (add == 0) level = 1;
599 return setLevel(nodemgr, level);
602 u32 MapNode::serializedLength(u8 version)
604 if(!ser_ver_supported(version))
605 throw VersionMismatchException("ERROR: MapNode format not supported");
609 else if(version <= 9)
611 else if(version <= 23)
616 void MapNode::serialize(u8 *dest, u8 version)
618 if(!ser_ver_supported(version))
619 throw VersionMismatchException("ERROR: MapNode format not supported");
621 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
622 // in memory; conversion just won't work in this direction.
624 throw SerializationError("MapNode::serialize: serialization to "
625 "version < 24 not possible");
627 writeU16(dest+0, param0);
628 writeU8(dest+2, param1);
629 writeU8(dest+3, param2);
631 void MapNode::deSerialize(u8 *source, u8 version)
633 if (!ser_ver_supported(version))
634 throw VersionMismatchException("ERROR: MapNode format not supported");
637 param0 = readU16(source + 0);
638 param1 = readU8(source + 2);
639 param2 = readU8(source + 3);
641 param0 = readU8(source + 0);
642 param1 = readU8(source + 1);
643 param2 = readU8(source + 2);
645 param0 |= ((param2 & 0xF0) << 4);
650 void MapNode::serializeBulk(std::ostream &os, int version,
651 const MapNode *nodes, u32 nodecount,
652 u8 content_width, u8 params_width, bool compressed)
654 if(!ser_ver_supported(version))
655 throw VersionMismatchException("ERROR: MapNode format not supported");
657 sanity_check(content_width == 2);
658 sanity_check(params_width == 2);
660 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
661 // in memory; conversion just won't work in this direction.
663 throw SerializationError("MapNode::serializeBulk: serialization to "
664 "version < 24 not possible");
666 SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
669 for(u32 i=0; i<nodecount; i++)
670 writeU16(&databuf[i*2], nodes[i].param0);
673 u32 start1 = content_width * nodecount;
674 for(u32 i=0; i<nodecount; i++)
675 writeU8(&databuf[start1 + i], nodes[i].param1);
678 u32 start2 = (content_width + 1) * nodecount;
679 for(u32 i=0; i<nodecount; i++)
680 writeU8(&databuf[start2 + i], nodes[i].param2);
683 Compress data to output stream
688 compressZlib(databuf, os);
692 os.write((const char*) &databuf[0], databuf.getSize());
696 // Deserialize bulk node data
697 void MapNode::deSerializeBulk(std::istream &is, int version,
698 MapNode *nodes, u32 nodecount,
699 u8 content_width, u8 params_width, bool compressed)
701 if(!ser_ver_supported(version))
702 throw VersionMismatchException("ERROR: MapNode format not supported");
705 || (content_width != 1 && content_width != 2)
706 || params_width != 2)
707 FATAL_ERROR("Deserialize bulk node data error");
709 // Uncompress or read data
710 u32 len = nodecount * (content_width + params_width);
711 SharedBuffer<u8> databuf(len);
714 std::ostringstream os(std::ios_base::binary);
715 decompressZlib(is, os);
716 std::string s = os.str();
718 throw SerializationError("deSerializeBulkNodes: "
719 "decompress resulted in invalid size");
720 memcpy(&databuf[0], s.c_str(), len);
724 is.read((char*) &databuf[0], len);
725 if(is.eof() || is.fail())
726 throw SerializationError("deSerializeBulkNodes: "
727 "failed to read bulk node data");
730 // Deserialize content
731 if(content_width == 1)
733 for(u32 i=0; i<nodecount; i++)
734 nodes[i].param0 = readU8(&databuf[i]);
736 else if(content_width == 2)
738 for(u32 i=0; i<nodecount; i++)
739 nodes[i].param0 = readU16(&databuf[i*2]);
742 // Deserialize param1
743 u32 start1 = content_width * nodecount;
744 for(u32 i=0; i<nodecount; i++)
745 nodes[i].param1 = readU8(&databuf[start1 + i]);
747 // Deserialize param2
748 u32 start2 = (content_width + 1) * nodecount;
749 if(content_width == 1)
751 for(u32 i=0; i<nodecount; i++) {
752 nodes[i].param2 = readU8(&databuf[start2 + i]);
753 if(nodes[i].param0 > 0x7F){
754 nodes[i].param0 <<= 4;
755 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
756 nodes[i].param2 &= 0x0F;
760 else if(content_width == 2)
762 for(u32 i=0; i<nodecount; i++)
763 nodes[i].param2 = readU8(&databuf[start2 + i]);