Move tool stuff to tool.{h,cpp}
[oweals/minetest.git] / src / map.cpp
index ba4130ca29e860a9b6196cc66a1be5ecdb990988..b70b76b71ef2efa24a5b3666cc12a646ad9d88d9 100644 (file)
@@ -21,7 +21,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "mapsector.h"
 #include "mapblock.h"
 #include "main.h"
+#ifndef SERVER
 #include "client.h"
+#endif
 #include "filesys.h"
 #include "utility.h"
 #include "voxel.h"
@@ -35,6 +37,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "settings.h"
 #include "log.h"
 #include "profiler.h"
+#include "mapnode_contentfeatures.h"
 
 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
 
@@ -1771,7 +1774,7 @@ void Map::transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks)
                /*
                        update the current node
                 */
-               bool flow_down_enabled = (flowing_down && ((n0.param2 & LIQUID_FLOW_DOWN_MASK) != LIQUID_FLOW_DOWN_MASK));
+               //bool flow_down_enabled = (flowing_down && ((n0.param2 & LIQUID_FLOW_DOWN_MASK) != LIQUID_FLOW_DOWN_MASK));
                if (content_features(new_node_content).liquid_type == LIQUID_FLOWING) {
                        // set level to last 3 bits, flowing down bit to 4th bit
                        n0.param2 = (flowing_down ? LIQUID_FLOW_DOWN_MASK : 0x00) | (new_node_level & LIQUID_LEVEL_MASK);
@@ -1833,7 +1836,7 @@ NodeMetadata* Map::getNodeMetadata(v3s16 p)
                                <<std::endl;
                return NULL;
        }
-       NodeMetadata *meta = block->m_node_metadata.get(p_rel);
+       NodeMetadata *meta = block->m_node_metadata->get(p_rel);
        return meta;
 }
 
@@ -1853,7 +1856,7 @@ void Map::setNodeMetadata(v3s16 p, NodeMetadata *meta)
                                <<std::endl;
                return;
        }
-       block->m_node_metadata.set(p_rel, meta);
+       block->m_node_metadata->set(p_rel, meta);
 }
 
 void Map::removeNodeMetadata(v3s16 p)
@@ -1867,7 +1870,7 @@ void Map::removeNodeMetadata(v3s16 p)
                                <<std::endl;
                return;
        }
-       block->m_node_metadata.remove(p_rel);
+       block->m_node_metadata->remove(p_rel);
 }
 
 void Map::nodeMetadataStep(float dtime,
@@ -1892,7 +1895,7 @@ void Map::nodeMetadataStep(float dtime,
                for(i=sectorblocks.begin(); i!=sectorblocks.end(); i++)
                {
                        MapBlock *block = *i;
-                       bool changed = block->m_node_metadata.step(dtime);
+                       bool changed = block->m_node_metadata->step(dtime);
                        if(changed)
                                changed_blocks[block->getPos()] = block;
                }
@@ -3611,6 +3614,35 @@ void ClientMap::OnRegisterSceneNode()
        ISceneNode::OnRegisterSceneNode();
 }
 
+static bool isOccluded(Map *map, v3s16 p0, v3s16 p1, float step, float stepfac,
+               float start_off, float end_off, u32 needed_count)
+{
+       float d0 = (float)BS * p0.getDistanceFrom(p1);
+       v3s16 u0 = p1 - p0;
+       v3f uf = v3f(u0.X, u0.Y, u0.Z) * BS;
+       uf.normalize();
+       v3f p0f = v3f(p0.X, p0.Y, p0.Z) * BS;
+       u32 count = 0;
+       for(float s=start_off; s<d0+end_off; s+=step){
+               v3f pf = p0f + uf * s;
+               v3s16 p = floatToInt(pf, BS);
+               MapNode n = map->getNodeNoEx(p);
+               bool is_transparent = false;
+               ContentFeatures &f = content_features(n);
+               if(f.solidness == 0)
+                       is_transparent = (f.visual_solidness != 2);
+               else
+                       is_transparent = (f.solidness != 2);
+               if(!is_transparent){
+                       count++;
+                       if(count >= needed_count)
+                               return true;
+               }
+               step *= stepfac;
+       }
+       return false;
+}
+
 void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
 {
        //m_dout<<DTIME<<"Rendering map..."<<std::endl;
@@ -3618,6 +3650,12 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
 
        bool is_transparent_pass = pass == scene::ESNRP_TRANSPARENT;
        
+       std::string prefix;
+       if(pass == scene::ESNRP_SOLID)
+               prefix = "CM: solid: ";
+       else
+               prefix = "CM: transparent: ";
+
        /*
                This is called two times per frame, reset on the non-transparent one
        */
@@ -3646,11 +3684,8 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
                Get all blocks and draw all visible ones
        */
 
-       v3s16 cam_pos_nodes(
-                       camera_position.X / BS,
-                       camera_position.Y / BS,
-                       camera_position.Z / BS);
-
+       v3s16 cam_pos_nodes = floatToInt(camera_position, BS);
+       
        v3s16 box_nodes_d = m_control.wanted_range * v3s16(1,1,1);
 
        v3s16 p_nodes_min = cam_pos_nodes - box_nodes_d;
@@ -3675,6 +3710,8 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
        
        // Number of blocks in rendering range
        u32 blocks_in_range = 0;
+       // Number of blocks occlusion culled
+       u32 blocks_occlusion_culled = 0;
        // Number of blocks in rendering range but don't have a mesh
        u32 blocks_in_range_without_mesh = 0;
        // Blocks that had mesh that would have been drawn according to
@@ -3687,27 +3724,19 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
        // Blocks from which stuff was actually drawn
        u32 blocks_without_stuff = 0;
 
-       int timecheck_counter = 0;
-       core::map<v2s16, MapSector*>::Iterator si;
-       si = m_sectors.getIterator();
-       for(; si.atEnd() == false; si++)
+       /*
+               Collect a set of blocks for drawing
+       */
+       
+       core::map<v3s16, MapBlock*> drawset;
+
        {
-               {
-                       timecheck_counter++;
-                       if(timecheck_counter > 50)
-                       {
-                               timecheck_counter = 0;
-                               int time2 = time(0);
-                               if(time2 > time1 + 4)
-                               {
-                                       infostream<<"ClientMap::renderMap(): "
-                                               "Rendering takes ages, returning."
-                                               <<std::endl;
-                                       return;
-                               }
-                       }
-               }
+       ScopeProfiler sp(g_profiler, prefix+"collecting blocks for drawing", SPT_AVG);
 
+       for(core::map<v2s16, MapSector*>::Iterator
+                       si = m_sectors.getIterator();
+                       si.atEnd() == false; si++)
+       {
                MapSector *sector = si.getNode()->getValue();
                v2s16 sp = sector->getPos();
                
@@ -3724,11 +3753,11 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
                sector->getBlocks(sectorblocks);
                
                /*
-                       Draw blocks
+                       Loop through blocks in sector
                */
-               
-               u32 sector_blocks_drawn = 0;
 
+               u32 sector_blocks_drawn = 0;
+               
                core::list< MapBlock * >::Iterator i;
                for(i=sectorblocks.begin(); i!=sectorblocks.end(); i++)
                {
@@ -3742,7 +3771,7 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
                        float range = 100000 * BS;
                        if(m_control.range_all == false)
                                range = m_control.wanted_range * BS;
-                       
+
                        float d = 0.0;
                        if(isBlockInSight(block->getPos(), camera_position,
                                        camera_direction, camera_fov,
@@ -3751,16 +3780,13 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
                                continue;
                        }
 
-                       // Okay, this block will be drawn. Reset usage timer.
-                       block->resetUsageTimer();
-                       
                        // This is ugly (spherical distance limit?)
                        /*if(m_control.range_all == false &&
                                        d - 0.5*BS*MAP_BLOCKSIZE > range)
                                continue;*/
-                       
-                       blocks_in_range++;
 
+                       blocks_in_range++;
+                       
 #if 1
                        /*
                                Update expired mesh (used for day/night change)
@@ -3810,10 +3836,51 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
 
                                mesh_expired = false;
                        }
-                       
 #endif
+
+                       /*
+                               Occlusion culling
+                       */
+
+                       v3s16 cpn = block->getPos() * MAP_BLOCKSIZE;
+                       cpn += v3s16(MAP_BLOCKSIZE/2, MAP_BLOCKSIZE/2, MAP_BLOCKSIZE/2);
+                       float step = BS*1;
+                       float stepfac = 1.1;
+                       float startoff = BS*1;
+                       float endoff = -BS*MAP_BLOCKSIZE*1.42*1.42;
+                       v3s16 spn = cam_pos_nodes + v3s16(0,0,0);
+                       s16 bs2 = MAP_BLOCKSIZE/2 + 1;
+                       u32 needed_count = 1;
+                       if(
+                               isOccluded(this, spn, cpn + v3s16(0,0,0),
+                                               step, stepfac, startoff, endoff, needed_count) &&
+                               isOccluded(this, spn, cpn + v3s16(bs2,bs2,bs2),
+                                               step, stepfac, startoff, endoff, needed_count) &&
+                               isOccluded(this, spn, cpn + v3s16(bs2,bs2,-bs2),
+                                               step, stepfac, startoff, endoff, needed_count) &&
+                               isOccluded(this, spn, cpn + v3s16(bs2,-bs2,bs2),
+                                               step, stepfac, startoff, endoff, needed_count) &&
+                               isOccluded(this, spn, cpn + v3s16(bs2,-bs2,-bs2),
+                                               step, stepfac, startoff, endoff, needed_count) &&
+                               isOccluded(this, spn, cpn + v3s16(-bs2,bs2,bs2),
+                                               step, stepfac, startoff, endoff, needed_count) &&
+                               isOccluded(this, spn, cpn + v3s16(-bs2,bs2,-bs2),
+                                               step, stepfac, startoff, endoff, needed_count) &&
+                               isOccluded(this, spn, cpn + v3s16(-bs2,-bs2,bs2),
+                                               step, stepfac, startoff, endoff, needed_count) &&
+                               isOccluded(this, spn, cpn + v3s16(-bs2,-bs2,-bs2),
+                                               step, stepfac, startoff, endoff, needed_count)
+                       )
+                       {
+                               blocks_occlusion_culled++;
+                               continue;
+                       }
+                       
+                       // This block is in range. Reset usage timer.
+                       block->resetUsageTimer();
+
                        /*
-                               Draw the faces of the block
+                               Ignore if mesh doesn't exist
                        */
                        {
                                JMutexAutoLock lock(block->mesh_mutex);
@@ -3824,72 +3891,112 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
                                        blocks_in_range_without_mesh++;
                                        continue;
                                }
-                               
-                               blocks_would_have_drawn++;
-                               if(blocks_drawn >= m_control.wanted_max_blocks
-                                               && m_control.range_all == false
-                                               && d > m_control.wanted_min_range * BS)
-                                       continue;
+                       }
+                       
+                       // Limit block count in case of a sudden increase
+                       blocks_would_have_drawn++;
+                       if(blocks_drawn >= m_control.wanted_max_blocks
+                                       && m_control.range_all == false
+                                       && d > m_control.wanted_min_range * BS)
+                               continue;
+                       
+                       // Add to set
+                       drawset[block->getPos()] = block;
+                       
+                       sector_blocks_drawn++;
+                       blocks_drawn++;
 
-                               blocks_drawn++;
-                               sector_blocks_drawn++;
+               } // foreach sectorblocks
 
-                               u32 c = mesh->getMeshBufferCount();
-                               bool stuff_actually_drawn = false;
-                               for(u32 i=0; i<c; i++)
+               if(sector_blocks_drawn != 0)
+                       m_last_drawn_sectors[sp] = true;
+       }
+       } // ScopeProfiler
+       
+       /*
+               Draw the selected MapBlocks
+       */
+
+       {
+       ScopeProfiler sp(g_profiler, prefix+"drawing blocks", SPT_AVG);
+
+       int timecheck_counter = 0;
+       for(core::map<v3s16, MapBlock*>::Iterator
+                       i = drawset.getIterator();
+                       i.atEnd() == false; i++)
+       {
+               {
+                       timecheck_counter++;
+                       if(timecheck_counter > 50)
+                       {
+                               timecheck_counter = 0;
+                               int time2 = time(0);
+                               if(time2 > time1 + 4)
                                {
-                                       scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
-                                       const video::SMaterial& material = buf->getMaterial();
-                                       video::IMaterialRenderer* rnd =
-                                                       driver->getMaterialRenderer(material.MaterialType);
-                                       bool transparent = (rnd && rnd->isTransparent());
-                                       // Render transparent on transparent pass and likewise.
-                                       if(transparent == is_transparent_pass)
-                                       {
-                                               if(buf->getVertexCount() == 0)
-                                                       errorstream<<"Block ["<<analyze_block(block)
-                                                                       <<"] contains an empty meshbuf"<<std::endl;
-                                               /*
-                                                       This *shouldn't* hurt too much because Irrlicht
-                                                       doesn't change opengl textures if the old
-                                                       material has the same texture.
-                                               */
-                                               driver->setMaterial(buf->getMaterial());
-                                               driver->drawMeshBuffer(buf);
-                                               vertex_count += buf->getVertexCount();
-                                               meshbuffer_count++;
-                                               stuff_actually_drawn = true;
-                                       }
+                                       infostream<<"ClientMap::renderMap(): "
+                                               "Rendering takes ages, returning."
+                                               <<std::endl;
+                                       return;
                                }
-                               if(stuff_actually_drawn)
-                                       blocks_had_pass_meshbuf++;
-                               else
-                                       blocks_without_stuff++;
                        }
-               } // foreach sectorblocks
+               }
+               
+               MapBlock *block = i.getNode()->getValue();
 
-               if(sector_blocks_drawn != 0)
+               /*
+                       Draw the faces of the block
+               */
                {
-                       m_last_drawn_sectors[sp] = true;
+                       JMutexAutoLock lock(block->mesh_mutex);
+
+                       scene::SMesh *mesh = block->mesh;
+                       assert(mesh);
+                       
+                       u32 c = mesh->getMeshBufferCount();
+                       bool stuff_actually_drawn = false;
+                       for(u32 i=0; i<c; i++)
+                       {
+                               scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
+                               const video::SMaterial& material = buf->getMaterial();
+                               video::IMaterialRenderer* rnd =
+                                               driver->getMaterialRenderer(material.MaterialType);
+                               bool transparent = (rnd && rnd->isTransparent());
+                               // Render transparent on transparent pass and likewise.
+                               if(transparent == is_transparent_pass)
+                               {
+                                       if(buf->getVertexCount() == 0)
+                                               errorstream<<"Block ["<<analyze_block(block)
+                                                               <<"] contains an empty meshbuf"<<std::endl;
+                                       /*
+                                               This *shouldn't* hurt too much because Irrlicht
+                                               doesn't change opengl textures if the old
+                                               material has the same texture.
+                                       */
+                                       driver->setMaterial(buf->getMaterial());
+                                       driver->drawMeshBuffer(buf);
+                                       vertex_count += buf->getVertexCount();
+                                       meshbuffer_count++;
+                                       stuff_actually_drawn = true;
+                               }
+                       }
+                       if(stuff_actually_drawn)
+                               blocks_had_pass_meshbuf++;
+                       else
+                               blocks_without_stuff++;
                }
        }
+       } // ScopeProfiler
        
-       std::string prefix = "CM: ";
-
        // Log only on solid pass because values are the same
        if(pass == scene::ESNRP_SOLID){
-               g_profiler->avg(prefix+"blocks in range", blocks_in_range);
+               g_profiler->avg("CM: blocks in range", blocks_in_range);
+               g_profiler->avg("CM: blocks occlusion culled", blocks_occlusion_culled);
                if(blocks_in_range != 0)
-                       g_profiler->avg(prefix+"blocks in range without mesh (frac)",
+                       g_profiler->avg("CM: blocks in range without mesh (frac)",
                                        (float)blocks_in_range_without_mesh/blocks_in_range);
-               g_profiler->avg(prefix+"blocks drawn", blocks_drawn);
+               g_profiler->avg("CM: blocks drawn", blocks_drawn);
        }
        
-       if(pass == scene::ESNRP_SOLID)
-               prefix = "CM: solid: ";
-       else
-               prefix = "CM: transparent: ";
-
        g_profiler->avg(prefix+"vertices drawn", vertex_count);
        if(blocks_had_pass_meshbuf != 0)
                g_profiler->avg(prefix+"meshbuffers per block",