feat(server): generate a cylinder of chunks around a center point
This commit is contained in:
parent
2cd3932c8d
commit
6bd8e5d3ba
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue