Skip to content

Commit

Permalink
save remaining chunks in memory
Browse files Browse the repository at this point in the history
  • Loading branch information
kralverde committed Feb 17, 2025
1 parent defa0d8 commit b9fa988
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
9 changes: 8 additions & 1 deletion pumpkin-world/src/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,14 @@ impl Level {
log::info!("Saving level...");

// chunks are automatically saved when all players get removed
// TODO: Await this ^
// TODO: Await chunks that have been called by this ^

// save all stragling chunks
for chunk in self.loaded_chunks.iter() {
let pos = chunk.key();
let chunk = chunk.value();
self.write_chunk((*pos, chunk.clone())).await;
}

// then lets save the world info
self.world_info_writer
Expand Down
14 changes: 10 additions & 4 deletions pumpkin/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,8 @@ impl Server {
);

// Spawn chunks are never unloaded
for x in -1..=1 {
for z in -1..=1 {
world.level.mark_chunk_as_newly_watched(Vector2::new(x, z));
}
for chunk in Self::spawn_chunks() {
world.level.mark_chunk_as_newly_watched(chunk);
}

Self {
Expand Down Expand Up @@ -144,6 +142,14 @@ impl Server {
}
}

const SPAWN_CHUNK_RADIUS: i32 = 1;

pub fn spawn_chunks() -> impl Iterator<Item = Vector2<i32>> {
(-Self::SPAWN_CHUNK_RADIUS..=Self::SPAWN_CHUNK_RADIUS).flat_map(|x| {
(-Self::SPAWN_CHUNK_RADIUS..=Self::SPAWN_CHUNK_RADIUS).map(move |z| Vector2::new(x, z))
})
}

/// Adds a new player to the server.
///
/// This function takes an `Arc<Client>` representing the connected client and performs the following actions:
Expand Down

0 comments on commit b9fa988

Please sign in to comment.