]> git.lizzy.rs Git - minetest.git/blobdiff - src/map.cpp
Add Client::getEnv() and remove some unnecessary wrappers
[minetest.git] / src / map.cpp
index 6ba33288d7a07c20f88dcdb84da96e8e6af40d85..03a842e7425bb3ae9e4f3014ad79eb9f7f0d25ce 100644 (file)
@@ -21,22 +21,22 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "mapsector.h"
 #include "mapblock.h"
 #include "main.h"
-#ifndef SERVER
-#include "client.h"
-#endif
 #include "filesys.h"
 #include "utility.h"
 #include "voxel.h"
 #include "porting.h"
 #include "mapgen.h"
 #include "nodemetadata.h"
-#include "content_mapnode.h"
-#ifndef SERVER
-#include <IMaterialRenderer.h>
-#endif
 #include "settings.h"
 #include "log.h"
 #include "profiler.h"
+#include "nodedef.h"
+#include "gamedef.h"
+#ifndef SERVER
+#include "client.h"
+#include "mapblock_mesh.h"
+#include <IMaterialRenderer.h>
+#endif
 
 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
 
@@ -61,8 +61,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
        Map
 */
 
-Map::Map(std::ostream &dout):
+Map::Map(std::ostream &dout, IGameDef *gamedef):
        m_dout(dout),
+       m_gamedef(gamedef),
        m_sector_cache(NULL)
 {
        /*m_sector_mutex.Init();
@@ -206,6 +207,15 @@ void Map::setNode(v3s16 p, MapNode & n)
        v3s16 blockpos = getNodeBlockPos(p);
        MapBlock *block = getBlockNoCreate(blockpos);
        v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
+       // Never allow placing CONTENT_IGNORE, it fucks up stuff
+       if(n.getContent() == CONTENT_IGNORE){
+               errorstream<<"Map::setNode(): Not allowing to place CONTENT_IGNORE"
+                               <<" while trying to replace \""
+                               <<m_gamedef->ndef()->get(block->getNodeNoCheck(relpos)).name
+                               <<"\" at "<<PP(p)<<" (block "<<PP(blockpos)<<")"<<std::endl;
+               debug_stacks_print_to(infostream);
+               return;
+       }
        block->setNodeNoCheck(relpos, n);
 }
 
@@ -232,6 +242,8 @@ void Map::unspreadLight(enum LightBank bank,
                core::map<v3s16, bool> & light_sources,
                core::map<v3s16, MapBlock*>  & modified_blocks)
 {
+       INodeDefManager *nodemgr = m_gamedef->ndef();
+
        v3s16 dirs[6] = {
                v3s16(0,0,1), // back
                v3s16(0,1,0), // top
@@ -328,19 +340,20 @@ void Map::unspreadLight(enum LightBank bank,
                                        If the neighbor is dimmer than what was specified
                                        as oldlight (the light of the previous node)
                                */
-                               if(n2.getLight(bank) < oldlight)
+                               if(n2.getLight(bank, nodemgr) < oldlight)
                                {
                                        /*
                                                And the neighbor is transparent and it has some light
                                        */
-                                       if(n2.light_propagates() && n2.getLight(bank) != 0)
+                                       if(nodemgr->get(n2).light_propagates
+                                                       && n2.getLight(bank, nodemgr) != 0)
                                        {
                                                /*
                                                        Set light to 0 and add to queue
                                                */
 
-                                               u8 current_light = n2.getLight(bank);
-                                               n2.setLight(bank, 0);
+                                               u8 current_light = n2.getLight(bank, nodemgr);
+                                               n2.setLight(bank, 0, nodemgr);
                                                block->setNode(relpos, n2);
 
                                                unlighted_nodes.insert(n2pos, current_light);
@@ -414,6 +427,8 @@ void Map::spreadLight(enum LightBank bank,
                core::map<v3s16, bool> & from_nodes,
                core::map<v3s16, MapBlock*> & modified_blocks)
 {
+       INodeDefManager *nodemgr = m_gamedef->ndef();
+
        const v3s16 dirs[6] = {
                v3s16(0,0,1), // back
                v3s16(0,1,0), // top
@@ -472,7 +487,7 @@ void Map::spreadLight(enum LightBank bank,
                // Get node straight from the block
                MapNode n = block->getNode(relpos);
 
-               u8 oldlight = n.getLight(bank);
+               u8 oldlight = n.getLight(bank, nodemgr);
                u8 newlight = diminish_light(oldlight);
 
                // Loop through 6 neighbors
@@ -510,7 +525,7 @@ void Map::spreadLight(enum LightBank bank,
                                        If the neighbor is brighter than the current node,
                                        add to list (it will light up this node on its turn)
                                */
-                               if(n2.getLight(bank) > undiminish_light(oldlight))
+                               if(n2.getLight(bank, nodemgr) > undiminish_light(oldlight))
                                {
                                        lighted_nodes.insert(n2pos, true);
                                        //lighted_nodes.push_back(n2pos);
@@ -520,11 +535,11 @@ void Map::spreadLight(enum LightBank bank,
                                        If the neighbor is dimmer than how much light this node
                                        would spread on it, add to list
                                */
-                               if(n2.getLight(bank) < newlight)
+                               if(n2.getLight(bank, nodemgr) < newlight)
                                {
-                                       if(n2.light_propagates())
+                                       if(nodemgr->get(n2).light_propagates)
                                        {
-                                               n2.setLight(bank, newlight);
+                                               n2.setLight(bank, newlight, nodemgr);
                                                block->setNode(relpos, n2);
                                                lighted_nodes.insert(n2pos, true);
                                                //lighted_nodes.push_back(n2pos);
@@ -573,6 +588,8 @@ void Map::lightNeighbors(enum LightBank bank,
 
 v3s16 Map::getBrightestNeighbour(enum LightBank bank, v3s16 p)
 {
+       INodeDefManager *nodemgr = m_gamedef->ndef();
+
        v3s16 dirs[6] = {
                v3s16(0,0,1), // back
                v3s16(0,1,0), // top
@@ -598,8 +615,8 @@ v3s16 Map::getBrightestNeighbour(enum LightBank bank, v3s16 p)
                {
                        continue;
                }
-               if(n2.getLight(bank) > brightest_light || found_something == false){
-                       brightest_light = n2.getLight(bank);
+               if(n2.getLight(bank, nodemgr) > brightest_light || found_something == false){
+                       brightest_light = n2.getLight(bank, nodemgr);
                        brightest_pos = n2pos;
                        found_something = true;
                }
@@ -622,6 +639,8 @@ v3s16 Map::getBrightestNeighbour(enum LightBank bank, v3s16 p)
 s16 Map::propagateSunlight(v3s16 start,
                core::map<v3s16, MapBlock*> & modified_blocks)
 {
+       INodeDefManager *nodemgr = m_gamedef->ndef();
+
        s16 y = start.Y;
        for(; ; y--)
        {
@@ -640,23 +659,15 @@ s16 Map::propagateSunlight(v3s16 start,
                v3s16 relpos = pos - blockpos*MAP_BLOCKSIZE;
                MapNode n = block->getNode(relpos);
 
-               if(n.sunlight_propagates())
+               if(nodemgr->get(n).sunlight_propagates)
                {
-                       n.setLight(LIGHTBANK_DAY, LIGHT_SUN);
+                       n.setLight(LIGHTBANK_DAY, LIGHT_SUN, nodemgr);
                        block->setNode(relpos, n);
 
                        modified_blocks.insert(blockpos, block);
                }
                else
                {
-                       /*// Turn mud into grass
-                       if(n.getContent() == CONTENT_MUD)
-                       {
-                               n.setContent(CONTENT_GRASS);
-                               block->setNode(relpos, n);
-                               modified_blocks.insert(blockpos, block);
-                       }*/
-
                        // Sunlight goes no further
                        break;
                }
@@ -668,6 +679,8 @@ void Map::updateLighting(enum LightBank bank,
                core::map<v3s16, MapBlock*> & a_blocks,
                core::map<v3s16, MapBlock*> & modified_blocks)
 {
+       INodeDefManager *nodemgr = m_gamedef->ndef();
+
        /*m_dout<<DTIME<<"Map::updateLighting(): "
                        <<a_blocks.size()<<" blocks."<<std::endl;*/
 
@@ -711,8 +724,8 @@ void Map::updateLighting(enum LightBank bank,
                                try{
                                        v3s16 p(x,y,z);
                                        MapNode n = block->getNode(v3s16(x,y,z));
-                                       u8 oldlight = n.getLight(bank);
-                                       n.setLight(bank, 0);
+                                       u8 oldlight = n.getLight(bank, nodemgr);
+                                       n.setLight(bank, 0, nodemgr);
                                        block->setNode(v3s16(x,y,z), n);
 
                                        // Collect borders for unlighting
@@ -846,15 +859,15 @@ void Map::updateLighting(enum LightBank bank,
                        for(s16 y=-1; y<=1; y++)
                        for(s16 x=-1; x<=1; x++)
                        {
-                               v3s16 p(x,y,z);
-                               MapBlock *block = getBlockNoCreateNoEx(p);
+                               v3s16 p2 = p + v3s16(x,y,z);
+                               MapBlock *block = getBlockNoCreateNoEx(p2);
                                if(block == NULL)
                                        continue;
                                if(block->isDummy())
                                        continue;
                                if(block->getLightingExpired())
                                        continue;
-                               vmanip.initialEmerge(p, p);
+                               vmanip.initialEmerge(p2, p2);
                        }*/
 
                        // Lighting of block will be updated completely
@@ -863,11 +876,11 @@ void Map::updateLighting(enum LightBank bank,
 
                {
                        //TimeTaker timer("unSpreadLight");
-                       vmanip.unspreadLight(bank, unlight_from, light_sources);
+                       vmanip.unspreadLight(bank, unlight_from, light_sources, nodemgr);
                }
                {
                        //TimeTaker timer("spreadLight");
-                       vmanip.spreadLight(bank, light_sources);
+                       vmanip.spreadLight(bank, light_sources, nodemgr);
                }
                {
                        //TimeTaker timer("blitBack");
@@ -901,8 +914,10 @@ void Map::updateLighting(core::map<v3s16, MapBlock*> & a_blocks,
 /*
 */
 void Map::addNodeAndUpdate(v3s16 p, MapNode n,
-               core::map<v3s16, MapBlock*> &modified_blocks, std::string &player_name)
+               core::map<v3s16, MapBlock*> &modified_blocks)
 {
+       INodeDefManager *nodemgr = m_gamedef->ndef();
+
        /*PrintInfo(m_dout);
        m_dout<<DTIME<<"Map::addNodeAndUpdate(): p=("
                        <<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
@@ -929,46 +944,13 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n,
        try{
                MapNode topnode = getNode(toppos);
 
-               if(topnode.getLight(LIGHTBANK_DAY) != LIGHT_SUN)
+               if(topnode.getLight(LIGHTBANK_DAY, nodemgr) != LIGHT_SUN)
                        node_under_sunlight = false;
        }
        catch(InvalidPositionException &e)
        {
        }
 
-#if 0
-       /*
-               If the new node is solid and there is grass below, change it to mud
-       */
-       if(content_features(n).walkable == true)
-       {
-               try{
-                       MapNode bottomnode = getNode(bottompos);
-
-                       if(bottomnode.getContent() == CONTENT_GRASS
-                                       || bottomnode.getContent() == CONTENT_GRASS_FOOTSTEPS)
-                       {
-                               bottomnode.setContent(CONTENT_MUD);
-                               setNode(bottompos, bottomnode);
-                       }
-               }
-               catch(InvalidPositionException &e)
-               {
-               }
-       }
-#endif
-
-#if 0
-       /*
-               If the new node is mud and it is under sunlight, change it
-               to grass
-       */
-       if(n.getContent() == CONTENT_MUD && node_under_sunlight)
-       {
-               n.setContent(CONTENT_GRASS);
-       }
-#endif
-
        /*
                Remove all light that has come out of this node
        */
@@ -982,7 +964,7 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n,
        {
                enum LightBank bank = banks[i];
 
-               u8 lightwas = getNode(p).getLight(bank);
+               u8 lightwas = getNode(p).getLight(bank, nodemgr);
 
                // Add the block of the added node to modified_blocks
                v3s16 blockpos = getNodeBlockPos(p);
@@ -999,16 +981,16 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n,
                // light again into this.
                unLightNeighbors(bank, p, lightwas, light_sources, modified_blocks);
 
-               n.setLight(bank, 0);
+               n.setLight(bank, 0, nodemgr);
        }
 
        /*
                If node lets sunlight through and is under sunlight, it has
                sunlight too.
        */
-       if(node_under_sunlight && content_features(n).sunlight_propagates)
+       if(node_under_sunlight && nodemgr->get(n).sunlight_propagates)
        {
-               n.setLight(LIGHTBANK_DAY, LIGHT_SUN);
+               n.setLight(LIGHTBANK_DAY, LIGHT_SUN, nodemgr);
        }
 
        /*
@@ -1020,13 +1002,16 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n,
        /*
                Add intial metadata
        */
-
-       NodeMetadata *meta_proto = content_features(n).initial_metadata;
-       if(meta_proto)
-       {
-               NodeMetadata *meta = meta_proto->clone();
-               meta->setOwner(player_name);
-               setNodeMetadata(p, meta);
+       
+       std::string metadata_name = nodemgr->get(n).metadata_name;
+       if(metadata_name != ""){
+               NodeMetadata *meta = NodeMetadata::create(metadata_name, m_gamedef);
+               if(!meta){
+                       errorstream<<"Failed to create node metadata \""
+                                       <<metadata_name<<"\""<<std::endl;
+               } else {
+                       setNodeMetadata(p, meta);
+               }
        }
 
        /*
@@ -1036,7 +1021,7 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n,
                TODO: This could be optimized by mass-unlighting instead
                          of looping
        */
-       if(node_under_sunlight && !content_features(n).sunlight_propagates)
+       if(node_under_sunlight && !nodemgr->get(n).sunlight_propagates)
        {
                s16 y = p.Y - 1;
                for(;; y--){
@@ -1052,12 +1037,12 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n,
                                break;
                        }
 
-                       if(n2.getLight(LIGHTBANK_DAY) == LIGHT_SUN)
+                       if(n2.getLight(LIGHTBANK_DAY, nodemgr) == LIGHT_SUN)
                        {
                                unLightNeighbors(LIGHTBANK_DAY,
-                                               n2pos, n2.getLight(LIGHTBANK_DAY),
+                                               n2pos, n2.getLight(LIGHTBANK_DAY, nodemgr),
                                                light_sources, modified_blocks);
-                               n2.setLight(LIGHTBANK_DAY, 0);
+                               n2.setLight(LIGHTBANK_DAY, 0, nodemgr);
                                setNode(n2pos, n2);
                        }
                        else
@@ -1107,7 +1092,7 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n,
                v3s16 p2 = p + dirs[i];
 
                MapNode n2 = getNode(p2);
-               if(content_liquid(n2.getContent()) || n2.getContent() == CONTENT_AIR)
+               if(nodemgr->get(n2).isLiquid() || n2.getContent() == CONTENT_AIR)
                {
                        m_transforming_liquid.push_back(p2);
                }
@@ -1123,6 +1108,8 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n,
 void Map::removeNodeAndUpdate(v3s16 p,
                core::map<v3s16, MapBlock*> &modified_blocks)
 {
+       INodeDefManager *nodemgr = m_gamedef->ndef();
+
        /*PrintInfo(m_dout);
        m_dout<<DTIME<<"Map::removeNodeAndUpdate(): p=("
                        <<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
@@ -1141,7 +1128,7 @@ void Map::removeNodeAndUpdate(v3s16 p,
        try{
                MapNode topnode = getNode(toppos);
 
-               if(topnode.getLight(LIGHTBANK_DAY) != LIGHT_SUN)
+               if(topnode.getLight(LIGHTBANK_DAY, nodemgr) != LIGHT_SUN)
                        node_under_sunlight = false;
        }
        catch(InvalidPositionException &e)
@@ -1163,7 +1150,7 @@ void Map::removeNodeAndUpdate(v3s16 p,
                        Unlight neighbors (in case the node is a light source)
                */
                unLightNeighbors(bank, p,
-                               getNode(p).getLight(bank),
+                               getNode(p).getLight(bank, nodemgr),
                                light_sources, modified_blocks);
        }
 
@@ -1225,7 +1212,7 @@ void Map::removeNodeAndUpdate(v3s16 p,
                // TODO: Is this needed? Lighting is cleared up there already.
                try{
                        MapNode n = getNode(p);
-                       n.setLight(LIGHTBANK_DAY, 0);
+                       n.setLight(LIGHTBANK_DAY, 0, nodemgr);
                        setNode(p, n);
                }
                catch(InvalidPositionException &e)
@@ -1281,7 +1268,7 @@ void Map::removeNodeAndUpdate(v3s16 p,
                v3s16 p2 = p + dirs[i];
 
                MapNode n2 = getNode(p2);
-               if(content_liquid(n2.getContent()) || n2.getContent() == CONTENT_AIR)
+               if(nodemgr->get(n2).isLiquid() || n2.getContent() == CONTENT_AIR)
                {
                        m_transforming_liquid.push_back(p2);
                }
@@ -1302,8 +1289,7 @@ bool Map::addNodeWithEvent(v3s16 p, MapNode n)
        bool succeeded = true;
        try{
                core::map<v3s16, MapBlock*> modified_blocks;
-               std::string st = std::string("");
-               addNodeAndUpdate(p, n, modified_blocks, st);
+               addNodeAndUpdate(p, n, modified_blocks);
 
                // Copy modified_blocks to event
                for(core::map<v3s16, MapBlock*>::Iterator
@@ -1415,9 +1401,13 @@ void Map::timerUpdate(float dtime, float unload_timeout,
 {
        bool save_before_unloading = (mapType() == MAPTYPE_SERVER);
        
+       // Profile modified reasons
+       Profiler modprofiler;
+       
        core::list<v2s16> sector_deletion_queue;
        u32 deleted_blocks_count = 0;
        u32 saved_blocks_count = 0;
+       u32 block_count_all = 0;
 
        core::map<v2s16, MapSector*>::Iterator si;
 
@@ -1447,6 +1437,7 @@ void Map::timerUpdate(float dtime, float unload_timeout,
                                if(block->getModified() != MOD_STATE_CLEAN
                                                && save_before_unloading)
                                {
+                                       modprofiler.add(block->getModifiedReason(), 1);
                                        saveBlock(block);
                                        saved_blocks_count++;
                                }
@@ -1462,6 +1453,7 @@ void Map::timerUpdate(float dtime, float unload_timeout,
                        else
                        {
                                all_blocks_deleted = false;
+                               block_count_all++;
                        }
                }
 
@@ -1482,7 +1474,13 @@ void Map::timerUpdate(float dtime, float unload_timeout,
                                <<" blocks from memory";
                if(save_before_unloading)
                        infostream<<", of which "<<saved_blocks_count<<" were written";
+               infostream<<", "<<block_count_all<<" blocks in memory";
                infostream<<"."<<std::endl;
+               if(saved_blocks_count != 0){
+                       PrintInfo(infostream); // ServerMap/ClientMap:
+                       infostream<<"Blocks modified by: "<<std::endl;
+                       modprofiler.print(infostream);
+               }
        }
 }
 
@@ -1578,6 +1576,8 @@ struct NodeNeighbor {
 
 void Map::transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks)
 {
+       INodeDefManager *nodemgr = m_gamedef->ndef();
+
        DSTACK(__FUNCTION_NAME);
        //TimeTaker timer("transformLiquids()");
 
@@ -1612,11 +1612,11 @@ void Map::transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks)
                 */
                s8 liquid_level = -1;
                u8 liquid_kind = CONTENT_IGNORE;
-               LiquidType liquid_type = content_features(n0.getContent()).liquid_type;
+               LiquidType liquid_type = nodemgr->get(n0).liquid_type;
                switch (liquid_type) {
                        case LIQUID_SOURCE:
                                liquid_level = LIQUID_LEVEL_SOURCE;
-                               liquid_kind = content_features(n0.getContent()).liquid_alternative_flowing;
+                               liquid_kind = nodemgr->getId(nodemgr->get(n0).liquid_alternative_flowing);
                                break;
                        case LIQUID_FLOWING:
                                liquid_level = (n0.param2 & LIQUID_LEVEL_MASK);
@@ -1656,7 +1656,7 @@ void Map::transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks)
                        }
                        v3s16 npos = p0 + dirs[i];
                        NodeNeighbor nb = {getNodeNoEx(npos), nt, npos};
-                       switch (content_features(nb.n.getContent()).liquid_type) {
+                       switch (nodemgr->get(nb.n.getContent()).liquid_type) {
                                case LIQUID_NONE:
                                        if (nb.n.getContent() == CONTENT_AIR) {
                                                airs[num_airs++] = nb;
@@ -1676,18 +1676,20 @@ void Map::transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks)
                                case LIQUID_SOURCE:
                                        // if this node is not (yet) of a liquid type, choose the first liquid type we encounter 
                                        if (liquid_kind == CONTENT_AIR)
-                                               liquid_kind = content_features(nb.n.getContent()).liquid_alternative_flowing;
-                                       if (content_features(nb.n.getContent()).liquid_alternative_flowing !=liquid_kind) {
+                                               liquid_kind = nodemgr->getId(nodemgr->get(nb.n).liquid_alternative_flowing);
+                                       if (nodemgr->getId(nodemgr->get(nb.n).liquid_alternative_flowing) != liquid_kind) {
                                                neutrals[num_neutrals++] = nb;
                                        } else {
-                                               sources[num_sources++] = nb;
+                                               // Do not count bottom source, it will screw things up
+                                               if(dirs[i].Y != -1)
+                                                       sources[num_sources++] = nb;
                                        }
                                        break;
                                case LIQUID_FLOWING:
                                        // if this node is not (yet) of a liquid type, choose the first liquid type we encounter
                                        if (liquid_kind == CONTENT_AIR)
-                                               liquid_kind = content_features(nb.n.getContent()).liquid_alternative_flowing;
-                                       if (content_features(nb.n.getContent()).liquid_alternative_flowing != liquid_kind) {
+                                               liquid_kind = nodemgr->getId(nodemgr->get(nb.n).liquid_alternative_flowing);
+                                       if (nodemgr->getId(nodemgr->get(nb.n).liquid_alternative_flowing) != liquid_kind) {
                                                neutrals[num_neutrals++] = nb;
                                        } else {
                                                flows[num_flows++] = nb;
@@ -1708,7 +1710,7 @@ void Map::transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks)
                        // liquid_kind will be set to either the flowing alternative of the node (if it's a liquid)
                        // or the flowing alternative of the first of the surrounding sources (if it's air), so
                        // it's perfectly safe to use liquid_kind here to determine the new node content.
-                       new_node_content = content_features(liquid_kind).liquid_alternative_source;
+                       new_node_content = nodemgr->getId(nodemgr->get(liquid_kind).liquid_alternative_source);
                } else if (num_sources == 1 && sources[0].t != NEIGHBOR_LOWER) {
                        // liquid_kind is set properly, see above
                        new_node_content = liquid_kind;
@@ -1737,7 +1739,7 @@ void Map::transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks)
                                }
                        }
 
-                       u8 viscosity = content_features(liquid_kind).liquid_viscosity;
+                       u8 viscosity = nodemgr->get(liquid_kind).liquid_viscosity;
                        if (viscosity > 1 && max_node_level != liquid_level) {
                                // amount to gain, limited by viscosity
                                // must be at least 1 in absolute value
@@ -1763,7 +1765,7 @@ void Map::transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks)
                /*
                        check if anything has changed. if not, just continue with the next node.
                 */
-               if (new_node_content == n0.getContent() && (content_features(n0.getContent()).liquid_type != LIQUID_FLOWING ||
+               if (new_node_content == n0.getContent() && (nodemgr->get(n0.getContent()).liquid_type != LIQUID_FLOWING ||
                                                                                 ((n0.param2 & LIQUID_LEVEL_MASK) == (u8)new_node_level &&
                                                                                 ((n0.param2 & LIQUID_FLOW_DOWN_MASK) == LIQUID_FLOW_DOWN_MASK)
                                                                                 == flowing_down)))
@@ -1773,8 +1775,8 @@ void Map::transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks)
                /*
                        update the current node
                 */
-               bool flow_down_enabled = (flowing_down && ((n0.param2 & LIQUID_FLOW_DOWN_MASK) != LIQUID_FLOW_DOWN_MASK));
-               if (content_features(new_node_content).liquid_type == LIQUID_FLOWING) {
+               //bool flow_down_enabled = (flowing_down && ((n0.param2 & LIQUID_FLOW_DOWN_MASK) != LIQUID_FLOW_DOWN_MASK));
+               if (nodemgr->get(new_node_content).liquid_type == LIQUID_FLOWING) {
                        // set level to last 3 bits, flowing down bit to 4th bit
                        n0.param2 = (flowing_down ? LIQUID_FLOW_DOWN_MASK : 0x00) | (new_node_level & LIQUID_LEVEL_MASK);
                } else {
@@ -1788,14 +1790,14 @@ void Map::transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks)
                if(block != NULL) {
                        modified_blocks.insert(blockpos, block);
                        // If node emits light, MapBlock requires lighting update
-                       if(content_features(n0).light_source != 0)
+                       if(nodemgr->get(n0).light_source != 0)
                                lighting_modified_blocks[block->getPos()] = block;
                }
 
                /*
                        enqueue neighbors for update if neccessary
                 */
-               switch (content_features(n0.getContent()).liquid_type) {
+               switch (nodemgr->get(n0.getContent()).liquid_type) {
                        case LIQUID_SOURCE:
                        case LIQUID_FLOWING:
                                // make sure source flows into all neighboring nodes
@@ -1835,7 +1837,7 @@ NodeMetadata* Map::getNodeMetadata(v3s16 p)
                                <<std::endl;
                return NULL;
        }
-       NodeMetadata *meta = block->m_node_metadata.get(p_rel);
+       NodeMetadata *meta = block->m_node_metadata->get(p_rel);
        return meta;
 }
 
@@ -1855,7 +1857,7 @@ void Map::setNodeMetadata(v3s16 p, NodeMetadata *meta)
                                <<std::endl;
                return;
        }
-       block->m_node_metadata.set(p_rel, meta);
+       block->m_node_metadata->set(p_rel, meta);
 }
 
 void Map::removeNodeMetadata(v3s16 p)
@@ -1869,7 +1871,7 @@ void Map::removeNodeMetadata(v3s16 p)
                                <<std::endl;
                return;
        }
-       block->m_node_metadata.remove(p_rel);
+       block->m_node_metadata->remove(p_rel);
 }
 
 void Map::nodeMetadataStep(float dtime,
@@ -1894,7 +1896,7 @@ void Map::nodeMetadataStep(float dtime,
                for(i=sectorblocks.begin(); i!=sectorblocks.end(); i++)
                {
                        MapBlock *block = *i;
-                       bool changed = block->m_node_metadata.step(dtime);
+                       bool changed = block->m_node_metadata->step(dtime);
                        if(changed)
                                changed_blocks[block->getPos()] = block;
                }
@@ -1905,15 +1907,15 @@ void Map::nodeMetadataStep(float dtime,
        ServerMap
 */
 
-ServerMap::ServerMap(std::string savedir):
-       Map(dout_server),
+ServerMap::ServerMap(std::string savedir, IGameDef *gamedef):
+       Map(dout_server, gamedef),
        m_seed(0),
        m_map_metadata_changed(true),
        m_database(NULL),
        m_database_read(NULL),
        m_database_write(NULL)
 {
-       infostream<<__FUNCTION_NAME<<std::endl;
+       verbosestream<<__FUNCTION_NAME<<std::endl;
 
        //m_chunksize = 8; // Takes a few seconds
 
@@ -1951,7 +1953,7 @@ ServerMap::ServerMap(std::string savedir):
                        // If directory is empty, it is safe to save into it.
                        if(fs::GetDirListing(m_savedir).size() == 0)
                        {
-                               infostream<<"Server: Empty save directory is valid."
+                               infostream<<"ServerMap: Empty save directory is valid."
                                                <<std::endl;
                                m_map_saving_enabled = true;
                        }
@@ -1968,25 +1970,10 @@ ServerMap::ServerMap(std::string savedir):
                                        //m_chunksize = 0;
                                }
 
-                               /*try{
-                                       // Load chunk metadata
-                                       loadChunkMeta();
-                               }
-                               catch(FileNotGoodException &e){
-                                       infostream<<"WARNING: Could not load chunk metadata."
-                                                       <<" Disabling chunk-based generator."
-                                                       <<std::endl;
-                                       m_chunksize = 0;
-                               }*/
-
-                               /*infostream<<"Server: Successfully loaded chunk "
-                                               "metadata and sector (0,0) from "<<savedir<<
-                                               ", assuming valid save directory."
-                                               <<std::endl;*/
-
-                               infostream<<"Server: Successfully loaded map "
-                                               <<"and chunk metadata from "<<savedir
+                               infostream<<"ServerMap: Successfully loaded map "
+                                               <<"metadata from "<<savedir
                                                <<", assuming valid save directory."
+                                               <<" seed="<<m_seed<<"."
                                                <<std::endl;
 
                                m_map_saving_enabled = true;
@@ -2001,7 +1988,7 @@ ServerMap::ServerMap(std::string savedir):
        }
        catch(std::exception &e)
        {
-               infostream<<"WARNING: Server: Failed to load map from "<<savedir
+               infostream<<"WARNING: ServerMap: Failed to load map from "<<savedir
                                <<", exception: "<<e.what()<<std::endl;
                infostream<<"Please remove the map or fix it."<<std::endl;
                infostream<<"WARNING: Map saving will be disabled."<<std::endl;
@@ -2013,29 +2000,29 @@ ServerMap::ServerMap(std::string savedir):
        emergeSector(v2s16(0,0));
 
        // Initially write whole map
-       save(false);
+       save(MOD_STATE_CLEAN);
 }
 
 ServerMap::~ServerMap()
 {
-       infostream<<__FUNCTION_NAME<<std::endl;
+       verbosestream<<__FUNCTION_NAME<<std::endl;
 
        try
        {
                if(m_map_saving_enabled)
                {
                        // Save only changed parts
-                       save(true);
-                       infostream<<"Server: saved map to "<<m_savedir<<std::endl;
+                       save(MOD_STATE_WRITE_AT_UNLOAD);
+                       infostream<<"ServerMap: Saved map to "<<m_savedir<<std::endl;
                }
                else
                {
-                       infostream<<"Server: map not saved"<<std::endl;
+                       infostream<<"ServerMap: Map not saved"<<std::endl;
                }
        }
        catch(std::exception &e)
        {
-               infostream<<"Server: Failed to save map to "<<m_savedir
+               infostream<<"ServerMap: Failed to save map to "<<m_savedir
                                <<", exception: "<<e.what()<<std::endl;
        }
 
@@ -2080,6 +2067,7 @@ void ServerMap::initBlockMake(mapgen::BlockMakeData *data, v3s16 blockpos)
        data->no_op = false;
        data->seed = m_seed;
        data->blockpos = blockpos;
+       data->nodedef = m_gamedef->ndef();
 
        /*
                Create the whole area of this and the neighboring blocks
@@ -2163,7 +2151,17 @@ MapBlock* ServerMap::finishBlockMake(mapgen::BlockMakeData *data,
 
        /*infostream<<"Resulting vmanip:"<<std::endl;
        data->vmanip.print(infostream);*/
-       
+
+       // Make sure affected blocks are loaded
+       for(s16 x=-1; x<=1; x++)
+       for(s16 z=-1; z<=1; z++)
+       for(s16 y=-1; y<=1; y++)
+       {
+               v3s16 p(blockpos.X+x, blockpos.Y+y, blockpos.Z+z);
+               // Load from disk if not already in memory
+               emergeBlock(p, false);
+       }
+
        /*
                Blit generated stuff to map
                NOTE: blitBackAll adds nearly everything to changed_blocks
@@ -2302,7 +2300,8 @@ MapBlock* ServerMap::finishBlockMake(mapgen::BlockMakeData *data,
                /*
                        Set block as modified
                */
-               block->raiseModified(MOD_STATE_WRITE_NEEDED);
+               block->raiseModified(MOD_STATE_WRITE_NEEDED,
+                               "finishBlockMake updateDayNightDiff");
        }
 
        /*
@@ -2314,7 +2313,7 @@ MapBlock* ServerMap::finishBlockMake(mapgen::BlockMakeData *data,
                Save changed parts of map
                NOTE: Will be saved later.
        */
-       //save(true);
+       //save(MOD_STATE_WRITE_AT_UNLOAD);
 
        /*infostream<<"finishBlockMake() done for ("<<blockpos.X<<","<<blockpos.Y<<","
                        <<blockpos.Z<<")"<<std::endl;*/
@@ -2387,7 +2386,7 @@ ServerMapSector * ServerMap::createSector(v2s16 p2d)
                Generate blank sector
        */
        
-       sector = new ServerMapSector(this, p2d);
+       sector = new ServerMapSector(this, p2d, m_gamedef);
        
        // Sector position on map in nodes
        v2s16 nodepos2d = p2d * MAP_BLOCKSIZE;
@@ -2500,10 +2499,7 @@ MapBlock * ServerMap::generateBlock(
                        for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
                        {
                                MapNode n;
-                               if(y0%2==0)
-                                       n.setContent(CONTENT_AIR);
-                               else
-                                       n.setContent(CONTENT_STONE);
+                               n.setContent(CONTENT_AIR);
                                block->setNode(v3s16(x0,y0,z0), n);
                        }
                }
@@ -2681,7 +2677,7 @@ void ServerMap::createDatabase() {
        if(e == SQLITE_ABORT)
                throw FileNotGoodException("Could not create database structure");
        else
-               infostream<<"Server: Database structure was created";
+               infostream<<"ServerMap: Database structure was created";
 }
 
 void ServerMap::verifyDatabase() {
@@ -2729,7 +2725,7 @@ void ServerMap::verifyDatabase() {
                        throw FileNotGoodException("Cannot prepare read statement");
                }
                
-               infostream<<"Server: Database opened"<<std::endl;
+               infostream<<"ServerMap: Database opened"<<std::endl;
        }
 }
 
@@ -2825,7 +2821,7 @@ std::string ServerMap::getBlockFilename(v3s16 p)
        return cc;
 }
 
-void ServerMap::save(bool only_changed)
+void ServerMap::save(ModifiedState save_level)
 {
        DSTACK(__FUNCTION_NAME);
        if(m_map_saving_enabled == false)
@@ -2834,27 +2830,32 @@ void ServerMap::save(bool only_changed)
                return;
        }
        
-       if(only_changed == false)
+       if(save_level == MOD_STATE_CLEAN)
                infostream<<"ServerMap: Saving whole map, this can take time."
                                <<std::endl;
        
-       if(only_changed == false || m_map_metadata_changed)
+       if(m_map_metadata_changed || save_level == MOD_STATE_CLEAN)
        {
                saveMapMeta();
        }
 
+       // Profile modified reasons
+       Profiler modprofiler;
+       
        u32 sector_meta_count = 0;
        u32 block_count = 0;
        u32 block_count_all = 0; // Number of blocks in memory
        
-       beginSave();
+       // Don't do anything with sqlite unless something is really saved
+       bool save_started = false;
+
        core::map<v2s16, MapSector*>::Iterator i = m_sectors.getIterator();
        for(; i.atEnd() == false; i++)
        {
                ServerMapSector *sector = (ServerMapSector*)i.getNode()->getValue();
                assert(sector->getId() == MAPSECTOR_SERVER);
        
-               if(sector->differs_from_disk || only_changed == false)
+               if(sector->differs_from_disk || save_level == MOD_STATE_CLEAN)
                {
                        saveSectorMeta(sector);
                        sector_meta_count++;
@@ -2863,16 +2864,22 @@ void ServerMap::save(bool only_changed)
                sector->getBlocks(blocks);
                core::list<MapBlock*>::Iterator j;
                
-               //sqlite3_exec(m_database, "BEGIN;", NULL, NULL, NULL);
                for(j=blocks.begin(); j!=blocks.end(); j++)
                {
                        MapBlock *block = *j;
                        
                        block_count_all++;
 
-                       if(block->getModified() >= MOD_STATE_WRITE_NEEDED 
-                                       || only_changed == false)
+                       if(block->getModified() >= save_level)
                        {
+                               // Lazy beginSave()
+                               if(!save_started){
+                                       beginSave();
+                                       save_started = true;
+                               }
+
+                               modprofiler.add(block->getModifiedReason(), 1);
+
                                saveBlock(block);
                                block_count++;
 
@@ -2882,15 +2889,15 @@ void ServerMap::save(bool only_changed)
                                                <<block->getPos().Z<<")"
                                                <<std::endl;*/
                        }
-               //sqlite3_exec(m_database, "COMMIT;", NULL, NULL, NULL);
                }
        }
-       endSave();
+       if(save_started)
+               endSave();
 
        /*
                Only print if something happened or saved whole map
        */
-       if(only_changed == false || sector_meta_count != 0
+       if(save_level == MOD_STATE_CLEAN || sector_meta_count != 0
                        || block_count != 0)
        {
                infostream<<"ServerMap: Written: "
@@ -2898,6 +2905,9 @@ void ServerMap::save(bool only_changed)
                                <<block_count<<" block files"
                                <<", "<<block_count_all<<" blocks in memory."
                                <<std::endl;
+               PrintInfo(infostream); // ServerMap/ClientMap:
+               infostream<<"Blocks modified by: "<<std::endl;
+               modprofiler.print(infostream);
        }
 }
 
@@ -2951,9 +2961,9 @@ void ServerMap::saveMapMeta()
 {
        DSTACK(__FUNCTION_NAME);
        
-       infostream<<"ServerMap::saveMapMeta(): "
+       /*infostream<<"ServerMap::saveMapMeta(): "
                        <<"seed="<<m_seed
-                       <<std::endl;
+                       <<std::endl;*/
 
        createDirs(m_savedir);
        
@@ -2980,8 +2990,8 @@ void ServerMap::loadMapMeta()
 {
        DSTACK(__FUNCTION_NAME);
        
-       infostream<<"ServerMap::loadMapMeta(): Loading map metadata"
-                       <<std::endl;
+       /*infostream<<"ServerMap::loadMapMeta(): Loading map metadata"
+                       <<std::endl;*/
 
        std::string fullpath = m_savedir + DIR_DELIM + "map_meta.txt";
        std::ifstream is(fullpath.c_str(), std::ios_base::binary);
@@ -3009,7 +3019,7 @@ void ServerMap::loadMapMeta()
 
        m_seed = params.getU64("seed");
 
-       infostream<<"ServerMap::loadMapMeta(): "<<"seed="<<m_seed<<std::endl;
+       verbosestream<<"ServerMap::loadMapMeta(): "<<"seed="<<m_seed<<std::endl;
 }
 
 void ServerMap::saveSectorMeta(ServerMapSector *sector)
@@ -3052,7 +3062,7 @@ MapSector* ServerMap::loadSectorMeta(std::string sectordir, bool save_after_load
                                        <<fullpath<<" doesn't exist but directory does."
                                        <<" Continuing with a sector with no metadata."
                                        <<std::endl;*/
-                       sector = new ServerMapSector(this, p2d);
+                       sector = new ServerMapSector(this, p2d, m_gamedef);
                        m_sectors.insert(p2d, sector);
                }
                else
@@ -3063,7 +3073,7 @@ MapSector* ServerMap::loadSectorMeta(std::string sectordir, bool save_after_load
        else
        {
                sector = ServerMapSector::deSerialize
-                               (is, this, p2d, m_sectors);
+                               (is, this, p2d, m_sectors, m_gamedef);
                if(save_after_load)
                        saveSectorMeta(sector);
        }
@@ -3243,10 +3253,7 @@ void ServerMap::saveBlock(MapBlock *block)
        o.write((char*)&version, 1);
        
        // Write basic data
-       block->serialize(o, version);
-       
-       // Write extra data stored on disk
-       block->serializeDiskExtra(o, version);
+       block->serialize(o, version, true);
        
        // Write block to database
        
@@ -3308,11 +3315,8 @@ void ServerMap::loadBlock(std::string sectordir, std::string blockfile, MapSecto
                }
                
                // Read basic data
-               block->deSerialize(is, version);
+               block->deSerialize(is, version, true);
 
-               // Read extra data stored on disk
-               block->deSerializeDiskExtra(is, version);
-               
                // If it's a new block, insert it to the map
                if(created_new)
                        sector->insertBlock(block);
@@ -3378,10 +3382,7 @@ void ServerMap::loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool
                }
                
                // Read basic data
-               block->deSerialize(is, version);
-
-               // Read extra data stored on disk
-               block->deSerializeDiskExtra(is, version);
+               block->deSerialize(is, version, true);
                
                // If it's a new block, insert it to the map
                if(created_new)
@@ -3391,10 +3392,10 @@ void ServerMap::loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool
                        Save blocks loaded in old format in new format
                */
 
-               if(version < SER_FMT_VER_HIGHEST || save_after_load)
-               {
+               //if(version < SER_FMT_VER_HIGHEST || save_after_load)
+               // Only save if asked to; no need to update version
+               if(save_after_load)
                        saveBlock(block);
-               }
                
                // We just loaded it from, so it's up-to-date.
                block->resetModified();
@@ -3521,12 +3522,13 @@ void ServerMap::PrintInfo(std::ostream &out)
 
 ClientMap::ClientMap(
                Client *client,
+               IGameDef *gamedef,
                MapDrawControl &control,
                scene::ISceneNode* parent,
                scene::ISceneManager* mgr,
                s32 id
 ):
-       Map(dout_client),
+       Map(dout_client, gamedef),
        scene::ISceneNode(parent, mgr, id),
        m_client(client),
        m_control(control),
@@ -3564,7 +3566,7 @@ MapSector * ClientMap::emergeSector(v2s16 p2d)
        }
        
        // Create a sector
-       ClientMapSector *sector = new ClientMapSector(this, p2d);
+       ClientMapSector *sector = new ClientMapSector(this, p2d, m_gamedef);
        
        {
                //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
@@ -3614,7 +3616,7 @@ void ClientMap::OnRegisterSceneNode()
 }
 
 static bool isOccluded(Map *map, v3s16 p0, v3s16 p1, float step, float stepfac,
-               float start_off, float end_off, u32 needed_count)
+               float start_off, float end_off, u32 needed_count, INodeDefManager *nodemgr)
 {
        float d0 = (float)BS * p0.getDistanceFrom(p1);
        v3s16 u0 = p1 - p0;
@@ -3627,7 +3629,7 @@ static bool isOccluded(Map *map, v3s16 p0, v3s16 p1, float step, float stepfac,
                v3s16 p = floatToInt(pf, BS);
                MapNode n = map->getNodeNoEx(p);
                bool is_transparent = false;
-               ContentFeatures &f = content_features(n);
+               const ContentFeatures &f = nodemgr->get(n);
                if(f.solidness == 0)
                        is_transparent = (f.visual_solidness != 2);
                else
@@ -3644,6 +3646,8 @@ static bool isOccluded(Map *map, v3s16 p0, v3s16 p1, float step, float stepfac,
 
 void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
 {
+       INodeDefManager *nodemgr = m_gamedef->ndef();
+
        //m_dout<<DTIME<<"Rendering map..."<<std::endl;
        DSTACK(__FUNCTION_NAME);
 
@@ -3671,7 +3675,12 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
        */
        int time1 = time(0);
 
-       //u32 daynight_ratio = m_client->getDayNightRatio();
+       /*
+               Get animation parameters
+       */
+       float animation_time = m_client->getAnimationTime();
+       int crack = m_client->getCrackLevel();
+       u32 daynight_ratio = m_client->getDayNightRatio();
 
        m_camera_mutex.Lock();
        v3f camera_position = m_camera_position;
@@ -3704,8 +3713,9 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
        u32 vertex_count = 0;
        u32 meshbuffer_count = 0;
        
-       // For limiting number of mesh updates per frame
-       u32 mesh_update_count = 0;
+       // For limiting number of mesh animations per frame
+       u32 mesh_animate_count = 0;
+       u32 mesh_animate_count_far = 0;
        
        // Number of blocks in rendering range
        u32 blocks_in_range = 0;
@@ -3786,66 +3796,24 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
 
                        blocks_in_range++;
                        
-                       // This block is in range. Reset usage timer.
-                       block->resetUsageTimer();
-
-#if 1
                        /*
-                               Update expired mesh (used for day/night change)
-
-                               It doesn't work exactly like it should now with the
-                               tasked mesh update but whatever.
+                               Ignore if mesh doesn't exist
                        */
-
-                       bool mesh_expired = false;
-                       
                        {
-                               JMutexAutoLock lock(block->mesh_mutex);
+                               //JMutexAutoLock lock(block->mesh_mutex);
 
-                               mesh_expired = block->getMeshExpired();
-
-                               // Mesh has not been expired and there is no mesh:
-                               // block has no content
-                               if(block->mesh == NULL && mesh_expired == false){
+                               if(block->mesh == NULL){
                                        blocks_in_range_without_mesh++;
                                        continue;
                                }
                        }
 
-                       f32 faraway = BS*50;
-                       //f32 faraway = m_control.wanted_range * BS;
-                       
-                       /*
-                               This has to be done with the mesh_mutex unlocked
-                       */
-                       // Pretty random but this should work somewhat nicely
-                       if(mesh_expired && (
-                                       (mesh_update_count < 3
-                                               && (d < faraway || mesh_update_count < 2)
-                                       )
-                                       || 
-                                       (m_control.range_all && mesh_update_count < 20)
-                               )
-                       )
-                       /*if(mesh_expired && mesh_update_count < 6
-                                       && (d < faraway || mesh_update_count < 3))*/
-                       {
-                               mesh_update_count++;
-
-                               // Mesh has been expired: generate new mesh
-                               //block->updateMesh(daynight_ratio);
-                               m_client->addUpdateMeshTask(block->getPos());
-
-                               mesh_expired = false;
-                       }
-#endif
-
                        /*
                                Occlusion culling
                        */
 
-                       v3s16 cpn = v3s16(block->getPos() * MAP_BLOCKSIZE)
-                                       + v3s16(MAP_BLOCKSIZE)/2;
+                       v3s16 cpn = block->getPos() * MAP_BLOCKSIZE;
+                       cpn += v3s16(MAP_BLOCKSIZE/2, MAP_BLOCKSIZE/2, MAP_BLOCKSIZE/2);
                        float step = BS*1;
                        float stepfac = 1.1;
                        float startoff = BS*1;
@@ -3855,50 +3823,66 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
                        u32 needed_count = 1;
                        if(
                                isOccluded(this, spn, cpn + v3s16(0,0,0),
-                                               step, stepfac, startoff, endoff, needed_count) &&
+                                       step, stepfac, startoff, endoff, needed_count, nodemgr) &&
                                isOccluded(this, spn, cpn + v3s16(bs2,bs2,bs2),
-                                               step, stepfac, startoff, endoff, needed_count) &&
+                                       step, stepfac, startoff, endoff, needed_count, nodemgr) &&
                                isOccluded(this, spn, cpn + v3s16(bs2,bs2,-bs2),
-                                               step, stepfac, startoff, endoff, needed_count) &&
+                                       step, stepfac, startoff, endoff, needed_count, nodemgr) &&
                                isOccluded(this, spn, cpn + v3s16(bs2,-bs2,bs2),
-                                               step, stepfac, startoff, endoff, needed_count) &&
+                                       step, stepfac, startoff, endoff, needed_count, nodemgr) &&
                                isOccluded(this, spn, cpn + v3s16(bs2,-bs2,-bs2),
-                                               step, stepfac, startoff, endoff, needed_count) &&
+                                       step, stepfac, startoff, endoff, needed_count, nodemgr) &&
                                isOccluded(this, spn, cpn + v3s16(-bs2,bs2,bs2),
-                                               step, stepfac, startoff, endoff, needed_count) &&
+                                       step, stepfac, startoff, endoff, needed_count, nodemgr) &&
                                isOccluded(this, spn, cpn + v3s16(-bs2,bs2,-bs2),
-                                               step, stepfac, startoff, endoff, needed_count) &&
+                                       step, stepfac, startoff, endoff, needed_count, nodemgr) &&
                                isOccluded(this, spn, cpn + v3s16(-bs2,-bs2,bs2),
-                                               step, stepfac, startoff, endoff, needed_count) &&
+                                       step, stepfac, startoff, endoff, needed_count, nodemgr) &&
                                isOccluded(this, spn, cpn + v3s16(-bs2,-bs2,-bs2),
-                                               step, stepfac, startoff, endoff, needed_count)
+                                       step, stepfac, startoff, endoff, needed_count, nodemgr)
                        )
                        {
                                blocks_occlusion_culled++;
                                continue;
                        }
                        
-                       /*
-                               Ignore if mesh doesn't exist
-                       */
-                       {
-                               JMutexAutoLock lock(block->mesh_mutex);
+                       // This block is in range. Reset usage timer.
+                       block->resetUsageTimer();
 
-                               scene::SMesh *mesh = block->mesh;
-                               
-                               if(mesh == NULL){
-                                       blocks_in_range_without_mesh++;
-                                       continue;
-                               }
-                       }
-                       
                        // Limit block count in case of a sudden increase
                        blocks_would_have_drawn++;
                        if(blocks_drawn >= m_control.wanted_max_blocks
                                        && m_control.range_all == false
                                        && d > m_control.wanted_min_range * BS)
                                continue;
-                       
+
+                       // Mesh animation
+                       {
+                               //JMutexAutoLock lock(block->mesh_mutex);
+                               MapBlockMesh *mapBlockMesh = block->mesh;
+                               // Pretty random but this should work somewhat nicely
+                               bool faraway = d >= BS*50;
+                               //bool faraway = d >= m_control.wanted_range * BS;
+                               if(mapBlockMesh->isAnimationForced() ||
+                                               !faraway ||
+                                               mesh_animate_count_far < (m_control.range_all ? 200 : 50))
+                               {
+                                       bool animated = mapBlockMesh->animate(
+                                                       faraway,
+                                                       animation_time,
+                                                       crack,
+                                                       daynight_ratio);
+                                       if(animated)
+                                               mesh_animate_count++;
+                                       if(animated && faraway)
+                                               mesh_animate_count_far++;
+                               }
+                               else
+                               {
+                                       mapBlockMesh->decreaseAnimationForceTimer();
+                               }
+                       }
+
                        // Add to set
                        drawset[block->getPos()] = block;
                        
@@ -3946,11 +3930,14 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
                        Draw the faces of the block
                */
                {
-                       JMutexAutoLock lock(block->mesh_mutex);
+                       //JMutexAutoLock lock(block->mesh_mutex);
+
+                       MapBlockMesh *mapBlockMesh = block->mesh;
+                       assert(mapBlockMesh);
 
-                       scene::SMesh *mesh = block->mesh;
+                       scene::SMesh *mesh = mapBlockMesh->getMesh();
                        assert(mesh);
-                       
+
                        u32 c = mesh->getMeshBufferCount();
                        bool stuff_actually_drawn = false;
                        for(u32 i=0; i<c; i++)
@@ -3994,6 +3981,8 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
                        g_profiler->avg("CM: blocks in range without mesh (frac)",
                                        (float)blocks_in_range_without_mesh/blocks_in_range);
                g_profiler->avg("CM: blocks drawn", blocks_drawn);
+               g_profiler->avg("CM: animated meshes", mesh_animate_count);
+               g_profiler->avg("CM: animated meshes (far)", mesh_animate_count_far);
        }
        
        g_profiler->avg(prefix+"vertices drawn", vertex_count);
@@ -4013,6 +4002,8 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
 
 void ClientMap::renderPostFx()
 {
+       INodeDefManager *nodemgr = m_gamedef->ndef();
+
        // Sadly ISceneManager has no "post effects" render pass, in that case we
        // could just register for that and handle it in renderMap().
 
@@ -4024,7 +4015,7 @@ void ClientMap::renderPostFx()
 
        // - If the player is in a solid node, make everything black.
        // - If the player is in liquid, draw a semi-transparent overlay.
-       ContentFeatures& features = content_features(n);
+       const ContentFeatures& features = nodemgr->get(n);
        video::SColor post_effect_color = features.post_effect_color;
        if(features.solidness == 2 && g_settings->getBool("free_move") == false)
        {
@@ -4040,205 +4031,6 @@ void ClientMap::renderPostFx()
        }
 }
 
-bool ClientMap::setTempMod(v3s16 p, NodeMod mod,
-               core::map<v3s16, MapBlock*> *affected_blocks)
-{
-       bool changed = false;
-       /*
-               Add it to all blocks touching it
-       */
-       v3s16 dirs[7] = {
-               v3s16(0,0,0), // this
-               v3s16(0,0,1), // back
-               v3s16(0,1,0), // top
-               v3s16(1,0,0), // right
-               v3s16(0,0,-1), // front
-               v3s16(0,-1,0), // bottom
-               v3s16(-1,0,0), // left
-       };
-       for(u16 i=0; i<7; i++)
-       {
-               v3s16 p2 = p + dirs[i];
-               // Block position of neighbor (or requested) node
-               v3s16 blockpos = getNodeBlockPos(p2);
-               MapBlock * blockref = getBlockNoCreateNoEx(blockpos);
-               if(blockref == NULL)
-                       continue;
-               // Relative position of requested node
-               v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
-               if(blockref->setTempMod(relpos, mod))
-               {
-                       changed = true;
-               }
-       }
-       if(changed && affected_blocks!=NULL)
-       {
-               for(u16 i=0; i<7; i++)
-               {
-                       v3s16 p2 = p + dirs[i];
-                       // Block position of neighbor (or requested) node
-                       v3s16 blockpos = getNodeBlockPos(p2);
-                       MapBlock * blockref = getBlockNoCreateNoEx(blockpos);
-                       if(blockref == NULL)
-                               continue;
-                       affected_blocks->insert(blockpos, blockref);
-               }
-       }
-       return changed;
-}
-
-bool ClientMap::clearTempMod(v3s16 p,
-               core::map<v3s16, MapBlock*> *affected_blocks)
-{
-       bool changed = false;
-       v3s16 dirs[7] = {
-               v3s16(0,0,0), // this
-               v3s16(0,0,1), // back
-               v3s16(0,1,0), // top
-               v3s16(1,0,0), // right
-               v3s16(0,0,-1), // front
-               v3s16(0,-1,0), // bottom
-               v3s16(-1,0,0), // left
-       };
-       for(u16 i=0; i<7; i++)
-       {
-               v3s16 p2 = p + dirs[i];
-               // Block position of neighbor (or requested) node
-               v3s16 blockpos = getNodeBlockPos(p2);
-               MapBlock * blockref = getBlockNoCreateNoEx(blockpos);
-               if(blockref == NULL)
-                       continue;
-               // Relative position of requested node
-               v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
-               if(blockref->clearTempMod(relpos))
-               {
-                       changed = true;
-               }
-       }
-       if(changed && affected_blocks!=NULL)
-       {
-               for(u16 i=0; i<7; i++)
-               {
-                       v3s16 p2 = p + dirs[i];
-                       // Block position of neighbor (or requested) node
-                       v3s16 blockpos = getNodeBlockPos(p2);
-                       MapBlock * blockref = getBlockNoCreateNoEx(blockpos);
-                       if(blockref == NULL)
-                               continue;
-                       affected_blocks->insert(blockpos, blockref);
-               }
-       }
-       return changed;
-}
-
-void ClientMap::expireMeshes(bool only_daynight_diffed)
-{
-       TimeTaker timer("expireMeshes()");
-
-       core::map<v2s16, MapSector*>::Iterator si;
-       si = m_sectors.getIterator();
-       for(; si.atEnd() == false; si++)
-       {
-               MapSector *sector = si.getNode()->getValue();
-
-               core::list< MapBlock * > sectorblocks;
-               sector->getBlocks(sectorblocks);
-               
-               core::list< MapBlock * >::Iterator i;
-               for(i=sectorblocks.begin(); i!=sectorblocks.end(); i++)
-               {
-                       MapBlock *block = *i;
-
-                       if(only_daynight_diffed && dayNightDiffed(block->getPos()) == false)
-                       {
-                               continue;
-                       }
-                       
-                       {
-                               JMutexAutoLock lock(block->mesh_mutex);
-                               if(block->mesh != NULL)
-                               {
-                                       /*block->mesh->drop();
-                                       block->mesh = NULL;*/
-                                       block->setMeshExpired(true);
-                               }
-                       }
-               }
-       }
-}
-
-void ClientMap::updateMeshes(v3s16 blockpos, u32 daynight_ratio)
-{
-       assert(mapType() == MAPTYPE_CLIENT);
-
-       try{
-               v3s16 p = blockpos + v3s16(0,0,0);
-               MapBlock *b = getBlockNoCreate(p);
-               b->updateMesh(daynight_ratio);
-               //b->setMeshExpired(true);
-       }
-       catch(InvalidPositionException &e){}
-       // Leading edge
-       try{
-               v3s16 p = blockpos + v3s16(-1,0,0);
-               MapBlock *b = getBlockNoCreate(p);
-               b->updateMesh(daynight_ratio);
-               //b->setMeshExpired(true);
-       }
-       catch(InvalidPositionException &e){}
-       try{
-               v3s16 p = blockpos + v3s16(0,-1,0);
-               MapBlock *b = getBlockNoCreate(p);
-               b->updateMesh(daynight_ratio);
-               //b->setMeshExpired(true);
-       }
-       catch(InvalidPositionException &e){}
-       try{
-               v3s16 p = blockpos + v3s16(0,0,-1);
-               MapBlock *b = getBlockNoCreate(p);
-               b->updateMesh(daynight_ratio);
-               //b->setMeshExpired(true);
-       }
-       catch(InvalidPositionException &e){}
-}
-
-#if 0
-/*
-       Update mesh of block in which the node is, and if the node is at the
-       leading edge, update the appropriate leading blocks too.
-*/
-void ClientMap::updateNodeMeshes(v3s16 nodepos, u32 daynight_ratio)
-{
-       v3s16 dirs[4] = {
-               v3s16(0,0,0),
-               v3s16(-1,0,0),
-               v3s16(0,-1,0),
-               v3s16(0,0,-1),
-       };
-       v3s16 blockposes[4];
-       for(u32 i=0; i<4; i++)
-       {
-               v3s16 np = nodepos + dirs[i];
-               blockposes[i] = getNodeBlockPos(np);
-               // Don't update mesh of block if it has been done already
-               bool already_updated = false;
-               for(u32 j=0; j<i; j++)
-               {
-                       if(blockposes[j] == blockposes[i])
-                       {
-                               already_updated = true;
-                               break;
-                       }
-               }
-               if(already_updated)
-                       continue;
-               // Update mesh
-               MapBlock *b = getBlockNoCreate(blockposes[i]);
-               b->updateMesh(daynight_ratio);
-       }
-}
-#endif
-
 void ClientMap::PrintInfo(std::ostream &out)
 {
        out<<"ClientMap: ";