Enhance caves for mgv7, add ravines
authorkwolekr <kwolekr@minetest.net>
Sun, 28 Apr 2013 02:40:52 +0000 (22:40 -0400)
committerkwolekr <kwolekr@minetest.net>
Sun, 28 Apr 2013 02:40:52 +0000 (22:40 -0400)
src/cavegen.cpp
src/cavegen.h
src/mapgen_indev.cpp
src/mapgen_indev.h
src/mapgen_v6.cpp
src/mapgen_v7.cpp

index bef50c7a7df4c910a743cbf4abd56b48f2734a35..01543252a718c99ba9bd4f30c9b2355779ad912f 100644 (file)
@@ -20,18 +20,21 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "util/numeric.h"
 #include "map.h"
 #include "mapgen.h"
+#include "mapgen_v6.h"
+#include "mapgen_v7.h"
 #include "cavegen.h"
 
 
-CaveV6::CaveV6(Mapgen *mg, PseudoRandom *ps, PseudoRandom *ps2,
-                       bool is_large_cave, content_t c_water, content_t c_lava) {
-       this->vm = mg->vm;
+CaveV6::CaveV6(MapgenV6 *mg, PseudoRandom *ps, PseudoRandom *ps2, bool is_large_cave) {
+       this->mg   = mg;
+       this->vm   = mg->vm;
+       this->ndef = mg->ndef;
        this->water_level = mg->water_level;
        this->large_cave = is_large_cave;
        this->ps  = ps;
        this->ps2 = ps2;
-       this->c_water_source = c_water;
-       this->c_lava_source  = c_lava;
+       this->c_water_source = mg->c_water_source;
+       this->c_lava_source  = mg->c_lava_source;
 
        min_tunnel_diameter = 2;
        max_tunnel_diameter = ps->range(2, 6);
@@ -152,13 +155,6 @@ void CaveV6::makeTunnel(bool dirswitch) {
                );
        }
 
-       /*if(large_cave){
-               v3f p = orp + vec;
-               s16 h = find_ground_level_clever(vmanip, v2s16(p.X, p.Z), ndef);
-               route_y_min = h - rs/3;
-               route_y_max = h + rs;
-       }*/
-
        vec += main_direction;
 
        v3f rp = orp + vec;
@@ -181,7 +177,7 @@ void CaveV6::makeTunnel(bool dirswitch) {
 
        float veclen = vec.getLength();
        // As odd as it sounds, veclen is *exactly* 0.0 sometimes, causing a FPE
-       if (veclen == 0.0)
+       if (veclen < 0.05)
                veclen = 1.0;
                
        // Every second section is rough
@@ -220,11 +216,7 @@ void CaveV6::carveRoute(v3f vec, float f, bool randomize_xz) {
                for (s16 x0 = -si - ps->range(0,1); x0 <= si - 1 + ps->range(0,1); x0++) {
                        s16 maxabsxz = MYMAX(abs(x0), abs(z0));
                        s16 si2 = rs / 2 - MYMAX(0, maxabsxz - rs / 7 - 1);
-                       for (s16 y0 = -si2; y0 <= si2; y0++) {
-                               // Make better floors in small caves
-                               //if(y0 <= -rs/2 && rs<=7)
-                               //      continue;
-                               
+                       for (s16 y0 = -si2; y0 <= si2; y0++) {                          
                                if (large_cave_is_flat) {
                                        // Make large caves not so tall
                                        if (rs > 7 && abs(y0) >= rs / 3)
@@ -242,7 +234,7 @@ void CaveV6::carveRoute(v3f vec, float f, bool randomize_xz) {
                                if (large_cave) {
                                        int full_ymin = node_min.Y - MAP_BLOCKSIZE;
                                        int full_ymax = node_max.Y + MAP_BLOCKSIZE;
-                                       
+
                                        if (flooded && full_ymin < water_level && full_ymax > water_level) {
                                                vm->m_data[i] = (p.Y <= water_level) ? waternode : airnode;
                                        } else if (flooded && full_ymax < water_level) {
@@ -264,3 +256,278 @@ void CaveV6::carveRoute(v3f vec, float f, bool randomize_xz) {
                }
        }
 }
+
+
+///////////////////////////////////////// Caves V7
+
+CaveV7::CaveV7(MapgenV7 *mg, PseudoRandom *ps, bool is_large_cave) {
+       this->mg   = mg;
+       this->vm   = mg->vm;
+       this->ndef = mg->ndef;
+       this->water_level = mg->water_level;
+       this->large_cave  = is_large_cave;
+       this->ps = ps;
+       this->c_water_source = mg->c_water_source;
+       this->c_lava_source  = mg->c_lava_source;
+
+       dswitchint = ps->range(1, 14);
+       flooded    = ps->range(1, 2) == 2;
+       
+       if (large_cave) {
+               part_max_length_rs = ps->range(2, 4);
+               tunnel_routepoints = ps->range(5, ps->range(15, 30));
+               min_tunnel_diameter = 5;
+               max_tunnel_diameter = ps->range(7, ps->range(8, 24));
+       } else {
+               part_max_length_rs = ps->range(2, 9);
+               tunnel_routepoints = ps->range(10, ps->range(15, 30));
+               min_tunnel_diameter = 2;
+               max_tunnel_diameter = ps->range(2, 6);
+       }
+       
+       large_cave_is_flat = (ps->range(0, 1) == 0);
+}
+
+
+void CaveV7::makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height) {
+       node_min = nmin;
+       node_max = nmax;
+       max_stone_y = max_stone_height;
+       main_direction = v3f(0, 0, 0);
+
+       // Allowed route area size in nodes
+       ar = node_max - node_min + v3s16(1, 1, 1);
+       // Area starting point in nodes
+       of = node_min;
+
+       // Allow a bit more
+       //(this should be more than the maximum radius of the tunnel)
+       s16 insure = 10;
+       s16 more = MAP_BLOCKSIZE - max_tunnel_diameter / 2 - insure;
+       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
+       route_y_max = -of.Y + max_stone_y + max_tunnel_diameter / 2 + 7;
+
+       // Limit maximum to area
+       route_y_max = rangelim(route_y_max, 0, ar.Y - 1);
+
+       if (large_cave) {
+               s16 min = 0;
+               if (node_min.Y < water_level && node_max.Y > water_level) {
+                       min = water_level - max_tunnel_diameter/3 - of.Y;
+                       route_y_max = water_level + max_tunnel_diameter/3 - of.Y;
+               }
+               route_y_min = ps->range(min, min + max_tunnel_diameter);
+               route_y_min = rangelim(route_y_min, 0, route_y_max);
+       }
+
+       s16 route_start_y_min = route_y_min;
+       s16 route_start_y_max = route_y_max;
+
+       route_start_y_min = rangelim(route_start_y_min, 0, ar.Y - 1);
+       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
+       );
+
+       // Generate some tunnel starting from orp
+       for (u16 j = 0; j < tunnel_routepoints; j++)
+               makeTunnel(j % dswitchint == 0);
+}
+
+
+void CaveV7::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 *= (float)ps->range(0, 10) / 10;
+       }
+
+       // Randomize size
+       s16 min_d = min_tunnel_diameter;
+       s16 max_d = max_tunnel_diameter;
+       rs = ps->range(min_d, max_d);
+
+       v3s16 maxlen;
+       if (large_cave) {
+               maxlen = v3s16(
+                       rs * part_max_length_rs,
+                       rs * part_max_length_rs / 2,
+                       rs * part_max_length_rs
+               );
+       } else {
+               maxlen = v3s16(
+                       rs * part_max_length_rs,
+                       ps->range(1, rs * part_max_length_rs),
+                       rs * part_max_length_rs
+               );
+       }
+
+       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
+               );
+       } 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
+               );
+       }
+
+       // Do not make large caves that are above ground.
+       // It is only necessary to check the startpoint and endpoint.
+       if (large_cave) {
+               v3s16 orpi(orp.X, orp.Y, orp.Z);
+               v3s16 veci(vec.X, vec.Y, vec.Z);
+               v3s16 p;
+               
+               p = orpi + veci + of + rs / 2;
+               if (p.Z >= node_min.Z && p.Z <= node_max.Z &&
+                       p.X >= node_min.X && p.X <= node_max.X) {
+                       u32 index = (p.Z - node_min.Z) * mg->ystride + (p.X - node_min.X);
+                       s16 h = mg->ridge_heightmap[index];
+                       if (h < p.Y)
+                               return;
+               } else if (p.Y > water_level) {
+                       return; // If it's not in our heightmap, use a simple heuristic
+               }
+               
+               p = orpi + of + rs / 2;
+               if (p.Z >= node_min.Z && p.Z <= node_max.Z &&
+                       p.X >= node_min.X && p.X <= node_max.X) {
+                       u32 index = (p.Z - node_min.Z) * mg->ystride + (p.X - node_min.X);
+                       s16 h = mg->ridge_heightmap[index];
+                       if (h < p.Y)
+                               return;
+               } else if (p.Y > water_level) { 
+                       return;
+               }
+       }
+
+       vec += main_direction;
+
+       v3f rp = orp + vec;
+       if (rp.X < 0)
+               rp.X = 0;
+       else if (rp.X >= ar.X)
+               rp.X = ar.X - 1;
+       
+       if (rp.Y < route_y_min)
+               rp.Y = route_y_min;
+       else if (rp.Y >= route_y_max)
+               rp.Y = route_y_max - 1;
+       
+       if (rp.Z < 0)
+               rp.Z = 0;
+       else if (rp.Z >= ar.Z)
+               rp.Z = ar.Z - 1;
+       
+       vec = rp - orp;
+
+       float veclen = vec.getLength();
+       if (veclen < 0.05)
+               veclen = 1.0;
+               
+       // Every second section is rough
+       bool randomize_xz = (ps->range(1, 2) == 1);
+
+       // Make a ravine every once in a while if it's long enough
+       float xylen = vec.X * vec.X + vec.Z * vec.Z;
+       bool is_ravine = (xylen > 500.0) && !large_cave && (ps->range(1, 8) == 1);
+
+       // Carve routes
+       for (float f = 0; f < 1.0; f += 1.0 / veclen)
+               carveRoute(vec, f, randomize_xz, is_ravine);
+       
+       orp = rp;
+}
+
+
+void CaveV7::carveRoute(v3f vec, float f, bool randomize_xz, bool is_ravine) {
+       MapNode airnode(CONTENT_AIR);
+       MapNode waternode(c_water_source);
+       MapNode lavanode(c_lava_source);
+       MapNode liquidnode = ps->range(0, 4) ? lavanode : waternode;
+       
+       v3s16 startp(orp.X, orp.Y, orp.Z);
+       startp += of;
+       
+       v3f fp = orp + vec * f;
+       fp.X += 0.1 * ps->range(-10, 10);
+       fp.Z += 0.1 * ps->range(-10, 10);
+       v3s16 cp(fp.X, fp.Y, fp.Z);
+
+       s16 d0 = -rs/2;
+       s16 d1 = d0 + rs;
+       if (randomize_xz) {
+               d0 += ps->range(-1, 1);
+               d1 += ps->range(-1, 1);
+       }
+       
+       bool flat_cave_floor = !large_cave && ps->range(0, 2) == 2;
+       
+       for (s16 z0 = d0; z0 <= d1; z0++) {
+               s16 si = rs / 2 - MYMAX(0, abs(z0) - rs / 7 - 1);
+               for (s16 x0 = -si - ps->range(0,1); x0 <= si - 1 + ps->range(0,1); x0++) {
+                       s16 maxabsxz = MYMAX(abs(x0), abs(z0));
+       
+                       s16 si2 = is_ravine ? MYMIN(ps->range(25, 26), ar.Y) :
+                                                                rs / 2 - MYMAX(0, maxabsxz - rs / 7 - 1);
+                       
+                       for (s16 y0 = -si2; y0 <= si2; y0++) {
+                               // Make better floors in small caves
+                               if(flat_cave_floor && y0 <= -rs/2 && rs<=7)
+                                       continue;
+                               
+                               if (large_cave_is_flat) {
+                                       // Make large caves not so tall
+                                       if (rs > 7 && abs(y0) >= rs / 3)
+                                               continue;
+                               }
+
+                               v3s16 p(cp.X + x0, cp.Y + y0, cp.Z + z0);
+                               p += of;
+
+                               if (vm->m_area.contains(p) == false)
+                                       continue;
+
+                               u32 i = vm->m_area.index(p);
+                               
+                               // Don't replace air or water or lava
+                               content_t c = vm->m_data[i].getContent();
+                               if (c == CONTENT_AIR || c == c_water_source || c == c_lava_source)
+                                       continue;
+                                       
+                               if (large_cave) {
+                                       int full_ymin = node_min.Y - MAP_BLOCKSIZE;
+                                       int full_ymax = node_max.Y + MAP_BLOCKSIZE;
+
+                                       if (flooded && full_ymin < water_level && full_ymax > water_level)
+                                               vm->m_data[i] = (p.Y <= water_level) ? waternode : airnode;
+                                       else if (flooded && full_ymax < water_level)
+                                               vm->m_data[i] = (p.Y < startp.Y - 4) ? liquidnode : airnode;
+                                       else
+                                               vm->m_data[i] = airnode;
+                               } else {
+                                       vm->m_data[i] = airnode;
+                                       vm->m_flags[i] |= VMANIP_FLAG_CAVE;
+                               }
+                       }
+               }
+       }
+}
index 979226b3ef47c43675a85e026124755ae133427f..7822816d6abc9a828c0291ac29358d521860bd49 100644 (file)
@@ -22,9 +22,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #define VMANIP_FLAG_CAVE VOXELFLAG_CHECKED1
 
+class MapgenV6;
+class MapgenV7;
+
 class CaveV6 {
 public:
+       MapgenV6 *mg;
        ManualMapVoxelManipulator *vm;
+       INodeDefManager *ndef;
 
        s16 min_tunnel_diameter;
        s16 max_tunnel_diameter;
@@ -40,10 +45,10 @@ public:
        v3s16 node_min;
        v3s16 node_max;
        
-       v3f orp;  //original point
-       v3s16 of;
+       v3f orp;  // starting point, relative to caved space
+       v3s16 of; // absolute coordinates of caved space
        v3s16 ar; // allowed route area
-       s16 rs;   // radius size
+       s16 rs;   // tunnel radius size
        v3f main_direction;
        
        s16 route_y_min;
@@ -58,11 +63,53 @@ public:
        int water_level;
 
        CaveV6() {}
-       CaveV6(Mapgen *mg, PseudoRandom *ps, PseudoRandom *ps2, bool large_cave,
-                       content_t c_water, content_t c_lava);
+       CaveV6(MapgenV6 *mg, PseudoRandom *ps, PseudoRandom *ps2, bool large_cave);
        void makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height);
        void makeTunnel(bool dirswitch);
        void carveRoute(v3f vec, float f, bool randomize_xz);
 };
 
+class CaveV7 {
+public:
+       MapgenV7 *mg;
+       ManualMapVoxelManipulator *vm;
+       INodeDefManager *ndef;
+
+       s16 min_tunnel_diameter;
+       s16 max_tunnel_diameter;
+       u16 tunnel_routepoints;
+       int dswitchint;
+       int part_max_length_rs;
+
+       bool large_cave;
+       bool large_cave_is_flat;
+       bool flooded;
+
+       s16 max_stone_y;
+       v3s16 node_min;
+       v3s16 node_max;
+       
+       v3f orp;  // starting point, relative to caved space
+       v3s16 of; // absolute coordinates of caved space
+       v3s16 ar; // allowed route area
+       s16 rs;   // tunnel radius size
+       v3f main_direction;
+       
+       s16 route_y_min;
+       s16 route_y_max;
+       
+       PseudoRandom *ps;
+       
+       content_t c_water_source;
+       content_t c_lava_source;
+       
+       int water_level;
+
+       CaveV7() {}
+       CaveV7(MapgenV7 *mg, PseudoRandom *ps, bool large_cave);
+       void makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height);
+       void makeTunnel(bool dirswitch);
+       void carveRoute(v3f vec, float f, bool randomize_xz, bool is_ravine);
+};
+
 #endif
index ce5cfbfeb6836f20b81df357801e0f7a324d78c2..2eeaf13ebc7c7b20a14357e95084aaca05063041 100644 (file)
@@ -274,23 +274,21 @@ void MapgenIndev::generateCaves(int max_stone_y) {
        
        for (u32 i = 0; i < caves_count + bruises_count; i++) {
                bool large_cave = (i >= caves_count);
-               CaveIndev cave(this, &ps, &ps2, node_min, large_cave,
-                                               c_water_source, c_lava_source);
+               CaveIndev cave(this, &ps, &ps2, node_min, large_cave);
 
                cave.makeCave(node_min, node_max, max_stone_y);
        }
 }
 
-CaveIndev::CaveIndev(Mapgen *mg, PseudoRandom *ps, PseudoRandom *ps2,
-                               v3s16 node_min, bool is_large_cave,
-                               content_t c_water, content_t c_lava) {
+CaveIndev::CaveIndev(MapgenIndev *mg, PseudoRandom *ps, PseudoRandom *ps2,
+                               v3s16 node_min, bool is_large_cave) {
        this->vm = mg->vm;
        this->water_level = mg->water_level;
        this->large_cave = is_large_cave;
        this->ps  = ps;
        this->ps2 = ps2;
-       this->c_water_source = c_water;
-       this->c_lava_source  = c_lava;
+       this->c_water_source = mg->c_water_source;
+       this->c_lava_source  = mg->c_lava_source;
 
        min_tunnel_diameter = 2;
        max_tunnel_diameter = ps->range(2,6);
index bd6faa21c59dd09db3054d50c38617020e25d496..87ae39d0993798e97c2b1b3757b19fd9836203e9 100644 (file)
@@ -153,8 +153,8 @@ struct MapgenFactoryIndev : public MapgenFactoryV6 {
 
 class CaveIndev : public CaveV6 {
 public:
-       CaveIndev(Mapgen *mg, PseudoRandom *ps, PseudoRandom *ps2, v3s16 node_min,
-                       bool is_large_cave, content_t c_water, content_t c_lava);
+       CaveIndev(MapgenIndev *mg, PseudoRandom *ps, PseudoRandom *ps2,
+                       v3s16 node_min, bool is_large_cave);
 };
 
 #endif
index 352afd2f30d39dd9d4307d7de56cbd9dec6bf04a..d90d142d50ed75d911c1c686ba356a7656934809 100644 (file)
@@ -948,7 +948,7 @@ void MapgenV6::generateCaves(int max_stone_y) {
        
        for (u32 i = 0; i < caves_count + bruises_count; i++) {
                bool large_cave = (i >= caves_count);
-               CaveV6 cave(this, &ps, &ps2, large_cave, c_water_source, c_lava_source);
+               CaveV6 cave(this, &ps, &ps2, large_cave);
 
                cave.makeCave(node_min, node_max, max_stone_y);
        }
index efcf7867203d39772047e45b52bebe525620d093..4bfc866e246ccaad962b2139f56e44b3055e0254 100644 (file)
@@ -78,7 +78,7 @@ MapgenV7::MapgenV7(int mapgenid, MapgenV7Params *params, EmergeManager *emerge)
        this->water_level = params->water_level;
        this->flags   = params->flags;
        this->csize   = v3s16(1, 1, 1) * params->chunksize * MAP_BLOCKSIZE;
-//     this->ystride = csize.X; //////fix this
+       this->ystride = csize.X; //////fix this
 
        this->biomemap  = new u8[csize.X * csize.Z];
        this->heightmap = new s16[csize.X * csize.Z];
@@ -187,7 +187,6 @@ void MapgenV7::makeChunk(BlockMakeData *data) {
        
        generateCaves(stone_surface_max_y);
        addTopNodes();
-       //v3s16 central_area_size = node_max - node_min + v3s16(1,1,1);
 
        if (flags & MG_DUNGEONS) {
                DungeonGen dgen(ndef, data->seed, water_level);
@@ -452,7 +451,8 @@ void MapgenV7::addTopNodes() {
                                vm->m_area.add_y(em, i, 1);
                        }
                        // If dirt, grow grass on it.
-                       if (vm->m_data[i].getContent() == CONTENT_AIR) {
+                       if (y > water_level - 10 &&
+                               vm->m_data[i].getContent() == CONTENT_AIR) {
                                vm->m_area.add_y(em, i, -1);
                                if (vm->m_data[i].getContent() == c_dirt)
                                        vm->m_data[i] = MapNode(c_dirt_with_grass);
@@ -489,7 +489,8 @@ void MapgenV7::addTopNodes() {
                                vm->m_area.add_y(em, i, 1);
                        }
                        // If dirt, grow grass on it.
-                       if (vm->m_data[i].getContent() == CONTENT_AIR) {
+                       if (y > water_level - 10 &&
+                               vm->m_data[i].getContent() == CONTENT_AIR) {
                                vm->m_area.add_y(em, i, -1);
                                if (vm->m_data[i].getContent() == c_dirt)
                                        vm->m_data[i] = MapNode(c_dirt_with_grass);
@@ -502,22 +503,22 @@ void MapgenV7::addTopNodes() {
 #include "mapgen_v6.h"
 void MapgenV7::generateCaves(int max_stone_y) {
        PseudoRandom ps(blockseed + 21343);
-       PseudoRandom ps2(blockseed + 1032);
 
        int volume_nodes = (node_max.X - node_min.X + 1) *
-                                          (node_max.Y - node_min.Y + 1) * MAP_BLOCKSIZE;
+                                          (node_max.Y - node_min.Y + 1) *
+                                          (node_max.Z - node_min.Z + 1);
        float cave_amount = NoisePerlin2D(&nparams_v6_def_cave,
                                                                node_min.X, node_min.Y, seed);
-       
-       u32 caves_count = MYMAX(0.0, cave_amount) * volume_nodes / 50000;
+
+       u32 caves_count = MYMAX(0.0, cave_amount) * volume_nodes / 250000;
        for (u32 i = 0; i < caves_count; i++) {
-               CaveV6 cave(this, &ps, &ps2, false, c_water_source, c_lava_source);
+               CaveV7 cave(this, &ps, false);
                cave.makeCave(node_min, node_max, max_stone_y);
        }
-       
-       u32 bruises_count = (ps.range(1, 6) == 1) ? ps.range(0, ps.range(0, 2)) : 1;
+
+       u32 bruises_count = (ps.range(1, 8) == 1) ? ps.range(0, ps.range(0, 2)) : 1;
        for (u32 i = 0; i < bruises_count; i++) {
-               CaveV6 cave(this, &ps, &ps2, true, c_water_source, c_lava_source);
+               CaveV7 cave(this, &ps, true);
                cave.makeCave(node_min, node_max, max_stone_y);
        }       
 }