aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/FastNoise.cpp
diff options
context:
space:
mode:
authordaoge_cmd <3523206925@qq.com>2026-03-01 12:16:08 +0800
committerdaoge_cmd <3523206925@qq.com>2026-03-01 12:16:08 +0800
commitb691c43c44ff180d10e7d4a9afc83b98551ff586 (patch)
tree3e9849222cbc6ba49f2f1fc6e5fe7179632c7390 /Minecraft.World/FastNoise.cpp
parentdef8cb415354ac390b7e89052a50605285f1aca9 (diff)
Initial commit
Diffstat (limited to 'Minecraft.World/FastNoise.cpp')
-rw-r--r--Minecraft.World/FastNoise.cpp114
1 files changed, 114 insertions, 0 deletions
diff --git a/Minecraft.World/FastNoise.cpp b/Minecraft.World/FastNoise.cpp
new file mode 100644
index 00000000..91d039b3
--- /dev/null
+++ b/Minecraft.World/FastNoise.cpp
@@ -0,0 +1,114 @@
+#include "stdafx.h"
+#include "FastNoise.h"
+
+FastNoise::FastNoise(int levels)
+{
+ Random random;
+ init(&random, levels);
+}
+
+FastNoise::FastNoise(Random *random, int levels)
+{
+ init(random,levels);
+}
+
+void FastNoise::init(Random *random, int levels)
+{
+ this->levels = levels;
+ noiseMaps = new byte *[levels];
+ for (int i = 0; i < levels; i++)
+ {
+ noiseMaps[i] = new byte[0x100000];
+ random->nextBytes(noiseMaps[i],0x100000);
+ }
+}
+
+FastNoise::~FastNoise()
+{
+ for( int i = 0; i < levels; i++ )
+ {
+ delete [] noiseMaps[i];
+ }
+ delete [] noiseMaps;
+}
+
+doubleArray FastNoise::getRegion(doubleArray buffer, double x, double y, double z, int xSize, int ySize, int zSize, double xScale, double yScale, double zScale)
+{
+ if (buffer.data == NULL) buffer = doubleArray(xSize * ySize * zSize);
+ else for (unsigned int i = 0; i < buffer.length; i++)
+ buffer[i] = 0;
+
+
+ double pow = 1;
+ int AA = 487211441;
+ int BB = 21771;
+ for (int i = 0; i < levels; i++)
+ {
+ byte *map = noiseMaps[i];
+ int pp = 0;
+
+ for (int zp = 0; zp < zSize; zp++)
+ {
+ double zz = (z + zp) * zScale;
+ int Z = (int) zz;
+ if (zz < Z) Z -= 1;
+ int zl = (int) ((zz - Z) * 65536);
+
+ for (int yp = 0; yp < ySize; yp++)
+ {
+ double yy = (y + yp) * yScale;
+ int Y = (int) yy;
+ if (yy < Y) Y -= 1;
+ int yl = (int) ((yy - Y) * 65536);
+
+ for (int xp = 0; xp < xSize; xp++)
+ {
+ double xx = (x + xp) * xScale;
+ int X = (int) xx;
+ if (xx < X) X -= 1;
+ int xl = (int) ((xx - X) * 65536);
+
+ int X0 = (X + 0) * AA;
+ int X1 = (X + 1) * AA;
+
+ int Y0 = (Y + 0);
+ int Y1 = (Y + 1);
+ int Z0 = (Z + 0);
+ int Z1 = (Z + 1);
+
+ int X0Y0 = (X0 + Y0) * BB;
+ int X1Y0 = (X1 + Y0) * BB;
+ int X0Y1 = (X0 + Y1) * BB;
+ int X1Y1 = (X1 + Y1) * BB;
+
+ int a = map[(X0Y0 + Z0) & 0xfffff];
+ int b = map[(X1Y0 + Z0) & 0xfffff];
+ int c = map[(X0Y1 + Z0) & 0xfffff];
+ int d = map[(X1Y1 + Z0) & 0xfffff];
+ int e = map[(X0Y0 + Z1) & 0xfffff];
+ int f = map[(X1Y0 + Z1) & 0xfffff];
+ int g = map[(X0Y1 + Z1) & 0xfffff];
+ int h = map[(X1Y1 + Z1) & 0xfffff];
+
+ int ab = a + (((b - a) * xl) >> 16);
+ int cd = c + (((d - c) * xl) >> 16);
+ int ef = e + (((f - e) * xl) >> 16);
+ int gh = g + (((h - g) * xl) >> 16);
+
+ int abcd = ab + (((cd - ab) * yl) >> 16);
+ int efgh = ef + (((gh - ef) * yl) >> 16);
+
+ int res = abcd + (((efgh - abcd) * zl) >> 16);
+
+ buffer[pp++] += res*pow;
+ }
+ }
+ }
+ pow /= 2;
+ xScale*=2;
+ yScale*=2;
+ zScale*=2;
+ }
+
+ return buffer;
+}