A little refactoring

This commit is contained in:
Aleksey 2021-01-12 11:30:31 +03:00
parent a2d7a5f7c8
commit 0ffbb1c8f2
2 changed files with 75 additions and 69 deletions

View file

@ -161,6 +161,14 @@ public class MHelper {
return i * i; return i * i;
} }
public static float pow2(float f) {
return f * f;
}
public static double pow2(double d) {
return d * d;
}
public static int fromHSBtoRGB(float hue, float saturation, float brightness) { public static int fromHSBtoRGB(float hue, float saturation, float brightness) {
int red = 0; int red = 0;
int green = 0; int green = 0;

View file

@ -48,7 +48,7 @@ public class LakePiece extends BasePiece {
this.center = center; this.center = center;
this.radius = radius; this.radius = radius;
this.depth = depth; this.depth = depth;
this.r2 = radius * radius; this.r2 = MHelper.pow2(radius);
this.seed1 = random.nextInt(); this.seed1 = random.nextInt();
this.seed2 = random.nextInt(); this.seed2 = random.nextInt();
this.noise1 = new OpenSimplexNoise(this.seed1); this.noise1 = new OpenSimplexNoise(this.seed1);
@ -77,7 +77,7 @@ public class LakePiece extends BasePiece {
center = NbtHelper.toBlockPos(tag.getCompound("center")); center = NbtHelper.toBlockPos(tag.getCompound("center"));
radius = tag.getFloat("radius"); radius = tag.getFloat("radius");
depth = tag.getFloat("depth"); depth = tag.getFloat("depth");
r2 = radius * radius; r2 = MHelper.pow2(radius);
seed1 = tag.getInt("seed1"); seed1 = tag.getInt("seed1");
seed2 = tag.getInt("seed2"); seed2 = tag.getInt("seed2");
noise1 = new OpenSimplexNoise(seed1); noise1 = new OpenSimplexNoise(seed1);
@ -94,84 +94,82 @@ public class LakePiece extends BasePiece {
Heightmap map = chunk.getHeightmap(Type.WORLD_SURFACE_WG); Heightmap map = chunk.getHeightmap(Type.WORLD_SURFACE_WG);
for (int x = 0; x < 16; x++) { for (int x = 0; x < 16; x++) {
int px = x + sx; int px = x + sx;
int px2 = px - center.getX(); int px2 = MHelper.pow2(px - center.getX());
px2 *= px2;
pos.setX(x); pos.setX(x);
for (int z = 0; z < 16; z++) { for (int z = 0; z < 16; z++) {
int pz = z + sz; int pz = z + sz;
int pz2 = pz - center.getZ(); int pz2 = MHelper.pow2(pz - center.getZ());
pz2 *= pz2;
float dist = px2 + pz2; float dist = px2 + pz2;
if (dist < r2) { if (dist > r2) continue;
pos.setZ(z);
dist = 1 - dist / r2; pos.setZ(z);
int maxY = map.get(x, z); dist = 1 - dist / r2;
if (MathHelper.abs(maxY - center.getY()) < 8) { int maxY = map.get(x, z);
float minY = (float) Math.sqrt(dist) * depth * getHeightClamp(world, 8, px, pz); if (MathHelper.abs(maxY - center.getY()) < 8) {
if (minY > 0) { float minY = (float) Math.sqrt(dist) * depth * getHeightClamp(world, 8, px, pz);
minY *= (float) noise1.eval(px * 0.05, pz * 0.05) * 0.3F + 0.7F; if (minY > 0) {
minY *= (float) noise1.eval(px * 0.1, pz * 0.1) * 0.1F + 0.8F; minY *= (float) noise1.eval(px * 0.05, pz * 0.05) * 0.3F + 0.7F;
float lerp = minY / 4F; minY *= (float) noise1.eval(px * 0.1, pz * 0.1) * 0.1F + 0.8F;
if (lerp > 1) { float lerp = minY / 4F;
lerp = 1; if (lerp > 1) {
} lerp = 1;
minY = MathHelper.lerp(lerp, maxY - minY, center.getY() - minY); }
pos.setY(maxY); minY = MathHelper.lerp(lerp, maxY - minY, center.getY() - minY);
while (!chunk.getBlockState(pos).getMaterial().isReplaceable()) { pos.setY(maxY);
pos.setY(maxY ++); while (!chunk.getBlockState(pos).getMaterial().isReplaceable()) {
} pos.setY(maxY ++);
}
if (!waterIsNear(world, pos.getX() + sx, center.getY(), pos.getZ() + sz, 2)) {
if (maxY <= center.getY() + 1) { if (!waterIsNear(world, pos.getX() + sx, center.getY(), pos.getZ() + sz, 2)) {
maxY = MathHelper.clamp(maxY + 4, 0, center.getY()); if (maxY <= center.getY() + 1) {
BlockState surf = random.nextBoolean() ? EndBlocks.ENDSTONE_DUST.getDefaultState() : world.getBiome(pos.add(sx, 0, sz)).getGenerationSettings().getSurfaceConfig().getTopMaterial(); maxY = MathHelper.clamp(maxY + 4, 0, center.getY());
BlockState under = world.getBiome(pos.add(sx, 0, sz)).getGenerationSettings().getSurfaceConfig().getUnderMaterial(); BlockState surf = random.nextBoolean() ? EndBlocks.ENDSTONE_DUST.getDefaultState() : world.getBiome(pos.add(sx, 0, sz)).getGenerationSettings().getSurfaceConfig().getTopMaterial();
for (int y = maxY; y >= minY; y--) { BlockState under = world.getBiome(pos.add(sx, 0, sz)).getGenerationSettings().getSurfaceConfig().getUnderMaterial();
pos.setY(y); for (int y = maxY; y >= minY; y--) {
BlockState state = chunk.getBlockState(pos); pos.setY(y);
if (state.getMaterial().isReplaceable() || state.isIn(EndTags.GEN_TERRAIN)) { BlockState state = chunk.getBlockState(pos);
chunk.setBlockState(pos, y > center.getY() ? AIR : y == maxY ? surf : under, false);
}
else {
break;
}
}
}
else {
for (int y = maxY; y >= minY; y--) {
pos.setY(y);
BlockState state = chunk.getBlockState(pos);
if (state.getMaterial().isReplaceable() || state.isIn(EndTags.GEN_TERRAIN)) {
if (maxY <= center.getY() + 1)
chunk.setBlockState(pos, Blocks.REDSTONE_BLOCK.getDefaultState(), false);
else
chunk.setBlockState(pos, y > center.getY() ? AIR : WATER, false);
}
else {
break;
}
}
}
}
boolean under = pos.getY() <= center.getY() || (pos.getY() == center.getY() + 1 && random.nextBoolean());
pos.setY(pos.getY() - 1);
BlockState state = chunk.getBlockState(pos);
if (state.isIn(EndTags.GEN_TERRAIN) || (state.getMaterial().isReplaceable() && pos.getY() <= center.getY())) {
state = under ? EndBlocks.ENDSTONE_DUST.getDefaultState() : world.getBiome(pos.add(sx, 0, sz)).getGenerationSettings().getSurfaceConfig().getTopMaterial();
chunk.setBlockState(pos, state, false);
maxY = (int) (noise1.eval((pos.getX() + sx) * 0.1, (pos.getZ() + sz) * 0.1) + 2);
state = world.getBiome(pos.add(sx, 0, sz)).getGenerationSettings().getSurfaceConfig().getUnderMaterial();
for (int i = 0; i < maxY; i++) {
pos.setY(pos.getY() - 1);
if (state.getMaterial().isReplaceable() || state.isIn(EndTags.GEN_TERRAIN)) { if (state.getMaterial().isReplaceable() || state.isIn(EndTags.GEN_TERRAIN)) {
chunk.setBlockState(pos, state, false); chunk.setBlockState(pos, y > center.getY() ? AIR : y == maxY ? surf : under, false);
} }
else { else {
break; break;
} }
} }
} }
else {
for (int y = maxY; y >= minY; y--) {
pos.setY(y);
BlockState state = chunk.getBlockState(pos);
if (state.getMaterial().isReplaceable() || state.isIn(EndTags.GEN_TERRAIN)) {
if (maxY <= center.getY() + 1)
chunk.setBlockState(pos, Blocks.REDSTONE_BLOCK.getDefaultState(), false);
else
chunk.setBlockState(pos, y > center.getY() ? AIR : WATER, false);
}
else {
break;
}
}
}
}
boolean under = pos.getY() <= center.getY() || (pos.getY() == center.getY() + 1 && random.nextBoolean());
pos.setY(pos.getY() - 1);
BlockState state = chunk.getBlockState(pos);
if (state.isIn(EndTags.GEN_TERRAIN) || (state.getMaterial().isReplaceable() && pos.getY() <= center.getY())) {
state = under ? EndBlocks.ENDSTONE_DUST.getDefaultState() : world.getBiome(pos.add(sx, 0, sz)).getGenerationSettings().getSurfaceConfig().getTopMaterial();
chunk.setBlockState(pos, state, false);
maxY = (int) (noise1.eval((pos.getX() + sx) * 0.1, (pos.getZ() + sz) * 0.1) + 2);
state = world.getBiome(pos.add(sx, 0, sz)).getGenerationSettings().getSurfaceConfig().getUnderMaterial();
for (int i = 0; i < maxY; i++) {
pos.setY(pos.getY() - 1);
if (state.getMaterial().isReplaceable() || state.isIn(EndTags.GEN_TERRAIN)) {
chunk.setBlockState(pos, state, false);
}
else {
break;
}
}
} }
} }
} }