diff --git a/preview/app.js b/preview/app.js index acd79f4..b1bd53a 100644 --- a/preview/app.js +++ b/preview/app.js @@ -2,6 +2,7 @@ import { basicsView } from "./basics.js"; import { mountNavigation } from "./navigation.js"; import { pictogram, shipLocator, specializationIcons } from "./icons.js"; import { createTacticalView } from "./tactical.js"; +import { forecastMovement, checkpointAppearance } from "./threat-track.js"; import { illustration } from "./art.js"; import { specialDescription, @@ -688,19 +689,24 @@ export function mountConsole(root, host = null) { function trackHtml(th, g) { const track = th.track ?? g.tracks[th.zone ?? "internal"]; const pending = ["pending", "announced", "waiting"].includes(th.status); - return `
${pending ? t("À l’arrivée", "On arrival") : t("Approche", "Approach")}
${Array.from( + const context = (replayFrame !== null ? g.resolution?.frames[replayFrame] : g.resolution) ?? {}; + const groups = forecastMovement(th, track, { ...context, threats: context.threats ?? g.threats.map(item => ({ ...item, ...item.card })) }); + const projection = t("Prévision sans intervention de l’équipage ; changements de vitesse propres à la menace inclus.", "Forecast without crew intervention; includes the threat’s own speed changes."); + return `
${Array.from( { length: track.length }, (_, i) => track.length - i, ) .map(n => { const mark = track.marks.find(([position]) => position === n)?.[1]; const effects = th.effects?.[mark] ?? []; - const attack = effects.length === 1 && effects[0].type === "attack" && typeof effects[0].amount === "number" ? effects[0] : null; - const description = mark ? `${mark} · ${effects.map(effect => effectDescription(effect, locale)).join(" · ") || t("Aucun effet", "No effect")}` : String(n); + const appearance = checkpointAppearance(th, effects); + const groupIndex = groups.findIndex(group => group.positions.includes(n)); + const group = groups[groupIndex]; + const description = `${mark ? effects.map(effect => effectDescription(effect, locale)).join(" · ") || t("Aucun effet", "No effect") : t("Case", "Space") + " " + n}${group ? ` · T+${group.turn} · ${projection}` : ""}`; const current = !pending && th.positionOnTrack === n; - return `${attack ? `${pictogram("laser")}${attack.amount}` : mark ?? (current ? "●" : "")}`; + return `${appearance ? `${pictogram(appearance.icon)}${esc(appearance.value)}` : current ? "●" : ""}`; }) - .join("")}
${pictogram("ship")}
`; + .join("")}
${groups.length ? `
T+${groups.map((group, i) => `${group.turn}`).join("")}
` : ""}
${pictogram("ship")}
`; } function logText(e) { return eventDescription( diff --git a/preview/icons.js b/preview/icons.js index 972aecf..5b4e541 100644 --- a/preview/icons.js +++ b/preview/icons.js @@ -1,5 +1,6 @@ // Shared silhouettes keep the ship, cards and controls visually consistent. const paths = { + impact: "m12 2 2 6 6-4-3 7 5 3-7 1 1 7-5-5-5 4 1-7-5-3 7-1 3-8Z", wait: "M6 3h12M6 21h12M7 3v4l5 5-5 5v4m10-18v4l-5 5 5 5v4", clock: "M2 12a10 10 0 1 0 20 0 10 10 0 1 0-20 0Zm10-6v6l4 2", pause: "M7 4h3v16H7zM14 4h3v16h-3z", diff --git a/preview/style.css b/preview/style.css index 49c9f5b..2275f24 100644 --- a/preview/style.css +++ b/preview/style.css @@ -2537,7 +2537,23 @@ input:not([type="checkbox"]):not([type="range"]) { box-shadow: 0 0 0 4px #ffe19a18; } #program .slot.current .turn { color: #ffe19a; font-weight: 700; } -.approach-track .track:has(.attack-mark) > span { height: 30px; line-height: 28px; } -.approach-track .track .attack-mark { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; line-height: 1; } -.attack-mark .pictogram { width: 11px; height: 11px; } -.attack-mark b { font-size: 10px; line-height: 10px; } +.track-distance { flex: 1; min-width: 0; overflow-x: auto; padding: 2px; } +.approach-track .track, .track-turns { + display: grid; + grid-template-columns: repeat(var(--track-length), minmax(14px, 1fr)); + gap: 2px; +} +.approach-track .track > span { height: 32px; line-height: 30px; } +.approach-track .track .effect-mark { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; line-height: 1; } +.effect-mark .pictogram { width: 12px; height: 12px; } +.effect-mark b { font-size: 10px; line-height: 10px; } +.approach-track .track .forecast-tone-0 { background: #264d55; } +.approach-track .track .forecast-tone-1 { background: #30405c; } +.approach-track .track .forecast-start { border-left: 1px solid #91b7bb; } +.approach-track .track .forecast-end { border-right: 1px solid #91b7bb; } +.approach-track .track .current { background: #ffe19a; color: #13202c; } +.track-turns { margin-top: 4px; align-items: center; text-align: center; font-size: 10px; } +.track-turns > small { grid-column: 1; color: var(--muted); font-size: 8px; } +.track-turns > span { padding: 2px 0; border-radius: 2px; border-bottom: 2px solid; } +.track-turns .forecast-tone-0 { color: #acf0e5; border-color: #72c9bb; background: #264d5580; } +.track-turns .forecast-tone-1 { color: #c0d5fa; border-color: #87a9e1; background: #30405c80; } diff --git a/preview/threat-track.js b/preview/threat-track.js new file mode 100644 index 0000000..8ccb84b --- /dev/null +++ b/preview/threat-track.js @@ -0,0 +1,73 @@ +// Display-only forecast from public threat state. No planned crew actions are read. +// Preserve the real distance spaces: each group is one upcoming movement step. +export function forecastMovement(threat, track, context = {}) { + const pending = !threat.status || ["pending", "announced", "waiting"].includes(threat.status); + if (!pending && threat.status !== "active") return []; + if (!Number.isFinite(threat.speed) || threat.speed <= 0) return []; + // Another threat can move this token outside its own movement step. + if (context.threats?.some(other => other.instance !== threat.instance && + !["destroyed", "survived", "discarded"].includes(other.status) && + Object.values(other.effects ?? {}).flat().some(effect => + (effect.type === "advance-others" && (effect.position ?? "external") === threat.position) || + (effect.type === "advance-zone" && effect.zone === threat.zone)))) return []; + + const nextTurn = (context.turn ?? 0) + (['crew', 'damage', 'threats'].includes(context.step) ? 0 : 1); + let turn = Math.max(threat.turn ?? 1, nextTurn); + const t = structuredClone(threat); + t.flags ??= []; + t.damage ??= 0; + let position = pending ? track.length : t.positionOnTrack; + const groups = []; + for (; position > 1 && groups.length < 24; turn++) { + // Changes crossed this turn affect the NEXT movement, just as in the resolver. + const speed = Math.max(0, t.speed); + if (!speed) break; + const end = Math.max(1, position - speed); + const group = { turn, speed, positions: [] }; + let stop = false; + for (let at = position - 1; at >= end; at--) { + group.positions.push(at); + const mark = track.marks.find(([n]) => n === at)?.[1]; + for (const effect of t.effects?.[mark] ?? []) { + if ((effect.onlyPhasedIn && t.phasedOut) || + (effect.ifDamaged && !t.damage) || (effect.ifUndamaged && t.damage) || + (effect.belowDamage !== undefined && t.damage >= effect.belowDamage) || + (effect.unlessFlag && t.flags.includes(effect.unlessFlag))) continue; + if (effect.ifCrew || ["jump", "absorb-reactor"].includes(effect.type)) stop = true; + else if (effect.type === "change") t[effect.stat] = (t[effect.stat] ?? 0) + effect.amount; + else if (effect.type === "set") t[effect.stat] = effect.amount; + else if (effect.type === "flag") t.flags.push(effect.name); + else if (effect.type === "heal") t.damage = Math.max(0, t.damage - (effect.amount === "half" ? Math.floor(t.damage / 2) : effect.amount)); + else if (effect.type === "self-damage") { + t.damage += effect.amount; + if (t.damage >= t.hp) stop = true; + } else if (effect.type === "destroy-ship") stop = true; + } + if (mark === "Z") stop = true; + // A jump or conditional effect makes the remainder uncertain. Do not + // label spaces beyond that checkpoint with a promised arrival turn. + if (stop) break; + } + groups.push(group); + if (stop) break; + position = end; + if (t.phasing) t.phasedOut = !t.phasedOut; + t.speed += t.accelerates ?? 0; + } + return groups; +} + +export function checkpointAppearance(threat, effects) { + if (!effects.length) return null; + if (effects.length > 1) return { icon: "systems", value: "+" }; + const effect = effects[0]; + if (effect.type === "attack") { + const hpMultiplier = { hp: 1, "double-hp": 2, "triple-hp": 3 }[effect.amount]; + if (hpMultiplier) return { icon: "impact", value: hpMultiplier * Math.max(0, threat.hp - (threat.damage ?? 0)) }; + return { icon: "laser", value: typeof effect.amount === "number" ? effect.amount : "?" }; + } + if (effect.type === "heal") return { icon: "medic", value: typeof effect.amount === "number" ? effect.amount : "½" }; + if (["change", "set"].includes(effect.type) && ["speed", "shield"].includes(effect.stat)) + return { icon: effect.stat, value: `${effect.type === "change" && effect.amount > 0 ? "+" : ""}${effect.amount}` }; + return { icon: "systems", value: "" }; +} diff --git a/test/threat-track.test.js b/test/threat-track.test.js new file mode 100644 index 0000000..881040b --- /dev/null +++ b/test/threat-track.test.js @@ -0,0 +1,71 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { forecastMovement, checkpointAppearance } from "../preview/threat-track.js"; +import { THREATS, TRACKS } from "../src/game/catalog.js"; +import { beginResolution, stepResolution } from "../src/game/resolution.js"; + +function scenario(id) { + return beginResolution({ crewSize: 4, seed: "track-forecast", programs: Array.from({ length: 4 }, () => Array(12).fill(null)), + threats: [{ cardId: id, turn: 2, zone: "white" }], + tracks: Object.fromEntries(["red", "white", "blue", "internal"].map(zone => [zone, TRACKS[6]])) }); +} + +test("distance groups match actual constant speed, checkpoint acceleration, and per-turn acceleration", () => { + for (const id of ["E1-07", "E1-10", "E2-01", "SE2-04", "SE3-107", "E3-109"]) { + let r = scenario(id); + const original = structuredClone(r); + const groups = forecastMovement(r.threats[0], TRACKS[6]); + assert.deepEqual(r, original, "forecast leaves game state untouched"); + for (const group of groups) { + while (!r.outcome && !(r.turn === group.turn && r.step === "threats")) r = stepResolution(r); + if (r.outcome) break; + const start = r.threats[0].positionOnTrack; + r = stepResolution(r); + assert.equal(group.positions[0], start - 1, id); + assert.equal(group.positions.at(-1), r.threats[0].positionOnTrack, `${id} at turn ${group.turn}`); + } + } +}); + +test("natural slowdown applies on the next turn, even when multiple checkpoints are crossed", () => { + let r = scenario("E1-07"); + r.threats[0].effects.X = [{ type: "change", stat: "speed", amount: -2 }]; + const groups = forecastMovement(r.threats[0], TRACKS[6]); + assert.deepEqual(groups.slice(0, 4).map(g => g.positions), [[15,14,13], [12,11,10], [9], [8]]); + for (const group of groups.slice(0, 5)) { + while (!(r.turn === group.turn && r.step === "threats")) r = stepResolution(r); + r = stepResolution(r); + assert.equal(group.positions.at(-1), r.threats[0].positionOnTrack); + } +}); + +test("forecast rebases on the displayed resolution frame without replaying the current movement", () => { + let r = scenario("E2-01"); + while (!(r.turn === 3 && r.step === "threats")) r = stepResolution(r); + assert.equal(forecastMovement(r.threats[0], TRACKS[6], r)[0].turn, 3); + r = stepResolution(r); + const group = forecastMovement(r.threats[0], TRACKS[6], r)[0]; + assert.equal(group.turn, 4); + assert.equal(group.positions[0], r.threats[0].positionOnTrack - 1); + assert.equal(forecastMovement({ ...r.threats[0], status: "destroyed" }, TRACKS[6], r).length, 0); +}); + +test("crew-dependent slowdown is not assumed and uncertain trajectory changes stop the forecast", () => { + const snake = { ...structuredClone(THREATS.find(t => t.id === "E3-109")), turn: 1 }; + assert.equal(forecastMovement(snake, TRACKS[6])[0].positions.length, 4); + snake.effects.X = [{ type: "jump", direction: "left" }]; + const groups = forecastMovement(snake, TRACKS[6]); + assert.equal(groups.length, 1); + assert.equal(groups[0].positions.at(-1), 12); + const other = { instance: 8, effects: { X: [{ type: "advance-others" }] } }; + assert.deepEqual(forecastMovement(snake, TRACKS[6], { threats: [other] }), []); +}); + +test("meteoroid empty checkpoints have no symbols and impact follows remaining health", () => { + const meteor = structuredClone(THREATS.find(t => t.id === "E1-10")); + assert.equal(checkpointAppearance(meteor, meteor.effects.X), null); + assert.equal(checkpointAppearance(meteor, meteor.effects.Y), null); + assert.deepEqual(checkpointAppearance(meteor, meteor.effects.Z), { icon: "impact", value: 5 }); + meteor.damage = 3; + assert.deepEqual(checkpointAppearance(meteor, meteor.effects.Z), { icon: "impact", value: 2 }); +});