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"
24 #include "content_mapnode.h" // For mapnode_translate_*_internal
25 #include "serialization.h" // For ser_ver_supported
26 #include "util/serialize.h"
28 #include "util/numeric.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;
73 assert("Invalid light bank" == NULL);
76 bool MapNode::isLightDayNightEq(INodeDefManager *nodemgr) const
78 const ContentFeatures &f = nodemgr->get(*this);
81 if (f.param_type == CPT_LIGHT) {
82 u8 day = MYMAX(f.light_source, param1 & 0x0f);
83 u8 night = MYMAX(f.light_source, (param1 >> 4) & 0x0f);
84 isEqual = day == night;
92 u8 MapNode::getLight(enum LightBank bank, INodeDefManager *nodemgr) const
94 // Select the brightest of [light source, propagated light]
95 const ContentFeatures &f = nodemgr->get(*this);
98 if(f.param_type == CPT_LIGHT)
99 light = bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f;
103 return MYMAX(f.light_source, light);
106 u8 MapNode::getLightNoChecks(enum LightBank bank, const ContentFeatures *f) const
108 return MYMAX(f->light_source,
109 bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f);
112 bool MapNode::getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const
114 // Select the brightest of [light source, propagated light]
115 const ContentFeatures &f = nodemgr->get(*this);
116 if(f.param_type == CPT_LIGHT)
118 lightday = param1 & 0x0f;
119 lightnight = (param1>>4)&0x0f;
126 if(f.light_source > lightday)
127 lightday = f.light_source;
128 if(f.light_source > lightnight)
129 lightnight = f.light_source;
130 return f.param_type == CPT_LIGHT || f.light_source != 0;
133 u8 MapNode::getFaceDir(INodeDefManager *nodemgr) const
135 const ContentFeatures &f = nodemgr->get(*this);
136 if(f.param_type_2 == CPT2_FACEDIR)
137 return (getParam2() & 0x1F) % 24;
141 u8 MapNode::getWallMounted(INodeDefManager *nodemgr) const
143 const ContentFeatures &f = nodemgr->get(*this);
144 if(f.param_type_2 == CPT2_WALLMOUNTED)
145 return getParam2() & 0x07;
149 v3s16 MapNode::getWallMountedDir(INodeDefManager *nodemgr) const
151 switch(getWallMounted(nodemgr))
153 case 0: default: return v3s16(0,1,0);
154 case 1: return v3s16(0,-1,0);
155 case 2: return v3s16(1,0,0);
156 case 3: return v3s16(-1,0,0);
157 case 4: return v3s16(0,0,1);
158 case 5: return v3s16(0,0,-1);
162 void MapNode::rotateAlongYAxis(INodeDefManager *nodemgr, Rotation rot) {
163 ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
165 if (cpt2 == CPT2_FACEDIR) {
169 u8 newrot = param2 & 3;
171 param2 |= (newrot + rot) & 3;
172 } else if (cpt2 == CPT2_WALLMOUNTED) {
173 u8 wmountface = (param2 & 7);
177 Rotation oldrot = wallmounted_to_rot[wmountface - 2];
179 param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
183 static std::vector<aabb3f> transformNodeBox(const MapNode &n,
184 const NodeBox &nodebox, INodeDefManager *nodemgr)
186 std::vector<aabb3f> boxes;
187 if(nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED)
189 const std::vector<aabb3f> &fixed = nodebox.fixed;
190 int facedir = n.getFaceDir(nodemgr);
191 u8 axisdir = facedir>>2;
193 for(std::vector<aabb3f>::const_iterator
195 i != fixed.end(); i++)
199 if (nodebox.type == NODEBOX_LEVELED) {
200 box.MaxEdge.Y = -BS/2 + BS*((float)1/LEVELED_MAX) * n.getLevel(nodemgr);
208 box.MinEdge.rotateXZBy(-90);
209 box.MaxEdge.rotateXZBy(-90);
211 else if(facedir == 2)
213 box.MinEdge.rotateXZBy(180);
214 box.MaxEdge.rotateXZBy(180);
216 else if(facedir == 3)
218 box.MinEdge.rotateXZBy(90);
219 box.MaxEdge.rotateXZBy(90);
223 box.MinEdge.rotateYZBy(90);
224 box.MaxEdge.rotateYZBy(90);
227 box.MinEdge.rotateXYBy(90);
228 box.MaxEdge.rotateXYBy(90);
230 else if(facedir == 2)
232 box.MinEdge.rotateXYBy(180);
233 box.MaxEdge.rotateXYBy(180);
235 else if(facedir == 3)
237 box.MinEdge.rotateXYBy(-90);
238 box.MaxEdge.rotateXYBy(-90);
242 box.MinEdge.rotateYZBy(-90);
243 box.MaxEdge.rotateYZBy(-90);
246 box.MinEdge.rotateXYBy(-90);
247 box.MaxEdge.rotateXYBy(-90);
249 else if(facedir == 2)
251 box.MinEdge.rotateXYBy(180);
252 box.MaxEdge.rotateXYBy(180);
254 else if(facedir == 3)
256 box.MinEdge.rotateXYBy(90);
257 box.MaxEdge.rotateXYBy(90);
261 box.MinEdge.rotateXYBy(-90);
262 box.MaxEdge.rotateXYBy(-90);
265 box.MinEdge.rotateYZBy(90);
266 box.MaxEdge.rotateYZBy(90);
268 else if(facedir == 2)
270 box.MinEdge.rotateYZBy(180);
271 box.MaxEdge.rotateYZBy(180);
273 else if(facedir == 3)
275 box.MinEdge.rotateYZBy(-90);
276 box.MaxEdge.rotateYZBy(-90);
280 box.MinEdge.rotateXYBy(90);
281 box.MaxEdge.rotateXYBy(90);
284 box.MinEdge.rotateYZBy(-90);
285 box.MaxEdge.rotateYZBy(-90);
287 else if(facedir == 2)
289 box.MinEdge.rotateYZBy(180);
290 box.MaxEdge.rotateYZBy(180);
292 else if(facedir == 3)
294 box.MinEdge.rotateYZBy(90);
295 box.MaxEdge.rotateYZBy(90);
299 box.MinEdge.rotateXYBy(-180);
300 box.MaxEdge.rotateXYBy(-180);
303 box.MinEdge.rotateXZBy(90);
304 box.MaxEdge.rotateXZBy(90);
306 else if(facedir == 2)
308 box.MinEdge.rotateXZBy(180);
309 box.MaxEdge.rotateXZBy(180);
311 else if(facedir == 3)
313 box.MinEdge.rotateXZBy(-90);
314 box.MaxEdge.rotateXZBy(-90);
321 boxes.push_back(box);
324 else if(nodebox.type == NODEBOX_WALLMOUNTED)
326 v3s16 dir = n.getWallMountedDir(nodemgr);
329 if(dir == v3s16(0,1,0))
331 boxes.push_back(nodebox.wall_top);
334 else if(dir == v3s16(0,-1,0))
336 boxes.push_back(nodebox.wall_bottom);
343 nodebox.wall_side.MinEdge,
344 nodebox.wall_side.MaxEdge
347 for(s32 i=0; i<2; i++)
349 if(dir == v3s16(-1,0,0))
350 vertices[i].rotateXZBy(0);
351 if(dir == v3s16(1,0,0))
352 vertices[i].rotateXZBy(180);
353 if(dir == v3s16(0,0,-1))
354 vertices[i].rotateXZBy(90);
355 if(dir == v3s16(0,0,1))
356 vertices[i].rotateXZBy(-90);
359 aabb3f box = aabb3f(vertices[0]);
360 box.addInternalPoint(vertices[1]);
361 boxes.push_back(box);
364 else // NODEBOX_REGULAR
366 boxes.push_back(aabb3f(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2));
371 std::vector<aabb3f> MapNode::getNodeBoxes(INodeDefManager *nodemgr) const
373 const ContentFeatures &f = nodemgr->get(*this);
374 return transformNodeBox(*this, f.node_box, nodemgr);
377 std::vector<aabb3f> MapNode::getCollisionBoxes(INodeDefManager *nodemgr) const
379 const ContentFeatures &f = nodemgr->get(*this);
380 if (f.collision_box.fixed.empty())
381 return transformNodeBox(*this, f.node_box, nodemgr);
383 return transformNodeBox(*this, f.collision_box, nodemgr);
386 std::vector<aabb3f> MapNode::getSelectionBoxes(INodeDefManager *nodemgr) const
388 const ContentFeatures &f = nodemgr->get(*this);
389 return transformNodeBox(*this, f.selection_box, nodemgr);
392 u8 MapNode::getMaxLevel(INodeDefManager *nodemgr) const
394 const ContentFeatures &f = nodemgr->get(*this);
395 // todo: after update in all games leave only if (f.param_type_2 ==
396 if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
397 return LIQUID_LEVEL_MAX;
398 if(f.leveled || f.param_type_2 == CPT2_LEVELED)
403 u8 MapNode::getLevel(INodeDefManager *nodemgr) const
405 const ContentFeatures &f = nodemgr->get(*this);
406 // todo: after update in all games leave only if (f.param_type_2 ==
407 if(f.liquid_type == LIQUID_SOURCE)
408 return LIQUID_LEVEL_SOURCE;
409 if (f.param_type_2 == CPT2_FLOWINGLIQUID)
410 return getParam2() & LIQUID_LEVEL_MASK;
411 if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
412 return getParam2() & LIQUID_LEVEL_MASK;
413 if(f.leveled || f.param_type_2 == CPT2_LEVELED) {
414 u8 level = getParam2() & LEVELED_MASK;
417 if(f.leveled > LEVELED_MAX)
419 return f.leveled; //default
424 u8 MapNode::setLevel(INodeDefManager *nodemgr, s8 level)
428 setContent(CONTENT_AIR);
431 const ContentFeatures &f = nodemgr->get(*this);
432 if (f.param_type_2 == CPT2_FLOWINGLIQUID
433 || f.liquid_type == LIQUID_FLOWING
434 || f.liquid_type == LIQUID_SOURCE) {
435 if (level >= LIQUID_LEVEL_SOURCE) {
436 rest = level - LIQUID_LEVEL_SOURCE;
437 setContent(nodemgr->getId(f.liquid_alternative_source));
439 setContent(nodemgr->getId(f.liquid_alternative_flowing));
440 setParam2(level & LIQUID_LEVEL_MASK);
442 } else if (f.leveled || f.param_type_2 == CPT2_LEVELED) {
443 if (level > LEVELED_MAX) {
444 rest = level - LEVELED_MAX;
447 setParam2(level & LEVELED_MASK);
452 u8 MapNode::addLevel(INodeDefManager *nodemgr, s8 add)
454 s8 level = getLevel(nodemgr);
455 if (add == 0) level = 1;
457 return setLevel(nodemgr, level);
460 u32 MapNode::serializedLength(u8 version)
462 if(!ser_ver_supported(version))
463 throw VersionMismatchException("ERROR: MapNode format not supported");
467 else if(version <= 9)
469 else if(version <= 23)
474 void MapNode::serialize(u8 *dest, u8 version)
476 if(!ser_ver_supported(version))
477 throw VersionMismatchException("ERROR: MapNode format not supported");
479 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
480 // in memory; conversion just won't work in this direction.
482 throw SerializationError("MapNode::serialize: serialization to "
483 "version < 24 not possible");
485 writeU16(dest+0, param0);
486 writeU8(dest+2, param1);
487 writeU8(dest+3, param2);
489 void MapNode::deSerialize(u8 *source, u8 version)
491 if(!ser_ver_supported(version))
492 throw VersionMismatchException("ERROR: MapNode format not supported");
496 deSerialize_pre22(source, version);
501 param0 = readU16(source+0);
502 param1 = readU8(source+2);
503 param2 = readU8(source+3);
505 param0 = readU8(source+0);
506 param1 = readU8(source+1);
507 param2 = readU8(source+2);
509 param0 |= ((param2&0xF0)<<4);
514 void MapNode::serializeBulk(std::ostream &os, int version,
515 const MapNode *nodes, u32 nodecount,
516 u8 content_width, u8 params_width, bool compressed)
518 if(!ser_ver_supported(version))
519 throw VersionMismatchException("ERROR: MapNode format not supported");
521 sanity_check(content_width == 2);
522 sanity_check(params_width == 2);
524 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
525 // in memory; conversion just won't work in this direction.
527 throw SerializationError("MapNode::serializeBulk: serialization to "
528 "version < 24 not possible");
530 SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
533 for(u32 i=0; i<nodecount; i++)
534 writeU16(&databuf[i*2], nodes[i].param0);
537 u32 start1 = content_width * nodecount;
538 for(u32 i=0; i<nodecount; i++)
539 writeU8(&databuf[start1 + i], nodes[i].param1);
542 u32 start2 = (content_width + 1) * nodecount;
543 for(u32 i=0; i<nodecount; i++)
544 writeU8(&databuf[start2 + i], nodes[i].param2);
547 Compress data to output stream
552 compressZlib(databuf, os);
556 os.write((const char*) &databuf[0], databuf.getSize());
560 // Deserialize bulk node data
561 void MapNode::deSerializeBulk(std::istream &is, int version,
562 MapNode *nodes, u32 nodecount,
563 u8 content_width, u8 params_width, bool compressed)
565 if(!ser_ver_supported(version))
566 throw VersionMismatchException("ERROR: MapNode format not supported");
569 || (content_width != 1 && content_width != 2)
570 || params_width != 2)
571 FATAL_ERROR("Deserialize bulk node data error");
573 // Uncompress or read data
574 u32 len = nodecount * (content_width + params_width);
575 SharedBuffer<u8> databuf(len);
578 std::ostringstream os(std::ios_base::binary);
579 decompressZlib(is, os);
580 std::string s = os.str();
582 throw SerializationError("deSerializeBulkNodes: "
583 "decompress resulted in invalid size");
584 memcpy(&databuf[0], s.c_str(), len);
588 is.read((char*) &databuf[0], len);
589 if(is.eof() || is.fail())
590 throw SerializationError("deSerializeBulkNodes: "
591 "failed to read bulk node data");
594 // Deserialize content
595 if(content_width == 1)
597 for(u32 i=0; i<nodecount; i++)
598 nodes[i].param0 = readU8(&databuf[i]);
600 else if(content_width == 2)
602 for(u32 i=0; i<nodecount; i++)
603 nodes[i].param0 = readU16(&databuf[i*2]);
606 // Deserialize param1
607 u32 start1 = content_width * nodecount;
608 for(u32 i=0; i<nodecount; i++)
609 nodes[i].param1 = readU8(&databuf[start1 + i]);
611 // Deserialize param2
612 u32 start2 = (content_width + 1) * nodecount;
613 if(content_width == 1)
615 for(u32 i=0; i<nodecount; i++) {
616 nodes[i].param2 = readU8(&databuf[start2 + i]);
617 if(nodes[i].param0 > 0x7F){
618 nodes[i].param0 <<= 4;
619 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
620 nodes[i].param2 &= 0x0F;
624 else if(content_width == 2)
626 for(u32 i=0; i<nodecount; i++)
627 nodes[i].param2 = readU8(&databuf[start2 + i]);
634 void MapNode::deSerialize_pre22(u8 *source, u8 version)
640 else if(version <= 9)
652 param0 |= (param2&0xf0)>>4;
657 // Convert special values from old version to new
660 // In these versions, CONTENT_IGNORE and CONTENT_AIR
662 // Version 19 is fucked up with sometimes the old values and sometimes not
664 param0 = CONTENT_IGNORE;
665 else if(param0 == 254)
666 param0 = CONTENT_AIR;
669 // Translate to our known version
670 *this = mapnode_translate_to_internal(*this, version);