From 65414ac3be78591021c88d3e6876c30cae1b11f8 Mon Sep 17 00:00:00 2001 From: Cubitect Date: Tue, 23 Nov 2021 19:58:08 +0100 Subject: [PATCH] An approximation is sufficient for scaled 1.18 noise sampling. --- finders.c | 2 +- javarnd.h | 129 ------------------------------------------------------ layers.c | 14 +++--- layers.h | 2 +- 4 files changed, 11 insertions(+), 136 deletions(-) delete mode 100644 javarnd.h diff --git a/finders.c b/finders.c index b2f821c..95dcb2c 100644 --- a/finders.c +++ b/finders.c @@ -2452,7 +2452,7 @@ int checkForBiomes( if (ret) { uint64_t b = 0, bm = 0; - for (int i = 0; i < r.sx*r.sy*r.sz; i++) + for (i = 0; i < r.sx*r.sy*r.sz; i++) { int id = ids[i]; if (id < 128) b |= (1ULL << id); diff --git a/javarnd.h b/javarnd.h deleted file mode 100644 index 9141210..0000000 --- a/javarnd.h +++ /dev/null @@ -1,129 +0,0 @@ -#ifndef JAVARND_H_ -#define JAVARND_H_ - -#include - - -/********************** C copy of the Java Random methods ********************** - */ - -static inline void setSeed(uint64_t *seed, uint64_t value) -{ - *seed = (value ^ 0x5deece66d) & ((1ULL << 48) - 1); -} - -static inline int next(uint64_t *seed, const int bits) -{ - *seed = (*seed * 0x5deece66d + 0xb) & ((1ULL << 48) - 1); - return (int) ((int64_t)*seed >> (48 - bits)); -} - -static inline int nextInt(uint64_t *seed, const int n) -{ - int bits, val; - const int m = n - 1; - - if ((m & n) == 0) { - uint64_t x = n * (uint64_t)next(seed, 31); - return (int) ((int64_t) x >> 31); - } - - do { - bits = next(seed, 31); - val = bits % n; - } - while (bits - val + m < 0); - return val; -} - -static inline uint64_t nextLong(uint64_t *seed) -{ - return ((uint64_t) next(seed, 32) << 32) + next(seed, 32); -} - -static inline float nextFloat(uint64_t *seed) -{ - return next(seed, 24) / (float) (1 << 24); -} - -static inline double nextDouble(uint64_t *seed) -{ - uint64_t x = (uint64_t)next(seed, 26); - x <<= 27; - x += next(seed, 27); - return (int64_t) x / (double) (1ULL << 53); -} - -/* A macro to generate the ideal assembly for X = nextInt(S, 24) - * This is a macro and not an inline function, as many compilers can make use - * of the additional optimisation passes for the surrounding code. - */ -#define JAVA_NEXT_INT24(S,X) \ - do { \ - uint64_t a = (1ULL << 48) - 1; \ - uint64_t c = 0x5deece66dULL * (S); \ - c += 11; a &= c; \ - (S) = a; \ - a = (uint64_t) ((int64_t)a >> 17); \ - c = 0xaaaaaaab * a; \ - c = (uint64_t) ((int64_t)c >> 36); \ - (X) = (int)a - (int)(c << 3) * 3; \ - } while (0) - - -/* skipNextN - * --------- - * Jumps forwards in the random number sequence by simulating 'n' calls to next. - */ -static inline void skipNextN(uint64_t *seed, uint64_t n) -{ - uint64_t m = 1; - uint64_t a = 0; - uint64_t im = 0x5deece66dULL; - uint64_t ia = 0xb; - uint64_t k; - - for (k = n; k; k >>= 1) - { - if (k & 1) - { - m *= im; - a = im * a + ia; - } - ia = (im + 1) * ia; - im *= im; - } - - *seed = *seed * m + a; - *seed &= 0xffffffffffffULL; -} - - -/* Find the modular inverse: (1/x) | mod m. - * Assumes x and m are positive (less than 2^63), co-prime. - */ -static inline __attribute__((const)) -uint64_t mulInv(uint64_t x, uint64_t m) -{ - uint64_t t, q, a, b, n; - if ((int64_t)m <= 1) - return 0; // no solution - - n = m; - a = 0; b = 1; - - while ((int64_t)x > 1) - { - if (m == 0) - return 0; // x and m are co-prime - q = x / m; - t = m; m = x % m; x = t; - t = a; a = b - q * a; b = t; - } - - if ((int64_t)b < 0) - b += n; - return b; -} - -#endif /* JAVARND_H_ */ diff --git a/layers.c b/layers.c index dc739bb..4d98fd3 100644 --- a/layers.c +++ b/layers.c @@ -1354,11 +1354,15 @@ int p2overworld(const uint64_t np[6], uint64_t *dat); #endif /// Biome sampler for MC 1.18 -int sampleBiomeNoise(const BiomeNoise *bn, int x, int y, int z, uint64_t *dat) +int sampleBiomeNoise(const BiomeNoise *bn, int x, int y, int z, uint64_t *dat, int approx) { float t = 0, h = 0, c = 0, e = 0, d = 0, w = 0; - double px = x + sampleDoublePerlin(&bn->shift, x, 0, z) * 4.0; - double pz = z + sampleDoublePerlin(&bn->shift, z, x, 0) * 4.0; + double px = x, pz = z; + if (approx == 0) + { + px += sampleDoublePerlin(&bn->shift, x, 0, z) * 4.0; + pz += sampleDoublePerlin(&bn->shift, z, x, 0) * 4.0; + } c = sampleDoublePerlin(&bn->continentalness, px, 0, pz); e = sampleDoublePerlin(&bn->erosion, px, 0, pz); @@ -1405,7 +1409,7 @@ static void genBiomeNoise3D(const BiomeNoise *bn, int *out, Range r, int opt) for (i = 0; i < r.sx; i++) { int xi = (r.x+i)*scale + mid; - *p = sampleBiomeNoise(bn, xi, yk, zj, opt ? &dat : NULL); + *p = sampleBiomeNoise(bn, xi, yk, zj, opt ? &dat : NULL, opt); p++; } } @@ -1453,7 +1457,7 @@ int genBiomeNoiseScaled(const BiomeNoise *bn, int *out, Range r, int mc, uint64_ } else { - *p = sampleBiomeNoise(bn, x4, y4, z4, 0); + *p = sampleBiomeNoise(bn, x4, y4, z4, 0, 0); } p++; } diff --git a/layers.h b/layers.h index a65058b..fc7b079 100644 --- a/layers.h +++ b/layers.h @@ -438,7 +438,7 @@ int genEndScaled(const EndNoise *en, int *out, Range r, int mc, uint64_t sha); */ void initBiomeNoise(BiomeNoise *bn, int mc); void setBiomeSeed(BiomeNoise *bn, uint64_t seed, int large); -int sampleBiomeNoise(const BiomeNoise *bn, int x, int y, int z, uint64_t *dat); +int sampleBiomeNoise(const BiomeNoise *bn, int x, int y, int z, uint64_t *dat, int approx); /** * The scaled biome noise generation applies for the Overworld version 1.18. * The 'sha' hash of the seed is only required for voronoi at scale 1:1.