diff --git a/src/main/kotlin/ArrayBackedPathfinder.kt b/src/main/kotlin/ArrayBackedPathfinder.kt index fe91869..a677ce1 100644 --- a/src/main/kotlin/ArrayBackedPathfinder.kt +++ b/src/main/kotlin/ArrayBackedPathfinder.kt @@ -9,14 +9,10 @@ import kotlin.math.abs //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 { var gVals = TileNavigatedArray(World.sizeX, World.sizeY, false) 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) { if(!start.isInBounds() || !end.isInBounds()) { throw IndexOutOfBoundsException("Cannot generate a path to or from an out of bounds tile") @@ -31,7 +27,7 @@ object ArrayBackedPathfinder { gVals = TileNavigatedArray(World.sizeX, World.sizeY, false) val frontier = TileHeap(end, this::fValue) - //Prime the things + //Prime the frontier gVals.set(start, 0) frontier.insert(start) @@ -42,7 +38,6 @@ object ArrayBackedPathfinder { current = frontier.popMin() currentG = gVals.get(current) ?: 0.also { println("Failed to get gVal that must exist") } - current.getConnections().forEach { candidateTile -> val candidateG = gVals.get(candidateTile)?:-1 //Ensure that the tile is within bounds @@ -58,7 +53,6 @@ object ArrayBackedPathfinder { //At this point, a path is found markPath(start, end) - //println("Path found!") } fun markPath(start: Tile, end:Tile) { diff --git a/src/main/kotlin/BFSPathfinder.kt b/src/main/kotlin/BFSPathfinder.kt index de8b34a..0d95b57 100644 --- a/src/main/kotlin/BFSPathfinder.kt +++ b/src/main/kotlin/BFSPathfinder.kt @@ -23,7 +23,11 @@ object BFSPathfinder { //Queue for tiles to be checked val frontier = ArrayDeque() frontier.addLast(start) + + //Set the starting Tile's gValue to 0 because it is 0 steps away from itself gVals.set(start, 0) + + //Add the starting tile to the frontier to kick off the process World.update(start, Directions.BFSFRONTIER) var current:Tile diff --git a/src/main/kotlin/World.kt b/src/main/kotlin/World.kt index b87ccbd..6c127ed 100644 --- a/src/main/kotlin/World.kt +++ b/src/main/kotlin/World.kt @@ -136,10 +136,4 @@ object World { else -> '•' } } - - //Reads array left to right, top to bottom - //Only looks at SOUTH and EAST connections - //Either connection exists or it does not, whitespace character for exists, some block-appearing char for not - //Needs one monowidth char space between each column of array - //Needs one line between each row, line containing vertical connections } \ No newline at end of file diff --git a/src/main/kotlin/data/Directions.kt b/src/main/kotlin/data/Directions.kt index a0f62c6..b1dbb9c 100644 --- a/src/main/kotlin/data/Directions.kt +++ b/src/main/kotlin/data/Directions.kt @@ -18,7 +18,7 @@ enum class Directions(val dir: Int) { companion object { - //Todo: This breaks the connect() function when used + //Todo: This breaks the connect() function when used. Fixing it would be nice, but is unnecessary. fun opposite(dir: Directions): Directions { return when(dir) { UP -> DOWN diff --git a/src/main/kotlin/data/TileNavigatedArray.kt b/src/main/kotlin/data/TileNavigatedArray.kt index ac7b472..14b6384 100644 --- a/src/main/kotlin/data/TileNavigatedArray.kt +++ b/src/main/kotlin/data/TileNavigatedArray.kt @@ -1,8 +1,5 @@ package technology.zim.data -import java.util.Arrays -import kotlin.arrayOfNulls - //Generic array that accepts a Tile for item lookups //Row-major for improved memory locality.. maybe make it flexible?