1
0
forked from noxious/server

Move bug fixes

This commit is contained in:
2024-08-17 23:14:58 +02:00
parent cd538bc4db
commit 5ce844c919
2 changed files with 34 additions and 26 deletions

View File

@ -10,9 +10,6 @@ export interface Node extends Position {
f: number
}
/**
* A* pathfinding algorithm.
*/
export class AStar {
private static readonly ORTHOGONAL_DIRECTIONS: Position[] = [
{ x: 0, y: -1 }, { x: 0, y: 1 }, { x: -1, y: 0 }, { x: 1, y: 0 }
@ -22,14 +19,6 @@ export class AStar {
{ x: -1, y: -1 }, { x: -1, y: 1 }, { x: 1, y: -1 }, { x: 1, y: 1 }
]
/**
* Finds the shortest path from start to end on the given grid.
* @param start - Start position.
* @param end - End position.
* @param grid - The grid representing the map (0 = open space, 1 = obstacle).
* @param allowDiagonal - Whether diagonal movements are allowed.
* @returns Array of `Node` representing the path from start to end.
*/
static findPath(start: Position, end: Position, grid: number[][], allowDiagonal: boolean = false): Node[] {
const openList: Node[] = []
const closedSet = new Set<string>()
@ -103,8 +92,16 @@ export class AStar {
private static isValidPosition(pos: Position, grid: number[][], end: Position): boolean {
const { x, y } = pos
return x >= 0 && y >= 0 && x < grid.length && y < grid[0].length &&
(grid[y][x] === 0 || (x === end.x && y === end.y))
if (!grid || grid.length === 0 || !Array.isArray(grid[0])) {
return false;
}
const height = grid.length;
const width = grid[0].length;
return x >= 0 && x < width && y >= 0 && y < height &&
(grid[y][x] === 0 || (x === end.x && y === end.y));
}
private static isInOpenList(openList: Node[], node: Position): boolean {