diff --git a/Cargo.lock b/Cargo.lock index b5e29b2..b94db69 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -548,6 +548,7 @@ dependencies = [ "anyhow", "ash-window", "glam 0.33.2", + "net", "raw-window-handle", "renderer", "serde_json", @@ -2421,6 +2422,7 @@ dependencies = [ "crossbeam-channel", "glam 0.33.2", "lru", + "net", "serde_json", "shared", "tempfile", diff --git a/crates/server/Cargo.toml b/crates/server/Cargo.toml index 81108d4..dcc2722 100644 --- a/crates/server/Cargo.toml +++ b/crates/server/Cargo.toml @@ -14,6 +14,7 @@ bevy_ecs = "0.19" crossbeam-channel = "0.5.16" glam.workspace = true lru = "0.18.1" +net = { version = "0.1.0", path = "../net" } serde_json.workspace = true shared = { path = "../shared" } tracing.workspace = true diff --git a/crates/server/src/main.rs b/crates/server/src/main.rs index 6f967ac..2c38f8f 100644 --- a/crates/server/src/main.rs +++ b/crates/server/src/main.rs @@ -15,6 +15,8 @@ pub mod world_server; use std::collections::HashSet; use std::fs; +use std::net::{Ipv4Addr, SocketAddr}; +use std::time::Duration; use anyhow::Context; use bevy_ecs::prelude::{Query, ResMut, Schedule, With, World}; @@ -23,6 +25,7 @@ use shared::generator::{VoxelGenerator, WorldGenConfig}; use shared::world::{ChunkPos, EntityPos}; use tracing::{debug, info}; +use net::NetworkServer; use player::{Player, Position, ViewDistance}; use world_server::{ServerWorld, cylinder_chunks}; @@ -116,16 +119,26 @@ fn main() -> anyhow::Result<()> { } info!("Initial region ready; granting player control"); - // Drive several ticks, marching the dummy one chunk along +X between each. The manual movement stands in for network-driven player input and exists only to exercise load/unload as the anchor moves. - for step in 0..5 { - info!(step, "tick"); + // Spawn the networking thread and bind the QUIC endpoint. The synchronous simulation loop below communicates with it only by draining events. + let bind = SocketAddr::from((Ipv4Addr::LOCALHOST, net::DEFAULT_PORT)); + let (network, local_addr) = NetworkServer::spawn( + bind, + env!("CARGO_PKG_VERSION").to_owned(), + // Placeholder advisory tick rate. + 20, + ) + .context("spawning network server")?; + info!(%local_addr, "network endpoint listening"); + + // Authoritative simulation loop. + loop { schedule.run(&mut world); - let mut movers = world.query_filtered::<&mut Position, With>(); - for mut position in movers.iter_mut(&mut world) { - position.0.chunk.x += 1; + for event in network.poll_events() { + info!(?event, "network event"); } - } - Ok(()) + // Advisory ~20 Hz cadence until the real tick scheduler lands. + std::thread::sleep(Duration::from_millis(50)); + } }