1
0
forked from noxious/server
noxious_server/src/services/characterService.ts

134 lines
4.2 KiB
TypeScript

import { BaseService } from '#application/base/baseService'
import config from '#application/config'
import { Character } from '#entities/character'
import { Zone } from '#entities/zone'
import ZoneManager from '#managers/zoneManager'
import CharacterRepository from '#repositories/characterRepository'
import ZoneRepository from '#repositories/zoneRepository'
type Position = { x: number; y: number }
export type Node = Position & { parent?: Node; g: number; h: number; f: number }
class CharacterService extends BaseService {
private readonly MOVEMENT_DELAY_MS = 250
private readonly DIRECTIONS = [
{ x: 0, y: -1 }, // Up
{ x: 0, y: 1 }, // Down
{ x: -1, y: 0 }, // Left
{ x: 1, y: 0 }, // Right
{ x: -1, y: -1 }, // Up left
{ x: -1, y: 1 }, // Up right
{ x: 1, y: -1 }, // Down left
{ x: 1, y: 1 } // Down right
]
public async calculatePath(character: Character, targetX: number, targetY: number): Promise<Position[] | null> {
const zone = ZoneManager.getZoneById(character.zone!.id)
const grid = await zone?.getGrid()
if (!grid?.length) {
this.logger.error('character:move error: Grid not found or empty')
return null
}
const start: Position = {
x: Math.floor(character.positionX),
y: Math.floor(character.positionY)
}
const end: Position = {
x: Math.floor(targetX),
y: Math.floor(targetY)
}
return this.findPath(start, end, grid)
}
static calculateRotation(X1: number, Y1: number, X2: number, Y2: number): number {
if (config.ALLOW_DIAGONAL_MOVEMENT) {
// Check diagonal movements
if (X1 > X2 && Y1 > Y2) {
return 7
} else if (X1 < X2 && Y1 < Y2) {
return 3
} else if (X1 > X2 && Y1 < Y2) {
return 5
} else if (X1 < X2 && Y1 > Y2) {
return 1
}
}
// Non-diagonal movements
if (X1 > X2) {
return 6
} else if (X1 < X2) {
return 2
} else if (Y1 < Y2) {
return 4
} else if (Y1 > Y2) {
return 0
}
return 0 // Default case
}
public async applyMovementDelay(): Promise<void> {
await new Promise((resolve) => setTimeout(resolve, this.MOVEMENT_DELAY_MS))
}
private findPath(start: Position, end: Position, grid: number[][]): Node[] {
const openList: Node[] = [{ ...start, g: 0, h: 0, f: 0 }]
const closedSet = new Set<string>()
const getKey = (p: Position) => `${p.x},${p.y}`
while (openList.length > 0) {
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)
openList.splice(openList.indexOf(current), 1)
closedSet.add(getKey(current))
const neighbors = this.DIRECTIONS.slice(0, config.ALLOW_DIAGONAL_MOVEMENT ? 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 neighbors) {
if (closedSet.has(getKey(neighbor))) continue
const g = current.g + this.getDistance(current, neighbor)
const existing = openList.find((node) => node.x === neighbor.x && node.y === neighbor.y)
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)
}
}
}
return [] // No path found
}
private isValidPosition(pos: Position, grid: number[][], end: Position): boolean {
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 getDistance(a: Position, b: Position): number {
const dx = Math.abs(a.x - b.x),
dy = Math.abs(a.y - b.y)
// Manhattan distance for straight paths, then Euclidean for diagonals
return dx + dy + (Math.sqrt(2) - 2) * Math.min(dx, dy)
}
private reconstructPath(endNode: Node): Node[] {
const path: Node[] = []
for (let current: Node | undefined = endNode; current; current = current.parent) {
path.unshift(current)
}
return path
}
}
export default new CharacterService()