    001package net.minecraft.world.gen;
    002
    003import java.util.Random;
    004
    005public class NoiseGeneratorPerlin extends NoiseGenerator
    006{
    007    private int[] permutations;
    008    public double xCoord;
    009    public double yCoord;
    010    public double zCoord;
    011
    012    public NoiseGeneratorPerlin()
    013    {
    014        this(new Random());
    015    }
    016
    017    public NoiseGeneratorPerlin(Random par1Random)
    018    {
    019        this.permutations = new int[512];
    020        this.xCoord = par1Random.nextDouble() * 256.0D;
    021        this.yCoord = par1Random.nextDouble() * 256.0D;
    022        this.zCoord = par1Random.nextDouble() * 256.0D;
    023        int i;
    024
    025        for (i = 0; i < 256; this.permutations[i] = i++)
    026        {
    027            ;
    028        }
    029
    030        for (i = 0; i < 256; ++i)
    031        {
    032            int j = par1Random.nextInt(256 - i) + i;
    033            int k = this.permutations[i];
    034            this.permutations[i] = this.permutations[j];
    035            this.permutations[j] = k;
    036            this.permutations[i + 256] = this.permutations[i];
    037        }
    038    }
    039
    040    public final double lerp(double par1, double par3, double par5)
    041    {
    042        return par3 + par1 * (par5 - par3);
    043    }
    044
    045    public final double func_76309_a(int par1, double par2, double par4)
    046    {
    047        int j = par1 & 15;
    048        double d2 = (double)(1 - ((j & 8) >> 3)) * par2;
    049        double d3 = j < 4 ? 0.0D : (j != 12 && j != 14 ? par4 : par2);
    050        return ((j & 1) == 0 ? d2 : -d2) + ((j & 2) == 0 ? d3 : -d3);
    051    }
    052
    053    public final double grad(int par1, double par2, double par4, double par6)
    054    {
    055        int j = par1 & 15;
    056        double d3 = j < 8 ? par2 : par4;
    057        double d4 = j < 4 ? par4 : (j != 12 && j != 14 ? par6 : par2);
    058        return ((j & 1) == 0 ? d3 : -d3) + ((j & 2) == 0 ? d4 : -d4);
    059    }
    060
    061    /**
    062     * pars: noiseArray , xOffset , yOffset , zOffset , xSize , ySize , zSize , xScale, yScale , zScale , noiseScale.
    063     * noiseArray should be xSize*ySize*zSize in size
    064     */
    065    public void populateNoiseArray(double[] par1ArrayOfDouble, double par2, double par4, double par6, int par8, int par9, int par10, double par11, double par13, double par15, double par17)
    066    {
    067        int l;
    068        int i1;
    069        double d7;
    070        double d8;
    071        double d9;
    072        int j1;
    073        double d10;
    074        int k1;
    075        int l1;
    076        int i2;
    077        int j2;
    078
    079        if (par9 == 1)
    080        {
    081            boolean flag = false;
    082            boolean flag1 = false;
    083            boolean flag2 = false;
    084            boolean flag3 = false;
    085            double d11 = 0.0D;
    086            double d12 = 0.0D;
    087            j2 = 0;
    088            double d13 = 1.0D / par17;
    089
    090            for (int k2 = 0; k2 < par8; ++k2)
    091            {
    092                d7 = par2 + (double)k2 * par11 + this.xCoord;
    093                int l2 = (int)d7;
    094
    095                if (d7 < (double)l2)
    096                {
    097                    --l2;
    098                }
    099
    100                int i3 = l2 & 255;
    101                d7 -= (double)l2;
    102                d8 = d7 * d7 * d7 * (d7 * (d7 * 6.0D - 15.0D) + 10.0D);
    103
    104                for (j1 = 0; j1 < par10; ++j1)
    105                {
    106                    d9 = par6 + (double)j1 * par15 + this.zCoord;
    107                    k1 = (int)d9;
    108
    109                    if (d9 < (double)k1)
    110                    {
    111                        --k1;
    112                    }
    113
    114                    l1 = k1 & 255;
    115                    d9 -= (double)k1;
    116                    d10 = d9 * d9 * d9 * (d9 * (d9 * 6.0D - 15.0D) + 10.0D);
    117                    l = this.permutations[i3] + 0;
    118                    int j3 = this.permutations[l] + l1;
    119                    int k3 = this.permutations[i3 + 1] + 0;
    120                    i1 = this.permutations[k3] + l1;
    121                    d11 = this.lerp(d8, this.func_76309_a(this.permutations[j3], d7, d9), this.grad(this.permutations[i1], d7 - 1.0D, 0.0D, d9));
    122                    d12 = this.lerp(d8, this.grad(this.permutations[j3 + 1], d7, 0.0D, d9 - 1.0D), this.grad(this.permutations[i1 + 1], d7 - 1.0D, 0.0D, d9 - 1.0D));
    123                    double d14 = this.lerp(d10, d11, d12);
    124                    i2 = j2++;
    125                    par1ArrayOfDouble[i2] += d14 * d13;
    126                }
    127            }
    128        }
    129        else
    130        {
    131            l = 0;
    132            double d15 = 1.0D / par17;
    133            i1 = -1;
    134            boolean flag4 = false;
    135            boolean flag5 = false;
    136            boolean flag6 = false;
    137            boolean flag7 = false;
    138            boolean flag8 = false;
    139            boolean flag9 = false;
    140            double d16 = 0.0D;
    141            d7 = 0.0D;
    142            double d17 = 0.0D;
    143            d8 = 0.0D;
    144
    145            for (j1 = 0; j1 < par8; ++j1)
    146            {
    147                d9 = par2 + (double)j1 * par11 + this.xCoord;
    148                k1 = (int)d9;
    149
    150                if (d9 < (double)k1)
    151                {
    152                    --k1;
    153                }
    154
    155                l1 = k1 & 255;
    156                d9 -= (double)k1;
    157                d10 = d9 * d9 * d9 * (d9 * (d9 * 6.0D - 15.0D) + 10.0D);
    158
    159                for (int l3 = 0; l3 < par10; ++l3)
    160                {
    161                    double d18 = par6 + (double)l3 * par15 + this.zCoord;
    162                    int i4 = (int)d18;
    163
    164                    if (d18 < (double)i4)
    165                    {
    166                        --i4;
    167                    }
    168
    169                    int j4 = i4 & 255;
    170                    d18 -= (double)i4;
    171                    double d19 = d18 * d18 * d18 * (d18 * (d18 * 6.0D - 15.0D) + 10.0D);
    172
    173                    for (int k4 = 0; k4 < par9; ++k4)
    174                    {
    175                        double d20 = par4 + (double)k4 * par13 + this.yCoord;
    176                        int l4 = (int)d20;
    177
    178                        if (d20 < (double)l4)
    179                        {
    180                            --l4;
    181                        }
    182
    183                        int i5 = l4 & 255;
    184                        d20 -= (double)l4;
    185                        double d21 = d20 * d20 * d20 * (d20 * (d20 * 6.0D - 15.0D) + 10.0D);
    186
    187                        if (k4 == 0 || i5 != i1)
    188                        {
    189                            i1 = i5;
    190                            int j5 = this.permutations[l1] + i5;
    191                            int k5 = this.permutations[j5] + j4;
    192                            int l5 = this.permutations[j5 + 1] + j4;
    193                            int i6 = this.permutations[l1 + 1] + i5;
    194                            j2 = this.permutations[i6] + j4;
    195                            int j6 = this.permutations[i6 + 1] + j4;
    196                            d16 = this.lerp(d10, this.grad(this.permutations[k5], d9, d20, d18), this.grad(this.permutations[j2], d9 - 1.0D, d20, d18));
    197                            d7 = this.lerp(d10, this.grad(this.permutations[l5], d9, d20 - 1.0D, d18), this.grad(this.permutations[j6], d9 - 1.0D, d20 - 1.0D, d18));
    198                            d17 = this.lerp(d10, this.grad(this.permutations[k5 + 1], d9, d20, d18 - 1.0D), this.grad(this.permutations[j2 + 1], d9 - 1.0D, d20, d18 - 1.0D));
    199                            d8 = this.lerp(d10, this.grad(this.permutations[l5 + 1], d9, d20 - 1.0D, d18 - 1.0D), this.grad(this.permutations[j6 + 1], d9 - 1.0D, d20 - 1.0D, d18 - 1.0D));
    200                        }
    201
    202                        double d22 = this.lerp(d21, d16, d7);
    203                        double d23 = this.lerp(d21, d17, d8);
    204                        double d24 = this.lerp(d19, d22, d23);
    205                        i2 = l++;
    206                        par1ArrayOfDouble[i2] += d24 * d15;
    207                    }
    208                }
    209            }
    210        }
    211    }
    212}