Noise: Automatically transform noise maps if needed
authorkwolekr <kwolekr@minetest.net>
Thu, 11 Dec 2014 04:35:37 +0000 (23:35 -0500)
committerkwolekr <kwolekr@minetest.net>
Thu, 11 Dec 2014 04:35:37 +0000 (23:35 -0500)
src/mapgen_v5.cpp
src/mapgen_v6.cpp
src/mapgen_v7.cpp
src/noise.cpp
src/noise.h
src/script/lua_api/l_noise.cpp

index f2cb7917904693f61dd4dbfe83daf2fe45bd8418..0ec19ebda42cf621b6f2b356de7f80a03e09f6ef 100644 (file)
@@ -299,14 +299,10 @@ void MapgenV5::calculateNoise() {
        noise_filler_depth->perlinMap2D(x, z);
        noise_factor->perlinMap2D(x, z);
        noise_height->perlinMap2D(x, z);
-       noise_height->transformNoiseMap();
 
        noise_cave1->perlinMap3D(x, y, z);
-       noise_cave1->transformNoiseMap();
        noise_cave2->perlinMap3D(x, y, z);
-       noise_cave2->transformNoiseMap();
        noise_ground->perlinMap3D(x, y, z);
-       noise_ground->transformNoiseMap();
 
        if (spflags & MGV5_BLOBS) {
                noise_crumble->perlinMap3D(x, y, z);
index 04c3b147eed8cfe00c9cce346216348b25c22105..004aef8c7f4d48a1052fd18af8a41eb5435d62e7 100644 (file)
@@ -125,7 +125,7 @@ MapgenV6Params::MapgenV6Params() {
        np_terrain_base   = NoiseParams(-4,  20.0, v3f(250.0, 250.0, 250.0), 82341,  5, 0.6, 2.0);
        np_terrain_higher = NoiseParams(20,  16.0, v3f(500.0, 500.0, 500.0), 85039,  5, 0.6, 2.0);
        np_steepness      = NoiseParams(0.85,0.5,  v3f(125.0, 125.0, 125.0), -932,   5, 0.7, 2.0);
-       np_height_select  = NoiseParams(0.5, 1.0,  v3f(250.0, 250.0, 250.0), 4213,   5, 0.69, 2.0);
+       np_height_select  = NoiseParams(0,   1.0,  v3f(250.0, 250.0, 250.0), 4213,   5, 0.69, 2.0);
        np_mud            = NoiseParams(4,   2.0,  v3f(200.0, 200.0, 200.0), 91013,  3, 0.55, 2.0);
        np_beach          = NoiseParams(0,   1.0,  v3f(250.0, 250.0, 250.0), 59420,  3, 0.50, 2.0);
        np_biome          = NoiseParams(0,   1.0,  v3f(250.0, 250.0, 250.0), 9130,   3, 0.50, 2.0);
@@ -552,17 +552,14 @@ void MapgenV6::calculateNoise() {
                noise_terrain_base->perlinMap2D(
                        x + 0.5 * noise_terrain_base->np.spread.X,
                        z + 0.5 * noise_terrain_base->np.spread.Z);
-               noise_terrain_base->transformNoiseMap();
 
                noise_terrain_higher->perlinMap2D(
                        x + 0.5 * noise_terrain_higher->np.spread.X,
                        z + 0.5 * noise_terrain_higher->np.spread.Z);
-               noise_terrain_higher->transformNoiseMap();
 
                noise_steepness->perlinMap2D(
                        x + 0.5 * noise_steepness->np.spread.X,
                        z + 0.5 * noise_steepness->np.spread.Z);
-               noise_steepness->transformNoiseMap();
 
                noise_height_select->perlinMap2D(
                        x + 0.5 * noise_height_select->np.spread.X,
@@ -571,7 +568,6 @@ void MapgenV6::calculateNoise() {
                noise_mud->perlinMap2D(
                        x + 0.5 * noise_mud->np.spread.X,
                        z + 0.5 * noise_mud->np.spread.Z);
-               noise_mud->transformNoiseMap();
        }
 
        noise_beach->perlinMap2D(
index be60a2c98ff3ccf73bdc7ec320268c645b2da782..097e709341ef54123a6beb37c79a8bd435797bd5 100644 (file)
@@ -270,26 +270,18 @@ void MapgenV7::calculateNoise() {
        int z = node_min.Z;
 
        noise_height_select->perlinMap2D(x, z);
-       noise_height_select->transformNoiseMap();
-
        noise_terrain_persist->perlinMap2D(x, z);
-       noise_terrain_persist->transformNoiseMap();
        float *persistmap = noise_terrain_persist->result;
        for (int i = 0; i != csize.X * csize.Z; i++)
                persistmap[i] = rangelim(persistmap[i], 0.4, 0.9);
 
        noise_terrain_base->perlinMap2D(x, z, persistmap);
-       noise_terrain_base->transformNoiseMap();
-
        noise_terrain_alt->perlinMap2D(x, z, persistmap);
-       noise_terrain_alt->transformNoiseMap();
-
        noise_filler_depth->perlinMap2D(x, z);
 
        if (spflags & MGV7_MOUNTAINS) {
                noise_mountain->perlinMap3D(x, y, z);
                noise_mount_height->perlinMap2D(x, z);
-               noise_mount_height->transformNoiseMap();
        }
 
        if (spflags & MGV7_RIDGES) {
index 0d8b118aa8d88b602089b8192dc24921260653c8..069c60d44ef9695ce69df7432fa6c91d455fc560 100644 (file)
@@ -611,6 +611,11 @@ float *Noise::perlinMap2D(float x, float y, float *persistence_map)
                g *= np.persist;
        }
 
+       if (fabs(np.offset - 0.f) > 0.00001 || fabs(np.scale - 1.f) > 0.00001) {
+               for (size_t i = 0; i != bufsize; i++)
+                       result[i] = result[i] * np.scale + np.offset;
+       }
+
        return result;
 }
 
@@ -644,6 +649,11 @@ float *Noise::perlinMap3D(float x, float y, float z, float *persistence_map)
                g *= np.persist;
        }
 
+       if (fabs(np.offset - 0.f) > 0.00001 || fabs(np.scale - 1.f) > 0.00001) {
+               for (size_t i = 0; i != bufsize; i++)
+                       result[i] = result[i] * np.scale + np.offset;
+       }
+
        return result;
 }
 
@@ -675,16 +685,3 @@ void Noise::updateResults(float g, float *gmap,
                }
        }
 }
-
-
-void Noise::transformNoiseMap()
-{
-       // Because sx, sy, and sz are object members whose values may conceivably be
-       // modified in other threads. gcc (at least) will consider the buffer size
-       // computation as invalidated between loop comparisons, resulting in a ~2x
-       // slowdown even with -O2.  To prevent this, store the value in a local.
-       size_t bufsize = sx * sy * sz;
-       for (size_t i = 0; i != bufsize; i++)
-               result[i] = result[i] * np.scale + np.offset;
-}
-
index 66be8db93d440cd0d781c58181474e9a8c5ba644..2cdf4203d2fcae4bf84a70e75742e40b2a7f76d3 100644 (file)
@@ -151,8 +151,6 @@ public:
        float *perlinMap2D(float x, float y, float *persistence_map=NULL);
        float *perlinMap3D(float x, float y, float z, float *persistence_map=NULL);
 
-       void transformNoiseMap();
-
 private:
        void allocBuffers();
        void resizeNoiseBuf(bool is3d);
index c66f54e6f3593553ce8860476403a8aa808ba548..6c6b353584b28b5151be236527d8e1c9efd50093 100644 (file)
@@ -149,7 +149,7 @@ int LuaPerlinNoiseMap::gc_object(lua_State *L)
 int LuaPerlinNoiseMap::l_get2dMap(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
-       int i = 0;
+       size_t i = 0;
 
        LuaPerlinNoiseMap *o = checkobject(L, 1);
        v2f p = read_v2f(L, 2);
@@ -161,8 +161,7 @@ int LuaPerlinNoiseMap::l_get2dMap(lua_State *L)
        for (int y = 0; y != n->sy; y++) {
                lua_newtable(L);
                for (int x = 0; x != n->sx; x++) {
-                       float noiseval = n->np.offset + n->np.scale * n->result[i++];
-                       lua_pushnumber(L, noiseval);
+                       lua_pushnumber(L, n->result[i++]);
                        lua_rawseti(L, -2, x + 1);
                }
                lua_rawseti(L, -2, y + 1);
@@ -181,12 +180,11 @@ int LuaPerlinNoiseMap::l_get2dMap_flat(lua_State *L)
        Noise *n = o->noise;
        n->perlinMap2D(p.X, p.Y);
 
-       int maplen = n->sx * n->sy;
+       size_t maplen = n->sx * n->sy;
 
        lua_newtable(L);
-       for (int i = 0; i != maplen; i++) {
-               float noiseval = n->np.offset + n->np.scale * n->result[i];
-               lua_pushnumber(L, noiseval);
+       for (size_t i = 0; i != maplen; i++) {
+               lua_pushnumber(L, n->result[i]);
                lua_rawseti(L, -2, i + 1);
        }
        return 1;
@@ -196,7 +194,7 @@ int LuaPerlinNoiseMap::l_get2dMap_flat(lua_State *L)
 int LuaPerlinNoiseMap::l_get3dMap(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
-       int i = 0;
+       size_t i = 0;
 
        LuaPerlinNoiseMap *o = checkobject(L, 1);
        v3f p = read_v3f(L, 2);
@@ -210,7 +208,7 @@ int LuaPerlinNoiseMap::l_get3dMap(lua_State *L)
                for (int y = 0; y != n->sy; y++) {
                        lua_newtable(L);
                        for (int x = 0; x != n->sx; x++) {
-                               lua_pushnumber(L, n->np.offset + n->np.scale * n->result[i++]);
+                               lua_pushnumber(L, n->result[i++]);
                                lua_rawseti(L, -2, x + 1);
                        }
                        lua_rawseti(L, -2, y + 1);
@@ -231,13 +229,11 @@ int LuaPerlinNoiseMap::l_get3dMap_flat(lua_State *L)
        Noise *n = o->noise;
        n->perlinMap3D(p.X, p.Y, p.Z);
 
-
-       int maplen = n->sx * n->sy * n->sz;
+       size_t maplen = n->sx * n->sy * n->sz;
 
        lua_newtable(L);
-       for (int i = 0; i != maplen; i++) {
-               float noiseval = n->np.offset + n->np.scale * n->result[i];
-               lua_pushnumber(L, noiseval);
+       for (size_t i = 0; i != maplen; i++) {
+               lua_pushnumber(L, n->result[i]);
                lua_rawseti(L, -2, i + 1);
        }
        return 1;