From ab01c8a033a60c69011e05f52701b7d67ab46528 Mon Sep 17 00:00:00 2001 From: Serkyo Date: Sat, 16 May 2026 19:04:08 +0200 Subject: [PATCH] feat(server): implement authoritative chunk tracking --- crates/server/Cargo.toml | 2 ++ crates/server/src/main.rs | 38 ++++++++++++++++++++++++++++--- crates/server/src/world_server.rs | 28 +++++++++++++++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 crates/server/src/world_server.rs diff --git a/crates/server/Cargo.toml b/crates/server/Cargo.toml index 65ae803..843a82d 100644 --- a/crates/server/Cargo.toml +++ b/crates/server/Cargo.toml @@ -7,5 +7,7 @@ edition = "2024" workspace = true [dependencies] +serde_json = "1.0.149" +shared = { version = "0.1.0", path = "../shared" } tracing = "0.1.44" tracing-subscriber = { version = "0.3.23", features = ["env-filter"] } diff --git a/crates/server/src/main.rs b/crates/server/src/main.rs index f4b23f9..ac61c9f 100644 --- a/crates/server/src/main.rs +++ b/crates/server/src/main.rs @@ -3,12 +3,44 @@ //! The server handles the authoritative game simulation, including world //! management, physics, and combat. -use tracing::info; +/// Authoritative chunk storage and generation logic for the server. +pub mod world_server; + +use std::fs; + +use shared::{generator::{VoxelGenerator, WorldGenConfig}, world::ChunkPos}; +use tracing::{debug, info}; fn main() { tracing_subscriber::fmt() - .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) + .with_env_filter( + tracing_subscriber::EnvFilter::try_from_default_env() + .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("debug")) + ) .init(); info!("Starting Project Catalyst server"); -} + + #[allow(clippy::expect_used)] + let config_str = fs::read_to_string("assets/data/worldgen/default.json").expect("Failed to read worldgen config"); + + #[allow(clippy::expect_used)] + let worldgen_config: WorldGenConfig = serde_json::from_str(&config_str).expect("Failed to parse worldgen config"); + + info!("Successfully loaded world configuration"); + debug!("Base height: {}", worldgen_config.base_height); + debug!("Noise scale: {}", worldgen_config.noise_scale); + debug!("Surface block: {}", worldgen_config.surface_block.0); + debug!("Subsurface block: {}", worldgen_config.subsurface_block.0); + debug!("Stone block: {}", worldgen_config.stone_block.0); + + let seed = 4_813_530; + + let generator = VoxelGenerator::new(worldgen_config, seed); + + 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); +} \ No newline at end of file diff --git a/crates/server/src/world_server.rs b/crates/server/src/world_server.rs new file mode 100644 index 0000000..2d7d8a3 --- /dev/null +++ b/crates/server/src/world_server.rs @@ -0,0 +1,28 @@ +//! Authoritative chunk storage and generation logic for the server. + +use shared::{generator::VoxelGenerator, world::{Chunk, ChunkPos}}; +use std::collections::HashMap; + +/// The server's authoritative representation of the world. +pub struct ServerWorld { + generator: VoxelGenerator, + chunks: HashMap, +} + +impl ServerWorld { + /// Initializes a new authoritative server world with the provided generator. + #[must_use] + pub fn new(generator: VoxelGenerator) -> Self { + Self { + generator, + chunks: HashMap::new(), + } + } + + /// Retrieves a reference to the chunk at the given position, generating it if necessary. + pub fn get_chunk(&mut self, pos: ChunkPos) -> &Chunk { + self.chunks.entry(pos).or_insert_with(|| { + self.generator.generate_chunk(pos) + }) + } +} \ No newline at end of file