]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/mapblock.cpp
Wieldmesh: don't force anisotropic filtering on, instead disable mipmaps
[dragonfireclient.git] / src / mapblock.cpp
index a6e9b3951f9d8103bbc53176d2f99d39b547e3d0..255b661b0beac3dead9edd4b82dc4d36e952cca7 100644 (file)
@@ -21,8 +21,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #include <sstream>
 #include "map.h"
-// For g_settings
-#include "main.h"
 #include "light.h"
 #include "nodedef.h"
 #include "nodemetadata.h"
@@ -31,6 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "nameidmapping.h"
 #include "content_mapnode.h" // For legacy name-id mapping
 #include "content_nodemeta.h" // For legacy deserialization
+#include "serialization.h"
 #ifndef SERVER
 #include "mapblock_mesh.h"
 #endif
@@ -65,7 +64,6 @@ MapBlock::MapBlock(Map *parent, v3s16 pos, IGameDef *gamedef, bool dummy):
                reallocate();
        
 #ifndef SERVER
-       //mesh_mutex.Init();
        mesh = NULL;
 #endif
 }
@@ -99,54 +97,19 @@ bool MapBlock::isValidPositionParent(v3s16 p)
        }
 }
 
-MapNode MapBlock::getNodeParent(v3s16 p)
+MapNode MapBlock::getNodeParent(v3s16 p, bool *is_valid_position)
 {
-       if(isValidPosition(p) == false)
-       {
-               return m_parent->getNode(getPosRelative() + p);
-       }
-       else
-       {
-               if(data == NULL)
-                       throw InvalidPositionException();
-               return data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X];
-       }
-}
+       if (isValidPosition(p) == false)
+               return m_parent->getNodeNoEx(getPosRelative() + p, is_valid_position);
 
-void MapBlock::setNodeParent(v3s16 p, MapNode & n)
-{
-       if(isValidPosition(p) == false)
-       {
-               m_parent->setNode(getPosRelative() + p, n);
-       }
-       else
-       {
-               if(data == NULL)
-                       throw InvalidPositionException();
-               data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X] = n;
-       }
-}
-
-MapNode MapBlock::getNodeParentNoEx(v3s16 p)
-{
-       if(isValidPosition(p) == false)
-       {
-               try{
-                       return m_parent->getNode(getPosRelative() + p);
-               }
-               catch(InvalidPositionException &e)
-               {
-                       return MapNode(CONTENT_IGNORE);
-               }
-       }
-       else
-       {
-               if(data == NULL)
-               {
-                       return MapNode(CONTENT_IGNORE);
-               }
-               return data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X];
+       if (data == NULL) {
+               if (is_valid_position)
+                       *is_valid_position = false;
+               return MapNode(CONTENT_IGNORE);
        }
+       if (is_valid_position)
+               *is_valid_position = true;
+       return data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X];
 }
 
 /*
@@ -168,7 +131,7 @@ MapNode MapBlock::getNodeParentNoEx(v3s16 p)
        if black_air_left!=NULL, it is set to true if non-sunlighted
        air is left in block.
 */
-bool MapBlock::propagateSunlight(core::map<v3s16, bool> & light_sources,
+bool MapBlock::propagateSunlight(std::set<v3s16> & light_sources,
                bool remove_light, bool *black_air_left)
 {
        INodeDefManager *nodemgr = m_gamedef->ndef();
@@ -185,9 +148,14 @@ bool MapBlock::propagateSunlight(core::map<v3s16, bool> & light_sources,
 #if 1
                        bool no_sunlight = false;
                        bool no_top_block = false;
+
                        // Check if node above block has sunlight
-                       try{
-                               MapNode n = getNodeParent(v3s16(x, MAP_BLOCKSIZE, z));
+
+                       bool is_valid_position;
+                       MapNode n = getNodeParent(v3s16(x, MAP_BLOCKSIZE, z),
+                               &is_valid_position);
+                       if (is_valid_position)
+                       {
                                if(n.getContent() == CONTENT_IGNORE)
                                {
                                        // Trust heuristics
@@ -198,7 +166,7 @@ bool MapBlock::propagateSunlight(core::map<v3s16, bool> & light_sources,
                                        no_sunlight = true;
                                }
                        }
-                       catch(InvalidPositionException &e)
+                       else
                        {
                                no_top_block = true;
                                
@@ -210,7 +178,7 @@ bool MapBlock::propagateSunlight(core::map<v3s16, bool> & light_sources,
                                }
                                else
                                {
-                                       MapNode n = getNode(v3s16(x, MAP_BLOCKSIZE-1, z));
+                                       MapNode n = getNodeNoEx(v3s16(x, MAP_BLOCKSIZE-1, z));
                                        if(m_gamedef->ndef()->get(n).sunlight_propagates == false)
                                        {
                                                no_sunlight = true;
@@ -287,7 +255,7 @@ bool MapBlock::propagateSunlight(core::map<v3s16, bool> & light_sources,
                                
                                if(diminish_light(current_light) != 0)
                                {
-                                       light_sources.insert(pos_relative + pos, true);
+                                       light_sources.insert(pos_relative + pos);
                                }
 
                                if(current_light == 0 && stopped_to_solid_object)
@@ -310,27 +278,27 @@ bool MapBlock::propagateSunlight(core::map<v3s16, bool> & light_sources,
                                
                                Ignore non-transparent nodes as they always have no light
                        */
-                       try
-                       {
+
                        if(block_below_is_valid)
                        {
-                               MapNode n = getNodeParent(v3s16(x, -1, z));
-                               if(nodemgr->get(n).light_propagates)
+                               MapNode n = getNodeParent(v3s16(x, -1, z), &is_valid_position);
+                               if (is_valid_position) {
+                                       if(nodemgr->get(n).light_propagates)
+                                       {
+                                               if(n.getLight(LIGHTBANK_DAY, nodemgr) == LIGHT_SUN
+                                                               && sunlight_should_go_down == false)
+                                                       block_below_is_valid = false;
+                                               else if(n.getLight(LIGHTBANK_DAY, nodemgr) != LIGHT_SUN
+                                                               && sunlight_should_go_down == true)
+                                                       block_below_is_valid = false;
+                                       }
+                               }
+                               else
                                {
-                                       if(n.getLight(LIGHTBANK_DAY, nodemgr) == LIGHT_SUN
-                                                       && sunlight_should_go_down == false)
-                                               block_below_is_valid = false;
-                                       else if(n.getLight(LIGHTBANK_DAY, nodemgr) != LIGHT_SUN
-                                                       && sunlight_should_go_down == true)
-                                               block_below_is_valid = false;
+                                       /*std::cout<<"InvalidBlockException for bottom block node"
+                                                       <<std::endl;*/
+                                       // Just no block below, no need to panic.
                                }
-                       }//if
-                       }//try
-                       catch(InvalidPositionException &e)
-                       {
-                               /*std::cout<<"InvalidBlockException for bottom block node"
-                                               <<std::endl;*/
-                               // Just no block below, no need to panic.
                        }
                }
        }
@@ -454,10 +422,16 @@ s16 MapBlock::getGroundLevel(v2s16 p2d)
 */
 // List relevant id-name pairs for ids in the block using nodedef
 // Renumbers the content IDs (starting at 0 and incrementing
+// use static memory requires about 65535 * sizeof(int) ram in order to be
+// sure we can handle all content ids. But it's absolutely worth it as it's
+// a speedup of 4 for one of the major time consuming functions on storing
+// mapblocks.
+static content_t getBlockNodeIdMapping_mapping[USHRT_MAX];
 static void getBlockNodeIdMapping(NameIdMapping *nimap, MapNode *nodes,
                INodeDefManager *nodedef)
 {
-       std::map<content_t, content_t> mapping;
+       memset(getBlockNodeIdMapping_mapping, 0xFF, USHRT_MAX * sizeof(content_t));
+
        std::set<content_t> unknown_contents;
        content_t id_counter = 0;
        for(u32 i=0; i<MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE; i++)
@@ -466,16 +440,14 @@ static void getBlockNodeIdMapping(NameIdMapping *nimap, MapNode *nodes,
                content_t id = CONTENT_IGNORE;
 
                // Try to find an existing mapping
-               std::map<content_t, content_t>::iterator j = mapping.find(global_id);
-               if(j != mapping.end())
-               {
-                       id = j->second;
+               if (getBlockNodeIdMapping_mapping[global_id] != 0xFFFF) {
+                       id = getBlockNodeIdMapping_mapping[global_id];
                }
                else
                {
                        // We have to assign a new mapping
                        id = id_counter++;
-                       mapping.insert(std::make_pair(global_id, id));
+                       getBlockNodeIdMapping_mapping[global_id] = id;
 
                        const ContentFeatures &f = nodedef->get(global_id);
                        const std::string &name = f.name;
@@ -635,6 +607,21 @@ void MapBlock::serialize(std::ostream &os, u8 version, bool disk)
        }
 }
 
+void MapBlock::serializeNetworkSpecific(std::ostream &os, u16 net_proto_version)
+{
+       if(data == NULL)
+       {
+               throw SerializationError("ERROR: Not writing dummy block.");
+       }
+
+       if(net_proto_version >= 21){
+               int version = 1;
+               writeU8(os, version);
+               writeF1000(os, 0); // deprecated heat
+               writeF1000(os, 0); // deprecated humidity
+       }
+}
+
 void MapBlock::deSerialize(std::istream &is, u8 version, bool disk)
 {
        if(!ser_ver_supported(version))
@@ -740,6 +727,24 @@ void MapBlock::deSerialize(std::istream &is, u8 version, bool disk)
                        <<": Done."<<std::endl);
 }
 
+void MapBlock::deSerializeNetworkSpecific(std::istream &is)
+{
+       try {
+               int version = readU8(is);
+               //if(version != 1)
+               //      throw SerializationError("unsupported MapBlock version");
+               if(version >= 1) {
+                       readF1000(is); // deprecated heat
+                       readF1000(is); // deprecated humidity
+               }
+       }
+       catch(SerializationError &e)
+       {
+               errorstream<<"WARNING: MapBlock::deSerializeNetworkSpecific(): Ignoring an error"
+                               <<": "<<e.what()<<std::endl;
+       }
+}
+
 /*
        Legacy serialization
 */
@@ -1035,7 +1040,7 @@ std::string analyze_block(MapBlock *block)
                for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
                {
                        v3s16 p(x0,y0,z0);
-                       MapNode n = block->getNode(p);
+                       MapNode n = block->getNodeNoEx(p);
                        content_t c = n.getContent();
                        if(c == CONTENT_IGNORE)
                                some_ignore = true;