Add a Caves view to the studio

The carve module registers a scene view that lists every file of the
pack's caves/ folder and draws each one carved over 128 by 128 blocks
under a flat surface at Y 96, with the seed generation uses. Carved
space is drawn as blocks, tunnels light blue and caverns orange, so the
shape of the cave shows instead of the rock around it. Four variants
show the same cave in four places. Each file opens as Cave, Tunnel,
Cavern and Pillars nodes, and new caves can be created from the view.

BlockParts copied its map with Map.copyOf, whose open addressing takes
quadratic time on the colliding hash codes of a dense block grid; one
cave scene took 538 seconds. It now copies into a HashMap.

A studio that connected after the bridge's first poll got no world
event of its own, took the first hotload for its starting state and did
not redraw. Each joining studio is now sent the world state on its own.
This commit is contained in:
piexel committed 2026-11-12 22:15:59 +01:00
1 parent 7d166df70f
commit e862a80615
12 files changed
+638 -24

No files matched your search

@@ -45,6 +45,8 @@ final class StudioEvents {
private final StudioWorld world;
private final Gson gson;
private final List<OutputStream> listeners = new CopyOnWriteArrayList<>();
/** Listeners not yet told the world's state. */
private final List<OutputStream> joining = new CopyOnWriteArrayList<>();
private final AtomicBoolean listenerJoined = new AtomicBoolean();
private final AtomicBoolean usageWanted = new AtomicBoolean();
private final AtomicBoolean hotloaded = new AtomicBoolean();
@@ -60,6 +62,7 @@ final class StudioEvents {
void listen(OutputStream stream) {
listeners.add(stream);
joining.add(stream);
listenerJoined.set(true);
usageWanted.set(true);
}
@@ -76,10 +79,14 @@ final class StudioEvents {
}
StudioWorld.Session session = world.session();
Engine engine = session == null ? null : session.engine();
List<OutputStream> joined = List.copyOf(joining);
if (engine != lastEngine || hotloaded.getAndSet(false)) {
lastEngine = engine;
send("world", new WorldChange(engine != null));
} else {
greet(joined, engine != null);
}
joining.removeAll(joined);
List<GuiMarker> players = session == null || session.overlay() == null ? List.of() : session.overlay().players();
if (listenerJoined.getAndSet(false) || !Objects.equals(players, lastPlayers)) {
lastPlayers = players;
@@ -100,22 +107,46 @@ final class StudioEvents {
closeQuietly(listener);
}
listeners.clear();
joining.clear();
}
/**
* Tells studios that just connected what state the world is in, them alone. A studio takes its first world event as
* the state it connected to, so without one of its own it would take the next hotload for it and not redraw.
*/
private void greet(List<OutputStream> joined, boolean running) {
if (joined.isEmpty()) {
return;
}
byte[] message = eventOf("world", new WorldChange(running));
for (OutputStream listener : joined) {
sendTo(listener, message);
}
}
private void send(String event, Object data) {
sendToAll(("event: " + event + "\ndata: " + gson.toJson(data) + "\n\n").getBytes(StandardCharsets.UTF_8));
sendToAll(eventOf(event, data));
}
private byte[] eventOf(String event, Object data) {
return ("event: " + event + "\ndata: " + gson.toJson(data) + "\n\n").getBytes(StandardCharsets.UTF_8);
}
private void sendToAll(byte[] message) {
lastSentMillis = System.currentTimeMillis();
for (OutputStream listener : listeners) {
try {
listener.write(message);
listener.flush();
} catch (IOException gone) {
listeners.remove(listener);
closeQuietly(listener);
}
sendTo(listener, message);
}
}
private void sendTo(OutputStream listener, byte[] message) {
try {
listener.write(message);
listener.flush();
} catch (IOException gone) {
listeners.remove(listener);
joining.remove(listener);
closeQuietly(listener);
}
}
@@ -248,6 +248,9 @@ public final class CarveTestWorld implements PackWorld {
@Override
public List<ResourceId> ids(ResourceKind<?> kind) {
if (kind.equals(CaveResources.CAVES)) {
return caves.keySet().stream().sorted().toList();
}
return kind.equals(BIOMES) ? biomes.keySet().stream().sorted().toList() : List.of();
}
}
@@ -0,0 +1,61 @@
package art.arcane.iris.core.studio.bridge;
import com.google.gson.Gson;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import static org.junit.Assert.assertEquals;
public class StudioEventsTest {
private static final String WORLD_EVENT = "event: world\n";
private final StudioEvents events = new StudioEvents(new StudioWorld("studio", () -> null, null), new Gson());
@Test
public void aStudioThatConnectsGetsTheWorldStateFirst() {
ByteArrayOutputStream studio = new ByteArrayOutputStream();
events.listen(studio);
events.poll();
assertEquals(1, worldEvents(studio));
}
// A studio skips its first world event as the state it connected to, so without one of its own a later window
// would take the first hotload for that state and not redraw.
@Test
public void aStudioThatConnectsLaterGetsItsOwnWorldStateAndTheOthersAreNotRedrawn() {
ByteArrayOutputStream first = new ByteArrayOutputStream();
ByteArrayOutputStream later = new ByteArrayOutputStream();
events.listen(first);
events.poll();
events.listen(later);
events.poll();
assertEquals(1, worldEvents(first));
assertEquals(1, worldEvents(later));
}
@Test
public void everyStudioHearsOfAHotloadAfterItsWorldState() {
ByteArrayOutputStream first = new ByteArrayOutputStream();
ByteArrayOutputStream later = new ByteArrayOutputStream();
events.listen(first);
events.poll();
events.listen(later);
events.poll();
events.hotloaded();
events.poll();
assertEquals(2, worldEvents(first));
assertEquals(2, worldEvents(later));
}
private static int worldEvents(ByteArrayOutputStream stream) {
return stream.toString(StandardCharsets.UTF_8).split(WORLD_EVENT, -1).length - 1;
}
}
@@ -0,0 +1,94 @@
package art.arcane.iris.modules.carve;
import art.arcane.iris.core.modules.CarveTestWorld;
import art.arcane.iris.engine.object.TestSettings;
import art.arcane.iris.modules.carve.field.CavePart;
import com.volmit.object.GeneratedObject;
import com.volmit.object.ObjectBlock;
import com.volmit.tooling.BlockParts;
import com.volmit.tooling.GraphNode;
import com.volmit.tooling.JsonText;
import com.volmit.tooling.NodeGraph;
import com.volmit.tooling.SceneEntry;
import org.junit.Test;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
public class CaveScenesTest {
private static final String MIXED = """
{"tunnel": {"width": {"min": 4, "max": 6}, "stretch": 48},
"cavern": {"size": 40, "height": 18, "coverage": 0.2, "pillars": {"spacing": 14, "thickness": 4}}}
""";
private static final String TUNNELS_ONLY = "{\"tunnel\": {\"width\": {\"min\": 4, \"max\": 6}, \"stretch\": 48}}";
private final CaveScenes view = new CaveScenes();
private final CarveTestWorld world = new CarveTestWorld()
.cave("deep/mixed", TestSettings.of(Cave.class, MIXED))
.cave("tunnels", TestSettings.of(Cave.class, TUNNELS_ONLY));
@Test
public void everyCaveFileIsAnEntryUnderItsFolder() {
List<SceneEntry> entries = view.entries(world);
assertEquals(List.of("deep/mixed", "tunnels"), entries.stream().map(SceneEntry::key).toList());
assertEquals(Optional.of("deep"), entries.getFirst().group());
assertEquals("mixed", entries.getFirst().name());
assertEquals(Optional.empty(), entries.get(1).group());
}
@Test
public void theSceneIsTheCarvedSpaceAndEveryBlockBelongsToAPart() {
GeneratedObject scene = view.scene(world, "deep/mixed", 0).orElseThrow();
BlockParts parts = view.parts(world, "deep/mixed", 0).orElseThrow();
assertFalse(scene.blocks().isEmpty());
Set<Integer> partsSeen = new HashSet<>();
for (ObjectBlock block : scene.blocks()) {
int part = parts.partAt(block.position());
assertNotEquals(BlockParts.NO_PART, part);
partsSeen.add(part);
}
assertEquals(Set.of(CavePart.TUNNEL.ordinal(), CavePart.CAVERN.ordinal()), partsSeen);
}
@Test
public void eachVariantIsTheCaveSomewhereElse() {
GeneratedObject first = view.scene(world, "tunnels", 0).orElseThrow();
assertEquals(first.blocks(), view.scene(world, "tunnels", 0).orElseThrow().blocks());
assertNotEquals(first.blocks(), view.scene(world, "tunnels", 1).orElseThrow().blocks());
assertTrue(view.scene(world, "tunnels", CavePreview.VARIANTS).isEmpty());
assertTrue(view.scene(world, "missing", 0).isEmpty());
}
@Test
public void aCaveFileReadsAsACaveWithItsTunnelAndCavernAndPillars() {
NodeGraph graph = view.graph(world, "deep/mixed", JsonText.parse(MIXED));
GraphNode cave = graph.roots().getFirst();
assertEquals("mixed", cave.title());
GraphNode tunnel = cave.children().get(0).node();
GraphNode cavern = cave.children().get(1).node();
assertEquals("Tunnel", tunnel.title());
assertEquals(CavePart.TUNNEL.ordinal(), tunnel.part().getAsInt());
assertEquals(CavePart.CAVERN.ordinal(), cavern.part().getAsInt());
assertEquals("Pillars", cavern.children().getFirst().node().title());
}
@Test
public void anUnwrittenPartIsShownSoSettingItWritesIt() {
NodeGraph graph = view.graph(world, "tunnels", JsonText.parse(TUNNELS_ONLY));
GraphNode cavern = graph.roots().getFirst().children().get(1).node();
assertEquals("Cavern (unset)", cavern.title());
assertTrue("a cavern the cave does not have draws nothing", cavern.part().isEmpty());
}
}