    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 var5 = 2;
    062        int var6 = var5 + 1;
    063        byte var7 = 33;
    064        int var8 = var5 + 1;
    065        this.densities = this.initializeNoiseField(this.densities, par1 * var5, 0, par2 * var5, var6, var7, var8);
    066
    067        for (int var9 = 0; var9 < var5; ++var9)
    068        {
    069            for (int var10 = 0; var10 < var5; ++var10)
    070            {
    071                for (int var11 = 0; var11 < 32; ++var11)
    072                {
    073                    double var12 = 0.25D;
    074                    double var14 = this.densities[((var9 + 0) * var8 + var10 + 0) * var7 + var11 + 0];
    075                    double var16 = this.densities[((var9 + 0) * var8 + var10 + 1) * var7 + var11 + 0];
    076                    double var18 = this.densities[((var9 + 1) * var8 + var10 + 0) * var7 + var11 + 0];
    077                    double var20 = this.densities[((var9 + 1) * var8 + var10 + 1) * var7 + var11 + 0];
    078                    double var22 = (this.densities[((var9 + 0) * var8 + var10 + 0) * var7 + var11 + 1] - var14) * var12;
    079                    double var24 = (this.densities[((var9 + 0) * var8 + var10 + 1) * var7 + var11 + 1] - var16) * var12;
    080                    double var26 = (this.densities[((var9 + 1) * var8 + var10 + 0) * var7 + var11 + 1] - var18) * var12;
    081                    double var28 = (this.densities[((var9 + 1) * var8 + var10 + 1) * var7 + var11 + 1] - var20) * var12;
    082
    083                    for (int var30 = 0; var30 < 4; ++var30)
    084                    {
    085                        double var31 = 0.125D;
    086                        double var33 = var14;
    087                        double var35 = var16;
    088                        double var37 = (var18 - var14) * var31;
    089                        double var39 = (var20 - var16) * var31;
    090
    091                        for (int var41 = 0; var41 < 8; ++var41)
    092                        {
    093                            int var42 = var41 + var9 * 8 << 11 | 0 + var10 * 8 << 7 | var11 * 4 + var30;
    094                            short var43 = 128;
    095                            double var44 = 0.125D;
    096                            double var46 = var33;
    097                            double var48 = (var35 - var33) * var44;
    098
    099                            for (int var50 = 0; var50 < 8; ++var50)
    100                            {
    101                                int var51 = 0;
    102
    103                                if (var46 > 0.0D)
    104                                {
    105                                    var51 = Block.whiteStone.blockID;
    106                                }
    107
    108                                par3ArrayOfByte[var42] = (byte)var51;
    109                                var42 += var43;
    110                                var46 += var48;
    111                            }
    112
    113                            var33 += var37;
    114                            var35 += var39;
    115                        }
    116
    117                        var14 += var22;
    118                        var16 += var24;
    119                        var18 += var26;
    120                        var20 += var28;
    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 var5 = 0; var5 < 16; ++var5)
    134        {
    135            for (int var6 = 0; var6 < 16; ++var6)
    136            {
    137                byte var7 = 1;
    138                int var8 = -1;
    139                byte var9 = (byte)Block.whiteStone.blockID;
    140                byte var10 = (byte)Block.whiteStone.blockID;
    141
    142                for (int var11 = 127; var11 >= 0; --var11)
    143                {
    144                    int var12 = (var6 * 16 + var5) * 128 + var11;
    145                    byte var13 = par3ArrayOfByte[var12];
    146
    147                    if (var13 == 0)
    148                    {
    149                        var8 = -1;
    150                    }
    151                    else if (var13 == Block.stone.blockID)
    152                    {
    153                        if (var8 == -1)
    154                        {
    155                            if (var7 <= 0)
    156                            {
    157                                var9 = 0;
    158                                var10 = (byte)Block.whiteStone.blockID;
    159                            }
    160
    161                            var8 = var7;
    162
    163                            if (var11 >= 0)
    164                            {
    165                                par3ArrayOfByte[var12] = var9;
    166                            }
    167                            else
    168                            {
    169                                par3ArrayOfByte[var12] = var10;
    170                            }
    171                        }
    172                        else if (var8 > 0)
    173                        {
    174                            --var8;
    175                            par3ArrayOfByte[var12] = var10;
    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[] var3 = new byte[32768];
    199        this.biomesForGeneration = this.endWorld.getWorldChunkManager().loadBlockGeneratorData(this.biomesForGeneration, par1 * 16, par2 * 16, 16, 16);
    200        this.generateTerrain(par1, par2, var3, this.biomesForGeneration);
    201        this.replaceBlocksForBiome(par1, par2, var3, this.biomesForGeneration);
    202        Chunk var4 = new Chunk(this.endWorld, var3, par1, par2);
    203        byte[] var5 = var4.getBiomeArray();
    204
    205        for (int var6 = 0; var6 < var5.length; ++var6)
    206        {
    207            var5[var6] = (byte)this.biomesForGeneration[var6].biomeID;
    208        }
    209
    210        var4.generateSkylightMap();
    211        return var4;
    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 var8 = 684.412D;
    230        double var10 = 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        var8 *= 2.0D;
    234        this.noiseData1 = this.noiseGen3.generateNoiseOctaves(this.noiseData1, par2, par3, par4, par5, par6, par7, var8 / 80.0D, var10 / 160.0D, var8 / 80.0D);
    235        this.noiseData2 = this.noiseGen1.generateNoiseOctaves(this.noiseData2, par2, par3, par4, par5, par6, par7, var8, var10, var8);
    236        this.noiseData3 = this.noiseGen2.generateNoiseOctaves(this.noiseData3, par2, par3, par4, par5, par6, par7, var8, var10, var8);
    237        int var12 = 0;
    238        int var13 = 0;
    239
    240        for (int var14 = 0; var14 < par5; ++var14)
    241        {
    242            for (int var15 = 0; var15 < par7; ++var15)
    243            {
    244                double var16 = (this.noiseData4[var13] + 256.0D) / 512.0D;
    245
    246                if (var16 > 1.0D)
    247                {
    248                    var16 = 1.0D;
    249                }
    250
    251                double var18 = this.noiseData5[var13] / 8000.0D;
    252
    253                if (var18 < 0.0D)
    254                {
    255                    var18 = -var18 * 0.3D;
    256                }
    257
    258                var18 = var18 * 3.0D - 2.0D;
    259                float var20 = (float)(var14 + par2 - 0) / 1.0F;
    260                float var21 = (float)(var15 + par4 - 0) / 1.0F;
    261                float var22 = 100.0F - MathHelper.sqrt_float(var20 * var20 + var21 * var21) * 8.0F;
    262
    263                if (var22 > 80.0F)
    264                {
    265                    var22 = 80.0F;
    266                }
    267
    268                if (var22 < -100.0F)
    269                {
    270                    var22 = -100.0F;
    271                }
    272
    273                if (var18 > 1.0D)
    274                {
    275                    var18 = 1.0D;
    276                }
    277
    278                var18 /= 8.0D;
    279                var18 = 0.0D;
    280
    281                if (var16 < 0.0D)
    282                {
    283                    var16 = 0.0D;
    284                }
    285
    286                var16 += 0.5D;
    287                var18 = var18 * (double)par6 / 16.0D;
    288                ++var13;
    289                double var23 = (double)par6 / 2.0D;
    290
    291                for (int var25 = 0; var25 < par6; ++var25)
    292                {
    293                    double var26 = 0.0D;
    294                    double var28 = ((double)var25 - var23) * 8.0D / var16;
    295
    296                    if (var28 < 0.0D)
    297                    {
    298                        var28 *= -1.0D;
    299                    }
    300
    301                    double var30 = this.noiseData2[var12] / 512.0D;
    302                    double var32 = this.noiseData3[var12] / 512.0D;
    303                    double var34 = (this.noiseData1[var12] / 10.0D + 1.0D) / 2.0D;
    304
    305                    if (var34 < 0.0D)
    306                    {
    307                        var26 = var30;
    308                    }
    309                    else if (var34 > 1.0D)
    310                    {
    311                        var26 = var32;
    312                    }
    313                    else
    314                    {
    315                        var26 = var30 + (var32 - var30) * var34;
    316                    }
    317
    318                    var26 -= 8.0D;
    319                    var26 += (double)var22;
    320                    byte var36 = 2;
    321                    double var37;
    322
    323                    if (var25 > par6 / 2 - var36)
    324                    {
    325                        var37 = (double)((float)(var25 - (par6 / 2 - var36)) / 64.0F);
    326
    327                        if (var37 < 0.0D)
    328                        {
    329                            var37 = 0.0D;
    330                        }
    331
    332                        if (var37 > 1.0D)
    333                        {
    334                            var37 = 1.0D;
    335                        }
    336
    337                        var26 = var26 * (1.0D - var37) + -3000.0D * var37;
    338                    }
    339
    340                    var36 = 8;
    341
    342                    if (var25 < var36)
    343                    {
    344                        var37 = (double)((float)(var36 - var25) / ((float)var36 - 1.0F));
    345                        var26 = var26 * (1.0D - var37) + -30.0D * var37;
    346                    }
    347
    348                    par1ArrayOfDouble[var12] = var26;
    349                    ++var12;
    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 var4 = par2 * 16;
    375        int var5 = par3 * 16;
    376        BiomeGenBase var6 = this.endWorld.getBiomeGenForCoords(var4 + 16, var5 + 16);
    377        var6.decorate(this.endWorld, this.endWorld.rand, var4, var5);
    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 the 100 oldest chunks from memory, due to a bug with chunkSet.add() never being called it thinks the list
    395     * is always empty and will not remove any chunks.
    396     */
    397    public boolean unload100OldestChunks()
    398    {
    399        return false;
    400    }
    401
    402    /**
    403     * Returns if the IChunkProvider supports saving.
    404     */
    405    public boolean canSave()
    406    {
    407        return true;
    408    }
    409
    410    /**
    411     * Converts the instance data to a readable string.
    412     */
    413    public String makeString()
    414    {
    415        return "RandomLevelSource";
    416    }
    417
    418    /**
    419     * Returns a list of creatures of the specified type that can spawn at the given location.
    420     */
    421    public List getPossibleCreatures(EnumCreatureType par1EnumCreatureType, int par2, int par3, int par4)
    422    {
    423        BiomeGenBase var5 = this.endWorld.getBiomeGenForCoords(par2, par4);
    424        return var5 == null ? null : var5.getSpawnableList(par1EnumCreatureType);
    425    }
    426
    427    /**
    428     * Returns the location of the closest structure of the specified type. If not found returns null.
    429     */
    430    public ChunkPosition findClosestStructure(World par1World, String par2Str, int par3, int par4, int par5)
    431    {
    432        return null;
    433    }
    434
    435    public int getLoadedChunkCount()
    436    {
    437        return 0;
    438    }
    439
    440    public void recreateStructures(int par1, int par2) {}
    441}