    001package net.minecraft.world.gen;
    002
    003import java.util.Random;
    004import net.minecraft.block.Block;
    005import net.minecraft.util.MathHelper;
    006import net.minecraft.world.World;
    007import net.minecraft.world.biome.BiomeGenBase;
    008
    009public class MapGenRavine extends MapGenBase
    010{
    011    private float[] field_75046_d = new float[1024];
    012
    013    protected void generateRavine(long par1, int par3, int par4, byte[] par5ArrayOfByte, double par6, double par8, double par10, float par12, float par13, float par14, int par15, int par16, double par17)
    014    {
    015        Random random = new Random(par1);
    016        double d4 = (double)(par3 * 16 + 8);
    017        double d5 = (double)(par4 * 16 + 8);
    018        float f3 = 0.0F;
    019        float f4 = 0.0F;
    020
    021        if (par16 <= 0)
    022        {
    023            int j1 = this.range * 16 - 16;
    024            par16 = j1 - random.nextInt(j1 / 4);
    025        }
    026
    027        boolean flag = false;
    028
    029        if (par15 == -1)
    030        {
    031            par15 = par16 / 2;
    032            flag = true;
    033        }
    034
    035        float f5 = 1.0F;
    036
    037        for (int k1 = 0; k1 < 128; ++k1)
    038        {
    039            if (k1 == 0 || random.nextInt(3) == 0)
    040            {
    041                f5 = 1.0F + random.nextFloat() * random.nextFloat() * 1.0F;
    042            }
    043
    044            this.field_75046_d[k1] = f5 * f5;
    045        }
    046
    047        for (; par15 < par16; ++par15)
    048        {
    049            double d6 = 1.5D + (double)(MathHelper.sin((float)par15 * (float)Math.PI / (float)par16) * par12 * 1.0F);
    050            double d7 = d6 * par17;
    051            d6 *= (double)random.nextFloat() * 0.25D + 0.75D;
    052            d7 *= (double)random.nextFloat() * 0.25D + 0.75D;
    053            float f6 = MathHelper.cos(par14);
    054            float f7 = MathHelper.sin(par14);
    055            par6 += (double)(MathHelper.cos(par13) * f6);
    056            par8 += (double)f7;
    057            par10 += (double)(MathHelper.sin(par13) * f6);
    058            par14 *= 0.7F;
    059            par14 += f4 * 0.05F;
    060            par13 += f3 * 0.05F;
    061            f4 *= 0.8F;
    062            f3 *= 0.5F;
    063            f4 += (random.nextFloat() - random.nextFloat()) * random.nextFloat() * 2.0F;
    064            f3 += (random.nextFloat() - random.nextFloat()) * random.nextFloat() * 4.0F;
    065
    066            if (flag || random.nextInt(4) != 0)
    067            {
    068                double d8 = par6 - d4;
    069                double d9 = par10 - d5;
    070                double d10 = (double)(par16 - par15);
    071                double d11 = (double)(par12 + 2.0F + 16.0F);
    072
    073                if (d8 * d8 + d9 * d9 - d10 * d10 > d11 * d11)
    074                {
    075                    return;
    076                }
    077
    078                if (par6 >= d4 - 16.0D - d6 * 2.0D && par10 >= d5 - 16.0D - d6 * 2.0D && par6 <= d4 + 16.0D + d6 * 2.0D && par10 <= d5 + 16.0D + d6 * 2.0D)
    079                {
    080                    int l1 = MathHelper.floor_double(par6 - d6) - par3 * 16 - 1;
    081                    int i2 = MathHelper.floor_double(par6 + d6) - par3 * 16 + 1;
    082                    int j2 = MathHelper.floor_double(par8 - d7) - 1;
    083                    int k2 = MathHelper.floor_double(par8 + d7) + 1;
    084                    int l2 = MathHelper.floor_double(par10 - d6) - par4 * 16 - 1;
    085                    int i3 = MathHelper.floor_double(par10 + d6) - par4 * 16 + 1;
    086
    087                    if (l1 < 0)
    088                    {
    089                        l1 = 0;
    090                    }
    091
    092                    if (i2 > 16)
    093                    {
    094                        i2 = 16;
    095                    }
    096
    097                    if (j2 < 1)
    098                    {
    099                        j2 = 1;
    100                    }
    101
    102                    if (k2 > 120)
    103                    {
    104                        k2 = 120;
    105                    }
    106
    107                    if (l2 < 0)
    108                    {
    109                        l2 = 0;
    110                    }
    111
    112                    if (i3 > 16)
    113                    {
    114                        i3 = 16;
    115                    }
    116
    117                    boolean flag1 = false;
    118                    int j3;
    119                    int k3;
    120
    121                    for (j3 = l1; !flag1 && j3 < i2; ++j3)
    122                    {
    123                        for (int l3 = l2; !flag1 && l3 < i3; ++l3)
    124                        {
    125                            for (int i4 = k2 + 1; !flag1 && i4 >= j2 - 1; --i4)
    126                            {
    127                                k3 = (j3 * 16 + l3) * 128 + i4;
    128
    129                                if (i4 >= 0 && i4 < 128)
    130                                {
    131                                    if (isOceanBlock(par5ArrayOfByte, k3, j3, i4, l3, par3, par4))
    132                                    {
    133                                        flag1 = true;
    134                                    }
    135
    136                                    if (i4 != j2 - 1 && j3 != l1 && j3 != i2 - 1 && l3 != l2 && l3 != i3 - 1)
    137                                    {
    138                                        i4 = j2;
    139                                    }
    140                                }
    141                            }
    142                        }
    143                    }
    144
    145                    if (!flag1)
    146                    {
    147                        for (j3 = l1; j3 < i2; ++j3)
    148                        {
    149                            double d12 = ((double)(j3 + par3 * 16) + 0.5D - par6) / d6;
    150
    151                            for (k3 = l2; k3 < i3; ++k3)
    152                            {
    153                                double d13 = ((double)(k3 + par4 * 16) + 0.5D - par10) / d6;
    154                                int j4 = (j3 * 16 + k3) * 128 + k2;
    155                                boolean flag2 = false;
    156
    157                                if (d12 * d12 + d13 * d13 < 1.0D)
    158                                {
    159                                    for (int k4 = k2 - 1; k4 >= j2; --k4)
    160                                    {
    161                                        double d14 = ((double)k4 + 0.5D - par8) / d7;
    162
    163                                        if ((d12 * d12 + d13 * d13) * (double)this.field_75046_d[k4] + d14 * d14 / 6.0D < 1.0D)
    164                                        {
    165                                            if (isTopBlock(par5ArrayOfByte, j4, j3, k4, k3, par3, par4))
    166                                            {
    167                                                flag2 = true;
    168                                            }
    169
    170                                            digBlock(par5ArrayOfByte, j4, j3, k4, k3, par3, par4, flag2);
    171                                        }
    172
    173                                        --j4;
    174                                    }
    175                                }
    176                            }
    177                        }
    178
    179                        if (flag)
    180                        {
    181                            break;
    182                        }
    183                    }
    184                }
    185            }
    186        }
    187    }
    188
    189    /**
    190     * Recursively called by generate() (generate) and optionally by itself.
    191     */
    192    protected void recursiveGenerate(World par1World, int par2, int par3, int par4, int par5, byte[] par6ArrayOfByte)
    193    {
    194        if (this.rand.nextInt(50) == 0)
    195        {
    196            double d0 = (double)(par2 * 16 + this.rand.nextInt(16));
    197            double d1 = (double)(this.rand.nextInt(this.rand.nextInt(40) + 8) + 20);
    198            double d2 = (double)(par3 * 16 + this.rand.nextInt(16));
    199            byte b0 = 1;
    200
    201            for (int i1 = 0; i1 < b0; ++i1)
    202            {
    203                float f = this.rand.nextFloat() * (float)Math.PI * 2.0F;
    204                float f1 = (this.rand.nextFloat() - 0.5F) * 2.0F / 8.0F;
    205                float f2 = (this.rand.nextFloat() * 2.0F + this.rand.nextFloat()) * 2.0F;
    206                this.generateRavine(this.rand.nextLong(), par4, par5, par6ArrayOfByte, d0, d1, d2, f2, f, f1, 0, 0, 3.0D);
    207            }
    208        }
    209    }
    210
    211    protected boolean isOceanBlock(byte[] data, int index, int x, int y, int z, int chunkX, int chunkZ)
    212    {
    213        return data[index] == Block.waterMoving.blockID || data[index] == Block.waterStill.blockID;
    214    }
    215
    216    //Exception biomes to make sure we generate like vanilla
    217    private boolean isExceptionBiome(BiomeGenBase biome)
    218    {
    219        if (biome == BiomeGenBase.mushroomIsland) return true;
    220        if (biome == BiomeGenBase.beach) return true;
    221        if (biome == BiomeGenBase.desert) return true;
    222        return false;
    223    }
    224
    225    //Determine if the block at the specified location is the top block for the biome, we take into account
    226    //Vanilla bugs to make sure that we generate the map the same way vanilla does.
    227    private boolean isTopBlock(byte[] data, int index, int x, int y, int z, int chunkX, int chunkZ)
    228    {
    229        BiomeGenBase biome = worldObj.getBiomeGenForCoords(x + chunkX * 16, z + chunkZ * 16);
    230        return (isExceptionBiome(biome) ? data[index] == Block.grass.blockID : data[index] == biome.topBlock);
    231    }
    232
    233    /**
    234     * Digs out the current block, default implementation removes stone, filler, and top block
    235     * Sets the block to lava if y is less then 10, and air other wise.
    236     * If setting to air, it also checks to see if we've broken the surface and if so
    237     * tries to make the floor the biome's top block
    238     *
    239     * @param data Block data array
    240     * @param index Pre-calculated index into block data
    241     * @param x local X position
    242     * @param y local Y position
    243     * @param z local Z position
    244     * @param chunkX Chunk X position
    245     * @param chunkZ Chunk Y position
    246     * @param foundTop True if we've encountered the biome's top block. Ideally if we've broken the surface.
    247     */
    248    protected void digBlock(byte[] data, int index, int x, int y, int z, int chunkX, int chunkZ, boolean foundTop)
    249    {
    250        BiomeGenBase biome = worldObj.getBiomeGenForCoords(x + chunkX * 16, z + chunkZ * 16);
    251        int top    = (isExceptionBiome(biome) ? Block.grass.blockID : biome.topBlock);
    252        int filler = (isExceptionBiome(biome) ? Block.dirt.blockID  : biome.fillerBlock);
    253        int block  = data[index];
    254
    255        if (block == Block.stone.blockID || block == filler || block == top)
    256        {
    257            if (y < 10)
    258            {
    259                data[index] = (byte)Block.lavaMoving.blockID;
    260            }
    261            else
    262            {
    263                data[index] = 0;
    264
    265                if (foundTop && data[index - 1] == filler)
    266                {
    267                    data[index - 1] = (byte)top;
    268                }
    269            }
    270        }
    271    }
    272}