From f24a9001ef0fafcdbcf20e23eef67f4b71dcf3a5 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 13 Aug 2024 19:44:23 +0200 Subject: summonbot command --- server/bot/src/algos/mod.rs | 2 +- server/bot/src/lib.rs | 6 ++++++ server/src/state.rs | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) (limited to 'server') diff --git a/server/bot/src/algos/mod.rs b/server/bot/src/algos/mod.rs index d3aba8f4..54cdd495 100644 --- a/server/bot/src/algos/mod.rs +++ b/server/bot/src/algos/mod.rs @@ -25,7 +25,7 @@ pub use simple::Simple; pub use test::Test; pub use waiter::Waiter; -pub const ALGO_CONSTRUCTORS: &'static [(&'static str, fn() -> Box)] = &[ +pub const ALGO_CONSTRUCTORS: &'static [(&'static str, fn() -> crate::DynBotAlgo)] = &[ ("test", || Box::new(Test::default())), ("simple", || Box::new(Simple::default())), ("waiter", || Box::new(Waiter::default())), diff --git a/server/bot/src/lib.rs b/server/bot/src/lib.rs index b2334333..ce8ac8f2 100644 --- a/server/bot/src/lib.rs +++ b/server/bot/src/lib.rs @@ -38,3 +38,9 @@ pub type DynBotAlgo = Box; pub trait BotAlgo { fn tick(&mut self, me: PlayerID, game: &Game, dt: f32) -> BotInput; } + +impl BotAlgo for Box { + fn tick(&mut self, me: PlayerID, game: &Game, dt: f32) -> BotInput { + (**self).tick(me, game, dt) + } +} diff --git a/server/src/state.rs b/server/src/state.rs index 343d130f..2b112913 100644 --- a/server/src/state.rs +++ b/server/src/state.rs @@ -17,11 +17,13 @@ */ use crate::{ data::DataIndex, + entity::bot::BotDriver, server::{Server, ServerState}, ConnectionID, }; use anyhow::{anyhow, bail, Result}; use clap::{Parser, ValueEnum}; +use hurrycurry_bot::algos::ALGO_CONSTRUCTORS; use hurrycurry_client_lib::Game; use hurrycurry_protocol::{Message, PacketC, PacketS, PlayerID}; use log::{debug, trace}; @@ -70,6 +72,8 @@ enum Command { Item { name: String }, /// Reload the resource index ReloadIndex, + #[clap(alias = "summon-bot", alias = "spawn-bot")] + CreateBot { algo: String, name: Option }, /// Reload the current map #[clap(alias = "r")] Reload, @@ -300,6 +304,18 @@ impl State { }) .ok(); } + Command::CreateBot { algo, name } => { + let (aname, cons) = ALGO_CONSTRUCTORS + .iter() + .find(|(name, _)| *name == algo.as_str()) + .ok_or(anyhow!("algo name unknown"))?; + let algo = cons(); + self.server.entities.push(Box::new(BotDriver::new( + format!("{}-bot", name.unwrap_or((*aname).to_owned())), + 51, + algo, + ))); + } } Ok(()) } -- cgit v1.2.3-70-g09d2