]> git.lizzy.rs Git - minetest.git/blobdiff - src/map.cpp
updated minetest.conf.example and changed client_unload_unused_data_timeout default...
[minetest.git] / src / map.cpp
index 3164028b7b1d2e9a7403b32ad2afdab2b744f43b..0f3741691532da3283786c80c93502f63a7578be 100644 (file)
@@ -26,10 +26,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "utility.h"
 #include "voxel.h"
 #include "porting.h"
-#include "mineral.h"
-#include "noise.h"
-#include "serverobject.h"
-#include "content_mapnode.h"
 #include "mapgen.h"
 #include "nodemetadata.h"
 
@@ -759,6 +755,25 @@ void Map::updateLighting(enum LightBank bank,
 
                }
        }
+       
+       /*
+               Enable this to disable proper lighting for speeding up map
+               generation for testing or whatever
+       */
+#if 0
+       //if(g_settings.get(""))
+       {
+               core::map<v3s16, MapBlock*>::Iterator i;
+               i = blocks_to_update.getIterator();
+               for(; i.atEnd() == false; i++)
+               {
+                       MapBlock *block = i.getNode()->getValue();
+                       v3s16 p = block->getPos();
+                       block->setLightingExpired(false);
+               }
+               return;
+       }
+#endif
 
 #if 0
        {
@@ -901,7 +916,7 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n,
        {
        }
 
-#if 1
+#if 0
        /*
                If the new node is solid and there is grass below, change it to mud
        */
@@ -1371,9 +1386,14 @@ bool Map::dayNightDiffed(v3s16 blockpos)
 /*
        Updates usage timers
 */
-void Map::timerUpdate(float dtime)
+void Map::timerUpdate(float dtime, float unload_timeout,
+               core::list<v3s16> *unloaded_blocks)
 {
-       //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
+       bool save_before_unloading = (mapType() == MAPTYPE_SERVER);
+       
+       core::list<v2s16> sector_deletion_queue;
+       u32 deleted_blocks_count = 0;
+       u32 saved_blocks_count = 0;
 
        core::map<v2s16, MapSector*>::Iterator si;
 
@@ -1382,48 +1402,85 @@ void Map::timerUpdate(float dtime)
        {
                MapSector *sector = si.getNode()->getValue();
 
+               bool all_blocks_deleted = true;
+
                core::list<MapBlock*> blocks;
                sector->getBlocks(blocks);
                for(core::list<MapBlock*>::Iterator i = blocks.begin();
                                i != blocks.end(); i++)
                {
-                       (*i)->incrementUsageTimer(dtime);
+                       MapBlock *block = (*i);
+                       
+                       block->incrementUsageTimer(dtime);
+                       
+                       if(block->getUsageTimer() > unload_timeout)
+                       {
+                               v3s16 p = block->getPos();
+
+                               // Save if modified
+                               if(block->getModified() != MOD_STATE_CLEAN
+                                               && save_before_unloading)
+                               {
+                                       saveBlock(block);
+                                       saved_blocks_count++;
+                               }
+
+                               // Delete from memory
+                               sector->deleteBlock(block);
+
+                               if(unloaded_blocks)
+                                       unloaded_blocks->push_back(p);
+
+                               deleted_blocks_count++;
+                       }
+                       else
+                       {
+                               all_blocks_deleted = false;
+                       }
+               }
+
+               if(all_blocks_deleted)
+               {
+                       sector_deletion_queue.push_back(si.getNode()->getKey());
                }
        }
+       
+       // Finally delete the empty sectors
+       deleteSectors(sector_deletion_queue);
+       
+       if(deleted_blocks_count != 0)
+       {
+               PrintInfo(dstream); // ServerMap/ClientMap:
+               dstream<<"Unloaded "<<deleted_blocks_count
+                               <<" blocks from memory";
+               if(save_before_unloading)
+                       dstream<<", of which "<<saved_blocks_count<<" were written";
+               dstream<<"."<<std::endl;
+       }
 }
 
-void Map::deleteSectors(core::list<v2s16> &list, bool only_blocks)
+void Map::deleteSectors(core::list<v2s16> &list)
 {
        core::list<v2s16>::Iterator j;
        for(j=list.begin(); j!=list.end(); j++)
        {
                MapSector *sector = m_sectors[*j];
-               if(only_blocks)
-               {
-                       sector->deleteBlocks();
-               }
-               else
-               {
-                       /*
-                               If sector is in sector cache, remove it from there
-                       */
-                       if(m_sector_cache == sector)
-                       {
-                               m_sector_cache = NULL;
-                       }
-                       /*
-                               Remove from map and delete
-                       */
-                       m_sectors.remove(*j);
-                       delete sector;
-               }
+               // If sector is in sector cache, remove it from there
+               if(m_sector_cache == sector)
+                       m_sector_cache = NULL;
+               // Remove from map and delete
+               m_sectors.remove(*j);
+               delete sector;
        }
 }
 
-u32 Map::unloadUnusedData(float timeout, bool only_blocks,
+#if 0
+void Map::unloadUnusedData(float timeout,
                core::list<v3s16> *deleted_blocks)
 {
        core::list<v2s16> sector_deletion_queue;
+       u32 deleted_blocks_count = 0;
+       u32 saved_blocks_count = 0;
 
        core::map<v2s16, MapSector*>::Iterator si = m_sectors.getIterator();
        for(; si.atEnd() == false; si++)
@@ -1438,14 +1495,18 @@ u32 Map::unloadUnusedData(float timeout, bool only_blocks,
                                i != blocks.end(); i++)
                {
                        MapBlock *block = (*i);
-
+                       
                        if(block->getUsageTimer() > timeout)
                        {
                                // Save if modified
                                if(block->getModified() != MOD_STATE_CLEAN)
+                               {
                                        saveBlock(block);
+                                       saved_blocks_count++;
+                               }
                                // Delete from memory
                                sector->deleteBlock(block);
+                               deleted_blocks_count++;
                        }
                        else
                        {
@@ -1459,37 +1520,16 @@ u32 Map::unloadUnusedData(float timeout, bool only_blocks,
                }
        }
 
-#if 0
-       core::map<v2s16, MapSector*>::Iterator i = m_sectors.getIterator();
-       for(; i.atEnd() == false; i++)
-       {
-               MapSector *sector = i.getNode()->getValue();
-               /*
-                       Delete sector from memory if it hasn't been used in a long time
-               */
-               if(sector->usage_timer > timeout)
-               {
-                       sector_deletion_queue.push_back(i.getNode()->getKey());
+       deleteSectors(sector_deletion_queue);
 
-                       if(deleted_blocks != NULL)
-                       {
-                               // Collect positions of blocks of sector
-                               MapSector *sector = i.getNode()->getValue();
-                               core::list<MapBlock*> blocks;
-                               sector->getBlocks(blocks);
-                               for(core::list<MapBlock*>::Iterator i = blocks.begin();
-                                               i != blocks.end(); i++)
-                               {
-                                       deleted_blocks->push_back((*i)->getPos());
-                               }
-                       }
-               }
-       }
-#endif
+       dstream<<"Map: Unloaded "<<deleted_blocks_count<<" blocks from memory"
+                       <<", of which "<<saved_blocks_count<<" were wr."
+                       <<std::endl;
 
-       deleteSectors(sector_deletion_queue, only_blocks);
-       return sector_deletion_queue.getSize();
+       //return sector_deletion_queue.getSize();
+       //return deleted_blocks_count;
 }
+#endif
 
 void Map::PrintInfo(std::ostream &out)
 {
@@ -1516,7 +1556,7 @@ void Map::transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks)
                */
                v3s16 p0 = m_transforming_liquid.pop_front();
 
-               MapNode n0 = getNode(p0);
+               MapNode n0 = getNodeNoEx(p0);
 
                // Don't deal with non-liquids
                if(content_liquid(n0.d) == false)
@@ -1548,13 +1588,10 @@ void Map::transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks)
                        };
                        for(u16 i=0; i<5; i++)
                        {
-                               try
-                               {
-
                                bool from_top = (i==0);
 
                                v3s16 p2 = p0 + dirs_from[i];
-                               MapNode n2 = getNode(p2);
+                               MapNode n2 = getNodeNoEx(p2);
 
                                if(content_liquid(n2.d))
                                {
@@ -1588,10 +1625,6 @@ void Map::transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks)
                                        if(new_liquid_level > new_liquid_level_max)
                                                new_liquid_level_max = new_liquid_level;
                                }
-
-                               }catch(InvalidPositionException &e)
-                               {
-                               }
                        } //for
 
                        /*
@@ -1634,20 +1667,13 @@ void Map::transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks)
                                };
                                for(u16 i=0; i<6; i++)
                                {
-                                       try
-                                       {
-
                                        v3s16 p2 = p0 + dirs[i];
 
-                                       MapNode n2 = getNode(p2);
+                                       MapNode n2 = getNodeNoEx(p2);
                                        if(content_flowing_liquid(n2.d))
                                        {
                                                m_transforming_liquid.push_back(p2);
                                        }
-
-                                       }catch(InvalidPositionException &e)
-                                       {
-                                       }
                                }
                        }
                }
@@ -1668,9 +1694,6 @@ void Map::transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks)
                };
                for(u16 i=0; i<5; i++)
                {
-                       try
-                       {
-
                        bool to_bottom = (i == 0);
 
                        // If liquid is at lowest possible height, it's not going
@@ -1696,7 +1719,7 @@ void Map::transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks)
 
                        v3s16 p2 = p0 + dirs_to[i];
 
-                       MapNode n2 = getNode(p2);
+                       MapNode n2 = getNodeNoEx(p2);
                        //dstream<<"[1] n2.param="<<(int)n2.param<<std::endl;
 
                        if(content_liquid(n2.d))
@@ -1762,10 +1785,6 @@ void Map::transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks)
                        // If n2_changed to bottom, don't flow anywhere else
                        if(to_bottom && flowed && !is_source)
                                break;
-
-                       }catch(InvalidPositionException &e)
-                       {
-                       }
                }
 
                loopcount++;
@@ -1961,7 +1980,6 @@ ServerMap::~ServerMap()
        {
                if(m_map_saving_enabled)
                {
-                       //save(false);
                        // Save only changed parts
                        save(true);
                        dstream<<DTIME<<"Server: saved map to "<<m_savedir<<std::endl;
@@ -2068,6 +2086,8 @@ MapBlock* ServerMap::finishBlockMake(mapgen::BlockMakeData *data,
                return NULL;
        }
 
+       bool enable_mapgen_debug_info = g_settings.getBool("enable_mapgen_debug_info");
+
        /*dstream<<"Resulting vmanip:"<<std::endl;
        data->vmanip.print(dstream);*/
        
@@ -2080,10 +2100,11 @@ MapBlock* ServerMap::finishBlockMake(mapgen::BlockMakeData *data,
                //TimeTaker timer("finishBlockMake() blitBackAll");
                data->vmanip->blitBackAll(&changed_blocks);
        }
-#if 1
-       dstream<<"finishBlockMake: changed_blocks.size()="
-                       <<changed_blocks.size()<<std::endl;
-#endif
+
+       if(enable_mapgen_debug_info)
+               dstream<<"finishBlockMake: changed_blocks.size()="
+                               <<changed_blocks.size()<<std::endl;
+
        /*
                Copy transforming liquid information
        */
@@ -2117,11 +2138,15 @@ MapBlock* ServerMap::finishBlockMake(mapgen::BlockMakeData *data,
        /*
                NOTE: Lighting and object adding shouldn't really be here, but
                lighting is a bit tricky to move properly to makeBlock.
-               TODO: Do this the right way anyway.
+               TODO: Do this the right way anyway, that is, move it to makeBlock.
+                     - There needs to be some way for makeBlock to report back if
+                           the lighting update is going further down because of the
+                               new block blocking light
        */
 
        /*
                Update lighting
+               NOTE: This takes ~60ms, TODO: Investigate why
        */
        {
                TimeTaker t("finishBlockMake lighting update");
@@ -2140,6 +2165,9 @@ MapBlock* ServerMap::finishBlockMake(mapgen::BlockMakeData *data,
                }
        #endif
                updateLighting(lighting_update_blocks, changed_blocks);
+
+               if(enable_mapgen_debug_info == false)
+                       t.stop(true); // Hide output
        }
 
        /*
@@ -2254,6 +2282,8 @@ MapBlock * ServerMap::generateBlock(
                        <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
                        <<std::endl;*/
        
+       bool enable_mapgen_debug_info = g_settings.getBool("enable_mapgen_debug_info");
+
        TimeTaker timer("generateBlock");
        
        //MapBlock *block = original_dummy;
@@ -2282,6 +2312,9 @@ MapBlock * ServerMap::generateBlock(
        {
                TimeTaker t("mapgen::make_block()");
                mapgen::make_block(&data);
+
+               if(enable_mapgen_debug_info == false)
+                       t.stop(true); // Hide output
        }
 
        /*
@@ -2340,6 +2373,9 @@ MapBlock * ServerMap::generateBlock(
        }
 #endif
 
+       if(enable_mapgen_debug_info == false)
+               timer.stop(true); // Hide output
+
        return block;
 }
 
@@ -2869,10 +2905,10 @@ MapSector* ServerMap::loadSectorMeta(std::string sectordir, bool save_after_load
                // format. Just go ahead and create the sector.
                if(fs::PathExists(sectordir))
                {
-                       dstream<<"ServerMap::loadSectorMeta(): Sector metafile "
+                       /*dstream<<"ServerMap::loadSectorMeta(): Sector metafile "
                                        <<fullpath<<" doesn't exist but directory does."
                                        <<" Continuing with a sector with no metadata."
-                                       <<std::endl;
+                                       <<std::endl;*/
                        sector = new ServerMapSector(this, p2d);
                        m_sectors.insert(p2d, sector);
                }
@@ -3420,6 +3456,9 @@ 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 &&