]> git.lizzy.rs Git - minetest.git/blobdiff - src/cavegen.cpp
Face shading: Add shade factor comments
[minetest.git] / src / cavegen.cpp
index 1beac6f27aa2469a0b9bd788f882349ea1daa6a5..bb6aa25a6ede7bf0f4acb93640056211756c4401 100644 (file)
@@ -23,9 +23,114 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "mapgen_v5.h"
 #include "mapgen_v6.h"
 #include "mapgen_v7.h"
+#include "mg_biome.h"
 #include "cavegen.h"
 
-NoiseParams nparams_caveliquids(0, 1, v3f(150.0, 150.0, 150.0), 776, 3, 0.6, 2.0);
+static NoiseParams nparams_caveliquids(0, 1, v3f(150.0, 150.0, 150.0), 776, 3, 0.6, 2.0);
+
+
+////
+//// CavesNoiseIntersection
+////
+
+CavesNoiseIntersection::CavesNoiseIntersection(
+       INodeDefManager *nodedef, BiomeManager *biomemgr, v3s16 chunksize,
+       NoiseParams *np_cave1, NoiseParams *np_cave2, s32 seed, float cave_width)
+{
+       assert(nodedef);
+       assert(biomemgr);
+
+       m_ndef = nodedef;
+       m_bmgr = biomemgr;
+
+       m_csize = chunksize;
+       m_cave_width = cave_width;
+
+       m_ystride    = m_csize.X;
+       m_zstride_1d = m_csize.X * (m_csize.Y + 1);
+
+       // Noises are created using 1-down overgeneration
+       // A Nx-by-1-by-Nz-sized plane is at the bottom of the desired for
+       // re-carving the solid overtop placed for blocking sunlight
+       noise_cave1 = new Noise(np_cave1, seed, m_csize.X, m_csize.Y + 1, m_csize.Z);
+       noise_cave2 = new Noise(np_cave2, seed, m_csize.X, m_csize.Y + 1, m_csize.Z);
+}
+
+
+CavesNoiseIntersection::~CavesNoiseIntersection()
+{
+       delete noise_cave1;
+       delete noise_cave2;
+}
+
+
+void CavesNoiseIntersection::generateCaves(MMVManip *vm,
+       v3s16 nmin, v3s16 nmax, u8 *biomemap)
+{
+       assert(vm);
+       assert(biomemap);
+
+       noise_cave1->perlinMap3D(nmin.X, nmin.Y - 1, nmin.Z);
+       noise_cave2->perlinMap3D(nmin.X, nmin.Y - 1, nmin.Z);
+
+       v3s16 em = vm->m_area.getExtent();
+       u32 index2d = 0;
+
+       for (s16 z = nmin.Z; z <= nmax.Z; z++)
+       for (s16 x = nmin.X; x <= nmax.X; x++, index2d++) {
+               bool column_is_open = false;  // Is column open to overground
+               bool is_under_river = false;  // Is column under river water
+               bool is_tunnel = false;  // Is tunnel or tunnel floor
+               u32 vi = vm->m_area.index(x, nmax.Y, z);
+               u32 index3d = (z - nmin.Z) * m_zstride_1d + m_csize.Y * m_ystride +
+                       (x - nmin.X);
+               // Biome of column
+               Biome *biome = (Biome *)m_bmgr->getRaw(biomemap[index2d]);
+
+               // Don't excavate the overgenerated stone at nmax.Y + 1,
+               // this creates a 'roof' over the tunnel, preventing light in
+               // tunnels at mapchunk borders when generating mapchunks upwards.
+               // This 'roof' is removed when the mapchunk above is generated.
+               for (s16 y = nmax.Y; y >= nmin.Y - 1; y--,
+                               index3d -= m_ystride,
+                               vm->m_area.add_y(em, vi, -1)) {
+
+                       content_t c = vm->m_data[vi].getContent();
+                       if (c == CONTENT_AIR || c == biome->c_water_top ||
+                                       c == biome->c_water) {
+                               column_is_open = true;
+                               continue;
+                       } else if (c == biome->c_river_water) {
+                               column_is_open = true;
+                               is_under_river = true;
+                               continue;
+                       }
+                       // Ground
+                       float d1 = contour(noise_cave1->result[index3d]);
+                       float d2 = contour(noise_cave2->result[index3d]);
+
+                       if (d1 * d2 > m_cave_width && m_ndef->get(c).is_ground_content) {
+                               // In tunnel and ground content, excavate
+                               vm->m_data[vi] = MapNode(CONTENT_AIR);
+                               is_tunnel = true;
+                       } else {
+                               // Not in tunnel or not ground content
+                               if (is_tunnel && column_is_open &&
+                                               (c == biome->c_filler || c == biome->c_stone)) {
+                                       // Tunnel entrance floor
+                                       if (is_under_river)
+                                               vm->m_data[vi] = MapNode(biome->c_riverbed);
+                                       else
+                                               vm->m_data[vi] = MapNode(biome->c_top);
+                               }
+
+                               column_is_open = false;
+                               is_tunnel = false;
+                       }
+               }
+       }
+}
+
 
 ////
 //// CavesRandomWalk
@@ -34,7 +139,7 @@ NoiseParams nparams_caveliquids(0, 1, v3f(150.0, 150.0, 150.0), 776, 3, 0.6, 2.0
 CavesRandomWalk::CavesRandomWalk(
        INodeDefManager *ndef,
        GenerateNotifier *gennotify,
-       int seed,
+       s32 seed,
        int water_level,
        content_t water_source,
        content_t lava_source)
@@ -78,8 +183,8 @@ void CavesRandomWalk::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax,
        this->ystride = nmax.X - nmin.X + 1;
 
        // Set initial parameters from randomness
-       dswitchint = ps->range(1, 14);
-       flooded    = ps->range(1, 2) == 2;
+       int dswitchint = ps->range(1, 14);
+       flooded = ps->range(1, 2) == 2;
 
        if (large_cave) {
                part_max_length_rs = ps->range(2, 4);
@@ -104,10 +209,10 @@ void CavesRandomWalk::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax,
 
        // Allow a bit more
        //(this should be more than the maximum radius of the tunnel)
-       s16 insure = 10;
+       const s16 insure = 10;
        s16 more = MYMAX(MAP_BLOCKSIZE - max_tunnel_diameter / 2 - insure, 1);
-       ar += v3s16(1,0,1) * more * 2;
-       of -= v3s16(1,0,1) * more;
+       ar += v3s16(1, 0, 1) * more * 2;
+       of -= v3s16(1, 0, 1) * more;
 
        route_y_min = 0;
        // Allow half a diameter + 7 over stone surface
@@ -133,11 +238,9 @@ void CavesRandomWalk::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax,
        route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y - 1);
 
        // Randomize starting position
-       orp = v3f(
-               (float)(ps->next() % ar.X) + 0.5,
-               (float)(ps->range(route_start_y_min, route_start_y_max)) + 0.5,
-               (float)(ps->next() % ar.Z) + 0.5
-       );
+       orp.Z = (float)(ps->next() % ar.Z) + 0.5f;
+       orp.Y = (float)(ps->range(route_start_y_min, route_start_y_max)) + 0.5f;
+       orp.X = (float)(ps->next() % ar.X) + 0.5f;
 
        // Add generation notify begin event
        if (gennotify) {
@@ -164,11 +267,10 @@ void CavesRandomWalk::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax,
 void CavesRandomWalk::makeTunnel(bool dirswitch)
 {
        if (dirswitch && !large_cave) {
-               main_direction = v3f(
-                       ((float)(ps->next() % 20) - (float)10) / 10,
-                       ((float)(ps->next() % 20) - (float)10) / 30,
-                       ((float)(ps->next() % 20) - (float)10) / 10
-               );
+               main_direction.Z = ((float)(ps->next() % 20) - (float)10) / 10;
+               main_direction.Y = ((float)(ps->next() % 20) - (float)10) / 30;
+               main_direction.X = ((float)(ps->next() % 20) - (float)10) / 10;
+
                main_direction *= (float)ps->range(0, 10) / 10;
        }
 
@@ -196,17 +298,13 @@ void CavesRandomWalk::makeTunnel(bool dirswitch)
        v3f vec;
        // Jump downward sometimes
        if (!large_cave && ps->range(0, 12) == 0) {
-               vec = v3f(
-                       (float)(ps->next() % (maxlen.X * 1)) - (float)maxlen.X / 2,
-                       (float)(ps->next() % (maxlen.Y * 2)) - (float)maxlen.Y,
-                       (float)(ps->next() % (maxlen.Z * 1)) - (float)maxlen.Z / 2
-               );
+               vec.Z = (float)(ps->next() % (maxlen.Z * 1)) - (float)maxlen.Z / 2;
+               vec.Y = (float)(ps->next() % (maxlen.Y * 2)) - (float)maxlen.Y;
+               vec.X = (float)(ps->next() % (maxlen.X * 1)) - (float)maxlen.X / 2;
        } else {
-               vec = v3f(
-                       (float)(ps->next() % (maxlen.X * 1)) - (float)maxlen.X / 2,
-                       (float)(ps->next() % (maxlen.Y * 1)) - (float)maxlen.Y / 2,
-                       (float)(ps->next() % (maxlen.Z * 1)) - (float)maxlen.Z / 2
-               );
+               vec.Z = (float)(ps->next() % (maxlen.Z * 1)) - (float)maxlen.Z / 2;
+               vec.Y = (float)(ps->next() % (maxlen.Y * 1)) - (float)maxlen.Y / 2;
+               vec.X = (float)(ps->next() % (maxlen.X * 1)) - (float)maxlen.X / 2;
        }
 
        // Do not make caves that are above ground.
@@ -237,14 +335,14 @@ void CavesRandomWalk::makeTunnel(bool dirswitch)
        vec = rp - orp;
 
        float veclen = vec.getLength();
-       if (veclen < 0.05)
-               veclen = 1.0;
+       if (veclen < 0.05f)
+               veclen = 1.0f;
 
        // Every second section is rough
        bool randomize_xz = (ps->range(1, 2) == 1);
 
        // Carve routes
-       for (float f = 0; f < 1.0; f += 1.0 / veclen)
+       for (float f = 0.f; f < 1.0f; f += 1.0f / veclen)
                carveRoute(vec, f, randomize_xz);
 
        orp = rp;
@@ -262,15 +360,15 @@ void CavesRandomWalk::carveRoute(v3f vec, float f, bool randomize_xz)
 
        float nval = NoisePerlin3D(np_caveliquids, startp.X,
                startp.Y, startp.Z, seed);
-       MapNode liquidnode = (nval < 0.40 && node_max.Y < lava_depth) ?
+       MapNode liquidnode = (nval < 0.40f && node_max.Y < lava_depth) ?
                lavanode : waternode;
 
        v3f fp = orp + vec * f;
-       fp.X += 0.1 * ps->range(-10, 10);
-       fp.Z += 0.1 * ps->range(-10, 10);
+       fp.X += 0.1f * ps->range(-10, 10);
+       fp.Z += 0.1f * ps->range(-10, 10);
        v3s16 cp(fp.X, fp.Y, fp.Z);
 
-       s16 d0 = -rs/2;
+       s16 d0 = -rs / 2;
        s16 d1 = d0 + rs;
        if (randomize_xz) {
                d0 += ps->range(-1, 1);
@@ -395,7 +493,7 @@ void CavesV6::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax,
        // Set initial parameters from randomness
        min_tunnel_diameter = 2;
        max_tunnel_diameter = ps->range(2, 6);
-       dswitchint          = ps->range(1, 14);
+       int dswitchint      = ps->range(1, 14);
        if (large_cave) {
                part_max_length_rs  = ps->range(2, 4);
                tunnel_routepoints  = ps->range(5, ps->range(15, 30));
@@ -417,7 +515,7 @@ void CavesV6::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax,
        // Allow a bit more
        //(this should be more than the maximum radius of the tunnel)
        const s16 max_spread_amount = MAP_BLOCKSIZE;
-       s16 insure = 10;
+       const s16 insure = 10;
        s16 more = MYMAX(max_spread_amount - max_tunnel_diameter / 2 - insure, 1);
        ar += v3s16(1, 0, 1) * more * 2;
        of -= v3s16(1, 0, 1) * more;
@@ -446,11 +544,9 @@ void CavesV6::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax,
        route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y - 1);
 
        // Randomize starting position
-       orp = v3f(
-               (float)(ps->next() % ar.X) + 0.5,
-               (float)(ps->range(route_start_y_min, route_start_y_max)) + 0.5,
-               (float)(ps->next() % ar.Z) + 0.5
-       );
+       orp.Z = (float)(ps->next() % ar.Z) + 0.5f;
+       orp.Y = (float)(ps->range(route_start_y_min, route_start_y_max)) + 0.5f;
+       orp.X = (float)(ps->next() % ar.X) + 0.5f;
 
        // Add generation notify begin event
        if (gennotify != NULL) {
@@ -477,11 +573,10 @@ void CavesV6::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax,
 void CavesV6::makeTunnel(bool dirswitch)
 {
        if (dirswitch && !large_cave) {
-               main_direction = v3f(
-                       ((float)(ps->next() % 20) - (float)10) / 10,
-                       ((float)(ps->next() % 20) - (float)10) / 30,
-                       ((float)(ps->next() % 20) - (float)10) / 10
-               );
+               main_direction.Z = ((float)(ps->next() % 20) - (float)10) / 10;
+               main_direction.Y = ((float)(ps->next() % 20) - (float)10) / 30;
+               main_direction.X = ((float)(ps->next() % 20) - (float)10) / 10;
+
                main_direction *= (float)ps->range(0, 10) / 10;
        }
 
@@ -506,19 +601,16 @@ void CavesV6::makeTunnel(bool dirswitch)
                );
        }
 
-       v3f vec(
-               (float)(ps->next() % maxlen.X) - (float)maxlen.X / 2,
-               (float)(ps->next() % maxlen.Y) - (float)maxlen.Y / 2,
-               (float)(ps->next() % maxlen.Z) - (float)maxlen.Z / 2
-       );
+       v3f vec;
+       vec.Z = (float)(ps->next() % maxlen.Z) - (float)maxlen.Z / 2;
+       vec.Y = (float)(ps->next() % maxlen.Y) - (float)maxlen.Y / 2;
+       vec.X = (float)(ps->next() % maxlen.X) - (float)maxlen.X / 2;
 
        // Jump downward sometimes
        if (!large_cave && ps->range(0, 12) == 0) {
-               vec = v3f(
-                       (float)(ps->next() % maxlen.X) - (float)maxlen.X / 2,
-                       (float)(ps->next() % (maxlen.Y * 2)) - (float)maxlen.Y,
-                       (float)(ps->next() % maxlen.Z) - (float)maxlen.Z / 2
-               );
+               vec.Z = (float)(ps->next() % maxlen.Z) - (float)maxlen.Z / 2;
+               vec.Y = (float)(ps->next() % (maxlen.Y * 2)) - (float)maxlen.Y;
+               vec.X = (float)(ps->next() % maxlen.X) - (float)maxlen.X / 2;
        }
 
        // Do not make caves that are entirely above ground, to fix shadow bugs
@@ -556,14 +648,14 @@ void CavesV6::makeTunnel(bool dirswitch)
 
        float veclen = vec.getLength();
        // As odd as it sounds, veclen is *exactly* 0.0 sometimes, causing a FPE
-       if (veclen < 0.05)
-               veclen = 1.0;
+       if (veclen < 0.05f)
+               veclen = 1.0f;
 
        // Every second section is rough
        bool randomize_xz = (ps2->range(1, 2) == 1);
 
        // Carve routes
-       for (float f = 0; f < 1.0; f += 1.0 / veclen)
+       for (float f = 0.f; f < 1.0f; f += 1.0f / veclen)
                carveRoute(vec, f, randomize_xz, tunnel_above_ground);
 
        orp = rp;
@@ -581,8 +673,8 @@ void CavesV6::carveRoute(v3f vec, float f, bool randomize_xz,
        startp += of;
 
        v3f fp = orp + vec * f;
-       fp.X += 0.1 * ps->range(-10, 10);
-       fp.Z += 0.1 * ps->range(-10, 10);
+       fp.X += 0.1f * ps->range(-10, 10);
+       fp.Z += 0.1f * ps->range(-10, 10);
        v3s16 cp(fp.X, fp.Y, fp.Z);
 
        s16 d0 = -rs / 2;