get path-finding.sml to use function in build-graph.sml to find reachable platforms, and remove now-dead code from path-finding.sml

This commit is contained in:
2025-01-30 13:06:32 +00:00
parent 755e5da7f7
commit bbb957f016
3 changed files with 98 additions and 372 deletions

View File

@@ -1,64 +1,60 @@
structure BuildGraph =
struct
fun insertIfNotExistsOrShorter
(dist, eKeys, eVals, foldPlatID, q, fromPlatID) =
let
val pos = IntSet.findInsPos (foldPlatID, eKeys)
in
if pos <> ~1 andalso pos <> Vector.length eKeys then
let
val key = IntSet.sub (eKeys, pos)
in
if pos = key then
(* may need to update record in eVals if it is shorter *)
let
val {distance = oldDist, ...} = ValSet.sub (eVals, pos)
in
if dist < oldDist then
(* update values as we found a shorter path *)
let
val eVals =
ValSet.updateAtIdx
(eVals, {distance = dist, from = fromPlatID}, pos)
val _ = print "UPDATE foldPlatID\n"
in
(eVals, q)
end
else
(* return existing *)
fun insertIfNotExistsOrShorter (dist, eKeys, eVals, foldPlatID, q, fromPlatID) =
let
val pos = IntSet.findInsPos (foldPlatID, eKeys)
in
if pos <> ~1 andalso pos <> Vector.length eKeys then
let
val key = IntSet.sub (eKeys, pos)
in
if pos = key then
(* may need to update record in eVals if it is shorter *)
let
val {distance = oldDist, ...} = ValSet.sub (eVals, pos)
in
if dist < oldDist then
(* update values as we found a shorter path *)
let
val eVals =
ValSet.updateAtIdx
(eVals, {distance = dist, from = fromPlatID}, pos)
in
(eVals, q)
end
else
(* key not explored, so add to queue *)
let
val insRecord =
{distance = dist, id = foldPlatID, comesFrom = fromPlatID}
val insPos = DistVec.findInsPos (insRecord, q)
val q = DistVec.insert (q, insRecord, insPos)
val _ = print "INSERT NEW PLAT\n"
in
end
else
(* return existing *)
(eVals, q)
end
end
else
(* key not explored, so add to queue *)
let
val insRecord =
{distance = dist, id = foldPlatID, comesFrom = fromPlatID}
val insPos = DistVec.findInsPos (insRecord, q)
val q = DistVec.insert (q, insRecord, insPos)
val _ = print "UPDATE foldPlatID\n"
in
(eVals, q)
end
end
end
else
(* key not explored, so add to queue *)
let
val insRecord =
{distance = dist, id = foldPlatID, comesFrom = fromPlatID}
val insPos = DistVec.findInsPos (insRecord, q)
val q = DistVec.insert (q, insRecord, insPos)
in
(eVals, q)
end
end
else
(* key not explored, so add to queue *)
let
val insRecord =
{distance = dist, id = foldPlatID, comesFrom = fromPlatID}
val insPos = DistVec.findInsPos (insRecord, q)
val q = DistVec.insert (q, insRecord, insPos)
in
(eVals, q)
end
end
type env =
{ platforms: GameType.platform vector
, currentPlat: GameType.platform
, eKeys: IntSet.elem vector
, distSoFar: int
}
type env =
{ platforms: GameType.platform vector
, currentPlat: GameType.platform
, eKeys: IntSet.elem vector
, distSoFar: int
}
(* adds platforms to queue if they have not been explored
* or, if they have been explored and distance is smaller than previous,
@@ -69,7 +65,7 @@ struct
structure Vertical =
MakeQuadTreeFold
(struct
type env = env
type env = env
type state = ValSet.elem vector * DistVec.elem vector
@@ -77,28 +73,15 @@ struct
let
val {platforms, currentPlat, eKeys, distSoFar} = env
val _ = print ("foldPlatID = " ^ Int.toString foldPlatID ^ "\n")
val {y = foldPlatY, ...} = Platform.find (foldPlatID, platforms)
val {y = currentPlatY, id = fromPlatID, ...} = currentPlat
val newDist = abs (foldPlatY - currentPlatY) + distSoFar
in
insertIfNotExistsOrShorter
(newDist, eKeys, eVals, foldPlatID, q, fromPlatID)
(newDist, eKeys, eVals, foldPlatID, q, fromPlatID)
end
end)
fun helpPrint (pos, vec) =
if pos = Vector.length vec then
()
else
let
val {id, distance, comesFrom} = DistVec.sub (vec, pos)
val _ = print ("contains (id = " ^ Int.toString id ^")\n")
in
helpPrint (pos + 1, vec)
end
fun start (currentPlat: GameType.platform, env: env, state, platformTree) =
let
val {x, y, width, ...} = currentPlat
@@ -107,14 +90,8 @@ struct
val searchY = y - Constants.jumpLimit
val height = Constants.worldHeight - searchY
val _ = print "start fold\n"
val (eVals, q) =
Vertical.foldRegion (x, searchY, width, height, env, state, platformTree)
val _ = print "finish fold\n"
val _ = print "BuildGraph q contains IDs:\n"
val _ = helpPrint (0, q)
val _ = print "\n"
val (eVals, q) = Vertical.foldRegion
(x, searchY, width, height, env, state, platformTree)
in
(eVals, q)
end