summaryrefslogtreecommitdiff
path: root/server/bot/src/algos/test.rs
blob: 7cfafc2927abc3736ba674430deb03bda6f08a2e (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
/*
    Hurry Curry! - a game about cooking
    Copyright 2024 metamuffin

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3 of the License only.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

*/
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, dt);
            return BotInput {
                direction,
                boost: false,
                interact: None,
                ..Default::default()
            };
        } 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,
        })
}