Moved fValue functions into the pathfinding algorithms and pass it to TileHeap for use

main
Bryson Zimmerman 11 months ago
parent b90526f3f1
commit d2d6fc4dd4

@ -3,6 +3,8 @@ package technology.zim
import technology.zim.data.Directions
import technology.zim.data.Tile
import technology.zim.data.TileHeap
import technology.zim.data.TileNavigatedArray
import kotlin.math.abs
//A* pathfinder backed by an array to improve efficiency
@ -11,7 +13,7 @@ import technology.zim.data.TileHeap
object ArrayBackedPathfinder {
//TODO: Replace with array for coordinate lookups for speed, do it in a separate pathfinder class to demonstrate the difference
val gVals = HashMap<Tile, Int>()
val gVals = TileNavigatedArray<Int>()
//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) {
@ -24,10 +26,10 @@ object ArrayBackedPathfinder {
return
}
val frontier = TileHeap(end, gVals)
val frontier = TileHeap(end, this::fValue)
//Prime the things
gVals.put(start, 0)
gVals.set(start, 0)
frontier.insert(start)
var current: Tile
@ -44,7 +46,7 @@ object ArrayBackedPathfinder {
if(candidateTile.isInBounds() && candidateG == -1)
{
//Otherwise, the tile has been reached and this path is not better, so carry on
gVals.put(candidateTile, currentG + 1)
gVals.set(candidateTile, currentG + 1)
frontier.insert(candidateTile)
World.update(candidateTile, candidateTile.getProperties() +Directions.FRONTIER)
}
@ -74,4 +76,11 @@ object ArrayBackedPathfinder {
World.update(start, start.getProperties() + Directions.INPATH)
}
private fun fValue(prospect: Tile, end: Tile): Int {
return hValue(prospect, end).plus(MapBackedPathfinder.gVals.get(prospect) ?: 0)
}
private fun hValue(prospect: Tile, end:Tile): Int {
return abs(prospect.x() - end.x()) + abs(prospect.y() - end.y())
}
}

@ -3,6 +3,7 @@ package technology.zim
import technology.zim.data.Directions
import technology.zim.data.Tile
import technology.zim.data.TileHeap
import kotlin.math.abs
//A* to be upgraded with hierarchies
@ -23,7 +24,7 @@ object MapBackedPathfinder {
return
}
val frontier = TileHeap(end, gVals)
val frontier = TileHeap(end, this::fValue)
//Prime the things
gVals.put(start, 0)
@ -52,6 +53,15 @@ object MapBackedPathfinder {
//At this point, a path is found
println("Path found!")
markPath(start, end)
}
private fun fValue(prospect: Tile, end: Tile): Int {
return hValue(prospect, end).plus(gVals.get(prospect) ?: 0)
}
private fun hValue(prospect: Tile, end:Tile): Int {
return abs(prospect.x() - end.x()) + abs(prospect.y() - end.y())
}
fun markPath(start: Tile, end:Tile) {

@ -6,7 +6,7 @@ import kotlin.math.abs
//Translated code from CS222 MaxHeap homework
//Cannot use index 0 due to Integer limitations
//TODO: Consider better options than passing in the information this thing needs
class TileHeap(val end: Tile, val gVals: HashMap<Tile, Int>) {
class TileHeap(val end: Tile, val fValue:(Tile, Tile) -> Int) {
val dat = ArrayList<Tile>()
init {
//Shove some data into the zero slot
@ -36,7 +36,7 @@ class TileHeap(val end: Tile, val gVals: HashMap<Tile, Int>) {
if(dat.isEmpty())
throw ArrayIndexOutOfBoundsException()
if(fValue(dat[index]) < fValue(dat[parent(index)]) && index > 1) {
if(fValue(dat[index], end) < fValue(dat[parent(index)], end) && index > 1) {
swap(index, parent(index))
siftUp(parent(index))
}
@ -47,12 +47,12 @@ class TileHeap(val end: Tile, val gVals: HashMap<Tile, Int>) {
var minValueIndex = index
val l = leftChild(index)
if(l < dat.size && fValue(dat[l]) < fValue(dat[minValueIndex]) ) {
if(l < dat.size && fValue(dat[l], end) < fValue(dat[minValueIndex], end) ) {
minValueIndex = l
}
val r = rightChild(index)
if(r < dat.size && fValue(dat[r]) < fValue(dat[minValueIndex])) {
if(r < dat.size && fValue(dat[r], end) < fValue(dat[minValueIndex], end) ) {
minValueIndex = r
}
@ -78,12 +78,4 @@ class TileHeap(val end: Tile, val gVals: HashMap<Tile, Int>) {
return index > (dat.size / 2)
}
private fun fValue(prospect: Tile): Int {
return hValue(prospect).plus(gVals.get(prospect) ?: 0)
}
private fun hValue(prospect: Tile): Int {
return abs(prospect.x() - end.x()) + abs(prospect.y() - end.y())
}
}
Loading…
Cancel
Save