package net.minecraft.world.level.biome;

import com.google.common.collect.ImmutableSet;
import net.minecraft.util.Mth;
import java.util.Random;
import java.util.Set;
import net.minecraft.world.level.levelgen.WorldgenRandom;
import net.minecraft.world.level.levelgen.synth.SimplexNoise;

public class TheEndBiomeSource extends BiomeSource {
    private final SimplexNoise islandNoise;
    private final WorldgenRandom random;
    private static final Set<Biome> POSSIBLE_BIOMES;
    
    public TheEndBiomeSource(final TheEndBiomeSourceSettings bny) {
        super(TheEndBiomeSource.POSSIBLE_BIOMES);
        (this.random = new WorldgenRandom(bny.getSeed())).consumeCount(17292);
        this.islandNoise = new SimplexNoise(this.random);
    }
    
    @Override
    public Biome getNoiseBiome(final int integer1, final int integer2, final int integer3) {
        final int integer4 = integer1 >> 2;
        final int integer5 = integer3 >> 2;
        if (integer4 * (long)integer4 + integer5 * (long)integer5 <= 4096L) {
            return Biomes.THE_END;
        }
        final float float7 = this.getHeightValue(integer4 * 2 + 1, integer5 * 2 + 1);
        if (float7 > 40.0f) {
            return Biomes.END_HIGHLANDS;
        }
        if (float7 >= 0.0f) {
            return Biomes.END_MIDLANDS;
        }
        if (float7 < -20.0f) {
            return Biomes.SMALL_END_ISLANDS;
        }
        return Biomes.END_BARRENS;
    }
    
    @Override
    public float getHeightValue(final int integer1, final int integer2) {
        final int integer3 = integer1 / 2;
        final int integer4 = integer2 / 2;
        final int integer5 = integer1 % 2;
        final int integer6 = integer2 % 2;
        float float8 = 100.0f - Mth.sqrt((float)(integer1 * integer1 + integer2 * integer2)) * 8.0f;
        float8 = Mth.clamp(float8, -100.0f, 80.0f);
        for (int integer7 = -12; integer7 <= 12; ++integer7) {
            for (int integer8 = -12; integer8 <= 12; ++integer8) {
                final long long11 = integer3 + integer7;
                final long long12 = integer4 + integer8;
                if (long11 * long11 + long12 * long12 > 4096L && this.islandNoise.getValue((double)long11, (double)long12) < -0.8999999761581421) {
                    final float float9 = (Mth.abs((float)long11) * 3439.0f + Mth.abs((float)long12) * 147.0f) % 13.0f + 9.0f;
                    final float float10 = (float)(integer5 - integer7 * 2);
                    final float float11 = (float)(integer6 - integer8 * 2);
                    float float12 = 100.0f - Mth.sqrt(float10 * float10 + float11 * float11) * float9;
                    float12 = Mth.clamp(float12, -100.0f, 80.0f);
                    float8 = Math.max(float8, float12);
                }
            }
        }
        return float8;
    }
    
    static {
        POSSIBLE_BIOMES = ImmutableSet.<Biome>of(Biomes.THE_END, Biomes.END_HIGHLANDS, Biomes.END_MIDLANDS, Biomes.SMALL_END_ISLANDS, Biomes.END_BARRENS);
    }
}
