Skip to content

Commit

Permalink
Allow clients to handle block updates for unloaded chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
patowen committed Dec 18, 2023
1 parent 4f99184 commit 47d6832
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 9 additions & 0 deletions client/src/graphics/voxels/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ impl Voxels {
while let Some(chunk) = self.worldgen.poll() {
let chunk_id = ChunkId::new(chunk.node, chunk.chunk);
sim.graph.populate_chunk(chunk_id, chunk.voxels, false);

// Now that the block is populated, we can apply any pending block updates the server
// provided that the client couldn't apply.
if let Some(block_updates) = sim.pending_modified_chunks.remove(&chunk_id) {
for block_update in block_updates {
// The chunk was just populated, so a block update should always succeed.
assert!(sim.graph.update_block(&block_update));
}
}
}

// Determine what to load/render
Expand Down
10 changes: 7 additions & 3 deletions client/src/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use common::{
collision_math::Ray,
graph::{Graph, NodeId},
graph_ray_casting,
node::{populate_fresh_nodes, VoxelData},
node::{populate_fresh_nodes, ChunkId, VoxelData},
proto::{
self, BlockUpdate, Character, CharacterInput, CharacterState, Command, Component, Position,
},
Expand All @@ -25,6 +25,7 @@ use common::{
pub struct Sim {
// World state
pub graph: Graph,
pub pending_modified_chunks: FxHashMap<ChunkId, Vec<BlockUpdate>>,
pub graph_entities: GraphEntities,
entity_ids: FxHashMap<EntityId, Entity>,
pub world: hecs::World,
Expand Down Expand Up @@ -67,6 +68,7 @@ impl Sim {
populate_fresh_nodes(&mut graph);
Self {
graph,
pending_modified_chunks: FxHashMap::default(),
graph_entities: GraphEntities::new(),
entity_ids: FxHashMap::default(),
world: hecs::World::new(),
Expand Down Expand Up @@ -301,8 +303,10 @@ impl Sim {
populate_fresh_nodes(&mut self.graph);
for block_update in msg.block_updates.into_iter() {
if !self.graph.update_block(&block_update) {
// TODO: This case should be handled to properly support multiple players.
tracing::error!("Voxel data received from server for ungenerated chunk.")
self.pending_modified_chunks
.entry(block_update.chunk_id)
.or_default()
.push(block_update);
}
}
for (chunk_id, voxel_data) in msg.modified_chunks {
Expand Down

0 comments on commit 47d6832

Please sign in to comment.