forked from noxious/server
npm run format, improved a* and move event
This commit is contained in:
@ -5,8 +5,7 @@ export function isCommand(message: string, command?: string) {
|
||||
return message.startsWith(':')
|
||||
}
|
||||
|
||||
export function getArgs(command: string, message: string): string[] | undefined
|
||||
{
|
||||
export function getArgs(command: string, message: string): string[] | undefined {
|
||||
if (!isCommand(message, command)) return
|
||||
return message.split(`:${command} `)[1].split(' ')
|
||||
}
|
||||
}
|
||||
|
@ -1,55 +1,45 @@
|
||||
interface Position {
|
||||
x: number
|
||||
y: number
|
||||
}
|
||||
|
||||
export interface Node extends Position {
|
||||
parent?: Node
|
||||
g: number
|
||||
h: number
|
||||
f: number
|
||||
}
|
||||
type Position = { x: number; y: number }
|
||||
export type Node = Position & { parent?: Node; g: number; h: number; f: number }
|
||||
|
||||
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 }
|
||||
private static readonly DIRECTIONS = [
|
||||
{ x: 0, y: -1 },
|
||||
{ x: 0, y: 1 },
|
||||
{ x: -1, y: 0 },
|
||||
{ x: 1, y: 0 },
|
||||
{ x: -1, y: -1 },
|
||||
{ x: -1, y: 1 },
|
||||
{ x: 1, y: -1 },
|
||||
{ x: 1, y: 1 }
|
||||
]
|
||||
|
||||
private static readonly DIAGONAL_DIRECTIONS: Position[] = [
|
||||
{ x: -1, y: -1 }, { x: -1, y: 1 }, { x: 1, y: -1 }, { x: 1, y: 1 }
|
||||
]
|
||||
|
||||
static findPath(start: Position, end: Position, grid: number[][], allowDiagonal: boolean = false): Node[] {
|
||||
const openList: Node[] = []
|
||||
static findPath(start: Position, end: Position, grid: number[][], allowDiagonal = false): Node[] {
|
||||
const openList: Node[] = [{ ...start, g: 0, h: 0, f: 0 }]
|
||||
const closedSet = new Set<string>()
|
||||
|
||||
const startNode: Node = { ...start, g: 0, h: 0, f: 0 }
|
||||
openList.push(startNode)
|
||||
const getKey = (p: Position) => `${p.x},${p.y}`
|
||||
|
||||
while (openList.length > 0) {
|
||||
const currentNode = this.getLowestFScoreNode(openList)
|
||||
const current = openList.reduce((min, node) => (node.f < min.f ? node : min))
|
||||
if (current.x === end.x && current.y === end.y) return this.reconstructPath(current)
|
||||
|
||||
if (this.isEndNode(currentNode, end)) {
|
||||
return this.reconstructPath(currentNode)
|
||||
}
|
||||
openList.splice(openList.indexOf(current), 1)
|
||||
closedSet.add(getKey(current))
|
||||
|
||||
this.removeNodeFromOpenList(openList, currentNode)
|
||||
closedSet.add(this.nodeToString(currentNode))
|
||||
const neighbors = this.DIRECTIONS.slice(0, allowDiagonal ? 8 : 4)
|
||||
.map((dir) => ({ x: current.x + dir.x, y: current.y + dir.y }))
|
||||
.filter((pos) => this.isValidPosition(pos, grid, end))
|
||||
|
||||
for (const neighbor of this.getValidNeighbors(currentNode, grid, end, allowDiagonal)) {
|
||||
if (closedSet.has(this.nodeToString(neighbor))) continue
|
||||
for (const neighbor of neighbors) {
|
||||
if (closedSet.has(getKey(neighbor))) continue
|
||||
|
||||
const tentativeGScore = currentNode.g + this.getDistance(currentNode, neighbor)
|
||||
const g = current.g + this.getDistance(current, neighbor)
|
||||
const existing = openList.find((node) => node.x === neighbor.x && node.y === neighbor.y)
|
||||
|
||||
if (!this.isInOpenList(openList, neighbor) || tentativeGScore < neighbor.g) {
|
||||
neighbor.parent = currentNode
|
||||
neighbor.g = tentativeGScore
|
||||
neighbor.h = this.heuristic(neighbor, end)
|
||||
neighbor.f = neighbor.g + neighbor.h
|
||||
|
||||
if (!this.isInOpenList(openList, neighbor)) {
|
||||
openList.push(neighbor)
|
||||
}
|
||||
if (!existing || g < existing.g) {
|
||||
const h = this.getDistance(neighbor, end)
|
||||
const node: Node = { ...neighbor, g, h, f: g + h, parent: current }
|
||||
if (!existing) openList.push(node)
|
||||
else Object.assign(existing, node)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -57,76 +47,21 @@ export class AStar {
|
||||
return [] // No path found
|
||||
}
|
||||
|
||||
private static getLowestFScoreNode(nodes: Node[]): Node {
|
||||
return nodes.reduce((min, node) => (node.f < min.f ? node : min))
|
||||
}
|
||||
|
||||
private static isEndNode(node: Node, end: Position): boolean {
|
||||
return node.x === end.x && node.y === end.y
|
||||
}
|
||||
|
||||
private static removeNodeFromOpenList(openList: Node[], node: Node): void {
|
||||
const index = openList.findIndex((n) => n.x === node.x && n.y === node.y)
|
||||
if (index !== -1) openList.splice(index, 1)
|
||||
}
|
||||
|
||||
private static nodeToString(node: Position): string {
|
||||
return `${node.x},${node.y}`
|
||||
}
|
||||
|
||||
private static getValidNeighbors(node: Node, grid: number[][], end: Position, allowDiagonal: boolean): Node[] {
|
||||
const directions = allowDiagonal
|
||||
? [...this.ORTHOGONAL_DIRECTIONS, ...this.DIAGONAL_DIRECTIONS]
|
||||
: this.ORTHOGONAL_DIRECTIONS
|
||||
|
||||
return directions
|
||||
.map((dir) => ({
|
||||
x: node.x + dir.x,
|
||||
y: node.y + dir.y,
|
||||
g: 0,
|
||||
h: 0,
|
||||
f: 0
|
||||
}))
|
||||
.filter((pos) => this.isValidPosition(pos, grid, end))
|
||||
}
|
||||
|
||||
private static isValidPosition(pos: Position, grid: number[][], end: Position): boolean {
|
||||
const { x, y } = pos
|
||||
|
||||
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 {
|
||||
return openList.some((n) => n.x === node.x && n.y === node.y)
|
||||
return pos.x >= 0 && pos.y >= 0 && pos.x < grid[0].length && pos.y < grid.length && (grid[pos.y][pos.x] === 0 || (pos.x === end.x && pos.y === end.y))
|
||||
}
|
||||
|
||||
private static getDistance(a: Position, b: Position): number {
|
||||
const dx = Math.abs(a.x - b.x)
|
||||
const dy = Math.abs(a.y - b.y)
|
||||
const dx = Math.abs(a.x - b.x),
|
||||
dy = Math.abs(a.y - b.y)
|
||||
return Math.sqrt(dx * dx + dy * dy)
|
||||
}
|
||||
|
||||
private static heuristic(node: Position, goal: Position): number {
|
||||
return this.getDistance(node, goal)
|
||||
}
|
||||
|
||||
private static reconstructPath(endNode: Node): Node[] {
|
||||
const path: Node[] = []
|
||||
let currentNode: Node | undefined = endNode
|
||||
|
||||
while (currentNode) {
|
||||
path.unshift(currentNode)
|
||||
currentNode = currentNode.parent
|
||||
for (let current: Node | undefined = endNode; current; current = current.parent) {
|
||||
path.unshift(current)
|
||||
}
|
||||
|
||||
return path
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -31,4 +31,4 @@ export type TAsset = {
|
||||
// export type TCharacter = Socket & {
|
||||
// user?: User
|
||||
// character?: Character
|
||||
// }
|
||||
// }
|
||||
|
Reference in New Issue
Block a user