Translated using Weblate (Japanese)
[oweals/minetest.git] / src / mapblock.h
index 9e36fe113c0139ef1d1d39452ae57f412ba0710c..334136b92f0f442d404a144d23c4ce18781ee34f 100644 (file)
@@ -31,6 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "nodetimer.h"
 #include "modifiedstate.h"
 #include "util/numeric.h" // getContainerPos
+#include "settings.h"
 
 class Map;
 class NodeMetadataList;
@@ -145,9 +146,8 @@ public:
        void reallocate()
        {
                delete[] data;
-               u32 datasize = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
-               data = new MapNode[datasize];
-               for (u32 i = 0; i < datasize; i++)
+               data = new MapNode[nodecount];
+               for (u32 i = 0; i < nodecount; i++)
                        data[i] = MapNode(CONTENT_IGNORE);
 
                raiseModified(MOD_STATE_WRITE_NEEDED, MOD_REASON_REALLOCATE);
@@ -259,7 +259,7 @@ public:
 
        inline v3s16 getPosRelative()
        {
-               return m_pos * MAP_BLOCKSIZE;
+               return m_pos_relative;
        }
 
        inline core::aabbox3d<s16> getBox()
@@ -294,7 +294,7 @@ public:
                if (!*valid_position)
                        return MapNode(CONTENT_IGNORE);
 
-               return data[z * MAP_BLOCKSIZE * MAP_BLOCKSIZE + y * MAP_BLOCKSIZE + x];
+               return data[z * zstride + y * ystride + x];
        }
 
        inline MapNode getNode(v3s16 p, bool *valid_position)
@@ -553,6 +553,8 @@ public:
        static const u32 ystride = MAP_BLOCKSIZE;
        static const u32 zstride = MAP_BLOCKSIZE * MAP_BLOCKSIZE;
 
+       static const u32 nodecount = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
+
 private:
        /*
                Private member variables
@@ -563,6 +565,14 @@ private:
        // Position in blocks on parent
        v3s16 m_pos;
 
+       /* This is the precalculated m_pos_relative value
+       * This caches the value, improving performance by removing 3 s16 multiplications
+       * at runtime on each getPosRelative call
+       * For a 5 minutes runtime with valgrind this removes 3 * 19M s16 multiplications
+       * The gain can be estimated in Release Build to 3 * 100M multiply operations for 5 mins
+       */
+       v3s16 m_pos_relative;
+
        IGameDef *m_gamedef;
 
        /*
@@ -629,13 +639,14 @@ typedef std::vector<MapBlock*> MapBlockVect;
 
 inline bool blockpos_over_limit(v3s16 p)
 {
-       return
-         (p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
-       || p.X >  MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
-       || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
-       || p.Y >  MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
-       || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
-       || p.Z >  MAP_GENERATION_LIMIT / MAP_BLOCKSIZE);
+       const static u16 map_gen_limit = MYMIN(MAX_MAP_GENERATION_LIMIT,
+               g_settings->getU16("map_generation_limit"));
+       return (p.X < -map_gen_limit / MAP_BLOCKSIZE
+                       || p.X >  map_gen_limit / MAP_BLOCKSIZE
+                       || p.Y < -map_gen_limit / MAP_BLOCKSIZE
+                       || p.Y >  map_gen_limit / MAP_BLOCKSIZE
+                       || p.Z < -map_gen_limit / MAP_BLOCKSIZE
+                       || p.Z >  map_gen_limit / MAP_BLOCKSIZE);
 }
 
 /*
@@ -672,4 +683,3 @@ inline void getNodeSectorPosWithOffset(const v2s16 &p, v2s16 &block, v2s16 &offs
 std::string analyze_block(MapBlock *block);
 
 #endif
-