fix pathfinding for real
This commit is contained in:
+8
-12
@@ -7,10 +7,11 @@ export default class Pathfind {
|
||||
static precision = 0.001
|
||||
static multiplier = 1000 // (1 / this.precision)
|
||||
|
||||
static key(pos) {
|
||||
return `${pos.x},${pos.y}`
|
||||
static key2(a, b) {
|
||||
return `${a},${b}`
|
||||
}
|
||||
|
||||
// TODO: Value exceeds safe integer limit: collisions cause waypointing anomalies
|
||||
static floatKey4(a, b, c, d) {
|
||||
return Math.floor(a * Pathfind.multiplier) +
|
||||
Math.floor(b * Pathfind.multiplier) * Pathfind.multiplier +
|
||||
@@ -18,16 +19,11 @@ export default class Pathfind {
|
||||
Math.floor(d * Pathfind.multiplier) * Pathfind.multiplier ** 3
|
||||
}
|
||||
|
||||
static floatKey2(a, b) {
|
||||
return Math.floor(a * Pathfind.multiplier) +
|
||||
Math.floor(b * Pathfind.multiplier) * Pathfind.multiplier
|
||||
}
|
||||
|
||||
static uniqueWaypoints(waypoints) {
|
||||
const included = new Set()
|
||||
const uniqueWaypoints = []
|
||||
for (const waypoint of waypoints) {
|
||||
const key = Pathfind.floatKey2(waypoint[0], waypoint[1])
|
||||
const key = Pathfind.key2(waypoint[0], waypoint[1])
|
||||
if (!included.has(key)) {
|
||||
included.add(key)
|
||||
uniqueWaypoints.push(waypoint)
|
||||
@@ -52,16 +48,16 @@ export default class Pathfind {
|
||||
return path
|
||||
}
|
||||
|
||||
const waypointKey = Pathfind.floatKey2(waypoint)
|
||||
const waypointKey = Pathfind.key2(waypoint[0], waypoint[1])
|
||||
if (!visited.has(waypointKey) || visited.get(waypointKey) > cost) {
|
||||
visited.set(waypointKey, cost)
|
||||
|
||||
for (let i = 0; i < graph.length; i += 5) {
|
||||
if (Math.abs(waypoint[0] - graph[i]) < Pathfind.precision && Math.abs(waypoint[1] - graph[i + 1]) < Pathfind.precision) {
|
||||
continue
|
||||
if (Math.abs(waypoint[0] - graph[i]) > Pathfind.precision || Math.abs(waypoint[1] - graph[i + 1]) > Pathfind.precision) {
|
||||
continue // waypoint and graph.from aren't the same (so graph.to isn't a neighbor)
|
||||
}
|
||||
|
||||
const nextKey = `${graph[i + 2]},${graph[i + 3]}`
|
||||
const nextKey = Pathfind.key2(graph[i + 2], graph[i + 3])
|
||||
if (!visited.has(nextKey) || visited.get(nextKey) > cost + graph[i + 4]) {
|
||||
const next = new Float32Array(2)
|
||||
next[0] = graph[i + 2]
|
||||
|
||||
Reference in New Issue
Block a user