write code for new loop implementation of dijkstra's algorithm, which uses a precomputed graphy; however, this is not used yet as we need to add the graph to the game type first, and pass it along with other parameters, down to the path-finding.sml function

This commit is contained in:
2025-01-31 04:10:58 +00:00
parent a24ada0f37
commit f3a0b2bc81
2 changed files with 73 additions and 5 deletions

View File

@@ -35,6 +35,69 @@ struct
fun getPathList (pID, eID, eKeys, eVals) =
helpGetPathList (pID, eID, eKeys, eVals, [])
fun helpExplore (pos, graphNode, minID, distSoFar, q) =
if pos = Vector.length graphNode then
q
else
let
val {distance = newPlatDist, id = newPlatID} =
Vector.sub (graphNode, pos)
val totalDist = newPlatDist + distSoFar
val insRecord =
{distance = totalDist, id = newPlatID, comesFrom = minID}
val insPos = DistVec.findInsPos (insRecord, q)
val q = DistVec.insert (q, insRecord, insPos)
in
helpExplore (pos + 1, graphNode, minID, distSoFar, q)
end
fun explore (graphNode, minID, distSoFar, q) =
helpExplore (0, graphNode, minID, distSoFar, q)
fun loop (pID, eID, platforms, platformTree, q, eKeys, eVals, graph) =
if IntSet.contains (pID, eKeys) then
(* return path if we explored pid *)
getPathList (pID, eID, eKeys, eVals)
else
(* continue dijkstra's algorithm *)
let
(* filtering duplicates because we have no decrease-key operation *)
val q = filterMinDuplicates (q, eKeys)
in
if DistVec.isEmpty q then
(* return empty list to signify that there is no path *)
[]
else
(* find reachable values from min in quad tree *)
let
val {distance = distSoFar, id = minID, comesFrom} =
DistVec.findMin q
(* explore platforms connected to minID *)
val platPos = Platform.findPos (minID, platforms)
val plat = Vector.sub (platforms, platPos)
val graphNode = Vector.sub (graph, platPos)
(* on each loop, increment distSoFar by 15.
* Result: paths that require jumps to fewer platforms are
* incentivised a little bit. *)
val q = explore (graphNode, minID, distSoFar + 15, q)
(* mark platform with (id = minID) as explored *)
val insPos = IntSet.findInsPos (minID, eKeys)
val eKeys = IntSet.insert (eKeys, minID, insPos)
val eVals =
ValSet.insert
(eVals, {distance = distSoFar, from = comesFrom}, insPos)
in
loop (pID, eID, platforms, platformTree, q, eKeys, eVals, graph)
end
end
(* dead loop function: remove after adding graph to game type
* and moving to other loop *)
fun loop (pID, eID, platforms, platformTree, q, eKeys, eVals) =
if IntSet.contains (pID, eKeys) then
(* return path if we explored pid *)