Skip to content

Commit

Permalink
Stop directly storing chunk length in Coordinates
Browse files Browse the repository at this point in the history
This will make them use up a bit less memory, though this change is done
as the only place that chunk lengths are ever not 16 is during biome resolution,
so it makes more sense to just enter the side length manually in those
cases and not store that value elsewhere.
  • Loading branch information
Pixaurora committed Sep 13, 2023
1 parent de60454 commit 635088a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
26 changes: 17 additions & 9 deletions src/main/java/net/pixaurora/janerator/graphing/Coordinate.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import org.joml.Vector2i;

public record Coordinate(int x, int z, int scale) {
public record Coordinate(int x, int z) {
private static final Vector2i[] NEIGHBOR_OFFSETS = new Vector2i[]{
new Vector2i(1, 0),
new Vector2i(1, 1),
Expand All @@ -17,32 +17,40 @@ public record Coordinate(int x, int z, int scale) {
new Vector2i(1, -1)
};

public Coordinate(int x, int z) {
this(x, z, 16);
public int toListIndex(int chunkLength) {
return chunkLength * GraphingUtils.mod(this.x, chunkLength) + GraphingUtils.mod(this.z, chunkLength);
}

public int toListIndex() {
return this.scale * GraphingUtils.mod(this.x, this.scale) + GraphingUtils.mod(this.z, this.scale);
return this.toListIndex(16);
}

public static Coordinate fromListIndex(int index, int scale) {
return new Coordinate(Math.floorDiv(index, scale), GraphingUtils.mod(index, scale), scale);
public static Coordinate fromListIndex(int index, int chunkLength) {
return new Coordinate(Math.floorDiv(index, chunkLength), GraphingUtils.mod(index, chunkLength));
}

public static Coordinate fromListIndex(int index) {
return fromListIndex(index, 16);
}

public boolean isLegal(int chunkLength) {
return 0 <= this.x && this.x < chunkLength && 0 <= this.z && this.z < chunkLength;
}

public boolean isLegal() {
return 0 <= this.x && this.x < this.scale && 0 <= this.z && this.z < this.scale;
return this.isLegal(16);
}

public Coordinate makeLegal(int chunkLength) {
return new Coordinate(GraphingUtils.mod(this.x, chunkLength), GraphingUtils.mod(this.z, chunkLength));
}

public Coordinate makeLegal() {
return new Coordinate(GraphingUtils.mod(this.x, this.scale), GraphingUtils.mod(this.z, this.scale), this.scale);
return this.makeLegal(16);
}

public Coordinate offsetBy(Vector2i delta) {
return new Coordinate(this.x + delta.x, this.z + delta.y, this.scale);
return new Coordinate(this.x + delta.x, this.z + delta.y);
}

public List<Coordinate> getNeighbors() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class FullGeneratorLookup extends GeneratorLookup {
private GeneratorLookup biomeScale;

public FullGeneratorLookup(List<ChunkGenerator> blockLevelMapping, List<ChunkGenerator> biomeLevel) {
super(blockLevelMapping);
this.biomeScale = new GeneratorLookup(biomeLevel);
super(blockLevelMapping, 16);
this.biomeScale = new GeneratorLookup(biomeLevel, 4);
}

public GeneratorLookup atBiomeScale() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@

public class GeneratorLookup {
private List<ChunkGenerator> generatorMap;
private int chunkLength;

private Map<ChunkGenerator, PlacementSelection> selections;
private ChunkGenerator fallbackGenerator;

public GeneratorLookup(
List<ChunkGenerator> generatorMap
List<ChunkGenerator> generatorMap,
int chunkLength
) {
this.generatorMap = generatorMap;
this.chunkLength = chunkLength;

this.selections = this.generatorMap
.stream()
Expand All @@ -38,11 +41,11 @@ public int size() {
}

public ChunkGenerator getAt(Coordinate coordinate) {
return this.generatorMap.get(coordinate.toListIndex());
return this.generatorMap.get(coordinate.makeLegal(this.chunkLength).toListIndex(this.chunkLength));
}

public ChunkGenerator getAt(BlockPos pos) {
return this.getAt(new Coordinate(pos.getX(), pos.getZ()).makeLegal());
return this.getAt(new Coordinate(pos.getX(), pos.getZ()));
}

public Collection<PlacementSelection> getAllSelections() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static BiomeResolver getBiomeResolverForGenerator(ChunkGenerator generato
}

public Holder<Biome> getNoiseBiome(int x, int y, int z, Climate.Sampler sampler) {
ChunkGenerator generator = this.generators.getAt(new Coordinate(x, z, 4));
ChunkGenerator generator = this.generators.getAt(new Coordinate(x, z));

return biomeResolvers.get(
generator
Expand Down

0 comments on commit 635088a

Please sign in to comment.