GenericNodeMetadata and an example furnace
[oweals/minetest.git] / src / map.cpp
index 7d5bd700d7855d437cd373597833ffc0d004b4f3..34bc31ba4098300c5a3b1cbde388ae1f6450ad96 100644 (file)
@@ -208,6 +208,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(errorstream);
+               return;
+       }
        block->setNodeNoCheck(relpos, n);
 }
 
@@ -851,15 +860,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
@@ -994,13 +1003,17 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n,
        /*
                Add intial metadata
        */
-
-       NodeMetadata *meta_proto = nodemgr->get(n).initial_metadata;
-       if(meta_proto)
-       {
-               NodeMetadata *meta = meta_proto->clone(m_gamedef);
-               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 {
+                       meta->setOwner(player_name);
+                       setNodeMetadata(p, meta);
+               }
        }
 
        /*
@@ -1397,6 +1410,7 @@ void Map::timerUpdate(float dtime, float unload_timeout,
        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;
 
@@ -1442,6 +1456,7 @@ void Map::timerUpdate(float dtime, float unload_timeout,
                        else
                        {
                                all_blocks_deleted = false;
+                               block_count_all++;
                        }
                }
 
@@ -1462,6 +1477,7 @@ 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:
@@ -1603,7 +1619,7 @@ void Map::transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks)
                switch (liquid_type) {
                        case LIQUID_SOURCE:
                                liquid_level = LIQUID_LEVEL_SOURCE;
-                               liquid_kind = nodemgr->get(n0).liquid_alternative_flowing;
+                               liquid_kind = nodemgr->getId(nodemgr->get(n0).liquid_alternative_flowing);
                                break;
                        case LIQUID_FLOWING:
                                liquid_level = (n0.param2 & LIQUID_LEVEL_MASK);
@@ -1663,8 +1679,8 @@ 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 = nodemgr->get(nb.n.getContent()).liquid_alternative_flowing;
-                                       if (nodemgr->get(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;
@@ -1673,8 +1689,8 @@ void Map::transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks)
                                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 = nodemgr->get(nb.n.getContent()).liquid_alternative_flowing;
-                                       if (nodemgr->get(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;
@@ -1695,7 +1711,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 = nodemgr->get(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;
@@ -2000,7 +2016,7 @@ ServerMap::ServerMap(std::string savedir, IGameDef *gamedef):
        emergeSector(v2s16(0,0));
 
        // Initially write whole map
-       save(false);
+       save(MOD_STATE_CLEAN);
 }
 
 ServerMap::~ServerMap()
@@ -2012,7 +2028,7 @@ ServerMap::~ServerMap()
                if(m_map_saving_enabled)
                {
                        // Save only changed parts
-                       save(true);
+                       save(MOD_STATE_WRITE_AT_UNLOAD);
                        infostream<<"Server: saved map to "<<m_savedir<<std::endl;
                }
                else
@@ -2151,7 +2167,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
@@ -2303,7 +2329,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;*/
@@ -2811,7 +2837,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)
@@ -2820,11 +2846,11 @@ 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();
        }
@@ -2845,7 +2871,7 @@ void ServerMap::save(bool only_changed)
                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++;
@@ -2860,8 +2886,7 @@ void ServerMap::save(bool only_changed)
                        
                        block_count_all++;
 
-                       if(block->getModified() >= MOD_STATE_WRITE_NEEDED 
-                                       || only_changed == false)
+                       if(block->getModified() >= save_level)
                        {
                                // Lazy beginSave()
                                if(!save_started){
@@ -2888,7 +2913,7 @@ void ServerMap::save(bool only_changed)
        /*
                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: "