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