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