Skip to content

Commit

Permalink
Optimize outline finding in Graphed Chunk
Browse files Browse the repository at this point in the history
This optimization is pretty simple, simply pull the graphs of
surrounding chunks from the caching ChunkGrapher instead of re-graphing
them.
  • Loading branch information
Pixaurora committed Oct 11, 2023
1 parent 2b1095b commit f5b68d1
Showing 1 changed file with 16 additions and 23 deletions.
39 changes: 16 additions & 23 deletions src/main/java/net/pixaurora/janerator/graphing/GraphedChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.IntStream;

Expand All @@ -20,21 +19,22 @@ public class GraphedChunk {
public static boolean UNSHADED = false;

private final List<Boolean> shading;
private final ChunkPos pos;
private final ChunkPos chunk;

private final ChunkGrapher grapher;

private GraphedChunk(ChunkGrapher grapher, ChunkPos pos, List<Boolean> shading) {
this.grapher = grapher;
this.pos = pos;
private GraphedChunk(ChunkGrapher grapher, ChunkPos chunk, List<Boolean> shading) {
this.chunk = chunk;
this.shading = shading;

this.grapher = grapher;
}

public static <E> List<E> doGraphing(Function<Coordinate, E> graphEvaluator, ChunkPos pos) {
public static <E> List<E> doGraphing(Function<Coordinate, E> graphEvaluator, ChunkPos chunk) {
List<E> graph = new ArrayList<>();

int startX = pos.getMinBlockX();
int startZ = pos.getMinBlockZ();
int startX = chunk.getMinBlockX();
int startZ = chunk.getMinBlockZ();

int endX = startX + 16;
int endZ = startZ + 16;
Expand All @@ -48,8 +48,8 @@ public static <E> List<E> doGraphing(Function<Coordinate, E> graphEvaluator, Chu
return graph;
}

public GraphedChunk(ChunkGrapher grapher, ChunkPos pos) {
this(grapher, pos, doGraphing(grapher::isPointShaded, pos));
public GraphedChunk(ChunkGrapher grapher, ChunkPos chunk) {
this(grapher, chunk, doGraphing(grapher::isPointShaded, chunk));
}

public static GraphedChunk allUnshaded(ChunkGrapher grapher, ChunkPos pos) {
Expand Down Expand Up @@ -100,8 +100,7 @@ private List<ChunkGenerator> getBiomeScaleMap(MultiGenerator multiGenerator) {
}
}

boolean sampledShade = generatorSample.entrySet()
.stream()
boolean sampledShade = generatorSample.entrySet().stream()
.max((entry1, entry2) -> entry1.getValue() - entry2.getValue())
.get().getKey();

Expand All @@ -117,7 +116,7 @@ private List<ChunkGenerator> getBiomeScaleMap(MultiGenerator multiGenerator) {
private List<Coordinate> findOutlinedPortion() {
List<Coordinate> outlinedPortion = new ArrayList<>();

Map<ChunkPos, GraphedChunk> neighboringChunks = new HashMap<>(4);
Map<ChunkPos, GraphedChunk> neighboringChunks = new HashMap<>(8);

for (Coordinate coordinate : GraphingUtils.getIndices(this.shading, GraphedChunk.SHADED)) {
boolean hasContrastingNeighbor = coordinate.getNeighbors()
Expand All @@ -127,20 +126,14 @@ private List<Coordinate> findOutlinedPortion() {
boolean neighborShading;

if (neighbor.isLegal()) {
neighborShading = this.shading.get(neighbor.toListIndex());
neighborShading = this.isShaded(neighbor);
} else {
int deltaX = neighbor.x() < 0 ? -1 : neighbor.x() < 16 ? 0 : 1;
int deltaZ = neighbor.z() < 0 ? -1 : neighbor.z() < 16 ? 0 : 1;
ChunkPos neighborPos = new ChunkPos(this.pos.x + deltaX, this.pos.z + deltaZ);

GraphedChunk neighboringGraphedArea = neighboringChunks.get(neighborPos);

if (Objects.isNull(neighboringGraphedArea)) {
neighboringGraphedArea = new GraphedChunk(this.grapher, neighborPos);
neighboringChunks.put(neighborPos, neighboringGraphedArea);
}
ChunkPos neighborChunk = new ChunkPos(this.chunk.x + deltaX, this.chunk.z + deltaZ);

neighborShading = neighboringGraphedArea.shading.get(neighbor.makeLegal().toListIndex());
GraphedChunk neighborChunkGraph = neighboringChunks.computeIfAbsent(neighborChunk, pos -> this.grapher.getChunkGraph(pos));
neighborShading = neighborChunkGraph.isShaded(neighbor.makeLegal());
}

return neighborShading != GraphedChunk.SHADED;
Expand Down

0 comments on commit f5b68d1

Please sign in to comment.