mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-12-02 02:34:24 +00:00
Cave biomes ask the world's CaveBiomes provider through CaveBiomeAnswers, kept per world: the provider is found once, each biome's minimum depth asked once and each overriding biome loaded once (it was a catalog scan twice per cave block)
This commit is contained in:
1 parent
b6a216462d
commit
339efd6174
4 files changed
+126
-15
No files matched your search
@@ -0,0 +1,87 @@
|
|||||||
|
/*
|
||||||
|
* Iris is a World Generator for Minecraft Servers
|
||||||
|
* Copyright (c) 2026 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.core.modules;
|
||||||
|
|
||||||
|
import art.arcane.iris.core.loader.IrisData;
|
||||||
|
import art.arcane.iris.engine.framework.Engine;
|
||||||
|
import art.arcane.iris.engine.object.world.biome.IrisBiome;
|
||||||
|
import com.volmit.service.CaveBiomes;
|
||||||
|
import com.volmit.world.PackWorld;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* What the world's {@link CaveBiomes} provider says, kept for the world. The host asks it for every cave block it gives a
|
||||||
|
* biome, so the provider is found once per world, each biome's minimum depth asked once and each overriding biome
|
||||||
|
* loaded once.
|
||||||
|
*/
|
||||||
|
public final class CaveBiomeAnswers {
|
||||||
|
private static final CaveBiomes NO_PROVIDER = new CaveBiomes() {
|
||||||
|
@Override
|
||||||
|
public Optional<String> overriddenBiome(PackWorld world, int x, int y, int z) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int minDepthBelowSurface(PackWorld world, String biomeKey) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private final PackWorld world;
|
||||||
|
private final IrisData data;
|
||||||
|
private final CaveBiomes provider;
|
||||||
|
private final Map<String, IrisBiome> overridingBiomes = new ConcurrentHashMap<>();
|
||||||
|
private final Map<String, Integer> minDepths = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
CaveBiomeAnswers(Engine engine, CaveBiomes provider) {
|
||||||
|
this.world = engine.getGeneration().world();
|
||||||
|
this.data = engine.getData();
|
||||||
|
this.provider = provider == null ? NO_PROVIDER : provider;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The pack biome the module puts at the position whatever the regions say; null where it overrides nothing or names a biome the pack lacks. {@code y} is the height above the dimension's bottom. */
|
||||||
|
public IrisBiome overriddenBiome(int x, int y, int z) {
|
||||||
|
Optional<String> key = provider.overriddenBiome(world, x, y, z);
|
||||||
|
return key.isEmpty() ? null : overridingBiome(key.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** How many blocks below the surface the caves of the pack biome {@code biomeKey} begin; 0 where nothing limits them. */
|
||||||
|
public int minDepthBelowSurface(String biomeKey) {
|
||||||
|
Integer depth = minDepths.get(biomeKey);
|
||||||
|
if (depth != null) {
|
||||||
|
return depth;
|
||||||
|
}
|
||||||
|
return minDepths.computeIfAbsent(biomeKey, key -> provider.minDepthBelowSurface(world, key));
|
||||||
|
}
|
||||||
|
|
||||||
|
private IrisBiome overridingBiome(String key) {
|
||||||
|
IrisBiome biome = overridingBiomes.get(key);
|
||||||
|
if (biome != null) {
|
||||||
|
return biome;
|
||||||
|
}
|
||||||
|
biome = data.getBiomeLoader().load(key);
|
||||||
|
if (biome != null) {
|
||||||
|
overridingBiomes.putIfAbsent(key, biome);
|
||||||
|
}
|
||||||
|
return biome;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -50,17 +50,10 @@ public final class ModuleServices {
|
|||||||
private ModuleServices() {
|
private ModuleServices() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The pack biome key the module that decides cave biomes puts at the position, whatever the regions say; empty where it overrides nothing. {@code y} is the height above the dimension's bottom. */
|
/** What the module that decides cave biomes says for the engine's world, kept for the world. */
|
||||||
public static Optional<String> overriddenCaveBiome(Engine engine, int x, int y, int z) {
|
public static CaveBiomeAnswers caveBiomes(Engine engine) {
|
||||||
return catalog(engine.getData()).service(CaveBiomes.class)
|
return engine.getGeneration().state(CaveBiomeAnswers.class, owner ->
|
||||||
.flatMap(provider -> provider.overriddenBiome(engine.getGeneration().world(), x, y, z));
|
new CaveBiomeAnswers(owner, catalog(owner.getData()).service(CaveBiomes.class).orElse(null)));
|
||||||
}
|
|
||||||
|
|
||||||
/** How many blocks below the surface the caves of the pack biome {@code biomeKey} begin; 0 where nothing limits them. */
|
|
||||||
public static int caveMinDepthBelowSurface(Engine engine, String biomeKey) {
|
|
||||||
return catalog(engine.getData()).service(CaveBiomes.class)
|
|
||||||
.map(provider -> provider.minDepthBelowSurface(engine.getGeneration().world(), biomeKey))
|
|
||||||
.orElse(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The objects every module would place in the chunk at {@code chunkX}, {@code chunkZ}, guessed without generating it. */
|
/** The objects every module would place in the chunk at {@code chunkX}, {@code chunkZ}, guessed without generating it. */
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import art.arcane.iris.core.loader.IrisData;
|
|||||||
import art.arcane.iris.core.loader.IrisRegistrant;
|
import art.arcane.iris.core.loader.IrisRegistrant;
|
||||||
import art.arcane.iris.core.nms.container.BlockPos;
|
import art.arcane.iris.core.nms.container.BlockPos;
|
||||||
import art.arcane.iris.core.nms.container.Pair;
|
import art.arcane.iris.core.nms.container.Pair;
|
||||||
|
import art.arcane.iris.core.modules.CaveBiomeAnswers;
|
||||||
import art.arcane.iris.core.modules.ModuleServices;
|
import art.arcane.iris.core.modules.ModuleServices;
|
||||||
import art.arcane.iris.engine.IrisComplex;
|
import art.arcane.iris.engine.IrisComplex;
|
||||||
import art.arcane.iris.engine.UpperDimensionContext;
|
import art.arcane.iris.engine.UpperDimensionContext;
|
||||||
@@ -71,7 +72,6 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
@@ -275,8 +275,8 @@ public interface Engine extends DataProvider, Fallible, BlockUpdater, Renderer,
|
|||||||
@BlockCoordinates
|
@BlockCoordinates
|
||||||
default IrisBiome getCaveBiome(int x, int y, int z) {
|
default IrisBiome getCaveBiome(int x, int y, int z) {
|
||||||
IrisBiome surfaceBiome = getSurfaceBiome(x, z);
|
IrisBiome surfaceBiome = getSurfaceBiome(x, z);
|
||||||
Optional<String> overriddenKey = ModuleServices.overriddenCaveBiome(this, x, y, z);
|
CaveBiomeAnswers caves = ModuleServices.caveBiomes(this);
|
||||||
IrisBiome overriddenBiome = overriddenKey.map(key -> getData().getBiomeLoader().load(key)).orElse(null);
|
IrisBiome overriddenBiome = caves.overriddenBiome(x, y, z);
|
||||||
if (overriddenBiome != null) {
|
if (overriddenBiome != null) {
|
||||||
return overriddenBiome;
|
return overriddenBiome;
|
||||||
}
|
}
|
||||||
@@ -292,7 +292,7 @@ public interface Engine extends DataProvider, Fallible, BlockUpdater, Renderer,
|
|||||||
return surfaceBiome;
|
return surfaceBiome;
|
||||||
}
|
}
|
||||||
|
|
||||||
int minDepth = Math.max(0, ModuleServices.caveMinDepthBelowSurface(this, caveBiome.getLoadKey()));
|
int minDepth = Math.max(0, caves.minDepthBelowSurface(caveBiome.getLoadKey()));
|
||||||
if (depthBelowSurface < minDepth) {
|
if (depthBelowSurface < minDepth) {
|
||||||
return surfaceBiome;
|
return surfaceBiome;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,13 +39,17 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertSame;
|
import static org.junit.Assert.assertSame;
|
||||||
import static org.mockito.ArgumentMatchers.anyDouble;
|
import static org.mockito.ArgumentMatchers.anyDouble;
|
||||||
import static org.mockito.ArgumentMatchers.anyInt;
|
import static org.mockito.ArgumentMatchers.anyInt;
|
||||||
import static org.mockito.Mockito.CALLS_REAL_METHODS;
|
import static org.mockito.Mockito.CALLS_REAL_METHODS;
|
||||||
import static org.mockito.Mockito.doReturn;
|
import static org.mockito.Mockito.doReturn;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -166,6 +170,33 @@ public class EngineCaveBiomeTest {
|
|||||||
assertSame(cave, engine.getCaveBiome(10, SURFACE_Y - 1, 20));
|
assertSame(cave, engine.getCaveBiome(10, SURFACE_Y - 1, 20));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void theWorldAsksTheProviderForABiomesDepthOnceAndLoadsAnOverridingBiomeOnce() {
|
||||||
|
AtomicInteger depthQuestions = new AtomicInteger();
|
||||||
|
CaveBiomes counting = caves(null, 10);
|
||||||
|
serve(new CaveBiomes() {
|
||||||
|
@Override
|
||||||
|
public Optional<String> overriddenBiome(PackWorld world, int x, int y, int z) {
|
||||||
|
return y > SURFACE_Y ? Optional.of("band/deep") : Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int minDepthBelowSurface(PackWorld world, String biomeKey) {
|
||||||
|
depthQuestions.incrementAndGet();
|
||||||
|
return counting.minDepthBelowSurface(world, biomeKey);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
for (int y = 0; y < SURFACE_Y; y++) {
|
||||||
|
engine.getCaveBiome(10, y, 20);
|
||||||
|
}
|
||||||
|
assertSame(band, engine.getCaveBiome(10, SURFACE_Y + 5, 20));
|
||||||
|
assertSame(band, engine.getCaveBiome(10, SURFACE_Y + 6, 20));
|
||||||
|
|
||||||
|
assertEquals(1, depthQuestions.get());
|
||||||
|
verify(data.getBiomeLoader(), times(1)).load("band/deep");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void theCarveModulesProviderReadsTheDepthFromTheBiomesSettings() {
|
public void theCarveModulesProviderReadsTheDepthFromTheBiomesSettings() {
|
||||||
IrisBiome deep = mock(IrisBiome.class);
|
IrisBiome deep = mock(IrisBiome.class);
|
||||||
|
|||||||
Reference in new issue
Block a user