    001    package net.minecraft.src;
    002
    003    import java.util.List;
    004    import java.util.Random;
    005
    006    public class ChunkProviderGenerate implements IChunkProvider
    007    {
    008        /** RNG. */
    009        private Random rand;
    010
    011        /** A NoiseGeneratorOctaves used in generating terrain */
    012        private NoiseGeneratorOctaves noiseGen1;
    013
    014        /** A NoiseGeneratorOctaves used in generating terrain */
    015        private NoiseGeneratorOctaves noiseGen2;
    016
    017        /** A NoiseGeneratorOctaves used in generating terrain */
    018        private NoiseGeneratorOctaves noiseGen3;
    019
    020        /** A NoiseGeneratorOctaves used in generating terrain */
    021        private NoiseGeneratorOctaves noiseGen4;
    022
    023        /** A NoiseGeneratorOctaves used in generating terrain */
    024        public NoiseGeneratorOctaves noiseGen5;
    025
    026        /** A NoiseGeneratorOctaves used in generating terrain */
    027        public NoiseGeneratorOctaves noiseGen6;
    028        public NoiseGeneratorOctaves mobSpawnerNoise;
    029
    030        /** Reference to the World object. */
    031        private World worldObj;
    032
    033        /** are map structures going to be generated (e.g. strongholds) */
    034        private final boolean mapFeaturesEnabled;
    035
    036        /** Holds the overall noise array used in chunk generation */
    037        private double[] noiseArray;
    038        private double[] stoneNoise = new double[256];
    039        private MapGenBase caveGenerator = new MapGenCaves();
    040
    041        /** Holds Stronghold Generator */
    042        private MapGenStronghold strongholdGenerator = new MapGenStronghold();
    043
    044        /** Holds Village Generator */
    045        private MapGenVillage villageGenerator = new MapGenVillage(0);
    046
    047        /** Holds Mineshaft Generator */
    048        private MapGenMineshaft mineshaftGenerator = new MapGenMineshaft();
    049        private MapGenScatteredFeature scatteredFeatureGenerator = new MapGenScatteredFeature();
    050
    051        /** Holds ravine generator */
    052        private MapGenBase ravineGenerator = new MapGenRavine();
    053
    054        /** The biomes that are used to generate the chunk */
    055        private BiomeGenBase[] biomesForGeneration;
    056
    057        /** A double array that hold terrain noise from noiseGen3 */
    058        double[] noise3;
    059
    060        /** A double array that hold terrain noise */
    061        double[] noise1;
    062
    063        /** A double array that hold terrain noise from noiseGen2 */
    064        double[] noise2;
    065
    066        /** A double array that hold terrain noise from noiseGen5 */
    067        double[] noise5;
    068
    069        /** A double array that holds terrain noise from noiseGen6 */
    070        double[] noise6;
    071
    072        /**
    073         * Used to store the 5x5 parabolic field that is used during terrain generation.
    074         */
    075        float[] parabolicField;
    076        int[][] field_73219_j = new int[32][32];
    077
    078        public ChunkProviderGenerate(World par1World, long par2, boolean par4)
    079        {
    080            this.worldObj = par1World;
    081            this.mapFeaturesEnabled = par4;
    082            this.rand = new Random(par2);
    083            this.noiseGen1 = new NoiseGeneratorOctaves(this.rand, 16);
    084            this.noiseGen2 = new NoiseGeneratorOctaves(this.rand, 16);
    085            this.noiseGen3 = new NoiseGeneratorOctaves(this.rand, 8);
    086            this.noiseGen4 = new NoiseGeneratorOctaves(this.rand, 4);
    087            this.noiseGen5 = new NoiseGeneratorOctaves(this.rand, 10);
    088            this.noiseGen6 = new NoiseGeneratorOctaves(this.rand, 16);
    089            this.mobSpawnerNoise = new NoiseGeneratorOctaves(this.rand, 8);
    090        }
    091
    092        /**
    093         * Generates the shape of the terrain for the chunk though its all stone though the water is frozen if the
    094         * temperature is low enough
    095         */
    096        public void generateTerrain(int par1, int par2, byte[] par3ArrayOfByte)
    097        {
    098            byte var4 = 4;
    099            byte var5 = 16;
    100            byte var6 = 63;
    101            int var7 = var4 + 1;
    102            byte var8 = 17;
    103            int var9 = var4 + 1;
    104            this.biomesForGeneration = this.worldObj.getWorldChunkManager().getBiomesForGeneration(this.biomesForGeneration, par1 * 4 - 2, par2 * 4 - 2, var7 + 5, var9 + 5);
    105            this.noiseArray = this.initializeNoiseField(this.noiseArray, par1 * var4, 0, par2 * var4, var7, var8, var9);
    106
    107            for (int var10 = 0; var10 < var4; ++var10)
    108            {
    109                for (int var11 = 0; var11 < var4; ++var11)
    110                {
    111                    for (int var12 = 0; var12 < var5; ++var12)
    112                    {
    113                        double var13 = 0.125D;
    114                        double var15 = this.noiseArray[((var10 + 0) * var9 + var11 + 0) * var8 + var12 + 0];
    115                        double var17 = this.noiseArray[((var10 + 0) * var9 + var11 + 1) * var8 + var12 + 0];
    116                        double var19 = this.noiseArray[((var10 + 1) * var9 + var11 + 0) * var8 + var12 + 0];
    117                        double var21 = this.noiseArray[((var10 + 1) * var9 + var11 + 1) * var8 + var12 + 0];
    118                        double var23 = (this.noiseArray[((var10 + 0) * var9 + var11 + 0) * var8 + var12 + 1] - var15) * var13;
    119                        double var25 = (this.noiseArray[((var10 + 0) * var9 + var11 + 1) * var8 + var12 + 1] - var17) * var13;
    120                        double var27 = (this.noiseArray[((var10 + 1) * var9 + var11 + 0) * var8 + var12 + 1] - var19) * var13;
    121                        double var29 = (this.noiseArray[((var10 + 1) * var9 + var11 + 1) * var8 + var12 + 1] - var21) * var13;
    122
    123                        for (int var31 = 0; var31 < 8; ++var31)
    124                        {
    125                            double var32 = 0.25D;
    126                            double var34 = var15;
    127                            double var36 = var17;
    128                            double var38 = (var19 - var15) * var32;
    129                            double var40 = (var21 - var17) * var32;
    130
    131                            for (int var42 = 0; var42 < 4; ++var42)
    132                            {
    133                                int var43 = var42 + var10 * 4 << 11 | 0 + var11 * 4 << 7 | var12 * 8 + var31;
    134                                short var44 = 128;
    135                                var43 -= var44;
    136                                double var45 = 0.25D;
    137                                double var49 = (var36 - var34) * var45;
    138                                double var47 = var34 - var49;
    139
    140                                for (int var51 = 0; var51 < 4; ++var51)
    141                                {
    142                                    if ((var47 += var49) > 0.0D)
    143                                    {
    144                                        par3ArrayOfByte[var43 += var44] = (byte)Block.stone.blockID;
    145                                    }
    146                                    else if (var12 * 8 + var31 < var6)
    147                                    {
    148                                        par3ArrayOfByte[var43 += var44] = (byte)Block.waterStill.blockID;
    149                                    }
    150                                    else
    151                                    {
    152                                        par3ArrayOfByte[var43 += var44] = 0;
    153                                    }
    154                                }
    155
    156                                var34 += var38;
    157                                var36 += var40;
    158                            }
    159
    160                            var15 += var23;
    161                            var17 += var25;
    162                            var19 += var27;
    163                            var21 += var29;
    164                        }
    165                    }
    166                }
    167            }
    168        }
    169
    170        /**
    171         * Replaces the stone that was placed in with blocks that match the biome
    172         */
    173        public void replaceBlocksForBiome(int par1, int par2, byte[] par3ArrayOfByte, BiomeGenBase[] par4ArrayOfBiomeGenBase)
    174        {
    175            byte var5 = 63;
    176            double var6 = 0.03125D;
    177            this.stoneNoise = this.noiseGen4.generateNoiseOctaves(this.stoneNoise, par1 * 16, par2 * 16, 0, 16, 16, 1, var6 * 2.0D, var6 * 2.0D, var6 * 2.0D);
    178
    179            for (int var8 = 0; var8 < 16; ++var8)
    180            {
    181                for (int var9 = 0; var9 < 16; ++var9)
    182                {
    183                    BiomeGenBase var10 = par4ArrayOfBiomeGenBase[var9 + var8 * 16];
    184                    float var11 = var10.getFloatTemperature();
    185                    int var12 = (int)(this.stoneNoise[var8 + var9 * 16] / 3.0D + 3.0D + this.rand.nextDouble() * 0.25D);
    186                    int var13 = -1;
    187                    byte var14 = var10.topBlock;
    188                    byte var15 = var10.fillerBlock;
    189
    190                    for (int var16 = 127; var16 >= 0; --var16)
    191                    {
    192                        int var17 = (var9 * 16 + var8) * 128 + var16;
    193
    194                        if (var16 <= 0 + this.rand.nextInt(5))
    195                        {
    196                            par3ArrayOfByte[var17] = (byte)Block.bedrock.blockID;
    197                        }
    198                        else
    199                        {
    200                            byte var18 = par3ArrayOfByte[var17];
    201
    202                            if (var18 == 0)
    203                            {
    204                                var13 = -1;
    205                            }
    206                            else if (var18 == Block.stone.blockID)
    207                            {
    208                                if (var13 == -1)
    209                                {
    210                                    if (var12 <= 0)
    211                                    {
    212                                        var14 = 0;
    213                                        var15 = (byte)Block.stone.blockID;
    214                                    }
    215                                    else if (var16 >= var5 - 4 && var16 <= var5 + 1)
    216                                    {
    217                                        var14 = var10.topBlock;
    218                                        var15 = var10.fillerBlock;
    219                                    }
    220
    221                                    if (var16 < var5 && var14 == 0)
    222                                    {
    223                                        if (var11 < 0.15F)
    224                                        {
    225                                            var14 = (byte)Block.ice.blockID;
    226                                        }
    227                                        else
    228                                        {
    229                                            var14 = (byte)Block.waterStill.blockID;
    230                                        }
    231                                    }
    232
    233                                    var13 = var12;
    234
    235                                    if (var16 >= var5 - 1)
    236                                    {
    237                                        par3ArrayOfByte[var17] = var14;
    238                                    }
    239                                    else
    240                                    {
    241                                        par3ArrayOfByte[var17] = var15;
    242                                    }
    243                                }
    244                                else if (var13 > 0)
    245                                {
    246                                    --var13;
    247                                    par3ArrayOfByte[var17] = var15;
    248
    249                                    if (var13 == 0 && var15 == Block.sand.blockID)
    250                                    {
    251                                        var13 = this.rand.nextInt(4);
    252                                        var15 = (byte)Block.sandStone.blockID;
    253                                    }
    254                                }
    255                            }
    256                        }
    257                    }
    258                }
    259            }
    260        }
    261
    262        /**
    263         * loads or generates the chunk at the chunk location specified
    264         */
    265        public Chunk loadChunk(int par1, int par2)
    266        {
    267            return this.provideChunk(par1, par2);
    268        }
    269
    270        /**
    271         * Will return back a chunk, if it doesn't exist and its not a MP client it will generates all the blocks for the
    272         * specified chunk from the map seed and chunk seed
    273         */
    274        public Chunk provideChunk(int par1, int par2)
    275        {
    276            this.rand.setSeed((long)par1 * 341873128712L + (long)par2 * 132897987541L);
    277            byte[] var3 = new byte[32768];
    278            this.generateTerrain(par1, par2, var3);
    279            this.biomesForGeneration = this.worldObj.getWorldChunkManager().loadBlockGeneratorData(this.biomesForGeneration, par1 * 16, par2 * 16, 16, 16);
    280            this.replaceBlocksForBiome(par1, par2, var3, this.biomesForGeneration);
    281            this.caveGenerator.generate(this, this.worldObj, par1, par2, var3);
    282            this.ravineGenerator.generate(this, this.worldObj, par1, par2, var3);
    283
    284            if (this.mapFeaturesEnabled)
    285            {
    286                this.mineshaftGenerator.generate(this, this.worldObj, par1, par2, var3);
    287                this.villageGenerator.generate(this, this.worldObj, par1, par2, var3);
    288                this.strongholdGenerator.generate(this, this.worldObj, par1, par2, var3);
    289                this.scatteredFeatureGenerator.generate(this, this.worldObj, par1, par2, var3);
    290            }
    291
    292            Chunk var4 = new Chunk(this.worldObj, var3, par1, par2);
    293            byte[] var5 = var4.getBiomeArray();
    294
    295            for (int var6 = 0; var6 < var5.length; ++var6)
    296            {
    297                var5[var6] = (byte)this.biomesForGeneration[var6].biomeID;
    298            }
    299
    300            var4.generateSkylightMap();
    301            return var4;
    302        }
    303
    304        /**
    305         * generates a subset of the level's terrain data. Takes 7 arguments: the [empty] noise array, the position, and the
    306         * size.
    307         */
    308        private double[] initializeNoiseField(double[] par1ArrayOfDouble, int par2, int par3, int par4, int par5, int par6, int par7)
    309        {
    310            if (par1ArrayOfDouble == null)
    311            {
    312                par1ArrayOfDouble = new double[par5 * par6 * par7];
    313            }
    314
    315            if (this.parabolicField == null)
    316            {
    317                this.parabolicField = new float[25];
    318
    319                for (int var8 = -2; var8 <= 2; ++var8)
    320                {
    321                    for (int var9 = -2; var9 <= 2; ++var9)
    322                    {
    323                        float var10 = 10.0F / MathHelper.sqrt_float((float)(var8 * var8 + var9 * var9) + 0.2F);
    324                        this.parabolicField[var8 + 2 + (var9 + 2) * 5] = var10;
    325                    }
    326                }
    327            }
    328
    329            double var44 = 684.412D;
    330            double var45 = 684.412D;
    331            this.noise5 = this.noiseGen5.generateNoiseOctaves(this.noise5, par2, par4, par5, par7, 1.121D, 1.121D, 0.5D);
    332            this.noise6 = this.noiseGen6.generateNoiseOctaves(this.noise6, par2, par4, par5, par7, 200.0D, 200.0D, 0.5D);
    333            this.noise3 = this.noiseGen3.generateNoiseOctaves(this.noise3, par2, par3, par4, par5, par6, par7, var44 / 80.0D, var45 / 160.0D, var44 / 80.0D);
    334            this.noise1 = this.noiseGen1.generateNoiseOctaves(this.noise1, par2, par3, par4, par5, par6, par7, var44, var45, var44);
    335            this.noise2 = this.noiseGen2.generateNoiseOctaves(this.noise2, par2, par3, par4, par5, par6, par7, var44, var45, var44);
    336            boolean var43 = false;
    337            boolean var42 = false;
    338            int var12 = 0;
    339            int var13 = 0;
    340
    341            for (int var14 = 0; var14 < par5; ++var14)
    342            {
    343                for (int var15 = 0; var15 < par7; ++var15)
    344                {
    345                    float var16 = 0.0F;
    346                    float var17 = 0.0F;
    347                    float var18 = 0.0F;
    348                    byte var19 = 2;
    349                    BiomeGenBase var20 = this.biomesForGeneration[var14 + 2 + (var15 + 2) * (par5 + 5)];
    350
    351                    for (int var21 = -var19; var21 <= var19; ++var21)
    352                    {
    353                        for (int var22 = -var19; var22 <= var19; ++var22)
    354                        {
    355                            BiomeGenBase var23 = this.biomesForGeneration[var14 + var21 + 2 + (var15 + var22 + 2) * (par5 + 5)];
    356                            float var24 = this.parabolicField[var21 + 2 + (var22 + 2) * 5] / (var23.minHeight + 2.0F);
    357
    358                            if (var23.minHeight > var20.minHeight)
    359                            {
    360                                var24 /= 2.0F;
    361                            }
    362
    363                            var16 += var23.maxHeight * var24;
    364                            var17 += var23.minHeight * var24;
    365                            var18 += var24;
    366                        }
    367                    }
    368
    369                    var16 /= var18;
    370                    var17 /= var18;
    371                    var16 = var16 * 0.9F + 0.1F;
    372                    var17 = (var17 * 4.0F - 1.0F) / 8.0F;
    373                    double var47 = this.noise6[var13] / 8000.0D;
    374
    375                    if (var47 < 0.0D)
    376                    {
    377                        var47 = -var47 * 0.3D;
    378                    }
    379
    380                    var47 = var47 * 3.0D - 2.0D;
    381
    382                    if (var47 < 0.0D)
    383                    {
    384                        var47 /= 2.0D;
    385
    386                        if (var47 < -1.0D)
    387                        {
    388                            var47 = -1.0D;
    389                        }
    390
    391                        var47 /= 1.4D;
    392                        var47 /= 2.0D;
    393                    }
    394                    else
    395                    {
    396                        if (var47 > 1.0D)
    397                        {
    398                            var47 = 1.0D;
    399                        }
    400
    401                        var47 /= 8.0D;
    402                    }
    403
    404                    ++var13;
    405
    406                    for (int var46 = 0; var46 < par6; ++var46)
    407                    {
    408                        double var48 = (double)var17;
    409                        double var26 = (double)var16;
    410                        var48 += var47 * 0.2D;
    411                        var48 = var48 * (double)par6 / 16.0D;
    412                        double var28 = (double)par6 / 2.0D + var48 * 4.0D;
    413                        double var30 = 0.0D;
    414                        double var32 = ((double)var46 - var28) * 12.0D * 128.0D / 128.0D / var26;
    415
    416                        if (var32 < 0.0D)
    417                        {
    418                            var32 *= 4.0D;
    419                        }
    420
    421                        double var34 = this.noise1[var12] / 512.0D;
    422                        double var36 = this.noise2[var12] / 512.0D;
    423                        double var38 = (this.noise3[var12] / 10.0D + 1.0D) / 2.0D;
    424
    425                        if (var38 < 0.0D)
    426                        {
    427                            var30 = var34;
    428                        }
    429                        else if (var38 > 1.0D)
    430                        {
    431                            var30 = var36;
    432                        }
    433                        else
    434                        {
    435                            var30 = var34 + (var36 - var34) * var38;
    436                        }
    437
    438                        var30 -= var32;
    439
    440                        if (var46 > par6 - 4)
    441                        {
    442                            double var40 = (double)((float)(var46 - (par6 - 4)) / 3.0F);
    443                            var30 = var30 * (1.0D - var40) + -10.0D * var40;
    444                        }
    445
    446                        par1ArrayOfDouble[var12] = var30;
    447                        ++var12;
    448                    }
    449                }
    450            }
    451
    452            return par1ArrayOfDouble;
    453        }
    454
    455        /**
    456         * Checks to see if a chunk exists at x, y
    457         */
    458        public boolean chunkExists(int par1, int par2)
    459        {
    460            return true;
    461        }
    462
    463        /**
    464         * Populates chunk with ores etc etc
    465         */
    466        public void populate(IChunkProvider par1IChunkProvider, int par2, int par3)
    467        {
    468            BlockSand.fallInstantly = true;
    469            int var4 = par2 * 16;
    470            int var5 = par3 * 16;
    471            BiomeGenBase var6 = this.worldObj.getBiomeGenForCoords(var4 + 16, var5 + 16);
    472            this.rand.setSeed(this.worldObj.getSeed());
    473            long var7 = this.rand.nextLong() / 2L * 2L + 1L;
    474            long var9 = this.rand.nextLong() / 2L * 2L + 1L;
    475            this.rand.setSeed((long)par2 * var7 + (long)par3 * var9 ^ this.worldObj.getSeed());
    476            boolean var11 = false;
    477
    478            if (this.mapFeaturesEnabled)
    479            {
    480                this.mineshaftGenerator.generateStructuresInChunk(this.worldObj, this.rand, par2, par3);
    481                var11 = this.villageGenerator.generateStructuresInChunk(this.worldObj, this.rand, par2, par3);
    482                this.strongholdGenerator.generateStructuresInChunk(this.worldObj, this.rand, par2, par3);
    483                this.scatteredFeatureGenerator.generateStructuresInChunk(this.worldObj, this.rand, par2, par3);
    484            }
    485
    486            int var12;
    487            int var13;
    488            int var14;
    489
    490            if (!var11 && this.rand.nextInt(4) == 0)
    491            {
    492                var12 = var4 + this.rand.nextInt(16) + 8;
    493                var13 = this.rand.nextInt(128);
    494                var14 = var5 + this.rand.nextInt(16) + 8;
    495                (new WorldGenLakes(Block.waterStill.blockID)).generate(this.worldObj, this.rand, var12, var13, var14);
    496            }
    497
    498            if (!var11 && this.rand.nextInt(8) == 0)
    499            {
    500                var12 = var4 + this.rand.nextInt(16) + 8;
    501                var13 = this.rand.nextInt(this.rand.nextInt(120) + 8);
    502                var14 = var5 + this.rand.nextInt(16) + 8;
    503
    504                if (var13 < 63 || this.rand.nextInt(10) == 0)
    505                {
    506                    (new WorldGenLakes(Block.lavaStill.blockID)).generate(this.worldObj, this.rand, var12, var13, var14);
    507                }
    508            }
    509
    510            for (var12 = 0; var12 < 8; ++var12)
    511            {
    512                var13 = var4 + this.rand.nextInt(16) + 8;
    513                var14 = this.rand.nextInt(128);
    514                int var15 = var5 + this.rand.nextInt(16) + 8;
    515
    516                if ((new WorldGenDungeons()).generate(this.worldObj, this.rand, var13, var14, var15))
    517                {
    518                    ;
    519                }
    520            }
    521
    522            var6.decorate(this.worldObj, this.rand, var4, var5);
    523            SpawnerAnimals.performWorldGenSpawning(this.worldObj, var6, var4 + 8, var5 + 8, 16, 16, this.rand);
    524            var4 += 8;
    525            var5 += 8;
    526
    527            for (var12 = 0; var12 < 16; ++var12)
    528            {
    529                for (var13 = 0; var13 < 16; ++var13)
    530                {
    531                    var14 = this.worldObj.getPrecipitationHeight(var4 + var12, var5 + var13);
    532
    533                    if (this.worldObj.isBlockFreezable(var12 + var4, var14 - 1, var13 + var5))
    534                    {
    535                        this.worldObj.setBlockWithNotify(var12 + var4, var14 - 1, var13 + var5, Block.ice.blockID);
    536                    }
    537
    538                    if (this.worldObj.canSnowAt(var12 + var4, var14, var13 + var5))
    539                    {
    540                        this.worldObj.setBlockWithNotify(var12 + var4, var14, var13 + var5, Block.snow.blockID);
    541                    }
    542                }
    543            }
    544
    545            BlockSand.fallInstantly = false;
    546        }
    547
    548        /**
    549         * Two modes of operation: if passed true, save all Chunks in one go.  If passed false, save up to two chunks.
    550         * Return true if all chunks have been saved.
    551         */
    552        public boolean saveChunks(boolean par1, IProgressUpdate par2IProgressUpdate)
    553        {
    554            return true;
    555        }
    556
    557        /**
    558         * Unloads the 100 oldest chunks from memory, due to a bug with chunkSet.add() never being called it thinks the list
    559         * is always empty and will not remove any chunks.
    560         */
    561        public boolean unload100OldestChunks()
    562        {
    563            return false;
    564        }
    565
    566        /**
    567         * Returns if the IChunkProvider supports saving.
    568         */
    569        public boolean canSave()
    570        {
    571            return true;
    572        }
    573
    574        /**
    575         * Converts the instance data to a readable string.
    576         */
    577        public String makeString()
    578        {
    579            return "RandomLevelSource";
    580        }
    581
    582        /**
    583         * Returns a list of creatures of the specified type that can spawn at the given location.
    584         */
    585        public List getPossibleCreatures(EnumCreatureType par1EnumCreatureType, int par2, int par3, int par4)
    586        {
    587            BiomeGenBase var5 = this.worldObj.getBiomeGenForCoords(par2, par4);
    588            return var5 == null ? null : var5.getSpawnableList(par1EnumCreatureType);
    589        }
    590
    591        /**
    592         * Returns the location of the closest structure of the specified type. If not found returns null.
    593         */
    594        public ChunkPosition findClosestStructure(World par1World, String par2Str, int par3, int par4, int par5)
    595        {
    596            return "Stronghold".equals(par2Str) && this.strongholdGenerator != null ? this.strongholdGenerator.getNearestInstance(par1World, par3, par4, par5) : null;
    597        }
    598
    599        public int getLoadedChunkCount()
    600        {
    601            return 0;
    602        }
    603    }