|
| 1 | +package supersymmetry.common.world.gen; |
| 2 | + |
| 3 | +import java.util.Random; |
| 4 | + |
| 5 | +import net.minecraft.block.Block; |
| 6 | +import net.minecraft.block.state.IBlockState; |
| 7 | +import net.minecraft.init.Blocks; |
| 8 | +import net.minecraft.util.math.BlockPos; |
| 9 | +import net.minecraft.util.math.MathHelper; |
| 10 | +import net.minecraft.world.World; |
| 11 | +import net.minecraft.world.chunk.ChunkPrimer; |
| 12 | +import net.minecraft.world.gen.MapGenBase; |
| 13 | + |
| 14 | +import com.google.common.base.MoreObjects; |
| 15 | + |
| 16 | +import gregtech.common.blocks.MetaBlocks; |
| 17 | +import gregtech.common.blocks.StoneVariantBlock; |
| 18 | +import supersymmetry.common.blocks.SuSyBlocks; |
| 19 | +import supersymmetry.common.blocks.SusyStoneVariantBlock; |
| 20 | + |
| 21 | +public class MapGenLunarLavaTube extends MapGenBase { |
| 22 | + |
| 23 | + protected static final IBlockState AIR = Blocks.AIR.getDefaultState(); |
| 24 | + public static final IBlockState BASALT = MetaBlocks.STONE_BLOCKS.get(StoneVariantBlock.StoneVariant.SMOOTH) |
| 25 | + .getState(StoneVariantBlock.StoneType.BASALT); |
| 26 | + public static final Block PIT = Blocks.END_PORTAL_FRAME; |
| 27 | + |
| 28 | + // modified from net.minecraft.world.gen.MapGenCaves |
| 29 | + protected void addTunnel(long seed, int x, int z, ChunkPrimer primer, double startX, double startY, double startZ, |
| 30 | + float widthDiff, float yaw, float pitch, int currentLength, int length, double squish, |
| 31 | + double width) { |
| 32 | + double centerX = x * 16 + 8; |
| 33 | + double centerZ = z * 16 + 8; |
| 34 | + float deltaYaw = 0.0F; |
| 35 | + float deltaPitch = 0.0F; |
| 36 | + Random random = new Random(seed); |
| 37 | + |
| 38 | + if (length <= 0) { |
| 39 | + int i = this.range * 16 - 16; |
| 40 | + length = i - random.nextInt(i / 4); |
| 41 | + } |
| 42 | + |
| 43 | + int j = random.nextInt(length); |
| 44 | + |
| 45 | + boolean deep = random.nextInt(6) == 0; |
| 46 | + for (; currentLength < length; ++currentLength) { |
| 47 | + double currentWidth = width + widthDiff; |
| 48 | + double height = currentWidth * squish; |
| 49 | + float cos = MathHelper.cos(pitch); |
| 50 | + float sin = MathHelper.sin(pitch); |
| 51 | + startX += MathHelper.cos(yaw) * cos; |
| 52 | + startY += sin; |
| 53 | + startZ += MathHelper.sin(yaw) * cos; |
| 54 | + |
| 55 | + if (deep) { |
| 56 | + pitch *= 0x.fp0f; |
| 57 | + if (random.nextInt(0x20) == 0) deep = false; |
| 58 | + } else { |
| 59 | + pitch *= 0.7F; |
| 60 | + if (random.nextInt(0x100) == 0) deep = true; |
| 61 | + } |
| 62 | + |
| 63 | + pitch += deltaPitch * 0.1F; |
| 64 | + yaw += deltaYaw * 0.1F; |
| 65 | + deltaPitch *= 0.9F; |
| 66 | + deltaYaw *= 0x.cp0f; |
| 67 | + widthDiff *= 0x.fp0f; |
| 68 | + if (currentLength > length * 0x.fp0) width *= 0x.f8p0; |
| 69 | + // generate upwards for better lava flow merge |
| 70 | + deltaPitch += (random.nextFloat() - random.nextFloat()) * random.nextFloat() * 1.0F + 0x.08p0f; |
| 71 | + deltaYaw += (random.nextFloat() - random.nextFloat()) * random.nextFloat() * 0x.cp0f; |
| 72 | + widthDiff += (random.nextFloat() - random.nextFloat()) * random.nextFloat() * 0x.8p0f; |
| 73 | + if (startY + height > 0x38) deltaPitch -= 0x.1p0f; |
| 74 | + if (startY + height > 0x3c) { |
| 75 | + deltaPitch -= 0x.4p0f; |
| 76 | + if (deep) { |
| 77 | + pitch = 0; |
| 78 | + deep = false; |
| 79 | + } |
| 80 | + } |
| 81 | + if (startY + height > 0x40) pitch = -0x.1p0f; |
| 82 | + |
| 83 | + if (width < 2) deltaPitch -= 1; |
| 84 | + |
| 85 | + if (currentLength == j && width > 5) { |
| 86 | + double large = width * (1 - random.nextFloat() * random.nextFloat() * 0x.6p0); |
| 87 | + double small = MathHelper.sqrt(width * width - large * large); |
| 88 | + if (large < 3) large = 3; |
| 89 | + if (small < 3) small = 3; |
| 90 | + double newLen = (length - currentLength) * random.nextFloat(0x.8p0f, 0x1.4p0f); |
| 91 | + this.addTunnel(random.nextLong(), x, z, primer, startX, startY, startZ, (float) (width - large), |
| 92 | + yaw + random.nextFloat() * 0x.8p0f - 0x.4p0f, |
| 93 | + pitch * 0x.ep0f + random.nextFloat() * 0x.2p0f - 0x.1p0f, currentLength, length, squish, large); |
| 94 | + this.addTunnel(random.nextLong(), x, z, primer, startX, startY, startZ, |
| 95 | + (float) (width - small) * 0x.cp0f, |
| 96 | + yaw + random.nextFloat() * 0x4p0f - 0x2p0f, |
| 97 | + pitch / 2.0F + random.nextFloat() * 0x.8p0f - 0x.4p0f, (int) (newLen * currentLength / length), |
| 98 | + (int) newLen, squish / 2 + 0x.8p0, small); |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + if (random.nextInt(4) != 0) { |
| 103 | + double offsetX = startX - centerX; |
| 104 | + double offsetZ = startZ - centerZ; |
| 105 | + double nearEnd = length - currentLength; |
| 106 | + double d7 = currentWidth + widthDiff + 1f + 16.0F; |
| 107 | + |
| 108 | + if (offsetX * offsetX + offsetZ * offsetZ - nearEnd * nearEnd > d7 * d7) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + double basaltFillLevel = currentLength < length * 0x.1p0 ? |
| 113 | + (0x.1p0 - (double) currentLength / length) : |
| 114 | + currentLength > length * 0x.fp0 ? |
| 115 | + (-0x.fp0 + (double) currentLength / length) : |
| 116 | + 0; // 0 ~ 0x.1 |
| 117 | + basaltFillLevel *= 0x180 * basaltFillLevel; // 0 ~ 0x1.8 |
| 118 | + double stoneFillLevel = basaltFillLevel * basaltFillLevel - 0x1.2p0; // -0x1.2 ~ 0x1.2; |
| 119 | + |
| 120 | + long localRandomSeed = random.nextLong(); |
| 121 | + boolean canHavePit = random.nextInt(8) == 0; |
| 122 | + |
| 123 | + // carving spheres |
| 124 | + if (startX >= centerX - 16.0D - currentWidth * 2.0D && |
| 125 | + startZ >= centerZ - 16.0D - currentWidth * 2.0D && |
| 126 | + startX <= centerX + 16.0D + currentWidth * 2.0D && |
| 127 | + startZ <= centerZ + 16.0D + currentWidth * 2.0D) { |
| 128 | + int x1 = MathHelper.floor(startX - currentWidth) - x * 16 - 1; |
| 129 | + int x2 = MathHelper.floor(startX + currentWidth) - x * 16 + 1; |
| 130 | + int y1 = MathHelper.floor(startY - height) - 1; |
| 131 | + int y2 = MathHelper.floor(startY + height) + 1; |
| 132 | + int z1 = MathHelper.floor(startZ - currentWidth) - z * 16 - 1; |
| 133 | + int z2 = MathHelper.floor(startZ + currentWidth) - z * 16 + 1; |
| 134 | + |
| 135 | + if (x1 < 0) x1 = 0; |
| 136 | + if (x2 > 16) x2 = 16; |
| 137 | + if (y1 < 1) y1 = 1; |
| 138 | + if (y2 > 248) y2 = 248; |
| 139 | + if (z1 < 0) z1 = 0; |
| 140 | + if (z2 > 16) z2 = 16; |
| 141 | + |
| 142 | + // for consistency |
| 143 | + Random localRandom = new Random(localRandomSeed); |
| 144 | + |
| 145 | + int x3 = MathHelper.floor(startX) - x * 16; |
| 146 | + int z3 = MathHelper.floor(startZ) - z * 16; |
| 147 | + if (0 <= x3 && x3 < 16 && 0 < z3 && z3 < 16 && width > 0x3 && localRandom.nextInt(0x4) == 1 && y2 > primer.findGroundBlockIdx(x3, z3 - 1) - 2) { |
| 148 | + fillBlock(primer, x3, 0x60, z3, null, AIR, PIT.getStateFromMeta(width > 0xa ? 7 : (int) (width - 3))); |
| 149 | + } |
| 150 | + |
| 151 | + // lx: local x |
| 152 | + for (int localX = x1; localX < x2; ++localX) { |
| 153 | + double distX = ((double) (localX + x * 16) + 0.5D - startX) / currentWidth; |
| 154 | + |
| 155 | + for (int localZ = z1; localZ < z2; ++localZ) { |
| 156 | + double distZ = ((double) (localZ + z * 16) + 0.5D - startZ) / currentWidth; |
| 157 | + boolean foundTop = false; |
| 158 | + |
| 159 | + if (distX * distX + distZ * distZ < 1.0D) { |
| 160 | + int localY = y2; |
| 161 | + float lavacicles = y2 > 0x3c ? 1 : localRandom.nextFloat(0x.ap0f, 0x1.4p0f); |
| 162 | + for (; localY > y1; --localY) { |
| 163 | + double distY = ((double) (localY - 1) + 0.5D - startY) / height; |
| 164 | + if (distY > 0) distY *= lavacicles; |
| 165 | + |
| 166 | + if (distX * distX + distY * distY + distZ * distZ < 1.0D) { |
| 167 | + IBlockState state = primer.getBlockState(localX, localY, localZ); |
| 168 | + IBlockState up = MoreObjects |
| 169 | + .firstNonNull(primer.getBlockState(localX, localY + 1, localZ), AIR); |
| 170 | + |
| 171 | + if (isTopBlock(primer, localX, localY, localZ, x, z)) { |
| 172 | + foundTop = true; |
| 173 | + } |
| 174 | + |
| 175 | + double edge = distX * distX + distZ * distZ < 0x.5p0 ? 0 : |
| 176 | + distX * distX + distZ * distZ - 0x.5p0; |
| 177 | + if (distY > stoneFillLevel) { |
| 178 | + if (distY - edge > -0.5D + basaltFillLevel) { |
| 179 | + digBlock(primer, localX, localY, localZ, x, z, foundTop, state, up); |
| 180 | + } else { |
| 181 | + fillBlock(primer, localX, localY, localZ, true, state, BASALT); |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + protected boolean canReplaceBlock(IBlockState state, IBlockState up) { |
| 195 | + Block block = state.getBlock(); |
| 196 | + return block == SuSyBlocks.SUSY_STONE_BLOCKS.get(SusyStoneVariantBlock.StoneVariant.SMOOTH) || |
| 197 | + block == SuSyBlocks.REGOLITH || |
| 198 | + block == MetaBlocks.STONE_BLOCKS.get(StoneVariantBlock.StoneVariant.SMOOTH) || block == Blocks.AIR; |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public void generate(World worldIn, int x, int z, ChunkPrimer primer) { |
| 203 | + this.range = 32; // maybe it's too large? |
| 204 | + super.generate(worldIn, x, z, primer); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Recursively called by generate() |
| 209 | + */ |
| 210 | + protected void recursiveGenerate(World worldIn, int chunkX, int chunkZ, int originalX, int originalZ, |
| 211 | + ChunkPrimer chunkPrimerIn) { |
| 212 | + int i = 1; |
| 213 | + |
| 214 | + if (this.rand.nextInt(16) == 0) { |
| 215 | + i = rand.nextInt(2); |
| 216 | + } |
| 217 | + |
| 218 | + if (this.rand.nextInt(0x30) != 0) { |
| 219 | + i = 0; |
| 220 | + } |
| 221 | + |
| 222 | + for (int j = 0; j < i; ++j) { |
| 223 | + double startX = chunkX * 16 + this.rand.nextInt(16); |
| 224 | + double startY = this.rand.nextInt(this.rand.nextInt(16) + 4) + 4; |
| 225 | + double startZ = chunkZ * 16 + this.rand.nextInt(16); |
| 226 | + int k = 1; |
| 227 | + |
| 228 | + if (this.rand.nextInt(4) == 0) { |
| 229 | + this.addRoom(this.rand.nextLong(), originalX, originalZ, chunkPrimerIn, startX, startY, startZ); |
| 230 | + k += this.rand.nextInt(4); |
| 231 | + } |
| 232 | + |
| 233 | + for (int l = 0; l < k; ++l) { |
| 234 | + float yaw = this.rand.nextFloat() * ((float) Math.PI * 2F); |
| 235 | + float pitch = (this.rand.nextFloat() - 0.5F) * 0x0.2p0f; |
| 236 | + float f2 = this.rand.nextFloat() * 0x.4p0f; |
| 237 | + |
| 238 | + this.addTunnel(this.rand.nextLong(), originalX, originalZ, chunkPrimerIn, startX, startY, startZ, f2, |
| 239 | + yaw, pitch, 0, 0, 0x.cp0, |
| 240 | + rand.nextFloat() * (rand.nextFloat() * 0x10) + rand.nextFloat() * 2 + 2); |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + private void addRoom(long l, int originalX, int originalZ, ChunkPrimer chunkPrimerIn, double d0, double d1, |
| 246 | + double d2) {} |
| 247 | + |
| 248 | + private boolean isTopBlock(ChunkPrimer data, int x, int y, int z, int chunkX, int chunkZ) { |
| 249 | + net.minecraft.world.biome.Biome biome = world.getBiome(new BlockPos(x + chunkX * 16, 0, z + chunkZ * 16)); |
| 250 | + IBlockState state = data.getBlockState(x, y, z); |
| 251 | + return state.getBlock() == biome.topBlock; |
| 252 | + } |
| 253 | + |
| 254 | + protected void digBlock(ChunkPrimer data, int x, int y, int z, int chunkX, int chunkZ, boolean foundTop, |
| 255 | + IBlockState state, IBlockState up) { |
| 256 | + net.minecraft.world.biome.Biome biome = world.getBiome(new BlockPos(x + chunkX * 16, 0, z + chunkZ * 16)); |
| 257 | + IBlockState top = biome.topBlock; |
| 258 | + IBlockState filler = biome.fillerBlock; |
| 259 | + |
| 260 | + if (this.canReplaceBlock(state, up) || state.getBlock() == top.getBlock() || |
| 261 | + state.getBlock() == filler.getBlock()) { |
| 262 | + if (y < 6) { |
| 263 | + data.setBlockState(x, y, z, BASALT); |
| 264 | + } else { |
| 265 | + data.setBlockState(x, y, z, AIR); |
| 266 | + |
| 267 | + if (foundTop && data.getBlockState(x, y - 1, z).getBlock() == filler.getBlock()) { |
| 268 | + data.setBlockState(x, y - 1, z, top.getBlock().getDefaultState()); |
| 269 | + } |
| 270 | + } |
| 271 | + } |
| 272 | + } |
| 273 | + |
| 274 | + // replace = null for both |
| 275 | + protected void fillBlock(ChunkPrimer primer, int x, int y, int z, Boolean replace, IBlockState state, |
| 276 | + IBlockState toReplace) { |
| 277 | + if (canReplaceBlock(state, null) && (replace == null || ((state == AIR) ^ replace))) { |
| 278 | + primer.setBlockState(x, y, z, toReplace); |
| 279 | + } |
| 280 | + } |
| 281 | +} |
0 commit comments