001package net.minecraft.world.gen;
002
003import java.util.List;
004import java.util.Random;
005import net.minecraft.block.Block;
006import net.minecraft.block.BlockSand;
007import net.minecraft.entity.EnumCreatureType;
008import net.minecraft.util.IProgressUpdate;
009import net.minecraft.util.MathHelper;
010import net.minecraft.world.ChunkPosition;
011import net.minecraft.world.World;
012import net.minecraft.world.biome.BiomeGenBase;
013import net.minecraft.world.chunk.Chunk;
014import net.minecraft.world.chunk.IChunkProvider;
015
016import net.minecraftforge.common.*;
017import net.minecraftforge.event.Event.*;
018import net.minecraftforge.event.terraingen.*;
019
020public class ChunkProviderEnd implements IChunkProvider
021{
022    private Random endRNG;
023    private NoiseGeneratorOctaves noiseGen1;
024    private NoiseGeneratorOctaves noiseGen2;
025    private NoiseGeneratorOctaves noiseGen3;
026    public NoiseGeneratorOctaves noiseGen4;
027    public NoiseGeneratorOctaves noiseGen5;
028    private World endWorld;
029    private double[] densities;
030
031    /** The biomes that are used to generate the chunk */
032    private BiomeGenBase[] biomesForGeneration;
033    double[] noiseData1;
034    double[] noiseData2;
035    double[] noiseData3;
036    double[] noiseData4;
037    double[] noiseData5;
038    int[][] field_73203_h = new int[32][32];
039
040    public ChunkProviderEnd(World par1World, long par2)
041    {
042        this.endWorld = par1World;
043        this.endRNG = new Random(par2);
044        this.noiseGen1 = new NoiseGeneratorOctaves(this.endRNG, 16);
045        this.noiseGen2 = new NoiseGeneratorOctaves(this.endRNG, 16);
046        this.noiseGen3 = new NoiseGeneratorOctaves(this.endRNG, 8);
047        this.noiseGen4 = new NoiseGeneratorOctaves(this.endRNG, 10);
048        this.noiseGen5 = new NoiseGeneratorOctaves(this.endRNG, 16);
049
050        NoiseGeneratorOctaves[] noiseGens = {noiseGen1, noiseGen2, noiseGen3, noiseGen4, noiseGen5};
051        noiseGens = TerrainGen.getModdedNoiseGenerators(par1World, this.endRNG, noiseGens);
052        this.noiseGen1 = noiseGens[0];
053        this.noiseGen2 = noiseGens[1];
054        this.noiseGen3 = noiseGens[2];
055        this.noiseGen4 = noiseGens[3];
056        this.noiseGen5 = noiseGens[4];
057   }
058
059    public void generateTerrain(int par1, int par2, byte[] par3ArrayOfByte, BiomeGenBase[] par4ArrayOfBiomeGenBase)
060    {
061        byte b0 = 2;
062        int k = b0 + 1;
063        byte b1 = 33;
064        int l = b0 + 1;
065        this.densities = this.initializeNoiseField(this.densities, par1 * b0, 0, par2 * b0, k, b1, l);
066
067        for (int i1 = 0; i1 < b0; ++i1)
068        {
069            for (int j1 = 0; j1 < b0; ++j1)
070            {
071                for (int k1 = 0; k1 < 32; ++k1)
072                {
073                    double d0 = 0.25D;
074                    double d1 = this.densities[((i1 + 0) * l + j1 + 0) * b1 + k1 + 0];
075                    double d2 = this.densities[((i1 + 0) * l + j1 + 1) * b1 + k1 + 0];
076                    double d3 = this.densities[((i1 + 1) * l + j1 + 0) * b1 + k1 + 0];
077                    double d4 = this.densities[((i1 + 1) * l + j1 + 1) * b1 + k1 + 0];
078                    double d5 = (this.densities[((i1 + 0) * l + j1 + 0) * b1 + k1 + 1] - d1) * d0;
079                    double d6 = (this.densities[((i1 + 0) * l + j1 + 1) * b1 + k1 + 1] - d2) * d0;
080                    double d7 = (this.densities[((i1 + 1) * l + j1 + 0) * b1 + k1 + 1] - d3) * d0;
081                    double d8 = (this.densities[((i1 + 1) * l + j1 + 1) * b1 + k1 + 1] - d4) * d0;
082
083                    for (int l1 = 0; l1 < 4; ++l1)
084                    {
085                        double d9 = 0.125D;
086                        double d10 = d1;
087                        double d11 = d2;
088                        double d12 = (d3 - d1) * d9;
089                        double d13 = (d4 - d2) * d9;
090
091                        for (int i2 = 0; i2 < 8; ++i2)
092                        {
093                            int j2 = i2 + i1 * 8 << 11 | 0 + j1 * 8 << 7 | k1 * 4 + l1;
094                            short short1 = 128;
095                            double d14 = 0.125D;
096                            double d15 = d10;
097                            double d16 = (d11 - d10) * d14;
098
099                            for (int k2 = 0; k2 < 8; ++k2)
100                            {
101                                int l2 = 0;
102
103                                if (d15 > 0.0D)
104                                {
105                                    l2 = Block.whiteStone.blockID;
106                                }
107
108                                par3ArrayOfByte[j2] = (byte)l2;
109                                j2 += short1;
110                                d15 += d16;
111                            }
112
113                            d10 += d12;
114                            d11 += d13;
115                        }
116
117                        d1 += d5;
118                        d2 += d6;
119                        d3 += d7;
120                        d4 += d8;
121                    }
122                }
123            }
124        }
125    }
126
127    public void replaceBlocksForBiome(int par1, int par2, byte[] par3ArrayOfByte, BiomeGenBase[] par4ArrayOfBiomeGenBase)
128    {
129        ChunkProviderEvent.ReplaceBiomeBlocks event = new ChunkProviderEvent.ReplaceBiomeBlocks(this, par1, par2, par3ArrayOfByte, par4ArrayOfBiomeGenBase);
130        MinecraftForge.EVENT_BUS.post(event);
131        if (event.getResult() == Result.DENY) return;
132
133        for (int k = 0; k < 16; ++k)
134        {
135            for (int l = 0; l < 16; ++l)
136            {
137                byte b0 = 1;
138                int i1 = -1;
139                byte b1 = (byte)Block.whiteStone.blockID;
140                byte b2 = (byte)Block.whiteStone.blockID;
141
142                for (int j1 = 127; j1 >= 0; --j1)
143                {
144                    int k1 = (l * 16 + k) * 128 + j1;
145                    byte b3 = par3ArrayOfByte[k1];
146
147                    if (b3 == 0)
148                    {
149                        i1 = -1;
150                    }
151                    else if (b3 == Block.stone.blockID)
152                    {
153                        if (i1 == -1)
154                        {
155                            if (b0 <= 0)
156                            {
157                                b1 = 0;
158                                b2 = (byte)Block.whiteStone.blockID;
159                            }
160
161                            i1 = b0;
162
163                            if (j1 >= 0)
164                            {
165                                par3ArrayOfByte[k1] = b1;
166                            }
167                            else
168                            {
169                                par3ArrayOfByte[k1] = b2;
170                            }
171                        }
172                        else if (i1 > 0)
173                        {
174                            --i1;
175                            par3ArrayOfByte[k1] = b2;
176                        }
177                    }
178                }
179            }
180        }
181    }
182
183    /**
184     * loads or generates the chunk at the chunk location specified
185     */
186    public Chunk loadChunk(int par1, int par2)
187    {
188        return this.provideChunk(par1, par2);
189    }
190
191    /**
192     * Will return back a chunk, if it doesn't exist and its not a MP client it will generates all the blocks for the
193     * specified chunk from the map seed and chunk seed
194     */
195    public Chunk provideChunk(int par1, int par2)
196    {
197        this.endRNG.setSeed((long)par1 * 341873128712L + (long)par2 * 132897987541L);
198        byte[] abyte = new byte[32768];
199        this.biomesForGeneration = this.endWorld.getWorldChunkManager().loadBlockGeneratorData(this.biomesForGeneration, par1 * 16, par2 * 16, 16, 16);
200        this.generateTerrain(par1, par2, abyte, this.biomesForGeneration);
201        this.replaceBlocksForBiome(par1, par2, abyte, this.biomesForGeneration);
202        Chunk chunk = new Chunk(this.endWorld, abyte, par1, par2);
203        byte[] abyte1 = chunk.getBiomeArray();
204
205        for (int k = 0; k < abyte1.length; ++k)
206        {
207            abyte1[k] = (byte)this.biomesForGeneration[k].biomeID;
208        }
209
210        chunk.generateSkylightMap();
211        return chunk;
212    }
213
214    /**
215     * generates a subset of the level's terrain data. Takes 7 arguments: the [empty] noise array, the position, and the
216     * size.
217     */
218    private double[] initializeNoiseField(double[] par1ArrayOfDouble, int par2, int par3, int par4, int par5, int par6, int par7)
219    {
220        ChunkProviderEvent.InitNoiseField event = new ChunkProviderEvent.InitNoiseField(this, par1ArrayOfDouble, par2, par3, par4, par5, par6, par7);
221        MinecraftForge.EVENT_BUS.post(event);
222        if (event.getResult() == Result.DENY) return event.noisefield;
223
224        if (par1ArrayOfDouble == null)
225        {
226            par1ArrayOfDouble = new double[par5 * par6 * par7];
227        }
228
229        double d0 = 684.412D;
230        double d1 = 684.412D;
231        this.noiseData4 = this.noiseGen4.generateNoiseOctaves(this.noiseData4, par2, par4, par5, par7, 1.121D, 1.121D, 0.5D);
232        this.noiseData5 = this.noiseGen5.generateNoiseOctaves(this.noiseData5, par2, par4, par5, par7, 200.0D, 200.0D, 0.5D);
233        d0 *= 2.0D;
234        this.noiseData1 = this.noiseGen3.generateNoiseOctaves(this.noiseData1, par2, par3, par4, par5, par6, par7, d0 / 80.0D, d1 / 160.0D, d0 / 80.0D);
235        this.noiseData2 = this.noiseGen1.generateNoiseOctaves(this.noiseData2, par2, par3, par4, par5, par6, par7, d0, d1, d0);
236        this.noiseData3 = this.noiseGen2.generateNoiseOctaves(this.noiseData3, par2, par3, par4, par5, par6, par7, d0, d1, d0);
237        int k1 = 0;
238        int l1 = 0;
239
240        for (int i2 = 0; i2 < par5; ++i2)
241        {
242            for (int j2 = 0; j2 < par7; ++j2)
243            {
244                double d2 = (this.noiseData4[l1] + 256.0D) / 512.0D;
245
246                if (d2 > 1.0D)
247                {
248                    d2 = 1.0D;
249                }
250
251                double d3 = this.noiseData5[l1] / 8000.0D;
252
253                if (d3 < 0.0D)
254                {
255                    d3 = -d3 * 0.3D;
256                }
257
258                d3 = d3 * 3.0D - 2.0D;
259                float f = (float)(i2 + par2 - 0) / 1.0F;
260                float f1 = (float)(j2 + par4 - 0) / 1.0F;
261                float f2 = 100.0F - MathHelper.sqrt_float(f * f + f1 * f1) * 8.0F;
262
263                if (f2 > 80.0F)
264                {
265                    f2 = 80.0F;
266                }
267
268                if (f2 < -100.0F)
269                {
270                    f2 = -100.0F;
271                }
272
273                if (d3 > 1.0D)
274                {
275                    d3 = 1.0D;
276                }
277
278                d3 /= 8.0D;
279                d3 = 0.0D;
280
281                if (d2 < 0.0D)
282                {
283                    d2 = 0.0D;
284                }
285
286                d2 += 0.5D;
287                d3 = d3 * (double)par6 / 16.0D;
288                ++l1;
289                double d4 = (double)par6 / 2.0D;
290
291                for (int k2 = 0; k2 < par6; ++k2)
292                {
293                    double d5 = 0.0D;
294                    double d6 = ((double)k2 - d4) * 8.0D / d2;
295
296                    if (d6 < 0.0D)
297                    {
298                        d6 *= -1.0D;
299                    }
300
301                    double d7 = this.noiseData2[k1] / 512.0D;
302                    double d8 = this.noiseData3[k1] / 512.0D;
303                    double d9 = (this.noiseData1[k1] / 10.0D + 1.0D) / 2.0D;
304
305                    if (d9 < 0.0D)
306                    {
307                        d5 = d7;
308                    }
309                    else if (d9 > 1.0D)
310                    {
311                        d5 = d8;
312                    }
313                    else
314                    {
315                        d5 = d7 + (d8 - d7) * d9;
316                    }
317
318                    d5 -= 8.0D;
319                    d5 += (double)f2;
320                    byte b0 = 2;
321                    double d10;
322
323                    if (k2 > par6 / 2 - b0)
324                    {
325                        d10 = (double)((float)(k2 - (par6 / 2 - b0)) / 64.0F);
326
327                        if (d10 < 0.0D)
328                        {
329                            d10 = 0.0D;
330                        }
331
332                        if (d10 > 1.0D)
333                        {
334                            d10 = 1.0D;
335                        }
336
337                        d5 = d5 * (1.0D - d10) + -3000.0D * d10;
338                    }
339
340                    b0 = 8;
341
342                    if (k2 < b0)
343                    {
344                        d10 = (double)((float)(b0 - k2) / ((float)b0 - 1.0F));
345                        d5 = d5 * (1.0D - d10) + -30.0D * d10;
346                    }
347
348                    par1ArrayOfDouble[k1] = d5;
349                    ++k1;
350                }
351            }
352        }
353
354        return par1ArrayOfDouble;
355    }
356
357    /**
358     * Checks to see if a chunk exists at x, y
359     */
360    public boolean chunkExists(int par1, int par2)
361    {
362        return true;
363    }
364
365    /**
366     * Populates chunk with ores etc etc
367     */
368    public void populate(IChunkProvider par1IChunkProvider, int par2, int par3)
369    {
370        BlockSand.fallInstantly = true;
371
372        MinecraftForge.EVENT_BUS.post(new PopulateChunkEvent.Pre(par1IChunkProvider, endWorld, endWorld.rand, par2, par3, false));
373
374        int k = par2 * 16;
375        int l = par3 * 16;
376        BiomeGenBase biomegenbase = this.endWorld.getBiomeGenForCoords(k + 16, l + 16);
377        biomegenbase.decorate(this.endWorld, this.endWorld.rand, k, l);
378
379        MinecraftForge.EVENT_BUS.post(new PopulateChunkEvent.Post(par1IChunkProvider, endWorld, endWorld.rand, par2, par3, false));
380
381        BlockSand.fallInstantly = false;
382    }
383
384    /**
385     * Two modes of operation: if passed true, save all Chunks in one go.  If passed false, save up to two chunks.
386     * Return true if all chunks have been saved.
387     */
388    public boolean saveChunks(boolean par1, IProgressUpdate par2IProgressUpdate)
389    {
390        return true;
391    }
392
393    /**
394     * Unloads chunks that are marked to be unloaded. This is not guaranteed to unload every such chunk.
395     */
396    public boolean unloadQueuedChunks()
397    {
398        return false;
399    }
400
401    /**
402     * Returns if the IChunkProvider supports saving.
403     */
404    public boolean canSave()
405    {
406        return true;
407    }
408
409    /**
410     * Converts the instance data to a readable string.
411     */
412    public String makeString()
413    {
414        return "RandomLevelSource";
415    }
416
417    /**
418     * Returns a list of creatures of the specified type that can spawn at the given location.
419     */
420    public List getPossibleCreatures(EnumCreatureType par1EnumCreatureType, int par2, int par3, int par4)
421    {
422        BiomeGenBase biomegenbase = this.endWorld.getBiomeGenForCoords(par2, par4);
423        return biomegenbase == null ? null : biomegenbase.getSpawnableList(par1EnumCreatureType);
424    }
425
426    /**
427     * Returns the location of the closest structure of the specified type. If not found returns null.
428     */
429    public ChunkPosition findClosestStructure(World par1World, String par2Str, int par3, int par4, int par5)
430    {
431        return null;
432    }
433
434    public int getLoadedChunkCount()
435    {
436        return 0;
437    }
438
439    public void recreateStructures(int par1, int par2) {}
440}