diff --git a/api/src/main/java/com/volmit/tooling/BlockPattern.java b/api/src/main/java/com/volmit/tooling/BlockPattern.java new file mode 100644 index 000000000..36d9f9c24 --- /dev/null +++ b/api/src/main/java/com/volmit/tooling/BlockPattern.java @@ -0,0 +1,63 @@ +package com.volmit.tooling; + +import com.volmit.block.BlockState; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * What a {@link PatternView} places over a square of columns: the distinct blocks, and which of them each column holds, + * row by row from the north west. + */ +public final class BlockPattern { + /** What {@link #pickAt} gives for a column the pattern places nothing at. */ + public static final int NO_BLOCK = -1; + + private final int columns; + private final List blocks; + private final int[] picks; + + private BlockPattern(int columns, List blocks, int[] picks) { + this.columns = columns; + this.blocks = List.copyOf(blocks); + this.picks = picks; + } + + /** + * The pattern of {@code columns} by {@code columns} columns holding {@code columnBlocks}, row by row from the north + * west, null where nothing is placed. + */ + public static BlockPattern of(int columns, BlockState[] columnBlocks) { + if (columns < 1 || columnBlocks.length != columns * columns) { + throw new IllegalArgumentException("A pattern " + columns + " columns across holds " + columns * columns + + " columns, not " + columnBlocks.length); + } + List blocks = new ArrayList<>(); + Map indexByBlock = new HashMap<>(); + int[] picks = new int[columnBlocks.length]; + for (int column = 0; column < columnBlocks.length; column++) { + BlockState block = columnBlocks[column]; + picks[column] = block == null ? NO_BLOCK : indexByBlock.computeIfAbsent(block, added -> { + blocks.add(added); + return blocks.size() - 1; + }); + } + return new BlockPattern(columns, blocks, picks); + } + + public int columns() { + return columns; + } + + /** The distinct blocks the pattern places, in the order they first appear. */ + public List blocks() { + return blocks; + } + + /** Which of {@link #blocks()} the column holds, or {@link #NO_BLOCK}. */ + public int pickAt(int column, int row) { + return picks[row * columns + column]; + } +} diff --git a/api/src/main/java/com/volmit/tooling/PatternArea.java b/api/src/main/java/com/volmit/tooling/PatternArea.java new file mode 100644 index 000000000..609b545ed --- /dev/null +++ b/api/src/main/java/com/volmit/tooling/PatternArea.java @@ -0,0 +1,26 @@ +package com.volmit.tooling; + +/** + * A square of columns a {@link PatternView} is asked for: {@code columns} by {@code columns} of them, one every + * {@code blocksPerColumn} blocks, the first at block ({@code x}, {@code z}), row by row from the north west. + */ +public record PatternArea(int x, int z, int columns, int blocksPerColumn) { + public PatternArea { + if (columns < 1) { + throw new IllegalArgumentException("A pattern area has at least 1 column across, not " + columns); + } + if (blocksPerColumn < 1) { + throw new IllegalArgumentException("A pattern area has a column every block or farther apart, not every " + blocksPerColumn); + } + } + + /** The block x of the column {@code column} across. */ + public int blockX(int column) { + return x + column * blocksPerColumn; + } + + /** The block z of the row {@code row} down. */ + public int blockZ(int row) { + return z + row * blocksPerColumn; + } +} diff --git a/api/src/main/java/com/volmit/tooling/PatternEntry.java b/api/src/main/java/com/volmit/tooling/PatternEntry.java new file mode 100644 index 000000000..54cefef08 --- /dev/null +++ b/api/src/main/java/com/volmit/tooling/PatternEntry.java @@ -0,0 +1,28 @@ +package com.volmit.tooling; + +import java.util.List; +import java.util.Objects; + +/** + * One entry of a {@link PatternView}'s catalogue. + * + * @param key what the view names the entry by in {@link PatternView#pattern} + * @param name what the studio lists it as, such as "Layer 1" + * @param group what the studio lists it under, such as its biome's name; blank for none + * @param depths how many blocks deep the entry can reach, at least 1 + * @param facts short lines the studio shows with the entry, such as "2 to 4 blocks thick" + */ +public record PatternEntry(String key, String name, String group, int depths, List facts) { + public PatternEntry { + Objects.requireNonNull(key, "key"); + Objects.requireNonNull(name, "name"); + Objects.requireNonNull(group, "group"); + facts = List.copyOf(facts); + if (key.isBlank()) { + throw new IllegalArgumentException("A pattern entry needs a key"); + } + if (depths < 1) { + throw new IllegalArgumentException("Pattern entry " + key + " reaches " + depths + " blocks deep; an entry reaches at least 1"); + } + } +} diff --git a/api/src/main/java/com/volmit/tooling/PatternView.java b/api/src/main/java/com/volmit/tooling/PatternView.java new file mode 100644 index 000000000..9700ab838 --- /dev/null +++ b/api/src/main/java/com/volmit/tooling/PatternView.java @@ -0,0 +1,23 @@ +package com.volmit.tooling; + +import com.volmit.world.PackWorld; + +import java.util.List; +import java.util.Optional; + +/** + * A studio view of the block patterns a module lays over the world, such as a biome's palettes: a catalogue of + * entries, each drawn from above, one block per column, at a depth below its top. + */ +public non-sealed interface PatternView extends StudioView { + /** The view's entries in the world's pack, in the order the studio lists them. */ + List entries(PackWorld world); + + /** + * The blocks the entry named {@code key} places {@code depth} blocks below its top across {@code area}, as the world + * places them; empty when the pack has no such entry. + * + * @param depth from 0, the top block, to the entry's {@link PatternEntry#depths()}, exclusive + */ + Optional pattern(PackWorld world, String key, int depth, PatternArea area); +} diff --git a/api/src/main/java/com/volmit/tooling/SceneView.java b/api/src/main/java/com/volmit/tooling/SceneView.java index 25268f2c0..3edf8e00a 100644 --- a/api/src/main/java/com/volmit/tooling/SceneView.java +++ b/api/src/main/java/com/volmit/tooling/SceneView.java @@ -1,7 +1,6 @@ package com.volmit.tooling; import com.volmit.object.GeneratedObject; -import com.volmit.resource.ResourceId; import com.volmit.world.PackWorld; import java.util.List; @@ -9,19 +8,9 @@ import java.util.Optional; /** * A studio view of the block scenes a module builds from the pack, such as its objects: a catalogue of entries, each - * with variants the studio draws in 3D. The studio asks on its own threads, several at once, for a world of the pack - * it shows; a view keeps nothing between calls that the world's state ({@link PackWorld#state}) should keep. + * with variants the studio draws in 3D. */ -public interface SceneView { - /** The module's id, or one starting with it and {@code /}, such as {@code iris:objects}. */ - ResourceId id(); - - /** The view's name in the studio's list of views, such as "Objects". */ - String label(); - - /** One line on what the view shows. */ - String description(); - +public non-sealed interface SceneView extends StudioView { /** The view's entries in the world's pack, in the order the studio lists them. */ List entries(PackWorld world); diff --git a/api/src/main/java/com/volmit/tooling/StudioView.java b/api/src/main/java/com/volmit/tooling/StudioView.java new file mode 100644 index 000000000..514baee4e --- /dev/null +++ b/api/src/main/java/com/volmit/tooling/StudioView.java @@ -0,0 +1,20 @@ +package com.volmit.tooling; + +import com.volmit.resource.ResourceId; +import com.volmit.world.PackWorld; + +/** + * A view a module adds to the studio, of one of the kinds the studio draws. The studio asks on its own threads, several + * at once, for a world of the pack it shows; a view keeps nothing between calls that the world's state + * ({@link PackWorld#state}) should keep. + */ +public sealed interface StudioView permits SceneView, PatternView { + /** The module's id, or one starting with it and {@code /}, such as {@code iris:objects}. */ + ResourceId id(); + + /** The view's name in the studio's list of views, such as "Objects". */ + String label(); + + /** One line on what the view shows. */ + String description(); +} diff --git a/api/src/main/java/com/volmit/tooling/ToolingRegistry.java b/api/src/main/java/com/volmit/tooling/ToolingRegistry.java index be1a6d29d..2c4c40348 100644 --- a/api/src/main/java/com/volmit/tooling/ToolingRegistry.java +++ b/api/src/main/java/com/volmit/tooling/ToolingRegistry.java @@ -3,9 +3,9 @@ package com.volmit.tooling; /** * Where a module adds its tools to the studio, given in {@link com.volmit.ModuleSetup#tooling()} and accepted only while * {@link com.volmit.IrisModule#setup} runs. A view's id is the module's id or starts with it and {@code /}; a second - * view with an id already registered fails the pack load, naming both modules. + * view with an id already registered, of either kind, fails the pack load, naming both modules. */ public interface ToolingRegistry { - /** Adds a view of block scenes the module builds from the pack, such as its objects. */ - void registerSceneView(SceneView view); + /** Adds a view to the studio: block scenes such as the pack's objects, or block patterns such as its palettes. */ + void registerView(StudioView view); } diff --git a/api/src/test/java/com/volmit/tooling/BlockPatternTest.java b/api/src/test/java/com/volmit/tooling/BlockPatternTest.java new file mode 100644 index 000000000..d68ae5dc0 --- /dev/null +++ b/api/src/test/java/com/volmit/tooling/BlockPatternTest.java @@ -0,0 +1,42 @@ +package com.volmit.tooling; + +import com.volmit.block.BlockState; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class BlockPatternTest { + private static final BlockState GRASS = BlockState.parse("minecraft:grass_block"); + private static final BlockState DIRT = BlockState.parse("minecraft:dirt"); + + @Test + void keepsEachBlockOnceAndWhichColumnHoldsIt() { + BlockPattern pattern = BlockPattern.of(2, new BlockState[]{GRASS, DIRT, null, BlockState.parse("minecraft:grass_block")}); + assertEquals(List.of(GRASS, DIRT), pattern.blocks()); + assertEquals(0, pattern.pickAt(0, 0)); + assertEquals(1, pattern.pickAt(1, 0)); + assertEquals(BlockPattern.NO_BLOCK, pattern.pickAt(0, 1)); + assertEquals(0, pattern.pickAt(1, 1)); + } + + @Test + void needsAColumnForEverySpotOfTheSquare() { + var short_ = assertThrows(IllegalArgumentException.class, () -> BlockPattern.of(2, new BlockState[3])); + assertTrue(short_.getMessage().contains("holds 4 columns, not 3"), short_.getMessage()); + } + + @Test + void placesAreaColumnsFromItsCornerAtItsSpacing() { + PatternArea area = new PatternArea(-64, 32, 16, 4); + assertEquals(-56, area.blockX(2)); + assertEquals(44, area.blockZ(3)); + assertThrows(IllegalArgumentException.class, () -> new PatternArea(0, 0, 16, 0)); + } + + @Test + void anEntryReachesAtLeastOneBlockDeep() { + assertThrows(IllegalArgumentException.class, () -> new PatternEntry("plains#layer/0", "Layer 1", "Plains", 0, List.of())); + } +} diff --git a/host/src/main/java/com/volmit/host/FeatureCatalog.java b/host/src/main/java/com/volmit/host/FeatureCatalog.java index 18733e8fb..73ac93d33 100644 --- a/host/src/main/java/com/volmit/host/FeatureCatalog.java +++ b/host/src/main/java/com/volmit/host/FeatureCatalog.java @@ -41,7 +41,7 @@ public final class FeatureCatalog { private final Map, List> registrationsByService; private final Map, List> providersByService; private final Optional placementType; - private final List sceneViews; + private final List studioViews; FeatureCatalog( Map, FeatureDescriptor> settings, @@ -52,9 +52,9 @@ public final class FeatureCatalog { List lifecycles, List services, PlacementTypeRegistration placementType, - Collection sceneViews) { + Collection studioViews) { this.placementType = Optional.ofNullable(placementType); - this.sceneViews = sorted(sceneViews, registration -> registration.view().id()); + this.studioViews = sorted(studioViews, registration -> registration.view().id()); this.settings = Map.copyOf(settings); this.objects = Map.copyOf(objects); this.mantle = Map.copyOf(mantle); @@ -93,9 +93,9 @@ public final class FeatureCatalog { return validators; } - /** The studio scene views modules registered, by view id. */ - public List sceneViews() { - return sceneViews; + /** The studio views modules registered, of every kind, by view id. */ + public List studioViews() { + return studioViews; } /** The lifecycle hooks modules registered, by module id, each module's in the order it registered them. */ diff --git a/host/src/main/java/com/volmit/host/ModuleRegistry.java b/host/src/main/java/com/volmit/host/ModuleRegistry.java index a14d35198..771bb59cc 100644 --- a/host/src/main/java/com/volmit/host/ModuleRegistry.java +++ b/host/src/main/java/com/volmit/host/ModuleRegistry.java @@ -11,7 +11,7 @@ import com.volmit.object.ObjectGeneratorFactory; import com.volmit.resource.ResourceId; import com.volmit.resource.ResourceKind; import com.volmit.service.Providers; -import com.volmit.tooling.SceneView; +import com.volmit.tooling.StudioView; import com.volmit.tooling.ToolingRegistry; import com.volmit.validation.Validator; import java.util.ArrayList; @@ -31,7 +31,7 @@ import java.util.Set; * settings ids must be the registering module's id or start with it and '/' ({@link com.volmit.ModuleInfo#ownsId}). * Resource kinds have unique ids, types and folders, and no kind may read a folder the host reserves for its own * files. Lifecycle hooks and service providers are registered by the module that owns them; a service whose providers - * are {@link Providers#ONE} has at most one. Studio scene views have unique ids the registering module owns. A failed + * are {@link Providers#ONE} has at most one. Studio views have ids unique across their kinds that the registering module owns. A failed * registration leaves the registry unchanged. A failed module callback invalidates the host's whole build. This does * not decode JSON, discover modules or instantiate settings. */ @@ -47,7 +47,7 @@ public final class ModuleRegistry implements FeatureRegistry, ToolingRegistry { private final List validators = new ArrayList<>(); private final List lifecycles = new ArrayList<>(); private final List services = new ArrayList<>(); - private final Map sceneViews = new HashMap<>(); + private final Map studioViews = new HashMap<>(); private final Set reservedFolders; private final List registered = new ArrayList<>(); private PlacementTypeRegistration placementType; @@ -199,18 +199,18 @@ public final class ModuleRegistry implements FeatureRegistry, ToolingRegistry { } @Override - public void registerSceneView(SceneView view) { + public void registerView(StudioView view) { checkOpen(); Objects.requireNonNull(view, "view"); ResourceId id = Objects.requireNonNull(view.id(), "view id"); - requireOwnedId("Scene view", id); - var added = new SceneViewRegistration(view, registering); - var existing = sceneViews.get(id); + requireOwnedId("Studio view", id); + var added = new StudioViewRegistration(view, registering); + var existing = studioViews.get(id); if (existing != null) { - throw new IllegalArgumentException("Scene view " + id + " is registered by module " + existing.module() + throw new IllegalArgumentException("Studio view " + id + " is registered by module " + existing.module() + " already, and module " + added.module() + " registers it too; give one of them another id"); } - sceneViews.put(id, added); + studioViews.put(id, added); } /** Catalogs a settings type named by a stage's when(...), whose owner {@link StageGraphBuilder} @@ -234,7 +234,7 @@ public final class ModuleRegistry implements FeatureRegistry, ToolingRegistry { public FeatureCatalog freeze() { if (catalog == null) { catalog = new FeatureCatalog(settings, objects, mantle, resourceKinds.values(), validators, lifecycles, services, - placementType, sceneViews.values()); + placementType, studioViews.values()); } return catalog; } diff --git a/host/src/main/java/com/volmit/host/SceneViewRegistration.java b/host/src/main/java/com/volmit/host/SceneViewRegistration.java deleted file mode 100644 index f83334dfd..000000000 --- a/host/src/main/java/com/volmit/host/SceneViewRegistration.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.volmit.host; - -import com.volmit.tooling.SceneView; - -import java.util.Objects; - -/** A studio scene view and the module that registered it. */ -public record SceneViewRegistration(SceneView view, DiscoveredModule module) { - public SceneViewRegistration { - Objects.requireNonNull(view, "view"); - Objects.requireNonNull(module, "module"); - } - - @Override - public String toString() { - return "scene view " + view.id() + " of module " + module; - } -} diff --git a/host/src/main/java/com/volmit/host/StudioViewRegistration.java b/host/src/main/java/com/volmit/host/StudioViewRegistration.java new file mode 100644 index 000000000..1e5e5abcc --- /dev/null +++ b/host/src/main/java/com/volmit/host/StudioViewRegistration.java @@ -0,0 +1,27 @@ +package com.volmit.host; + +import com.volmit.tooling.PatternView; +import com.volmit.tooling.SceneView; +import com.volmit.tooling.StudioView; + +import java.util.Objects; + +/** A studio view and the module that registered it. */ +public record StudioViewRegistration(StudioView view, DiscoveredModule module) { + public StudioViewRegistration { + Objects.requireNonNull(view, "view"); + Objects.requireNonNull(module, "module"); + } + + @Override + public String toString() { + return kindOf(view) + " " + view.id() + " of module " + module; + } + + static String kindOf(StudioView view) { + return switch (view) { + case SceneView ignored -> "scene view"; + case PatternView ignored -> "pattern view"; + }; + } +} diff --git a/host/src/test/java/com/volmit/host/ModuleRegistryTest.java b/host/src/test/java/com/volmit/host/ModuleRegistryTest.java index 498078df7..0d00e4fc5 100644 --- a/host/src/test/java/com/volmit/host/ModuleRegistryTest.java +++ b/host/src/test/java/com/volmit/host/ModuleRegistryTest.java @@ -119,25 +119,41 @@ class ModuleRegistryTest { } @Test - void sceneViewsHaveUniqueIdsTheirModuleOwnsAndAreCataloguedById() { + void studioViewsHaveIdsUniqueAcrossKindsTheirModuleOwnsAndAreCataloguedById() { var registry = new ModuleRegistry(); ModuleSetups.run(DiscoveredModule.builtIn(new IrisModule() { public ModuleInfo info() { return ModuleInfo.of("iris:objects", "Objects", "1"); } public void setup(ModuleSetup setup) { - setup.tooling().registerSceneView(sceneView("iris:objects/procedural")); - setup.tooling().registerSceneView(sceneView("iris:objects")); + setup.tooling().registerView(sceneView("iris:objects/procedural")); + setup.tooling().registerView(sceneView("iris:objects")); + setup.tooling().registerView(patternView("iris:objects/footprints")); var duplicate = assertThrows(IllegalArgumentException.class, - () -> setup.tooling().registerSceneView(sceneView("iris:objects"))); - assertTrue(duplicate.getMessage().contains("Scene view iris:objects is registered by module"), duplicate.getMessage()); + () -> setup.tooling().registerView(patternView("iris:objects"))); + assertTrue(duplicate.getMessage().contains("Studio view iris:objects is registered by module"), duplicate.getMessage()); var foreign = assertThrows(IllegalArgumentException.class, - () -> setup.tooling().registerSceneView(sceneView("iris:terrain/palettes"))); + () -> setup.tooling().registerView(sceneView("iris:terrain/palettes"))); assertTrue(foreign.getMessage().contains("must be iris:objects or start with iris:objects/"), foreign.getMessage()); } }), new StageGraphBuilder(), registry); - assertThrows(IllegalStateException.class, () -> registry.registerSceneView(sceneView("iris:objects/loose"))); - assertEquals(List.of("iris:objects", "iris:objects/procedural"), - registry.freeze().sceneViews().stream().map(view -> view.view().id().toString()).toList()); + assertThrows(IllegalStateException.class, () -> registry.registerView(sceneView("iris:objects/loose"))); + var views = registry.freeze().studioViews(); + assertEquals(List.of("iris:objects", "iris:objects/footprints", "iris:objects/procedural"), + views.stream().map(view -> view.view().id().toString()).toList()); + assertTrue(views.get(1).toString().startsWith("pattern view iris:objects/footprints of module"), views.get(1).toString()); + } + + private static com.volmit.tooling.PatternView patternView(String id) { + return new com.volmit.tooling.PatternView() { + public com.volmit.resource.ResourceId id() { return com.volmit.resource.ResourceId.parse(id); } + public String label() { return id; } + public String description() { return id; } + public List entries(com.volmit.world.PackWorld world) { return List.of(); } + public Optional pattern(com.volmit.world.PackWorld world, String key, int depth, + com.volmit.tooling.PatternArea area) { + return Optional.empty(); + } + }; } private static com.volmit.tooling.SceneView sceneView(String id) {