From a7776e845b28245aad114184cd7b5e7f20f073c9 Mon Sep 17 00:00:00 2001 From: Patrick Owen Date: Mon, 16 Oct 2023 23:16:30 -0400 Subject: [PATCH] Include voxel data in world saves Not yet testable, as world loads are unimplemented --- server/src/sim.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/server/src/sim.rs b/server/src/sim.rs index ddabdcfa..6073095f 100644 --- a/server/src/sim.rs +++ b/server/src/sim.rs @@ -36,6 +36,7 @@ pub struct Sim { despawns: Vec, graph_entities: GraphEntities, dirty_nodes: FxHashSet, + dirty_voxel_nodes: FxHashSet, modified_chunks: FxHashMap>, } @@ -52,6 +53,7 @@ impl Sim { despawns: Vec::new(), graph_entities: GraphEntities::new(), dirty_nodes: FxHashSet::default(), + dirty_voxel_nodes: FxHashSet::default(), modified_chunks: FxHashMap::default(), }; @@ -86,10 +88,15 @@ impl Sim { } let dirty_nodes = self.dirty_nodes.drain().collect::>(); + let dirty_voxel_nodes = self.dirty_voxel_nodes.drain().collect::>(); for node in dirty_nodes { let entities = self.snapshot_node(node); writer.put_entity_node(self.graph.hash_of(node), &entities)?; } + for node in dirty_voxel_nodes { + let voxels = self.snapshot_voxel_node(node); + writer.put_voxel_node(self.graph.hash_of(node), &voxels)?; + } drop(writer); tx.commit()?; @@ -128,6 +135,27 @@ impl Sim { } } + fn snapshot_voxel_node(&self, node: NodeId) -> save::VoxelNode { + let mut chunks = vec![]; + let node_data = self.graph.get(node).as_ref().unwrap(); + for &vertex in self.modified_chunks.get(&self.graph.hash_of(node)).unwrap() { + let mut serialized_voxels = Vec::new(); + let Chunk::Populated { ref voxels, .. } = node_data.chunks[vertex] else { + panic!("Unknown chunk listed as modified"); + }; + postcard_helpers::serialize( + &voxels.to_serializable(self.cfg.chunk_size), + &mut serialized_voxels, + ) + .unwrap(); + chunks.push(save::Chunk { + vertex: vertex as u32, + voxels: serialized_voxels, + }) + } + save::VoxelNode { chunks } + } + pub fn spawn_character(&mut self, hello: ClientHello) -> (EntityId, Entity) { let id = self.new_id(); info!(%id, name = %hello.name, "spawning character");