]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/mapnode.cpp
Bring po update out of cmake again
[dragonfireclient.git] / src / mapnode.cpp
index 3717bf0ab85f0f1910eaab373d42ea428dfe9064..1e9b64989767f440e45e87061076fa3131cf8c7d 100644 (file)
@@ -30,8 +30,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 ContentFeatures::~ContentFeatures()
 {
-       if(translate_to)
-               delete translate_to;
+       /*if(translate_to)
+               delete translate_to;*/
        if(initial_metadata)
                delete initial_metadata;
 }
@@ -138,6 +138,19 @@ void init_mapnode()
                        f->tiles[j].material_type = initial_material_type;
        }
 
+       /*
+               Initially set every block to be shown as an unknown block.
+               Don't touch CONTENT_IGNORE or CONTENT_AIR.
+       */
+       for(u16 i=0; i<256; i++)
+       {
+               if(i == CONTENT_IGNORE || i == CONTENT_AIR)
+                       continue;
+               ContentFeatures *f = &g_content_features[i];
+               f->setAllTextures("unknown_block.png");
+               f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
+       }
+
        /*
                Initialize mapnode content
        */
@@ -230,4 +243,150 @@ u8 MapNode::getMineral()
        return MINERAL_NONE;
 }
 
+u32 MapNode::serializedLength(u8 version)
+{
+       if(!ser_ver_supported(version))
+               throw VersionMismatchException("ERROR: MapNode format not supported");
+               
+       if(version == 0)
+               return 1;
+       else if(version <= 9)
+               return 2;
+       else
+               return 3;
+}
+void MapNode::serialize(u8 *dest, u8 version)
+{
+       if(!ser_ver_supported(version))
+               throw VersionMismatchException("ERROR: MapNode format not supported");
+               
+       u8 actual_d = d;
+
+       // Convert from new version to old
+       if(version <= 18)
+       {
+               // In these versions, CONTENT_IGNORE and CONTENT_AIR
+               // are 255 and 254
+               if(actual_d == CONTENT_IGNORE)
+                       actual_d = 255;
+               else if(actual_d == CONTENT_AIR)
+                       actual_d = 254;
+       }
+
+       if(version == 0)
+       {
+               dest[0] = actual_d;
+       }
+       else if(version <= 9)
+       {
+               dest[0] = actual_d;
+               dest[1] = param;
+       }
+       else
+       {
+               dest[0] = actual_d;
+               dest[1] = param;
+               dest[2] = param2;
+       }
+}
+void MapNode::deSerialize(u8 *source, u8 version)
+{
+       if(!ser_ver_supported(version))
+               throw VersionMismatchException("ERROR: MapNode format not supported");
+               
+       if(version == 0)
+       {
+               d = source[0];
+       }
+       else if(version == 1)
+       {
+               d = source[0];
+               // This version doesn't support saved lighting
+               if(light_propagates() || light_source() > 0)
+                       param = 0;
+               else
+                       param = source[1];
+       }
+       else if(version <= 9)
+       {
+               d = source[0];
+               param = source[1];
+       }
+       else
+       {
+               d = source[0];
+               param = source[1];
+               param2 = source[2];
+       }
+       
+       // Convert from old version to new
+       if(version <= 18)
+       {
+               // In these versions, CONTENT_IGNORE and CONTENT_AIR
+               // are 255 and 254
+               if(d == 255)
+                       d = CONTENT_IGNORE;
+               else if(d == 254)
+                       d = CONTENT_AIR;
+       }
+       // version 19 is fucked up with sometimes the old values and sometimes not
+       if(version == 19)
+       {
+               if(d == 255)
+                       d = CONTENT_IGNORE;
+               else if(d == 254)
+                       d = CONTENT_AIR;
+       }
+}
+
+/*
+       Gets lighting value at face of node
+       
+       Parameters must consist of air and !air.
+       Order doesn't matter.
+
+       If either of the nodes doesn't exist, light is 0.
+       
+       parameters:
+               daynight_ratio: 0...1000
+               n: getNodeParent(p)
+               n2: getNodeParent(p + face_dir)
+               face_dir: axis oriented unit vector from p to p2
+       
+       returns encoded light value.
+*/
+u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,
+               v3s16 face_dir)
+{
+       try{
+               u8 light;
+               u8 l1 = n.getLightBlend(daynight_ratio);
+               u8 l2 = n2.getLightBlend(daynight_ratio);
+               if(l1 > l2)
+                       light = l1;
+               else
+                       light = l2;
+
+               // Make some nice difference to different sides
+
+               // This makes light come from a corner
+               /*if(face_dir.X == 1 || face_dir.Z == 1 || face_dir.Y == -1)
+                       light = diminish_light(diminish_light(light));
+               else if(face_dir.X == -1 || face_dir.Z == -1)
+                       light = diminish_light(light);*/
+               
+               // All neighboring faces have different shade (like in minecraft)
+               if(face_dir.X == 1 || face_dir.X == -1 || face_dir.Y == -1)
+                       light = diminish_light(diminish_light(light));
+               else if(face_dir.Z == 1 || face_dir.Z == -1)
+                       light = diminish_light(light);
+
+               return light;
+       }
+       catch(InvalidPositionException &e)
+       {
+               return 0;
+       }
+}
+