Share carve ownership rule

- Add CarveOwnership, ranking hydrology over subterrain over transformed over natural
- Resolve IrisCarveModifier cell ownership through IrisCarveModifier.carveOwner and CarveOwnership
- Let hydrology win over subterrain rooms and transformed columns in MantleWriter isCarved and isPrerequisiteCarved
- Check subterrain rooms in EngineMantle.isCarved and let hydrology win over transformed columns
- Add CarveOwnershipTest running every reader over every flag combination
This commit is contained in:
Brian Neumann-Fopiano committed 2026-11-01 17:48:15 -05:00
1 parent 6f38d98e9e
commit ccd5551854
6 files changed
+247 -52

No files matched your search

@@ -0,0 +1,23 @@
package art.arcane.iris.generation.mantle;
public final class CarveOwnership {
private CarveOwnership() {
}
public static Owner resolve(boolean hydrology, boolean subterrain, boolean transformed) {
if (hydrology) {
return Owner.HYDROLOGY;
}
if (subterrain) {
return Owner.SUBTERRAIN;
}
return transformed ? Owner.TRANSFORMED : Owner.NATURAL;
}
public enum Owner {
HYDROLOGY,
SUBTERRAIN,
TRANSFORMED,
NATURAL
}
}
@@ -31,6 +31,7 @@ import art.arcane.iris.generation.terrain.IrisDimension;
import art.arcane.iris.generation.terrain.Terrain3DColumn;
import art.arcane.iris.generation.hydrology.cave.HydrologyCaveCell;
import art.arcane.iris.generation.hydrology.cave.HydrologyCaveStorage;
import art.arcane.iris.generation.subterrain.SubterrainCell;
import art.arcane.iris.pack.value.IrisPosition;
import art.arcane.iris.generation.block.BoundBlockState;
import art.arcane.iris.world.storage.matter.PreObjectMatterCell;
@@ -123,16 +124,16 @@ public interface EngineMantle extends MatterGenerator {
}
default boolean isCarved(int x, int h, int z) {
Terrain3DColumn transformed = getComplex().transformedColumn(x, z);
if (transformed != null) {
return h >= 0 && h < transformed.topY() && !transformed.isSolid(h);
}
HydrologyCaveCell hydrology = HydrologyCaveStorage.getIfPresent(getMantle(), x, h, z);
if (hydrology != null) {
return hydrology.carves();
}
return getMantle().get(x, h, z, MatterCavern.class) != null
|| getComplex().isTerrain3DOpening(x, h, z);
SubterrainCell feature = getEngine().getSubterrainCell(x, h, z);
Terrain3DColumn transformed = getComplex().transformedColumn(x, z);
return switch (CarveOwnership.resolve(hydrology != null, feature != null && feature.owned(), transformed != null)) {
case HYDROLOGY -> hydrology.carves();
case SUBTERRAIN -> feature.carve();
case TRANSFORMED -> h >= 0 && h < transformed.topY() && !transformed.isSolid(h);
case NATURAL -> getMantle().get(x, h, z, MatterCavern.class) != null
|| getComplex().isTerrain3DOpening(x, h, z);
};
}
default NativeBlockState get(int x, int y, int z) {
@@ -460,20 +460,16 @@ public class MantleWriter implements ObjectPassPlacer, AutoCloseable {
}
public boolean isPrerequisiteCarved(int x, int y, int z) {
SubterrainCell feature = getEngine().getSubterrainCell(x, y, z);
if (feature != null && feature.owned()) {
return feature.carve();
}
Terrain3DColumn transformed = engineMantle.getComplex().transformedColumn(x, z);
if (transformed != null) {
return y >= 0 && y < transformed.topY() && !transformed.isSolid(y);
}
HydrologyCaveCell hydrology = getPrerequisiteDataIfPresent(x, y, z, HydrologyCaveCell.class);
if (hydrology != null) {
return hydrology.carves();
}
return getPrerequisiteDataIfPresent(x, y, z, MatterCavern.class) != null
|| engineMantle.getComplex().isTerrain3DOpening(x, y, z);
SubterrainCell feature = getEngine().getSubterrainCell(x, y, z);
Terrain3DColumn transformed = engineMantle.getComplex().transformedColumn(x, z);
return switch (CarveOwnership.resolve(hydrology != null, feature != null && feature.owned(), transformed != null)) {
case HYDROLOGY -> hydrology.carves();
case SUBTERRAIN -> feature.carve();
case TRANSFORMED -> y >= 0 && y < transformed.topY() && !transformed.isSolid(y);
case NATURAL -> getPrerequisiteDataIfPresent(x, y, z, MatterCavern.class) != null
|| engineMantle.getComplex().isTerrain3DOpening(x, y, z);
};
}
public byte[] getPrerequisiteCarvedColumn(int x, int z, int height) {
@@ -820,20 +816,16 @@ public class MantleWriter implements ObjectPassPlacer, AutoCloseable {
@Override
public boolean isCarved(int x, int y, int z) {
SubterrainCell feature = getEngine().getSubterrainCell(x, y, z);
if (feature != null && feature.owned()) {
return feature.carve();
}
Terrain3DColumn transformed = engineMantle.getComplex().transformedColumn(x, z);
if (transformed != null) {
return y >= 0 && y < transformed.topY() && !transformed.isSolid(y);
}
HydrologyCaveCell hydrology = getDataIfPresent(x, y, z, HydrologyCaveCell.class);
if (hydrology != null) {
return hydrology.carves();
}
return getDataIfPresent(x, y, z, MatterCavern.class) != null
|| engineMantle.getComplex().isTerrain3DOpening(x, y, z);
SubterrainCell feature = getEngine().getSubterrainCell(x, y, z);
Terrain3DColumn transformed = engineMantle.getComplex().transformedColumn(x, z);
return switch (CarveOwnership.resolve(hydrology != null, feature != null && feature.owned(), transformed != null)) {
case HYDROLOGY -> hydrology.carves();
case SUBTERRAIN -> feature.carve();
case TRANSFORMED -> y >= 0 && y < transformed.topY() && !transformed.isSolid(y);
case NATURAL -> getDataIfPresent(x, y, z, MatterCavern.class) != null
|| engineMantle.getComplex().isTerrain3DOpening(x, y, z);
};
}
@@ -34,6 +34,7 @@ import art.arcane.iris.generation.runtime.IrisComplex;
import art.arcane.iris.generation.terrain.IrisDimension;
import art.arcane.volmlib.util.stream.ProceduralStream;
import art.arcane.iris.generation.runtime.EngineAssignedModifier;
import art.arcane.iris.generation.mantle.CarveOwnership;
import art.arcane.iris.generation.mantle.CaveCeilingMask;
import art.arcane.iris.generation.mantle.CaveTerrainSnapshot;
import art.arcane.iris.generation.mantle.TerrainMatterView;
@@ -379,8 +380,12 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
return cavern.getLiquid() == 3 ? air : null;
}
static boolean subterrainOwns(SubterrainCell feature, HydrologyCaveCell hydrology) {
return hydrology == null && feature.owned() && (!feature.solid() || feature.room().reservedSolid());
public static CarveOwnership.Owner carveOwner(HydrologyCaveCell hydrology, SubterrainCell feature, Terrain3DColumn terrain, int y) {
return CarveOwnership.resolve(
hydrology != null,
feature.owned() && (!feature.solid() || feature.room().reservedSolid()),
terrain != null && y >= terrain.minY() && y < terrain.topY() && !terrain.isSolid(y)
);
}
static NativeBlockState sealGuardState(NativeBlockState current, NativeBlockState rock) {
@@ -473,7 +478,9 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
SubterrainCell feature = context.subterrainSampler() == null ? SubterrainCell.OUTSIDE
: context.subterrainSampler().sampleCandidates(context.chunkBlockX() + localX, y + context.minimumY(),
context.chunkBlockZ() + localZ);
if (subterrainOwns(feature, hydrology)) {
CarveOwnership.Owner owner = carveOwner(hydrology, feature,
context.terrainColumns() == null ? null : context.terrainColumns()[columnIndex], y);
if (owner == CarveOwnership.Owner.SUBTERRAIN) {
if (!feature.solid() && context.caveCeilings() != null && y > context.caveCeilings()[columnIndex]) {
return;
}
@@ -495,13 +502,10 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
return;
}
NativeBlockState current = context.output().getRaw(localX, y, localZ);
if (hydrology == null && context.terrainColumns() != null) {
Terrain3DColumn column = context.terrainColumns()[columnIndex];
if (column != null && y >= column.minY() && y < column.topY() && !column.isSolid(y)) {
return;
}
if (owner == CarveOwnership.Owner.TRANSFORMED) {
return;
}
NativeBlockState current = context.output().getRaw(localX, y, localZ);
if (hydrology != null && hydrology.action() == HydrologyCaveAction.SEAL_GUARD) {
NativeBlockState sealed = sealGuardState(current, context.chunkContext().getRock().get(localX, localZ));
if (sealed != current) {
@@ -0,0 +1,171 @@
package art.arcane.iris.generation.mantle;
import art.arcane.iris.generation.hydrology.cave.HydrologyCaveAction;
import art.arcane.iris.generation.hydrology.cave.HydrologyCaveCell;
import art.arcane.iris.generation.runtime.Engine;
import art.arcane.iris.generation.runtime.IrisComplex;
import art.arcane.iris.generation.stage.IrisCarveModifier;
import art.arcane.iris.generation.subterrain.IrisSubterrainFamily;
import art.arcane.iris.generation.subterrain.SubterrainCell;
import art.arcane.iris.generation.subterrain.SubterrainRoom;
import art.arcane.iris.generation.terrain.Terrain3DColumn;
import art.arcane.iris.testsupport.PlatformBinding;
import art.arcane.volmlib.util.mantle.runtime.Mantle;
import art.arcane.volmlib.util.mantle.runtime.MantleChunk;
import art.arcane.volmlib.util.mantle.runtime.TectonicPlate;
import art.arcane.volmlib.util.matter.Matter;
import art.arcane.volmlib.util.matter.MatterCavern;
import art.arcane.volmlib.util.matter.MatterSlice;
import org.junit.Rule;
import org.junit.Test;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class CarveOwnershipTest {
private static final int X = 1;
private static final int Y = 2;
private static final int Z = 3;
private static final int HEIGHT = 64;
private static final SubterrainRoom ROOM = new SubterrainRoom("room", "", IrisSubterrainFamily.CENOTE,
0, 0, 0, 0, 0, 0, -10, 10, 1D, 0, false, true, SubterrainCell.Kind.AIR);
@Rule
public final PlatformBinding platform = PlatformBinding.mockPlatform();
@Test
public void resolveRanksHydrologyThenSubterrainThenTransformed() {
assertEquals(CarveOwnership.Owner.NATURAL, CarveOwnership.resolve(false, false, false));
assertEquals(CarveOwnership.Owner.TRANSFORMED, CarveOwnership.resolve(false, false, true));
assertEquals(CarveOwnership.Owner.SUBTERRAIN, CarveOwnership.resolve(false, true, false));
assertEquals(CarveOwnership.Owner.SUBTERRAIN, CarveOwnership.resolve(false, true, true));
assertEquals(CarveOwnership.Owner.HYDROLOGY, CarveOwnership.resolve(true, false, false));
assertEquals(CarveOwnership.Owner.HYDROLOGY, CarveOwnership.resolve(true, false, true));
assertEquals(CarveOwnership.Owner.HYDROLOGY, CarveOwnership.resolve(true, true, false));
assertEquals(CarveOwnership.Owner.HYDROLOGY, CarveOwnership.resolve(true, true, true));
}
@Test
public void allReadersAgree() {
List<String> disagreements = new ArrayList<>();
for (int flags = 0; flags < 8; flags++) {
boolean hydrology = (flags & 4) != 0;
boolean subterrain = (flags & 2) != 0;
boolean transformed = (flags & 1) != 0;
CarveOwnership.Owner expected = CarveOwnership.resolve(hydrology, subterrain, transformed);
Map<String, CarveOwnership.Owner> owners = new LinkedHashMap<>();
owners.put("IrisCarveModifier", IrisCarveModifier.carveOwner(
hydrology ? HydrologyCaveCell.of(HydrologyCaveAction.DRY_AIR) : null,
subterrain ? new SubterrainCell(SubterrainCell.Kind.AIR, "", ROOM) : SubterrainCell.OUTSIDE,
transformed ? column(true) : null,
Y));
owners.put("MantleWriter.isCarved",
witnessedOwner(hydrology, subterrain, transformed, CarveOwnershipTest::writerCarved));
owners.put("MantleWriter.isPrerequisiteCarved",
witnessedOwner(hydrology, subterrain, transformed, CarveOwnershipTest::writerPrerequisiteCarved));
owners.put("EngineMantle.isCarved",
witnessedOwner(hydrology, subterrain, transformed, CarveOwnershipTest::engineMantleCarved));
for (Map.Entry<String, CarveOwnership.Owner> owner : owners.entrySet()) {
if (owner.getValue() != expected) {
disagreements.add("hydrology=" + hydrology + " subterrain=" + subterrain + " transformed=" + transformed
+ ": " + owner.getKey() + " gives " + owner.getValue() + ", the rule gives " + expected);
}
}
}
assertTrue(String.join("\n", disagreements), disagreements.isEmpty());
}
private static CarveOwnership.Owner witnessedOwner(boolean hydrology, boolean subterrain, boolean transformed,
Predicate<Sources> reader) {
List<CarveOwnership.Owner> carving = new ArrayList<>();
for (CarveOwnership.Owner candidate : CarveOwnership.Owner.values()) {
boolean present = switch (candidate) {
case HYDROLOGY -> hydrology;
case SUBTERRAIN -> subterrain;
case TRANSFORMED -> transformed;
case NATURAL -> true;
};
if (present && reader.test(Sources.carvedOnlyBy(candidate, hydrology, subterrain, transformed))) {
carving.add(candidate);
}
}
return carving.size() == 1 ? carving.getFirst() : null;
}
private static boolean writerCarved(Sources sources) {
try (MantleWriter writer = new MantleWriter(sources.engineMantle(), sources.mantle(), 0, 0, 0, false)) {
return writer.isCarved(X, Y, Z);
}
}
private static boolean writerPrerequisiteCarved(Sources sources) {
try (MantleWriter writer = new MantleWriter(sources.engineMantle(), sources.mantle(), 0, 0, 0, false)) {
return writer.isPrerequisiteCarved(X, Y, Z);
}
}
private static boolean engineMantleCarved(Sources sources) {
return sources.engineMantle().isCarved(X, Y, Z);
}
private static Terrain3DColumn column(boolean openAtY) {
return Terrain3DColumn.fromOccupancy(HEIGHT - 1, HEIGHT, y -> !openAtY || y != Y);
}
private record Sources(EngineMantle engineMantle, Mantle<Matter> mantle) {
private static Sources carvedOnlyBy(CarveOwnership.Owner carving, boolean hydrology, boolean subterrain,
boolean transformed) {
return mocked(
hydrology ? HydrologyCaveCell.of(carving == CarveOwnership.Owner.HYDROLOGY
? HydrologyCaveAction.DRY_AIR : HydrologyCaveAction.SEAL_GUARD) : null,
subterrain ? new SubterrainCell(carving == CarveOwnership.Owner.SUBTERRAIN
? SubterrainCell.Kind.AIR : SubterrainCell.Kind.SOLID, "", ROOM) : SubterrainCell.OUTSIDE,
transformed ? column(carving == CarveOwnership.Owner.TRANSFORMED) : null,
carving == CarveOwnership.Owner.NATURAL ? new MatterCavern(true, "", (byte) 0) : null);
}
@SuppressWarnings("unchecked")
private static Sources mocked(HydrologyCaveCell hydrology, SubterrainCell subterrain, Terrain3DColumn transformed,
MatterCavern cavern) {
EngineMantle engineMantle = mock(EngineMantle.class);
Engine engine = mock(Engine.class);
IrisComplex complex = mock(IrisComplex.class);
Mantle<Matter> mantle = mock(Mantle.class);
TectonicPlate<Matter> plate = mock(TectonicPlate.class);
MantleChunk<Matter> chunk = mock(MantleChunk.class);
Matter matter = mock(Matter.class);
MatterSlice<HydrologyCaveCell> hydrologySlice = mock(MatterSlice.class);
MatterSlice<MatterCavern> cavernSlice = mock(MatterSlice.class);
when(engineMantle.getEngine()).thenReturn(engine);
when(engineMantle.getComplex()).thenReturn(complex);
when(engineMantle.getMantle()).thenReturn(mantle);
when(engineMantle.isCarved(anyInt(), anyInt(), anyInt())).thenCallRealMethod();
when(engine.getSubterrainCell(X, Y, Z)).thenReturn(subterrain);
when(complex.transformedColumn(X, Z)).thenReturn(transformed);
when(mantle.getWorldHeight()).thenReturn(HEIGHT);
when(mantle.useChunk(0, 0)).thenReturn(chunk);
when(mantle.getLoadedRegion(0, 0)).thenReturn(plate);
when(mantle.get(X, Y, Z, MatterCavern.class)).thenReturn(cavern);
when(plate.get(0, 0)).thenReturn(chunk);
when(chunk.exists(0)).thenReturn(true);
when(chunk.get(0)).thenReturn(matter);
when(matter.hasSlice(HydrologyCaveCell.class)).thenReturn(true);
when(matter.hasSlice(MatterCavern.class)).thenReturn(true);
doReturn(hydrologySlice).when(matter).getSlice(HydrologyCaveCell.class);
doReturn(cavernSlice).when(matter).getSlice(MatterCavern.class);
when(hydrologySlice.get(X, Y, Z)).thenReturn(hydrology);
when(cavernSlice.get(X, Y, Z)).thenReturn(cavern);
return new Sources(engineMantle, mantle);
}
}
}
@@ -7,6 +7,7 @@ import art.arcane.iris.generation.hydrology.plan.HydrologySettings;
import art.arcane.iris.generation.hydrology.underground.CavePoolFiller;
import art.arcane.iris.generation.hydrology.underground.CaveVoxel;
import art.arcane.iris.generation.hydrology.underground.CarvedTerrain;
import art.arcane.iris.generation.mantle.CarveOwnership;
import art.arcane.iris.generation.mantle.CaveTerrainSnapshot;
import art.arcane.iris.generation.mantle.MantleWriter;
import art.arcane.iris.generation.runtime.IrisComplex;
@@ -203,16 +204,19 @@ public class IrisCarveModifierHydrologyTest {
SubterrainCell.Kind.AIR);
SubterrainCell open = new SubterrainCell(SubterrainCell.Kind.AIR, "", room);
assertTrue(IrisCarveModifier.subterrainOwns(open, null));
assertFalse(IrisCarveModifier.subterrainOwns(open, HydrologyCaveCell.of(HydrologyCaveAction.DRY_AIR)));
assertFalse(IrisCarveModifier.subterrainOwns(open, HydrologyCaveCell.of(HydrologyCaveAction.WET_SOURCE)));
assertFalse(IrisCarveModifier.subterrainOwns(open, HydrologyCaveCell.of(HydrologyCaveAction.SEAL_GUARD)));
assertFalse(IrisCarveModifier.subterrainOwns(SubterrainCell.OUTSIDE, null));
assertEquals(CarveOwnership.Owner.SUBTERRAIN, IrisCarveModifier.carveOwner(null, open, null, 0));
assertEquals(CarveOwnership.Owner.HYDROLOGY,
IrisCarveModifier.carveOwner(HydrologyCaveCell.of(HydrologyCaveAction.DRY_AIR), open, null, 0));
assertEquals(CarveOwnership.Owner.HYDROLOGY,
IrisCarveModifier.carveOwner(HydrologyCaveCell.of(HydrologyCaveAction.WET_SOURCE), open, null, 0));
assertEquals(CarveOwnership.Owner.HYDROLOGY,
IrisCarveModifier.carveOwner(HydrologyCaveCell.of(HydrologyCaveAction.SEAL_GUARD), open, null, 0));
assertEquals(CarveOwnership.Owner.NATURAL, IrisCarveModifier.carveOwner(null, SubterrainCell.OUTSIDE, null, 0));
for (SubterrainCell.Kind kind : new SubterrainCell.Kind[]{SubterrainCell.Kind.WATER, SubterrainCell.Kind.LAVA}) {
SubterrainCell fluid = new SubterrainCell(kind, "", room);
assertTrue(IrisCarveModifier.subterrainOwns(fluid, null));
assertFalse("subterrain " + kind + " yields the inlet seal", IrisCarveModifier.subterrainOwns(fluid,
HydrologyCaveCell.of(HydrologyCaveAction.SEAL_GUARD)));
assertEquals(CarveOwnership.Owner.SUBTERRAIN, IrisCarveModifier.carveOwner(null, fluid, null, 0));
assertEquals("subterrain " + kind + " yields the inlet seal", CarveOwnership.Owner.HYDROLOGY,
IrisCarveModifier.carveOwner(HydrologyCaveCell.of(HydrologyCaveAction.SEAL_GUARD), fluid, null, 0));
}
}