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"
35 // Create directly from a nodename
36 // If name is unknown, sets CONTENT_IGNORE
37 MapNode::MapNode(INodeDefManager *ndef, const std::string &name,
38 u8 a_param1, u8 a_param2)
40 content_t id = CONTENT_IGNORE;
41 ndef->getId(name, id);
47 void MapNode::setLight(enum LightBank bank, u8 a_light, INodeDefManager *nodemgr)
49 // If node doesn't contain light data, ignore this
50 if(nodemgr->get(*this).param_type != CPT_LIGHT)
52 if(bank == LIGHTBANK_DAY)
55 param1 |= a_light & 0x0f;
57 else if(bank == LIGHTBANK_NIGHT)
60 param1 |= (a_light & 0x0f)<<4;
66 u8 MapNode::getLight(enum LightBank bank, INodeDefManager *nodemgr) const
68 // Select the brightest of [light source, propagated light]
69 const ContentFeatures &f = nodemgr->get(*this);
71 if(f.param_type == CPT_LIGHT)
73 if(bank == LIGHTBANK_DAY)
74 light = param1 & 0x0f;
75 else if(bank == LIGHTBANK_NIGHT)
76 light = (param1>>4)&0x0f;
80 if(f.light_source > light)
81 light = f.light_source;
85 bool MapNode::getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const
87 // Select the brightest of [light source, propagated light]
88 const ContentFeatures &f = nodemgr->get(*this);
89 if(f.param_type == CPT_LIGHT)
91 lightday = param1 & 0x0f;
92 lightnight = (param1>>4)&0x0f;
99 if(f.light_source > lightday)
100 lightday = f.light_source;
101 if(f.light_source > lightnight)
102 lightnight = f.light_source;
103 return f.param_type == CPT_LIGHT || f.light_source != 0;
106 u8 MapNode::getFaceDir(INodeDefManager *nodemgr) const
108 const ContentFeatures &f = nodemgr->get(*this);
109 if(f.param_type_2 == CPT2_FACEDIR)
110 return getParam2() & 0x03;
114 u8 MapNode::getWallMounted(INodeDefManager *nodemgr) const
116 const ContentFeatures &f = nodemgr->get(*this);
117 if(f.param_type_2 == CPT2_WALLMOUNTED)
118 return getParam2() & 0x07;
122 v3s16 MapNode::getWallMountedDir(INodeDefManager *nodemgr) const
124 switch(getWallMounted(nodemgr))
126 case 0: default: return v3s16(0,1,0);
127 case 1: return v3s16(0,-1,0);
128 case 2: return v3s16(1,0,0);
129 case 3: return v3s16(-1,0,0);
130 case 4: return v3s16(0,0,1);
131 case 5: return v3s16(0,0,-1);
135 static std::vector<aabb3f> transformNodeBox(const MapNode &n,
136 const NodeBox &nodebox, INodeDefManager *nodemgr)
138 std::vector<aabb3f> boxes;
139 if(nodebox.type == NODEBOX_FIXED)
141 const std::vector<aabb3f> &fixed = nodebox.fixed;
142 int facedir = n.getFaceDir(nodemgr);
143 for(std::vector<aabb3f>::const_iterator
145 i != fixed.end(); i++)
150 box.MinEdge.rotateXZBy(-90);
151 box.MaxEdge.rotateXZBy(-90);
154 else if(facedir == 2)
156 box.MinEdge.rotateXZBy(180);
157 box.MaxEdge.rotateXZBy(180);
160 else if(facedir == 3)
162 box.MinEdge.rotateXZBy(90);
163 box.MaxEdge.rotateXZBy(90);
166 boxes.push_back(box);
169 else if(nodebox.type == NODEBOX_WALLMOUNTED)
171 v3s16 dir = n.getWallMountedDir(nodemgr);
174 if(dir == v3s16(0,1,0))
176 boxes.push_back(nodebox.wall_top);
179 else if(dir == v3s16(0,-1,0))
181 boxes.push_back(nodebox.wall_bottom);
188 nodebox.wall_side.MinEdge,
189 nodebox.wall_side.MaxEdge
192 for(s32 i=0; i<2; i++)
194 if(dir == v3s16(-1,0,0))
195 vertices[i].rotateXZBy(0);
196 if(dir == v3s16(1,0,0))
197 vertices[i].rotateXZBy(180);
198 if(dir == v3s16(0,0,-1))
199 vertices[i].rotateXZBy(90);
200 if(dir == v3s16(0,0,1))
201 vertices[i].rotateXZBy(-90);
204 aabb3f box = aabb3f(vertices[0]);
205 box.addInternalPoint(vertices[1]);
206 boxes.push_back(box);
209 else // NODEBOX_REGULAR
211 boxes.push_back(aabb3f(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2));
216 std::vector<aabb3f> MapNode::getNodeBoxes(INodeDefManager *nodemgr) const
218 const ContentFeatures &f = nodemgr->get(*this);
219 return transformNodeBox(*this, f.node_box, nodemgr);
222 std::vector<aabb3f> MapNode::getSelectionBoxes(INodeDefManager *nodemgr) const
224 const ContentFeatures &f = nodemgr->get(*this);
225 return transformNodeBox(*this, f.selection_box, nodemgr);
228 u32 MapNode::serializedLength(u8 version)
230 if(!ser_ver_supported(version))
231 throw VersionMismatchException("ERROR: MapNode format not supported");
235 else if(version <= 9)
237 else if(version <= 23)
242 void MapNode::serialize(u8 *dest, u8 version)
244 if(!ser_ver_supported(version))
245 throw VersionMismatchException("ERROR: MapNode format not supported");
247 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
248 // in memory; conversion just won't work in this direction.
250 throw SerializationError("MapNode::serialize: serialization to "
251 "version < 24 not possible");
253 writeU16(dest+0, param0);
254 writeU8(dest+2, param1);
255 writeU8(dest+3, param2);
257 void MapNode::deSerialize(u8 *source, u8 version)
259 if(!ser_ver_supported(version))
260 throw VersionMismatchException("ERROR: MapNode format not supported");
264 deSerialize_pre22(source, version);
269 param0 = readU16(source+0);
270 param1 = readU8(source+2);
271 param2 = readU8(source+3);
274 param0 = readU8(source+0);
275 param1 = readU8(source+1);
276 param2 = readU8(source+2);
278 param0 |= ((param2&0xF0)<<4);
283 void MapNode::serializeBulk(std::ostream &os, int version,
284 const MapNode *nodes, u32 nodecount,
285 u8 content_width, u8 params_width, bool compressed)
287 if(!ser_ver_supported(version))
288 throw VersionMismatchException("ERROR: MapNode format not supported");
290 assert(content_width == 2);
291 assert(params_width == 2);
293 // Can't do this anymore; we have 16-bit dynamically allocated node IDs
294 // in memory; conversion just won't work in this direction.
296 throw SerializationError("MapNode::serializeBulk: serialization to "
297 "version < 24 not possible");
299 SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
302 for(u32 i=0; i<nodecount; i++)
303 writeU16(&databuf[i*2], nodes[i].param0);
306 u32 start1 = content_width * nodecount;
307 for(u32 i=0; i<nodecount; i++)
308 writeU8(&databuf[start1 + i], nodes[i].param1);
311 u32 start2 = (content_width + 1) * nodecount;
312 for(u32 i=0; i<nodecount; i++)
313 writeU8(&databuf[start2 + i], nodes[i].param2);
316 Compress data to output stream
321 compressZlib(databuf, os);
325 os.write((const char*) &databuf[0], databuf.getSize());
329 // Deserialize bulk node data
330 void MapNode::deSerializeBulk(std::istream &is, int version,
331 MapNode *nodes, u32 nodecount,
332 u8 content_width, u8 params_width, bool compressed)
334 if(!ser_ver_supported(version))
335 throw VersionMismatchException("ERROR: MapNode format not supported");
337 assert(version >= 22);
338 assert(content_width == 1 || content_width == 2);
339 assert(params_width == 2);
341 // Uncompress or read data
342 u32 len = nodecount * (content_width + params_width);
343 SharedBuffer<u8> databuf(len);
346 std::ostringstream os(std::ios_base::binary);
347 decompressZlib(is, os);
348 std::string s = os.str();
350 throw SerializationError("deSerializeBulkNodes: "
351 "decompress resulted in invalid size");
352 memcpy(&databuf[0], s.c_str(), len);
356 is.read((char*) &databuf[0], len);
357 if(is.eof() || is.fail())
358 throw SerializationError("deSerializeBulkNodes: "
359 "failed to read bulk node data");
362 // Deserialize content
363 if(content_width == 1)
365 for(u32 i=0; i<nodecount; i++)
366 nodes[i].param0 = readU8(&databuf[i]);
368 else if(content_width == 2)
370 for(u32 i=0; i<nodecount; i++)
371 nodes[i].param0 = readU16(&databuf[i*2]);
374 // Deserialize param1
375 u32 start1 = content_width * nodecount;
376 for(u32 i=0; i<nodecount; i++)
377 nodes[i].param1 = readU8(&databuf[start1 + i]);
379 // Deserialize param2
380 u32 start2 = (content_width + 1) * nodecount;
381 if(content_width == 1)
383 for(u32 i=0; i<nodecount; i++) {
384 nodes[i].param2 = readU8(&databuf[start2 + i]);
385 if(nodes[i].param0 > 0x7F){
386 nodes[i].param0 <<= 4;
387 nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
388 nodes[i].param2 &= 0x0F;
392 else if(content_width == 2)
394 for(u32 i=0; i<nodecount; i++)
395 nodes[i].param2 = readU8(&databuf[start2 + i]);
402 void MapNode::deSerialize_pre22(u8 *source, u8 version)
408 else if(version <= 9)
420 param0 |= (param2&0xf0)>>4;
425 // Convert special values from old version to new
428 // In these versions, CONTENT_IGNORE and CONTENT_AIR
430 // Version 19 is fucked up with sometimes the old values and sometimes not
432 param0 = CONTENT_IGNORE;
433 else if(param0 == 254)
434 param0 = CONTENT_AIR;
437 // Translate to our known version
438 *this = mapnode_translate_to_internal(*this, version);