Skip to content

Commit

Permalink
Include voxel data in world saves
Browse files Browse the repository at this point in the history
Not yet testable, as world loads are unimplemented
  • Loading branch information
patowen committed Oct 17, 2023
1 parent 6acc0b9 commit a7776e8
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions server/src/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub struct Sim {
despawns: Vec<EntityId>,
graph_entities: GraphEntities,
dirty_nodes: FxHashSet<NodeId>,
dirty_voxel_nodes: FxHashSet<NodeId>,
modified_chunks: FxHashMap<u128, FxHashSet<Vertex>>,
}

Expand All @@ -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(),
};

Expand Down Expand Up @@ -86,10 +88,15 @@ impl Sim {
}

let dirty_nodes = self.dirty_nodes.drain().collect::<Vec<_>>();
let dirty_voxel_nodes = self.dirty_voxel_nodes.drain().collect::<Vec<_>>();
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()?;
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit a7776e8

Please sign in to comment.