diff options
author | kwolekr <kwolekr@minetest.net> | 2016-05-10 13:35:39 -0400 |
---|---|---|
committer | kwolekr <kwolekr@minetest.net> | 2016-05-27 23:23:58 -0400 |
commit | 68b1cd8d1bda704dc174c2e39a03276f68984164 (patch) | |
tree | 02fd46e447289880f08e43f47b367b0a2968da51 /src | |
parent | a605d690921670008d605d067f1cf8cd1619def7 (diff) | |
download | minetest-68b1cd8d1bda704dc174c2e39a03276f68984164.tar.gz minetest-68b1cd8d1bda704dc174c2e39a03276f68984164.tar.bz2 minetest-68b1cd8d1bda704dc174c2e39a03276f68984164.zip |
Cavegen: Merge instances of repetitive surface level-finding code
Diffstat (limited to 'src')
-rw-r--r-- | src/cavegen.cpp | 97 | ||||
-rw-r--r-- | src/cavegen.h | 5 |
2 files changed, 47 insertions, 55 deletions
diff --git a/src/cavegen.cpp b/src/cavegen.cpp index bc73b7206..59878c890 100644 --- a/src/cavegen.cpp +++ b/src/cavegen.cpp @@ -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; + } +} diff --git a/src/cavegen.h b/src/cavegen.h index 74cd0b416..5a41966fe 100644 --- a/src/cavegen.h +++ b/src/cavegen.h @@ -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 |