| 1 | package net.minecraft.world.level.levelgen.densityfunction;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Either;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import it.unimi.dsi.fastutil.floats.FloatList;
|
| 7 | import java.util.List;
|
| 8 | import java.util.function.Function;
|
| 9 | import net.minecraft.core.Direction;
|
| 10 | import net.minecraft.core.Holder;
|
| 11 | import net.minecraft.core.Registry;
|
| 12 | import net.minecraft.core.Vec3i;
|
| 13 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 14 | import net.minecraft.util.CubicSpline;
|
| 15 | import net.minecraft.util.Interval;
|
| 16 | import net.minecraft.world.level.levelgen.densityfunction.generator.ConstantFunction;
|
| 17 | import net.minecraft.world.level.levelgen.densityfunction.generator.DistanceToPointFunction;
|
| 18 | import net.minecraft.world.level.levelgen.densityfunction.generator.EndIslandFunction;
|
| 19 | import net.minecraft.world.level.levelgen.densityfunction.generator.GradientFunction;
|
| 20 | import net.minecraft.world.level.levelgen.densityfunction.generator.NoiseFunction;
|
| 21 | import net.minecraft.world.level.levelgen.densityfunction.generator.ShiftNoiseFunction;
|
| 22 | import net.minecraft.world.level.levelgen.densityfunction.generator.SimpleDensityFunction;
|
| 23 | import net.minecraft.world.level.levelgen.densityfunction.op.BinaryFunction;
|
| 24 | import net.minecraft.world.level.levelgen.densityfunction.op.BlendDensityFunction;
|
| 25 | import net.minecraft.world.level.levelgen.densityfunction.op.CacheFunction;
|
| 26 | import net.minecraft.world.level.levelgen.densityfunction.op.ClampFunction;
|
| 27 | import net.minecraft.world.level.levelgen.densityfunction.op.FindTopSurfaceFunction;
|
| 28 | import net.minecraft.world.level.levelgen.densityfunction.op.InterpolatedFunction;
|
| 29 | import net.minecraft.world.level.levelgen.densityfunction.op.IntervalSelectFunction;
|
| 30 | import net.minecraft.world.level.levelgen.densityfunction.op.LerpFunction;
|
| 31 | import net.minecraft.world.level.levelgen.densityfunction.op.PowFunction;
|
| 32 | import net.minecraft.world.level.levelgen.densityfunction.op.RangeChoiceFunction;
|
| 33 | import net.minecraft.world.level.levelgen.densityfunction.op.RoundFunction;
|
| 34 | import net.minecraft.world.level.levelgen.densityfunction.op.SliceFunction;
|
| 35 | import net.minecraft.world.level.levelgen.densityfunction.op.SplineFunction;
|
| 36 | import net.minecraft.world.level.levelgen.densityfunction.op.UnaryFunction;
|
| 37 | import net.minecraft.world.level.levelgen.synth.BlendedNoise;
|
| 38 | import net.minecraft.world.level.levelgen.synth.NormalNoise;
|
| 39 |
|
| 40 | public final class DensityFunctions {
|
| 41 | public static final float MAX_REASONABLE_NOISE_VALUE = 1000000.0F;
|
| 42 | public static final Codec<Float> NOISE_VALUE_CODEC = Codec.floatRange(-1000000.0F, 1000000.0F);
|
| 43 | private static final Codec<DensityFunction> FULL_DIRECT_CODEC = BuiltInRegistries.DENSITY_FUNCTION_TYPE
|
| 44 | .byNameCodec()
|
| 45 | .dispatch(DensityFunction::codec, Function.identity());
|
| 46 | public static final Codec<DensityFunction> DIRECT_CODEC = Codec.either(NOISE_VALUE_CODEC, FULL_DIRECT_CODEC)
|
| 47 | .xmap(either -> either.map(DensityFunctions::constant, Function.identity()), function -> {
|
| 48 | return switch (function) {
|
| 49 | case ConstantFunction(float value) -> Either.left(value);
|
| 50 | default -> Either.right(function);
|
| 51 | };
|
| 52 | });
|
| 53 | private static final ConstantFunction ZERO = new ConstantFunction(0.0F);
|
| 54 |
|
| 55 | public static MapCodec<? extends DensityFunction> bootstrap(final Registry<MapCodec<? extends DensityFunction>> registry) {
|
| 56 | MapCodec<? extends DensityFunction> constant = register(registry, "constant", ConstantFunction.CODEC);
|
| 57 |
|
| 58 | for (SimpleDensityFunction simple : SimpleDensityFunction.values()) {
|
| 59 | register(registry, simple.id(), simple.codec());
|
| 60 | }
|
| 61 |
|
| 62 | register(registry, "noise", NoiseFunction.CODEC);
|
| 63 | register(registry, "end_outer_islands", EndIslandFunction.CODEC);
|
| 64 | register(registry, "distance_to_point", DistanceToPointFunction.CODEC);
|
| 65 | register(registry, "gradient", GradientFunction.CODEC);
|
| 66 | register(registry, "shift_a", ShiftNoiseFunction.ShiftA.CODEC);
|
| 67 | register(registry, "shift_b", ShiftNoiseFunction.ShiftB.CODEC);
|
| 68 | register(registry, "shift", ShiftNoiseFunction.Shift.CODEC);
|
| 69 |
|
| 70 | for (UnaryFunction.Type unary : UnaryFunction.Type.values()) {
|
| 71 | register(registry, unary.id, unary.codec);
|
| 72 | }
|
| 73 |
|
| 74 | for (RoundFunction.Type type : RoundFunction.Type.values()) {
|
| 75 | register(registry, type.id, type.codec);
|
| 76 | }
|
| 77 |
|
| 78 | for (BinaryFunction.Type binary : BinaryFunction.Type.values()) {
|
| 79 | register(registry, binary.id, binary.codec);
|
| 80 | }
|
| 81 |
|
| 82 | register(registry, "pow", PowFunction.CODEC);
|
| 83 | register(registry, "spline", SplineFunction.CODEC);
|
| 84 | register(registry, "lerp", LerpFunction.CODEC);
|
| 85 | register(registry, "clamp", ClampFunction.CODEC);
|
| 86 | register(registry, "range_choice", RangeChoiceFunction.CODEC);
|
| 87 | register(registry, "interval_select", IntervalSelectFunction.CODEC);
|
| 88 | register(registry, "cache", CacheFunction.CODEC);
|
| 89 | register(registry, "blend_density", BlendDensityFunction.CODEC);
|
| 90 | register(registry, "interpolated", InterpolatedFunction.CODEC);
|
| 91 | register(registry, "slice", SliceFunction.CODEC);
|
| 92 | register(registry, "find_top_surface", FindTopSurfaceFunction.CODEC);
|
| 93 | register(registry, "old_blended_noise", BlendedNoise.CODEC);
|
| 94 | return constant;
|
| 95 | }
|
| 96 |
|
| 97 | private static MapCodec<? extends DensityFunction> register(
|
| 98 | final Registry<MapCodec<? extends DensityFunction>> registry, final String name, final MapCodec<? extends DensityFunction> codec
|
| 99 | ) {
|
| 100 | return Registry.register(registry, name, codec);
|
| 101 | }
|
| 102 |
|
| 103 | private DensityFunctions() {
|
| 104 | }
|
| 105 |
|
| 106 | public static DensityFunction zero() {
|
| 107 | return ZERO;
|
| 108 | }
|
| 109 |
|
| 110 | public static DensityFunction constant(final float value) {
|
| 111 | return new ConstantFunction(value);
|
| 112 | }
|
| 113 |
|
| 114 | public static DensityFunction abs(final DensityFunction input) {
|
| 115 | return new UnaryFunction(UnaryFunction.Type.ABS, input);
|
| 116 | }
|
| 117 |
|
| 118 | public static DensityFunction square(final DensityFunction input) {
|
| 119 | return new UnaryFunction(UnaryFunction.Type.SQUARE, input);
|
| 120 | }
|
| 121 |
|
| 122 | public static DensityFunction cube(final DensityFunction input) {
|
| 123 | return new UnaryFunction(UnaryFunction.Type.CUBE, input);
|
| 124 | }
|
| 125 |
|
| 126 | public static DensityFunction sqrt(final DensityFunction input) {
|
| 127 | return new UnaryFunction(UnaryFunction.Type.SQRT, input);
|
| 128 | }
|
| 129 |
|
| 130 | public static DensityFunction halfNegative(final DensityFunction input) {
|
| 131 | return new UnaryFunction(UnaryFunction.Type.HALF_NEGATIVE, input);
|
| 132 | }
|
| 133 |
|
| 134 | public static DensityFunction quarterNegative(final DensityFunction input) {
|
| 135 | return new UnaryFunction(UnaryFunction.Type.QUARTER_NEGATIVE, input);
|
| 136 | }
|
| 137 |
|
| 138 | public static DensityFunction reciprocal(final DensityFunction input) {
|
| 139 | return new UnaryFunction(UnaryFunction.Type.RECIPROCAL, input);
|
| 140 | }
|
| 141 |
|
| 142 | public static DensityFunction negate(final DensityFunction input) {
|
| 143 | return new UnaryFunction(UnaryFunction.Type.NEGATE, input);
|
| 144 | }
|
| 145 |
|
| 146 | public static DensityFunction squeeze(final DensityFunction input) {
|
| 147 | return new UnaryFunction(UnaryFunction.Type.SQUEEZE, input);
|
| 148 | }
|
| 149 |
|
| 150 | public static DensityFunction log(final DensityFunction input) {
|
| 151 | return new UnaryFunction(UnaryFunction.Type.LOG, input);
|
| 152 | }
|
| 153 |
|
| 154 | public static DensityFunction sign(final DensityFunction input) {
|
| 155 | return new UnaryFunction(UnaryFunction.Type.SIGN, input);
|
| 156 | }
|
| 157 |
|
| 158 | public static DensityFunction interpolated(final DensityFunction function, final int cellSizeXz, final int cellSizeY) {
|
| 159 | return new InterpolatedFunction(function, cellSizeXz, cellSizeY);
|
| 160 | }
|
| 161 |
|
| 162 | public static DensityFunction cache(final DensityFunction function) {
|
| 163 | return new CacheFunction(function);
|
| 164 | }
|
| 165 |
|
| 166 | public static DensityFunction mappedNoise(
|
| 167 | final Holder<NormalNoise> noiseData, @Deprecated final double xzScale, final double yScale, final float minTarget, final float maxTarget
|
| 168 | ) {
|
| 169 | DensityFunction noise = new NoiseFunction(noiseData, xzScale, yScale, zero(), zero(), zero());
|
| 170 | return remap(noise, -1.0F, 1.0F, minTarget, maxTarget);
|
| 171 | }
|
| 172 |
|
| 173 | public static DensityFunction mappedNoise(final Holder<NormalNoise> noiseData, final double yScale, final float minTarget, final float maxTarget) {
|
| 174 | return mappedNoise(noiseData, 1.0, yScale, minTarget, maxTarget);
|
| 175 | }
|
| 176 |
|
| 177 | public static DensityFunction mappedNoise(final Holder<NormalNoise> noiseData, final float minTarget, final float maxTarget) {
|
| 178 | return mappedNoise(noiseData, 1.0, 1.0, minTarget, maxTarget);
|
| 179 | }
|
| 180 |
|
| 181 | public static DensityFunction shiftedNoise2d(
|
| 182 | final DensityFunction shiftX, final DensityFunction shiftZ, final double xzScale, final Holder<NormalNoise> noiseData
|
| 183 | ) {
|
| 184 | return new NoiseFunction(noiseData, xzScale, 0.0, shiftX, zero(), shiftZ);
|
| 185 | }
|
| 186 |
|
| 187 | public static DensityFunction noise(final Holder<NormalNoise> noiseData) {
|
| 188 | return noise(noiseData, 1.0, 1.0);
|
| 189 | }
|
| 190 |
|
| 191 | public static DensityFunction noise(final Holder<NormalNoise> noiseData, final double xzScale, final double yScale) {
|
| 192 | return new NoiseFunction(noiseData, xzScale, yScale, zero(), zero(), zero());
|
| 193 | }
|
| 194 |
|
| 195 | public static DensityFunction noise(final Holder<NormalNoise> noiseData, final double yScale) {
|
| 196 | return noise(noiseData, 1.0, yScale);
|
| 197 | }
|
| 198 |
|
| 199 | public static DensityFunction rangeChoice(
|
| 200 | final DensityFunction input, final float minInclusive, final float maxExclusive, final DensityFunction whenInRange, final DensityFunction whenOutOfRange
|
| 201 | ) {
|
| 202 | return new RangeChoiceFunction(input, minInclusive, maxExclusive, whenInRange, whenOutOfRange);
|
| 203 | }
|
| 204 |
|
| 205 | public static DensityFunction intervalSelect(final DensityFunction input, final FloatList thresholds, final List<DensityFunction> functions) {
|
| 206 | return new IntervalSelectFunction(input, thresholds, functions);
|
| 207 | }
|
| 208 |
|
| 209 | public static DensityFunction shiftA(final Holder<NormalNoise> noiseData) {
|
| 210 | return new ShiftNoiseFunction.ShiftA(noiseData);
|
| 211 | }
|
| 212 |
|
| 213 | public static DensityFunction shiftB(final Holder<NormalNoise> noiseData) {
|
| 214 | return new ShiftNoiseFunction.ShiftB(noiseData);
|
| 215 | }
|
| 216 |
|
| 217 | public static DensityFunction shift(final Holder<NormalNoise> noiseData) {
|
| 218 | return new ShiftNoiseFunction.Shift(noiseData);
|
| 219 | }
|
| 220 |
|
| 221 | public static DensityFunction blendDensity(final DensityFunction input) {
|
| 222 | return new BlendDensityFunction(input);
|
| 223 | }
|
| 224 |
|
| 225 | public static DensityFunction endOuterIslands() {
|
| 226 | return new EndIslandFunction();
|
| 227 | }
|
| 228 |
|
| 229 | public static DensityFunction distanceToPoint(final Vec3i point, final DistanceMetric metric) {
|
| 230 | return new DistanceToPointFunction(point, metric);
|
| 231 | }
|
| 232 |
|
| 233 | public static DensityFunction add(final DensityFunction left, final DensityFunction right) {
|
| 234 | return new BinaryFunction(BinaryFunction.Type.ADD, left, right);
|
| 235 | }
|
| 236 |
|
| 237 | public static DensityFunction sub(final DensityFunction left, final DensityFunction right) {
|
| 238 | return new BinaryFunction(BinaryFunction.Type.SUB, left, right);
|
| 239 | }
|
| 240 |
|
| 241 | public static DensityFunction mul(final DensityFunction left, final DensityFunction right) {
|
| 242 | return new BinaryFunction(BinaryFunction.Type.MUL, left, right);
|
| 243 | }
|
| 244 |
|
| 245 | public static DensityFunction div(final DensityFunction left, final DensityFunction right) {
|
| 246 | return new BinaryFunction(BinaryFunction.Type.DIV, left, right);
|
| 247 | }
|
| 248 |
|
| 249 | public static DensityFunction pow(final DensityFunction base, final DensityFunction exponent) {
|
| 250 | return new PowFunction(base, exponent);
|
| 251 | }
|
| 252 |
|
| 253 | public static DensityFunction clamp(final DensityFunction input, final float min, final float max) {
|
| 254 | return new ClampFunction(input, min, max);
|
| 255 | }
|
| 256 |
|
| 257 | public static DensityFunction min(final DensityFunction left, final DensityFunction right) {
|
| 258 | return new BinaryFunction(BinaryFunction.Type.MIN, left, right);
|
| 259 | }
|
| 260 |
|
| 261 | public static DensityFunction max(final DensityFunction left, final DensityFunction right) {
|
| 262 | return new BinaryFunction(BinaryFunction.Type.MAX, left, right);
|
| 263 | }
|
| 264 |
|
| 265 | public static DensityFunction spline(final CubicSpline<SplineFunction.Coordinate> spline) {
|
| 266 | return new SplineFunction(spline);
|
| 267 | }
|
| 268 |
|
| 269 | public static DensityFunction sliceY(final DensityFunction input, final int y) {
|
| 270 | return slice(Direction.Axis.Y, y, input);
|
| 271 | }
|
| 272 |
|
| 273 | public static DensityFunction slice(final Direction.Axis axis, final int coordinate, final DensityFunction input) {
|
| 274 | return new SliceFunction(axis, coordinate, input);
|
| 275 | }
|
| 276 |
|
| 277 | public static DensityFunction yClampedGradient(final int fromY, final int toY, final float fromValue, final float toValue) {
|
| 278 | return gradient(Direction.Axis.Y, TilingMode.CLAMP_TO_EDGE, fromY, toY, fromValue, toValue);
|
| 279 | }
|
| 280 |
|
| 281 | public static DensityFunction gradient(
|
| 282 | final Direction.Axis axis, final TilingMode tiling, final int fromCoordinate, final int toCoordinate, final float fromValue, final float toValue
|
| 283 | ) {
|
| 284 | return new GradientFunction(axis, tiling, fromCoordinate, toCoordinate, fromValue, toValue);
|
| 285 | }
|
| 286 |
|
| 287 | public static DensityFunction blendAlpha() {
|
| 288 | return SimpleDensityFunction.BLEND_ALPHA;
|
| 289 | }
|
| 290 |
|
| 291 | public static DensityFunction blendOffset() {
|
| 292 | return SimpleDensityFunction.BLEND_OFFSET;
|
| 293 | }
|
| 294 |
|
| 295 | public static DensityFunction beardifier() {
|
| 296 | return SimpleDensityFunction.BEARDIFIER;
|
| 297 | }
|
| 298 |
|
| 299 | public static DensityFunction lerp(final DensityFunction alpha, final DensityFunction first, final DensityFunction second) {
|
| 300 | return new LerpFunction(alpha, first, second);
|
| 301 | }
|
| 302 |
|
| 303 | public static DensityFunction lerp(final DensityFunction factor, final float first, final DensityFunction second) {
|
| 304 | return lerp(factor, constant(first), second);
|
| 305 | }
|
| 306 |
|
| 307 | public static DensityFunction round(final RoundFunction.Type type, final DensityFunction input, final DensityFunction multiple) {
|
| 308 | return new RoundFunction(type, input, multiple);
|
| 309 | }
|
| 310 |
|
| 311 | public static DensityFunction round(final RoundFunction.Type type, final DensityFunction input) {
|
| 312 | return round(type, input, constant(1.0F));
|
| 313 | }
|
| 314 |
|
| 315 | public static DensityFunction floor(final DensityFunction input, final DensityFunction multiple) {
|
| 316 | return round(RoundFunction.Type.FLOOR, input, multiple);
|
| 317 | }
|
| 318 |
|
| 319 | public static DensityFunction floor(final DensityFunction input) {
|
| 320 | return floor(input, constant(1.0F));
|
| 321 | }
|
| 322 |
|
| 323 | public static DensityFunction round(final DensityFunction input, final DensityFunction multiple) {
|
| 324 | return round(RoundFunction.Type.ROUND, input, multiple);
|
| 325 | }
|
| 326 |
|
| 327 | public static DensityFunction round(final DensityFunction input) {
|
| 328 | return round(input, constant(1.0F));
|
| 329 | }
|
| 330 |
|
| 331 | public static DensityFunction ceil(final DensityFunction input, final DensityFunction multiple) {
|
| 332 | return round(RoundFunction.Type.CEIL, input, multiple);
|
| 333 | }
|
| 334 |
|
| 335 | public static DensityFunction ceil(final DensityFunction input) {
|
| 336 | return ceil(input, constant(1.0F));
|
| 337 | }
|
| 338 |
|
| 339 | public static DensityFunction truncate(final DensityFunction input, final DensityFunction multiple) {
|
| 340 | return round(RoundFunction.Type.TRUNCATE, input, multiple);
|
| 341 | }
|
| 342 |
|
| 343 | public static DensityFunction truncate(final DensityFunction input) {
|
| 344 | return truncate(input, constant(1.0F));
|
| 345 | }
|
| 346 |
|
| 347 | public static DensityFunction findTopSurface(final DensityFunction density, final DensityFunction upperBound, final int lowerBound, final int stepSize) {
|
| 348 | return new FindTopSurfaceFunction(density, upperBound, lowerBound, stepSize);
|
| 349 | }
|
| 350 |
|
| 351 | public static DensityFunction remap(final DensityFunction input, final float fromMin, final float fromMax, final float toMin, final float toMax) {
|
| 352 | float factor = (toMax - toMin) / (fromMax - fromMin);
|
| 353 | float offset = toMin - fromMin * factor;
|
| 354 | return offset == 0.0F ? input.mul(factor) : input.mul(factor).add(offset);
|
| 355 | }
|
| 356 |
|
| 357 | public static DensityFunction clampedMap(final DensityFunction input, final float fromMin, final float fromMax, final float toMin, final float toMax) {
|
| 358 | return remap(input.clamp(fromMin, fromMax), fromMin, fromMax, toMin, toMax);
|
| 359 | }
|
| 360 |
|
| 361 | public record HolderHolder(Holder<DensityFunction> function) implements DensityFunction {
|
| 362 | @Override
|
| 363 | public DensitySampler compileSampler(final DensityFunction.CompileContext context) {
|
| 364 | return this.function.value().compileSampler(context);
|
| 365 | }
|
| 366 |
|
| 367 | @Override
|
| 368 | public DensityFunction rewriteChildren(final DfRewriteRule rule) {
|
| 369 | DensityFunction newFunction = rule.rewrite(this.function.value());
|
| 370 | return newFunction == this.function.value() ? this : newFunction;
|
| 371 | }
|
| 372 |
|
| 373 | @Override
|
| 374 | public Interval range() {
|
| 375 | return this.function.value().range();
|
| 376 | }
|
| 377 |
|
| 378 | @Override
|
| 379 | public @DensityFunction.Axes int domainAxes() {
|
| 380 | return this.function.value().domainAxes();
|
| 381 | }
|
| 382 |
|
| 383 | @Override
|
| 384 | public MapCodec<DensityFunctions.HolderHolder> codec() {
|
| 385 | throw new UnsupportedOperationException("Calling .codec() on HolderHolder");
|
| 386 | }
|
| 387 | }
|
| 388 | }
|
| 389 |
|