Guest User

Untitled

a guest
Jul 19th, 2021
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.17 KB | None | 0 0
  1. import net.minecraft.block.BlockFalling;
  2. import net.minecraft.block.state.IBlockState;
  3. import net.minecraft.entity.EnumCreatureType;
  4. import net.minecraft.init.Blocks;
  5. import net.minecraft.util.math.BlockPos;
  6. import net.minecraft.util.math.MathHelper;
  7. import net.minecraft.world.World;
  8. import net.minecraft.world.WorldEntitySpawner;
  9. import net.minecraft.world.biome.Biome;
  10. import net.minecraft.world.chunk.Chunk;
  11. import net.minecraft.world.chunk.ChunkPrimer;
  12. import net.minecraft.world.gen.IChunkGenerator;
  13. import net.minecraft.world.gen.MapGenBase;
  14. import net.minecraft.world.gen.NoiseGeneratorOctaves;
  15. import net.minecraft.world.gen.NoiseGeneratorPerlin;
  16. import net.minecraft.world.gen.feature.WorldGenLakes;
  17. import net.minecraftforge.event.ForgeEventFactory;
  18. import net.minecraftforge.event.terraingen.PopulateChunkEvent;
  19.  
  20. import javax.annotation.Nonnull;
  21. import javax.annotation.Nullable;
  22. import javax.annotation.ParametersAreNonnullByDefault;
  23. import java.util.List;
  24. import java.util.Random;
  25.  
  26. public class ChunkGeneratorDarkness implements IChunkGenerator {
  27.  
  28. public static final IBlockState ROCK = Blocks.STONE.getDefaultState();
  29. public static final IBlockState OCEAN_BLOCK = Blocks.WATER.getDefaultState();
  30.  
  31. private final Random rand;
  32. private NoiseGeneratorOctaves minLimitPerlinNoise;
  33. private NoiseGeneratorOctaves maxLimitPerlinNoise;
  34. private NoiseGeneratorOctaves mainPerlinNoise;
  35. private NoiseGeneratorPerlin surfaceNoise;
  36. public NoiseGeneratorOctaves scaleNoise;
  37. public NoiseGeneratorOctaves depthNoise;
  38. public NoiseGeneratorOctaves forestNoise;
  39. private final World world;
  40. private final double[] heightMap;
  41. private final float[] biomeWeights;
  42. private double[] depthBuffer = new double[256];
  43. private Biome[] biomesForGeneration;
  44. double[] mainNoiseRegion;
  45. double[] minLimitRegion;
  46. double[] maxLimitRegion;
  47. double[] depthRegion;
  48.  
  49. public ChunkGeneratorDarkness(World world){
  50. this.world = world;
  51. this.rand = new Random(world.getSeed());
  52. this.minLimitPerlinNoise = new NoiseGeneratorOctaves(this.rand,16);
  53. this.maxLimitPerlinNoise = new NoiseGeneratorOctaves(this.rand,16);
  54. this.surfaceNoise = new NoiseGeneratorPerlin(this.rand, 4);
  55. this.scaleNoise = new NoiseGeneratorOctaves(this.rand, 10);
  56. this.depthNoise = new NoiseGeneratorOctaves(this.rand, 16);
  57. this.forestNoise = new NoiseGeneratorOctaves(this.rand, 8);
  58. this.heightMap = new double[825];
  59. this.biomeWeights = new float[25];
  60. for (int i = -2; i <= 2; ++i) {
  61. for (int j = -2; j <= 2; ++j) {
  62. float f = 10.0F / MathHelper.sqrt((float) (i * i + j * j) + 0.2F);
  63. this.biomeWeights[i + 2 + (j + 2) * 5] = f;
  64. }
  65. }
  66.  
  67. net.minecraftforge.event.terraingen.InitNoiseGensEvent.ContextOverworld ctx = new net.minecraftforge.event.terraingen.InitNoiseGensEvent.ContextOverworld(minLimitPerlinNoise, maxLimitPerlinNoise, mainPerlinNoise, surfaceNoise, scaleNoise, depthNoise, forestNoise);
  68. ctx = net.minecraftforge.event.terraingen.TerrainGen.getModdedNoiseGenerators(world, this.rand, ctx);
  69. this.minLimitPerlinNoise = ctx.getLPerlin1();
  70. this.maxLimitPerlinNoise = ctx.getLPerlin2();
  71. this.mainPerlinNoise = ctx.getPerlin();
  72. this.surfaceNoise = ctx.getHeight();
  73. this.scaleNoise = ctx.getScale();
  74. this.depthNoise = ctx.getDepth();
  75. this.forestNoise = ctx.getForest();
  76. }
  77.  
  78. public void setBlocksInChunk(int x, int z, ChunkPrimer primer) {
  79. this.biomesForGeneration = this.world.getBiomeProvider().getBiomesForGeneration(this.biomesForGeneration, x * 4 - 2, z * 4 - 2, 10, 10);
  80. this.generateHeightmap(x * 4, z * 4);
  81.  
  82. for (int i = 0; i < 4; ++i) {
  83. int j = i * 5;
  84. int k = (i + 1) * 5;
  85.  
  86. for (int l = 0; l < 4; ++l) {
  87. int i1 = (j + l) * 33;
  88. int j1 = (j + l + 1) * 33;
  89. int k1 = (k + l) * 33;
  90. int l1 = (k + l + 1) * 33;
  91.  
  92. for (int i2 = 0; i2 < 32; ++i2) {
  93. //double d0 = 0.125D;
  94. double d1 = this.heightMap[i1 + i2];
  95. double d2 = this.heightMap[j1 + i2];
  96. double d3 = this.heightMap[k1 + i2];
  97. double d4 = this.heightMap[l1 + i2];
  98. double d5 = (this.heightMap[i1 + i2 + 1] - d1) * 0.125D;
  99. double d6 = (this.heightMap[j1 + i2 + 1] - d2) * 0.125D;
  100. double d7 = (this.heightMap[k1 + i2 + 1] - d3) * 0.125D;
  101. double d8 = (this.heightMap[l1 + i2 + 1] - d4) * 0.125D;
  102.  
  103. for (int j2 = 0; j2 < 8; ++j2) {
  104. //double d9 = 0.25D;
  105. double d10 = d1;
  106. double d11 = d2;
  107. double d12 = (d3 - d1) * 0.25D;
  108. double d13 = (d4 - d2) * 0.25D;
  109.  
  110. for (int k2 = 0; k2 < 4; ++k2) {
  111. //double d14 = 0.25D;
  112. double d16 = (d11 - d10) * 0.25D;
  113. double lvt_45_1_ = d10 - d16;
  114.  
  115. for (int l2 = 0; l2 < 4; ++l2) {
  116. if ((lvt_45_1_ += d16) > 0.0D) {
  117. primer.setBlockState(i * 4 + k2, i2 * 8 + j2, l * 4 + l2, ROCK);
  118. } else if (i2 * 8 + j2 < world.getSeaLevel()) {
  119. primer.setBlockState(i * 4 + k2, i2 * 8 + j2, l * 4 + l2, OCEAN_BLOCK);
  120. }
  121. }
  122.  
  123. d10 += d12;
  124. d11 += d13;
  125. }
  126.  
  127. d1 += d5;
  128. d2 += d6;
  129. d3 += d7;
  130. d4 += d8;
  131. }
  132. }
  133. }
  134. }
  135. }
  136.  
  137. public void replaceBiomeBlocks(int x, int z, ChunkPrimer primer, Biome[] biomesIn) {
  138. if (!net.minecraftforge.event.ForgeEventFactory.onReplaceBiomeBlocks(this, x, z, primer, this.world)) return;
  139. //double d0 = 0.03125D;
  140. this.depthBuffer = this.surfaceNoise.getRegion(this.depthBuffer, (double) (x * 16), (double) (z * 16), 16, 16, 0.0625D, 0.0625D, 1.0D);
  141.  
  142. for (int i = 0; i < 16; ++i) {
  143. for (int j = 0; j < 16; ++j) {
  144. Biome biome = biomesIn[j + i * 16];
  145. biome.genTerrainBlocks(this.world, this.rand, primer, x * 16 + i, z * 16 + j, this.depthBuffer[j + i * 16]);
  146. }
  147. }
  148. }
  149.  
  150. @Nonnull
  151. @Override
  152. public Chunk generateChunk(int x, int z) {
  153. this.rand.setSeed((long) x * 341873128712L + (long) z * 132897987541L);
  154. ChunkPrimer chunkprimer = new ChunkPrimer();
  155. this.setBlocksInChunk(x, z, chunkprimer);
  156. this.biomesForGeneration = this.world.getBiomeProvider().getBiomes(this.biomesForGeneration, x * 16, z * 16, 16, 16);
  157. this.replaceBiomeBlocks(x, z, chunkprimer, this.biomesForGeneration);
  158.  
  159. Chunk chunk = new Chunk(this.world, chunkprimer, x, z);
  160. byte[] abyte = chunk.getBiomeArray();
  161.  
  162. for (int i = 0; i < abyte.length; ++i) {
  163. abyte[i] = (byte) Biome.getIdForBiome(this.biomesForGeneration[i]);
  164. }
  165.  
  166. chunk.generateSkylightMap();
  167. return chunk;
  168. }
  169.  
  170. private void generateHeightmap(int x, int z) {
  171. this.depthRegion = this.depthNoise.generateNoiseOctaves(this.depthRegion, x, z, 5, 5, 200D, 200D, 0.5D);
  172. float f = 684.412F;
  173. float f1 = 684.412F;
  174. this.mainNoiseRegion = this.mainPerlinNoise.generateNoiseOctaves(this.mainNoiseRegion, x, 0, z, 5, 33, 5, f / 80D, f1 / 160D, f / 80D);
  175. this.minLimitRegion = this.minLimitPerlinNoise.generateNoiseOctaves(this.minLimitRegion, x, 0, z, 5, 33, 5, (double) f, (double) f1, (double) f);
  176. this.maxLimitRegion = this.maxLimitPerlinNoise.generateNoiseOctaves(this.maxLimitRegion, x, 0, z, 5, 33, 5, (double) f, (double) f1, (double) f);
  177. int i = 0;
  178. int j = 0;
  179.  
  180. for (int k = 0; k < 5; ++k) {
  181. for (int l = 0; l < 5; ++l) {
  182. float f2 = 0.0F;
  183. float f3 = 0.0F;
  184. float f4 = 0.0F;
  185. //int i1 = 2;
  186. Biome biome = this.biomesForGeneration[k + 2 + (l + 2) * 10];
  187.  
  188. for (int j1 = -2; j1 <= 2; ++j1) {
  189. for (int k1 = -2; k1 <= 2; ++k1) {
  190. Biome biome1 = this.biomesForGeneration[k + j1 + 2 + (l + k1 + 2) * 10];
  191. float f5 = biome1.getBaseHeight();
  192. float f6 = biome1.getHeightVariation();
  193.  
  194. float f7 = this.biomeWeights[j1 + 2 + (k1 + 2) * 5] / (f5 + 2.0F);
  195.  
  196. if (biome1.getBaseHeight() > biome.getBaseHeight()) {
  197. f7 /= 2.0F;
  198. }
  199.  
  200. f2 += f6 * f7;
  201. f3 += f5 * f7;
  202. f4 += f7;
  203. }
  204. }
  205.  
  206. f2 = f2 / f4;
  207. f3 = f3 / f4;
  208. f2 = f2 * 0.9F + 0.1F;
  209. f3 = (f3 * 4.0F - 1.0F) / 8.0F;
  210. double d7 = this.depthRegion[j] / 8000.0D;
  211.  
  212. if (d7 < 0.0D) {
  213. d7 = -d7 * 0.3D;
  214. }
  215.  
  216. d7 = d7 * 3.0D - 2.0D;
  217.  
  218. if (d7 < 0.0D) {
  219. d7 = d7 / 2.0D;
  220.  
  221. if (d7 < -1.0D) {
  222. d7 = -1.0D;
  223. }
  224.  
  225. d7 = d7 / 1.4D;
  226. d7 = d7 / 2.0D;
  227. } else {
  228. if (d7 > 1.0D) {
  229. d7 = 1.0D;
  230. }
  231.  
  232. d7 = d7 / 8.0D;
  233. }
  234.  
  235. ++j;
  236. double d8 = (double) f3;
  237. double d9 = (double) f2;
  238. d8 = d8 + d7 * 0.2D;
  239. d8 = d8 * 8.5D / 8.0D;
  240. double d0 = 8.5D + d8 * 4.0D;
  241.  
  242. for (int l1 = 0; l1 < 33; ++l1) {
  243. double d1 = ((double) l1 - d0) * 12D * 128.0D / 256.0D / d9;
  244.  
  245. if (d1 < 0.0D) {
  246. d1 *= 4.0D;
  247. }
  248.  
  249. double d2 = this.minLimitRegion[i] / 512D;
  250. double d3 = this.maxLimitRegion[i] / 512D;
  251. double d4 = (this.mainNoiseRegion[i] / 10.0D + 1.0D) / 2.0D;
  252. double d5 = MathHelper.clampedLerp(d2, d3, d4) - d1;
  253.  
  254. if (l1 > 29) {
  255. double d6 = (double) ((float) (l1 - 29) / 3.0F);
  256. d5 = d5 * (1.0D - d6) + -10.0D * d6;
  257. }
  258.  
  259. this.heightMap[i] = d5;
  260. ++i;
  261. }
  262. }
  263. }
  264. }
  265.  
  266. @Override
  267. public void populate(int x, int z) {
  268. BlockFalling.fallInstantly = true;
  269. int i = x * 16;
  270. int j = z * 16;
  271. BlockPos blockpos = new BlockPos(i, 0, j);
  272. Biome biome = this.world.getBiome(blockpos.add(16, 0, 16));
  273. this.rand.setSeed(this.world.getSeed());
  274. long k = this.rand.nextLong() / 2L * 2L + 1L;
  275. long l = this.rand.nextLong() / 2L * 2L + 1L;
  276. this.rand.setSeed((long) x * k + (long) z * l ^ this.world.getSeed());
  277.  
  278. ForgeEventFactory.onChunkPopulate(true, this, this.world, this.rand, x, z, false);
  279.  
  280. if (net.minecraftforge.event.terraingen.TerrainGen.populate(this, this.world, this.rand, x, z, false, PopulateChunkEvent.Populate.EventType.CUSTOM))
  281. if (this.rand.nextInt(25) == 0) {
  282. int x0 = this.rand.nextInt(16) + 8;
  283. int y0 = this.rand.nextInt(256);
  284. int z0 = this.rand.nextInt(16) + 8;
  285. (new WorldGenLakes(OCEAN_BLOCK.getBlock())).generate(this.world, this.rand, blockpos.add(x0, y0, z0));
  286. }
  287.  
  288. if (net.minecraftforge.event.terraingen.TerrainGen.populate(this, this.world, this.rand, x, z, false, PopulateChunkEvent.Populate.EventType.CUSTOM))
  289. if (this.rand.nextInt(8) == 0) {
  290. int x0 = this.rand.nextInt(16) + 8;
  291. int y0 = this.rand.nextInt(this.rand.nextInt(248) + 8);
  292. int z0 = this.rand.nextInt(16) + 8;
  293.  
  294. if (y0 < this.world.getSeaLevel()) {
  295. (new WorldGenLakes(Blocks.WATER)).generate(this.world, this.rand, blockpos.add(x0, y0, z0));
  296. }
  297. }
  298.  
  299. biome.decorate(this.world, this.rand, new BlockPos(i, 0, j));
  300. WorldEntitySpawner.performWorldGenSpawning(this.world, biome, i + 8, j + 8, 16, 16, this.rand);
  301.  
  302. ForgeEventFactory.onChunkPopulate(false, this, this.world, this.rand, x, z, false);
  303.  
  304. BlockFalling.fallInstantly = false;
  305. }
  306.  
  307. @Override
  308. @ParametersAreNonnullByDefault
  309. public boolean generateStructures(Chunk chunkIn, int x, int z) {
  310. return false;
  311. }
  312.  
  313. @Nonnull
  314. @Override
  315. @ParametersAreNonnullByDefault
  316. public List<Biome.SpawnListEntry> getPossibleCreatures(EnumCreatureType creatureType, BlockPos pos) {
  317. return this.world.getBiome(pos).getSpawnableList(creatureType);
  318. }
  319.  
  320. @Override
  321. @ParametersAreNonnullByDefault
  322. public boolean isInsideStructure(World worldIn, String structureName, BlockPos pos) {
  323. return false;
  324. }
  325.  
  326. @Override
  327. @Nullable
  328. @ParametersAreNonnullByDefault
  329. public BlockPos getNearestStructurePos(World worldIn, String structureName, BlockPos position, boolean findUnexplored) {
  330. return null;
  331. }
  332.  
  333. /**
  334. * Recreates data about structures intersecting given chunk (used for example by getPossibleCreatures), without
  335. * placing any blocks. When called for the first time before any chunk is generated - also initializes the internal
  336. * state needed by getPossibleCreatures.
  337. */
  338. @Override
  339. @ParametersAreNonnullByDefault
  340. public void recreateStructures(Chunk chunkIn, int x, int z) {
  341. }
  342. }
Advertisement
Add Comment
Please, Sign In to add comment