Skip to content

Commit 7ddd020

Browse files
committed
.
1 parent ebf73cf commit 7ddd020

File tree

4 files changed

+94
-9
lines changed

4 files changed

+94
-9
lines changed

packages/demo/demo.astar.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { createCanvas } from "./canvas";
2+
import "./menu";
3+
import { grid } from "./sample";
4+
5+
(async () => {
6+
const api = await import("@snk/solver-r");
7+
8+
const g = api.IGrid.create(grid.width, grid.height, grid.data);
9+
const path = api.iastar(
10+
g,
11+
api.IPoint.create(-1, 0),
12+
api.IPoint.create(47, 4),
13+
);
14+
15+
{
16+
const { canvas, draw, highlightCell } = createCanvas(g);
17+
document.body.appendChild(canvas);
18+
19+
draw({ width: g.width, height: g.height, data: g.data }, [] as any, []);
20+
21+
if (path)
22+
for (const p of path) {
23+
highlightCell(p.x, p.y);
24+
}
25+
}
26+
})();

packages/demo/demo.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
[
2-
"interactive",
3-
"getBestRoute",
4-
"getBestTunnel",
5-
"outside",
6-
"getPathToPose",
7-
"getPathTo",
8-
"svg",
9-
"rust"
2+
"interactive",
3+
"getBestRoute",
4+
"getBestTunnel",
5+
"outside",
6+
"getPathToPose",
7+
"getPathTo",
8+
"svg",
9+
"rust",
10+
"astar"
1011
]

packages/solver-r/src/lib.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
mod astar;
12
mod grid;
23
mod snake;
34
mod snake_compact;
45
mod snake_walk;
56
mod solver;
67

7-
use grid::{Cell, Grid};
8+
use astar::get_path;
9+
use grid::{Cell, Grid, Point};
810
use js_sys;
911
use solver::get_free_cell;
1012
use wasm_bindgen::prelude::*;
@@ -67,6 +69,34 @@ impl From<IGrid> for Grid {
6769
}
6870
}
6971

72+
#[wasm_bindgen]
73+
pub struct IPoint {
74+
pub x: i8,
75+
pub y: i8,
76+
}
77+
#[wasm_bindgen]
78+
impl IPoint {
79+
pub fn create(x: i8, y: i8) -> IPoint {
80+
IPoint { x, y }
81+
}
82+
}
83+
impl From<Point> for IPoint {
84+
fn from(value: Point) -> Self {
85+
Self {
86+
x: value.x,
87+
y: value.y,
88+
}
89+
}
90+
}
91+
impl From<IPoint> for Point {
92+
fn from(value: IPoint) -> Self {
93+
Self {
94+
x: value.x,
95+
y: value.y,
96+
}
97+
}
98+
}
99+
70100
#[wasm_bindgen]
71101
pub fn iget_free_cell(grid: &IGrid) -> js_sys::Uint8Array {
72102
let g = Grid::from(grid.clone());
@@ -77,3 +107,21 @@ pub fn iget_free_cell(grid: &IGrid) -> js_sys::Uint8Array {
77107

78108
js_sys::Uint8Array::from(&o[..])
79109
}
110+
111+
#[wasm_bindgen]
112+
pub fn iastar(grid: &IGrid, start: IPoint, end: IPoint) -> Option<js_sys::Array> {
113+
let g = Grid::from(grid.clone());
114+
let res = get_path(
115+
|p| {
116+
(!g.is_inside(p) || g.get_cell(p) <= Cell::Color1)
117+
&& (-3 <= p.x
118+
&& p.x <= grid.width as i8 + 4
119+
&& -3 <= p.y
120+
&& p.y <= grid.height as i8 + 4)
121+
},
122+
&Point::from(start),
123+
&Point::from(end),
124+
);
125+
126+
res.map(|l| l.into_iter().map(IPoint::from).map(JsValue::from).collect())
127+
}

packages/solver-r/src/snake_walk.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
use std::collections::HashSet;
22

3+
use crate::astar::get_path;
34
use crate::grid::{Cell, Grid, Point, DIRECTIONS};
45
use crate::snake::{
56
get_next_snake_head, get_snake_head, move_snake, snake_will_self_collide, Snake,
67
};
78

9+
struct Node {
10+
point: Point,
11+
weight: u8,
12+
h: u8,
13+
parent: Option<Point>,
14+
}
15+
816
pub fn get_route_to_eat_all(
917
grid: &Grid,
1018
walkable: Cell,
@@ -13,6 +21,8 @@ pub fn get_route_to_eat_all(
1321
) -> Vec<Point> {
1422
// let mut targets: Vec<Point> = cells_to_eat.iter().map(|p| p.clone()).collect();
1523

24+
// let open_list = get_path(|p|);
25+
1626
let mut targets: Vec<&Point> = cells_to_eat.iter().collect();
1727

1828
let mut path: Vec<Point> = Vec::new();

0 commit comments

Comments
 (0)