diff options
Diffstat (limited to 'server/bot/src/main.rs')
-rw-r--r-- | server/bot/src/main.rs | 47 |
1 files changed, 13 insertions, 34 deletions
diff --git a/server/bot/src/main.rs b/server/bot/src/main.rs index 08d17d19..9e3fbee1 100644 --- a/server/bot/src/main.rs +++ b/server/bot/src/main.rs @@ -15,36 +15,26 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -#![feature(isqrt)] -pub mod algos; -pub mod pathfinding; - use anyhow::Result; +use bot::{algos::ALGO_CONSTRUCTORS, BotAlgo, BotInput}; use clap::Parser; use hurrycurry_client_lib::{network::sync::Network, Game}; -use hurrycurry_protocol::{ - glam::{IVec2, Vec2}, - PacketC, PacketS, PlayerID, -}; +use hurrycurry_protocol::{PacketC, PacketS, PlayerID}; use log::warn; use std::{thread::sleep, time::Duration}; -#[derive(Default, Clone, Copy)] -pub struct BotInput { - direction: Vec2, - boost: bool, - interact: Option<IVec2>, -} -pub trait BotAlgo { - fn tick(&mut self, me: PlayerID, game: &Game, dt: f32) -> BotInput; -} - #[derive(Parser)] struct Args { algo: String, address: String, } +pub struct BotDriver { + pub interacting: bool, + id: PlayerID, + state: Box<dyn BotAlgo>, +} + fn main() -> Result<()> { env_logger::init_from_env("LOG"); rustls::crypto::ring::default_provider() @@ -53,15 +43,6 @@ fn main() -> Result<()> { let args = Args::parse(); - let algo = args.algo.to_owned(); - let init_algo = move || -> Box<dyn BotAlgo> { - match algo.as_str() { - "test" => Box::new(algos::Test::default()), - "simple" => Box::new(algos::Simple::default()), - _ => panic!("unknown algo {algo:?}"), - } - }; - let mut network = Network::connect(&args.address)?; let mut game = Game::default(); @@ -83,7 +64,11 @@ fn main() -> Result<()> { PacketC::Joined { id } => bots.push(BotDriver { id: *id, interacting: false, - state: init_algo(), + state: ALGO_CONSTRUCTORS + .iter() + .find(|(n, _)| n == &args.algo) + .map(|(_, c)| c()) + .expect(&format!("unknown algo {:?}", args.algo)), }), PacketC::Error { message } => { warn!("server error message: {message}"); @@ -117,9 +102,3 @@ fn main() -> Result<()> { sleep(Duration::from_secs_f32(dt)); } } - -pub struct BotDriver { - pub interacting: bool, - id: PlayerID, - state: Box<dyn BotAlgo>, -} |