Skip to content

Commit

Permalink
Generate world only in the positive axes
Browse files Browse the repository at this point in the history
Avoids many off-by-one errors due to negative rounding.
  • Loading branch information
Nixinova committed Jul 24, 2024
1 parent 42e5e8b commit 7529f53
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 22 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Next
- Added block face culling
- World now only generates along the positive axes

## 0.0.17_3
*2024-07-24 23:03*
Expand Down
27 changes: 5 additions & 22 deletions src/com/nixinova/world/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,11 @@ public class World {
public final BlockCoord minCorner;
public final BlockCoord maxCorner;

private final int[] arrSize;

private Render[][][] blockTextures;

public World() {
this.minCorner = new BlockCoord(-Options.worldSize, 0, -Options.worldSize);
this.minCorner = new BlockCoord(0, 0, 0);
this.maxCorner = new BlockCoord(Options.worldSize, SKY_Y, Options.worldSize);
this.arrSize = new int[] {
// to go from +size to -size incl. 0
maxCorner.x - minCorner.x + 1,
maxCorner.y - minCorner.y + 1,
maxCorner.z - minCorner.z + 1,
};

this.mapBlockTextures();
}
Expand Down Expand Up @@ -92,8 +84,7 @@ public boolean isAir(int blockX, int blockY, int blockZ) {
public Render getTextureAt(int blockX, int blockY, int blockZ) {
if (isWithinWorld(blockX, blockY, blockZ)) {
// If within the world, return texture
BlockCoord coordI = toBlockIndex(blockX, blockY, blockZ);
return this.blockTextures[coordI.x][coordI.y][coordI.z];
return this.blockTextures[blockX][blockY][blockZ];
} else {
// When outside of world, return sky
return Block.SKY.getTexture();
Expand All @@ -102,12 +93,13 @@ public Render getTextureAt(int blockX, int blockY, int blockZ) {

public void setTextureAt(int blockX, int blockY, int blockZ, Render texture) {
if (isWithinWorld(blockX, blockY, blockZ)) {
BlockCoord coordI = toBlockIndex(blockX, blockY, blockZ);
this.blockTextures[coordI.x][coordI.y][coordI.z] = texture;
this.blockTextures[blockX][blockY][blockZ] = texture;
}
}

// NOTE: does not abide by this.minCorner
private void mapBlockTextures() {
int arrSize[] = new int[] { this.maxCorner.x+1, this.maxCorner.y+1, this.maxCorner.z+1 };
this.blockTextures = new Render[arrSize[0]][arrSize[1]][arrSize[2]];

Random random = new Random(Options.seed);
Expand Down Expand Up @@ -151,13 +143,4 @@ else if (y <= GROUND_Y + 1 && random.nextBoolean())
}
}

private int toBlockIndex(int blockCoord) {
return blockCoord + Options.worldSize;
}

private BlockCoord toBlockIndex(int blockX, int blockY, int blockZ) {
// Note: Y cannot be negative, so no blockIndexing
return new BlockCoord(toBlockIndex(blockX), blockY, toBlockIndex(blockZ));
}

}

0 comments on commit 7529f53

Please sign in to comment.