diff options
| -rw-r--r-- | server/bot/src/algos/mod.rs | 2 | ||||
| -rw-r--r-- | server/bot/src/lib.rs | 6 | ||||
| -rw-r--r-- | server/src/state.rs | 16 | 
3 files changed, 23 insertions, 1 deletions
| 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<dyn crate::BotAlgo>)] = &[ +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<dyn BotAlgo + Send + Sync + 'static>;  pub trait BotAlgo {      fn tick(&mut self, me: PlayerID, game: &Game, dt: f32) -> BotInput;  } + +impl<T: BotAlgo + ?Sized> BotAlgo for Box<T> { +    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<String> },      /// 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(())      } | 
