Skip to content

Commit

Permalink
Update neighbor block state on the server.
Browse files Browse the repository at this point in the history
fixes #354
  • Loading branch information
IntegratedQuantum committed May 15, 2024
1 parent 814bced commit 13f27a6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
6 changes: 1 addition & 5 deletions src/network.zig
Original file line number Diff line number Diff line change
Expand Up @@ -818,11 +818,7 @@ pub const Protocols = struct {
const z = std.mem.readInt(i32, data[8..12], .big);
const newBlock = Block.fromInt(std.mem.readInt(u32, data[12..16], .big));
if(conn.user != null) { // TODO: Send update event to other players.
const mask = ~@as(i32, chunk.chunkMask);
const ch = main.server.world.?.getOrGenerateChunk(.{.wx = x & mask, .wy = y & mask, .wz = z & mask, .voxelSize = 1});
ch.mutex.lock();
defer ch.mutex.unlock();
ch.updateBlockAndSetChanged(x & chunk.chunkMask, y & chunk.chunkMask, z & chunk.chunkMask, newBlock);
main.server.world.?.updateBlock(x, y, z, newBlock);
} else {
renderer.mesh_storage.updateBlock(x, y, z, newBlock);
}
Expand Down
36 changes: 36 additions & 0 deletions src/server/world.zig
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,42 @@ pub const ServerWorld = struct {
return Block {.typ = 0, .data = 0};
}

pub fn updateBlock(_: *ServerWorld, wx: i32, wy: i32, wz: i32, _newBlock: Block) void {
const baseChunk = ChunkManager.getOrGenerateChunk(.{.wx = wx & ~@as(i32, chunk.chunkMask), .wy = wy & ~@as(i32, chunk.chunkMask), .wz = wz & ~@as(i32, chunk.chunkMask), .voxelSize = 1});
const x: u5 = @intCast(wx & chunk.chunkMask);
const y: u5 = @intCast(wy & chunk.chunkMask);
const z: u5 = @intCast(wz & chunk.chunkMask);
var newBlock = _newBlock;
for(chunk.Neighbors.iterable) |neighbor| {
const nx = x + chunk.Neighbors.relX[neighbor];
const ny = y + chunk.Neighbors.relY[neighbor];
const nz = z + chunk.Neighbors.relZ[neighbor];
var ch = baseChunk;
if(nx & chunk.chunkMask != nx or ny & chunk.chunkMask != ny or nz & chunk.chunkMask != nz) {
ch = ChunkManager.getOrGenerateChunk(.{
.wx = baseChunk.pos.wx + nx,
.wy = baseChunk.pos.wy + ny,
.wz = baseChunk.pos.wz + nz,
.voxelSize = 1,
});
}
ch.mutex.lock();
defer ch.mutex.unlock();
var neighborBlock = ch.getBlock(nx & chunk.chunkMask, ny & chunk.chunkMask, nz & chunk.chunkMask);
if(neighborBlock.mode().dependsOnNeighbors) {
if(neighborBlock.mode().updateData(&neighborBlock, neighbor ^ 1, newBlock)) {
ch.updateBlockAndSetChanged(nx & chunk.chunkMask, ny & chunk.chunkMask, nz & chunk.chunkMask, neighborBlock);
}
}
if(newBlock.mode().dependsOnNeighbors) {
_ = newBlock.mode().updateData(&newBlock, neighbor, neighborBlock);
}
}
baseChunk.mutex.lock();
defer baseChunk.mutex.unlock();
baseChunk.updateBlock(x, y, z, newBlock);
}

pub fn queueChunkUpdate(self: *ServerWorld, ch: *Chunk) void {
self.mutex.lock();
self.chunkUpdateQueue.enqueue(.{.ch = ch, .milliTimeStamp = std.time.milliTimestamp()});
Expand Down

0 comments on commit 13f27a6

Please sign in to comment.