aboutsummaryrefslogtreecommitdiff
path: root/server/bot/src/algos/test.rs
blob: d17b079ddf3784fa3102f4b3a00d47318b36bcc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use crate::{
    pathfinding::{find_path_to_neighbour, 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_to_neighbour(&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,
        })
}