Skip to content

Commit

Permalink
Add support for default generator access in config
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixaurora committed Oct 26, 2023
1 parent f7ae5f0 commit 1015dd7
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.quiltmc.loader.api.QuiltLoader;
Expand Down Expand Up @@ -97,11 +96,8 @@ private JaneratorConfig createDefault() {
new GraphProperties(
Level.OVERWORLD,
new MultiGenOrganizer(
DefaultGenerators.createUnshadedOverworldGenerator(),
Map.of(
"grassy_mushroom", DefaultGenerators.createShadedOverworldGenerator(),
"rainbow_outline", DefaultGenerators.createOutlineOverworldGenerator()
),
DefaultGenerators.getOverworldGenerators(),
"default_overworld",
List.of(
new JaneratorLayer(
new FunctionGrapher(
Expand Down
26 changes: 16 additions & 10 deletions src/main/java/net/pixaurora/janerator/config/DefaultGenerators.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import net.minecraft.core.Holder;
import net.minecraft.core.HolderSet;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.Biomes;
import net.minecraft.world.level.biome.FixedBiomeSource;
Expand All @@ -22,6 +24,7 @@
import net.minecraft.world.level.levelgen.structure.StructureSet;
import net.pixaurora.janerator.RegistryCache;
import net.pixaurora.janerator.graphing.GraphFunctionDefinition;
import net.pixaurora.janerator.worldgen.generator.PreparedGenerators;
import net.pixaurora.janerator.worldgen.generator.SlantedFlatGenerator;
import net.pixaurora.janerator.worldgen.settings.SlantedFlatGeneratorSettings;

Expand All @@ -38,16 +41,6 @@ public static ChunkGenerator createShadedOverworldGenerator() {
);
}

public static ChunkGenerator createUnshadedOverworldGenerator() {
return createGenerator(
Biomes.MUSHROOM_FIELDS,
new FlatLayerInfo(1, Blocks.WHITE_CONCRETE),
new FlatLayerInfo(125, Blocks.GRAY_CONCRETE),

new FlatLayerInfo(1, Blocks.BEDROCK)
);
}

public static ChunkGenerator createOutlineOverworldGenerator() {
return new SlantedFlatGenerator(
new FixedBiomeSource(getBiome(Biomes.MUSHROOM_FIELDS)),
Expand Down Expand Up @@ -106,4 +99,17 @@ private static ChunkGenerator createGenerator(ResourceKey<Biome> biomeKey, FlatL
.withBiomeAndLayers(layers, optional, biome)
);
}

public static PreparedGenerators getOverworldGenerators() {
return new PreparedGenerators(
Map.of(
"grassy_mushroom", createShadedOverworldGenerator(),
"rainbow_outline", createOutlineOverworldGenerator()
),
Map.of(
"default_overworld", Level.OVERWORLD
),
RegistryCache.INSTANCE.getProvider(Registries.LEVEL_STEM)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@
public class MultiGenOrganizer {
public static final Codec<MultiGenOrganizer> CODEC = RecordCodecBuilder.create(
instance -> instance.group(
ChunkGenerator.CODEC.fieldOf("default_generator").forGetter(MultiGenOrganizer::getDefaultGenerator),
Codec.unboundedMap(Codec.STRING, ChunkGenerator.CODEC).fieldOf("other_generators").forGetter(MultiGenOrganizer::getOtherGenerators),
PreparedGenerators.CODEC.fieldOf("generators").forGetter(MultiGenOrganizer::getGenerators),
Codec.STRING.fieldOf("default_generator_key").forGetter(MultiGenOrganizer::getDefaultGeneratorKey),
JaneratorLayer.CODEC.listOf().fieldOf("layers").forGetter(MultiGenOrganizer::getLayers)
).apply(instance, MultiGenOrganizer::new)
);

private final ChunkGenerator defaultGenerator;
private final Map<String, ChunkGenerator> otherGenerators;
private final PreparedGenerators generators;
private final String defaultGeneratorKey;

private final List<JaneratorLayer> layers;

private int generatorCount;
private LoadingCache<ChunkPos, FullGeneratorLookup> selectionCache;

public MultiGenOrganizer(ChunkGenerator defaultGenerator, Map<String, ChunkGenerator> keyedGenerators, List<JaneratorLayer> layers) {
this.defaultGenerator = defaultGenerator;
this.otherGenerators = keyedGenerators;
public MultiGenOrganizer(PreparedGenerators generators, String defaultGeneratorKey, List<JaneratorLayer> layers) {
this.generators = generators;
this.defaultGeneratorKey = defaultGeneratorKey;

this.layers = layers;

Expand All @@ -60,12 +60,16 @@ public MultiGenOrganizer(ChunkGenerator defaultGenerator, Map<String, ChunkGener
.build(CacheLoader.from(this::createLookup));
}

public String getDefaultGeneratorKey() {
return this.defaultGeneratorKey;
}

public ChunkGenerator getDefaultGenerator() {
return this.defaultGenerator;
return this.generators.get(defaultGeneratorKey);
}

public Map<String, ChunkGenerator> getOtherGenerators() {
return this.otherGenerators;
public PreparedGenerators getGenerators() {
return this.generators;
}

public List<JaneratorLayer> getLayers() {
Expand All @@ -80,7 +84,7 @@ private void validateGeneratorKeys() {

List<String> missingKeys = layer.involvedGeneratorKeys()
.stream()
.filter(key -> this.generatorByKey(key) == null)
.filter(key -> this.generators.get(key) == null)
.distinct()
.toList();

Expand All @@ -106,16 +110,11 @@ private void validateGeneratorKeys() {

public List<ChunkGenerator> involvedGenerators() {
List<ChunkGenerator> involvedGenerators = new ArrayList<>();
involvedGenerators.add(this.defaultGenerator);
involvedGenerators.addAll(this.otherGenerators.values());
involvedGenerators.addAll(this.generators.getAll());

return involvedGenerators;
}

public ChunkGenerator generatorByKey(String generatorKey) {
return generatorKey == "default" ? defaultGenerator : otherGenerators.get(generatorKey);
}

private ChunkGenerator sampleOne(List<ChunkGenerator> regularShading, int sectionX, int sectionZ) {
Object2IntMap<ChunkGenerator> areaSample = new Object2IntOpenHashMap<>(this.generatorCount);

Expand Down Expand Up @@ -154,12 +153,12 @@ public List<ChunkGenerator> sampleForBiomes(List<ChunkGenerator> regularShading)
}

private FullGeneratorLookup createLookup(ChunkPos chunk) {
List<ChunkGenerator> generatorShading = new ArrayList<>(Collections.nCopies(256, this.defaultGenerator));
List<ChunkGenerator> generatorShading = new ArrayList<>(Collections.nCopies(256, this.getDefaultGenerator()));
List<JaneratorLayerData> layerShading = new ArrayList<>(Collections.nCopies(256, JaneratorLayerData.DEFAULT));

for (JaneratorLayer layer : this.layers) {
for (ShadeData shade : layer.shadesIn(chunk)) {
generatorShading.set(shade.index(), this.generatorByKey(shade.generatorKey()));
generatorShading.set(shade.index(), this.generators.get(shade.generatorKey()));
layerShading.set(shade.index(), layer);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package net.pixaurora.janerator.worldgen.generator;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

import net.minecraft.core.HolderGetter;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.RegistryOps;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.dimension.LevelStem;

public class PreparedGenerators {
public static final Codec<PreparedGenerators> CODEC = RecordCodecBuilder.create(
instance -> instance.group(
Codec.unboundedMap(Codec.STRING, ChunkGenerator.CODEC).fieldOf("defined").forGetter(PreparedGenerators::userGenerators),
Codec.unboundedMap(Codec.STRING, ResourceKey.codec(Registries.DIMENSION)).fieldOf("default_generator_dimensions").forGetter(PreparedGenerators::defaultGeneratorDimensions),
RegistryOps.retrieveGetter(Registries.LEVEL_STEM)
).apply(instance, PreparedGenerators::new)
);

public final Map<String, ChunkGenerator> userGenerators;
public final Map<String, ResourceKey<Level>> defaultGenerators;

public final Map<String, ChunkGenerator> allGenerators;

public PreparedGenerators(Map<String, ChunkGenerator> userGenerators,
Map<String, ResourceKey<Level>> defaultGeneratorDimensions,
HolderGetter<LevelStem> dimensionGeneratorRegistry) {
this.userGenerators = userGenerators;
this.defaultGenerators = defaultGeneratorDimensions;

this.allGenerators = new HashMap<>();

this.allGenerators.putAll(userGenerators);
for (Map.Entry<String, ResourceKey<Level>> specifiedDefault : defaultGeneratorDimensions.entrySet()) {
ResourceKey<LevelStem> generatorLocation = Registries.levelToLevelStem(specifiedDefault.getValue());
LevelStem defaultGenerator = dimensionGeneratorRegistry.get(generatorLocation).get().value();

this.allGenerators.put(specifiedDefault.getKey(), defaultGenerator.generator());
}
}

public Map<String, ChunkGenerator> userGenerators() {
return this.userGenerators;
}

public Map<String, ResourceKey<Level>> defaultGeneratorDimensions() {
return this.defaultGenerators;
}

public ChunkGenerator get(String name) {
return this.allGenerators.get(name);
}

public Collection<ChunkGenerator> getAll() {
return this.allGenerators.values();
}
}

0 comments on commit 1015dd7

Please sign in to comment.