Skip to content

Commit

Permalink
Store surrounding 3×3×3 chunks on edit.
Browse files Browse the repository at this point in the history
Should make the world more resilient to terrain generation changes.
fixes PixelGuys#349
  • Loading branch information
IntegratedQuantum committed Jun 5, 2024
1 parent 8111218 commit 53730ff
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/chunk.zig
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ pub const ServerChunk = struct {

wasChanged: bool = false,
generated: bool = false,
wasStored: bool = false,

mutex: std.Thread.Mutex = .{},
refCount: std.atomic.Value(u16),
Expand Down Expand Up @@ -329,6 +330,33 @@ pub const ServerChunk = struct {
const z = _z >> self.super.voxelSizeShift;
const index = getIndex(x, y, z);
self.super.data.setValue(index, newBlock);
if(!self.wasChanged and self.super.pos.voxelSize == 1) {
self.mutex.unlock();
defer self.mutex.lock();
// Store all the neighbor chunks as well:
var dx: i32 = -@as(i32, chunkSize);
while(dx <= chunkSize) : (dx += chunkSize) {
var dy: i32 = -@as(i32, chunkSize);
while(dy <= chunkSize) : (dy += chunkSize) {
var dz: i32 = -@as(i32, chunkSize);
while(dz <= chunkSize) : (dz += chunkSize) {
if(dx == 0 and dy == 0 and dz == 0) continue;
const ch = main.server.world.?.getOrGenerateChunkAndIncreaseRefCount(.{
.wx = self.super.pos.wx +% dx,
.wy = self.super.pos.wy +% dy,
.wz = self.super.pos.wz +% dz,
.voxelSize = 1
});
defer ch.decreaseRefCount();
ch.mutex.lock();
defer ch.mutex.unlock();
if(!ch.wasStored) {
ch.setChanged();
}
}
}
}
}
self.setChanged();
}

Expand Down Expand Up @@ -430,6 +458,7 @@ pub const ServerChunk = struct {
pub fn save(self: *ServerChunk, world: *main.server.ServerWorld) void {
self.mutex.lock();
defer self.mutex.unlock();
self.wasStored = true;
if(self.wasChanged) {
const pos = self.super.pos;
const regionSize = pos.voxelSize*chunkSize*main.server.storage.RegionFile.regionSize;
Expand Down
1 change: 1 addition & 0 deletions src/server/world.zig
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ const ChunkManager = struct {
std.log.err("Storage for chunk {} in region file at {} is corrupted", .{pos, region.pos});
break :blk;
};
ch.wasStored = true;
return ch;
}
ch.generated = true;
Expand Down

0 comments on commit 53730ff

Please sign in to comment.