Skip to content
Draft

wip #27

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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
node_modules
coverage
target
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "oh_pathfinding"
version = "0.0.0"
edition = "2024"

[lib]
path = "src/lib.rs"
crate-type = ["cdylib"]

[dependencies]

[[bin]]
name = "start"
path = "src/lib.rs"
3 changes: 3 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
"exports": "./mod.ts",
"imports": {
"std/": "https://deno.land/std@0.224.0/"
},
"tasks": {
"build:lib": "cargo build --release"
}
}
3 changes: 2 additions & 1 deletion deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 24 additions & 42 deletions preview/bundle.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// src/utils/grid.utils.ts
var makeSquare = (layout) => {
const maxLength = Math.max(layout.length, ...layout.map((row) => row.length));
const squareLayout = Array.from({ length: maxLength }, () =>
Array(maxLength).fill(null),
const squareLayout = Array.from(
{ length: maxLength },
() => Array(maxLength).fill(null)
);
for (let i = 0; i < layout.length; i++) {
for (let j = 0; j < layout[i].length; j++) {
Expand All @@ -13,8 +14,9 @@ var makeSquare = (layout) => {
};
var transpose = (matrix) => {
const maxCols = Math.max(...matrix.map((row) => row.length));
const transposed = Array.from({ length: maxCols }, () =>
Array(matrix.length).fill(null),
const transposed = Array.from(
{ length: maxCols },
() => Array(matrix.length).fill(null)
);
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
Expand Down Expand Up @@ -92,7 +94,7 @@ var findPath = (startPoint, endPoint, grid, config) => {
const orthogonalCostMultiplier = config.orthogonalCostMultiplier ?? 1;
const maxJumpCost = config.maxJumpCost ?? 5;
const maxIterations = config.maxIterations ?? 99999;
const jumpDiagonals = config.jumpDiagonals ?? false;
const jumpBlockedDiagonals = config.jumpBlockedDiagonals ?? false;
const index = (point) => {
return point.y * grid.height + point.x;
};
Expand All @@ -110,7 +112,7 @@ var findPath = (startPoint, endPoint, grid, config) => {
while (true) {
const target = {
x: src.x + dirX * jumpDistance,
y: src.y + dirY * jumpDistance,
y: src.y + dirY * jumpDistance
};
if (!grid.isWalkable(target)) {
break;
Expand All @@ -125,7 +127,7 @@ var findPath = (startPoint, endPoint, grid, config) => {
if (totalCost < visited[targetIndex]) {
visited[targetIndex] = totalCost;
queue.push(
new PathNode(target, prevNode, totalCost, heuristic(target)),
new PathNode(target, prevNode, totalCost, heuristic(target))
);
}
prevPoint = target;
Expand All @@ -137,24 +139,13 @@ var findPath = (startPoint, endPoint, grid, config) => {
};
const addDiagonal = (prevNode, src, srcCost, dirX, dirY) => {
const target = { x: src.x + dirX, y: src.y + dirY };
const moveCost =
srcCost +
(getMoveCostAt(src, target) ?? NOT_REACHED_COST) * diagonalCostMultiplier;
const moveCost = srcCost + (getMoveCostAt(src, target) ?? NOT_REACHED_COST) * diagonalCostMultiplier;
const targetHeight = grid.getHeightAt(target);
const aux1 = { x: src.x, y: src.y + dirY };
const aux2 = { x: src.x + dirX, y: src.y };
const targetIndex = index(target);
const canJumpDiagonals =
jumpDiagonals ||
(grid.isWalkable(aux1) &&
grid.isWalkable(aux2) &&
targetHeight == grid.getHeightAt(aux1) &&
targetHeight == grid.getHeightAt(aux2));
if (
grid.isWalkable(target) &&
canJumpDiagonals &&
moveCost < visited[targetIndex]
) {
const canJumpDiagonals = jumpBlockedDiagonals || grid.isWalkable(aux1) && grid.isWalkable(aux2) && targetHeight == grid.getHeightAt(aux1) && targetHeight == grid.getHeightAt(aux2);
if (grid.isWalkable(target) && canJumpDiagonals && moveCost < visited[targetIndex]) {
visited[targetIndex] = moveCost;
queue.push(new PathNode(target, prevNode, moveCost, heuristic(target)));
}
Expand Down Expand Up @@ -253,24 +244,15 @@ var Grid = class _Grid {
return new _Grid(
this.width,
this.height,
new Float32Array(this.heightMatrix),
new Float32Array(this.heightMatrix)
);
}
inBounds(point) {
return (
point.x >= 0 &&
point.x < this.width &&
point.y >= 0 &&
point.y < this.height
);
return point.x >= 0 && point.x < this.width && point.y >= 0 && point.y < this.height;
}
isWalkable(point) {
const heightAt = this.getHeightAt(point);
return (
this.inBounds(point) &&
heightAt !== null &&
heightAt !== NON_WALKABLE_HEIGHT
);
return this.inBounds(point) && heightAt !== null && heightAt !== NON_WALKABLE_HEIGHT;
}
walkMatrix(callback) {
for (let y = 0; y < this.height; y++) {
Expand All @@ -291,15 +273,15 @@ var Grid = class _Grid {
}
return matrix;
}
findPath(
startPoint,
endPoint,
config = {
maxJumpCost: 5,
travelCosts: void 0,
},
) {
findPath(startPoint, endPoint, config = {
maxJumpCost: 5,
travelCosts: void 0
}) {
return findPath(startPoint, endPoint, this, config);
}
};
export { Grid, makeSquare, transpose };
export {
Grid,
makeSquare,
transpose
};
26 changes: 26 additions & 0 deletions src/find_path_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#[derive(Debug, Clone, Default)]
pub struct FindPathConfig {
pub diagonal_cost_multiplier: f32,
pub orthogonal_cost_multiplier: f32,
pub max_jump_cost: f32,
pub max_iterations: u32,
pub jump_blocked_diagonals: bool,
}

impl FindPathConfig {
pub fn new(
diagonal_cost_multiplier: Option<f32>,
orthogonal_cost_multiplier: Option<f32>,
max_jump_cost: Option<f32>,
max_iterations: Option<u32>,
jump_blocked_diagonals: Option<bool>,
) -> Self {
FindPathConfig {
diagonal_cost_multiplier: diagonal_cost_multiplier.unwrap_or(1.0),
orthogonal_cost_multiplier: orthogonal_cost_multiplier.unwrap_or(1.0),
max_jump_cost: max_jump_cost.unwrap_or(5.0),
max_iterations: max_iterations.unwrap_or(99999),
jump_blocked_diagonals: jump_blocked_diagonals.unwrap_or(false),
}
}
}
Loading
Loading