From e59c0d09e26308783b0d4dce2f5beeaf87a048f4 Mon Sep 17 00:00:00 2001 From: Kai Sandstrom Date: Sun, 5 Feb 2023 12:38:24 -0500 Subject: [PATCH] Beta 1.7 fixes and tweaks (generator.c) - getMinCacheSize(): - Replaced integer mults/divs by powers of 2 with bit shifts. - Fixed a bug that made the cache buffer 2 SeaLevelColumnNoises too short. I'm kind of surprised this didn't cause segfaults. - genBiomes(): - Now copies cache values from y=0 to higher y values when sy > 1, to match behavior of b1.8 - 1.17. (noise.c) - removed sampleOctave2D(). (layers.c) - genColumnNoise(): Now uses sampleOctaveAmp() for 2D Perlin samples. - processColumnNoise(), sampleBlocks(), sampleBetaBiomeOneBlock(), genBetaBiomeNoiseScaled(): - Replaced integer mults/divs and modulos by powers of 2 with left/right bit shifts and bitwise and, respectively. --- generator.c | 18 ++++++++++---- layers.c | 67 ++++++++++++++++++++++++++++------------------------- noise.c | 17 -------------- 3 files changed, 48 insertions(+), 54 deletions(-) diff --git a/generator.c b/generator.c index f6492ed..0c9a672 100644 --- a/generator.c +++ b/generator.c @@ -137,9 +137,9 @@ size_t getMinCacheSize(const Generator *g, int scale, int sx, int sy, int sz) size_t len = (size_t)sx * sz * sy; if (g->mc <= MC_B1_7 && scale <=4 && !(g->flags & NO_BETA_OCEAN)) { - int sxa = (int) floor((double) sx / (4.0/scale)) + 1; - int sza = (int) floor((double) sz / (4.0/scale)) + 1; - len += (2*((sxa <= sza) ? sxa : sza)-1)*sizeof(SeaLevelColumnNoiseBeta); + int sxa = (sx >> (2>>(scale>>1))) + 1; + int sza = (sz >> (2>>(scale>>1))) + 1; + len += ((((sxa <= sza) ? sxa : sza) << 1) + 1)*sizeof(SeaLevelColumnNoiseBeta); } if (g->mc >= MC_B1_8 && g->mc <= MC_1_17 && g->dim == DIM_OVERWORLD) { // recursively check the layer stack for the max buffer @@ -198,9 +198,17 @@ int genBiomes(const Generator *g, int *cache, Range r) { SurfaceNoiseBeta snb; initSurfaceNoiseBeta(&snb, g->seed); - return genBetaBiomeNoiseScaled(&g->bnb, &snb, cache, r, g->mc, 0); + err = genBetaBiomeNoiseScaled(&g->bnb, &snb, cache, r, g->mc, 0); } - return genBetaBiomeNoiseScaled(&g->bnb, NULL, cache, r, g->mc, 1); + else + err = genBetaBiomeNoiseScaled(&g->bnb, NULL, cache, r, g->mc, 1); + if (err) return err; + for (k = 1; k < r.sy; k++) + { // overworld has no vertical noise: expanding 2D into 3D + for (i = 0; i < r.sx*r.sz; i++) + cache[k*r.sx*r.sz + i] = cache[i]; + } + return 0; } } else if (g->dim == DIM_NETHER) diff --git a/layers.c b/layers.c index 373e4b0..93eb237 100644 --- a/layers.c +++ b/layers.c @@ -1698,8 +1698,8 @@ int genBiomeNoiseScaled(const BiomeNoise *bn, int *out, Range r, int mc, uint64_ void genColumnNoise(const SurfaceNoiseBeta *snb, SeaLevelColumnNoiseBeta *dest, int cx, int cz) { - dest->contASample = sampleOctave2D(&snb->octcontA, cx, cz); - dest->contBSample = sampleOctave2D(&snb->octcontB, cx, cz); + dest->contASample = sampleOctaveAmp(&snb->octcontA, cx, 0, cz, 0, 0, 1); + dest->contBSample = sampleOctaveAmp(&snb->octcontB, cx, 0, cz, 0, 0, 1); sampleOctaveOldBetaTerrain3D(&snb->octmin, dest->minSample, cx, cz, 0); sampleOctaveOldBetaTerrain3D(&snb->octmax, dest->maxSample, cx, cz, 0); sampleOctaveOldBetaTerrain3D(&snb->octmain, dest->mainSample, cx, cz, 1); @@ -1708,8 +1708,8 @@ void genColumnNoise(const SurfaceNoiseBeta *snb, SeaLevelColumnNoiseBeta *dest, void processColumnNoise(double *out, SeaLevelColumnNoiseBeta *src, const BiomeNoiseBeta *bnb, int x, int z, int chunkBorderX, int chunkBorderZ) { - int climateX = (chunkBorderX) ? ((x/4)-1)*16+13 : x/4*16 + (x%4)*3+1; - int climateZ = (chunkBorderZ) ? ((z/4)-1)*16+13 : z/4*16 + (z%4)*3+1; + int climateX = (chunkBorderX) ? (((x>>2)-1)<<4)+13 : ((x>>2)<<4) + (x&3)*3+1; + int climateZ = (chunkBorderZ) ? (((z>>2)-1)<<4)+13 : ((z>>2)<<4) + (z&3)*3+1; double climate[2]; sampleBiomeNoiseBeta(bnb, NULL, climate, climateX, climateZ); double humi = 1 - climate[0] * climate[1]; @@ -1750,7 +1750,7 @@ void processColumnNoise(double *out, SeaLevelColumnNoiseBeta *src, void sampleBlocks(double *src, uint8_t *out, int scale) { - int offs = (scale >= 8) ? 0 : ((scale > 4) ? 4 : scale)/2; + int offs = (scale >= 8) ? 0 : ((scale > 4) ? 4 : scale)>>1; scale = (scale > 4) ? 4 : scale; int idx = 0; double b000 = src[0]; @@ -1770,16 +1770,16 @@ void sampleBlocks(double *src, uint8_t *out, int scale) double bd2 = (b110 - b010) * 0.25; for (z = 0; z < 4; z++) { - int validZ = ((z - offs) % scale == 0); + int validZ = (((z - offs) & (scale-1)) == 0); double b000c1 = b000c; double bDiag = (b010c - b000c) * 0.25; for (x = 0; x < 4; x++) { - int validX = ((x - offs) % scale == 0); + int validX = (((x - offs) & (scale-1)) == 0); if (y == 7 && (scale == 1 || (validX && validZ))) { out[idx++] = (b000c1 > 0); - if (idx == 4/scale * 4/scale) + if (idx == (4>>(scale>>1)) * (4>>(scale>>1))) return; } b000c1 += bDiag; @@ -1799,21 +1799,21 @@ int sampleBetaBiomeOneBlock(const BiomeNoiseBeta *bnb, { double colsProcessed[8]; SeaLevelColumnNoiseBeta colNoise; - genColumnNoise(snb, &colNoise, x/4, z/4); + genColumnNoise(snb, &colNoise, x>>2, z>>2); processColumnNoise(&colsProcessed[0], &colNoise, bnb, - x/4, z/4, 0, 0); + x>>2, z>>2, 0, 0); - genColumnNoise(snb, &colNoise, x/4+1, z/4); + genColumnNoise(snb, &colNoise, (x>>2)+1, z>>2); processColumnNoise(&colsProcessed[2], &colNoise, bnb, - x/4+1, z/4, x%16 >= 12, 0); + (x>>2)+1, z>>2, (x & 15) >= 12, 0); - genColumnNoise(snb, &colNoise, x/4, z/4+1); + genColumnNoise(snb, &colNoise, x>>2, (z>>2)+1); processColumnNoise(&colsProcessed[4], &colNoise, bnb, - x/4, z/4+1, 0, z%16 >= 12); + x>>2, (z>>2)+1, 0, (z & 15) >= 12); - genColumnNoise(snb, &colNoise, x/4+1, z/4+1); + genColumnNoise(snb, &colNoise, (x>>2)+1, (z>>2)+1); processColumnNoise(&colsProcessed[6], &colNoise, bnb, - x/4+1, z/4+1, x%16 >= 12, z%16 >= 12); + (x>>2)+1, (z>>2)+1, (x & 15) >= 12, (z & 15) >= 12); uint8_t blockSample; sampleBlocks(colsProcessed, &blockSample, 8); @@ -1836,7 +1836,7 @@ int genBetaBiomeNoiseScaled(const BiomeNoiseBeta *bnb, { int i, j; int *p = out; - int mid = r.scale / 2; + int mid = r.scale >> 1; for (j = 0; j < r.sz; j++) { int zj = (r.z+j)*r.scale + mid; @@ -1854,10 +1854,10 @@ int genBetaBiomeNoiseScaled(const BiomeNoiseBeta *bnb, int scale = r.scale; int id; - int cx1 = (int) floor((double) r.x / (4.0/scale)); - int cz1 = (int) floor((double) r.z / (4.0/scale)); - int cx2 = cx1 + (int) floor((double) r.sx / (4.0/scale)) + 1; - int cz2 = cz1 + (int) floor((double) r.sz / (4.0/scale)) + 1; + int cx1 = r.x >> (2>>(scale>>1)); + int cz1 = r.z >> (2>>(scale>>1)); + int cx2 = cx1 + (r.sx >> (2>>(scale>>1))) + 1; + int cz2 = cz1 + (r.sz >> (2>>(scale>>1))) + 1; int minDim, maxDim; if (cx2-cx1 > cz2-cz1) { maxDim = cx2-cx1; @@ -1866,7 +1866,7 @@ int genBetaBiomeNoiseScaled(const BiomeNoiseBeta *bnb, maxDim = cz2-cz1; minDim = cx2-cx1; } - int bufLen = 2*minDim+1; + int bufLen = (minDim<<1)+1; int xi, zi, xii, zii; int xStart = cx1; @@ -1896,37 +1896,40 @@ int genBetaBiomeNoiseScaled(const BiomeNoiseBeta *bnb, genColumnNoise(snb, &buf[(idx + minDim + 1) % bufLen], xi+1, zi); colNoise = &buf[(idx + minDim + 1) % bufLen]; processColumnNoise(&colsProcessed[2], colNoise, bnb, - xi+1, zi, xi%4 == 3, 0); + xi+1, zi, (xi & 3) == 3, 0); if (xi == cx1) genColumnNoise(snb, &buf[(idx + minDim) % bufLen], xi, zi+1); colNoise = &buf[(idx + minDim) % bufLen]; processColumnNoise(&colsProcessed[4], colNoise, bnb, - xi, zi+1, 0, zi%4 == 3); + xi, zi+1, 0, (zi & 3) == 3); genColumnNoise(snb, &buf[idx], xi+1, zi+1); colNoise = &buf[idx]; processColumnNoise(&colsProcessed[6], colNoise, bnb, - xi+1, zi+1, xi%4 == 3, zi%4 == 3); + xi+1, zi+1, (xi & 3) == 3, (zi & 3) == 3); sampleBlocks(colsProcessed, blockSamples, scale); - for (zii = 0; zii < 4/scale; zii++) + for (zii = 0; zii < (4>>(scale>>1)); zii++) { - if (zi * 4/scale + zii < r.z || zi * 4/scale + zii >= r.z + r.sz) + if (zi * (4>>(scale>>1)) + zii < r.z || + zi * (4>>(scale>>1)) + zii >= r.z + r.sz) continue; - for (xii = 0; xii < 4/scale; xii++) + for (xii = 0; xii < (4>>(scale>>1)); xii++) { - if (xi * 4/scale + xii < r.x || xi * 4/scale + xii >= r.x + r.sx) + if (xi * (4>>(scale>>1)) + xii < r.x || + xi * (4>>(scale>>1)) + xii >= r.x + r.sx) continue; double climate[2]; sampleBiomeNoiseBeta(bnb, NULL, climate, - xi*4 + xii + scale/2, zi*4 + zii + scale/2); - if (blockSamples[zii * 4/scale + xii] == 1) + (xi<<2) + xii + (scale>>1), (zi<<2) + zii + (scale>>1)); + if (blockSamples[zii * (4>>(scale>>1)) + xii] == 1) id = getOldBetaBiome((float) climate[0], (float) climate[1]); else id = (climate[0] < 0.5) ? frozen_ocean : ocean; - out[(zi * 4/scale + zii - r.z) * r.sx + (xi * 4/scale + xii - r.x)] = id; + out[(zi * (4>>(scale>>1)) + zii - r.z) * r.sx + + (xi * (4>>(scale>>1)) + xii - r.x)] = id; } } diff --git a/noise.c b/noise.c index 503c73c..0bfa460 100644 --- a/noise.c +++ b/noise.c @@ -448,23 +448,6 @@ double sampleOctave(const OctaveNoise *noise, double x, double y, double z) return v; } -double sampleOctave2D(const OctaveNoise *noise, double x, double z) -{ - double v = 0; - int i; - for (i = 0; i < noise->octcnt; i++) - { - PerlinNoise *p = noise->octaves + i; - double lf = p->lacunarity; - double ax = maintainPrecision(x * lf); - double ay = -(p->b); - double az = maintainPrecision(z * lf); - double pv = samplePerlin(p, ax, ay, az, 0, 0); - v += p->amplitude * pv; - } - return v; -} - double sampleOctaveOldBetaBiome(const OctaveNoise *noise, double x, double z) { double v = 0;