| 1 | package net.minecraft.world.level.levelgen.densityfunction.generator;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import net.minecraft.util.Interval;
|
| 5 | import net.minecraft.util.Mth;
|
| 6 | import net.minecraft.util.RandomSource;
|
| 7 | import net.minecraft.world.level.levelgen.densityfunction.DensityBuffer;
|
| 8 | import net.minecraft.world.level.levelgen.densityfunction.DensityFunction;
|
| 9 | import net.minecraft.world.level.levelgen.densityfunction.DensitySampler;
|
| 10 | import net.minecraft.world.level.levelgen.densityfunction.DensityVolume;
|
| 11 | import net.minecraft.world.level.levelgen.densityfunction.DfRewriteRule;
|
| 12 | import net.minecraft.world.level.levelgen.densityfunction.SamplerContext;
|
| 13 | import net.minecraft.world.level.levelgen.synth.SimplexNoise;
|
| 14 |
|
| 15 | public record EndIslandFunction() implements DensityFunction {
|
| 16 | public static final MapCodec<EndIslandFunction> CODEC = MapCodec.unit(new EndIslandFunction());
|
| 17 | private static final float ISLAND_THRESHOLD = -0.9F;
|
| 18 |
|
| 19 | private static float getHeightValue(final SimplexNoise islandNoise, final int sectionX, final int sectionZ) {
|
| 20 | int chunkX = sectionX / 2;
|
| 21 | int chunkZ = sectionZ / 2;
|
| 22 | int subSectionX = sectionX % 2;
|
| 23 | int subSectionZ = sectionZ % 2;
|
| 24 | float doffs = -100.0F;
|
| 25 |
|
| 26 | for (int xo = -12; xo <= 12; xo++) {
|
| 27 | for (int zo = -12; zo <= 12; zo++) {
|
| 28 | long totalChunkX = chunkX + xo;
|
| 29 | long totalChunkZ = chunkZ + zo;
|
| 30 | if (totalChunkX * totalChunkX + totalChunkZ * totalChunkZ > 4096L && islandNoise.get(totalChunkX, totalChunkZ) < -0.9F) {
|
| 31 | float islandSize = (Mth.abs((float)totalChunkX) * 3439.0F + Mth.abs((float)totalChunkZ) * 147.0F) % 13.0F + 9.0F;
|
| 32 | float xd = subSectionX - xo * 2;
|
| 33 | float zd = subSectionZ - zo * 2;
|
| 34 | float newDoffs = 100.0F - Mth.sqrt(xd * xd + zd * zd) * islandSize;
|
| 35 | newDoffs = Mth.clamp(newDoffs, -100.0F, 80.0F);
|
| 36 | doffs = Math.max(doffs, newDoffs);
|
| 37 | }
|
| 38 | }
|
| 39 | }
|
| 40 |
|
| 41 | return doffs;
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public DensitySampler compileSampler(final DensityFunction.CompileContext context) {
|
| 46 | RandomSource islandRandom = context.createEndIslandRandom();
|
| 47 | islandRandom.consumeCount(17292);
|
| 48 | SimplexNoise islandNoise = new SimplexNoise(islandRandom, true);
|
| 49 | return new EndIslandFunction.Sampler(islandNoise);
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public Interval range() {
|
| 54 | return Interval.of(-0.84375F, 0.5625F);
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public @DensityFunction.Axes int domainAxes() {
|
| 59 | return 5;
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public MapCodec<EndIslandFunction> codec() {
|
| 64 | return CODEC;
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public DensityFunction rewriteChildren(final DfRewriteRule rule) {
|
| 69 | return this;
|
| 70 | }
|
| 71 |
|
| 72 | private record Sampler(SimplexNoise islandNoise) implements DensitySampler {
|
| 73 | @Override
|
| 74 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 75 | for (int z = 0; z < volume.sizeZ(); z++) {
|
| 76 | int blockZ = volume.blockZ(z);
|
| 77 |
|
| 78 | for (int x = 0; x < volume.sizeX(); x++) {
|
| 79 | int blockX = volume.blockX(x);
|
| 80 | float value = this.sampleValue(context, blockX, 0, blockZ);
|
| 81 | int index = volume.indexUnchecked(x, 0, z);
|
| 82 | outputBuffer.setRange(index, volume.sizeY(), value);
|
| 83 | }
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 89 | return (EndIslandFunction.getHeightValue(this.islandNoise, blockX / 8, blockZ / 8) - 8.0F) / 128.0F;
|
| 90 | }
|
| 91 | }
|
| 92 | }
|
| 93 |
|