package net.minecraft.world.level.levelgen.structure.templatesystem;

import com.mojang.datafixers.kinds.Applicative;
import com.mojang.datafixers.kinds.App;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.util.Mth;
import net.minecraft.core.Vec3i;
import java.util.Random;
import net.minecraft.core.BlockPos;
import com.mojang.serialization.Codec;

public class LinearPosTest extends PosRuleTest {
    public static final Codec<LinearPosTest> CODEC;
    private final float minChance;
    private final float maxChance;
    private final int minDist;
    private final int maxDist;
    
    public LinearPosTest(final float float1, final float float2, final int integer3, final int integer4) {
        if (integer3 >= integer4) {
            throw new IllegalArgumentException("Invalid range: [" + integer3 + "," + integer4 + "]");
        }
        this.minChance = float1;
        this.maxChance = float2;
        this.minDist = integer3;
        this.maxDist = integer4;
    }
    
    @Override
    public boolean test(final BlockPos fu1, final BlockPos fu2, final BlockPos fu3, final Random random) {
        final int integer6 = fu2.distManhattan(fu3);
        final float float7 = random.nextFloat();
        return float7 <= Mth.clampedLerp(this.minChance, this.maxChance, Mth.inverseLerp(integer6, this.minDist, this.maxDist));
    }
    
    @Override
    protected PosRuleTestType<?> getType() {
        return PosRuleTestType.LINEAR_POS_TEST;
    }
    
    static {
        CODEC = RecordCodecBuilder.create(instance -> ((RecordCodecBuilder.Instance)instance).group((App)Codec.FLOAT.fieldOf("min_chance").withDefault(0.0f).forGetter(cup -> cup.minChance), (App)Codec.FLOAT.fieldOf("max_chance").withDefault(0.0f).forGetter(cup -> cup.maxChance), (App)Codec.INT.fieldOf("min_dist").withDefault(0).forGetter(cup -> cup.minDist), (App)Codec.INT.fieldOf("max_dist").withDefault(0).forGetter(cup -> cup.maxDist)).apply(instance, LinearPosTest::new));
    }
}
