Camellias_

ChunkProviderEnd

May 8th, 2018
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.64 KB | None | 0 0
  1. package net.minecraft.world.gen;
  2.  
  3. import java.util.List;
  4. import java.util.Random;
  5. import javax.annotation.Nullable;
  6. import net.minecraft.block.BlockChorusFlower;
  7. import net.minecraft.block.BlockFalling;
  8. import net.minecraft.block.material.Material;
  9. import net.minecraft.block.state.IBlockState;
  10. import net.minecraft.entity.EnumCreatureType;
  11. import net.minecraft.init.Blocks;
  12. import net.minecraft.tileentity.TileEntity;
  13. import net.minecraft.tileentity.TileEntityEndGateway;
  14. import net.minecraft.util.math.BlockPos;
  15. import net.minecraft.util.math.ChunkPos;
  16. import net.minecraft.util.math.MathHelper;
  17. import net.minecraft.world.World;
  18. import net.minecraft.world.biome.Biome;
  19. import net.minecraft.world.chunk.Chunk;
  20. import net.minecraft.world.chunk.ChunkPrimer;
  21. import net.minecraft.world.gen.feature.WorldGenEndGateway;
  22. import net.minecraft.world.gen.feature.WorldGenEndIsland;
  23. import net.minecraft.world.gen.structure.MapGenEndCity;
  24.  
  25. public class ChunkGeneratorEnd implements IChunkGenerator
  26. {
  27.     /** RNG. */
  28.     private final Random rand;
  29.     protected static final IBlockState END_STONE = Blocks.END_STONE.getDefaultState();
  30.     protected static final IBlockState AIR = Blocks.AIR.getDefaultState();
  31.     private NoiseGeneratorOctaves lperlinNoise1;
  32.     private NoiseGeneratorOctaves lperlinNoise2;
  33.     private NoiseGeneratorOctaves perlinNoise1;
  34.     /** A NoiseGeneratorOctaves used in generating terrain */
  35.     public NoiseGeneratorOctaves noiseGen5;
  36.     /** A NoiseGeneratorOctaves used in generating terrain */
  37.     public NoiseGeneratorOctaves noiseGen6;
  38.     /** Reference to the World object. */
  39.     private final World world;
  40.     /** are map structures going to be generated (e.g. strongholds) */
  41.     private final boolean mapFeaturesEnabled;
  42.     private final BlockPos spawnPoint;
  43.     private MapGenEndCity endCityGen = new MapGenEndCity(this);
  44.     private NoiseGeneratorSimplex islandNoise;
  45.     private double[] buffer;
  46.     /** The biomes that are used to generate the chunk */
  47.     private Biome[] biomesForGeneration;
  48.     double[] pnr;
  49.     double[] ar;
  50.     double[] br;
  51.     private final WorldGenEndIsland endIslands = new WorldGenEndIsland();
  52.     // temporary variables used during event handling
  53.     private int chunkX = 0;
  54.     private int chunkZ = 0;
  55.  
  56.     public ChunkGeneratorEnd(World p_i47241_1_, boolean p_i47241_2_, long p_i47241_3_, BlockPos p_i47241_5_)
  57.     {
  58.         this.world = p_i47241_1_;
  59.         this.mapFeaturesEnabled = p_i47241_2_;
  60.         this.spawnPoint = p_i47241_5_;
  61.         this.rand = new Random(p_i47241_3_);
  62.         this.lperlinNoise1 = new NoiseGeneratorOctaves(this.rand, 16);
  63.         this.lperlinNoise2 = new NoiseGeneratorOctaves(this.rand, 16);
  64.         this.perlinNoise1 = new NoiseGeneratorOctaves(this.rand, 8);
  65.         this.noiseGen5 = new NoiseGeneratorOctaves(this.rand, 10);
  66.         this.noiseGen6 = new NoiseGeneratorOctaves(this.rand, 16);
  67.         this.islandNoise = new NoiseGeneratorSimplex(this.rand);
  68.  
  69.         net.minecraftforge.event.terraingen.InitNoiseGensEvent.ContextEnd ctx =
  70.                 new net.minecraftforge.event.terraingen.InitNoiseGensEvent.ContextEnd(lperlinNoise1, lperlinNoise2, perlinNoise1, noiseGen5, noiseGen6, islandNoise);
  71.         ctx = net.minecraftforge.event.terraingen.TerrainGen.getModdedNoiseGenerators(p_i47241_1_, this.rand, ctx);
  72.         this.lperlinNoise1 = ctx.getLPerlin1();
  73.         this.lperlinNoise2 = ctx.getLPerlin2();
  74.         this.perlinNoise1 = ctx.getPerlin();
  75.         this.noiseGen5 = ctx.getDepth();
  76.         this.noiseGen6 = ctx.getScale();
  77.         this.islandNoise = ctx.getIsland();
  78.         this.endCityGen = (MapGenEndCity) net.minecraftforge.event.terraingen.TerrainGen.getModdedMapGen(this.endCityGen, net.minecraftforge.event.terraingen.InitMapGenEvent.EventType.END_CITY);
  79.     }
  80.  
  81.     /**
  82.      * Generates a bare-bones chunk of nothing but stone or ocean blocks, formed, but featureless.
  83.      */
  84.     public void setBlocksInChunk(int x, int z, ChunkPrimer primer)
  85.     {
  86.         int i = 2;
  87.         int j = 3;
  88.         int k = 33;
  89.         int l = 3;
  90.         this.buffer = this.getHeights(this.buffer, x * 2, 0, z * 2, 3, 33, 3);
  91.  
  92.         for (int i1 = 0; i1 < 2; ++i1)
  93.         {
  94.             for (int j1 = 0; j1 < 2; ++j1)
  95.             {
  96.                 for (int k1 = 0; k1 < 32; ++k1)
  97.                 {
  98.                     double d0 = 0.25D;
  99.                     double d1 = this.buffer[((i1 + 0) * 3 + j1 + 0) * 33 + k1 + 0];
  100.                     double d2 = this.buffer[((i1 + 0) * 3 + j1 + 1) * 33 + k1 + 0];
  101.                     double d3 = this.buffer[((i1 + 1) * 3 + j1 + 0) * 33 + k1 + 0];
  102.                     double d4 = this.buffer[((i1 + 1) * 3 + j1 + 1) * 33 + k1 + 0];
  103.                     double d5 = (this.buffer[((i1 + 0) * 3 + j1 + 0) * 33 + k1 + 1] - d1) * 0.25D;
  104.                     double d6 = (this.buffer[((i1 + 0) * 3 + j1 + 1) * 33 + k1 + 1] - d2) * 0.25D;
  105.                     double d7 = (this.buffer[((i1 + 1) * 3 + j1 + 0) * 33 + k1 + 1] - d3) * 0.25D;
  106.                     double d8 = (this.buffer[((i1 + 1) * 3 + j1 + 1) * 33 + k1 + 1] - d4) * 0.25D;
  107.  
  108.                     for (int l1 = 0; l1 < 4; ++l1)
  109.                     {
  110.                         double d9 = 0.125D;
  111.                         double d10 = d1;
  112.                         double d11 = d2;
  113.                         double d12 = (d3 - d1) * 0.125D;
  114.                         double d13 = (d4 - d2) * 0.125D;
  115.  
  116.                         for (int i2 = 0; i2 < 8; ++i2)
  117.                         {
  118.                             double d14 = 0.125D;
  119.                             double d15 = d10;
  120.                             double d16 = (d11 - d10) * 0.125D;
  121.  
  122.                             for (int j2 = 0; j2 < 8; ++j2)
  123.                             {
  124.                                 IBlockState iblockstate = AIR;
  125.  
  126.                                 if (d15 > 0.0D)
  127.                                 {
  128.                                     iblockstate = END_STONE;
  129.                                 }
  130.  
  131.                                 int k2 = i2 + i1 * 8;
  132.                                 int l2 = l1 + k1 * 4;
  133.                                 int i3 = j2 + j1 * 8;
  134.                                 primer.setBlockState(k2, l2, i3, iblockstate);
  135.                                 d15 += d16;
  136.                             }
  137.  
  138.                             d10 += d12;
  139.                             d11 += d13;
  140.                         }
  141.  
  142.                         d1 += d5;
  143.                         d2 += d6;
  144.                         d3 += d7;
  145.                         d4 += d8;
  146.                     }
  147.                 }
  148.             }
  149.         }
  150.     }
  151.  
  152.     public void buildSurfaces(ChunkPrimer primer)
  153.     {
  154.         if (!net.minecraftforge.event.ForgeEventFactory.onReplaceBiomeBlocks(this, this.chunkX, this.chunkZ, primer, this.world)) return;
  155.         for (int i = 0; i < 16; ++i)
  156.         {
  157.             for (int j = 0; j < 16; ++j)
  158.             {
  159.                 int k = 1;
  160.                 int l = -1;
  161.                 IBlockState iblockstate = END_STONE;
  162.                 IBlockState iblockstate1 = END_STONE;
  163.  
  164.                 for (int i1 = 127; i1 >= 0; --i1)
  165.                 {
  166.                     IBlockState iblockstate2 = primer.getBlockState(i, i1, j);
  167.  
  168.                     if (iblockstate2.getMaterial() == Material.AIR)
  169.                     {
  170.                         l = -1;
  171.                     }
  172.                     else if (iblockstate2.getBlock() == Blocks.STONE)
  173.                     {
  174.                         if (l == -1)
  175.                         {
  176.                             l = 1;
  177.  
  178.                             if (i1 >= 0)
  179.                             {
  180.                                 primer.setBlockState(i, i1, j, iblockstate);
  181.                             }
  182.                             else
  183.                             {
  184.                                 primer.setBlockState(i, i1, j, iblockstate1);
  185.                             }
  186.                         }
  187.                         else if (l > 0)
  188.                         {
  189.                             --l;
  190.                             primer.setBlockState(i, i1, j, iblockstate1);
  191.                         }
  192.                     }
  193.                 }
  194.             }
  195.         }
  196.     }
  197.  
  198.     /**
  199.      * Generates the chunk at the specified position, from scratch
  200.      */
  201.     public Chunk generateChunk(int x, int z)
  202.     {
  203.         this.chunkX = x; this.chunkZ = z;
  204.         this.rand.setSeed((long)x * 341873128712L + (long)z * 132897987541L);
  205.         ChunkPrimer chunkprimer = new ChunkPrimer();
  206.         this.biomesForGeneration = this.world.getBiomeProvider().getBiomes(this.biomesForGeneration, x * 16, z * 16, 16, 16);
  207.         this.setBlocksInChunk(x, z, chunkprimer);
  208.         this.buildSurfaces(chunkprimer);
  209.  
  210.         if (this.mapFeaturesEnabled)
  211.         {
  212.             this.endCityGen.generate(this.world, x, z, chunkprimer);
  213.         }
  214.  
  215.         Chunk chunk = new Chunk(this.world, chunkprimer, x, z);
  216.         byte[] abyte = chunk.getBiomeArray();
  217.  
  218.         for (int i = 0; i < abyte.length; ++i)
  219.         {
  220.             abyte[i] = (byte)Biome.getIdForBiome(this.biomesForGeneration[i]);
  221.         }
  222.  
  223.         chunk.generateSkylightMap();
  224.         return chunk;
  225.     }
  226.  
  227.     private float getIslandHeightValue(int p_185960_1_, int p_185960_2_, int p_185960_3_, int p_185960_4_)
  228.     {
  229.         float f = (float)(p_185960_1_ * 2 + p_185960_3_);
  230.         float f1 = (float)(p_185960_2_ * 2 + p_185960_4_);
  231.         float f2 = 100.0F - MathHelper.sqrt(f * f + f1 * f1) * 8.0F;
  232.  
  233.         if (f2 > 80.0F)
  234.         {
  235.             f2 = 80.0F;
  236.         }
  237.  
  238.         if (f2 < -100.0F)
  239.         {
  240.             f2 = -100.0F;
  241.         }
  242.  
  243.         for (int i = -12; i <= 12; ++i)
  244.         {
  245.             for (int j = -12; j <= 12; ++j)
  246.             {
  247.                 long k = (long)(p_185960_1_ + i);
  248.                 long l = (long)(p_185960_2_ + j);
  249.  
  250.                 if (k * k + l * l > 4096L && this.islandNoise.getValue((double)k, (double)l) < -0.8999999761581421D)
  251.                 {
  252.                     float f3 = (MathHelper.abs((float)k) * 3439.0F + MathHelper.abs((float)l) * 147.0F) % 13.0F + 9.0F;
  253.                     f = (float)(p_185960_3_ - i * 2);
  254.                     f1 = (float)(p_185960_4_ - j * 2);
  255.                     float f4 = 100.0F - MathHelper.sqrt(f * f + f1 * f1) * f3;
  256.  
  257.                     if (f4 > 80.0F)
  258.                     {
  259.                         f4 = 80.0F;
  260.                     }
  261.  
  262.                     if (f4 < -100.0F)
  263.                     {
  264.                         f4 = -100.0F;
  265.                     }
  266.  
  267.                     if (f4 > f2)
  268.                     {
  269.                         f2 = f4;
  270.                     }
  271.                 }
  272.             }
  273.         }
  274.  
  275.         return f2;
  276.     }
  277.  
  278.     public boolean isIslandChunk(int p_185961_1_, int p_185961_2_)
  279.     {
  280.         return (long)p_185961_1_ * (long)p_185961_1_ + (long)p_185961_2_ * (long)p_185961_2_ > 4096L && this.getIslandHeightValue(p_185961_1_, p_185961_2_, 1, 1) >= 0.0F;
  281.     }
  282.  
  283.     private double[] getHeights(double[] p_185963_1_, int p_185963_2_, int p_185963_3_, int p_185963_4_, int p_185963_5_, int p_185963_6_, int p_185963_7_)
  284.     {
  285.         net.minecraftforge.event.terraingen.ChunkGeneratorEvent.InitNoiseField event = new net.minecraftforge.event.terraingen.ChunkGeneratorEvent.InitNoiseField(this, p_185963_1_, p_185963_2_, p_185963_3_, p_185963_4_, p_185963_5_, p_185963_6_, p_185963_7_);
  286.         net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(event);
  287.         if (event.getResult() == net.minecraftforge.fml.common.eventhandler.Event.Result.DENY) return event.getNoisefield();
  288.  
  289.         if (p_185963_1_ == null)
  290.         {
  291.             p_185963_1_ = new double[p_185963_5_ * p_185963_6_ * p_185963_7_];
  292.         }
  293.  
  294.         double d0 = 684.412D;
  295.         double d1 = 684.412D;
  296.         d0 = d0 * 2.0D;
  297.         this.pnr = this.perlinNoise1.generateNoiseOctaves(this.pnr, p_185963_2_, p_185963_3_, p_185963_4_, p_185963_5_, p_185963_6_, p_185963_7_, d0 / 80.0D, 4.277575000000001D, d0 / 80.0D);
  298.         this.ar = this.lperlinNoise1.generateNoiseOctaves(this.ar, p_185963_2_, p_185963_3_, p_185963_4_, p_185963_5_, p_185963_6_, p_185963_7_, d0, 684.412D, d0);
  299.         this.br = this.lperlinNoise2.generateNoiseOctaves(this.br, p_185963_2_, p_185963_3_, p_185963_4_, p_185963_5_, p_185963_6_, p_185963_7_, d0, 684.412D, d0);
  300.         int i = p_185963_2_ / 2;
  301.         int j = p_185963_4_ / 2;
  302.         int k = 0;
  303.  
  304.         for (int l = 0; l < p_185963_5_; ++l)
  305.         {
  306.             for (int i1 = 0; i1 < p_185963_7_; ++i1)
  307.             {
  308.                 float f = this.getIslandHeightValue(i, j, l, i1);
  309.  
  310.                 for (int j1 = 0; j1 < p_185963_6_; ++j1)
  311.                 {
  312.                     double d2 = this.ar[k] / 512.0D;
  313.                     double d3 = this.br[k] / 512.0D;
  314.                     double d5 = (this.pnr[k] / 10.0D + 1.0D) / 2.0D;
  315.                     double d4;
  316.  
  317.                     if (d5 < 0.0D)
  318.                     {
  319.                         d4 = d2;
  320.                     }
  321.                     else if (d5 > 1.0D)
  322.                     {
  323.                         d4 = d3;
  324.                     }
  325.                     else
  326.                     {
  327.                         d4 = d2 + (d3 - d2) * d5;
  328.                     }
  329.  
  330.                     d4 = d4 - 8.0D;
  331.                     d4 = d4 + (double)f;
  332.                     int k1 = 2;
  333.  
  334.                     if (j1 > p_185963_6_ / 2 - k1)
  335.                     {
  336.                         double d6 = (double)((float)(j1 - (p_185963_6_ / 2 - k1)) / 64.0F);
  337.                         d6 = MathHelper.clamp(d6, 0.0D, 1.0D);
  338.                         d4 = d4 * (1.0D - d6) + -3000.0D * d6;
  339.                     }
  340.  
  341.                     k1 = 8;
  342.  
  343.                     if (j1 < k1)
  344.                     {
  345.                         double d7 = (double)((float)(k1 - j1) / ((float)k1 - 1.0F));
  346.                         d4 = d4 * (1.0D - d7) + -30.0D * d7;
  347.                     }
  348.  
  349.                     p_185963_1_[k] = d4;
  350.                     ++k;
  351.                 }
  352.             }
  353.         }
  354.  
  355.         return p_185963_1_;
  356.     }
  357.  
  358.     /**
  359.      * Generate initial structures in this chunk, e.g. mineshafts, temples, lakes, and dungeons
  360.      */
  361.     public void populate(int x, int z)
  362.     {
  363.         BlockFalling.fallInstantly = true;
  364.         net.minecraftforge.event.ForgeEventFactory.onChunkPopulate(true, this, this.world, this.rand, x, z, false);
  365.         BlockPos blockpos = new BlockPos(x * 16, 0, z * 16);
  366.  
  367.         if (this.mapFeaturesEnabled)
  368.         {
  369.             this.endCityGen.generateStructure(this.world, this.rand, new ChunkPos(x, z));
  370.         }
  371.  
  372.         this.world.getBiome(blockpos.add(16, 0, 16)).decorate(this.world, this.world.rand, blockpos);
  373.         long i = (long)x * (long)x + (long)z * (long)z;
  374.  
  375.         if (i > 4096L)
  376.         {
  377.             float f = this.getIslandHeightValue(x, z, 1, 1);
  378.  
  379.             if (f < -20.0F && this.rand.nextInt(14) == 0)
  380.             {
  381.                 this.endIslands.generate(this.world, this.rand, blockpos.add(this.rand.nextInt(16) + 8, 55 + this.rand.nextInt(16), this.rand.nextInt(16) + 8));
  382.  
  383.                 if (this.rand.nextInt(4) == 0)
  384.                 {
  385.                     this.endIslands.generate(this.world, this.rand, blockpos.add(this.rand.nextInt(16) + 8, 55 + this.rand.nextInt(16), this.rand.nextInt(16) + 8));
  386.                 }
  387.             }
  388.  
  389.             if (this.getIslandHeightValue(x, z, 1, 1) > 40.0F)
  390.             {
  391.                 int j = this.rand.nextInt(5);
  392.  
  393.                 for (int k = 0; k < j; ++k)
  394.                 {
  395.                     int l = this.rand.nextInt(16) + 8;
  396.                     int i1 = this.rand.nextInt(16) + 8;
  397.                     int j1 = this.world.getHeight(blockpos.add(l, 0, i1)).getY();
  398.  
  399.                     if (j1 > 0)
  400.                     {
  401.                         int k1 = j1 - 1;
  402.  
  403.                         if (this.world.isAirBlock(blockpos.add(l, k1 + 1, i1)) && this.world.getBlockState(blockpos.add(l, k1, i1)).getBlock() == Blocks.END_STONE)
  404.                         {
  405.                             BlockChorusFlower.generatePlant(this.world, blockpos.add(l, k1 + 1, i1), this.rand, 8);
  406.                         }
  407.                     }
  408.                 }
  409.  
  410.                 if (this.rand.nextInt(700) == 0)
  411.                 {
  412.                     int l1 = this.rand.nextInt(16) + 8;
  413.                     int i2 = this.rand.nextInt(16) + 8;
  414.                     int j2 = this.world.getHeight(blockpos.add(l1, 0, i2)).getY();
  415.  
  416.                     if (j2 > 0)
  417.                     {
  418.                         int k2 = j2 + 3 + this.rand.nextInt(7);
  419.                         BlockPos blockpos1 = blockpos.add(l1, k2, i2);
  420.                         (new WorldGenEndGateway()).generate(this.world, this.rand, blockpos1);
  421.                         TileEntity tileentity = this.world.getTileEntity(blockpos1);
  422.  
  423.                         if (tileentity instanceof TileEntityEndGateway)
  424.                         {
  425.                             TileEntityEndGateway tileentityendgateway = (TileEntityEndGateway)tileentity;
  426.                             tileentityendgateway.setExactPosition(this.spawnPoint);
  427.                         }
  428.                     }
  429.                 }
  430.             }
  431.         }
  432.  
  433.         net.minecraftforge.event.ForgeEventFactory.onChunkPopulate(false, this, this.world, this.rand, x, z, false);
  434.         BlockFalling.fallInstantly = false;
  435.     }
  436.  
  437.     /**
  438.      * Called to generate additional structures after initial worldgen, used by ocean monuments
  439.      */
  440.     public boolean generateStructures(Chunk chunkIn, int x, int z)
  441.     {
  442.         return false;
  443.     }
  444.  
  445.     public List<Biome.SpawnListEntry> getPossibleCreatures(EnumCreatureType creatureType, BlockPos pos)
  446.     {
  447.         return this.world.getBiome(pos).getSpawnableList(creatureType);
  448.     }
  449.  
  450.     @Nullable
  451.     public BlockPos getNearestStructurePos(World worldIn, String structureName, BlockPos position, boolean findUnexplored)
  452.     {
  453.         return "EndCity".equals(structureName) && this.endCityGen != null ? this.endCityGen.getNearestStructurePos(worldIn, position, findUnexplored) : null;
  454.     }
  455.  
  456.     public boolean isInsideStructure(World worldIn, String structureName, BlockPos pos)
  457.     {
  458.         return "EndCity".equals(structureName) && this.endCityGen != null ? this.endCityGen.isInsideStructure(pos) : false;
  459.     }
  460.  
  461.     /**
  462.      * Recreates data about structures intersecting given chunk (used for example by getPossibleCreatures), without
  463.      * placing any blocks. When called for the first time before any chunk is generated - also initializes the internal
  464.      * state needed by getPossibleCreatures.
  465.      */
  466.     public void recreateStructures(Chunk chunkIn, int x, int z)
  467.     {
  468.     }
  469. }
Advertisement
Add Comment
Please, Sign In to add comment