]> git.lizzy.rs Git - minetest.git/blobdiff - src/serverenvironment.cpp
Add callback on_mapblocks_changed
[minetest.git] / src / serverenvironment.cpp
index efb93b9a26d56b110cfe09e6b9d483a5a7bfd6df..aa6ba2f96770d89c76b4707e27fbab97ad525c84 100644 (file)
@@ -17,8 +17,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
+#include <algorithm>
 #include "serverenvironment.h"
-#include "content_sao.h"
 #include "settings.h"
 #include "log.h"
 #include "mapblock.h"
@@ -44,7 +44,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #if USE_POSTGRESQL
 #include "database/database-postgresql.h"
 #endif
-#include <algorithm>
+#if USE_LEVELDB
+#include "database/database-leveldb.h"
+#endif
+#include "server/luaentity_sao.h"
+#include "server/player_sao.h"
 
 #define LBM_NAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyz0123456789_:"
 
@@ -245,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,
@@ -254,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);
                                        }
                                }
        }
@@ -367,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);
        }
 }
 
@@ -384,48 +397,102 @@ void ActiveBlockList::update(std::vector<PlayerSAO*> &active_players,
        ServerEnvironment
 */
 
+// Random device to seed pseudo random generators.
+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_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";
+       std::string auth_backend_name = "sqlite3";
+
        bool succeeded = conf.readConfigFile(conf_path.c_str());
-       if (!succeeded || !conf.exists("player_backend")) {
-               // fall back to files
-               conf.set("player_backend", "files");
+
+       // 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");
+
+               // player backend is not set, assume it's legacy file backend.
+               if (!player_backend_exists) {
+                       // fall back to files
+                       conf.set("player_backend", "files");
+                       player_backend_name = "files";
+
+                       if (!conf.updateConfigFile(conf_path.c_str())) {
+                               errorstream << "ServerEnvironment::ServerEnvironment(): "
+                                               << "Failed to update world.mt!" << std::endl;
+                       }
+               } else {
+                       conf.getNoEx("player_backend", player_backend_name);
+               }
+
+               // auth backend is not set, assume it's legacy file backend.
+               if (!auth_backend_exists) {
+                       conf.set("auth_backend", "files");
+                       auth_backend_name = "files";
+
+                       if (!conf.updateConfigFile(conf_path.c_str())) {
+                               errorstream << "ServerEnvironment::ServerEnvironment(): "
+                                               << "Failed to update world.mt!" << std::endl;
+                       }
+               } else {
+                       conf.getNoEx("auth_backend", auth_backend_name);
+               }
+       }
+
+       if (player_backend_name == "files") {
                warningstream << "/!\\ You are using old player file backend. "
-                               << "This backend is deprecated and will be removed in next release /!\\"
+                               << "This backend is deprecated and will be removed in a future release /!\\"
                                << std::endl << "Switching to SQLite3 or PostgreSQL is advised, "
                                << "please read http://wiki.minetest.net/Database_backends." << std::endl;
+       }
 
-               if (!conf.updateConfigFile(conf_path.c_str())) {
-                       errorstream << "ServerEnvironment::ServerEnvironment(): "
-                               << "Failed to update world.mt!" << std::endl;
-               }
+       if (auth_backend_name == "files") {
+               warningstream << "/!\\ You are using old auth file backend. "
+                               << "This backend is deprecated and will be removed in a future release /!\\"
+                               << std::endl << "Switching to SQLite3 is advised, "
+                               << "please read http://wiki.minetest.net/Database_backends." << std::endl;
        }
 
-       std::string name;
-       conf.getNoEx("player_backend", name);
-       m_player_database = openPlayerDatabase(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);
 
-       std::string auth_name = "files";
-       if (conf.exists("auth_backend")) {
-               conf.getNoEx("auth_backend", auth_name);
-       } else {
-               conf.set("auth_backend", "files");
-               if (!conf.updateConfigFile(conf_path.c_str())) {
-                       errorstream << "ServerEnvironment::ServerEnvironment(): "
-                                       << "Failed to update world.mt!" << std::endl;
-               }
+       if (m_map && m_script->has_on_mapblocks_changed()) {
+               m_map->addEventReceiver(&m_on_mapblocks_changed_receiver);
+               m_on_mapblocks_changed_receiver.receiving = true;
        }
-       m_auth_database = openAuthDatabase(auth_name, path_world, conf);
 }
 
 ServerEnvironment::~ServerEnvironment()
@@ -438,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) {
@@ -515,40 +583,17 @@ bool ServerEnvironment::removePlayerFromDatabase(const std::string &name)
        return m_player_database->removePlayer(name);
 }
 
-bool ServerEnvironment::line_of_sight(v3f pos1, v3f pos2, v3s16 *p)
-{
-       // Iterate trough nodes on the line
-       voxalgo::VoxelLineIterator iterator(pos1 / BS, (pos2 - pos1) / BS);
-       do {
-               MapNode n = getMap().getNodeNoEx(iterator.m_current_node_pos);
-
-               // Return non-air
-               if (n.param0 != CONTENT_AIR) {
-                       if (p)
-                               *p = iterator.m_current_node_pos;
-                       return false;
-               }
-               iterator.next();
-       } while (iterator.m_current_index <= iterator.m_last_index);
-       return true;
-}
-
 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()
+void ServerEnvironment::saveLoadedPlayers(bool force)
 {
-       std::string players_path = m_path_world + DIR_DELIM + "players";
-       fs::CreateDir(players_path);
-
        for (RemotePlayer *player : m_players) {
-               if (player->checkModified() || (player->getPlayerSAO() &&
+               if (force || player->checkModified() || (player->getPlayerSAO() &&
                                player->getPlayerSAO()->getMeta().isModified())) {
                        try {
                                m_player_database->savePlayer(player);
@@ -606,17 +651,23 @@ 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;
 }
 
 void ServerEnvironment::saveMeta()
 {
+       if (!m_meta_loaded)
+               return;
+
        std::string path = m_path_world + DIR_DELIM "env_meta.txt";
 
        // Open file and serialize
        std::ostringstream ss(std::ios_base::binary);
 
-       Settings args;
+       Settings args("EnvArgsEnd");
        args.setU64("game_time", m_game_time);
        args.setU64("time_of_day", getTimeOfDay());
        args.setU64("last_clear_objects_time", m_last_clear_objects_time);
@@ -625,7 +676,6 @@ void ServerEnvironment::saveMeta()
                m_lbm_mgr.createIntroductionTimesString());
        args.setU64("day_count", m_day_count);
        args.writeLines(ss);
-       ss<<"EnvArgsEnd\n";
 
        if(!fs::safeWriteToFile(path, ss.str()))
        {
@@ -637,6 +687,9 @@ void ServerEnvironment::saveMeta()
 
 void ServerEnvironment::loadMeta()
 {
+       SANITY_CHECK(!m_meta_loaded);
+       m_meta_loaded = true;
+
        // If file doesn't exist, load default environment metadata
        if (!fs::PathExists(m_path_world + DIR_DELIM "env_meta.txt")) {
                infostream << "ServerEnvironment: Loading default environment metadata"
@@ -657,9 +710,9 @@ void ServerEnvironment::loadMeta()
                throw SerializationError("Couldn't load env meta");
        }
 
-       Settings args;
+       Settings args("EnvArgsEnd");
 
-       if (!args.parseConfigLines(is, "EnvArgsEnd")) {
+       if (!args.parseConfigLines(is)) {
                throw SerializationError("ServerEnvironment::loadMeta(): "
                        "EnvArgsEnd not found!");
        }
@@ -711,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
@@ -741,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 =
@@ -786,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;
@@ -804,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
@@ -850,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) {
@@ -867,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;
 
@@ -883,11 +942,11 @@ 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
-                                                       MapNode n = map->getNodeNoEx(p1 + block->getPosRelative());
+                                                       MapNode n = map->getNode(p1 + block->getPosRelative());
                                                        c = n.getContent();
                                                }
                                                if (CONTAINS(aabm.required_neighbors, c))
@@ -909,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;
@@ -937,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
        }
 
@@ -951,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)
@@ -981,7 +1036,7 @@ void ServerEnvironment::addLoadingBlockModifierDef(LoadingBlockModifierDef *lbm)
 bool ServerEnvironment::setNode(v3s16 p, const MapNode &n)
 {
        const NodeDefManager *ndef = m_server->ndef();
-       MapNode n_old = m_map->getNodeNoEx(p);
+       MapNode n_old = m_map->getNode(p);
 
        const ContentFeatures &cf_old = ndef->get(n_old);
 
@@ -1014,7 +1069,7 @@ bool ServerEnvironment::setNode(v3s16 p, const MapNode &n)
 bool ServerEnvironment::removeNode(v3s16 p)
 {
        const NodeDefManager *ndef = m_server->ndef();
-       MapNode n_old = m_map->getNodeNoEx(p);
+       MapNode n_old = m_map->getNode(p);
 
        // Call destructor
        if (ndef->get(n_old).has_on_destruct)
@@ -1047,37 +1102,106 @@ bool ServerEnvironment::swapNode(v3s16 p, const MapNode &n)
        return true;
 }
 
-void ServerEnvironment::getObjectsInsideRadius(std::vector<u16> &objects, v3f pos,
-       float radius)
+u8 ServerEnvironment::findSunlight(v3s16 pos) const
 {
-       for (auto &activeObject : m_active_objects) {
-               ServerActiveObject* obj = activeObject.second;
-               u16 id = activeObject.first;
-               v3f objectpos = obj->getBasePosition();
-               if (objectpos.getDistanceFrom(pos) > radius)
-                       continue;
-               objects.push_back(id);
+       // 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)
+       };
+
+       const NodeDefManager *ndef = m_server->ndef();
+
+       // found_light remembers the highest known sunlight value at pos
+       u8 found_light = 0;
+
+       struct stack_entry {
+               v3s16 pos;
+               s16 dist;
+       };
+       std::stack<stack_entry> stack;
+       stack.push({pos, 0});
+
+       std::unordered_map<s64, s8> dists;
+       dists[MapDatabase::getBlockAsInteger(pos)] = 0;
+
+       while (!stack.empty()) {
+               struct stack_entry e = stack.top();
+               stack.pop();
+
+               v3s16 currentPos = e.pos;
+               s8 dist = e.dist + 1;
+
+               for (const v3s16& off : dirs) {
+                       v3s16 neighborPos = currentPos + off;
+                       s64 neighborHash = MapDatabase::getBlockAsInteger(neighborPos);
+
+                       // Do not walk neighborPos multiple times unless the distance to the start
+                       // position is shorter
+                       auto it = dists.find(neighborHash);
+                       if (it != dists.end() && dist >= it->second)
+                               continue;
+
+                       // Position to walk
+                       bool is_position_ok;
+                       MapNode node = m_map->getNode(neighborPos, &is_position_ok);
+                       if (!is_position_ok) {
+                               // This happens very rarely because the map at currentPos is loaded
+                               m_map->emergeBlock(neighborPos, false);
+                               node = m_map->getNode(neighborPos, &is_position_ok);
+                               if (!is_position_ok)
+                                       continue; // not generated
+                       }
+
+                       const ContentFeatures &def = ndef->get(node);
+                       if (!def.sunlight_propagates) {
+                               // Do not test propagation here again
+                               dists[neighborHash] = -1;
+                               continue;
+                       }
+
+                       // Sunlight could have come from here
+                       dists[neighborHash] = dist;
+                       u8 daylight = node.param1 & 0x0f;
+
+                       // In the special case where sunlight shines from above and thus
+                       // does not decrease with upwards distance, daylight is always
+                       // bigger than nightlight, which never reaches 15
+                       int possible_finlight = daylight - dist;
+                       if (possible_finlight <= found_light) {
+                               // Light from here cannot make a brighter light at currentPos than
+                               // found_light
+                               continue;
+                       }
+
+                       u8 nightlight = node.param1 >> 4;
+                       if (daylight > nightlight) {
+                               // Found a valid daylight
+                               found_light = possible_finlight;
+                       } else {
+                               // Sunlight may be darker, so walk the neighbors
+                               stack.push({neighborPos, dist});
+                       }
+               }
        }
+       return found_light;
 }
 
 void ServerEnvironment::clearObjects(ClearObjectsMode mode)
 {
        infostream << "ServerEnvironment::clearObjects(): "
                << "Removing all active objects" << std::endl;
-       std::vector<u16> objects_to_remove;
-       for (auto &it : m_active_objects) {
-               u16 id = it.first;
-               ServerActiveObject* obj = it.second;
+       auto cb_removal = [this] (ServerActiveObject *obj, u16 id) {
                if (obj->getType() == ACTIVEOBJECT_TYPE_PLAYER)
-                       continue;
+                       return false;
 
                // Delete static object if block is loaded
                deleteStaticFromBlock(obj, id, MOD_REASON_CLEAR_ALL_OBJECTS, true);
 
                // If known by some client, don't delete immediately
                if (obj->m_known_by_count > 0) {
-                       obj->m_pending_removal = true;
-                       continue;
+                       obj->markForRemoval();
+                       return false;
                }
 
                // Tell the object about removal
@@ -1088,14 +1212,11 @@ void ServerEnvironment::clearObjects(ClearObjectsMode mode)
                // Delete active object
                if (obj->environmentDeletes())
                        delete obj;
-               // Id to be removed from m_active_objects
-               objects_to_remove.push_back(id);
-       }
 
-       // Remove references from m_active_objects
-       for (u16 i : objects_to_remove) {
-               m_active_objects.erase(i);
-       }
+               return true;
+       };
+
+       m_ao_manager.clear(cb_removal);
 
        // Get list of loaded blocks
        std::vector<v3s16> loaded_blocks;
@@ -1149,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++;
@@ -1192,6 +1309,9 @@ 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);
 
@@ -1216,7 +1336,7 @@ void ServerEnvironment::step(float dtime)
                Handle players
        */
        {
-               ScopeProfiler sp(g_profiler, "SEnv: handle players avg", SPT_AVG);
+               ScopeProfiler sp(g_profiler, "ServerEnv: move players", SPT_AVG);
                for (RemotePlayer *player : m_players) {
                        // Ignore disconnected players
                        if (player->getPeerId() == PEER_ID_INEXISTENT)
@@ -1230,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)) {
-               ScopeProfiler sp(g_profiler, "SEnv: manage act. block list avg per interval", SPT_AVG);
+       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;
@@ -1284,20 +1406,29 @@ 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;
        }
 
        /*
                Mess around in active blocks
        */
        if (m_active_blocks_nodemetadata_interval.step(dtime, m_cache_nodetimer_interval)) {
-               ScopeProfiler sp(g_profiler, "SEnv: mess in act. blocks avg per interval", SPT_AVG);
+               ScopeProfiler sp(g_profiler, "ServerEnv: Run node timers", SPT_AVG);
 
                float dtime = m_cache_nodetimer_interval;
 
@@ -1318,107 +1449,108 @@ 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);
+                       });
                }
        }
 
-       if (m_active_block_modifier_interval.step(dtime, m_cache_abm_interval))
-               do { // breakable
-                       if (m_active_block_interval_overload_skip > 0) {
-                               ScopeProfiler sp(g_profiler, "SEnv: ABM overload skips");
-                               m_active_block_interval_overload_skip--;
-                               break;
-                       }
-                       ScopeProfiler sp(g_profiler, "SEnv: modify in blocks avg per interval", SPT_AVG);
-                       TimeTaker timer("modify in active blocks per interval");
-
-                       // Initialize handling of ActiveBlockModifiers
-                       ABMHandler abmhandler(m_abms, m_cache_abm_interval, this, true);
-
-                       int blocks_scanned = 0;
-                       int abms_run = 0;
-                       int blocks_cached = 0;
-                       for (const v3s16 &p : m_active_blocks.m_abm_list) {
-                               MapBlock *block = m_map->getBlockNoCreateNoEx(p);
-                               if (!block)
-                                       continue;
+       if (m_active_block_modifier_interval.step(dtime, m_cache_abm_interval)) {
+               ScopeProfiler sp(g_profiler, "SEnv: modify in blocks avg per interval", SPT_AVG);
+               TimeTaker timer("modify in active blocks per interval");
 
-                               // Set current time as timestamp
-                               block->setTimestampNoChangedFlag(m_game_time);
+               // Shuffle to prevent persistent artifacts of ordering
+               std::shuffle(m_abms.begin(), m_abms.end(), m_rgen);
 
-                               /* Handle ActiveBlockModifiers */
-                               abmhandler.apply(block, blocks_scanned, abms_run, blocks_cached);
-                       }
-                       g_profiler->avg("SEnv: active blocks", m_active_blocks.m_abm_list.size());
-                       g_profiler->avg("SEnv: active blocks cached", blocks_cached);
-                       g_profiler->avg("SEnv: active blocks scanned for ABMs", blocks_scanned);
-                       g_profiler->avg("SEnv: ABMs run", abms_run);
+               // Initialize handling of ActiveBlockModifiers
+               ABMHandler abmhandler(m_abms, m_cache_abm_interval, this, true);
+
+               int blocks_scanned = 0;
+               int abms_run = 0;
+               int blocks_cached = 0;
+
+               std::vector<v3s16> output(m_active_blocks.m_abm_list.size());
+
+               // Shuffle the active blocks so that each block gets an equal chance
+               // of having its ABMs run.
+               std::copy(m_active_blocks.m_abm_list.begin(), m_active_blocks.m_abm_list.end(), output.begin());
+               std::shuffle(output.begin(), output.end(), m_rgen);
+
+               int i = 0;
+               // determine the time budget for ABMs
+               u32 max_time_ms = m_cache_abm_interval * 1000 * m_cache_abm_time_budget;
+               for (const v3s16 &p : output) {
+                       MapBlock *block = m_map->getBlockNoCreateNoEx(p);
+                       if (!block)
+                               continue;
+
+                       i++;
+
+                       // Set current time as timestamp
+                       block->setTimestampNoChangedFlag(m_game_time);
+
+                       /* Handle ActiveBlockModifiers */
+                       abmhandler.apply(block, blocks_scanned, abms_run, blocks_cached);
+
+                       u32 time_ms = timer.getTimerTime();
 
-                       u32 time_ms = timer.stop(true);
-                       u32 max_time_ms = 200;
                        if (time_ms > max_time_ms) {
-                               warningstream<<"active block modifiers took "
-                                       <<time_ms<<"ms (longer than "
-                                       <<max_time_ms<<"ms)"<<std::endl;
-                               m_active_block_interval_overload_skip = (time_ms / max_time_ms) + 1;
+                               warningstream << "active block modifiers took "
+                                         << time_ms << "ms (processed " << i << " of "
+                                         << output.size() << " active blocks)" << std::endl;
+                               break;
                        }
-               }while(0);
+               }
+               g_profiler->avg("ServerEnv: active blocks", m_active_blocks.m_abm_list.size());
+               g_profiler->avg("ServerEnv: active blocks cached", blocks_cached);
+               g_profiler->avg("ServerEnv: active blocks scanned for ABMs", blocks_scanned);
+               g_profiler->avg("ServerEnv: ABMs run", abms_run);
+
+               timer.stop(true);
+       }
 
        /*
                Step script environment (run global on_step())
        */
        m_script->environment_Step(dtime);
 
+       m_script->stepAsync();
+
        /*
                Step active objects
        */
        {
-               ScopeProfiler sp(g_profiler, "SEnv: step act. objs avg", SPT_AVG);
-               //TimeTaker timer("Step active objects");
-
-               g_profiler->avg("SEnv: num of objects", m_active_objects.size());
+               ScopeProfiler sp(g_profiler, "ServerEnv: Run SAO::step()", SPT_AVG);
 
                // This helps the objects to send data at the same time
                bool send_recommended = false;
                m_send_recommended_timer += dtime;
-               if(m_send_recommended_timer > getSendRecommendedInterval())
-               {
+               if (m_send_recommended_timer > getSendRecommendedInterval()) {
                        m_send_recommended_timer -= getSendRecommendedInterval();
                        send_recommended = true;
                }
 
-               for (auto &ao_it : m_active_objects) {
-                       ServerActiveObject* obj = ao_it.second;
+               u32 object_count = 0;
+
+               auto cb_state = [&](ServerActiveObject *obj) {
                        if (obj->isGone())
-                               continue;
+                               return;
+                       object_count++;
 
                        // Step object
                        obj->step(dtime, send_recommended);
                        // Read messages from object
-                       while (!obj->m_messages_out.empty()) {
-                               m_active_object_messages.push(obj->m_messages_out.front());
-                               obj->m_messages_out.pop();
-                       }
-               }
+                       obj->dumpAOMessagesToQueue(m_active_object_messages);
+               };
+               m_ao_manager.step(dtime, cb_state);
+
+               m_active_object_gauge->set(object_count);
        }
 
        /*
                Manage active objects
        */
        if (m_object_management_interval.step(dtime, 0.5)) {
-               ScopeProfiler sp(g_profiler, "SEnv: remove removed objs avg /.5s", SPT_AVG);
                removeRemovedObjects();
        }
 
@@ -1441,6 +1573,45 @@ void ServerEnvironment::step(float dtime)
                                ++i;
                }
        }
+
+       // Send outdated player inventories
+       for (RemotePlayer *player : m_players) {
+               if (player->getPeerId() == PEER_ID_INEXISTENT)
+                       continue;
+
+               PlayerSAO *sao = player->getPlayerSAO();
+               if (sao && player->inventory.checkModified())
+                       m_server->SendInventory(sao, true);
+       }
+
+       // 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)
+{
+       if (m_active_blocks.contains(blockpos))
+               return BS_ACTIVE;
+
+       const MapBlock *block = m_map->getBlockNoCreateNoEx(blockpos);
+       if (block)
+               return BS_LOADED;
+
+       if (m_map->isBlockInQueue(blockpos))
+               return BS_EMERGING;
+
+       return BS_UNKNOWN;
 }
 
 u32 ServerEnvironment::addParticleSpawner(float exptime)
@@ -1484,44 +1655,6 @@ void ServerEnvironment::deleteParticleSpawner(u32 id, bool remove_from_object)
        }
 }
 
-ServerActiveObject* ServerEnvironment::getActiveObject(u16 id)
-{
-       ServerActiveObjectMap::const_iterator n = m_active_objects.find(id);
-       return (n != m_active_objects.end() ? n->second : NULL);
-}
-
-/**
- * Verify if id is a free active object id
- * @param id
- * @return true if slot is free
- */
-bool ServerEnvironment::isFreeServerActiveObjectId(u16 id) const
-{
-       if (id == 0)
-               return false;
-
-       return m_active_objects.find(id) == m_active_objects.end();
-}
-
-/**
- * Retrieve the first free ActiveObject ID
- * @return free activeobject ID or 0 if none was found
- */
-u16 ServerEnvironment::getFreeServerActiveObjectId()
-{
-       // try to reuse id's as late as possible
-       static u16 last_used_id = 0;
-       u16 startid = last_used_id;
-       for (;;) {
-               last_used_id++;
-               if (isFreeServerActiveObjectId(last_used_id))
-                       return last_used_id;
-
-               if (last_used_id == startid)
-                       return 0;
-       }
-}
-
 u16 ServerEnvironment::addActiveObject(ServerActiveObject *object)
 {
        assert(object); // Pre-condition
@@ -1542,43 +1675,11 @@ void ServerEnvironment::getAddedActiveObjects(PlayerSAO *playersao, s16 radius,
        f32 radius_f = radius * BS;
        f32 player_radius_f = player_radius * BS;
 
-       if (player_radius_f < 0)
-               player_radius_f = 0;
-       /*
-               Go through the object list,
-               - discard removed/deactivated objects,
-               - discard objects that are too far away,
-               - discard objects that are found in current_objects.
-               - add remaining objects to added_objects
-       */
-       for (auto &ao_it : m_active_objects) {
-               u16 id = ao_it.first;
-
-               // Get object
-               ServerActiveObject *object = ao_it.second;
-               if (object == NULL)
-                       continue;
-
-               if (object->isGone())
-                       continue;
-
-               f32 distance_f = object->getBasePosition().
-                       getDistanceFrom(playersao->getBasePosition());
-               if (object->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
-                       // Discard if too far
-                       if (distance_f > player_radius_f && player_radius_f != 0)
-                               continue;
-               } else if (distance_f > radius_f)
-                       continue;
+       if (player_radius_f < 0.0f)
+               player_radius_f = 0.0f;
 
-               // Discard if already on current_objects
-               std::set<u16>::iterator n;
-               n = current_objects.find(id);
-               if(n != current_objects.end())
-                       continue;
-               // Add to added_objects
-               added_objects.push(id);
-       }
+       m_ao_manager.getAddedActiveObjectsAroundPos(playersao->getBasePosition(), radius_f,
+               player_radius_f, current_objects, added_objects);
 }
 
 /*
@@ -1637,10 +1738,10 @@ 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
-               ServerActiveObjectMap::const_iterator ao_it = m_active_objects.find(so_it.first);
-               if (ao_it == m_active_objects.end()) {
+               ServerActiveObject *sao = m_ao_manager.getActiveObject(so_it.first);
+               if (!sao) {
                        // If this ever happens, there must be some kind of nasty bug.
                        errorstream << "ServerEnvironment::setStaticForObjectsInBlock(): "
                                "Object from MapBlock::m_static_objects::m_active not found "
@@ -1648,49 +1749,59 @@ void ServerEnvironment::setStaticForActiveObjectsInBlock(
                        continue;
                }
 
-               ServerActiveObject *sao = ao_it->second;
                sao->m_static_exists = static_exists;
                sao->m_static_block  = static_block;
        }
 }
 
-ActiveObjectMessage ServerEnvironment::getActiveObjectMessage()
+bool ServerEnvironment::getActiveObjectMessage(ActiveObjectMessage *dest)
 {
-       if(m_active_object_messages.empty())
-               return ActiveObjectMessage(0);
+       if (m_active_object_messages.empty())
+               return false;
 
-       ActiveObjectMessage message = m_active_object_messages.front();
+       *dest = std::move(m_active_object_messages.front());
        m_active_object_messages.pop();
-       return message;
+       return true;
 }
 
 void ServerEnvironment::getSelectedActiveObjects(
        const core::line3d<f32> &shootline_on_map,
        std::vector<PointedThing> &objects)
 {
-       std::vector<u16> objectIds;
-       getObjectsInsideRadius(objectIds, shootline_on_map.start,
-               shootline_on_map.getLength() + 10.0f);
+       std::vector<ServerActiveObject *> objs;
+       getObjectsInsideRadius(objs, shootline_on_map.start,
+               shootline_on_map.getLength() + 10.0f, nullptr);
        const v3f line_vector = shootline_on_map.getVector();
 
-       for (u16 objectId : objectIds) {
-               ServerActiveObject* obj = getActiveObject(objectId);
-
+       for (auto obj : objs) {
+               if (obj->isGone())
+                       continue;
                aabb3f selection_box;
                if (!obj->getSelectionBox(&selection_box))
                        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) objectId, current_intersection, current_normal,
+                               (s16) obj->getId(), current_intersection, current_normal, current_raw_normal,
                                (current_intersection - shootline_on_map.start).getLengthSQ());
                }
        }
@@ -1703,68 +1814,25 @@ void ServerEnvironment::getSelectedActiveObjects(
 u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
        bool set_changed, u32 dtime_s)
 {
-       assert(object); // Pre-condition
-       if(object->getId() == 0){
-               u16 new_id = getFreeServerActiveObjectId();
-               if(new_id == 0)
-               {
-                       errorstream<<"ServerEnvironment::addActiveObjectRaw(): "
-                               <<"no free ids available"<<std::endl;
-                       if(object->environmentDeletes())
-                               delete object;
-                       return 0;
-               }
-               object->setId(new_id);
-       }
-       else{
-               verbosestream<<"ServerEnvironment::addActiveObjectRaw(): "
-                       <<"supplied with id "<<object->getId()<<std::endl;
-       }
-
-       if(!isFreeServerActiveObjectId(object->getId())) {
-               errorstream<<"ServerEnvironment::addActiveObjectRaw(): "
-                       <<"id is not free ("<<object->getId()<<")"<<std::endl;
-               if(object->environmentDeletes())
-                       delete object;
+       if (!m_ao_manager.registerObject(object)) {
                return 0;
        }
 
-       if (objectpos_over_limit(object->getBasePosition())) {
-               v3f p = object->getBasePosition();
-               warningstream << "ServerEnvironment::addActiveObjectRaw(): "
-                       << "object position (" << p.X << "," << p.Y << "," << p.Z
-                       << ") outside maximum range" << std::endl;
-               if (object->environmentDeletes())
-                       delete object;
-               return 0;
-       }
-
-       /*infostream<<"ServerEnvironment::addActiveObjectRaw(): "
-                       <<"added (id="<<object->getId()<<")"<<std::endl;*/
-
-       m_active_objects[object->getId()] = object;
-
-       verbosestream<<"ServerEnvironment::addActiveObjectRaw(): "
-               <<"Added id="<<object->getId()<<"; there are now "
-               <<m_active_objects.size()<<" active objects."
-               <<std::endl;
-
        // Register reference in scripting api (must be done before post-init)
        m_script->addObjectReference(object);
        // Post-initialize object
        object->addedToEnvironment(dtime_s);
 
        // Add static data to block
-       if(object->isStaticAllowed())
-       {
+       if (object->isStaticAllowed()) {
                // Add static object to active static list of the block
                v3f objectpos = object->getBasePosition();
                StaticObject s_obj(object, objectpos);
                // 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;
 
@@ -1787,50 +1855,39 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
 */
 void ServerEnvironment::removeRemovedObjects()
 {
-       std::vector<u16> objects_to_remove;
-       for (auto &ao_it : m_active_objects) {
-               u16 id = ao_it.first;
-               ServerActiveObject* obj = ao_it.second;
+       ScopeProfiler sp(g_profiler, "ServerEnvironment::removeRemovedObjects()", SPT_AVG);
 
+       auto clear_cb = [this](ServerActiveObject *obj, u16 id) {
                // This shouldn't happen but check it
                if (!obj) {
                        errorstream << "ServerEnvironment::removeRemovedObjects(): "
                                        << "NULL object found. id=" << id << std::endl;
-                       objects_to_remove.push_back(id);
-                       continue;
+                       return true;
                }
 
                /*
                        We will handle objects marked for removal or deactivation
                */
                if (!obj->isGone())
-                       continue;
+                       return false;
 
                /*
                        Delete static data from block if removed
                */
-               if (obj->m_pending_removal)
+               if (obj->isPendingRemoval())
                        deleteStaticFromBlock(obj, id, MOD_REASON_REMOVE_OBJECTS_REMOVE, false);
 
                // 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)
-                       continue;
+               if (obj->m_known_by_count > 0)
+                       return false;
 
                /*
                        Move static data from active to stored if deactivated
                */
-               if (!obj->m_pending_removal && obj->m_static_exists) {
-                       MapBlock *block = m_map->emergeBlock(obj->m_static_block, false);
-                       if (block) {
-                               std::map<u16, StaticObject>::iterator 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 (!obj->isPendingRemoval() && obj->m_static_exists) {
+                       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 "
@@ -1848,109 +1905,101 @@ void ServerEnvironment::removeRemovedObjects()
                m_script->removeObjectReference(obj);
 
                // Delete
-               if(obj->environmentDeletes())
+               if (obj->environmentDeletes())
                        delete obj;
 
-               objects_to_remove.push_back(id);
-       }
-       // Remove references from m_active_objects
-       for (u16 i : objects_to_remove) {
-               m_active_objects.erase(i);
-       }
+               return true;
+       };
+
+       m_ao_manager.clear(clear_cb);
 }
 
 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;
        }
 }
 
+ServerActiveObject* ServerEnvironment::createSAO(ActiveObjectType type, v3f pos,
+               const std::string &data)
+{
+       switch (type) {
+               case ACTIVEOBJECT_TYPE_LUAENTITY:
+                       return new LuaEntitySAO(this, pos, data);
+               default:
+                       warningstream << "ServerActiveObject: No factory for type=" << type << std::endl;
+       }
+       return nullptr;
+}
+
 /*
        Convert stored objects from blocks near the players to active.
 */
 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 = ServerActiveObject::create
-                       ((ActiveObjectType) s_obj.type, this, 0, 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 == NULL) {
-                       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;
+               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;
                        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);
        }
 
        /*
@@ -1976,24 +2025,19 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s)
 */
 void ServerEnvironment::deactivateFarObjects(bool _force_delete)
 {
-       std::vector<u16> objects_to_remove;
-       for (auto &ao_it : m_active_objects) {
-               // 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;
 
-               ServerActiveObject* obj = ao_it.second;
-               assert(obj);
-
-               // Do not deactivate if static data creation not allowed
-               if(!force_delete && !obj->isStaticAllowed())
-                       continue;
+               // Do not deactivate if disallowed
+               if (!force_delete && !obj->shouldUnload())
+                       return false;
 
                // removeRemovedObjects() is responsible for these
-               if(!force_delete && obj->isGone())
-                       continue;
+               if (!force_delete && obj->isGone())
+                       return false;
 
-               u16 id = ao_it.first;
-               v3f objectpos = obj->getBasePosition();
+               const v3f &objectpos = obj->getBasePosition();
 
                // The block in which the object resides in
                v3s16 blockpos_o = getNodeBlockPos(floatToInt(objectpos, BS));
@@ -2001,11 +2045,10 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete)
                // If object's static data is stored in a deactivated block and object
                // is actually located in an active block, re-save to the block in
                // which the object is actually located in.
-               if(!force_delete &&
-                       obj->m_static_exists &&
-                       !m_active_blocks.contains(obj->m_static_block) &&
-                       m_active_blocks.contains(blockpos_o))
-               {
+               if (!force_delete && obj->m_static_exists &&
+                  !m_active_blocks.contains(obj->m_static_block) &&
+                  m_active_blocks.contains(blockpos_o)) {
+
                        // Delete from block where object was located
                        deleteStaticFromBlock(obj, id, MOD_REASON_STATIC_DATA_REMOVED, false);
 
@@ -2013,16 +2056,19 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete)
                        // Save to block where object is located
                        saveStaticToBlock(blockpos_o, id, obj, s_obj, MOD_REASON_STATIC_DATA_ADDED);
 
-                       continue;
+                       return false;
                }
 
                // If block is still active, don't remove
-               if(!force_delete && m_active_blocks.contains(blockpos_o))
-                       continue;
+               bool still_active = obj->isStaticAllowed() ?
+                       m_active_blocks.contains(blockpos_o) :
+                       getMap().getBlockNoCreateNoEx(blockpos_o) != nullptr;
+               if (!force_delete && still_active)
+                       return false;
 
                verbosestream << "ServerEnvironment::deactivateFarObjects(): "
-                               << "deactivating object id=" << id << " on inactive block "
-                               << PP(blockpos_o) << std::endl;
+                                         << "deactivating object id=" << id << " on inactive block "
+                                         << PP(blockpos_o) << std::endl;
 
                // If known by some client, don't immediately delete.
                bool pending_delete = (obj->m_known_by_count > 0 && !force_delete);
@@ -2042,12 +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) {
-                                       std::map<u16, StaticObject>::iterator 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();
@@ -2081,22 +2124,25 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete)
                                force_delete = true;
                }
 
+               // Regardless of what happens to the object at this point, deactivate it first.
+               // This ensures that LuaEntity on_deactivate is always called.
+               obj->markForDeactivation();
+
                /*
                        If known by some client, set pending deactivation.
                        Otherwise delete it immediately.
                */
-               if(pending_delete && !force_delete)
-               {
+               if (pending_delete && !force_delete) {
                        verbosestream << "ServerEnvironment::deactivateFarObjects(): "
-                                       << "object id=" << id << " is known by clients"
-                                       << "; not deleting yet" << std::endl;
+                                                 << "object id=" << id << " is known by clients"
+                                                 << "; not deleting yet" << std::endl;
 
-                       obj->m_pending_deactivation = true;
-                       continue;
+                       return false;
                }
+
                verbosestream << "ServerEnvironment::deactivateFarObjects(): "
-                               << "object id=" << id << " is not known by clients"
-                               << "; deleting" << std::endl;
+                                         << "object id=" << id << " is not known by clients"
+                                         << "; deleting" << std::endl;
 
                // Tell the object about removal
                obj->removingFromEnvironment();
@@ -2104,16 +2150,13 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete)
                m_script->removeObjectReference(obj);
 
                // Delete active object
-               if(obj->environmentDeletes())
+               if (obj->environmentDeletes())
                        delete obj;
-               // Id to be removed from m_active_objects
-               objects_to_remove.push_back(id);
-       }
 
-       // Remove references from m_active_objects
-       for (u16 i : objects_to_remove) {
-               m_active_objects.erase(i);
-       }
+               return true;
+       };
+
+       m_ao_manager.clear(cb_deactivate);
 }
 
 void ServerEnvironment::deleteStaticFromBlock(
@@ -2160,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;
@@ -2188,6 +2222,7 @@ PlayerDatabase *ServerEnvironment::openPlayerDatabase(const std::string &name,
 
        if (name == "dummy")
                return new Database_Dummy();
+
 #if USE_POSTGRESQL
        if (name == "postgresql") {
                std::string connect_string;
@@ -2195,6 +2230,12 @@ PlayerDatabase *ServerEnvironment::openPlayerDatabase(const std::string &name,
                return new PlayerDatabasePostgreSQL(connect_string);
        }
 #endif
+
+#if USE_LEVELDB
+       if (name == "leveldb")
+               return new PlayerDatabaseLevelDB(savedir);
+#endif
+
        if (name == "files")
                return new PlayerDatabaseFiles(savedir + DIR_DELIM + "players");
 
@@ -2215,7 +2256,7 @@ bool ServerEnvironment::migratePlayersDatabase(const GameParams &game_params,
        if (!world_mt.exists("player_backend")) {
                errorstream << "Please specify your current backend in world.mt:"
                        << std::endl
-                       << "    player_backend = {files|sqlite3|postgresql}"
+                       << "    player_backend = {files|sqlite3|leveldb|postgresql}"
                        << std::endl;
                return false;
        }
@@ -2282,7 +2323,7 @@ bool ServerEnvironment::migratePlayersDatabase(const GameParams &game_params,
                delete dstdb;
 
        } catch (BaseException &e) {
-               errorstream << "An error occured during migration: " << e.what() << std::endl;
+               errorstream << "An error occurred during migration: " << e.what() << std::endl;
                return false;
        }
        return true;
@@ -2294,9 +2335,22 @@ AuthDatabase *ServerEnvironment::openAuthDatabase(
        if (name == "sqlite3")
                return new AuthDatabaseSQLite3(savedir);
 
+#if USE_POSTGRESQL
+       if (name == "postgresql") {
+               std::string connect_string;
+               conf.getNoEx("pgsql_auth_connection", connect_string);
+               return new AuthDatabasePostgreSQL(connect_string);
+       }
+#endif
+
        if (name == "files")
                return new AuthDatabaseFiles(savedir);
 
+#if USE_LEVELDB
+       if (name == "leveldb")
+               return new AuthDatabaseLevelDB(savedir);
+#endif
+
        throw BaseException(std::string("Database backend ") + name + " not supported.");
 }
 
@@ -2369,7 +2423,7 @@ bool ServerEnvironment::migrateAuthDatabase(
                }
 
        } catch (BaseException &e) {
-               errorstream << "An error occured during migration: " << e.what()
+               errorstream << "An error occurred during migration: " << e.what()
                            << std::endl;
                return false;
        }