]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/mapblock.cpp
comments
[dragonfireclient.git] / src / mapblock.cpp
index cf9114b20210f2879da2b7258a13b91b708c659e..15f3ad9a67591748161185b4b17b1d70c86d1e5e 100644 (file)
@@ -141,11 +141,32 @@ MapNode MapBlock::getNodeParentNoEx(v3s16 p)
                n: getNodeParent(p)
                n2: getNodeParent(p + face_dir)
                face_dir: axis oriented unit vector from p to p2
+       
+       returns encoded light value.
 */
 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);
@@ -243,12 +264,10 @@ void MapBlock::makeFastFace(TileSpec tile, u8 light, v3f p,
        //u8 li = decode_light(light);
        u8 li = light;
 
-       u8 alpha = 255;
-
+       u8 alpha = tile.alpha;
+       /*u8 alpha = 255;
        if(tile.id == TILE_WATER)
-       {
-               alpha = 128;
-       }
+               alpha = WATER_ALPHA;*/
 
        video::SColor c = video::SColor(alpha,li,li,li);
 
@@ -276,17 +295,8 @@ void MapBlock::makeFastFace(TileSpec tile, u8 light, v3f p,
 TileSpec MapBlock::getNodeTile(MapNode mn, v3s16 p, v3s16 face_dir)
 {
        TileSpec spec;
-
-       /*//DEBUG
-       {
-               spec.id = TILE_STONE;
-               return spec;
-       }*/
-
-       spec.feature = TILEFEAT_NONE;
-       //spec.id = TILE_STONE;
-       spec.id = mn.getTile(face_dir);
-
+       spec = mn.getTile(face_dir);
+       
        /*
                Check temporary modifications on this node
        */
@@ -299,12 +309,15 @@ TileSpec MapBlock::getNodeTile(MapNode mn, v3s16 p, v3s16 face_dir)
                struct NodeMod mod = n->getValue();
                if(mod.type == NODEMOD_CHANGECONTENT)
                {
-                       spec.id = content_tile(mod.param, face_dir);
+                       //spec = content_tile(mod.param, face_dir);
+                       MapNode mn2(mod.param);
+                       spec = mn2.getTile(face_dir);
                }
                if(mod.type == NODEMOD_CRACK)
                {
-                       spec.feature = TILEFEAT_CRACK;
-                       spec.param.crack.progression = mod.param;
+                       std::ostringstream os;
+                       os<<"[[mod:crack"<<mod.param;
+                       spec.name += os.str();
                }
        }
        
@@ -588,6 +601,8 @@ void MapBlock::updateMesh(u32 daynight_ratio)
        */
        
        {
+               //TimeTaker timer2("updateMesh() collect");
+
                // Lock this, as m_temp_mods will be used directly
                JMutexAutoLock lock(m_temp_mods_mutex);
 
@@ -645,56 +660,32 @@ void MapBlock::updateMesh(u32 daynight_ratio)
        
        mesh_new = new scene::SMesh();
        
+       MeshCollector collector;
+
        if(fastfaces_new.size() > 0)
        {
-               MeshCollector collector;
+               // avg 0ms (100ms spikes when loading textures the first time)
+               //TimeTaker timer2("updateMesh() mesh building");
 
                for(u32 i=0; i<fastfaces_new.size(); i++)
                {
                        FastFace &f = fastfaces_new[i];
 
                        const u16 indices[] = {0,1,2,2,3,0};
-                       
-                       if(f.tile.feature == TILEFEAT_NONE)
-                       {
-                               collector.append(tile_material_get(f.tile.id), f.vertices, 4,
-                                               indices, 6);
-                       }
-                       else if(f.tile.feature == TILEFEAT_CRACK)
-                       {
-                               const char *path = tile_texture_path_get(f.tile.id);
-
-                               u16 progression = f.tile.param.crack.progression;
 
-                               std::string name = (std::string)path + "_cracked_"
-                                               + (char)('0' + progression);
-
-                               TextureMod *mod = new CrackTextureMod(progression);
-
-                               video::ITexture *texture = g_irrlicht->getTexture(
-                                               TextureSpec(name, path, mod));
-
-                               video::SMaterial material = tile_material_get(f.tile.id);
-                               material.setTexture(0, texture);
-
-                               collector.append(material, f.vertices, 4, indices, 6);
-                       }
-                       else
-                       {
-                               // No such feature
-                               assert(0);
-                       }
+                       video::ITexture *texture = g_irrlicht->getTexture(f.tile.name);
+                       video::SMaterial material;
+                       material.Lighting = false;
+                       material.BackfaceCulling = false;
+                       material.setFlag(video::EMF_BILINEAR_FILTER, false);
+                       material.setFlag(video::EMF_ANTI_ALIASING, video::EAAM_OFF);
+                       material.setFlag(video::EMF_FOG_ENABLE, true);
+                       material.setTexture(0, texture);
+                       if(f.tile.alpha != 255)
+                               material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
+                       
+                       collector.append(material, f.vertices, 4, indices, 6);
                }
-
-               collector.fillMesh(mesh_new);
-
-               // Use VBO for mesh (this just would set this for ever buffer)
-               // This will lead to infinite memory usage because or irrlicht.
-               //mesh_new->setHardwareMappingHint(scene::EHM_STATIC);
-               
-               /*std::cout<<"MapBlock has "<<fastfaces_new.size()<<" faces "
-                               <<"and uses "<<mesh_new->getMeshBufferCount()
-                               <<" materials (meshbuffers)"<<std::endl;*/
        }
 
        /*
@@ -704,6 +695,9 @@ void MapBlock::updateMesh(u32 daynight_ratio)
                TODO: Optimize by using same meshbuffer for same textures
        */
 
+       // 0ms
+       //TimeTaker timer2("updateMesh() adding special stuff");
+
        for(s16 z=0; z<MAP_BLOCKSIZE; z++)
        for(s16 y=0; y<MAP_BLOCKSIZE; y++)
        for(s16 x=0; x<MAP_BLOCKSIZE; x++)
@@ -712,10 +706,11 @@ void MapBlock::updateMesh(u32 daynight_ratio)
 
                MapNode &n = getNodeRef(x,y,z);
                
+               /*
+                       Add torches to mesh
+               */
                if(n.d == CONTENT_TORCH)
                {
-                       //scene::IMeshBuffer *buf = new scene::SMeshBuffer();
-                       scene::SMeshBuffer *buf = new scene::SMeshBuffer();
                        video::SColor c(255,255,255,255);
 
                        video::S3DVertex vertices[4] =
@@ -746,36 +741,272 @@ void MapBlock::updateMesh(u32 daynight_ratio)
                                vertices[i].Pos += intToFloat(p + getPosRelative());
                        }
 
-                       u16 indices[] = {0,1,2,2,3,0};
-                       buf->append(vertices, 4, indices, 6);
-
                        // Set material
-                       buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
-                       buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
-                       buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
-                       //buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
-                       buf->getMaterial().MaterialType
+                       video::SMaterial material;
+                       material.setFlag(video::EMF_LIGHTING, false);
+                       material.setFlag(video::EMF_BACK_FACE_CULLING, false);
+                       material.setFlag(video::EMF_BILINEAR_FILTER, false);
+                       //material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
+                       material.MaterialType
                                        = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
                        if(dir == v3s16(0,-1,0))
-                               buf->getMaterial().setTexture(0,
+                               material.setTexture(0,
                                                g_irrlicht->getTexture(porting::getDataPath("torch_on_floor.png").c_str()));
                        else if(dir == v3s16(0,1,0))
-                               buf->getMaterial().setTexture(0,
+                               material.setTexture(0,
                                                g_irrlicht->getTexture(porting::getDataPath("torch_on_ceiling.png").c_str()));
                        // For backwards compatibility
                        else if(dir == v3s16(0,0,0))
-                               buf->getMaterial().setTexture(0,
+                               material.setTexture(0,
                                                g_irrlicht->getTexture(porting::getDataPath("torch_on_floor.png").c_str()));
                        else
-                               buf->getMaterial().setTexture(0, 
+                               material.setTexture(0, 
                                                g_irrlicht->getTexture(porting::getDataPath("torch.png").c_str()));
 
-                       // Add to mesh
-                       mesh_new->addMeshBuffer(buf);
-                       buf->drop();
+                       u16 indices[] = {0,1,2,2,3,0};
+                       // Add to mesh collector
+                       collector.append(material, vertices, 4, indices, 6);
+               }
+               /*
+                       Add flowing water to mesh
+               */
+               else if(n.d == CONTENT_WATER)
+               {
+                       bool top_is_water = false;
+                       try{
+                               MapNode n = getNodeParent(v3s16(x,y+1,z));
+                               if(n.d == CONTENT_WATER || n.d == CONTENT_WATERSOURCE)
+                                       top_is_water = true;
+                       }catch(InvalidPositionException &e){}
+                       
+                       u8 l = decode_light(n.getLightBlend(daynight_ratio));
+                       video::SColor c(WATER_ALPHA,l,l,l);
+                       
+                       // Neighbor water levels (key = relative position)
+                       // Includes current node
+                       core::map<v3s16, f32> neighbor_levels;
+                       core::map<v3s16, u8> neighbor_contents;
+                       v3s16 neighbor_dirs[9] = {
+                               v3s16(0,0,0),
+                               v3s16(0,0,1),
+                               v3s16(0,0,-1),
+                               v3s16(1,0,0),
+                               v3s16(-1,0,0),
+                               v3s16(1,0,1),
+                               v3s16(-1,0,-1),
+                               v3s16(1,0,-1),
+                               v3s16(-1,0,1),
+                       };
+                       for(u32 i=0; i<9; i++)
+                       {
+                               u8 content = CONTENT_AIR;
+                               float level = -0.5 * BS;
+                               try{
+                                       v3s16 p2 = p + neighbor_dirs[i];
+                                       MapNode n2 = getNodeParent(p2);
+
+                                       content = n2.d;
+
+                                       if(n2.d == CONTENT_WATERSOURCE)
+                                               level = 0.5 * BS;
+                                       else if(n2.d == CONTENT_WATER)
+                                               level = (-0.5 + ((float)n2.param2 + 0.5) / 8.0) * BS;
+                               }
+                               catch(InvalidPositionException &e){}
+                               
+                               neighbor_levels.insert(neighbor_dirs[i], level);
+                               neighbor_contents.insert(neighbor_dirs[i], content);
+                       }
+
+                       //float water_level = (-0.5 + ((float)n.param2 + 0.5) / 8.0) * BS;
+                       //float water_level = neighbor_levels[v3s16(0,0,0)];
+
+                       // Corner heights (average between four waters)
+                       f32 corner_levels[4];
+                       
+                       v3s16 halfdirs[4] = {
+                               v3s16(0,0,0),
+                               v3s16(1,0,0),
+                               v3s16(1,0,1),
+                               v3s16(0,0,1),
+                       };
+                       for(u32 i=0; i<4; i++)
+                       {
+                               v3s16 cornerdir = halfdirs[i];
+                               float cornerlevel = 0;
+                               u32 valid_count = 0;
+                               for(u32 j=0; j<4; j++)
+                               {
+                                       v3s16 neighbordir = cornerdir - halfdirs[j];
+                                       u8 content = neighbor_contents[neighbordir];
+                                       // Special case for source nodes
+                                       if(content == CONTENT_WATERSOURCE)
+                                       {
+                                               cornerlevel = 0.5*BS;
+                                               valid_count = 1;
+                                               break;
+                                       }
+                                       else if(content == CONTENT_WATER)
+                                       {
+                                               cornerlevel += neighbor_levels[neighbordir];
+                                               valid_count++;
+                                       }
+                                       else if(content == CONTENT_AIR)
+                                       {
+                                               cornerlevel += -0.5*BS;
+                                               valid_count++;
+                                       }
+                               }
+                               if(valid_count > 0)
+                                       cornerlevel /= valid_count;
+                               corner_levels[i] = cornerlevel;
+                       }
+
+                       /*
+                               Generate sides
+                       */
+
+                       v3s16 side_dirs[4] = {
+                               v3s16(1,0,0),
+                               v3s16(-1,0,0),
+                               v3s16(0,0,1),
+                               v3s16(0,0,-1),
+                       };
+                       s16 side_corners[4][2] = {
+                               {1, 2},
+                               {3, 0},
+                               {2, 3},
+                               {0, 1},
+                       };
+                       for(u32 i=0; i<4; i++)
+                       {
+                               v3s16 dir = side_dirs[i];
+
+                               //float neighbor_level = neighbor_levels[dir];
+                               /*if(neighbor_level > -0.5*BS + 0.001)
+                                       continue;*/
+                               /*if(neighbor_level > water_level - 0.1*BS)
+                                       continue;*/
+
+                               u8 neighbor_content = neighbor_contents[dir];
+
+                               if(neighbor_content != CONTENT_AIR
+                                               && neighbor_content != CONTENT_WATER)
+                                       continue;
+                               
+                               bool neighbor_is_water = (neighbor_content == CONTENT_WATER);
+
+                               if(neighbor_is_water == true && top_is_water == false)
+                                       continue;
+                               
+                               video::S3DVertex vertices[4] =
+                               {
+                                       /*video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c, 0,1),
+                                       video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c, 1,1),
+                                       video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c, 1,0),
+                                       video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c, 0,0),*/
+                                       video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,1),
+                                       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),
+                               };
+                               
+                               if(top_is_water)
+                               {
+                                       vertices[2].Pos.Y = 0.5*BS;
+                                       vertices[3].Pos.Y = 0.5*BS;
+                               }
+                               else
+                               {
+                                       vertices[2].Pos.Y = corner_levels[side_corners[i][0]];
+                                       vertices[3].Pos.Y = corner_levels[side_corners[i][1]];
+                               }
+
+                               if(neighbor_is_water)
+                               {
+                                       vertices[0].Pos.Y = corner_levels[side_corners[i][1]];
+                                       vertices[1].Pos.Y = corner_levels[side_corners[i][0]];
+                               }
+                               else
+                               {
+                                       vertices[0].Pos.Y = -0.5*BS;
+                                       vertices[1].Pos.Y = -0.5*BS;
+                               }
+                               
+                               for(s32 j=0; j<4; j++)
+                               {
+                                       if(dir == v3s16(0,0,1))
+                                               vertices[j].Pos.rotateXZBy(0);
+                                       if(dir == v3s16(0,0,-1))
+                                               vertices[j].Pos.rotateXZBy(180);
+                                       if(dir == v3s16(-1,0,0))
+                                               vertices[j].Pos.rotateXZBy(90);
+                                       if(dir == v3s16(1,0,-0))
+                                               vertices[j].Pos.rotateXZBy(-90);
+
+                                       vertices[j].Pos += intToFloat(p + getPosRelative());
+                               }
+
+                               // Set material
+                               video::SMaterial material;
+                               material.setFlag(video::EMF_LIGHTING, false);
+                               material.setFlag(video::EMF_BACK_FACE_CULLING, false);
+                               material.setFlag(video::EMF_BILINEAR_FILTER, false);
+                               material.setFlag(video::EMF_FOG_ENABLE, true);
+                               material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
+                               material.setTexture(0,
+                                               g_irrlicht->getTexture(porting::getDataPath("water.png").c_str()));
+
+                               u16 indices[] = {0,1,2,2,3,0};
+                               // Add to mesh collector
+                               collector.append(material, vertices, 4, indices, 6);
+                       }
+                       
+                       /*
+                               Generate top side, if appropriate
+                       */
+                       
+                       if(top_is_water == false)
+                       {
+                               video::S3DVertex vertices[4] =
+                               {
+                                       video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c, 0,1),
+                                       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),
+                               };
+
+                               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());
+                               }
+
+                               // Set material
+                               video::SMaterial material;
+                               material.setFlag(video::EMF_LIGHTING, false);
+                               material.setFlag(video::EMF_BACK_FACE_CULLING, false);
+                               material.setFlag(video::EMF_BILINEAR_FILTER, false);
+                               material.setFlag(video::EMF_FOG_ENABLE, true);
+                               material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
+                               material.setTexture(0,
+                                               g_irrlicht->getTexture(porting::getDataPath("water.png").c_str()));
+
+                               u16 indices[] = {0,1,2,2,3,0};
+                               // Add to mesh collector
+                               collector.append(material, vertices, 4, indices, 6);
+                       }
                }
        }
+
+       /*
+               Add stuff from collector to mesh
+       */
        
+       collector.fillMesh(mesh_new);
+
        /*
                Do some stuff to the mesh
        */
@@ -792,6 +1023,14 @@ void MapBlock::updateMesh(u32 daynight_ratio)
                mesh_new = NULL;
        }
 
+       // Use VBO for mesh (this just would set this for ever buffer)
+       // This will lead to infinite memory usage because or irrlicht.
+       //mesh_new->setHardwareMappingHint(scene::EHM_STATIC);
+       
+       /*std::cout<<"MapBlock has "<<fastfaces_new.size()<<" faces "
+                       <<"and uses "<<mesh_new->getMeshBufferCount()
+                       <<" materials (meshbuffers)"<<std::endl;*/
+       
        /*
                Replace the mesh
        */
@@ -872,7 +1111,8 @@ void MapBlock::updateMesh(u32 daynight_ratio)
        air is left in block.
 */
 bool MapBlock::propagateSunlight(core::map<v3s16, bool> & light_sources,
-               bool remove_light, bool *black_air_left)
+               bool remove_light, bool *black_air_left,
+               bool grow_grass)
 {
        // Whether the sunlight at the top of the bottom block is valid
        bool block_below_is_valid = true;
@@ -962,10 +1202,23 @@ bool MapBlock::propagateSunlight(core::map<v3s16, bool> & light_sources,
                                }
                                else if(n.light_propagates() == false)
                                {
-                                       // Turn mud into grass
-                                       if(n.d == CONTENT_MUD && current_light == LIGHT_SUN)
+                                       if(grow_grass)
                                        {
-                                               n.d = CONTENT_GRASS;
+                                               bool upper_is_air = false;
+                                               try
+                                               {
+                                                       if(getNodeParent(pos+v3s16(0,1,0)).d == CONTENT_AIR)
+                                                               upper_is_air = true;
+                                               }
+                                               catch(InvalidPositionException &e)
+                                               {
+                                               }
+                                               // Turn mud into grass
+                                               if(upper_is_air && n.d == CONTENT_MUD
+                                                               && current_light == LIGHT_SUN)
+                                               {
+                                                       n.d = CONTENT_GRASS;
+                                               }
                                        }
 
                                        // A solid object is on the way.
@@ -1157,7 +1410,8 @@ s16 MapBlock::getGroundLevel(v2s16 p2d)
                s16 y = MAP_BLOCKSIZE-1;
                for(; y>=0; y--)
                {
-                       if(is_ground_content(getNodeRef(p2d.X, y, p2d.Y).d))
+                       //if(is_ground_content(getNodeRef(p2d.X, y, p2d.Y).d))
+                       if(content_features(getNodeRef(p2d.X, y, p2d.Y).d).walkable)
                        {
                                if(y == MAP_BLOCKSIZE-1)
                                        return -2;
@@ -1234,13 +1488,13 @@ void MapBlock::serialize(std::ostream &os, u8 version)
                
                if(version >= 10)
                {
-                       // Get and compress pressure
-                       SharedBuffer<u8> pressuredata(nodecount);
+                       // Get and compress param2
+                       SharedBuffer<u8> param2data(nodecount);
                        for(u32 i=0; i<nodecount; i++)
                        {
-                               pressuredata[i] = data[i].pressure;
+                               param2data[i] = data[i].param2;
                        }
-                       compress(pressuredata, os, version);
+                       compress(param2data, os, version);
                }
        }
        // All other versions (newest)
@@ -1274,10 +1528,10 @@ void MapBlock::serialize(std::ostream &os, u8 version)
                        databuf[i+nodecount] = data[i].param;
                }
 
-               // Get pressure
+               // Get param2
                for(u32 i=0; i<nodecount; i++)
                {
-                       databuf[i+nodecount*2] = data[i].pressure;
+                       databuf[i+nodecount*2] = data[i].param2;
                }
 
                /*
@@ -1351,7 +1605,7 @@ void MapBlock::deSerialize(std::istream &is, u8 version)
        
                if(version >= 10)
                {
-                       // Uncompress and set pressure data
+                       // Uncompress and set param2 data
                        std::ostringstream os(std::ios_base::binary);
                        decompress(is, os, version);
                        std::string s = os.str();
@@ -1360,7 +1614,7 @@ void MapBlock::deSerialize(std::istream &is, u8 version)
                                                ("MapBlock::deSerialize: invalid format");
                        for(u32 i=0; i<s.size(); i++)
                        {
-                               data[i].pressure = s[i];
+                               data[i].param2 = s[i];
                        }
                }
        }
@@ -1392,10 +1646,10 @@ void MapBlock::deSerialize(std::istream &is, u8 version)
                {
                        data[i].param = s[i+nodecount];
                }
-               // Set pressure
+               // Set param2
                for(u32 i=0; i<nodecount; i++)
                {
-                       data[i].pressure = s[i+nodecount*2];
+                       data[i].param2 = s[i+nodecount*2];
                }
        }
 }