diff options
author | metamuffin <metamuffin@disroot.org> | 2024-08-11 17:26:58 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-08-11 17:26:58 +0200 |
commit | 34a5444dba1442f8676223f73a977cb643004953 (patch) | |
tree | c53842daf9d89da4d53ef4753d4a2a84b9076ec1 /server/bot/src/algos/test.rs | |
parent | 52d99b16534631e293a23ddbc18c4ea70b71392f (diff) | |
download | hurrycurry-34a5444dba1442f8676223f73a977cb643004953.tar hurrycurry-34a5444dba1442f8676223f73a977cb643004953.tar.bz2 hurrycurry-34a5444dba1442f8676223f73a977cb643004953.tar.zst |
bot helper functions
Diffstat (limited to 'server/bot/src/algos/test.rs')
-rw-r--r-- | server/bot/src/algos/test.rs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/server/bot/src/algos/test.rs b/server/bot/src/algos/test.rs new file mode 100644 index 00000000..17999f5e --- /dev/null +++ b/server/bot/src/algos/test.rs @@ -0,0 +1,51 @@ +use crate::{ + pathfinding::{find_path, Path}, + BotAlgo, BotInput, +}; +use hurrycurry_client_lib::Game; +use hurrycurry_protocol::{glam::IVec2, ItemIndex, Message, PlayerID}; +use log::info; + +#[derive(Default)] +pub struct Test { + path: Option<Path>, +} + +impl BotAlgo for Test { + fn tick(&mut self, me: PlayerID, game: &Game, _dt: f32) -> BotInput { + let Some(player) = game.players.get(&me) else { + return BotInput::default(); + }; + let pos = player.movement.position; + + if let Some(path) = &mut self.path { + let direction = path.next_direction(pos); + return BotInput { + direction, + boost: false, + interact: None, + }; + } else { + if let Some((item, near)) = find_demand(game) { + info!("demand {item:?} near {near}"); + if let Some(path) = find_path(&game.walkable, pos.as_ivec2(), near) { + self.path = Some(path); + } + } + } + BotInput::default() + } +} + +fn find_demand(game: &Game) -> Option<(ItemIndex, IVec2)> { + game.players + .iter() + .find_map(|(_, pl)| match &pl.communicate_persist { + Some(Message::Item(item)) => { + let pos = pl.movement.position.as_ivec2(); + let t = pos; + Some((*item, t)) + } + _ => None, + }) +} |