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 bool MapNode::isLightDayNightEq(INodeDefManager *nodemgr) const
79 const ContentFeatures &f = nodemgr->get(*this);
82 if (f.param_type == CPT_LIGHT) {
83 u8 day = MYMAX(f.light_source, param1 & 0x0f);
84 u8 night = MYMAX(f.light_source, (param1 >> 4) & 0x0f);
85 isEqual = day == night;
93 u8 MapNode::getLight(enum LightBank bank, INodeDefManager *nodemgr) const
95 // Select the brightest of [light source, propagated light]
96 const ContentFeatures &f = nodemgr->get(*this);
99 if(f.param_type == CPT_LIGHT)
100 light = bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f;
104 return MYMAX(f.light_source, light);
107 u8 MapNode::getLightNoChecks(enum LightBank bank, const ContentFeatures *f) const
109 return MYMAX(f->light_source,
110 bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f);
113 bool MapNode::getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const
115 // Select the brightest of [light source, propagated light]
116 const ContentFeatures &f = nodemgr->get(*this);
117 if(f.param_type == CPT_LIGHT)
119 lightday = param1 & 0x0f;
120 lightnight = (param1>>4)&0x0f;
127 if(f.light_source > lightday)
128 lightday = f.light_source;
129 if(f.light_source > lightnight)
130 lightnight = f.light_source;
131 return f.param_type == CPT_LIGHT || f.light_source != 0;
134 u8 MapNode::getFaceDir(INodeDefManager *nodemgr) const
136 const ContentFeatures &f = nodemgr->get(*this);
137 if(f.param_type_2 == CPT2_FACEDIR)
138 return (getParam2() & 0x1F) % 24;
142 u8 MapNode::getWallMounted(INodeDefManager *nodemgr) const
144 const ContentFeatures &f = nodemgr->get(*this);
145 if(f.param_type_2 == CPT2_WALLMOUNTED)
146 return getParam2() & 0x07;
150 v3s16 MapNode::getWallMountedDir(INodeDefManager *nodemgr) const
152 switch(getWallMounted(nodemgr))
154 case 0: default: return v3s16(0,1,0);
155 case 1: return v3s16(0,-1,0);
156 case 2: return v3s16(1,0,0);
157 case 3: return v3s16(-1,0,0);
158 case 4: return v3s16(0,0,1);
159 case 5: return v3s16(0,0,-1);
163 void MapNode::rotateAlongYAxis(INodeDefManager *nodemgr, Rotation rot) {
164 ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
166 if (cpt2 == CPT2_FACEDIR) {
167 u8 newrot = param2 & 3;
169 param2 |= (newrot + rot) & 3;
170 } else if (cpt2 == CPT2_WALLMOUNTED) {
171 u8 wmountface = (param2 & 7);
175 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
177 param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
181 static std::vector<aabb3f> transformNodeBox(const MapNode &n,
182 const NodeBox &nodebox, INodeDefManager *nodemgr)
184 std::vector<aabb3f> boxes;
185 if(nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED)
187 const std::vector<aabb3f> &fixed = nodebox.fixed;
188 int facedir = n.getFaceDir(nodemgr);
189 u8 axisdir = facedir>>2;
191 for(std::vector<aabb3f>::const_iterator
193 i != fixed.end(); i++)
197 if (nodebox.type == NODEBOX_LEVELED) {
198 box.MaxEdge.Y = -BS/2 + BS*((float)1/LEVELED_MAX) * n.getLevel(nodemgr);
206 box.MinEdge.rotateXZBy(-90);
207 box.MaxEdge.rotateXZBy(-90);
209 else if(facedir == 2)
211 box.MinEdge.rotateXZBy(180);
212 box.MaxEdge.rotateXZBy(180);
214 else if(facedir == 3)
216 box.MinEdge.rotateXZBy(90);
217 box.MaxEdge.rotateXZBy(90);
221 box.MinEdge.rotateYZBy(90);
222 box.MaxEdge.rotateYZBy(90);
225 box.MinEdge.rotateXYBy(90);
226 box.MaxEdge.rotateXYBy(90);
228 else if(facedir == 2)
230 box.MinEdge.rotateXYBy(180);
231 box.MaxEdge.rotateXYBy(180);
233 else if(facedir == 3)
235 box.MinEdge.rotateXYBy(-90);
236 box.MaxEdge.rotateXYBy(-90);
240 box.MinEdge.rotateYZBy(-90);
241 box.MaxEdge.rotateYZBy(-90);
244 box.MinEdge.rotateXYBy(-90);
245 box.MaxEdge.rotateXYBy(-90);
247 else if(facedir == 2)
249 box.MinEdge.rotateXYBy(180);
250 box.MaxEdge.rotateXYBy(180);
252 else if(facedir == 3)
254 box.MinEdge.rotateXYBy(90);
255 box.MaxEdge.rotateXYBy(90);
259 box.MinEdge.rotateXYBy(-90);
260 box.MaxEdge.rotateXYBy(-90);
263 box.MinEdge.rotateYZBy(90);
264 box.MaxEdge.rotateYZBy(90);
266 else if(facedir == 2)
268 box.MinEdge.rotateYZBy(180);
269 box.MaxEdge.rotateYZBy(180);
271 else if(facedir == 3)
273 box.MinEdge.rotateYZBy(-90);
274 box.MaxEdge.rotateYZBy(-90);
278 box.MinEdge.rotateXYBy(90);
279 box.MaxEdge.rotateXYBy(90);
282 box.MinEdge.rotateYZBy(-90);
283 box.MaxEdge.rotateYZBy(-90);
285 else if(facedir == 2)
287 box.MinEdge.rotateYZBy(180);
288 box.MaxEdge.rotateYZBy(180);
290 else if(facedir == 3)
292 box.MinEdge.rotateYZBy(90);
293 box.MaxEdge.rotateYZBy(90);
297 box.MinEdge.rotateXYBy(-180);
298 box.MaxEdge.rotateXYBy(-180);
301 box.MinEdge.rotateXZBy(90);
302 box.MaxEdge.rotateXZBy(90);
304 else if(facedir == 2)
306 box.MinEdge.rotateXZBy(180);
307 box.MaxEdge.rotateXZBy(180);
309 else if(facedir == 3)
311 box.MinEdge.rotateXZBy(-90);
312 box.MaxEdge.rotateXZBy(-90);
319 boxes.push_back(box);
322 else if(nodebox.type == NODEBOX_WALLMOUNTED)
324 v3s16 dir = n.getWallMountedDir(nodemgr);
327 if(dir == v3s16(0,1,0))
329 boxes.push_back(nodebox.wall_top);
332 else if(dir == v3s16(0,-1,0))
334 boxes.push_back(nodebox.wall_bottom);
341 nodebox.wall_side.MinEdge,
342 nodebox.wall_side.MaxEdge
345 for(s32 i=0; i<2; i++)
347 if(dir == v3s16(-1,0,0))
348 vertices[i].rotateXZBy(0);
349 if(dir == v3s16(1,0,0))
350 vertices[i].rotateXZBy(180);
351 if(dir == v3s16(0,0,-1))
352 vertices[i].rotateXZBy(90);
353 if(dir == v3s16(0,0,1))
354 vertices[i].rotateXZBy(-90);
357 aabb3f box = aabb3f(vertices[0]);
358 box.addInternalPoint(vertices[1]);
359 boxes.push_back(box);
362 else // NODEBOX_REGULAR
364 boxes.push_back(aabb3f(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2));
369 std::vector<aabb3f> MapNode::getNodeBoxes(INodeDefManager *nodemgr) const
371 const ContentFeatures &f = nodemgr->get(*this);
372 return transformNodeBox(*this, f.node_box, nodemgr);
375 std::vector<aabb3f> MapNode::getCollisionBoxes(INodeDefManager *nodemgr) const
377 const ContentFeatures &f = nodemgr->get(*this);
378 if (f.collision_box.fixed.empty())
379 return transformNodeBox(*this, f.node_box, nodemgr);
381 return transformNodeBox(*this, f.collision_box, nodemgr);
384 std::vector<aabb3f> MapNode::getSelectionBoxes(INodeDefManager *nodemgr) const
386 const ContentFeatures &f = nodemgr->get(*this);
387 return transformNodeBox(*this, f.selection_box, nodemgr);
390 u8 MapNode::getMaxLevel(INodeDefManager *nodemgr) const
392 const ContentFeatures &f = nodemgr->get(*this);
393 // todo: after update in all games leave only if (f.param_type_2 ==
394 if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
395 return LIQUID_LEVEL_MAX;
396 if(f.leveled || f.param_type_2 == CPT2_LEVELED)
401 u8 MapNode::getLevel(INodeDefManager *nodemgr) const
403 const ContentFeatures &f = nodemgr->get(*this);
404 // todo: after update in all games leave only if (f.param_type_2 ==
405 if(f.liquid_type == LIQUID_SOURCE)
406 return LIQUID_LEVEL_SOURCE;
407 if (f.param_type_2 == CPT2_FLOWINGLIQUID)
408 return getParam2() & LIQUID_LEVEL_MASK;
409 if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
410 return getParam2() & LIQUID_LEVEL_MASK;
411 if(f.leveled || f.param_type_2 == CPT2_LEVELED) {
412 u8 level = getParam2() & LEVELED_MASK;
415 if(f.leveled > LEVELED_MAX)
417 return f.leveled; //default
422 u8 MapNode::setLevel(INodeDefManager *nodemgr, s8 level)
426 setContent(CONTENT_AIR);
429 const ContentFeatures &f = nodemgr->get(*this);
430 if (f.param_type_2 == CPT2_FLOWINGLIQUID
431 || f.liquid_type == LIQUID_FLOWING
432 || f.liquid_type == LIQUID_SOURCE) {
433 if (level >= LIQUID_LEVEL_SOURCE) {
434 rest = level - LIQUID_LEVEL_SOURCE;
435 setContent(nodemgr->getId(f.liquid_alternative_source));
437 setContent(nodemgr->getId(f.liquid_alternative_flowing));
438 setParam2(level & LIQUID_LEVEL_MASK);
440 } else if (f.leveled || f.param_type_2 == CPT2_LEVELED) {
441 if (level > LEVELED_MAX) {
442 rest = level - LEVELED_MAX;
445 setParam2(level & LEVELED_MASK);
450 u8 MapNode::addLevel(INodeDefManager *nodemgr, s8 add)
452 s8 level = getLevel(nodemgr);
453 if (add == 0) level = 1;
455 return setLevel(nodemgr, level);
458 u32 MapNode::serializedLength(u8 version)
460 if(!ser_ver_supported(version))
461 throw VersionMismatchException("ERROR: MapNode format not supported");
465 else if(version <= 9)
467 else if(version <= 23)
472 void MapNode::serialize(u8 *dest, u8 version)
474 if(!ser_ver_supported(version))
475 throw VersionMismatchException("ERROR: MapNode format not supported");
477 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
478 // in memory; conversion just won't work in this direction.
480 throw SerializationError("MapNode::serialize: serialization to "
481 "version < 24 not possible");
483 writeU16(dest+0, param0);
484 writeU8(dest+2, param1);
485 writeU8(dest+3, param2);
487 void MapNode::deSerialize(u8 *source, u8 version)
489 if(!ser_ver_supported(version))
490 throw VersionMismatchException("ERROR: MapNode format not supported");
494 deSerialize_pre22(source, version);
499 param0 = readU16(source+0);
500 param1 = readU8(source+2);
501 param2 = readU8(source+3);
503 param0 = readU8(source+0);
504 param1 = readU8(source+1);
505 param2 = readU8(source+2);
507 param0 |= ((param2&0xF0)<<4);
512 void MapNode::serializeBulk(std::ostream &os, int version,
513 const MapNode *nodes, u32 nodecount,
514 u8 content_width, u8 params_width, bool compressed)
516 if(!ser_ver_supported(version))
517 throw VersionMismatchException("ERROR: MapNode format not supported");
519 assert(content_width == 2);
520 assert(params_width == 2);
522 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
523 // in memory; conversion just won't work in this direction.
525 throw SerializationError("MapNode::serializeBulk: serialization to "
526 "version < 24 not possible");
528 SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
531 for(u32 i=0; i<nodecount; i++)
532 writeU16(&databuf[i*2], nodes[i].param0);
535 u32 start1 = content_width * nodecount;
536 for(u32 i=0; i<nodecount; i++)
537 writeU8(&databuf[start1 + i], nodes[i].param1);
540 u32 start2 = (content_width + 1) * nodecount;
541 for(u32 i=0; i<nodecount; i++)
542 writeU8(&databuf[start2 + i], nodes[i].param2);
545 Compress data to output stream
550 compressZlib(databuf, os);
554 os.write((const char*) &databuf[0], databuf.getSize());
558 // Deserialize bulk node data
559 void MapNode::deSerializeBulk(std::istream &is, int version,
560 MapNode *nodes, u32 nodecount,
561 u8 content_width, u8 params_width, bool compressed)
563 if(!ser_ver_supported(version))
564 throw VersionMismatchException("ERROR: MapNode format not supported");
566 assert(version >= 22);
567 assert(content_width == 1 || content_width == 2);
568 assert(params_width == 2);
570 // Uncompress or read data
571 u32 len = nodecount * (content_width + params_width);
572 SharedBuffer<u8> databuf(len);
575 std::ostringstream os(std::ios_base::binary);
576 decompressZlib(is, os);
577 std::string s = os.str();
579 throw SerializationError("deSerializeBulkNodes: "
580 "decompress resulted in invalid size");
581 memcpy(&databuf[0], s.c_str(), len);
585 is.read((char*) &databuf[0], len);
586 if(is.eof() || is.fail())
587 throw SerializationError("deSerializeBulkNodes: "
588 "failed to read bulk node data");
591 // Deserialize content
592 if(content_width == 1)
594 for(u32 i=0; i<nodecount; i++)
595 nodes[i].param0 = readU8(&databuf[i]);
597 else if(content_width == 2)
599 for(u32 i=0; i<nodecount; i++)
600 nodes[i].param0 = readU16(&databuf[i*2]);
603 // Deserialize param1
604 u32 start1 = content_width * nodecount;
605 for(u32 i=0; i<nodecount; i++)
606 nodes[i].param1 = readU8(&databuf[start1 + i]);
608 // Deserialize param2
609 u32 start2 = (content_width + 1) * nodecount;
610 if(content_width == 1)
612 for(u32 i=0; i<nodecount; i++) {
613 nodes[i].param2 = readU8(&databuf[start2 + i]);
614 if(nodes[i].param0 > 0x7F){
615 nodes[i].param0 <<= 4;
616 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
617 nodes[i].param2 &= 0x0F;
621 else if(content_width == 2)
623 for(u32 i=0; i<nodecount; i++)
624 nodes[i].param2 = readU8(&databuf[start2 + i]);
631 void MapNode::deSerialize_pre22(u8 *source, u8 version)
637 else if(version <= 9)
649 param0 |= (param2&0xf0)>>4;
654 // Convert special values from old version to new
657 // In these versions, CONTENT_IGNORE and CONTENT_AIR
659 // Version 19 is fucked up with sometimes the old values and sometimes not
661 param0 = CONTENT_IGNORE;
662 else if(param0 == 254)
663 param0 = CONTENT_AIR;
666 // Translate to our known version
667 *this = mapnode_translate_to_internal(*this, version);