Skip to content

Commit

Permalink
allow for only-adjacent-neighbor outlines
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixaurora committed Oct 27, 2023
1 parent 678e690 commit 493689c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ private JaneratorConfig createDefault() {
),
new NormalShading("grassy_mushroom"),
Optional.of(new NormalShading("rainbow_outline")),
false,
false
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ public Coordinate offsetIn(Direction8 direction) {
return this.offset(direction.getStepX(), direction.getStepZ());
}

public List<Coordinate> getNeighbors() {
return this.getNeighbors(Direction8.values());
}

public List<Coordinate> getNeighbors(Direction8... neighborDirections) {
public List<Coordinate> getNeighbors(Direction8[] neighborDirections) {
List<Coordinate> neighbors = new ArrayList<>(neighborDirections.length);

for (Direction8 direction : neighborDirections) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Map;
import java.util.function.Function;

import net.minecraft.core.Direction8;
import net.minecraft.world.level.ChunkPos;
import net.pixaurora.janerator.graphing.grapher.ChunkGrapher;

Expand Down Expand Up @@ -70,13 +71,13 @@ public List<Coordinate> getShaded() {
return shadedCoordinates;
}

public List<Coordinate> getOutlines() {
public List<Coordinate> getOutlines(Direction8[] neighborsToCheck) {
List<Coordinate> outlinedPortion = new ArrayList<>();

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

for (Coordinate coordinate : this.getShaded()) {
boolean hasContrastingNeighbor = coordinate.getNeighbors()
boolean hasContrastingNeighbor = coordinate.getNeighbors(neighborsToCheck)
.stream()
.anyMatch(
neighbor -> {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/net/pixaurora/janerator/graphing/OutlineType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.pixaurora.janerator.graphing;

import net.minecraft.core.Direction8;

public enum OutlineType {
ADJACENT_ONLY(Direction8.WEST, Direction8.SOUTH, Direction8.NORTH, Direction8.EAST),
INCLUDES_DIAGONALS(Direction8.values());

private final Direction8[] neighborsToCheck;

private OutlineType(Direction8... neighborsToCheck) {
this.neighborsToCheck = neighborsToCheck;
}

public Direction8[] getNeighborsToCheck() {
return this.neighborsToCheck;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.pixaurora.janerator.graphing.GraphFunction;
import net.pixaurora.janerator.graphing.GraphFunctionDefinition;
import net.pixaurora.janerator.graphing.GraphedChunk;
import net.pixaurora.janerator.graphing.OutlineType;

public class GrowingTileGrapher implements ChunkGrapher {
public static final Codec<GrowingTileGrapher> CODEC = RecordCodecBuilder.create(
Expand Down Expand Up @@ -130,8 +131,6 @@ public boolean isCornerShaded(Coordinate realPos, Tile xTile, Tile zTile, Coordi
return this.cornerFunction.get().evaluate(cornerX, cornerZ) == 1.0;
}

public static final Direction8[] CROSS = new Direction8[]{Direction8.WEST, Direction8.SOUTH, Direction8.NORTH, Direction8.EAST};

@Override
public boolean isPointShaded(Coordinate pos) {
Tile xTile = this.convertToTile(pos.x());
Expand All @@ -144,7 +143,7 @@ public boolean isPointShaded(Coordinate pos) {

Coordinate offset = noOffset;
int nudges = 0;
for (Direction8 direction : CROSS) {
for (Direction8 direction : OutlineType.INCLUDES_DIAGONALS.getNeighborsToCheck()) {
Coordinate neighborPos = tilePos.offsetIn(direction);

if (shade != this.isTileShaded(neighborPos)) {
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/net/pixaurora/janerator/shade/JaneratorLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import net.minecraft.world.level.ChunkPos;
import net.pixaurora.janerator.graphing.GraphedChunk;
import net.pixaurora.janerator.graphing.OutlineType;
import net.pixaurora.janerator.graphing.grapher.ChunkGrapher;
import net.pixaurora.janerator.shade.method.ShadeData;
import net.pixaurora.janerator.shade.method.ShadingMethod;
Expand All @@ -19,6 +20,7 @@ public class JaneratorLayer implements JaneratorLayerData {
ChunkGrapher.CODEC.fieldOf("grapher").forGetter(JaneratorLayer::getGrapher),
ShadingMethod.CODEC.fieldOf("shading").forGetter(JaneratorLayer::getShading),
ShadingMethod.CODEC.optionalFieldOf("outline_shading").forGetter(JaneratorLayer::getOutlineShading),
Codec.BOOL.fieldOf("outlines_include_diagonal").orElse(false).forGetter(JaneratorLayer::outlinesIncludeDiagonals),
Codec.BOOL.fieldOf("generate_structures").orElse(false).forGetter(JaneratorLayer::generateStructures)
).apply(instance, JaneratorLayer::new)
);
Expand All @@ -27,14 +29,16 @@ public class JaneratorLayer implements JaneratorLayerData {

private final ShadingMethod shadingMethod;
private final Optional<ShadingMethod> outlineShading;
private final OutlineType outlinesIncludeDiagonal;

private final boolean generateStructures;

public JaneratorLayer(ChunkGrapher grapher, ShadingMethod shadingMethod, Optional<ShadingMethod> outlineShading, boolean generateStructures) {
public JaneratorLayer(ChunkGrapher grapher, ShadingMethod shadingMethod, Optional<ShadingMethod> outlineShading, boolean outlinesIncludeDiagonal, boolean generateStructures) {
this.grapher = grapher;

this.shadingMethod = shadingMethod;
this.outlineShading = outlineShading;
this.outlinesIncludeDiagonal = outlinesIncludeDiagonal ? OutlineType.INCLUDES_DIAGONALS : OutlineType.ADJACENT_ONLY;

this.generateStructures = generateStructures;
}
Expand All @@ -51,6 +55,10 @@ public Optional<ShadingMethod> getOutlineShading() {
return outlineShading;
}

public boolean outlinesIncludeDiagonals() {
return outlinesIncludeDiagonal == OutlineType.INCLUDES_DIAGONALS;
}

public List<String> involvedGeneratorKeys() {
List<String> keys = new ArrayList<>(this.shadingMethod.involvedGeneratorKeys());
this.outlineShading.ifPresent(outlineShading -> keys.addAll(outlineShading.involvedGeneratorKeys()));
Expand All @@ -67,7 +75,7 @@ public List<ShadeData> shadesIn(ChunkPos chunk) {
GraphedChunk graph = this.grapher.getChunkGraph(chunk);

List<ShadeData> shades = new ArrayList<>(this.shadingMethod.shadeIn(graph.getShaded(), chunk));
this.outlineShading.ifPresent(outline -> shades.addAll(outline.shadeIn(graph.getOutlines(), chunk)));
this.outlineShading.ifPresent(outline -> shades.addAll(outline.shadeIn(graph.getOutlines(this.outlinesIncludeDiagonal.getNeighborsToCheck()), chunk)));

return shades;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ private int getSlice(Coordinate pos) {
return Math.min(currentSlice, lastSliceIndex); // Because there is a straight 1-thick line that is out of bounds otherwise
}

@Override
public String getShade(Coordinate pos) {
return this.generatorKeys.get(this.getSlice(pos));
}
@Override
public String getShade(Coordinate pos) {
return this.generatorKeys.get(this.getSlice(pos));
}
}

0 comments on commit 493689c

Please sign in to comment.