forked from noxious/server
Greatly improved server code base
This commit is contained in:
@ -1,23 +1,28 @@
|
||||
import { AStar } from '#application/character/aStar'
|
||||
import Rotation from '#application/character/rotation'
|
||||
import { appLogger, gameLogger } from '#application/logger'
|
||||
import config from '#application/config'
|
||||
import { gameLogger } from '#application/logger'
|
||||
import { Character } from '#entities/character'
|
||||
import { Zone } from '#entities/zone'
|
||||
import ZoneManager from '#managers/zoneManager'
|
||||
import CharacterHairRepository from '#repositories/characterHairRepository'
|
||||
import CharacterRepository from '#repositories/characterRepository'
|
||||
import UserRepository from '#repositories/userRepository'
|
||||
import ZoneRepository from '#repositories/zoneRepository'
|
||||
|
||||
interface Position {
|
||||
x: number
|
||||
y: number
|
||||
}
|
||||
type Position = { x: number; y: number }
|
||||
export type Node = Position & { parent?: Node; g: number; h: number; f: number }
|
||||
|
||||
export class CharacterService {
|
||||
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 },
|
||||
{ x: -1, y: 1 },
|
||||
{ x: 1, y: -1 },
|
||||
{ x: 1, y: 1 }
|
||||
]
|
||||
|
||||
async updateCharacterPosition(id: number, positionX: number, positionY: number, rotation: number, zoneId: number) {
|
||||
public async updateCharacterPosition(id: number, positionX: number, positionY: number, rotation: number, zoneId: number) {
|
||||
const character = await CharacterRepository.getById(id)
|
||||
if (!character) return null
|
||||
|
||||
@ -51,14 +56,91 @@ export class CharacterService {
|
||||
y: Math.floor(targetY)
|
||||
}
|
||||
|
||||
return AStar.findPath(start, end, grid)
|
||||
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 isValidPosition(position: Position): boolean {
|
||||
return Number.isFinite(position.x) && Number.isFinite(position.y) && position.x >= 0 && position.y >= 0
|
||||
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
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user