Parallel cave generation & block fixing

This commit is contained in:
paulevsGitch 2021-06-21 21:38:56 +03:00
parent 4dce2d5d7e
commit 37394eba11
4 changed files with 243 additions and 235 deletions

View file

@ -1,6 +1,7 @@
package ru.betterend.util; package ru.betterend.util;
import java.util.Set; import java.util.Set;
import java.util.stream.IntStream;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
@ -20,17 +21,19 @@ import ru.betterend.blocks.basis.FurBlock;
import ru.betterend.registry.EndBlocks; import ru.betterend.registry.EndBlocks;
public class BlockFixer { public class BlockFixer {
private static final MutableBlockPos POS = new MutableBlockPos();
private static final BlockState AIR = Blocks.AIR.defaultBlockState(); private static final BlockState AIR = Blocks.AIR.defaultBlockState();
private static final BlockState WATER = Blocks.WATER.defaultBlockState(); private static final BlockState WATER = Blocks.WATER.defaultBlockState();
public static void fixBlocks(LevelAccessor world, BlockPos start, BlockPos end) { public static void fixBlocks(LevelAccessor world, BlockPos start, BlockPos end) {
Set<BlockPos> 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; BlockState state;
Set<BlockPos> 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++) { for (int y = start.getY(); y <= end.getY(); y++) {
POS.setY(y); POS.setY(y);
state = world.getBlockState(POS); state = world.getBlockState(POS);
@ -41,12 +44,12 @@ public class BlockFixer {
// Liquids // Liquids
else if (!state.getFluidState().isEmpty()) { else if (!state.getFluidState().isEmpty()) {
if (!state.canSurvive(world, POS)) { if (!state.canSurvive(world, POS)) {
BlocksHelper.setWithoutUpdate(world, POS, WATER); setWithoutUpdate(world, POS, WATER);
POS.setY(POS.getY() - 1); POS.setY(POS.getY() - 1);
state = world.getBlockState(POS); state = world.getBlockState(POS);
while (!state.canSurvive(world, POS)) { while (!state.canSurvive(world, POS)) {
state = state.getFluidState().isEmpty() ? AIR : WATER; state = state.getFluidState().isEmpty() ? AIR : WATER;
BlocksHelper.setWithoutUpdate(world, POS, state); setWithoutUpdate(world, POS, state);
POS.setY(POS.getY() - 1); POS.setY(POS.getY() - 1);
state = world.getBlockState(POS); state = world.getBlockState(POS);
} }
@ -55,7 +58,7 @@ public class BlockFixer {
if (world.isEmptyBlock(POS)) { if (world.isEmptyBlock(POS)) {
POS.setY(y); POS.setY(y);
while (!world.getFluidState(POS).isEmpty()) { while (!world.getFluidState(POS).isEmpty()) {
BlocksHelper.setWithoutUpdate(world, POS, AIR); setWithoutUpdate(world, POS, AIR);
POS.setY(POS.getY() + 1); POS.setY(POS.getY() + 1);
} }
continue; continue;
@ -72,7 +75,7 @@ public class BlockFixer {
if (world.isEmptyBlock(POS)) { if (world.isEmptyBlock(POS)) {
POS.setY(POS.getY() + 1); POS.setY(POS.getY() + 1);
while (state.is(EndBlocks.SMARAGDANT_CRYSTAL)) { while (state.is(EndBlocks.SMARAGDANT_CRYSTAL)) {
BlocksHelper.setWithoutUpdate(world, POS, AIR); setWithoutUpdate(world, POS, AIR);
POS.setY(POS.getY() + 1); POS.setY(POS.getY() + 1);
state = world.getBlockState(POS); state = world.getBlockState(POS);
} }
@ -82,14 +85,14 @@ public class BlockFixer {
if (!state.canSurvive(world, POS)) { if (!state.canSurvive(world, POS)) {
if (world.getBlockState(POS.above()).getBlock() instanceof StalactiteBlock) { if (world.getBlockState(POS.above()).getBlock() instanceof StalactiteBlock) {
while (state.getBlock() instanceof StalactiteBlock) { while (state.getBlock() instanceof StalactiteBlock) {
BlocksHelper.setWithoutUpdate(world, POS, AIR); setWithoutUpdate(world, POS, AIR);
POS.setY(POS.getY() + 1); POS.setY(POS.getY() + 1);
state = world.getBlockState(POS); state = world.getBlockState(POS);
} }
} }
else { else {
while (state.getBlock() instanceof StalactiteBlock) { while (state.getBlock() instanceof StalactiteBlock) {
BlocksHelper.setWithoutUpdate(world, POS, AIR); setWithoutUpdate(world, POS, AIR);
POS.setY(POS.getY() - 1); POS.setY(POS.getY() - 1);
state = world.getBlockState(POS); state = world.getBlockState(POS);
} }
@ -98,7 +101,7 @@ public class BlockFixer {
} }
else if (state.is(EndBlocks.CAVE_PUMPKIN)) { else if (state.is(EndBlocks.CAVE_PUMPKIN)) {
if (!world.getBlockState(POS.above()).is(EndBlocks.CAVE_PUMPKIN_SEED)) { if (!world.getBlockState(POS.above()).is(EndBlocks.CAVE_PUMPKIN_SEED)) {
BlocksHelper.setWithoutUpdate(world, POS, AIR); setWithoutUpdate(world, POS, AIR);
} }
} }
else if (!state.canSurvive(world, POS)) { else if (!state.canSurvive(world, POS)) {
@ -110,7 +113,7 @@ public class BlockFixer {
for (int i = 0; i < 64 && !ends.isEmpty(); i++) { for (int i = 0; i < 64 && !ends.isEmpty(); i++) {
ends.forEach((pos) -> { ends.forEach((pos) -> {
BlocksHelper.setWithoutUpdate(world, pos, AIR); setWithoutUpdate(world, pos, AIR);
for (Direction dir : BlocksHelper.HORIZONTAL) { for (Direction dir : BlocksHelper.HORIZONTAL) {
BlockPos p = pos.relative(dir); BlockPos p = pos.relative(dir);
BlockState st = world.getBlockState(p); BlockState st = world.getBlockState(p);
@ -132,7 +135,7 @@ public class BlockFixer {
// Vines // Vines
else if (state.getBlock() instanceof BaseVineBlock) { else if (state.getBlock() instanceof BaseVineBlock) {
while (world.getBlockState(POS).getBlock() instanceof BaseVineBlock) { while (world.getBlockState(POS).getBlock() instanceof BaseVineBlock) {
BlocksHelper.setWithoutUpdate(world, POS, AIR); setWithoutUpdate(world, POS, AIR);
POS.setY(POS.getY() - 1); POS.setY(POS.getY() - 1);
} }
} }
@ -145,11 +148,11 @@ public class BlockFixer {
int ray = BlocksHelper.downRayRep(world, POS.immutable(), 64); int ray = BlocksHelper.downRayRep(world, POS.immutable(), 64);
if (ray > 32) { if (ray > 32) {
BlocksHelper.setWithoutUpdate(world, POS, Blocks.END_STONE.defaultBlockState()); setWithoutUpdate(world, POS, Blocks.END_STONE.defaultBlockState());
if (world.getRandom().nextBoolean()) { if (world.getRandom().nextBoolean()) {
POS.setY(POS.getY() - 1); POS.setY(POS.getY() - 1);
state = world.getBlockState(POS); state = world.getBlockState(POS);
BlocksHelper.setWithoutUpdate(world, POS, Blocks.END_STONE.defaultBlockState()); setWithoutUpdate(world, POS, Blocks.END_STONE.defaultBlockState());
} }
} }
else { else {
@ -162,9 +165,9 @@ public class BlockFixer {
break; break;
} }
} }
BlocksHelper.setWithoutUpdate(world, POS, replacement); setWithoutUpdate(world, POS, replacement);
POS.setY(y - ray); POS.setY(y - ray);
BlocksHelper.setWithoutUpdate(world, POS, falling); setWithoutUpdate(world, POS, falling);
} }
} }
// Blocks without support // Blocks without support
@ -172,30 +175,29 @@ public class BlockFixer {
// Blue Vine // Blue Vine
if (state.getBlock() instanceof BlueVineBlock) { if (state.getBlock() instanceof BlueVineBlock) {
while (state.is(EndBlocks.BLUE_VINE) || state.is(EndBlocks.BLUE_VINE_LANTERN) || state.is(EndBlocks.BLUE_VINE_FUR)) { while (state.is(EndBlocks.BLUE_VINE) || state.is(EndBlocks.BLUE_VINE_LANTERN) || state.is(EndBlocks.BLUE_VINE_FUR)) {
BlocksHelper.setWithoutUpdate(world, POS, AIR); setWithoutUpdate(world, POS, AIR);
POS.setY(POS.getY() + 1); POS.setY(POS.getY() + 1);
state = world.getBlockState(POS); state = world.getBlockState(POS);
} }
} }
// Double plants // Double plants
if (state.getBlock() instanceof BaseDoublePlantBlock) { if (state.getBlock() instanceof BaseDoublePlantBlock) {
BlocksHelper.setWithoutUpdate(world, POS, AIR); setWithoutUpdate(world, POS, AIR);
POS.setY(POS.getY() + 1); POS.setY(POS.getY() + 1);
BlocksHelper.setWithoutUpdate(world, POS, AIR); setWithoutUpdate(world, POS, AIR);
} }
// Other blocks // Other blocks
else { else {
BlocksHelper.setWithoutUpdate(world, POS, getAirOrFluid(state)); setWithoutUpdate(world, POS, getAirOrFluid(state));
}
}
} }
} }
} }
} }
});
doubleCheck.forEach((pos) -> { doubleCheck.forEach((pos) -> {
if (!world.getBlockState(pos).canSurvive(world, pos)) { if (!world.getBlockState(pos).canSurvive(world, pos)) {
BlocksHelper.setWithoutUpdate(world, pos, AIR); setWithoutUpdate(world, pos, AIR);
} }
}); });
} }
@ -203,4 +205,10 @@ public class BlockFixer {
private static BlockState getAirOrFluid(BlockState state) { private static BlockState getAirOrFluid(BlockState state) {
return state.getFluidState().isEmpty() ? AIR : state.getFluidState().createLegacyBlock(); 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);
}
}
} }

View file

@ -58,19 +58,17 @@ public abstract class EndCaveFeature extends DefaultFeature {
if (!caveBlocks.isEmpty()) { if (!caveBlocks.isEmpty()) {
if (biome != null) { if (biome != null) {
setBiomes(world, biome, caveBlocks); setBiomes(world, biome, caveBlocks);
Set<BlockPos> floorPositions = Sets.newHashSet(); Set<BlockPos> floorPositions = Sets.newConcurrentHashSet();
Set<BlockPos> ceilPositions = Sets.newHashSet(); Set<BlockPos> ceilPositions = Sets.newConcurrentHashSet();
MutableBlockPos mut = new MutableBlockPos(); caveBlocks.parallelStream().forEach((bpos) -> {
caveBlocks.forEach((bpos) -> { if (world.getBlockState(bpos).getMaterial().isReplaceable()) {
mut.set(bpos); BlockPos side = bpos.below();
if (world.getBlockState(mut).getMaterial().isReplaceable()) { if (world.getBlockState(side).is(TagAPI.GEN_TERRAIN)) {
mut.setY(bpos.getY() - 1); floorPositions.add(side);
if (world.getBlockState(mut).is(TagAPI.GEN_TERRAIN)) {
floorPositions.add(mut.immutable());
} }
mut.setY(bpos.getY() + 1); side = bpos.above();
if (world.getBlockState(mut).is(TagAPI.GEN_TERRAIN)) { if (world.getBlockState(side).is(TagAPI.GEN_TERRAIN)) {
ceilPositions.add(mut.immutable()); ceilPositions.add(side);
} }
} }
}); });

View file

@ -2,6 +2,7 @@ package ru.betterend.world.features.terrain.caves;
import java.util.Random; import java.util.Random;
import java.util.Set; import java.util.Set;
import java.util.stream.IntStream;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
@ -30,47 +31,45 @@ public class RoundCaveFeature extends EndCaveFeature {
double hr = radius * 0.75; double hr = radius * 0.75;
double nr = radius * 0.25; double nr = radius * 0.25;
BlockState state; int dx = x2 - x1 + 1;
int dz = z2 - z1 + 1;
int count = dx * dz;
Set<BlockPos> blocks = Sets.newConcurrentHashSet();
IntStream.range(0, count).parallel().forEach(index -> {
MutableBlockPos bpos = new MutableBlockPos(); MutableBlockPos bpos = new MutableBlockPos();
Set<BlockPos> blocks = Sets.newHashSet(); int x = (index % dx) + x1;
for (int x = x1; x <= x2; x++) { int z = (index / dx) + z1;
int xsq = x - center.getX();
xsq *= xsq;
bpos.setX(x); bpos.setX(x);
for (int z = z1; z <= z2; z++) {
int zsq = z - center.getZ();
zsq *= zsq;
bpos.setZ(z); 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++) { for (int y = y1; y <= y2; y++) {
int ysq = y - center.getY(); int ysq = (int) MHelper.sqr((y - center.getY()) * 1.6);
ysq *= 1.6;
ysq *= ysq;
bpos.setY(y);
double r = noise.eval(x * 0.1, y * 0.1, z * 0.1) * nr + hr; double r = noise.eval(x * 0.1, y * 0.1, z * 0.1) * nr + hr;
double dist = xsq + ysq + zsq; double dist = dxz + ysq;
if (dist < r * r) { if (dist < r * r) {
bpos.setY(y);
state = world.getBlockState(bpos); state = world.getBlockState(bpos);
if (isReplaceable(state) && !isWaterNear(world, bpos)) { if (isReplaceable(state) && !isWaterNear(world, bpos)) {
BlocksHelper.setWithoutUpdate(world, bpos, CAVE_AIR);
blocks.add(bpos.immutable()); blocks.add(bpos.immutable());
while (state.getMaterial().equals(Material.LEAVES)) { while (state.getMaterial().equals(Material.LEAVES)) {
BlocksHelper.setWithoutUpdate(world, bpos, CAVE_AIR);
bpos.setY(bpos.getY() + 1); bpos.setY(bpos.getY() + 1);
state = world.getBlockState(bpos); state = world.getBlockState(bpos);
} }
bpos.setY(y - 1); bpos.setY(y - 1);
while (state.getMaterial().equals(Material.LEAVES)) { while (state.getMaterial().equals(Material.LEAVES)) {
BlocksHelper.setWithoutUpdate(world, bpos, CAVE_AIR);
bpos.setY(bpos.getY() - 1); bpos.setY(bpos.getY() - 1);
state = world.getBlockState(bpos); state = world.getBlockState(bpos);
} }
} }
} }
} }
} });
} blocks.forEach(bpos -> BlocksHelper.setWithoutUpdate(world, bpos, CAVE_AIR));
return blocks; return blocks;
} }

View file

@ -3,6 +3,7 @@ package ru.betterend.world.features.terrain.caves;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.Set; import java.util.Set;
import java.util.stream.IntStream;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Sets; 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.biome.Biome;
import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState; 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.chunk.ChunkGenerator;
import net.minecraft.world.level.levelgen.Heightmap.Types; import net.minecraft.world.level.levelgen.Heightmap.Types;
import net.minecraft.world.level.levelgen.feature.Feature; 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) { if ((long) cx * (long) cx + (long) cz + (long) cz < 256) {
return Sets.newHashSet(); return Sets.newHashSet();
} }
int x1 = cx << 4; int x1 = cx << 4;
int z1 = cz << 4; int z1 = cz << 4;
int x2 = x1 + 16; int x2 = x1 + 16;
int z2 = z1 + 16; int z2 = z1 + 16;
int y2 = world.getHeight();
Random rand = new Random(world.getSeed()); Random rand = new Random(world.getSeed());
OpenSimplexNoise noiseH = new OpenSimplexNoise(rand.nextInt()); OpenSimplexNoise noiseH = new OpenSimplexNoise(rand.nextInt());
OpenSimplexNoise noiseV = new OpenSimplexNoise(rand.nextInt()); OpenSimplexNoise noiseV = new OpenSimplexNoise(rand.nextInt());
OpenSimplexNoise noiseD = new OpenSimplexNoise(rand.nextInt()); OpenSimplexNoise noiseD = new OpenSimplexNoise(rand.nextInt());
Set<BlockPos> positions = Sets.newHashSet(); Set<BlockPos> positions = Sets.newConcurrentHashSet();
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;
ChunkAccess chunk = world.getChunk(cx, cz);
IntStream.range(0, 256).parallel().forEach(index -> {
MutableBlockPos pos = new MutableBlockPos(); MutableBlockPos pos = new MutableBlockPos();
int x = index & 15;
float a = hasCaves(world, pos.set(x1, 0, z1)) ? 1F : 0F; int z = index >> 4;
float b = hasCaves(world, pos.set(x2, 0, z1)) ? 1F : 0F; int wheight = chunk.getHeight(Types.WORLD_SURFACE_WG, x, z);
float c = hasCaves(world, pos.set(x1, 0, z2)) ? 1F : 0F; float dx = x / 16F;
float d = hasCaves(world, pos.set(x2, 0, z2)) ? 1F : 0F; float dz = z / 16F;
pos.setX(x + x1);
for (int x = x1; x < x2; x++) { pos.setZ(z + z1);
pos.setX(x);
float dx = (float) (x - x1) / 16F;
float da = Mth.lerp(dx, a, b); float da = Mth.lerp(dx, a, b);
float db = Mth.lerp(dx, c, d); 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); float density = 1 - Mth.lerp(dz, da, db);
int wheight = world.getHeight(Types.WORLD_SURFACE_WG, x, z); if (density < 0.5) {
for (int y = 0; y < y2; y++) { for (int y = 0; y < wheight; y++) {
pos.setY(y); pos.setY(y);
float gradient = 1 - Mth.clamp((wheight - y) * 0.1F, 0F, 1F); 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)); if (gradient > 0.5) {
float vert = Mth.sin((y + (float) noiseV.eval(x * 0.01, z * 0.01) * 20) * 0.1F) * 0.9F; break;
float dist = (float) noiseD.eval(x * 0.1, y * 0.1, z * 0.1) * 0.12F; }
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; val = (val + vert * vert + dist) + density + gradient;
if (val < 0.15 && world.getBlockState(pos).is(TagAPI.GEN_TERRAIN) && noWaterNear(world, pos)) { if (val < 0.15 && world.getBlockState(pos).is(TagAPI.GEN_TERRAIN) && noWaterNear(world, pos)) {
BlocksHelper.setWithoutUpdate(world, pos, AIR);
positions.add(pos.immutable()); 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; return positions;
} }