diff options
Diffstat (limited to 'server/bot')
| -rw-r--r-- | server/bot/Cargo.toml | 1 | ||||
| -rw-r--r-- | server/bot/src/algos/customer.rs | 11 | ||||
| -rw-r--r-- | server/bot/src/algos/frank.rs | 6 | ||||
| -rw-r--r-- | server/bot/src/lib.rs | 9 |
4 files changed, 15 insertions, 12 deletions
diff --git a/server/bot/Cargo.toml b/server/bot/Cargo.toml index c78410b3..e73be6e9 100644 --- a/server/bot/Cargo.toml +++ b/server/bot/Cargo.toml @@ -11,6 +11,7 @@ anyhow = "1.0.99" env_logger = "0.11.8" rustls = { version = "0.23.31", features = ["ring"] } clap = { version = "4.5.47", features = ["derive"] } +rand = "0.10.0" [features] # default = ["debug_events"] diff --git a/server/bot/src/algos/customer.rs b/server/bot/src/algos/customer.rs index 0ed1ba9a..fdd911dc 100644 --- a/server/bot/src/algos/customer.rs +++ b/server/bot/src/algos/customer.rs @@ -18,7 +18,7 @@ use crate::{ BotAlgo, PacketSink, pathfinding::{HoldLocation, Path, find_path}, - random_float, + random_usize, }; use hurrycurry_game_core::Game; use hurrycurry_protocol::{ @@ -26,7 +26,6 @@ use hurrycurry_protocol::{ glam::{IVec2, Vec2}, }; use log::debug; -use std::random::random; #[derive(Debug, Clone)] pub struct Customer { @@ -109,7 +108,8 @@ impl CustomerState { .flatten() .copied() .collect::<Vec<_>>(); //? maybe opt alloc - if let Some(&chair) = chairs.get(random::<usize>(..) % chairs.len().max(1)) + if let Some(&chair) = + chairs.get(random_usize(&mut rand::rng()) % chairs.len().max(1)) && let Some(path) = find_path(game, pos.as_ivec2(), chair) { debug!("{me:?} -> entering"); @@ -131,10 +131,11 @@ impl CustomerState { *ticks += 1; let check = *ticks % 10 == 0; if path.is_done() { - let demand = DemandIndex(random::<usize>(..) % game.data.demands.len()); + let demand = + DemandIndex(random_usize(&mut rand::rng()) % game.data.demands.len()); let requested_item = game.data.demands[demand.0].input; debug!("{me:?} -> waiting"); - let timeout = 90. + random_float() * 60.; + let timeout = 90. + rand::random::<f32>() * 60.; let mut facing = Vec2::ZERO; for off in [IVec2::NEG_X, IVec2::NEG_Y, IVec2::X, IVec2::Y] { if game diff --git a/server/bot/src/algos/frank.rs b/server/bot/src/algos/frank.rs index 8bd6d17b..156d655d 100644 --- a/server/bot/src/algos/frank.rs +++ b/server/bot/src/algos/frank.rs @@ -21,7 +21,7 @@ use crate::{ }; use hurrycurry_game_core::Game; use hurrycurry_protocol::{Message, PacketS, PlayerClass, PlayerID, glam::Vec2}; -use std::random::random; +use rand::{random, seq::IndexedRandom}; pub struct Frank { me: PlayerID, @@ -86,7 +86,7 @@ impl BotAlgo for Frank { out.push(PacketS::Communicate { player: self.me, message: Some(Message::Translation { - id: format!("s.bot.frank.line.{}", random::<u32>(..) % 8), + id: format!("s.bot.frank.line.{}", random::<u32>() % 8), params: vec![Message::Text(player.name.clone())], }), timeout: Some(3.), @@ -122,5 +122,5 @@ fn find_chef(game: &Game, me: PlayerID) -> Option<PlayerID> { .filter(|(i, p)| p.class == PlayerClass::Chef && **i != me) .map(|(i, _)| *i) .collect::<Vec<_>>(); - chefs.get(random::<usize>(..) % chefs.len().max(1)).copied() + chefs.choose(&mut rand::rng()).copied() } diff --git a/server/bot/src/lib.rs b/server/bot/src/lib.rs index 078314b1..31f2132a 100644 --- a/server/bot/src/lib.rs +++ b/server/bot/src/lib.rs @@ -15,7 +15,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -#![feature(random)] + pub mod algos; pub mod pathfinding; pub mod step; @@ -24,7 +24,8 @@ use hurrycurry_game_core::Game; use hurrycurry_protocol::PacketS; #[cfg(feature = "debug_events")] use hurrycurry_protocol::{PlayerID, glam::Vec3}; -use std::{collections::VecDeque, random::random}; +use rand::{Rng, RngExt}; +use std::{collections::VecDeque}; pub struct PacketSink<'a> { buf: &'a mut VecDeque<PacketS>, @@ -54,8 +55,8 @@ impl<T: BotAlgo + ?Sized> BotAlgo for Box<T> { } } -fn random_float() -> f32 { - random::<u32>(..) as f32 / u32::MAX as f32 +fn random_usize<T: Rng>(rng: &mut T) -> usize { + usize::from_ne_bytes(rng.random()) } #[cfg(feature = "debug_events")] |