diff --git a/crates/server/src/main.rs b/crates/server/src/main.rs index 2f0f198..0d49011 100644 --- a/crates/server/src/main.rs +++ b/crates/server/src/main.rs @@ -10,10 +10,7 @@ pub mod world_server; use std::fs; -use shared::{ - generator::{VoxelGenerator, WorldGenConfig}, - world::ChunkPos, -}; +use shared::generator::{VoxelGenerator, WorldGenConfig}; use tracing::{debug, info}; fn main() { @@ -47,10 +44,5 @@ fn main() { let mut world = world_server::ServerWorld::new(generator); - let spawn_chunk = world.get_chunk(ChunkPos::new(0, 0, 0)); - - tracing::info!( - "Spawn chunk generated. Block at (0,0,0) is {}", - spawn_chunk.get(0, 0, 0).0 - ); + world.load_around(0.0, 0.0, 0.0, 4); } diff --git a/crates/server/src/world_server.rs b/crates/server/src/world_server.rs index 1cc269f..5215144 100644 --- a/crates/server/src/world_server.rs +++ b/crates/server/src/world_server.rs @@ -30,4 +30,28 @@ impl ServerWorld { .entry(pos) .or_insert_with(|| self.generator.generate_chunk(pos)) } + + /// Generates and caches all chunks in a cylindrical region around the given position. + pub fn load_around(&mut self, center_x: f64, center_y: f64, center_z: f64, radius: i32) { + let center = ChunkPos::from_world(center_x, center_y, center_z); + + // Counts the chunks inside the cylinder that were generated or already resident. + let mut count = 0; + + for x in center.x - radius..=center.x + radius { + for z in center.z - radius..=center.z + radius { + for y in center.y - radius / 2..=center.y + radius / 2 { + let dx = x - center.x; + let dz = z - center.z; + + if (dx * dx + dz * dz) <= radius * radius { + self.get_chunk(ChunkPos::new(x, y, z)); + count += 1; + } + } + } + } + + tracing::info!(chunks = count, center = ?center, radius, "loaded chunk cylinder"); + } }