aboutsummaryrefslogtreecommitdiff
path: root/server/bot/src/lib.rs
blob: 078314b1627da99738ce03f3df5a34fec3f4a2cb (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
/*
    Hurry Curry! - a game about cooking
    Copyright (C) 2025 Hurry Curry! Contributors

    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/>.

*/
#![feature(random)]
pub mod algos;
pub mod pathfinding;
pub mod step;

use hurrycurry_game_core::Game;
use hurrycurry_protocol::PacketS;
#[cfg(feature = "debug_events")]
use hurrycurry_protocol::{PlayerID, glam::Vec3};
use std::{collections::VecDeque, random::random};

pub struct PacketSink<'a> {
    buf: &'a mut VecDeque<PacketS>,
}
impl<'a> PacketSink<'a> {
    pub fn new(buf: &'a mut VecDeque<PacketS>) -> Self {
        Self { buf }
    }
    pub fn push(&mut self, p: PacketS) {
        self.buf.push_back(p);
    }
}

pub type DynBotAlgo = Box<dyn BotAlgo + Send + Sync + 'static>;
pub trait BotAlgo {
    fn tick(&mut self, out: PacketSink, game: &Game, dt: f32);
    fn is_finished(&self) -> bool {
        false
    }
}
impl<T: BotAlgo + ?Sized> BotAlgo for Box<T> {
    fn tick(&mut self, out: PacketSink, game: &Game, dt: f32) {
        (**self).tick(out, game, dt)
    }
    fn is_finished(&self) -> bool {
        (**self).is_finished()
    }
}

fn random_float() -> f32 {
    random::<u32>(..) as f32 / u32::MAX as f32
}

#[cfg(feature = "debug_events")]
fn debug_player_color(d: PlayerID) -> Vec3 {
    use std::f32::consts::TAU;
    Vec3::new(
        (d.0 as f32 + TAU / 3. * 0.).sin(),
        (d.0 as f32 + TAU / 3. * 1.).sin(),
        (d.0 as f32 + TAU / 3. * 2.).sin(),
    )
}