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 u8 MapNode::getFaceDir(const NodeDefManager *nodemgr,
57 bool allow_wallmounted) const
59 const ContentFeatures &f = nodemgr->get(*this);
60 if (f.param_type_2 == CPT2_FACEDIR ||
61 f.param_type_2 == CPT2_COLORED_FACEDIR)
62 return (getParam2() & 0x1F) % 24;
63 if (f.param_type_2 == CPT2_4DIR ||
64 f.param_type_2 == CPT2_COLORED_4DIR)
65 return getParam2() & 0x03;
66 if (allow_wallmounted && (f.param_type_2 == CPT2_WALLMOUNTED ||
67 f.param_type_2 == CPT2_COLORED_WALLMOUNTED))
68 return wallmounted_to_facedir[getParam2() & 0x07];
72 u8 MapNode::getWallMounted(const NodeDefManager *nodemgr) const
74 const ContentFeatures &f = nodemgr->get(*this);
75 if (f.param_type_2 == CPT2_WALLMOUNTED ||
76 f.param_type_2 == CPT2_COLORED_WALLMOUNTED) {
77 return getParam2() & 0x07;
78 } else if (f.drawtype == NDT_SIGNLIKE || f.drawtype == NDT_TORCHLIKE ||
79 f.drawtype == NDT_PLANTLIKE ||
80 f.drawtype == NDT_PLANTLIKE_ROOTED) {
86 v3s16 MapNode::getWallMountedDir(const NodeDefManager *nodemgr) const
88 switch(getWallMounted(nodemgr))
90 case 0: default: return v3s16(0,1,0);
91 case 1: return v3s16(0,-1,0);
92 case 2: return v3s16(1,0,0);
93 case 3: return v3s16(-1,0,0);
94 case 4: return v3s16(0,0,1);
95 case 5: return v3s16(0,0,-1);
99 u8 MapNode::getDegRotate(const NodeDefManager *nodemgr) const
101 const ContentFeatures &f = nodemgr->get(*this);
102 if (f.param_type_2 == CPT2_DEGROTATE)
103 return getParam2() % 240;
104 if (f.param_type_2 == CPT2_COLORED_DEGROTATE)
105 return 10 * ((getParam2() & 0x1F) % 24);
109 void MapNode::rotateAlongYAxis(const NodeDefManager *nodemgr, Rotation rot)
111 ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
113 if (cpt2 == CPT2_FACEDIR || cpt2 == CPT2_COLORED_FACEDIR ||
114 cpt2 == CPT2_4DIR || cpt2 == CPT2_COLORED_4DIR) {
115 static const u8 rotate_facedir[24 * 4] = {
116 // Table value = rotated facedir
117 // Columns: 0, 90, 180, 270 degrees rotation around vertical axis
118 // Rotation is anticlockwise as seen from above (+Y)
120 0, 1, 2, 3, // Initial facedir 0 to 3
125 4, 13, 10, 19, // 4 to 7
130 8, 17, 6, 15, // 8 to 11
135 12, 9, 18, 7, // 12 to 15
140 16, 5, 14, 11, // 16 to 19
145 20, 23, 22, 21, // 20 to 23
150 if (cpt2 == CPT2_FACEDIR || cpt2 == CPT2_COLORED_FACEDIR) {
151 u8 facedir = (param2 & 31) % 24;
152 u8 index = facedir * 4 + rot;
154 param2 |= rotate_facedir[index];
155 } else if (cpt2 == CPT2_4DIR || cpt2 == CPT2_COLORED_4DIR) {
156 u8 fourdir = param2 & 3;
157 u8 index = fourdir + rot;
159 param2 |= rotate_facedir[index];
161 } else if (cpt2 == CPT2_WALLMOUNTED ||
162 cpt2 == CPT2_COLORED_WALLMOUNTED) {
163 u8 wmountface = (param2 & 7);
167 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
169 param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
170 } else if (cpt2 == CPT2_DEGROTATE) {
171 int angle = param2; // in 1.5°
172 angle += 60 * rot; // don’t do that on u8
175 } else if (cpt2 == CPT2_COLORED_DEGROTATE) {
176 int angle = param2 & 0x1F; // in 15°
177 int color = param2 & 0xE0;
180 param2 = color | angle;
184 void transformNodeBox(const MapNode &n, const NodeBox &nodebox,
185 const NodeDefManager *nodemgr, std::vector<aabb3f> *p_boxes,
188 std::vector<aabb3f> &boxes = *p_boxes;
190 if (nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED) {
191 const auto &fixed = nodebox.fixed;
192 int facedir = n.getFaceDir(nodemgr, true);
193 u8 axisdir = facedir>>2;
196 boxes.reserve(boxes.size() + fixed.size());
197 for (aabb3f box : fixed) {
198 if (nodebox.type == NODEBOX_LEVELED)
199 box.MaxEdge.Y = (-0.5f + n.getLevel(nodemgr) / 64.0f) * BS;
205 box.MinEdge.rotateXZBy(-90);
206 box.MaxEdge.rotateXZBy(-90);
208 else if(facedir == 2)
210 box.MinEdge.rotateXZBy(180);
211 box.MaxEdge.rotateXZBy(180);
213 else if(facedir == 3)
215 box.MinEdge.rotateXZBy(90);
216 box.MaxEdge.rotateXZBy(90);
220 box.MinEdge.rotateYZBy(90);
221 box.MaxEdge.rotateYZBy(90);
224 box.MinEdge.rotateXYBy(90);
225 box.MaxEdge.rotateXYBy(90);
227 else if(facedir == 2)
229 box.MinEdge.rotateXYBy(180);
230 box.MaxEdge.rotateXYBy(180);
232 else if(facedir == 3)
234 box.MinEdge.rotateXYBy(-90);
235 box.MaxEdge.rotateXYBy(-90);
239 box.MinEdge.rotateYZBy(-90);
240 box.MaxEdge.rotateYZBy(-90);
243 box.MinEdge.rotateXYBy(-90);
244 box.MaxEdge.rotateXYBy(-90);
246 else if(facedir == 2)
248 box.MinEdge.rotateXYBy(180);
249 box.MaxEdge.rotateXYBy(180);
251 else if(facedir == 3)
253 box.MinEdge.rotateXYBy(90);
254 box.MaxEdge.rotateXYBy(90);
258 box.MinEdge.rotateXYBy(-90);
259 box.MaxEdge.rotateXYBy(-90);
262 box.MinEdge.rotateYZBy(90);
263 box.MaxEdge.rotateYZBy(90);
265 else if(facedir == 2)
267 box.MinEdge.rotateYZBy(180);
268 box.MaxEdge.rotateYZBy(180);
270 else if(facedir == 3)
272 box.MinEdge.rotateYZBy(-90);
273 box.MaxEdge.rotateYZBy(-90);
277 box.MinEdge.rotateXYBy(90);
278 box.MaxEdge.rotateXYBy(90);
281 box.MinEdge.rotateYZBy(-90);
282 box.MaxEdge.rotateYZBy(-90);
284 else if(facedir == 2)
286 box.MinEdge.rotateYZBy(180);
287 box.MaxEdge.rotateYZBy(180);
289 else if(facedir == 3)
291 box.MinEdge.rotateYZBy(90);
292 box.MaxEdge.rotateYZBy(90);
296 box.MinEdge.rotateXYBy(-180);
297 box.MaxEdge.rotateXYBy(-180);
300 box.MinEdge.rotateXZBy(90);
301 box.MaxEdge.rotateXZBy(90);
303 else if(facedir == 2)
305 box.MinEdge.rotateXZBy(180);
306 box.MaxEdge.rotateXZBy(180);
308 else if(facedir == 3)
310 box.MinEdge.rotateXZBy(-90);
311 box.MaxEdge.rotateXZBy(-90);
318 boxes.push_back(box);
321 else if(nodebox.type == NODEBOX_WALLMOUNTED)
323 v3s16 dir = n.getWallMountedDir(nodemgr);
326 if(dir == v3s16(0,1,0))
328 boxes.push_back(nodebox.wall_top);
331 else if(dir == v3s16(0,-1,0))
333 boxes.push_back(nodebox.wall_bottom);
340 nodebox.wall_side.MinEdge,
341 nodebox.wall_side.MaxEdge
344 for (v3f &vertex : vertices) {
345 if(dir == v3s16(-1,0,0))
346 vertex.rotateXZBy(0);
347 if(dir == v3s16(1,0,0))
348 vertex.rotateXZBy(180);
349 if(dir == v3s16(0,0,-1))
350 vertex.rotateXZBy(90);
351 if(dir == v3s16(0,0,1))
352 vertex.rotateXZBy(-90);
355 aabb3f box = aabb3f(vertices[0]);
356 box.addInternalPoint(vertices[1]);
357 boxes.push_back(box);
360 else if (nodebox.type == NODEBOX_CONNECTED)
362 size_t boxes_size = boxes.size();
363 boxes_size += nodebox.fixed.size();
364 const auto &c = nodebox.getConnected();
367 boxes_size += c.connect_top.size();
369 boxes_size += c.disconnected_top.size();
372 boxes_size += c.connect_bottom.size();
374 boxes_size += c.disconnected_bottom.size();
377 boxes_size += c.connect_front.size();
379 boxes_size += c.disconnected_front.size();
382 boxes_size += c.connect_left.size();
384 boxes_size += c.disconnected_left.size();
387 boxes_size += c.connect_back.size();
389 boxes_size += c.disconnected_back.size();
392 boxes_size += c.connect_right.size();
394 boxes_size += c.disconnected_right.size();
397 boxes_size += c.disconnected.size();
400 boxes_size += c.disconnected_sides.size();
402 boxes.reserve(boxes_size);
404 #define BOXESPUSHBACK(c) \
405 for (std::vector<aabb3f>::const_iterator \
407 it != (c).end(); ++it) \
408 (boxes).push_back(*it);
410 BOXESPUSHBACK(nodebox.fixed);
413 BOXESPUSHBACK(c.connect_top);
415 BOXESPUSHBACK(c.disconnected_top);
419 BOXESPUSHBACK(c.connect_bottom);
421 BOXESPUSHBACK(c.disconnected_bottom);
425 BOXESPUSHBACK(c.connect_front);
427 BOXESPUSHBACK(c.disconnected_front);
431 BOXESPUSHBACK(c.connect_left);
433 BOXESPUSHBACK(c.disconnected_left);
436 if (neighbors & 16) {
437 BOXESPUSHBACK(c.connect_back);
439 BOXESPUSHBACK(c.disconnected_back);
442 if (neighbors & 32) {
443 BOXESPUSHBACK(c.connect_right);
445 BOXESPUSHBACK(c.disconnected_right);
448 if (neighbors == 0) {
449 BOXESPUSHBACK(c.disconnected);
453 BOXESPUSHBACK(c.disconnected_sides);
457 else // NODEBOX_REGULAR
459 boxes.emplace_back(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2);
463 static inline void getNeighborConnectingFace(
464 const v3s16 &p, const NodeDefManager *nodedef,
465 Map *map, MapNode n, u8 bitmask, u8 *neighbors)
467 MapNode n2 = map->getNode(p);
468 if (nodedef->nodeboxConnects(n, n2, bitmask))
469 *neighbors |= bitmask;
472 u8 MapNode::getNeighbors(v3s16 p, Map *map) const
474 const NodeDefManager *nodedef = map->getNodeDefManager();
476 const ContentFeatures &f = nodedef->get(*this);
477 // locate possible neighboring nodes to connect to
478 if (f.drawtype == NDT_NODEBOX && f.node_box.type == NODEBOX_CONNECTED) {
482 getNeighborConnectingFace(p2, nodedef, map, *this, 1, &neighbors);
486 getNeighborConnectingFace(p2, nodedef, map, *this, 2, &neighbors);
490 getNeighborConnectingFace(p2, nodedef, map, *this, 4, &neighbors);
494 getNeighborConnectingFace(p2, nodedef, map, *this, 8, &neighbors);
498 getNeighborConnectingFace(p2, nodedef, map, *this, 16, &neighbors);
502 getNeighborConnectingFace(p2, nodedef, map, *this, 32, &neighbors);
508 void MapNode::getNodeBoxes(const NodeDefManager *nodemgr,
509 std::vector<aabb3f> *boxes, u8 neighbors) const
511 const ContentFeatures &f = nodemgr->get(*this);
512 transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
515 void MapNode::getCollisionBoxes(const NodeDefManager *nodemgr,
516 std::vector<aabb3f> *boxes, u8 neighbors) const
518 const ContentFeatures &f = nodemgr->get(*this);
519 if (f.collision_box.fixed.empty())
520 transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
522 transformNodeBox(*this, f.collision_box, nodemgr, boxes, neighbors);
525 void MapNode::getSelectionBoxes(const NodeDefManager *nodemgr,
526 std::vector<aabb3f> *boxes, u8 neighbors) const
528 const ContentFeatures &f = nodemgr->get(*this);
529 transformNodeBox(*this, f.selection_box, nodemgr, boxes, neighbors);
532 u8 MapNode::getMaxLevel(const NodeDefManager *nodemgr) const
534 const ContentFeatures &f = nodemgr->get(*this);
535 // todo: after update in all games leave only if (f.param_type_2 ==
536 if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
537 return LIQUID_LEVEL_MAX;
538 if(f.leveled || f.param_type_2 == CPT2_LEVELED)
539 return f.leveled_max;
543 u8 MapNode::getLevel(const NodeDefManager *nodemgr) const
545 const ContentFeatures &f = nodemgr->get(*this);
546 // todo: after update in all games leave only if (f.param_type_2 ==
547 if(f.liquid_type == LIQUID_SOURCE)
548 return LIQUID_LEVEL_SOURCE;
549 if (f.param_type_2 == CPT2_FLOWINGLIQUID)
550 return getParam2() & LIQUID_LEVEL_MASK;
551 if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 set
552 return getParam2() & LIQUID_LEVEL_MASK;
553 if (f.param_type_2 == CPT2_LEVELED) {
554 u8 level = getParam2() & LEVELED_MASK;
558 // Return static value from nodedef if param2 isn't used for level
559 if (f.leveled > f.leveled_max)
560 return f.leveled_max;
564 s8 MapNode::setLevel(const NodeDefManager *nodemgr, s16 level)
567 const ContentFeatures &f = nodemgr->get(*this);
568 if (f.param_type_2 == CPT2_FLOWINGLIQUID
569 || f.liquid_type == LIQUID_FLOWING
570 || f.liquid_type == LIQUID_SOURCE) {
571 if (level <= 0) { // liquid can’t exist with zero level
572 setContent(CONTENT_AIR);
575 if (level >= LIQUID_LEVEL_SOURCE) {
576 rest = level - LIQUID_LEVEL_SOURCE;
577 setContent(f.liquid_alternative_source_id);
580 setContent(f.liquid_alternative_flowing_id);
581 setParam2((level & LIQUID_LEVEL_MASK) | (getParam2() & ~LIQUID_LEVEL_MASK));
583 } else if (f.param_type_2 == CPT2_LEVELED) {
584 if (level < 0) { // zero means default for a leveled nodebox
587 } else if (level > f.leveled_max) {
588 rest = level - f.leveled_max;
589 level = f.leveled_max;
591 setParam2((level & LEVELED_MASK) | (getParam2() & ~LEVELED_MASK));
596 s8 MapNode::addLevel(const NodeDefManager *nodemgr, s16 add)
598 s16 level = getLevel(nodemgr);
600 return setLevel(nodemgr, level);
603 u32 MapNode::serializedLength(u8 version)
605 if(!ser_ver_supported(version))
606 throw VersionMismatchException("ERROR: MapNode format not supported");
619 void MapNode::serialize(u8 *dest, u8 version) const
621 if(!ser_ver_supported(version))
622 throw VersionMismatchException("ERROR: MapNode format not supported");
624 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
625 // in memory; conversion just won't work in this direction.
627 throw SerializationError("MapNode::serialize: serialization to "
628 "version < 24 not possible");
630 writeU16(dest+0, param0);
631 writeU8(dest+2, param1);
632 writeU8(dest+3, param2);
634 void MapNode::deSerialize(u8 *source, u8 version)
636 if(!ser_ver_supported(version))
637 throw VersionMismatchException("ERROR: MapNode format not supported");
641 deSerialize_pre22(source, version);
646 param0 = readU16(source+0);
647 param1 = readU8(source+2);
648 param2 = readU8(source+3);
650 param0 = readU8(source+0);
651 param1 = readU8(source+1);
652 param2 = readU8(source+2);
654 param0 |= ((param2&0xF0)<<4);
660 SharedBuffer<u8> MapNode::serializeBulk(int version,
661 const MapNode *nodes, u32 nodecount,
662 u8 content_width, u8 params_width)
664 if (!ser_ver_supported(version))
665 throw VersionMismatchException("ERROR: MapNode format not supported");
667 sanity_check(content_width == 2);
668 sanity_check(params_width == 2);
670 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
671 // in memory; conversion just won't work in this direction.
673 throw SerializationError("MapNode::serializeBulk: serialization to "
674 "version < 24 not possible");
676 SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
678 u32 start1 = content_width * nodecount;
679 u32 start2 = (content_width + 1) * nodecount;
682 for (u32 i = 0; i < nodecount; i++) {
683 writeU16(&databuf[i * 2], nodes[i].param0);
684 writeU8(&databuf[start1 + i], nodes[i].param1);
685 writeU8(&databuf[start2 + i], nodes[i].param2);
690 // Deserialize bulk node data
691 void MapNode::deSerializeBulk(std::istream &is, int version,
692 MapNode *nodes, u32 nodecount,
693 u8 content_width, u8 params_width)
695 if(!ser_ver_supported(version))
696 throw VersionMismatchException("ERROR: MapNode format not supported");
699 || (content_width != 1 && content_width != 2)
700 || params_width != 2)
701 FATAL_ERROR("Deserialize bulk node data error");
704 const u32 len = nodecount * (content_width + params_width);
705 Buffer<u8> databuf(len);
706 is.read(reinterpret_cast<char*>(*databuf), len);
708 // Deserialize content
709 if(content_width == 1)
711 for(u32 i=0; i<nodecount; i++)
712 nodes[i].param0 = readU8(&databuf[i]);
714 else if(content_width == 2)
716 for(u32 i=0; i<nodecount; i++)
717 nodes[i].param0 = readU16(&databuf[i*2]);
720 // Deserialize param1
721 u32 start1 = content_width * nodecount;
722 for(u32 i=0; i<nodecount; i++)
723 nodes[i].param1 = readU8(&databuf[start1 + i]);
725 // Deserialize param2
726 u32 start2 = (content_width + 1) * nodecount;
727 if(content_width == 1)
729 for(u32 i=0; i<nodecount; i++) {
730 nodes[i].param2 = readU8(&databuf[start2 + i]);
731 if(nodes[i].param0 > 0x7F){
732 nodes[i].param0 <<= 4;
733 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
734 nodes[i].param2 &= 0x0F;
738 else if(content_width == 2)
740 for(u32 i=0; i<nodecount; i++)
741 nodes[i].param2 = readU8(&databuf[start2 + i]);
748 void MapNode::deSerialize_pre22(const u8 *source, u8 version)
754 else if(version <= 9)
766 param0 |= (param2&0xf0)>>4;
771 // Convert special values from old version to new
774 // In these versions, CONTENT_IGNORE and CONTENT_AIR
776 // Version 19 is messed up with sometimes the old values and sometimes not
778 param0 = CONTENT_IGNORE;
779 else if(param0 == 254)
780 param0 = CONTENT_AIR;
783 // Translate to our known version
784 *this = mapnode_translate_to_internal(*this, version);