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;
388 if(f.leveled > LEVELED_MAX)
390 return f.leveled; //default
395 u8 MapNode::setLevel(INodeDefManager *nodemgr, s8 level)
399 setContent(CONTENT_AIR);
402 const ContentFeatures &f = nodemgr->get(*this);
403 if (f.param_type_2 == CPT2_FLOWINGLIQUID
404 || f.liquid_type == LIQUID_FLOWING
405 || f.liquid_type == LIQUID_SOURCE) {
406 if (level >= LIQUID_LEVEL_SOURCE) {
407 rest = level - LIQUID_LEVEL_SOURCE;
408 setContent(nodemgr->getId(f.liquid_alternative_source));
410 setContent(nodemgr->getId(f.liquid_alternative_flowing));
411 setParam2(level & LIQUID_LEVEL_MASK);
413 } else if (f.leveled || f.param_type_2 == CPT2_LEVELED) {
414 if (level > LEVELED_MAX) {
415 rest = level - LEVELED_MAX;
418 setParam2(level & LEVELED_MASK);
423 u8 MapNode::addLevel(INodeDefManager *nodemgr, s8 add)
425 s8 level = getLevel(nodemgr);
426 if (add == 0) level = 1;
428 return setLevel(nodemgr, level);
431 void MapNode::freezeMelt(INodeDefManager *ndef) {
432 u8 level_was_max = this->getMaxLevel(ndef);
433 u8 level_was = this->getLevel(ndef);
434 this->setContent(ndef->getId(ndef->get(*this).freezemelt));
435 u8 level_now_max = this->getMaxLevel(ndef);
436 if (level_was_max && level_was_max != level_now_max) {
437 u8 want = (float)level_now_max / level_was_max * level_was;
440 if (want != level_was)
441 this->setLevel(ndef, want);
442 //errorstream<<"was="<<(int)level_was<<"/"<<(int)level_was_max<<" nowm="<<(int)want<<"/"<<(int)level_now_max<< " => "<<(int)this->getLevel(ndef)<< std::endl;
444 if (this->getMaxLevel(ndef) && !this->getLevel(ndef))
445 this->addLevel(ndef);
448 u32 MapNode::serializedLength(u8 version)
450 if(!ser_ver_supported(version))
451 throw VersionMismatchException("ERROR: MapNode format not supported");
455 else if(version <= 9)
457 else if(version <= 23)
462 void MapNode::serialize(u8 *dest, u8 version)
464 if(!ser_ver_supported(version))
465 throw VersionMismatchException("ERROR: MapNode format not supported");
467 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
468 // in memory; conversion just won't work in this direction.
470 throw SerializationError("MapNode::serialize: serialization to "
471 "version < 24 not possible");
473 writeU16(dest+0, param0);
474 writeU8(dest+2, param1);
475 writeU8(dest+3, param2);
477 void MapNode::deSerialize(u8 *source, u8 version)
479 if(!ser_ver_supported(version))
480 throw VersionMismatchException("ERROR: MapNode format not supported");
484 deSerialize_pre22(source, version);
489 param0 = readU16(source+0);
490 param1 = readU8(source+2);
491 param2 = readU8(source+3);
493 param0 = readU8(source+0);
494 param1 = readU8(source+1);
495 param2 = readU8(source+2);
497 param0 |= ((param2&0xF0)<<4);
502 void MapNode::serializeBulk(std::ostream &os, int version,
503 const MapNode *nodes, u32 nodecount,
504 u8 content_width, u8 params_width, bool compressed)
506 if(!ser_ver_supported(version))
507 throw VersionMismatchException("ERROR: MapNode format not supported");
509 assert(content_width == 2);
510 assert(params_width == 2);
512 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
513 // in memory; conversion just won't work in this direction.
515 throw SerializationError("MapNode::serializeBulk: serialization to "
516 "version < 24 not possible");
518 SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
521 for(u32 i=0; i<nodecount; i++)
522 writeU16(&databuf[i*2], nodes[i].param0);
525 u32 start1 = content_width * nodecount;
526 for(u32 i=0; i<nodecount; i++)
527 writeU8(&databuf[start1 + i], nodes[i].param1);
530 u32 start2 = (content_width + 1) * nodecount;
531 for(u32 i=0; i<nodecount; i++)
532 writeU8(&databuf[start2 + i], nodes[i].param2);
535 Compress data to output stream
540 compressZlib(databuf, os);
544 os.write((const char*) &databuf[0], databuf.getSize());
548 // Deserialize bulk node data
549 void MapNode::deSerializeBulk(std::istream &is, int version,
550 MapNode *nodes, u32 nodecount,
551 u8 content_width, u8 params_width, bool compressed)
553 if(!ser_ver_supported(version))
554 throw VersionMismatchException("ERROR: MapNode format not supported");
556 assert(version >= 22);
557 assert(content_width == 1 || content_width == 2);
558 assert(params_width == 2);
560 // Uncompress or read data
561 u32 len = nodecount * (content_width + params_width);
562 SharedBuffer<u8> databuf(len);
565 std::ostringstream os(std::ios_base::binary);
566 decompressZlib(is, os);
567 std::string s = os.str();
569 throw SerializationError("deSerializeBulkNodes: "
570 "decompress resulted in invalid size");
571 memcpy(&databuf[0], s.c_str(), len);
575 is.read((char*) &databuf[0], len);
576 if(is.eof() || is.fail())
577 throw SerializationError("deSerializeBulkNodes: "
578 "failed to read bulk node data");
581 // Deserialize content
582 if(content_width == 1)
584 for(u32 i=0; i<nodecount; i++)
585 nodes[i].param0 = readU8(&databuf[i]);
587 else if(content_width == 2)
589 for(u32 i=0; i<nodecount; i++)
590 nodes[i].param0 = readU16(&databuf[i*2]);
593 // Deserialize param1
594 u32 start1 = content_width * nodecount;
595 for(u32 i=0; i<nodecount; i++)
596 nodes[i].param1 = readU8(&databuf[start1 + i]);
598 // Deserialize param2
599 u32 start2 = (content_width + 1) * nodecount;
600 if(content_width == 1)
602 for(u32 i=0; i<nodecount; i++) {
603 nodes[i].param2 = readU8(&databuf[start2 + i]);
604 if(nodes[i].param0 > 0x7F){
605 nodes[i].param0 <<= 4;
606 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
607 nodes[i].param2 &= 0x0F;
611 else if(content_width == 2)
613 for(u32 i=0; i<nodecount; i++)
614 nodes[i].param2 = readU8(&databuf[start2 + i]);
621 void MapNode::deSerialize_pre22(u8 *source, u8 version)
627 else if(version <= 9)
639 param0 |= (param2&0xf0)>>4;
644 // Convert special values from old version to new
647 // In these versions, CONTENT_IGNORE and CONTENT_AIR
649 // Version 19 is fucked up with sometimes the old values and sometimes not
651 param0 = CONTENT_IGNORE;
652 else if(param0 == 254)
653 param0 = CONTENT_AIR;
656 // Translate to our known version
657 *this = mapnode_translate_to_internal(*this, version);