Skip to content

Commit

Permalink
add a growing tiles graph type
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixaurora committed Sep 1, 2023
1 parent 252350a commit 80d9db2
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,32 @@
public class CustomGrapher extends ChunkGrapher {
public static final Codec<CustomGrapher> 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<GraphFunction> function;
private GraphFunctionDefinition graphDefinition;
private ThreadLocal<GraphFunction> 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
public GrapherType type() {
return GrapherType.CUSTOM;
}

public GraphFunctionDefinition getSettings() {
return settings;
public GraphFunctionDefinition getGraphDefinition() {
return graphDefinition;
}

public static class LocalGrapher {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ public class GrapherType {
public static Codec<GrapherType> 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<GrapherType> TYPES = List.of(
CUSTOM
CUSTOM,
GROWING_TILES
);

private final String name;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<GrowingTileGrapher> 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<GraphFunction> 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;
}
}

0 comments on commit 80d9db2

Please sign in to comment.