tcr/java/src/game/worldgen/GeneratorCavern.java

200 lines
6.7 KiB
Java
Executable file

package game.worldgen;
import game.rng.OctaveGen;
import game.rng.Random;
import game.util.ExtMath;
import game.world.State;
import game.world.WorldServer;
public class GeneratorCavern implements ChunkGenerator
{
private final OctaveGen noiseGen2;
private final OctaveGen noiseGen3;
private final OctaveGen noiseGen1;
// private final OctaveGen noiseGen4;
// private final OctaveGen noiseGen5;
private final double[] noiseField = new double[425];
private final double[] noiseData1 = new double[425];
private final double[] noiseData2 = new double[425];
private final double[] noiseData3 = new double[425];
// private final double[] noiseData4 = new double[25];
// private final double[] noiseData5 = new double[25];
private final State filler;
private final State liquid;
// public GeneratorCavern(Dimension dim, Random rand) {
// this(rand);
// }
public GeneratorCavern(Random rand, State filler, State liquid)
{
this.noiseGen2 = new OctaveGen(rand, 16);
this.noiseGen3 = new OctaveGen(rand, 16);
this.noiseGen1 = new OctaveGen(rand, 8);
// this.noiseGen4 = new OctaveGen(rand, 10);
// this.noiseGen5 = new OctaveGen(rand, 16);
this.filler = filler;
this.liquid = liquid;
}
public int getMaximumHeight() {
return 128;
}
public void generateChunk(WorldServer world, int x, int z, ChunkPrimer primer)
{
int range = 4;
int lh = world.getSeaLevel() / 2 + 1;
int xr = range + 1;
int yr = 17;
int zr = range + 1;
this.genNoisemap(x * range, 0, z * range);
for (int j1 = 0; j1 < range; ++j1)
{
for (int k1 = 0; k1 < range; ++k1)
{
for (int l1 = 0; l1 < 16; ++l1)
{
double d0 = 0.125D;
double d1 = this.noiseField[((j1 + 0) * zr + k1 + 0) * yr + l1 + 0];
double d2 = this.noiseField[((j1 + 0) * zr + k1 + 1) * yr + l1 + 0];
double d3 = this.noiseField[((j1 + 1) * zr + k1 + 0) * yr + l1 + 0];
double d4 = this.noiseField[((j1 + 1) * zr + k1 + 1) * yr + l1 + 0];
double d5 = (this.noiseField[((j1 + 0) * zr + k1 + 0) * yr + l1 + 1] - d1) * d0;
double d6 = (this.noiseField[((j1 + 0) * zr + k1 + 1) * yr + l1 + 1] - d2) * d0;
double d7 = (this.noiseField[((j1 + 1) * zr + k1 + 0) * yr + l1 + 1] - d3) * d0;
double d8 = (this.noiseField[((j1 + 1) * zr + k1 + 1) * yr + l1 + 1] - d4) * d0;
for (int i2 = 0; i2 < 8; ++i2)
{
double d9 = 0.25D;
double d10 = d1;
double d11 = d2;
double d12 = (d3 - d1) * d9;
double d13 = (d4 - d2) * d9;
for (int j2 = 0; j2 < 4; ++j2)
{
double d14 = 0.25D;
double d15 = d10;
double d16 = (d11 - d10) * d14;
for (int k2 = 0; k2 < 4; ++k2)
{
State iblockstate = null;
if (l1 * 8 + i2 < lh)
{
iblockstate = this.liquid;
}
if (d15 > 0.0D)
{
iblockstate = this.filler;
}
int l2 = j2 + j1 * 4;
int i3 = i2 + l1 * 8;
int j3 = k2 + k1 * 4;
primer.set(l2, i3, j3, iblockstate);
d15 += d16;
}
d10 += d12;
d11 += d13;
}
d1 += d5;
d2 += d6;
d3 += d7;
d4 += d8;
}
}
}
}
}
private void genNoisemap(int x, int y, int z)
{
int xs = 5;
int ys = 17;
int zs = 5;
double d0 = 684.412D;
double d1 = 2053.236D;
// this.noiseGen4.generate(this.noiseData4, x, y, z, xs, 1, zs, 1.0D, 0.0D, 1.0D);
// this.noiseGen5.generate(this.noiseData5, x, y, z, xs, 1, zs, 100.0D, 0.0D, 100.0D);
this.noiseGen1.generate(this.noiseData1, x, y, z, xs, ys, zs, d0 / 80.0D, d1 / 60.0D, d0 / 80.0D);
this.noiseGen2.generate(this.noiseData2, x, y, z, xs, ys, zs, d0, d1, d0);
this.noiseGen3.generate(this.noiseData3, x, y, z, xs, ys, zs, d0, d1, d0);
int i = 0;
double[] adouble = new double[ys];
for (int j = 0; j < ys; ++j)
{
adouble[j] = Math.cos((double)j * Math.PI * 6.0D / (double)ys) * 2.0D;
double d2 = (double)j;
if (j > ys / 2)
{
d2 = (double)(ys - 1 - j);
}
if (d2 < 4.0D)
{
d2 = 4.0D - d2;
adouble[j] -= d2 * d2 * d2 * 10.0D;
}
}
for (int l = 0; l < xs; ++l)
{
for (int i1 = 0; i1 < zs; ++i1)
{
double d3 = 0.0D;
for (int k = 0; k < ys; ++k)
{
double d4 = 0.0D;
double d5 = adouble[k];
double d6 = this.noiseData2[i] / 512.0D;
double d7 = this.noiseData3[i] / 512.0D;
double d8 = (this.noiseData1[i] / 10.0D + 1.0D) / 2.0D;
if (d8 < 0.0D)
{
d4 = d6;
}
else if (d8 > 1.0D)
{
d4 = d7;
}
else
{
d4 = d6 + (d7 - d6) * d8;
}
d4 = d4 - d5;
if (k > ys - 4)
{
double d9 = (double)((float)(k - (ys - 4)) / 3.0F);
d4 = d4 * (1.0D - d9) + -10.0D * d9;
}
if ((double)k < d3)
{
double d10 = (d3 - (double)k) / 4.0D;
d10 = ExtMath.clampd(d10, 0.0D, 1.0D);
d4 = d4 * (1.0D - d10) + -10.0D * d10;
}
this.noiseField[i] = d4;
++i;
}
}
}
}
}