1
0
forked from noxious/server

Added extra walk checks

This commit is contained in:
Dennis Postma 2025-02-12 15:27:56 +01:00
parent f76bf3df1f
commit 22b776ef0f

View File

@ -107,7 +107,16 @@ class CharacterMoveService extends BaseService {
const grid = await map?.getGrid() const grid = await map?.getGrid()
if (!grid?.length) { if (!grid?.length) {
this.logger.error('map:character:move error: Grid not found or empty') return null
}
// Validate target position is within map boundaries
if (targetX < 0 || targetY < 0 || targetX >= grid[0]!.length || targetY >= grid.length) {
return null
}
// Check if target position is walkable
if (grid[Math.floor(targetY)]![Math.floor(targetX)] !== 0) {
return null return null
} }
@ -165,13 +174,16 @@ class CharacterMoveService extends BaseService {
private findPath(start: Position, end: Position, grid: number[][]): Node[] { private findPath(start: Position, end: Position, grid: number[][]): Node[] {
const openList = new PriorityQueue<Node>((a, b) => a.f - b.f) const openList = new PriorityQueue<Node>((a, b) => a.f - b.f)
const closedSet = new Set<string>() const closedSet = new Set<string>()
const MAX_ITERATIONS = 1000 // Prevent infinite loops for impossible paths
let iterations = 0
const getKey = (p: Position) => `${p.positionX},${p.positionY}` const getKey = (p: Position) => `${p.positionX},${p.positionY}`
openList.enqueue({ ...start, g: 0, h: 0, f: 0 }) openList.enqueue({ ...start, g: 0, h: 0, f: 0 })
try { try {
while (openList.length > 0) { while (openList.length > 0 && iterations < MAX_ITERATIONS) {
iterations++
const current = openList.dequeue() const current = openList.dequeue()
if (!current) break if (!current) break
@ -195,8 +207,10 @@ class CharacterMoveService extends BaseService {
} }
} }
this.logger.warn(`Path not found after ${iterations} iterations`)
return [] // No path found return [] // No path found
} finally { } finally {
// Clean up resources
while (openList.length > 0) openList.dequeue() while (openList.length > 0) openList.dequeue()
closedSet.clear() closedSet.clear()
} }