a bit of bug fixing regarding implementation of dijstra's algorithm (next to fix: query towards quad/platform tree)

This commit is contained in:
2025-01-20 02:38:22 +00:00
parent 322a394138
commit 6783dac9e0
3 changed files with 33 additions and 49 deletions

View File

@@ -31,7 +31,7 @@ struct
val ey = ey + Constants.enemySize - 1 val ey = ey + Constants.enemySize - 1
val width = Constants.enemySize val width = Constants.enemySize
val height = 2 val height = Platform.platHeight
val ww = Constants.worldWidth val ww = Constants.worldWidth
val wh = Constants.worldHeight val wh = Constants.worldHeight
@@ -746,26 +746,12 @@ struct
val eID = getPlatformBelowEnemy (enemy, platformTree, platforms) val eID = getPlatformBelowEnemy (enemy, platformTree, platforms)
val (bestDist, bestPath) = val _ = print "start best path:\n"
if eID > ~1 andalso pID > ~1 then
getUpwardsPath (pID, eID, platforms, platformTree, 0, "")
else
(~1, [])
val _ = val bestPath = PathFinding.start (pID, eID, platforms, platformTree)
if bestDist = ~1 then val _ = List.map (fn c => print (Int.toString c ^ "\n")) bestPath
print "no path\n"
else val _ = print "finished best path\n\n"
let
val _ = print "has path:\n"
val _ =
List.map
(fn platID =>
print ("best path platID: " ^ Int.toString platID ^ "\n"))
bestPath
in
()
end
val distance = Constants.moveEnemyBy * Constants.jumpLimit val distance = Constants.moveEnemyBy * Constants.jumpLimit

View File

@@ -74,13 +74,17 @@ struct
| insert (x, ts) = | insert (x, ts) =
NODE (x, 0, []) :: ts NODE (x, 0, []) :: ts
fun findMin [] = Elem.default fun helpFindMin (prev, []) = root prev
| findMin [t] = root t | helpFindMin (prev, [t]) = root t
| findMin (t :: ts) = | helpFindMin (prev, t :: ts) =
let val x = findMin ts let val x = helpFindMin (t, ts)
in if Elem.leq (root t, x) then root t else x in if Elem.leq (root t, x) then root t else x
end end
fun findMin [] = Elem.default
| findMin [t] = root t
| findMin (t :: ts) = helpFindMin (t, ts)
fun getMin (prevT, t) = fun getMin (prevT, t) =
case t of case t of
[t] => (t, []) [t] => (t, [])

View File

@@ -249,10 +249,12 @@ struct
let let
val acc = curID :: acc val acc = curID :: acc
val pos = IntSet.findInsPos (curID, eKeys) val pos = IntSet.findInsPos (curID, eKeys)
val {from, ...} = ValSet.sub (eVals, pos) val {from, ...} = ValSet.sub (eVals, pos)
val from = Char.ord from val from = Char.ord from
in in
helpGetPathList (from, eID, eKeys, eVals, acc) if from = curID then acc
else helpGetPathList (from, eID, eKeys, eVals, acc)
end end
fun getPathList (pID, eID, eKeys, eVals) = fun getPathList (pID, eID, eKeys, eVals) =
@@ -262,9 +264,8 @@ struct
let let
(* filtering duplicates because we have no decrease-key operation *) (* filtering duplicates because we have no decrease-key operation *)
val q = filterMinDuplicates (q, eKeys) val q = filterMinDuplicates (q, eKeys)
val pidPos = IntSet.findInsPos (pID, eKeys)
in in
if pidPos = ~1 orelse pidPos = Vector.length eKeys then if IntSet.contains (pID, eKeys) then
(* return path if we explored pid *) (* return path if we explored pid *)
getPathList (pID, eID, eKeys, eVals) getPathList (pID, eID, eKeys, eVals)
else else
@@ -275,10 +276,6 @@ struct
if minID = ~1 then if minID = ~1 then
(* return empty list to signify that there is no path *) (* return empty list to signify that there is no path *)
[] []
else if minID = pID then
(* found path to destination so reconstruct path and return
* todo: maybe delete branch? pID is not explored... *)
getPathList (pID, eID, eKeys, eVals)
else else
(* find reachable values from min in quad tree *) (* find reachable values from min in quad tree *)
let let
@@ -300,12 +297,14 @@ struct
val state = (eVals, q) val state = (eVals, q)
val {x, y, width, ...} = plat (* calculate area to fold over quad tree *)
val height = Platform.platHeight
val ww = Constants.worldWidth val ww = Constants.worldWidth
val wh = Constants.worldHeight val wh = Constants.worldHeight
val {x, y, width, ...} = plat
val y = y - Constants.jumpLimit
val height = wh - y
(* fold over quad tree, updating any distances (* fold over quad tree, updating any distances
* we find the shortest path for *) * we find the shortest path for *)
val (eVals, q) = FindReachable.foldRegion val (eVals, q) = FindReachable.foldRegion
@@ -322,13 +321,10 @@ struct
val q = DistHeap.empty val q = DistHeap.empty
val q = DistHeap.insert ({distance = 0, id = eID}, q) val q = DistHeap.insert ({distance = 0, id = eID}, q)
(* initialise explored keys and values *) (* explored keys and values *)
val eKeys = IntSet.empty val eKeys = IntSet.empty
val eVals = ValSet.empty val eVals = ValSet.empty
val insPos = IntSet.findInsPos (eID, eKeys)
val eKeys = IntSet.insert (eKeys, eID, insPos)
(* important: starting node will have a key that points to itself. (* important: starting node will have a key that points to itself.
* For example, if starting node is #"e", then the record will say * For example, if starting node is #"e", then the record will say
* the "from" field is #"e". * the "from" field is #"e".
@@ -338,8 +334,6 @@ struct
* If the key matching this value is the same as the "from" node, * If the key matching this value is the same as the "from" node,
* then we're done reconstructing the path and can return the path list. * then we're done reconstructing the path and can return the path list.
* *) * *)
val eVal = {distance = 0, from = Char.chr eID}
val eVals = ValSet.insert (eVals, eVal, insPos)
in in
loop (pID, eID, platforms, platformTree, q, eKeys, eVals) loop (pID, eID, platforms, platformTree, q, eKeys, eVals)
end end