]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/map.cpp
Merge branch 'master' of https://github.com/minetest/minetest
[dragonfireclient.git] / src / map.cpp
index 1648adec3d4b1c5a7ad83efe49d369ae721f59ba..7bc1334b0fbf9aa2238e7abfe29e2006e5c74628 100644 (file)
@@ -154,13 +154,6 @@ void Map::listAllLoadedBlocks(std::vector<v3s16> &dst)
        }
 }
 
-bool Map::isNodeUnderground(v3s16 p)
-{
-       v3s16 blockpos = getNodeBlockPos(p);
-       MapBlock *block = getBlockNoCreateNoEx(blockpos);
-       return block && block->getIsUnderground();
-}
-
 bool Map::isValidPosition(v3s16 p)
 {
        v3s16 blockpos = getNodeBlockPos(p);
@@ -187,24 +180,32 @@ MapNode Map::getNode(v3s16 p, bool *is_valid_position)
        return node;
 }
 
-// throws InvalidPositionException if not found
-void Map::setNode(v3s16 p, MapNode & n)
+static void set_node_in_block(MapBlock *block, v3s16 relpos, MapNode n)
 {
-       v3s16 blockpos = getNodeBlockPos(p);
-       MapBlock *block = getBlockNoCreate(blockpos);
-       v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
        // Never allow placing CONTENT_IGNORE, it causes problems
        if(n.getContent() == CONTENT_IGNORE){
+               const NodeDefManager *nodedef = block->getParent()->getNodeDefManager();
+               v3s16 blockpos = block->getPos();
+               v3s16 p = blockpos * MAP_BLOCKSIZE + relpos;
                bool temp_bool;
-               errorstream<<"Map::setNode(): Not allowing to place CONTENT_IGNORE"
+               errorstream<<"Not allowing to place CONTENT_IGNORE"
                                <<" while trying to replace \""
-                               <<m_nodedef->get(block->getNodeNoCheck(relpos, &temp_bool)).name
+                               <<nodedef->get(block->getNodeNoCheck(relpos, &temp_bool)).name
                                <<"\" at "<<PP(p)<<" (block "<<PP(blockpos)<<")"<<std::endl;
                return;
        }
        block->setNodeNoCheck(relpos, n);
 }
 
+// throws InvalidPositionException if not found
+void Map::setNode(v3s16 p, MapNode & n)
+{
+       v3s16 blockpos = getNodeBlockPos(p);
+       MapBlock *block = getBlockNoCreate(blockpos);
+       v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
+       set_node_in_block(block, relpos, n);
+}
+
 void Map::addNodeAndUpdate(v3s16 p, MapNode n,
                std::map<v3s16, MapBlock*> &modified_blocks,
                bool remove_metadata)
@@ -212,8 +213,14 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n,
        // Collect old node for rollback
        RollbackNode rollback_oldnode(this, p, m_gamedef);
 
+       v3s16 blockpos = getNodeBlockPos(p);
+       MapBlock *block = getBlockNoCreate(blockpos);
+       if (block->isDummy())
+               throw InvalidPositionException();
+       v3s16 relpos = p - blockpos * MAP_BLOCKSIZE;
+
        // This is needed for updating the lighting
-       MapNode oldnode = getNode(p);
+       MapNode oldnode = block->getNodeUnsafe(relpos);
 
        // Remove node metadata
        if (remove_metadata) {
@@ -221,18 +228,29 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n,
        }
 
        // Set the node on the map
-       // Ignore light (because calling voxalgo::update_lighting_nodes)
-       n.setLight(LIGHTBANK_DAY, 0, m_nodedef);
-       n.setLight(LIGHTBANK_NIGHT, 0, m_nodedef);
-       setNode(p, n);
+       const ContentFeatures &cf = m_nodedef->get(n);
+       const ContentFeatures &oldcf = m_nodedef->get(oldnode);
+       if (cf.lightingEquivalent(oldcf)) {
+               // No light update needed, just copy over the old light.
+               n.setLight(LIGHTBANK_DAY, oldnode.getLightRaw(LIGHTBANK_DAY, oldcf), cf);
+               n.setLight(LIGHTBANK_NIGHT, oldnode.getLightRaw(LIGHTBANK_NIGHT, oldcf), cf);
+               set_node_in_block(block, relpos, n);
+
+               modified_blocks[blockpos] = block;
+       } else {
+               // Ignore light (because calling voxalgo::update_lighting_nodes)
+               n.setLight(LIGHTBANK_DAY, 0, cf);
+               n.setLight(LIGHTBANK_NIGHT, 0, cf);
+               set_node_in_block(block, relpos, n);
 
-       // Update lighting
-       std::vector<std::pair<v3s16, MapNode> > oldnodes;
-       oldnodes.emplace_back(p, oldnode);
-       voxalgo::update_lighting_nodes(this, oldnodes, modified_blocks);
+               // Update lighting
+               std::vector<std::pair<v3s16, MapNode> > oldnodes;
+               oldnodes.emplace_back(p, oldnode);
+               voxalgo::update_lighting_nodes(this, oldnodes, modified_blocks);
 
-       for (auto &modified_block : modified_blocks) {
-               modified_block.second->expireDayNightDiff();
+               for (auto &modified_block : modified_blocks) {
+                       modified_block.second->expireDayNightDiff();
+               }
        }
 
        // Report for rollback
@@ -243,22 +261,6 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n,
                action.setSetNode(p, rollback_oldnode, rollback_newnode);
                m_gamedef->rollback()->reportAction(action);
        }
-
-       /*
-               Add neighboring liquid nodes and this node to transform queue.
-               (it's vital for the node itself to get updated last, if it was removed.)
-        */
-
-       for (const v3s16 &dir : g_7dirs) {
-               v3s16 p2 = p + dir;
-
-               bool is_valid_position;
-               MapNode n2 = getNode(p2, &is_valid_position);
-               if(is_valid_position &&
-                               (m_nodedef->get(n2).isLiquid() ||
-                               n2.getContent() == CONTENT_AIR))
-                       m_transforming_liquid.push_back(p2);
-       }
 }
 
 void Map::removeNodeAndUpdate(v3s16 p,
@@ -339,7 +341,7 @@ struct TimeOrderedMapBlock {
 void Map::timerUpdate(float dtime, float unload_timeout, u32 max_loaded_blocks,
                std::vector<v3s16> *unloaded_blocks)
 {
-       bool save_before_unloading = (mapType() == MAPTYPE_SERVER);
+       bool save_before_unloading = maySaveBlocks();
 
        // Profile modified reasons
        Profiler modprofiler;
@@ -349,6 +351,7 @@ void Map::timerUpdate(float dtime, float unload_timeout, u32 max_loaded_blocks,
        u32 saved_blocks_count = 0;
        u32 block_count_all = 0;
 
+       const auto start_time = porting::getTimeUs();
        beginSave();
 
        // If there is no practical limit, we spare creation of mapblock_queue
@@ -390,6 +393,7 @@ void Map::timerUpdate(float dtime, float unload_timeout, u32 max_loaded_blocks,
                                }
                        }
 
+                       // Delete sector if we emptied it
                        if (all_blocks_deleted) {
                                sector_deletion_queue.push_back(sector_it.first);
                        }
@@ -408,6 +412,7 @@ void Map::timerUpdate(float dtime, float unload_timeout, u32 max_loaded_blocks,
                        }
                }
                block_count_all = mapblock_queue.size();
+
                // Delete old blocks, and blocks over the limit from the memory
                while (!mapblock_queue.empty() && (mapblock_queue.size() > max_loaded_blocks
                                || mapblock_queue.top().block->getUsageTimer() > unload_timeout)) {
@@ -438,6 +443,7 @@ void Map::timerUpdate(float dtime, float unload_timeout, u32 max_loaded_blocks,
                        deleted_blocks_count++;
                        block_count_all--;
                }
+
                // Delete empty sectors
                for (auto &sector_it : m_sectors) {
                        if (sector_it.second->empty()) {
@@ -445,7 +451,11 @@ void Map::timerUpdate(float dtime, float unload_timeout, u32 max_loaded_blocks,
                        }
                }
        }
+
        endSave();
+       const auto end_time = porting::getTimeUs();
+
+       reportMetrics(end_time - start_time, saved_blocks_count, block_count_all);
 
        // Finally delete the empty sectors
        deleteSectors(sector_deletion_queue);
@@ -524,11 +534,11 @@ struct NodeNeighbor {
        { }
 };
 
-void Map::transforming_liquid_add(v3s16 p) {
+void ServerMap::transforming_liquid_add(v3s16 p) {
         m_transforming_liquid.push_back(p);
 }
 
-void Map::transformLiquids(std::map<v3s16, MapBlock*> &modified_blocks,
+void ServerMap::transformLiquids(std::map<v3s16, MapBlock*> &modified_blocks,
                ServerEnvironment *env)
 {
        u32 loopcount = 0;
@@ -1231,7 +1241,12 @@ ServerMap::ServerMap(const std::string &savedir, IGameDef *gamedef,
        m_savedir = savedir;
        m_map_saving_enabled = false;
 
-       m_save_time_counter = mb->addCounter("minetest_core_map_save_time", "Map save time (in nanoseconds)");
+       m_save_time_counter = mb->addCounter(
+               "minetest_map_save_time", "Time spent saving blocks (in microseconds)");
+       m_save_count_counter = mb->addCounter(
+               "minetest_map_saved_blocks", "Number of blocks saved");
+       m_loaded_blocks_gauge = mb->addGauge(
+               "minetest_map_loaded_blocks", "Number of loaded blocks");
 
        m_map_compression_level = rangelim(g_settings->getS16("map_compression_level_disk"), -1, 9);
 
@@ -1466,11 +1481,7 @@ MapSector *ServerMap::createSector(v2s16 p2d)
        /*
                Do not create over max mapgen limit
        */
-       const s16 max_limit_bp = MAX_MAP_GENERATION_LIMIT / MAP_BLOCKSIZE;
-       if (p2d.X < -max_limit_bp ||
-                       p2d.X >  max_limit_bp ||
-                       p2d.Y < -max_limit_bp ||
-                       p2d.Y >  max_limit_bp)
+       if (blockpos_over_max_limit(v3s16(p2d.X, 0, p2d.Y)))
                throw InvalidPositionException("createSector(): pos. over max mapgen limit");
 
        /*
@@ -1479,9 +1490,6 @@ MapSector *ServerMap::createSector(v2s16 p2d)
 
        sector = new MapSector(this, p2d, m_gamedef);
 
-       // Sector position on map in nodes
-       //v2s16 nodepos2d = p2d * MAP_BLOCKSIZE;
-
        /*
                Insert to container
        */
@@ -1569,6 +1577,29 @@ bool ServerMap::isBlockInQueue(v3s16 pos)
        return m_emerge && m_emerge->isBlockInQueue(pos);
 }
 
+void ServerMap::addNodeAndUpdate(v3s16 p, MapNode n,
+               std::map<v3s16, MapBlock*> &modified_blocks,
+               bool remove_metadata)
+{
+       Map::addNodeAndUpdate(p, n, modified_blocks, remove_metadata);
+
+       /*
+               Add neighboring liquid nodes and this node to transform queue.
+               (it's vital for the node itself to get updated last, if it was removed.)
+        */
+
+       for (const v3s16 &dir : g_7dirs) {
+               v3s16 p2 = p + dir;
+
+               bool is_valid_position;
+               MapNode n2 = getNode(p2, &is_valid_position);
+               if(is_valid_position &&
+                               (m_nodedef->get(n2).isLiquid() ||
+                               n2.getContent() == CONTENT_AIR))
+                       m_transforming_liquid.push_back(p2);
+       }
+}
+
 // N.B.  This requires no synchronization, since data will not be modified unless
 // the VoxelManipulator being updated belongs to the same thread.
 void ServerMap::updateVManip(v3s16 pos)
@@ -1591,6 +1622,13 @@ void ServerMap::updateVManip(v3s16 pos)
        vm->m_is_dirty = true;
 }
 
+void ServerMap::reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks)
+{
+       m_loaded_blocks_gauge->set(all_blocks);
+       m_save_time_counter->increment(save_time_us);
+       m_save_count_counter->increment(saved_blocks);
+}
+
 void ServerMap::save(ModifiedState save_level)
 {
        if (!m_map_saving_enabled) {
@@ -1598,7 +1636,7 @@ void ServerMap::save(ModifiedState save_level)
                return;
        }
 
-       u64 start_time = porting::getTimeNs();
+       const auto start_time = porting::getTimeUs();
 
        if(save_level == MOD_STATE_CLEAN)
                infostream<<"ServerMap: Saving whole map, this can take time."
@@ -1659,8 +1697,8 @@ void ServerMap::save(ModifiedState save_level)
                modprofiler.print(infostream);
        }
 
-       auto end_time = porting::getTimeNs();
-       m_save_time_counter->increment(end_time - start_time);
+       const auto end_time = porting::getTimeUs();
+       reportMetrics(end_time - start_time, block_count, block_count_all);
 }
 
 void ServerMap::listAllLoadableBlocks(std::vector<v3s16> &dst)
@@ -1878,6 +1916,7 @@ MMVManip::MMVManip(Map *map):
                VoxelManipulator(),
                m_map(map)
 {
+       assert(map);
 }
 
 void MMVManip::initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
@@ -1885,6 +1924,8 @@ void MMVManip::initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
 {
        TimeTaker timer1("initialEmerge", &emerge_time);
 
+       assert(m_map);
+
        // Units of these are MapBlocks
        v3s16 p_min = blockpos_min;
        v3s16 p_max = blockpos_max;
@@ -1968,6 +2009,7 @@ void MMVManip::blitBackAll(std::map<v3s16, MapBlock*> *modified_blocks,
 {
        if(m_area.getExtent() == v3s16(0,0,0))
                return;
+       assert(m_map);
 
        /*
                Copy data of all blocks
@@ -1988,4 +2030,33 @@ void MMVManip::blitBackAll(std::map<v3s16, MapBlock*> *modified_blocks,
        }
 }
 
+MMVManip *MMVManip::clone() const
+{
+       MMVManip *ret = new MMVManip();
+
+       const s32 size = m_area.getVolume();
+       ret->m_area = m_area;
+       if (m_data) {
+               ret->m_data = new MapNode[size];
+               memcpy(ret->m_data, m_data, size * sizeof(MapNode));
+       }
+       if (m_flags) {
+               ret->m_flags = new u8[size];
+               memcpy(ret->m_flags, m_flags, size * sizeof(u8));
+       }
+
+       ret->m_is_dirty = m_is_dirty;
+       // Even if the copy is disconnected from a map object keep the information
+       // needed to write it back to one
+       ret->m_loaded_blocks = m_loaded_blocks;
+
+       return ret;
+}
+
+void MMVManip::reparent(Map *map)
+{
+       assert(map && !m_map);
+       m_map = map;
+}
+
 //END