From 49a122baa6dfa19b5c4060b983d6601041a17519 Mon Sep 17 00:00:00 2001 From: bal7hazar Date: Mon, 16 Sep 2024 14:44:41 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20pseudo=20randomness=20into=20?= =?UTF-8?q?pathfinding=20decision?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/map/src/helpers/astar.cairo | 37 ++++++++++++++++------------ crates/map/src/map.cairo | 30 +++++++++++----------- crates/map/src/types/direction.cairo | 22 +++++++++++++++++ 3 files changed, 58 insertions(+), 31 deletions(-) diff --git a/crates/map/src/helpers/astar.cairo b/crates/map/src/helpers/astar.cairo index 9425d1d..c60bd6b 100644 --- a/crates/map/src/helpers/astar.cairo +++ b/crates/map/src/helpers/astar.cairo @@ -8,8 +8,9 @@ use core::dict::{Felt252Dict, Felt252DictTrait}; use origami_map::helpers::heap::{Heap, HeapTrait}; use origami_map::helpers::bitmap::Bitmap; +use origami_map::helpers::seeder::Seeder; use origami_map::types::node::{Node, NodeTrait}; -use origami_map::types::direction::Direction; +use origami_map::types::direction::{Direction, DirectionTrait}; #[generate_trait] pub impl Astar of AstarTrait { @@ -44,20 +45,26 @@ pub impl Astar of AstarTrait { break; } // [Compute] Evaluate the neighbors for all 4 directions - if Self::check(grid, width, height, current.position, Direction::North, ref visited) { - let neighbor_position = current.position + width; + let seed = Seeder::shuffle(grid, current.position.into()); + let mut directions = DirectionTrait::compute_shuffled_directions(seed); + let direction: Direction = DirectionTrait::pop_front(ref directions); + if Self::check(grid, width, height, current.position, direction, ref visited) { + let neighbor_position = direction.next(current.position, width); Self::assess(width, neighbor_position, current, target, ref heap); } - if Self::check(grid, width, height, current.position, Direction::East, ref visited) { - let neighbor_position = current.position - 1; + let direction: Direction = DirectionTrait::pop_front(ref directions); + if Self::check(grid, width, height, current.position, direction, ref visited) { + let neighbor_position = direction.next(current.position, width); Self::assess(width, neighbor_position, current, target, ref heap); } - if Self::check(grid, width, height, current.position, Direction::South, ref visited) { - let neighbor_position = current.position - width; + let direction: Direction = DirectionTrait::pop_front(ref directions); + if Self::check(grid, width, height, current.position, direction, ref visited) { + let neighbor_position = direction.next(current.position, width); Self::assess(width, neighbor_position, current, target, ref heap); } - if Self::check(grid, width, height, current.position, Direction::West, ref visited) { - let neighbor_position = current.position + 1; + let direction: Direction = DirectionTrait::pop_front(ref directions); + if Self::check(grid, width, height, current.position, direction, ref visited) { + let neighbor_position = direction.next(current.position, width); Self::assess(width, neighbor_position, current, target, ref heap); } }; @@ -246,10 +253,10 @@ mod test { // 0 0 0 1 1 1 1 ┌───x 0 0 0 0 0 0 0 0 // 0 0 0 0 1 1 1 │ 0 0 0 1 0 0 1 0 0 0 // 0 0 0 1 1 1 1 │ 0 0 0 1 1 1 1 1 0 0 - // 0 0 1 1 1 1 1 └─────────────────┐ 0 - // 0 0 0 1 1 1 1 0 1 1 1 0 1 1 1 1 │ 0 - // 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 │ 0 - // 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 s 0 + // 0 0 1 1 1 1 1 └─────────────┐ 1 1 0 + // 0 0 0 1 1 1 1 0 1 1 1 0 1 1 └─┐ 1 0 + // 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 │ 1 0 + // 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 └─s 0 // 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 // 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 // 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 @@ -261,9 +268,7 @@ mod test { let mut path = Astar::search(grid, width, height, from, to); assert_eq!( path, - array![ - 170, 171, 172, 154, 136, 118, 117, 116, 115, 114, 113, 112, 111, 110, 109, 91, 73 - ] + array![170, 171, 172, 154, 136, 118, 117, 116, 115, 114, 113, 112, 94, 93, 75, 74, 56] .span() ); } diff --git a/crates/map/src/map.cairo b/crates/map/src/map.cairo index 8f1b469..2515b09 100644 --- a/crates/map/src/map.cairo +++ b/crates/map/src/map.cairo @@ -326,25 +326,25 @@ mod tests { #[test] fn test_map_search_path() { - // 000000000000000000 - // 000000000011000000 - // 000000000111001100 - // 000001000111111110 - // 000011100011111110 - // 000011111111111110 - // 0000100111x─┐11110 - // 000010011101│11110 - // 000011111111│11110 - // 000011111111│11110 - // 000011111111│11110 - // 000011111111│00000 - // 000001111111x00000 - // 000000000000000000 + // 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + // 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 + // 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 1 0 0 + // 0 0 0 0 0 1 0 0 0 1 1 1 1 1 1 1 1 0 + // 0 0 0 0 1 1 1 0 0 0 1 1 1 1 1 1 1 0 + // 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 + // 0 0 0 0 1 0 0 1 1 1 x─┐ 1 1 1 1 1 0 + // 0 0 0 0 1 0 0 1 1 1 0 │ 1 1 1 1 1 0 + // 0 0 0 0 1 1 1 1 1 1 1 │ 1 1 1 1 1 0 + // 0 0 0 0 1 1 1 1 1 1 1 │ 1 1 1 1 1 0 + // 0 0 0 0 1 1 1 1 1 1 1 │ 1 1 1 1 1 0 + // 0 0 0 0 1 1 1 1 1 1 1 │ 1 0 0 0 0 0 + // 0 0 0 0 0 1 1 1 1 1 1 └─x 0 0 0 0 0 + // 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 let width = 18; let height = 14; let steps: u16 = 2 * width.into() * height.into(); let mut map: Map = MapTrait::new_random_walk(width, height, steps, SEED); let path = map.search_path(23, 133); - assert_eq!(path, array![133, 132, 131, 113, 95, 77, 59, 41].span()); + assert_eq!(path, array![133, 132, 114, 96, 78, 60, 42, 24].span()); } } diff --git a/crates/map/src/types/direction.cairo b/crates/map/src/types/direction.cairo index d6faf2a..cc4ef7f 100644 --- a/crates/map/src/types/direction.cairo +++ b/crates/map/src/types/direction.cairo @@ -75,6 +75,28 @@ pub impl DirectionImpl of DirectionTrait { directions /= DIRECTION_SIZE; direciton.into() } + + /// Get the next direction from a given position and direction. + /// # Arguments + /// * `self` - The current direction + /// * `position` - The current position + /// * `width` - The width of the grid + /// # Returns + /// * The next position + #[inline] + fn next(self: Direction, position: u8, width: u8) -> u8 { + match self { + Direction::None => position, + Direction::NorthWest => position + width + 1, + Direction::North => position + width, + Direction::NorthEast => position + width - 1, + Direction::East => position - 1, + Direction::SouthEast => position - width - 1, + Direction::South => position - width, + Direction::SouthWest => position - width + 1, + Direction::West => position + 1, + } + } } pub impl DirectionIntoFelt252 of Into {