]> git.lizzy.rs Git - minetest.git/blobdiff - src/serverenvironment.cpp
Add callback on_mapblocks_changed
[minetest.git] / src / serverenvironment.cpp
index 413a785e624651cfe6190341e57e26dea39372d9..aa6ba2f96770d89c76b4707e27fbab97ad525c84 100644 (file)
@@ -249,7 +249,8 @@ std::string LBMManager::createIntroductionTimesString()
        return oss.str();
 }
 
-void LBMManager::applyLBMs(ServerEnvironment *env, MapBlock *block, u32 stamp)
+void LBMManager::applyLBMs(ServerEnvironment *env, MapBlock *block,
+               const u32 stamp, const float dtime_s)
 {
        // Precondition, we need m_lbm_lookup to be initialized
        FATAL_ERROR_IF(!m_query_mode,
@@ -258,30 +259,29 @@ void LBMManager::applyLBMs(ServerEnvironment *env, MapBlock *block, u32 stamp)
        v3s16 pos;
        MapNode n;
        content_t c;
-       lbm_lookup_map::const_iterator it = getLBMsIntroducedAfter(stamp);
+       auto it = getLBMsIntroducedAfter(stamp);
        for (; it != m_lbm_lookup.end(); ++it) {
                // Cache previous version to speedup lookup which has a very high performance
                // penalty on each call
-               content_t previous_c{};
-               std::vector<LoadingBlockModifierDef *> *lbm_list = nullptr;
+               content_t previous_c = CONTENT_IGNORE;
+               const std::vector<LoadingBlockModifierDef *> *lbm_list = nullptr;
 
                for (pos.X = 0; pos.X < MAP_BLOCKSIZE; pos.X++)
                        for (pos.Y = 0; pos.Y < MAP_BLOCKSIZE; pos.Y++)
                                for (pos.Z = 0; pos.Z < MAP_BLOCKSIZE; pos.Z++) {
-                                       n = block->getNodeNoEx(pos);
+                                       n = block->getNodeNoCheck(pos);
                                        c = n.getContent();
 
                                        // If content_t are not matching perform an LBM lookup
                                        if (previous_c != c) {
-                                               lbm_list = (std::vector<LoadingBlockModifierDef *> *)
-                                                       it->second.lookup(c);
+                                               lbm_list = it->second.lookup(c);
                                                previous_c = c;
                                        }
 
                                        if (!lbm_list)
                                                continue;
                                        for (auto lbmdef : *lbm_list) {
-                                               lbmdef->trigger(env, pos + pos_of_block, n);
+                                               lbmdef->trigger(env, pos + pos_of_block, n, dtime_s);
                                        }
                                }
        }
@@ -371,16 +371,25 @@ void ActiveBlockList::update(std::vector<PlayerSAO*> &active_players,
        // Go through new list
        for (v3s16 p : newlist) {
                // If not on old list, it's been added
-               if(m_list.find(p) == m_list.end())
+               if (m_list.find(p) == m_list.end())
                        blocks_added.insert(p);
        }
 
        /*
                Update m_list
        */
-       m_list.clear();
-       for (v3s16 p : newlist) {
-               m_list.insert(p);
+       m_list = std::move(newlist);
+}
+
+/*
+       OnMapblocksChangedReceiver
+*/
+
+void OnMapblocksChangedReceiver::onMapEditEvent(const MapEditEvent &event)
+{
+       assert(receiving);
+       for (const v3s16 &p : event.modified_blocks) {
+               modified_blocks.insert(p);
        }
 }
 
@@ -392,17 +401,29 @@ void ActiveBlockList::update(std::vector<PlayerSAO*> &active_players,
 static std::random_device seed;
 
 ServerEnvironment::ServerEnvironment(ServerMap *map,
-       ServerScripting *scriptIface, Server *server,
-       const std::string &path_world):
+       ServerScripting *script_iface, Server *server,
+       const std::string &path_world, MetricsBackend *mb):
        Environment(server),
        m_map(map),
-       m_script(scriptIface),
+       m_script(script_iface),
        m_server(server),
        m_path_world(path_world),
        m_rgen(seed())
+{
+       m_step_time_counter = mb->addCounter(
+               "minetest_env_step_time", "Time spent in environment step (in microseconds)");
+
+       m_active_block_gauge = mb->addGauge(
+               "minetest_env_active_blocks", "Number of active blocks");
+
+       m_active_object_gauge = mb->addGauge(
+               "minetest_env_active_objects", "Number of active objects");
+}
+
+void ServerEnvironment::init()
 {
        // Determine which database backend to use
-       std::string conf_path = path_world + DIR_DELIM + "world.mt";
+       std::string conf_path = m_path_world + DIR_DELIM + "world.mt";
        Settings conf;
 
        std::string player_backend_name = "sqlite3";
@@ -412,6 +433,13 @@ ServerEnvironment::ServerEnvironment(ServerMap *map,
 
        // If we open world.mt read the backend configurations.
        if (succeeded) {
+               // Check that the world's blocksize matches the compiled MAP_BLOCKSIZE
+               u16 blocksize = 16;
+               conf.getU16NoEx("blocksize", blocksize);
+               if (blocksize != MAP_BLOCKSIZE) {
+                       throw BaseException(std::string("The map's blocksize is not supported."));
+               }
+
                // Read those values before setting defaults
                bool player_backend_exists = conf.exists("player_backend");
                bool auth_backend_exists = conf.exists("auth_backend");
@@ -458,8 +486,13 @@ ServerEnvironment::ServerEnvironment(ServerMap *map,
                                << "please read http://wiki.minetest.net/Database_backends." << std::endl;
        }
 
-       m_player_database = openPlayerDatabase(player_backend_name, path_world, conf);
-       m_auth_database = openAuthDatabase(auth_backend_name, path_world, conf);
+       m_player_database = openPlayerDatabase(player_backend_name, m_path_world, conf);
+       m_auth_database = openAuthDatabase(auth_backend_name, m_path_world, conf);
+
+       if (m_map && m_script->has_on_mapblocks_changed()) {
+               m_map->addEventReceiver(&m_on_mapblocks_changed_receiver);
+               m_on_mapblocks_changed_receiver.receiving = true;
+       }
 }
 
 ServerEnvironment::~ServerEnvironment()
@@ -472,7 +505,8 @@ ServerEnvironment::~ServerEnvironment()
        deactivateFarObjects(true);
 
        // Drop/delete map
-       m_map->drop();
+       if (m_map)
+               m_map->drop();
 
        // Delete ActiveBlockModifiers
        for (ABMWithState &m_abm : m_abms) {
@@ -552,10 +586,8 @@ bool ServerEnvironment::removePlayerFromDatabase(const std::string &name)
 void ServerEnvironment::kickAllPlayers(AccessDeniedCode reason,
        const std::string &str_reason, bool reconnect)
 {
-       for (RemotePlayer *player : m_players) {
-               m_server->DenyAccessVerCompliant(player->getPeerId(),
-                       player->protocol_version, reason, str_reason, reconnect);
-       }
+       for (RemotePlayer *player : m_players)
+               m_server->DenyAccess(player->getPeerId(), reason, str_reason, reconnect);
 }
 
 void ServerEnvironment::saveLoadedPlayers(bool force)
@@ -619,6 +651,9 @@ PlayerSAO *ServerEnvironment::loadPlayer(RemotePlayer *player, bool *new_player,
        /* Add object to environment */
        addActiveObject(playersao);
 
+       // Update active blocks quickly for a bit so objects in those blocks appear on the client
+       m_fast_active_block_divider = 10;
+
        return playersao;
 }
 
@@ -729,6 +764,8 @@ struct ActiveABM
        int chance;
        std::vector<content_t> required_neighbors;
        bool check_required_neighbors; // false if required_neighbors is known to be empty
+       s16 min_y;
+       s16 max_y;
 };
 
 class ABMHandler
@@ -759,20 +796,23 @@ class ABMHandler
                                actual_interval = trigger_interval;
                        }
                        float chance = abm->getTriggerChance();
-                       if(chance == 0)
+                       if (chance == 0)
                                chance = 1;
                        ActiveABM aabm;
                        aabm.abm = abm;
                        if (abm->getSimpleCatchUp()) {
                                float intervals = actual_interval / trigger_interval;
-                               if(intervals == 0)
+                               if (intervals == 0)
                                        continue;
                                aabm.chance = chance / intervals;
-                               if(aabm.chance == 0)
+                               if (aabm.chance == 0)
                                        aabm.chance = 1;
                        } else {
                                aabm.chance = chance;
                        }
+                       // y limits
+                       aabm.min_y = abm->getMinY();
+                       aabm.max_y = abm->getMaxY();
 
                        // Trigger neighbors
                        const std::vector<std::string> &required_neighbors_s =
@@ -804,10 +844,10 @@ class ABMHandler
                        delete aabms;
        }
 
-       // Find out how many objects the given block and its neighbours contain.
+       // Find out how many objects the given block and its neighbors contain.
        // Returns the number of objects in the block, and also in 'wider' the
-       // number of objects in the block and all its neighbours. The latter
-       // may an estimate if any neighbours are unloaded.
+       // number of objects in the block and all its neighbors. The latter
+       // may an estimate if any neighbors are unloaded.
        u32 countObjects(MapBlock *block, ServerMap * map, u32 &wider)
        {
                wider = 0;
@@ -822,19 +862,17 @@ class ABMHandler
                                                wider_unknown_count++;
                                                continue;
                                        }
-                                       wider += block2->m_static_objects.m_active.size()
-                                               + block2->m_static_objects.m_stored.size();
+                                       wider += block2->m_static_objects.size();
                                }
                // Extrapolate
-               u32 active_object_count = block->m_static_objects.m_active.size();
-               u32 wider_known_count = 3*3*3 - wider_unknown_count;
+               u32 active_object_count = block->m_static_objects.getActiveSize();
+               u32 wider_known_count = 3 * 3 * 3 - wider_unknown_count;
                wider += wider_unknown_count * wider / wider_known_count;
                return active_object_count;
-
        }
        void apply(MapBlock *block, int &blocks_scanned, int &abms_run, int &blocks_cached)
        {
-               if(m_aabms.empty() || block->isDummy())
+               if (m_aabms.empty())
                        return;
 
                // Check the content type cache first
@@ -868,7 +906,7 @@ class ABMHandler
                for(p0.Y=0; p0.Y<MAP_BLOCKSIZE; p0.Y++)
                for(p0.Z=0; p0.Z<MAP_BLOCKSIZE; p0.Z++)
                {
-                       const MapNode &n = block->getNodeUnsafe(p0);
+                       MapNode n = block->getNodeNoCheck(p0);
                        content_t c = n.getContent();
                        // Cache content types as we go
                        if (!block->contents_cached && !block->do_not_cache_contents) {
@@ -885,6 +923,9 @@ class ABMHandler
 
                        v3s16 p = p0 + block->getPosRelative();
                        for (ActiveABM &aabm : *m_aabms[c]) {
+                               if ((p.Y < aabm.min_y) || (p.Y > aabm.max_y))
+                                       continue;
+
                                if (myrand() % aabm.chance != 0)
                                        continue;
 
@@ -901,7 +942,7 @@ class ABMHandler
                                                if (block->isValidPosition(p1)) {
                                                        // if the neighbor is found on the same map block
                                                        // get it straight from there
-                                                       const MapNode &n = block->getNodeUnsafe(p1);
+                                                       const MapNode &n = block->getNodeNoCheck(p1);
                                                        c = n.getContent();
                                                } else {
                                                        // otherwise consult the map
@@ -927,6 +968,11 @@ class ABMHandler
                                        active_object_count = countObjects(block, map, active_object_count_wider);
                                        m_env->m_added_objects = 0;
                                }
+
+                               // Update and check node after possible modification
+                               n = block->getNodeNoCheck(p0);
+                               if (n.getContent() != c)
+                                       break;
                        }
                }
                block->contents_cached = !block->do_not_cache_contents;
@@ -955,7 +1001,7 @@ void ServerEnvironment::activateBlock(MapBlock *block, u32 additional_dtime)
 
        // Remove stored static objects if clearObjects was called since block's timestamp
        if (stamp == BLOCK_TIMESTAMP_UNDEFINED || stamp < m_last_clear_objects_time) {
-               block->m_static_objects.m_stored.clear();
+               block->m_static_objects.clearStored();
                // do not set changed flag to avoid unnecessary mapblock writes
        }
 
@@ -969,21 +1015,12 @@ void ServerEnvironment::activateBlock(MapBlock *block, u32 additional_dtime)
        activateObjects(block, dtime_s);
 
        /* Handle LoadingBlockModifiers */
-       m_lbm_mgr.applyLBMs(this, block, stamp);
+       m_lbm_mgr.applyLBMs(this, block, stamp, (float)dtime_s);
 
        // Run node timers
-       std::vector<NodeTimer> elapsed_timers =
-               block->m_node_timers.step((float)dtime_s);
-       if (!elapsed_timers.empty()) {
-               MapNode n;
-               for (const NodeTimer &elapsed_timer : elapsed_timers) {
-                       n = block->getNodeNoEx(elapsed_timer.position);
-                       v3s16 p = elapsed_timer.position + block->getPosRelative();
-                       if (m_script->node_on_timer(p, n, elapsed_timer.elapsed))
-                               block->setNodeTimer(NodeTimer(elapsed_timer.timeout, 0,
-                                       elapsed_timer.position));
-               }
-       }
+       block->step((float)dtime_s, [&](v3s16 p, MapNode n, f32 d) -> bool {
+               return m_script->node_on_timer(p, n, d);
+       });
 }
 
 void ServerEnvironment::addActiveBlockModifier(ActiveBlockModifier *abm)
@@ -1067,7 +1104,7 @@ bool ServerEnvironment::swapNode(v3s16 p, const MapNode &n)
 
 u8 ServerEnvironment::findSunlight(v3s16 pos) const
 {
-       // Directions for neighbouring nodes with specified order
+       // Directions for neighboring nodes with specified order
        static const v3s16 dirs[] = {
                v3s16(-1, 0, 0), v3s16(1, 0, 0), v3s16(0, 0, -1), v3s16(0, 0, 1),
                v3s16(0, -1, 0), v3s16(0, 1, 0)
@@ -1113,7 +1150,7 @@ u8 ServerEnvironment::findSunlight(v3s16 pos) const
                                m_map->emergeBlock(neighborPos, false);
                                node = m_map->getNode(neighborPos, &is_position_ok);
                                if (!is_position_ok)
-                                       continue;  // not generated
+                                       continue; // not generated
                        }
 
                        const ContentFeatures &def = ndef->get(node);
@@ -1142,7 +1179,7 @@ u8 ServerEnvironment::findSunlight(v3s16 pos) const
                                // Found a valid daylight
                                found_light = possible_finlight;
                        } else {
-                               // Sunlight may be darker, so walk the neighbours
+                               // Sunlight may be darker, so walk the neighbors
                                stack.push({neighborPos, dist});
                        }
                }
@@ -1233,14 +1270,10 @@ void ServerEnvironment::clearObjects(ClearObjectsMode mode)
                                << "Failed to emerge block " << PP(p) << std::endl;
                        continue;
                }
-               u32 num_stored = block->m_static_objects.m_stored.size();
-               u32 num_active = block->m_static_objects.m_active.size();
-               if (num_stored != 0 || num_active != 0) {
-                       block->m_static_objects.m_stored.clear();
-                       block->m_static_objects.m_active.clear();
-                       block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                               MOD_REASON_CLEAR_ALL_OBJECTS);
-                       num_objs_cleared += num_stored + num_active;
+
+               u32 num_cleared = block->clearObjects();
+               if (num_cleared > 0) {
+                       num_objs_cleared += num_cleared;
                        num_blocks_cleared++;
                }
                num_blocks_checked++;
@@ -1277,6 +1310,8 @@ void ServerEnvironment::clearObjects(ClearObjectsMode mode)
 void ServerEnvironment::step(float dtime)
 {
        ScopeProfiler sp2(g_profiler, "ServerEnv::step()", SPT_AVG);
+       const auto start_time = porting::getTimeUs();
+
        /* Step time of day */
        stepTimeOfDay(dtime);
 
@@ -1315,13 +1350,15 @@ void ServerEnvironment::step(float dtime)
        /*
                Manage active block list
        */
-       if (m_active_blocks_management_interval.step(dtime, m_cache_active_block_mgmt_interval)) {
+       if (m_active_blocks_mgmt_interval.step(dtime, m_cache_active_block_mgmt_interval / m_fast_active_block_divider)) {
                ScopeProfiler sp(g_profiler, "ServerEnv: update active blocks", SPT_AVG);
+
                /*
                        Get player block positions
                */
                std::vector<PlayerSAO*> players;
-               for (RemotePlayer *player: m_players) {
+               players.reserve(m_players.size());
+               for (RemotePlayer *player : m_players) {
                        // Ignore disconnected players
                        if (player->getPeerId() == PEER_ID_INEXISTENT)
                                continue;
@@ -1369,13 +1406,22 @@ void ServerEnvironment::step(float dtime)
                for (const v3s16 &p: blocks_added) {
                        MapBlock *block = m_map->getBlockOrEmerge(p);
                        if (!block) {
-                               m_active_blocks.m_list.erase(p);
-                               m_active_blocks.m_abm_list.erase(p);
+                               // TODO: The blocks removed here will only be picked up again
+                               // on the next cycle. To minimize the latency of objects being
+                               // activated we could remember the blocks pending activating
+                               // and activate them instantly as soon as they're loaded.
+                               m_active_blocks.remove(p);
                                continue;
                        }
 
                        activateBlock(block);
                }
+
+               // Some blocks may be removed again by the code above so do this here
+               m_active_block_gauge->set(m_active_blocks.size());
+
+               if (m_fast_active_block_divider > 1)
+                       --m_fast_active_block_divider;
        }
 
        /*
@@ -1403,19 +1449,9 @@ void ServerEnvironment::step(float dtime)
                                        MOD_REASON_BLOCK_EXPIRED);
 
                        // Run node timers
-                       std::vector<NodeTimer> elapsed_timers = block->m_node_timers.step(dtime);
-                       if (!elapsed_timers.empty()) {
-                               MapNode n;
-                               v3s16 p2;
-                               for (const NodeTimer &elapsed_timer: elapsed_timers) {
-                                       n = block->getNodeNoEx(elapsed_timer.position);
-                                       p2 = elapsed_timer.position + block->getPosRelative();
-                                       if (m_script->node_on_timer(p2, n, elapsed_timer.elapsed)) {
-                                               block->setNodeTimer(NodeTimer(
-                                                       elapsed_timer.timeout, 0, elapsed_timer.position));
-                                       }
-                               }
-                       }
+                       block->step(dtime, [&](v3s16 p, MapNode n, f32 d) -> bool {
+                               return m_script->node_on_timer(p, n, d);
+                       });
                }
        }
 
@@ -1423,6 +1459,9 @@ void ServerEnvironment::step(float dtime)
                ScopeProfiler sp(g_profiler, "SEnv: modify in blocks avg per interval", SPT_AVG);
                TimeTaker timer("modify in active blocks per interval");
 
+               // Shuffle to prevent persistent artifacts of ordering
+               std::shuffle(m_abms.begin(), m_abms.end(), m_rgen);
+
                // Initialize handling of ActiveBlockModifiers
                ABMHandler abmhandler(m_abms, m_cache_abm_interval, this, true);
 
@@ -1475,6 +1514,8 @@ void ServerEnvironment::step(float dtime)
        */
        m_script->environment_Step(dtime);
 
+       m_script->stepAsync();
+
        /*
                Step active objects
        */
@@ -1489,9 +1530,12 @@ void ServerEnvironment::step(float dtime)
                        send_recommended = true;
                }
 
-               auto cb_state = [this, dtime, send_recommended] (ServerActiveObject *obj) {
+               u32 object_count = 0;
+
+               auto cb_state = [&](ServerActiveObject *obj) {
                        if (obj->isGone())
                                return;
+                       object_count++;
 
                        // Step object
                        obj->step(dtime, send_recommended);
@@ -1499,6 +1543,8 @@ void ServerEnvironment::step(float dtime)
                        obj->dumpAOMessagesToQueue(m_active_object_messages);
                };
                m_ao_manager.step(dtime, cb_state);
+
+               m_active_object_gauge->set(object_count);
        }
 
        /*
@@ -1540,6 +1586,17 @@ void ServerEnvironment::step(float dtime)
 
        // Send outdated detached inventories
        m_server->sendDetachedInventories(PEER_ID_INEXISTENT, true);
+
+       // Notify mods of modified mapblocks
+       if (m_on_mapblocks_changed_receiver.receiving &&
+                       !m_on_mapblocks_changed_receiver.modified_blocks.empty()) {
+               std::unordered_set<v3s16> modified_blocks;
+               std::swap(modified_blocks, m_on_mapblocks_changed_receiver.modified_blocks);
+               m_script->on_mapblocks_changed(modified_blocks);
+       }
+
+       const auto end_time = porting::getTimeUs();
+       m_step_time_counter->increment(end_time - start_time);
 }
 
 ServerEnvironment::BlockStatus ServerEnvironment::getBlockStatus(v3s16 blockpos)
@@ -1548,7 +1605,7 @@ ServerEnvironment::BlockStatus ServerEnvironment::getBlockStatus(v3s16 blockpos)
                return BS_ACTIVE;
 
        const MapBlock *block = m_map->getBlockNoCreateNoEx(blockpos);
-       if (block && !block->isDummy())
+       if (block)
                return BS_LOADED;
 
        if (m_map->isBlockInQueue(blockpos))
@@ -1681,7 +1738,7 @@ void ServerEnvironment::setStaticForActiveObjectsInBlock(
        if (!block)
                return;
 
-       for (auto &so_it : block->m_static_objects.m_active) {
+       for (auto &so_it : block->m_static_objects.getAllActives()) {
                // Get the ServerActiveObject counterpart to this StaticObject
                ServerActiveObject *sao = m_ao_manager.getActiveObject(so_it.first);
                if (!sao) {
@@ -1699,7 +1756,7 @@ void ServerEnvironment::setStaticForActiveObjectsInBlock(
 
 bool ServerEnvironment::getActiveObjectMessage(ActiveObjectMessage *dest)
 {
-       if(m_active_object_messages.empty())
+       if (m_active_object_messages.empty())
                return false;
 
        *dest = std::move(m_active_object_messages.front());
@@ -1724,16 +1781,27 @@ void ServerEnvironment::getSelectedActiveObjects(
                        continue;
 
                v3f pos = obj->getBasePosition();
-
-               aabb3f offsetted_box(selection_box.MinEdge + pos,
-                       selection_box.MaxEdge + pos);
+               v3f rel_pos = shootline_on_map.start - pos;
 
                v3f current_intersection;
-               v3s16 current_normal;
-               if (boxLineCollision(offsetted_box, shootline_on_map.start, line_vector,
-                               &current_intersection, &current_normal)) {
+               v3f current_normal;
+               v3f current_raw_normal;
+
+               ObjectProperties *props = obj->accessObjectProperties();
+               bool collision;
+               UnitSAO* usao = dynamic_cast<UnitSAO*>(obj);
+               if (props->rotate_selectionbox && usao != nullptr) {
+                       collision = boxLineCollision(selection_box, usao->getTotalRotation(),
+                               rel_pos, line_vector, &current_intersection, &current_normal, &current_raw_normal);
+               } else {
+                       collision = boxLineCollision(selection_box, rel_pos, line_vector,
+                               &current_intersection, &current_normal);
+                       current_raw_normal = current_normal;
+               }
+               if (collision) {
+                       current_intersection += pos;
                        objects.emplace_back(
-                               (s16) obj->getId(), current_intersection, current_normal,
+                               (s16) obj->getId(), current_intersection, current_normal, current_raw_normal,
                                (current_intersection - shootline_on_map.start).getLengthSQ());
                }
        }
@@ -1763,8 +1831,8 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
                // Add to the block where the object is located in
                v3s16 blockpos = getNodeBlockPos(floatToInt(objectpos, BS));
                MapBlock *block = m_map->emergeBlock(blockpos);
-               if(block){
-                       block->m_static_objects.m_active[object->getId()] = s_obj;
+               if (block) {
+                       block->m_static_objects.setActive(object->getId(), s_obj);
                        object->m_static_exists = true;
                        object->m_static_block = blockpos;
 
@@ -1789,7 +1857,7 @@ void ServerEnvironment::removeRemovedObjects()
 {
        ScopeProfiler sp(g_profiler, "ServerEnvironment::removeRemovedObjects()", SPT_AVG);
 
-       auto clear_cb = [this] (ServerActiveObject *obj, u16 id) {
+       auto clear_cb = [this](ServerActiveObject *obj, u16 id) {
                // This shouldn't happen but check it
                if (!obj) {
                        errorstream << "ServerEnvironment::removeRemovedObjects(): "
@@ -1811,22 +1879,15 @@ void ServerEnvironment::removeRemovedObjects()
 
                // If still known by clients, don't actually remove. On some future
                // invocation this will be 0, which is when removal will continue.
-               if(obj->m_known_by_count > 0)
+               if (obj->m_known_by_count > 0)
                        return false;
 
                /*
                        Move static data from active to stored if deactivated
                */
                if (!obj->isPendingRemoval() && obj->m_static_exists) {
-                       MapBlock *block = m_map->emergeBlock(obj->m_static_block, false);
-                       if (block) {
-                               const auto i = block->m_static_objects.m_active.find(id);
-                               if (i != block->m_static_objects.m_active.end()) {
-                                       block->m_static_objects.m_stored.push_back(i->second);
-                                       block->m_static_objects.m_active.erase(id);
-                                       block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                                               MOD_REASON_REMOVE_OBJECTS_DEACTIVATE);
-                               } else {
+                       if (MapBlock *block = m_map->emergeBlock(obj->m_static_block, false)) {
+                               if (!block->storeActiveObject(id)) {
                                        warningstream << "ServerEnvironment::removeRemovedObjects(): "
                                                        << "id=" << id << " m_static_exists=true but "
                                                        << "static data doesn't actually exist in "
@@ -1856,33 +1917,33 @@ void ServerEnvironment::removeRemovedObjects()
 static void print_hexdump(std::ostream &o, const std::string &data)
 {
        const int linelength = 16;
-       for(int l=0; ; l++){
+       for (int l = 0;; l++) {
                int i0 = linelength * l;
                bool at_end = false;
                int thislinelength = linelength;
-               if(i0 + thislinelength > (int)data.size()){
+               if (i0 + thislinelength > (int)data.size()) {
                        thislinelength = data.size() - i0;
                        at_end = true;
                }
-               for(int di=0; di<linelength; di++){
+               for (int di = 0; di < linelength; di++) {
                        int i = i0 + di;
                        char buf[4];
-                       if(di<thislinelength)
+                       if (di < thislinelength)
                                porting::mt_snprintf(buf, sizeof(buf), "%.2x ", data[i]);
                        else
                                porting::mt_snprintf(buf, sizeof(buf), "   ");
-                       o<<buf;
+                       o << buf;
                }
-               o<<" ";
-               for(int di=0; di<thislinelength; di++){
+               o << " ";
+               for (int di = 0; di < thislinelength; di++) {
                        int i = i0 + di;
-                       if(data[i] >= 32)
-                               o<<data[i];
+                       if (data[i] >= 32)
+                               o << data[i];
                        else
-                               o<<".";
+                               o << ".";
                }
-               o<<std::endl;
-               if(at_end)
+               o << std::endl;
+               if (at_end)
                        break;
        }
 }
@@ -1904,59 +1965,41 @@ ServerActiveObject* ServerEnvironment::createSAO(ActiveObjectType type, v3f pos,
 */
 void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s)
 {
-       if(block == NULL)
+       if (block == NULL)
                return;
 
-       // Ignore if no stored objects (to not set changed flag)
-       if(block->m_static_objects.m_stored.empty())
+       if (!block->onObjectsActivation())
                return;
 
-       verbosestream<<"ServerEnvironment::activateObjects(): "
-               <<"activating objects of block "<<PP(block->getPos())
-               <<" ("<<block->m_static_objects.m_stored.size()
-               <<" objects)"<<std::endl;
-       bool large_amount = (block->m_static_objects.m_stored.size() > g_settings->getU16("max_objects_per_block"));
-       if (large_amount) {
-               errorstream<<"suspiciously large amount of objects detected: "
-                       <<block->m_static_objects.m_stored.size()<<" in "
-                       <<PP(block->getPos())
-                       <<"; removing all of them."<<std::endl;
-               // Clear stored list
-               block->m_static_objects.m_stored.clear();
-               block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                       MOD_REASON_TOO_MANY_OBJECTS);
-               return;
-       }
-
        // Activate stored objects
        std::vector<StaticObject> new_stored;
-       for (const StaticObject &s_obj : block->m_static_objects.m_stored) {
+       for (const StaticObject &s_obj : block->m_static_objects.getAllStored()) {
                // Create an active object from the data
-               ServerActiveObject *obj = createSAO((ActiveObjectType) s_obj.type, s_obj.pos,
-                       s_obj.data);
+               ServerActiveObject *obj =
+                               createSAO((ActiveObjectType)s_obj.type, s_obj.pos, s_obj.data);
                // If couldn't create object, store static data back.
                if (!obj) {
-                       errorstream<<"ServerEnvironment::activateObjects(): "
-                               <<"failed to create active object from static object "
-                               <<"in block "<<PP(s_obj.pos/BS)
-                               <<" type="<<(int)s_obj.type<<" data:"<<std::endl;
+                       errorstream << "ServerEnvironment::activateObjects(): "
+                               << "failed to create active object from static object "
+                               << "in block " << PP(s_obj.pos / BS)
+                               << " type=" << (int)s_obj.type << " data:" << std::endl;
                        print_hexdump(verbosestream, s_obj.data);
 
                        new_stored.push_back(s_obj);
                        continue;
                }
-               verbosestream<<"ServerEnvironment::activateObjects(): "
-                       <<"activated static object pos="<<PP(s_obj.pos/BS)
-                       <<" type="<<(int)s_obj.type<<std::endl;
+               verbosestream << "ServerEnvironment::activateObjects(): "
+                       << "activated static object pos=" << PP(s_obj.pos / BS)
+                       << " type=" << (int)s_obj.type << std::endl;
                // This will also add the object to the active static list
                addActiveObjectRaw(obj, false, dtime_s);
        }
 
        // Clear stored list
-       block->m_static_objects.m_stored.clear();
+       block->m_static_objects.clearStored();
        // Add leftover failed stuff to stored list
        for (const StaticObject &s_obj : new_stored) {
-               block->m_static_objects.m_stored.push_back(s_obj);
+               block->m_static_objects.pushStored(s_obj);
        }
 
        /*
@@ -1982,8 +2025,8 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s)
 */
 void ServerEnvironment::deactivateFarObjects(bool _force_delete)
 {
-       auto cb_deactivate = [this, _force_delete] (ServerActiveObject *obj, u16 id) {
-               // force_delete might be overriden per object
+       auto cb_deactivate = [this, _force_delete](ServerActiveObject *obj, u16 id) {
+               // force_delete might be overridden per object
                bool force_delete = _force_delete;
 
                // Do not deactivate if disallowed
@@ -2045,11 +2088,9 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete)
                                if (obj->m_static_block == blockpos_o)
                                        stays_in_same_block = true;
 
-                               MapBlock *block = m_map->emergeBlock(obj->m_static_block, false);
-
-                               if (block) {
-                                       const auto n = block->m_static_objects.m_active.find(id);
-                                       if (n != block->m_static_objects.m_active.end()) {
+                               if (MapBlock *block = m_map->emergeBlock(obj->m_static_block, false)) {
+                                       const auto n = block->m_static_objects.getAllActives().find(id);
+                                       if (n != block->m_static_objects.getAllActives().end()) {
                                                StaticObject static_old = n->second;
 
                                                float save_movem = obj->getMinimumSavedMovement();
@@ -2162,18 +2203,9 @@ bool ServerEnvironment::saveStaticToBlock(
                                << " when saving static data of object to it. id=" << store_id << std::endl;
                return false;
        }
-       if (block->m_static_objects.m_stored.size() >= g_settings->getU16("max_objects_per_block")) {
-               warningstream << "ServerEnv: Trying to store id = " << store_id
-                               << " statically but block " << PP(blockpos)
-                               << " already contains "
-                               << block->m_static_objects.m_stored.size()
-                               << " objects." << std::endl;
-               return false;
-       }
 
-       block->m_static_objects.insert(store_id, s_obj);
-       if (mod_reason != MOD_REASON_UNKNOWN) // Do not mark as modified if requested
-               block->raiseModified(MOD_STATE_WRITE_NEEDED, mod_reason);
+       if (!block->saveStaticObject(store_id, s_obj, mod_reason))
+               return false;
 
        obj->m_static_exists = true;
        obj->m_static_block = blockpos;