feat(cockpit): ride the selected vessel from its bridge

With no aircraft tracked, Cockpit can ride the selected AIS vessel. Entry
needs only Live AIS Vessels and a selected vessel; C, double-click and the
entry buttons (reading BRIDGE with a ship icon) enter it, while voice stays
aircraft-only.

The eye sits on the AIS position at a representative bridge height for the
vessel's type family, looks along true heading and moves along course over
ground. The last fix is dead-reckoned for up to 180 s between the layer's
refreshes, and on rivers and lakes the waterline is read from the rendered
surface around the ship rather than the geoid.

The bridge HUD shows speed and course over ground, a straight heading
tape, window mullions, the AIS destination as a reported voyage and a
heading-up radar of nearby ships (3 to 24 NM). The arrow keys look around,
Home or 0 looks ahead again. The vessel layer hides the ridden ship's
sprite and card, ignores scene clicks while held, and returns the camera
to the vessel's map view on exit.
This commit is contained in:
Denis Groshev committed 2026-11-07 19:52:30 +01:00
1 parent 52187c91d1
commit 72112ecb45
58 files changed
+5313 -137

No files matched your search

+131 -33
View File
@@ -6,6 +6,12 @@ import {
resolveTrackedAircraftInfo,
} from '../cockpitMath.js';
/** The camera's own heading in degrees, or 0 when it has none. */
function cameraHeadingDeg(camera) {
const heading = camera?.heading;
return Number.isFinite(heading) ? (heading * 180) / Math.PI : 0;
}
export function readAircraftInfo() {
// In cockpit mode the controller takes the entity off `viewer.trackedEntity`
// (see update()), so the cockpit's own handle is the tracked identity there.
@@ -17,14 +23,20 @@ export function readAircraftInfo() {
});
}
/** The selected vessel the bridge view would ride, or rides; null without one. */
export function readVesselInfo() {
return this.services.aisLiveVesselsLayer?.getCockpitSubject?.() || null;
}
/**
* The layer the active view rides: the aircraft's own layer. It is read from
* the tracked entity's id once the layer has dropped its tracking, so a layer
* turned off still names itself.
* @returns {'flights'|'military'|null}
* The layer the active view rides: Live AIS Vessels on a bridge, else the
* aircraft's own layer. It is read from the tracked entity's id once the
* layer has dropped its tracking, so a layer turned off still names itself.
* @returns {'flights'|'military'|'ais-live-vessels'|null}
*/
export function readSubjectLayerId() {
if (!this.active) return null;
if (this.subjectKind === 'vessel') return 'ais-live-vessels';
const layerId = this.readAircraftInfo()?.layerId;
if (layerId) return layerId;
const [prefix] = String(this.trackedEntity?.gevTrackedId || '').split(':');
@@ -32,23 +44,26 @@ export function readSubjectLayerId() {
}
/**
* The subject entry would take: a Cesium-tracked aircraft.
* @returns {'aircraft'|null}
* The subject entry would take: a Cesium-tracked aircraft first, else the
* selected vessel. Vessels never take Cesium tracking, so an aircraft and a
* vessel cannot be live through the same owner.
* @returns {'aircraft'|'vessel'|null}
*/
export function resolveEntryKind() {
return this.readAircraftInfo() && this.viewer.trackedEntity?.position
? 'aircraft'
: null;
if (this.readAircraftInfo() && this.viewer.trackedEntity?.position)
return 'aircraft';
return this.readVesselInfo() ? 'vessel' : null;
}
export function dispatchCockpitModeChanged(active, info = null) {
const subjectId = active
? String(info?.icao24 || '')
? String(info?.icao24 || info?.mmsi || '')
.trim()
.toLowerCase() || null
: null;
const layerId =
active && ['flights', 'military'].includes(info?.layerId)
active &&
['flights', 'military', 'ais-live-vessels'].includes(info?.layerId)
? info.layerId
: null;
window.dispatchEvent(
@@ -103,17 +118,29 @@ export function syncEntry() {
const info = this.readAircraftInfo();
const trackedContact = !!(info && this.viewer.trackedEntity?.position);
this.syncTr3bToggle(trackedContact ? info : null);
const available = !!(this.isEntryAllowed() && trackedContact);
const bridgeContact = !trackedContact && !!this.readVesselInfo();
let entryKind = null;
if (trackedContact) entryKind = this.isEntryAllowed() ? 'aircraft' : null;
else if (bridgeContact)
entryKind = this.isBridgeEntryAllowed() ? 'vessel' : null;
const available = entryKind !== null;
// Change-only publishes: this runs on a preUpdate cadence, and a fresh
// presence every frame would re-render the views even when nothing
// changed. (perf item 9)
if (this.presence.entryAvailable === available) return;
if (
this.presence.entryAvailable === available &&
this.presence.entryKind === entryKind
)
return;
this.presence.entryAvailable = available;
this.presence.entryKind = entryKind;
this.publishPresenceView();
}
export function navigateContext(direction, options = {}) {
if (this.destroyed) return false;
// Contact navigation moves between aircraft; a bridge does not jump ship.
if (this.subjectKind === 'vessel') return false;
const method = direction < 0 ? 'navigatePrevious' : 'navigateNext';
const wasActive = this.active;
if (wasActive) this.contextNavigationDeadlineMs = performance.now() + 1500;
@@ -132,6 +159,9 @@ export function navigateContext(direction, options = {}) {
}
export function _adoptTrackedEntity(nowMs, suppliedInfo = null) {
// An aircraft tracked from the bridge ends it (see the vessel camera
// update); it is never adopted as the bridge's subject.
if (this.subjectKind === 'vessel') return false;
const nextEntity = this.viewer.trackedEntity;
if (
!this.active ||
@@ -160,13 +190,24 @@ export function _adoptTrackedEntity(nowMs, suppliedInfo = null) {
return true;
}
export function enter() {
/**
* Enter first person on a tracked aircraft or, with none tracked, the
* selected vessel's bridge.
* @param {{kind?: 'aircraft'|'vessel'|null}} [options] Restrict
* entry to one subject kind; voice entry is aircraft-only.
* @returns {boolean} True when the view was entered.
*/
export function enter({ kind = null } = {}) {
if (this.destroyed) return false;
if (this.active) return false;
if (!this.isEntryAllowed()) return false;
const info = this.readAircraftInfo();
const entity = this.viewer.trackedEntity;
if (!info || !entity?.position) return false;
const subjectKind = kind ?? this.resolveEntryKind() ?? 'aircraft';
const vessel = subjectKind === 'vessel';
const allowed = vessel ? this.isBridgeEntryAllowed() : this.isEntryAllowed();
if (!allowed) return false;
const info = vessel ? this.readVesselInfo() : this.readAircraftInfo();
// Only an aircraft is ridden through its Cesium entity.
const entity = vessel ? null : this.viewer.trackedEntity;
if (!info || (!vessel && !entity?.position)) return false;
this.entryFocusOrigin =
document.activeElement instanceof HTMLElement
? document.activeElement
@@ -174,10 +215,26 @@ export function enter() {
// Retire deferred navigation before cancelFlight can run its callbacks.
this.onCameraTakeover?.();
this.viewer.camera.cancelFlight();
this.subjectKind = subjectKind;
// Each session reads its own water: never a height left from the last.
this.vesselWaterline = null;
this.subjectId = vessel ? info.mmsi : null;
this.trackedEntity = entity;
this.trackedEntityWasShown = entity.show;
entity.show = false;
this.heading = normalizeHeading(info.track ?? 0);
if (entity) {
this.trackedEntityWasShown = entity.show;
entity.show = false;
} else {
this.services.aisLiveVesselsLayer?.setCockpitSubject?.(info.mmsi);
}
// A ship reporting neither heading nor course keeps the camera's own
// heading: no direction is made up for it.
this.heading = normalizeHeading(
Number.isFinite(info.track)
? info.track
: vessel
? cameraHeadingDeg(this.viewer.camera)
: 0,
);
this.lastFrameMs = performance.now();
this.lastCameraUpdateMs = 0;
this.lastHudUpdateMs = 0;
@@ -189,6 +246,8 @@ export function enter() {
this.surfaceFallback = false;
this.cockpitAnchorValid = false;
this.lastCameraUpdateMs = 0;
// Every bridge opens looking over the bow.
this.resetLook({ immediate: true });
this.active = true;
// Cockpit animates the camera from preUpdate every frame — preUpdate only
// runs on rendered frames, so idle mode would freeze the cockpit solid.
@@ -197,31 +256,58 @@ export function enter() {
this.viewer.trackedEntity = undefined;
this.viewer.scene.screenSpaceCameraController.enableInputs = false;
document.body.classList.add('cockpit-mode');
// Bridge styling reaches surfaces outside the HUD (exit row, side panels).
if (vessel) document.body.classList.add('cockpit-bridge');
// Activation hides the entry and TR-3B chip; clearing the chip's signature
// lets the exit-path syncEntry publish it again.
this._tr3bSignature = null;
Object.assign(this.presence, {
active: true,
entryAvailable: false,
entryKind: null,
subjectKind,
tr3b: null,
});
this.publishPresenceView();
// Only a bridge has a radar.
if (!vessel) this.publishRadar(null);
this.signalItems = [];
this.signalSignatures.clear();
this.showBriefPage(0);
this.startBriefRotation();
const trackLabel =
info.callsign || info.registration || info.icao24 || t('AIRCRAFT');
const trackHeading = formatHeadingReadout(info.track ?? 0);
this.pushCockpitSignal(
'track',
'track',
t('TRACK ACQUIRED'),
t('{label} · COURSE {heading}°', {
label: trackLabel,
heading: trackHeading,
}),
);
if (vessel) {
this.pushCockpitSignal(
'track',
'track',
t('TRACK ACQUIRED'),
// A ship with no true heading is held on its course over ground; one
// with neither is named without a direction.
info.trackSource === 'course'
? t('{label} · COURSE {heading}°', {
label: info.name,
heading: trackHeading,
})
: info.trackSource === 'heading'
? t('{label} · HEADING {heading}°', {
label: info.name,
heading: trackHeading,
})
: t('{label} · NO HEADING', { label: info.name }),
);
} else {
const trackLabel =
info.callsign || info.registration || info.icao24 || t('AIRCRAFT');
this.pushCockpitSignal(
'track',
'track',
t('TRACK ACQUIRED'),
t('{label} · COURSE {heading}°', {
label: trackLabel,
heading: trackHeading,
}),
);
}
this.updateHud(info, performance.now(), true);
this.setVisionMode(this.getInitialVisionMode());
this.scheduleContextLayout();
@@ -239,10 +325,15 @@ export function enter() {
export function exit({ restoreTracking = true } = {}) {
if (!this.active) return false;
const entity = this.trackedEntity;
const vesselSubject = this.subjectKind === 'vessel' ? this.subjectId : null;
this.active = false;
this.subjectKind = null;
this.vesselWaterline = null;
this.subjectId = null;
this.services.releaseContinuousRender('cockpit');
this.trackedEntity = null;
this.heading = null;
this.resetLook({ immediate: true });
this.cockpitAnchorValid = false;
this.surfaceWaitStartedMs = 0;
this.surfaceAcquiring = false;
@@ -255,22 +346,29 @@ export function exit({ restoreTracking = true } = {}) {
this.regionalBriefAbort = null;
this.regionalBriefRequestToken += 1;
this.regionalBriefSubjectId = null;
document.body.classList.remove('cockpit-mode');
document.body.classList.remove('cockpit-mode', 'cockpit-bridge');
this.onExited?.();
this.publishUtilityLayout(null);
this.presence.active = false;
this.presence.subjectKind = null;
this.publishPresenceView();
this.routeView.visible = false;
this.clearPredictiveRoute();
this.publishRadar(null);
this.setVisionMode('optical');
this.viewer.scene.screenSpaceCameraController.enableInputs = true;
if (entity && this.viewer.entities.contains(entity))
entity.show = this.trackedEntityWasShown;
this.trackedEntityWasShown = true;
if (vesselSubject)
this.services.aisLiveVesselsLayer?.setCockpitSubject?.(null);
this.dispatchCockpitModeChanged(false);
if (restoreTracking && entity && this.viewer.entities.contains(entity)) {
this.viewer.trackedEntity = entity;
this.restoreTrackingFrame(entity);
} else if (restoreTracking && vesselSubject) {
// Back to the selected vessel's map view, through the layer's own focus.
this.services.aisLiveVesselsLayer?.focusSelectedVessel?.();
}
this.syncEntry();
// Back to whichever entry was used: the Contacts button or the readout's.