the whole game
This commit is contained in:
205
src/world/level/levelgen/synth/ImprovedNoise.cpp
Executable file
205
src/world/level/levelgen/synth/ImprovedNoise.cpp
Executable file
@@ -0,0 +1,205 @@
|
||||
#include "ImprovedNoise.h"
|
||||
#include "../../../../util/Random.h"
|
||||
|
||||
ImprovedNoise::ImprovedNoise()
|
||||
{
|
||||
Random random(1);
|
||||
init(&random);
|
||||
}
|
||||
|
||||
ImprovedNoise::ImprovedNoise( Random* random )
|
||||
{
|
||||
init(random);
|
||||
}
|
||||
|
||||
void ImprovedNoise::init( Random* random )
|
||||
{
|
||||
xo = random->nextFloat() * 256.f;
|
||||
yo = random->nextFloat() * 256.f;
|
||||
zo = random->nextFloat() * 256.f;
|
||||
for (int i = 0; i < 256; i++) {
|
||||
p[i] = i;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 256; i++) {
|
||||
int j = random->nextInt(256 - i) + i;
|
||||
int tmp = p[i];
|
||||
p[i] = p[j];
|
||||
p[j] = tmp;
|
||||
|
||||
p[i + 256] = p[i];
|
||||
}
|
||||
}
|
||||
|
||||
float ImprovedNoise::noise( float _x, float _y, float _z )
|
||||
{
|
||||
float x = _x + xo;
|
||||
float y = _y + yo;
|
||||
float z = _z + zo;
|
||||
|
||||
int xf = (int) x;
|
||||
int yf = (int) y;
|
||||
int zf = (int) z;
|
||||
|
||||
if (x < xf) xf--;
|
||||
if (y < yf) yf--;
|
||||
if (z < zf) zf--;
|
||||
|
||||
int X = xf & 255, // FIND UNIT CUBE THAT
|
||||
Y = yf & 255, // CONTAINS POINT.
|
||||
Z = zf & 255;
|
||||
|
||||
x -= xf; // FIND RELATIVE X,Y,Z
|
||||
y -= yf; // OF POINT IN CUBE.
|
||||
z -= zf;
|
||||
|
||||
float u = x * x * x * (x * (x * 6 - 15) + 10), // COMPUTE FADE CURVES
|
||||
v = y * y * y * (y * (y * 6 - 15) + 10), // FOR EACH OF X,Y,Z.
|
||||
w = z * z * z * (z * (z * 6 - 15) + 10);
|
||||
|
||||
int A = p[X] + Y, AA = p[A] + Z, AB = p[A + 1] + Z, // HASH COORDINATES OF
|
||||
B = p[X + 1] + Y, BA = p[B] + Z, BB = p[B + 1] + Z; // THE 8 CUBE CORNERS,
|
||||
|
||||
return lerp(w, lerp(v, lerp(u, grad(p[AA], x, y, z), // AND ADD
|
||||
grad(p[BA], x - 1, y, z)), // BLENDED
|
||||
lerp(u, grad(p[AB], x, y - 1, z), // RESULTS
|
||||
grad(p[BB], x - 1, y - 1, z))),// FROM 8
|
||||
lerp(v, lerp(u, grad(p[AA + 1], x, y, z - 1), // CORNERS
|
||||
grad(p[BA + 1], x - 1, y, z - 1)), // OF CUBE
|
||||
lerp(u, grad(p[AB + 1], x, y - 1, z - 1), grad(p[BB + 1], x - 1, y - 1, z - 1))));
|
||||
}
|
||||
|
||||
const float ImprovedNoise::lerp( float t, float a, float b )
|
||||
{
|
||||
return a + t * (b - a);
|
||||
}
|
||||
|
||||
const float ImprovedNoise::grad2( int hash, float x, float z )
|
||||
{
|
||||
int h = hash & 15; // CONVERT LO 4 BITS OF HASH CODE
|
||||
|
||||
float u = (1-((h&8)>>3))*x, // INTO 12 GRADIENT DIRECTIONS.
|
||||
v = h < 4 ? 0 : h == 12 || h == 14 ? x : z;
|
||||
|
||||
return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
|
||||
}
|
||||
|
||||
const float ImprovedNoise::grad( int hash, float x, float y, float z )
|
||||
{
|
||||
int h = hash & 15; // CONVERT LO 4 BITS OF HASH CODE
|
||||
|
||||
float u = h < 8 ? x : y, // INTO 12 GRADIENT DIRECTIONS.
|
||||
v = h < 4 ? y : h == 12 || h == 14 ? x : z;
|
||||
|
||||
return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
|
||||
}
|
||||
|
||||
float ImprovedNoise::getValue( float x, float y )
|
||||
{
|
||||
return noise(x, y, 0);
|
||||
}
|
||||
|
||||
float ImprovedNoise::getValue( float x, float y, float z )
|
||||
{
|
||||
return noise(x, y, z);
|
||||
}
|
||||
|
||||
void ImprovedNoise::add( float* buffer, float _x, float _y, float _z, int xSize, int ySize, int zSize, float xs, float ys, float zs, float pow )
|
||||
{
|
||||
if (ySize==1) {
|
||||
int A = 0, AA = 0, B = 0, BA = 0;
|
||||
float vv0 = 0, vv2 = 0;
|
||||
int pp = 0;
|
||||
float scale = 1.0f / pow;
|
||||
for (int xx = 0; xx < xSize; xx++) {
|
||||
float x = (_x + xx) * xs + xo;
|
||||
int xf = (int) x;
|
||||
if (x < xf) xf--;
|
||||
int X = xf & 255;
|
||||
x -= xf;
|
||||
float u = x * x * x * (x * (x * 6 - 15) + 10);
|
||||
|
||||
for (int zz = 0; zz < zSize; zz++) {
|
||||
float z = (_z + zz) * zs + zo;
|
||||
int zf = (int) z;
|
||||
if (z < zf) zf--;
|
||||
int Z = zf & 255;
|
||||
z -= zf;
|
||||
float w = z * z * z * (z * (z * 6 - 15) + 10);
|
||||
|
||||
A = p[X] + 0;
|
||||
AA = p[A] + Z;
|
||||
B = p[X + 1] + 0;
|
||||
BA = p[B] + Z;
|
||||
vv0 = lerp(u, grad2(p[AA], x, z), grad(p[BA], x - 1, 0, z));
|
||||
vv2 = lerp(u, grad(p[AA + 1], x, 0, z - 1), grad(p[BA + 1], x - 1, 0, z - 1));
|
||||
|
||||
float val = lerp(w, vv0, vv2);
|
||||
|
||||
buffer[pp++] += val * scale;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
int pp = 0;
|
||||
float scale = 1 / pow;
|
||||
int yOld = -1;
|
||||
int A = 0, AA = 0, AB = 0, B = 0, BA = 0, BB = 0;
|
||||
float vv0 = 0, vv1 = 0, vv2 = 0, vv3 = 0;
|
||||
|
||||
for (int xx = 0; xx < xSize; xx++) {
|
||||
float x = (_x + xx) * xs + xo;
|
||||
int xf = (int) x;
|
||||
if (x < xf) xf--;
|
||||
int X = xf & 255;
|
||||
x -= xf;
|
||||
float u = x * x * x * (x * (x * 6 - 15) + 10);
|
||||
|
||||
|
||||
for (int zz = 0; zz < zSize; zz++) {
|
||||
float z = (_z + zz) * zs + zo;
|
||||
int zf = (int) z;
|
||||
if (z < zf) zf--;
|
||||
int Z = zf & 255;
|
||||
z -= zf;
|
||||
float w = z * z * z * (z * (z * 6 - 15) + 10);
|
||||
|
||||
for (int yy = 0; yy < ySize; yy++) {
|
||||
float y = (_y + yy) * ys + yo;
|
||||
int yf = (int) y;
|
||||
if (y < yf) yf--;
|
||||
int Y = yf & 255;
|
||||
y -= yf;
|
||||
float v = y * y * y * (y * (y * 6 - 15) + 10);
|
||||
|
||||
if (yy == 0 || Y != yOld) {
|
||||
yOld = Y;
|
||||
A = p[X] + Y;
|
||||
AA = p[A] + Z;
|
||||
AB = p[A + 1] + Z;
|
||||
B = p[X + 1] + Y;
|
||||
BA = p[B] + Z;
|
||||
BB = p[B + 1] + Z;
|
||||
vv0 = lerp(u, grad(p[AA], x, y, z), grad(p[BA], x - 1, y, z));
|
||||
vv1 = lerp(u, grad(p[AB], x, y - 1, z), grad(p[BB], x - 1, y - 1, z));
|
||||
vv2 = lerp(u, grad(p[AA + 1], x, y, z - 1), grad(p[BA + 1], x - 1, y, z - 1));
|
||||
vv3 = lerp(u, grad(p[AB + 1], x, y - 1, z - 1), grad(p[BB + 1], x - 1, y - 1, z - 1));
|
||||
}
|
||||
|
||||
float v0 = lerp(v, vv0, vv1);
|
||||
float v1 = lerp(v, vv2, vv3);
|
||||
float val = lerp(w, v0, v1);
|
||||
|
||||
buffer[pp++] += val * scale;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int ImprovedNoise::hashCode() {
|
||||
int x = 4711;
|
||||
for (int i = 0; i < 512; ++i)
|
||||
x = x * 37 + p[i];
|
||||
return x;
|
||||
}
|
||||
|
||||
38
src/world/level/levelgen/synth/ImprovedNoise.h
Executable file
38
src/world/level/levelgen/synth/ImprovedNoise.h
Executable file
@@ -0,0 +1,38 @@
|
||||
#ifndef NET_MINECRAFT_WORLD_LEVEL_LEVELGEN_SYNTH__ImprovedNoise_H__
|
||||
#define NET_MINECRAFT_WORLD_LEVEL_LEVELGEN_SYNTH__ImprovedNoise_H__
|
||||
|
||||
//package net.minecraft.world.level.levelgen.synth;
|
||||
|
||||
#include "Synth.h"
|
||||
class Random;
|
||||
|
||||
class ImprovedNoise: public Synth
|
||||
{
|
||||
public:
|
||||
ImprovedNoise();
|
||||
|
||||
ImprovedNoise(Random* random);
|
||||
|
||||
void init(Random* random);
|
||||
|
||||
float noise(float _x, float _y, float _z);
|
||||
|
||||
const float lerp(float t, float a, float b);
|
||||
|
||||
const float grad2(int hash, float x, float z);
|
||||
const float grad(int hash, float x, float y, float z);
|
||||
|
||||
float getValue(float x, float y);
|
||||
float getValue(float x, float y, float z);
|
||||
|
||||
void add(float* buffer, float _x, float _y, float _z, int xSize, int ySize, int zSize, float xs, float ys, float zs, float pow);
|
||||
|
||||
int hashCode();
|
||||
|
||||
float scale;
|
||||
float xo, yo, zo;
|
||||
private:
|
||||
int p[512];
|
||||
};
|
||||
|
||||
#endif /*NET_MINECRAFT_WORLD_LEVEL_LEVELGEN_SYNTH__ImprovedNoise_H__*/
|
||||
88
src/world/level/levelgen/synth/PerlinNoise.cpp
Executable file
88
src/world/level/levelgen/synth/PerlinNoise.cpp
Executable file
@@ -0,0 +1,88 @@
|
||||
#include "PerlinNoise.h"
|
||||
#include "ImprovedNoise.h"
|
||||
|
||||
void PerlinNoise::init( int levels )
|
||||
{
|
||||
this->levels = levels;
|
||||
noiseLevels = new ImprovedNoise* [levels];
|
||||
for (int i = 0; i < levels; i++) {
|
||||
noiseLevels[i] = new ImprovedNoise(_rndPtr);
|
||||
}
|
||||
}
|
||||
|
||||
PerlinNoise::~PerlinNoise()
|
||||
{
|
||||
for (int i = 0; i < levels; ++i)
|
||||
delete noiseLevels[i];
|
||||
delete[] noiseLevels;
|
||||
}
|
||||
|
||||
PerlinNoise::PerlinNoise( int levels )
|
||||
{
|
||||
_rndPtr = &_random;
|
||||
init(levels);
|
||||
}
|
||||
|
||||
PerlinNoise::PerlinNoise( Random* random, int levels )
|
||||
{
|
||||
_rndPtr = random;
|
||||
init(levels);
|
||||
}
|
||||
|
||||
float PerlinNoise::getValue( float x, float y )
|
||||
{
|
||||
float value = 0;
|
||||
float pow = 1;
|
||||
|
||||
for (int i = 0; i < levels; i++) {
|
||||
value += noiseLevels[i]->getValue(x * pow, y * pow) / pow;
|
||||
pow /= 2;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
float PerlinNoise::getValue( float x, float y, float z )
|
||||
{
|
||||
float value = 0;
|
||||
float pow = 1;
|
||||
|
||||
for (int i = 0; i < levels; i++) {
|
||||
value += noiseLevels[i]->getValue(x * pow, y * pow, z * pow) / pow;
|
||||
pow /= 2;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
float* PerlinNoise::getRegion( float* buffer, float x, float y, float z, int xSize, int ySize, int zSize, float xScale, float yScale, float zScale )
|
||||
{
|
||||
const int size = xSize * ySize * zSize;
|
||||
if (buffer == 0) {
|
||||
buffer = new float[size];
|
||||
}
|
||||
for (int i = 0; i < size; i++)
|
||||
buffer[i] = 0;
|
||||
|
||||
float pow = 1;
|
||||
|
||||
for (int i = 0; i < levels; i++) {
|
||||
noiseLevels[i]->add(buffer, x, y, z, xSize, ySize, zSize, xScale * pow, yScale * pow, zScale * pow, pow);
|
||||
pow /= 2;
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
float* PerlinNoise::getRegion( float* sr, int x, int z, int xSize, int zSize, float xScale, float zScale, float pow )
|
||||
{
|
||||
return getRegion(sr, (float)x, 10.0f, (float)z, xSize, 1, zSize, xScale, 1, zScale);
|
||||
}
|
||||
|
||||
int PerlinNoise::hashCode() {
|
||||
int x = 4711;
|
||||
for (int i = 0; i < levels; ++i)
|
||||
x *= noiseLevels[i]->hashCode();
|
||||
return x;
|
||||
}
|
||||
|
||||
37
src/world/level/levelgen/synth/PerlinNoise.h
Executable file
37
src/world/level/levelgen/synth/PerlinNoise.h
Executable file
@@ -0,0 +1,37 @@
|
||||
#ifndef NET_MINECRAFT_WORLD_LEVEL_LEVELGEN_SYNTH__PerlinNoise_H__
|
||||
#define NET_MINECRAFT_WORLD_LEVEL_LEVELGEN_SYNTH__PerlinNoise_H__
|
||||
|
||||
//package net.minecraft.world.level.levelgen.synth;
|
||||
|
||||
#include "../../../../util/Random.h"
|
||||
#include "Synth.h"
|
||||
|
||||
class ImprovedNoise;
|
||||
|
||||
class PerlinNoise: public Synth
|
||||
{
|
||||
public:
|
||||
PerlinNoise(int levels);
|
||||
PerlinNoise(Random* random, int levels);
|
||||
~PerlinNoise();
|
||||
|
||||
float getValue(float x, float y);
|
||||
float getValue(float x, float y, float z);
|
||||
|
||||
//float[] getRegion(float[] buffer, float x, float y, float z, int xSize, int ySize, int zSize, float xScale, float yScale, float zScale) {
|
||||
float* getRegion(float* buffer, float x, float y, float z, int xSize, int ySize, int zSize, float xScale, float yScale, float zScale);
|
||||
float* getRegion(float* sr, int x, int z, int xSize, int zSize, float xScale, float zScale, float pow);
|
||||
|
||||
int hashCode();
|
||||
|
||||
private:
|
||||
ImprovedNoise** noiseLevels;
|
||||
int levels;
|
||||
|
||||
Random _random;
|
||||
Random* _rndPtr;
|
||||
|
||||
void init(int levels);
|
||||
};
|
||||
|
||||
#endif /*NET_MINECRAFT_WORLD_LEVEL_LEVELGEN_SYNTH__PerlinNoise_H__*/
|
||||
21
src/world/level/levelgen/synth/Synth.cpp
Executable file
21
src/world/level/levelgen/synth/Synth.cpp
Executable file
@@ -0,0 +1,21 @@
|
||||
#include "Synth.h"
|
||||
|
||||
Synth::~Synth()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int Synth::getDataSize( int width, int height )
|
||||
{
|
||||
return width * height * sizeof(float);
|
||||
}
|
||||
|
||||
void Synth::create( int width, int height, float* result )
|
||||
{
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
result[x + y * width] = getValue((float)x, (float)y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
18
src/world/level/levelgen/synth/Synth.h
Executable file
18
src/world/level/levelgen/synth/Synth.h
Executable file
@@ -0,0 +1,18 @@
|
||||
#ifndef NET_MINECRAFT_WORLD_LEVEL_LEVELGEN_SYNTH__Synth_H__
|
||||
#define NET_MINECRAFT_WORLD_LEVEL_LEVELGEN_SYNTH__Synth_H__
|
||||
|
||||
//package net.minecraft.world.level.levelgen.synth;
|
||||
|
||||
class Synth
|
||||
{
|
||||
public:
|
||||
virtual ~Synth();
|
||||
|
||||
int getDataSize(int width, int height);
|
||||
|
||||
virtual float getValue(float x, float y) = 0;
|
||||
|
||||
void create(int width, int height, float* result);
|
||||
};
|
||||
|
||||
#endif /*NET_MINECRAFT_WORLD_LEVEL_LEVELGEN_SYNTH__Synth_H__*/
|
||||
Reference in New Issue
Block a user