    001    package net.minecraft.world.gen.feature;
    002
    003    import java.util.Random;
    004    import net.minecraft.block.Block;
    005    import net.minecraft.util.Direction;
    006    import net.minecraft.world.World;
    007
    008    public class WorldGenTrees extends WorldGenerator
    009    {
    010        /** The minimum height of a generated tree. */
    011        private final int minTreeHeight;
    012
    013        /** True if this tree should grow Vines. */
    014        private final boolean vinesGrow;
    015
    016        /** The metadata value of the wood to use in tree generation. */
    017        private final int metaWood;
    018
    019        /** The metadata value of the leaves to use in tree generation. */
    020        private final int metaLeaves;
    021
    022        public WorldGenTrees(boolean par1)
    023        {
    024            this(par1, 4, 0, 0, false);
    025        }
    026
    027        public WorldGenTrees(boolean par1, int par2, int par3, int par4, boolean par5)
    028        {
    029            super(par1);
    030            this.minTreeHeight = par2;
    031            this.metaWood = par3;
    032            this.metaLeaves = par4;
    033            this.vinesGrow = par5;
    034        }
    035
    036        public boolean generate(World par1World, Random par2Random, int par3, int par4, int par5)
    037        {
    038            int var6 = par2Random.nextInt(3) + this.minTreeHeight;
    039            boolean var7 = true;
    040
    041            if (par4 >= 1 && par4 + var6 + 1 <= 256)
    042            {
    043                int var8;
    044                byte var9;
    045                int var11;
    046                int var12;
    047
    048                for (var8 = par4; var8 <= par4 + 1 + var6; ++var8)
    049                {
    050                    var9 = 1;
    051
    052                    if (var8 == par4)
    053                    {
    054                        var9 = 0;
    055                    }
    056
    057                    if (var8 >= par4 + 1 + var6 - 2)
    058                    {
    059                        var9 = 2;
    060                    }
    061
    062                    for (int var10 = par3 - var9; var10 <= par3 + var9 && var7; ++var10)
    063                    {
    064                        for (var11 = par5 - var9; var11 <= par5 + var9 && var7; ++var11)
    065                        {
    066                            if (var8 >= 0 && var8 < 256)
    067                            {
    068                                var12 = par1World.getBlockId(var10, var8, var11);
    069
    070                                Block block = Block.blocksList[var12];
    071
    072                                if (var12 != 0 &&
    073                                   !block.isLeaves(par1World, var10, var8, var11) &&
    074                                    var12 != Block.grass.blockID &&
    075                                    var12 != Block.dirt.blockID &&
    076                                   !block.isWood(par1World, var10, var8, var11))
    077                                {
    078                                    var7 = false;
    079                                }
    080                            }
    081                            else
    082                            {
    083                                var7 = false;
    084                            }
    085                        }
    086                    }
    087                }
    088
    089                if (!var7)
    090                {
    091                    return false;
    092                }
    093                else
    094                {
    095                    var8 = par1World.getBlockId(par3, par4 - 1, par5);
    096
    097                    if ((var8 == Block.grass.blockID || var8 == Block.dirt.blockID) && par4 < 256 - var6 - 1)
    098                    {
    099                        this.setBlock(par1World, par3, par4 - 1, par5, Block.dirt.blockID);
    100                        var9 = 3;
    101                        byte var18 = 0;
    102                        int var13;
    103                        int var14;
    104                        int var15;
    105
    106                        for (var11 = par4 - var9 + var6; var11 <= par4 + var6; ++var11)
    107                        {
    108                            var12 = var11 - (par4 + var6);
    109                            var13 = var18 + 1 - var12 / 2;
    110
    111                            for (var14 = par3 - var13; var14 <= par3 + var13; ++var14)
    112                            {
    113                                var15 = var14 - par3;
    114
    115                                for (int var16 = par5 - var13; var16 <= par5 + var13; ++var16)
    116                                {
    117                                    int var17 = var16 - par5;
    118
    119                                    Block block = Block.blocksList[par1World.getBlockId(var14, var11, var16)];
    120
    121                                    if ((Math.abs(var15) != var13 || Math.abs(var17) != var13 || par2Random.nextInt(2) != 0 && var12 != 0) &&
    122                                        (block == null || block.canBeReplacedByLeaves(par1World, var14, var11, var16)))
    123                                    {
    124                                        this.setBlockAndMetadata(par1World, var14, var11, var16, Block.leaves.blockID, this.metaLeaves);
    125                                    }
    126                                }
    127                            }
    128                        }
    129
    130                        for (var11 = 0; var11 < var6; ++var11)
    131                        {
    132                            var12 = par1World.getBlockId(par3, par4 + var11, par5);
    133
    134                            Block block = Block.blocksList[var12];
    135
    136                            if (var12 == 0 || block == null || block.isLeaves(par1World, par3, par4 + var11, par5))
    137                            {
    138                                this.setBlockAndMetadata(par1World, par3, par4 + var11, par5, Block.wood.blockID, this.metaWood);
    139
    140                                if (this.vinesGrow && var11 > 0)
    141                                {
    142                                    if (par2Random.nextInt(3) > 0 && par1World.isAirBlock(par3 - 1, par4 + var11, par5))
    143                                    {
    144                                        this.setBlockAndMetadata(par1World, par3 - 1, par4 + var11, par5, Block.vine.blockID, 8);
    145                                    }
    146
    147                                    if (par2Random.nextInt(3) > 0 && par1World.isAirBlock(par3 + 1, par4 + var11, par5))
    148                                    {
    149                                        this.setBlockAndMetadata(par1World, par3 + 1, par4 + var11, par5, Block.vine.blockID, 2);
    150                                    }
    151
    152                                    if (par2Random.nextInt(3) > 0 && par1World.isAirBlock(par3, par4 + var11, par5 - 1))
    153                                    {
    154                                        this.setBlockAndMetadata(par1World, par3, par4 + var11, par5 - 1, Block.vine.blockID, 1);
    155                                    }
    156
    157                                    if (par2Random.nextInt(3) > 0 && par1World.isAirBlock(par3, par4 + var11, par5 + 1))
    158                                    {
    159                                        this.setBlockAndMetadata(par1World, par3, par4 + var11, par5 + 1, Block.vine.blockID, 4);
    160                                    }
    161                                }
    162                            }
    163                        }
    164
    165                        if (this.vinesGrow)
    166                        {
    167                            for (var11 = par4 - 3 + var6; var11 <= par4 + var6; ++var11)
    168                            {
    169                                var12 = var11 - (par4 + var6);
    170                                var13 = 2 - var12 / 2;
    171
    172                                for (var14 = par3 - var13; var14 <= par3 + var13; ++var14)
    173                                {
    174                                    for (var15 = par5 - var13; var15 <= par5 + var13; ++var15)
    175                                    {
    176                                        Block block = Block.blocksList[par1World.getBlockId(var14, var11, var15)];
    177                                        if (block != null && block.isLeaves(par1World, var14, var11, var15))
    178                                        {
    179                                            if (par2Random.nextInt(4) == 0 && par1World.getBlockId(var14 - 1, var11, var15) == 0)
    180                                            {
    181                                                this.growVines(par1World, var14 - 1, var11, var15, 8);
    182                                            }
    183
    184                                            if (par2Random.nextInt(4) == 0 && par1World.getBlockId(var14 + 1, var11, var15) == 0)
    185                                            {
    186                                                this.growVines(par1World, var14 + 1, var11, var15, 2);
    187                                            }
    188
    189                                            if (par2Random.nextInt(4) == 0 && par1World.getBlockId(var14, var11, var15 - 1) == 0)
    190                                            {
    191                                                this.growVines(par1World, var14, var11, var15 - 1, 1);
    192                                            }
    193
    194                                            if (par2Random.nextInt(4) == 0 && par1World.getBlockId(var14, var11, var15 + 1) == 0)
    195                                            {
    196                                                this.growVines(par1World, var14, var11, var15 + 1, 4);
    197                                            }
    198                                        }
    199                                    }
    200                                }
    201                            }
    202
    203                            if (par2Random.nextInt(5) == 0 && var6 > 5)
    204                            {
    205                                for (var11 = 0; var11 < 2; ++var11)
    206                                {
    207                                    for (var12 = 0; var12 < 4; ++var12)
    208                                    {
    209                                        if (par2Random.nextInt(4 - var11) == 0)
    210                                        {
    211                                            var13 = par2Random.nextInt(3);
    212                                            this.setBlockAndMetadata(par1World, par3 + Direction.offsetX[Direction.footInvisibleFaceRemap[var12]], par4 + var6 - 5 + var11, par5 + Direction.offsetZ[Direction.footInvisibleFaceRemap[var12]], Block.cocoaPlant.blockID, var13 << 2 | var12);
    213                                        }
    214                                    }
    215                                }
    216                            }
    217                        }
    218
    219                        return true;
    220                    }
    221                    else
    222                    {
    223                        return false;
    224                    }
    225                }
    226            }
    227            else
    228            {
    229                return false;
    230            }
    231        }
    232
    233        /**
    234         * Grows vines downward from the given block for a given length. Args: World, x, starty, z, vine-length
    235         */
    236        private void growVines(World par1World, int par2, int par3, int par4, int par5)
    237        {
    238            this.setBlockAndMetadata(par1World, par2, par3, par4, Block.vine.blockID, par5);
    239            int var6 = 4;
    240
    241            while (true)
    242            {
    243                --par3;
    244
    245                if (par1World.getBlockId(par2, par3, par4) != 0 || var6 <= 0)
    246                {
    247                    return;
    248                }
    249
    250                this.setBlockAndMetadata(par1World, par2, par3, par4, Block.vine.blockID, par5);
    251                --var6;
    252            }
    253        }
    254    }