    001    package net.minecraft.world.gen;
    002
    003    import java.util.Random;
    004    import net.minecraft.block.Block;
    005    import net.minecraft.util.MathHelper;
    006    import net.minecraft.world.World;
    007
    008    public class MapGenCaves extends MapGenBase
    009    {
    010        /**
    011         * Generates a larger initial cave node than usual. Called 25% of the time.
    012         */
    013        protected void generateLargeCaveNode(long par1, int par3, int par4, byte[] par5ArrayOfByte, double par6, double par8, double par10)
    014        {
    015            this.generateCaveNode(par1, par3, par4, par5ArrayOfByte, par6, par8, par10, 1.0F + this.rand.nextFloat() * 6.0F, 0.0F, 0.0F, -1, -1, 0.5D);
    016        }
    017
    018        /**
    019         * Generates a node in the current cave system recursion tree.
    020         */
    021        protected void generateCaveNode(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)
    022        {
    023            double var19 = (double)(par3 * 16 + 8);
    024            double var21 = (double)(par4 * 16 + 8);
    025            float var23 = 0.0F;
    026            float var24 = 0.0F;
    027            Random var25 = new Random(par1);
    028
    029            if (par16 <= 0)
    030            {
    031                int var26 = this.range * 16 - 16;
    032                par16 = var26 - var25.nextInt(var26 / 4);
    033            }
    034
    035            boolean var54 = false;
    036
    037            if (par15 == -1)
    038            {
    039                par15 = par16 / 2;
    040                var54 = true;
    041            }
    042
    043            int var27 = var25.nextInt(par16 / 2) + par16 / 4;
    044
    045            for (boolean var28 = var25.nextInt(6) == 0; par15 < par16; ++par15)
    046            {
    047                double var29 = 1.5D + (double)(MathHelper.sin((float)par15 * (float)Math.PI / (float)par16) * par12 * 1.0F);
    048                double var31 = var29 * par17;
    049                float var33 = MathHelper.cos(par14);
    050                float var34 = MathHelper.sin(par14);
    051                par6 += (double)(MathHelper.cos(par13) * var33);
    052                par8 += (double)var34;
    053                par10 += (double)(MathHelper.sin(par13) * var33);
    054
    055                if (var28)
    056                {
    057                    par14 *= 0.92F;
    058                }
    059                else
    060                {
    061                    par14 *= 0.7F;
    062                }
    063
    064                par14 += var24 * 0.1F;
    065                par13 += var23 * 0.1F;
    066                var24 *= 0.9F;
    067                var23 *= 0.75F;
    068                var24 += (var25.nextFloat() - var25.nextFloat()) * var25.nextFloat() * 2.0F;
    069                var23 += (var25.nextFloat() - var25.nextFloat()) * var25.nextFloat() * 4.0F;
    070
    071                if (!var54 && par15 == var27 && par12 > 1.0F && par16 > 0)
    072                {
    073                    this.generateCaveNode(var25.nextLong(), par3, par4, par5ArrayOfByte, par6, par8, par10, var25.nextFloat() * 0.5F + 0.5F, par13 - ((float)Math.PI / 2F), par14 / 3.0F, par15, par16, 1.0D);
    074                    this.generateCaveNode(var25.nextLong(), par3, par4, par5ArrayOfByte, par6, par8, par10, var25.nextFloat() * 0.5F + 0.5F, par13 + ((float)Math.PI / 2F), par14 / 3.0F, par15, par16, 1.0D);
    075                    return;
    076                }
    077
    078                if (var54 || var25.nextInt(4) != 0)
    079                {
    080                    double var35 = par6 - var19;
    081                    double var37 = par10 - var21;
    082                    double var39 = (double)(par16 - par15);
    083                    double var41 = (double)(par12 + 2.0F + 16.0F);
    084
    085                    if (var35 * var35 + var37 * var37 - var39 * var39 > var41 * var41)
    086                    {
    087                        return;
    088                    }
    089
    090                    if (par6 >= var19 - 16.0D - var29 * 2.0D && par10 >= var21 - 16.0D - var29 * 2.0D && par6 <= var19 + 16.0D + var29 * 2.0D && par10 <= var21 + 16.0D + var29 * 2.0D)
    091                    {
    092                        int var55 = MathHelper.floor_double(par6 - var29) - par3 * 16 - 1;
    093                        int var36 = MathHelper.floor_double(par6 + var29) - par3 * 16 + 1;
    094                        int var57 = MathHelper.floor_double(par8 - var31) - 1;
    095                        int var38 = MathHelper.floor_double(par8 + var31) + 1;
    096                        int var56 = MathHelper.floor_double(par10 - var29) - par4 * 16 - 1;
    097                        int var40 = MathHelper.floor_double(par10 + var29) - par4 * 16 + 1;
    098
    099                        if (var55 < 0)
    100                        {
    101                            var55 = 0;
    102                        }
    103
    104                        if (var36 > 16)
    105                        {
    106                            var36 = 16;
    107                        }
    108
    109                        if (var57 < 1)
    110                        {
    111                            var57 = 1;
    112                        }
    113
    114                        if (var38 > 120)
    115                        {
    116                            var38 = 120;
    117                        }
    118
    119                        if (var56 < 0)
    120                        {
    121                            var56 = 0;
    122                        }
    123
    124                        if (var40 > 16)
    125                        {
    126                            var40 = 16;
    127                        }
    128
    129                        boolean var58 = false;
    130                        int var42;
    131                        int var45;
    132
    133                        for (var42 = var55; !var58 && var42 < var36; ++var42)
    134                        {
    135                            for (int var43 = var56; !var58 && var43 < var40; ++var43)
    136                            {
    137                                for (int var44 = var38 + 1; !var58 && var44 >= var57 - 1; --var44)
    138                                {
    139                                    var45 = (var42 * 16 + var43) * 128 + var44;
    140
    141                                    if (var44 >= 0 && var44 < 128)
    142                                    {
    143                                        if (par5ArrayOfByte[var45] == Block.waterMoving.blockID || par5ArrayOfByte[var45] == Block.waterStill.blockID)
    144                                        {
    145                                            var58 = true;
    146                                        }
    147
    148                                        if (var44 != var57 - 1 && var42 != var55 && var42 != var36 - 1 && var43 != var56 && var43 != var40 - 1)
    149                                        {
    150                                            var44 = var57;
    151                                        }
    152                                    }
    153                                }
    154                            }
    155                        }
    156
    157                        if (!var58)
    158                        {
    159                            for (var42 = var55; var42 < var36; ++var42)
    160                            {
    161                                double var59 = ((double)(var42 + par3 * 16) + 0.5D - par6) / var29;
    162
    163                                for (var45 = var56; var45 < var40; ++var45)
    164                                {
    165                                    double var46 = ((double)(var45 + par4 * 16) + 0.5D - par10) / var29;
    166                                    int var48 = (var42 * 16 + var45) * 128 + var38;
    167                                    boolean var49 = false;
    168
    169                                    if (var59 * var59 + var46 * var46 < 1.0D)
    170                                    {
    171                                        for (int var50 = var38 - 1; var50 >= var57; --var50)
    172                                        {
    173                                            double var51 = ((double)var50 + 0.5D - par8) / var31;
    174
    175                                            if (var51 > -0.7D && var59 * var59 + var51 * var51 + var46 * var46 < 1.0D)
    176                                            {
    177                                                byte var53 = par5ArrayOfByte[var48];
    178
    179                                                if (var53 == Block.grass.blockID)
    180                                                {
    181                                                    var49 = true;
    182                                                }
    183
    184                                                if (var53 == Block.stone.blockID || var53 == Block.dirt.blockID || var53 == Block.grass.blockID)
    185                                                {
    186                                                    if (var50 < 10)
    187                                                    {
    188                                                        par5ArrayOfByte[var48] = (byte)Block.lavaMoving.blockID;
    189                                                    }
    190                                                    else
    191                                                    {
    192                                                        par5ArrayOfByte[var48] = 0;
    193
    194                                                        if (var49 && par5ArrayOfByte[var48 - 1] == Block.dirt.blockID)
    195                                                        {
    196                                                            par5ArrayOfByte[var48 - 1] = this.worldObj.getBiomeGenForCoords(var42 + par3 * 16, var45 + par4 * 16).topBlock;
    197                                                        }
    198                                                    }
    199                                                }
    200                                            }
    201
    202                                            --var48;
    203                                        }
    204                                    }
    205                                }
    206                            }
    207
    208                            if (var54)
    209                            {
    210                                break;
    211                            }
    212                        }
    213                    }
    214                }
    215            }
    216        }
    217
    218        /**
    219         * Recursively called by generate() (generate) and optionally by itself.
    220         */
    221        protected void recursiveGenerate(World par1World, int par2, int par3, int par4, int par5, byte[] par6ArrayOfByte)
    222        {
    223            int var7 = this.rand.nextInt(this.rand.nextInt(this.rand.nextInt(40) + 1) + 1);
    224
    225            if (this.rand.nextInt(15) != 0)
    226            {
    227                var7 = 0;
    228            }
    229
    230            for (int var8 = 0; var8 < var7; ++var8)
    231            {
    232                double var9 = (double)(par2 * 16 + this.rand.nextInt(16));
    233                double var11 = (double)this.rand.nextInt(this.rand.nextInt(120) + 8);
    234                double var13 = (double)(par3 * 16 + this.rand.nextInt(16));
    235                int var15 = 1;
    236
    237                if (this.rand.nextInt(4) == 0)
    238                {
    239                    this.generateLargeCaveNode(this.rand.nextLong(), par4, par5, par6ArrayOfByte, var9, var11, var13);
    240                    var15 += this.rand.nextInt(4);
    241                }
    242
    243                for (int var16 = 0; var16 < var15; ++var16)
    244                {
    245                    float var17 = this.rand.nextFloat() * (float)Math.PI * 2.0F;
    246                    float var18 = (this.rand.nextFloat() - 0.5F) * 2.0F / 8.0F;
    247                    float var19 = this.rand.nextFloat() * 2.0F + this.rand.nextFloat();
    248
    249                    if (this.rand.nextInt(10) == 0)
    250                    {
    251                        var19 *= this.rand.nextFloat() * this.rand.nextFloat() * 3.0F + 1.0F;
    252                    }
    253
    254                    this.generateCaveNode(this.rand.nextLong(), par4, par5, par6ArrayOfByte, var9, var11, var13, var19, var17, var18, 0, 0, 1.0D);
    255                }
    256            }
    257        }
    258    }