1
0
forked from noxious/server

Added logics for a* pathfinding , rotation calculation and anti cheat, npm update.

This commit is contained in:
2024-07-28 00:46:13 +02:00
parent 7f84c4fedf
commit f2fcc67cb6
6 changed files with 242 additions and 31 deletions

View File

@ -0,0 +1,84 @@
// utilities/AStar.ts
type Node = {
x: number
y: number
parent?: Node
g: number
h: number
f: number
}
class AStar {
static findPath(start: Node, end: Node, grid: number[][]): Node[] {
// Initialize the open and closed lists
let openList: Node[] = []
let closedList: Node[] = []
// Push the start node onto the open list
openList.push(start)
while (openList.length > 0) {
// Get the node with the lowest f value
let currentNode = openList.reduce((prev, curr) => (prev.f < curr.f ? prev : curr))
// Move the current node to the closed list
openList = openList.filter((node) => node !== currentNode)
closedList.push(currentNode)
// Check if we've reached the end
if (currentNode.x === end.x && currentNode.y === end.y) {
let path: Node[] = []
let curr = currentNode
while (curr) {
path.push(curr)
curr = curr.parent as Node
}
return path.reverse()
}
// Get the neighboring nodes
let neighbors = this.getNeighbors(currentNode, grid)
for (let neighbor of neighbors) {
if (closedList.some((node) => node.x === neighbor.x && node.y === neighbor.y)) {
continue
}
neighbor.g = currentNode.g + 1
neighbor.h = Math.abs(neighbor.x - end.x) + Math.abs(neighbor.y - end.y)
neighbor.f = neighbor.g + neighbor.h
neighbor.parent = currentNode
if (!openList.some((node) => node.x === neighbor.x && node.y === neighbor.y)) {
openList.push(neighbor)
}
}
}
// No path found
return []
}
private static getNeighbors(node: Node, grid: number[][]): Node[] {
let neighbors: Node[] = []
let directions = [
{ x: 0, y: -1 },
{ x: 0, y: 1 },
{ x: -1, y: 0 },
{ x: 1, y: 0 }
]
for (let direction of directions) {
let x = node.x + direction.x
let y = node.y + direction.y
if (x >= 0 && y >= 0 && x < grid.length && y < grid[0].length && grid[x][y] === 0) {
neighbors.push({ x, y, g: 0, h: 0, f: 0 })
}
}
return neighbors
}
}
export default AStar

View File

@ -0,0 +1,25 @@
class Rotation {
static calculate(X1: number, Y1: number, X2: number, Y2: number): number {
let rotation = 0
if (X1 > X2 && Y1 > Y2) {
rotation = 7
} else if (X1 < X2 && Y1 < Y2) {
rotation = 3
} else if (X1 > X2 && Y1 < Y2) {
rotation = 5
} else if (X1 < X2 && Y1 > Y2) {
rotation = 1
} else if (X1 > X2) {
rotation = 6
} else if (X1 < X2) {
rotation = 2
} else if (Y1 < Y2) {
rotation = 4
} else if (Y1 > Y2) {
rotation = 0
}
return rotation
}
}
export default Rotation