1
0
forked from noxious/server

npm run format, improved a* and move event

This commit is contained in:
Dennis Postma 2024-08-20 02:16:06 +02:00
parent cb90e0cdf8
commit 079d9408a8
5 changed files with 82 additions and 216 deletions

View File

@ -1,143 +1,76 @@
import { Server } from 'socket.io' import { Server } from 'socket.io'
import { TSocket } from '../../utilities/Types' import { TSocket, ExtendedCharacter } from '../../utilities/Types'
import ZoneManager from '../../managers/ZoneManager' import ZoneManager from '../../managers/ZoneManager'
import prisma from '../../utilities/Prisma' import prisma from '../../utilities/Prisma'
import { AStar, type Node } from '../../utilities/Player/AStar' import { AStar } from '../../utilities/Player/AStar'
import Rotation from '../../utilities/Player/Rotation' import Rotation from '../../utilities/Player/Rotation'
import { ExtendedCharacter as Character } from '../../utilities/Types'
interface SocketResponse { const moveTokens = new Map<number, symbol>()
position_x: number
position_y: number
}
const characterMoveTokens = new Map<number, Symbol>()
export default function setupCharacterMove(socket: TSocket, io: Server) { export default function setupCharacterMove(socket: TSocket, io: Server) {
socket.on('character:move', async (data: SocketResponse) => { socket.on('character:move', async ({ position_x, position_y }: { position_x: number; position_y: number }) => {
try { const { character } = socket
console.log('character:move requested', data) if (!character) return console.error('character:move error', 'Character not found')
if (!socket.character) { moveTokens.set(character.id, Symbol('moveToken'))
console.error('character:move error', 'Character not found') const grid = await ZoneManager.getGrid(character.zoneId)
return if (!grid?.length) return console.error('character:move error', 'Grid not found or empty')
}
const oldMoveToken = characterMoveTokens.get(socket.character.id) const start = { x: Math.floor(character.position_x), y: Math.floor(character.position_y) }
if (oldMoveToken) { const end = { x: Math.floor(position_x), y: Math.floor(position_y) }
characterMoveTokens.delete(socket.character.id)
}
const moveToken = Symbol('moveToken') if (isObstacle(end, grid)) return socket.emit('character:moveError', 'Destination is an obstacle')
characterMoveTokens.set(socket.character.id, moveToken)
const grid = await ZoneManager.getGrid(socket.character.zoneId)
if (!grid || grid.length === 0) {
console.error('character:move error', 'Grid not found or empty')
return
}
const start = { x: Math.floor(socket.character.position_x), y: Math.floor(socket.character.position_y) }
const end = { x: Math.floor(data.position_x), y: Math.floor(data.position_y) }
console.log('Pathfinding from', start, 'to', end)
console.log('Grid dimensions:', grid.length, 'x', grid[0].length)
// Check if the destination is an obstacle
if (isObstacle(end, grid)) {
console.log('character:move error', 'Destination is an obstacle')
socket.emit('character:moveError', 'Destination is an obstacle')
return
}
const path = AStar.findPath(start, end, grid) const path = AStar.findPath(start, end, grid)
if (!path.length) return socket.emit('character:moveError', 'No valid path found')
if (path.length > 0) { character.isMoving = true
socket.character.isMoving = true io.in(character.zoneId.toString()).emit('character:moved', character)
io.in(socket.character.zoneId.toString()).emit('character:moved', socket.character) moveAlongPath(socket, io, path, grid).catch(console.error)
moveAlongPath(socket, io, path, grid, moveToken).catch(console.error)
} else {
console.log('character:move error', 'No valid path found')
socket.emit('character:moveError', 'No valid path found')
}
} catch (error) {
console.error('character:move error', error)
if (socket.character) {
socket.character.isMoving = false
io.in(socket.character.zoneId.toString()).emit('character:moved', socket.character)
}
}
}) })
} }
async function moveAlongPath(socket: TSocket, io: Server, path: Node[], grid: number[][], moveToken: Symbol) { async function moveAlongPath(socket: TSocket, io: Server, path: Array<{ x: number; y: number }>, grid: number[][]) {
if (!socket.character) return const { character } = socket
if (!character) return
const totalSteps = path.length const moveToken = moveTokens.get(character.id)
const updateInterval = 50 // milliseconds between updates const stepDuration = 250
const updateInterval = 50
for (let step = 0; step < totalSteps - 1; step++) {
if (characterMoveTokens.get(socket.character.id) !== moveToken) {
console.log('Movement cancelled for character', socket.character.id)
return
}
for (let i = 0; i < path.length - 1; i++) {
const startTime = Date.now() const startTime = Date.now()
const stepDuration = 250 // 250ms per tile
while (Date.now() - startTime < stepDuration) { while (Date.now() - startTime < stepDuration) {
if (characterMoveTokens.get(socket.character.id) !== moveToken) { if (moveTokens.get(character.id) !== moveToken) return
console.log('Movement cancelled for character', socket.character.id)
return
}
const progress = (Date.now() - startTime) / stepDuration const progress = (Date.now() - startTime) / stepDuration
const currentPosition = interpolatePosition(path[step], path[step + 1], progress) const current = interpolatePosition(path[i], path[i + 1], progress)
if (isObstacle(currentPosition, grid)) { if (isObstacle(current, grid)) break
console.log('Obstacle encountered at', currentPosition)
break
}
const rotation = Rotation.calculate(path[step].x, path[step].y, path[step + 1].x, path[step + 1].y) await updateCharacter(character, current, Rotation.calculate(path[i].x, path[i].y, path[i + 1].x, path[i + 1].y))
io.in(character.zoneId.toString()).emit('character:moved', character)
await updateCharacterPosition(socket.character, currentPosition.x, currentPosition.y, rotation) await new Promise((resolve) => setTimeout(resolve, updateInterval))
ZoneManager.updateCharacterInZone(socket.character.zoneId, socket.character)
io.in(socket.character.zoneId.toString()).emit('character:moved', socket.character)
await new Promise(resolve => setTimeout(resolve, updateInterval))
} }
} }
if (socket.character && characterMoveTokens.get(socket.character.id) === moveToken) { if (moveTokens.get(character.id) === moveToken) {
socket.character.isMoving = false character.isMoving = false
characterMoveTokens.delete(socket.character.id) moveTokens.delete(character.id)
io.in(socket.character.zoneId.toString()).emit('character:moved', socket.character) io.in(character.zoneId.toString()).emit('character:moved', character)
} }
} }
function interpolatePosition(start: Node, end: Node, progress: number): Node { const interpolatePosition = (start: { x: number; y: number }, end: { x: number; y: number }, progress: number) => ({
return {
f: 0, g: 0, h: 0,
x: start.x + (end.x - start.x) * progress, x: start.x + (end.x - start.x) * progress,
y: start.y + (end.y - start.y) * progress y: start.y + (end.y - start.y) * progress
} })
}
function isObstacle(position: { x: number; y: number }, grid: number[][]): boolean { const isObstacle = ({ x, y }: { x: number; y: number }, grid: number[][]) => grid[Math.floor(y)]?.[Math.floor(x)] === 1
const x = Math.floor(position.x)
const y = Math.floor(position.y)
return grid[y] && grid[y][x] === 1
}
async function updateCharacterPosition(character: Character, x: number, y: number, rotation: number) {
character.position_x = x
character.position_y = y
character.rotation = rotation
async function updateCharacter(character: ExtendedCharacter, { x, y }: { x: number; y: number }, rotation: number) {
Object.assign(character, { position_x: x, position_y: y, rotation })
ZoneManager.updateCharacterInZone(character.zoneId, character)
await prisma.character.update({ await prisma.character.update({
where: { id: character.id }, where: { id: character.id },
data: { position_x: x, position_y: y, rotation } data: { position_x: x, position_y: y, rotation }

View File

@ -20,7 +20,6 @@ export default function (socket: TSocket, io: Server) {
const zone = await ZoneRepository.getById(character.zoneId) const zone = await ZoneRepository.getById(character.zoneId)
if (!zone) return if (!zone) return
callback(true) callback(true)
io.to(zone.id.toString()).emit('chat:message', { io.to(zone.id.toString()).emit('chat:message', {

View File

@ -5,8 +5,7 @@ export function isCommand(message: string, command?: string) {
return message.startsWith(':') return message.startsWith(':')
} }
export function getArgs(command: string, message: string): string[] | undefined export function getArgs(command: string, message: string): string[] | undefined {
{
if (!isCommand(message, command)) return if (!isCommand(message, command)) return
return message.split(`:${command} `)[1].split(' ') return message.split(`:${command} `)[1].split(' ')
} }

View File

@ -1,55 +1,45 @@
interface Position { type Position = { x: number; y: number }
x: number export type Node = Position & { parent?: Node; g: number; h: number; f: number }
y: number
}
export interface Node extends Position {
parent?: Node
g: number
h: number
f: number
}
export class AStar { export class AStar {
private static readonly ORTHOGONAL_DIRECTIONS: Position[] = [ private static readonly DIRECTIONS = [
{ x: 0, y: -1 }, { x: 0, y: 1 }, { x: -1, y: 0 }, { x: 1, y: 0 } { x: 0, y: -1 },
{ x: 0, y: 1 },
{ x: -1, y: 0 },
{ x: 1, y: 0 },
{ x: -1, y: -1 },
{ x: -1, y: 1 },
{ x: 1, y: -1 },
{ x: 1, y: 1 }
] ]
private static readonly DIAGONAL_DIRECTIONS: Position[] = [ static findPath(start: Position, end: Position, grid: number[][], allowDiagonal = false): Node[] {
{ x: -1, y: -1 }, { x: -1, y: 1 }, { x: 1, y: -1 }, { x: 1, y: 1 } const openList: Node[] = [{ ...start, g: 0, h: 0, f: 0 }]
]
static findPath(start: Position, end: Position, grid: number[][], allowDiagonal: boolean = false): Node[] {
const openList: Node[] = []
const closedSet = new Set<string>() const closedSet = new Set<string>()
const getKey = (p: Position) => `${p.x},${p.y}`
const startNode: Node = { ...start, g: 0, h: 0, f: 0 }
openList.push(startNode)
while (openList.length > 0) { while (openList.length > 0) {
const currentNode = this.getLowestFScoreNode(openList) 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)
if (this.isEndNode(currentNode, end)) { openList.splice(openList.indexOf(current), 1)
return this.reconstructPath(currentNode) closedSet.add(getKey(current))
}
this.removeNodeFromOpenList(openList, currentNode) const neighbors = this.DIRECTIONS.slice(0, allowDiagonal ? 8 : 4)
closedSet.add(this.nodeToString(currentNode)) .map((dir) => ({ x: current.x + dir.x, y: current.y + dir.y }))
.filter((pos) => this.isValidPosition(pos, grid, end))
for (const neighbor of this.getValidNeighbors(currentNode, grid, end, allowDiagonal)) { for (const neighbor of neighbors) {
if (closedSet.has(this.nodeToString(neighbor))) continue if (closedSet.has(getKey(neighbor))) continue
const tentativeGScore = currentNode.g + this.getDistance(currentNode, neighbor) const g = current.g + this.getDistance(current, neighbor)
const existing = openList.find((node) => node.x === neighbor.x && node.y === neighbor.y)
if (!this.isInOpenList(openList, neighbor) || tentativeGScore < neighbor.g) { if (!existing || g < existing.g) {
neighbor.parent = currentNode const h = this.getDistance(neighbor, end)
neighbor.g = tentativeGScore const node: Node = { ...neighbor, g, h, f: g + h, parent: current }
neighbor.h = this.heuristic(neighbor, end) if (!existing) openList.push(node)
neighbor.f = neighbor.g + neighbor.h else Object.assign(existing, node)
if (!this.isInOpenList(openList, neighbor)) {
openList.push(neighbor)
}
} }
} }
} }
@ -57,76 +47,21 @@ export class AStar {
return [] // No path found return [] // No path found
} }
private static getLowestFScoreNode(nodes: Node[]): Node {
return nodes.reduce((min, node) => (node.f < min.f ? node : min))
}
private static isEndNode(node: Node, end: Position): boolean {
return node.x === end.x && node.y === end.y
}
private static removeNodeFromOpenList(openList: Node[], node: Node): void {
const index = openList.findIndex((n) => n.x === node.x && n.y === node.y)
if (index !== -1) openList.splice(index, 1)
}
private static nodeToString(node: Position): string {
return `${node.x},${node.y}`
}
private static getValidNeighbors(node: Node, grid: number[][], end: Position, allowDiagonal: boolean): Node[] {
const directions = allowDiagonal
? [...this.ORTHOGONAL_DIRECTIONS, ...this.DIAGONAL_DIRECTIONS]
: this.ORTHOGONAL_DIRECTIONS
return directions
.map((dir) => ({
x: node.x + dir.x,
y: node.y + dir.y,
g: 0,
h: 0,
f: 0
}))
.filter((pos) => this.isValidPosition(pos, grid, end))
}
private static isValidPosition(pos: Position, grid: number[][], end: Position): boolean { private static isValidPosition(pos: Position, grid: number[][], end: Position): boolean {
const { x, y } = pos 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))
if (!grid || grid.length === 0 || !Array.isArray(grid[0])) {
return false;
}
const height = grid.length;
const width = grid[0].length;
return x >= 0 && x < width && y >= 0 && y < height &&
(grid[y][x] === 0 || (x === end.x && y === end.y));
}
private static isInOpenList(openList: Node[], node: Position): boolean {
return openList.some((n) => n.x === node.x && n.y === node.y)
} }
private static getDistance(a: Position, b: Position): number { private static getDistance(a: Position, b: Position): number {
const dx = Math.abs(a.x - b.x) const dx = Math.abs(a.x - b.x),
const dy = Math.abs(a.y - b.y) dy = Math.abs(a.y - b.y)
return Math.sqrt(dx * dx + dy * dy) return Math.sqrt(dx * dx + dy * dy)
} }
private static heuristic(node: Position, goal: Position): number {
return this.getDistance(node, goal)
}
private static reconstructPath(endNode: Node): Node[] { private static reconstructPath(endNode: Node): Node[] {
const path: Node[] = [] const path: Node[] = []
let currentNode: Node | undefined = endNode for (let current: Node | undefined = endNode; current; current = current.parent) {
path.unshift(current)
while (currentNode) {
path.unshift(currentNode)
currentNode = currentNode.parent
} }
return path return path
} }
} }