diff options
Diffstat (limited to 'server/src')
| -rw-r--r-- | server/src/entity/bot.rs | 5 | ||||
| -rw-r--r-- | server/src/entity/customers.rs | 13 | ||||
| -rw-r--r-- | server/src/entity/environment_effect.rs | 7 | ||||
| -rw-r--r-- | server/src/entity/mod.rs | 11 | ||||
| -rw-r--r-- | server/src/entity/pedestrians.rs | 19 | ||||
| -rw-r--r-- | server/src/entity/tram.rs | 7 | ||||
| -rw-r--r-- | server/src/lib.rs | 10 | ||||
| -rw-r--r-- | server/src/main.rs | 1 | ||||
| -rw-r--r-- | server/src/network/mdns.rs | 5 | ||||
| -rw-r--r-- | server/src/network/register.rs | 10 | ||||
| -rw-r--r-- | server/src/server.rs | 18 |
11 files changed, 58 insertions, 48 deletions
diff --git a/server/src/entity/bot.rs b/server/src/entity/bot.rs index 8757cc97..26ae77a6 100644 --- a/server/src/entity/bot.rs +++ b/server/src/entity/bot.rs @@ -20,8 +20,7 @@ use anyhow::Result; use hurrycurry_bot::{BotAlgo, DynBotAlgo}; use hurrycurry_protocol::{Character, Hand, PacketS, PlayerClass, PlayerID}; use log::info; -use rand::random; -use std::any::Any; +use std::{any::Any, random::random}; pub type DynBotDriver = BotDriver<DynBotAlgo>; @@ -50,7 +49,7 @@ impl<T: BotAlgo + Any> Entity for BotDriver<T> { } fn tick(&mut self, c: EntityContext<'_>) -> Result<()> { if let Some((name, character, class)) = self.join_data.take() { - self.id = PlayerID(random()); // TODO bad code, can collide + self.id = PlayerID(random(..)); // TODO bad code, can collide info!("spawn {:?} ({name:?})", self.id); c.packet_in.push_back(PacketS::Join { name, diff --git a/server/src/entity/customers.rs b/server/src/entity/customers.rs index 22522e4e..1e795ac5 100644 --- a/server/src/entity/customers.rs +++ b/server/src/entity/customers.rs @@ -1,3 +1,7 @@ +use std::random::random; + +use crate::random_float; + /* Hurry Curry! - a game about cooking Copyright (C) 2025 Hurry Curry! Contributors @@ -15,11 +19,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -use super::{bot::BotDriver, Entity, EntityContext}; +use super::{Entity, EntityContext, bot::BotDriver}; use anyhow::Result; use hurrycurry_bot::algos::{Customer, CustomerConfig}; use hurrycurry_protocol::{Character, PlayerClass}; -use rand::random; pub struct Customers { customers: Vec<BotDriver<Customer>>, @@ -53,12 +56,12 @@ impl Entity for Customers { self.spawn_cooldown -= c.dt; self.spawn_cooldown = self.spawn_cooldown.max(0.); if self.customers.len() < max_count && self.spawn_cooldown <= 0. { - self.spawn_cooldown = 5. + random::<f32>() * 5.; + self.spawn_cooldown = 5. + random_float() * 5.; let bot = BotDriver::new( "".to_string(), Character { - color: random(), - hairstyle: random(), + color: random(..), + hairstyle: random(..), headwear: 0, }, PlayerClass::Customer, diff --git a/server/src/entity/environment_effect.rs b/server/src/entity/environment_effect.rs index ba2c395e..b5344a27 100644 --- a/server/src/entity/environment_effect.rs +++ b/server/src/entity/environment_effect.rs @@ -1,3 +1,5 @@ +use crate::random_float; + /* Hurry Curry! - a game about cooking Copyright (C) 2025 Hurry Curry! Contributors @@ -18,7 +20,6 @@ use super::{Entity, EntityContext}; use hurrycurry_data::entities::EnvironmentEffect; use hurrycurry_protocol::PacketC; -use rand::random; use std::time::{Duration, Instant}; #[derive(Clone, Debug)] @@ -42,12 +43,12 @@ impl Entity for EnvironmentEffectController { if self.next_transition < Instant::now() { if self.active { self.next_transition += - Duration::from_secs_f32(self.config.on * (0.5 + random::<f32>())); + Duration::from_secs_f32(self.config.on * (0.5 + random_float())); self.active = false; c.game.environment_effects.remove(&self.config.name); } else { self.next_transition += - Duration::from_secs_f32(self.config.off * (0.5 + random::<f32>())); + Duration::from_secs_f32(self.config.off * (0.5 + random_float())); self.active = true; c.game.environment_effects.insert(self.config.name.clone()); } diff --git a/server/src/entity/mod.rs b/server/src/entity/mod.rs index 47d37f3d..ce7cb849 100644 --- a/server/src/entity/mod.rs +++ b/server/src/entity/mod.rs @@ -35,9 +35,9 @@ use conveyor::Conveyor; use customers::Customers; use environment_effect::{EnvironmentController, EnvironmentEffectController}; use hurrycurry_client_lib::Game; -use hurrycurry_data::{entities::EntityDecl, Serverdata}; +use hurrycurry_data::{Serverdata, entities::EntityDecl}; use hurrycurry_locale::TrError; -use hurrycurry_protocol::{glam::IVec2, Character, Gamedata, PacketC, PacketS, PlayerID}; +use hurrycurry_protocol::{Character, Gamedata, PacketC, PacketS, PlayerID, glam::IVec2}; use item_portal::ItemPortal; use player_portal::PlayerPortal; use std::{ @@ -133,11 +133,8 @@ pub fn construct_entity(decl: &EntityDecl, data: &Gamedata) -> DynEntity { } => Box::new(Pedestrians { players: HashMap::new(), points, - spawn_delay_distr: rand_distr::Normal::new( - spawn_delay, - spawn_delay_stdev.unwrap_or(0.), - ) - .unwrap(), + spawn_delay, + spawn_delay_stdev: spawn_delay_stdev.unwrap_or(0.), cooldown: 0., speed: speed.unwrap_or(0.6), }), diff --git a/server/src/entity/pedestrians.rs b/server/src/entity/pedestrians.rs index fa3276d2..cc5d6d90 100644 --- a/server/src/entity/pedestrians.rs +++ b/server/src/entity/pedestrians.rs @@ -1,3 +1,5 @@ +use crate::random_gauss; + /* Hurry Curry! - a game about cooking Copyright (C) 2025 Hurry Curry! Contributors @@ -17,15 +19,14 @@ */ use super::{Entity, EntityContext}; use anyhow::Result; -use hurrycurry_protocol::{glam::Vec2, Character, PacketS, PlayerClass, PlayerID}; -use rand::{random, rng}; -use rand_distr::Distribution; -use std::collections::HashMap; +use hurrycurry_protocol::{Character, PacketS, PlayerClass, PlayerID, glam::Vec2}; +use std::{collections::HashMap, random::random}; pub struct Pedestrians { pub players: HashMap<PlayerID, usize>, pub points: Vec<Vec2>, - pub spawn_delay_distr: rand_distr::Normal<f32>, + pub spawn_delay: f32, + pub spawn_delay_stdev: f32, pub cooldown: f32, pub speed: f32, } @@ -37,12 +38,12 @@ impl Entity for Pedestrians { fn tick(&mut self, c: EntityContext<'_>) -> Result<()> { self.cooldown -= c.dt; if self.cooldown <= 0. && self.players.len() < 32 { - let id = PlayerID(random()); + let id = PlayerID(random(..)); c.packet_in.push_back(PacketS::Join { name: "Pedestrian".to_string(), character: Character { - color: random(), - hairstyle: random(), + color: random(..), + hairstyle: random(..), headwear: 0, }, class: PlayerClass::Customer, @@ -50,7 +51,7 @@ impl Entity for Pedestrians { position: self.points.first().copied(), }); self.players.insert(id, 0); - self.cooldown += self.spawn_delay_distr.sample(&mut rng()).max(0.1); + self.cooldown += (self.spawn_delay + self.spawn_delay_stdev * random_gauss()).max(0.1); } let mut remove = Vec::new(); diff --git a/server/src/entity/tram.rs b/server/src/entity/tram.rs index 3186b650..391d16a9 100644 --- a/server/src/entity/tram.rs +++ b/server/src/entity/tram.rs @@ -1,3 +1,5 @@ +use std::random::random; + /* Hurry Curry! - a game about cooking Copyright (C) 2025 Hurry Curry! Contributors @@ -17,8 +19,7 @@ */ use super::{Entity, EntityContext}; use anyhow::Result; -use hurrycurry_protocol::{glam::Vec2, Character, PacketS, PlayerClass, PlayerID}; -use rand::random; +use hurrycurry_protocol::{Character, PacketS, PlayerClass, PlayerID, glam::Vec2}; pub struct Tram { pub length: usize, @@ -37,7 +38,7 @@ impl Entity for Tram { fn tick(&mut self, c: EntityContext<'_>) -> Result<()> { if self.ids.is_empty() { for i in 0..self.length { - let id = PlayerID(random()); + let id = PlayerID(random(..)); c.packet_in.push_back(PacketS::Join { name: format!("Tram {i}"), character: self.character, diff --git a/server/src/lib.rs b/server/src/lib.rs index da49f85d..2d3a62cc 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -15,7 +15,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -#![feature(if_let_guard, let_chains, iterator_try_collect, stmt_expr_attributes)] +#![feature(if_let_guard, iterator_try_collect, stmt_expr_attributes, random)] pub mod commands; pub mod entity; pub mod interaction; @@ -25,6 +25,7 @@ pub mod server; pub mod state; use hurrycurry_protocol::glam::Vec2; +use std::random::random; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ConnectionID(pub i64); @@ -43,3 +44,10 @@ impl InterpolateExt for f32 { *self = target + (*self - target) * (-dt).exp(); } } + +fn random_float() -> f32 { + random::<u32>(..) as f32 / u32::MAX as f32 +} +fn random_gauss() -> f32 { + [(); 12].map(|()| random_float()).iter().sum::<f32>() - 6. +} diff --git a/server/src/main.rs b/server/src/main.rs index 7ab19f30..be0bb005 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -47,6 +47,7 @@ pub(crate) struct Args { listen: SocketAddr, /// Enables submissions to the public server registry #[arg(long)] + #[cfg(feature = "register")] register: bool, /// Enables the mDNS responder for local network discovery #[cfg(feature = "mdns")] diff --git a/server/src/network/mdns.rs b/server/src/network/mdns.rs index b15a197a..590ad656 100644 --- a/server/src/network/mdns.rs +++ b/server/src/network/mdns.rs @@ -21,8 +21,7 @@ use get_if_addrs::get_if_addrs; use hurrycurry_protocol::VERSION; use log::{info, warn}; use mdns_sd::{ServiceDaemon, ServiceInfo}; -use rand::random; -use std::{collections::HashMap, net::SocketAddr, sync::Arc, time::Duration}; +use std::{collections::HashMap, net::SocketAddr, random::random, sync::Arc, time::Duration}; use tokio::{sync::RwLock, time::interval}; pub async fn mdns_loop(name: String, listen_addr: SocketAddr, state: Arc<RwLock<Server>>) { @@ -34,7 +33,7 @@ pub async fn mdns_loop(name: String, listen_addr: SocketAddr, state: Arc<RwLock< } }; let mut interval = interval(Duration::from_secs(60)); - let hostname = format!("hurrycurry-{}.local.", random::<u64>()); // TODO use system hostname + let hostname = format!("hurrycurry-{}.local.", random::<u64>(..)); // TODO use system hostname loop { interval.tick().await; if let Err(e) = update_service(&d, &state, &name, &hostname, listen_addr).await { diff --git a/server/src/network/register.rs b/server/src/network/register.rs index 8617af4d..47dac4a8 100644 --- a/server/src/network/register.rs +++ b/server/src/network/register.rs @@ -16,13 +16,13 @@ */ use crate::server::Server; -use anyhow::{bail, Result}; -use hurrycurry_protocol::{registry::Submission, VERSION}; +use anyhow::{Result, bail}; +use hurrycurry_protocol::{VERSION, registry::Submission}; use log::{debug, error, info, warn}; -use rand::random; -use reqwest::{header::USER_AGENT, Client, Url}; +use reqwest::{Client, Url, header::USER_AGENT}; use std::{ net::{IpAddr, Ipv4Addr, Ipv6Addr}, + random::random, str::FromStr, sync::Arc, time::Duration, @@ -56,7 +56,7 @@ impl Register { register_uri, players: 0, port, - secret: random(), + secret: random(..), state, ip4_client: if no4 { None diff --git a/server/src/server.rs b/server/src/server.rs index 3f54fe38..48439b5f 100644 --- a/server/src/server.rs +++ b/server/src/server.rs @@ -16,23 +16,23 @@ */ use crate::{ - entity::{construct_entity, Entities, EntityContext}, + ConnectionID, + entity::{Entities, EntityContext, construct_entity}, interaction::{interact, tick_slot}, + random_float, scoreboard::ScoreboardStore, - ConnectionID, }; use anyhow::{Context, Result}; -use hurrycurry_client_lib::{gamedata_index::GamedataIndex, Game, Involvement, Item, Player, Tile}; -use hurrycurry_data::{index::DataIndex, Serverdata}; -use hurrycurry_locale::{tre, TrError}; +use hurrycurry_client_lib::{Game, Involvement, Item, Player, Tile, gamedata_index::GamedataIndex}; +use hurrycurry_data::{Serverdata, index::DataIndex}; +use hurrycurry_locale::{TrError, tre}; use hurrycurry_protocol::{ - glam::{IVec2, Vec2}, - movement::MovementBase, Character, Gamedata, Hand, ItemLocation, Menu, MessageTimeout, PacketC, PacketS, PlayerClass, PlayerID, Score, TileIndex, + glam::{IVec2, Vec2}, + movement::MovementBase, }; use log::{info, warn}; -use rand::random; use std::{ collections::{HashMap, HashSet, VecDeque}, path::PathBuf, @@ -277,7 +277,7 @@ impl GameServerExt for Game { PlayerClass::Customer => serverdata.customer_spawn.unwrap_or(serverdata.chef_spawn), PlayerClass::Bot | PlayerClass::Chef => serverdata.chef_spawn, PlayerClass::Tram => Vec2::ZERO, // should always have custom location - }) + (Vec2::new(random(), random()) - 0.5); + }) + (Vec2::new(random_float(), random_float()) - 0.5); self.players.insert( id, Player { |