111 changes: 28 additions & 83 deletions src/mapnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,28 +241,16 @@ void MapNode::serialize(u8 *dest, u8 version)
{
if(!ser_ver_supported(version))
throw VersionMismatchException("ERROR: MapNode format not supported");

if(version <= 21)
{
serialize_pre22(dest, version);
return;
}

if(version >= 24){
writeU16(dest+0, param0);
writeU8(dest+2, param1);
writeU8(dest+3, param2);
}
else{
writeU8(dest+0, (param0&0xFF));
writeU8(dest+1, param1);
if (param0 > 0x7F){
writeU8(dest+2, ((param2&0x0F) | ((param0&0x0F00)>>4)));
}
else{
writeU8(dest+2, param2);
}
}

// Can't do this anymore; we have 16-bit dynamically allocated node IDs
// in memory; conversion just won't work in this direction.
if(version < 24)
throw SerializationError("MapNode::serialize: serialization to "
"version < 24 not possible");

writeU16(dest+0, param0);
writeU8(dest+2, param1);
writeU8(dest+3, param2);
}
void MapNode::deSerialize(u8 *source, u8 version)
{
Expand Down Expand Up @@ -297,22 +285,20 @@ void MapNode::serializeBulk(std::ostream &os, int version,
if(!ser_ver_supported(version))
throw VersionMismatchException("ERROR: MapNode format not supported");

assert(version >= 22);
assert(content_width == 1 || content_width == 2);
assert(content_width == 2);
assert(params_width == 2);

// Can't do this anymore; we have 16-bit dynamically allocated node IDs
// in memory; conversion just won't work in this direction.
if(version < 24)
throw SerializationError("MapNode::serializeBulk: serialization to "
"version < 24 not possible");

SharedBuffer<u8> databuf(nodecount * (content_width + params_width));

// Serialize content
if(content_width == 1)
{
for(u32 i=0; i<nodecount; i++)
writeU8(&databuf[i], (nodes[i].param0&0x00FF));
}else if(content_width == 2)
{
for(u32 i=0; i<nodecount; i++)
writeU16(&databuf[i*2], nodes[i].param0);
}
for(u32 i=0; i<nodecount; i++)
writeU16(&databuf[i*2], nodes[i].param0);

// Serialize param1
u32 start1 = content_width * nodecount;
Expand All @@ -321,21 +307,8 @@ void MapNode::serializeBulk(std::ostream &os, int version,

// Serialize param2
u32 start2 = (content_width + 1) * nodecount;
if(content_width == 1)
{
for(u32 i=0; i<nodecount; i++) {
if(nodes[i].param0 > 0x7F){
writeU8(&databuf[start2 + i], ((nodes[i].param2&0x0F) | ((nodes[i].param0&0x0F00)>>4)));
}
else{
writeU8(&databuf[start2 + i], nodes[i].param2);
}
}
}else if(content_width == 2)
{
for(u32 i=0; i<nodecount; i++)
writeU8(&databuf[start2 + i], nodes[i].param2);
}
for(u32 i=0; i<nodecount; i++)
writeU8(&databuf[start2 + i], nodes[i].param2);

/*
Compress data to output stream
Expand Down Expand Up @@ -408,7 +381,8 @@ void MapNode::deSerializeBulk(std::istream &is, int version,
for(u32 i=0; i<nodecount; i++) {
nodes[i].param2 = readU8(&databuf[start2 + i]);
if(nodes[i].param0 > 0x7F){
nodes[i].param0 |= ((nodes[i].param2&0xF0)<<4);
nodes[i].param0 <<= 4;
nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
nodes[i].param2 &= 0x0F;
}
}
Expand All @@ -423,40 +397,6 @@ void MapNode::deSerializeBulk(std::istream &is, int version,
/*
Legacy serialization
*/
void MapNode::serialize_pre22(u8 *dest, u8 version)
{
// Translate to wanted version
MapNode n_foreign = mapnode_translate_from_internal(*this, version);

u8 actual_param0 = n_foreign.param0;

// Convert special values from new version to old
if(version <= 18)
{
// In these versions, CONTENT_IGNORE and CONTENT_AIR
// are 255 and 254
if(actual_param0 == CONTENT_IGNORE)
actual_param0 = 255;
else if(actual_param0 == CONTENT_AIR)
actual_param0 = 254;
}

if(version == 0)
{
dest[0] = actual_param0;
}
else if(version <= 9)
{
dest[0] = actual_param0;
dest[1] = n_foreign.param1;
}
else
{
dest[0] = actual_param0;
dest[1] = n_foreign.param1;
dest[2] = n_foreign.param2;
}
}
void MapNode::deSerialize_pre22(u8 *source, u8 version)
{
if(version <= 1)
Expand All @@ -473,6 +413,11 @@ void MapNode::deSerialize_pre22(u8 *source, u8 version)
param0 = source[0];
param1 = source[1];
param2 = source[2];
if(param0 > 0x7f){
param0 <<= 4;
param0 |= (param2&0xf0)>>4;
param2 &= 0x0f;
}
}

// Convert special values from old version to new
Expand Down
7 changes: 0 additions & 7 deletions src/mapnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ class INodeDefManager;
- Material = irrlicht's Material class
- Content = (content_t) content of a node
- Tile = TileSpec at some side of a node of some content type
Content ranges:
0x000...0x07f: param2 is fully usable
0x800...0xfff: param2 lower 4 bits are free
*/
typedef u16 content_t;
#define MAX_CONTENT 0xfff
Expand Down Expand Up @@ -84,8 +80,6 @@ struct MapNode
{
/*
Main content
0x00-0x7f: Short content type
0x80-0xff: Long content type
*/
u16 param0;

Expand Down Expand Up @@ -208,7 +202,6 @@ struct MapNode

private:
// Deprecated serialization methods
void serialize_pre22(u8 *dest, u8 version);
void deSerialize_pre22(u8 *source, u8 version);
};

Expand Down
23 changes: 3 additions & 20 deletions src/nodedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,18 +377,9 @@ class CNodeDefManager: public IWritableNodeDefManager
}
}
// CONTENT_IGNORE = not found
content_t getFreeId(bool require_full_param2)
content_t getFreeId()
{
// If allowed, first search in the large 4-bit-param2 pool
if(!require_full_param2){
for(u16 i=0x800; i<=0xfff; i++){
const ContentFeatures &f = m_content_features[i];
if(f.name == "")
return i;
}
}
// Then search from the small 8-bit-param2 pool
for(u16 i=0; i<=125; i++){
for(u32 i=0; i<=0xffff; i++){
const ContentFeatures &f = m_content_features[i];
if(f.name == "")
return i;
Expand Down Expand Up @@ -492,16 +483,8 @@ class CNodeDefManager: public IWritableNodeDefManager
u16 id = CONTENT_IGNORE;
bool found = m_name_id_mapping.getId(name, id); // ignore aliases
if(!found){
// Determine if full param2 is required
bool require_full_param2 = (
def.param_type_2 == CPT2_FULL
||
def.param_type_2 == CPT2_FLOWINGLIQUID
||
def.legacy_wallmounted
);
// Get some id
id = getFreeId(require_full_param2);
id = getFreeId();
if(id == CONTENT_IGNORE)
return CONTENT_IGNORE;
if(name != "")
Expand Down
2 changes: 1 addition & 1 deletion src/serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
21: dynamic content type allocation
22: minerals removed, facedir & wallmounted changed
23: new node metadata format
24: NodeTimers
24: 16-bit node ids and node timers
*/
// This represents an uninitialized or invalid format
#define SER_FMT_VER_INVALID 255
Expand Down