Advertisement
Guest User

PlanetoidChunkProvider

a guest
May 5th, 2014
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.98 KB | None | 0 0
  1. public class PlanetoidChunkProvider implements IChunkProvider {
  2.  
  3.     private Random rand;
  4.  
  5.     ArrayList<Planet> finished = new ArrayList<Planet>(); // finished planets
  6.     ArrayList<Planet> unfinished = new ArrayList<Planet>(); // unfinished planets
  7.     ArrayList<Point> pregen = new ArrayList<Point>(); // already pregenerated areas
  8.     int pregenChunkSize = 4;
  9.  
  10.     private World world;
  11.     private long seed;
  12.     private boolean mapFeaturesEnabled;
  13.     private PlanetoidGeneratorInfo generatorInfo;
  14.  
  15.     private NoiseGeneratorOctaves field_147431_j;
  16.     private NoiseGeneratorOctaves field_147432_k;
  17.     private NoiseGeneratorOctaves field_147429_l;
  18.     private NoiseGeneratorPerlin field_147430_m;
  19.     double[] field_147427_d;
  20.     double[] field_147428_e;
  21.     double[] field_147425_f;
  22.     double[] field_147426_g;
  23.     private final double[] field_147434_q;
  24.     private final float[] parabolicField;
  25.     public NoiseGeneratorOctaves noiseGen5;
  26.     public NoiseGeneratorOctaves noiseGen6;
  27.     public NoiseGeneratorOctaves mobSpawnerNoise;
  28.     private double[] stoneNoise = new double[256];
  29.     private MapGenBase caveGenerator = new MapGenCaves();
  30.     private MapGenStronghold strongholdGenerator = new MapGenStronghold();
  31.     private MapGenVillage villageGenerator = new MapGenVillage();
  32.     private MapGenMineshaft mineshaftGenerator = new MapGenMineshaft();
  33.     private MapGenScatteredFeature scatteredFeatureGenerator = new MapGenScatteredFeature();
  34.     private MapGenBase ravineGenerator = new MapGenRavine();
  35.     private BiomeGenBase[] biomesForGeneration;
  36.  
  37.     {
  38.         caveGenerator = TerrainGen.getModdedMapGen(caveGenerator, CAVE);
  39.         strongholdGenerator = (MapGenStronghold) TerrainGen.getModdedMapGen(strongholdGenerator, STRONGHOLD);
  40.         villageGenerator = (MapGenVillage) TerrainGen.getModdedMapGen(villageGenerator, VILLAGE);
  41.         mineshaftGenerator = (MapGenMineshaft) TerrainGen.getModdedMapGen(mineshaftGenerator, MINESHAFT);
  42.         scatteredFeatureGenerator = (MapGenScatteredFeature) TerrainGen.getModdedMapGen(scatteredFeatureGenerator, SCATTERED_FEATURE);
  43.         ravineGenerator = TerrainGen.getModdedMapGen(ravineGenerator, RAVINE);
  44.     }
  45.  
  46.     // ChunkProviderGenerate
  47.     public PlanetoidChunkProvider(World world, long seed, boolean mapFeaturesEnabled, String generatorOptions) {
  48.         this.world = world;
  49.         this.seed = seed;
  50.         this.rand = new Random(seed);
  51.  
  52.         this.mapFeaturesEnabled = mapFeaturesEnabled;
  53.         this.generatorInfo = PlanetoidGeneratorInfo.createGeneratorFromString(generatorOptions);
  54.  
  55.         this.field_147431_j = new NoiseGeneratorOctaves(this.rand, 16);
  56.         this.field_147432_k = new NoiseGeneratorOctaves(this.rand, 16);
  57.         this.field_147429_l = new NoiseGeneratorOctaves(this.rand, 8);
  58.         this.field_147430_m = new NoiseGeneratorPerlin(this.rand, 4);
  59.         this.noiseGen5 = new NoiseGeneratorOctaves(this.rand, 10);
  60.         this.noiseGen6 = new NoiseGeneratorOctaves(this.rand, 16);
  61.         this.mobSpawnerNoise = new NoiseGeneratorOctaves(this.rand, 8);
  62.         this.field_147434_q = new double[825];
  63.         this.parabolicField = new float[25];
  64.  
  65.         for (int j = -2; j <= 2; ++j) {
  66.             for (int k = -2; k <= 2; ++k) {
  67.                 float f = 10.0F / MathHelper.sqrt_float((float) (j * j + k * k) + 0.2F);
  68.                 this.parabolicField[j + 2 + (k + 2) * 5] = f;
  69.             }
  70.         }
  71.  
  72.         NoiseGenerator[] noiseGens = { field_147431_j, field_147432_k, field_147429_l, field_147430_m, noiseGen5, noiseGen6, mobSpawnerNoise };
  73.         noiseGens = TerrainGen.getModdedNoiseGenerators(world, this.rand, noiseGens);
  74.         this.field_147431_j = (NoiseGeneratorOctaves) noiseGens[0];
  75.         this.field_147432_k = (NoiseGeneratorOctaves) noiseGens[1];
  76.         this.field_147429_l = (NoiseGeneratorOctaves) noiseGens[2];
  77.         this.field_147430_m = (NoiseGeneratorPerlin) noiseGens[3];
  78.         this.noiseGen5 = (NoiseGeneratorOctaves) noiseGens[4];
  79.         this.noiseGen6 = (NoiseGeneratorOctaves) noiseGens[5];
  80.         this.mobSpawnerNoise = (NoiseGeneratorOctaves) noiseGens[6];
  81.     }
  82.  
  83.     @Override
  84.     public boolean chunkExists(int var1, int var2) {
  85.         return true;
  86.     }
  87.  
  88.     @Override
  89.     public Chunk provideChunk(int chunkX, int chunkZ) {
  90.         TimeAnalyzer.start("provideChunk");
  91.         this.rand.setSeed((long) chunkX * 341873128712L + (long) chunkZ * 132897987541L);
  92.         preGenerate(chunkX, chunkZ); // pregenerate planet positions because they spread over multiple chunks
  93.  
  94.         Block[] ablock = new Block[65536];
  95.         byte[] abyte = new byte[65536];
  96.         this.biomesForGeneration = this.world.getWorldChunkManager().loadBlockGeneratorData(this.biomesForGeneration, chunkX * 16, chunkZ * 16, 16, 16);
  97.  
  98.         generatePlanetoid(chunkX, chunkZ, ablock, abyte); // generate planetoids
  99.  
  100.         this.replaceBlocksForBiome(chunkX, chunkZ, ablock, abyte, this.biomesForGeneration);
  101.         this.caveGenerator.func_151539_a(this, this.world, chunkX, chunkZ, ablock);
  102.         this.ravineGenerator.func_151539_a(this, this.world, chunkX, chunkZ, ablock);
  103.  
  104.         if (this.mapFeaturesEnabled) {
  105.             this.mineshaftGenerator.func_151539_a(this, this.world, chunkX, chunkZ, ablock);
  106.             this.villageGenerator.func_151539_a(this, this.world, chunkX, chunkZ, ablock);
  107.             this.strongholdGenerator.func_151539_a(this, this.world, chunkX, chunkZ, ablock);
  108.             this.scatteredFeatureGenerator.func_151539_a(this, this.world, chunkX, chunkZ, ablock);
  109.         }
  110.  
  111.         Chunk chunk = new Chunk(this.world, ablock, abyte, chunkX, chunkZ);
  112.         byte[] abyte1 = chunk.getBiomeArray();
  113.  
  114.         for (int k = 0; k < abyte1.length; ++k) {
  115.             abyte1[k] = (byte) this.biomesForGeneration[k].biomeID;
  116.         }
  117.  
  118.         chunk.generateSkylightMap();
  119.         TimeAnalyzer.end("provideChunk");
  120.         return chunk;
  121.     }
  122.  
  123.     public void preGenerate(int cx, int cz) {
  124.         TimeAnalyzer.start("preGenerate");
  125.         int x = round(cx / this.pregenChunkSize);
  126.         int z = round(cz / this.pregenChunkSize);
  127.  
  128.         preGenerate2(x - 1, z - 1);
  129.         preGenerate2(x - 1, z);
  130.         preGenerate2(x - 1, z + 1);
  131.         preGenerate2(x, z - 1);
  132.         preGenerate2(x, z);
  133.         preGenerate2(x, z + 1);
  134.         preGenerate2(x + 1, z - 1);
  135.         preGenerate2(x + 1, z);
  136.         preGenerate2(x + 1, z + 1);
  137.  
  138.         TimeAnalyzer.end("preGenerate");
  139.     }
  140.  
  141.     private void preGenerate2(int x, int z) {
  142.         if (!this.pregen.contains(new Point(x, z))) {
  143.             this.rand.setSeed(x * 341873128712L + z * 132897987541L);
  144.             int x2 = x * this.pregenChunkSize * 16;
  145.             int z2 = z * this.pregenChunkSize * 16;
  146.  
  147.             preGenerate_do(x2, z2, x2 + this.pregenChunkSize * 16, z2 + this.pregenChunkSize * 16);
  148.  
  149.             this.pregen.add(new Point(x, z));
  150.         }
  151.     }
  152.  
  153.     private void preGenerate_do(int x1, int z1, int x2, int z2) {
  154.         TimeAnalyzer.start("preGenerate_do");
  155.  
  156.         for (int l = 0; l < Option.SPAWNTRIES.getValue(generatorInfo); l++) {
  157.             double min = Option.MIN_RADIUS.getValue(generatorInfo);
  158.             double max = Option.MAX_RADIUS.getValue(generatorInfo);
  159.             double r = round(this.rand.nextDouble() * (max - min) + min);
  160.             double x = x1 + this.rand.nextInt(x2 - x1);
  161.             double y = round(r + (256 - 2 * r) * this.rand.nextDouble());
  162.             double z = z1 + this.rand.nextInt(z2 - z1);
  163.  
  164.             Planet p = new Planet(this, this.world, x, y, z, round(r));
  165.  
  166.             if (p.isAreaClear()) {
  167.                 for (int i = round(x) - round(r); i <= round(x) + round(r); i++) {
  168.                     for (int k = round(z) - round(r); k <= round(z) + round(r); k++) {
  169.                         int cx = (int) Math.floor(i / 16.0D);
  170.                         int cz = (int) Math.floor(k / 16.0D);
  171.  
  172.                         if (!p.unfinished.contains(new Point(cx, cz))) {
  173.                             p.unfinished.add(new Point(cx, cz));
  174.                         }
  175.                     }
  176.                 }
  177.                 this.unfinished.add(p);
  178.             }
  179.         }
  180.         TimeAnalyzer.end("preGenerate_do");
  181.     }
  182.  
  183.     public void generatePlanetoid(int chunkX, int chunkZ, Block[] ablock, byte[] ameta) {
  184.         TimeAnalyzer.start("generate");
  185.  
  186.         TimeAnalyzer.start("finishPlanets");
  187.         for (int i = 0; i < this.unfinished.size(); i++) {
  188.             Planet p = (Planet) this.unfinished.get(i);
  189.             if (p.shouldFinishChunk(chunkX, chunkZ))
  190.                 p.generateChunk(chunkX, chunkZ, ablock, ameta);
  191.             if (p.isFinished()) {
  192.                 this.unfinished.remove(p);
  193.                 this.finished.add(p);
  194.  
  195.                 i--;
  196.             }
  197.         }
  198.         TimeAnalyzer.end("finishPlanets");
  199.  
  200.         TimeAnalyzer.start("generateWater");
  201.         for (int x = 0; x < 16; x++) {
  202.             for (int y = 0; y < 4; y++) {
  203.                 for (int z = 0; z < 16; z++) {
  204.                     Planet.setBlock(x, y, z, (y == 0 ? Blocks.bedrock : Blocks.water), 0, ablock, ameta);
  205.                 }
  206.             }
  207.         }
  208.         TimeAnalyzer.end("generateWater");
  209.  
  210.         TimeAnalyzer.end("generate");
  211.     }
  212.  
  213.     public void replaceBlocksForBiome(int p_147422_1_, int p_147422_2_, Block[] p_147422_3_, byte[] p_147422_4_, BiomeGenBase[] p_147422_5_) {
  214.         ChunkProviderEvent.ReplaceBiomeBlocks event = new ChunkProviderEvent.ReplaceBiomeBlocks(this, p_147422_1_, p_147422_2_, p_147422_3_, p_147422_5_);
  215.         MinecraftForge.EVENT_BUS.post(event);
  216.         if (event.getResult() == Result.DENY)
  217.             return;
  218.  
  219.         double d0 = 0.03125D;
  220.         this.stoneNoise = this.field_147430_m.func_151599_a(this.stoneNoise, (double) (p_147422_1_ * 16), (double) (p_147422_2_ * 16), 16, 16, d0 * 2.0D, d0 * 2.0D, 1.0D);
  221.  
  222.         for (int k = 0; k < 16; ++k) {
  223.             for (int l = 0; l < 16; ++l) {
  224.                 BiomeGenBase biomegenbase = p_147422_5_[l + k * 16];
  225.                 biomegenbase.genTerrainBlocks(this.world, this.rand, p_147422_3_, p_147422_4_, p_147422_1_ * 16 + k, p_147422_2_ * 16 + l, this.stoneNoise[l + k * 16]);
  226.             }
  227.         }
  228.     }
  229.  
  230.     @Override
  231.     public Chunk loadChunk(int var1, int var2) {
  232.         return this.provideChunk(var1, var2);
  233.     }
  234.  
  235.     @Override
  236.     // ChunkProviderGenerate
  237.     public void populate(IChunkProvider provider, int chunkX, int chunkZ) {
  238.         BlockFalling.fallInstantly = true;
  239.         int k = chunkX * 16;
  240.         int l = chunkZ * 16;
  241.         BiomeGenBase biomegenbase = this.world.getBiomeGenForCoords(k + 16, l + 16);
  242.         this.rand.setSeed(this.world.getSeed());
  243.         long i1 = this.rand.nextLong() / 2L * 2L + 1L;
  244.         long j1 = this.rand.nextLong() / 2L * 2L + 1L;
  245.         this.rand.setSeed((long) chunkX * i1 + (long) chunkZ * j1 ^ this.world.getSeed());
  246.         boolean hasGeneratedVillage = false;
  247.  
  248.         MinecraftForge.EVENT_BUS.post(new PopulateChunkEvent.Pre(provider, world, rand, chunkX, chunkZ, hasGeneratedVillage));
  249.  
  250.         if (this.mapFeaturesEnabled) {
  251.             this.mineshaftGenerator.generateStructuresInChunk(this.world, this.rand, chunkX, chunkZ);
  252.             hasGeneratedVillage = this.villageGenerator.generateStructuresInChunk(this.world, this.rand, chunkX, chunkZ);
  253.             this.strongholdGenerator.generateStructuresInChunk(this.world, this.rand, chunkX, chunkZ);
  254.             this.scatteredFeatureGenerator.generateStructuresInChunk(this.world, this.rand, chunkX, chunkZ);
  255.         }
  256.  
  257.         int k1;
  258.         int l1;
  259.         int i2;
  260.  
  261.         if (biomegenbase != BiomeGenBase.desert && biomegenbase != BiomeGenBase.desertHills && !hasGeneratedVillage && this.rand.nextInt(4) == 0
  262.                 && TerrainGen.populate(provider, world, rand, chunkX, chunkZ, hasGeneratedVillage, LAKE)) {
  263.             k1 = k + this.rand.nextInt(16) + 8;
  264.             l1 = this.rand.nextInt(256);
  265.             i2 = l + this.rand.nextInt(16) + 8;
  266.             (new WorldGenLakes(Blocks.water)).generate(this.world, this.rand, k1, l1, i2);
  267.         }
  268.  
  269.         if (TerrainGen.populate(provider, world, rand, chunkX, chunkZ, hasGeneratedVillage, LAVA) && !hasGeneratedVillage && this.rand.nextInt(8) == 0) {
  270.             k1 = k + this.rand.nextInt(16) + 8;
  271.             l1 = this.rand.nextInt(this.rand.nextInt(248) + 8);
  272.             i2 = l + this.rand.nextInt(16) + 8;
  273.  
  274.             if (l1 < 63 || this.rand.nextInt(10) == 0) {
  275.                 (new WorldGenLakes(Blocks.lava)).generate(this.world, this.rand, k1, l1, i2);
  276.             }
  277.         }
  278.  
  279.         boolean doGen = TerrainGen.populate(provider, world, rand, chunkX, chunkZ, hasGeneratedVillage, DUNGEON);
  280.         for (k1 = 0; doGen && k1 < 8; ++k1) {
  281.             l1 = k + this.rand.nextInt(16) + 8;
  282.             i2 = this.rand.nextInt(256);
  283.             int j2 = l + this.rand.nextInt(16) + 8;
  284.             (new WorldGenDungeons()).generate(this.world, this.rand, l1, i2, j2);
  285.         }
  286.  
  287.         biomegenbase.decorate(this.world, this.rand, k, l);
  288.         if (TerrainGen.populate(provider, world, rand, chunkX, chunkZ, hasGeneratedVillage, ANIMALS)) {
  289.             SpawnerAnimals.performWorldGenSpawning(this.world, biomegenbase, k + 8, l + 8, 16, 16, this.rand);
  290.         }
  291.         k += 8;
  292.         l += 8;
  293.  
  294.         doGen = TerrainGen.populate(provider, world, rand, chunkX, chunkZ, hasGeneratedVillage, ICE);
  295.         for (k1 = 0; doGen && k1 < 16; ++k1) {
  296.             for (l1 = 0; l1 < 16; ++l1) {
  297.                 i2 = this.world.getPrecipitationHeight(k + k1, l + l1);
  298.  
  299.                 if (this.world.isBlockFreezable(k1 + k, i2 - 1, l1 + l)) {
  300.                     this.world.setBlock(k1 + k, i2 - 1, l1 + l, Blocks.ice, 0, 2);
  301.                 }
  302.  
  303.                 if (this.world.func_147478_e(k1 + k, i2, l1 + l, true)) {
  304.                     this.world.setBlock(k1 + k, i2, l1 + l, Blocks.snow_layer, 0, 2);
  305.                 }
  306.             }
  307.         }
  308.  
  309.         MinecraftForge.EVENT_BUS.post(new PopulateChunkEvent.Post(provider, world, rand, chunkX, chunkZ, hasGeneratedVillage));
  310.  
  311.         BlockFalling.fallInstantly = false;
  312.     }
  313.  
  314.     @Override
  315.     public boolean saveChunks(boolean var1, IProgressUpdate var2) {
  316.         return true;
  317.     }
  318.  
  319.     @Override
  320.     public boolean unloadQueuedChunks() {
  321.         return false;
  322.     }
  323.  
  324.     @Override
  325.     public boolean canSave() {
  326.         return true;
  327.     }
  328.  
  329.     @Override
  330.     public String makeString() {
  331.         return "RandomLevelSource";
  332.     }
  333.  
  334.     @Override
  335.     public List getPossibleCreatures(EnumCreatureType par1EnumCreatureType, int par2, int par3, int par4) {
  336.         BiomeGenBase biomegenbase = this.world.getBiomeGenForCoords(par2, par4);
  337.         return par1EnumCreatureType == EnumCreatureType.monster && this.scatteredFeatureGenerator.func_143030_a(par2, par3, par4) ? this.scatteredFeatureGenerator
  338.                 .getScatteredFeatureSpawnList() : biomegenbase.getSpawnableList(par1EnumCreatureType);
  339.     }
  340.  
  341.     @Override
  342.     // findClosestStructure
  343.     public ChunkPosition func_147416_a(World world, String structure, int x, int y, int z) {
  344.         return "Stronghold".equals(structure) && this.strongholdGenerator != null ? this.strongholdGenerator.func_151545_a(world, x, y, z) : null;
  345.     }
  346.  
  347.     @Override
  348.     public int getLoadedChunkCount() {
  349.         return 0;
  350.     }
  351.  
  352.     @Override
  353.     public void recreateStructures(int x, int z) {
  354.         if (this.mapFeaturesEnabled) {
  355.             this.mineshaftGenerator.func_151539_a(this, this.world, x, z, (Block[]) null);
  356.             this.villageGenerator.func_151539_a(this, this.world, x, z, (Block[]) null);
  357.             this.strongholdGenerator.func_151539_a(this, this.world, x, z, (Block[]) null);
  358.             this.scatteredFeatureGenerator.func_151539_a(this, this.world, x, z, (Block[]) null);
  359.         }
  360.     }
  361.  
  362.     public static int round(double d) {
  363.         return (int) Math.round(d);
  364.     }
  365.  
  366.     @Override
  367.     public void saveExtraData() {
  368.  
  369.     }
  370.  
  371. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement