Skip to content

Commit e74d34a

Browse files
committed
.
1 parent 9699800 commit e74d34a

File tree

11 files changed

+133
-499
lines changed

11 files changed

+133
-499
lines changed

packages/demo/demo.rust.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ import { grid, snake } from "./sample";
66
(async () => {
77
const api = await import("@snk/solver-r");
88

9-
const g = api.IGrid.create(grid.width, grid.height, grid.data);
9+
const iColorGrid = api.IColorGrid.create(grid.width, grid.height, grid.data);
10+
const iSnake = snakeToCells(snake).map((p) => api.IPoint.create(p.x, p.y));
1011

11-
const { canvas, draw, highlightCell } = createCanvas(g);
12+
// const colorGrid = api.get_color_grid_sample(api.SampleGrid.Labyrinthe);
13+
14+
const { canvas, draw, highlightCell } = createCanvas(iColorGrid);
1215
document.body.appendChild(canvas);
13-
draw({ width: g.width, height: g.height, data: g.data }, snake, []);
16+
draw(iColorGrid, snake, []);
1417

1518
api.greet();
1619

1720
const a = performance.now();
18-
const path = api.ieat_free_cells(
19-
g,
20-
snakeToCells(snake).map((p) => api.IPoint.create(p.x, p.y)),
21-
);
21+
const path = api.solve(iColorGrid, iSnake).reverse();
2222
console.log(performance.now() - a);
2323

2424
{
@@ -28,7 +28,7 @@ import { grid, snake } from "./sample";
2828
const i = +input.value;
2929
const s = createSnakeFromCells(path.slice(i, i + snakeLength).reverse());
3030

31-
draw({ width: g.width, height: g.height, data: g.data }, s, []);
31+
draw(iColorGrid, s, []);
3232

3333
for (let j = i + snakeLength; j--; ) {
3434
highlightCell(path[j].x, path[j].y, "#123bde");

packages/solver-r/package.json

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
2-
"name": "@snk/solver-r",
3-
"version": "1.0.0",
4-
"devDependencies": {
5-
"wasm-pack": "0.13.1"
6-
},
7-
"main": "./pkg/snk_solver_rust.js",
8-
"scripts": {
9-
"build": "wasm-pack build"
10-
}
2+
"name": "@snk/solver-r",
3+
"version": "1.0.0",
4+
"devDependencies": {
5+
"wasm-pack": "0.13.1"
6+
},
7+
"main": "./pkg/snk_solver_rust.js",
8+
"scripts": {
9+
"build": "wasm-pack build",
10+
"dev": "bunx nodemon --watch src -e rs --exec wasm-pack build"
11+
}
1112
}

packages/solver-r/src/_test_grid_samples.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
1-
use crate::grid::{Cell, Grid, Point};
1+
use wasm_bindgen::prelude::wasm_bindgen;
22

3-
fn grid_from_ascii(ascii: &str) -> Grid {
3+
use crate::grid::{Color, Grid, Point};
4+
5+
fn grid_from_ascii(ascii: &str) -> Grid<Color> {
46
let rows: Vec<_> = ascii.trim().split('\n').collect();
57

68
let width = rows.iter().fold(0, |max_len, r| max_len.max(r.len()));
79
let height = rows.len();
810

9-
let mut grid = Grid::create_empty(width as u8, height as u8);
11+
let mut grid = Grid::create(width as u8, height as u8, Color::Empty);
1012

1113
for x in 0..width {
1214
for y in 0..height {
1315
let c = rows[y].chars().nth(x).unwrap_or(' ');
1416
let cell = match c {
15-
' ' => Cell::Empty,
16-
'_' => Cell::Empty,
17-
'1' => Cell::Color1,
18-
'2' => Cell::Color2,
19-
'3' => Cell::Color3,
20-
'4' => Cell::Color4,
17+
' ' => Color::Empty,
18+
'_' => Color::Empty,
19+
'1' => Color::Color1,
20+
'2' => Color::Color2,
21+
'3' => Color::Color3,
22+
'4' => Color::Color4,
2123

22-
'.' => Cell::Color1,
23-
'o' => Cell::Color2,
24-
'O' => Cell::Color3,
25-
'#' => Cell::Color4,
24+
'.' => Color::Color1,
25+
'o' => Color::Color2,
26+
'O' => Color::Color3,
27+
'#' => Color::Color4,
2628
_ => panic!("unknow char \"{}\"", c),
2729
};
28-
grid.set_cell(
30+
grid.set(
2931
&Point {
3032
x: x as i8,
3133
y: y as i8,
@@ -38,7 +40,7 @@ fn grid_from_ascii(ascii: &str) -> Grid {
3840
grid
3941
}
4042

41-
fn randomly_fill_grid(grid: &mut Grid, probability: &[Cell], seed: u32) -> () {
43+
fn randomly_fill_grid(grid: &mut Grid<Color>, probability: &[Color], seed: u32) -> () {
4244
// Pseudorandom number generator from the "Xorshift RNGs" paper by George Marsaglia.
4345
// https://github.com/rust-lang/rust/blob/1.55.0/library/core/src/slice/sort.rs#L559-L573
4446
fn random_numbers(seed: u32) -> impl Iterator<Item = u32> {
@@ -57,19 +59,20 @@ fn randomly_fill_grid(grid: &mut Grid, probability: &[Cell], seed: u32) -> () {
5759
for y in 0..(grid.height as i8) {
5860
let random = rng.next().unwrap();
5961
let cell = probability[random as usize % probability.len()];
60-
grid.set_cell(&Point { x, y }, cell);
62+
grid.set(&Point { x, y }, cell);
6163
}
6264
}
6365
}
6466

67+
#[wasm_bindgen]
6568
pub enum SampleGrid {
6669
Empty,
6770
OneDot,
6871
Realistic,
6972
Labyrinthe,
7073
RandomPack,
7174
}
72-
pub fn get_grid_sample(g: SampleGrid) -> Grid {
75+
pub fn get_grid_sample(g: SampleGrid) -> Grid<Color> {
7376
match g {
7477
SampleGrid::Empty => grid_from_ascii(
7578
&r#"
@@ -115,10 +118,10 @@ pub fn get_grid_sample(g: SampleGrid) -> Grid {
115118
),
116119

117120
SampleGrid::RandomPack => {
118-
let mut grid = Grid::create_empty(52, 7);
121+
let mut grid = Grid::create(52, 7, Color::Empty);
119122
randomly_fill_grid(
120123
&mut grid,
121-
&[Cell::Empty, Cell::Empty, Cell::Color1, Cell::Color4],
124+
&[Color::Empty, Color::Empty, Color::Color1, Color::Color4],
122125
92u32,
123126
);
124127
grid

packages/solver-r/src/astar_snake.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::grid::{get_distance, Point, DIRECTIONS};
2-
use crate::snake::Snake;
32
use std::cmp::Reverse;
43
use std::collections::{BinaryHeap, HashSet};
54

packages/solver-r/src/exitable.rs

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

3-
use crate::grid::{Point, WalkableGrid, DIRECTIONS};
3+
use crate::grid::{Color, Grid, Point, DIRECTIONS};
44

5-
pub fn propagate_exitable(exitable_cells: &mut HashSet<Point>, grid: &WalkableGrid) -> () {
6-
for x in 0..(grid.grid.width as i8) {
5+
/**
6+
* mark as exitable all the cells from which a path can be found to the outside
7+
*/
8+
pub fn propagate_exitable(
9+
exitable_cells: &mut HashSet<Point>,
10+
grid: &Grid<Color>,
11+
walkable: Color,
12+
) -> () {
13+
for x in 0..(grid.width as i8) {
714
{
815
let p = Point { x, y: 0 };
9-
if grid.is_cell_walkable(&p) {
16+
if grid.get(&p) <= walkable || !grid.is_inside(&p) {
1017
exitable_cells.insert(p);
1118
}
1219
}
1320
{
1421
let p = Point {
1522
x,
16-
y: (grid.grid.height as i8) - 1,
23+
y: (grid.height as i8) - 1,
1724
};
18-
if grid.is_cell_walkable(&p) {
25+
if grid.get(&p) <= walkable || !grid.is_inside(&p) {
1926
exitable_cells.insert(p);
2027
}
2128
}
2229
}
23-
for y in 0..(grid.grid.height as i8) {
30+
for y in 0..(grid.height as i8) {
2431
{
2532
let p = Point { x: 0, y };
26-
if grid.is_cell_walkable(&p) {
33+
if grid.get(&p) <= walkable || !grid.is_inside(&p) {
2734
exitable_cells.insert(p);
2835
}
2936
}
3037
{
3138
let p = Point {
32-
x: (grid.grid.width as i8) - 1,
39+
x: (grid.width as i8) - 1,
3340
y,
3441
};
35-
if grid.is_cell_walkable(&p) {
42+
if grid.get(&p) <= walkable || !grid.is_inside(&p) {
3643
exitable_cells.insert(p);
3744
}
3845
}
@@ -47,7 +54,7 @@ pub fn propagate_exitable(exitable_cells: &mut HashSet<Point>, grid: &WalkableGr
4754
y: p.y + dir.y,
4855
};
4956

50-
if !exitable_cells.contains(&u) && grid.is_inside(&u) && grid.is_cell_walkable(&u) {
57+
if !exitable_cells.contains(&u) && grid.is_inside(&u) && grid.get(&u) <= walkable {
5158
open_list.push(u);
5259
exitable_cells.insert(u);
5360
}

packages/solver-r/src/free_cells.rs

Lines changed: 0 additions & 127 deletions
This file was deleted.

0 commit comments

Comments
 (0)