Skip to content

Commit

Permalink
✨ Add pseudo randomness into pathfinding decision
Browse files Browse the repository at this point in the history
  • Loading branch information
bal7hazar committed Sep 16, 2024
1 parent 694d35f commit 49a122b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 31 deletions.
37 changes: 21 additions & 16 deletions crates/map/src/helpers/astar.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
};
Expand Down Expand Up @@ -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
Expand All @@ -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()
);
}
Expand Down
30 changes: 15 additions & 15 deletions crates/map/src/map.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
22 changes: 22 additions & 0 deletions crates/map/src/types/direction.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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<Direction, felt252> {
Expand Down

0 comments on commit 49a122b

Please sign in to comment.