diff --git a/src/main/java/net/pixaurora/janerator/graphing/grapher/CustomGrapher.java b/src/main/java/net/pixaurora/janerator/graphing/grapher/CustomGrapher.java index b2cd192..0e44cde 100644 --- a/src/main/java/net/pixaurora/janerator/graphing/grapher/CustomGrapher.java +++ b/src/main/java/net/pixaurora/janerator/graphing/grapher/CustomGrapher.java @@ -13,23 +13,23 @@ public class CustomGrapher extends ChunkGrapher { public static final Codec CODEC = RecordCodecBuilder.create( instance -> instance.group( - GraphFunctionDefinition.BIVARIATE_CODEC.fieldOf("graph(x, z)").forGetter(CustomGrapher::getSettings) + GraphFunctionDefinition.BIVARIATE_CODEC.fieldOf("graph(x, z)").forGetter(CustomGrapher::getGraphDefinition) ).apply(instance, CustomGrapher::new) ); - private GraphFunctionDefinition settings; - private ThreadLocal function; + private GraphFunctionDefinition graphDefinition; + private ThreadLocal graphFunction; - public CustomGrapher(GraphFunctionDefinition settings) { + public CustomGrapher(GraphFunctionDefinition graphDefinition) { super(); - this.settings = settings; - this.function = ThreadLocal.withInitial(() -> GraphFunction.fromDefinition(this.settings)); + this.graphDefinition = graphDefinition; + this.graphFunction = ThreadLocal.withInitial(() -> GraphFunction.fromDefinition(this.graphDefinition)); } @Override public boolean isPointShaded(int x, int z) { - return this.function.get().evaluate(x, z) == 1.0; + return this.graphFunction.get().evaluate(x, z) == 1.0; } @Override @@ -37,8 +37,8 @@ public GrapherType type() { return GrapherType.CUSTOM; } - public GraphFunctionDefinition getSettings() { - return settings; + public GraphFunctionDefinition getGraphDefinition() { + return graphDefinition; } public static class LocalGrapher { diff --git a/src/main/java/net/pixaurora/janerator/graphing/grapher/GrapherType.java b/src/main/java/net/pixaurora/janerator/graphing/grapher/GrapherType.java index faa3239..fff606c 100644 --- a/src/main/java/net/pixaurora/janerator/graphing/grapher/GrapherType.java +++ b/src/main/java/net/pixaurora/janerator/graphing/grapher/GrapherType.java @@ -11,8 +11,11 @@ public class GrapherType { public static Codec CODEC = Codec.STRING.xmap(GrapherType::fromName, GrapherType::getName); public static GrapherType CUSTOM = new GrapherType("custom", CustomGrapher.CODEC); + public static GrapherType GROWING_TILES = new GrapherType("growing_tiles", GrowingTileGrapher.CODEC); + public static List TYPES = List.of( - CUSTOM + CUSTOM, + GROWING_TILES ); private final String name; diff --git a/src/main/java/net/pixaurora/janerator/graphing/grapher/GrowingTileGrapher.java b/src/main/java/net/pixaurora/janerator/graphing/grapher/GrowingTileGrapher.java new file mode 100644 index 0000000..6b9da32 --- /dev/null +++ b/src/main/java/net/pixaurora/janerator/graphing/grapher/GrowingTileGrapher.java @@ -0,0 +1,56 @@ +package net.pixaurora.janerator.graphing.grapher; + +import com.mojang.serialization.Codec; +import com.mojang.serialization.codecs.RecordCodecBuilder; + +import net.pixaurora.janerator.graphing.GraphFunction; +import net.pixaurora.janerator.graphing.GraphFunctionDefinition; + +public class GrowingTileGrapher extends CustomGrapher { + public static final Codec CODEC = RecordCodecBuilder.create( + instance -> instance.group( + GraphFunctionDefinition.BIVARIATE_CODEC.fieldOf("graph(x, z)").forGetter(GrowingTileGrapher::getTileDefinition), + GraphFunctionDefinition.UNIVARIATE_CODEC.fieldOf("tileSize(v)").forGetter(GrowingTileGrapher::getGraphDefinition) + ).apply(instance, GrowingTileGrapher::new) + ); + + private GraphFunctionDefinition tileDefinition; + private ThreadLocal tileFunction; + + public GrowingTileGrapher(GraphFunctionDefinition graphDefinition, GraphFunctionDefinition tileGrowthDefinition) { + super(graphDefinition); + + this.tileDefinition = tileGrowthDefinition; + this.tileFunction = ThreadLocal.withInitial(() -> GraphFunction.fromDefinition(this.tileDefinition)); + } + + public int inverseOfTileGrowth(int value) { + int tileSizeSum = 0; + int guess = 0; + + while (tileSizeSum < value) { + tileSizeSum += Math.max(1, Math.abs(this.tileFunction.get().evaluate(guess))); + guess++; + } + + return guess - 1; + } + + public int convertToTile(int value) { + return (int) Math.signum(value) * this.inverseOfTileGrowth(Math.abs(value)); + } + + @Override + public boolean isPointShaded(int x, int z) { + return super.isPointShaded(this.convertToTile(x), this.convertToTile(z)); + } + + @Override + public GrapherType type() { + return GrapherType.GROWING_TILES; + } + + public GraphFunctionDefinition getTileDefinition() { + return this.tileDefinition; + } +}