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(INodeDefManager *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(enum LightBank bank, u8 a_light, const ContentFeatures &f)
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(enum LightBank bank, u8 a_light, INodeDefManager *nodemgr)
89 setLight(bank, a_light, nodemgr->get(*this));
92 bool MapNode::isLightDayNightEq(INodeDefManager *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(enum LightBank bank, INodeDefManager *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(enum LightBank bank, const ContentFeatures &f) const
124 if(f.param_type == CPT_LIGHT)
125 return bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f;
129 u8 MapNode::getLightNoChecks(enum LightBank bank, const ContentFeatures *f) const
131 return MYMAX(f->light_source,
132 bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f);
135 bool MapNode::getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const
137 // Select the brightest of [light source, propagated light]
138 const ContentFeatures &f = nodemgr->get(*this);
139 if(f.param_type == CPT_LIGHT)
141 lightday = param1 & 0x0f;
142 lightnight = (param1>>4)&0x0f;
149 if(f.light_source > lightday)
150 lightday = f.light_source;
151 if(f.light_source > lightnight)
152 lightnight = f.light_source;
153 return f.param_type == CPT_LIGHT || f.light_source != 0;
156 u8 MapNode::getFaceDir(INodeDefManager *nodemgr, bool allow_wallmounted) const
158 const ContentFeatures &f = nodemgr->get(*this);
159 if (f.param_type_2 == CPT2_FACEDIR ||
160 f.param_type_2 == CPT2_COLORED_FACEDIR)
161 return (getParam2() & 0x1F) % 24;
162 if (allow_wallmounted && (f.param_type_2 == CPT2_WALLMOUNTED ||
163 f.param_type_2 == CPT2_COLORED_WALLMOUNTED))
164 return wallmounted_to_facedir[getParam2() & 0x07];
168 u8 MapNode::getWallMounted(INodeDefManager *nodemgr) const
170 const ContentFeatures &f = nodemgr->get(*this);
171 if (f.param_type_2 == CPT2_WALLMOUNTED ||
172 f.param_type_2 == CPT2_COLORED_WALLMOUNTED)
173 return getParam2() & 0x07;
177 v3s16 MapNode::getWallMountedDir(INodeDefManager *nodemgr) const
179 switch(getWallMounted(nodemgr))
181 case 0: default: return v3s16(0,1,0);
182 case 1: return v3s16(0,-1,0);
183 case 2: return v3s16(1,0,0);
184 case 3: return v3s16(-1,0,0);
185 case 4: return v3s16(0,0,1);
186 case 5: return v3s16(0,0,-1);
190 void MapNode::rotateAlongYAxis(INodeDefManager *nodemgr, Rotation rot)
192 ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
194 if (cpt2 == CPT2_FACEDIR || cpt2 == CPT2_COLORED_FACEDIR) {
195 static const u8 rotate_facedir[24 * 4] = {
196 // Table value = rotated facedir
197 // Columns: 0, 90, 180, 270 degrees rotation around vertical axis
198 // Rotation is anticlockwise as seen from above (+Y)
200 0, 1, 2, 3, // Initial facedir 0 to 3
205 4, 13, 10, 19, // 4 to 7
210 8, 17, 6, 15, // 8 to 11
215 12, 9, 18, 7, // 12 to 15
220 16, 5, 14, 11, // 16 to 19
225 20, 23, 22, 21, // 20 to 23
230 u8 facedir = (param2 & 31) % 24;
231 u8 index = facedir * 4 + rot;
233 param2 |= rotate_facedir[index];
234 } else if (cpt2 == CPT2_WALLMOUNTED ||
235 cpt2 == CPT2_COLORED_WALLMOUNTED) {
236 u8 wmountface = (param2 & 7);
240 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
242 param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
246 void transformNodeBox(const MapNode &n, const NodeBox &nodebox,
247 INodeDefManager *nodemgr, std::vector<aabb3f> *p_boxes, u8 neighbors = 0)
249 std::vector<aabb3f> &boxes = *p_boxes;
251 if (nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED) {
252 const std::vector<aabb3f> &fixed = nodebox.fixed;
253 int facedir = n.getFaceDir(nodemgr, true);
254 u8 axisdir = facedir>>2;
256 for (aabb3f box : fixed) {
257 if (nodebox.type == NODEBOX_LEVELED)
258 box.MaxEdge.Y = (-0.5f + n.getLevel(nodemgr) / 64.0f) * BS;
264 box.MinEdge.rotateXZBy(-90);
265 box.MaxEdge.rotateXZBy(-90);
267 else if(facedir == 2)
269 box.MinEdge.rotateXZBy(180);
270 box.MaxEdge.rotateXZBy(180);
272 else if(facedir == 3)
274 box.MinEdge.rotateXZBy(90);
275 box.MaxEdge.rotateXZBy(90);
279 box.MinEdge.rotateYZBy(90);
280 box.MaxEdge.rotateYZBy(90);
283 box.MinEdge.rotateXYBy(90);
284 box.MaxEdge.rotateXYBy(90);
286 else if(facedir == 2)
288 box.MinEdge.rotateXYBy(180);
289 box.MaxEdge.rotateXYBy(180);
291 else if(facedir == 3)
293 box.MinEdge.rotateXYBy(-90);
294 box.MaxEdge.rotateXYBy(-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.rotateXYBy(-90);
318 box.MaxEdge.rotateXYBy(-90);
321 box.MinEdge.rotateYZBy(90);
322 box.MaxEdge.rotateYZBy(90);
324 else if(facedir == 2)
326 box.MinEdge.rotateYZBy(180);
327 box.MaxEdge.rotateYZBy(180);
329 else if(facedir == 3)
331 box.MinEdge.rotateYZBy(-90);
332 box.MaxEdge.rotateYZBy(-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(-180);
356 box.MaxEdge.rotateXYBy(-180);
359 box.MinEdge.rotateXZBy(90);
360 box.MaxEdge.rotateXZBy(90);
362 else if(facedir == 2)
364 box.MinEdge.rotateXZBy(180);
365 box.MaxEdge.rotateXZBy(180);
367 else if(facedir == 3)
369 box.MinEdge.rotateXZBy(-90);
370 box.MaxEdge.rotateXZBy(-90);
377 boxes.push_back(box);
380 else if(nodebox.type == NODEBOX_WALLMOUNTED)
382 v3s16 dir = n.getWallMountedDir(nodemgr);
385 if(dir == v3s16(0,1,0))
387 boxes.push_back(nodebox.wall_top);
390 else if(dir == v3s16(0,-1,0))
392 boxes.push_back(nodebox.wall_bottom);
399 nodebox.wall_side.MinEdge,
400 nodebox.wall_side.MaxEdge
403 for (v3f &vertex : vertices) {
404 if(dir == v3s16(-1,0,0))
405 vertex.rotateXZBy(0);
406 if(dir == v3s16(1,0,0))
407 vertex.rotateXZBy(180);
408 if(dir == v3s16(0,0,-1))
409 vertex.rotateXZBy(90);
410 if(dir == v3s16(0,0,1))
411 vertex.rotateXZBy(-90);
414 aabb3f box = aabb3f(vertices[0]);
415 box.addInternalPoint(vertices[1]);
416 boxes.push_back(box);
419 else if (nodebox.type == NODEBOX_CONNECTED)
421 size_t boxes_size = boxes.size();
422 boxes_size += nodebox.fixed.size();
424 boxes_size += nodebox.connect_top.size();
426 boxes_size += nodebox.connect_bottom.size();
428 boxes_size += nodebox.connect_front.size();
430 boxes_size += nodebox.connect_left.size();
432 boxes_size += nodebox.connect_back.size();
434 boxes_size += nodebox.connect_right.size();
435 boxes.reserve(boxes_size);
437 #define BOXESPUSHBACK(c) \
438 for (std::vector<aabb3f>::const_iterator \
440 it != (c).end(); ++it) \
441 (boxes).push_back(*it);
443 BOXESPUSHBACK(nodebox.fixed);
446 BOXESPUSHBACK(nodebox.connect_top);
448 BOXESPUSHBACK(nodebox.connect_bottom);
450 BOXESPUSHBACK(nodebox.connect_front);
452 BOXESPUSHBACK(nodebox.connect_left);
454 BOXESPUSHBACK(nodebox.connect_back);
456 BOXESPUSHBACK(nodebox.connect_right);
458 else // NODEBOX_REGULAR
460 boxes.emplace_back(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2);
464 static inline void getNeighborConnectingFace(
465 const v3s16 &p, INodeDefManager *nodedef,
466 Map *map, MapNode n, u8 bitmask, u8 *neighbors)
468 MapNode n2 = map->getNodeNoEx(p);
469 if (nodedef->nodeboxConnects(n, n2, bitmask))
470 *neighbors |= bitmask;
473 u8 MapNode::getNeighbors(v3s16 p, Map *map)
475 INodeDefManager *nodedef=map->getNodeDefManager();
477 const ContentFeatures &f = nodedef->get(*this);
478 // locate possible neighboring nodes to connect to
479 if (f.drawtype == NDT_NODEBOX && f.node_box.type == NODEBOX_CONNECTED) {
483 getNeighborConnectingFace(p2, nodedef, map, *this, 1, &neighbors);
487 getNeighborConnectingFace(p2, nodedef, map, *this, 2, &neighbors);
491 getNeighborConnectingFace(p2, nodedef, map, *this, 4, &neighbors);
495 getNeighborConnectingFace(p2, nodedef, map, *this, 8, &neighbors);
499 getNeighborConnectingFace(p2, nodedef, map, *this, 16, &neighbors);
503 getNeighborConnectingFace(p2, nodedef, map, *this, 32, &neighbors);
509 void MapNode::getNodeBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes, u8 neighbors)
511 const ContentFeatures &f = nodemgr->get(*this);
512 transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
515 void MapNode::getCollisionBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes, u8 neighbors)
517 const ContentFeatures &f = nodemgr->get(*this);
518 if (f.collision_box.fixed.empty())
519 transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
521 transformNodeBox(*this, f.collision_box, nodemgr, boxes, neighbors);
524 void MapNode::getSelectionBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes, u8 neighbors)
526 const ContentFeatures &f = nodemgr->get(*this);
527 transformNodeBox(*this, f.selection_box, nodemgr, boxes, neighbors);
530 u8 MapNode::getMaxLevel(INodeDefManager *nodemgr) const
532 const ContentFeatures &f = nodemgr->get(*this);
533 // todo: after update in all games leave only if (f.param_type_2 ==
534 if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
535 return LIQUID_LEVEL_MAX;
536 if(f.leveled || f.param_type_2 == CPT2_LEVELED)
541 u8 MapNode::getLevel(INodeDefManager *nodemgr) const
543 const ContentFeatures &f = nodemgr->get(*this);
544 // todo: after update in all games leave only if (f.param_type_2 ==
545 if(f.liquid_type == LIQUID_SOURCE)
546 return LIQUID_LEVEL_SOURCE;
547 if (f.param_type_2 == CPT2_FLOWINGLIQUID)
548 return getParam2() & LIQUID_LEVEL_MASK;
549 if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
550 return getParam2() & LIQUID_LEVEL_MASK;
551 if(f.leveled || f.param_type_2 == CPT2_LEVELED) {
552 u8 level = getParam2() & LEVELED_MASK;
555 if(f.leveled > LEVELED_MAX)
557 return f.leveled; //default
562 u8 MapNode::setLevel(INodeDefManager *nodemgr, s8 level)
566 setContent(CONTENT_AIR);
569 const ContentFeatures &f = nodemgr->get(*this);
570 if (f.param_type_2 == CPT2_FLOWINGLIQUID
571 || f.liquid_type == LIQUID_FLOWING
572 || f.liquid_type == LIQUID_SOURCE) {
573 if (level >= LIQUID_LEVEL_SOURCE) {
574 rest = level - LIQUID_LEVEL_SOURCE;
575 setContent(nodemgr->getId(f.liquid_alternative_source));
577 setContent(nodemgr->getId(f.liquid_alternative_flowing));
578 setParam2(level & LIQUID_LEVEL_MASK);
580 } else if (f.leveled || f.param_type_2 == CPT2_LEVELED) {
581 if (level > LEVELED_MAX) {
582 rest = level - LEVELED_MAX;
585 setParam2(level & LEVELED_MASK);
590 u8 MapNode::addLevel(INodeDefManager *nodemgr, s8 add)
592 s8 level = getLevel(nodemgr);
593 if (add == 0) level = 1;
595 return setLevel(nodemgr, level);
598 u32 MapNode::serializedLength(u8 version)
600 if(!ser_ver_supported(version))
601 throw VersionMismatchException("ERROR: MapNode format not supported");
614 void MapNode::serialize(u8 *dest, u8 version)
616 if(!ser_ver_supported(version))
617 throw VersionMismatchException("ERROR: MapNode format not supported");
619 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
620 // in memory; conversion just won't work in this direction.
622 throw SerializationError("MapNode::serialize: serialization to "
623 "version < 24 not possible");
625 writeU16(dest+0, param0);
626 writeU8(dest+2, param1);
627 writeU8(dest+3, param2);
629 void MapNode::deSerialize(u8 *source, u8 version)
631 if(!ser_ver_supported(version))
632 throw VersionMismatchException("ERROR: MapNode format not supported");
636 deSerialize_pre22(source, version);
641 param0 = readU16(source+0);
642 param1 = readU8(source+2);
643 param2 = readU8(source+3);
645 param0 = readU8(source+0);
646 param1 = readU8(source+1);
647 param2 = readU8(source+2);
649 param0 |= ((param2&0xF0)<<4);
654 void MapNode::serializeBulk(std::ostream &os, int version,
655 const MapNode *nodes, u32 nodecount,
656 u8 content_width, u8 params_width, bool compressed)
658 if (!ser_ver_supported(version))
659 throw VersionMismatchException("ERROR: MapNode format not supported");
661 sanity_check(content_width == 2);
662 sanity_check(params_width == 2);
664 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
665 // in memory; conversion just won't work in this direction.
667 throw SerializationError("MapNode::serializeBulk: serialization to "
668 "version < 24 not possible");
670 size_t databuf_size = nodecount * (content_width + params_width);
671 u8 *databuf = new u8[databuf_size];
673 u32 start1 = content_width * nodecount;
674 u32 start2 = (content_width + 1) * nodecount;
677 for (u32 i = 0; i < nodecount; i++) {
678 writeU16(&databuf[i * 2], nodes[i].param0);
679 writeU8(&databuf[start1 + i], nodes[i].param1);
680 writeU8(&databuf[start2 + i], nodes[i].param2);
684 Compress data to output stream
688 compressZlib(databuf, databuf_size, os);
690 os.write((const char*) &databuf[0], databuf_size);
695 // Deserialize bulk node data
696 void MapNode::deSerializeBulk(std::istream &is, int version,
697 MapNode *nodes, u32 nodecount,
698 u8 content_width, u8 params_width, bool compressed)
700 if(!ser_ver_supported(version))
701 throw VersionMismatchException("ERROR: MapNode format not supported");
704 || (content_width != 1 && content_width != 2)
705 || params_width != 2)
706 FATAL_ERROR("Deserialize bulk node data error");
708 // Uncompress or read data
709 u32 len = nodecount * (content_width + params_width);
710 SharedBuffer<u8> databuf(len);
713 std::ostringstream os(std::ios_base::binary);
714 decompressZlib(is, os);
715 std::string s = os.str();
717 throw SerializationError("deSerializeBulkNodes: "
718 "decompress resulted in invalid size");
719 memcpy(&databuf[0], s.c_str(), len);
723 is.read((char*) &databuf[0], len);
724 if(is.eof() || is.fail())
725 throw SerializationError("deSerializeBulkNodes: "
726 "failed to read bulk node data");
729 // Deserialize content
730 if(content_width == 1)
732 for(u32 i=0; i<nodecount; i++)
733 nodes[i].param0 = readU8(&databuf[i]);
735 else if(content_width == 2)
737 for(u32 i=0; i<nodecount; i++)
738 nodes[i].param0 = readU16(&databuf[i*2]);
741 // Deserialize param1
742 u32 start1 = content_width * nodecount;
743 for(u32 i=0; i<nodecount; i++)
744 nodes[i].param1 = readU8(&databuf[start1 + i]);
746 // Deserialize param2
747 u32 start2 = (content_width + 1) * nodecount;
748 if(content_width == 1)
750 for(u32 i=0; i<nodecount; i++) {
751 nodes[i].param2 = readU8(&databuf[start2 + i]);
752 if(nodes[i].param0 > 0x7F){
753 nodes[i].param0 <<= 4;
754 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
755 nodes[i].param2 &= 0x0F;
759 else if(content_width == 2)
761 for(u32 i=0; i<nodecount; i++)
762 nodes[i].param2 = readU8(&databuf[start2 + i]);
769 void MapNode::deSerialize_pre22(const u8 *source, u8 version)
775 else if(version <= 9)
787 param0 |= (param2&0xf0)>>4;
792 // Convert special values from old version to new
795 // In these versions, CONTENT_IGNORE and CONTENT_AIR
797 // Version 19 is fucked up with sometimes the old values and sometimes not
799 param0 = CONTENT_IGNORE;
800 else if(param0 == 254)
801 param0 = CONTENT_AIR;
804 // Translate to our known version
805 *this = mapnode_translate_to_internal(*this, version);