Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes missing block faces on chunk borders #324

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions OctoAwesome/OctoAwesome/Chunking/LocalChunkCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,43 @@ public void SetBlock(Index3 index, ushort block)
/// <inheritdoc />
public void SetBlock(int x, int y, int z, ushort block)
{
UpdateAdjacentChunks(x, y, z);
var chunk = GetChunk(x >> Chunk.LimitX, y >> Chunk.LimitY, z >> Chunk.LimitZ);

chunk?.SetBlock(x, y, z, block);
}

private void UpdateAdjacentChunks(int x, int y, int z)
{
var xBorder = x & (Chunk.CHUNKSIZE_X - 1);
var yBorder = y & (Chunk.CHUNKSIZE_Y - 1);
var zBorder = z & (Chunk.CHUNKSIZE_Z - 1);

static int CalcDir(int v, int chunkLimit)
{
return ((v + 1) >> (chunkLimit - 1)) - 1;
}

if (xBorder is 0 or Chunk.CHUNKSIZE_X - 1)
{
var xDir = CalcDir(xBorder, Chunk.LimitX);
// TODO: do not update if on border of chunk cache.
GetChunk((x >> Chunk.LimitX) + xDir, y >> Chunk.LimitY, z >> Chunk.LimitZ)?.FlagDirty();
}
if (yBorder is 0 or Chunk.CHUNKSIZE_Y - 1)
{
var yDir = CalcDir(yBorder, Chunk.LimitX);
// TODO: do not update if on border of chunk cache.
GetChunk(x >> Chunk.LimitX, (y >> Chunk.LimitY) + yDir, z >> Chunk.LimitZ)?.FlagDirty();
}
if (zBorder is 0 or Chunk.CHUNKSIZE_Z - 1)
{
var zDir = CalcDir(zBorder, Chunk.LimitX);
// TODO: do not update if on border of chunk cache.
GetChunk(x >> Chunk.LimitX, y >> Chunk.LimitY, (z >> Chunk.LimitZ) + zDir)?.FlagDirty();
}
}

/// <inheritdoc />
public int GetBlockMeta(int x, int y, int z)
{
Expand All @@ -292,6 +324,7 @@ public int GetBlockMeta(Index3 index)
/// <inheritdoc />
public void SetBlockMeta(int x, int y, int z, int meta)
{
UpdateAdjacentChunks(x, y, z);
var chunk = GetChunk(x >> Chunk.LimitX, y >> Chunk.LimitY, z >> Chunk.LimitZ);

chunk?.SetBlockMeta(x, y, z, meta);
Expand Down
Loading