|
| 1 | +package com.ishland.c2me.opts.dfc.common.ast.misc; |
| 2 | + |
| 3 | +import com.ishland.c2me.opts.dfc.common.ast.AstNode; |
| 4 | +import com.ishland.c2me.opts.dfc.common.ast.AstTransformer; |
| 5 | +import com.ishland.c2me.opts.dfc.common.ast.EvalType; |
| 6 | +import com.ishland.c2me.opts.dfc.common.gen.BytecodeGen; |
| 7 | +import net.fabricmc.loader.api.FabricLoader; |
| 8 | +import net.minecraft.util.math.MathHelper; |
| 9 | +import org.objectweb.asm.Label; |
| 10 | +import org.objectweb.asm.Type; |
| 11 | +import org.objectweb.asm.commons.InstructionAdapter; |
| 12 | + |
| 13 | +import java.util.Objects; |
| 14 | + |
| 15 | +public class FindTopSurfaceNode implements AstNode { |
| 16 | + |
| 17 | + private final AstNode density; |
| 18 | + private final AstNode upperBound; |
| 19 | + private final AstNode lowerBound; |
| 20 | + private final int cellHeight; |
| 21 | + |
| 22 | + public FindTopSurfaceNode(AstNode density, AstNode upperBound, AstNode lowerBound, int cellHeight) { |
| 23 | + this.density = Objects.requireNonNull(density); |
| 24 | + this.upperBound = Objects.requireNonNull(upperBound); |
| 25 | + this.lowerBound = Objects.requireNonNull(lowerBound); |
| 26 | + this.cellHeight = cellHeight; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public double evalSingle(int x, int y, int z, EvalType type) { |
| 31 | + int topCellBlockY = MathHelper.floor(this.upperBound.evalSingle(x, y, z, type) / this.cellHeight) * this.cellHeight; |
| 32 | + int lowerBoundEval = (int) this.lowerBound.evalSingle(x, y, z, type); |
| 33 | + for (int y1 = topCellBlockY; y1 > lowerBoundEval; y1 -= this.cellHeight) { |
| 34 | + if (this.density.evalSingle(x, y1, z, EvalType.NORMAL) > 0.0) { |
| 35 | + return y1; |
| 36 | + } |
| 37 | + } |
| 38 | + return lowerBoundEval; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void evalMulti(double[] res, int[] x, int[] y, int[] z, EvalType type) { |
| 43 | + this.upperBound.evalMulti(res, x, y, z, type); |
| 44 | + int[] topCellBlockY = new int[res.length]; |
| 45 | + for (int i = 0; i < res.length; i++) { |
| 46 | + topCellBlockY[i] = MathHelper.floor(res[i] / this.cellHeight) * this.cellHeight; |
| 47 | + } |
| 48 | + this.lowerBound.evalMulti(res, x, y, z, type); |
| 49 | + int[] lowerBoundEval = new int[res.length]; |
| 50 | + for (int i = 0; i < res.length; i++) { |
| 51 | + lowerBoundEval[i] = (int) res[i]; |
| 52 | + } |
| 53 | + for (int i = 0; i < res.length; i ++) { |
| 54 | + res[i] = lowerBoundEval[i]; // default to lower bound |
| 55 | + for (int y1 = topCellBlockY[i]; y1 > lowerBoundEval[i]; y1 -= this.cellHeight) { |
| 56 | + if (this.density.evalSingle(x[i], y1, z[i], type) > 0.0) { |
| 57 | + res[i] = y1; |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public AstNode[] getChildren() { |
| 66 | + return new AstNode[] {this.density, this.upperBound, this.lowerBound}; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public AstNode transform(AstTransformer transformer) { |
| 71 | + AstNode density = this.density.transform(transformer); |
| 72 | + AstNode upperBound = this.upperBound.transform(transformer); |
| 73 | + AstNode lowerBound = this.lowerBound.transform(transformer); |
| 74 | + if (density == this.density && upperBound == this.upperBound && lowerBound == this.lowerBound) { |
| 75 | + return transformer.transform(this); |
| 76 | + } else { |
| 77 | + return transformer.transform(new FindTopSurfaceNode(density, upperBound, lowerBound, this.cellHeight)); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void doBytecodeGenSingle(BytecodeGen.Context ctx, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) { |
| 83 | + String densityMethod = ctx.newSingleMethod(this.density); |
| 84 | + String upperBoundMethod = ctx.newSingleMethod(this.upperBound); |
| 85 | + String lowerBoundMethod = ctx.newSingleMethod(this.lowerBound); |
| 86 | + |
| 87 | + int topCellBlockY = localVarConsumer.createLocalVariable("topCellBlockY", Type.INT_TYPE.getDescriptor()); |
| 88 | + int lowerBoundEval = localVarConsumer.createLocalVariable("lowerBoundEval", Type.INT_TYPE.getDescriptor()); |
| 89 | + ctx.callDelegateSingle(m, upperBoundMethod); |
| 90 | + m.dconst(this.cellHeight); |
| 91 | + m.div(Type.DOUBLE_TYPE); |
| 92 | + m.invokestatic( |
| 93 | + Type.getInternalName(MathHelper.class), |
| 94 | + FabricLoader.getInstance().getMappingResolver().mapMethodName("intermediary", "net.minecraft.class_3532", "method_15357", "(D)I"), |
| 95 | + "(D)I", |
| 96 | + false |
| 97 | + ); |
| 98 | + m.iconst(this.cellHeight); |
| 99 | + m.mul(Type.INT_TYPE); |
| 100 | + m.store(topCellBlockY, Type.INT_TYPE); |
| 101 | + |
| 102 | + ctx.callDelegateSingle(m, lowerBoundMethod); |
| 103 | + m.cast(Type.DOUBLE_TYPE, Type.INT_TYPE); |
| 104 | + m.store(lowerBoundEval, Type.INT_TYPE); |
| 105 | + |
| 106 | + Label loopStart = new Label(); |
| 107 | + Label loopEnd = new Label(); |
| 108 | + |
| 109 | + int y1 = localVarConsumer.createLocalVariable("y1", Type.INT_TYPE.getDescriptor()); |
| 110 | + m.load(topCellBlockY, Type.INT_TYPE); |
| 111 | + m.store(y1, Type.INT_TYPE); |
| 112 | + |
| 113 | + m.visitLabel(loopStart); |
| 114 | + |
| 115 | + m.load(y1, Type.INT_TYPE); |
| 116 | + m.load(lowerBoundEval, Type.INT_TYPE); |
| 117 | + m.ificmple(loopEnd); |
| 118 | + m.load(0, InstructionAdapter.OBJECT_TYPE); |
| 119 | + m.load(1, Type.INT_TYPE); |
| 120 | + m.load(y1, Type.INT_TYPE); |
| 121 | + m.load(3, Type.INT_TYPE); |
| 122 | + m.getstatic( |
| 123 | + Type.getInternalName(EvalType.class), |
| 124 | + "NORMAL", |
| 125 | + Type.getDescriptor(EvalType.class) |
| 126 | + ); |
| 127 | + m.invokevirtual(ctx.className, densityMethod, BytecodeGen.Context.SINGLE_DESC, false); |
| 128 | + m.dconst(0.0); |
| 129 | + m.cmpl(Type.DOUBLE_TYPE); |
| 130 | + |
| 131 | + Label notSatisfied = new Label(); |
| 132 | + m.ifle(notSatisfied); |
| 133 | + m.load(y1, Type.INT_TYPE); |
| 134 | + m.cast(Type.INT_TYPE, Type.DOUBLE_TYPE); |
| 135 | + m.areturn(Type.DOUBLE_TYPE); |
| 136 | + m.visitLabel(notSatisfied); |
| 137 | + |
| 138 | + m.load(y1, Type.INT_TYPE); |
| 139 | + m.iconst(this.cellHeight); |
| 140 | + m.sub(Type.INT_TYPE); |
| 141 | + m.store(y1, Type.INT_TYPE); |
| 142 | + m.goTo(loopStart); |
| 143 | + |
| 144 | + m.visitLabel(loopEnd); |
| 145 | + |
| 146 | + m.load(lowerBoundEval, Type.INT_TYPE); |
| 147 | + m.cast(Type.INT_TYPE, Type.DOUBLE_TYPE); |
| 148 | + m.areturn(Type.DOUBLE_TYPE); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public void doBytecodeGenMulti(BytecodeGen.Context ctx, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) { |
| 153 | + ctx.delegateToSingle(m, localVarConsumer, this); |
| 154 | + m.areturn(Type.VOID_TYPE); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public boolean equals(Object o) { |
| 159 | + if (o == null || getClass() != o.getClass()) return false; |
| 160 | + FindTopSurfaceNode that = (FindTopSurfaceNode) o; |
| 161 | + return cellHeight == that.cellHeight && Objects.equals(density, that.density) && Objects.equals(upperBound, that.upperBound) && Objects.equals(lowerBound, that.lowerBound); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public int hashCode() { |
| 166 | + int result = 1; |
| 167 | + |
| 168 | + result = 31 * result + this.getClass().hashCode(); |
| 169 | + result = 31 * result + this.density.hashCode(); |
| 170 | + result = 31 * result + this.upperBound.hashCode(); |
| 171 | + result = 31 * result + this.lowerBound.hashCode(); |
| 172 | + result = 31 * result + Integer.hashCode(cellHeight); |
| 173 | + |
| 174 | + return result; |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public boolean relaxedEquals(AstNode o) { |
| 179 | + if (o == null || getClass() != o.getClass()) return false; |
| 180 | + FindTopSurfaceNode that = (FindTopSurfaceNode) o; |
| 181 | + return cellHeight == that.cellHeight && this.density.relaxedEquals(that.density) && this.upperBound.relaxedEquals(that.upperBound) && this.lowerBound.relaxedEquals(that.lowerBound); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public int relaxedHashCode() { |
| 186 | + int result = 1; |
| 187 | + |
| 188 | + result = 31 * result + this.getClass().hashCode(); |
| 189 | + result = 31 * result + this.density.relaxedHashCode(); |
| 190 | + result = 31 * result + this.upperBound.relaxedHashCode(); |
| 191 | + result = 31 * result + this.lowerBound.relaxedHashCode(); |
| 192 | + result = 31 * result + Integer.hashCode(cellHeight); |
| 193 | + |
| 194 | + return result; |
| 195 | + } |
| 196 | +} |
0 commit comments