Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/pathfinding #95

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
pull_request:

env:
DOJO_VERSION: v1.0.0-alpha.11
DOJO_VERSION: v1.0.0-alpha.12
SCARB_VERSION: 2.7.0

jobs:
Expand Down
18 changes: 9 additions & 9 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ source = "git+https://github.com/dojoengine/cubit?branch=update_to_2.7.0#075bf5a
[[package]]
name = "dojo"
version = "1.0.0-alpha.4"
source = "git+https://github.com/dojoengine/dojo?tag=v1.0.0-alpha.11#fbff45b249e2b931f5007edf2d526beef7dc553f"
source = "git+https://github.com/dojoengine/dojo?tag=v1.0.0-alpha.12#b391948911356cbf8a91daa93314809c87411098"
dependencies = [
"dojo_plugin",
]
Expand All @@ -21,44 +21,44 @@ source = "git+https://github.com/dojoengine/dojo?rev=f15def33#f15def330c0d099e79

[[package]]
name = "origami_algebra"
version = "1.0.0-alpha.11"
version = "1.0.0-alpha.12"
dependencies = [
"cubit",
]

[[package]]
name = "origami_defi"
version = "1.0.0-alpha.11"
version = "1.0.0-alpha.12"
dependencies = [
"cubit",
]

[[package]]
name = "origami_governance"
version = "1.0.0-alpha.11"
version = "1.0.0-alpha.12"
dependencies = [
"dojo",
]

[[package]]
name = "origami_map"
version = "1.0.0-alpha.11"
version = "1.0.0-alpha.12"

[[package]]
name = "origami_random"
version = "1.0.0-alpha.11"
version = "1.0.0-alpha.12"

[[package]]
name = "origami_rating"
version = "1.0.0-alpha.11"
version = "1.0.0-alpha.12"

[[package]]
name = "origami_security"
version = "1.0.0-alpha.11"
version = "1.0.0-alpha.12"

[[package]]
name = "origami_token"
version = "1.0.0-alpha.11"
version = "1.0.0-alpha.12"
dependencies = [
"dojo",
]
4 changes: 2 additions & 2 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ members = [
]

[workspace.package]
version = "1.0.0-alpha.11"
version = "1.0.0-alpha.12"

[workspace.dependencies]
dojo = { git = "https://github.com/dojoengine/dojo", tag = "v1.0.0-alpha.11" }
dojo = { git = "https://github.com/dojoengine/dojo", tag = "v1.0.0-alpha.12" }
# dojo = { path = "../dojo/crates/dojo-core" }
cubit = { git = "https://github.com/dojoengine/cubit", branch = "update_to_2.7.0" }
starknet = "^2.7.0"
Expand Down
104 changes: 82 additions & 22 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 All @@ -36,29 +37,34 @@ pub impl Astar of AstarTrait {
let mut visited: Felt252Dict<bool> = Default::default();
heap.add(start);
// [Compute] Evaluate the path until the target is reached
while !heap.is_empty() {
while let Option::Some(current) = heap.pop_front() {
// [Compute] Get the less expensive node
let current: Node = heap.pop_front().unwrap();
visited.insert(current.position.into(), true);
// [Check] Stop if we reached the target
if current.position == target.position {
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());
bal7hazar marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -91,15 +97,15 @@ pub impl Astar of AstarTrait {
Direction::North => (y < height - 1)
&& (Bitmap::get(grid, position + width) == 1)
&& !visisted.get((position + width).into()),
Direction::East => (x < width - 1)
&& (Bitmap::get(grid, position + 1) == 1)
&& !visisted.get((position + 1).into()),
Direction::East => (x > 0)
&& (Bitmap::get(grid, position - 1) == 1)
&& !visisted.get((position - 1).into()),
Direction::South => (y > 0)
&& (Bitmap::get(grid, position - width) == 1)
&& !visisted.get((position - width).into()),
Direction::West => (x > 0)
&& (Bitmap::get(grid, position - 1) == 1)
&& !visisted.get((position - 1).into()),
Direction::West => (x < width - 1)
&& (Bitmap::get(grid, position + 1) == 1)
&& !visisted.get((position + 1).into()),
_ => false,
}
}
Expand Down Expand Up @@ -247,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 └───┐ 1 1 1 1 1 1 1 0
// 0 0 0 1 1 1 1 0 1 1 0 1 1 1 1 1 0
// 0 0 0 0 1 1 1 1 1 └─┐ 1 1 1 1 1 1 0
// 0 0 0 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 @@ -262,7 +268,61 @@ mod test {
let mut path = Astar::search(grid, width, height, from, to);
assert_eq!(
path,
array![170, 171, 172, 154, 136, 118, 117, 116, 98, 80, 79, 61, 60, 59, 58, 57, 56]
array![170, 171, 172, 154, 136, 118, 117, 116, 115, 114, 113, 112, 94, 93, 75, 74, 56]
.span()
);
}

#[test]
fn test_astar_search_issue() {
// 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
// 0 1 1 1 1 1 1 1 0 1 1 0 0 1 0
// 0 1 1 1 1 1 1 1 0 0 E * 1 1 0 <-- 180 + 4 = 184
// 0 1 1 1 1 1 1 1 1 1 0 * 0 1 0
// 0 1 1 1 1 1 1 1 1 1 0 * 0 1 0
// 0 1 1 1 0 0 1 0 0 0 1 * * 0 0
// 0 1 1 1 1 0 0 1 1 1 1 0 * * 0
// 1 1 1 1 0 1 1 1 0 0 1 1 0 * 1
// 0 1 1 1 1 0 0 0 0 1 0 0 * * 0
// 0 1 1 1 0 0 * * * * * * * 0 0
// 0 1 1 1 0 0 * 1 0 1 1 1 0 0 0
// 0 1 1 * * * * 0 1 0 1 1 1 1 0
// 0 1 1 S 1 1 1 0 1 0 1 1 1 1 0 <-- 30 + 11 = 41
// 0 1 1 1 1 1 1 1 1 1 0 1 1 1 0
// 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
let grid: felt252 = 0x201fd93f9e7fd4ffa9c8e3cf6f736f099cfe39b87ebcfd79ff70080;
let width = 15;
let height = 15;
let from = 41;
let to = 184;
let mut path = Astar::search(grid, width, height, from, to);
assert_eq!(
path,
array![
184,
183,
168,
153,
138,
137,
122,
121,
106,
91,
92,
77,
78,
79,
80,
81,
82,
83,
68,
53,
54,
55,
56,
]
.span()
);
}
Expand Down
Loading