Skip to content

Commit

Permalink
fix: blending hopefully
Browse files Browse the repository at this point in the history
  • Loading branch information
ishland committed Oct 8, 2024
1 parent 0b7231d commit 8e26af8
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.ishland.c2me.opts.dfc.common.ducks;

public interface IBlendingAwareVisitor {

boolean c2me$isBlendingEnabled();

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.base.Suppliers;
import com.ishland.c2me.opts.dfc.common.ast.EvalType;
import com.ishland.c2me.opts.dfc.common.ducks.IArrayCacheCapable;
import com.ishland.c2me.opts.dfc.common.ducks.IBlendingAwareVisitor;
import com.ishland.c2me.opts.dfc.common.ducks.IFastCacheLike;
import com.ishland.c2me.opts.dfc.common.util.ArrayCache;
import com.ishland.c2me.opts.dfc.common.vif.EachApplierVanillaInterface;
Expand Down Expand Up @@ -32,6 +33,13 @@ private CompiledDensityFunction(CompiledEntry compiledEntry, Supplier<DensityFun

@Override
public DensityFunction apply(DensityFunctionVisitor visitor) {
if (visitor instanceof IBlendingAwareVisitor blendingAwareVisitor && blendingAwareVisitor.c2me$isBlendingEnabled()) {
DensityFunction fallback1 = this.getFallback();
if (fallback1 == null) {
throw new IllegalStateException("blendingFallback is no more");
}
return fallback1.apply(visitor);
}
boolean modified = false;
List<Object> args = this.compiledEntry.getArgs();
for (ListIterator<Object> iterator = args.listIterator(); iterator.hasNext(); ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.ishland.c2me.opts.dfc.common.gen;

import com.ishland.c2me.opts.dfc.common.ducks.IBlendingAwareVisitor;
import net.minecraft.world.gen.densityfunction.DensityFunction;

import java.util.Objects;

public class DelegatingBlendingAwareVisitor implements IBlendingAwareVisitor, DensityFunction.DensityFunctionVisitor {

private final DensityFunction.DensityFunctionVisitor delegate;
private final boolean blendingEnabled;

public DelegatingBlendingAwareVisitor(DensityFunction.DensityFunctionVisitor delegate, boolean blendingEnabled) {
this.delegate = Objects.requireNonNull(delegate);
this.blendingEnabled = blendingEnabled;
}

@Override
public DensityFunction apply(DensityFunction densityFunction) {
return this.delegate.apply(densityFunction);
}

@Override
public DensityFunction.Noise apply(DensityFunction.Noise noiseDensityFunction) {
return this.delegate.apply(noiseDensityFunction);
}

@Override
public boolean c2me$isBlendingEnabled() {
return this.blendingEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@
import com.google.common.base.Suppliers;
import com.ishland.c2me.opts.dfc.common.ast.EvalType;
import com.ishland.c2me.opts.dfc.common.ducks.IArrayCacheCapable;
import com.ishland.c2me.opts.dfc.common.ducks.IBlendingAwareVisitor;
import com.ishland.c2me.opts.dfc.common.ducks.ICoordinatesFilling;
import com.ishland.c2me.opts.dfc.common.util.ArrayCache;
import com.ishland.c2me.opts.dfc.common.vif.EachApplierVanillaInterface;
import net.minecraft.util.dynamic.CodecHolder;
import net.minecraft.world.gen.chunk.Blender;
import net.minecraft.world.gen.chunk.ChunkNoiseSampler;
import net.minecraft.world.gen.densityfunction.DensityFunction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;
import java.util.function.Supplier;

public class SubCompiledDensityFunction implements DensityFunction {

private static final Logger LOGGER = LoggerFactory.getLogger(SubCompiledDensityFunction.class);

private final ISingleMethod singleMethod;
private final IMultiMethod multiMethod;
protected final Supplier<DensityFunction> blendingFallback;
Expand All @@ -35,7 +40,7 @@ private static Supplier<DensityFunction> unwrap(DensityFunction densityFunction)
if (densityFunction instanceof SubCompiledDensityFunction scdf) {
return scdf.blendingFallback;
} else {
return densityFunction != null ? () -> densityFunction : null;
return densityFunction != null ? Suppliers.ofInstance(densityFunction) : null;
}
}

Expand Down Expand Up @@ -91,6 +96,13 @@ public DensityFunction apply(DensityFunctionVisitor visitor) {
if (this.getClass() != SubCompiledDensityFunction.class) {
throw new AbstractMethodError();
}
if (visitor instanceof IBlendingAwareVisitor blendingAwareVisitor && blendingAwareVisitor.c2me$isBlendingEnabled()) {
DensityFunction fallback1 = this.getFallback();
if (fallback1 == null) {
throw new IllegalStateException("blendingFallback is no more");
}
return fallback1.apply(visitor);
}
boolean modified = false;
Supplier<DensityFunction> fallback = this.blendingFallback != null ? Suppliers.memoize(() -> {
DensityFunction densityFunction = this.blendingFallback.get();
Expand Down Expand Up @@ -125,7 +137,7 @@ public CodecHolder<? extends DensityFunction> getCodecHolder() {
throw new UnsupportedOperationException();
}

private DensityFunction getFallback() {
protected DensityFunction getFallback() {
return this.blendingFallback != null ? this.blendingFallback.get() : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,34 @@

import com.ishland.c2me.opts.dfc.common.ducks.IArrayCacheCapable;
import com.ishland.c2me.opts.dfc.common.ducks.ICoordinatesFilling;
import com.ishland.c2me.opts.dfc.common.gen.DelegatingBlendingAwareVisitor;
import com.ishland.c2me.opts.dfc.common.util.ArrayCache;
import net.minecraft.world.gen.chunk.Blender;
import net.minecraft.world.gen.chunk.ChunkNoiseSampler;
import net.minecraft.world.gen.densityfunction.DensityFunction;
import net.minecraft.world.gen.densityfunction.DensityFunctionTypes;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(ChunkNoiseSampler.class)
public class MixinChunkNoiseSampler implements IArrayCacheCapable, ICoordinatesFilling {
public abstract class MixinChunkNoiseSampler implements IArrayCacheCapable, ICoordinatesFilling {

@Shadow @Final private int verticalCellBlockCount;
@Shadow @Final private int horizontalCellBlockCount;
@Shadow private int startBlockY;
@Shadow private int startBlockX;
@Shadow private int startBlockZ;
@Shadow private boolean isInInterpolationLoop;

@Shadow public abstract Blender getBlender();

private final ArrayCache c2me$arrayCache = new ArrayCache();

@Override
Expand Down Expand Up @@ -55,4 +63,20 @@ private void protectInterpolationLoop(DensityFunction function, CallbackInfoRetu
throw new IllegalStateException("Cannot create more wrapping during interpolation loop");
}
}

@ModifyArg(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/gen/noise/NoiseRouter;apply(Lnet/minecraft/world/gen/densityfunction/DensityFunction$DensityFunctionVisitor;)Lnet/minecraft/world/gen/noise/NoiseRouter;"))
private DensityFunction.DensityFunctionVisitor modifyVisitor1(DensityFunction.DensityFunctionVisitor visitor) {
return c2me$getDelegatingBlendingAwareVisitor(visitor);
}

@Unique
private @NotNull DelegatingBlendingAwareVisitor c2me$getDelegatingBlendingAwareVisitor(DensityFunction.DensityFunctionVisitor visitor) {
return new DelegatingBlendingAwareVisitor(visitor, this.getBlender() != Blender.getNoBlending());
}

@ModifyArg(method = {"<init>", "createMultiNoiseSampler"}, at = @At(value = "INVOKE", target = "Lnet/minecraft/world/gen/densityfunction/DensityFunction;apply(Lnet/minecraft/world/gen/densityfunction/DensityFunction$DensityFunctionVisitor;)Lnet/minecraft/world/gen/densityfunction/DensityFunction;"), require = 7, expect = 7)
private DensityFunction.DensityFunctionVisitor modifyVisitor2(DensityFunction.DensityFunctionVisitor visitor) {
return c2me$getDelegatingBlendingAwareVisitor(visitor);
}

}

0 comments on commit 8e26af8

Please sign in to comment.