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, INodeDefManager *nodemgr)
59 // If node doesn't contain light data, ignore this
60 if(nodemgr->get(*this).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 bool MapNode::isLightDayNightEq(INodeDefManager *nodemgr) const
78 const ContentFeatures &f = nodemgr->get(*this);
81 if (f.param_type == CPT_LIGHT) {
82 u8 day = MYMAX(f.light_source, param1 & 0x0f);
83 u8 night = MYMAX(f.light_source, (param1 >> 4) & 0x0f);
84 isEqual = day == night;
92 u8 MapNode::getLight(enum LightBank bank, INodeDefManager *nodemgr) const
94 // Select the brightest of [light source, propagated light]
95 const ContentFeatures &f = nodemgr->get(*this);
98 if(f.param_type == CPT_LIGHT)
99 light = bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f;
103 return MYMAX(f.light_source, light);
106 u8 MapNode::getLightNoChecks(enum LightBank bank, const ContentFeatures *f) const
108 return MYMAX(f->light_source,
109 bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f);
112 bool MapNode::getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const
114 // Select the brightest of [light source, propagated light]
115 const ContentFeatures &f = nodemgr->get(*this);
116 if(f.param_type == CPT_LIGHT)
118 lightday = param1 & 0x0f;
119 lightnight = (param1>>4)&0x0f;
126 if(f.light_source > lightday)
127 lightday = f.light_source;
128 if(f.light_source > lightnight)
129 lightnight = f.light_source;
130 return f.param_type == CPT_LIGHT || f.light_source != 0;
133 u8 MapNode::getFaceDir(INodeDefManager *nodemgr) const
135 const ContentFeatures &f = nodemgr->get(*this);
136 if(f.param_type_2 == CPT2_FACEDIR)
137 return (getParam2() & 0x1F) % 24;
141 u8 MapNode::getWallMounted(INodeDefManager *nodemgr) const
143 const ContentFeatures &f = nodemgr->get(*this);
144 if(f.param_type_2 == CPT2_WALLMOUNTED)
145 return getParam2() & 0x07;
149 v3s16 MapNode::getWallMountedDir(INodeDefManager *nodemgr) const
151 switch(getWallMounted(nodemgr))
153 case 0: default: return v3s16(0,1,0);
154 case 1: return v3s16(0,-1,0);
155 case 2: return v3s16(1,0,0);
156 case 3: return v3s16(-1,0,0);
157 case 4: return v3s16(0,0,1);
158 case 5: return v3s16(0,0,-1);
162 void MapNode::rotateAlongYAxis(INodeDefManager *nodemgr, Rotation rot)
164 ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
166 if (cpt2 == CPT2_FACEDIR) {
167 static const u8 rotate_facedir[24 * 4] = {
168 // Table value = rotated facedir
169 // Columns: 0, 90, 180, 270 degrees rotation around vertical axis
170 // Rotation is anticlockwise as seen from above (+Y)
172 0, 1, 2, 3, // Initial facedir 0 to 3
177 4, 13, 10, 19, // 4 to 7
182 8, 17, 6, 15, // 8 to 11
187 12, 9, 18, 7, // 12 to 15
192 16, 5, 14, 11, // 16 to 19
197 20, 23, 22, 21, // 20 to 23
202 u8 facedir = (param2 & 31) % 24;
203 u8 index = facedir * 4 + rot;
205 param2 |= rotate_facedir[index];
206 } else if (cpt2 == CPT2_WALLMOUNTED) {
207 u8 wmountface = (param2 & 7);
211 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
213 param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
217 static std::vector<aabb3f> transformNodeBox(const MapNode &n,
218 const NodeBox &nodebox, INodeDefManager *nodemgr)
220 std::vector<aabb3f> boxes;
221 if(nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED)
223 const std::vector<aabb3f> &fixed = nodebox.fixed;
224 int facedir = n.getFaceDir(nodemgr);
225 u8 axisdir = facedir>>2;
227 for(std::vector<aabb3f>::const_iterator
229 i != fixed.end(); ++i)
233 if (nodebox.type == NODEBOX_LEVELED) {
234 box.MaxEdge.Y = -BS/2 + BS*((float)1/LEVELED_MAX) * n.getLevel(nodemgr);
242 box.MinEdge.rotateXZBy(-90);
243 box.MaxEdge.rotateXZBy(-90);
245 else if(facedir == 2)
247 box.MinEdge.rotateXZBy(180);
248 box.MaxEdge.rotateXZBy(180);
250 else if(facedir == 3)
252 box.MinEdge.rotateXZBy(90);
253 box.MaxEdge.rotateXZBy(90);
257 box.MinEdge.rotateYZBy(90);
258 box.MaxEdge.rotateYZBy(90);
261 box.MinEdge.rotateXYBy(90);
262 box.MaxEdge.rotateXYBy(90);
264 else if(facedir == 2)
266 box.MinEdge.rotateXYBy(180);
267 box.MaxEdge.rotateXYBy(180);
269 else if(facedir == 3)
271 box.MinEdge.rotateXYBy(-90);
272 box.MaxEdge.rotateXYBy(-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.rotateXYBy(-90);
296 box.MaxEdge.rotateXYBy(-90);
299 box.MinEdge.rotateYZBy(90);
300 box.MaxEdge.rotateYZBy(90);
302 else if(facedir == 2)
304 box.MinEdge.rotateYZBy(180);
305 box.MaxEdge.rotateYZBy(180);
307 else if(facedir == 3)
309 box.MinEdge.rotateYZBy(-90);
310 box.MaxEdge.rotateYZBy(-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(-180);
334 box.MaxEdge.rotateXYBy(-180);
337 box.MinEdge.rotateXZBy(90);
338 box.MaxEdge.rotateXZBy(90);
340 else if(facedir == 2)
342 box.MinEdge.rotateXZBy(180);
343 box.MaxEdge.rotateXZBy(180);
345 else if(facedir == 3)
347 box.MinEdge.rotateXZBy(-90);
348 box.MaxEdge.rotateXZBy(-90);
355 boxes.push_back(box);
358 else if(nodebox.type == NODEBOX_WALLMOUNTED)
360 v3s16 dir = n.getWallMountedDir(nodemgr);
363 if(dir == v3s16(0,1,0))
365 boxes.push_back(nodebox.wall_top);
368 else if(dir == v3s16(0,-1,0))
370 boxes.push_back(nodebox.wall_bottom);
377 nodebox.wall_side.MinEdge,
378 nodebox.wall_side.MaxEdge
381 for(s32 i=0; i<2; i++)
383 if(dir == v3s16(-1,0,0))
384 vertices[i].rotateXZBy(0);
385 if(dir == v3s16(1,0,0))
386 vertices[i].rotateXZBy(180);
387 if(dir == v3s16(0,0,-1))
388 vertices[i].rotateXZBy(90);
389 if(dir == v3s16(0,0,1))
390 vertices[i].rotateXZBy(-90);
393 aabb3f box = aabb3f(vertices[0]);
394 box.addInternalPoint(vertices[1]);
395 boxes.push_back(box);
398 else // NODEBOX_REGULAR
400 boxes.push_back(aabb3f(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2));
405 std::vector<aabb3f> MapNode::getNodeBoxes(INodeDefManager *nodemgr) const
407 const ContentFeatures &f = nodemgr->get(*this);
408 return transformNodeBox(*this, f.node_box, nodemgr);
411 std::vector<aabb3f> MapNode::getCollisionBoxes(INodeDefManager *nodemgr) const
413 const ContentFeatures &f = nodemgr->get(*this);
414 if (f.collision_box.fixed.empty())
415 return transformNodeBox(*this, f.node_box, nodemgr);
417 return transformNodeBox(*this, f.collision_box, nodemgr);
420 std::vector<aabb3f> MapNode::getSelectionBoxes(INodeDefManager *nodemgr) const
422 const ContentFeatures &f = nodemgr->get(*this);
423 return transformNodeBox(*this, f.selection_box, nodemgr);
426 u8 MapNode::getMaxLevel(INodeDefManager *nodemgr) const
428 const ContentFeatures &f = nodemgr->get(*this);
429 // todo: after update in all games leave only if (f.param_type_2 ==
430 if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
431 return LIQUID_LEVEL_MAX;
432 if(f.leveled || f.param_type_2 == CPT2_LEVELED)
437 u8 MapNode::getLevel(INodeDefManager *nodemgr) const
439 const ContentFeatures &f = nodemgr->get(*this);
440 // todo: after update in all games leave only if (f.param_type_2 ==
441 if(f.liquid_type == LIQUID_SOURCE)
442 return LIQUID_LEVEL_SOURCE;
443 if (f.param_type_2 == CPT2_FLOWINGLIQUID)
444 return getParam2() & LIQUID_LEVEL_MASK;
445 if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
446 return getParam2() & LIQUID_LEVEL_MASK;
447 if(f.leveled || f.param_type_2 == CPT2_LEVELED) {
448 u8 level = getParam2() & LEVELED_MASK;
451 if(f.leveled > LEVELED_MAX)
453 return f.leveled; //default
458 u8 MapNode::setLevel(INodeDefManager *nodemgr, s8 level)
462 setContent(CONTENT_AIR);
465 const ContentFeatures &f = nodemgr->get(*this);
466 if (f.param_type_2 == CPT2_FLOWINGLIQUID
467 || f.liquid_type == LIQUID_FLOWING
468 || f.liquid_type == LIQUID_SOURCE) {
469 if (level >= LIQUID_LEVEL_SOURCE) {
470 rest = level - LIQUID_LEVEL_SOURCE;
471 setContent(nodemgr->getId(f.liquid_alternative_source));
473 setContent(nodemgr->getId(f.liquid_alternative_flowing));
474 setParam2(level & LIQUID_LEVEL_MASK);
476 } else if (f.leveled || f.param_type_2 == CPT2_LEVELED) {
477 if (level > LEVELED_MAX) {
478 rest = level - LEVELED_MAX;
481 setParam2(level & LEVELED_MASK);
486 u8 MapNode::addLevel(INodeDefManager *nodemgr, s8 add)
488 s8 level = getLevel(nodemgr);
489 if (add == 0) level = 1;
491 return setLevel(nodemgr, level);
494 u32 MapNode::serializedLength(u8 version)
496 if(!ser_ver_supported(version))
497 throw VersionMismatchException("ERROR: MapNode format not supported");
501 else if(version <= 9)
503 else if(version <= 23)
508 void MapNode::serialize(u8 *dest, u8 version)
510 if(!ser_ver_supported(version))
511 throw VersionMismatchException("ERROR: MapNode format not supported");
513 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
514 // in memory; conversion just won't work in this direction.
516 throw SerializationError("MapNode::serialize: serialization to "
517 "version < 24 not possible");
519 writeU16(dest+0, param0);
520 writeU8(dest+2, param1);
521 writeU8(dest+3, param2);
523 void MapNode::deSerialize(u8 *source, u8 version)
525 if(!ser_ver_supported(version))
526 throw VersionMismatchException("ERROR: MapNode format not supported");
530 deSerialize_pre22(source, version);
535 param0 = readU16(source+0);
536 param1 = readU8(source+2);
537 param2 = readU8(source+3);
539 param0 = readU8(source+0);
540 param1 = readU8(source+1);
541 param2 = readU8(source+2);
543 param0 |= ((param2&0xF0)<<4);
548 void MapNode::serializeBulk(std::ostream &os, int version,
549 const MapNode *nodes, u32 nodecount,
550 u8 content_width, u8 params_width, bool compressed)
552 if(!ser_ver_supported(version))
553 throw VersionMismatchException("ERROR: MapNode format not supported");
555 sanity_check(content_width == 2);
556 sanity_check(params_width == 2);
558 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
559 // in memory; conversion just won't work in this direction.
561 throw SerializationError("MapNode::serializeBulk: serialization to "
562 "version < 24 not possible");
564 SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
567 for(u32 i=0; i<nodecount; i++)
568 writeU16(&databuf[i*2], nodes[i].param0);
571 u32 start1 = content_width * nodecount;
572 for(u32 i=0; i<nodecount; i++)
573 writeU8(&databuf[start1 + i], nodes[i].param1);
576 u32 start2 = (content_width + 1) * nodecount;
577 for(u32 i=0; i<nodecount; i++)
578 writeU8(&databuf[start2 + i], nodes[i].param2);
581 Compress data to output stream
586 compressZlib(databuf, os);
590 os.write((const char*) &databuf[0], databuf.getSize());
594 // Deserialize bulk node data
595 void MapNode::deSerializeBulk(std::istream &is, int version,
596 MapNode *nodes, u32 nodecount,
597 u8 content_width, u8 params_width, bool compressed)
599 if(!ser_ver_supported(version))
600 throw VersionMismatchException("ERROR: MapNode format not supported");
603 || (content_width != 1 && content_width != 2)
604 || params_width != 2)
605 FATAL_ERROR("Deserialize bulk node data error");
607 // Uncompress or read data
608 u32 len = nodecount * (content_width + params_width);
609 SharedBuffer<u8> databuf(len);
612 std::ostringstream os(std::ios_base::binary);
613 decompressZlib(is, os);
614 std::string s = os.str();
616 throw SerializationError("deSerializeBulkNodes: "
617 "decompress resulted in invalid size");
618 memcpy(&databuf[0], s.c_str(), len);
622 is.read((char*) &databuf[0], len);
623 if(is.eof() || is.fail())
624 throw SerializationError("deSerializeBulkNodes: "
625 "failed to read bulk node data");
628 // Deserialize content
629 if(content_width == 1)
631 for(u32 i=0; i<nodecount; i++)
632 nodes[i].param0 = readU8(&databuf[i]);
634 else if(content_width == 2)
636 for(u32 i=0; i<nodecount; i++)
637 nodes[i].param0 = readU16(&databuf[i*2]);
640 // Deserialize param1
641 u32 start1 = content_width * nodecount;
642 for(u32 i=0; i<nodecount; i++)
643 nodes[i].param1 = readU8(&databuf[start1 + i]);
645 // Deserialize param2
646 u32 start2 = (content_width + 1) * nodecount;
647 if(content_width == 1)
649 for(u32 i=0; i<nodecount; i++) {
650 nodes[i].param2 = readU8(&databuf[start2 + i]);
651 if(nodes[i].param0 > 0x7F){
652 nodes[i].param0 <<= 4;
653 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
654 nodes[i].param2 &= 0x0F;
658 else if(content_width == 2)
660 for(u32 i=0; i<nodecount; i++)
661 nodes[i].param2 = readU8(&databuf[start2 + i]);
668 void MapNode::deSerialize_pre22(u8 *source, u8 version)
674 else if(version <= 9)
686 param0 |= (param2&0xf0)>>4;
691 // Convert special values from old version to new
694 // In these versions, CONTENT_IGNORE and CONTENT_AIR
696 // Version 19 is fucked up with sometimes the old values and sometimes not
698 param0 = CONTENT_IGNORE;
699 else if(param0 == 254)
700 param0 = CONTENT_AIR;
703 // Translate to our known version
704 *this = mapnode_translate_to_internal(*this, version);