diff options
-rw-r--r-- | Cargo.lock | 60 | ||||
-rw-r--r-- | server/Cargo.toml | 1 | ||||
-rw-r--r-- | server/src/customer.rs | 50 | ||||
-rw-r--r-- | test-client/main.ts | 6 |
4 files changed, 90 insertions, 27 deletions
@@ -487,8 +487,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ "libc", - "rand_chacha", - "rand_core", + "rand_chacha 0.3.1", + "rand_core 0.6.4", +] + +[[package]] +name = "rand" +version = "0.9.0-alpha.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d31e63ea85be51c423e52ba8f2e68a3efd53eed30203ee029dd09947333693e" +dependencies = [ + "rand_chacha 0.9.0-alpha.1", + "rand_core 0.9.0-alpha.1", + "zerocopy", ] [[package]] @@ -498,7 +509,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0-alpha.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78674ef918c19451dbd250f8201f8619b494f64c9aa6f3adb28fd8a0f1f6da46" +dependencies = [ + "ppv-lite86", + "rand_core 0.9.0-alpha.1", ] [[package]] @@ -511,6 +532,16 @@ dependencies = [ ] [[package]] +name = "rand_core" +version = "0.9.0-alpha.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc89dffba8377c5ec847d12bb41492bda235dba31a25e8b695cd0fe6589eb8c9" +dependencies = [ + "getrandom", + "zerocopy", +] + +[[package]] name = "redox_syscall" version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -740,7 +771,7 @@ dependencies = [ "http", "httparse", "log", - "rand", + "rand 0.8.5", "sha1", "thiserror", "utf-8", @@ -761,6 +792,7 @@ dependencies = [ "futures-util", "glam", "log", + "rand 0.9.0-alpha.1", "serde", "serde_json", "serde_yaml", @@ -942,3 +974,23 @@ name = "windows_x86_64_msvc" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" + +[[package]] +name = "zerocopy" +version = "0.8.0-alpha.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db678a6ee512bd06adf35c35be471cae2f9c82a5aed2b5d15e03628c98bddd57" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.0-alpha.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "201585ea96d37ee69f2ac769925ca57160cef31acb137c16f38b02b76f4c1e62" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/server/Cargo.toml b/server/Cargo.toml index 447a3ff1..535d5a1e 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -14,3 +14,4 @@ serde_json = "1.0.117" tokio-tungstenite = "0.23.1" futures-util = "0.3.30" serde_yaml = "0.9.34+deprecated" +rand = "0.9.0-alpha.1" diff --git a/server/src/customer.rs b/server/src/customer.rs index 403b891e..b003c935 100644 --- a/server/src/customer.rs +++ b/server/src/customer.rs @@ -4,7 +4,8 @@ use crate::{ protocol::{PacketC, PacketS, PlayerID}, }; use glam::{IVec2, Vec2}; -use log::{error, info}; +use log::{debug, error}; +use rand::thread_rng; use std::{ cmp::Ordering, collections::{BinaryHeap, HashMap, HashSet}, @@ -19,13 +20,13 @@ use tokio::{ struct DemandState { data: Gamedata, walkable: HashSet<IVec2>, - chairs: HashSet<IVec2>, + chairs: HashMap<IVec2, bool>, customers: Vec<Customer>, } enum CustomerState { - WalkingToChair { path: Vec<Vec2> }, - Waiting, + WalkingToChair { path: Vec<Vec2>, chair: IVec2 }, + Waiting { chair: IVec2 }, } struct Customer { @@ -51,11 +52,11 @@ pub async fn customer(game: Arc<RwLock<Game>>, mut grx: broadcast::Receiver<Pack } PacketC::UpdateMap { pos, tile, .. } => { let tilename = &state.data.tile_names[tile]; - if tilename == "floor" || tilename == "door" { + if tilename == "floor" || tilename == "door" || tilename == "chair" { state.walkable.insert(pos); } if tilename == "chair" { - state.chairs.insert(pos); + state.chairs.insert(pos, true); } } _ => (), @@ -94,41 +95,51 @@ impl DemandState { character: 0, }, )); - let path = find_path( - &self.walkable, - self.data.customer_spawn.as_ivec2(), - IVec2::new(20, 1), - ) - .expect("no path"); + let chair = select_chair(&mut self.chairs); + let path = find_path(&self.walkable, self.data.customer_spawn.as_ivec2(), chair) + .expect("no path"); self.customers.push(Customer { id: -1, position: self.data.customer_spawn, facing: Vec2::X, vel: Vec2::ZERO, - state: CustomerState::WalkingToChair { path }, + state: CustomerState::WalkingToChair { path, chair }, }); } for p in &mut self.customers { match &mut p.state { - CustomerState::WalkingToChair { path } => { + CustomerState::WalkingToChair { path, chair } => { if let Some(next) = path.last().copied() { - info!("next {next}"); - if next.distance(p.position) < 0.6 { + debug!("next {next}"); + if next.distance(p.position) < if path.len() == 1 { 0.1 } else { 0.6 } { path.pop(); } packets_out .push((p.id, move_player(p, &self.walkable, next - p.position, dt))); } else { - p.state = CustomerState::Waiting; + p.state = CustomerState::Waiting { chair: *chair }; } } - CustomerState::Waiting => (), + CustomerState::Waiting { chair } => { + debug!("waiting") + } } } } } +pub fn select_chair(chairs: &mut HashMap<IVec2, bool>) -> IVec2 { + use rand::seq::IteratorRandom; + let (chosen, free) = chairs + .iter_mut() + .filter(|(_p, free)| **free) + .choose(&mut thread_rng()) + .unwrap(); + *free = false; + *chosen +} + pub fn find_path(map: &HashSet<IVec2>, from: IVec2, to: IVec2) -> Option<Vec<Vec2>> { #[derive(Debug, PartialEq, Eq)] struct Open(i32, IVec2, IVec2); @@ -170,14 +181,13 @@ pub fn find_path(map: &HashSet<IVec2>, from: IVec2, to: IVec2) -> Option<Vec<Vec let mut path = Vec::new(); let mut c = to; loop { + path.push(c.as_vec2() + 0.5); let cn = visited[&c]; - path.push(cn.as_vec2() + 0.5); if cn == c { break; } c = cn } - Some(path) } diff --git a/test-client/main.ts b/test-client/main.ts index b189f08b..807567c8 100644 --- a/test-client/main.ts +++ b/test-client/main.ts @@ -307,9 +307,9 @@ function draw_ingame() { ctx.fillStyle = `hsl(${player.character}rad, 80%, 70%)` ctx.beginPath() - ctx.moveTo(-0.04, 0.35) - ctx.lineTo(0.04, 0.35) - ctx.lineTo(0, 0.5) + ctx.moveTo(-0.04, 0.25) + ctx.lineTo(0.04, 0.25) + ctx.lineTo(0, 0.4) ctx.fill() ctx.restore() |