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 void MapNode::getColor(const ContentFeatures &f, video::SColor *color) const
50 *color = (*f.palette)[param2];
56 void MapNode::setLight(LightBank bank, u8 a_light, const ContentFeatures &f) noexcept
58 // If node doesn't contain light data, ignore this
59 if(f.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;
72 assert("Invalid light bank" == NULL);
75 void MapNode::setLight(LightBank bank, u8 a_light, const NodeDefManager *nodemgr)
77 setLight(bank, a_light, nodemgr->get(*this));
80 bool MapNode::isLightDayNightEq(const NodeDefManager *nodemgr) const
82 const ContentFeatures &f = nodemgr->get(*this);
85 if (f.param_type == CPT_LIGHT) {
86 u8 day = MYMAX(f.light_source, param1 & 0x0f);
87 u8 night = MYMAX(f.light_source, (param1 >> 4) & 0x0f);
88 isEqual = day == night;
96 u8 MapNode::getLight(LightBank bank, const NodeDefManager *nodemgr) const
98 // Select the brightest of [light source, propagated light]
99 const ContentFeatures &f = nodemgr->get(*this);
102 if(f.param_type == CPT_LIGHT)
103 light = bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f;
107 return MYMAX(f.light_source, light);
110 u8 MapNode::getLightRaw(LightBank bank, const ContentFeatures &f) const noexcept
112 if(f.param_type == CPT_LIGHT)
113 return bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f;
117 u8 MapNode::getLightNoChecks(LightBank bank, const ContentFeatures *f) const noexcept
119 return MYMAX(f->light_source,
120 bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f);
123 bool MapNode::getLightBanks(u8 &lightday, u8 &lightnight,
124 const NodeDefManager *nodemgr) const
126 // Select the brightest of [light source, propagated light]
127 const ContentFeatures &f = nodemgr->get(*this);
128 if(f.param_type == CPT_LIGHT)
130 lightday = param1 & 0x0f;
131 lightnight = (param1>>4)&0x0f;
138 if(f.light_source > lightday)
139 lightday = f.light_source;
140 if(f.light_source > lightnight)
141 lightnight = f.light_source;
142 return f.param_type == CPT_LIGHT || f.light_source != 0;
145 u8 MapNode::getFaceDir(const NodeDefManager *nodemgr,
146 bool allow_wallmounted) const
148 const ContentFeatures &f = nodemgr->get(*this);
149 if (f.param_type_2 == CPT2_FACEDIR ||
150 f.param_type_2 == CPT2_COLORED_FACEDIR)
151 return (getParam2() & 0x1F) % 24;
152 if (allow_wallmounted && (f.param_type_2 == CPT2_WALLMOUNTED ||
153 f.param_type_2 == CPT2_COLORED_WALLMOUNTED))
154 return wallmounted_to_facedir[getParam2() & 0x07];
158 u8 MapNode::getWallMounted(const NodeDefManager *nodemgr) const
160 const ContentFeatures &f = nodemgr->get(*this);
161 if (f.param_type_2 == CPT2_WALLMOUNTED ||
162 f.param_type_2 == CPT2_COLORED_WALLMOUNTED) {
163 return getParam2() & 0x07;
164 } else if (f.drawtype == NDT_SIGNLIKE || f.drawtype == NDT_TORCHLIKE ||
165 f.drawtype == NDT_PLANTLIKE ||
166 f.drawtype == NDT_PLANTLIKE_ROOTED) {
172 v3s16 MapNode::getWallMountedDir(const NodeDefManager *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 u8 MapNode::getDegRotate(const NodeDefManager *nodemgr) const
187 const ContentFeatures &f = nodemgr->get(*this);
188 if (f.param_type_2 == CPT2_DEGROTATE)
189 return getParam2() % 240;
190 if (f.param_type_2 == CPT2_COLORED_DEGROTATE)
191 return 10 * ((getParam2() & 0x1F) % 24);
195 void MapNode::rotateAlongYAxis(const NodeDefManager *nodemgr, Rotation rot)
197 ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
199 if (cpt2 == CPT2_FACEDIR || cpt2 == CPT2_COLORED_FACEDIR) {
200 static const u8 rotate_facedir[24 * 4] = {
201 // Table value = rotated facedir
202 // Columns: 0, 90, 180, 270 degrees rotation around vertical axis
203 // Rotation is anticlockwise as seen from above (+Y)
205 0, 1, 2, 3, // Initial facedir 0 to 3
210 4, 13, 10, 19, // 4 to 7
215 8, 17, 6, 15, // 8 to 11
220 12, 9, 18, 7, // 12 to 15
225 16, 5, 14, 11, // 16 to 19
230 20, 23, 22, 21, // 20 to 23
235 u8 facedir = (param2 & 31) % 24;
236 u8 index = facedir * 4 + rot;
238 param2 |= rotate_facedir[index];
239 } else if (cpt2 == CPT2_WALLMOUNTED ||
240 cpt2 == CPT2_COLORED_WALLMOUNTED) {
241 u8 wmountface = (param2 & 7);
245 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
247 param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
248 } else if (cpt2 == CPT2_DEGROTATE) {
249 int angle = param2; // in 1.5°
250 angle += 60 * rot; // don’t do that on u8
253 } else if (cpt2 == CPT2_COLORED_DEGROTATE) {
254 int angle = param2 & 0x1F; // in 15°
255 int color = param2 & 0xE0;
258 param2 = color | angle;
262 void transformNodeBox(const MapNode &n, const NodeBox &nodebox,
263 const NodeDefManager *nodemgr, std::vector<aabb3f> *p_boxes,
266 std::vector<aabb3f> &boxes = *p_boxes;
268 if (nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED) {
269 const auto &fixed = nodebox.fixed;
270 int facedir = n.getFaceDir(nodemgr, true);
271 u8 axisdir = facedir>>2;
274 boxes.reserve(boxes.size() + fixed.size());
275 for (aabb3f box : fixed) {
276 if (nodebox.type == NODEBOX_LEVELED)
277 box.MaxEdge.Y = (-0.5f + n.getLevel(nodemgr) / 64.0f) * BS;
283 box.MinEdge.rotateXZBy(-90);
284 box.MaxEdge.rotateXZBy(-90);
286 else if(facedir == 2)
288 box.MinEdge.rotateXZBy(180);
289 box.MaxEdge.rotateXZBy(180);
291 else if(facedir == 3)
293 box.MinEdge.rotateXZBy(90);
294 box.MaxEdge.rotateXZBy(90);
298 box.MinEdge.rotateYZBy(90);
299 box.MaxEdge.rotateYZBy(90);
302 box.MinEdge.rotateXYBy(90);
303 box.MaxEdge.rotateXYBy(90);
305 else if(facedir == 2)
307 box.MinEdge.rotateXYBy(180);
308 box.MaxEdge.rotateXYBy(180);
310 else if(facedir == 3)
312 box.MinEdge.rotateXYBy(-90);
313 box.MaxEdge.rotateXYBy(-90);
317 box.MinEdge.rotateYZBy(-90);
318 box.MaxEdge.rotateYZBy(-90);
321 box.MinEdge.rotateXYBy(-90);
322 box.MaxEdge.rotateXYBy(-90);
324 else if(facedir == 2)
326 box.MinEdge.rotateXYBy(180);
327 box.MaxEdge.rotateXYBy(180);
329 else if(facedir == 3)
331 box.MinEdge.rotateXYBy(90);
332 box.MaxEdge.rotateXYBy(90);
336 box.MinEdge.rotateXYBy(-90);
337 box.MaxEdge.rotateXYBy(-90);
340 box.MinEdge.rotateYZBy(90);
341 box.MaxEdge.rotateYZBy(90);
343 else if(facedir == 2)
345 box.MinEdge.rotateYZBy(180);
346 box.MaxEdge.rotateYZBy(180);
348 else if(facedir == 3)
350 box.MinEdge.rotateYZBy(-90);
351 box.MaxEdge.rotateYZBy(-90);
355 box.MinEdge.rotateXYBy(90);
356 box.MaxEdge.rotateXYBy(90);
359 box.MinEdge.rotateYZBy(-90);
360 box.MaxEdge.rotateYZBy(-90);
362 else if(facedir == 2)
364 box.MinEdge.rotateYZBy(180);
365 box.MaxEdge.rotateYZBy(180);
367 else if(facedir == 3)
369 box.MinEdge.rotateYZBy(90);
370 box.MaxEdge.rotateYZBy(90);
374 box.MinEdge.rotateXYBy(-180);
375 box.MaxEdge.rotateXYBy(-180);
378 box.MinEdge.rotateXZBy(90);
379 box.MaxEdge.rotateXZBy(90);
381 else if(facedir == 2)
383 box.MinEdge.rotateXZBy(180);
384 box.MaxEdge.rotateXZBy(180);
386 else if(facedir == 3)
388 box.MinEdge.rotateXZBy(-90);
389 box.MaxEdge.rotateXZBy(-90);
396 boxes.push_back(box);
399 else if(nodebox.type == NODEBOX_WALLMOUNTED)
401 v3s16 dir = n.getWallMountedDir(nodemgr);
404 if(dir == v3s16(0,1,0))
406 boxes.push_back(nodebox.wall_top);
409 else if(dir == v3s16(0,-1,0))
411 boxes.push_back(nodebox.wall_bottom);
418 nodebox.wall_side.MinEdge,
419 nodebox.wall_side.MaxEdge
422 for (v3f &vertex : vertices) {
423 if(dir == v3s16(-1,0,0))
424 vertex.rotateXZBy(0);
425 if(dir == v3s16(1,0,0))
426 vertex.rotateXZBy(180);
427 if(dir == v3s16(0,0,-1))
428 vertex.rotateXZBy(90);
429 if(dir == v3s16(0,0,1))
430 vertex.rotateXZBy(-90);
433 aabb3f box = aabb3f(vertices[0]);
434 box.addInternalPoint(vertices[1]);
435 boxes.push_back(box);
438 else if (nodebox.type == NODEBOX_CONNECTED)
440 size_t boxes_size = boxes.size();
441 boxes_size += nodebox.fixed.size();
442 const auto &c = nodebox.getConnected();
445 boxes_size += c.connect_top.size();
447 boxes_size += c.disconnected_top.size();
450 boxes_size += c.connect_bottom.size();
452 boxes_size += c.disconnected_bottom.size();
455 boxes_size += c.connect_front.size();
457 boxes_size += c.disconnected_front.size();
460 boxes_size += c.connect_left.size();
462 boxes_size += c.disconnected_left.size();
465 boxes_size += c.connect_back.size();
467 boxes_size += c.disconnected_back.size();
470 boxes_size += c.connect_right.size();
472 boxes_size += c.disconnected_right.size();
475 boxes_size += c.disconnected.size();
478 boxes_size += c.disconnected_sides.size();
480 boxes.reserve(boxes_size);
482 #define BOXESPUSHBACK(c) \
483 for (std::vector<aabb3f>::const_iterator \
485 it != (c).end(); ++it) \
486 (boxes).push_back(*it);
488 BOXESPUSHBACK(nodebox.fixed);
491 BOXESPUSHBACK(c.connect_top);
493 BOXESPUSHBACK(c.disconnected_top);
497 BOXESPUSHBACK(c.connect_bottom);
499 BOXESPUSHBACK(c.disconnected_bottom);
503 BOXESPUSHBACK(c.connect_front);
505 BOXESPUSHBACK(c.disconnected_front);
509 BOXESPUSHBACK(c.connect_left);
511 BOXESPUSHBACK(c.disconnected_left);
514 if (neighbors & 16) {
515 BOXESPUSHBACK(c.connect_back);
517 BOXESPUSHBACK(c.disconnected_back);
520 if (neighbors & 32) {
521 BOXESPUSHBACK(c.connect_right);
523 BOXESPUSHBACK(c.disconnected_right);
526 if (neighbors == 0) {
527 BOXESPUSHBACK(c.disconnected);
531 BOXESPUSHBACK(c.disconnected_sides);
535 else // NODEBOX_REGULAR
537 boxes.emplace_back(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2);
541 static inline void getNeighborConnectingFace(
542 const v3s16 &p, const NodeDefManager *nodedef,
543 Map *map, MapNode n, u8 bitmask, u8 *neighbors)
545 MapNode n2 = map->getNode(p);
546 if (nodedef->nodeboxConnects(n, n2, bitmask))
547 *neighbors |= bitmask;
550 u8 MapNode::getNeighbors(v3s16 p, Map *map) const
552 const NodeDefManager *nodedef = map->getNodeDefManager();
554 const ContentFeatures &f = nodedef->get(*this);
555 // locate possible neighboring nodes to connect to
556 if (f.drawtype == NDT_NODEBOX && f.node_box.type == NODEBOX_CONNECTED) {
560 getNeighborConnectingFace(p2, nodedef, map, *this, 1, &neighbors);
564 getNeighborConnectingFace(p2, nodedef, map, *this, 2, &neighbors);
568 getNeighborConnectingFace(p2, nodedef, map, *this, 4, &neighbors);
572 getNeighborConnectingFace(p2, nodedef, map, *this, 8, &neighbors);
576 getNeighborConnectingFace(p2, nodedef, map, *this, 16, &neighbors);
580 getNeighborConnectingFace(p2, nodedef, map, *this, 32, &neighbors);
586 void MapNode::getNodeBoxes(const NodeDefManager *nodemgr,
587 std::vector<aabb3f> *boxes, u8 neighbors) const
589 const ContentFeatures &f = nodemgr->get(*this);
590 transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
593 void MapNode::getCollisionBoxes(const NodeDefManager *nodemgr,
594 std::vector<aabb3f> *boxes, u8 neighbors) const
596 const ContentFeatures &f = nodemgr->get(*this);
597 if (f.collision_box.fixed.empty())
598 transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
600 transformNodeBox(*this, f.collision_box, nodemgr, boxes, neighbors);
603 void MapNode::getSelectionBoxes(const NodeDefManager *nodemgr,
604 std::vector<aabb3f> *boxes, u8 neighbors) const
606 const ContentFeatures &f = nodemgr->get(*this);
607 transformNodeBox(*this, f.selection_box, nodemgr, boxes, neighbors);
610 u8 MapNode::getMaxLevel(const NodeDefManager *nodemgr) const
612 const ContentFeatures &f = nodemgr->get(*this);
613 // todo: after update in all games leave only if (f.param_type_2 ==
614 if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
615 return LIQUID_LEVEL_MAX;
616 if(f.leveled || f.param_type_2 == CPT2_LEVELED)
617 return f.leveled_max;
621 u8 MapNode::getLevel(const NodeDefManager *nodemgr) const
623 const ContentFeatures &f = nodemgr->get(*this);
624 // todo: after update in all games leave only if (f.param_type_2 ==
625 if(f.liquid_type == LIQUID_SOURCE)
626 return LIQUID_LEVEL_SOURCE;
627 if (f.param_type_2 == CPT2_FLOWINGLIQUID)
628 return getParam2() & LIQUID_LEVEL_MASK;
629 if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
630 return getParam2() & LIQUID_LEVEL_MASK;
631 if (f.param_type_2 == CPT2_LEVELED) {
632 u8 level = getParam2() & LEVELED_MASK;
636 // Return static value from nodedef if param2 isn't used for level
637 if (f.leveled > f.leveled_max)
638 return f.leveled_max;
642 s8 MapNode::setLevel(const NodeDefManager *nodemgr, s16 level)
645 const ContentFeatures &f = nodemgr->get(*this);
646 if (f.param_type_2 == CPT2_FLOWINGLIQUID
647 || f.liquid_type == LIQUID_FLOWING
648 || f.liquid_type == LIQUID_SOURCE) {
649 if (level <= 0) { // liquid can’t exist with zero level
650 setContent(CONTENT_AIR);
653 if (level >= LIQUID_LEVEL_SOURCE) {
654 rest = level - LIQUID_LEVEL_SOURCE;
655 setContent(f.liquid_alternative_source_id);
658 setContent(f.liquid_alternative_flowing_id);
659 setParam2((level & LIQUID_LEVEL_MASK) | (getParam2() & ~LIQUID_LEVEL_MASK));
661 } else if (f.param_type_2 == CPT2_LEVELED) {
662 if (level < 0) { // zero means default for a leveled nodebox
665 } else if (level > f.leveled_max) {
666 rest = level - f.leveled_max;
667 level = f.leveled_max;
669 setParam2((level & LEVELED_MASK) | (getParam2() & ~LEVELED_MASK));
674 s8 MapNode::addLevel(const NodeDefManager *nodemgr, s16 add)
676 s16 level = getLevel(nodemgr);
678 return setLevel(nodemgr, level);
681 u32 MapNode::serializedLength(u8 version)
683 if(!ser_ver_supported(version))
684 throw VersionMismatchException("ERROR: MapNode format not supported");
697 void MapNode::serialize(u8 *dest, u8 version) const
699 if(!ser_ver_supported(version))
700 throw VersionMismatchException("ERROR: MapNode format not supported");
702 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
703 // in memory; conversion just won't work in this direction.
705 throw SerializationError("MapNode::serialize: serialization to "
706 "version < 24 not possible");
708 writeU16(dest+0, param0);
709 writeU8(dest+2, param1);
710 writeU8(dest+3, param2);
712 void MapNode::deSerialize(u8 *source, u8 version)
714 if(!ser_ver_supported(version))
715 throw VersionMismatchException("ERROR: MapNode format not supported");
719 deSerialize_pre22(source, version);
724 param0 = readU16(source+0);
725 param1 = readU8(source+2);
726 param2 = readU8(source+3);
728 param0 = readU8(source+0);
729 param1 = readU8(source+1);
730 param2 = readU8(source+2);
732 param0 |= ((param2&0xF0)<<4);
738 SharedBuffer<u8> MapNode::serializeBulk(int version,
739 const MapNode *nodes, u32 nodecount,
740 u8 content_width, u8 params_width)
742 if (!ser_ver_supported(version))
743 throw VersionMismatchException("ERROR: MapNode format not supported");
745 sanity_check(content_width == 2);
746 sanity_check(params_width == 2);
748 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
749 // in memory; conversion just won't work in this direction.
751 throw SerializationError("MapNode::serializeBulk: serialization to "
752 "version < 24 not possible");
754 SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
756 u32 start1 = content_width * nodecount;
757 u32 start2 = (content_width + 1) * nodecount;
760 for (u32 i = 0; i < nodecount; i++) {
761 writeU16(&databuf[i * 2], nodes[i].param0);
762 writeU8(&databuf[start1 + i], nodes[i].param1);
763 writeU8(&databuf[start2 + i], nodes[i].param2);
768 // Deserialize bulk node data
769 void MapNode::deSerializeBulk(std::istream &is, int version,
770 MapNode *nodes, u32 nodecount,
771 u8 content_width, u8 params_width)
773 if(!ser_ver_supported(version))
774 throw VersionMismatchException("ERROR: MapNode format not supported");
777 || (content_width != 1 && content_width != 2)
778 || params_width != 2)
779 FATAL_ERROR("Deserialize bulk node data error");
782 const u32 len = nodecount * (content_width + params_width);
783 Buffer<u8> databuf(len);
784 is.read(reinterpret_cast<char*>(*databuf), len);
786 // Deserialize content
787 if(content_width == 1)
789 for(u32 i=0; i<nodecount; i++)
790 nodes[i].param0 = readU8(&databuf[i]);
792 else if(content_width == 2)
794 for(u32 i=0; i<nodecount; i++)
795 nodes[i].param0 = readU16(&databuf[i*2]);
798 // Deserialize param1
799 u32 start1 = content_width * nodecount;
800 for(u32 i=0; i<nodecount; i++)
801 nodes[i].param1 = readU8(&databuf[start1 + i]);
803 // Deserialize param2
804 u32 start2 = (content_width + 1) * nodecount;
805 if(content_width == 1)
807 for(u32 i=0; i<nodecount; i++) {
808 nodes[i].param2 = readU8(&databuf[start2 + i]);
809 if(nodes[i].param0 > 0x7F){
810 nodes[i].param0 <<= 4;
811 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
812 nodes[i].param2 &= 0x0F;
816 else if(content_width == 2)
818 for(u32 i=0; i<nodecount; i++)
819 nodes[i].param2 = readU8(&databuf[start2 + i]);
826 void MapNode::deSerialize_pre22(const u8 *source, u8 version)
832 else if(version <= 9)
844 param0 |= (param2&0xf0)>>4;
849 // Convert special values from old version to new
852 // In these versions, CONTENT_IGNORE and CONTENT_AIR
854 // Version 19 is messed up with sometimes the old values and sometimes not
856 param0 = CONTENT_IGNORE;
857 else if(param0 == 254)
858 param0 = CONTENT_AIR;
861 // Translate to our known version
862 *this = mapnode_translate_to_internal(*this, version);