forked from noxious/server
Improved pathfinding
This commit is contained in:
@ -1,79 +1,101 @@
|
||||
import { Server } from 'socket.io'
|
||||
import { TSocket } from '../../utilities/Types'
|
||||
import ZoneManager from '../../managers/ZoneManager'
|
||||
import prisma from '../../utilities/Prisma'
|
||||
import AStar from '../../utilities/Player/AStar'
|
||||
import Rotation from '../../utilities/Player/Rotation'
|
||||
import { Server } from 'socket.io';
|
||||
import { TSocket } from '../../utilities/Types';
|
||||
import ZoneManager from '../../managers/ZoneManager';
|
||||
import prisma from '../../utilities/Prisma';
|
||||
import AStar, { type Node } from '../../utilities/Player/AStar';
|
||||
import Rotation from '../../utilities/Player/Rotation';
|
||||
|
||||
type SocketResponseT = {
|
||||
position_x: number
|
||||
position_y: number
|
||||
interface SocketResponse {
|
||||
position_x: number;
|
||||
position_y: number;
|
||||
}
|
||||
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('character:move', async (data: SocketResponseT) => {
|
||||
interface Character {
|
||||
id: number;
|
||||
position_x: number;
|
||||
position_y: number;
|
||||
rotation: number;
|
||||
zoneId: number;
|
||||
}
|
||||
|
||||
export default function setupCharacterMove(socket: TSocket, io: Server) {
|
||||
socket.on('character:move', async (data: SocketResponse) => {
|
||||
try {
|
||||
console.log('character:move requested', data)
|
||||
console.log('character:move requested', data);
|
||||
|
||||
if (!socket.character) {
|
||||
console.log('character:move error', 'Character not found')
|
||||
return
|
||||
console.error('character:move error', 'Character not found');
|
||||
return;
|
||||
}
|
||||
|
||||
const start = {
|
||||
x: socket.character.position_x,
|
||||
y: socket.character.position_y,
|
||||
g: 0,
|
||||
h: 0,
|
||||
f: 0
|
||||
}
|
||||
const end = {
|
||||
x: data.position_x,
|
||||
y: data.position_y,
|
||||
g: 0,
|
||||
h: 0,
|
||||
f: 0
|
||||
}
|
||||
|
||||
const grid = await ZoneManager.getGrid(socket.character.zoneId)
|
||||
const grid = await ZoneManager.getGrid(socket.character.zoneId);
|
||||
|
||||
if (grid.length === 0) {
|
||||
console.log('character:move error', 'Grid not found')
|
||||
return
|
||||
console.error('character:move error', 'Grid not found');
|
||||
return;
|
||||
}
|
||||
|
||||
const path = AStar.findPath(start, end, grid)
|
||||
const start = { x: socket.character.position_x, y: socket.character.position_y };
|
||||
const end = { x: data.position_x, y: data.position_y };
|
||||
|
||||
console.log('Starting position:', start);
|
||||
console.log('Target position:', end);
|
||||
console.log('Grid:', grid);
|
||||
|
||||
const path = AStar.findPath(start, end, grid);
|
||||
|
||||
console.log('Calculated path:', path);
|
||||
|
||||
if (path.length > 0) {
|
||||
const finalPosition = path[path.length - 1]
|
||||
|
||||
// Calculate the rotation
|
||||
const rotation = Rotation.calculate(socket.character.position_x, socket.character.position_y, finalPosition.x, finalPosition.y)
|
||||
|
||||
socket.character.position_x = finalPosition.x
|
||||
socket.character.position_y = finalPosition.y
|
||||
socket.character.rotation = rotation // Assuming character has a rotation property
|
||||
|
||||
await prisma.character.update({
|
||||
where: {
|
||||
id: socket.character.id
|
||||
},
|
||||
data: {
|
||||
position_x: finalPosition.x,
|
||||
position_y: finalPosition.y,
|
||||
rotation: rotation // Update the rotation in the database
|
||||
}
|
||||
})
|
||||
|
||||
ZoneManager.updateCharacterInZone(socket.character.zoneId, socket.character)
|
||||
io.in(socket.character.zoneId.toString()).emit('character:moved', socket.character)
|
||||
|
||||
console.log(socket.character)
|
||||
await moveAlongPath(socket, io, path, grid);
|
||||
} else {
|
||||
console.log('character:move error', 'No valid path found')
|
||||
console.log('character:move error', 'No valid path found');
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('character:move error', error)
|
||||
console.error('character:move error', error);
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
async function moveAlongPath(socket: TSocket, io: Server, path: Node[], grid: number[][]) {
|
||||
if (!socket.character) return;
|
||||
|
||||
for (const position of path) {
|
||||
if (isObstacle(position, grid)) {
|
||||
console.log('Obstacle encountered at', position);
|
||||
break;
|
||||
}
|
||||
|
||||
const rotation = Rotation.calculate(
|
||||
socket.character.position_x,
|
||||
socket.character.position_y,
|
||||
position.x,
|
||||
position.y
|
||||
);
|
||||
|
||||
await updateCharacterPosition(socket.character, position.x, position.y, rotation);
|
||||
|
||||
ZoneManager.updateCharacterInZone(socket.character.zoneId, socket.character);
|
||||
io.in(socket.character.zoneId.toString()).emit('character:moved', socket.character);
|
||||
|
||||
console.log('Character moved to', position);
|
||||
|
||||
// Add a small delay between moves to avoid overwhelming the server
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
}
|
||||
}
|
||||
|
||||
function isObstacle(position: Node, grid: number[][]): boolean {
|
||||
return grid[position.y][position.x] === 1;
|
||||
}
|
||||
|
||||
async function updateCharacterPosition(character: Character, x: number, y: number, rotation: number) {
|
||||
character.position_x = x;
|
||||
character.position_y = y;
|
||||
character.rotation = rotation;
|
||||
|
||||
await prisma.character.update({
|
||||
where: { id: character.id },
|
||||
data: { position_x: x, position_y: y, rotation }
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user