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 void transformNodeBox(const MapNode &n, const NodeBox &nodebox,
218 INodeDefManager *nodemgr, std::vector<aabb3f> *p_boxes, u8 neighbors = 0)
220 std::vector<aabb3f> &boxes = *p_boxes;
222 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 if (nodebox.type == NODEBOX_CONNECTED)
400 size_t boxes_size = boxes.size();
401 boxes_size += nodebox.fixed.size();
403 boxes_size += nodebox.connect_top.size();
405 boxes_size += nodebox.connect_bottom.size();
407 boxes_size += nodebox.connect_front.size();
409 boxes_size += nodebox.connect_left.size();
411 boxes_size += nodebox.connect_back.size();
413 boxes_size += nodebox.connect_right.size();
414 boxes.reserve(boxes_size);
416 #define BOXESPUSHBACK(c) do { \
417 for (std::vector<aabb3f>::const_iterator \
419 it != (c).end(); ++it) \
420 (boxes).push_back(*it); \
423 BOXESPUSHBACK(nodebox.fixed);
426 BOXESPUSHBACK(nodebox.connect_top);
428 BOXESPUSHBACK(nodebox.connect_bottom);
430 BOXESPUSHBACK(nodebox.connect_front);
432 BOXESPUSHBACK(nodebox.connect_left);
434 BOXESPUSHBACK(nodebox.connect_back);
436 BOXESPUSHBACK(nodebox.connect_right);
438 else // NODEBOX_REGULAR
440 boxes.push_back(aabb3f(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2));
444 void MapNode::getNodeBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes, u8 neighbors)
446 const ContentFeatures &f = nodemgr->get(*this);
447 transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
450 void MapNode::getCollisionBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes, u8 neighbors)
452 const ContentFeatures &f = nodemgr->get(*this);
453 if (f.collision_box.fixed.empty())
454 transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
456 transformNodeBox(*this, f.collision_box, nodemgr, boxes, neighbors);
459 void MapNode::getSelectionBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes)
461 const ContentFeatures &f = nodemgr->get(*this);
462 transformNodeBox(*this, f.selection_box, nodemgr, boxes);
465 u8 MapNode::getMaxLevel(INodeDefManager *nodemgr) const
467 const ContentFeatures &f = nodemgr->get(*this);
468 // todo: after update in all games leave only if (f.param_type_2 ==
469 if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
470 return LIQUID_LEVEL_MAX;
471 if(f.leveled || f.param_type_2 == CPT2_LEVELED)
476 u8 MapNode::getLevel(INodeDefManager *nodemgr) const
478 const ContentFeatures &f = nodemgr->get(*this);
479 // todo: after update in all games leave only if (f.param_type_2 ==
480 if(f.liquid_type == LIQUID_SOURCE)
481 return LIQUID_LEVEL_SOURCE;
482 if (f.param_type_2 == CPT2_FLOWINGLIQUID)
483 return getParam2() & LIQUID_LEVEL_MASK;
484 if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
485 return getParam2() & LIQUID_LEVEL_MASK;
486 if(f.leveled || f.param_type_2 == CPT2_LEVELED) {
487 u8 level = getParam2() & LEVELED_MASK;
490 if(f.leveled > LEVELED_MAX)
492 return f.leveled; //default
497 u8 MapNode::setLevel(INodeDefManager *nodemgr, s8 level)
501 setContent(CONTENT_AIR);
504 const ContentFeatures &f = nodemgr->get(*this);
505 if (f.param_type_2 == CPT2_FLOWINGLIQUID
506 || f.liquid_type == LIQUID_FLOWING
507 || f.liquid_type == LIQUID_SOURCE) {
508 if (level >= LIQUID_LEVEL_SOURCE) {
509 rest = level - LIQUID_LEVEL_SOURCE;
510 setContent(nodemgr->getId(f.liquid_alternative_source));
512 setContent(nodemgr->getId(f.liquid_alternative_flowing));
513 setParam2(level & LIQUID_LEVEL_MASK);
515 } else if (f.leveled || f.param_type_2 == CPT2_LEVELED) {
516 if (level > LEVELED_MAX) {
517 rest = level - LEVELED_MAX;
520 setParam2(level & LEVELED_MASK);
525 u8 MapNode::addLevel(INodeDefManager *nodemgr, s8 add)
527 s8 level = getLevel(nodemgr);
528 if (add == 0) level = 1;
530 return setLevel(nodemgr, level);
533 u32 MapNode::serializedLength(u8 version)
535 if(!ser_ver_supported(version))
536 throw VersionMismatchException("ERROR: MapNode format not supported");
540 else if(version <= 9)
542 else if(version <= 23)
547 void MapNode::serialize(u8 *dest, u8 version)
549 if(!ser_ver_supported(version))
550 throw VersionMismatchException("ERROR: MapNode format not supported");
552 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
553 // in memory; conversion just won't work in this direction.
555 throw SerializationError("MapNode::serialize: serialization to "
556 "version < 24 not possible");
558 writeU16(dest+0, param0);
559 writeU8(dest+2, param1);
560 writeU8(dest+3, param2);
562 void MapNode::deSerialize(u8 *source, u8 version)
564 if(!ser_ver_supported(version))
565 throw VersionMismatchException("ERROR: MapNode format not supported");
569 deSerialize_pre22(source, version);
574 param0 = readU16(source+0);
575 param1 = readU8(source+2);
576 param2 = readU8(source+3);
578 param0 = readU8(source+0);
579 param1 = readU8(source+1);
580 param2 = readU8(source+2);
582 param0 |= ((param2&0xF0)<<4);
587 void MapNode::serializeBulk(std::ostream &os, int version,
588 const MapNode *nodes, u32 nodecount,
589 u8 content_width, u8 params_width, bool compressed)
591 if(!ser_ver_supported(version))
592 throw VersionMismatchException("ERROR: MapNode format not supported");
594 sanity_check(content_width == 2);
595 sanity_check(params_width == 2);
597 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
598 // in memory; conversion just won't work in this direction.
600 throw SerializationError("MapNode::serializeBulk: serialization to "
601 "version < 24 not possible");
603 SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
606 for(u32 i=0; i<nodecount; i++)
607 writeU16(&databuf[i*2], nodes[i].param0);
610 u32 start1 = content_width * nodecount;
611 for(u32 i=0; i<nodecount; i++)
612 writeU8(&databuf[start1 + i], nodes[i].param1);
615 u32 start2 = (content_width + 1) * nodecount;
616 for(u32 i=0; i<nodecount; i++)
617 writeU8(&databuf[start2 + i], nodes[i].param2);
620 Compress data to output stream
625 compressZlib(databuf, os);
629 os.write((const char*) &databuf[0], databuf.getSize());
633 // Deserialize bulk node data
634 void MapNode::deSerializeBulk(std::istream &is, int version,
635 MapNode *nodes, u32 nodecount,
636 u8 content_width, u8 params_width, bool compressed)
638 if(!ser_ver_supported(version))
639 throw VersionMismatchException("ERROR: MapNode format not supported");
642 || (content_width != 1 && content_width != 2)
643 || params_width != 2)
644 FATAL_ERROR("Deserialize bulk node data error");
646 // Uncompress or read data
647 u32 len = nodecount * (content_width + params_width);
648 SharedBuffer<u8> databuf(len);
651 std::ostringstream os(std::ios_base::binary);
652 decompressZlib(is, os);
653 std::string s = os.str();
655 throw SerializationError("deSerializeBulkNodes: "
656 "decompress resulted in invalid size");
657 memcpy(&databuf[0], s.c_str(), len);
661 is.read((char*) &databuf[0], len);
662 if(is.eof() || is.fail())
663 throw SerializationError("deSerializeBulkNodes: "
664 "failed to read bulk node data");
667 // Deserialize content
668 if(content_width == 1)
670 for(u32 i=0; i<nodecount; i++)
671 nodes[i].param0 = readU8(&databuf[i]);
673 else if(content_width == 2)
675 for(u32 i=0; i<nodecount; i++)
676 nodes[i].param0 = readU16(&databuf[i*2]);
679 // Deserialize param1
680 u32 start1 = content_width * nodecount;
681 for(u32 i=0; i<nodecount; i++)
682 nodes[i].param1 = readU8(&databuf[start1 + i]);
684 // Deserialize param2
685 u32 start2 = (content_width + 1) * nodecount;
686 if(content_width == 1)
688 for(u32 i=0; i<nodecount; i++) {
689 nodes[i].param2 = readU8(&databuf[start2 + i]);
690 if(nodes[i].param0 > 0x7F){
691 nodes[i].param0 <<= 4;
692 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
693 nodes[i].param2 &= 0x0F;
697 else if(content_width == 2)
699 for(u32 i=0; i<nodecount; i++)
700 nodes[i].param2 = readU8(&databuf[start2 + i]);
707 void MapNode::deSerialize_pre22(u8 *source, u8 version)
713 else if(version <= 9)
725 param0 |= (param2&0xf0)>>4;
730 // Convert special values from old version to new
733 // In these versions, CONTENT_IGNORE and CONTENT_AIR
735 // Version 19 is fucked up with sometimes the old values and sometimes not
737 param0 = CONTENT_IGNORE;
738 else if(param0 == 254)
739 param0 = CONTENT_AIR;
742 // Translate to our known version
743 *this = mapnode_translate_to_internal(*this, version);