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 "content_mapnode.h" // For mapnode_translate_*_internal
26 #include "serialization.h" // For ser_ver_supported
27 #include "util/serialize.h"
29 #include "util/directiontables.h"
30 #include "util/numeric.h"
34 static const Rotation wallmounted_to_rot[] = {
35 ROTATE_0, ROTATE_180, ROTATE_90, ROTATE_270
38 static const u8 rot_to_wallmounted[] = {
47 // Create directly from a nodename
48 // If name is unknown, sets CONTENT_IGNORE
49 MapNode::MapNode(const NodeDefManager *ndef, const std::string &name,
50 u8 a_param1, u8 a_param2)
52 content_t id = CONTENT_IGNORE;
53 ndef->getId(name, id);
59 void MapNode::getColor(const ContentFeatures &f, video::SColor *color) const
62 *color = (*f.palette)[param2];
68 void MapNode::setLight(LightBank bank, u8 a_light, const ContentFeatures &f) noexcept
70 // If node doesn't contain light data, ignore this
71 if(f.param_type != CPT_LIGHT)
73 if(bank == LIGHTBANK_DAY)
76 param1 |= a_light & 0x0f;
78 else if(bank == LIGHTBANK_NIGHT)
81 param1 |= (a_light & 0x0f)<<4;
84 assert("Invalid light bank" == NULL);
87 void MapNode::setLight(LightBank bank, u8 a_light, const NodeDefManager *nodemgr)
89 setLight(bank, a_light, nodemgr->get(*this));
92 bool MapNode::isLightDayNightEq(const NodeDefManager *nodemgr) const
94 const ContentFeatures &f = nodemgr->get(*this);
97 if (f.param_type == CPT_LIGHT) {
98 u8 day = MYMAX(f.light_source, param1 & 0x0f);
99 u8 night = MYMAX(f.light_source, (param1 >> 4) & 0x0f);
100 isEqual = day == night;
108 u8 MapNode::getLight(LightBank bank, const NodeDefManager *nodemgr) const
110 // Select the brightest of [light source, propagated light]
111 const ContentFeatures &f = nodemgr->get(*this);
114 if(f.param_type == CPT_LIGHT)
115 light = bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f;
119 return MYMAX(f.light_source, light);
122 u8 MapNode::getLightRaw(LightBank bank, const ContentFeatures &f) const noexcept
124 if(f.param_type == CPT_LIGHT)
125 return bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f;
129 u8 MapNode::getLightNoChecks(LightBank bank, const ContentFeatures *f) const noexcept
131 return MYMAX(f->light_source,
132 bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f);
135 bool MapNode::getLightBanks(u8 &lightday, u8 &lightnight,
136 const NodeDefManager *nodemgr) const
138 // Select the brightest of [light source, propagated light]
139 const ContentFeatures &f = nodemgr->get(*this);
140 if(f.param_type == CPT_LIGHT)
142 lightday = param1 & 0x0f;
143 lightnight = (param1>>4)&0x0f;
150 if(f.light_source > lightday)
151 lightday = f.light_source;
152 if(f.light_source > lightnight)
153 lightnight = f.light_source;
154 return f.param_type == CPT_LIGHT || f.light_source != 0;
157 u8 MapNode::getFaceDir(const NodeDefManager *nodemgr,
158 bool allow_wallmounted) const
160 const ContentFeatures &f = nodemgr->get(*this);
161 if (f.param_type_2 == CPT2_FACEDIR ||
162 f.param_type_2 == CPT2_COLORED_FACEDIR)
163 return (getParam2() & 0x1F) % 24;
164 if (allow_wallmounted && (f.param_type_2 == CPT2_WALLMOUNTED ||
165 f.param_type_2 == CPT2_COLORED_WALLMOUNTED))
166 return wallmounted_to_facedir[getParam2() & 0x07];
170 u8 MapNode::getWallMounted(const NodeDefManager *nodemgr) const
172 const ContentFeatures &f = nodemgr->get(*this);
173 if (f.param_type_2 == CPT2_WALLMOUNTED ||
174 f.param_type_2 == CPT2_COLORED_WALLMOUNTED)
175 return getParam2() & 0x07;
179 v3s16 MapNode::getWallMountedDir(const NodeDefManager *nodemgr) const
181 switch(getWallMounted(nodemgr))
183 case 0: default: return v3s16(0,1,0);
184 case 1: return v3s16(0,-1,0);
185 case 2: return v3s16(1,0,0);
186 case 3: return v3s16(-1,0,0);
187 case 4: return v3s16(0,0,1);
188 case 5: return v3s16(0,0,-1);
192 void MapNode::rotateAlongYAxis(const NodeDefManager *nodemgr, Rotation rot)
194 ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
196 if (cpt2 == CPT2_FACEDIR || cpt2 == CPT2_COLORED_FACEDIR) {
197 static const u8 rotate_facedir[24 * 4] = {
198 // Table value = rotated facedir
199 // Columns: 0, 90, 180, 270 degrees rotation around vertical axis
200 // Rotation is anticlockwise as seen from above (+Y)
202 0, 1, 2, 3, // Initial facedir 0 to 3
207 4, 13, 10, 19, // 4 to 7
212 8, 17, 6, 15, // 8 to 11
217 12, 9, 18, 7, // 12 to 15
222 16, 5, 14, 11, // 16 to 19
227 20, 23, 22, 21, // 20 to 23
232 u8 facedir = (param2 & 31) % 24;
233 u8 index = facedir * 4 + rot;
235 param2 |= rotate_facedir[index];
236 } else if (cpt2 == CPT2_WALLMOUNTED ||
237 cpt2 == CPT2_COLORED_WALLMOUNTED) {
238 u8 wmountface = (param2 & 7);
242 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
244 param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
248 void transformNodeBox(const MapNode &n, const NodeBox &nodebox,
249 const NodeDefManager *nodemgr, std::vector<aabb3f> *p_boxes,
252 std::vector<aabb3f> &boxes = *p_boxes;
254 if (nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED) {
255 const std::vector<aabb3f> &fixed = nodebox.fixed;
256 int facedir = n.getFaceDir(nodemgr, true);
257 u8 axisdir = facedir>>2;
259 for (aabb3f box : fixed) {
260 if (nodebox.type == NODEBOX_LEVELED)
261 box.MaxEdge.Y = (-0.5f + n.getLevel(nodemgr) / 64.0f) * BS;
267 box.MinEdge.rotateXZBy(-90);
268 box.MaxEdge.rotateXZBy(-90);
270 else if(facedir == 2)
272 box.MinEdge.rotateXZBy(180);
273 box.MaxEdge.rotateXZBy(180);
275 else if(facedir == 3)
277 box.MinEdge.rotateXZBy(90);
278 box.MaxEdge.rotateXZBy(90);
282 box.MinEdge.rotateYZBy(90);
283 box.MaxEdge.rotateYZBy(90);
286 box.MinEdge.rotateXYBy(90);
287 box.MaxEdge.rotateXYBy(90);
289 else if(facedir == 2)
291 box.MinEdge.rotateXYBy(180);
292 box.MaxEdge.rotateXYBy(180);
294 else if(facedir == 3)
296 box.MinEdge.rotateXYBy(-90);
297 box.MaxEdge.rotateXYBy(-90);
301 box.MinEdge.rotateYZBy(-90);
302 box.MaxEdge.rotateYZBy(-90);
305 box.MinEdge.rotateXYBy(-90);
306 box.MaxEdge.rotateXYBy(-90);
308 else if(facedir == 2)
310 box.MinEdge.rotateXYBy(180);
311 box.MaxEdge.rotateXYBy(180);
313 else if(facedir == 3)
315 box.MinEdge.rotateXYBy(90);
316 box.MaxEdge.rotateXYBy(90);
320 box.MinEdge.rotateXYBy(-90);
321 box.MaxEdge.rotateXYBy(-90);
324 box.MinEdge.rotateYZBy(90);
325 box.MaxEdge.rotateYZBy(90);
327 else if(facedir == 2)
329 box.MinEdge.rotateYZBy(180);
330 box.MaxEdge.rotateYZBy(180);
332 else if(facedir == 3)
334 box.MinEdge.rotateYZBy(-90);
335 box.MaxEdge.rotateYZBy(-90);
339 box.MinEdge.rotateXYBy(90);
340 box.MaxEdge.rotateXYBy(90);
343 box.MinEdge.rotateYZBy(-90);
344 box.MaxEdge.rotateYZBy(-90);
346 else if(facedir == 2)
348 box.MinEdge.rotateYZBy(180);
349 box.MaxEdge.rotateYZBy(180);
351 else if(facedir == 3)
353 box.MinEdge.rotateYZBy(90);
354 box.MaxEdge.rotateYZBy(90);
358 box.MinEdge.rotateXYBy(-180);
359 box.MaxEdge.rotateXYBy(-180);
362 box.MinEdge.rotateXZBy(90);
363 box.MaxEdge.rotateXZBy(90);
365 else if(facedir == 2)
367 box.MinEdge.rotateXZBy(180);
368 box.MaxEdge.rotateXZBy(180);
370 else if(facedir == 3)
372 box.MinEdge.rotateXZBy(-90);
373 box.MaxEdge.rotateXZBy(-90);
380 boxes.push_back(box);
383 else if(nodebox.type == NODEBOX_WALLMOUNTED)
385 v3s16 dir = n.getWallMountedDir(nodemgr);
388 if(dir == v3s16(0,1,0))
390 boxes.push_back(nodebox.wall_top);
393 else if(dir == v3s16(0,-1,0))
395 boxes.push_back(nodebox.wall_bottom);
402 nodebox.wall_side.MinEdge,
403 nodebox.wall_side.MaxEdge
406 for (v3f &vertex : vertices) {
407 if(dir == v3s16(-1,0,0))
408 vertex.rotateXZBy(0);
409 if(dir == v3s16(1,0,0))
410 vertex.rotateXZBy(180);
411 if(dir == v3s16(0,0,-1))
412 vertex.rotateXZBy(90);
413 if(dir == v3s16(0,0,1))
414 vertex.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.disconnected_top.size();
432 boxes_size += nodebox.connect_bottom.size();
434 boxes_size += nodebox.disconnected_bottom.size();
437 boxes_size += nodebox.connect_front.size();
439 boxes_size += nodebox.disconnected_front.size();
442 boxes_size += nodebox.connect_left.size();
444 boxes_size += nodebox.disconnected_left.size();
447 boxes_size += nodebox.connect_back.size();
449 boxes_size += nodebox.disconnected_back.size();
452 boxes_size += nodebox.connect_right.size();
454 boxes_size += nodebox.disconnected_right.size();
457 boxes_size += nodebox.disconnected.size();
460 boxes_size += nodebox.disconnected_sides.size();
462 boxes.reserve(boxes_size);
464 #define BOXESPUSHBACK(c) \
465 for (std::vector<aabb3f>::const_iterator \
467 it != (c).end(); ++it) \
468 (boxes).push_back(*it);
470 BOXESPUSHBACK(nodebox.fixed);
473 BOXESPUSHBACK(nodebox.connect_top);
475 BOXESPUSHBACK(nodebox.disconnected_top);
479 BOXESPUSHBACK(nodebox.connect_bottom);
481 BOXESPUSHBACK(nodebox.disconnected_bottom);
485 BOXESPUSHBACK(nodebox.connect_front);
487 BOXESPUSHBACK(nodebox.disconnected_front);
491 BOXESPUSHBACK(nodebox.connect_left);
493 BOXESPUSHBACK(nodebox.disconnected_left);
496 if (neighbors & 16) {
497 BOXESPUSHBACK(nodebox.connect_back);
499 BOXESPUSHBACK(nodebox.disconnected_back);
502 if (neighbors & 32) {
503 BOXESPUSHBACK(nodebox.connect_right);
505 BOXESPUSHBACK(nodebox.disconnected_right);
508 if (neighbors == 0) {
509 BOXESPUSHBACK(nodebox.disconnected);
513 BOXESPUSHBACK(nodebox.disconnected_sides);
517 else // NODEBOX_REGULAR
519 boxes.emplace_back(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2);
523 static inline void getNeighborConnectingFace(
524 const v3s16 &p, const NodeDefManager *nodedef,
525 Map *map, MapNode n, u8 bitmask, u8 *neighbors)
527 MapNode n2 = map->getNodeNoEx(p);
528 if (nodedef->nodeboxConnects(n, n2, bitmask))
529 *neighbors |= bitmask;
532 u8 MapNode::getNeighbors(v3s16 p, Map *map) const
534 const NodeDefManager *nodedef = map->getNodeDefManager();
536 const ContentFeatures &f = nodedef->get(*this);
537 // locate possible neighboring nodes to connect to
538 if (f.drawtype == NDT_NODEBOX && f.node_box.type == NODEBOX_CONNECTED) {
542 getNeighborConnectingFace(p2, nodedef, map, *this, 1, &neighbors);
546 getNeighborConnectingFace(p2, nodedef, map, *this, 2, &neighbors);
550 getNeighborConnectingFace(p2, nodedef, map, *this, 4, &neighbors);
554 getNeighborConnectingFace(p2, nodedef, map, *this, 8, &neighbors);
558 getNeighborConnectingFace(p2, nodedef, map, *this, 16, &neighbors);
562 getNeighborConnectingFace(p2, nodedef, map, *this, 32, &neighbors);
568 void MapNode::getNodeBoxes(const NodeDefManager *nodemgr,
569 std::vector<aabb3f> *boxes, u8 neighbors) const
571 const ContentFeatures &f = nodemgr->get(*this);
572 transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
575 void MapNode::getCollisionBoxes(const NodeDefManager *nodemgr,
576 std::vector<aabb3f> *boxes, u8 neighbors) const
578 const ContentFeatures &f = nodemgr->get(*this);
579 if (f.collision_box.fixed.empty())
580 transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
582 transformNodeBox(*this, f.collision_box, nodemgr, boxes, neighbors);
585 void MapNode::getSelectionBoxes(const NodeDefManager *nodemgr,
586 std::vector<aabb3f> *boxes, u8 neighbors) const
588 const ContentFeatures &f = nodemgr->get(*this);
589 transformNodeBox(*this, f.selection_box, nodemgr, boxes, neighbors);
592 u8 MapNode::getMaxLevel(const NodeDefManager *nodemgr) const
594 const ContentFeatures &f = nodemgr->get(*this);
595 // todo: after update in all games leave only if (f.param_type_2 ==
596 if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
597 return LIQUID_LEVEL_MAX;
598 if(f.leveled || f.param_type_2 == CPT2_LEVELED)
603 u8 MapNode::getLevel(const NodeDefManager *nodemgr) const
605 const ContentFeatures &f = nodemgr->get(*this);
606 // todo: after update in all games leave only if (f.param_type_2 ==
607 if(f.liquid_type == LIQUID_SOURCE)
608 return LIQUID_LEVEL_SOURCE;
609 if (f.param_type_2 == CPT2_FLOWINGLIQUID)
610 return getParam2() & LIQUID_LEVEL_MASK;
611 if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
612 return getParam2() & LIQUID_LEVEL_MASK;
613 if (f.param_type_2 == CPT2_LEVELED) {
614 u8 level = getParam2() & LEVELED_MASK;
618 if (f.leveled > LEVELED_MAX)
623 u8 MapNode::setLevel(const NodeDefManager *nodemgr, s8 level)
626 const ContentFeatures &f = nodemgr->get(*this);
627 if (f.param_type_2 == CPT2_FLOWINGLIQUID
628 || f.liquid_type == LIQUID_FLOWING
629 || f.liquid_type == LIQUID_SOURCE) {
630 if (level <= 0) { // liquid can’t exist with zero level
631 setContent(CONTENT_AIR);
634 if (level >= LIQUID_LEVEL_SOURCE) {
635 rest = level - LIQUID_LEVEL_SOURCE;
636 setContent(nodemgr->getId(f.liquid_alternative_source));
639 setContent(nodemgr->getId(f.liquid_alternative_flowing));
640 setParam2((level & LIQUID_LEVEL_MASK) | (getParam2() & ~LIQUID_LEVEL_MASK));
642 } else if (f.param_type_2 == CPT2_LEVELED) {
643 if (level < 0) { // zero means default for a leveled nodebox
646 } else if (level > LEVELED_MAX) {
647 rest = level - LEVELED_MAX;
650 setParam2((level & LEVELED_MASK) | (getParam2() & ~LEVELED_MASK));
655 u8 MapNode::addLevel(const NodeDefManager *nodemgr, s8 add)
657 s8 level = getLevel(nodemgr);
659 return setLevel(nodemgr, level);
662 u32 MapNode::serializedLength(u8 version)
664 if(!ser_ver_supported(version))
665 throw VersionMismatchException("ERROR: MapNode format not supported");
678 void MapNode::serialize(u8 *dest, u8 version) const
680 if(!ser_ver_supported(version))
681 throw VersionMismatchException("ERROR: MapNode format not supported");
683 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
684 // in memory; conversion just won't work in this direction.
686 throw SerializationError("MapNode::serialize: serialization to "
687 "version < 24 not possible");
689 writeU16(dest+0, param0);
690 writeU8(dest+2, param1);
691 writeU8(dest+3, param2);
693 void MapNode::deSerialize(u8 *source, u8 version)
695 if(!ser_ver_supported(version))
696 throw VersionMismatchException("ERROR: MapNode format not supported");
700 deSerialize_pre22(source, version);
705 param0 = readU16(source+0);
706 param1 = readU8(source+2);
707 param2 = readU8(source+3);
709 param0 = readU8(source+0);
710 param1 = readU8(source+1);
711 param2 = readU8(source+2);
713 param0 |= ((param2&0xF0)<<4);
718 void MapNode::serializeBulk(std::ostream &os, int version,
719 const MapNode *nodes, u32 nodecount,
720 u8 content_width, u8 params_width, bool compressed)
722 if (!ser_ver_supported(version))
723 throw VersionMismatchException("ERROR: MapNode format not supported");
725 sanity_check(content_width == 2);
726 sanity_check(params_width == 2);
728 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
729 // in memory; conversion just won't work in this direction.
731 throw SerializationError("MapNode::serializeBulk: serialization to "
732 "version < 24 not possible");
734 size_t databuf_size = nodecount * (content_width + params_width);
735 u8 *databuf = new u8[databuf_size];
737 u32 start1 = content_width * nodecount;
738 u32 start2 = (content_width + 1) * nodecount;
741 for (u32 i = 0; i < nodecount; i++) {
742 writeU16(&databuf[i * 2], nodes[i].param0);
743 writeU8(&databuf[start1 + i], nodes[i].param1);
744 writeU8(&databuf[start2 + i], nodes[i].param2);
748 Compress data to output stream
752 compressZlib(databuf, databuf_size, os);
754 os.write((const char*) &databuf[0], databuf_size);
759 // Deserialize bulk node data
760 void MapNode::deSerializeBulk(std::istream &is, int version,
761 MapNode *nodes, u32 nodecount,
762 u8 content_width, u8 params_width, bool compressed)
764 if(!ser_ver_supported(version))
765 throw VersionMismatchException("ERROR: MapNode format not supported");
768 || (content_width != 1 && content_width != 2)
769 || params_width != 2)
770 FATAL_ERROR("Deserialize bulk node data error");
772 // Uncompress or read data
773 u32 len = nodecount * (content_width + params_width);
774 SharedBuffer<u8> databuf(len);
777 std::ostringstream os(std::ios_base::binary);
778 decompressZlib(is, os);
779 std::string s = os.str();
781 throw SerializationError("deSerializeBulkNodes: "
782 "decompress resulted in invalid size");
783 memcpy(&databuf[0], s.c_str(), len);
787 is.read((char*) &databuf[0], len);
788 if(is.eof() || is.fail())
789 throw SerializationError("deSerializeBulkNodes: "
790 "failed to read bulk node data");
793 // Deserialize content
794 if(content_width == 1)
796 for(u32 i=0; i<nodecount; i++)
797 nodes[i].param0 = readU8(&databuf[i]);
799 else if(content_width == 2)
801 for(u32 i=0; i<nodecount; i++)
802 nodes[i].param0 = readU16(&databuf[i*2]);
805 // Deserialize param1
806 u32 start1 = content_width * nodecount;
807 for(u32 i=0; i<nodecount; i++)
808 nodes[i].param1 = readU8(&databuf[start1 + i]);
810 // Deserialize param2
811 u32 start2 = (content_width + 1) * nodecount;
812 if(content_width == 1)
814 for(u32 i=0; i<nodecount; i++) {
815 nodes[i].param2 = readU8(&databuf[start2 + i]);
816 if(nodes[i].param0 > 0x7F){
817 nodes[i].param0 <<= 4;
818 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
819 nodes[i].param2 &= 0x0F;
823 else if(content_width == 2)
825 for(u32 i=0; i<nodecount; i++)
826 nodes[i].param2 = readU8(&databuf[start2 + i]);
833 void MapNode::deSerialize_pre22(const u8 *source, u8 version)
839 else if(version <= 9)
851 param0 |= (param2&0xf0)>>4;
856 // Convert special values from old version to new
859 // In these versions, CONTENT_IGNORE and CONTENT_AIR
861 // Version 19 is fucked up with sometimes the old values and sometimes not
863 param0 = CONTENT_IGNORE;
864 else if(param0 == 254)
865 param0 = CONTENT_AIR;
868 // Translate to our known version
869 *this = mapnode_translate_to_internal(*this, version);