Skip to content

Commit

Permalink
Update adjacent chunks from LocalChunkCache when a block at the borde…
Browse files Browse the repository at this point in the history
…r of a chunk was changed
  • Loading branch information
jvbsl committed Nov 1, 2023
1 parent e901af9 commit b01b233
Showing 1 changed file with 33 additions and 0 deletions.
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

0 comments on commit b01b233

Please sign in to comment.