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"
31 static const Rotation wallmounted_to_rot[] = {
32 ROTATE_0, ROTATE_180, ROTATE_90, ROTATE_270
35 static const u8 rot_to_wallmounted[] = {
44 // Create directly from a nodename
45 // If name is unknown, sets CONTENT_IGNORE
46 MapNode::MapNode(INodeDefManager *ndef, const std::string &name,
47 u8 a_param1, u8 a_param2)
49 content_t id = CONTENT_IGNORE;
50 ndef->getId(name, id);
56 void MapNode::setLight(enum LightBank bank, u8 a_light, INodeDefManager *nodemgr)
58 // If node doesn't contain light data, ignore this
59 if(nodemgr->get(*this).param_type != CPT_LIGHT)
61 if(bank == LIGHTBANK_DAY)
64 param1 |= a_light & 0x0f;
66 else if(bank == LIGHTBANK_NIGHT)
69 param1 |= (a_light & 0x0f)<<4;
75 u8 MapNode::getLight(enum LightBank bank, INodeDefManager *nodemgr) const
77 // Select the brightest of [light source, propagated light]
78 const ContentFeatures &f = nodemgr->get(*this);
80 if(f.param_type == CPT_LIGHT)
82 if(bank == LIGHTBANK_DAY)
83 light = param1 & 0x0f;
84 else if(bank == LIGHTBANK_NIGHT)
85 light = (param1>>4)&0x0f;
89 if(f.light_source > light)
90 light = f.light_source;
94 bool MapNode::getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const
96 // Select the brightest of [light source, propagated light]
97 const ContentFeatures &f = nodemgr->get(*this);
98 if(f.param_type == CPT_LIGHT)
100 lightday = param1 & 0x0f;
101 lightnight = (param1>>4)&0x0f;
108 if(f.light_source > lightday)
109 lightday = f.light_source;
110 if(f.light_source > lightnight)
111 lightnight = f.light_source;
112 return f.param_type == CPT_LIGHT || f.light_source != 0;
115 u8 MapNode::getFaceDir(INodeDefManager *nodemgr) const
117 const ContentFeatures &f = nodemgr->get(*this);
118 if(f.param_type_2 == CPT2_FACEDIR)
119 return getParam2() & 0x1F;
123 u8 MapNode::getWallMounted(INodeDefManager *nodemgr) const
125 const ContentFeatures &f = nodemgr->get(*this);
126 if(f.param_type_2 == CPT2_WALLMOUNTED)
127 return getParam2() & 0x07;
131 v3s16 MapNode::getWallMountedDir(INodeDefManager *nodemgr) const
133 switch(getWallMounted(nodemgr))
135 case 0: default: return v3s16(0,1,0);
136 case 1: return v3s16(0,-1,0);
137 case 2: return v3s16(1,0,0);
138 case 3: return v3s16(-1,0,0);
139 case 4: return v3s16(0,0,1);
140 case 5: return v3s16(0,0,-1);
144 void MapNode::rotateAlongYAxis(INodeDefManager *nodemgr, Rotation rot) {
145 ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
147 if (cpt2 == CPT2_FACEDIR) {
148 u8 newrot = param2 & 3;
150 param2 |= (newrot + rot) & 3;
151 } else if (cpt2 == CPT2_WALLMOUNTED) {
152 u8 wmountface = (param2 & 7);
156 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
158 param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
162 static std::vector<aabb3f> transformNodeBox(const MapNode &n,
163 const NodeBox &nodebox, INodeDefManager *nodemgr)
165 std::vector<aabb3f> boxes;
166 if(nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED)
168 const std::vector<aabb3f> &fixed = nodebox.fixed;
169 int facedir = n.getFaceDir(nodemgr);
170 u8 axisdir = facedir>>2;
172 for(std::vector<aabb3f>::const_iterator
174 i != fixed.end(); i++)
178 if (nodebox.type == NODEBOX_LEVELED) {
179 box.MaxEdge.Y = -BS/2 + BS*((float)1/LEVELED_MAX) * n.getLevel(nodemgr);
187 box.MinEdge.rotateXZBy(-90);
188 box.MaxEdge.rotateXZBy(-90);
190 else if(facedir == 2)
192 box.MinEdge.rotateXZBy(180);
193 box.MaxEdge.rotateXZBy(180);
195 else if(facedir == 3)
197 box.MinEdge.rotateXZBy(90);
198 box.MaxEdge.rotateXZBy(90);
202 box.MinEdge.rotateYZBy(90);
203 box.MaxEdge.rotateYZBy(90);
206 box.MinEdge.rotateXYBy(90);
207 box.MaxEdge.rotateXYBy(90);
209 else if(facedir == 2)
211 box.MinEdge.rotateXYBy(180);
212 box.MaxEdge.rotateXYBy(180);
214 else if(facedir == 3)
216 box.MinEdge.rotateXYBy(-90);
217 box.MaxEdge.rotateXYBy(-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.rotateXYBy(-90);
241 box.MaxEdge.rotateXYBy(-90);
244 box.MinEdge.rotateYZBy(90);
245 box.MaxEdge.rotateYZBy(90);
247 else if(facedir == 2)
249 box.MinEdge.rotateYZBy(180);
250 box.MaxEdge.rotateYZBy(180);
252 else if(facedir == 3)
254 box.MinEdge.rotateYZBy(-90);
255 box.MaxEdge.rotateYZBy(-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(-180);
279 box.MaxEdge.rotateXYBy(-180);
282 box.MinEdge.rotateXZBy(90);
283 box.MaxEdge.rotateXZBy(90);
285 else if(facedir == 2)
287 box.MinEdge.rotateXZBy(180);
288 box.MaxEdge.rotateXZBy(180);
290 else if(facedir == 3)
292 box.MinEdge.rotateXZBy(-90);
293 box.MaxEdge.rotateXZBy(-90);
300 boxes.push_back(box);
303 else if(nodebox.type == NODEBOX_WALLMOUNTED)
305 v3s16 dir = n.getWallMountedDir(nodemgr);
308 if(dir == v3s16(0,1,0))
310 boxes.push_back(nodebox.wall_top);
313 else if(dir == v3s16(0,-1,0))
315 boxes.push_back(nodebox.wall_bottom);
322 nodebox.wall_side.MinEdge,
323 nodebox.wall_side.MaxEdge
326 for(s32 i=0; i<2; i++)
328 if(dir == v3s16(-1,0,0))
329 vertices[i].rotateXZBy(0);
330 if(dir == v3s16(1,0,0))
331 vertices[i].rotateXZBy(180);
332 if(dir == v3s16(0,0,-1))
333 vertices[i].rotateXZBy(90);
334 if(dir == v3s16(0,0,1))
335 vertices[i].rotateXZBy(-90);
338 aabb3f box = aabb3f(vertices[0]);
339 box.addInternalPoint(vertices[1]);
340 boxes.push_back(box);
343 else // NODEBOX_REGULAR
345 boxes.push_back(aabb3f(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2));
350 std::vector<aabb3f> MapNode::getNodeBoxes(INodeDefManager *nodemgr) const
352 const ContentFeatures &f = nodemgr->get(*this);
353 return transformNodeBox(*this, f.node_box, nodemgr);
356 std::vector<aabb3f> MapNode::getSelectionBoxes(INodeDefManager *nodemgr) const
358 const ContentFeatures &f = nodemgr->get(*this);
359 return transformNodeBox(*this, f.selection_box, nodemgr);
362 u8 MapNode::getLevel(INodeDefManager *nodemgr) const
364 const ContentFeatures &f = nodemgr->get(*this);
365 if(f.liquid_type == LIQUID_SOURCE)
366 return LIQUID_LEVEL_SOURCE;
367 if (f.param_type_2 == CPT2_FLOWINGLIQUID)
368 return getParam2() & LIQUID_LEVEL_MASK;
369 if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
370 return getParam2() & LIQUID_LEVEL_MASK;
371 if(f.leveled || f.param_type_2 == CPT2_LEVELED) {
372 u8 level = getParam2() & LEVELED_MASK;
373 if(level) return level;
374 if(f.leveled > LEVELED_MAX) return LEVELED_MAX;
375 return f.leveled; //default
380 u32 MapNode::serializedLength(u8 version)
382 if(!ser_ver_supported(version))
383 throw VersionMismatchException("ERROR: MapNode format not supported");
387 else if(version <= 9)
389 else if(version <= 23)
394 void MapNode::serialize(u8 *dest, u8 version)
396 if(!ser_ver_supported(version))
397 throw VersionMismatchException("ERROR: MapNode format not supported");
399 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
400 // in memory; conversion just won't work in this direction.
402 throw SerializationError("MapNode::serialize: serialization to "
403 "version < 24 not possible");
405 writeU16(dest+0, param0);
406 writeU8(dest+2, param1);
407 writeU8(dest+3, param2);
409 void MapNode::deSerialize(u8 *source, u8 version)
411 if(!ser_ver_supported(version))
412 throw VersionMismatchException("ERROR: MapNode format not supported");
416 deSerialize_pre22(source, version);
421 param0 = readU16(source+0);
422 param1 = readU8(source+2);
423 param2 = readU8(source+3);
426 param0 = readU8(source+0);
427 param1 = readU8(source+1);
428 param2 = readU8(source+2);
430 param0 |= ((param2&0xF0)<<4);
435 void MapNode::serializeBulk(std::ostream &os, int version,
436 const MapNode *nodes, u32 nodecount,
437 u8 content_width, u8 params_width, bool compressed)
439 if(!ser_ver_supported(version))
440 throw VersionMismatchException("ERROR: MapNode format not supported");
442 assert(content_width == 2);
443 assert(params_width == 2);
445 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
446 // in memory; conversion just won't work in this direction.
448 throw SerializationError("MapNode::serializeBulk: serialization to "
449 "version < 24 not possible");
451 SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
454 for(u32 i=0; i<nodecount; i++)
455 writeU16(&databuf[i*2], nodes[i].param0);
458 u32 start1 = content_width * nodecount;
459 for(u32 i=0; i<nodecount; i++)
460 writeU8(&databuf[start1 + i], nodes[i].param1);
463 u32 start2 = (content_width + 1) * nodecount;
464 for(u32 i=0; i<nodecount; i++)
465 writeU8(&databuf[start2 + i], nodes[i].param2);
468 Compress data to output stream
473 compressZlib(databuf, os);
477 os.write((const char*) &databuf[0], databuf.getSize());
481 // Deserialize bulk node data
482 void MapNode::deSerializeBulk(std::istream &is, int version,
483 MapNode *nodes, u32 nodecount,
484 u8 content_width, u8 params_width, bool compressed)
486 if(!ser_ver_supported(version))
487 throw VersionMismatchException("ERROR: MapNode format not supported");
489 assert(version >= 22);
490 assert(content_width == 1 || content_width == 2);
491 assert(params_width == 2);
493 // Uncompress or read data
494 u32 len = nodecount * (content_width + params_width);
495 SharedBuffer<u8> databuf(len);
498 std::ostringstream os(std::ios_base::binary);
499 decompressZlib(is, os);
500 std::string s = os.str();
502 throw SerializationError("deSerializeBulkNodes: "
503 "decompress resulted in invalid size");
504 memcpy(&databuf[0], s.c_str(), len);
508 is.read((char*) &databuf[0], len);
509 if(is.eof() || is.fail())
510 throw SerializationError("deSerializeBulkNodes: "
511 "failed to read bulk node data");
514 // Deserialize content
515 if(content_width == 1)
517 for(u32 i=0; i<nodecount; i++)
518 nodes[i].param0 = readU8(&databuf[i]);
520 else if(content_width == 2)
522 for(u32 i=0; i<nodecount; i++)
523 nodes[i].param0 = readU16(&databuf[i*2]);
526 // Deserialize param1
527 u32 start1 = content_width * nodecount;
528 for(u32 i=0; i<nodecount; i++)
529 nodes[i].param1 = readU8(&databuf[start1 + i]);
531 // Deserialize param2
532 u32 start2 = (content_width + 1) * nodecount;
533 if(content_width == 1)
535 for(u32 i=0; i<nodecount; i++) {
536 nodes[i].param2 = readU8(&databuf[start2 + i]);
537 if(nodes[i].param0 > 0x7F){
538 nodes[i].param0 <<= 4;
539 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
540 nodes[i].param2 &= 0x0F;
544 else if(content_width == 2)
546 for(u32 i=0; i<nodecount; i++)
547 nodes[i].param2 = readU8(&databuf[start2 + i]);
554 void MapNode::deSerialize_pre22(u8 *source, u8 version)
560 else if(version <= 9)
572 param0 |= (param2&0xf0)>>4;
577 // Convert special values from old version to new
580 // In these versions, CONTENT_IGNORE and CONTENT_AIR
582 // Version 19 is fucked up with sometimes the old values and sometimes not
584 param0 = CONTENT_IGNORE;
585 else if(param0 == 254)
586 param0 = CONTENT_AIR;
589 // Translate to our known version
590 *this = mapnode_translate_to_internal(*this, version);