Cavegen: Merge instances of repetitive surface level-finding code
authorkwolekr <kwolekr@minetest.net>
Tue, 10 May 2016 17:35:39 +0000 (13:35 -0400)
committerkwolekr <kwolekr@minetest.net>
Sat, 28 May 2016 03:23:58 +0000 (23:23 -0400)
src/cavegen.cpp
src/cavegen.h

index bc73b7206012a5af645ad675e65aa8811c85d091..59878c890660dd008e43013a1401492a0d92778a 100644 (file)
@@ -147,31 +147,10 @@ void CavesRandomWalk::makeTunnel(bool dirswitch)
 
        // Do not make caves that are above ground.
        // It is only necessary to check the startpoint and endpoint.
-       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) * ystride + (p.X - node_min.X);
-               s16 h = mg->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) * ystride + (p.X - node_min.X);
-               s16 h = mg->heightmap[index];
-               if (h < p.Y)
-                       return;
-       } else if (p.Y > water_level) {
+       v3s16 p1 = v3s16(orp.X, orp.Y, orp.Z) + of + rs / 2;
+       v3s16 p2 = v3s16(vec.X, vec.Y, vec.Z) + p1;
+       if (isPosAboveSurface(p1) || isPosAboveSurface(p2))
                return;
-       }
 
        vec += main_direction;
 
@@ -274,6 +253,22 @@ void CavesRandomWalk::carveRoute(v3f vec, float f, bool randomize_xz)
 }
 
 
+inline bool CavesRandomWalk::isPosAboveSurface(v3s16 p)
+{
+       if (heightmap != NULL &&
+                       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) * ystride + (p.X - node_min.X);
+               if (heightmap[index] < p.Y)
+                       return true;
+       } else if (p.Y > water_level) {
+               return true;
+       }
+
+       return false;
+}
+
+
 ////
 //// CavesV6
 ////
@@ -448,39 +443,18 @@ void CavesV6::makeTunnel(bool dirswitch)
                );
        }
 
-       // Do not make caves that are entirely above ground, to fix
-       // shadow bugs caused by overgenerated large caves.
+       // Do not make caves that are entirely above ground, to fix shadow bugs
+       // caused by overgenerated large caves.
        // It is only necessary to check the startpoint and endpoint.
-       v3s16 orpi(orp.X, orp.Y, orp.Z);
-       v3s16 veci(vec.X, vec.Y, vec.Z);
-       s16 h1;
-       s16 h2;
-
-       v3s16 p1 = orpi + veci + of + rs / 2;
-       if (p1.Z >= node_min.Z && p1.Z <= node_max.Z &&
-                       p1.X >= node_min.X && p1.X <= node_max.X) {
-               u32 index1 = (p1.Z - node_min.Z) * ystride +
-                       (p1.X - node_min.X);
-               h1 = heightmap[index1];
-       } else {
-               h1 = water_level; // If not in heightmap
-       }
-
-       v3s16 p2 = orpi + of + rs / 2;
-       if (p2.Z >= node_min.Z && p2.Z <= node_max.Z &&
-                       p2.X >= node_min.X && p2.X <= node_max.X) {
-               u32 index2 = (p2.Z - node_min.Z) * ystride +
-                       (p2.X - node_min.X);
-               h2 = heightmap[index2];
-       } else {
-               h2 = water_level;
-       }
+       v3s16 p1 = v3s16(orp.X, orp.Y, orp.Z) + of + rs / 2;
+       v3s16 p2 = v3s16(vec.X, vec.Y, vec.Z) + p1;
 
-       // If startpoint and endpoint are above ground,
-       // disable placing of nodes in carveRoute while
-       // still running all pseudorandom calls to ensure
-       // caves consistent with existing worlds.
-       bool tunnel_above_ground = p1.Y > h1 && p2.Y > h2;
+       // If startpoint and endpoint are above ground, disable placement of nodes
+       // in carveRoute while still running all PseudoRandom calls to ensure caves
+       // are consistent with existing worlds.
+       bool tunnel_above_ground =
+               p1.Y > getSurfaceFromHeightmap(p1) &&
+               p2.Y > getSurfaceFromHeightmap(p2);
 
        vec += main_direction;
 
@@ -588,3 +562,16 @@ void CavesV6::carveRoute(v3f vec, float f, bool randomize_xz,
                }
        }
 }
+
+
+inline s16 CavesV6::getSurfaceFromHeightmap(v3s16 p)
+{
+       if (heightmap != NULL &&
+                       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) * ystride + (p.X - node_min.X);
+               return heightmap[index];
+       } else {
+               return water_level;
+       }
+}
index 74cd0b41658f6f9b2adca8d10aa53c203f791191..5a41966fe13a260ed7b01f723116b761e6cb43e3 100644 (file)
@@ -71,6 +71,9 @@ public:
        void makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height);
        void makeTunnel(bool dirswitch);
        void carveRoute(v3f vec, float f, bool randomize_xz);
+
+private:
+       inline bool isPosAboveSurface(v3s16 p);
 };
 
 /*
@@ -140,6 +143,8 @@ public:
 private:
        void makeTunnel(bool dirswitch);
        void carveRoute(v3f vec, float f, bool randomize_xz, bool tunnel_above_ground);
+
+       inline s16 getSurfaceFromHeightmap(v3s16 p);
 };
 
 #endif