| 1 | package net.minecraft.world.level.levelgen.carver;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import java.util.function.Function;
|
| 6 | import net.minecraft.core.Holder;
|
| 7 | import net.minecraft.core.HolderSet;
|
| 8 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 9 | import net.minecraft.core.registries.Registries;
|
| 10 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import net.minecraft.util.RandomSource;
|
| 13 | import net.minecraft.world.level.ChunkPos;
|
| 14 | import net.minecraft.world.level.chunk.CarverOutput;
|
| 15 | import net.minecraft.world.level.levelgen.WorldGenerationContext;
|
| 16 |
|
| 17 | public interface WorldCarver {
|
| 18 | Codec<WorldCarver> DIRECT_CODEC = BuiltInRegistries.CARVER_TYPE.byNameCodec().dispatch(WorldCarver::codec, Function.identity());
|
| 19 | Codec<Holder<WorldCarver>> CODEC = RegistryCodecs.holder(Registries.CARVER, DIRECT_CODEC);
|
| 20 | Codec<HolderSet<WorldCarver>> LIST_CODEC = RegistryCodecs.holderSet(Registries.CARVER, DIRECT_CODEC);
|
| 21 |
|
| 22 | default int getRange() {
|
| 23 | return 4;
|
| 24 | }
|
| 25 |
|
| 26 | static void carveEllipsoid(
|
| 27 | final ChunkPos chunkPos,
|
| 28 | final double x,
|
| 29 | final double y,
|
| 30 | final double z,
|
| 31 | final double horizontalRadius,
|
| 32 | final double verticalRadius,
|
| 33 | final CarverOutput output,
|
| 34 | final WorldCarver.CarveSkipChecker skipChecker
|
| 35 | ) {
|
| 36 | double centerX = chunkPos.getMiddleBlockX();
|
| 37 | double centerZ = chunkPos.getMiddleBlockZ();
|
| 38 | double maxDelta = 16.0 + horizontalRadius * 2.0;
|
| 39 | if (!(Math.abs(x - centerX) > maxDelta) && !(Math.abs(z - centerZ) > maxDelta)) {
|
| 40 | int chunkMinX = chunkPos.getMinBlockX();
|
| 41 | int chunkMinZ = chunkPos.getMinBlockZ();
|
| 42 | int minXIndex = Math.max(Mth.floor(x - horizontalRadius) - chunkMinX - 1, 0);
|
| 43 | int maxXIndex = Math.min(Mth.floor(x + horizontalRadius) - chunkMinX, 15);
|
| 44 | int minY = Math.max(Mth.floor(y - verticalRadius) - 1, output.minY());
|
| 45 | int maxY = Math.min(Mth.floor(y + verticalRadius) + 1, output.maxY());
|
| 46 | int minZIndex = Math.max(Mth.floor(z - horizontalRadius) - chunkMinZ - 1, 0);
|
| 47 | int maxZIndex = Math.min(Mth.floor(z + horizontalRadius) - chunkMinZ, 15);
|
| 48 |
|
| 49 | for (int xIndex = minXIndex; xIndex <= maxXIndex; xIndex++) {
|
| 50 | int worldX = chunkPos.getBlockX(xIndex);
|
| 51 | double xd = (worldX + 0.5 - x) / horizontalRadius;
|
| 52 |
|
| 53 | for (int zIndex = minZIndex; zIndex <= maxZIndex; zIndex++) {
|
| 54 | int worldZ = chunkPos.getBlockZ(zIndex);
|
| 55 | double zd = (worldZ + 0.5 - z) / horizontalRadius;
|
| 56 | if (!(xd * xd + zd * zd >= 1.0)) {
|
| 57 | for (int worldY = maxY; worldY > minY; worldY--) {
|
| 58 | double yd = (worldY - 0.5 - y) / verticalRadius;
|
| 59 | if (!skipChecker.shouldSkip(xd, yd, zd, worldY)) {
|
| 60 | output.carve(xIndex, worldY, zIndex);
|
| 61 | }
|
| 62 | }
|
| 63 | }
|
| 64 | }
|
| 65 | }
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | boolean carve(WorldGenerationContext context, RandomSource random, ChunkPos chunkPos, ChunkPos sourceChunkPos, CarverOutput output);
|
| 70 |
|
| 71 | boolean isStartChunk(RandomSource random);
|
| 72 |
|
| 73 | MapCodec<? extends WorldCarver> codec();
|
| 74 |
|
| 75 | static boolean canReach(final ChunkPos chunkPos, final double x, final double z, final int currentStep, final int totalSteps, final float thickness) {
|
| 76 | double xMid = chunkPos.getMiddleBlockX();
|
| 77 | double zMid = chunkPos.getMiddleBlockZ();
|
| 78 | double xd = x - xMid;
|
| 79 | double zd = z - zMid;
|
| 80 | double remaining = totalSteps - currentStep;
|
| 81 | double rr = thickness + 2.0F + 16.0F;
|
| 82 | return xd * xd + zd * zd - remaining * remaining <= rr * rr;
|
| 83 | }
|
| 84 |
|
| 85 | interface CarveSkipChecker {
|
| 86 | boolean shouldSkip(double xd, double yd, double zd, int y);
|
| 87 | }
|
| 88 | }
|
| 89 |
|