explodorama / files / src / ui / routePreview.ts
routePreview.ts
| 1 | import { isDeadly } from '../sim/danger' |
| 2 | import { autoMoveAction, autoRoute, playerPath } from '../sim/path' |
| 3 | import type { GameState, Vec } from '../sim/types' |
| 4 | |
| 5 | /** |
| 6 | * What the board shows for auto-move's way: the tiles it lands on (the line of arrows runs |
| 7 | * through them) and whether the first one is drawn red (the first move is not safe, or would |
| 8 | * cost a life). `routePreview` works it out for a destination that is only being looked at (the |
| 9 | * pad's or keyboard's cursor, the mouse's hover; decisions.md 2026-11-02), by the same rules |
| 10 | * as the real display in main.ts, without setting the way or taking a step. |
| 11 | */ |
| 12 | export interface RouteView { |
| 13 | way: Vec[] |
| 14 | blocked: boolean |
| 15 | } |
| 16 | |
| 17 | /** The way auto-move would show for `to` as it is now, or null when there is no way there. */ |
| 18 | export function routePreview(s: GameState, to: Vec, safe: (p: Vec) => boolean): RouteView | null { |
| 19 | const path = playerPath(s, to) |
| 20 | if (!path) return null |
| 21 | const next = autoMoveAction(s, to, safe) |
| 22 | const deadly = !!next && isDeadly(s, next) |
| 23 | // A deadly next move shows as a blocked one: the way is the plain path, its first tile red. |
| 24 | const route = deadly ? [] : autoRoute(s, to, safe) |
| 25 | const pathBlocked = !safe(path[0]!) || deadly |
| 26 | return route.length ? { way: route, blocked: false } : { way: path, blocked: pathBlocked } |
| 27 | } |