feat(server): implement authoritative chunk tracking
This commit is contained in:
parent
72b9c9e885
commit
ab01c8a033
|
|
@ -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"] }
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
28
crates/server/src/world_server.rs
Normal file
28
crates/server/src/world_server.rs
Normal file
|
|
@ -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<ChunkPos, Chunk>,
|
||||
}
|
||||
|
||||
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)
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue