summaryrefslogtreecommitdiff
path: root/server/src/customer/pathfinding.rs
blob: 5056975e3974807e4db7d79c11346688f989eeb9 (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
/*
    Undercooked - 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 super::movement::MovementBase;
use crate::protocol::PacketS;
use glam::{IVec2, Vec2};
use log::debug;
use std::{
    cmp::Ordering,
    collections::{BinaryHeap, HashMap, HashSet},
};

pub struct Path(Vec<Vec2>);

impl Path {
    pub fn execute_tick(
        &mut self,
        player: &mut MovementBase,
        walkable: &HashSet<IVec2>,
        dt: f32,
    ) -> PacketS {
        if let Some(next) = self.0.last().copied() {
            debug!("next {next}");
            if next.distance(player.position) < if self.0.len() == 1 { 0.1 } else { 0.6 } {
                self.0.pop();
            }
            player.update(
                &walkable,
                (next - player.position).normalize_or_zero() * 0.5,
                dt,
            )
        } else {
            player.update(&walkable, Vec2::ZERO, dt)
        }
    }
    pub fn is_done(&self) -> bool {
        self.0.is_empty()
    }
}

pub fn find_path(walkable: &HashSet<IVec2>, from: IVec2, to: IVec2) -> Option<Path> {
    #[derive(Debug, PartialEq, Eq)]
    struct Open(i32, IVec2, IVec2);
    impl PartialOrd for Open {
        fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
            self.0.partial_cmp(&other.0)
        }
    }
    impl Ord for Open {
        fn cmp(&self, other: &Self) -> Ordering {
            self.0.cmp(&other.0)
        }
    }

    let mut visited = HashMap::new();
    let mut open = BinaryHeap::new();
    open.push(Open(1, from, from));

    loop {
        let Some(Open(_, p, f)) = open.pop() else {
            eprintln!("{visited:?}");
            return None;
        };
        if visited.contains_key(&p) {
            continue;
        }
        visited.insert(p, f);
        if p == to {
            break;
        }
        for d in [IVec2::NEG_X, IVec2::NEG_Y, IVec2::X, IVec2::Y] {
            let n = p + d;
            if walkable.contains(&n) {
                open.push(Open(-d.distance_squared(to), n, p));
            }
        }
    }

    let mut path = Vec::new();
    let mut c = to;
    loop {
        path.push(c.as_vec2() + 0.5);
        let cn = visited[&c];
        if cn == c {
            break;
        }
        c = cn
    }
    Some(Path(path))
}