|
|
|
@ -78,9 +78,36 @@ fun findPath(
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Add out-of-bounds pathfinding
|
|
|
|
|
// If there is not a path to the requested tile, try to find the
|
|
|
|
|
// nearest pathable tile.
|
|
|
|
|
val dist = { sp: ScenePoint ->
|
|
|
|
|
val dx = sp.getX() - to.getX()
|
|
|
|
|
val dy = sp.getY() - to.getY()
|
|
|
|
|
|
|
|
|
|
return null
|
|
|
|
|
(dx * dx) + (dy * dy)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val candidates = ArrayList(backEdges.keys)
|
|
|
|
|
candidates.add(from)
|
|
|
|
|
|
|
|
|
|
val candidateDistances = candidates.map({ sp ->
|
|
|
|
|
Pair(sp, dist(sp))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
val minDistance = candidateDistances.minByOrNull({ c -> c.second })
|
|
|
|
|
if (minDistance == null) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val minimalCandidates = candidateDistances.filter({ c ->
|
|
|
|
|
c.second == minDistance.second
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
val minPath = minimalCandidates.mapNotNull({ c ->
|
|
|
|
|
buildPath(backEdges, c.first)
|
|
|
|
|
}).minWithOrNull({ a, b -> a.size - b.size })
|
|
|
|
|
|
|
|
|
|
return minPath
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun sceneX(t: RLTile) = t.sceneLocation.x
|
|
|
|
|