mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 00:04:42 +00:00
Merge wave1/a2
This commit is contained in:
commit
99a914633d
31 files changed
+1018
-42
No files matched your search
@@ -8,6 +8,7 @@ import com.volmit.resource.ResourceKind;
|
||||
import com.volmit.service.Service;
|
||||
import com.volmit.stage.StageGraph;
|
||||
import com.volmit.validation.Validator;
|
||||
import com.volmit.vanilla.VanillaStructureHook;
|
||||
|
||||
/** Host implementation collects non-stage registrations before loading pack configuration.
|
||||
* Feature IDs must be unique. JSON fields must be unique per attachment target, including the
|
||||
@@ -51,4 +52,9 @@ public interface FeatureRegistry {
|
||||
* are {@link com.volmit.service.Providers#ONE} fails the pack load when a second module provides it, naming both.
|
||||
*/
|
||||
<S> void registerService(Class<S> service, S provider);
|
||||
|
||||
/** Lets this module decide over the platform's own structures in the pack's worlds: block them, leave them to the
|
||||
* platform, or place them itself.
|
||||
*/
|
||||
void registerVanillaStructureHook(VanillaStructureHook hook);
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.volmit.block;
|
||||
|
||||
import com.volmit.object.BlockEntityData;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.util.Optional;
|
||||
|
||||
/** Host adapter for resolved states and cached behavior. No native platform objects escape.
|
||||
* Unknown materials, unsupported properties, and malformed states throw IllegalArgumentException.
|
||||
@@ -9,6 +11,19 @@ import com.volmit.resource.ResourceId;
|
||||
public interface BlockRegistry {
|
||||
BlockState resolve(String key);
|
||||
default BlockState resolve(BlockState state) { return resolve(state.key()); }
|
||||
|
||||
/** The state {@code spec} names, resolved the way packs expect, so it never throws for a block the platform lacks:
|
||||
* a block of the pack's {@code blocks} folder with the spec's properties over its own, else the platform's block
|
||||
* (a key without a namespace is {@code minecraft:}) with the spec's properties, else the backup resolved the same
|
||||
* way, else air.
|
||||
*/
|
||||
BlockState resolve(BlockSpec spec);
|
||||
|
||||
/** The block entity {@code spec}'s {@code tileData} describes, in the host's format; empty when the spec has none
|
||||
* or its resolved block has no block entity.
|
||||
*/
|
||||
Optional<BlockEntityData> blockEntity(BlockSpec spec);
|
||||
|
||||
boolean hasTrait(BlockState state, BlockTrait trait);
|
||||
boolean inTag(BlockState state, ResourceId tag);
|
||||
/** Whether state can attach to the specified face of support, ignoring other neighbors. */
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
package com.volmit.block;
|
||||
|
||||
import com.volmit.config.Desc;
|
||||
import com.volmit.config.MaxNumber;
|
||||
import com.volmit.config.MinNumber;
|
||||
import com.volmit.config.Ref;
|
||||
import com.volmit.config.Reference;
|
||||
import com.volmit.config.Required;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
/** A block as packs write it in settings, such as {@code {"block": "iron_ore"}} or
|
||||
* {@code {"block": "minecraft:oak_log", "data": {"axis": "y"}, "backup": {"block": "stone"}}}. Settings hold it
|
||||
* where a pack picks a block it may not be able to name exactly: a key without a namespace, a custom block of
|
||||
* the pack's {@code blocks} folder, or a block that only some versions or mods have.
|
||||
*
|
||||
* <p>The host resolves it with {@link BlockRegistry#resolve(BlockSpec)} and its block entity with
|
||||
* {@link BlockRegistry#blockEntity(BlockSpec)}. A spec is a value: the host creates it with the no-argument
|
||||
* constructor and reads the JSON into it, and nothing changes it afterwards. Property values are text however the
|
||||
* pack writes them, so {@code "waterlogged": true} reads as {@code "true"}.
|
||||
*/
|
||||
@Desc("A block, with optional properties, block entity data and a backup for platforms without it")
|
||||
public final class BlockSpec {
|
||||
private static final String AIR = "air";
|
||||
|
||||
@Required
|
||||
@Ref(Reference.BLOCK)
|
||||
@Desc("The block to use. A key without a namespace is a Minecraft block or a block of the pack's blocks folder.")
|
||||
private String block = AIR;
|
||||
|
||||
@Desc("Optional properties for this block, such as 'waterlogged': true")
|
||||
private Map<String, String> data = Map.of();
|
||||
|
||||
@MinNumber(1)
|
||||
@MaxNumber(1000)
|
||||
@Desc("The weight of this block in a list it is picked from. A weight of two is as if the block were in the list twice.")
|
||||
private int weight = 1;
|
||||
|
||||
@Desc("If the block cannot be created on this platform, this block is used instead.")
|
||||
private BlockSpec backup;
|
||||
|
||||
@Desc("Optional block entity data for this block, such as a spawner's SpawnData")
|
||||
private Map<String, Object> tileData = Map.of();
|
||||
|
||||
private BlockSpec() {
|
||||
}
|
||||
|
||||
private BlockSpec(String block, Map<String, String> data, int weight, BlockSpec backup, Map<String, Object> tileData) {
|
||||
this.block = Objects.requireNonNull(block, "block");
|
||||
this.data = Map.copyOf(data);
|
||||
this.weight = weight;
|
||||
this.backup = backup;
|
||||
this.tileData = Collections.unmodifiableMap(new LinkedHashMap<>(tileData));
|
||||
}
|
||||
|
||||
/** The block {@code block}, such as {@code minecraft:stone} or {@code stone}, without properties, weight 1. */
|
||||
public static BlockSpec of(String block) {
|
||||
return new BlockSpec(block, Map.of(), 1, null, Map.of());
|
||||
}
|
||||
|
||||
/** This spec with the property {@code name} set to {@code value}. */
|
||||
public BlockSpec withProperty(String name, String value) {
|
||||
var changed = new LinkedHashMap<>(data());
|
||||
changed.put(name, value);
|
||||
return new BlockSpec(block, changed, weight, backup, tileData());
|
||||
}
|
||||
|
||||
public BlockSpec withWeight(int weight) {
|
||||
return new BlockSpec(block, data(), weight, backup, tileData());
|
||||
}
|
||||
|
||||
public BlockSpec withBackup(BlockSpec backup) {
|
||||
return new BlockSpec(block, data(), weight, backup, tileData());
|
||||
}
|
||||
|
||||
public BlockSpec withTileData(Map<String, Object> tileData) {
|
||||
return new BlockSpec(block, data(), weight, backup, tileData);
|
||||
}
|
||||
|
||||
/** The block key as the pack wrote it. */
|
||||
public String block() {
|
||||
return block == null ? AIR : block;
|
||||
}
|
||||
|
||||
/** The properties as the pack wrote them, as text. */
|
||||
public Map<String, String> data() {
|
||||
return data == null ? Map.of() : Collections.unmodifiableMap(data);
|
||||
}
|
||||
|
||||
public int weight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public Optional<BlockSpec> backup() {
|
||||
return Optional.ofNullable(backup);
|
||||
}
|
||||
|
||||
/** The block entity data as the pack wrote it: JSON objects as maps, arrays as lists, numbers as {@code Double}. */
|
||||
public Map<String, Object> tileData() {
|
||||
return tileData == null ? Map.of() : Collections.unmodifiableMap(tileData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
return other instanceof BlockSpec spec && block().equals(spec.block()) && data().equals(spec.data())
|
||||
&& weight == spec.weight && Objects.equals(backup, spec.backup) && tileData().equals(spec.tileData());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(block(), data(), weight, backup, tileData());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BlockSpec[" + block() + (data().isEmpty() ? "" : " " + data()) + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.volmit.config;
|
||||
|
||||
import com.volmit.random.WorldRandom;
|
||||
import java.util.Objects;
|
||||
|
||||
/** A span of numbers from {@code min} to {@code max}, both included, as packs write it: {@code {"min": 0, "max": 15}},
|
||||
* or a snippet under {@code snippet/range/}. An omitted bound keeps its default, so give a settings field its own
|
||||
* default with {@link #of}. A value the host creates with the no-argument constructor and reads the JSON into.
|
||||
*/
|
||||
@Snippet("range")
|
||||
@Desc("Represents a range")
|
||||
public final class Range {
|
||||
@Desc("The minimum value")
|
||||
private double min = 16;
|
||||
|
||||
@Desc("The maximum value")
|
||||
private double max = 32;
|
||||
|
||||
private Range() {
|
||||
}
|
||||
|
||||
private Range(double min, double max) {
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
public static Range of(double min, double max) {
|
||||
return new Range(min, max);
|
||||
}
|
||||
|
||||
public double min() {
|
||||
return min;
|
||||
}
|
||||
|
||||
public double max() {
|
||||
return max;
|
||||
}
|
||||
|
||||
/** Whether {@code value} lies between the bounds, both included. */
|
||||
public boolean contains(double value) {
|
||||
return value >= min && value <= max;
|
||||
}
|
||||
|
||||
/** A number between the bounds drawn from {@code random}: {@code min + nextDouble() * (max - min)}, one draw,
|
||||
* or {@code min} without a draw when the bounds are equal.
|
||||
*/
|
||||
public double pick(WorldRandom random) {
|
||||
if (min == max) {
|
||||
return min;
|
||||
}
|
||||
return min + random.nextDouble() * (max - min);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
return other instanceof Range that && Double.compare(min, that.min) == 0 && Double.compare(max, that.max) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(min, max);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Range[" + min + ".." + max + "]";
|
||||
}
|
||||
}
|
||||
@@ -25,5 +25,11 @@ public enum Reference {
|
||||
/** A region of the pack. */
|
||||
PACK_REGION,
|
||||
/** An object of the pack. */
|
||||
PACK_OBJECT
|
||||
PACK_OBJECT,
|
||||
/** One of the host's noise styles, such as {@code SIMPLEX}; see {@link com.volmit.noise.NoiseStyle}. */
|
||||
NOISE_STYLE,
|
||||
/** An expression of the pack. */
|
||||
PACK_EXPRESSION,
|
||||
/** An image map of the pack. */
|
||||
PACK_IMAGE_MAP
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.volmit.host;
|
||||
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.host.schema.ValueKind;
|
||||
import com.volmit.host.schema.ValueKind.AnyKind;
|
||||
import com.volmit.host.schema.ValueKind.BooleanKind;
|
||||
import com.volmit.host.schema.ValueKind.DecimalKind;
|
||||
import com.volmit.host.schema.ValueKind.EnumKind;
|
||||
@@ -47,7 +48,7 @@ final class DefaultValues {
|
||||
case ListKind list -> list(elements(value), list);
|
||||
case MapKind map -> map((Map<?, ?>) value, map.value());
|
||||
case ObjectKind object -> object(value, types.get(object.type()));
|
||||
case HostKind _ -> Optional.empty();
|
||||
case HostKind _, AnyKind _ -> Optional.empty();
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -11,9 +11,10 @@ import java.util.NoSuchElementException;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
/** Frozen registrations. Lists use lexical feature/layer/kind ID order, and validators, lifecycle hooks and service
|
||||
* providers are in module id order, each module's in the order it registered them, so nothing depends on discovery.
|
||||
* Factory, codec, validator, hook and provider implementations must themselves obey their concurrency contracts.
|
||||
/** Frozen registrations. Lists use lexical feature/layer/kind ID order, and validators, lifecycle hooks, service
|
||||
* providers and vanilla structure hooks are in module id order, each module's in the order it registered them, so
|
||||
* nothing depends on discovery. Factory, codec, validator, hook and provider implementations must themselves obey their
|
||||
* concurrency contracts.
|
||||
*/
|
||||
public final class FeatureCatalog {
|
||||
private final Map<Class<?>, FeatureDescriptor<?>> settings;
|
||||
@@ -23,6 +24,7 @@ public final class FeatureCatalog {
|
||||
private final List<ValidatorRegistration> validators;
|
||||
private final List<LifecycleRegistration> lifecycles;
|
||||
private final List<ServiceRegistration> services;
|
||||
private final List<VanillaStructureHookRegistration> vanillaStructureHooks;
|
||||
|
||||
FeatureCatalog(
|
||||
Map<Class<?>, FeatureDescriptor<?>> settings,
|
||||
@@ -31,7 +33,8 @@ public final class FeatureCatalog {
|
||||
Collection<ResourceKindRegistration> resourceKinds,
|
||||
List<ValidatorRegistration> validators,
|
||||
List<LifecycleRegistration> lifecycles,
|
||||
List<ServiceRegistration> services) {
|
||||
List<ServiceRegistration> services,
|
||||
List<VanillaStructureHookRegistration> vanillaStructureHooks) {
|
||||
this.settings = Map.copyOf(settings);
|
||||
this.objects = Map.copyOf(objects);
|
||||
this.mantle = Map.copyOf(mantle);
|
||||
@@ -39,6 +42,7 @@ public final class FeatureCatalog {
|
||||
this.validators = inModuleOrder(validators, ValidatorRegistration::module);
|
||||
this.lifecycles = inModuleOrder(lifecycles, LifecycleRegistration::module);
|
||||
this.services = inModuleOrder(services, ServiceRegistration::module);
|
||||
this.vanillaStructureHooks = inModuleOrder(vanillaStructureHooks, VanillaStructureHookRegistration::module);
|
||||
}
|
||||
|
||||
/** Settings types that activate stages; the host decodes them from pack JSON. */
|
||||
@@ -64,6 +68,11 @@ public final class FeatureCatalog {
|
||||
return validators;
|
||||
}
|
||||
|
||||
/** The vanilla structure hooks modules registered, by module id, each module's in the order it registered them. */
|
||||
public List<VanillaStructureHookRegistration> vanillaStructureHooks() {
|
||||
return vanillaStructureHooks;
|
||||
}
|
||||
|
||||
/** The lifecycle hooks modules registered, by module id, each module's in the order it registered them. */
|
||||
public List<LifecycleRegistration> lifecycles() {
|
||||
return lifecycles;
|
||||
|
||||
@@ -13,6 +13,7 @@ import com.volmit.resource.ResourceId;
|
||||
import com.volmit.resource.ResourceKind;
|
||||
import com.volmit.service.Providers;
|
||||
import com.volmit.validation.Validator;
|
||||
import com.volmit.vanilla.VanillaStructureHook;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
@@ -45,6 +46,7 @@ public final class ModuleRegistry implements FeatureRegistry {
|
||||
private final List<ValidatorRegistration> validators = new ArrayList<>();
|
||||
private final List<LifecycleRegistration> lifecycles = new ArrayList<>();
|
||||
private final List<ServiceRegistration> services = new ArrayList<>();
|
||||
private final List<VanillaStructureHookRegistration> vanillaStructureHooks = new ArrayList<>();
|
||||
private final Set<String> reservedFolders;
|
||||
private final List<DiscoveredModule> registered = new ArrayList<>();
|
||||
private FeatureCatalog catalog;
|
||||
@@ -160,6 +162,14 @@ public final class ModuleRegistry implements FeatureRegistry {
|
||||
services.add(added);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerVanillaStructureHook(VanillaStructureHook hook) {
|
||||
checkOpen();
|
||||
Objects.requireNonNull(hook, "hook");
|
||||
requireRegistering("Vanilla structure hook " + hook);
|
||||
vanillaStructureHooks.add(new VanillaStructureHookRegistration(hook, registering));
|
||||
}
|
||||
|
||||
/** Catalogs a settings type named by a stage's when(...), whose owner {@link StageGraphBuilder}
|
||||
* has checked. Several stages, and the owner's {@link #registerSettings}, may name one type. Call
|
||||
* it after every module has registered, so the owner is known.
|
||||
@@ -180,7 +190,8 @@ public final class ModuleRegistry implements FeatureRegistry {
|
||||
|
||||
public FeatureCatalog freeze() {
|
||||
if (catalog == null) {
|
||||
catalog = new FeatureCatalog(settings, objects, mantle, resourceKinds.values(), validators, lifecycles, services);
|
||||
catalog = new FeatureCatalog(settings, objects, mantle, resourceKinds.values(), validators, lifecycles, services,
|
||||
vanillaStructureHooks);
|
||||
}
|
||||
return catalog;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.volmit.config.OptionsMetadata;
|
||||
import com.volmit.config.Reference;
|
||||
import com.volmit.host.schema.TextFormat;
|
||||
import com.volmit.host.schema.ValueKind;
|
||||
import com.volmit.host.schema.ValueKind.AnyKind;
|
||||
import com.volmit.host.schema.ValueKind.BooleanKind;
|
||||
import com.volmit.host.schema.ValueKind.DecimalKind;
|
||||
import com.volmit.host.schema.ValueKind.EnumConstant;
|
||||
@@ -39,13 +40,14 @@ import java.util.function.Function;
|
||||
*/
|
||||
final class ValueKinds {
|
||||
static final String SUPPORTED_TYPES = "settings fields can be booleans, numbers, String, ResourceId, BlockState, enums, "
|
||||
+ "lists, sets and arrays of these, Map with String keys and classes with a no-argument constructor";
|
||||
+ "lists, sets and arrays of these, Map with String keys, Object for any JSON value and classes with a no-argument constructor";
|
||||
private static final Set<Class<?>> BOOLEANS = Set.of(boolean.class, Boolean.class);
|
||||
private static final Set<Class<?>> INTEGERS = Set.of(byte.class, short.class, int.class, long.class,
|
||||
Byte.class, Short.class, Integer.class, Long.class);
|
||||
private static final Set<Class<?>> DECIMALS = Set.of(float.class, double.class, Float.class, Double.class);
|
||||
private static final String DECLARE_COLLECTION = "declare it as List<...>, Set<...> or Map<String, ...>";
|
||||
private static final Set<Reference> PACK_REFERENCES = EnumSet.of(Reference.PACK_BIOME, Reference.PACK_REGION, Reference.PACK_OBJECT);
|
||||
private static final Set<Reference> PACK_REFERENCES = EnumSet.of(Reference.PACK_BIOME, Reference.PACK_REGION, Reference.PACK_OBJECT,
|
||||
Reference.PACK_EXPRESSION, Reference.PACK_IMAGE_MAP);
|
||||
|
||||
private final Function<Class<?>, ObjectKind> objects;
|
||||
private final HostTypes hostTypes;
|
||||
@@ -99,6 +101,7 @@ final class ValueKinds {
|
||||
if (INTEGERS.contains(type)) return new IntegerKind();
|
||||
if (DECIMALS.contains(type)) return new DecimalKind();
|
||||
if (type.isEnum()) return enumKind(type);
|
||||
if (type == Object.class) return new AnyKind();
|
||||
return objects.apply(type);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.vanilla.VanillaStructureHook;
|
||||
import java.util.Objects;
|
||||
|
||||
/** A vanilla structure hook and the module that registered it, which the host names when the hook fails. */
|
||||
public record VanillaStructureHookRegistration(VanillaStructureHook hook, DiscoveredModule module) {
|
||||
public VanillaStructureHookRegistration {
|
||||
Objects.requireNonNull(hook, "hook");
|
||||
Objects.requireNonNull(module, "module");
|
||||
}
|
||||
}
|
||||
@@ -83,6 +83,13 @@ public sealed interface ValueKind {
|
||||
}
|
||||
}
|
||||
|
||||
/** Any JSON value, which the module reads as it is: {@code Object} fields, and the values of {@code Map<String, Object>}
|
||||
* and {@code List<Object>}. For data whose shape the module does not own, such as a block entity's data. The host
|
||||
* decodes objects as maps with string keys, arrays as lists, numbers as {@code Double}, and strings and booleans
|
||||
* as themselves.
|
||||
*/
|
||||
record AnyKind() implements ValueKind {}
|
||||
|
||||
/** An enum constant and its {@code @Desc}, or an empty description. */
|
||||
record EnumConstant(String name, String description) {
|
||||
public EnumConstant {
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.volmit.noise;
|
||||
|
||||
/** Builds noise from styles a pack describes. Noise is the host's: modules describe it with {@link NoiseStyle} in
|
||||
* their settings and get samplers from here ({@link com.volmit.resource.PackResources#noise()}), never the host's
|
||||
* noise classes. Safe for concurrent calls.
|
||||
*/
|
||||
public interface NoiseFactory {
|
||||
/** The noise {@code style} describes, seeded with {@code seed}. The same style and seed give the same values
|
||||
* in every world of the pack, so derive the seed from the world seed ({@code context.worldSeed()}), not from a
|
||||
* stage's random stream, when the noise must line up across chunks. The host keeps what it builds for the pack
|
||||
* snapshot, so asking again for the same style and seed is cheap.
|
||||
*
|
||||
* <p>A style name the host does not know samples as {@link NoiseStyle#FLAT}, and an expression the pack does not
|
||||
* have falls back to the base style, as packs have always been read. An image map the pack does not have, or cannot
|
||||
* use as noise, throws {@link IllegalArgumentException}.
|
||||
*/
|
||||
NoiseSampler sampler(NoiseStyle style, long seed);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.volmit.noise;
|
||||
|
||||
/** Immutable, seeded noise the host built from a {@link NoiseStyle} ({@link NoiseFactory#sampler}). X and Z are
|
||||
* world coordinates; Y is the height above the dimension's bottom (0 = bottom). The host's own styles give values
|
||||
* from 0 to 1; expressions and image maps give what they compute. Two-dimensional and three-dimensional samples
|
||||
* are separate fields, so {@code sample(x, z)} is not {@code sample(x, 0, z)}. Safe for concurrent calls.
|
||||
*/
|
||||
public interface NoiseSampler {
|
||||
double sample(double worldX, double worldZ);
|
||||
|
||||
double sample(double worldX, double y, double worldZ);
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
package com.volmit.noise;
|
||||
|
||||
import com.volmit.config.Desc;
|
||||
import com.volmit.config.MaxNumber;
|
||||
import com.volmit.config.MinNumber;
|
||||
import com.volmit.config.Ref;
|
||||
import com.volmit.config.Reference;
|
||||
import com.volmit.config.Snippet;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
/** How noise is shaped, as packs write it: {@code {"style": "SIMPLEX", "zoom": 4}}, or a snippet under
|
||||
* {@code snippet/style/}. The style names one of the host's noise generators ({@link Reference#NOISE_STYLE}); an
|
||||
* expression or image map of the pack replaces it. A zoom stretches the noise, an exponent bends it, a fracture
|
||||
* style distorts its coordinates and a cellular frequency turns it into cells.
|
||||
*
|
||||
* <p>A style is a description, not noise: the host builds the noise ({@link NoiseFactory#sampler}). It is a value
|
||||
* the host creates with the no-argument constructor and reads the JSON into; nothing changes it afterwards.
|
||||
*/
|
||||
@Snippet("style")
|
||||
@Desc("A noise style: one of the host's noise generators, or an expression or image map of the pack, shaped by zoom, exponent, fracture and cells")
|
||||
public final class NoiseStyle {
|
||||
/** The style without noise, the default. */
|
||||
public static final String FLAT = "FLAT";
|
||||
|
||||
@Ref(Reference.NOISE_STYLE)
|
||||
@Desc("The base noise style. Used when neither expression nor imageMap is set; a failed expression also falls back to this style.")
|
||||
private String style = FLAT;
|
||||
|
||||
@Desc("If set above 0, this style will be cellularized")
|
||||
private double cellularFrequency = 0;
|
||||
|
||||
@Desc("Cell zooms")
|
||||
private double cellularZoom = 1;
|
||||
|
||||
@MinNumber(0.00001)
|
||||
@Desc("The zoom of this style")
|
||||
private double zoom = 1;
|
||||
|
||||
@Ref(Reference.PACK_EXPRESSION)
|
||||
@Desc("Instead of using the style property, use a custom expression to represent this style.")
|
||||
private String expression;
|
||||
|
||||
@Ref(Reference.PACK_IMAGE_MAP)
|
||||
@Desc("Use an Image map instead of a generated value")
|
||||
private String imageMap;
|
||||
|
||||
@MinNumber(0.00001)
|
||||
@Desc("The Output multiplier. Only used if parent is fracture.")
|
||||
private double multiplier = 1;
|
||||
|
||||
@Desc("Apply a generator to the coordinate field fed into this parent generator. I.e. Distort your generator with another generator.")
|
||||
private NoiseStyle fracture;
|
||||
|
||||
@MinNumber(0.01562)
|
||||
@MaxNumber(64)
|
||||
@Desc("The exponent")
|
||||
private double exponent = 1;
|
||||
|
||||
@MinNumber(0)
|
||||
@MaxNumber(8192)
|
||||
@Desc("If the cache size is set above 0, this generator will be cached")
|
||||
private int cacheSize = 0;
|
||||
|
||||
private NoiseStyle() {
|
||||
}
|
||||
|
||||
private NoiseStyle(NoiseStyle copy) {
|
||||
style = copy.style;
|
||||
cellularFrequency = copy.cellularFrequency;
|
||||
cellularZoom = copy.cellularZoom;
|
||||
zoom = copy.zoom;
|
||||
expression = copy.expression;
|
||||
imageMap = copy.imageMap;
|
||||
multiplier = copy.multiplier;
|
||||
fracture = copy.fracture;
|
||||
exponent = copy.exponent;
|
||||
cacheSize = copy.cacheSize;
|
||||
}
|
||||
|
||||
/** The host's noise style {@code style}, such as {@code SIMPLEX}, with every other option at its default. */
|
||||
public static NoiseStyle of(String style) {
|
||||
var created = new NoiseStyle();
|
||||
created.style = Objects.requireNonNull(style, "style");
|
||||
return created;
|
||||
}
|
||||
|
||||
/** This style with zoom {@code zoom}: above 1 stretches the noise, below 1 squeezes it. */
|
||||
public NoiseStyle withZoom(double zoom) {
|
||||
var changed = new NoiseStyle(this);
|
||||
changed.zoom = zoom;
|
||||
return changed;
|
||||
}
|
||||
|
||||
/** The base style's name; {@link #FLAT} where the pack wrote none. */
|
||||
public String style() {
|
||||
return style == null ? FLAT : style;
|
||||
}
|
||||
|
||||
public double cellularFrequency() {
|
||||
return cellularFrequency;
|
||||
}
|
||||
|
||||
public double cellularZoom() {
|
||||
return cellularZoom;
|
||||
}
|
||||
|
||||
public double zoom() {
|
||||
return zoom;
|
||||
}
|
||||
|
||||
/** The key of the pack expression that replaces the base style. */
|
||||
public Optional<String> expression() {
|
||||
return Optional.ofNullable(expression);
|
||||
}
|
||||
|
||||
/** The key of the pack image map that replaces the base style when there is no expression. */
|
||||
public Optional<String> imageMap() {
|
||||
return Optional.ofNullable(imageMap);
|
||||
}
|
||||
|
||||
public double multiplier() {
|
||||
return multiplier;
|
||||
}
|
||||
|
||||
public Optional<NoiseStyle> fracture() {
|
||||
return Optional.ofNullable(fracture);
|
||||
}
|
||||
|
||||
public double exponent() {
|
||||
return exponent;
|
||||
}
|
||||
|
||||
public int cacheSize() {
|
||||
return cacheSize;
|
||||
}
|
||||
|
||||
/** Whether the style gives no noise: the {@link #FLAT} base style without an expression or image map. */
|
||||
public boolean isFlat() {
|
||||
return FLAT.equals(style()) && expression == null && imageMap == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
return other instanceof NoiseStyle that && style().equals(that.style())
|
||||
&& Double.compare(cellularFrequency, that.cellularFrequency) == 0
|
||||
&& Double.compare(cellularZoom, that.cellularZoom) == 0 && Double.compare(zoom, that.zoom) == 0
|
||||
&& Objects.equals(expression, that.expression) && Objects.equals(imageMap, that.imageMap)
|
||||
&& Double.compare(multiplier, that.multiplier) == 0 && Objects.equals(fracture, that.fracture)
|
||||
&& Double.compare(exponent, that.exponent) == 0 && cacheSize == that.cacheSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(style(), cellularFrequency, cellularZoom, zoom, expression, imageMap, multiplier, fracture,
|
||||
exponent, cacheSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "NoiseStyle[" + style() + expression().map(key -> ", expression " + key).orElse("")
|
||||
+ imageMap().map(key -> ", image map " + key).orElse("") + ", zoom " + zoom + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.volmit.noise;
|
||||
|
||||
import com.volmit.config.Desc;
|
||||
import com.volmit.config.Snippet;
|
||||
import java.util.Objects;
|
||||
|
||||
/** A number between {@code min} and {@code max} that a noise style picks per column, as packs write it:
|
||||
* {@code {"min": 4, "max": 12, "style": {"style": "SIMPLEX"}}}, or a snippet under {@code snippet/style-range/}.
|
||||
* A value the host creates with the no-argument constructor and reads the JSON into.
|
||||
*/
|
||||
@Snippet("style-range")
|
||||
@Desc("Represents a range styled with a custom generator")
|
||||
public final class StyledRange {
|
||||
private static final String DEFAULT_STYLE = "STATIC";
|
||||
|
||||
@Desc("The minimum value")
|
||||
private double min = 16;
|
||||
|
||||
@Desc("The maximum value")
|
||||
private double max = 32;
|
||||
|
||||
@Desc("The style to pick the range")
|
||||
private NoiseStyle style = NoiseStyle.of(DEFAULT_STYLE);
|
||||
|
||||
private StyledRange() {
|
||||
}
|
||||
|
||||
private StyledRange(double min, double max, NoiseStyle style) {
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
this.style = Objects.requireNonNull(style, "style");
|
||||
}
|
||||
|
||||
public static StyledRange of(double min, double max, NoiseStyle style) {
|
||||
return new StyledRange(min, max, style);
|
||||
}
|
||||
|
||||
public double min() {
|
||||
return min;
|
||||
}
|
||||
|
||||
public double max() {
|
||||
return max;
|
||||
}
|
||||
|
||||
/** The style; {@link NoiseStyle#FLAT} where the pack wrote {@code null}. */
|
||||
public NoiseStyle style() {
|
||||
return style == null ? NoiseStyle.of(NoiseStyle.FLAT) : style;
|
||||
}
|
||||
|
||||
/** Whether every column gets the same value: the bounds are equal or the style is flat. */
|
||||
public boolean isFlat() {
|
||||
return min == max || style().isFlat();
|
||||
}
|
||||
|
||||
/** The value at column {@code worldX}, {@code worldZ}: {@code min} when the bounds are equal, halfway between
|
||||
* them when the style is flat, otherwise {@code min + (max - min) * noise} of the style's noise seeded with
|
||||
* {@code seed}.
|
||||
*/
|
||||
public double at(NoiseFactory noise, long seed, double worldX, double worldZ) {
|
||||
if (min == max) {
|
||||
return min;
|
||||
}
|
||||
if (style().isFlat()) {
|
||||
return (min + max) / 2;
|
||||
}
|
||||
return min + (max - min) * noise.sampler(style(), seed).sample(worldX, worldZ);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
return other instanceof StyledRange that && Double.compare(min, that.min) == 0
|
||||
&& Double.compare(max, that.max) == 0 && style().equals(that.style());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(min, max, style());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "StyledRange[" + min + ".." + max + ", " + style() + "]";
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.volmit.resource;
|
||||
|
||||
import com.volmit.block.BlockRegistry;
|
||||
import com.volmit.noise.NoiseFactory;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Optional;
|
||||
@@ -19,6 +20,10 @@ public interface PackResources {
|
||||
ResourceId packId();
|
||||
UUID snapshotId();
|
||||
BlockRegistry blocks();
|
||||
|
||||
/** Builds the noise of the {@link com.volmit.noise.NoiseStyle}s the pack's settings describe. */
|
||||
NoiseFactory noise();
|
||||
|
||||
<T> Optional<T> find(ResourceKey<T> key);
|
||||
|
||||
/** The ids of every resource of the kind {@code type} selects, in id order. A type the host does not serve
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.volmit.vanilla;
|
||||
|
||||
import com.volmit.config.PackSettings;
|
||||
import com.volmit.resource.PackResources;
|
||||
|
||||
/** The world a {@link VanillaStructureHook} decides for. Valid only while the hook runs. */
|
||||
public interface VanillaStructureContext {
|
||||
/** The resources of the world's pack, module resource kinds included. */
|
||||
PackResources resources();
|
||||
|
||||
/** The module settings the world's dimension declares: its own, and every region and biome it can reach. */
|
||||
PackSettings settings();
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.volmit.vanilla;
|
||||
|
||||
import com.volmit.resource.ResourceId;
|
||||
|
||||
/** A module's say over the platform's own structures. Modules register it with
|
||||
* {@link com.volmit.FeatureRegistry#registerVanillaStructureHook}; the host asks every hook of the pack before the
|
||||
* platform starts one of its structures in a world, and when it locates one.
|
||||
*
|
||||
* <p>The host asks once per world, structure and pack snapshot and keeps the answer, so a hook decides from the
|
||||
* pack (its settings and resources) and never from where a start would be. Rules of several modules combine with
|
||||
* {@link VanillaStructureRule#and}: any module may block the platform's starts.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface VanillaStructureHook {
|
||||
/** What this module wants for the platform structure {@code structure}, such as {@code minecraft:village_plains},
|
||||
* in the world {@code context} describes. {@link VanillaStructureRule#GENERATE} when the module has no part in it.
|
||||
*/
|
||||
VanillaStructureRule rule(ResourceId structure, VanillaStructureContext context);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.volmit.vanilla;
|
||||
|
||||
/** What a module wants for one of the platform's own structures, the ones Minecraft, data packs and mods register
|
||||
* (such as {@code minecraft:village_plains}), in a world: whether the platform may start it where it would on its
|
||||
* own, and whether the module places it itself.
|
||||
*/
|
||||
public enum VanillaStructureRule {
|
||||
/** The platform generates the structure where it would; the module has no part in it. */
|
||||
GENERATE,
|
||||
/** The platform generates the structure where it would, and the module also places it, where it chooses. */
|
||||
ALSO_PLACE,
|
||||
/** The module places the structure instead of the platform: the platform's own starts of it are blocked. */
|
||||
REPLACE,
|
||||
/** The platform's own starts of the structure are blocked, and the module does not place it either. */
|
||||
BLOCK;
|
||||
|
||||
/** Whether the platform's own starts of the structure are blocked. */
|
||||
public boolean blocksPlatform() {
|
||||
return this == REPLACE || this == BLOCK;
|
||||
}
|
||||
|
||||
/** Whether the module places the structure itself, so the host locates it through the module. */
|
||||
public boolean placedByModule() {
|
||||
return this == ALSO_PLACE || this == REPLACE;
|
||||
}
|
||||
|
||||
/** What two modules' rules for one structure mean together: the platform's starts are blocked when either
|
||||
* blocks them, and the structure is placed by a module when either places it. The order does not matter.
|
||||
*/
|
||||
public VanillaStructureRule and(VanillaStructureRule other) {
|
||||
return of(blocksPlatform() || other.blocksPlatform(), placedByModule() || other.placedByModule());
|
||||
}
|
||||
|
||||
private static VanillaStructureRule of(boolean blocksPlatform, boolean placedByModule) {
|
||||
if (blocksPlatform) {
|
||||
return placedByModule ? REPLACE : BLOCK;
|
||||
}
|
||||
return placedByModule ? ALSO_PLACE : GENERATE;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,13 @@ package com.volmit.world;
|
||||
* world X and Z and a Y that is the height above the dimension's bottom (0 = bottom).
|
||||
*/
|
||||
public record BlockPosition(int x, int y, int z) implements Comparable<BlockPosition> {
|
||||
/** The origin, which settings start from before the host reads their JSON: packs write {@code {"x": 1, "y": 2, "z": 3}},
|
||||
* and an omitted coordinate is 0.
|
||||
*/
|
||||
private BlockPosition() {
|
||||
this(0, 0, 0);
|
||||
}
|
||||
|
||||
public BlockPosition add(BlockPosition offset) {
|
||||
return new BlockPosition(Math.addExact(x, offset.x), Math.addExact(y, offset.y), Math.addExact(z, offset.z));
|
||||
}
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.volmit.world;
|
||||
|
||||
/** Immutable, seeded noise resource. X and Z are world coordinates; Y is the
|
||||
* height above the dimension's bottom (0 = bottom).
|
||||
* Range and scale belong to the resource; no implicit normalization is performed.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface NoiseSampler {
|
||||
double sample(double worldX, double y, double worldZ);
|
||||
}
|
||||
Reference in new issue
Block a user