mirror of
https://github.com/db9979/uconsole_asw_game.git
synced 2026-12-06 21:04:18 +01:00
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tk5v1FfvkLKPYqPVE98DaY
250 lines
19 KiB
JavaScript
250 lines
19 KiB
JavaScript
import { S } from "../state/store.js";
|
|
import { $, damageStates, isBoatCommand, isSonar, opforRoles } from "../core/base.js";
|
|
import { enumText, finite, number, stateText, t, unit } from "../core/format.js";
|
|
import { palette, paletteAlpha } from "../core/palette.js";
|
|
import { syncPlotAnimation } from "../plot/clock.js";
|
|
import { heatmap } from "../plot/heatmap.js";
|
|
import { spectrum } from "../plot/spectrum.js";
|
|
import { elokaShortLabel, groupedEloka, mapRoles, overviewPlotList, ultraWide, wideScreen } from "../state/shared.js";
|
|
import { drawSonarVisuals } from "../stations/sonar-visuals.js";
|
|
import { clearVisuals, node } from "./dom.js";
|
|
import { drawRoleMap, syncOpzSweepAnimation } from "./role-map.js";
|
|
import { drawEmpty, roseFace, visualContext } from "./visual-common.js";
|
|
import { schedule } from "../core/scheduler.js";
|
|
import { drawRadioVisual } from "../stations/radio.js";
|
|
import { drawHelicopterDip } from "../stations/helicopter.js";
|
|
import { drawUbootEngineDials, renderUbootEngineConsole } from "../stations/uboot-engine-room.js";
|
|
import { drawEngineDials, renderEngineConsole } from "../stations/engine-room.js";
|
|
import { drawShipPlan, renderDamageLamps } from "../stations/damage-plan.js";
|
|
|
|
function drawDamageVisual() {
|
|
const plot = visualContext("damage-schematic"), payload = S.v2State.damage;
|
|
S.damageHits = [];
|
|
renderDamageLamps(payload);
|
|
if (!plot) return;
|
|
S.damageHits = drawShipPlan(plot, payload, Number($("damage-team").value));
|
|
$("damage-schematic-text").replaceChildren(...payload.compartments.map((room) => node("p", t("damage_compartment_equivalent", {name: room.name, flood: number(room.flood, 0), fire: number(room.fire, 0), flood_trend: number(room.trend.flood_rate, 2), fire_trend: number(room.trend.fire_rate, 2), teams: payload.teams.filter((team) => team.compartment === room.key).map((team) => team.team).join(", ") || t("station_none")}))));
|
|
if (!payload.compartments.length) $("damage-schematic-text").textContent = t("visual_empty");
|
|
}
|
|
function drawElokaVisual() {
|
|
const plot = visualContext("eloka-scope"), payload = S.v2State.eloka;
|
|
if (!plot) return;
|
|
const intercepts = groupedEloka(payload.intercepts, S.selected);
|
|
const simNow = finite(S.v2State.clock?.sim) ? S.v2State.clock.sim : 0;
|
|
const scopeWidth = plot.width * .43;
|
|
const radius = Math.min(scopeWidth, plot.height) * .38, cx = scopeWidth / 2, cy = plot.height / 2;
|
|
roseFace(plot.context, cx, cy, radius);
|
|
const focused = intercepts.find((row) => row.ref === S.selected) ?? intercepts[0];
|
|
// One strobe per listed emitter: fresh ones full length, older ones
|
|
// shorter and fainter; a group is drawn a little bolder, the selected one
|
|
// in the text colour on top with its name at the rim.
|
|
for (const row of [...intercepts].sort((a, b) => Number(a === focused) - Number(b === focused))) {
|
|
const angle = row.bearing * Math.PI / 180, chosen = row === focused;
|
|
const fresh = Math.max(0, 1 - row.age_s / 300), length = radius * (.45 + .55 * fresh);
|
|
plot.context.save();
|
|
plot.context.globalAlpha = chosen ? 1 : .25 + .65 * fresh * row.quality;
|
|
plot.context.strokeStyle = chosen ? palette().text : palette().amber;
|
|
plot.context.lineWidth = chosen ? 3 : row.members.length > 1 ? 2.5 : 1.5;
|
|
if (row.signal_state !== "LIVE" && !chosen) plot.context.setLineDash([5, 5]);
|
|
plot.context.beginPath(); plot.context.moveTo(cx, cy);
|
|
plot.context.lineTo(cx + Math.sin(angle) * length, cy - Math.cos(angle) * length); plot.context.stroke();
|
|
if (chosen) {
|
|
plot.context.fillStyle = palette().text; plot.context.textAlign = Math.sin(angle) < 0 ? "right" : "left";
|
|
plot.context.fillText(elokaShortLabel(row.label) + (row.members.length > 1 ? ` \u00d7${row.members.length}` : ""),
|
|
cx + Math.sin(angle) * (length + 8), cy - Math.cos(angle) * (length + 8));
|
|
}
|
|
plot.context.restore();
|
|
}
|
|
const gx = scopeWidth + 12, gy = 16, gw = plot.width - gx - 12, gh = plot.height - 32;
|
|
plot.context.strokeStyle = palette().line; plot.context.lineWidth = 1; plot.context.strokeRect(gx, gy, gw, gh);
|
|
plot.context.beginPath(); plot.context.moveTo(gx, gy + gh * .52); plot.context.lineTo(gx + gw, gy + gh * .52); plot.context.stroke();
|
|
if (focused && gw > 40 && gh > 60) {
|
|
const motionNow = Math.min(simNow, simNow - focused.age_s + 2);
|
|
const rate = .35 + Math.min(2.4, Math.log10(Math.max(1, focused.prf_hz || 500)) * .38);
|
|
const techniqueRate = focused.jamming_technique === "vgpo" ? 1 + (focused.jamming_effectiveness || 0) * 1.8 : 1;
|
|
const phase = (((Math.round(focused.frequency_hz / 1e6) + Math.round(focused.prf_hz || 500)) % 360) * Math.PI / 180) + motionNow * rate * techniqueRate;
|
|
const pulses = Math.max(3, Math.min(12, Math.round(2 + Math.log10(Math.max(1, focused.prf_hz || 500)) * 2)));
|
|
const hop = focused.modulation === "frequency_agile" ? ((Math.floor(motionNow * 2) % 7) - 3) * .025 : 0;
|
|
const centers = (focused.modulation === "frequency_agile" ? [.28, .5, .72] : [.5]).map((value) => value + hop);
|
|
const width = focused.modulation === "continuous_wave" ? .035 : ["pulse_doppler", "frequency_agile"].includes(focused.modulation) ? .09 : .14;
|
|
plot.context.strokeStyle = palette().amber; plot.context.lineWidth = 2; plot.context.beginPath();
|
|
for (let i = 0; i < 40; i++) { const x = i / 39; let value = Math.max(...centers.map((center) => Math.exp(-Math.pow((x - center) / width, 2)))); if (focused.jamming_technique === "noise") value = Math.max(value, (.28 + .08 * Math.sin(i * .71 + motionNow * 5)) * (focused.jamming_effectiveness || 0)); if (focused.jamming_technique === "false_targets") value = Math.max(value, ...centers.map((center) => .55 * (focused.jamming_effectiveness || 0) * Math.exp(-Math.pow((x - center - .14) / width, 2)))); const px = gx + 6 + x * (gw - 12), py = gy + gh * .47 - value * focused.quality * gh * .34; if (i) plot.context.lineTo(px, py); else plot.context.moveTo(px, py); }
|
|
plot.context.stroke(); plot.context.strokeStyle = palette().accent; plot.context.beginPath();
|
|
for (let i = 0; i < 48; i++) { const x = i / 47; const movingX = (x + motionNow * rate * .08 + (focused.jamming_technique === "rgpo" ? motionNow * (focused.jamming_effectiveness || 0) * .03 : 0)) % 1; let value;
|
|
if (focused.modulation === "continuous_wave") value = Math.sin(Math.PI * 10 * x + phase);
|
|
else if (focused.modulation === "frequency_agile") { const steps = [-.75, .15, .7, -.25, .45, -.55, .85, 0]; value = steps[(Math.min(7, Math.floor(movingX * 8)) + Math.floor(motionNow * 2)) % 8]; }
|
|
else if (["pulse", "pulse_doppler"].includes(focused.modulation)) { const envelope = Math.max(0, 1 - ((movingX * pulses) % 1) / .16); value = envelope * 2 - .85; if (focused.modulation === "pulse_doppler") value *= .65 + .35 * Math.sin(Math.PI * 3 * pulses * movingX + phase); }
|
|
else value = .55 * Math.sin(Math.PI * 6 * x + phase) + .25 * Math.sin(Math.PI * 22 * x + phase * .37);
|
|
if (focused.jamming_technique === "noise") value = value * (1 - .45 * (focused.jamming_effectiveness || 0)) + Math.sin(i * 2.17 + motionNow * 11) * .55 * (focused.jamming_effectiveness || 0);
|
|
if (focused.jamming_technique === "false_targets") value += Math.sin(Math.PI * 6 * ((movingX + .14) % 1) + phase) * .35 * (focused.jamming_effectiveness || 0);
|
|
value *= focused.quality;
|
|
const px = gx + 6 + x * (gw - 12), py = gy + gh * .76 - value * gh * .17; if (i) plot.context.lineTo(px, py); else plot.context.moveTo(px, py); }
|
|
plot.context.stroke(); plot.context.fillStyle = palette().text; plot.context.textAlign = "left";
|
|
plot.context.fillText(`${elokaShortLabel(focused.label)} / ${t(`eloka_modulation_${focused.modulation}`)}`, gx + 8, gy + 17, gw - 16);
|
|
plot.context.fillStyle = focused.signal_state === "LIVE" ? palette().accent : palette().amber; plot.context.textAlign = "right";
|
|
const signalLabel = focused.signal_state === "MEMORY" ? t("eloka_signal_memory", {age: number(focused.age_s, 0)}) : t(`eloka_signal_${focused.signal_state.toLowerCase()}`);
|
|
plot.context.fillText(signalLabel, gx + gw - 8, gy + 17, gw / 2);
|
|
plot.context.fillStyle = palette().text; plot.context.textAlign = "left";
|
|
plot.context.fillText(`${number(focused.frequency_hz / 1e9, 3)} GHz / ${number(focused.prf_hz, 0)} Hz`, gx + 8, gy + gh - 7, gw - 16);
|
|
}
|
|
if (!intercepts.length) drawEmpty(plot);
|
|
const equivalents = intercepts.map((row) => node("p", t("eloka_equivalent", {ref: elokaShortLabel(row.label), bearing: number(row.bearing, 0), frequency: number(row.frequency_hz, 0), prf: number(row.prf_hz, 0), modulation: t(`eloka_modulation_${row.modulation}`), candidates: row.candidates.map((item) => item.name).join(", ") || t("station_none"), correlations: row.correlations.map((item) => item.ref).join(", ") || t("station_none")})));
|
|
if (focused) equivalents.push(node("p", t("eloka_signal_equivalent", {ref: elokaShortLabel(focused.label), modulation: t(`eloka_modulation_${focused.modulation}`), frequency: number(focused.frequency_hz / 1e9, 3), prf: number(focused.prf_hz, 0)})));
|
|
$("eloka-scope-text").replaceChildren(...equivalents);
|
|
if (!intercepts.length) $("eloka-scope-text").textContent = t("visual_empty");
|
|
}
|
|
// Annunciator lamp on the canvas: an LED, a label and a value in a framed tile.
|
|
function canvasLamp(g, x, y, w, h, label, value, color) {
|
|
g.fillStyle = paletteAlpha("raised", .92); g.fillRect(x, y, w, h);
|
|
g.strokeStyle = color; g.globalAlpha = .75; g.strokeRect(x + .5, y + .5, w - 1, h - 1); g.globalAlpha = 1;
|
|
const r = Math.min(7, h / 4), cx = x + 12 + r, cy = y + h / 2;
|
|
g.fillStyle = color; g.shadowColor = color; g.shadowBlur = 8;
|
|
g.beginPath(); g.arc(cx, cy, r, 0, Math.PI * 2); g.fill(); g.shadowBlur = 0;
|
|
g.textAlign = "left"; g.fillStyle = palette().muted; g.fillText(label, cx + r + 8, cy - 4, w / 2);
|
|
g.textAlign = "right"; g.fillStyle = palette().text; g.fillText(value, x + w - 10, cy + 12, w - 2 * r - 30);
|
|
}
|
|
function drawWeaponsVisual() {
|
|
const plot = visualContext("weapons-system"), payload = S.v2State.weapons;
|
|
if (!plot) return;
|
|
const g = plot.context, p = palette(), readiness = payload.readiness, w = plot.width;
|
|
const down = readiness.station_down, blocked = Boolean(readiness.interlock);
|
|
const stages = [
|
|
[t("weapons_lamp_station"), down ? t("station_down_state") : t("station_live_state"), down ? p.red : p.accent],
|
|
[t("weapons_lamp_roe"), stateText("roe", readiness.roe), p.accent],
|
|
[t("weapons_lamp_interlock"), blocked ? readiness.interlock : readiness.state, blocked ? p.amber : p.accent]];
|
|
const lampW = (w - 20 - 2 * 10) / 3;
|
|
stages.forEach(([label, value, color], index) => canvasLamp(g, 10 + index * (lampW + 10), 12, lampW, 46, label, value, color));
|
|
// Tubes as cards in a row (numbered from 1 as on the uConsole): a lamp, the
|
|
// state and the reload as a bar along the bottom edge.
|
|
const tubes = payload.tubes, top = 72, cardH = 58, gap = 10;
|
|
const cardW = Math.min(220, (w - 20 - gap * Math.max(0, tubes.length - 1)) / Math.max(1, tubes.length));
|
|
tubes.forEach((tube, index) => {
|
|
const x = 10 + index * (cardW + gap), ready = tube.state === "ready", reloading = tube.state === "reloading";
|
|
const color = ready ? p.accent : reloading ? p.amber : p.muted;
|
|
g.fillStyle = paletteAlpha("raised", .92); g.fillRect(x, top, cardW, cardH);
|
|
g.strokeStyle = p.line; g.strokeRect(x + .5, top + .5, cardW - 1, cardH - 1);
|
|
g.fillStyle = color; g.fillRect(x, top, 4, cardH);
|
|
g.shadowColor = color; g.shadowBlur = 8; g.beginPath(); g.arc(x + 20, top + 19, 5, 0, Math.PI * 2); g.fill(); g.shadowBlur = 0;
|
|
g.textAlign = "left"; g.fillStyle = p.text;
|
|
g.fillText(`${t("weapons_tube")} ${number(tube.tube + 1, 0)}`, x + 32, top + 23, cardW - 40);
|
|
g.fillStyle = color;
|
|
const state = reloading && finite(tube.reload_s) ? `${stateText("tube_state", tube.state)} ${unit(tube.reload_s, "s", 0)}` : stateText("tube_state", tube.state);
|
|
g.fillText(state, x + 32, top + 41, cardW - 40);
|
|
const fill = ready ? 1 : reloading ? .35 : 0;
|
|
g.fillStyle = p.line; g.fillRect(x + 10, top + cardH - 8, cardW - 20, 3);
|
|
g.fillStyle = color; g.fillRect(x + 10, top + cardH - 8, (cardW - 20) * fill, 3);
|
|
});
|
|
// The firing chain in the order the fire control checks it; the step that
|
|
// holds the shot is marked and the line below says what is missing.
|
|
const chain = ["target", "release", "solution", "tube", "fire"];
|
|
const held = readiness.stage === "station" ? chain.length - 1 : chain.indexOf(readiness.stage);
|
|
const chainTop = top + cardH + 22, stepW = (w - 20) / chain.length;
|
|
chain.forEach((stage, index) => {
|
|
const x = 10 + index * stepW, done = index < held || readiness.stage === "fire", holding = index === held && readiness.stage !== "fire";
|
|
const color = done ? p.accent : holding ? p.amber : p.muted;
|
|
g.strokeStyle = color; g.lineWidth = holding ? 2 : 1;
|
|
g.strokeRect(x + .5, chainTop + .5, stepW - 22, 30); g.lineWidth = 1;
|
|
g.fillStyle = color; g.textAlign = "center";
|
|
g.fillText(`${done ? "\u2713" : holding ? "!" : "\u25CB"} ${t(`weapons_chain_${stage}`)}`, x + (stepW - 22) / 2, chainTop + 20, stepW - 30);
|
|
if (index < chain.length - 1) { g.fillStyle = p.muted; g.fillText("\u203A", x + stepW - 11, chainTop + 20); }
|
|
});
|
|
g.textAlign = "left";
|
|
g.fillStyle = readiness.stage === "fire" ? p.accent : p.amber;
|
|
g.fillText(readiness.stage === "fire" ? t("weapons_chain_ready") : readiness.interlock, 12, chainTop + 52, w - 24);
|
|
g.fillStyle = p.muted;
|
|
g.fillText(t(`weapons_chain_hint_${readiness.stage}`), 12, chainTop + 72, w - 24);
|
|
$("weapons-system-text").textContent = t("weapons_equivalent", {state: readiness.state, interlock: readiness.interlock, tubes: tubes.map((tube) => `${tube.tube + 1}:${tube.state}/${number(tube.reload_s, 0)}s`).join(", ") || t("station_none"), nixies: number(payload.inventory.nixies, 0), active: payload.active_assets.length});
|
|
}
|
|
function visualStationDown(role) {
|
|
const payload = S.v2State?.[role];
|
|
return isSonar(role) ? payload.settings.station_down : isBoatCommand(role) ? ["sinking", "sunk"].includes(payload.status.state) :
|
|
role === "weapons" ? payload.readiness.station_down :
|
|
role === "opz" ? !payload.radar.live : role === "radio" ? payload.station_down :
|
|
role === "engine" ? payload.machinery.station_state === "ZERSTOERT" : role === "eloka" ? payload.station_down : false;
|
|
}
|
|
function drawHelicopterAcoustic() {
|
|
const acoustic = S.v2State?.helicopter?.acoustic;
|
|
if (!acoustic || $("helicopter-buoy-console").hidden) return;
|
|
const aged = (rows) => rows.map((bins, index) => ({bins, age_s: (rows.length - 1 - index) * .25}));
|
|
// The buoy relay keeps a fixed number of 0.25 s rows; that span is its time axis.
|
|
const span = (rows) => Math.max(20, rows.length * .25);
|
|
heatmap("helicopter-broadband-canvas", aged(acoustic.broadband_history), null, null, (broadband) => {
|
|
if (!finite(acoustic.listen_bearing)) return;
|
|
broadband.context.strokeStyle = palette().amber;
|
|
const x = acoustic.listen_bearing / 360 * broadband.width;
|
|
broadband.context.beginPath(); broadband.context.moveTo(x, 0);
|
|
broadband.context.lineTo(x, broadband.height); broadband.context.stroke();
|
|
}, span(acoustic.broadband_history));
|
|
spectrum("helicopter-spectrum-canvas", acoustic.spectrum,
|
|
[], acoustic.bin_frequencies_hz, 300);
|
|
heatmap("helicopter-lofar-canvas", aged(acoustic.history), acoustic.bin_frequencies_hz, null, null, span(acoustic.history));
|
|
spectrum("helicopter-demon-canvas", acoustic.demon,
|
|
[], acoustic.demon.map((_, index) => index + 1), 80);
|
|
}
|
|
function drawRoleVisuals() {
|
|
const role = S.v2State?.role;
|
|
if (!role || $("role-visuals").hidden) return;
|
|
if (mapRoles.has(role) && $("map-visual").hidden === false) drawRoleMap(role);
|
|
if (role === "helicopter") { drawHelicopterAcoustic(); drawHelicopterDip(S.v2State.helicopter?.dip_observations ?? []); }
|
|
if (isSonar(role)) drawSonarVisuals();
|
|
if (role === "damage") drawDamageVisual();
|
|
if (role === "engine") { renderEngineConsole(S.v2State.engine); drawEngineDials(); }
|
|
if (role === "eloka") drawElokaVisual();
|
|
if (role === "weapons") drawWeaponsVisual();
|
|
if (role === "radio") drawRadioVisual();
|
|
if (role === "uboot_engine") { renderUbootEngineConsole(S.v2State.uboot_engine); drawUbootEngineDials(); }
|
|
}
|
|
export function queueVisualDraw() {
|
|
schedule("role-visuals", () => { drawRoleVisuals(); syncPlotAnimation(); });
|
|
}
|
|
// The helicopter's pages on the stage, in the order Page Down steps through
|
|
// them: acoustic analysis, dipping sonar picture, tactical map.
|
|
export const HELICOPTER_PAGES = ["acoustic", "dip", "map"];
|
|
export function showHelicopterPage(mode) {
|
|
if (!HELICOPTER_PAGES.includes(mode)) return;
|
|
S.helicopterVisualPage = mode;
|
|
renderRoleVisuals("helicopter");
|
|
}
|
|
export function stepHelicopterPage(step) {
|
|
const index = Math.max(0, HELICOPTER_PAGES.indexOf(S.helicopterVisualPage));
|
|
showHelicopterPage(HELICOPTER_PAGES[(index + step + HELICOPTER_PAGES.length) % HELICOPTER_PAGES.length]);
|
|
}
|
|
export function renderRoleVisuals(role) {
|
|
$("role-visuals").hidden = !role;
|
|
// Very wide screens show the helicopter's acoustic console and map side by side.
|
|
const split = role === "helicopter" && ultraWide.matches;
|
|
$("role-visuals").dataset.split = split ? "on" : "off";
|
|
$("helicopter-visual-tabs").hidden = role !== "helicopter" || split;
|
|
$("helicopter-buoy-console").hidden = role !== "helicopter" || (!split && S.helicopterVisualPage !== "acoustic");
|
|
$("helicopter-dip-display").hidden = role !== "helicopter" || split || S.helicopterVisualPage !== "dip";
|
|
for (const mode of HELICOPTER_PAGES) {
|
|
const tab = $(`helicopter-visual-${mode}`), on = S.helicopterVisualPage === mode;
|
|
tab.setAttribute("aria-selected", String(on)); tab.tabIndex = on ? 0 : -1;
|
|
}
|
|
for (const plot of ["broadband", "lofar", "demon"]) {
|
|
$(`helicopter-plot-${plot}`).hidden = S.helicopterPlot !== plot;
|
|
$(`helicopter-acoustic-plot-tabs`).querySelector(`[data-helicopter-plot-tab="${plot}"]`)
|
|
.setAttribute("aria-selected", String(S.helicopterPlot === plot));
|
|
}
|
|
for (const [id, active] of [["map-visual", mapRoles.has(role) && (role !== "helicopter" || split || S.helicopterVisualPage === "map")], ["sonar-visual", isSonar(role)],
|
|
["damage-visual", role === "damage"], ["engine-visual", role === "engine"],
|
|
["eloka-visual", role === "eloka"], ["weapons-visual", role === "weapons"], ["radio-visual", role === "radio"],
|
|
["uboot-engine-visual", role === "uboot_engine"]]) $(id).hidden = !active;
|
|
// Each side has its own grease-pencil plot: the submarine commander draws on
|
|
// the boat's, never the frigate's; the boat's sonar room has none.
|
|
$("plot-tools").hidden = opforRoles.has(role) && !["uboot", "uboot_nav"].includes(role);
|
|
if (!role) { clearVisuals(); return; }
|
|
const stateKey = !S.connected ? "visual_stale" : S.v2State.phase !== "live" ? "visual_inactive" : visualStationDown(role) ? "visual_station_down" : "visual_live";
|
|
$("role-visual-state").textContent = t(stateKey);
|
|
for (const button of $("sonar-page-tabs").querySelectorAll("button:not(.key-cap)")) {
|
|
const selectedPage = button.dataset.sonarVisual === S.sonarVisualPage;
|
|
button.setAttribute("aria-selected", String(selectedPage)); button.tabIndex = selectedPage ? 0 : -1;
|
|
}
|
|
const shownPlots = S.sonarVisualPage === "overview" ? overviewPlotList() : [S.sonarVisualPage];
|
|
$("sonar-plots").dataset.layout = S.sonarVisualPage !== "overview" ? "single" :
|
|
wideScreen.matches ? "overview-wide" : "overview";
|
|
for (const panel of document.querySelectorAll("[data-sonar-plot]")) panel.hidden = !shownPlots.includes(panel.dataset.sonarPlot);
|
|
queueVisualDraw();
|
|
syncOpzSweepAnimation();
|
|
}
|