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 u8 MapNode::getLightNoChecks(enum LightBank bank, const ContentFeatures *f)
93 return MYMAX(f->light_source,
94 bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f);
97 bool MapNode::getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const
99 // Select the brightest of [light source, propagated light]
100 const ContentFeatures &f = nodemgr->get(*this);
101 if(f.param_type == CPT_LIGHT)
103 lightday = param1 & 0x0f;
104 lightnight = (param1>>4)&0x0f;
111 if(f.light_source > lightday)
112 lightday = f.light_source;
113 if(f.light_source > lightnight)
114 lightnight = f.light_source;
115 return f.param_type == CPT_LIGHT || f.light_source != 0;
118 u8 MapNode::getFaceDir(INodeDefManager *nodemgr) const
120 const ContentFeatures &f = nodemgr->get(*this);
121 if(f.param_type_2 == CPT2_FACEDIR)
122 return (getParam2() & 0x1F) % 24;
126 u8 MapNode::getWallMounted(INodeDefManager *nodemgr) const
128 const ContentFeatures &f = nodemgr->get(*this);
129 if(f.param_type_2 == CPT2_WALLMOUNTED)
130 return getParam2() & 0x07;
134 v3s16 MapNode::getWallMountedDir(INodeDefManager *nodemgr) const
136 switch(getWallMounted(nodemgr))
138 case 0: default: return v3s16(0,1,0);
139 case 1: return v3s16(0,-1,0);
140 case 2: return v3s16(1,0,0);
141 case 3: return v3s16(-1,0,0);
142 case 4: return v3s16(0,0,1);
143 case 5: return v3s16(0,0,-1);
147 void MapNode::rotateAlongYAxis(INodeDefManager *nodemgr, Rotation rot) {
148 ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
150 if (cpt2 == CPT2_FACEDIR) {
151 u8 newrot = param2 & 3;
153 param2 |= (newrot + rot) & 3;
154 } else if (cpt2 == CPT2_WALLMOUNTED) {
155 u8 wmountface = (param2 & 7);
159 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
161 param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
165 static std::vector<aabb3f> transformNodeBox(const MapNode &n,
166 const NodeBox &nodebox, INodeDefManager *nodemgr)
168 std::vector<aabb3f> boxes;
169 if(nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED)
171 const std::vector<aabb3f> &fixed = nodebox.fixed;
172 int facedir = n.getFaceDir(nodemgr);
173 u8 axisdir = facedir>>2;
175 for(std::vector<aabb3f>::const_iterator
177 i != fixed.end(); i++)
181 if (nodebox.type == NODEBOX_LEVELED) {
182 box.MaxEdge.Y = -BS/2 + BS*((float)1/LEVELED_MAX) * n.getLevel(nodemgr);
190 box.MinEdge.rotateXZBy(-90);
191 box.MaxEdge.rotateXZBy(-90);
193 else if(facedir == 2)
195 box.MinEdge.rotateXZBy(180);
196 box.MaxEdge.rotateXZBy(180);
198 else if(facedir == 3)
200 box.MinEdge.rotateXZBy(90);
201 box.MaxEdge.rotateXZBy(90);
205 box.MinEdge.rotateYZBy(90);
206 box.MaxEdge.rotateYZBy(90);
209 box.MinEdge.rotateXYBy(90);
210 box.MaxEdge.rotateXYBy(90);
212 else if(facedir == 2)
214 box.MinEdge.rotateXYBy(180);
215 box.MaxEdge.rotateXYBy(180);
217 else if(facedir == 3)
219 box.MinEdge.rotateXYBy(-90);
220 box.MaxEdge.rotateXYBy(-90);
224 box.MinEdge.rotateYZBy(-90);
225 box.MaxEdge.rotateYZBy(-90);
228 box.MinEdge.rotateXYBy(-90);
229 box.MaxEdge.rotateXYBy(-90);
231 else if(facedir == 2)
233 box.MinEdge.rotateXYBy(180);
234 box.MaxEdge.rotateXYBy(180);
236 else if(facedir == 3)
238 box.MinEdge.rotateXYBy(90);
239 box.MaxEdge.rotateXYBy(90);
243 box.MinEdge.rotateXYBy(-90);
244 box.MaxEdge.rotateXYBy(-90);
247 box.MinEdge.rotateYZBy(90);
248 box.MaxEdge.rotateYZBy(90);
250 else if(facedir == 2)
252 box.MinEdge.rotateYZBy(180);
253 box.MaxEdge.rotateYZBy(180);
255 else if(facedir == 3)
257 box.MinEdge.rotateYZBy(-90);
258 box.MaxEdge.rotateYZBy(-90);
262 box.MinEdge.rotateXYBy(90);
263 box.MaxEdge.rotateXYBy(90);
266 box.MinEdge.rotateYZBy(-90);
267 box.MaxEdge.rotateYZBy(-90);
269 else if(facedir == 2)
271 box.MinEdge.rotateYZBy(180);
272 box.MaxEdge.rotateYZBy(180);
274 else if(facedir == 3)
276 box.MinEdge.rotateYZBy(90);
277 box.MaxEdge.rotateYZBy(90);
281 box.MinEdge.rotateXYBy(-180);
282 box.MaxEdge.rotateXYBy(-180);
285 box.MinEdge.rotateXZBy(90);
286 box.MaxEdge.rotateXZBy(90);
288 else if(facedir == 2)
290 box.MinEdge.rotateXZBy(180);
291 box.MaxEdge.rotateXZBy(180);
293 else if(facedir == 3)
295 box.MinEdge.rotateXZBy(-90);
296 box.MaxEdge.rotateXZBy(-90);
303 boxes.push_back(box);
306 else if(nodebox.type == NODEBOX_WALLMOUNTED)
308 v3s16 dir = n.getWallMountedDir(nodemgr);
311 if(dir == v3s16(0,1,0))
313 boxes.push_back(nodebox.wall_top);
316 else if(dir == v3s16(0,-1,0))
318 boxes.push_back(nodebox.wall_bottom);
325 nodebox.wall_side.MinEdge,
326 nodebox.wall_side.MaxEdge
329 for(s32 i=0; i<2; i++)
331 if(dir == v3s16(-1,0,0))
332 vertices[i].rotateXZBy(0);
333 if(dir == v3s16(1,0,0))
334 vertices[i].rotateXZBy(180);
335 if(dir == v3s16(0,0,-1))
336 vertices[i].rotateXZBy(90);
337 if(dir == v3s16(0,0,1))
338 vertices[i].rotateXZBy(-90);
341 aabb3f box = aabb3f(vertices[0]);
342 box.addInternalPoint(vertices[1]);
343 boxes.push_back(box);
346 else // NODEBOX_REGULAR
348 boxes.push_back(aabb3f(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2));
353 std::vector<aabb3f> MapNode::getNodeBoxes(INodeDefManager *nodemgr) const
355 const ContentFeatures &f = nodemgr->get(*this);
356 return transformNodeBox(*this, f.node_box, nodemgr);
359 std::vector<aabb3f> MapNode::getCollisionBoxes(INodeDefManager *nodemgr) const
361 const ContentFeatures &f = nodemgr->get(*this);
362 if (f.collision_box.fixed.empty())
363 return transformNodeBox(*this, f.node_box, nodemgr);
365 return transformNodeBox(*this, f.collision_box, nodemgr);
368 std::vector<aabb3f> MapNode::getSelectionBoxes(INodeDefManager *nodemgr) const
370 const ContentFeatures &f = nodemgr->get(*this);
371 return transformNodeBox(*this, f.selection_box, nodemgr);
374 u8 MapNode::getMaxLevel(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_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
379 return LIQUID_LEVEL_MAX;
380 if(f.leveled || f.param_type_2 == CPT2_LEVELED)
385 u8 MapNode::getLevel(INodeDefManager *nodemgr) const
387 const ContentFeatures &f = nodemgr->get(*this);
388 // todo: after update in all games leave only if (f.param_type_2 ==
389 if(f.liquid_type == LIQUID_SOURCE)
390 return LIQUID_LEVEL_SOURCE;
391 if (f.param_type_2 == CPT2_FLOWINGLIQUID)
392 return getParam2() & LIQUID_LEVEL_MASK;
393 if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
394 return getParam2() & LIQUID_LEVEL_MASK;
395 if(f.leveled || f.param_type_2 == CPT2_LEVELED) {
396 u8 level = getParam2() & LEVELED_MASK;
399 if(f.leveled > LEVELED_MAX)
401 return f.leveled; //default
406 u8 MapNode::setLevel(INodeDefManager *nodemgr, s8 level)
410 setContent(CONTENT_AIR);
413 const ContentFeatures &f = nodemgr->get(*this);
414 if (f.param_type_2 == CPT2_FLOWINGLIQUID
415 || f.liquid_type == LIQUID_FLOWING
416 || f.liquid_type == LIQUID_SOURCE) {
417 if (level >= LIQUID_LEVEL_SOURCE) {
418 rest = level - LIQUID_LEVEL_SOURCE;
419 setContent(nodemgr->getId(f.liquid_alternative_source));
421 setContent(nodemgr->getId(f.liquid_alternative_flowing));
422 setParam2(level & LIQUID_LEVEL_MASK);
424 } else if (f.leveled || f.param_type_2 == CPT2_LEVELED) {
425 if (level > LEVELED_MAX) {
426 rest = level - LEVELED_MAX;
429 setParam2(level & LEVELED_MASK);
434 u8 MapNode::addLevel(INodeDefManager *nodemgr, s8 add)
436 s8 level = getLevel(nodemgr);
437 if (add == 0) level = 1;
439 return setLevel(nodemgr, level);
442 u32 MapNode::serializedLength(u8 version)
444 if(!ser_ver_supported(version))
445 throw VersionMismatchException("ERROR: MapNode format not supported");
449 else if(version <= 9)
451 else if(version <= 23)
456 void MapNode::serialize(u8 *dest, u8 version)
458 if(!ser_ver_supported(version))
459 throw VersionMismatchException("ERROR: MapNode format not supported");
461 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
462 // in memory; conversion just won't work in this direction.
464 throw SerializationError("MapNode::serialize: serialization to "
465 "version < 24 not possible");
467 writeU16(dest+0, param0);
468 writeU8(dest+2, param1);
469 writeU8(dest+3, param2);
471 void MapNode::deSerialize(u8 *source, u8 version)
473 if(!ser_ver_supported(version))
474 throw VersionMismatchException("ERROR: MapNode format not supported");
478 deSerialize_pre22(source, version);
483 param0 = readU16(source+0);
484 param1 = readU8(source+2);
485 param2 = readU8(source+3);
487 param0 = readU8(source+0);
488 param1 = readU8(source+1);
489 param2 = readU8(source+2);
491 param0 |= ((param2&0xF0)<<4);
496 void MapNode::serializeBulk(std::ostream &os, int version,
497 const MapNode *nodes, u32 nodecount,
498 u8 content_width, u8 params_width, bool compressed)
500 if(!ser_ver_supported(version))
501 throw VersionMismatchException("ERROR: MapNode format not supported");
503 assert(content_width == 2);
504 assert(params_width == 2);
506 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
507 // in memory; conversion just won't work in this direction.
509 throw SerializationError("MapNode::serializeBulk: serialization to "
510 "version < 24 not possible");
512 SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
515 for(u32 i=0; i<nodecount; i++)
516 writeU16(&databuf[i*2], nodes[i].param0);
519 u32 start1 = content_width * nodecount;
520 for(u32 i=0; i<nodecount; i++)
521 writeU8(&databuf[start1 + i], nodes[i].param1);
524 u32 start2 = (content_width + 1) * nodecount;
525 for(u32 i=0; i<nodecount; i++)
526 writeU8(&databuf[start2 + i], nodes[i].param2);
529 Compress data to output stream
534 compressZlib(databuf, os);
538 os.write((const char*) &databuf[0], databuf.getSize());
542 // Deserialize bulk node data
543 void MapNode::deSerializeBulk(std::istream &is, int version,
544 MapNode *nodes, u32 nodecount,
545 u8 content_width, u8 params_width, bool compressed)
547 if(!ser_ver_supported(version))
548 throw VersionMismatchException("ERROR: MapNode format not supported");
550 assert(version >= 22);
551 assert(content_width == 1 || content_width == 2);
552 assert(params_width == 2);
554 // Uncompress or read data
555 u32 len = nodecount * (content_width + params_width);
556 SharedBuffer<u8> databuf(len);
559 std::ostringstream os(std::ios_base::binary);
560 decompressZlib(is, os);
561 std::string s = os.str();
563 throw SerializationError("deSerializeBulkNodes: "
564 "decompress resulted in invalid size");
565 memcpy(&databuf[0], s.c_str(), len);
569 is.read((char*) &databuf[0], len);
570 if(is.eof() || is.fail())
571 throw SerializationError("deSerializeBulkNodes: "
572 "failed to read bulk node data");
575 // Deserialize content
576 if(content_width == 1)
578 for(u32 i=0; i<nodecount; i++)
579 nodes[i].param0 = readU8(&databuf[i]);
581 else if(content_width == 2)
583 for(u32 i=0; i<nodecount; i++)
584 nodes[i].param0 = readU16(&databuf[i*2]);
587 // Deserialize param1
588 u32 start1 = content_width * nodecount;
589 for(u32 i=0; i<nodecount; i++)
590 nodes[i].param1 = readU8(&databuf[start1 + i]);
592 // Deserialize param2
593 u32 start2 = (content_width + 1) * nodecount;
594 if(content_width == 1)
596 for(u32 i=0; i<nodecount; i++) {
597 nodes[i].param2 = readU8(&databuf[start2 + i]);
598 if(nodes[i].param0 > 0x7F){
599 nodes[i].param0 <<= 4;
600 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
601 nodes[i].param2 &= 0x0F;
605 else if(content_width == 2)
607 for(u32 i=0; i<nodecount; i++)
608 nodes[i].param2 = readU8(&databuf[start2 + i]);
615 void MapNode::deSerialize_pre22(u8 *source, u8 version)
621 else if(version <= 9)
633 param0 |= (param2&0xf0)>>4;
638 // Convert special values from old version to new
641 // In these versions, CONTENT_IGNORE and CONTENT_AIR
643 // Version 19 is fucked up with sometimes the old values and sometimes not
645 param0 = CONTENT_IGNORE;
646 else if(param0 == 254)
647 param0 = CONTENT_AIR;
650 // Translate to our known version
651 *this = mapnode_translate_to_internal(*this, version);