mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 08:44:50 +00:00
iris:structure answers the new structure services and keeps its locator indexes in world state
StructureLocator.placed and VanillaStructures.startedStructures/locatableStarts/locateStart are answered from the module's placement index. The Caffeine caches and their invalidate() are gone: each world builds its index once in its state store, and a pack reload starts a new world. StructureReachability moves to Iris core, its only reader.
This commit is contained in:
1 parent
71d0780fcb
commit
75f3504ae7
6 files changed
+81
-161
No files matched your search
@@ -40,8 +40,6 @@ import art.arcane.iris.engine.object.structure.StructureDistribution;
|
||||
import art.arcane.volmlib.util.collection.KList;
|
||||
import art.arcane.volmlib.util.math.RNG;
|
||||
import art.arcane.iris.core.modules.IrisModuleHost;
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import com.volmit.ModuleLog;
|
||||
import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap;
|
||||
|
||||
@@ -59,7 +57,7 @@ import java.util.regex.Pattern;
|
||||
* Finds where Iris assembly and native jigsaw placements generate. A structure key matches
|
||||
* an Iris structure load key, its {@code vanillaSource}, or a configured native registry key.
|
||||
*
|
||||
* A per-engine index of placed keys is cached so {@code /locate} and {@code /iris goto} skip
|
||||
* Each world keeps an index of placed keys in its state, so {@code /locate} and {@code /iris goto} skip
|
||||
* the grid scan entirely for keys that are not placed (keeping them as fast as vanilla).
|
||||
*
|
||||
* For RANDOM_SPREAD placements (the common, vanilla-style spaced grid) the locator jumps
|
||||
@@ -75,9 +73,6 @@ public final class IrisStructureLocator {
|
||||
private static final long CAVE_ANCHOR_DOMAIN = 0x43415645414E4348L;
|
||||
private static final Pattern NAMESPACED_RESOURCE_KEY = Pattern.compile("[a-z0-9_.-]+:[a-z0-9/._-]+");
|
||||
|
||||
private static final Cache<Engine, PlacementIndex> INDEX_CACHE = Caffeine.newBuilder().weakKeys().build();
|
||||
private static final Cache<Engine, LocatableIndex> LOCATABLE_INDEX_CACHE =
|
||||
Caffeine.newBuilder().weakKeys().build();
|
||||
private static final PlacementIndex EMPTY_INDEX = new PlacementIndex(
|
||||
Collections.emptySet(), Collections.emptySet(), Collections.emptySet(), Collections.emptySet(),
|
||||
Collections.emptySet(), Collections.emptyList());
|
||||
@@ -99,6 +94,28 @@ public final class IrisStructureLocator {
|
||||
return index(engine).loadKeys;
|
||||
}
|
||||
|
||||
/**
|
||||
* Every key under which a structure the engine's placements place can be found: the placed keys and the platform
|
||||
* structures Iris structures stand in for ({@code vanillaSource}), the latter in lower case.
|
||||
*/
|
||||
public static Set<String> placedKeysAndAliases(Engine engine) {
|
||||
if (engine == null) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
PlacementIndex placementIndex = index(engine);
|
||||
Set<String> keys = new LinkedHashSet<>(placementIndex.loadKeys);
|
||||
keys.addAll(placementIndex.vanillaAliases);
|
||||
return Collections.unmodifiableSet(keys);
|
||||
}
|
||||
|
||||
/** The platform structures the engine's placements start natively ({@code nativeStructures}), in lower case. */
|
||||
public static Set<String> nativeKeys(Engine engine) {
|
||||
if (engine == null) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
return index(engine).nativeKeys;
|
||||
}
|
||||
|
||||
public static boolean isPlaced(Engine engine, String key) {
|
||||
if (engine == null || key == null || key.isEmpty()) {
|
||||
return false;
|
||||
@@ -168,13 +185,6 @@ public final class IrisStructureLocator {
|
||||
return index(engine).suppressedVanillaSources.contains(normalize(vanillaKey));
|
||||
}
|
||||
|
||||
public static void invalidate(Engine engine) {
|
||||
if (engine != null) {
|
||||
INDEX_CACHE.invalidate(engine);
|
||||
LOCATABLE_INDEX_CACHE.invalidate(engine);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean startsInChunk(Engine engine, String key, int cx, int cz) {
|
||||
if (!isPlaced(engine, key)) {
|
||||
return false;
|
||||
@@ -1057,14 +1067,14 @@ public final class IrisStructureLocator {
|
||||
if (engine.getData() == null || engine.getDimension() == null) {
|
||||
throw new IllegalStateException("Iris structure index requires a fully bound engine and dimension");
|
||||
}
|
||||
return INDEX_CACHE.get(engine, ignored -> build(engine));
|
||||
return engine.getGeneration().state(PlacementIndex.class, IrisStructureLocator::build);
|
||||
}
|
||||
|
||||
private static LocatableIndex locatableIndex(Engine engine) {
|
||||
if (engine == null) {
|
||||
return EMPTY_LOCATABLE_INDEX;
|
||||
}
|
||||
return LOCATABLE_INDEX_CACHE.get(engine, ignored -> buildLocatableIndex(engine));
|
||||
return engine.getGeneration().state(LocatableIndex.class, IrisStructureLocator::buildLocatableIndex);
|
||||
}
|
||||
|
||||
private static LocatableIndex buildLocatableIndex(Engine engine) {
|
||||
|
||||
@@ -1,127 +0,0 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2022 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package art.arcane.iris.engine.framework;
|
||||
|
||||
import art.arcane.iris.engine.object.world.IrisWorld;
|
||||
import art.arcane.iris.spi.IrisPlatforms;
|
||||
import art.arcane.volmlib.util.collection.KList;
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Determines which vanilla/datapack structures can ever generate in a world, using the same biome
|
||||
* gate vanilla applies: a structure is reachable when its biome filter intersects the set of vanilla
|
||||
* biomes the pack actually produces (its {@code possibleBiomes}). Structures whose required biomes are
|
||||
* never produced cannot generate and must not be scanned for by {@code /locate} or {@code /iris find}
|
||||
* (an unbounded scan for an absent biome is what stalls the server).
|
||||
*
|
||||
* <p>The reachable set is fixed per live engine/world and is rebuilt after hotload.
|
||||
*/
|
||||
public final class StructureReachability {
|
||||
private static final Cache<Engine, Set<String>> REACHABLE_CACHE = Caffeine.newBuilder().weakKeys().build();
|
||||
|
||||
private StructureReachability() {
|
||||
}
|
||||
|
||||
public static Set<String> reachableKeys(Engine engine) {
|
||||
Engine activeEngine = Objects.requireNonNull(engine, "Structure reachability requires an engine");
|
||||
if (activeEngine.getData() == null) {
|
||||
throw new IllegalStateException("Structure reachability requires a bound pack data loader");
|
||||
}
|
||||
return REACHABLE_CACHE.get(activeEngine, ignored -> build(activeEngine));
|
||||
}
|
||||
|
||||
public static boolean isReachable(Engine engine, String structureKey) {
|
||||
String normalizedStructureKey = normalizeStructureKey(structureKey);
|
||||
return reachableKeys(engine).contains(normalizedStructureKey);
|
||||
}
|
||||
|
||||
public static void invalidate(Engine engine) {
|
||||
if (engine == null) {
|
||||
return;
|
||||
}
|
||||
REACHABLE_CACHE.invalidate(engine);
|
||||
}
|
||||
|
||||
private static Set<String> build(Engine engine) {
|
||||
IrisWorld world = engine.getWorld();
|
||||
if (world == null) {
|
||||
throw new IllegalStateException("Structure reachability requires a bound Iris world");
|
||||
}
|
||||
if (world.platformWorld() == null) {
|
||||
throw new IllegalStateException("Structure reachability requires a bound platform world");
|
||||
}
|
||||
Set<String> reachable = new LinkedHashSet<>();
|
||||
for (String key : IrisPlatforms.get().structureHooks().reachableStructureKeys(world.platformWorld())) {
|
||||
if (key == null || key.isBlank()) {
|
||||
throw new IllegalStateException("Platform structure reachability returned a blank registry key");
|
||||
}
|
||||
reachable.add(key.toLowerCase(Locale.ROOT));
|
||||
}
|
||||
return Collections.unmodifiableSet(reachable);
|
||||
}
|
||||
|
||||
/**
|
||||
* The vanilla biome keys (e.g. {@code minecraft:taiga}) from a structure's filter that the pack
|
||||
* does not produce. An empty list means the structure is reachable. Used by the structure verify
|
||||
* diagnostic to explain why a structure cannot generate.
|
||||
*/
|
||||
public static KList<String> missingBiomeKeys(Engine engine, String structureKey) {
|
||||
KList<String> missing = new KList<>();
|
||||
Engine activeEngine = Objects.requireNonNull(engine, "Structure biome diagnostics require an engine");
|
||||
String normalizedStructureKey = normalizeStructureKey(structureKey);
|
||||
IrisWorld world = activeEngine.getWorld();
|
||||
if (world == null) {
|
||||
throw new IllegalStateException("Structure biome diagnostics require a bound Iris world");
|
||||
}
|
||||
if (world.platformWorld() == null) {
|
||||
throw new IllegalStateException("Structure biome diagnostics require a bound platform world");
|
||||
}
|
||||
Set<String> possible = new LinkedHashSet<>();
|
||||
for (String key : IrisPlatforms.get().structureHooks().possibleBiomeKeys(world.platformWorld())) {
|
||||
if (key == null || key.isBlank()) {
|
||||
throw new IllegalStateException("Platform structure biome query returned a blank possible-biome key");
|
||||
}
|
||||
possible.add(key.toLowerCase(Locale.ROOT));
|
||||
}
|
||||
for (String biomeKey : IrisPlatforms.get().structureHooks().structureBiomeKeys(normalizedStructureKey)) {
|
||||
if (biomeKey == null || biomeKey.isBlank()) {
|
||||
throw new IllegalStateException("Platform structure biome query returned a blank required-biome key for "
|
||||
+ normalizedStructureKey);
|
||||
}
|
||||
if (!possible.contains(biomeKey.toLowerCase(Locale.ROOT))) {
|
||||
missing.add(biomeKey);
|
||||
}
|
||||
}
|
||||
return missing;
|
||||
}
|
||||
|
||||
private static String normalizeStructureKey(String structureKey) {
|
||||
if (structureKey == null || structureKey.isBlank()) {
|
||||
throw new IllegalArgumentException("Registered structure key must not be blank");
|
||||
}
|
||||
return structureKey.trim().toLowerCase(Locale.ROOT);
|
||||
}
|
||||
}
|
||||
@@ -32,8 +32,9 @@ import java.util.Set;
|
||||
/**
|
||||
* How {@code iris:structure} lets Iris find its structures: the structures of the pack's {@code structures} folder,
|
||||
* the ones the world's placements assemble from jigsaw pools, and their nearest start, searched with
|
||||
* {@link IrisStructureLocator}. Vanilla structures that Iris places through native placements are not among them; the
|
||||
* host's vanilla structure handling finds those.
|
||||
* {@link IrisStructureLocator}. The keys it places also name the platform structures its native placements start and
|
||||
* the ones its structures stand in for; it locates those too. The platform's own locate of a natively started
|
||||
* structure goes through {@link StructureVanillaStructures#locateStart} instead.
|
||||
*/
|
||||
public final class StructureLocating implements StructureLocator {
|
||||
@Override
|
||||
@@ -46,6 +47,11 @@ public final class StructureLocating implements StructureLocator {
|
||||
return IrisStructureLocator.locatableEditableKeys(engine(world));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> placed(PackWorld world) {
|
||||
return IrisStructureLocator.placedKeysAndAliases(engine(world));
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocateResult locate(PackWorld world, String key, int fromX, int fromZ, int maxRadiusChunks) {
|
||||
Engine engine = engine(world);
|
||||
|
||||
+45
-2
@@ -28,6 +28,7 @@ import art.arcane.iris.engine.framework.structure.fit.StructureFitting;
|
||||
import art.arcane.iris.engine.object.structure.IrisStructurePlacement;
|
||||
import art.arcane.iris.engine.object.structure.vanilla.NativeStructureSuppression;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import com.volmit.service.LocateResult;
|
||||
import com.volmit.service.VanillaStructures;
|
||||
import com.volmit.vanilla.PlatformStart;
|
||||
import com.volmit.vanilla.StartMemo;
|
||||
@@ -38,16 +39,22 @@ import com.volmit.vanilla.VanillaStructureRule;
|
||||
import com.volmit.vanilla.VanillaStructureSetup;
|
||||
import com.volmit.vanilla.VanillaStructureStart;
|
||||
import com.volmit.vanilla.VanillaStructureStartContext;
|
||||
import com.volmit.world.BlockPosition;
|
||||
import com.volmit.world.PackWorld;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* What {@code iris:structure} says about the platform's own structures. The dimension's {@code importedStructures}
|
||||
* settings ({@link ImportedStructures}) disable, move, fit and set them up; the pack's structure placements replace
|
||||
* them ({@code vanillaSource} with native suppression), place them alongside ({@link IrisStructureLocator#isPlaced}),
|
||||
* start them where their {@code nativeStructures} grids say ({@link NativeStartPlanner}) and fit those starts the way
|
||||
* the placement says, which their memo carries to every chunk they reach ({@link StructureFitting}).
|
||||
* the placement says, which their memo carries to every chunk they reach ({@link StructureFitting}). It names the
|
||||
* structures its native placements start and finds their starts for the platform's own locate.
|
||||
*/
|
||||
public final class StructureVanillaStructures implements VanillaStructures {
|
||||
/**
|
||||
@@ -68,7 +75,7 @@ public final class StructureVanillaStructures implements VanillaStructures {
|
||||
|
||||
@Override
|
||||
public List<VanillaStructureStart> starts(int chunkX, int chunkZ, VanillaStructureStartContext context) {
|
||||
Engine engine = IrisPackWorld.of(context.world()).engine();
|
||||
Engine engine = engine(context.world());
|
||||
return NativeStartPlanner.plansAt(engine, chunkX, chunkZ).stream()
|
||||
.map(plan -> start(plan, engine.getMinHeight()))
|
||||
.toList();
|
||||
@@ -103,6 +110,42 @@ public final class StructureVanillaStructures implements VanillaStructures {
|
||||
return ImportedStructures.of(context.settings()).setup();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<ResourceId> startedStructures(PackWorld world) {
|
||||
return structureIds(IrisStructureLocator.nativeKeys(engine(world)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<ResourceId> locatableStarts(PackWorld world) {
|
||||
return structureIds(IrisStructureLocator.locatableNativeKeys(engine(world)));
|
||||
}
|
||||
|
||||
/** Searches the rings of the placements that start {@code structure} natively, as the platform's own locate does. */
|
||||
@Override
|
||||
public LocateResult locateStart(PackWorld world, ResourceId structure, int fromX, int fromZ, int maxRings,
|
||||
ChunkFilter accepts) {
|
||||
Engine engine = engine(world);
|
||||
IrisStructureLocator.LocateResult result = IrisStructureLocator.locateInPlacementRings(
|
||||
engine, structure.toString(), fromX, fromZ, maxRings, accepts::accepts);
|
||||
return switch (result.status()) {
|
||||
case FOUND -> LocateResult.found(new BlockPosition(result.originX(), result.baseY() - engine.getMinHeight(), result.originZ()));
|
||||
case NOT_FOUND -> LocateResult.NOT_FOUND;
|
||||
case SEARCH_LIMIT_REACHED -> LocateResult.SEARCH_LIMIT_REACHED;
|
||||
};
|
||||
}
|
||||
|
||||
private static Engine engine(PackWorld world) {
|
||||
return IrisPackWorld.of(world).engine();
|
||||
}
|
||||
|
||||
private static Set<ResourceId> structureIds(Set<String> keys) {
|
||||
Set<ResourceId> ids = new LinkedHashSet<>();
|
||||
for (String key : keys) {
|
||||
ids.add(ResourceId.parse(key.toLowerCase(Locale.ROOT)));
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
/** {@code plan} as the start the platform generates: heights above the bottom, the placement's identity and fit. */
|
||||
static VanillaStructureStart start(NativeStartPlanner.Plan plan, int minHeight) {
|
||||
IrisStructurePlacement placement = plan.placement();
|
||||
|
||||
@@ -24,9 +24,7 @@ import art.arcane.iris.builtin.structure.StructureStilts;
|
||||
import art.arcane.iris.builtin.structure.StructureTerrain;
|
||||
import art.arcane.iris.core.modules.IrisMantleChunk;
|
||||
import art.arcane.iris.core.modules.IrisPackSettings;
|
||||
import art.arcane.iris.core.modules.IrisPackWorld;
|
||||
import art.arcane.iris.engine.framework.Engine;
|
||||
import art.arcane.iris.engine.framework.IrisStructureLocator;
|
||||
import art.arcane.iris.engine.framework.structure.StructureLocating;
|
||||
import art.arcane.iris.engine.framework.structure.StructureResources;
|
||||
import art.arcane.iris.engine.framework.structure.StructureValidator;
|
||||
@@ -34,7 +32,6 @@ import art.arcane.iris.engine.framework.structure.StructureVanillaStructures;
|
||||
import com.volmit.FeatureRegistry;
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.ModuleInfo;
|
||||
import com.volmit.ModuleLifecycle;
|
||||
import com.volmit.ModuleLog;
|
||||
import com.volmit.config.PackSettings;
|
||||
import com.volmit.service.StructureLocator;
|
||||
@@ -42,7 +39,6 @@ import com.volmit.service.VanillaStructures;
|
||||
import com.volmit.stage.MantleStageContext;
|
||||
import com.volmit.stage.StageGraph;
|
||||
import com.volmit.vanilla.JigsawOverrides;
|
||||
import com.volmit.world.PackWorld;
|
||||
|
||||
/**
|
||||
* Module {@code iris:structure}: Iris structures, written into the mantle after objects, whose trees they clear
|
||||
@@ -52,8 +48,8 @@ import com.volmit.world.PackWorld;
|
||||
* The module owns the pack's structures, jigsaw pools and jigsaw pieces as resource kinds
|
||||
* ({@link StructureResources}), read from the folders packs have always used, and checks them with
|
||||
* {@link StructureValidator} wherever Iris validates a pack. It lets Iris find its structures through
|
||||
* {@link StructureLocating}, and forgets a world's structure index when the world stops, which includes every reload of
|
||||
* the pack's structure files.
|
||||
* {@link StructureLocating}; a world's structure index lives in the world's state, so a reload of the pack's structure
|
||||
* files, which starts a new world, builds it again.
|
||||
* <p>
|
||||
* It decides over the platform's own structures through its {@link VanillaStructures} provider
|
||||
* ({@link StructureVanillaStructures}): the dimension's {@code importedStructures} settings disable, move, fit and set
|
||||
@@ -61,7 +57,7 @@ import com.volmit.world.PackWorld;
|
||||
* {@code nativeStructures} grids say and fit them into the terrain. It describes the fit settings Iris's structure
|
||||
* placements hold.
|
||||
*/
|
||||
public final class StructureModule implements IrisModule, ModuleLifecycle {
|
||||
public final class StructureModule implements IrisModule {
|
||||
private static final String VERSION = "built-in";
|
||||
|
||||
private ModuleLog log;
|
||||
@@ -84,19 +80,12 @@ public final class StructureModule implements IrisModule, ModuleLifecycle {
|
||||
StructureResources.kinds().forEach(registry::registerResourceKind);
|
||||
registry.registerValidator(new StructureValidator());
|
||||
registry.registerService(StructureLocator.class, new StructureLocating());
|
||||
registry.registerLifecycle(this);
|
||||
registry.registerService(VanillaStructures.class, new StructureVanillaStructures());
|
||||
registry.registerHeldType(StructureTerrain.class);
|
||||
registry.registerHeldType(StructureStilts.class);
|
||||
registry.registerHeldType(JigsawOverrides.class);
|
||||
}
|
||||
|
||||
/** The structure index is still kept per engine rather than in the world's state, so a stopping world drops it. */
|
||||
@Override
|
||||
public void worldStopped(PackWorld world) {
|
||||
IrisStructureLocator.invalidate(IrisPackWorld.of(world).engine());
|
||||
}
|
||||
|
||||
private static void place(MantleStageContext context) {
|
||||
IrisMantleChunk chunk = IrisMantleChunk.current();
|
||||
placing(chunk.engine(), context.log()).place(chunk.writer(), chunk.chunkX(), chunk.chunkZ(), chunk.context());
|
||||
|
||||
Reference in new issue
Block a user