From 37394eba114387f59ca4242104cfaa26f6c3a0ba Mon Sep 17 00:00:00 2001 From: paulevsGitch Date: Mon, 21 Jun 2021 21:38:56 +0300 Subject: [PATCH] Parallel cave generation & block fixing --- .../java/ru/betterend/util/BlockFixer.java | 328 +++++++++--------- .../terrain/caves/EndCaveFeature.java | 22 +- .../terrain/caves/RoundCaveFeature.java | 69 ++-- .../terrain/caves/TunelCaveFeature.java | 59 ++-- 4 files changed, 243 insertions(+), 235 deletions(-) diff --git a/src/main/java/ru/betterend/util/BlockFixer.java b/src/main/java/ru/betterend/util/BlockFixer.java index 9e9a0dcb..f0cdbc91 100644 --- a/src/main/java/ru/betterend/util/BlockFixer.java +++ b/src/main/java/ru/betterend/util/BlockFixer.java @@ -1,6 +1,7 @@ package ru.betterend.util; import java.util.Set; +import java.util.stream.IntStream; import com.google.common.collect.Sets; @@ -20,187 +21,194 @@ import ru.betterend.blocks.basis.FurBlock; import ru.betterend.registry.EndBlocks; public class BlockFixer { - private static final MutableBlockPos POS = new MutableBlockPos(); private static final BlockState AIR = Blocks.AIR.defaultBlockState(); private static final BlockState WATER = Blocks.WATER.defaultBlockState(); - + public static void fixBlocks(LevelAccessor world, BlockPos start, BlockPos end) { - BlockState state; - Set doubleCheck = Sets.newHashSet(); - for (int x = start.getX(); x <= end.getX(); x++) { - POS.setX(x); - for (int z = start.getZ(); z <= end.getZ(); z++) { - POS.setZ(z); - for (int y = start.getY(); y <= end.getY(); y++) { - POS.setY(y); - state = world.getBlockState(POS); - - if (state.getBlock() instanceof FurBlock) { - doubleCheck.add(POS.immutable()); - } - // Liquids - else if (!state.getFluidState().isEmpty()) { - if (!state.canSurvive(world, POS)) { - BlocksHelper.setWithoutUpdate(world, POS, WATER); - POS.setY(POS.getY() - 1); - state = world.getBlockState(POS); - while (!state.canSurvive(world, POS)) { - state = state.getFluidState().isEmpty() ? AIR : WATER; - BlocksHelper.setWithoutUpdate(world, POS, state); - POS.setY(POS.getY() - 1); - state = world.getBlockState(POS); - } - } - POS.setY(y - 1); - if (world.isEmptyBlock(POS)) { - POS.setY(y); - while (!world.getFluidState(POS).isEmpty()) { - BlocksHelper.setWithoutUpdate(world, POS, AIR); - POS.setY(POS.getY() + 1); - } - continue; - } - for (Direction dir : BlocksHelper.HORIZONTAL) { - if (world.isEmptyBlock(POS.relative(dir))) { - world.getLiquidTicks().scheduleTick(POS, state.getFluidState().getType(), 0); - break; - } - } - } - else if (state.is(EndBlocks.SMARAGDANT_CRYSTAL)) { + Set doubleCheck = Sets.newConcurrentHashSet(); + int dx = end.getX() - start.getX() + 1; + int dz = end.getZ() - start.getZ() + 1; + int count = dx * dz; + IntStream.range(0, count).parallel().forEach(index -> { + MutableBlockPos POS = new MutableBlockPos(); + POS.setX((index % dx) + start.getX()); + POS.setZ((index / dx) + start.getZ()); + BlockState state; + for (int y = start.getY(); y <= end.getY(); y++) { + POS.setY(y); + state = world.getBlockState(POS); + + if (state.getBlock() instanceof FurBlock) { + doubleCheck.add(POS.immutable()); + } + // Liquids + else if (!state.getFluidState().isEmpty()) { + if (!state.canSurvive(world, POS)) { + setWithoutUpdate(world, POS, WATER); POS.setY(POS.getY() - 1); - if (world.isEmptyBlock(POS)) { + state = world.getBlockState(POS); + while (!state.canSurvive(world, POS)) { + state = state.getFluidState().isEmpty() ? AIR : WATER; + setWithoutUpdate(world, POS, state); + POS.setY(POS.getY() - 1); + state = world.getBlockState(POS); + } + } + POS.setY(y - 1); + if (world.isEmptyBlock(POS)) { + POS.setY(y); + while (!world.getFluidState(POS).isEmpty()) { + setWithoutUpdate(world, POS, AIR); POS.setY(POS.getY() + 1); - while (state.is(EndBlocks.SMARAGDANT_CRYSTAL)) { - BlocksHelper.setWithoutUpdate(world, POS, AIR); + } + continue; + } + for (Direction dir : BlocksHelper.HORIZONTAL) { + if (world.isEmptyBlock(POS.relative(dir))) { + world.getLiquidTicks().scheduleTick(POS, state.getFluidState().getType(), 0); + break; + } + } + } + else if (state.is(EndBlocks.SMARAGDANT_CRYSTAL)) { + POS.setY(POS.getY() - 1); + if (world.isEmptyBlock(POS)) { + POS.setY(POS.getY() + 1); + while (state.is(EndBlocks.SMARAGDANT_CRYSTAL)) { + setWithoutUpdate(world, POS, AIR); + POS.setY(POS.getY() + 1); + state = world.getBlockState(POS); + } + } + } + else if (state.getBlock() instanceof StalactiteBlock) { + if (!state.canSurvive(world, POS)) { + if (world.getBlockState(POS.above()).getBlock() instanceof StalactiteBlock) { + while (state.getBlock() instanceof StalactiteBlock) { + setWithoutUpdate(world, POS, AIR); POS.setY(POS.getY() + 1); state = world.getBlockState(POS); } } - } - else if (state.getBlock() instanceof StalactiteBlock) { - if (!state.canSurvive(world, POS)) { - if (world.getBlockState(POS.above()).getBlock() instanceof StalactiteBlock) { - while (state.getBlock() instanceof StalactiteBlock) { - BlocksHelper.setWithoutUpdate(world, POS, AIR); - POS.setY(POS.getY() + 1); - state = world.getBlockState(POS); - } - } - else { - while (state.getBlock() instanceof StalactiteBlock) { - BlocksHelper.setWithoutUpdate(world, POS, AIR); - POS.setY(POS.getY() - 1); - state = world.getBlockState(POS); - } - } - } - } - else if (state.is(EndBlocks.CAVE_PUMPKIN)) { - if (!world.getBlockState(POS.above()).is(EndBlocks.CAVE_PUMPKIN_SEED)) { - BlocksHelper.setWithoutUpdate(world, POS, AIR); - } - } - else if (!state.canSurvive(world, POS)) { - // Chorus - if (state.is(Blocks.CHORUS_PLANT)) { - Set ends = Sets.newHashSet(); - Set add = Sets.newHashSet(); - ends.add(POS.immutable()); - - for (int i = 0; i < 64 && !ends.isEmpty(); i++) { - ends.forEach((pos) -> { - BlocksHelper.setWithoutUpdate(world, pos, AIR); - for (Direction dir : BlocksHelper.HORIZONTAL) { - BlockPos p = pos.relative(dir); - BlockState st = world.getBlockState(p); - if ((st.is(Blocks.CHORUS_PLANT) || st.is(Blocks.CHORUS_FLOWER)) && !st.canSurvive(world, p)) { - add.add(p); - } - } - BlockPos p = pos.above(); - BlockState st = world.getBlockState(p); - if ((st.is(Blocks.CHORUS_PLANT) || st.is(Blocks.CHORUS_FLOWER)) && !st.canSurvive(world, p)) { - add.add(p); - } - }); - ends.clear(); - ends.addAll(add); - add.clear(); - } - } - // Vines - else if (state.getBlock() instanceof BaseVineBlock) { - while (world.getBlockState(POS).getBlock() instanceof BaseVineBlock) { - BlocksHelper.setWithoutUpdate(world, POS, AIR); - POS.setY(POS.getY() - 1); - } - } - // Falling blocks - else if (state.getBlock() instanceof FallingBlock) { - BlockState falling = state; - - POS.setY(POS.getY() - 1); - state = world.getBlockState(POS); - - int ray = BlocksHelper.downRayRep(world, POS.immutable(), 64); - if (ray > 32) { - BlocksHelper.setWithoutUpdate(world, POS, Blocks.END_STONE.defaultBlockState()); - if (world.getRandom().nextBoolean()) { - POS.setY(POS.getY() - 1); - state = world.getBlockState(POS); - BlocksHelper.setWithoutUpdate(world, POS, Blocks.END_STONE.defaultBlockState()); - } - } - else { - POS.setY(y); - BlockState replacement = AIR; - for (Direction dir : BlocksHelper.HORIZONTAL) { - state = world.getBlockState(POS.relative(dir)); - if (!state.getFluidState().isEmpty()) { - replacement = state; - break; - } - } - BlocksHelper.setWithoutUpdate(world, POS, replacement); - POS.setY(y - ray); - BlocksHelper.setWithoutUpdate(world, POS, falling); - } - } - // Blocks without support else { - // Blue Vine - if (state.getBlock() instanceof BlueVineBlock) { - while (state.is(EndBlocks.BLUE_VINE) || state.is(EndBlocks.BLUE_VINE_LANTERN) || state.is(EndBlocks.BLUE_VINE_FUR)) { - BlocksHelper.setWithoutUpdate(world, POS, AIR); - POS.setY(POS.getY() + 1); - state = world.getBlockState(POS); - } - } - // Double plants - if (state.getBlock() instanceof BaseDoublePlantBlock) { - BlocksHelper.setWithoutUpdate(world, POS, AIR); - POS.setY(POS.getY() + 1); - BlocksHelper.setWithoutUpdate(world, POS, AIR); - } - // Other blocks - else { - BlocksHelper.setWithoutUpdate(world, POS, getAirOrFluid(state)); + while (state.getBlock() instanceof StalactiteBlock) { + setWithoutUpdate(world, POS, AIR); + POS.setY(POS.getY() - 1); + state = world.getBlockState(POS); } } } } + else if (state.is(EndBlocks.CAVE_PUMPKIN)) { + if (!world.getBlockState(POS.above()).is(EndBlocks.CAVE_PUMPKIN_SEED)) { + setWithoutUpdate(world, POS, AIR); + } + } + else if (!state.canSurvive(world, POS)) { + // Chorus + if (state.is(Blocks.CHORUS_PLANT)) { + Set ends = Sets.newHashSet(); + Set add = Sets.newHashSet(); + ends.add(POS.immutable()); + + for (int i = 0; i < 64 && !ends.isEmpty(); i++) { + ends.forEach((pos) -> { + setWithoutUpdate(world, pos, AIR); + for (Direction dir : BlocksHelper.HORIZONTAL) { + BlockPos p = pos.relative(dir); + BlockState st = world.getBlockState(p); + if ((st.is(Blocks.CHORUS_PLANT) || st.is(Blocks.CHORUS_FLOWER)) && !st.canSurvive(world, p)) { + add.add(p); + } + } + BlockPos p = pos.above(); + BlockState st = world.getBlockState(p); + if ((st.is(Blocks.CHORUS_PLANT) || st.is(Blocks.CHORUS_FLOWER)) && !st.canSurvive(world, p)) { + add.add(p); + } + }); + ends.clear(); + ends.addAll(add); + add.clear(); + } + } + // Vines + else if (state.getBlock() instanceof BaseVineBlock) { + while (world.getBlockState(POS).getBlock() instanceof BaseVineBlock) { + setWithoutUpdate(world, POS, AIR); + POS.setY(POS.getY() - 1); + } + } + // Falling blocks + else if (state.getBlock() instanceof FallingBlock) { + BlockState falling = state; + + POS.setY(POS.getY() - 1); + state = world.getBlockState(POS); + + int ray = BlocksHelper.downRayRep(world, POS.immutable(), 64); + if (ray > 32) { + setWithoutUpdate(world, POS, Blocks.END_STONE.defaultBlockState()); + if (world.getRandom().nextBoolean()) { + POS.setY(POS.getY() - 1); + state = world.getBlockState(POS); + setWithoutUpdate(world, POS, Blocks.END_STONE.defaultBlockState()); + } + } + else { + POS.setY(y); + BlockState replacement = AIR; + for (Direction dir : BlocksHelper.HORIZONTAL) { + state = world.getBlockState(POS.relative(dir)); + if (!state.getFluidState().isEmpty()) { + replacement = state; + break; + } + } + setWithoutUpdate(world, POS, replacement); + POS.setY(y - ray); + setWithoutUpdate(world, POS, falling); + } + } + // Blocks without support + else { + // Blue Vine + if (state.getBlock() instanceof BlueVineBlock) { + while (state.is(EndBlocks.BLUE_VINE) || state.is(EndBlocks.BLUE_VINE_LANTERN) || state.is(EndBlocks.BLUE_VINE_FUR)) { + setWithoutUpdate(world, POS, AIR); + POS.setY(POS.getY() + 1); + state = world.getBlockState(POS); + } + } + // Double plants + if (state.getBlock() instanceof BaseDoublePlantBlock) { + setWithoutUpdate(world, POS, AIR); + POS.setY(POS.getY() + 1); + setWithoutUpdate(world, POS, AIR); + } + // Other blocks + else { + setWithoutUpdate(world, POS, getAirOrFluid(state)); + } + } + } } - } - + }); + doubleCheck.forEach((pos) -> { if (!world.getBlockState(pos).canSurvive(world, pos)) { - BlocksHelper.setWithoutUpdate(world, pos, AIR); + setWithoutUpdate(world, pos, AIR); } }); } - + private static BlockState getAirOrFluid(BlockState state) { return state.getFluidState().isEmpty() ? AIR : state.getFluidState().createLegacyBlock(); } + + private static void setWithoutUpdate(LevelAccessor world, BlockPos pos, BlockState state) { + synchronized (world) { + BlocksHelper.setWithoutUpdate(world, pos, state); + } + } } diff --git a/src/main/java/ru/betterend/world/features/terrain/caves/EndCaveFeature.java b/src/main/java/ru/betterend/world/features/terrain/caves/EndCaveFeature.java index 964fa6b3..61abf3ce 100644 --- a/src/main/java/ru/betterend/world/features/terrain/caves/EndCaveFeature.java +++ b/src/main/java/ru/betterend/world/features/terrain/caves/EndCaveFeature.java @@ -58,19 +58,17 @@ public abstract class EndCaveFeature extends DefaultFeature { if (!caveBlocks.isEmpty()) { if (biome != null) { setBiomes(world, biome, caveBlocks); - Set floorPositions = Sets.newHashSet(); - Set ceilPositions = Sets.newHashSet(); - MutableBlockPos mut = new MutableBlockPos(); - caveBlocks.forEach((bpos) -> { - mut.set(bpos); - if (world.getBlockState(mut).getMaterial().isReplaceable()) { - mut.setY(bpos.getY() - 1); - if (world.getBlockState(mut).is(TagAPI.GEN_TERRAIN)) { - floorPositions.add(mut.immutable()); + Set floorPositions = Sets.newConcurrentHashSet(); + Set ceilPositions = Sets.newConcurrentHashSet(); + caveBlocks.parallelStream().forEach((bpos) -> { + if (world.getBlockState(bpos).getMaterial().isReplaceable()) { + BlockPos side = bpos.below(); + if (world.getBlockState(side).is(TagAPI.GEN_TERRAIN)) { + floorPositions.add(side); } - mut.setY(bpos.getY() + 1); - if (world.getBlockState(mut).is(TagAPI.GEN_TERRAIN)) { - ceilPositions.add(mut.immutable()); + side = bpos.above(); + if (world.getBlockState(side).is(TagAPI.GEN_TERRAIN)) { + ceilPositions.add(side); } } }); diff --git a/src/main/java/ru/betterend/world/features/terrain/caves/RoundCaveFeature.java b/src/main/java/ru/betterend/world/features/terrain/caves/RoundCaveFeature.java index 58364bd6..05d2179f 100644 --- a/src/main/java/ru/betterend/world/features/terrain/caves/RoundCaveFeature.java +++ b/src/main/java/ru/betterend/world/features/terrain/caves/RoundCaveFeature.java @@ -2,6 +2,7 @@ package ru.betterend.world.features.terrain.caves; import java.util.Random; import java.util.Set; +import java.util.stream.IntStream; import com.google.common.collect.Sets; @@ -30,47 +31,45 @@ public class RoundCaveFeature extends EndCaveFeature { double hr = radius * 0.75; double nr = radius * 0.25; - BlockState state; - MutableBlockPos bpos = new MutableBlockPos(); - Set blocks = Sets.newHashSet(); - for (int x = x1; x <= x2; x++) { - int xsq = x - center.getX(); - xsq *= xsq; + int dx = x2 - x1 + 1; + int dz = z2 - z1 + 1; + int count = dx * dz; + Set blocks = Sets.newConcurrentHashSet(); + IntStream.range(0, count).parallel().forEach(index -> { + MutableBlockPos bpos = new MutableBlockPos(); + int x = (index % dx) + x1; + int z = (index / dx) + z1; bpos.setX(x); - for (int z = z1; z <= z2; z++) { - int zsq = z - center.getZ(); - zsq *= zsq; - bpos.setZ(z); - for (int y = y1; y <= y2; y++) { - int ysq = y - center.getY(); - ysq *= 1.6; - ysq *= ysq; + bpos.setZ(z); + int xsq = MHelper.sqr(x - center.getX()); + int zsq = MHelper.sqr(z - center.getZ()); + int dxz = xsq + zsq; + BlockState state; + for (int y = y1; y <= y2; y++) { + int ysq = (int) MHelper.sqr((y - center.getY()) * 1.6); + double r = noise.eval(x * 0.1, y * 0.1, z * 0.1) * nr + hr; + double dist = dxz + ysq; + if (dist < r * r) { bpos.setY(y); - double r = noise.eval(x * 0.1, y * 0.1, z * 0.1) * nr + hr; - double dist = xsq + ysq + zsq; - if (dist < r * r) { - state = world.getBlockState(bpos); - if (isReplaceable(state) && !isWaterNear(world, bpos)) { - BlocksHelper.setWithoutUpdate(world, bpos, CAVE_AIR); - blocks.add(bpos.immutable()); - - while (state.getMaterial().equals(Material.LEAVES)) { - BlocksHelper.setWithoutUpdate(world, bpos, CAVE_AIR); - bpos.setY(bpos.getY() + 1); - state = world.getBlockState(bpos); - } - - bpos.setY(y - 1); - while (state.getMaterial().equals(Material.LEAVES)) { - BlocksHelper.setWithoutUpdate(world, bpos, CAVE_AIR); - bpos.setY(bpos.getY() - 1); - state = world.getBlockState(bpos); - } + state = world.getBlockState(bpos); + if (isReplaceable(state) && !isWaterNear(world, bpos)) { + blocks.add(bpos.immutable()); + + while (state.getMaterial().equals(Material.LEAVES)) { + bpos.setY(bpos.getY() + 1); + state = world.getBlockState(bpos); + } + + bpos.setY(y - 1); + while (state.getMaterial().equals(Material.LEAVES)) { + bpos.setY(bpos.getY() - 1); + state = world.getBlockState(bpos); } } } } - } + }); + blocks.forEach(bpos -> BlocksHelper.setWithoutUpdate(world, bpos, CAVE_AIR)); return blocks; } diff --git a/src/main/java/ru/betterend/world/features/terrain/caves/TunelCaveFeature.java b/src/main/java/ru/betterend/world/features/terrain/caves/TunelCaveFeature.java index cac45184..24cbea5a 100644 --- a/src/main/java/ru/betterend/world/features/terrain/caves/TunelCaveFeature.java +++ b/src/main/java/ru/betterend/world/features/terrain/caves/TunelCaveFeature.java @@ -3,6 +3,7 @@ package ru.betterend.world.features.terrain.caves; import java.util.Map; import java.util.Random; import java.util.Set; +import java.util.stream.IntStream; import com.google.common.collect.Maps; import com.google.common.collect.Sets; @@ -15,6 +16,7 @@ import net.minecraft.world.level.WorldGenLevel; import net.minecraft.world.level.biome.Biome; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.chunk.ChunkAccess; import net.minecraft.world.level.chunk.ChunkGenerator; import net.minecraft.world.level.levelgen.Heightmap.Types; import net.minecraft.world.level.levelgen.feature.Feature; @@ -34,55 +36,56 @@ public class TunelCaveFeature extends EndCaveFeature { if ((long) cx * (long) cx + (long) cz + (long) cz < 256) { return Sets.newHashSet(); } + int x1 = cx << 4; int z1 = cz << 4; int x2 = x1 + 16; int z2 = z1 + 16; - int y2 = world.getHeight(); + Random rand = new Random(world.getSeed()); OpenSimplexNoise noiseH = new OpenSimplexNoise(rand.nextInt()); OpenSimplexNoise noiseV = new OpenSimplexNoise(rand.nextInt()); OpenSimplexNoise noiseD = new OpenSimplexNoise(rand.nextInt()); - Set positions = Sets.newHashSet(); - MutableBlockPos pos = new MutableBlockPos(); + Set positions = Sets.newConcurrentHashSet(); - float a = hasCaves(world, pos.set(x1, 0, z1)) ? 1F : 0F; - float b = hasCaves(world, pos.set(x2, 0, z1)) ? 1F : 0F; - float c = hasCaves(world, pos.set(x1, 0, z2)) ? 1F : 0F; - float d = hasCaves(world, pos.set(x2, 0, z2)) ? 1F : 0F; + float a = hasCaves(world, new BlockPos(x1, 0, z1)) ? 1F : 0F; + float b = hasCaves(world, new BlockPos(x2, 0, z1)) ? 1F : 0F; + float c = hasCaves(world, new BlockPos(x1, 0, z2)) ? 1F : 0F; + float d = hasCaves(world, new BlockPos(x2, 0, z2)) ? 1F : 0F; - for (int x = x1; x < x2; x++) { - pos.setX(x); - float dx = (float) (x - x1) / 16F; + ChunkAccess chunk = world.getChunk(cx, cz); + IntStream.range(0, 256).parallel().forEach(index -> { + MutableBlockPos pos = new MutableBlockPos(); + int x = index & 15; + int z = index >> 4; + int wheight = chunk.getHeight(Types.WORLD_SURFACE_WG, x, z); + float dx = x / 16F; + float dz = z / 16F; + pos.setX(x + x1); + pos.setZ(z + z1); float da = Mth.lerp(dx, a, b); float db = Mth.lerp(dx, c, d); - for (int z = z1; z < z2; z++) { - pos.setZ(z); - float dz = (float) (z - z1) / 16F; - float density = 1 - Mth.lerp(dz, da, db); - int wheight = world.getHeight(Types.WORLD_SURFACE_WG, x, z); - for (int y = 0; y < y2; y++) { + float density = 1 - Mth.lerp(dz, da, db); + if (density < 0.5) { + for (int y = 0; y < wheight; y++) { pos.setY(y); float gradient = 1 - Mth.clamp((wheight - y) * 0.1F, 0F, 1F); - float val = Mth.abs((float) noiseH.eval(x * 0.02, y * 0.01, z * 0.02)); - float vert = Mth.sin((y + (float) noiseV.eval(x * 0.01, z * 0.01) * 20) * 0.1F) * 0.9F; - float dist = (float) noiseD.eval(x * 0.1, y * 0.1, z * 0.1) * 0.12F; + if (gradient > 0.5) { + break; + } + float val = Mth.abs((float) noiseH.eval(pos.getX() * 0.02, y * 0.01, pos.getZ() * 0.02)); + float vert = Mth.sin((y + (float) noiseV.eval(pos.getX() * 0.01, pos.getZ() * 0.01) * 20) * 0.1F) * 0.9F; + float dist = (float) noiseD.eval(pos.getX() * 0.1, y * 0.1, pos.getZ() * 0.1) * 0.12F; val = (val + vert * vert + dist) + density + gradient; if (val < 0.15 && world.getBlockState(pos).is(TagAPI.GEN_TERRAIN) && noWaterNear(world, pos)) { - BlocksHelper.setWithoutUpdate(world, pos, AIR); positions.add(pos.immutable()); - int height = world.getHeight(Types.WORLD_SURFACE_WG, pos.getX(), pos.getZ()); - if (height < pos.getY() + 4) { - while (pos.getY() < height && noWaterNear(world, pos)) { - pos.setY(pos.getY() + 1); - BlocksHelper.setWithoutUpdate(world, pos, AIR); - } - } } } } - } + }); + positions.forEach(bpos -> BlocksHelper.setWithoutUpdate(world, bpos, CAVE_AIR)); + return positions; }