]> git.lizzy.rs Git - minetest.git/blobdiff - src/environment.cpp
Use single box for halo mesh
[minetest.git] / src / environment.cpp
index 217d50fd594b14b9300d6fc1253d2e99d18ddc30..3bf5e1f0a41b6fd8803a10d231b8017a93ca18e4 100644 (file)
@@ -17,6 +17,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
+#include <fstream>
 #include "environment.h"
 #include "filesys.h"
 #include "porting.h"
@@ -31,7 +32,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "scripting_game.h"
 #include "nodedef.h"
 #include "nodemetadata.h"
-#include "main.h" // For g_settings, g_profiler
 #include "gamedef.h"
 #ifndef SERVER
 #include "clientmap.h"
@@ -39,19 +39,20 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "mapblock_mesh.h"
 #include "event.h"
 #endif
+#include "server.h"
 #include "daynightratio.h"
 #include "map.h"
 #include "emerge.h"
 #include "util/serialize.h"
-#include "jthread/jmutexautolock.h"
+#include "threading/mutex_auto_lock.h"
 
 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
 
 Environment::Environment():
+       m_time_of_day_speed(0),
        m_time_of_day(9000),
        m_time_of_day_f(9000./24000),
-       m_time_of_day_speed(0),
-       m_time_counter(0),
+       m_time_conversion_skew(0.0f),
        m_enable_day_night_ratio_override(false),
        m_day_night_ratio_override(0.0f)
 {
@@ -69,7 +70,7 @@ Environment::~Environment()
 
 void Environment::addPlayer(Player *player)
 {
-       DSTACK(__FUNCTION_NAME);
+       DSTACK(FUNCTION_NAME);
        /*
                Check that peer_ids are unique.
                Also check that names are unique.
@@ -77,35 +78,18 @@ void Environment::addPlayer(Player *player)
        */
        // If peer id is non-zero, it has to be unique.
        if(player->peer_id != 0)
-               assert(getPlayer(player->peer_id) == NULL);
+               FATAL_ERROR_IF(getPlayer(player->peer_id) != NULL, "Peer id not unique");
        // Name has to be unique.
-       assert(getPlayer(player->getName()) == NULL);
+       FATAL_ERROR_IF(getPlayer(player->getName()) != NULL, "Player name not unique");
        // Add.
        m_players.push_back(player);
 }
 
-void Environment::removePlayer(u16 peer_id)
-{
-       DSTACK(__FUNCTION_NAME);
-
-       for(std::vector<Player*>::iterator i = m_players.begin();
-                       i != m_players.end();)
-       {
-               Player *player = *i;
-               if(player->peer_id == peer_id) {
-                       delete player;
-                       i = m_players.erase(i);
-               } else {
-                       ++i;
-               }
-       }
-}
-
-void Environment::removePlayer(const char *name)
+void Environment::removePlayer(Player* player)
 {
        for (std::vector<Player*>::iterator it = m_players.begin();
                        it != m_players.end(); ++it) {
-               if (strcmp((*it)->getName(), name) == 0) {
+               if ((*it) == player) {
                        delete *it;
                        m_players.erase(it);
                        return;
@@ -182,7 +166,7 @@ std::vector<Player*> Environment::getPlayers(bool ignore_disconnected)
                        i = m_players.begin();
                        i != m_players.end(); ++i) {
                Player *player = *i;
-               
+
                if(ignore_disconnected) {
                        // Ignore disconnected players
                        if(player->peer_id == 0)
@@ -196,70 +180,76 @@ std::vector<Player*> Environment::getPlayers(bool ignore_disconnected)
 
 u32 Environment::getDayNightRatio()
 {
-       if(m_enable_day_night_ratio_override)
+       MutexAutoLock lock(this->m_time_lock);
+       if (m_enable_day_night_ratio_override)
                return m_day_night_ratio_override;
-       return time_to_daynight_ratio(m_time_of_day_f*24000, m_cache_enable_shaders);
+       return time_to_daynight_ratio(m_time_of_day_f * 24000, m_cache_enable_shaders);
 }
 
 void Environment::setTimeOfDaySpeed(float speed)
 {
-       JMutexAutoLock(this->m_timeofday_lock);
        m_time_of_day_speed = speed;
 }
 
 float Environment::getTimeOfDaySpeed()
 {
-       JMutexAutoLock(this->m_timeofday_lock);
-       float retval = m_time_of_day_speed;
-       return retval;
+       return m_time_of_day_speed;
+}
+
+void Environment::setDayNightRatioOverride(bool enable, u32 value)
+{
+       MutexAutoLock lock(this->m_time_lock);
+       m_enable_day_night_ratio_override = enable;
+       m_day_night_ratio_override = value;
 }
 
 void Environment::setTimeOfDay(u32 time)
 {
-       JMutexAutoLock(this->m_time_lock);
+       MutexAutoLock lock(this->m_time_lock);
        m_time_of_day = time;
        m_time_of_day_f = (float)time / 24000.0;
 }
 
 u32 Environment::getTimeOfDay()
 {
-       JMutexAutoLock(this->m_time_lock);
-       u32 retval = m_time_of_day;
-       return retval;
+       MutexAutoLock lock(this->m_time_lock);
+       return m_time_of_day;
 }
 
 float Environment::getTimeOfDayF()
 {
-       JMutexAutoLock(this->m_time_lock);
-       float retval = m_time_of_day_f;
-       return retval;
+       MutexAutoLock lock(this->m_time_lock);
+       return m_time_of_day_f;
 }
 
 void Environment::stepTimeOfDay(float dtime)
 {
-       // getTimeOfDaySpeed lock the value we need to prevent MT problems
-       float day_speed = getTimeOfDaySpeed();
-       
-       m_time_counter += dtime;
-       f32 speed = day_speed * 24000./(24.*3600);
-       u32 units = (u32)(m_time_counter*speed);
+       MutexAutoLock lock(this->m_time_lock);
+
+       // Cached in order to prevent the two reads we do to give
+       // different results (can be written by code not under the lock)
+       f32 cached_time_of_day_speed = m_time_of_day_speed;
+
+       f32 speed = cached_time_of_day_speed * 24000. / (24. * 3600);
+       m_time_conversion_skew += dtime;
+       u32 units = (u32)(m_time_conversion_skew * speed);
        bool sync_f = false;
-       if(units > 0){
+       if (units > 0) {
                // Sync at overflow
-               if(m_time_of_day + units >= 24000)
+               if (m_time_of_day + units >= 24000)
                        sync_f = true;
                m_time_of_day = (m_time_of_day + units) % 24000;
-               if(sync_f)
+               if (sync_f)
                        m_time_of_day_f = (float)m_time_of_day / 24000.0;
        }
        if (speed > 0) {
-               m_time_counter -= (f32)units / speed;
+               m_time_conversion_skew -= (f32)units / speed;
        }
-       if(!sync_f){
-               m_time_of_day_f += day_speed/24/3600*dtime;
-               if(m_time_of_day_f > 1.0)
+       if (!sync_f) {
+               m_time_of_day_f += cached_time_of_day_speed / 24 / 3600 * dtime;
+               if (m_time_of_day_f > 1.0)
                        m_time_of_day_f -= 1.0;
-               if(m_time_of_day_f < 0.0)
+               if (m_time_of_day_f < 0.0)
                        m_time_of_day_f += 1.0;
        }
 }
@@ -364,6 +354,7 @@ ServerEnvironment::ServerEnvironment(ServerMap *map,
        m_active_block_interval_overload_skip(0),
        m_game_time(0),
        m_game_time_fraction_counter(0),
+       m_last_clear_objects_time(0),
        m_recommended_send_interval(0.1),
        m_max_lag_estimate(0.1)
 {
@@ -382,7 +373,7 @@ ServerEnvironment::~ServerEnvironment()
        m_map->drop();
 
        // Delete ActiveBlockModifiers
-       for(std::list<ABMWithState>::iterator
+       for(std::vector<ABMWithState>::iterator
                        i = m_abms.begin(); i != m_abms.end(); ++i){
                delete i->abm;
        }
@@ -425,6 +416,18 @@ bool ServerEnvironment::line_of_sight(v3f pos1, v3f pos2, float stepsize, v3s16
        return true;
 }
 
+void ServerEnvironment::kickAllPlayers(AccessDeniedCode reason,
+               const std::string &str_reason, bool reconnect)
+{
+       for (std::vector<Player*>::iterator it = m_players.begin();
+                       it != m_players.end();
+                       ++it) {
+               ((Server*)m_gamedef)->DenyAccessVerCompliant((*it)->peer_id,
+                       (*it)->protocol_version, (AccessDeniedCode)reason,
+                       str_reason, reconnect);
+       }
+}
+
 void ServerEnvironment::saveLoadedPlayers()
 {
        std::string players_path = m_path_world + DIR_DELIM "players";
@@ -440,54 +443,53 @@ void ServerEnvironment::saveLoadedPlayers()
        }
 }
 
-void ServerEnvironment::savePlayer(const std::string &playername)
+void ServerEnvironment::savePlayer(RemotePlayer *player)
 {
        std::string players_path = m_path_world + DIR_DELIM "players";
        fs::CreateDir(players_path);
 
-       RemotePlayer *player = static_cast<RemotePlayer*>(getPlayer(playername.c_str()));
-       if (player) {
-               player->save(players_path);
-       }
+       player->save(players_path);
 }
 
 Player *ServerEnvironment::loadPlayer(const std::string &playername)
 {
-       std::string players_path = m_path_world + DIR_DELIM "players" DIR_DELIM;
-
-       RemotePlayer *player = static_cast<RemotePlayer*>(getPlayer(playername.c_str()));
        bool newplayer = false;
        bool found = false;
+       std::string players_path = m_path_world + DIR_DELIM "players" DIR_DELIM;
+       std::string path = players_path + playername;
+
+       RemotePlayer *player = static_cast<RemotePlayer *>(getPlayer(playername.c_str()));
        if (!player) {
-               player = new RemotePlayer(m_gamedef, playername.c_str());
+               player = new RemotePlayer(m_gamedef, "");
                newplayer = true;
        }
 
-       RemotePlayer testplayer(m_gamedef, "");
-       std::string path = players_path + playername;
        for (u32 i = 0; i < PLAYER_FILE_ALTERNATE_TRIES; i++) {
-               // Open file and deserialize
+               //// Open file and deserialize
                std::ifstream is(path.c_str(), std::ios_base::binary);
-               if (!is.good()) {
-                       return NULL;
-               }
-               testplayer.deSerialize(is, path);
+               if (!is.good())
+                       continue;
+               player->deSerialize(is, path);
                is.close();
-               if (testplayer.getName() == playername) {
-                       *player = testplayer;
+
+               if (player->getName() == playername) {
                        found = true;
                        break;
                }
+
                path = players_path + playername + itos(i);
        }
+
        if (!found) {
                infostream << "Player file for player " << playername
                                << " not found" << std::endl;
+               if (newplayer)
+                       delete player;
                return NULL;
        }
-       if (newplayer) {
+
+       if (newplayer)
                addPlayer(player);
-       }
        player->setModified(false);
        return player;
 }
@@ -502,6 +504,7 @@ void ServerEnvironment::saveMeta()
        Settings args;
        args.setU64("game_time", m_game_time);
        args.setU64("time_of_day", getTimeOfDay());
+       args.setU64("last_clear_objects_time", m_last_clear_objects_time);
        args.writeLines(ss);
        ss<<"EnvArgsEnd\n";
 
@@ -540,10 +543,17 @@ void ServerEnvironment::loadMeta()
        }
 
        try {
-               m_time_of_day = args.getU64("time_of_day");
+               setTimeOfDay(args.getU64("time_of_day"));
        } catch (SettingNotFoundException &e) {
                // This is not as important
-               m_time_of_day = 9000;
+               setTimeOfDay(9000);
+       }
+
+       try {
+               m_last_clear_objects_time = args.getU64("last_clear_objects_time");
+       } catch (SettingNotFoundException &e) {
+               // If missing, do as if clearObjects was never called
+               m_last_clear_objects_time = 0;
        }
 }
 
@@ -560,7 +570,7 @@ class ABMHandler
        ServerEnvironment *m_env;
        std::map<content_t, std::vector<ActiveABM> > m_aabms;
 public:
-       ABMHandler(std::list<ABMWithState> &abms,
+       ABMHandler(std::vector<ABMWithState> &abms,
                        float dtime_s, ServerEnvironment *env,
                        bool use_timers):
                m_env(env)
@@ -568,8 +578,8 @@ class ABMHandler
                if(dtime_s < 0.001)
                        return;
                INodeDefManager *ndef = env->getGameDef()->ndef();
-               for(std::list<ABMWithState>::iterator
-                               i = abms.begin(); i != abms.end(); ++i){
+               for(std::vector<ABMWithState>::iterator
+                               i = abms.begin(); i != abms.end(); ++i) {
                        ActiveBlockModifier *abm = i->abm;
                        float trigger_interval = abm->getTriggerInterval();
                        if(trigger_interval < 0.001)
@@ -582,35 +592,39 @@ class ABMHandler
                                i->timer -= trigger_interval;
                                actual_interval = trigger_interval;
                        }
-                       float intervals = actual_interval / trigger_interval;
-                       if(intervals == 0)
-                               continue;
                        float chance = abm->getTriggerChance();
                        if(chance == 0)
                                chance = 1;
                        ActiveABM aabm;
                        aabm.abm = abm;
-                       aabm.chance = chance / intervals;
-                       if(aabm.chance == 0)
-                               aabm.chance = 1;
+                       if(abm->getSimpleCatchUp()) {
+                               float intervals = actual_interval / trigger_interval;
+                               if(intervals == 0)
+                                       continue;
+                               aabm.chance = chance / intervals;
+                               if(aabm.chance == 0)
+                                       aabm.chance = 1;
+                       } else {
+                               aabm.chance = chance;
+                       }
                        // Trigger neighbors
                        std::set<std::string> required_neighbors_s
                                        = abm->getRequiredNeighbors();
                        for(std::set<std::string>::iterator
                                        i = required_neighbors_s.begin();
-                                       i != required_neighbors_s.end(); i++)
+                                       i != required_neighbors_s.end(); ++i)
                        {
                                ndef->getIds(*i, aabm.required_neighbors);
                        }
                        // Trigger contents
                        std::set<std::string> contents_s = abm->getTriggerContents();
                        for(std::set<std::string>::iterator
-                                       i = contents_s.begin(); i != contents_s.end(); i++)
+                                       i = contents_s.begin(); i != contents_s.end(); ++i)
                        {
                                std::set<content_t> ids;
                                ndef->getIds(*i, ids);
                                for(std::set<content_t>::const_iterator k = ids.begin();
-                                               k != ids.end(); k++)
+                                               k != ids.end(); ++k)
                                {
                                        content_t c = *k;
                                        std::map<content_t, std::vector<ActiveABM> >::iterator j;
@@ -679,7 +693,7 @@ class ABMHandler
                                continue;
 
                        for(std::vector<ActiveABM>::iterator
-                                       i = j->second.begin(); i != j->second.end(); i++) {
+                                       i = j->second.begin(); i != j->second.end(); ++i) {
                                if(myrand() % i->chance != 0)
                                        continue;
 
@@ -734,19 +748,25 @@ void ServerEnvironment::activateBlock(MapBlock *block, u32 additional_dtime)
        // Get time difference
        u32 dtime_s = 0;
        u32 stamp = block->getTimestamp();
-       if(m_game_time > stamp && stamp != BLOCK_TIMESTAMP_UNDEFINED)
-               dtime_s = m_game_time - block->getTimestamp();
+       if (m_game_time > stamp && stamp != BLOCK_TIMESTAMP_UNDEFINED)
+               dtime_s = m_game_time - stamp;
        dtime_s += additional_dtime;
 
        /*infostream<<"ServerEnvironment::activateBlock(): block timestamp: "
                        <<stamp<<", game time: "<<m_game_time<<std::endl;*/
 
+       // 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();
+               // do not set changed flag to avoid unnecessary mapblock writes
+       }
+
        // Set current time as timestamp
        block->setTimestampNoChangedFlag(m_game_time);
 
        /*infostream<<"ServerEnvironment::activateBlock(): block is "
                        <<dtime_s<<" seconds old."<<std::endl;*/
-       
+
        // Activate stored objects
        activateObjects(block, dtime_s);
 
@@ -757,7 +777,7 @@ void ServerEnvironment::activateBlock(MapBlock *block, u32 additional_dtime)
                MapNode n;
                for(std::map<v3s16, NodeTimer>::iterator
                                i = elapsed_timers.begin();
-                               i != elapsed_timers.end(); i++){
+                               i != elapsed_timers.end(); ++i){
                        n = block->getNodeNoEx(i->first);
                        v3s16 p = i->first + block->getPosRelative();
                        if(m_script->node_on_timer(p,n,i->second.elapsed))
@@ -838,9 +858,8 @@ bool ServerEnvironment::swapNode(v3s16 p, const MapNode &n)
        return true;
 }
 
-std::set<u16> ServerEnvironment::getObjectsInsideRadius(v3f pos, float radius)
+void ServerEnvironment::getObjectsInsideRadius(std::vector<u16> &objects, v3f pos, float radius)
 {
-       std::set<u16> objects;
        for(std::map<u16, ServerActiveObject*>::iterator
                        i = m_active_objects.begin();
                        i != m_active_objects.end(); ++i)
@@ -850,35 +869,34 @@ std::set<u16> ServerEnvironment::getObjectsInsideRadius(v3f pos, float radius)
                v3f objectpos = obj->getBasePosition();
                if(objectpos.getDistanceFrom(pos) > radius)
                        continue;
-               objects.insert(id);
+               objects.push_back(id);
        }
-       return objects;
 }
 
-void ServerEnvironment::clearAllObjects()
+void ServerEnvironment::clearObjects(ClearObjectsMode mode)
 {
-       infostream<<"ServerEnvironment::clearAllObjects(): "
-                       <<"Removing all active objects"<<std::endl;
+       infostream << "ServerEnvironment::clearObjects(): "
+               << "Removing all active objects" << std::endl;
        std::vector<u16> objects_to_remove;
-       for(std::map<u16, ServerActiveObject*>::iterator
+       for (std::map<u16, ServerActiveObject*>::iterator
                        i = m_active_objects.begin();
                        i != m_active_objects.end(); ++i) {
                ServerActiveObject* obj = i->second;
-               if(obj->getType() == ACTIVEOBJECT_TYPE_PLAYER)
+               if (obj->getType() == ACTIVEOBJECT_TYPE_PLAYER)
                        continue;
                u16 id = i->first;
                // Delete static object if block is loaded
-               if(obj->m_static_exists){
+               if (obj->m_static_exists) {
                        MapBlock *block = m_map->getBlockNoCreateNoEx(obj->m_static_block);
-                       if(block){
+                       if (block) {
                                block->m_static_objects.remove(id);
                                block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                                               "clearAllObjects");
+                                               MOD_REASON_CLEAR_ALL_OBJECTS);
                                obj->m_static_exists = false;
                        }
                }
                // If known by some client, don't delete immediately
-               if(obj->m_known_by_count > 0){
+               if (obj->m_known_by_count > 0) {
                        obj->m_pending_deactivation = true;
                        obj->m_removed = true;
                        continue;
@@ -890,91 +908,101 @@ void ServerEnvironment::clearAllObjects()
                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(std::vector<u16>::iterator i = objects_to_remove.begin();
+       for (std::vector<u16>::iterator i = objects_to_remove.begin();
                        i != objects_to_remove.end(); ++i) {
                m_active_objects.erase(*i);
        }
 
        // Get list of loaded blocks
        std::vector<v3s16> loaded_blocks;
-       infostream<<"ServerEnvironment::clearAllObjects(): "
-                       <<"Listing all loaded blocks"<<std::endl;
+       infostream << "ServerEnvironment::clearObjects(): "
+               << "Listing all loaded blocks" << std::endl;
        m_map->listAllLoadedBlocks(loaded_blocks);
-       infostream<<"ServerEnvironment::clearAllObjects(): "
-                       <<"Done listing all loaded blocks: "
-                       <<loaded_blocks.size()<<std::endl;
+       infostream << "ServerEnvironment::clearObjects(): "
+               << "Done listing all loaded blocks: "
+               << loaded_blocks.size()<<std::endl;
 
        // Get list of loadable blocks
        std::vector<v3s16> loadable_blocks;
-       infostream<<"ServerEnvironment::clearAllObjects(): "
-                       <<"Listing all loadable blocks"<<std::endl;
-       m_map->listAllLoadableBlocks(loadable_blocks);
-       infostream<<"ServerEnvironment::clearAllObjects(): "
-                       <<"Done listing all loadable blocks: "
-                       <<loadable_blocks.size()
-                       <<", now clearing"<<std::endl;
+       if (mode == CLEAR_OBJECTS_MODE_FULL) {
+               infostream << "ServerEnvironment::clearObjects(): "
+                       << "Listing all loadable blocks" << std::endl;
+               m_map->listAllLoadableBlocks(loadable_blocks);
+               infostream << "ServerEnvironment::clearObjects(): "
+                       << "Done listing all loadable blocks: "
+                       << loadable_blocks.size() << std::endl;
+       } else {
+               loadable_blocks = loaded_blocks;
+       }
+
+       infostream << "ServerEnvironment::clearObjects(): "
+               << "Now clearing objects in " << loadable_blocks.size()
+               << " blocks" << std::endl;
 
        // Grab a reference on each loaded block to avoid unloading it
-       for(std::vector<v3s16>::iterator i = loaded_blocks.begin();
+       for (std::vector<v3s16>::iterator i = loaded_blocks.begin();
                        i != loaded_blocks.end(); ++i) {
                v3s16 p = *i;
                MapBlock *block = m_map->getBlockNoCreateNoEx(p);
-               assert(block);
+               assert(block != NULL);
                block->refGrab();
        }
 
        // Remove objects in all loadable blocks
-       u32 unload_interval = g_settings->getS32("max_clearobjects_extra_loaded_blocks");
-       unload_interval = MYMAX(unload_interval, 1);
+       u32 unload_interval = U32_MAX;
+       if (mode == CLEAR_OBJECTS_MODE_FULL) {
+               unload_interval = g_settings->getS32("max_clearobjects_extra_loaded_blocks");
+               unload_interval = MYMAX(unload_interval, 1);
+       }
        u32 report_interval = loadable_blocks.size() / 10;
        u32 num_blocks_checked = 0;
        u32 num_blocks_cleared = 0;
        u32 num_objs_cleared = 0;
-       for(std::vector<v3s16>::iterator i = loadable_blocks.begin();
+       for (std::vector<v3s16>::iterator i = loadable_blocks.begin();
                        i != loadable_blocks.end(); ++i) {
                v3s16 p = *i;
                MapBlock *block = m_map->emergeBlock(p, false);
-               if(!block){
-                       errorstream<<"ServerEnvironment::clearAllObjects(): "
-                                       <<"Failed to emerge block "<<PP(p)<<std::endl;
+               if (!block) {
+                       errorstream << "ServerEnvironment::clearObjects(): "
+                               << "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){
+               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,
-                                       "clearAllObjects");
+                               MOD_REASON_CLEAR_ALL_OBJECTS);
                        num_objs_cleared += num_stored + num_active;
                        num_blocks_cleared++;
                }
                num_blocks_checked++;
 
-               if(report_interval != 0 &&
-                               num_blocks_checked % report_interval == 0){
+               if (report_interval != 0 &&
+                               num_blocks_checked % report_interval == 0) {
                        float percent = 100.0 * (float)num_blocks_checked /
-                                       loadable_blocks.size();
-                       infostream<<"ServerEnvironment::clearAllObjects(): "
-                                       <<"Cleared "<<num_objs_cleared<<" objects"
-                                       <<" in "<<num_blocks_cleared<<" blocks ("
-                                       <<percent<<"%)"<<std::endl;
+                               loadable_blocks.size();
+                       infostream << "ServerEnvironment::clearObjects(): "
+                               << "Cleared " << num_objs_cleared << " objects"
+                               << " in " << num_blocks_cleared << " blocks ("
+                               << percent << "%)" << std::endl;
                }
-               if(num_blocks_checked % unload_interval == 0){
+               if (num_blocks_checked % unload_interval == 0) {
                        m_map->unloadUnreferencedBlocks();
                }
        }
        m_map->unloadUnreferencedBlocks();
 
        // Drop references that were added above
-       for(std::vector<v3s16>::iterator i = loaded_blocks.begin();
+       for (std::vector<v3s16>::iterator i = loaded_blocks.begin();
                        i != loaded_blocks.end(); ++i) {
                v3s16 p = *i;
                MapBlock *block = m_map->getBlockNoCreateNoEx(p);
@@ -982,15 +1010,17 @@ void ServerEnvironment::clearAllObjects()
                block->refDrop();
        }
 
-       infostream<<"ServerEnvironment::clearAllObjects(): "
-                       <<"Finished: Cleared "<<num_objs_cleared<<" objects"
-                       <<" in "<<num_blocks_cleared<<" blocks"<<std::endl;
+       m_last_clear_objects_time = m_game_time;
+
+       infostream << "ServerEnvironment::clearObjects(): "
+               << "Finished: Cleared " << num_objs_cleared << " objects"
+               << " in " << num_blocks_cleared << " blocks" << std::endl;
 }
 
 void ServerEnvironment::step(float dtime)
 {
-       DSTACK(__FUNCTION_NAME);
-       
+       DSTACK(FUNCTION_NAME);
+
        //TimeTaker timer("ServerEnv step");
 
        /* Step time of day */
@@ -1010,7 +1040,7 @@ void ServerEnvironment::step(float dtime)
                m_game_time += inc_i;
                m_game_time_fraction_counter -= (float)inc_i;
        }
-       
+
        /*
                Handle players
        */
@@ -1020,11 +1050,11 @@ void ServerEnvironment::step(float dtime)
                                i != m_players.end(); ++i)
                {
                        Player *player = *i;
-                       
+
                        // Ignore disconnected players
                        if(player->peer_id == 0)
                                continue;
-                       
+
                        // Move
                        player->move(dtime, this, 100*BS);
                }
@@ -1052,7 +1082,7 @@ void ServerEnvironment::step(float dtime)
                                        floatToInt(player->getPosition(), BS));
                        players_blockpos.push_back(blockpos);
                }
-               
+
                /*
                        Update list of active blocks, collecting changes
                */
@@ -1068,7 +1098,7 @@ void ServerEnvironment::step(float dtime)
 
                // Convert active objects that are no more in active blocks to static
                deactivateFarObjects(false);
-               
+
                for(std::set<v3s16>::iterator
                                i = blocks_removed.begin();
                                i != blocks_removed.end(); ++i)
@@ -1077,11 +1107,11 @@ void ServerEnvironment::step(float dtime)
 
                        /* infostream<<"Server: Block " << PP(p)
                                << " became inactive"<<std::endl; */
-                       
+
                        MapBlock *block = m_map->getBlockNoCreateNoEx(p);
                        if(block==NULL)
                                continue;
-                       
+
                        // Set current time as timestamp (and let it set ChangedFlag)
                        block->setTimestamp(m_game_time);
                }
@@ -1114,7 +1144,7 @@ void ServerEnvironment::step(float dtime)
        if(m_active_blocks_nodemetadata_interval.step(dtime, 1.0))
        {
                ScopeProfiler sp(g_profiler, "SEnv: mess in act. blocks avg /1s", SPT_AVG);
-               
+
                float dtime = 1.0;
 
                for(std::set<v3s16>::iterator
@@ -1122,7 +1152,7 @@ void ServerEnvironment::step(float dtime)
                                i != m_active_blocks.m_list.end(); ++i)
                {
                        v3s16 p = *i;
-                       
+
                        /*infostream<<"Server: Block ("<<p.X<<","<<p.Y<<","<<p.Z
                                        <<") being handled"<<std::endl;*/
 
@@ -1132,14 +1162,14 @@ void ServerEnvironment::step(float dtime)
 
                        // Reset block usage timer
                        block->resetUsageTimer();
-                       
+
                        // Set current time as timestamp
                        block->setTimestampNoChangedFlag(m_game_time);
                        // If time has changed much from the one on disk,
                        // set block to be saved when it is unloaded
                        if(block->getTimestamp() > block->getDiskTimestamp() + 60)
                                block->raiseModified(MOD_STATE_WRITE_AT_UNLOAD,
-                                               "Timestamp older than 60s (step)");
+                                       MOD_REASON_BLOCK_EXPIRED);
 
                        // Run node timers
                        std::map<v3s16, NodeTimer> elapsed_timers =
@@ -1148,7 +1178,7 @@ void ServerEnvironment::step(float dtime)
                                MapNode n;
                                for(std::map<v3s16, NodeTimer>::iterator
                                                i = elapsed_timers.begin();
-                                               i != elapsed_timers.end(); i++){
+                                               i != elapsed_timers.end(); ++i){
                                        n = block->getNodeNoEx(i->first);
                                        p = i->first + block->getPosRelative();
                                        if(m_script->node_on_timer(p,n,i->second.elapsed))
@@ -1157,7 +1187,7 @@ void ServerEnvironment::step(float dtime)
                        }
                }
        }
-       
+
        const float abm_interval = 1.0;
        if(m_active_block_modifier_interval.step(dtime, abm_interval))
        do{ // breakable
@@ -1168,7 +1198,7 @@ void ServerEnvironment::step(float dtime)
                }
                ScopeProfiler sp(g_profiler, "SEnv: modify in blocks avg /1s", SPT_AVG);
                TimeTaker timer("modify in active blocks");
-               
+
                // Initialize handling of ActiveBlockModifiers
                ABMHandler abmhandler(m_abms, abm_interval, this, true);
 
@@ -1177,14 +1207,14 @@ void ServerEnvironment::step(float dtime)
                                i != m_active_blocks.m_list.end(); ++i)
                {
                        v3s16 p = *i;
-                       
+
                        /*infostream<<"Server: Block ("<<p.X<<","<<p.Y<<","<<p.Z
                                        <<") being handled"<<std::endl;*/
 
                        MapBlock *block = m_map->getBlockNoCreateNoEx(p);
-                       if(block==NULL)
+                       if(block == NULL)
                                continue;
-                       
+
                        // Set current time as timestamp
                        block->setTimestampNoChangedFlag(m_game_time);
 
@@ -1195,13 +1225,13 @@ void ServerEnvironment::step(float dtime)
                u32 time_ms = timer.stop(true);
                u32 max_time_ms = 200;
                if(time_ms > max_time_ms){
-                       infostream<<"WARNING: active block modifiers took "
+                       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;
                }
        }while(0);
-       
+
        /*
                Step script environment (run global on_step())
        */
@@ -1215,7 +1245,7 @@ void ServerEnvironment::step(float dtime)
                //TimeTaker timer("Step active objects");
 
                g_profiler->avg("SEnv: num of objects", m_active_objects.size());
-               
+
                // This helps the objects to send data at the same time
                bool send_recommended = false;
                m_send_recommended_timer += dtime;
@@ -1238,12 +1268,13 @@ void ServerEnvironment::step(float dtime)
                        // Read messages from object
                        while(!obj->m_messages_out.empty())
                        {
-                               m_active_object_messages.push_back(
-                                               obj->m_messages_out.pop_front());
+                               m_active_object_messages.push(
+                                               obj->m_messages_out.front());
+                               obj->m_messages_out.pop();
                        }
                }
        }
-       
+
        /*
                Manage active objects
        */
@@ -1286,7 +1317,7 @@ u16 getFreeServerActiveObjectId(
                last_used_id ++;
                if(isFreeServerActiveObjectId(last_used_id, objects))
                        return last_used_id;
-               
+
                if(last_used_id == startid)
                        return 0;
        }
@@ -1294,67 +1325,21 @@ u16 getFreeServerActiveObjectId(
 
 u16 ServerEnvironment::addActiveObject(ServerActiveObject *object)
 {
-       assert(object);
+       assert(object); // Pre-condition
        m_added_objects++;
        u16 id = addActiveObjectRaw(object, true, 0);
        return id;
 }
 
-#if 0
-bool ServerEnvironment::addActiveObjectAsStatic(ServerActiveObject *obj)
-{
-       assert(obj);
-
-       v3f objectpos = obj->getBasePosition();
-
-       // The block in which the object resides in
-       v3s16 blockpos_o = getNodeBlockPos(floatToInt(objectpos, BS));
-
-       /*
-               Update the static data
-       */
-
-       // Create new static object
-       std::string staticdata = obj->getStaticData();
-       StaticObject s_obj(obj->getType(), objectpos, staticdata);
-       // Add to the block where the object is located in
-       v3s16 blockpos = getNodeBlockPos(floatToInt(objectpos, BS));
-       // Get or generate the block
-       MapBlock *block = m_map->emergeBlock(blockpos);
-
-       bool succeeded = false;
-
-       if(block)
-       {
-               block->m_static_objects.insert(0, s_obj);
-               block->raiseModified(MOD_STATE_WRITE_AT_UNLOAD,
-                               "addActiveObjectAsStatic");
-               succeeded = true;
-       }
-       else{
-               infostream<<"ServerEnvironment::addActiveObjectAsStatic: "
-                               <<"Could not find or generate "
-                               <<"a block for storing static object"<<std::endl;
-               succeeded = false;
-       }
-
-       if(obj->environmentDeletes())
-               delete obj;
-
-       return succeeded;
-}
-#endif
-
 /*
        Finds out what new objects have been added to
        inside a radius around a position
 */
-void ServerEnvironment::getAddedActiveObjects(v3s16 pos, s16 radius,
+void ServerEnvironment::getAddedActiveObjects(Player *player, s16 radius,
                s16 player_radius,
                std::set<u16> &current_objects,
-               std::set<u16> &added_objects)
+               std::queue<u16> &added_objects)
 {
-       v3f pos_f = intToFloat(pos, BS);
        f32 radius_f = radius * BS;
        f32 player_radius_f = player_radius * BS;
 
@@ -1370,18 +1355,19 @@ void ServerEnvironment::getAddedActiveObjects(v3s16 pos, s16 radius,
        */
        for(std::map<u16, ServerActiveObject*>::iterator
                        i = m_active_objects.begin();
-                       i != m_active_objects.end(); ++i)
-       {
+                       i != m_active_objects.end(); ++i) {
                u16 id = i->first;
+
                // Get object
                ServerActiveObject *object = i->second;
                if(object == NULL)
                        continue;
+
                // Discard if removed or deactivating
                if(object->m_removed || object->m_pending_deactivation)
                        continue;
 
-               f32 distance_f = object->getBasePosition().getDistanceFrom(pos_f);
+               f32 distance_f = object->getBasePosition().getDistanceFrom(player->getPosition());
                if (object->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
                        // Discard if too far
                        if (distance_f > player_radius_f && player_radius_f != 0)
@@ -1395,7 +1381,7 @@ void ServerEnvironment::getAddedActiveObjects(v3s16 pos, s16 radius,
                if(n != current_objects.end())
                        continue;
                // Add to added_objects
-               added_objects.insert(id);
+               added_objects.push(id);
        }
 }
 
@@ -1403,12 +1389,11 @@ void ServerEnvironment::getAddedActiveObjects(v3s16 pos, s16 radius,
        Finds out what objects have been removed from
        inside a radius around a position
 */
-void ServerEnvironment::getRemovedActiveObjects(v3s16 pos, s16 radius,
+void ServerEnvironment::getRemovedActiveObjects(Player *player, s16 radius,
                s16 player_radius,
                std::set<u16> &current_objects,
-               std::set<u16> &removed_objects)
+               std::queue<u16> &removed_objects)
 {
-       v3f pos_f = intToFloat(pos, BS);
        f32 radius_f = radius * BS;
        f32 player_radius_f = player_radius * BS;
 
@@ -1430,20 +1415,19 @@ void ServerEnvironment::getRemovedActiveObjects(v3s16 pos, s16 radius,
                u16 id = *i;
                ServerActiveObject *object = getActiveObject(id);
 
-               if(object == NULL){
-                       infostream<<"ServerEnvironment::getRemovedActiveObjects():"
-                                       <<" object in current_objects is NULL"<<std::endl;
-                       removed_objects.insert(id);
+               if (object == NULL) {
+                       infostream << "ServerEnvironment::getRemovedActiveObjects():"
+                               << " object in current_objects is NULL" << std::endl;
+                       removed_objects.push(id);
                        continue;
                }
 
-               if(object->m_removed || object->m_pending_deactivation)
-               {
-                       removed_objects.insert(id);
+               if (object->m_removed || object->m_pending_deactivation) {
+                       removed_objects.push(id);
                        continue;
                }
-               
-               f32 distance_f = object->getBasePosition().getDistanceFrom(pos_f);
+
+               f32 distance_f = object->getBasePosition().getDistanceFrom(player->getPosition());
                if (object->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
                        if (distance_f <= player_radius_f || player_radius_f == 0)
                                continue;
@@ -1451,7 +1435,34 @@ void ServerEnvironment::getRemovedActiveObjects(v3s16 pos, s16 radius,
                        continue;
 
                // Object is no longer visible
-               removed_objects.insert(id);
+               removed_objects.push(id);
+       }
+}
+
+void ServerEnvironment::setStaticForActiveObjectsInBlock(
+       v3s16 blockpos, bool static_exists, v3s16 static_block)
+{
+       MapBlock *block = m_map->getBlockNoCreateNoEx(blockpos);
+       if (!block)
+               return;
+
+       for (std::map<u16, StaticObject>::iterator
+                       so_it = block->m_static_objects.m_active.begin();
+                       so_it != block->m_static_objects.m_active.end(); ++so_it) {
+               // Get the ServerActiveObject counterpart to this StaticObject
+               std::map<u16, ServerActiveObject *>::iterator ao_it;
+               ao_it = m_active_objects.find(so_it->first);
+               if (ao_it == m_active_objects.end()) {
+                       // 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 "
+                               "in m_active_objects";
+                       continue;
+               }
+
+               ServerActiveObject *sao = ao_it->second;
+               sao->m_static_exists = static_exists;
+               sao->m_static_block  = static_block;
        }
 }
 
@@ -1459,9 +1470,9 @@ ActiveObjectMessage ServerEnvironment::getActiveObjectMessage()
 {
        if(m_active_object_messages.empty())
                return ActiveObjectMessage(0);
-       
+
        ActiveObjectMessage message = m_active_object_messages.front();
-       m_active_object_messages.pop_front();
+       m_active_object_messages.pop();
        return message;
 }
 
@@ -1472,7 +1483,7 @@ ActiveObjectMessage ServerEnvironment::getActiveObjectMessage()
 u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
                bool set_changed, u32 dtime_s)
 {
-       assert(object);
+       assert(object); // Pre-condition
        if(object->getId() == 0){
                u16 new_id = getFreeServerActiveObjectId(m_active_objects);
                if(new_id == 0)
@@ -1497,21 +1508,32 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
                        delete object;
                return 0;
        }
+
+       if (objectpos_over_limit(object->getBasePosition())) {
+               v3f p = object->getBasePosition();
+               errorstream << "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())
        {
@@ -1529,7 +1551,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
 
                        if(set_changed)
                                block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                                               "addActiveObjectRaw");
+                                       MOD_REASON_ADD_ACTIVE_OBJECT_RAW);
                } else {
                        v3s16 p = floatToInt(objectpos, BS);
                        errorstream<<"ServerEnvironment::addActiveObjectRaw(): "
@@ -1537,7 +1559,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
                                        <<" statically (pos="<<PP(p)<<")"<<std::endl;
                }
        }
-       
+
        return object->getId();
 }
 
@@ -1578,7 +1600,7 @@ void ServerEnvironment::removeRemovedObjects()
                        if (block) {
                                block->m_static_objects.remove(id);
                                block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                                               "removeRemovedObjects/remove");
+                                       MOD_REASON_REMOVE_OBJECTS_REMOVE);
                                obj->m_static_exists = false;
                        } else {
                                infostream<<"Failed to emerge block from which an object to "
@@ -1603,7 +1625,7 @@ void ServerEnvironment::removeRemovedObjects()
                                        block->m_static_objects.m_stored.push_back(i->second);
                                        block->m_static_objects.m_active.erase(id);
                                        block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                                                       "removeRemovedObjects/deactivate");
+                                               MOD_REASON_REMOVE_OBJECTS_DEACTIVATE);
                                }
                        } else {
                                infostream<<"Failed to emerge block from which an object to "
@@ -1675,12 +1697,13 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s)
        // Ignore if no stored objects (to not set changed flag)
        if(block->m_static_objects.m_stored.empty())
                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){
+       if (large_amount) {
                errorstream<<"suspiciously large amount of objects detected: "
                                <<block->m_static_objects.m_stored.size()<<" in "
                                <<PP(block->getPos())
@@ -1688,14 +1711,13 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s)
                // Clear stored list
                block->m_static_objects.m_stored.clear();
                block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                               "stored list cleared in activateObjects due to "
-                               "large amount of objects");
+                       MOD_REASON_TOO_MANY_OBJECTS);
                return;
        }
 
        // Activate stored objects
        std::vector<StaticObject> new_stored;
-       for(std::list<StaticObject>::iterator
+       for (std::vector<StaticObject>::iterator
                        i = block->m_static_objects.m_stored.begin();
                        i != block->m_static_objects.m_stored.end(); ++i) {
                StaticObject &s_obj = *i;
@@ -1710,7 +1732,7 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s)
                                        <<"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;
                }
@@ -1771,7 +1793,7 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
                        i != m_active_objects.end(); ++i) {
                ServerActiveObject* obj = i->second;
                assert(obj);
-               
+
                // Do not deactivate if static data creation not allowed
                if(!force_delete && !obj->isStaticAllowed())
                        continue;
@@ -1810,7 +1832,7 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
                        block->m_static_objects.insert(id, s_obj);
                        obj->m_static_block = blockpos_o;
                        block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                                       "deactivateFarObjects: Static data moved in");
+                               MOD_REASON_STATIC_DATA_ADDED);
 
                        // Delete from block where object was located
                        block = m_map->emergeBlock(old_static_block, false);
@@ -1823,7 +1845,7 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
                        }
                        block->m_static_objects.remove(id);
                        block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                                       "deactivateFarObjects: Static data moved out");
+                               MOD_REASON_STATIC_DATA_REMOVED);
                        continue;
                }
 
@@ -1847,36 +1869,38 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
                        // Create new static object
                        std::string staticdata_new = obj->getStaticData();
                        StaticObject s_obj(obj->getType(), objectpos, staticdata_new);
-                       
+
                        bool stays_in_same_block = false;
                        bool data_changed = true;
 
-                       if(obj->m_static_exists){
-                               if(obj->m_static_block == blockpos_o)
+                       if (obj->m_static_exists) {
+                               if (obj->m_static_block == blockpos_o)
                                        stays_in_same_block = true;
 
                                MapBlock *block = m_map->emergeBlock(obj->m_static_block, false);
-                               
-                               std::map<u16, StaticObject>::iterator n =
+
+                               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()){
-                                       StaticObject static_old = n->second;
+                                       if (n != block->m_static_objects.m_active.end()) {
+                                               StaticObject static_old = n->second;
 
-                                       float save_movem = obj->getMinimumSavedMovement();
+                                               float save_movem = obj->getMinimumSavedMovement();
 
-                                       if(static_old.data == staticdata_new &&
-                                                       (static_old.pos - objectpos).getLength() < save_movem)
-                                               data_changed = false;
-                               } else {
-                                       errorstream<<"ServerEnvironment::deactivateFarObjects(): "
+                                               if (static_old.data == staticdata_new &&
+                                                               (static_old.pos - objectpos).getLength() < save_movem)
+                                                       data_changed = false;
+                                       } else {
+                                               errorstream<<"ServerEnvironment::deactivateFarObjects(): "
                                                        <<"id="<<id<<" m_static_exists=true but "
                                                        <<"static data doesn't actually exist in "
                                                        <<PP(obj->m_static_block)<<std::endl;
+                                       }
                                }
                        }
 
                        bool shall_be_written = (!stays_in_same_block || data_changed);
-                       
+
                        // Delete old static object
                        if(obj->m_static_exists)
                        {
@@ -1888,8 +1912,7 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
                                        // Only mark block as modified if data changed considerably
                                        if(shall_be_written)
                                                block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                                                               "deactivateFarObjects: Static data "
-                                                               "changed considerably");
+                                                       MOD_REASON_STATIC_DATA_CHANGED);
                                }
                        }
 
@@ -1924,20 +1947,19 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
                                        // reason. Unsuccessful attempts have been made to find
                                        // said reason.
                                        if(id && block->m_static_objects.m_active.find(id) != block->m_static_objects.m_active.end()){
-                                               infostream<<"ServerEnv: WARNING: Performing hack #83274"
+                                               warningstream<<"ServerEnv: Performing hack #83274"
                                                                <<std::endl;
                                                block->m_static_objects.remove(id);
                                        }
                                        // Store static data
                                        u16 store_id = pending_delete ? id : 0;
                                        block->m_static_objects.insert(store_id, s_obj);
-                                       
+
                                        // Only mark block as modified if data changed considerably
                                        if(shall_be_written)
                                                block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                                                               "deactivateFarObjects: Static data "
-                                                               "changed considerably");
-                                       
+                                                       MOD_REASON_STATIC_DATA_CHANGED);
+
                                        obj->m_static_exists = true;
                                        obj->m_static_block = block->getPos();
                                }
@@ -1967,7 +1989,7 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
                        obj->m_pending_deactivation = true;
                        continue;
                }
-               
+
                verbosestream<<"ServerEnvironment::deactivateFarObjects(): "
                                <<"object id="<<id<<" is not known by clients"
                                <<"; deleting"<<std::endl;
@@ -1991,7 +2013,6 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
        }
 }
 
-
 #ifndef SERVER
 
 #include "clientsimpleobject.h"
@@ -2010,7 +2031,7 @@ ClientEnvironment::ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr,
        m_irr(irr)
 {
        char zero = 0;
-       memset(m_attachements, zero, sizeof(m_attachements));
+       memset(attachement_parent_ids, zero, sizeof(attachement_parent_ids));
 }
 
 ClientEnvironment::~ClientEnvironment()
@@ -2023,9 +2044,8 @@ ClientEnvironment::~ClientEnvironment()
                delete i->second;
        }
 
-       for(std::list<ClientSimpleObject*>::iterator
-                       i = m_simple_objects.begin(); i != m_simple_objects.end(); ++i)
-       {
+       for(std::vector<ClientSimpleObject*>::iterator
+                       i = m_simple_objects.begin(); i != m_simple_objects.end(); ++i) {
                delete *i;
        }
 
@@ -2045,12 +2065,13 @@ ClientMap & ClientEnvironment::getClientMap()
 
 void ClientEnvironment::addPlayer(Player *player)
 {
-       DSTACK(__FUNCTION_NAME);
+       DSTACK(FUNCTION_NAME);
        /*
                It is a failure if player is local and there already is a local
                player
        */
-       assert(!(player->isLocal() == true && getLocalPlayer() != NULL));
+       FATAL_ERROR_IF(player->isLocal() == true && getLocalPlayer() != NULL,
+               "Player is local but there is already a local player");
 
        Environment::addPlayer(player);
 }
@@ -2068,7 +2089,7 @@ LocalPlayer * ClientEnvironment::getLocalPlayer()
 
 void ClientEnvironment::step(float dtime)
 {
-       DSTACK(__FUNCTION_NAME);
+       DSTACK(FUNCTION_NAME);
 
        /* Step time of day */
        stepTimeOfDay(dtime);
@@ -2081,15 +2102,15 @@ void ClientEnvironment::step(float dtime)
        LocalPlayer *lplayer = getLocalPlayer();
        assert(lplayer);
        // collision info queue
-       std::list<CollisionInfo> player_collisions;
-       
+       std::vector<CollisionInfo> player_collisions;
+
        /*
                Get the speed the player is going
        */
        bool is_climbing = lplayer->is_climbing;
-       
+
        f32 player_speed = lplayer->getSpeed().getLength();
-       
+
        /*
                Maximum position increment
        */
@@ -2101,15 +2122,15 @@ void ClientEnvironment::step(float dtime)
        f32 dtime_max_increment = 1;
        if(player_speed > 0.001)
                dtime_max_increment = position_max_increment / player_speed;
-       
+
        // Maximum time increment is 10ms or lower
        if(dtime_max_increment > 0.01)
                dtime_max_increment = 0.01;
-       
+
        // Don't allow overly huge dtime
        if(dtime > 0.5)
                dtime = 0.5;
-       
+
        f32 dtime_downcount = dtime;
 
        /*
@@ -2137,11 +2158,11 @@ void ClientEnvironment::step(float dtime)
                        */
                        dtime_downcount = 0;
                }
-               
+
                /*
                        Handle local player
                */
-               
+
                {
                        // Apply physics
                        if(free_move == false && is_climbing == false)
@@ -2167,18 +2188,9 @@ void ClientEnvironment::step(float dtime)
                                        if(dl > lplayer->movement_liquid_fluidity_smooth)
                                                dl = lplayer->movement_liquid_fluidity_smooth;
                                        dl *= (lplayer->liquid_viscosity * viscosity_factor) + (1 - viscosity_factor);
-                                       
+
                                        v3f d = d_wanted.normalize() * dl;
                                        speed += d;
-                                       
-#if 0 // old code
-                                       if(speed.X > lplayer->movement_liquid_fluidity + lplayer->movement_liquid_fluidity_smooth)      speed.X -= lplayer->movement_liquid_fluidity_smooth;
-                                       if(speed.X < -lplayer->movement_liquid_fluidity - lplayer->movement_liquid_fluidity_smooth)     speed.X += lplayer->movement_liquid_fluidity_smooth;
-                                       if(speed.Y > lplayer->movement_liquid_fluidity + lplayer->movement_liquid_fluidity_smooth)      speed.Y -= lplayer->movement_liquid_fluidity_smooth;
-                                       if(speed.Y < -lplayer->movement_liquid_fluidity - lplayer->movement_liquid_fluidity_smooth)     speed.Y += lplayer->movement_liquid_fluidity_smooth;
-                                       if(speed.Z > lplayer->movement_liquid_fluidity + lplayer->movement_liquid_fluidity_smooth)      speed.Z -= lplayer->movement_liquid_fluidity_smooth;
-                                       if(speed.Z < -lplayer->movement_liquid_fluidity - lplayer->movement_liquid_fluidity_smooth)     speed.Z += lplayer->movement_liquid_fluidity_smooth;
-#endif
                                }
 
                                lplayer->setSpeed(speed);
@@ -2193,13 +2205,11 @@ void ClientEnvironment::step(float dtime)
                }
        }
        while(dtime_downcount > 0.001);
-               
+
        //std::cout<<"Looped "<<loopcount<<" times."<<std::endl;
-       
-       for(std::list<CollisionInfo>::iterator
-                       i = player_collisions.begin();
-                       i != player_collisions.end(); ++i)
-       {
+
+       for(std::vector<CollisionInfo>::iterator i = player_collisions.begin();
+                       i != player_collisions.end(); ++i) {
                CollisionInfo &info = *i;
                v3f speed_diff = info.new_speed - info.old_speed;;
                // Handle only fall damage
@@ -2232,14 +2242,14 @@ void ClientEnvironment::step(float dtime)
                        }
                }
        }
-       
+
        /*
                A quick draft of lava damage
        */
        if(m_lava_hurt_interval.step(dtime, 1.0))
        {
                v3f pf = lplayer->getPosition();
-               
+
                // Feet, middle and head
                v3s16 p1 = floatToInt(pf + v3f(0, BS*0.1, 0), BS);
                MapNode n1 = m_map->getNodeNoEx(p1);
@@ -2255,7 +2265,7 @@ void ClientEnvironment::step(float dtime)
                                m_gamedef->ndef()->get(n2).damage_per_second);
                damage_per_second = MYMAX(damage_per_second,
                                m_gamedef->ndef()->get(n3).damage_per_second);
-               
+
                if(damage_per_second != 0)
                {
                        damageLocalPlayer(damage_per_second, true);
@@ -2317,7 +2327,7 @@ void ClientEnvironment::step(float dtime)
        for(std::vector<Player*>::iterator i = m_players.begin();
                        i != m_players.end(); ++i) {
                Player *player = *i;
-               
+
                /*
                        Handle non-local players
                */
@@ -2349,7 +2359,7 @@ void ClientEnvironment::step(float dtime)
        /*
                Step active objects and update lighting of them
        */
-       
+
        g_profiler->avg("CEnv: num of objects", m_active_objects.size());
        bool update_lighting = m_active_object_light_update_interval.step(dtime, 0.21);
        for(std::map<u16, ClientActiveObject*>::iterator
@@ -2382,25 +2392,36 @@ void ClientEnvironment::step(float dtime)
                Step and handle simple objects
        */
        g_profiler->avg("CEnv: num of simple objects", m_simple_objects.size());
-       for(std::list<ClientSimpleObject*>::iterator
-                       i = m_simple_objects.begin(); i != m_simple_objects.end();)
-       {
-               ClientSimpleObject *simple = *i;
-               std::list<ClientSimpleObject*>::iterator cur = i;
-               ++i;
+       for(std::vector<ClientSimpleObject*>::iterator
+                       i = m_simple_objects.begin(); i != m_simple_objects.end();) {
+               std::vector<ClientSimpleObject*>::iterator cur = i;
+               ClientSimpleObject *simple = *cur;
+
                simple->step(dtime);
-               if(simple->m_to_be_removed){
+               if(simple->m_to_be_removed) {
                        delete simple;
-                       m_simple_objects.erase(cur);
+                       i = m_simple_objects.erase(cur);
+               }
+               else {
+                       ++i;
                }
        }
 }
-       
+
 void ClientEnvironment::addSimpleObject(ClientSimpleObject *simple)
 {
        m_simple_objects.push_back(simple);
 }
 
+GenericCAO* ClientEnvironment::getGenericCAO(u16 id)
+{
+       ClientActiveObject *obj = getActiveObject(id);
+       if (obj && obj->getType() == ACTIVEOBJECT_TYPE_GENERIC)
+               return (GenericCAO*) obj;
+       else
+               return NULL;
+}
+
 ClientActiveObject* ClientEnvironment::getActiveObject(u16 id)
 {
        std::map<u16, ClientActiveObject*>::iterator n;
@@ -2430,7 +2451,7 @@ u16 getFreeClientActiveObjectId(
                last_used_id ++;
                if(isFreeClientActiveObjectId(last_used_id, objects))
                        return last_used_id;
-               
+
                if(last_used_id == startid)
                        return 0;
        }
@@ -2438,7 +2459,7 @@ u16 getFreeClientActiveObjectId(
 
 u16 ClientEnvironment::addActiveObject(ClientActiveObject *object)
 {
-       assert(object);
+       assert(object); // Pre-condition
        if(object->getId() == 0)
        {
                u16 new_id = getFreeClientActiveObjectId(m_active_objects);
@@ -2491,7 +2512,7 @@ void ClientEnvironment::addActiveObject(u16 id, u8 type,
                                <<std::endl;
                return;
        }
-       
+
        obj->setId(id);
 
        try
@@ -2527,28 +2548,23 @@ void ClientEnvironment::removeActiveObject(u16 id)
        m_active_objects.erase(id);
 }
 
-void ClientEnvironment::processActiveObjectMessage(u16 id,
-               const std::string &data)
+void ClientEnvironment::processActiveObjectMessage(u16 id, const std::string &data)
 {
-       ClientActiveObject* obj = getActiveObject(id);
-       if(obj == NULL)
-       {
-               infostream<<"ClientEnvironment::processActiveObjectMessage():"
-                               <<" got message for id="<<id<<", which doesn't exist."
-                               <<std::endl;
+       ClientActiveObject *obj = getActiveObject(id);
+       if (obj == NULL) {
+               infostream << "ClientEnvironment::processActiveObjectMessage():"
+                       << " got message for id=" << id << ", which doesn't exist."
+                       << std::endl;
                return;
        }
-       try
-       {
+
+       try {
                obj->processMessage(data);
-       }
-       catch(SerializationError &e)
-       {
+       } catch (SerializationError &e) {
                errorstream<<"ClientEnvironment::processActiveObjectMessage():"
-                               <<" id="<<id<<" type="<<obj->getType()
-                               <<" SerializationError in processMessage(),"
-                               <<" message="<<serializeJsonString(data)
-                               <<std::endl;
+                       << " id=" << id << " type=" << obj->getType()
+                       << " SerializationError in processMessage(): " << e.what()
+                       << std::endl;
        }
 }
 
@@ -2560,11 +2576,9 @@ void ClientEnvironment::damageLocalPlayer(u8 damage, bool handle_hp)
 {
        LocalPlayer *lplayer = getLocalPlayer();
        assert(lplayer);
-       
-       if(handle_hp){
-               if (lplayer->hp == 0) // Don't damage a dead player
-                       return;
-               if(lplayer->hp > damage)
+
+       if (handle_hp) {
+               if (lplayer->hp > damage)
                        lplayer->hp -= damage;
                else
                        lplayer->hp = 0;
@@ -2574,7 +2588,7 @@ void ClientEnvironment::damageLocalPlayer(u8 damage, bool handle_hp)
        event.type = CEE_PLAYER_DAMAGE;
        event.player_damage.amount = damage;
        event.player_damage.send_to_server = handle_hp;
-       m_client_event_queue.push_back(event);
+       m_client_event_queue.push(event);
 }
 
 void ClientEnvironment::updateLocalPlayerBreath(u16 breath)
@@ -2582,13 +2596,13 @@ void ClientEnvironment::updateLocalPlayerBreath(u16 breath)
        ClientEnvEvent event;
        event.type = CEE_PLAYER_BREATH;
        event.player_breath.amount = breath;
-       m_client_event_queue.push_back(event);
+       m_client_event_queue.push(event);
 }
 
 /*
        Client likes to call these
 */
-       
+
 void ClientEnvironment::getActiveObjects(v3f origin, f32 max_d,
                std::vector<DistanceSortedActiveObject> &dest)
 {
@@ -2616,7 +2630,7 @@ ClientEnvEvent ClientEnvironment::getClientEvent()
                event.type = CEE_NONE;
        else {
                event = m_client_event_queue.front();
-               m_client_event_queue.pop_front();
+               m_client_event_queue.pop();
        }
        return event;
 }