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/numeric.h"
33 static const Rotation wallmounted_to_rot[] = {
34 ROTATE_0, ROTATE_180, ROTATE_90, ROTATE_270
37 static const u8 rot_to_wallmounted[] = {
46 // Create directly from a nodename
47 // If name is unknown, sets CONTENT_IGNORE
48 MapNode::MapNode(INodeDefManager *ndef, const std::string &name,
49 u8 a_param1, u8 a_param2)
51 content_t id = CONTENT_IGNORE;
52 ndef->getId(name, id);
58 void MapNode::getColor(const ContentFeatures &f, video::SColor *color) const
61 *color = (*f.palette)[param2];
67 void MapNode::setLight(enum LightBank bank, u8 a_light, const ContentFeatures &f)
69 // If node doesn't contain light data, ignore this
70 if(f.param_type != CPT_LIGHT)
72 if(bank == LIGHTBANK_DAY)
75 param1 |= a_light & 0x0f;
77 else if(bank == LIGHTBANK_NIGHT)
80 param1 |= (a_light & 0x0f)<<4;
83 assert("Invalid light bank" == NULL);
86 void MapNode::setLight(enum LightBank bank, u8 a_light, INodeDefManager *nodemgr)
88 setLight(bank, a_light, nodemgr->get(*this));
91 bool MapNode::isLightDayNightEq(INodeDefManager *nodemgr) const
93 const ContentFeatures &f = nodemgr->get(*this);
96 if (f.param_type == CPT_LIGHT) {
97 u8 day = MYMAX(f.light_source, param1 & 0x0f);
98 u8 night = MYMAX(f.light_source, (param1 >> 4) & 0x0f);
99 isEqual = day == night;
107 u8 MapNode::getLight(enum LightBank bank, INodeDefManager *nodemgr) const
109 // Select the brightest of [light source, propagated light]
110 const ContentFeatures &f = nodemgr->get(*this);
113 if(f.param_type == CPT_LIGHT)
114 light = bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f;
118 return MYMAX(f.light_source, light);
121 u8 MapNode::getLightRaw(enum LightBank bank, const ContentFeatures &f) const
123 if(f.param_type == CPT_LIGHT)
124 return bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f;
128 u8 MapNode::getLightNoChecks(enum LightBank bank, const ContentFeatures *f) const
130 return MYMAX(f->light_source,
131 bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f);
134 bool MapNode::getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const
136 // Select the brightest of [light source, propagated light]
137 const ContentFeatures &f = nodemgr->get(*this);
138 if(f.param_type == CPT_LIGHT)
140 lightday = param1 & 0x0f;
141 lightnight = (param1>>4)&0x0f;
148 if(f.light_source > lightday)
149 lightday = f.light_source;
150 if(f.light_source > lightnight)
151 lightnight = f.light_source;
152 return f.param_type == CPT_LIGHT || f.light_source != 0;
155 u8 MapNode::getFaceDir(INodeDefManager *nodemgr) const
157 const ContentFeatures &f = nodemgr->get(*this);
158 if (f.param_type_2 == CPT2_FACEDIR ||
159 f.param_type_2 == CPT2_COLORED_FACEDIR)
160 return (getParam2() & 0x1F) % 24;
164 u8 MapNode::getWallMounted(INodeDefManager *nodemgr) const
166 const ContentFeatures &f = nodemgr->get(*this);
167 if (f.param_type_2 == CPT2_WALLMOUNTED ||
168 f.param_type_2 == CPT2_COLORED_WALLMOUNTED)
169 return getParam2() & 0x07;
173 v3s16 MapNode::getWallMountedDir(INodeDefManager *nodemgr) const
175 switch(getWallMounted(nodemgr))
177 case 0: default: return v3s16(0,1,0);
178 case 1: return v3s16(0,-1,0);
179 case 2: return v3s16(1,0,0);
180 case 3: return v3s16(-1,0,0);
181 case 4: return v3s16(0,0,1);
182 case 5: return v3s16(0,0,-1);
186 void MapNode::rotateAlongYAxis(INodeDefManager *nodemgr, Rotation rot)
188 ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
190 if (cpt2 == CPT2_FACEDIR || cpt2 == CPT2_COLORED_FACEDIR) {
191 static const u8 rotate_facedir[24 * 4] = {
192 // Table value = rotated facedir
193 // Columns: 0, 90, 180, 270 degrees rotation around vertical axis
194 // Rotation is anticlockwise as seen from above (+Y)
196 0, 1, 2, 3, // Initial facedir 0 to 3
201 4, 13, 10, 19, // 4 to 7
206 8, 17, 6, 15, // 8 to 11
211 12, 9, 18, 7, // 12 to 15
216 16, 5, 14, 11, // 16 to 19
221 20, 23, 22, 21, // 20 to 23
226 u8 facedir = (param2 & 31) % 24;
227 u8 index = facedir * 4 + rot;
229 param2 |= rotate_facedir[index];
230 } else if (cpt2 == CPT2_WALLMOUNTED ||
231 cpt2 == CPT2_COLORED_WALLMOUNTED) {
232 u8 wmountface = (param2 & 7);
236 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
238 param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
242 void transformNodeBox(const MapNode &n, const NodeBox &nodebox,
243 INodeDefManager *nodemgr, std::vector<aabb3f> *p_boxes, u8 neighbors = 0)
245 std::vector<aabb3f> &boxes = *p_boxes;
247 if (nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED) {
248 const std::vector<aabb3f> &fixed = nodebox.fixed;
249 int facedir = n.getFaceDir(nodemgr);
250 u8 axisdir = facedir>>2;
252 for (aabb3f box : fixed) {
253 if (nodebox.type == NODEBOX_LEVELED) {
254 box.MaxEdge.Y = -BS/2 + BS*((float)1/LEVELED_MAX) * n.getLevel(nodemgr);
261 box.MinEdge.rotateXZBy(-90);
262 box.MaxEdge.rotateXZBy(-90);
264 else if(facedir == 2)
266 box.MinEdge.rotateXZBy(180);
267 box.MaxEdge.rotateXZBy(180);
269 else if(facedir == 3)
271 box.MinEdge.rotateXZBy(90);
272 box.MaxEdge.rotateXZBy(90);
276 box.MinEdge.rotateYZBy(90);
277 box.MaxEdge.rotateYZBy(90);
280 box.MinEdge.rotateXYBy(90);
281 box.MaxEdge.rotateXYBy(90);
283 else if(facedir == 2)
285 box.MinEdge.rotateXYBy(180);
286 box.MaxEdge.rotateXYBy(180);
288 else if(facedir == 3)
290 box.MinEdge.rotateXYBy(-90);
291 box.MaxEdge.rotateXYBy(-90);
295 box.MinEdge.rotateYZBy(-90);
296 box.MaxEdge.rotateYZBy(-90);
299 box.MinEdge.rotateXYBy(-90);
300 box.MaxEdge.rotateXYBy(-90);
302 else if(facedir == 2)
304 box.MinEdge.rotateXYBy(180);
305 box.MaxEdge.rotateXYBy(180);
307 else if(facedir == 3)
309 box.MinEdge.rotateXYBy(90);
310 box.MaxEdge.rotateXYBy(90);
314 box.MinEdge.rotateXYBy(-90);
315 box.MaxEdge.rotateXYBy(-90);
318 box.MinEdge.rotateYZBy(90);
319 box.MaxEdge.rotateYZBy(90);
321 else if(facedir == 2)
323 box.MinEdge.rotateYZBy(180);
324 box.MaxEdge.rotateYZBy(180);
326 else if(facedir == 3)
328 box.MinEdge.rotateYZBy(-90);
329 box.MaxEdge.rotateYZBy(-90);
333 box.MinEdge.rotateXYBy(90);
334 box.MaxEdge.rotateXYBy(90);
337 box.MinEdge.rotateYZBy(-90);
338 box.MaxEdge.rotateYZBy(-90);
340 else if(facedir == 2)
342 box.MinEdge.rotateYZBy(180);
343 box.MaxEdge.rotateYZBy(180);
345 else if(facedir == 3)
347 box.MinEdge.rotateYZBy(90);
348 box.MaxEdge.rotateYZBy(90);
352 box.MinEdge.rotateXYBy(-180);
353 box.MaxEdge.rotateXYBy(-180);
356 box.MinEdge.rotateXZBy(90);
357 box.MaxEdge.rotateXZBy(90);
359 else if(facedir == 2)
361 box.MinEdge.rotateXZBy(180);
362 box.MaxEdge.rotateXZBy(180);
364 else if(facedir == 3)
366 box.MinEdge.rotateXZBy(-90);
367 box.MaxEdge.rotateXZBy(-90);
374 boxes.push_back(box);
377 else if(nodebox.type == NODEBOX_WALLMOUNTED)
379 v3s16 dir = n.getWallMountedDir(nodemgr);
382 if(dir == v3s16(0,1,0))
384 boxes.push_back(nodebox.wall_top);
387 else if(dir == v3s16(0,-1,0))
389 boxes.push_back(nodebox.wall_bottom);
396 nodebox.wall_side.MinEdge,
397 nodebox.wall_side.MaxEdge
400 for (v3f &vertex : vertices) {
401 if(dir == v3s16(-1,0,0))
402 vertex.rotateXZBy(0);
403 if(dir == v3s16(1,0,0))
404 vertex.rotateXZBy(180);
405 if(dir == v3s16(0,0,-1))
406 vertex.rotateXZBy(90);
407 if(dir == v3s16(0,0,1))
408 vertex.rotateXZBy(-90);
411 aabb3f box = aabb3f(vertices[0]);
412 box.addInternalPoint(vertices[1]);
413 boxes.push_back(box);
416 else if (nodebox.type == NODEBOX_CONNECTED)
418 size_t boxes_size = boxes.size();
419 boxes_size += nodebox.fixed.size();
421 boxes_size += nodebox.connect_top.size();
423 boxes_size += nodebox.connect_bottom.size();
425 boxes_size += nodebox.connect_front.size();
427 boxes_size += nodebox.connect_left.size();
429 boxes_size += nodebox.connect_back.size();
431 boxes_size += nodebox.connect_right.size();
432 boxes.reserve(boxes_size);
434 #define BOXESPUSHBACK(c) \
435 for (std::vector<aabb3f>::const_iterator \
437 it != (c).end(); ++it) \
438 (boxes).push_back(*it);
440 BOXESPUSHBACK(nodebox.fixed);
443 BOXESPUSHBACK(nodebox.connect_top);
445 BOXESPUSHBACK(nodebox.connect_bottom);
447 BOXESPUSHBACK(nodebox.connect_front);
449 BOXESPUSHBACK(nodebox.connect_left);
451 BOXESPUSHBACK(nodebox.connect_back);
453 BOXESPUSHBACK(nodebox.connect_right);
455 else // NODEBOX_REGULAR
457 boxes.emplace_back(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2);
461 static inline void getNeighborConnectingFace(
462 const v3s16 &p, INodeDefManager *nodedef,
463 Map *map, MapNode n, u8 bitmask, u8 *neighbors)
465 MapNode n2 = map->getNodeNoEx(p);
466 if (nodedef->nodeboxConnects(n, n2, bitmask))
467 *neighbors |= bitmask;
470 u8 MapNode::getNeighbors(v3s16 p, Map *map)
472 INodeDefManager *nodedef=map->getNodeDefManager();
474 const ContentFeatures &f = nodedef->get(*this);
475 // locate possible neighboring nodes to connect to
476 if (f.drawtype == NDT_NODEBOX && f.node_box.type == NODEBOX_CONNECTED) {
480 getNeighborConnectingFace(p2, nodedef, map, *this, 1, &neighbors);
484 getNeighborConnectingFace(p2, nodedef, map, *this, 2, &neighbors);
488 getNeighborConnectingFace(p2, nodedef, map, *this, 4, &neighbors);
492 getNeighborConnectingFace(p2, nodedef, map, *this, 8, &neighbors);
496 getNeighborConnectingFace(p2, nodedef, map, *this, 16, &neighbors);
500 getNeighborConnectingFace(p2, nodedef, map, *this, 32, &neighbors);
506 void MapNode::getNodeBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes, u8 neighbors)
508 const ContentFeatures &f = nodemgr->get(*this);
509 transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
512 void MapNode::getCollisionBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes, u8 neighbors)
514 const ContentFeatures &f = nodemgr->get(*this);
515 if (f.collision_box.fixed.empty())
516 transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
518 transformNodeBox(*this, f.collision_box, nodemgr, boxes, neighbors);
521 void MapNode::getSelectionBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes, u8 neighbors)
523 const ContentFeatures &f = nodemgr->get(*this);
524 transformNodeBox(*this, f.selection_box, nodemgr, boxes, neighbors);
527 u8 MapNode::getMaxLevel(INodeDefManager *nodemgr) const
529 const ContentFeatures &f = nodemgr->get(*this);
530 // todo: after update in all games leave only if (f.param_type_2 ==
531 if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
532 return LIQUID_LEVEL_MAX;
533 if(f.leveled || f.param_type_2 == CPT2_LEVELED)
538 u8 MapNode::getLevel(INodeDefManager *nodemgr) const
540 const ContentFeatures &f = nodemgr->get(*this);
541 // todo: after update in all games leave only if (f.param_type_2 ==
542 if(f.liquid_type == LIQUID_SOURCE)
543 return LIQUID_LEVEL_SOURCE;
544 if (f.param_type_2 == CPT2_FLOWINGLIQUID)
545 return getParam2() & LIQUID_LEVEL_MASK;
546 if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
547 return getParam2() & LIQUID_LEVEL_MASK;
548 if(f.leveled || f.param_type_2 == CPT2_LEVELED) {
549 u8 level = getParam2() & LEVELED_MASK;
552 if(f.leveled > LEVELED_MAX)
554 return f.leveled; //default
559 u8 MapNode::setLevel(INodeDefManager *nodemgr, s8 level)
563 setContent(CONTENT_AIR);
566 const ContentFeatures &f = nodemgr->get(*this);
567 if (f.param_type_2 == CPT2_FLOWINGLIQUID
568 || f.liquid_type == LIQUID_FLOWING
569 || f.liquid_type == LIQUID_SOURCE) {
570 if (level >= LIQUID_LEVEL_SOURCE) {
571 rest = level - LIQUID_LEVEL_SOURCE;
572 setContent(nodemgr->getId(f.liquid_alternative_source));
574 setContent(nodemgr->getId(f.liquid_alternative_flowing));
575 setParam2(level & LIQUID_LEVEL_MASK);
577 } else if (f.leveled || f.param_type_2 == CPT2_LEVELED) {
578 if (level > LEVELED_MAX) {
579 rest = level - LEVELED_MAX;
582 setParam2(level & LEVELED_MASK);
587 u8 MapNode::addLevel(INodeDefManager *nodemgr, s8 add)
589 s8 level = getLevel(nodemgr);
590 if (add == 0) level = 1;
592 return setLevel(nodemgr, level);
595 u32 MapNode::serializedLength(u8 version)
597 if(!ser_ver_supported(version))
598 throw VersionMismatchException("ERROR: MapNode format not supported");
611 void MapNode::serialize(u8 *dest, u8 version)
613 if(!ser_ver_supported(version))
614 throw VersionMismatchException("ERROR: MapNode format not supported");
616 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
617 // in memory; conversion just won't work in this direction.
619 throw SerializationError("MapNode::serialize: serialization to "
620 "version < 24 not possible");
622 writeU16(dest+0, param0);
623 writeU8(dest+2, param1);
624 writeU8(dest+3, param2);
626 void MapNode::deSerialize(u8 *source, u8 version)
628 if(!ser_ver_supported(version))
629 throw VersionMismatchException("ERROR: MapNode format not supported");
633 deSerialize_pre22(source, version);
638 param0 = readU16(source+0);
639 param1 = readU8(source+2);
640 param2 = readU8(source+3);
642 param0 = readU8(source+0);
643 param1 = readU8(source+1);
644 param2 = readU8(source+2);
646 param0 |= ((param2&0xF0)<<4);
651 void MapNode::serializeBulk(std::ostream &os, int version,
652 const MapNode *nodes, u32 nodecount,
653 u8 content_width, u8 params_width, bool compressed)
655 if (!ser_ver_supported(version))
656 throw VersionMismatchException("ERROR: MapNode format not supported");
658 sanity_check(content_width == 2);
659 sanity_check(params_width == 2);
661 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
662 // in memory; conversion just won't work in this direction.
664 throw SerializationError("MapNode::serializeBulk: serialization to "
665 "version < 24 not possible");
667 size_t databuf_size = nodecount * (content_width + params_width);
668 u8 *databuf = new u8[databuf_size];
670 u32 start1 = content_width * nodecount;
671 u32 start2 = (content_width + 1) * nodecount;
674 for (u32 i = 0; i < nodecount; i++) {
675 writeU16(&databuf[i * 2], nodes[i].param0);
676 writeU8(&databuf[start1 + i], nodes[i].param1);
677 writeU8(&databuf[start2 + i], nodes[i].param2);
681 Compress data to output stream
685 compressZlib(databuf, databuf_size, os);
687 os.write((const char*) &databuf[0], databuf_size);
692 // Deserialize bulk node data
693 void MapNode::deSerializeBulk(std::istream &is, int version,
694 MapNode *nodes, u32 nodecount,
695 u8 content_width, u8 params_width, bool compressed)
697 if(!ser_ver_supported(version))
698 throw VersionMismatchException("ERROR: MapNode format not supported");
701 || (content_width != 1 && content_width != 2)
702 || params_width != 2)
703 FATAL_ERROR("Deserialize bulk node data error");
705 // Uncompress or read data
706 u32 len = nodecount * (content_width + params_width);
707 SharedBuffer<u8> databuf(len);
710 std::ostringstream os(std::ios_base::binary);
711 decompressZlib(is, os);
712 std::string s = os.str();
714 throw SerializationError("deSerializeBulkNodes: "
715 "decompress resulted in invalid size");
716 memcpy(&databuf[0], s.c_str(), len);
720 is.read((char*) &databuf[0], len);
721 if(is.eof() || is.fail())
722 throw SerializationError("deSerializeBulkNodes: "
723 "failed to read bulk node data");
726 // Deserialize content
727 if(content_width == 1)
729 for(u32 i=0; i<nodecount; i++)
730 nodes[i].param0 = readU8(&databuf[i]);
732 else if(content_width == 2)
734 for(u32 i=0; i<nodecount; i++)
735 nodes[i].param0 = readU16(&databuf[i*2]);
738 // Deserialize param1
739 u32 start1 = content_width * nodecount;
740 for(u32 i=0; i<nodecount; i++)
741 nodes[i].param1 = readU8(&databuf[start1 + i]);
743 // Deserialize param2
744 u32 start2 = (content_width + 1) * nodecount;
745 if(content_width == 1)
747 for(u32 i=0; i<nodecount; i++) {
748 nodes[i].param2 = readU8(&databuf[start2 + i]);
749 if(nodes[i].param0 > 0x7F){
750 nodes[i].param0 <<= 4;
751 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
752 nodes[i].param2 &= 0x0F;
756 else if(content_width == 2)
758 for(u32 i=0; i<nodecount; i++)
759 nodes[i].param2 = readU8(&databuf[start2 + i]);
766 void MapNode::deSerialize_pre22(const u8 *source, u8 version)
772 else if(version <= 9)
784 param0 |= (param2&0xf0)>>4;
789 // Convert special values from old version to new
792 // In these versions, CONTENT_IGNORE and CONTENT_AIR
794 // Version 19 is fucked up with sometimes the old values and sometimes not
796 param0 = CONTENT_IGNORE;
797 else if(param0 == 254)
798 param0 = CONTENT_AIR;
801 // Translate to our known version
802 *this = mapnode_translate_to_internal(*this, version);