aboutsummaryrefslogtreecommitdiff
path: root/server/bot/src/algos/simple.rs
blob: 125f7e0b74d1385ffc202049a4fdff648bc01c4f (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use crate::{
    pathfinding::{find_path_to_neighbour, Path},
    BotAlgo, BotInput,
};
use hurrycurry_client_lib::Game;
use hurrycurry_protocol::{glam::IVec2, ItemIndex, Message, PlayerID};

#[derive(Default)]
pub struct Simple {
    path: Option<(Path, IVec2)>,
    cooldown: f32,
}

struct SimpleContext<'a> {
    game: &'a Game,
    me: PlayerID,
    own_position: IVec2,
    state: &'a mut Simple,
}

impl BotAlgo for Simple {
    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 self.cooldown > 0. {
            self.cooldown -= _dt;
        }

        if let Some((path, target)) = &mut self.path {
            let direction = path.next_direction(pos);
            let done = path.is_done();
            let target = *target;
            if done {
                self.path = None;
                self.cooldown = 1.;
            }
            return BotInput {
                direction,
                boost: false,
                interact: if done { Some(target) } else { None },
            };
        }

        SimpleContext {
            game,
            own_position: pos.as_ivec2(),
            me,
            state: self,
        }
        .update();

        BotInput::default()
    }
}

impl SimpleContext<'_> {
    pub fn is_hand_item(&self, item: ItemIndex) -> bool {
        self.game
            .players
            .get(&self.me)
            .map_or(false, |p| p.item.as_ref().map_or(false, |i| i.kind == item))
    }
    fn find_demand(&self) -> Option<(ItemIndex, IVec2)> {
        self.game
            .players
            .iter()
            .find_map(|(_, pl)| match &pl.communicate_persist {
                Some(Message::Item(item)) => {
                    let pos = pl.movement.position.as_ivec2();
                    [IVec2::X, IVec2::Y, -IVec2::X, -IVec2::Y]
                        .into_iter()
                        .find(|off| {
                            self.game
                                .tiles
                                .get(&(pos + *off))
                                .map_or(false, |t| self.game.data.tile_interact[t.kind.0])
                        })
                        .map(|off| pos + off)
                        .map(|pos| (*item, pos))
                }
                _ => None,
            })
    }
    pub fn aquire_item(&mut self, item: ItemIndex) -> bool {
        if self.is_hand_item(item) {
            return true;
        }

        false
    }
    pub fn update(&mut self) {
        if let Some((item, table)) = self.find_demand() {
            if !self.aquire_item(item) {
                return;
            }
            if let Some(path) =
                find_path_to_neighbour(&self.game.walkable, self.own_position, table)
            {
                self.state.path = Some((path, table));
            }
        }
    }
}

// fn find_item_on_map(game: &Game, item: ItemIndex) -> Option<IVec2> {}