| 1 | package net.minecraft.world.level.levelgen.densityfunction;
|
| 2 |
|
| 3 | import net.minecraft.core.Direction;
|
| 4 | import net.minecraft.core.Holder;
|
| 5 | import net.minecraft.world.level.levelgen.densityfunction.generator.ConstantFunction;
|
| 6 | import net.minecraft.world.level.levelgen.densityfunction.generator.GradientFunction;
|
| 7 | import net.minecraft.world.level.levelgen.densityfunction.op.SliceFunction;
|
| 8 |
|
| 9 | @FunctionalInterface
|
| 10 | public interface DfRewriteRule {
|
| 11 | DfRewriteRule INLINE_REFERENCE = function -> function instanceof DensityFunctions.HolderHolder(Holder<DensityFunction> holder) ? holder.value() : function;
|
| 12 | DfRewriteRule SLICE_UNIFORM_AXES = new DfRewriteRule.SliceUniformAxes(7);
|
| 13 |
|
| 14 | static DfRewriteRule sequence(final DfRewriteRule... rules) {
|
| 15 | return function -> {
|
| 16 | for (DfRewriteRule rule : rules) {
|
| 17 | function = rule.rewrite(function);
|
| 18 | }
|
| 19 |
|
| 20 | return function;
|
| 21 | };
|
| 22 | }
|
| 23 |
|
| 24 | DensityFunction rewrite(DensityFunction function);
|
| 25 |
|
| 26 | record SliceUniformAxes(@DensityFunction.Axes int parentDomainAxes) implements DfRewriteRule {
|
| 27 | @Override
|
| 28 | public DensityFunction rewrite(final DensityFunction function) {
|
| 29 | if (shouldSkip(function)) {
|
| 30 | return function;
|
| 31 | }
|
| 32 |
|
| 33 | int domainAxes = function.domainAxes();
|
| 34 | if (this.parentDomainAxes == domainAxes) {
|
| 35 | return function.rewriteChildren(this);
|
| 36 | }
|
| 37 |
|
| 38 | DensityFunction newFunction = function.rewriteChildren(new DfRewriteRule.SliceUniformAxes(domainAxes));
|
| 39 | int removedAxes = this.parentDomainAxes & ~domainAxes;
|
| 40 | return this.removeAxes(newFunction, removedAxes);
|
| 41 | }
|
| 42 |
|
| 43 | private DensityFunction removeAxes(DensityFunction function, final @DensityFunction.Axes int axes) {
|
| 44 | int filteredAxes = axes & ~getExistingRemovedAxes(function);
|
| 45 | if ((filteredAxes & 1) != 0) {
|
| 46 | function = new SliceFunction(Direction.Axis.X, 0, function);
|
| 47 | }
|
| 48 |
|
| 49 | if ((filteredAxes & 4) != 0) {
|
| 50 | function = new SliceFunction(Direction.Axis.Z, 0, function);
|
| 51 | }
|
| 52 |
|
| 53 | if ((filteredAxes & 2) != 0) {
|
| 54 | function = new SliceFunction(Direction.Axis.Y, 0, function);
|
| 55 | }
|
| 56 |
|
| 57 | return function;
|
| 58 | }
|
| 59 |
|
| 60 | private static @DensityFunction.Axes int getExistingRemovedAxes(DensityFunction function) {
|
| 61 | int axes = 0;
|
| 62 |
|
| 63 | while (function instanceof SliceFunction) {
|
| 64 | SliceFunction slice = (SliceFunction)function;
|
| 65 | axes |= DensityFunction.axesFrom(slice.axis());
|
| 66 | function = slice.input();
|
| 67 | }
|
| 68 |
|
| 69 | return axes;
|
| 70 | }
|
| 71 |
|
| 72 | private static boolean shouldSkip(final DensityFunction function) {
|
| 73 | return function instanceof ConstantFunction || function instanceof GradientFunction;
|
| 74 | }
|
| 75 | }
|
| 76 | }
|
| 77 |
|