Skip to content

Commit

Permalink
separate generators from the shades that use them in config
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixaurora committed Oct 17, 2023
1 parent 6a28d9a commit e528e94
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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 @@ -96,6 +97,11 @@ private JaneratorConfig createDefault() {
new GraphProperties(
Level.OVERWORLD,
new MultiGenOrganizer(
DefaultGenerators.createUnshadedOverworldGenerator(),
Map.of(
"grassy_mushroom", DefaultGenerators.createShadedOverworldGenerator(),
"rainbow_outline", DefaultGenerators.createOutlineOverworldGenerator()
),
List.of(
new JaneratorLayer(
new FunctionGrapher(
Expand All @@ -111,12 +117,11 @@ private JaneratorConfig createDefault() {
"(z - x * tan(angle)) * sgn(tan(angle) * csc(angle)) > 0"
)
),
new NormalShading(DefaultGenerators.createShadedOverworldGenerator()),
Optional.of(new NormalShading(DefaultGenerators.createOutlineOverworldGenerator())),
new NormalShading("grassy_mushroom"),
Optional.of(new NormalShading("rainbow_outline")),
false
)
),
DefaultGenerators.createUnshadedOverworldGenerator()
)
)

)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ public Optional<ShadingMethod> getOutlineShading() {
return outlineShading;
}

public List<String> involvedGeneratorKeys() {
List<String> keys = new ArrayList<>(this.shadingMethod.involvedGeneratorKeys());
this.outlineShading.ifPresent(outlineShading -> keys.addAll(outlineShading.involvedGeneratorKeys()));

return keys;
}

@Override
public boolean generateStructures() {
return this.generateStructures;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

import net.minecraft.world.level.chunk.ChunkGenerator;
import net.pixaurora.janerator.config.SerialType;
import net.pixaurora.janerator.graphing.Coordinate;

public record NormalShading(ChunkGenerator generator) implements SimpleShadingMethod {
public record NormalShading(String generatorKey) implements SimpleShadingMethod {
public static final Codec<NormalShading> CODEC = RecordCodecBuilder.create(
instance -> instance.group(
ChunkGenerator.CODEC.fieldOf("generator").forGetter(NormalShading::generator)
Codec.STRING.fieldOf("generator_key").forGetter(NormalShading::generatorKey)
).apply(instance, NormalShading::new)
);
public static final SerialType<ShadingMethod> TYPE = new SerialType<>("simple", CODEC);
Expand All @@ -24,11 +23,11 @@ public SerialType<? extends ShadingMethod> type() {

@Override
public ShadeData getShade(Coordinate pos) {
return new ShadeData(pos, this.generator);
return new ShadeData(pos, this.generatorKey);
}

@Override
public List<ChunkGenerator> involvedGenerators() {
return List.of(this.generator);
public List<String> involvedGeneratorKeys() {
return List.of(this.generatorKey);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package net.pixaurora.janerator.shade.method;

import net.minecraft.world.level.chunk.ChunkGenerator;
import net.pixaurora.janerator.graphing.Coordinate;

public record ShadeData(Coordinate location, ChunkGenerator generator) {
public record ShadeData(Coordinate location, String generatorKey) {
public int index() {
return this.location.toListIndex();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;

import net.minecraft.world.level.chunk.ChunkGenerator;
import net.pixaurora.janerator.config.SerialType;
import net.pixaurora.janerator.config.SpecifiesType;
import net.pixaurora.janerator.graphing.Coordinate;
Expand All @@ -16,13 +15,13 @@ public interface ShadingMethod extends SpecifiesType<ShadingMethod> {
public static final List<SerialType<ShadingMethod>> TYPES = new ArrayList<>(List.of(NormalShading.TYPE));
public static final Codec<ShadingMethod> BASE_CODEC = new SerialType.Group<>("Shading method", TYPES).dispatchCodec();
public static final Codec<ShadingMethod> CODEC = Codec.either(
ChunkGenerator.CODEC,
Codec.STRING,
ShadingMethod.BASE_CODEC
).xmap(
either -> either.map(NormalShading::new, Function.identity()),
shading -> {
if (shading instanceof NormalShading basicShading) {
return Either.left(basicShading.generator());
return Either.left(basicShading.generatorKey());
} else {
return Either.right(shading);
}
Expand All @@ -31,9 +30,9 @@ public interface ShadingMethod extends SpecifiesType<ShadingMethod> {

public List<ShadeData> shadeIn(List<Coordinate> points);

public List<ChunkGenerator> involvedGenerators();
public List<String> involvedGeneratorKeys();

public default int generatorCount() {
return this.involvedGenerators().size();
return this.involvedGeneratorKeys().size();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

Expand All @@ -17,6 +19,7 @@
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.pixaurora.janerator.config.GraphingConfigException;
import net.pixaurora.janerator.graphing.Coordinate;
import net.pixaurora.janerator.shade.JaneratorLayerData;
import net.pixaurora.janerator.shade.JaneratorLayer;
Expand All @@ -26,20 +29,28 @@
public class MultiGenOrganizer {
public static final Codec<MultiGenOrganizer> CODEC = RecordCodecBuilder.create(
instance -> instance.group(
JaneratorLayer.CODEC.listOf().fieldOf("layers").forGetter(MultiGenOrganizer::getLayers),
ChunkGenerator.CODEC.fieldOf("default_generator").forGetter(MultiGenOrganizer::getDefaultGenerator)
ChunkGenerator.CODEC.fieldOf("default_generator").forGetter(MultiGenOrganizer::getDefaultGenerator),
Codec.unboundedMap(Codec.STRING, ChunkGenerator.CODEC).fieldOf("other_generators").forGetter(MultiGenOrganizer::getOtherGenerators),
JaneratorLayer.CODEC.listOf().fieldOf("layers").forGetter(MultiGenOrganizer::getLayers)
).apply(instance, MultiGenOrganizer::new)
);

private final List<JaneratorLayer> layers;
private final ChunkGenerator defaultGenerator;
private final Map<String, ChunkGenerator> otherGenerators;

private final List<JaneratorLayer> layers;

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

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

this.layers = layers;
this.defaultGenerator = defaultShade;

this.validateGeneratorKeys();


this.generatorCount = this.involvedGenerators().size();

Expand All @@ -49,26 +60,62 @@ public MultiGenOrganizer(List<JaneratorLayer> layers, ChunkGenerator defaultShad
.build(CacheLoader.from(this::createLookup));
}

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

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

public List<JaneratorLayer> getLayers() {
return layers;
return this.layers;
}

public ChunkGenerator getDefaultGenerator() {
return defaultGenerator;
private void validateGeneratorKeys() {
Map<Integer, List<String>> layerErrors = new HashMap<>();

for (int layerNumber = 0; layerNumber < this.layers.size(); layerNumber++) {
JaneratorLayer layer = this.layers.get(layerNumber);

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

if (missingKeys.size() > 0) {
layerErrors.put(layerNumber, missingKeys);
}
}

if (layerErrors.size() > 0) {
throw new GraphingConfigException(
String.format(
"Generator definitions are missing for the following keys: %s",
String.join(
"\n",
layerErrors.entrySet().stream()
.map(entry -> String.format("In Layer %d: [%s]", entry.getKey(), String.join(",", entry.getValue())))
.toList()
)
)
);
}
}

public List<ChunkGenerator> involvedGenerators() {
List<ChunkGenerator> involvedGenerators = new ArrayList<>();
involvedGenerators.add(this.defaultGenerator);
involvedGenerators.addAll(
this.layers.stream()
.flatMap(layer -> layer.getShading().involvedGenerators().stream())
.toList()
);
involvedGenerators.addAll(this.otherGenerators.values());

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 @@ -112,7 +159,7 @@ private FullGeneratorLookup createLookup(ChunkPos chunk) {

for (JaneratorLayer layer : this.layers) {
for (ShadeData shade : layer.shadesIn(chunk)) {
generatorShading.set(shade.index(), shade.generator());
generatorShading.set(shade.index(), this.generatorByKey(shade.generatorKey()));
layerShading.set(shade.index(), layer);
}
}
Expand Down

0 comments on commit e528e94

Please sign in to comment.