diff --git a/src/main/java/net/pixaurora/janerator/graphing/Coordinate.java b/src/main/java/net/pixaurora/janerator/graphing/Coordinate.java index 079748b..d58e981 100644 --- a/src/main/java/net/pixaurora/janerator/graphing/Coordinate.java +++ b/src/main/java/net/pixaurora/janerator/graphing/Coordinate.java @@ -3,9 +3,15 @@ import java.util.ArrayList; import java.util.List; +import net.minecraft.core.BlockPos; import net.minecraft.core.Direction8; +import net.minecraft.world.level.ChunkPos; public record Coordinate(int x, int z) { + public Coordinate(BlockPos pos) { + this(pos.getX(), pos.getZ()); + } + public int toListIndex(int chunkLength) { return chunkLength * GraphingUtils.mod(this.x, chunkLength) + GraphingUtils.mod(this.z, chunkLength); } @@ -14,6 +20,14 @@ public int toListIndex() { return this.toListIndex(16); } + public ChunkPos toChunkPos(int chunkLength) { + return new ChunkPos(Math.floorDiv(this.x, chunkLength), Math.floorDiv(this.z, chunkLength)); + } + + public ChunkPos toChunkPos() { + return this.toChunkPos(16); + } + public static Coordinate fromListIndex(int index, int chunkLength) { return new Coordinate(Math.floorDiv(index, chunkLength), GraphingUtils.mod(index, chunkLength)); } diff --git a/src/main/java/net/pixaurora/janerator/graphing/GraphedChunk.java b/src/main/java/net/pixaurora/janerator/graphing/GraphedChunk.java index 703173a..95dce81 100644 --- a/src/main/java/net/pixaurora/janerator/graphing/GraphedChunk.java +++ b/src/main/java/net/pixaurora/janerator/graphing/GraphedChunk.java @@ -60,6 +60,10 @@ public static GraphedChunk allUnshaded(ChunkGrapher grapher, ChunkPos pos) { ); } + public boolean isShaded(Coordinate pos) { + return this.shading.get(pos.toListIndex()); + } + private List getBlockScaleMap(MultiGenerator multiGenerator) { List generatorMap = new ArrayList<>( IntStream.range(0, 256) diff --git a/src/main/java/net/pixaurora/janerator/graphing/grapher/CachingGrapher.java b/src/main/java/net/pixaurora/janerator/graphing/grapher/CachingGrapher.java new file mode 100644 index 0000000..8f6c48e --- /dev/null +++ b/src/main/java/net/pixaurora/janerator/graphing/grapher/CachingGrapher.java @@ -0,0 +1,31 @@ +package net.pixaurora.janerator.graphing.grapher; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; + +import net.minecraft.world.level.ChunkPos; +import net.pixaurora.janerator.graphing.GraphedChunk; + +public abstract class CachingGrapher implements ChunkGrapher { + private LoadingCache cache; + + public CachingGrapher() { + this.cache = CacheBuilder.newBuilder() + .expireAfterAccess(60, TimeUnit.SECONDS) + .maximumSize(1024) + .build(CacheLoader.from(chunk -> new GraphedChunk(this, chunk))); + } + + @Override + public GraphedChunk getChunkGraph(ChunkPos chunk) { + try { + return this.cache.get(chunk); + } catch (ExecutionException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/java/net/pixaurora/janerator/graphing/grapher/ChunkGrapher.java b/src/main/java/net/pixaurora/janerator/graphing/grapher/ChunkGrapher.java index 3a8f81c..714e558 100644 --- a/src/main/java/net/pixaurora/janerator/graphing/grapher/ChunkGrapher.java +++ b/src/main/java/net/pixaurora/janerator/graphing/grapher/ChunkGrapher.java @@ -1,57 +1,26 @@ package net.pixaurora.janerator.graphing.grapher; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; - -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.CacheLoader; -import com.google.common.cache.LoadingCache; import com.mojang.serialization.Codec; import net.minecraft.core.BlockPos; import net.minecraft.world.level.ChunkPos; import net.pixaurora.janerator.graphing.Coordinate; import net.pixaurora.janerator.graphing.GraphedChunk; -import net.pixaurora.janerator.graphing.GraphingUtils; -public abstract class ChunkGrapher { +public interface ChunkGrapher { public static Codec CODEC = GrapherType.CODEC.dispatch("type", ChunkGrapher::type, GrapherType::getAppliedCodec); - private LoadingCache cache; - - public ChunkGrapher() { - this.cache = CacheBuilder.newBuilder() - .expireAfterWrite(60, TimeUnit.SECONDS) - .maximumSize(1024) - .build(CacheLoader.from(this::graphChunk)); - } + public GrapherType type(); - public abstract boolean isPointShaded(int x, int z); + public GraphedChunk getChunkGraph(ChunkPos pos); - public abstract GrapherType type(); - - public boolean isPointShaded(Coordinate coord) { - return this.isPointShaded(coord.x(), coord.z()); - } - - public boolean isPointShaded(BlockPos pos) { - return this.isPointShaded(pos.getX(), pos.getZ()); - } - - private GraphedChunk graphChunk(ChunkPos pos) { - return new GraphedChunk(this, pos); - } + public boolean isPointShaded(Coordinate pos); - public GraphedChunk getChunkGraph(ChunkPos pos) { - try { - return this.cache.get(pos); - } catch (ExecutionException e) { - throw new RuntimeException(e); - } + public default boolean isPointShaded(int x, int z) { + return this.isPointShaded(new Coordinate(x, z)); } - public CompletableFuture scheduleChunkGraphing(ChunkPos pos) { - return CompletableFuture.supplyAsync(() -> this.getChunkGraph(pos), GraphingUtils.threadPool); + public default boolean isPointShaded(BlockPos pos) { + return this.isPointShaded(new Coordinate(pos)); } } 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 81db9e4..ffd7317 100644 --- a/src/main/java/net/pixaurora/janerator/graphing/grapher/CustomGrapher.java +++ b/src/main/java/net/pixaurora/janerator/graphing/grapher/CustomGrapher.java @@ -1,16 +1,13 @@ package net.pixaurora.janerator.graphing.grapher; -import java.util.ArrayList; -import java.util.List; - import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; +import net.pixaurora.janerator.graphing.Coordinate; import net.pixaurora.janerator.graphing.GraphFunction; import net.pixaurora.janerator.graphing.GraphFunctionDefinition; -import net.pixaurora.janerator.graphing.instruction.Instruction; -public class CustomGrapher extends ChunkGrapher { +public class CustomGrapher extends CachingGrapher { public static final Codec CODEC = RecordCodecBuilder.create( instance -> instance.group( GraphFunctionDefinition.BIVARIATE_CODEC.fieldOf("graph(x, z)").forGetter(CustomGrapher::getGraphDefinition) @@ -21,47 +18,21 @@ public class CustomGrapher extends ChunkGrapher { protected ThreadLocal graphFunction; public CustomGrapher(GraphFunctionDefinition graphDefinition) { - super(); - this.graphDefinition = graphDefinition; this.graphFunction = ThreadLocal.withInitial(() -> GraphFunction.fromDefinition(this.graphDefinition)); } + public GraphFunctionDefinition getGraphDefinition() { + return this.graphDefinition; + } + @Override - public boolean isPointShaded(int x, int z) { - return this.graphFunction.get().evaluate(x, z) == 1.0; + public boolean isPointShaded(Coordinate pos) { + return this.graphFunction.get().evaluate(pos.x(), pos.z()) == 1.0; } @Override public GrapherType type() { return GrapherType.CUSTOM; } - - public GraphFunctionDefinition getGraphDefinition() { - return graphDefinition; - } - - public static class LocalGrapher { - private List startingVariables; - private List appliedSteps; - private int returnIndex; - - public LocalGrapher(List startingVariables, List appliedSteps, int returnIndex) { - this.startingVariables = startingVariables; - this.appliedSteps = appliedSteps; - this.returnIndex = returnIndex; - } - - public boolean isShaded(double x, double z) { - List variables = new ArrayList<>(this.startingVariables); - variables.set(0, x); - variables.set(1, z); - - for (Instruction step : this.appliedSteps) { - step.execute(variables); - } - - return variables.get(this.returnIndex) == 1.0; - } - } } diff --git a/src/main/java/net/pixaurora/janerator/graphing/grapher/GrowingTileGrapher.java b/src/main/java/net/pixaurora/janerator/graphing/grapher/GrowingTileGrapher.java index 4520038..957681a 100644 --- a/src/main/java/net/pixaurora/janerator/graphing/grapher/GrowingTileGrapher.java +++ b/src/main/java/net/pixaurora/janerator/graphing/grapher/GrowingTileGrapher.java @@ -6,25 +6,31 @@ import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; +import net.minecraft.core.BlockPos; +import net.minecraft.world.level.ChunkPos; +import net.pixaurora.janerator.graphing.Coordinate; import net.pixaurora.janerator.graphing.GraphFunction; import net.pixaurora.janerator.graphing.GraphFunctionDefinition; +import net.pixaurora.janerator.graphing.GraphedChunk; import net.pixaurora.janerator.graphing.grapher.tile.TileData; -public class GrowingTileGrapher extends CustomGrapher { +public class GrowingTileGrapher implements ChunkGrapher { 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) + GraphFunctionDefinition.UNIVARIATE_CODEC.fieldOf("tile_size(v)").forGetter(GrowingTileGrapher::getGraphDefinition) ).apply(instance, GrowingTileGrapher::new) ); public static final int MAX_VALUE = (int) Math.pow(2, 25); + private CustomGrapher stretchedGrapher; + private GraphFunctionDefinition tileDefinition; private List tileSums; public GrowingTileGrapher(GraphFunctionDefinition graphDefinition, GraphFunctionDefinition tileGrowthDefinition) { - super(graphDefinition); + this.stretchedGrapher = new CustomGrapher(graphDefinition); this.tileDefinition = tileGrowthDefinition; this.tileSums = new ArrayList<>(); @@ -45,6 +51,10 @@ public GrowingTileGrapher(GraphFunctionDefinition graphDefinition, GraphFunction } } + public GraphFunctionDefinition getGraphDefinition() { + return this.stretchedGrapher.getGraphDefinition(); + } + public GraphFunctionDefinition getTileDefinition() { return this.tileDefinition; } @@ -67,12 +77,25 @@ public TileData convertToTile(int realPosition) { throw new RuntimeException(String.format("Value %d is above the limit of stored values %d!", realPosition, MAX_VALUE)); } + private boolean isTileShaded(Coordinate tilePos) { + GraphedChunk graph = this.stretchedGrapher.getChunkGraph(new ChunkPos(new BlockPos(tilePos.x(), 0, tilePos.z()))); + + return graph.isShaded(tilePos.makeLegal()); + } + @Override - public boolean isPointShaded(int x, int z) { - TileData tileX = this.convertToTile(x); - TileData tileZ = this.convertToTile(z); + public boolean isPointShaded(Coordinate pos) { + TileData tileX = this.convertToTile(pos.x()); + TileData tileZ = this.convertToTile(pos.z()); + + Coordinate tilePos = new Coordinate(tileX.pos(), tileZ.pos()); - return this.graphFunction.get().evaluate(tileX.pos(), tileZ.pos()) == 1.0; + return this.isTileShaded(tilePos); + } + + @Override + public GraphedChunk getChunkGraph(ChunkPos chunk) { + return new GraphedChunk(this, chunk); } @Override