mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-12-03 20:01:49 +00:00
perf: replace biome tag checks with bitset columns
This commit is contained in:
1 parent
6ddf039e49
commit
8a940f7e1f
45 files changed
+938
-314
No files matched your search
+1
-1
@@ -83,7 +83,7 @@ public class BiomePipelineAddon implements AddonInitializer {
|
||||
.register(addon, ConfigPackPostLoadEvent.class)
|
||||
.then(event -> {
|
||||
Registry<Biome> biomeRegistry = event.getPack().getRegistry(Biome.class);
|
||||
event.getPack().applyLoader(PipelineBiome.class, new PipelineBiomeLoader(biomeRegistry));
|
||||
event.getPack().applyLoader(PipelineBiome.class, new PipelineBiomeLoader(biomeRegistry, event.getPack().getBiomeTagIndex()));
|
||||
});
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -5,10 +5,11 @@ import java.util.Set;
|
||||
import com.dfsek.terra.api.world.biome.Biome;
|
||||
|
||||
|
||||
public final class DelegatedPipelineBiome implements PipelineBiome {
|
||||
public final class DelegatedPipelineBiome extends PipelineBiome {
|
||||
private final Biome biome;
|
||||
|
||||
public DelegatedPipelineBiome(Biome biome) {
|
||||
super(biome.getIntID());
|
||||
this.biome = biome;
|
||||
}
|
||||
|
||||
|
||||
+40
-9
@@ -4,30 +4,61 @@ import java.util.Set;
|
||||
|
||||
import com.dfsek.terra.api.registry.key.StringIdentifiable;
|
||||
import com.dfsek.terra.api.world.biome.Biome;
|
||||
import com.dfsek.terra.api.world.biome.tag.BiomeTagIndex;
|
||||
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
||||
public interface PipelineBiome extends StringIdentifiable {
|
||||
static PipelineBiome placeholder(String id) {
|
||||
return new PlaceholderPipelineBiome(id);
|
||||
public abstract sealed class PipelineBiome implements StringIdentifiable
|
||||
permits DelegatedPipelineBiome, PlaceholderPipelineBiome, SelfPipelineBiome {
|
||||
private final int intId;
|
||||
|
||||
PipelineBiome(int intId) {
|
||||
this.intId = intId;
|
||||
}
|
||||
|
||||
static PipelineBiome from(Biome biome) {
|
||||
/**
|
||||
* Registers a new ID per call; call before {@code index} is sealed.
|
||||
*/
|
||||
@ApiStatus.Internal
|
||||
public static PipelineBiome placeholder(String id, BiomeTagIndex index) {
|
||||
return new PlaceholderPipelineBiome(id, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code biome} must come from the same pack as the index it is tested against.
|
||||
*/
|
||||
public static PipelineBiome from(Biome biome) {
|
||||
return new DelegatedPipelineBiome(biome);
|
||||
}
|
||||
|
||||
static PipelineBiome self() {
|
||||
public static PipelineBiome self() {
|
||||
return SelfPipelineBiome.INSTANCE;
|
||||
}
|
||||
|
||||
Biome getBiome();
|
||||
/**
|
||||
* Dense per pack, not stable across loads.
|
||||
*/
|
||||
@Contract(pure = true)
|
||||
public final int getIntID() {
|
||||
return intId;
|
||||
}
|
||||
|
||||
Set<String> getTags();
|
||||
@NotNull
|
||||
public abstract Biome getBiome();
|
||||
|
||||
default boolean isPlaceholder() {
|
||||
@NotNull
|
||||
public abstract Set<String> getTags();
|
||||
|
||||
@Contract(pure = true)
|
||||
public boolean isPlaceholder() {
|
||||
return false;
|
||||
}
|
||||
|
||||
default boolean isSelf() {
|
||||
@Contract(pure = true)
|
||||
public boolean isSelf() {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
+14
-3
@@ -4,17 +4,28 @@ import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import com.dfsek.terra.api.world.biome.Biome;
|
||||
import com.dfsek.terra.api.world.biome.tag.BiomeTagIndex;
|
||||
|
||||
|
||||
final class PlaceholderPipelineBiome implements PipelineBiome {
|
||||
final class PlaceholderPipelineBiome extends PipelineBiome {
|
||||
private final Set<String> tags;
|
||||
private final String id;
|
||||
|
||||
public PlaceholderPipelineBiome(String id) {
|
||||
public PlaceholderPipelineBiome(String id, BiomeTagIndex index) {
|
||||
this(id, tagsOf(id), index);
|
||||
}
|
||||
|
||||
private PlaceholderPipelineBiome(String id, Set<String> tags, BiomeTagIndex index) {
|
||||
super(index.register(tags));
|
||||
this.id = id;
|
||||
tags = new HashSet<>();
|
||||
this.tags = tags;
|
||||
}
|
||||
|
||||
private static Set<String> tagsOf(String id) {
|
||||
Set<String> tags = new HashSet<>();
|
||||
tags.add(id);
|
||||
tags.add("ALL");
|
||||
return Set.copyOf(tags);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+2
-2
@@ -6,11 +6,11 @@ import java.util.Set;
|
||||
import com.dfsek.terra.api.world.biome.Biome;
|
||||
|
||||
|
||||
final class SelfPipelineBiome implements PipelineBiome {
|
||||
final class SelfPipelineBiome extends PipelineBiome {
|
||||
public static final SelfPipelineBiome INSTANCE = new SelfPipelineBiome();
|
||||
|
||||
private SelfPipelineBiome() {
|
||||
|
||||
super(0); // reserved in every pack's index, untagged
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+8
-2
@@ -7,17 +7,23 @@ import com.dfsek.tectonic.api.loader.type.TypeLoader;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import com.dfsek.terra.addons.biome.pipeline.api.biome.PipelineBiome;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.world.biome.Biome;
|
||||
import com.dfsek.terra.api.world.biome.tag.BiomeTagIndex;
|
||||
|
||||
|
||||
public class PipelineBiomeLoader implements TypeLoader<PipelineBiome> {
|
||||
private final Registry<Biome> biomeRegistry;
|
||||
|
||||
public PipelineBiomeLoader(Registry<Biome> biomeRegistry) {
|
||||
private final BiomeTagIndex tagIndex;
|
||||
private final ConcurrentHashMap<String, PipelineBiome> placeholders = new ConcurrentHashMap<>();
|
||||
|
||||
public PipelineBiomeLoader(Registry<Biome> biomeRegistry, BiomeTagIndex tagIndex) {
|
||||
this.biomeRegistry = biomeRegistry;
|
||||
this.tagIndex = tagIndex;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -27,6 +33,6 @@ public class PipelineBiomeLoader implements TypeLoader<PipelineBiome> {
|
||||
return biomeRegistry
|
||||
.getByID((String) c)
|
||||
.map(PipelineBiome::from)
|
||||
.orElseGet(() -> PipelineBiome.placeholder((String) c));
|
||||
.orElseGet(() -> placeholders.computeIfAbsent((String) c, id -> PipelineBiome.placeholder(id, tagIndex)));
|
||||
}
|
||||
}
|
||||
+3
-2
@@ -17,15 +17,16 @@ import com.dfsek.terra.addons.biome.pipeline.config.stage.StageTemplate;
|
||||
import com.dfsek.terra.addons.biome.pipeline.stage.mutators.BorderListStage;
|
||||
import com.dfsek.terra.api.config.meta.Meta;
|
||||
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
|
||||
import com.dfsek.terra.api.world.biome.tag.BiomeTag;
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class BorderListStageTemplate extends StageTemplate {
|
||||
@Value("from")
|
||||
private @Meta String from;
|
||||
private @Meta BiomeTag from;
|
||||
|
||||
@Value("default-replace")
|
||||
private @Meta String defaultReplace;
|
||||
private @Meta BiomeTag defaultReplace;
|
||||
|
||||
@Value("default-to")
|
||||
private @Meta ProbabilityCollection<@Meta PipelineBiome> defaultTo;
|
||||
|
||||
+3
-2
@@ -15,15 +15,16 @@ import com.dfsek.terra.addons.biome.pipeline.config.stage.StageTemplate;
|
||||
import com.dfsek.terra.addons.biome.pipeline.stage.mutators.BorderStage;
|
||||
import com.dfsek.terra.api.config.meta.Meta;
|
||||
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
|
||||
import com.dfsek.terra.api.world.biome.tag.BiomeTag;
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class BorderStageTemplate extends StageTemplate {
|
||||
@Value("from")
|
||||
private @Meta String from;
|
||||
private @Meta BiomeTag from;
|
||||
|
||||
@Value("replace")
|
||||
private @Meta String replace;
|
||||
private @Meta BiomeTag replace;
|
||||
|
||||
@Value("to")
|
||||
private @Meta ProbabilityCollection<@Meta PipelineBiome> to;
|
||||
|
||||
+2
-1
@@ -17,12 +17,13 @@ import com.dfsek.terra.addons.biome.pipeline.config.stage.StageTemplate;
|
||||
import com.dfsek.terra.addons.biome.pipeline.stage.mutators.ReplaceListStage;
|
||||
import com.dfsek.terra.api.config.meta.Meta;
|
||||
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
|
||||
import com.dfsek.terra.api.world.biome.tag.BiomeTag;
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class ReplaceListStageTemplate extends StageTemplate {
|
||||
@Value("default-from")
|
||||
private @Meta String defaultFrom;
|
||||
private @Meta BiomeTag defaultFrom;
|
||||
|
||||
@Value("default-to")
|
||||
private @Meta ProbabilityCollection<@Meta PipelineBiome> defaultTo;
|
||||
|
||||
+2
-1
@@ -15,12 +15,13 @@ import com.dfsek.terra.addons.biome.pipeline.config.stage.StageTemplate;
|
||||
import com.dfsek.terra.addons.biome.pipeline.stage.mutators.ReplaceStage;
|
||||
import com.dfsek.terra.api.config.meta.Meta;
|
||||
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
|
||||
import com.dfsek.terra.api.world.biome.tag.BiomeTag;
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class ReplaceStageTemplate extends StageTemplate {
|
||||
@Value("from")
|
||||
private @Meta String from;
|
||||
private @Meta BiomeTag from;
|
||||
|
||||
@Value("to")
|
||||
private @Meta ProbabilityCollection<@Meta PipelineBiome> to;
|
||||
|
||||
+14
-7
@@ -21,24 +21,29 @@ import com.dfsek.terra.addons.biome.pipeline.api.Stage;
|
||||
import com.dfsek.terra.addons.biome.pipeline.api.biome.PipelineBiome;
|
||||
import com.dfsek.terra.addons.biome.pipeline.pipeline.BiomeChunkImpl;
|
||||
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
|
||||
import com.dfsek.terra.api.world.biome.tag.BiomeTag;
|
||||
|
||||
|
||||
public class BorderListStage implements Stage {
|
||||
private final String border;
|
||||
private final BiomeTag border;
|
||||
private final Sampler Sampler;
|
||||
private final ProbabilityCollection<PipelineBiome> replaceDefault;
|
||||
private final String defaultReplace;
|
||||
private final BiomeTag defaultReplace;
|
||||
private final Map<PipelineBiome, ProbabilityCollection<PipelineBiome>> replace;
|
||||
// By PipelineBiome#getIntID(); relies on one ID per biome name.
|
||||
private final ProbabilityCollection<PipelineBiome>[] replaceById;
|
||||
|
||||
private final Vector2Int[] borderPoints;
|
||||
|
||||
public BorderListStage(Map<PipelineBiome, ProbabilityCollection<PipelineBiome>> replace, String border, String defaultReplace,
|
||||
public BorderListStage(Map<PipelineBiome, ProbabilityCollection<PipelineBiome>> replace, BiomeTag border,
|
||||
BiomeTag defaultReplace,
|
||||
Sampler Sampler, ProbabilityCollection<PipelineBiome> replaceDefault) {
|
||||
this.border = border;
|
||||
this.Sampler = Sampler;
|
||||
this.replaceDefault = replaceDefault;
|
||||
this.defaultReplace = defaultReplace;
|
||||
this.replace = replace;
|
||||
this.replaceById = ReplaceTable.byId(replace);
|
||||
|
||||
List<Vector2Int> points = new ArrayList<>();
|
||||
for(int x = -1; x <= 1; x++) {
|
||||
@@ -63,12 +68,14 @@ public class BorderListStage implements Stage {
|
||||
@Override
|
||||
public PipelineBiome apply(BiomeChunkImpl.ViewPoint viewPoint) {
|
||||
PipelineBiome center = viewPoint.getBiome();
|
||||
if(center.getTags().contains(defaultReplace)) {
|
||||
int centerId = center.getIntID();
|
||||
if(defaultReplace.has(centerId)) {
|
||||
for(Vector2Int point : borderPoints) {
|
||||
PipelineBiome current = viewPoint.getRelativeBiome(point.getX(), point.getZ());
|
||||
if(current != null && current.getTags().contains(border)) {
|
||||
if(replace.containsKey(center)) {
|
||||
PipelineBiome replacement = replace.get(center).get(Sampler, viewPoint.worldX(), viewPoint.worldZ(),
|
||||
if(current != null && border.has(current.getIntID())) {
|
||||
ProbabilityCollection<PipelineBiome> collection = centerId < replaceById.length ? replaceById[centerId] : null;
|
||||
if(collection != null) {
|
||||
PipelineBiome replacement = collection.get(Sampler, viewPoint.worldX(), viewPoint.worldZ(),
|
||||
viewPoint.worldSeed());
|
||||
return replacement.isSelf() ? center : replacement;
|
||||
}
|
||||
|
||||
+6
-5
@@ -20,16 +20,17 @@ import com.dfsek.terra.addons.biome.pipeline.api.Stage;
|
||||
import com.dfsek.terra.addons.biome.pipeline.api.biome.PipelineBiome;
|
||||
import com.dfsek.terra.addons.biome.pipeline.pipeline.BiomeChunkImpl;
|
||||
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
|
||||
import com.dfsek.terra.api.world.biome.tag.BiomeTag;
|
||||
|
||||
|
||||
public class BorderStage implements Stage {
|
||||
private final String border;
|
||||
private final BiomeTag border;
|
||||
private final Sampler Sampler;
|
||||
private final ProbabilityCollection<PipelineBiome> replace;
|
||||
private final String replaceTag;
|
||||
private final BiomeTag replaceTag;
|
||||
private final Vector2Int[] borderPoints;
|
||||
|
||||
public BorderStage(String border, String replaceTag, Sampler Sampler, ProbabilityCollection<PipelineBiome> replace) {
|
||||
public BorderStage(BiomeTag border, BiomeTag replaceTag, Sampler Sampler, ProbabilityCollection<PipelineBiome> replace) {
|
||||
this.border = border;
|
||||
this.Sampler = Sampler;
|
||||
this.replace = replace;
|
||||
@@ -47,10 +48,10 @@ public class BorderStage implements Stage {
|
||||
@Override
|
||||
public PipelineBiome apply(BiomeChunkImpl.ViewPoint viewPoint) {
|
||||
PipelineBiome center = viewPoint.getBiome();
|
||||
if(center.getTags().contains(replaceTag)) {
|
||||
if(replaceTag.has(center.getIntID())) {
|
||||
for(Vector2Int point : borderPoints) {
|
||||
PipelineBiome current = viewPoint.getRelativeBiome(point.getX(), point.getZ());
|
||||
if(current != null && current.getTags().contains(border)) {
|
||||
if(current != null && border.has(current.getIntID())) {
|
||||
PipelineBiome replacement = replace.get(Sampler, viewPoint.worldX(), viewPoint.worldZ(), viewPoint.worldSeed());
|
||||
return replacement.isSelf() ? center : replacement;
|
||||
}
|
||||
|
||||
+12
-6
@@ -18,17 +18,21 @@ import com.dfsek.terra.addons.biome.pipeline.api.Stage;
|
||||
import com.dfsek.terra.addons.biome.pipeline.api.biome.PipelineBiome;
|
||||
import com.dfsek.terra.addons.biome.pipeline.pipeline.BiomeChunkImpl;
|
||||
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
|
||||
import com.dfsek.terra.api.world.biome.tag.BiomeTag;
|
||||
|
||||
|
||||
public class ReplaceListStage implements Stage {
|
||||
private final Map<PipelineBiome, ProbabilityCollection<PipelineBiome>> replace;
|
||||
// By PipelineBiome#getIntID(); relies on one ID per biome name.
|
||||
private final ProbabilityCollection<PipelineBiome>[] replaceById;
|
||||
private final Sampler sampler;
|
||||
private final ProbabilityCollection<PipelineBiome> replaceDefault;
|
||||
private final String defaultTag;
|
||||
private final BiomeTag defaultTag;
|
||||
|
||||
public ReplaceListStage(Map<PipelineBiome, ProbabilityCollection<PipelineBiome>> replace, String defaultTag,
|
||||
public ReplaceListStage(Map<PipelineBiome, ProbabilityCollection<PipelineBiome>> replace, BiomeTag defaultTag,
|
||||
ProbabilityCollection<PipelineBiome> replaceDefault, Sampler sampler) {
|
||||
this.replace = replace;
|
||||
this.replaceById = ReplaceTable.byId(replace);
|
||||
this.sampler = sampler;
|
||||
this.defaultTag = defaultTag;
|
||||
this.replaceDefault = replaceDefault;
|
||||
@@ -37,11 +41,13 @@ public class ReplaceListStage implements Stage {
|
||||
@Override
|
||||
public PipelineBiome apply(BiomeChunkImpl.ViewPoint viewPoint) {
|
||||
PipelineBiome center = viewPoint.getBiome();
|
||||
if(replace.containsKey(center)) {
|
||||
PipelineBiome biome = replace.get(center).get(sampler, viewPoint.worldX(), viewPoint.worldZ(), viewPoint.worldSeed());
|
||||
int id = center.getIntID();
|
||||
ProbabilityCollection<PipelineBiome> collection = id < replaceById.length ? replaceById[id] : null;
|
||||
if(collection != null) {
|
||||
PipelineBiome biome = collection.get(sampler, viewPoint.worldX(), viewPoint.worldZ(), viewPoint.worldSeed());
|
||||
return biome.isSelf() ? viewPoint.getBiome() : biome;
|
||||
}
|
||||
if(viewPoint.getBiome().getTags().contains(defaultTag)) {
|
||||
if(defaultTag.has(id)) {
|
||||
PipelineBiome biome = replaceDefault.get(sampler, viewPoint.worldX(), viewPoint.worldZ(), viewPoint.worldSeed());
|
||||
return biome.isSelf() ? viewPoint.getBiome() : biome;
|
||||
}
|
||||
@@ -60,7 +66,7 @@ public class ReplaceListStage implements Stage {
|
||||
Set<PipelineBiome> reject = new HashSet<>();
|
||||
|
||||
biomes.forEach(biome -> {
|
||||
if(!biome.getTags().contains(defaultTag) && !replace.containsKey(biome)) {
|
||||
if(!biome.getTags().contains(defaultTag.name()) && !replace.containsKey(biome)) {
|
||||
biomeSet.add(biome);
|
||||
} else {
|
||||
reject.add(biome);
|
||||
|
||||
+5
-4
@@ -17,14 +17,15 @@ import com.dfsek.terra.addons.biome.pipeline.api.Stage;
|
||||
import com.dfsek.terra.addons.biome.pipeline.api.biome.PipelineBiome;
|
||||
import com.dfsek.terra.addons.biome.pipeline.pipeline.BiomeChunkImpl;
|
||||
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
|
||||
import com.dfsek.terra.api.world.biome.tag.BiomeTag;
|
||||
|
||||
|
||||
public class ReplaceStage implements Stage {
|
||||
private final String replaceableTag;
|
||||
private final BiomeTag replaceableTag;
|
||||
private final ProbabilityCollection<PipelineBiome> replace;
|
||||
private final Sampler sampler;
|
||||
|
||||
public ReplaceStage(String replaceable, ProbabilityCollection<PipelineBiome> replace, Sampler sampler) {
|
||||
public ReplaceStage(BiomeTag replaceable, ProbabilityCollection<PipelineBiome> replace, Sampler sampler) {
|
||||
this.replaceableTag = replaceable;
|
||||
this.replace = replace;
|
||||
this.sampler = sampler;
|
||||
@@ -32,7 +33,7 @@ public class ReplaceStage implements Stage {
|
||||
|
||||
@Override
|
||||
public PipelineBiome apply(BiomeChunkImpl.ViewPoint viewPoint) {
|
||||
if(viewPoint.getBiome().getTags().contains(replaceableTag)) {
|
||||
if(replaceableTag.has(viewPoint.getBiome().getIntID())) {
|
||||
PipelineBiome biome = replace.get(sampler, viewPoint.worldX(), viewPoint.worldZ(), viewPoint.worldSeed());
|
||||
return biome.isSelf() ? viewPoint.getBiome() : biome;
|
||||
}
|
||||
@@ -49,7 +50,7 @@ public class ReplaceStage implements Stage {
|
||||
Set<PipelineBiome> biomeSet = new HashSet<>();
|
||||
Set<PipelineBiome> reject = new HashSet<>();
|
||||
biomes.forEach(biome -> {
|
||||
if(!biome.getTags().contains(replaceableTag)) {
|
||||
if(!biome.getTags().contains(replaceableTag.name())) {
|
||||
biomeSet.add(biome);
|
||||
} else {
|
||||
reject.add(biome);
|
||||
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2025 Polyhedral Development
|
||||
*
|
||||
* The Terra Core Addons are licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in this module's root directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.addons.biome.pipeline.stage.mutators;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.dfsek.terra.addons.biome.pipeline.api.biome.PipelineBiome;
|
||||
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
|
||||
|
||||
|
||||
final class ReplaceTable {
|
||||
private ReplaceTable() { }
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
static ProbabilityCollection<PipelineBiome>[] byId(Map<PipelineBiome, ProbabilityCollection<PipelineBiome>> replace) {
|
||||
int max = -1;
|
||||
for(PipelineBiome key : replace.keySet()) max = Math.max(max, key.getIntID());
|
||||
ProbabilityCollection<PipelineBiome>[] table = new ProbabilityCollection[max + 1];
|
||||
replace.forEach((key, value) -> table[key.getIntID()] = value);
|
||||
return table;
|
||||
}
|
||||
}
|
||||
+190
@@ -0,0 +1,190 @@
|
||||
package pipeline;
|
||||
|
||||
import com.dfsek.seismic.type.sampler.Sampler;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import com.dfsek.terra.addons.biome.pipeline.api.Source;
|
||||
import com.dfsek.terra.addons.biome.pipeline.api.Stage;
|
||||
import com.dfsek.terra.addons.biome.pipeline.api.biome.PipelineBiome;
|
||||
import com.dfsek.terra.addons.biome.pipeline.config.PipelineBiomeLoader;
|
||||
import com.dfsek.terra.addons.biome.pipeline.pipeline.PipelineImpl;
|
||||
import com.dfsek.terra.addons.biome.pipeline.stage.mutators.BorderListStage;
|
||||
import com.dfsek.terra.addons.biome.pipeline.stage.mutators.ReplaceListStage;
|
||||
import com.dfsek.terra.addons.biome.pipeline.stage.mutators.ReplaceStage;
|
||||
import com.dfsek.terra.api.properties.Context;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.util.cache.SeededVector2Key;
|
||||
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
|
||||
import com.dfsek.terra.api.world.biome.Biome;
|
||||
import com.dfsek.terra.api.world.biome.PlatformBiome;
|
||||
import com.dfsek.terra.api.world.biome.tag.BiomeTagIndex;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
|
||||
class PipelineBiomeTagTest {
|
||||
private static final Sampler SAMPLER = new Sampler() {
|
||||
@Override
|
||||
public double getSample(long seed, double x, double y) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getSample(long seed, double x, double y, double z) {
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
private static final class StubBiome implements Biome {
|
||||
private final String id;
|
||||
private final Set<String> tags;
|
||||
private final int intId;
|
||||
|
||||
StubBiome(BiomeTagIndex index, String id, Set<String> tags) {
|
||||
this.id = id;
|
||||
this.tags = tags;
|
||||
this.intId = index.register(tags);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlatformBiome getPlatformBiome() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getColor() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIntID() {
|
||||
return intId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Context getContext() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getID() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Registry<Biome> emptyRegistry() {
|
||||
return (Registry<Biome>) Proxy.newProxyInstance(Registry.class.getClassLoader(), new Class<?>[]{ Registry.class },
|
||||
(proxy, method, args) -> {
|
||||
if(method.getName().equals("getByID")) return Optional.empty();
|
||||
throw new UnsupportedOperationException(method.getName());
|
||||
});
|
||||
}
|
||||
|
||||
private final BiomeTagIndex index = new BiomeTagIndex();
|
||||
private final Map<String, PipelineBiome> biomes = new LinkedHashMap<>();
|
||||
|
||||
{
|
||||
biome("border", "border");
|
||||
biome("c", "R", "D");
|
||||
biome("a", "R");
|
||||
biome("b", "R");
|
||||
biome("x");
|
||||
biome("y");
|
||||
biome("e");
|
||||
biome("f", "R", "D");
|
||||
}
|
||||
|
||||
private void biome(String id, String... tags) {
|
||||
biomes.put(id, PipelineBiome.from(new StubBiome(index, id, Set.of(tags))));
|
||||
}
|
||||
|
||||
private static ProbabilityCollection<PipelineBiome> only(PipelineBiome biome) {
|
||||
return new ProbabilityCollection<PipelineBiome>().add(biome, 1);
|
||||
}
|
||||
|
||||
private Map<PipelineBiome, ProbabilityCollection<PipelineBiome>> replaceTable() {
|
||||
Map<PipelineBiome, ProbabilityCollection<PipelineBiome>> replace = new HashMap<>();
|
||||
replace.put(biomes.get("a"), only(biomes.get("x")));
|
||||
replace.put(biomes.get("b"), only(PipelineBiome.self()));
|
||||
return replace;
|
||||
}
|
||||
|
||||
private String run(Stage stage, String centre) {
|
||||
index.seal();
|
||||
PipelineBiome centreBiome = biomes.get(centre);
|
||||
PipelineBiome border = biomes.get("border");
|
||||
Source source = new Source() {
|
||||
@Override
|
||||
public PipelineBiome get(long seed, int x, int z) {
|
||||
return x == 0 && z == 0 ? centreBiome : border;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<PipelineBiome> getBiomes() {
|
||||
return List.of(centreBiome, border);
|
||||
}
|
||||
};
|
||||
return new PipelineImpl(source, List.of(stage), 1, 3).generateChunk(new SeededVector2Key(0, 0, 0)).get(0, 0).getID();
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0} -> {1}")
|
||||
@CsvSource({ "a, x", "b, b", "c, y", "e, e", "f, y" })
|
||||
void borderListStage(String centre, String expected) {
|
||||
assertEquals(expected, run(new BorderListStage(replaceTable(), index.tag("border"), index.tag("R"), SAMPLER,
|
||||
only(biomes.get("y"))), centre));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0} -> {1}")
|
||||
@CsvSource({ "a, x", "b, b", "c, y", "e, e", "f, y" })
|
||||
void replaceListStage(String centre, String expected) {
|
||||
assertEquals(expected, run(new ReplaceListStage(replaceTable(), index.tag("D"), only(biomes.get("y")), SAMPLER), centre));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0}: {1} -> {2}")
|
||||
@CsvSource({ "D, c, y", "D, e, e", "TYPO, c, c", "TYPO, e, e" })
|
||||
void replaceStage(String tag, String centre, String expected) {
|
||||
assertEquals(expected, run(new ReplaceStage(index.tag(tag), only(biomes.get("y")), SAMPLER), centre));
|
||||
}
|
||||
|
||||
@Test
|
||||
void placeholderRegistration() {
|
||||
PipelineBiomeLoader loader = new PipelineBiomeLoader(emptyRegistry(), index);
|
||||
int before = index.size();
|
||||
PipelineBiome first = load(loader, "OCEAN");
|
||||
assertSame(first, load(loader, "OCEAN"));
|
||||
assertEquals(before + 1, index.size());
|
||||
assertDoesNotThrow(() -> load(loader, "ALL"));
|
||||
index.seal();
|
||||
assertTrue(index.tag("OCEAN").has(first.getIntID()));
|
||||
assertTrue(index.tag("ALL").has(first.getIntID()));
|
||||
assertFalse(index.tag("OTHER").has(first.getIntID()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void selfHasNoTags() {
|
||||
assertEquals(0, PipelineBiome.self().getIntID());
|
||||
index.seal();
|
||||
assertFalse(index.tag("ALL").has(PipelineBiome.self().getIntID()));
|
||||
}
|
||||
|
||||
private static PipelineBiome load(PipelineBiomeLoader loader, String id) {
|
||||
return loader.load((AnnotatedType) null, id, null, null);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user