Fixed leaves cascade update, basic structure classes separation

This commit is contained in:
paulevsGitch 2020-11-07 12:57:07 +03:00
parent d87b98fcd7
commit d0135e83e9
16 changed files with 136 additions and 53 deletions

View file

@ -0,0 +1,226 @@
package ru.betterend.world.features.terrain;
import java.util.Random;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.Material;
import net.minecraft.fluid.FluidState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockPos.Mutable;
import net.minecraft.world.StructureWorldAccess;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.feature.DefaultFeatureConfig;
import ru.betterend.noise.OpenSimplexNoise;
import ru.betterend.registry.EndBlocks;
import ru.betterend.registry.EndTags;
import ru.betterend.util.BlocksHelper;
import ru.betterend.util.MHelper;
import ru.betterend.world.features.DefaultFeature;
public class EndLakeFeature extends DefaultFeature {
private static final BlockState END_STONE = Blocks.END_STONE.getDefaultState();
private static final OpenSimplexNoise NOISE = new OpenSimplexNoise(15152);
private static final Mutable POS = new Mutable();
@Override
public boolean generate(StructureWorldAccess world, ChunkGenerator chunkGenerator, Random random, BlockPos blockPos, DefaultFeatureConfig featureConfig) {
double radius = MHelper.randRange(10.0, 20.0, random);
double depth = radius * 0.5 * MHelper.randRange(0.8, 1.2, random);
int dist = MHelper.floor(radius);
int dist2 = MHelper.floor(radius * 1.5);
int bott = MHelper.floor(depth);
blockPos = getPosOnSurfaceWG(world, blockPos);
if (blockPos.getY() < 10) return false;
int waterLevel = blockPos.getY();
BlockPos pos = getPosOnSurfaceRaycast(world, blockPos.north(dist).up(10), 20);
if (Math.abs(blockPos.getY() - pos.getY()) > 5) return false;
waterLevel = MHelper.min(pos.getY(), waterLevel);
pos = getPosOnSurfaceRaycast(world, blockPos.south(dist).up(10), 20);
if (Math.abs(blockPos.getY() - pos.getY()) > 5) return false;
waterLevel = MHelper.min(pos.getY(), waterLevel);
pos = getPosOnSurfaceRaycast(world, blockPos.east(dist).up(10), 20);
if (Math.abs(blockPos.getY() - pos.getY()) > 5) return false;
waterLevel = MHelper.min(pos.getY(), waterLevel);
pos = getPosOnSurfaceRaycast(world, blockPos.west(dist).up(10), 20);
if (Math.abs(blockPos.getY() - pos.getY()) > 5) return false;
waterLevel = MHelper.min(pos.getY(), waterLevel);
BlockState state;
int minX = blockPos.getX() - dist2;
int maxX = blockPos.getX() + dist2;
int minZ = blockPos.getZ() - dist2;
int maxZ = blockPos.getZ() + dist2;
int maskMinX = minX - 1;
int maskMinZ = minZ - 1;
boolean[][] mask = new boolean[maxX - minX + 3][maxZ - minZ + 3];
for (int x = minX; x <= maxX; x++) {
POS.setX(x);
int mx = x - maskMinX;
for (int z = minZ; z <= maxZ; z++) {
POS.setZ(z);
int mz = z - maskMinZ;
if (!mask[mx][mz]) {
for (int y = waterLevel + 1; y <= waterLevel + 20; y++) {
POS.setY(y);
FluidState fluid = world.getFluidState(POS);
if (!fluid.isEmpty()) {
for (int i = -1; i < 2; i++) {
int px = mx + i;
for (int j = -1; j < 2; j++) {
int pz = mz + j;
mask[px][pz] = true;
}
}
break;
}
}
}
}
}
for (int x = minX; x <= maxX; x++) {
POS.setX(x);
int x2 = x - blockPos.getX();
x2 *= x2;
int mx = x - maskMinX;
for (int z = minZ; z <= maxZ; z++) {
POS.setZ(z);
int z2 = z - blockPos.getZ();
z2 *= z2;
int mz = z - maskMinZ;
if (!mask[mx][mz]) {
double size = 1;
for (int y = blockPos.getY(); y <= blockPos.getY() + 20; y++) {
POS.setY(y);
double add = y - blockPos.getY();
if (add > 5) {
size *= 0.8;
add = 5;
}
double r = (add * 1.8 + radius * (NOISE.eval(x * 0.2, y * 0.2, z * 0.2) * 0.25 + 0.75)) - 1.0 / size;
if (r > 0) {
r *= r;
if (x2 + z2 <= r) {
state = world.getBlockState(POS);
if (state.isIn(EndTags.GEN_TERRAIN)) {
BlocksHelper.setWithoutUpdate(world, POS, AIR);
}
pos = POS.down();
if (world.getBlockState(pos).isIn(EndTags.GEN_TERRAIN)) {
state = world.getBiome(pos).getGenerationSettings().getSurfaceConfig().getTopMaterial();
if (y > waterLevel + 1)
BlocksHelper.setWithoutUpdate(world, pos, state);
else if (y > waterLevel)
BlocksHelper.setWithoutUpdate(world, pos, random.nextBoolean() ? state : EndBlocks.ENDSTONE_DUST.getDefaultState());
else
BlocksHelper.setWithoutUpdate(world, pos, EndBlocks.ENDSTONE_DUST.getDefaultState());
}
}
}
else {
break;
}
}
}
}
}
double aspect = ((double) radius / (double) depth);
for (int x = blockPos.getX() - dist; x <= blockPos.getX() + dist; x++) {
POS.setX(x);
int x2 = x - blockPos.getX();
x2 *= x2;
int mx = x - maskMinX;
for (int z = blockPos.getZ() - dist; z <= blockPos.getZ() + dist; z++) {
POS.setZ(z);
int z2 = z - blockPos.getZ();
z2 *= z2;
int mz = z - maskMinZ;
if (!mask[mx][mz]) {
for (int y = blockPos.getY() - bott; y < blockPos.getY(); y++) {
POS.setY(y);
double y2 = (double) (y - blockPos.getY()) * aspect;
y2 *= y2;
double r = radius * (NOISE.eval(x * 0.2, y * 0.2, z * 0.2) * 0.25 + 0.75);
double rb = r * 1.2;
r *= r;
rb *= rb;
if (y2 + x2 + z2 <= r) {
state = world.getBlockState(POS);
if (canReplace(state)) {
state = world.getBlockState(POS.up());
state = canReplace(state) ? (y < waterLevel ? WATER : AIR) : state;
BlocksHelper.setWithoutUpdate(world, POS, state);
/*if (y == waterLevel - 1 && !state.getFluidState().isEmpty()) {
world.getFluidTickScheduler().schedule(POS, state.getFluidState().getFluid(), 0);
}*/
}
pos = POS.down();
if (world.getBlockState(pos).getBlock().isIn(EndTags.GEN_TERRAIN)) {
BlocksHelper.setWithoutUpdate(world, POS.down(), EndBlocks.ENDSTONE_DUST.getDefaultState());
}
pos = POS.up();
while (canReplace(state = world.getBlockState(pos)) && !state.isAir() && state.getFluidState().isEmpty()) {
BlocksHelper.setWithoutUpdate(world, pos, pos.getY() < waterLevel ? WATER : AIR);
/*if (y == waterLevel - 1) {
world.getFluidTickScheduler().schedule(POS, WATER.getFluidState().getFluid(), 0);
}*/
pos = pos.up();
}
}
// Make border
else if (y < waterLevel && y2 + x2 + z2 <= rb) {
//if (world.getBlockState(POS).getMaterial().isReplaceable()) {
if (world.isAir(POS.up())) {
state = world.getBiome(POS).getGenerationSettings().getSurfaceConfig().getTopMaterial();
BlocksHelper.setWithoutUpdate(world, POS, random.nextBoolean() ? state : EndBlocks.ENDSTONE_DUST.getDefaultState());
BlocksHelper.setWithoutUpdate(world, POS.down(), END_STONE);
}
else {
BlocksHelper.setWithoutUpdate(world, POS, EndBlocks.ENDSTONE_DUST.getDefaultState());
BlocksHelper.setWithoutUpdate(world, POS.down(), END_STONE);
}
//}
}
}
}
}
}
BlocksHelper.fixBlocks(world, new BlockPos(minX - 2, waterLevel - 2, minZ - 2), new BlockPos(maxX + 2, blockPos.getY() + 20, maxZ + 2));
return true;
}
/*private void generateFoggyMushroomland(WorldAccess world, int x, int z, int waterLevel) {
if (NOISE.eval(x * 0.1, z * 0.1, 0) > 0) {
BlocksHelper.setWithoutUpdate(world, POS, BlockRegistry.BUBBLE_CORAL.getDefaultState());
}
else if (NOISE.eval(x * 0.1, z * 0.1, 1) > 0) {
BlocksHelper.setWithoutUpdate(world, POS, BlockRegistry.END_LILY.getDefaultState().with(BlockEndLily.SHAPE, TripleShape.BOTTOM));
BlockPos up = POS.up();
while (up.getY() < waterLevel) {
BlocksHelper.setWithoutUpdate(world, up, BlockRegistry.END_LILY.getDefaultState().with(BlockEndLily.SHAPE, TripleShape.MIDDLE));
up = up.up();
}
BlocksHelper.setWithoutUpdate(world, up, BlockRegistry.END_LILY.getDefaultState().with(BlockEndLily.SHAPE, TripleShape.TOP));
}
}*/
private boolean canReplace(BlockState state) {
return state.getMaterial().isReplaceable()
|| state.isIn(EndTags.GEN_TERRAIN)
|| state.isOf(EndBlocks.ENDSTONE_DUST)
|| state.getMaterial().equals(Material.PLANT)
|| state.getMaterial().equals(Material.UNDERWATER_PLANT)
|| state.getMaterial().equals(Material.UNUSED_PLANT);
}
}

View file

@ -0,0 +1,73 @@
package ru.betterend.world.features.terrain;
import java.util.Random;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.StructureWorldAccess;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.feature.DefaultFeatureConfig;
import ru.betterend.noise.OpenSimplexNoise;
import ru.betterend.util.MHelper;
import ru.betterend.util.sdf.SDF;
import ru.betterend.util.sdf.operator.SDFCoordModify;
import ru.betterend.util.sdf.operator.SDFScale3D;
import ru.betterend.util.sdf.primitive.SDFSphere;
import ru.betterend.world.features.DefaultFeature;
public class OreLayerFeature extends DefaultFeature {
private static final SDFSphere SPHERE;
private static final SDFCoordModify NOISE;
private static final SDF FUNCTION;
private final BlockState state;
private final float radius;
private final int minY;
private final int maxY;
private OpenSimplexNoise noise;
public OreLayerFeature(BlockState state, float radius, int minY, int maxY) {
this.state = state;
this.radius = radius;
this.minY = minY;
this.maxY = maxY;
}
@Override
public boolean generate(StructureWorldAccess world, ChunkGenerator chunkGenerator, Random random, BlockPos pos, DefaultFeatureConfig config) {
float radius = this.radius * 0.5F;
int r = MHelper.floor(radius + 1);
int posX = MHelper.randRange(Math.max(r - 16, 0), Math.min(31 - r, 15), random) + pos.getX();
int posZ = MHelper.randRange(Math.max(r - 16, 0), Math.min(31 - r, 15), random) + pos.getZ();
int posY = MHelper.randRange(minY, maxY, random);
if (noise == null) {
noise = new OpenSimplexNoise(world.getSeed());
}
SPHERE.setRadius(radius).setBlock(state);
NOISE.setFunction((vec) -> {
double x = (vec.getX() + pos.getX()) * 0.1;
double z = (vec.getZ() + pos.getZ()) * 0.1;
double offset = noise.eval(x, z);
vec.set(vec.getX(), vec.getY() + (float) offset * 8, vec.getZ());
});
FUNCTION.fillRecursive(world, new BlockPos(posX, posY, posZ));
return true;
}
static {
SPHERE = new SDFSphere();
NOISE = new SDFCoordModify();
SDF body = SPHERE;
body = new SDFScale3D().setScale(1, 0.2F, 1).setSource(body);
body = NOISE.setSource(body);
body.setReplaceFunction((state) -> {
return state.isOf(Blocks.END_STONE);
});
FUNCTION = body;
}
}

View file

@ -0,0 +1,187 @@
package ru.betterend.world.features.terrain;
import java.util.Random;
import java.util.Set;
import com.google.common.collect.Sets;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.Material;
import net.minecraft.client.util.math.Vector3f;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockPos.Mutable;
import net.minecraft.world.Heightmap;
import net.minecraft.world.StructureWorldAccess;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.feature.DefaultFeatureConfig;
import ru.betterend.noise.OpenSimplexNoise;
import ru.betterend.registry.EndBlocks;
import ru.betterend.registry.EndStructures;
import ru.betterend.registry.EndTags;
import ru.betterend.util.BlocksHelper;
import ru.betterend.util.MHelper;
import ru.betterend.util.sdf.SDF;
import ru.betterend.util.sdf.operator.SDFDisplacement;
import ru.betterend.util.sdf.operator.SDFRotation;
import ru.betterend.util.sdf.operator.SDFScale3D;
import ru.betterend.util.sdf.operator.SDFSubtraction;
import ru.betterend.util.sdf.operator.SDFTranslate;
import ru.betterend.util.sdf.primitive.SDFHexPrism;
import ru.betterend.util.sdf.primitive.SDFSphere;
import ru.betterend.world.features.DefaultFeature;
public class RoundCaveFeature extends DefaultFeature {
private static final BlockState CAVE_AIR = Blocks.CAVE_AIR.getDefaultState();
@Override
public boolean generate(StructureWorldAccess world, ChunkGenerator chunkGenerator, Random random, BlockPos pos, DefaultFeatureConfig config) {
if (pos.getX() * pos.getX() + pos.getZ() * pos.getZ() <= 22500) {
return false;
}
int radius = MHelper.randRange(10, 30, random);
int bottom = BlocksHelper.upRay(world, new BlockPos(pos.getX(), 0, pos.getZ()), 32) + radius + 5;
int top = world.getTopY(Heightmap.Type.WORLD_SURFACE, pos.getX(), pos.getZ());
Mutable bpos = new Mutable();
bpos.setX(pos.getX());
bpos.setZ(pos.getZ());
bpos.setY(top);
while (top > bottom && !world.getBlockState(bpos).isIn(EndTags.GEN_TERRAIN)) {
bpos.setY(--top);
}
top -= radius * 1.3F + 5;
if (top <= bottom) {
return false;
}
pos = new BlockPos(pos.getX(), MHelper.randRange(bottom, top, random), pos.getZ());
OpenSimplexNoise noise = new OpenSimplexNoise(MHelper.getSeed(534, pos.getX(), pos.getZ()));
int x1 = pos.getX() - radius - 5;
int z1 = pos.getZ() - radius - 5;
int x2 = pos.getX() + radius + 5;
int z2 = pos.getZ() + radius + 5;
int y1 = MHelper.floor(pos.getY() - (radius + 5) / 1.6);
int y2 = MHelper.floor(pos.getY() + (radius + 5) / 1.6);
double hr = radius * 0.75;
double nr = radius * 0.25;
Set<BlockPos> bushes = Sets.newHashSet();
BlockState terrain = EndBlocks.CAVE_MOSS.getDefaultState();
for (int x = x1; x <= x2; x++) {
int xsq = x - pos.getX();
xsq *= xsq;
bpos.setX(x);
for (int z = z1; z <= z2; z++) {
int zsq = z - pos.getZ();
zsq *= zsq;
bpos.setZ(z);
for (int y = y1; y <= y2; y++) {
int ysq = y - pos.getY();
ysq *= 1.6;
ysq *= ysq;
bpos.setY(y);
double r = noise.eval(x * 0.1, y * 0.1, z * 0.1) * nr + hr;
double r2 = r + 5;
double dist = xsq + ysq + zsq;
if (dist < r * r) {
BlockState state = world.getBlockState(bpos);
if (isReplaceable(state)) {
BlocksHelper.setWithoutUpdate(world, bpos, CAVE_AIR);
while (state.getMaterial().equals(Material.LEAVES)) {
BlocksHelper.setWithoutUpdate(world, bpos, CAVE_AIR);
bpos.setY(bpos.getY() + 1);
state = world.getBlockState(bpos);
}
bpos.setY(y - 1);
while (state.getMaterial().equals(Material.LEAVES)) {
BlocksHelper.setWithoutUpdate(world, bpos, CAVE_AIR);
bpos.setY(bpos.getY() - 1);
state = world.getBlockState(bpos);
}
}
bpos.setY(y - 1);
if (world.getBlockState(bpos).isIn(EndTags.GEN_TERRAIN)) {
BlocksHelper.setWithoutUpdate(world, bpos, terrain);
}
}
else if (dist < r2 * r2) {
BlockState state = world.getBlockState(bpos);
if (!state.getFluidState().isEmpty()) {
BlocksHelper.setWithoutUpdate(world, bpos, Blocks.END_STONE.getDefaultState());
}
else if (world.getBlockState(bpos).isIn(EndTags.GEN_TERRAIN)) {
if (world.isAir(bpos.down())) {
int h = BlocksHelper.downRay(world, bpos.down(), 64);
if (h > 6 && h < 32 && world.getBlockState(bpos.down(h + 3)).isIn(EndTags.GEN_TERRAIN)) {
bushes.add(bpos.down());
}
}
else if (world.isAir(bpos.up())) {
int h = BlocksHelper.upRay(world, bpos.up(), 64);
if (h > 6 && h < 32 && world.getBlockState(bpos.up(h + 3)).isIn(EndTags.GEN_TERRAIN)) {
bushes.add(bpos.up());
}
}
}
}
}
}
}
bushes.forEach((cpos) -> {
if (random.nextInt(32) == 0) {
generateBush(world, random, cpos);
}
});
if (random.nextBoolean() && world.getBiome(pos).getGenerationSettings().hasStructureFeature(EndStructures.MOUNTAIN.getStructure())) {
pos = pos.add(random.nextGaussian() * 5, random.nextGaussian() * 5, random.nextGaussian() * 5);
BlockPos down = pos.down(BlocksHelper.downRay(world, pos, 64) + 2);
if (isReplaceable(world.getBlockState(down))) {
SDF prism = new SDFHexPrism().setHeight(radius * MHelper.randRange(0.6F, 0.75F, random)).setRadius(MHelper.randRange(1.7F, 3F, random)).setBlock(EndBlocks.AURORA_CRYSTAL);
float angleY = MHelper.randRange(0, MHelper.PI2, random);
float vx = (float) Math.sin(angleY);
float vz = (float) Math.sin(angleY);
prism = new SDFRotation().setRotation(new Vector3f(vx, 0, vz), random.nextFloat()).setSource(prism);
prism.setReplaceFunction((state) -> {
return state.getMaterial().isReplaceable()
|| state.isIn(EndTags.GEN_TERRAIN)
|| state.getMaterial().equals(Material.PLANT)
|| state.getMaterial().equals(Material.LEAVES);
});
prism.fillRecursive(world, pos);
BlocksHelper.setWithoutUpdate(world, pos, EndBlocks.AURORA_CRYSTAL);
}
}
BlocksHelper.fixBlocks(world, new BlockPos(x1, y1, z1), new BlockPos(x2, y2, z2));
return true;
}
private boolean isReplaceable(BlockState state) {
return state.isIn(EndTags.GEN_TERRAIN)
|| state.getMaterial().isReplaceable()
|| state.getMaterial().equals(Material.PLANT)
|| state.getMaterial().equals(Material.LEAVES);
}
private void generateBush(StructureWorldAccess world, Random random, BlockPos blockPos) {
float radius = MHelper.randRange(1.0F, 3.2F, random);
OpenSimplexNoise noise = new OpenSimplexNoise(random.nextInt());
SDF sphere = new SDFSphere().setRadius(radius).setBlock(EndBlocks.CAVE_BUSH);
sphere = new SDFScale3D().setScale(MHelper.randRange(0.8F, 1.2F, random), MHelper.randRange(0.8F, 1.2F, random), MHelper.randRange(0.8F, 1.2F, random)).setSource(sphere);
sphere = new SDFDisplacement().setFunction((vec) -> { return (float) noise.eval(vec.getX() * 0.2, vec.getY() * 0.2, vec.getZ() * 0.2) * 3; }).setSource(sphere);
sphere = new SDFDisplacement().setFunction((vec) -> { return random.nextFloat() * 3F - 1.5F; }).setSource(sphere);
sphere = new SDFSubtraction().setSourceA(sphere).setSourceB(new SDFTranslate().setTranslate(0, -radius, 0).setSource(sphere));
sphere.fillRecursive(world, blockPos);
BlocksHelper.setWithoutUpdate(world, blockPos, EndBlocks.CAVE_BUSH);
}
}