| 1 | package net.minecraft.world.level.levelgen.carver;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import net.minecraft.util.ExtraCodecs;
|
| 7 | import net.minecraft.util.Mth;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.util.valueproviders.FloatProvider;
|
| 10 | import net.minecraft.util.valueproviders.FloatProviders;
|
| 11 | import net.minecraft.world.level.ChunkPos;
|
| 12 | import net.minecraft.world.level.chunk.CarverOutput;
|
| 13 | import net.minecraft.world.level.levelgen.WorldGenerationContext;
|
| 14 | import net.minecraft.world.level.levelgen.heightproviders.HeightProvider;
|
| 15 |
|
| 16 | public record CanyonWorldCarver(float probability, HeightProvider y, FloatProvider verticalRotation, CanyonWorldCarver.Shape shape) implements WorldCarver {
|
| 17 | public static final MapCodec<CanyonWorldCarver> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 18 | i -> i.group(
|
| 19 | Codec.floatRange(0.0F, 1.0F).fieldOf("probability").forGetter(c -> c.probability),
|
| 20 | HeightProvider.CODEC.fieldOf("y").forGetter(c -> c.y),
|
| 21 | FloatProviders.CODEC.fieldOf("vertical_rotation").forGetter(c -> c.verticalRotation),
|
| 22 | CanyonWorldCarver.Shape.CODEC.fieldOf("shape").forGetter(c -> c.shape)
|
| 23 | )
|
| 24 | .apply(i, CanyonWorldCarver::new)
|
| 25 | );
|
| 26 |
|
| 27 | @Override
|
| 28 | public boolean isStartChunk(final RandomSource random) {
|
| 29 | return random.nextFloat() <= this.probability;
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | public boolean carve(
|
| 34 | final WorldGenerationContext context, final RandomSource random, final ChunkPos chunkPos, final ChunkPos sourceChunkPos, final CarverOutput output
|
| 35 | ) {
|
| 36 | int maxDistance = (this.getRange() * 2 - 1) * 16;
|
| 37 | double x = sourceChunkPos.getBlockX(random.nextInt(16));
|
| 38 | int y = this.y.sample(random, context);
|
| 39 | double z = sourceChunkPos.getBlockZ(random.nextInt(16));
|
| 40 | float horizontalRotation = random.nextFloat() * (float) (Math.PI * 2);
|
| 41 | float verticalRotation = this.verticalRotation.sample(random);
|
| 42 | double yScale = this.shape.yScale().sample(random);
|
| 43 | float thickness = this.shape.thickness().sample(random);
|
| 44 | int distance = (int)(maxDistance * this.shape.distanceFactor().sample(random));
|
| 45 | int initialStep = 0;
|
| 46 | this.doCarve(context, chunkPos, random.nextLong(), x, y, z, thickness, horizontalRotation, verticalRotation, 0, distance, yScale, output);
|
| 47 | return true;
|
| 48 | }
|
| 49 |
|
| 50 | private void doCarve(
|
| 51 | final WorldGenerationContext context,
|
| 52 | final ChunkPos chunkPos,
|
| 53 | final long tunnelSeed,
|
| 54 | double x,
|
| 55 | double y,
|
| 56 | double z,
|
| 57 | final float thickness,
|
| 58 | float horizontalRotation,
|
| 59 | float verticalRotation,
|
| 60 | final int step,
|
| 61 | final int distance,
|
| 62 | final double yScale,
|
| 63 | final CarverOutput output
|
| 64 | ) {
|
| 65 | RandomSource random = RandomSource.createThreadLocalInstance(tunnelSeed);
|
| 66 | float[] widthFactorPerHeight = this.initWidthFactors(context, random);
|
| 67 | float yRota = 0.0F;
|
| 68 | float xRota = 0.0F;
|
| 69 |
|
| 70 | for (int currentStep = step; currentStep < distance; currentStep++) {
|
| 71 | double horizontalRadius = 1.5 + Mth.sin(currentStep * (float) Math.PI / distance) * thickness;
|
| 72 | double verticalRadius = horizontalRadius * yScale;
|
| 73 | horizontalRadius *= this.shape.horizontalRadiusFactor().sample(random);
|
| 74 | verticalRadius = this.updateVerticalRadius(random, verticalRadius, distance, currentStep);
|
| 75 | float xc = Mth.cos(verticalRotation);
|
| 76 | float xs = Mth.sin(verticalRotation);
|
| 77 | x += Mth.cos(horizontalRotation) * xc;
|
| 78 | y += xs;
|
| 79 | z += Mth.sin(horizontalRotation) * xc;
|
| 80 | verticalRotation *= 0.7F;
|
| 81 | verticalRotation += xRota * 0.05F;
|
| 82 | horizontalRotation += yRota * 0.05F;
|
| 83 | xRota *= 0.8F;
|
| 84 | yRota *= 0.5F;
|
| 85 | xRota += (random.nextFloat() - random.nextFloat()) * random.nextFloat() * 2.0F;
|
| 86 | yRota += (random.nextFloat() - random.nextFloat()) * random.nextFloat() * 4.0F;
|
| 87 | if (random.nextInt(4) != 0) {
|
| 88 | if (!WorldCarver.canReach(chunkPos, x, z, currentStep, distance, thickness)) {
|
| 89 | return;
|
| 90 | }
|
| 91 |
|
| 92 | WorldCarver.carveEllipsoid(
|
| 93 | chunkPos, x, y, z, horizontalRadius, verticalRadius, output, (xd, yd, zd, y1) -> this.shouldSkip(context, widthFactorPerHeight, xd, yd, zd, y1)
|
| 94 | );
|
| 95 | }
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | private float[] initWidthFactors(final WorldGenerationContext context, final RandomSource random) {
|
| 100 | int depth = context.getGenDepth();
|
| 101 | float[] widthFactorPerHeight = new float[depth];
|
| 102 | float widthFactor = 1.0F;
|
| 103 |
|
| 104 | for (int yIndex = 0; yIndex < depth; yIndex++) {
|
| 105 | if (yIndex == 0 || random.nextInt(this.shape.widthSmoothness()) == 0) {
|
| 106 | widthFactor = 1.0F + random.nextFloat() * random.nextFloat();
|
| 107 | }
|
| 108 |
|
| 109 | widthFactorPerHeight[yIndex] = widthFactor * widthFactor;
|
| 110 | }
|
| 111 |
|
| 112 | return widthFactorPerHeight;
|
| 113 | }
|
| 114 |
|
| 115 | private double updateVerticalRadius(final RandomSource random, final double verticalRadius, final float distance, final float currentStep) {
|
| 116 | float verticalMultiplier = 1.0F - Mth.abs(0.5F - currentStep / distance) * 2.0F;
|
| 117 | float factor = this.shape.verticalRadiusDefaultFactor() + this.shape.verticalRadiusCenterFactor() * verticalMultiplier;
|
| 118 | return factor * verticalRadius * Mth.randomBetween(random, 0.75F, 1.0F);
|
| 119 | }
|
| 120 |
|
| 121 | private boolean shouldSkip(
|
| 122 | final WorldGenerationContext context, final float[] widthFactorPerHeight, final double xd, final double yd, final double zd, final int y
|
| 123 | ) {
|
| 124 | int yIndex = y - context.getMinGenY();
|
| 125 | return (xd * xd + zd * zd) * widthFactorPerHeight[yIndex - 1] + yd * yd / 6.0 >= 1.0;
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | public MapCodec<CanyonWorldCarver> codec() {
|
| 130 | return MAP_CODEC;
|
| 131 | }
|
| 132 |
|
| 133 | public record Shape(
|
| 134 | FloatProvider distanceFactor,
|
| 135 | FloatProvider thickness,
|
| 136 | int widthSmoothness,
|
| 137 | FloatProvider horizontalRadiusFactor,
|
| 138 | float verticalRadiusDefaultFactor,
|
| 139 | float verticalRadiusCenterFactor,
|
| 140 | FloatProvider yScale
|
| 141 | ) {
|
| 142 | public static final Codec<CanyonWorldCarver.Shape> CODEC = RecordCodecBuilder.create(
|
| 143 | i -> i.group(
|
| 144 | FloatProviders.CODEC.fieldOf("distance_factor").forGetter(c -> c.distanceFactor),
|
| 145 | FloatProviders.CODEC.fieldOf("thickness").forGetter(c -> c.thickness),
|
| 146 | ExtraCodecs.POSITIVE_INT.fieldOf("width_smoothness").forGetter(c -> c.widthSmoothness),
|
| 147 | FloatProviders.CODEC.fieldOf("horizontal_radius_factor").forGetter(c -> c.horizontalRadiusFactor),
|
| 148 | Codec.FLOAT.fieldOf("vertical_radius_default_factor").forGetter(c -> c.verticalRadiusDefaultFactor),
|
| 149 | Codec.FLOAT.fieldOf("vertical_radius_center_factor").forGetter(c -> c.verticalRadiusCenterFactor),
|
| 150 | FloatProviders.CODEC.fieldOf("y_scale").forGetter(c -> c.yScale)
|
| 151 | )
|
| 152 | .apply(i, CanyonWorldCarver.Shape::new)
|
| 153 | );
|
| 154 | }
|
| 155 | }
|
| 156 |
|