Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.51 KB | None | 0 0
  1. package me.geometrically.prehistoric.common.world.gen;
  2.  
  3. import net.minecraft.init.Blocks;
  4. import net.minecraft.util.math.MathHelper;
  5. import net.minecraft.world.World;
  6. import net.minecraft.world.biome.Biome;
  7. import net.minecraft.world.chunk.ChunkPrimer;
  8. import net.minecraft.world.gen.IChunkGenerator;
  9. import net.minecraft.world.gen.NoiseGeneratorOctaves;
  10. import net.minecraft.world.gen.NoiseGeneratorPerlin;
  11.  
  12. import java.util.Random;
  13.  
  14. public class PrehistoricTerrainGenerator {
  15.     private World world;
  16.     private Random random;
  17.  
  18.     private final double[] heightMap;
  19.     private double[] mainNoiseRegion;
  20.     private double[] minLimitRegion;
  21.     private double[] maxLimitRegion;
  22.     private double[] depthRegion;
  23.  
  24.     private NoiseGeneratorOctaves minLimitPerlinNoise;
  25.     private NoiseGeneratorOctaves maxLimitPerlinNoise;
  26.     private NoiseGeneratorOctaves mainPerlinNoise;
  27.     private NoiseGeneratorPerlin surfaceNoise;
  28.  
  29.     // A NoiseGeneratorOctaves used in generating terrain
  30.     private NoiseGeneratorOctaves depthNoise;
  31.  
  32.     private final float[] biomeWeights;
  33.     private double[] depthBuffer = new double[256];
  34.  
  35.     private Biome[] biomesForGeneration;
  36.  
  37.     public PrehistoricTerrainGenerator() {
  38.         this.heightMap = new double[825];
  39.  
  40.         this.biomeWeights = new float[25];
  41.         for (int j = -2; j <= 2; ++j) {
  42.             for (int k = -2; k <= 2; ++k) {
  43.                 float f = 10.0F / MathHelper.sqrt((j * j + k * k) + 0.2F);
  44.                 this.biomeWeights[j + 2 + (k + 2) * 5] = f;
  45.             }
  46.         }
  47.     }
  48.  
  49.     public void setBiomesForGeneration(Biome[] biomesForGeneration) {
  50.         this.biomesForGeneration = biomesForGeneration;
  51.     }
  52.  
  53.     public void setup(World world, Random rand) {
  54.         this.world = world;
  55.         this.random = rand;
  56.  
  57.         this.minLimitPerlinNoise = new NoiseGeneratorOctaves(rand, 16);
  58.         this.maxLimitPerlinNoise = new NoiseGeneratorOctaves(rand, 16);
  59.         this.mainPerlinNoise = new NoiseGeneratorOctaves(rand, 8);
  60.         this.surfaceNoise = new NoiseGeneratorPerlin(rand, 4);
  61.         NoiseGeneratorOctaves noiseGen5 = new NoiseGeneratorOctaves(rand, 10);
  62.         this.depthNoise = new NoiseGeneratorOctaves(rand, 16);
  63.         NoiseGeneratorOctaves mobSpawnerNoise = new NoiseGeneratorOctaves(rand, 8);
  64.  
  65.         net.minecraftforge.event.terraingen.InitNoiseGensEvent.ContextOverworld ctx =
  66.                 new net.minecraftforge.event.terraingen.InitNoiseGensEvent.ContextOverworld(minLimitPerlinNoise, maxLimitPerlinNoise, mainPerlinNoise, surfaceNoise, noiseGen5, depthNoise, mobSpawnerNoise);
  67.         ctx = net.minecraftforge.event.terraingen.TerrainGen.getModdedNoiseGenerators(world, rand, ctx);
  68.         this.minLimitPerlinNoise = ctx.getLPerlin1();
  69.         this.maxLimitPerlinNoise = ctx.getLPerlin2();
  70.         this.mainPerlinNoise = ctx.getPerlin();
  71.         this.surfaceNoise = ctx.getHeight();
  72. //        this.field_185983_b = ctx.getScale();
  73.         this.depthNoise = ctx.getDepth();
  74. //        this.field_185985_d = ctx.getForest();
  75.     }
  76.  
  77.  
  78.     private void generateHeightmap(int chunkX4, int chunkY4, int chunkZ4) {
  79.         this.depthRegion = this.depthNoise.generateNoiseOctaves(this.depthRegion, chunkX4, chunkZ4, 5, 5, 200.0D, 200.0D, 0.5D);
  80.         this.mainNoiseRegion = this.mainPerlinNoise.generateNoiseOctaves(this.mainNoiseRegion, chunkX4, chunkY4, chunkZ4, 5, 33, 5, 8.555150000000001D, 4.277575000000001D, 8.555150000000001D);
  81.         this.minLimitRegion = this.minLimitPerlinNoise.generateNoiseOctaves(this.minLimitRegion, chunkX4, chunkY4, chunkZ4, 5, 33, 5, 684.412D, 684.412D, 684.412D);
  82.         this.maxLimitRegion = this.maxLimitPerlinNoise.generateNoiseOctaves(this.maxLimitRegion, chunkX4, chunkY4, chunkZ4, 5, 33, 5, 684.412D, 684.412D, 684.412D);
  83.         int l = 0;
  84.         int i1 = 0;
  85.  
  86.         for (int j1 = 0; j1 < 5; ++j1) {
  87.             for (int k1 = 0; k1 < 5; ++k1) {
  88.                 float f = 0.0F;
  89.                 float f1 = 0.0F;
  90.                 float f2 = 0.0F;
  91.                 byte b0 = 2;
  92.  
  93.                 for (int l1 = -b0; l1 <= b0; ++l1) {
  94.                     for (int i2 = -b0; i2 <= b0; ++i2) {
  95.                         Biome biome = this.biomesForGeneration[j1 + 2 + (k1 + 2) * 10];
  96.                         float baseHeight = biome.getBaseHeight();
  97.                         float variation = biome.getHeightVariation();
  98.  
  99.                         float f5 = biomeWeights[l1 + 2 + (i2 + 2) * 5] / (baseHeight + 2.0F);
  100.                         f += variation * f5;
  101.                         f1 += baseHeight * f5;
  102.                         f2 += f5;
  103.                     }
  104.                 }
  105.  
  106.                 f /= f2;
  107.                 f1 /= f2;
  108.                 f = f * 0.9F + 0.1F;
  109.                 f1 = (f1 * 4.0F - 1.0F) / 8.0F;
  110.                 double d12 = this.depthRegion[i1] / 8000.0D;
  111.  
  112.                 if (d12 < 0.0D) {
  113.                     d12 = -d12 * 0.3D;
  114.                 }
  115.  
  116.                 d12 = d12 * 3.0D - 2.0D;
  117.  
  118.                 if (d12 < 0.0D) {
  119.                     d12 /= 2.0D;
  120.  
  121.                     if (d12 < -1.0D) {
  122.                         d12 = -1.0D;
  123.                     }
  124.  
  125.                     d12 /= 1.4D;
  126.                     d12 /= 2.0D;
  127.                 } else {
  128.                     if (d12 > 1.0D) {
  129.                         d12 = 1.0D;
  130.                     }
  131.  
  132.                     d12 /= 8.0D;
  133.                 }
  134.  
  135.                 ++i1;
  136.                 double d13 = f1;
  137.                 double d14 = f;
  138.                 d13 += d12 * 0.2D;
  139.                 d13 = d13 * 8.5D / 8.0D;
  140.                 double d5 = 8.5D + d13 * 4.0D;
  141.  
  142.                 for (int j2 = 0; j2 < 33; ++j2) {
  143.                     double d6 = (j2 - d5) * 12.0D * 128.0D / 256.0D / d14;
  144.  
  145.                     if (d6 < 0.0D) {
  146.                         d6 *= 4.0D;
  147.                     }
  148.  
  149.                     double d7 = this.minLimitRegion[l] / 512.0D;
  150.                     double d8 = this.maxLimitRegion[l] / 512.0D;
  151.                     double d9 = (this.mainNoiseRegion[l] / 10.0D + 1.0D) / 2.0D;
  152.                     double d10 = MathHelper.clamp(d7, d8, d9) - d6;
  153.  
  154.                     if (j2 > 29) {
  155.                         double d11 = ((j2 - 29) / 3.0F);
  156.                         d10 = d10 * (1.0D - d11) + -10.0D * d11;
  157.                     }
  158.  
  159.                     this.heightMap[l] = d10;
  160.                     ++l;
  161.                 }
  162.             }
  163.         }
  164.     }
  165.  
  166.  
  167.     public void generate(int chunkX, int chunkZ, ChunkPrimer primer) {
  168.         generateHeightmap(chunkX * 4, 0, chunkZ * 4);
  169.  
  170.         byte waterLevel = 63;
  171.         for (int x4 = 0; x4 < 4; ++x4) {
  172.             int l = x4 * 5;
  173.             int i1 = (x4 + 1) * 5;
  174.  
  175.             for (int z4 = 0; z4 < 4; ++z4) {
  176.                 int k1 = (l + z4) * 33;
  177.                 int l1 = (l + z4 + 1) * 33;
  178.                 int i2 = (i1 + z4) * 33;
  179.                 int j2 = (i1 + z4 + 1) * 33;
  180.  
  181.                 for (int height32 = 0; height32 < 32; ++height32) {
  182.                     double d0 = 0.125D;
  183.                     double d1 = heightMap[k1 + height32];
  184.                     double d2 = heightMap[l1 + height32];
  185.                     double d3 = heightMap[i2 + height32];
  186.                     double d4 = heightMap[j2 + height32];
  187.                     double d5 = (heightMap[k1 + height32 + 1] - d1) * d0;
  188.                     double d6 = (heightMap[l1 + height32 + 1] - d2) * d0;
  189.                     double d7 = (heightMap[i2 + height32 + 1] - d3) * d0;
  190.                     double d8 = (heightMap[j2 + height32 + 1] - d4) * d0;
  191.  
  192.                     for (int h = 0; h < 8; ++h) {
  193.                         double d9 = 0.25D;
  194.                         double d10 = d1;
  195.                         double d11 = d2;
  196.                         double d12 = (d3 - d1) * d9;
  197.                         double d13 = (d4 - d2) * d9;
  198.                         int height = (height32 * 8) + h;
  199.  
  200.                         for (int x = 0; x < 4; ++x) {
  201.                             double d14 = 0.25D;
  202.                             double d16 = (d11 - d10) * d14;
  203.                             double d15 = d10 - d16;
  204.  
  205.                             for (int z = 0; z < 4; ++z) {
  206.                                 if (height < 2) {
  207.                                     primer.setBlockState(x4 * 4 + x, height32 * 8 + h, z4 * 4 + z, Blocks.BEDROCK.getDefaultState());
  208.                                 } else if ((d15 += d16) > 0.0D) {
  209.                                     primer.setBlockState(x4 * 4 + x, height32 * 8 + h, z4 * 4 + z, Blocks.STONE.getDefaultState());
  210.                                 }
  211.                             }
  212.  
  213.                             d10 += d12;
  214.                             d11 += d13;
  215.                         }
  216.  
  217.                         d1 += d5;
  218.                         d2 += d6;
  219.                         d3 += d7;
  220.                         d4 += d8;
  221.                     }
  222.                 }
  223.             }
  224.         }
  225.     }
  226.  
  227.     public void replaceBiomeBlocks(int x, int z, ChunkPrimer primer, IChunkGenerator generator, Biome[] biomes) {
  228.         if (!net.minecraftforge.event.ForgeEventFactory.onReplaceBiomeBlocks(generator, x, z, primer, this.world)) return;
  229.         this.depthBuffer = this.surfaceNoise.getRegion(this.depthBuffer, (x * 16), (z * 16), 16, 16, 0.0625D, 0.0625D, 1.0D);
  230.  
  231.         for (int i = 0; i < 16; ++i) {
  232.             for (int j = 0; j < 16; ++j) {
  233.                 Biome biome = biomes[j + i * 16];
  234.                 biome.genTerrainBlocks(this.world, this.random, primer, x * 16 + i, z * 16 + j, this.depthBuffer[j + i * 16]);
  235.             }
  236.         }
  237.     }
  238.  
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement