From 22f95edf32803683e366c873b1679ca71a802dd6 Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Fri, 25 Jun 2021 13:21:03 +0100 Subject: [PATCH] Added reset system for unused persisted blocks --- server/src/sys/terrain.rs | 15 +++++++++++++-- server/src/terrain_persistence.rs | 4 ++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/server/src/sys/terrain.rs b/server/src/sys/terrain.rs index 0a64f0269b..dcc0fa2f5b 100644 --- a/server/src/sys/terrain.rs +++ b/server/src/sys/terrain.rs @@ -156,8 +156,19 @@ impl<'a> System<'a> for Sys { }, }; - for (key, block) in terrain_persistence.load_chunk(key).blocks() { - chunk.set(key, block); + // Terrain persistence + let mut resets = Vec::new(); + for (rpos, new_block) in terrain_persistence.load_chunk(key).blocks() { + chunk.map(rpos, |block| { + if block == new_block { + resets.push(rpos); + } + new_block + }); + } + // Reset any unchanged blocks + for rpos in resets { + terrain_persistence.reset_block(key, rpos); } // Arcify the chunk diff --git a/server/src/terrain_persistence.rs b/server/src/terrain_persistence.rs index 67769c1b1b..91df2b4625 100644 --- a/server/src/terrain_persistence.rs +++ b/server/src/terrain_persistence.rs @@ -103,6 +103,10 @@ impl TerrainPersistence { let key = pos.xy().map2(TerrainChunk::RECT_SIZE, |e, sz| e.div_euclid(sz as i32)); self.load_chunk(key).blocks.insert(pos - key * TerrainChunk::RECT_SIZE.map(|e| e as i32), block); } + + pub fn reset_block(&mut self, key: Vec2, rpos: Vec3) { + self.load_chunk(key).blocks.remove(&rpos); + } } #[derive(Default, Serialize, Deserialize)]