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"
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;
76 u8 MapNode::getLight(enum LightBank bank, INodeDefManager *nodemgr) const
78 // Select the brightest of [light source, propagated light]
79 const ContentFeatures &f = nodemgr->get(*this);
81 if(f.param_type == CPT_LIGHT)
83 if(bank == LIGHTBANK_DAY)
84 light = param1 & 0x0f;
85 else if(bank == LIGHTBANK_NIGHT)
86 light = (param1>>4)&0x0f;
90 if(f.light_source > light)
91 light = f.light_source;
95 bool MapNode::getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const
97 // Select the brightest of [light source, propagated light]
98 const ContentFeatures &f = nodemgr->get(*this);
99 if(f.param_type == CPT_LIGHT)
101 lightday = param1 & 0x0f;
102 lightnight = (param1>>4)&0x0f;
109 if(f.light_source > lightday)
110 lightday = f.light_source;
111 if(f.light_source > lightnight)
112 lightnight = f.light_source;
113 return f.param_type == CPT_LIGHT || f.light_source != 0;
116 u8 MapNode::getFaceDir(INodeDefManager *nodemgr) const
118 const ContentFeatures &f = nodemgr->get(*this);
119 if(f.param_type_2 == CPT2_FACEDIR)
120 return getParam2() & 0x1F;
124 u8 MapNode::getWallMounted(INodeDefManager *nodemgr) const
126 const ContentFeatures &f = nodemgr->get(*this);
127 if(f.param_type_2 == CPT2_WALLMOUNTED)
128 return getParam2() & 0x07;
132 v3s16 MapNode::getWallMountedDir(INodeDefManager *nodemgr) const
134 switch(getWallMounted(nodemgr))
136 case 0: default: return v3s16(0,1,0);
137 case 1: return v3s16(0,-1,0);
138 case 2: return v3s16(1,0,0);
139 case 3: return v3s16(-1,0,0);
140 case 4: return v3s16(0,0,1);
141 case 5: return v3s16(0,0,-1);
145 void MapNode::rotateAlongYAxis(INodeDefManager *nodemgr, Rotation rot) {
146 ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
148 if (cpt2 == CPT2_FACEDIR) {
149 u8 newrot = param2 & 3;
151 param2 |= (newrot + rot) & 3;
152 } else if (cpt2 == CPT2_WALLMOUNTED) {
153 u8 wmountface = (param2 & 7);
157 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
159 param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
163 static std::vector<aabb3f> transformNodeBox(const MapNode &n,
164 const NodeBox &nodebox, INodeDefManager *nodemgr)
166 std::vector<aabb3f> boxes;
167 if(nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED)
169 const std::vector<aabb3f> &fixed = nodebox.fixed;
170 int facedir = n.getFaceDir(nodemgr);
171 u8 axisdir = facedir>>2;
173 for(std::vector<aabb3f>::const_iterator
175 i != fixed.end(); i++)
179 if (nodebox.type == NODEBOX_LEVELED) {
180 box.MaxEdge.Y = -BS/2 + BS*((float)1/LEVELED_MAX) * n.getLevel(nodemgr);
188 box.MinEdge.rotateXZBy(-90);
189 box.MaxEdge.rotateXZBy(-90);
191 else if(facedir == 2)
193 box.MinEdge.rotateXZBy(180);
194 box.MaxEdge.rotateXZBy(180);
196 else if(facedir == 3)
198 box.MinEdge.rotateXZBy(90);
199 box.MaxEdge.rotateXZBy(90);
203 box.MinEdge.rotateYZBy(90);
204 box.MaxEdge.rotateYZBy(90);
207 box.MinEdge.rotateXYBy(90);
208 box.MaxEdge.rotateXYBy(90);
210 else if(facedir == 2)
212 box.MinEdge.rotateXYBy(180);
213 box.MaxEdge.rotateXYBy(180);
215 else if(facedir == 3)
217 box.MinEdge.rotateXYBy(-90);
218 box.MaxEdge.rotateXYBy(-90);
222 box.MinEdge.rotateYZBy(-90);
223 box.MaxEdge.rotateYZBy(-90);
226 box.MinEdge.rotateXYBy(-90);
227 box.MaxEdge.rotateXYBy(-90);
229 else if(facedir == 2)
231 box.MinEdge.rotateXYBy(180);
232 box.MaxEdge.rotateXYBy(180);
234 else if(facedir == 3)
236 box.MinEdge.rotateXYBy(90);
237 box.MaxEdge.rotateXYBy(90);
241 box.MinEdge.rotateXYBy(-90);
242 box.MaxEdge.rotateXYBy(-90);
245 box.MinEdge.rotateYZBy(90);
246 box.MaxEdge.rotateYZBy(90);
248 else if(facedir == 2)
250 box.MinEdge.rotateYZBy(180);
251 box.MaxEdge.rotateYZBy(180);
253 else if(facedir == 3)
255 box.MinEdge.rotateYZBy(-90);
256 box.MaxEdge.rotateYZBy(-90);
260 box.MinEdge.rotateXYBy(90);
261 box.MaxEdge.rotateXYBy(90);
264 box.MinEdge.rotateYZBy(-90);
265 box.MaxEdge.rotateYZBy(-90);
267 else if(facedir == 2)
269 box.MinEdge.rotateYZBy(180);
270 box.MaxEdge.rotateYZBy(180);
272 else if(facedir == 3)
274 box.MinEdge.rotateYZBy(90);
275 box.MaxEdge.rotateYZBy(90);
279 box.MinEdge.rotateXYBy(-180);
280 box.MaxEdge.rotateXYBy(-180);
283 box.MinEdge.rotateXZBy(90);
284 box.MaxEdge.rotateXZBy(90);
286 else if(facedir == 2)
288 box.MinEdge.rotateXZBy(180);
289 box.MaxEdge.rotateXZBy(180);
291 else if(facedir == 3)
293 box.MinEdge.rotateXZBy(-90);
294 box.MaxEdge.rotateXZBy(-90);
301 boxes.push_back(box);
304 else if(nodebox.type == NODEBOX_WALLMOUNTED)
306 v3s16 dir = n.getWallMountedDir(nodemgr);
309 if(dir == v3s16(0,1,0))
311 boxes.push_back(nodebox.wall_top);
314 else if(dir == v3s16(0,-1,0))
316 boxes.push_back(nodebox.wall_bottom);
323 nodebox.wall_side.MinEdge,
324 nodebox.wall_side.MaxEdge
327 for(s32 i=0; i<2; i++)
329 if(dir == v3s16(-1,0,0))
330 vertices[i].rotateXZBy(0);
331 if(dir == v3s16(1,0,0))
332 vertices[i].rotateXZBy(180);
333 if(dir == v3s16(0,0,-1))
334 vertices[i].rotateXZBy(90);
335 if(dir == v3s16(0,0,1))
336 vertices[i].rotateXZBy(-90);
339 aabb3f box = aabb3f(vertices[0]);
340 box.addInternalPoint(vertices[1]);
341 boxes.push_back(box);
344 else // NODEBOX_REGULAR
346 boxes.push_back(aabb3f(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2));
351 std::vector<aabb3f> MapNode::getNodeBoxes(INodeDefManager *nodemgr) const
353 const ContentFeatures &f = nodemgr->get(*this);
354 return transformNodeBox(*this, f.node_box, nodemgr);
357 std::vector<aabb3f> MapNode::getSelectionBoxes(INodeDefManager *nodemgr) const
359 const ContentFeatures &f = nodemgr->get(*this);
360 return transformNodeBox(*this, f.selection_box, nodemgr);
363 u8 MapNode::getMaxLevel(INodeDefManager *nodemgr) const
365 const ContentFeatures &f = nodemgr->get(*this);
366 // todo: after update in all games leave only if (f.param_type_2 ==
367 if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
368 return LIQUID_LEVEL_MAX;
369 if(f.leveled || f.param_type_2 == CPT2_LEVELED)
374 u8 MapNode::getLevel(INodeDefManager *nodemgr) const
376 const ContentFeatures &f = nodemgr->get(*this);
377 // todo: after update in all games leave only if (f.param_type_2 ==
378 if(f.liquid_type == LIQUID_SOURCE)
379 return LIQUID_LEVEL_SOURCE;
380 if (f.param_type_2 == CPT2_FLOWINGLIQUID)
381 return getParam2() & LIQUID_LEVEL_MASK;
382 if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
383 return getParam2() & LIQUID_LEVEL_MASK;
384 if(f.leveled || f.param_type_2 == CPT2_LEVELED) {
385 u8 level = getParam2() & LEVELED_MASK;
386 if(level) return level;
387 if(f.leveled > LEVELED_MAX) return LEVELED_MAX;
388 return f.leveled; //default
393 u8 MapNode::setLevel(INodeDefManager *nodemgr, s8 level)
397 setContent(CONTENT_AIR);
400 const ContentFeatures &f = nodemgr->get(*this);
401 if ( f.param_type_2 == CPT2_FLOWINGLIQUID
402 || f.liquid_type == LIQUID_FLOWING
403 || f.liquid_type == LIQUID_SOURCE) {
404 if (level >= LIQUID_LEVEL_SOURCE) {
405 rest = level - LIQUID_LEVEL_SOURCE;
406 setContent(nodemgr->getId(f.liquid_alternative_source));
408 setContent(nodemgr->getId(f.liquid_alternative_flowing));
409 setParam2(level & LIQUID_LEVEL_MASK);
411 } else if (f.leveled || f.param_type_2 == CPT2_LEVELED) {
412 if (level > LEVELED_MAX) {
413 rest = level - LEVELED_MAX;
416 setParam2(level & LEVELED_MASK);
421 u8 MapNode::addLevel(INodeDefManager *nodemgr, s8 add)
423 s8 level = getLevel(nodemgr);
424 if (add == 0) level = 1;
426 return setLevel(nodemgr, level);
429 void MapNode::freezeMelt(INodeDefManager *ndef) {
430 u8 level_was_max = this->getMaxLevel(ndef);
431 u8 level_was = this->getLevel(ndef);
432 this->setContent(ndef->getId(ndef->get(*this).freezemelt));
433 u8 level_now_max = this->getMaxLevel(ndef);
434 if (level_was_max && level_was_max != level_now_max) {
435 u8 want = (float)level_now_max / level_was_max * level_was;
438 if (want != level_was)
439 this->setLevel(ndef, want);
440 //errorstream<<"was="<<(int)level_was<<"/"<<(int)level_was_max<<" nowm="<<(int)want<<"/"<<(int)level_now_max<< " => "<<(int)this->getLevel(ndef)<< std::endl;
442 if (this->getMaxLevel(ndef) && !this->getLevel(ndef))
443 this->addLevel(ndef);
446 u32 MapNode::serializedLength(u8 version)
448 if(!ser_ver_supported(version))
449 throw VersionMismatchException("ERROR: MapNode format not supported");
453 else if(version <= 9)
455 else if(version <= 23)
460 void MapNode::serialize(u8 *dest, u8 version)
462 if(!ser_ver_supported(version))
463 throw VersionMismatchException("ERROR: MapNode format not supported");
465 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
466 // in memory; conversion just won't work in this direction.
468 throw SerializationError("MapNode::serialize: serialization to "
469 "version < 24 not possible");
471 writeU16(dest+0, param0);
472 writeU8(dest+2, param1);
473 writeU8(dest+3, param2);
475 void MapNode::deSerialize(u8 *source, u8 version)
477 if(!ser_ver_supported(version))
478 throw VersionMismatchException("ERROR: MapNode format not supported");
482 deSerialize_pre22(source, version);
487 param0 = readU16(source+0);
488 param1 = readU8(source+2);
489 param2 = readU8(source+3);
492 param0 = readU8(source+0);
493 param1 = readU8(source+1);
494 param2 = readU8(source+2);
496 param0 |= ((param2&0xF0)<<4);
501 void MapNode::serializeBulk(std::ostream &os, int version,
502 const MapNode *nodes, u32 nodecount,
503 u8 content_width, u8 params_width, bool compressed)
505 if(!ser_ver_supported(version))
506 throw VersionMismatchException("ERROR: MapNode format not supported");
508 assert(content_width == 2);
509 assert(params_width == 2);
511 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
512 // in memory; conversion just won't work in this direction.
514 throw SerializationError("MapNode::serializeBulk: serialization to "
515 "version < 24 not possible");
517 SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
520 for(u32 i=0; i<nodecount; i++)
521 writeU16(&databuf[i*2], nodes[i].param0);
524 u32 start1 = content_width * nodecount;
525 for(u32 i=0; i<nodecount; i++)
526 writeU8(&databuf[start1 + i], nodes[i].param1);
529 u32 start2 = (content_width + 1) * nodecount;
530 for(u32 i=0; i<nodecount; i++)
531 writeU8(&databuf[start2 + i], nodes[i].param2);
534 Compress data to output stream
539 compressZlib(databuf, os);
543 os.write((const char*) &databuf[0], databuf.getSize());
547 // Deserialize bulk node data
548 void MapNode::deSerializeBulk(std::istream &is, int version,
549 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 assert(version >= 22);
556 assert(content_width == 1 || content_width == 2);
557 assert(params_width == 2);
559 // Uncompress or read data
560 u32 len = nodecount * (content_width + params_width);
561 SharedBuffer<u8> databuf(len);
564 std::ostringstream os(std::ios_base::binary);
565 decompressZlib(is, os);
566 std::string s = os.str();
568 throw SerializationError("deSerializeBulkNodes: "
569 "decompress resulted in invalid size");
570 memcpy(&databuf[0], s.c_str(), len);
574 is.read((char*) &databuf[0], len);
575 if(is.eof() || is.fail())
576 throw SerializationError("deSerializeBulkNodes: "
577 "failed to read bulk node data");
580 // Deserialize content
581 if(content_width == 1)
583 for(u32 i=0; i<nodecount; i++)
584 nodes[i].param0 = readU8(&databuf[i]);
586 else if(content_width == 2)
588 for(u32 i=0; i<nodecount; i++)
589 nodes[i].param0 = readU16(&databuf[i*2]);
592 // Deserialize param1
593 u32 start1 = content_width * nodecount;
594 for(u32 i=0; i<nodecount; i++)
595 nodes[i].param1 = readU8(&databuf[start1 + i]);
597 // Deserialize param2
598 u32 start2 = (content_width + 1) * nodecount;
599 if(content_width == 1)
601 for(u32 i=0; i<nodecount; i++) {
602 nodes[i].param2 = readU8(&databuf[start2 + i]);
603 if(nodes[i].param0 > 0x7F){
604 nodes[i].param0 <<= 4;
605 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
606 nodes[i].param2 &= 0x0F;
610 else if(content_width == 2)
612 for(u32 i=0; i<nodecount; i++)
613 nodes[i].param2 = readU8(&databuf[start2 + i]);
620 void MapNode::deSerialize_pre22(u8 *source, u8 version)
626 else if(version <= 9)
638 param0 |= (param2&0xf0)>>4;
643 // Convert special values from old version to new
646 // In these versions, CONTENT_IGNORE and CONTENT_AIR
648 // Version 19 is fucked up with sometimes the old values and sometimes not
650 param0 = CONTENT_IGNORE;
651 else if(param0 == 254)
652 param0 = CONTENT_AIR;
655 // Translate to our known version
656 *this = mapnode_translate_to_internal(*this, version);