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"
24 #include "content_mapnode.h" // For mapnode_translate_*_internal
25 #include "serialization.h" // For ser_ver_supported
26 #include "util/serialize.h"
28 #include "util/numeric.h"
32 static const Rotation wallmounted_to_rot[] = {
33 ROTATE_0, ROTATE_180, ROTATE_90, ROTATE_270
36 static const u8 rot_to_wallmounted[] = {
45 // Create directly from a nodename
46 // If name is unknown, sets CONTENT_IGNORE
47 MapNode::MapNode(INodeDefManager *ndef, const std::string &name,
48 u8 a_param1, u8 a_param2)
50 content_t id = CONTENT_IGNORE;
51 ndef->getId(name, id);
57 void MapNode::setLight(enum LightBank bank, u8 a_light, const ContentFeatures &f)
59 // If node doesn't contain light data, ignore this
60 if(f.param_type != CPT_LIGHT)
62 if(bank == LIGHTBANK_DAY)
65 param1 |= a_light & 0x0f;
67 else if(bank == LIGHTBANK_NIGHT)
70 param1 |= (a_light & 0x0f)<<4;
73 assert("Invalid light bank" == NULL);
76 void MapNode::setLight(enum LightBank bank, u8 a_light, INodeDefManager *nodemgr)
78 setLight(bank, a_light, nodemgr->get(*this));
81 bool MapNode::isLightDayNightEq(INodeDefManager *nodemgr) const
83 const ContentFeatures &f = nodemgr->get(*this);
86 if (f.param_type == CPT_LIGHT) {
87 u8 day = MYMAX(f.light_source, param1 & 0x0f);
88 u8 night = MYMAX(f.light_source, (param1 >> 4) & 0x0f);
89 isEqual = day == night;
97 u8 MapNode::getLight(enum LightBank bank, INodeDefManager *nodemgr) const
99 // Select the brightest of [light source, propagated light]
100 const ContentFeatures &f = nodemgr->get(*this);
103 if(f.param_type == CPT_LIGHT)
104 light = bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f;
108 return MYMAX(f.light_source, light);
111 u8 MapNode::getLightRaw(enum LightBank bank, const ContentFeatures &f) const
113 if(f.param_type == CPT_LIGHT)
114 return bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f;
118 u8 MapNode::getLightNoChecks(enum LightBank bank, const ContentFeatures *f) const
120 return MYMAX(f->light_source,
121 bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f);
124 bool MapNode::getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *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(INodeDefManager *nodemgr) const
147 const ContentFeatures &f = nodemgr->get(*this);
148 if(f.param_type_2 == CPT2_FACEDIR)
149 return (getParam2() & 0x1F) % 24;
153 u8 MapNode::getWallMounted(INodeDefManager *nodemgr) const
155 const ContentFeatures &f = nodemgr->get(*this);
156 if(f.param_type_2 == CPT2_WALLMOUNTED)
157 return getParam2() & 0x07;
161 v3s16 MapNode::getWallMountedDir(INodeDefManager *nodemgr) const
163 switch(getWallMounted(nodemgr))
165 case 0: default: return v3s16(0,1,0);
166 case 1: return v3s16(0,-1,0);
167 case 2: return v3s16(1,0,0);
168 case 3: return v3s16(-1,0,0);
169 case 4: return v3s16(0,0,1);
170 case 5: return v3s16(0,0,-1);
174 void MapNode::rotateAlongYAxis(INodeDefManager *nodemgr, Rotation rot)
176 ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
178 if (cpt2 == CPT2_FACEDIR) {
179 static const u8 rotate_facedir[24 * 4] = {
180 // Table value = rotated facedir
181 // Columns: 0, 90, 180, 270 degrees rotation around vertical axis
182 // Rotation is anticlockwise as seen from above (+Y)
184 0, 1, 2, 3, // Initial facedir 0 to 3
189 4, 13, 10, 19, // 4 to 7
194 8, 17, 6, 15, // 8 to 11
199 12, 9, 18, 7, // 12 to 15
204 16, 5, 14, 11, // 16 to 19
209 20, 23, 22, 21, // 20 to 23
214 u8 facedir = (param2 & 31) % 24;
215 u8 index = facedir * 4 + rot;
217 param2 |= rotate_facedir[index];
218 } else if (cpt2 == CPT2_WALLMOUNTED) {
219 u8 wmountface = (param2 & 7);
223 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
225 param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
229 void transformNodeBox(const MapNode &n, const NodeBox &nodebox,
230 INodeDefManager *nodemgr, std::vector<aabb3f> *p_boxes, u8 neighbors = 0)
232 std::vector<aabb3f> &boxes = *p_boxes;
234 if (nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED) {
235 const std::vector<aabb3f> &fixed = nodebox.fixed;
236 int facedir = n.getFaceDir(nodemgr);
237 u8 axisdir = facedir>>2;
239 for(std::vector<aabb3f>::const_iterator
241 i != fixed.end(); ++i)
245 if (nodebox.type == NODEBOX_LEVELED) {
246 box.MaxEdge.Y = -BS/2 + BS*((float)1/LEVELED_MAX) * n.getLevel(nodemgr);
254 box.MinEdge.rotateXZBy(-90);
255 box.MaxEdge.rotateXZBy(-90);
257 else if(facedir == 2)
259 box.MinEdge.rotateXZBy(180);
260 box.MaxEdge.rotateXZBy(180);
262 else if(facedir == 3)
264 box.MinEdge.rotateXZBy(90);
265 box.MaxEdge.rotateXZBy(90);
269 box.MinEdge.rotateYZBy(90);
270 box.MaxEdge.rotateYZBy(90);
273 box.MinEdge.rotateXYBy(90);
274 box.MaxEdge.rotateXYBy(90);
276 else if(facedir == 2)
278 box.MinEdge.rotateXYBy(180);
279 box.MaxEdge.rotateXYBy(180);
281 else if(facedir == 3)
283 box.MinEdge.rotateXYBy(-90);
284 box.MaxEdge.rotateXYBy(-90);
288 box.MinEdge.rotateYZBy(-90);
289 box.MaxEdge.rotateYZBy(-90);
292 box.MinEdge.rotateXYBy(-90);
293 box.MaxEdge.rotateXYBy(-90);
295 else if(facedir == 2)
297 box.MinEdge.rotateXYBy(180);
298 box.MaxEdge.rotateXYBy(180);
300 else if(facedir == 3)
302 box.MinEdge.rotateXYBy(90);
303 box.MaxEdge.rotateXYBy(90);
307 box.MinEdge.rotateXYBy(-90);
308 box.MaxEdge.rotateXYBy(-90);
311 box.MinEdge.rotateYZBy(90);
312 box.MaxEdge.rotateYZBy(90);
314 else if(facedir == 2)
316 box.MinEdge.rotateYZBy(180);
317 box.MaxEdge.rotateYZBy(180);
319 else if(facedir == 3)
321 box.MinEdge.rotateYZBy(-90);
322 box.MaxEdge.rotateYZBy(-90);
326 box.MinEdge.rotateXYBy(90);
327 box.MaxEdge.rotateXYBy(90);
330 box.MinEdge.rotateYZBy(-90);
331 box.MaxEdge.rotateYZBy(-90);
333 else if(facedir == 2)
335 box.MinEdge.rotateYZBy(180);
336 box.MaxEdge.rotateYZBy(180);
338 else if(facedir == 3)
340 box.MinEdge.rotateYZBy(90);
341 box.MaxEdge.rotateYZBy(90);
345 box.MinEdge.rotateXYBy(-180);
346 box.MaxEdge.rotateXYBy(-180);
349 box.MinEdge.rotateXZBy(90);
350 box.MaxEdge.rotateXZBy(90);
352 else if(facedir == 2)
354 box.MinEdge.rotateXZBy(180);
355 box.MaxEdge.rotateXZBy(180);
357 else if(facedir == 3)
359 box.MinEdge.rotateXZBy(-90);
360 box.MaxEdge.rotateXZBy(-90);
367 boxes.push_back(box);
370 else if(nodebox.type == NODEBOX_WALLMOUNTED)
372 v3s16 dir = n.getWallMountedDir(nodemgr);
375 if(dir == v3s16(0,1,0))
377 boxes.push_back(nodebox.wall_top);
380 else if(dir == v3s16(0,-1,0))
382 boxes.push_back(nodebox.wall_bottom);
389 nodebox.wall_side.MinEdge,
390 nodebox.wall_side.MaxEdge
393 for(s32 i=0; i<2; i++)
395 if(dir == v3s16(-1,0,0))
396 vertices[i].rotateXZBy(0);
397 if(dir == v3s16(1,0,0))
398 vertices[i].rotateXZBy(180);
399 if(dir == v3s16(0,0,-1))
400 vertices[i].rotateXZBy(90);
401 if(dir == v3s16(0,0,1))
402 vertices[i].rotateXZBy(-90);
405 aabb3f box = aabb3f(vertices[0]);
406 box.addInternalPoint(vertices[1]);
407 boxes.push_back(box);
410 else if (nodebox.type == NODEBOX_CONNECTED)
412 size_t boxes_size = boxes.size();
413 boxes_size += nodebox.fixed.size();
415 boxes_size += nodebox.connect_top.size();
417 boxes_size += nodebox.connect_bottom.size();
419 boxes_size += nodebox.connect_front.size();
421 boxes_size += nodebox.connect_left.size();
423 boxes_size += nodebox.connect_back.size();
425 boxes_size += nodebox.connect_right.size();
426 boxes.reserve(boxes_size);
428 #define BOXESPUSHBACK(c) do { \
429 for (std::vector<aabb3f>::const_iterator \
431 it != (c).end(); ++it) \
432 (boxes).push_back(*it); \
435 BOXESPUSHBACK(nodebox.fixed);
438 BOXESPUSHBACK(nodebox.connect_top);
440 BOXESPUSHBACK(nodebox.connect_bottom);
442 BOXESPUSHBACK(nodebox.connect_front);
444 BOXESPUSHBACK(nodebox.connect_left);
446 BOXESPUSHBACK(nodebox.connect_back);
448 BOXESPUSHBACK(nodebox.connect_right);
450 else // NODEBOX_REGULAR
452 boxes.push_back(aabb3f(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2));
456 void MapNode::getNodeBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes, u8 neighbors)
458 const ContentFeatures &f = nodemgr->get(*this);
459 transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
462 void MapNode::getCollisionBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes, u8 neighbors)
464 const ContentFeatures &f = nodemgr->get(*this);
465 if (f.collision_box.fixed.empty())
466 transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
468 transformNodeBox(*this, f.collision_box, nodemgr, boxes, neighbors);
471 void MapNode::getSelectionBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes, u8 neighbors)
473 const ContentFeatures &f = nodemgr->get(*this);
474 transformNodeBox(*this, f.selection_box, nodemgr, boxes, neighbors);
477 u8 MapNode::getMaxLevel(INodeDefManager *nodemgr) const
479 const ContentFeatures &f = nodemgr->get(*this);
480 // todo: after update in all games leave only if (f.param_type_2 ==
481 if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
482 return LIQUID_LEVEL_MAX;
483 if(f.leveled || f.param_type_2 == CPT2_LEVELED)
488 u8 MapNode::getLevel(INodeDefManager *nodemgr) const
490 const ContentFeatures &f = nodemgr->get(*this);
491 // todo: after update in all games leave only if (f.param_type_2 ==
492 if(f.liquid_type == LIQUID_SOURCE)
493 return LIQUID_LEVEL_SOURCE;
494 if (f.param_type_2 == CPT2_FLOWINGLIQUID)
495 return getParam2() & LIQUID_LEVEL_MASK;
496 if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
497 return getParam2() & LIQUID_LEVEL_MASK;
498 if(f.leveled || f.param_type_2 == CPT2_LEVELED) {
499 u8 level = getParam2() & LEVELED_MASK;
502 if(f.leveled > LEVELED_MAX)
504 return f.leveled; //default
509 u8 MapNode::setLevel(INodeDefManager *nodemgr, s8 level)
513 setContent(CONTENT_AIR);
516 const ContentFeatures &f = nodemgr->get(*this);
517 if (f.param_type_2 == CPT2_FLOWINGLIQUID
518 || f.liquid_type == LIQUID_FLOWING
519 || f.liquid_type == LIQUID_SOURCE) {
520 if (level >= LIQUID_LEVEL_SOURCE) {
521 rest = level - LIQUID_LEVEL_SOURCE;
522 setContent(nodemgr->getId(f.liquid_alternative_source));
524 setContent(nodemgr->getId(f.liquid_alternative_flowing));
525 setParam2(level & LIQUID_LEVEL_MASK);
527 } else if (f.leveled || f.param_type_2 == CPT2_LEVELED) {
528 if (level > LEVELED_MAX) {
529 rest = level - LEVELED_MAX;
532 setParam2(level & LEVELED_MASK);
537 u8 MapNode::addLevel(INodeDefManager *nodemgr, s8 add)
539 s8 level = getLevel(nodemgr);
540 if (add == 0) level = 1;
542 return setLevel(nodemgr, level);
545 u32 MapNode::serializedLength(u8 version)
547 if(!ser_ver_supported(version))
548 throw VersionMismatchException("ERROR: MapNode format not supported");
552 else if(version <= 9)
554 else if(version <= 23)
559 void MapNode::serialize(u8 *dest, u8 version)
561 if(!ser_ver_supported(version))
562 throw VersionMismatchException("ERROR: MapNode format not supported");
564 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
565 // in memory; conversion just won't work in this direction.
567 throw SerializationError("MapNode::serialize: serialization to "
568 "version < 24 not possible");
570 writeU16(dest+0, param0);
571 writeU8(dest+2, param1);
572 writeU8(dest+3, param2);
574 void MapNode::deSerialize(u8 *source, u8 version)
576 if(!ser_ver_supported(version))
577 throw VersionMismatchException("ERROR: MapNode format not supported");
581 deSerialize_pre22(source, version);
586 param0 = readU16(source+0);
587 param1 = readU8(source+2);
588 param2 = readU8(source+3);
590 param0 = readU8(source+0);
591 param1 = readU8(source+1);
592 param2 = readU8(source+2);
594 param0 |= ((param2&0xF0)<<4);
599 void MapNode::serializeBulk(std::ostream &os, int version,
600 const MapNode *nodes, u32 nodecount,
601 u8 content_width, u8 params_width, bool compressed)
603 if(!ser_ver_supported(version))
604 throw VersionMismatchException("ERROR: MapNode format not supported");
606 sanity_check(content_width == 2);
607 sanity_check(params_width == 2);
609 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
610 // in memory; conversion just won't work in this direction.
612 throw SerializationError("MapNode::serializeBulk: serialization to "
613 "version < 24 not possible");
615 SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
618 for(u32 i=0; i<nodecount; i++)
619 writeU16(&databuf[i*2], nodes[i].param0);
622 u32 start1 = content_width * nodecount;
623 for(u32 i=0; i<nodecount; i++)
624 writeU8(&databuf[start1 + i], nodes[i].param1);
627 u32 start2 = (content_width + 1) * nodecount;
628 for(u32 i=0; i<nodecount; i++)
629 writeU8(&databuf[start2 + i], nodes[i].param2);
632 Compress data to output stream
637 compressZlib(databuf, os);
641 os.write((const char*) &databuf[0], databuf.getSize());
645 // Deserialize bulk node data
646 void MapNode::deSerializeBulk(std::istream &is, int version,
647 MapNode *nodes, u32 nodecount,
648 u8 content_width, u8 params_width, bool compressed)
650 if(!ser_ver_supported(version))
651 throw VersionMismatchException("ERROR: MapNode format not supported");
654 || (content_width != 1 && content_width != 2)
655 || params_width != 2)
656 FATAL_ERROR("Deserialize bulk node data error");
658 // Uncompress or read data
659 u32 len = nodecount * (content_width + params_width);
660 SharedBuffer<u8> databuf(len);
663 std::ostringstream os(std::ios_base::binary);
664 decompressZlib(is, os);
665 std::string s = os.str();
667 throw SerializationError("deSerializeBulkNodes: "
668 "decompress resulted in invalid size");
669 memcpy(&databuf[0], s.c_str(), len);
673 is.read((char*) &databuf[0], len);
674 if(is.eof() || is.fail())
675 throw SerializationError("deSerializeBulkNodes: "
676 "failed to read bulk node data");
679 // Deserialize content
680 if(content_width == 1)
682 for(u32 i=0; i<nodecount; i++)
683 nodes[i].param0 = readU8(&databuf[i]);
685 else if(content_width == 2)
687 for(u32 i=0; i<nodecount; i++)
688 nodes[i].param0 = readU16(&databuf[i*2]);
691 // Deserialize param1
692 u32 start1 = content_width * nodecount;
693 for(u32 i=0; i<nodecount; i++)
694 nodes[i].param1 = readU8(&databuf[start1 + i]);
696 // Deserialize param2
697 u32 start2 = (content_width + 1) * nodecount;
698 if(content_width == 1)
700 for(u32 i=0; i<nodecount; i++) {
701 nodes[i].param2 = readU8(&databuf[start2 + i]);
702 if(nodes[i].param0 > 0x7F){
703 nodes[i].param0 <<= 4;
704 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
705 nodes[i].param2 &= 0x0F;
709 else if(content_width == 2)
711 for(u32 i=0; i<nodecount; i++)
712 nodes[i].param2 = readU8(&databuf[start2 + i]);
719 void MapNode::deSerialize_pre22(u8 *source, u8 version)
725 else if(version <= 9)
737 param0 |= (param2&0xf0)>>4;
742 // Convert special values from old version to new
745 // In these versions, CONTENT_IGNORE and CONTENT_AIR
747 // Version 19 is fucked up with sometimes the old values and sometimes not
749 param0 = CONTENT_IGNORE;
750 else if(param0 == 254)
751 param0 = CONTENT_AIR;
754 // Translate to our known version
755 *this = mapnode_translate_to_internal(*this, version);