diff options
| -rw-r--r-- | data/maps/sushibar.yaml | 2 | ||||
| -rw-r--r-- | server/src/customer/pathfinding.rs | 25 | ||||
| -rw-r--r-- | server/src/lib.rs | 2 | 
3 files changed, 17 insertions, 12 deletions
| diff --git a/data/maps/sushibar.yaml b/data/maps/sushibar.yaml index 4023d873..b96dcafb 100644 --- a/data/maps/sushibar.yaml +++ b/data/maps/sushibar.yaml @@ -17,7 +17,7 @@ map:      - "*''''*''''*''''''''*''"      - "'''*'''''''**''''''''*"      - "''█████████████'X'''*'" -    - "''█RGHI..#c...█'''''**" +    - "''█R.....#c...█'''''**"      - "''█T.....#c...d____'''"      - "'*█F..S..#c...d____'*'"      - "*'█L..S..#c...█''__'''" diff --git a/server/src/customer/pathfinding.rs b/server/src/customer/pathfinding.rs index 2fe938a9..2b9f98f4 100644 --- a/server/src/customer/pathfinding.rs +++ b/server/src/customer/pathfinding.rs @@ -57,7 +57,7 @@ impl Path {  pub fn find_path(walkable: &HashSet<IVec2>, from: IVec2, to: IVec2) -> Option<Path> {      #[derive(Debug, PartialEq, Eq)] -    struct Open(i32, IVec2, IVec2); +    struct Open(i32, IVec2, IVec2, i32);      impl PartialOrd for Open {          fn partial_cmp(&self, other: &Self) -> Option<Ordering> {              self.0.partial_cmp(&other.0) @@ -71,23 +71,28 @@ pub fn find_path(walkable: &HashSet<IVec2>, from: IVec2, to: IVec2) -> Option<Pa      let mut visited = HashMap::new();      let mut open = BinaryHeap::new(); -    open.push(Open(1, from, from)); +    open.push(Open(1, from, from, 0));      loop { -        let Some(Open(_, p, f)) = open.pop() else { +        let Some(Open(_, pos, f, distance)) = open.pop() else {              return None;          }; -        if visited.contains_key(&p) { +        if visited.contains_key(&pos) {              continue;          } -        visited.insert(p, f); -        if p == to { +        visited.insert(pos, f); +        if pos == 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)); +        for dir in [IVec2::NEG_X, IVec2::NEG_Y, IVec2::X, IVec2::Y] { +            let next = pos + dir; +            if walkable.contains(&next) { +                open.push(Open( +                    -(distance + next.distance_squared(to).isqrt()), +                    next, +                    pos, +                    distance + 1, +                ));              }          }      } diff --git a/server/src/lib.rs b/server/src/lib.rs index 6d0d4e26..afd6f1db 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -15,7 +15,7 @@      along with this program.  If not, see <https://www.gnu.org/licenses/>.  */ -#![feature(if_let_guard, map_many_mut, let_chains, iterator_try_collect)] +#![feature(if_let_guard, map_many_mut, let_chains, iterator_try_collect, isqrt)]  pub mod customer;  pub mod data;  pub mod entity; | 
