Batch 7 stage 3: objects and placement through the api

The api describes a pack's objects and lets a module place one through the
pack's placement graph: PlaceableObject, PackObjects (PackResources.objects()),
ScaleMethod, and Placements.place(PlacementRun) on PackWorld with
PlacementTerrain, StructureVolumes and PlacementOutcome, which moves here from
the host. Stages read PlacementContext.placementTerrain() and input(type);
mantle stages get MantleGenerationContext.placementTerrain().

BlockRegistry gains rotate (BlockTurn), merge, withProperty and matches;
BlockTrait gains STORAGE; ModuleLog gains debugging(); PackSettings gains
resources(); RunningWorld gains placementAt. README, DESIGN and CONTRACTS
describe them, and the extension ledger drops ObjectHost and ObjectPlacement.
This commit is contained in:
piexel committed 2026-10-28 01:25:41 +01:00
1 parent 398f3879ae
commit f1eb619344
34 files changed
+423 -27

No files matched your search

+5 -1
View File
@@ -10,10 +10,14 @@ package com.volmit;
*/
public interface ModuleLog {
/** Detail for people debugging the module. The host writes it only when its debug setting is on, so building the
* message costs time even when nobody reads it: guard a message that is expensive to build, or leave it out.
* message costs time even when nobody reads it: guard a message that is expensive to build with {@link #debugging()},
* or leave it out.
*/
void debug(String message);
/** Whether {@link #debug} lines are written now, so a module can skip building them, or skip work done only to trace. */
boolean debugging();
/** Something worth telling the server owner once in a while, such as what the module did at startup. */
void info(String message);
@@ -11,6 +11,11 @@ import java.util.Optional;
* <p>Two ways to get a state: {@link #resolve(BlockSpec)} reads a block the way packs write it (a custom block of the
* pack, a backup, never failing), {@link #find(String)} asks the platform for exactly one block. Both answer complete
* states, with every property the platform gives the block, like the states the host hands out; compare against those.
*
* <p>A content plugin's block ({@link BlockTrait#CUSTOM}) has the key of the block it is drawn as. The host knows it by
* the state instance it handed out, from this registry or from a block it read, so pass that instance on as it is, and
* change it through {@link #withProperty}, {@link #rotate} and {@link #merge}: a state built from the key is the plain
* block.
*/
public interface BlockRegistry {
/** The state {@code spec} names, resolved the way packs expect, so it never throws for a block the platform lacks:
@@ -55,4 +60,27 @@ public interface BlockRegistry {
* decoration, such as a flower on grass but not on stone. Only the kinds of block count, not their properties.
*/
boolean canPlaceOn(BlockState state, BlockState below);
/**
* {@code state} turned with an object: every direction the block faces (its facing, axis, connections, or the
* sixteen directions of a sign) is moved by {@code turn}, as the platform turns stairs, logs, walls and wires. A
* block without directions comes back unchanged; air when the block cannot face the way it is turned.
*/
BlockState rotate(BlockState state, BlockTurn turn);
/** {@code update} laid over {@code base}: the platform's merge of two states of one block, such as the sides of a
* fence, wall or pane; {@code update} when the platform does not merge them.
*/
BlockState merge(BlockState base, BlockState update);
/** {@code state} with the property {@code name} set to {@code value}, as the platform sets it. Unlike
* {@link BlockState#withProperty} it keeps a content plugin's block itself ({@link BlockTrait#CUSTOM}): such a block
* shares its key with the block it is drawn as, so only the state instance the registry handed out knows it.
*/
BlockState withProperty(BlockState state, String name, String value);
/** Whether {@code state} matches {@code pattern} as the platform compares them: the same block, and every property
* {@code pattern} sets equal.
*/
boolean matches(BlockState state, BlockState pattern);
}
@@ -2,8 +2,9 @@ package com.volmit.block;
/** What a block is, as the platform answers it ({@link BlockRegistry#hasTrait}); tags name further groups
* ({@link BlockRegistry#inTag}). {@code VINE} is a block that hangs from or clings to the faces around it, such as
* vines and glow lichen; {@code CUSTOM} a block of a content plugin or mod rather than of the platform itself.
* vines and glow lichen; {@code STORAGE} a chest-like container that keeps items, such as a chest or a barrel;
* {@code CUSTOM} a block of a content plugin or mod rather than of the platform itself.
*/
public enum BlockTrait {
AIR, SOLID, OCCLUDING, FLUID, WATER, GRAVITY, FOLIAGE, DECORATION, VINE, LOG, LEAVES, ORE, CUSTOM
AIR, SOLID, OCCLUDING, FLUID, WATER, GRAVITY, FOLIAGE, DECORATION, VINE, LOG, LEAVES, ORE, STORAGE, CUSTOM
}
@@ -0,0 +1,15 @@
package com.volmit.block;
/**
* How an object's rotation moves a direction its blocks face ({@link BlockRegistry#rotate}). A direction is a vector
* such as (0, 0, -1) for north, or (1, 0, -2) for one of a sign's sixteen; the turned vector need not be whole, and the
* platform takes the direction closest to it.
*/
@FunctionalInterface
public interface BlockTurn {
Direction turn(double x, double y, double z);
/** A direction vector. */
record Direction(double x, double y, double z) {
}
}
@@ -1,6 +1,7 @@
package com.volmit.config;
import com.volmit.IrisFeature;
import com.volmit.resource.PackResources;
import java.util.Optional;
/** Every settings declaration of the pack the host is loading, for what depends on the whole pack rather
@@ -16,6 +17,9 @@ public interface PackSettings {
*/
<F extends IrisFeature> SettingsStack<F> declared(Class<F> featureType);
/** The pack snapshot the settings were read from, such as for a reach that depends on the size of the pack's objects. */
PackResources resources();
/**
* What the host offers for the pack it is loading beyond the API, as an object of {@code type}: empty when it offers
* no such thing. The same door as {@link com.volmit.world.PackWorld#extension}, for what is worked out from the
@@ -1,6 +1,7 @@
package com.volmit.generation;
import com.volmit.mantle.Mantle;
import com.volmit.placement.PlacementTerrain;
import com.volmit.world.TerrainSampler;
/** Per-invocation inputs of the mantle pipeline for one chunk. Must not be retained after execution. */
@@ -14,4 +15,10 @@ public interface MantleGenerationContext extends ChunkContext {
/** The mantle of the chunk widened by the pipeline's reach, full height (heights 0 up to the dimension height). Writes outside it throw. */
Mantle mantle();
/**
* The mantle of the chunk and its reach as a world to place objects in ({@link com.volmit.placement.Placements}):
* its blocks are the mantle's planned blocks, and placements write theirs into the mantle.
*/
PlacementTerrain placementTerrain();
}
@@ -0,0 +1,35 @@
package com.volmit.object;
import com.volmit.geometry.BlockBox;
import com.volmit.resource.ResourceId;
import java.util.List;
import java.util.Optional;
/**
* The pack's objects, ready to place, and the objects made from them: scaled variants and objects a module builds block
* by block. The host loads, scales and keeps them for the pack snapshot, so asking again is cheap. Reached through
* {@link com.volmit.resource.PackResources#objects()}; safe for concurrent calls.
*/
public interface PackObjects {
/** The pack object {@code id} names, as {@link com.volmit.resource.PackResources#idOf} gives it for the key; empty when the pack has none. */
Optional<PlaceableObject> object(ResourceId id);
/** The pack object's size, read without loading its blocks: from the origin to its width, height and depth; empty when the pack has none. */
Optional<BlockBox> sizeOf(ResourceId id);
/**
* {@code object} resized by {@code factor}, filling between its blocks with {@code method}. Built once per object,
* factor and method, and kept within a memory budget.
*/
PlaceableObject scaled(PlaceableObject object, double factor, ScaleMethod method);
/**
* {@code object} with air in the rooms and pockets a placement should clear, found by casting rays through it. Done
* once per object; the object itself keeps the air, so later placements of it see it too.
*/
PlaceableObject smartBored(PlaceableObject object);
/** An object of {@code blocks}, at their offsets from its center, declared {@code width} by {@code height} by {@code depth} with its center in the middle; placeable like the pack's. */
PlaceableObject build(String name, int width, int height, int depth, List<ObjectBlock> blocks);
}
@@ -0,0 +1,28 @@
package com.volmit.object;
import com.volmit.geometry.BlockPosition;
/**
* An object ready to be placed: one of the pack's, a scaled variant of one, or one a module built
* ({@link PackObjects}). Its size is the size it was declared with, which boring and terrain bends cover even where it
* has no block. Immutable; safe to share between threads.
*/
public interface PlaceableObject {
/**
* The object's name: the key the pack's files name it by, such as {@code trees/oak}, also for its scaled variants, or
* the name the module that built it gave it.
*/
String name();
int width();
int height();
int depth();
/** The block the object's offsets count from, relative to its corner. */
BlockPosition center();
/** The object's blocks at their offsets from {@link #center()}, so the geometry's anchor is the origin. */
GeneratedObject geometry();
}
@@ -0,0 +1,13 @@
package com.volmit.object;
/** How a scaled object fills the blocks between its source blocks when it grows ({@link PackObjects#scaled}). */
public enum ScaleMethod {
/** Each source block becomes a box of blocks. */
NONE,
/** Linear interpolation in three dimensions. */
TRILINEAR,
/** Cubic spline interpolation in three dimensions; smoother, much slower. */
TRICUBIC,
/** Hermite spline interpolation in three dimensions; smoother, much slower. */
TRIHERMITE
}
@@ -6,6 +6,8 @@ import com.volmit.object.ObjectTransformer;
import com.volmit.world.TerrainSampler;
import com.volmit.world.BlockView;
import java.util.Optional;
/** Host-supplied, read-only inputs of one placement attempt. Working state is carried by stage
* outputs, never stored on a module or stage body. Context lifetime ends with the placement.
*/
@@ -15,4 +17,14 @@ public interface PlacementContext extends InvocationContext {
ObjectTransformer objects();
/** Prepared blocks before this placement. Use stage access for reads of buffered changes. */
BlockView blocks();
/**
* The world the placement is made in, for the questions about a column the stages ask. Its blocks are the ones before
* this placement, like {@link #blocks()}; stages write through their access, never into it.
*/
PlacementTerrain placementTerrain();
/**
* What the module that asked for this placement handed its stages ({@link PlacementRun#input}), when it is a
* {@code type}; empty otherwise, such as for a placement the host itself started.
*/
<T> Optional<T> input(Class<T> type);
}
@@ -0,0 +1,30 @@
package com.volmit.placement;
import java.util.Objects;
import java.util.Optional;
/** Result of one placement attempt ({@link Placements#place}). A rejected attempt committed nothing. */
public record PlacementOutcome(Status status, Optional<String> rejection) {
public enum Status { PLACED, REJECTED }
public PlacementOutcome {
Objects.requireNonNull(status, "status");
Objects.requireNonNull(rejection, "rejection");
if ((status == Status.REJECTED) != rejection.isPresent()) {
throw new IllegalArgumentException("Only rejected placements carry a rejection reason");
}
}
public static PlacementOutcome placed() {
return new PlacementOutcome(Status.PLACED, Optional.empty());
}
/** Rejected with the reason the stage gave, such as {@code slope}. */
public static PlacementOutcome rejected(String reason) {
return new PlacementOutcome(Status.REJECTED, Optional.of(reason));
}
public boolean isPlaced() {
return status == Status.PLACED;
}
}
@@ -0,0 +1,26 @@
package com.volmit.placement;
import com.volmit.geometry.BlockBox;
import java.util.Objects;
/**
* One object a module asks the host to place through the pack's placement stages ({@link Placements#place}).
*
* @param request the object and where it goes
* @param area the blocks the object may reach around its anchor in any way it is placed; the host widens it by the
* reach the pack's placement stages declare, and the result bounds the placement's {@link PlacementAccess}.
* Reaching outside fails the placement
* @param terrain the world the placement reads, and where the host writes the blocks when the placement commits
* @param input what the placing module hands its own stages, such as its placement settings; stages read it with
* {@link PlacementContext#input}, and stages of other modules do not know its type
* @param seed the seed the stages' random streams derive from; the same seed, stages and inputs give the same placement
*/
public record PlacementRun(PlacementRequest request, BlockBox area, PlacementTerrain terrain, Object input, long seed) {
public PlacementRun {
Objects.requireNonNull(request, "request");
Objects.requireNonNull(area, "area");
Objects.requireNonNull(terrain, "terrain");
Objects.requireNonNull(input, "input");
}
}
@@ -0,0 +1,66 @@
package com.volmit.placement;
import com.volmit.block.BlockState;
import com.volmit.object.BlockEntityData;
import java.util.Optional;
/**
* The world a placement is made in, as its stages and the module that asked for it see it: blocks, the questions about a
* column the stages ask, and what the host keeps beside each block. X and Z are world coordinates; Y is the height above
* the dimension's bottom.
*
* <p>A module that places objects hands one to {@link Placements#place}: the terrain of its mantle invocation
* ({@link com.volmit.generation.MantleGenerationContext#placementTerrain()}), or its own, such as one that collects a
* growing tree's blocks. It may wrap another to change what it answers or to hold writes back. Stages read it through
* {@link PlacementContext#placementTerrain()} and write through {@link PlacementAccess}; the host writes a placement's
* blocks into it when the placement commits. Not thread-safe; it lives as long as the invocation it came from.
*/
public interface PlacementTerrain {
/** The height of the highest block of the column; {@code ignoreFluid} leaves out the fluid above the ground. */
int highest(int x, int z, boolean ignoreFluid);
BlockState get(int x, int y, int z);
/** Writes the block without a block entity. */
void set(int x, int y, int z, BlockState state);
/** Writes the block with its block entity, such as a chest's contents. */
void set(int x, int y, int z, BlockState state, BlockEntityData blockEntity);
/** Whether the cave carver emptied the block. */
boolean isCarved(int x, int y, int z);
/** Whether the block is real, solid ground, which the terrain's heights alone cannot tell before the chunk is built. */
boolean isSurfaceSolid(int x, int y, int z);
boolean isUnderwater(int x, int z);
/** The height up to which the dimension is filled with fluid. */
int fluidHeight();
/** Whether leaves written here never decay. */
boolean preventsDecay();
/**
* Records that the block belongs to {@code placement}, the name the placing module gives one placed object, so a
* later placement can tell what is already there ({@link #placementAt}). The host keeps the name as it is.
*/
void recordPlacement(int x, int y, int z, String placement);
/** The placement recorded at the block ({@link #recordPlacement}); empty when none is. */
Optional<String> placementAt(int x, int y, int z);
/** The bounds of the platform's structure pieces planned around the columns, for placements that keep out of them. */
StructureVolumes structureVolumes(int minX, int minZ, int maxX, int maxZ);
/**
* Keeps {@code marker} beside its block in the world's generation storage: a stage's marker when the placement
* commits ({@link PlacementAccess#marker}), or one the placing module adds to a placed block. Its type is something the
* module that writes it and the module or host that reads it agree on, such as a spawn marker. The host keeps the
* types it knows in its own form.
*
* @throws IllegalArgumentException when the host keeps no marker of this type
*/
void mark(PlacedMarker marker);
}
@@ -0,0 +1,15 @@
package com.volmit.placement;
/**
* Runs the pack's placement stages for an object a module places itself, such as the objects of a biome or a growing
* tree. The host offers it for every world ({@link com.volmit.world.PackWorld#placements()}); the stages are every
* module's placement stages of the pack, in the order the stage graph gives them.
*/
public interface Placements {
/**
* Places {@code run}'s object: the stages run with buffered writes, and the host writes the blocks, then the markers,
* into the run's terrain only when no stage rejects the placement and none throws. A stage that throws fails the call and nothing
* is written. Call it from the invocation the terrain belongs to.
*/
PlacementOutcome place(PlacementRun run);
}
@@ -0,0 +1,15 @@
package com.volmit.placement;
/**
* The bounds of the platform's structure pieces planned around a placement ({@link PlacementTerrain#structureVolumes}).
* X and Z are world coordinates; Y is the height above the dimension's bottom.
*/
public interface StructureVolumes {
boolean isEmpty();
/** Whether any piece meets the box, bounds inclusive. */
boolean intersects(int minX, int minY, int minZ, int maxX, int maxY, int maxZ);
/** Whether any piece contains the block or comes within {@code margin} blocks of it. */
boolean containsWithin(int x, int y, int z, int margin);
}
@@ -6,6 +6,7 @@ import com.volmit.noise.NoiseFactory;
import com.volmit.noise.PackExpression;
import com.volmit.noise.PackImageMap;
import com.volmit.object.GeneratedObject;
import com.volmit.object.PackObjects;
import com.volmit.world.PackBiome;
import com.volmit.world.PackRegion;
import java.util.List;
@@ -47,6 +48,9 @@ public interface PackResources {
/** Builds the noise of the {@link com.volmit.noise.NoiseStyle}s the pack's settings describe. */
NoiseFactory noise();
/** The pack's objects ready to place, their scaled variants, and the objects modules build. */
PackObjects objects();
/** The resource of {@code kind} the pack has under {@code id}; empty when it has none. */
<T> Optional<T> find(ResourceKind<T> kind, ResourceId id);
@@ -2,6 +2,7 @@ package com.volmit.world;
import com.volmit.ModuleLog;
import com.volmit.config.PackSettings;
import com.volmit.placement.Placements;
import com.volmit.resource.PackResources;
import com.volmit.resource.ResourceId;
import java.util.Optional;
@@ -65,6 +66,9 @@ public interface PackWorld {
*/
<T> T state(Class<T> type, Function<? super PackWorld, ? extends T> create);
/** Runs the pack's placement stages for the objects a module places in this world. */
Placements placements();
/** The log of the module this world was handed to: every service call, lifecycle hook and stage body gets a view of
* the world whose log is that module's own, so a line is labelled with the module that wrote it.
*/
@@ -35,6 +35,12 @@ public interface RunningWorld {
*/
List<BlockPosition> caveFloors(int chunkX, int chunkZ);
/** The placement a module recorded at the block when it generated it
* ({@link com.volmit.placement.PlacementTerrain#recordPlacement}); empty when none was, or when the block's chunk
* keeps no generation data any more. Y is the height above the dimension's bottom.
*/
Optional<String> placementAt(int worldX, int y, int worldZ);
/** The time and weather of the world; empty while the platform has not loaded the world. */
Optional<Sky> sky();