Walk fixes
This commit is contained in:
@ -118,35 +118,42 @@ class CharacterMoveService extends BaseService {
|
||||
|
||||
private findPath(start: Position, end: Position, grid: number[][]): Node[] {
|
||||
const openList = new PriorityQueue<Node>((a, b) => a.f - b.f)
|
||||
openList.enqueue({ ...start, g: 0, h: 0, f: 0 })
|
||||
const closedSet = new Set<string>()
|
||||
|
||||
const getKey = (p: Position) => `${p.positionX},${p.positionY}`
|
||||
|
||||
while (openList.length > 0) {
|
||||
const current = openList.dequeue()
|
||||
if (!current) break
|
||||
openList.enqueue({ ...start, g: 0, h: 0, f: 0 })
|
||||
|
||||
if (current.positionX === end.positionX && current.positionY === end.positionY) {
|
||||
return this.reconstructPath(current)
|
||||
try {
|
||||
while (openList.length > 0) {
|
||||
const current = openList.dequeue()
|
||||
if (!current) break
|
||||
|
||||
if (current.positionX === end.positionX && current.positionY === end.positionY) {
|
||||
return this.reconstructPath(current)
|
||||
}
|
||||
|
||||
closedSet.add(getKey(current))
|
||||
|
||||
const neighbors = this.getValidNeighbors(current, grid, end)
|
||||
|
||||
for (const neighbor of neighbors) {
|
||||
if (closedSet.has(getKey(neighbor))) continue
|
||||
|
||||
const g = current.g + this.getDistance(current, neighbor)
|
||||
const h = this.getDistance(neighbor, end)
|
||||
const f = g + h
|
||||
|
||||
const node: Node = {...neighbor, g, h, f, parent: current}
|
||||
openList.enqueue(node)
|
||||
}
|
||||
}
|
||||
|
||||
closedSet.add(getKey(current))
|
||||
|
||||
const neighbors = this.getValidNeighbors(current, grid, end)
|
||||
|
||||
for (const neighbor of neighbors) {
|
||||
if (closedSet.has(getKey(neighbor))) continue
|
||||
|
||||
const g = current.g + this.getDistance(current, neighbor)
|
||||
const h = this.getDistance(neighbor, end)
|
||||
const f = g + h
|
||||
|
||||
const node: Node = { ...neighbor, g, h, f, parent: current }
|
||||
openList.enqueue(node)
|
||||
}
|
||||
return [] // No path found
|
||||
} finally {
|
||||
while(openList.length > 0) openList.dequeue()
|
||||
closedSet.clear()
|
||||
}
|
||||
|
||||
return [] // No path found
|
||||
}
|
||||
|
||||
private getValidNeighbors(current: Position, grid: number[][], end: Position): Position[] {
|
||||
@ -159,7 +166,13 @@ class CharacterMoveService extends BaseService {
|
||||
}
|
||||
|
||||
private isValidPosition(pos: Position, grid: number[][], end: Position): boolean {
|
||||
return pos.positionX >= 0 && pos.positionY >= 0 && pos.positionX < grid[0]!.length && pos.positionY < grid.length && (grid[pos.positionY]![pos.positionX] === 0 || (pos.positionX === end.positionX && pos.positionY === end.positionY))
|
||||
return (
|
||||
pos.positionX >= 0 &&
|
||||
pos.positionY >= 0 &&
|
||||
pos.positionX < grid[0]!.length &&
|
||||
pos.positionY < grid.length &&
|
||||
(grid[pos.positionY]![pos.positionX] === 0 || (pos.positionX === end.positionX && pos.positionY === end.positionY))
|
||||
)
|
||||
}
|
||||
|
||||
private getDistance(a: Position, b: Position): number {
|
||||
@ -179,7 +192,13 @@ class CharacterMoveService extends BaseService {
|
||||
return path
|
||||
}
|
||||
|
||||
public validateMovementDistance(currentX: number, currentY: number, lastKnownPosition: { x: number; y: number } | null, stepDelay: number, isMoving: boolean): { isValid: boolean; maxAllowedDistance: number; actualDistance: number } {
|
||||
public validateMovementDistance(
|
||||
currentX: number,
|
||||
currentY: number,
|
||||
lastKnownPosition: { x: number; y: number } | null,
|
||||
stepDelay: number,
|
||||
isMoving: boolean
|
||||
): { isValid: boolean; maxAllowedDistance: number; actualDistance: number } {
|
||||
if (!lastKnownPosition || !isMoving) {
|
||||
return { isValid: true, maxAllowedDistance: 0, actualDistance: 0 }
|
||||
}
|
||||
|
Reference in New Issue
Block a user