Compare commits

...

3 Commits

@ -13,10 +13,11 @@ import kotlin.math.abs
object ArrayBackedPathfinder { object ArrayBackedPathfinder {
//TODO: Replace with array for coordinate lookups for speed, do it in a separate pathfinder class to demonstrate the difference //TODO: Replace with array for coordinate lookups for speed, do it in a separate pathfinder class to demonstrate the difference
val gVals = TileNavigatedArray<Int>() val gVals = TileNavigatedArray<Int>(World.sizeX, World.sizeY, false)
//work along the path, marking tiles with VISITED along the way //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 //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")
} }
@ -54,6 +55,7 @@ object ArrayBackedPathfinder {
} while( current != end) } while( current != end)
//At this point, a path is found //At this point, a path is found
markPath(start, end)
println("Path found!") println("Path found!")
} }
@ -77,7 +79,7 @@ object ArrayBackedPathfinder {
} }
private fun fValue(prospect: Tile, end: Tile): Int { private fun fValue(prospect: Tile, end: Tile): Int {
return hValue(prospect, end).plus(MapBackedPathfinder.gVals.get(prospect) ?: 0) return hValue(prospect, end).plus(gVals.get(prospect) ?: 0)
} }
private fun hValue(prospect: Tile, end:Tile): Int { private fun hValue(prospect: Tile, end:Tile): Int {

@ -9,19 +9,33 @@ class HierarchicalPathfinding {
@JvmStatic @JvmStatic
fun main(args: Array<String>) { fun main(args: Array<String>) {
val n = 1000 val n = 1000
println("Building maze")
var startTime = System.currentTimeMillis()
buildMaze(n) buildMaze(n)
var endTime = System.currentTimeMillis()
val buildMazeTime = endTime - startTime
println("Pathfinding") println("Pathfinding")
var startTime = System.currentTimeMillis()
startTime = System.currentTimeMillis()
MapBackedPathfinder.generatePath(Tile(0, 0), Tile(n-1, (n-1))) MapBackedPathfinder.generatePath(Tile(0, 0), Tile(n-1, (n-1)))
var endTime = System.currentTimeMillis() endTime = System.currentTimeMillis()
val aStarMs = endTime - startTime val MapBackedPathfinderTime = endTime - startTime
World.tiles.scrubDirections(listOf(Directions.FRONTIER, Directions.INPATH, Directions.NOPATH))
startTime = System.currentTimeMillis()
ArrayBackedPathfinder.generatePath(Tile(0, 0), Tile(n-1, (n-1)))
endTime = System.currentTimeMillis()
val ArrayBackedPathfinderTime = endTime - startTime
println(World.toString()) println(World.toString())
println(n*n) println(n*n)
println((endTime - startTime).toString() + "ms") println("Maze build time: ${buildMazeTime} ms")
println("HashMap-Backed Pathfinder time: ${MapBackedPathfinderTime}ms")
println("Array-Backed Pathfinder time: ${ArrayBackedPathfinderTime} ms")
} }
//Clear the maze of pathfinding markers before running another pathfinding algorithm //Clear the maze of pathfinding markers before running another pathfinding algorithm

@ -1,16 +1,32 @@
package technology.zim.data package technology.zim.data
import java.util.Arrays
import kotlin.arrayOfNulls
//Generic array that accepts a Tile for item lookups //Generic array that accepts a Tile for item lookups
//Row-major for improved memory locality.. maybe make it flexible? //Row-major for improved memory locality.. maybe make it flexible?
//Delegation should allow direct access to functions, with added functions for convenience //Delegation should allow direct access to functions, with added functions for convenience
class TileNavigatedArray<T>(val data: ArrayList<T?> = ArrayList<T?>(), var colSize:Int = 10, class TileNavigatedArray<T>(val data: ArrayList<T?> = ArrayList<T?>(), var colSize:Int = 10,
var rowSize:Int = 10, val colMajor:Boolean = false) : MutableList<T?> by data { var rowSize:Int = 10, val colMajor:Boolean = false) : MutableList<T?> by data {
//TODO: Create initializer that fills array with null values
constructor(col:Int, row:Int,major:Boolean) : this(colSize = col, rowSize = row,colMajor = major) { constructor(col:Int, row:Int,major:Boolean) : this(colSize = col, rowSize = row,colMajor = major) {
} }
init {
for(i in 0..rowSize*colSize){
data.add(null)
}
}
//TODO: Test resize properly
fun resize(colSize:Int, rowSize:Int) {
this.colSize = colSize
this.rowSize = rowSize
for(i in data.size..colSize*rowSize-data.size) {
data.add(null)
}
assert(data.size == this.colSize*this.rowSize)
}
//Accept a tile, use its coordinates to pull the requested data //Accept a tile, use its coordinates to pull the requested data
fun get(tile: Tile):T? { fun get(tile: Tile):T? {

Loading…
Cancel
Save