|
|
@ -9,14 +9,10 @@ import kotlin.math.abs
|
|
|
|
|
|
|
|
|
|
|
|
//A* pathfinder backed by an array to improve efficiency
|
|
|
|
//A* pathfinder backed by an array to improve efficiency
|
|
|
|
|
|
|
|
|
|
|
|
//Needs https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/PriorityQueue.html
|
|
|
|
|
|
|
|
//and https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Comparator.html
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
object ArrayBackedPathfinder {
|
|
|
|
object ArrayBackedPathfinder {
|
|
|
|
var gVals = TileNavigatedArray<Int>(World.sizeX, World.sizeY, false)
|
|
|
|
var gVals = TileNavigatedArray<Int>(World.sizeX, World.sizeY, false)
|
|
|
|
var pathLength = 0
|
|
|
|
var pathLength = 0
|
|
|
|
//work along the path, marking tiles with VISITED along the way
|
|
|
|
|
|
|
|
//if marking with visited is too expensive, just make the path and finalize it
|
|
|
|
|
|
|
|
fun generatePath(start: Tile, end: Tile) {
|
|
|
|
fun generatePath(start: Tile, end: Tile) {
|
|
|
|
if(!start.isInBounds() || !end.isInBounds()) {
|
|
|
|
if(!start.isInBounds() || !end.isInBounds()) {
|
|
|
|
throw IndexOutOfBoundsException("Cannot generate a path to or from an out of bounds tile")
|
|
|
|
throw IndexOutOfBoundsException("Cannot generate a path to or from an out of bounds tile")
|
|
|
@ -31,7 +27,7 @@ object ArrayBackedPathfinder {
|
|
|
|
gVals = TileNavigatedArray<Int>(World.sizeX, World.sizeY, false)
|
|
|
|
gVals = TileNavigatedArray<Int>(World.sizeX, World.sizeY, false)
|
|
|
|
val frontier = TileHeap(end, this::fValue)
|
|
|
|
val frontier = TileHeap(end, this::fValue)
|
|
|
|
|
|
|
|
|
|
|
|
//Prime the things
|
|
|
|
//Prime the frontier
|
|
|
|
gVals.set(start, 0)
|
|
|
|
gVals.set(start, 0)
|
|
|
|
frontier.insert(start)
|
|
|
|
frontier.insert(start)
|
|
|
|
|
|
|
|
|
|
|
@ -42,7 +38,6 @@ object ArrayBackedPathfinder {
|
|
|
|
current = frontier.popMin()
|
|
|
|
current = frontier.popMin()
|
|
|
|
currentG = gVals.get(current) ?: 0.also { println("Failed to get gVal that must exist") }
|
|
|
|
currentG = gVals.get(current) ?: 0.also { println("Failed to get gVal that must exist") }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
current.getConnections().forEach { candidateTile ->
|
|
|
|
current.getConnections().forEach { candidateTile ->
|
|
|
|
val candidateG = gVals.get(candidateTile)?:-1
|
|
|
|
val candidateG = gVals.get(candidateTile)?:-1
|
|
|
|
//Ensure that the tile is within bounds
|
|
|
|
//Ensure that the tile is within bounds
|
|
|
@ -58,7 +53,6 @@ object ArrayBackedPathfinder {
|
|
|
|
|
|
|
|
|
|
|
|
//At this point, a path is found
|
|
|
|
//At this point, a path is found
|
|
|
|
markPath(start, end)
|
|
|
|
markPath(start, end)
|
|
|
|
//println("Path found!")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fun markPath(start: Tile, end:Tile) {
|
|
|
|
fun markPath(start: Tile, end:Tile) {
|
|
|
|