1
0
forked from noxious/server

A* and move logic improvements

This commit is contained in:
2024-08-20 21:33:57 +02:00
parent 079d9408a8
commit acc9eaae9e
3 changed files with 29 additions and 15 deletions

View File

@ -3,10 +3,10 @@ export type Node = Position & { parent?: Node; g: number; h: number; f: number }
export class AStar {
private static readonly DIRECTIONS = [
{ x: 0, y: -1 },
{ x: 0, y: 1 },
{ x: -1, y: 0 },
{ x: 1, y: 0 },
{ x: 0, y: -1 }, // Up
{ x: 0, y: 1 }, // Down
{ x: -1, y: 0 }, // Left
{ x: 1, y: 0 }, // Right
{ x: -1, y: -1 },
{ x: -1, y: 1 },
{ x: 1, y: -1 },
@ -54,7 +54,8 @@ export class AStar {
private static getDistance(a: Position, b: Position): number {
const dx = Math.abs(a.x - b.x),
dy = Math.abs(a.y - b.y)
return Math.sqrt(dx * dx + dy * dy)
// Manhattan distance for straight paths, then Euclidean for diagonals
return dx + dy + (Math.sqrt(2) - 2) * Math.min(dx, dy)
}
private static reconstructPath(endNode: Node): Node[] {