]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/mapblock.cpp
Fixed insufficient error handling in narrow_to_wide, which caused a crash if the...
[dragonfireclient.git] / src / mapblock.cpp
index 2ec63dbde6e21bdf4f142bc78d273eb8980f2e7b..d489ec8ac11d47137c7ddc60e93bec895d5e464f 100644 (file)
@@ -148,25 +148,6 @@ u8 MapBlock::getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,
                v3s16 face_dir)
 {
        try{
-               // DEBUG
-               /*{
-                       if(n.d == CONTENT_WATER)
-                       {
-                               u8 l = n.param2*2;
-                               if(l > LIGHT_MAX)
-                                       l = LIGHT_MAX;
-                               return l;
-                       }
-                       if(n2.d == CONTENT_WATER)
-                       {
-                               u8 l = n2.param2*2;
-                               if(l > LIGHT_MAX)
-                                       l = LIGHT_MAX;
-                               return l;
-                       }
-               }*/
-
-
                u8 light;
                u8 l1 = n.getLightBlend(daynight_ratio);
                u8 l2 = n2.getLightBlend(daynight_ratio);
@@ -177,11 +158,13 @@ u8 MapBlock::getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,
 
                // 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)
@@ -272,32 +255,18 @@ void MapBlock::makeFastFace(TileSpec tile, u8 light, v3f p,
 
        video::SColor c = video::SColor(alpha,li,li,li);
 
-       face.vertices[0] = video::S3DVertex(vertex_pos[0], zerovector, c,
-                       core::vector2d<f32>(abs_scale,1));
-       face.vertices[1] = video::S3DVertex(vertex_pos[1], zerovector, c,
-                       core::vector2d<f32>(0,1));
-       face.vertices[2] = video::S3DVertex(vertex_pos[2], zerovector, c,
-                       core::vector2d<f32>(0,0));
-       face.vertices[3] = video::S3DVertex(vertex_pos[3], zerovector, c,
-                       core::vector2d<f32>(abs_scale,0));
-       
-       /*float x0 = (float)tile.tx/256.0;
-       float y0 = (float)tile.ty/256.0;
-       float w = ((float)tile.tw + 1.0)/256.0;
-       float h = ((float)tile.th + 1.0)/256.0;*/
-
        float x0 = tile.texture.pos.X;
        float y0 = tile.texture.pos.Y;
        float w = tile.texture.size.X;
        float h = tile.texture.size.Y;
 
-       face.vertices[0] = video::S3DVertex(vertex_pos[0], zerovector, c,
+       face.vertices[0] = video::S3DVertex(vertex_pos[0], v3f(0,1,0), c,
                        core::vector2d<f32>(x0+w*abs_scale, y0+h));
-       face.vertices[1] = video::S3DVertex(vertex_pos[1], zerovector, c,
+       face.vertices[1] = video::S3DVertex(vertex_pos[1], v3f(0,1,0), c,
                        core::vector2d<f32>(x0, y0+h));
-       face.vertices[2] = video::S3DVertex(vertex_pos[2], zerovector, c,
+       face.vertices[2] = video::S3DVertex(vertex_pos[2], v3f(0,1,0), c,
                        core::vector2d<f32>(x0, y0));
-       face.vertices[3] = video::S3DVertex(vertex_pos[3], zerovector, c,
+       face.vertices[3] = video::S3DVertex(vertex_pos[3], v3f(0,1,0), c,
                        core::vector2d<f32>(x0+w*abs_scale, y0));
 
        face.tile = tile;
@@ -341,20 +310,21 @@ TileSpec MapBlock::getNodeTile(MapNode mn, v3s16 p, v3s16 face_dir,
                                Get texture id, translate it to name, append stuff to
                                name, get texture id
                        */
+
                        // Get original texture name
                        u32 orig_id = spec.texture.id;
                        std::string orig_name = g_texturesource->getTextureName(orig_id);
+
                        // Create new texture name
                        std::ostringstream os;
                        os<<orig_name<<"^[crack"<<mod.param;
-                       //os<<orig_name<<"^[progressbar0.5";
-                       //os<<"mese.png";
+
                        // Get new texture
                        u32 new_id = g_texturesource->getTextureId(os.str());
                        
-                       dstream<<"MapBlock::getNodeTile(): Switching from "
+                       /*dstream<<"MapBlock::getNodeTile(): Switching from "
                                        <<orig_name<<" to "<<os.str()<<" ("
-                                       <<orig_id<<" to "<<new_id<<")"<<std::endl;
+                                       <<orig_id<<" to "<<new_id<<")"<<std::endl;*/
                        
                        spec.texture = g_texturesource->getTexture(new_id);
                }
@@ -677,7 +647,7 @@ void MapBlock::updateMesh(u32 daynight_ratio)
        
        float node_water_level = 1.0;
        if(new_style_water)
-               node_water_level = 0.9;
+               node_water_level = 0.85;
        
        /*
                We are including the faces of the trailing edges of the block.
@@ -752,12 +722,11 @@ void MapBlock::updateMesh(u32 daynight_ratio)
                //TimeTaker timer2("updateMesh() mesh building");
 
                video::SMaterial material;
-               material.Lighting = false;
-               material.BackfaceCulling = false;
+               material.setFlag(video::EMF_LIGHTING, false);
                material.setFlag(video::EMF_BILINEAR_FILTER, false);
-               material.setFlag(video::EMF_ANTI_ALIASING, video::EAAM_OFF);
-               //material.setFlag(video::EMF_ANTI_ALIASING, video::EAAM_SIMPLE);
                material.setFlag(video::EMF_FOG_ENABLE, true);
+               //material.setFlag(video::EMF_ANTI_ALIASING, video::EAAM_OFF);
+               //material.setFlag(video::EMF_ANTI_ALIASING, video::EAAM_SIMPLE);
 
                for(u32 i=0; i<fastfaces_new.size(); i++)
                {
@@ -790,7 +759,7 @@ void MapBlock::updateMesh(u32 daynight_ratio)
        // Flowing water material
        video::SMaterial material_water1;
        material_water1.setFlag(video::EMF_LIGHTING, false);
-       material_water1.setFlag(video::EMF_BACK_FACE_CULLING, false);
+       //material_water1.setFlag(video::EMF_BACK_FACE_CULLING, false);
        material_water1.setFlag(video::EMF_BILINEAR_FILTER, false);
        material_water1.setFlag(video::EMF_FOG_ENABLE, true);
        material_water1.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
@@ -853,7 +822,7 @@ void MapBlock::updateMesh(u32 daynight_ratio)
                                if(dir == v3s16(0,1,0))
                                        vertices[i].Pos.rotateXZBy(-45);
 
-                               vertices[i].Pos += intToFloat(p + getPosRelative());
+                               vertices[i].Pos += intToFloat(p + getPosRelative(), BS);
                        }
 
                        // Set material
@@ -864,20 +833,20 @@ void MapBlock::updateMesh(u32 daynight_ratio)
                        //material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
                        material.MaterialType
                                        = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
-                       //TODO
-                       /*if(dir == v3s16(0,-1,0))
+
+                       if(dir == v3s16(0,-1,0))
                                material.setTexture(0,
-                                               g_irrlicht->getTexture("torch_on_floor.png"));
+                                               g_texturesource->getTextureRaw("torch_on_floor.png"));
                        else if(dir == v3s16(0,1,0))
                                material.setTexture(0,
-                                               g_irrlicht->getTexture("torch_on_ceiling.png"));
+                                               g_texturesource->getTextureRaw("torch_on_ceiling.png"));
                        // For backwards compatibility
                        else if(dir == v3s16(0,0,0))
                                material.setTexture(0,
-                                               g_irrlicht->getTexture("torch_on_floor.png"));
+                                               g_texturesource->getTextureRaw("torch_on_floor.png"));
                        else
                                material.setTexture(0, 
-                                               g_irrlicht->getTexture("torch.png"));*/
+                                               g_texturesource->getTextureRaw("torch.png"));
 
                        u16 indices[] = {0,1,2,2,3,0};
                        // Add to mesh collector
@@ -1039,9 +1008,9 @@ void MapBlock::updateMesh(u32 daynight_ratio)
                                        video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,1),
                                        video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
                                        video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),*/
-                                       video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
+                                       video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
                                                        pa_water1.x0(), pa_water1.y1()),
-                                       video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
+                                       video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
                                                        pa_water1.x1(), pa_water1.y1()),
                                        video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
                                                        pa_water1.x1(), pa_water1.y0()),
@@ -1097,7 +1066,7 @@ void MapBlock::updateMesh(u32 daynight_ratio)
                                        if(dir == v3s16(1,0,-0))
                                                vertices[j].Pos.rotateXZBy(-90);
 
-                                       vertices[j].Pos += intToFloat(p + getPosRelative());
+                                       vertices[j].Pos += intToFloat(p + getPosRelative(), BS);
                                }
 
                                u16 indices[] = {0,1,2,2,3,0};
@@ -1117,22 +1086,26 @@ void MapBlock::updateMesh(u32 daynight_ratio)
                                        video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c, 1,1),
                                        video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
                                        video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),*/
-                                       video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
+                                       video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
                                                        pa_water1.x0(), pa_water1.y1()),
-                                       video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
-                                                       pa_water1.x1(), pa_water1.y1()),
                                        video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
+                                                       pa_water1.x1(), pa_water1.y1()),
+                                       video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
                                                        pa_water1.x1(), pa_water1.y0()),
-                                       video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
+                                       video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
                                                        pa_water1.x0(), pa_water1.y0()),
                                };
+                               
+                               // This fixes a strange bug
+                               s32 corner_resolve[4] = {3,2,1,0};
 
                                for(s32 i=0; i<4; i++)
                                {
                                        //vertices[i].Pos.Y += water_level;
                                        //vertices[i].Pos.Y += neighbor_levels[v3s16(0,0,0)];
-                                       vertices[i].Pos.Y += corner_levels[i];
-                                       vertices[i].Pos += intToFloat(p + getPosRelative());
+                                       s32 j = corner_resolve[i];
+                                       vertices[i].Pos.Y += corner_levels[j];
+                                       vertices[i].Pos += intToFloat(p + getPosRelative(), BS);
                                }
 
                                u16 indices[] = {0,1,2,2,3,0};
@@ -1169,20 +1142,20 @@ void MapBlock::updateMesh(u32 daynight_ratio)
                                video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c, 1,1),
                                video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
                                video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),*/
-                               video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
+                               video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
                                                pa_water1.x0(), pa_water1.y1()),
-                               video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
-                                               pa_water1.x1(), pa_water1.y1()),
                                video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
+                                               pa_water1.x1(), pa_water1.y1()),
+                               video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
                                                pa_water1.x1(), pa_water1.y0()),
-                               video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
+                               video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
                                                pa_water1.x0(), pa_water1.y0()),
                        };
 
                        for(s32 i=0; i<4; i++)
                        {
                                vertices[i].Pos.Y += (-0.5+node_water_level)*BS;
-                               vertices[i].Pos += intToFloat(p + getPosRelative());
+                               vertices[i].Pos += intToFloat(p + getPosRelative(), BS);
                        }
 
                        u16 indices[] = {0,1,2,2,3,0};
@@ -1194,7 +1167,8 @@ void MapBlock::updateMesh(u32 daynight_ratio)
                */
                else if(n.d == CONTENT_LEAVES && new_style_leaves)
                {
-                       u8 l = decode_light(n.getLightBlend(daynight_ratio));
+                       /*u8 l = decode_light(n.getLightBlend(daynight_ratio));*/
+                       u8 l = decode_light(undiminish_light(n.getLightBlend(daynight_ratio)));
                        video::SColor c(255,l,l,l);
 
                        for(u32 j=0; j<6; j++)
@@ -1248,7 +1222,7 @@ void MapBlock::updateMesh(u32 daynight_ratio)
 
                                for(u16 i=0; i<4; i++)
                                {
-                                       vertices[i].Pos += intToFloat(p + getPosRelative());
+                                       vertices[i].Pos += intToFloat(p + getPosRelative(), BS);
                                }
 
                                u16 indices[] = {0,1,2,2,3,0};
@@ -1622,7 +1596,7 @@ void MapBlock::stepObjects(float dtime, bool server, u32 daynight_ratio)
                                if(getNode(p).d == CONTENT_AIR
                                                && getNode(p).getLightBlend(daynight_ratio) <= 11)
                                {
-                                       RatObject *obj = new RatObject(NULL, -1, intToFloat(p));
+                                       RatObject *obj = new RatObject(NULL, -1, intToFloat(p, BS));
                                        addObject(obj);
                                }
                        }
@@ -1782,11 +1756,11 @@ void MapBlock::serialize(std::ostream &os, u8 version)
                // First byte
                u8 flags = 0;
                if(is_underground)
-                       flags |= 1;
+                       flags |= 0x01;
                if(m_day_night_differs)
-                       flags |= 2;
+                       flags |= 0x02;
                if(m_lighting_expired)
-                       flags |= 3;
+                       flags |= 0x04;
                os.write((char*)&flags, 1);
 
                u32 nodecount = MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE;
@@ -1906,9 +1880,9 @@ void MapBlock::deSerialize(std::istream &is, u8 version)
 
                u8 flags;
                is.read((char*)&flags, 1);
-               is_underground = (flags & 1) ? true : false;
-               m_day_night_differs = (flags & 2) ? true : false;
-               m_lighting_expired = (flags & 3) ? true : false;
+               is_underground = (flags & 0x01) ? true : false;
+               m_day_night_differs = (flags & 0x02) ? true : false;
+               m_lighting_expired = (flags & 0x04) ? true : false;
 
                // Uncompress data
                std::ostringstream os(std::ios_base::binary);