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"
23 #include "main.h" // For g_settings
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::setLight(enum LightBank bank, u8 a_light, INodeDefManager *nodemgr)
60 // If node doesn't contain light data, ignore this
61 if(nodemgr->get(*this).param_type != CPT_LIGHT)
63 if(bank == LIGHTBANK_DAY)
66 param1 |= a_light & 0x0f;
68 else if(bank == LIGHTBANK_NIGHT)
71 param1 |= (a_light & 0x0f)<<4;
77 u8 MapNode::getLight(enum LightBank bank, INodeDefManager *nodemgr) const
79 // Select the brightest of [light source, propagated light]
80 const ContentFeatures &f = nodemgr->get(*this);
83 if(f.param_type == CPT_LIGHT)
84 light = bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f;
88 return MYMAX(f.light_source, light);
91 bool MapNode::getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const
93 // Select the brightest of [light source, propagated light]
94 const ContentFeatures &f = nodemgr->get(*this);
95 if(f.param_type == CPT_LIGHT)
97 lightday = param1 & 0x0f;
98 lightnight = (param1>>4)&0x0f;
105 if(f.light_source > lightday)
106 lightday = f.light_source;
107 if(f.light_source > lightnight)
108 lightnight = f.light_source;
109 return f.param_type == CPT_LIGHT || f.light_source != 0;
112 u8 MapNode::getFaceDir(INodeDefManager *nodemgr) const
114 const ContentFeatures &f = nodemgr->get(*this);
115 if(f.param_type_2 == CPT2_FACEDIR)
116 return getParam2() & 0x1F;
120 u8 MapNode::getWallMounted(INodeDefManager *nodemgr) const
122 const ContentFeatures &f = nodemgr->get(*this);
123 if(f.param_type_2 == CPT2_WALLMOUNTED)
124 return getParam2() & 0x07;
128 v3s16 MapNode::getWallMountedDir(INodeDefManager *nodemgr) const
130 switch(getWallMounted(nodemgr))
132 case 0: default: return v3s16(0,1,0);
133 case 1: return v3s16(0,-1,0);
134 case 2: return v3s16(1,0,0);
135 case 3: return v3s16(-1,0,0);
136 case 4: return v3s16(0,0,1);
137 case 5: return v3s16(0,0,-1);
141 void MapNode::rotateAlongYAxis(INodeDefManager *nodemgr, Rotation rot) {
142 ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
144 if (cpt2 == CPT2_FACEDIR) {
145 u8 newrot = param2 & 3;
147 param2 |= (newrot + rot) & 3;
148 } else if (cpt2 == CPT2_WALLMOUNTED) {
149 u8 wmountface = (param2 & 7);
153 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
155 param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
159 static std::vector<aabb3f> transformNodeBox(const MapNode &n,
160 const NodeBox &nodebox, INodeDefManager *nodemgr)
162 std::vector<aabb3f> boxes;
163 if(nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED)
165 const std::vector<aabb3f> &fixed = nodebox.fixed;
166 int facedir = n.getFaceDir(nodemgr);
167 u8 axisdir = facedir>>2;
169 for(std::vector<aabb3f>::const_iterator
171 i != fixed.end(); i++)
175 if (nodebox.type == NODEBOX_LEVELED) {
176 box.MaxEdge.Y = -BS/2 + BS*((float)1/LEVELED_MAX) * n.getLevel(nodemgr);
184 box.MinEdge.rotateXZBy(-90);
185 box.MaxEdge.rotateXZBy(-90);
187 else if(facedir == 2)
189 box.MinEdge.rotateXZBy(180);
190 box.MaxEdge.rotateXZBy(180);
192 else if(facedir == 3)
194 box.MinEdge.rotateXZBy(90);
195 box.MaxEdge.rotateXZBy(90);
199 box.MinEdge.rotateYZBy(90);
200 box.MaxEdge.rotateYZBy(90);
203 box.MinEdge.rotateXYBy(90);
204 box.MaxEdge.rotateXYBy(90);
206 else if(facedir == 2)
208 box.MinEdge.rotateXYBy(180);
209 box.MaxEdge.rotateXYBy(180);
211 else if(facedir == 3)
213 box.MinEdge.rotateXYBy(-90);
214 box.MaxEdge.rotateXYBy(-90);
218 box.MinEdge.rotateYZBy(-90);
219 box.MaxEdge.rotateYZBy(-90);
222 box.MinEdge.rotateXYBy(-90);
223 box.MaxEdge.rotateXYBy(-90);
225 else if(facedir == 2)
227 box.MinEdge.rotateXYBy(180);
228 box.MaxEdge.rotateXYBy(180);
230 else if(facedir == 3)
232 box.MinEdge.rotateXYBy(90);
233 box.MaxEdge.rotateXYBy(90);
237 box.MinEdge.rotateXYBy(-90);
238 box.MaxEdge.rotateXYBy(-90);
241 box.MinEdge.rotateYZBy(90);
242 box.MaxEdge.rotateYZBy(90);
244 else if(facedir == 2)
246 box.MinEdge.rotateYZBy(180);
247 box.MaxEdge.rotateYZBy(180);
249 else if(facedir == 3)
251 box.MinEdge.rotateYZBy(-90);
252 box.MaxEdge.rotateYZBy(-90);
256 box.MinEdge.rotateXYBy(90);
257 box.MaxEdge.rotateXYBy(90);
260 box.MinEdge.rotateYZBy(-90);
261 box.MaxEdge.rotateYZBy(-90);
263 else if(facedir == 2)
265 box.MinEdge.rotateYZBy(180);
266 box.MaxEdge.rotateYZBy(180);
268 else if(facedir == 3)
270 box.MinEdge.rotateYZBy(90);
271 box.MaxEdge.rotateYZBy(90);
275 box.MinEdge.rotateXYBy(-180);
276 box.MaxEdge.rotateXYBy(-180);
279 box.MinEdge.rotateXZBy(90);
280 box.MaxEdge.rotateXZBy(90);
282 else if(facedir == 2)
284 box.MinEdge.rotateXZBy(180);
285 box.MaxEdge.rotateXZBy(180);
287 else if(facedir == 3)
289 box.MinEdge.rotateXZBy(-90);
290 box.MaxEdge.rotateXZBy(-90);
297 boxes.push_back(box);
300 else if(nodebox.type == NODEBOX_WALLMOUNTED)
302 v3s16 dir = n.getWallMountedDir(nodemgr);
305 if(dir == v3s16(0,1,0))
307 boxes.push_back(nodebox.wall_top);
310 else if(dir == v3s16(0,-1,0))
312 boxes.push_back(nodebox.wall_bottom);
319 nodebox.wall_side.MinEdge,
320 nodebox.wall_side.MaxEdge
323 for(s32 i=0; i<2; i++)
325 if(dir == v3s16(-1,0,0))
326 vertices[i].rotateXZBy(0);
327 if(dir == v3s16(1,0,0))
328 vertices[i].rotateXZBy(180);
329 if(dir == v3s16(0,0,-1))
330 vertices[i].rotateXZBy(90);
331 if(dir == v3s16(0,0,1))
332 vertices[i].rotateXZBy(-90);
335 aabb3f box = aabb3f(vertices[0]);
336 box.addInternalPoint(vertices[1]);
337 boxes.push_back(box);
340 else // NODEBOX_REGULAR
342 boxes.push_back(aabb3f(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2));
347 std::vector<aabb3f> MapNode::getNodeBoxes(INodeDefManager *nodemgr) const
349 const ContentFeatures &f = nodemgr->get(*this);
350 return transformNodeBox(*this, f.node_box, nodemgr);
353 std::vector<aabb3f> MapNode::getCollisionBoxes(INodeDefManager *nodemgr) const
355 const ContentFeatures &f = nodemgr->get(*this);
356 if (f.collision_box.fixed.empty())
357 return transformNodeBox(*this, f.node_box, nodemgr);
359 return transformNodeBox(*this, f.collision_box, nodemgr);
362 std::vector<aabb3f> MapNode::getSelectionBoxes(INodeDefManager *nodemgr) const
364 const ContentFeatures &f = nodemgr->get(*this);
365 return transformNodeBox(*this, f.selection_box, nodemgr);
368 u8 MapNode::getMaxLevel(INodeDefManager *nodemgr) const
370 const ContentFeatures &f = nodemgr->get(*this);
371 // todo: after update in all games leave only if (f.param_type_2 ==
372 if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
373 return LIQUID_LEVEL_MAX;
374 if(f.leveled || f.param_type_2 == CPT2_LEVELED)
379 u8 MapNode::getLevel(INodeDefManager *nodemgr) const
381 const ContentFeatures &f = nodemgr->get(*this);
382 // todo: after update in all games leave only if (f.param_type_2 ==
383 if(f.liquid_type == LIQUID_SOURCE)
384 return LIQUID_LEVEL_SOURCE;
385 if (f.param_type_2 == CPT2_FLOWINGLIQUID)
386 return getParam2() & LIQUID_LEVEL_MASK;
387 if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
388 return getParam2() & LIQUID_LEVEL_MASK;
389 if(f.leveled || f.param_type_2 == CPT2_LEVELED) {
390 u8 level = getParam2() & LEVELED_MASK;
393 if(f.leveled > LEVELED_MAX)
395 return f.leveled; //default
400 u8 MapNode::setLevel(INodeDefManager *nodemgr, s8 level)
404 setContent(CONTENT_AIR);
407 const ContentFeatures &f = nodemgr->get(*this);
408 if (f.param_type_2 == CPT2_FLOWINGLIQUID
409 || f.liquid_type == LIQUID_FLOWING
410 || f.liquid_type == LIQUID_SOURCE) {
411 if (level >= LIQUID_LEVEL_SOURCE) {
412 rest = level - LIQUID_LEVEL_SOURCE;
413 setContent(nodemgr->getId(f.liquid_alternative_source));
415 setContent(nodemgr->getId(f.liquid_alternative_flowing));
416 setParam2(level & LIQUID_LEVEL_MASK);
418 } else if (f.leveled || f.param_type_2 == CPT2_LEVELED) {
419 if (level > LEVELED_MAX) {
420 rest = level - LEVELED_MAX;
423 setParam2(level & LEVELED_MASK);
428 u8 MapNode::addLevel(INodeDefManager *nodemgr, s8 add)
430 s8 level = getLevel(nodemgr);
431 if (add == 0) level = 1;
433 return setLevel(nodemgr, level);
436 void MapNode::freezeMelt(INodeDefManager *ndef) {
437 u8 level_was_max = this->getMaxLevel(ndef);
438 u8 level_was = this->getLevel(ndef);
439 this->setContent(ndef->getId(ndef->get(*this).freezemelt));
440 u8 level_now_max = this->getMaxLevel(ndef);
441 if (level_was_max && level_was_max != level_now_max) {
442 u8 want = (float)level_now_max / level_was_max * level_was;
445 if (want != level_was)
446 this->setLevel(ndef, want);
447 //errorstream<<"was="<<(int)level_was<<"/"<<(int)level_was_max<<" nowm="<<(int)want<<"/"<<(int)level_now_max<< " => "<<(int)this->getLevel(ndef)<< std::endl;
449 if (this->getMaxLevel(ndef) && !this->getLevel(ndef))
450 this->addLevel(ndef);
453 u32 MapNode::serializedLength(u8 version)
455 if(!ser_ver_supported(version))
456 throw VersionMismatchException("ERROR: MapNode format not supported");
460 else if(version <= 9)
462 else if(version <= 23)
467 void MapNode::serialize(u8 *dest, u8 version)
469 if(!ser_ver_supported(version))
470 throw VersionMismatchException("ERROR: MapNode format not supported");
472 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
473 // in memory; conversion just won't work in this direction.
475 throw SerializationError("MapNode::serialize: serialization to "
476 "version < 24 not possible");
478 writeU16(dest+0, param0);
479 writeU8(dest+2, param1);
480 writeU8(dest+3, param2);
482 void MapNode::deSerialize(u8 *source, u8 version)
484 if(!ser_ver_supported(version))
485 throw VersionMismatchException("ERROR: MapNode format not supported");
489 deSerialize_pre22(source, version);
494 param0 = readU16(source+0);
495 param1 = readU8(source+2);
496 param2 = readU8(source+3);
498 param0 = readU8(source+0);
499 param1 = readU8(source+1);
500 param2 = readU8(source+2);
502 param0 |= ((param2&0xF0)<<4);
507 void MapNode::serializeBulk(std::ostream &os, int version,
508 const MapNode *nodes, u32 nodecount,
509 u8 content_width, u8 params_width, bool compressed)
511 if(!ser_ver_supported(version))
512 throw VersionMismatchException("ERROR: MapNode format not supported");
514 assert(content_width == 2);
515 assert(params_width == 2);
517 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
518 // in memory; conversion just won't work in this direction.
520 throw SerializationError("MapNode::serializeBulk: serialization to "
521 "version < 24 not possible");
523 SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
526 for(u32 i=0; i<nodecount; i++)
527 writeU16(&databuf[i*2], nodes[i].param0);
530 u32 start1 = content_width * nodecount;
531 for(u32 i=0; i<nodecount; i++)
532 writeU8(&databuf[start1 + i], nodes[i].param1);
535 u32 start2 = (content_width + 1) * nodecount;
536 for(u32 i=0; i<nodecount; i++)
537 writeU8(&databuf[start2 + i], nodes[i].param2);
540 Compress data to output stream
545 compressZlib(databuf, os);
549 os.write((const char*) &databuf[0], databuf.getSize());
553 // Deserialize bulk node data
554 void MapNode::deSerializeBulk(std::istream &is, int version,
555 MapNode *nodes, u32 nodecount,
556 u8 content_width, u8 params_width, bool compressed)
558 if(!ser_ver_supported(version))
559 throw VersionMismatchException("ERROR: MapNode format not supported");
561 assert(version >= 22);
562 assert(content_width == 1 || content_width == 2);
563 assert(params_width == 2);
565 // Uncompress or read data
566 u32 len = nodecount * (content_width + params_width);
567 SharedBuffer<u8> databuf(len);
570 std::ostringstream os(std::ios_base::binary);
571 decompressZlib(is, os);
572 std::string s = os.str();
574 throw SerializationError("deSerializeBulkNodes: "
575 "decompress resulted in invalid size");
576 memcpy(&databuf[0], s.c_str(), len);
580 is.read((char*) &databuf[0], len);
581 if(is.eof() || is.fail())
582 throw SerializationError("deSerializeBulkNodes: "
583 "failed to read bulk node data");
586 // Deserialize content
587 if(content_width == 1)
589 for(u32 i=0; i<nodecount; i++)
590 nodes[i].param0 = readU8(&databuf[i]);
592 else if(content_width == 2)
594 for(u32 i=0; i<nodecount; i++)
595 nodes[i].param0 = readU16(&databuf[i*2]);
598 // Deserialize param1
599 u32 start1 = content_width * nodecount;
600 for(u32 i=0; i<nodecount; i++)
601 nodes[i].param1 = readU8(&databuf[start1 + i]);
603 // Deserialize param2
604 u32 start2 = (content_width + 1) * nodecount;
605 if(content_width == 1)
607 for(u32 i=0; i<nodecount; i++) {
608 nodes[i].param2 = readU8(&databuf[start2 + i]);
609 if(nodes[i].param0 > 0x7F){
610 nodes[i].param0 <<= 4;
611 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
612 nodes[i].param2 &= 0x0F;
616 else if(content_width == 2)
618 for(u32 i=0; i<nodecount; i++)
619 nodes[i].param2 = readU8(&databuf[start2 + i]);
626 void MapNode::deSerialize_pre22(u8 *source, u8 version)
632 else if(version <= 9)
644 param0 |= (param2&0xf0)>>4;
649 // Convert special values from old version to new
652 // In these versions, CONTENT_IGNORE and CONTENT_AIR
654 // Version 19 is fucked up with sometimes the old values and sometimes not
656 param0 = CONTENT_IGNORE;
657 else if(param0 == 254)
658 param0 = CONTENT_AIR;
661 // Translate to our known version
662 *this = mapnode_translate_to_internal(*this, version);