mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 00:04:42 +00:00
Space surface objects apart and vary their chance with noise
Ported from v3e (4be4f9774, 92a1fa645). An object placement can set a separation in blocks: each chunk rolls one candidate spot, and it stands only when no lower ranked candidate of a placement with the same objects lies closer. Spots and ranks come from the world seed, the chunk and the objects, so spacing holds across chunk, biome and region borders in any generation order. A chanceStyle multiplies the chance by noise.
This commit is contained in:
1 parent
9852f730a7
commit
d65da98624
5 files changed
+244
-33
No files matched your search
@@ -38,6 +38,8 @@ public final class IrisSeedPurposes {
|
||||
public static final ResourceId MANTLE = purpose("mantle");
|
||||
/** The noise that warps the columns of object placements with a surface warp. */
|
||||
public static final ResourceId OBJECT_WARP = purpose("object-warp");
|
||||
/** The candidate spots, ranks and chance noise of separated object placements. */
|
||||
public static final ResourceId OBJECT_SEPARATION = purpose("object-separation");
|
||||
public static final ResourceId POST = purpose("post");
|
||||
public static final ResourceId TERRAIN = purpose("terrain");
|
||||
|
||||
|
||||
@@ -98,8 +98,13 @@ public final class ObjectPlacement implements PlacementSource {
|
||||
@MaxNumber(1)
|
||||
@Desc("The chance for this to place in a chunk. If you need multiple per chunk, set this to 1 and use density.")
|
||||
private double chance = 1;
|
||||
@Desc("If set, chance is multiplied by this noise (0 to 1) at the chunk, so objects gather in some areas and thin out in others.")
|
||||
private NoiseStyle chanceStyle = null;
|
||||
@MinNumber(0)
|
||||
@Desc("Keeps objects of this placement at least this many blocks apart, also across chunks and biomes that place the same objects. Each chunk then places at most one. 0 turns it off. Surface placements only.")
|
||||
private int separation = 0;
|
||||
@MinNumber(1)
|
||||
@Desc("If the chance check passes, place this many in a single chunk")
|
||||
@Desc("If the chance check passes, place this many in a single chunk. Ignored when separation is set.")
|
||||
private int density = 1;
|
||||
@Desc("If the chance check passes, and you specify this, it picks a number in the range based on noise, and 'density' is ignored.")
|
||||
private StyledRange densityStyle = null;
|
||||
@@ -199,6 +204,8 @@ public final class ObjectPlacement implements PlacementSource {
|
||||
slopeCondition = source.slopeCondition;
|
||||
rotateTowardsSlope = source.rotateTowardsSlope;
|
||||
chance = source.chance;
|
||||
chanceStyle = source.chanceStyle;
|
||||
separation = source.separation;
|
||||
density = source.density;
|
||||
densityStyle = source.densityStyle;
|
||||
stiltSettings = source.stiltSettings;
|
||||
@@ -284,6 +291,20 @@ public final class ObjectPlacement implements PlacementSource {
|
||||
return chance;
|
||||
}
|
||||
|
||||
/** The noise, from 0 to 1, that {@link #chance()} is multiplied by at each chunk; empty when the chance is the same everywhere. */
|
||||
public Optional<NoiseStyle> chanceStyle() {
|
||||
return Optional.ofNullable(chanceStyle);
|
||||
}
|
||||
|
||||
/** The least distance in blocks between two objects of placements with the same objects; 0 when they are not kept apart. */
|
||||
public int separation() {
|
||||
return separation;
|
||||
}
|
||||
|
||||
public boolean isSeparated() {
|
||||
return separation > 0;
|
||||
}
|
||||
|
||||
/** The count of placements when the chance check passes, unless {@link #densityStyle()} picks it. */
|
||||
public int density() {
|
||||
return density;
|
||||
|
||||
@@ -35,6 +35,7 @@ import art.arcane.iris.modules.objects.CavePlacement;
|
||||
import art.arcane.iris.modules.objects.TerrainTransaction;
|
||||
import art.arcane.iris.modules.objects.EffectiveSettings;
|
||||
import art.arcane.iris.modules.objects.ObjectPool;
|
||||
import art.arcane.iris.modules.objects.ObjectSeparation;
|
||||
import art.arcane.iris.modules.objects.PlacementChanges;
|
||||
import art.arcane.iris.modules.objects.PlacementTags;
|
||||
import art.arcane.iris.modules.objects.ProceduralPlacement;
|
||||
@@ -73,6 +74,7 @@ final class ChunkObjectPlacing {
|
||||
private static final String REGEN_THREAD_PREFIX = "Iris-Regen-";
|
||||
|
||||
private final ObjectPool objects;
|
||||
private final ObjectSeparation separation;
|
||||
private final ModuleLog log;
|
||||
private final MantleStageContext context;
|
||||
private final ObjectChunk chunk;
|
||||
@@ -91,6 +93,7 @@ final class ChunkObjectPlacing {
|
||||
|
||||
ChunkObjectPlacing(ObjectPool objects, EffectiveSettings settings, ModuleLog log, MantleStageContext context) {
|
||||
this.objects = objects;
|
||||
this.separation = ObjectSeparation.of(context.world());
|
||||
this.log = log;
|
||||
this.context = context;
|
||||
this.chunk = new ObjectChunk(context, settings, log);
|
||||
@@ -241,13 +244,17 @@ final class ChunkObjectPlacing {
|
||||
private void placeSurfaceScope(Tally tally, String scope, List<ObjectPlacement> placements, String ownerName) {
|
||||
for (ObjectPlacement placement : placements) {
|
||||
tally.checked(scope);
|
||||
boolean passes = rng.chance(placement.chance() + rng.d(-CHANCE_JITTER, CHANCE_JITTER));
|
||||
traceChance(scope, passes, placement);
|
||||
if (!passes) {
|
||||
continue;
|
||||
}
|
||||
tally.triggered(scope);
|
||||
try {
|
||||
if (placement.isSeparated()) {
|
||||
placeSeparatedObject(tally, placement, scope);
|
||||
continue;
|
||||
}
|
||||
boolean passes = rng.chance(separation.chanceAt(placement, centerX, centerZ) + rng.d(-CHANCE_JITTER, CHANCE_JITTER));
|
||||
traceChance(scope, passes, placement);
|
||||
if (!passes) {
|
||||
continue;
|
||||
}
|
||||
tally.triggered(scope);
|
||||
placeObject(tally, placement, scope);
|
||||
} catch (Throwable failure) {
|
||||
tally.errors++;
|
||||
@@ -256,6 +263,25 @@ final class ChunkObjectPlacing {
|
||||
}
|
||||
}
|
||||
|
||||
/** Places the one object the chunk's candidate stands for, when no closer candidate of a lower rank outranks it. */
|
||||
private void placeSeparatedObject(Tally tally, ObjectPlacement placement, String scope) {
|
||||
Optional<ObjectSeparation.Candidate> kept = separation.keptCandidate(chunkX, chunkZ, placement, context.settings());
|
||||
traceChance(scope, kept.isPresent(), placement);
|
||||
if (kept.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
tally.triggered(scope);
|
||||
ObjectSeparation.Candidate candidate = kept.get();
|
||||
tally.attempts++;
|
||||
Optional<PlaceableObject> picked = objects.pickScaled(placement, candidate.rng());
|
||||
if (picked.isEmpty()) {
|
||||
tally.nullObjects++;
|
||||
traceNullObject(scope, 0, 1, placement);
|
||||
return;
|
||||
}
|
||||
placeOnSurfaceAt(tally, placement, scope, picked.get(), candidate.x(), candidate.z(), candidate.rng(), 0, 1);
|
||||
}
|
||||
|
||||
private void placeCaveScope(Tally tally, String scope, List<ObjectPlacement> placements, CaveProfile profile,
|
||||
ResourceId expectedCaveBiome, String ownerName, CaveAnchors anchors) {
|
||||
for (ObjectPlacement placement : placements) {
|
||||
@@ -297,31 +323,35 @@ final class ChunkObjectPlacing {
|
||||
traceNullObject(scope, i, density, placement);
|
||||
continue;
|
||||
}
|
||||
PlaceableObject object = picked.get();
|
||||
int x = rng.i(blockX, blockX + CHUNK_WIDTH_BLOCKS);
|
||||
int z = rng.i(blockZ, blockZ + CHUNK_WIDTH_BLOCKS);
|
||||
PlacementChanges changes = effective.forSurface(placement, object);
|
||||
boolean treePlacement = isTreePlacement(object, placement);
|
||||
int id = rng.i(0, Integer.MAX_VALUE);
|
||||
String goldenTag = golden ? scope + "/" + object.name() : null;
|
||||
if (golden) {
|
||||
log.debug("Goldendebug object attempt: chunk=" + chunkX + "," + chunkZ + " scope=" + scope + " object=" + object.name()
|
||||
+ " densityIndex=" + i + " xx=" + x + " zz=" + z + " mode=" + placement.mode());
|
||||
}
|
||||
try {
|
||||
PlacementTags tags = tags(object, id, "surface", treePlacement, placement);
|
||||
int result = chunk.placeOnSurface(object, x, z, placement, changes, tags, goldenTag);
|
||||
if (result >= 0) {
|
||||
tally.placed++;
|
||||
} else {
|
||||
tally.rejected++;
|
||||
}
|
||||
traceObjectResult(scope, object, result, x, z, i, density);
|
||||
} catch (Throwable failure) {
|
||||
tally.errors++;
|
||||
log.error("Regen object placement exception: chunk=" + chunkX + "," + chunkZ + " scope=" + scope + " object=" + object.name()
|
||||
+ " densityIndex=" + i + " density=" + density + " error=" + failure.getClass().getSimpleName() + ":" + failure.getMessage(), failure);
|
||||
placeOnSurfaceAt(tally, placement, scope, picked.get(), x, z, rng, i, density);
|
||||
}
|
||||
}
|
||||
|
||||
private void placeOnSurfaceAt(Tally tally, ObjectPlacement placement, String scope, PlaceableObject object, int x, int z,
|
||||
RNG placingRng, int densityIndex, int density) {
|
||||
PlacementChanges changes = effective.forSurface(placement, object);
|
||||
boolean treePlacement = isTreePlacement(object, placement);
|
||||
int id = placingRng.i(0, Integer.MAX_VALUE);
|
||||
String goldenTag = golden ? scope + "/" + object.name() : null;
|
||||
if (golden) {
|
||||
log.debug("Goldendebug object attempt: chunk=" + chunkX + "," + chunkZ + " scope=" + scope + " object=" + object.name()
|
||||
+ " densityIndex=" + densityIndex + " xx=" + x + " zz=" + z + " mode=" + placement.mode());
|
||||
}
|
||||
try {
|
||||
PlacementTags tags = tags(object, id, "surface", treePlacement, placement);
|
||||
int result = chunk.placeOnSurface(object, x, z, placement, changes, tags, goldenTag);
|
||||
if (result >= 0) {
|
||||
tally.placed++;
|
||||
} else {
|
||||
tally.rejected++;
|
||||
}
|
||||
traceObjectResult(scope, object, result, x, z, densityIndex, density);
|
||||
} catch (Throwable failure) {
|
||||
tally.errors++;
|
||||
log.error("Regen object placement exception: chunk=" + chunkX + "," + chunkZ + " scope=" + scope + " object=" + object.name()
|
||||
+ " densityIndex=" + densityIndex + " density=" + density + " error=" + failure.getClass().getSimpleName() + ":" + failure.getMessage(), failure);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,8 +22,10 @@ import art.arcane.iris.builtin.objects.ObjectPlacement;
|
||||
import art.arcane.iris.builtin.objects.ObjectSettings;
|
||||
import art.arcane.iris.modules.objects.EffectiveSettings;
|
||||
import art.arcane.iris.modules.objects.ObjectPool;
|
||||
import art.arcane.iris.modules.objects.ObjectSeparation;
|
||||
import art.arcane.volmlib.util.math.RNG;
|
||||
import com.volmit.ModuleLog;
|
||||
import com.volmit.config.FeatureSettings;
|
||||
import com.volmit.config.Level;
|
||||
import com.volmit.config.SettingsStack;
|
||||
import com.volmit.object.PlaceableObject;
|
||||
@@ -47,12 +49,14 @@ public final class ObjectPlacing {
|
||||
|
||||
private final PackWorld world;
|
||||
private final ObjectPool objects;
|
||||
private final ObjectSeparation separation;
|
||||
private final EffectiveSettings settings = new EffectiveSettings();
|
||||
private final ModuleLog log;
|
||||
|
||||
private ObjectPlacing(PackWorld world) {
|
||||
this.world = world;
|
||||
this.objects = ObjectPool.of(world);
|
||||
this.separation = ObjectSeparation.of(world);
|
||||
this.log = world.log();
|
||||
}
|
||||
|
||||
@@ -71,22 +75,36 @@ public final class ObjectPlacing {
|
||||
RNG rng = objects.chunkRandom(chunkX, chunkZ);
|
||||
int centerX = (chunkX << CHUNK_SHIFT) + CHUNK_CENTER_OFFSET_BLOCKS;
|
||||
int centerZ = (chunkZ << CHUNK_SHIFT) + CHUNK_CENTER_OFFSET_BLOCKS;
|
||||
SettingsStack<ObjectSettings> declared = world.running().orElseThrow().settings().at(ObjectSettings.class, centerX, centerZ);
|
||||
FeatureSettings worldSettings = world.running().orElseThrow().settings();
|
||||
SettingsStack<ObjectSettings> declared = worldSettings.at(ObjectSettings.class, centerX, centerZ);
|
||||
Set<String> keys = new HashSet<>();
|
||||
for (Level level : GUESSED_LEVELS) {
|
||||
declared.level(level).ifPresent(settings -> guessFrom(settings, rng, chunkX, chunkZ, keys));
|
||||
declared.level(level).ifPresent(settings -> guessFrom(settings, worldSettings, rng, chunkX, chunkZ, keys));
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
private void guessFrom(ObjectSettings settings, RNG rng, int chunkX, int chunkZ, Set<String> keys) {
|
||||
private void guessFrom(ObjectSettings settings, FeatureSettings worldSettings, RNG rng, int chunkX, int chunkZ, Set<String> keys) {
|
||||
int centerX = (chunkX << CHUNK_SHIFT) + CHUNK_CENTER_OFFSET_BLOCKS;
|
||||
int centerZ = (chunkZ << CHUNK_SHIFT) + CHUNK_CENTER_OFFSET_BLOCKS;
|
||||
for (ObjectPlacement placement : settings.objects()) {
|
||||
if (placement.carvingSupport().supportsSurface() && rng.chance(placement.chance() + rng.d(-CHANCE_JITTER, CHANCE_JITTER))) {
|
||||
if (!placement.carvingSupport().supportsSurface()) {
|
||||
continue;
|
||||
}
|
||||
if (placement.isSeparated()) {
|
||||
guessSeparatedKey(chunkX, chunkZ, placement, worldSettings).ifPresent(keys::add);
|
||||
} else if (rng.chance(separation.chanceAt(placement, centerX, centerZ) + rng.d(-CHANCE_JITTER, CHANCE_JITTER))) {
|
||||
keys.addAll(guessPlacedKeys(rng, chunkX, chunkZ, placement));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<String> guessSeparatedKey(int chunkX, int chunkZ, ObjectPlacement placement, FeatureSettings worldSettings) {
|
||||
return separation.keptCandidate(chunkX, chunkZ, placement, worldSettings)
|
||||
.flatMap(candidate -> objects.pickScaled(placement, candidate.rng()))
|
||||
.map(PlaceableObject::name);
|
||||
}
|
||||
|
||||
private Set<String> guessPlacedKeys(RNG rng, int x, int z, ObjectPlacement placement) {
|
||||
Set<String> keys = new HashSet<>();
|
||||
for (int i = 0; i < objects.density(placement, rng, x, z); i++) {
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
package art.arcane.iris.modules.objects;
|
||||
|
||||
import art.arcane.iris.builtin.IrisSeedPurposes;
|
||||
import art.arcane.iris.builtin.objects.ObjectPlacement;
|
||||
import art.arcane.iris.builtin.objects.ObjectSettings;
|
||||
import art.arcane.volmlib.util.math.RNG;
|
||||
import com.volmit.config.FeatureSettings;
|
||||
import com.volmit.config.Level;
|
||||
import com.volmit.config.SettingsStack;
|
||||
import com.volmit.noise.NoiseSampler;
|
||||
import com.volmit.noise.NoiseStyle;
|
||||
import com.volmit.world.PackWorld;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* The chance and spacing of surface object placements in a world.
|
||||
*
|
||||
* <p>A separated placement rolls one candidate spot per chunk. The candidate stands only when no candidate of a lower rank,
|
||||
* from a placement with the same objects, lies closer than the separation. Spots and ranks come from the world seed, the
|
||||
* chunk and the objects alone, so every chunk agrees on them across chunk, biome and region borders, and objects never
|
||||
* stand closer than the separation, whatever order chunks generate in.
|
||||
*/
|
||||
public final class ObjectSeparation {
|
||||
private static final int CHUNK_WIDTH_BLOCKS = 16;
|
||||
private static final int CHUNK_SHIFT = 4;
|
||||
/** Placements read a chunk's settings at local 8, 8, as Iris always did. */
|
||||
private static final int SETTINGS_COLUMN_OFFSET_BLOCKS = 8;
|
||||
/** A chunk places the biome's objects before the region's, so the biome's placement speaks for the chunk. */
|
||||
private static final List<Level> PLACING_LEVELS = List.of(Level.BIOME, Level.REGION);
|
||||
|
||||
private final PackWorld world;
|
||||
private final long seed;
|
||||
private final Map<NoiseStyle, NoiseSampler> chanceNoises = new ConcurrentHashMap<>();
|
||||
|
||||
private ObjectSeparation(PackWorld world) {
|
||||
this.world = world;
|
||||
this.seed = world.seed(IrisSeedPurposes.OBJECT_SEPARATION);
|
||||
}
|
||||
|
||||
/** The separation of {@code world}. */
|
||||
public static ObjectSeparation of(PackWorld world) {
|
||||
return world.state(ObjectSeparation.class, ObjectSeparation::new);
|
||||
}
|
||||
|
||||
/** The chance to place at the block column: the placement's chance, multiplied by its chance noise when it has one. */
|
||||
public double chanceAt(ObjectPlacement placement, double x, double z) {
|
||||
Optional<NoiseStyle> style = placement.chanceStyle();
|
||||
if (style.isEmpty()) {
|
||||
return placement.chance();
|
||||
}
|
||||
double noise = chanceNoises.computeIfAbsent(style.get(), declared -> world.resources().noise().sampler(declared, seed)).sample(x, z);
|
||||
return placement.chance() * Math.max(0D, Math.min(1D, noise));
|
||||
}
|
||||
|
||||
/**
|
||||
* The candidate the chunk places for the separated placement; empty when it rolled none or a lower ranked one is too
|
||||
* close. {@code settings} tells which placements the neighbouring chunks declare.
|
||||
*/
|
||||
public Optional<Candidate> keptCandidate(int chunkX, int chunkZ, ObjectPlacement placement, FeatureSettings settings) {
|
||||
Optional<Candidate> own = candidateOf(chunkX, chunkZ, placement);
|
||||
if (own.isEmpty() || isOutranked(own.get(), chunkX, chunkZ, placement, settings)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return own;
|
||||
}
|
||||
|
||||
private boolean isOutranked(Candidate own, int chunkX, int chunkZ, ObjectPlacement placement, FeatureSettings settings) {
|
||||
int reach = placement.separation();
|
||||
for (int x = Math.floorDiv(own.x() - reach, CHUNK_WIDTH_BLOCKS); x <= Math.floorDiv(own.x() + reach, CHUNK_WIDTH_BLOCKS); x++) {
|
||||
for (int z = Math.floorDiv(own.z() - reach, CHUNK_WIDTH_BLOCKS); z <= Math.floorDiv(own.z() + reach, CHUNK_WIDTH_BLOCKS); z++) {
|
||||
if (x == chunkX && z == chunkZ) {
|
||||
continue;
|
||||
}
|
||||
int rivalX = x;
|
||||
int rivalZ = z;
|
||||
Optional<Candidate> rival = placementOfSameObjectsAt(rivalX, rivalZ, placement.place(), settings)
|
||||
.flatMap(other -> candidateOf(rivalX, rivalZ, other));
|
||||
if (rival.isPresent() && rival.get().rank() < own.rank() && rival.get().isCloserThan(own, reach)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static Optional<ObjectPlacement> placementOfSameObjectsAt(int chunkX, int chunkZ, List<String> objects, FeatureSettings settings) {
|
||||
int x = (chunkX << CHUNK_SHIFT) + SETTINGS_COLUMN_OFFSET_BLOCKS;
|
||||
int z = (chunkZ << CHUNK_SHIFT) + SETTINGS_COLUMN_OFFSET_BLOCKS;
|
||||
SettingsStack<ObjectSettings> declared = settings.at(ObjectSettings.class, x, z);
|
||||
for (Level level : PLACING_LEVELS) {
|
||||
Optional<ObjectPlacement> placing = declared.level(level).flatMap(placements -> separatedPlacing(placements, objects));
|
||||
if (placing.isPresent()) {
|
||||
return placing;
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private static Optional<ObjectPlacement> separatedPlacing(ObjectSettings settings, List<String> objects) {
|
||||
return settings.objects().stream()
|
||||
.filter(placement -> placement.isSeparated() && placement.carvingSupport().supportsSurface() && placement.place().equals(objects))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
private Optional<Candidate> candidateOf(int chunkX, int chunkZ, ObjectPlacement placement) {
|
||||
RNG rng = new RNG(chunkSeed(chunkX, chunkZ, placement.place().hashCode()));
|
||||
int x = (chunkX << CHUNK_SHIFT) + rng.nextInt(CHUNK_WIDTH_BLOCKS);
|
||||
int z = (chunkZ << CHUNK_SHIFT) + rng.nextInt(CHUNK_WIDTH_BLOCKS);
|
||||
if (!rng.chance(chanceAt(placement, x, z))) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(new Candidate(x, z, rng.nextLong(), rng));
|
||||
}
|
||||
|
||||
// Each input is mixed in after the previous one: XORing scaled coordinates gave chunk x,z and -x,-z the same seed
|
||||
// whenever both were odd.
|
||||
private long chunkSeed(int chunkX, int chunkZ, int objectsHash) {
|
||||
return splitMix(splitMix(splitMix(seed + objectsHash) + chunkX) + chunkZ);
|
||||
}
|
||||
|
||||
private static long splitMix(long value) {
|
||||
long mixed = value + 0x9E3779B97F4A7C15L;
|
||||
mixed = (mixed ^ (mixed >>> 30)) * 0xBF58476D1CE4E5B9L;
|
||||
mixed = (mixed ^ (mixed >>> 27)) * 0x94D049BB133111EBL;
|
||||
return mixed ^ (mixed >>> 31);
|
||||
}
|
||||
|
||||
/** Where a chunk would place, in block coordinates, with the random that picks and places the object there. */
|
||||
public record Candidate(int x, int z, long rank, RNG rng) {
|
||||
boolean isCloserThan(Candidate other, int blocks) {
|
||||
long dx = x - other.x;
|
||||
long dz = z - other.z;
|
||||
return dx * dx + dz * dz < (long) blocks * blocks;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user