Skip to content

Commit

Permalink
More heightmap checks for ClusterBasedGenerator when appropriate. Imp…
Browse files Browse the repository at this point in the history
…roves permafrost speed
  • Loading branch information
quat1024 committed Dec 21, 2023
1 parent 043c884 commit 89d8d05
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ public void generateChunkPart(BlockPos src, ChunkGenerator generator, Random ran
final IGenerationContext context = createContext(src, generator, random, chunkCorner, world);

forEachChunkBlock(world, chunkCorner, shape.getLowerBound(), shape.getUpperBound(), (pos) -> {
double noise = shape.noiseDiff(pos);
if(noise > 0)
context.consume(pos, noise);
if(context.canPlaceAt(pos) && shape.isInside(pos))
context.consume(pos);
});
}

public abstract IGenerationContext createContext(BlockPos src, ChunkGenerator generator, Random random, BlockPos chunkCorner, WorldGenRegion world);

public interface IGenerationContext {
void consume(BlockPos pos, double noise);
boolean canPlaceAt(BlockPos pos);
void consume(BlockPos pos);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,18 @@ public record ClusterShape(BlockPos src, Vec3 radius,
PerlinSimplexNoise noiseGenerator) {

public boolean isInside(BlockPos pos) {
return noiseDiff(pos) > 0;
}

// For result of this method, positive result is the valid spots and negative is outside.
// 0 is edge of the cluster which is very useful for speedups in checks in
// UndergroundSpaceGenerator as some checks should happen only at edges of cave.
// You can use these kinds of tricks for speedups towards edge of other clusters as well.
public double noiseDiff(BlockPos pos) {
// normalize distances by the radius
double dx = (double) (pos.getX() - src.getX()) / radius.x;
double dy = (double) (pos.getY() - src.getY()) / radius.y;
double dz = (double) (pos.getZ() - src.getZ()) / radius.z;

double r = Math.sqrt(dx * dx + dy * dy + dz * dz);
double r = dx * dx + dy * dy + dz * dz;
if(r > 1)
return -1;
return false;
if(GeneralConfig.useFastWorldgen)
return 1;
return true;

r = Math.sqrt(r);

// convert to spherical
double phi = Math.atan2(dz, dx);
Expand All @@ -55,7 +49,7 @@ public double noiseDiff(BlockPos pos) {

// accept if within constrains
double maxR = noise + 0.5;
return maxR - r;
return (maxR - r) > 0;
}

public int getUpperBound() {
Expand Down Expand Up @@ -95,14 +89,6 @@ public int getRarity() {
return config.rarity;
}

public int getRandomYLevel(Random rand) {
return config.minYLevel + (config.minYLevel == config.maxYLevel ? 0 : rand.nextInt(Math.max(config.maxYLevel, config.minYLevel) - Math.min(config.maxYLevel, config.minYLevel)));
}

public IBiomeConfig getBiomeTypes() {
return config.biomes;
}

public Random randAroundBlockPos(BlockPos pos) {
return new Random(31 * (31L * (31 + pos.getX()) + pos.getY()) + pos.getZ());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
import net.minecraft.server.level.WorldGenRegion;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.chunk.ChunkGenerator;

import org.violetmoon.quark.base.Quark;
import org.violetmoon.quark.base.config.type.DimensionConfig;
import org.violetmoon.quark.base.world.generator.Generator;
import org.violetmoon.quark.content.world.undergroundstyle.PermafrostStyle;
import org.violetmoon.quark.content.world.undergroundstyle.base.UndergroundStyleGenerator;

import java.util.Random;
import java.util.function.BooleanSupplier;
Expand Down Expand Up @@ -51,14 +47,8 @@ public final void generateChunk(WorldGenRegion world, ChunkGenerator generator,
Random chunkRandom = new Random(chunkSeed);
BlockPos chunkCorner = new BlockPos(x << 4, 0, z << 4);

for(BlockPos source : getSourcesInChunk(world, chunkRandom, generator, chunkCorner)) {
for(BlockPos source : getSourcesInChunk(world, chunkRandom, generator, chunkCorner))
generateChunkPart(source, generator, ourRandom, pos, world);

// if(isInsideChunk(source, chunkX, chunkZ) && this instanceof UndergroundStyleGenerator what && what.info.style instanceof PermafrostStyle) {
// Quark.LOG.info(source);
// world.setBlock(source, Blocks.BEACON.defaultBlockState(), 0);
// }
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,16 @@ public String toString() {

@Override
public IGenerationContext createContext(BlockPos src, ChunkGenerator generator, Random random, BlockPos chunkCorner, WorldGenRegion world) {
return (pos, noise) -> {
if(canPlaceBlock(world, pos))
return new IGenerationContext() {
@Override
public boolean canPlaceAt(BlockPos pos) {
return canPlaceBlock(world, pos);
}

@Override
public void consume(BlockPos pos) {
world.setBlock(pos, placeState, 0);
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class PermafrostStyle extends BasicUndergroundStyle {

public PermafrostStyle() {
super(Blocks.PACKED_ICE.defaultBlockState(), Blocks.PACKED_ICE.defaultBlockState(), Blocks.PACKED_ICE.defaultBlockState(), true);
//super(Blocks.RED_STAINED_GLASS.defaultBlockState(), Blocks.LIME_STAINED_GLASS.defaultBlockState(), Blocks.BLUE_STAINED_GLASS.defaultBlockState(), true);
}

public void setBlock(BlockState state) {
Expand All @@ -36,9 +35,4 @@ public void fillFloor(Context context, BlockPos pos, BlockState state) {
}
}
}

@Override
public void fillInside(Context context, BlockPos pos, BlockState state) {
context.world.setBlock(pos, Blocks.PINK_STAINED_GLASS.defaultBlockState(), 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,6 @@ public final void fill(Context context, BlockPos pos) {
if(state.getDestroySpeed(world, pos) == -1)
return;

//boolean shrouded = false;
//BlockPos testPos = new MutableBlockPos(pos.getX(), pos.getY(), pos.getZ());
//while(!world.isOutsideBuildHeight(testPos)) {
// testPos = testPos.above();
// if(world.getBlockState(testPos).isSolidRender(world, testPos)) {
// shrouded = true;
// break;
// }
//}
//if(!shrouded)
// return;
if(world.getHeight(Heightmap.Types.WORLD_SURFACE_WG, pos.getX(), pos.getZ()) < pos.getY())
return;

if(isFloor(world, pos, state))
fillFloor(context, pos, state);
else if(isCeiling(world, pos, state))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ public Context(WorldGenRegion world, BlockPos source, ChunkGenerator generator,
}

@Override
public void consume(BlockPos pos, double noise) {
public boolean canPlaceAt(BlockPos pos) {
return world.getHeight(Heightmap.Types.WORLD_SURFACE_WG, pos.getX(), pos.getZ()) > pos.getY();
}

@Override
public void consume(BlockPos pos) {
info.style.fill(this, pos);
}

Expand Down

0 comments on commit 89d8d05

Please sign in to comment.