import { cameraOf, findCamera, findFrame } from '../../render/rendererOf.js'; import { entityFollowTarget, onFollowChanged, stopFollowing, } from '../../data/followed.js'; import { formatNumber, t } from '../../i18n/index.js'; import { unitLabel } from '../../ui/unitLabels.js'; import { nextCockpitNearContacts } from '../../data/cockpitAirLod.js'; import { courseScreenRotation, stabilizeScreenRotation, } from '../../data/screenRotation.js'; import { createTrackedAircraftIcon, isOtherTrackedIconSet, trackedContactHolder, trackedIconColor, } from '../../data/trackedAircraftIcon.js'; import { fromDegrees, toLonLatH } from '../../geo/ellipsoid.js'; import { julianToEpochMs } from '../../geo/julian.js'; import { renderedFrames } from '../../data/renderedFrames.js'; import { clone, distanceSquared, vec } from '../../geo/vector.js'; import { trailHeadLine } from '../../data/modelVisualAnchor.js'; import { isTrackingSelectionGesture, isTrackingClickGesture, } from '../../data/trackingClickGesture.js'; import { inputFor } from '../../input/dispatcher.js'; import { COCKPIT_AIR_ALL_ADD_M, COCKPIT_AIR_ALL_KEEP_M, COCKPIT_AIR_PROX_ADD_M, COCKPIT_AIR_PROX_KEEP_M, TRAIL_MAX_POINTS, displayDelayMs, followsClock, STALE_ALPHA, TRAIL_COLOR, } from './policy.js'; import { drawTrailSegments, historyFixEpochMs, insertTrailFix, joinTrailBackfill, trailBackfillFixes, trailBodyCount, trailBodySegments, trailHeadCurvePoints, trailHeadStartFix, } from '../../data/trailContinuity.js'; import { roundHeadingDegrees } from '../../data/headingDegrees.js'; import { entityRecord } from '../../data/entityRecords.js'; /** * The icao a click's hit carries (`event.hit`, the contract's `Hit`): a * draw-set item's id (the icon and model sets' rows), or the id of * something drawn outside the draw port (a hit with no set). */ function aircraftPickId(hit) { if (!hit || hit.surface) return undefined; if (!hit.set) return hit.id || undefined; return isOtherTrackedIconSet(hit.set, 'military') ? undefined : hit.id; } export function createTracking({ flightState, services, parts, layer, resolveAsset, }) { const { selectTrackedSubjectContext, refreshTrackedSubjectContext, clearTrackedSubjectContext, } = services.context; const { tr3bTypeLabel, isTr3b, tr3bConvertedIds } = services.aircraftPresentation; const { trackedLabelModelFromText, refreshTrackedReadout } = services.readout; const { floorAltitudeM, cachedGroundFloor, resolveGroundFloorCellsBounded } = services.groundFloor; const { createTrail } = services.trails; const { ensureGeoidReady, geoidHeight } = services.geoid; const { clearFocusTarget } = services.focus; const { FOLLOW_MIN_RANGE_M, SELECTION_FOLLOW_TRANSITION_S, followViewFrom } = services.camera; const { resolvePickId, isOwnedByOtherLayer } = services.picking; /** The tracked contact's billboard (an icon set on the draw port). */ const trackedIcon = createTrackedAircraftIcon({ viewer: () => flightState._viewer, setPrefix: 'military', }); /** The camera's pose, or null without a camera to ask. */ function _pose() { const viewer = flightState._viewer; try { return (viewer && findCamera(viewer)?.pose()) ?? null; } catch { return null; } } function _emitAwarenessEvent(type, detail) { if ( typeof window === 'undefined' || !window.dispatchEvent || typeof CustomEvent === 'undefined' ) return; window.dispatchEvent(new CustomEvent(type, { detail })); } function _publishTrackedSelection(icao24, origin = 'programmatic') { const position = parts.icons._iconPosition(icao24, vec()); const info = flightState.records.data.get(icao24); if (!position || !info) return false; if (flightState._trackedEntity) entityRecord(flightState._trackedEntity).selectionOrigin = origin; _emitAwarenessEvent('gev:awareness-subject-selected', { layerId: 'military', id: icao24, label: parts.queries._toCleanText(info.callsign) || parts.queries._toCleanText(info.registration) || icao24, position, origin, }); selectTrackedSubjectContext(_contextSubjectMetadata(icao24)); return true; } /** * Describe the selected contact for the shared context slot the voice tools * and Cockpit read. Values are the LIVE descriptor, not a selection-time * snapshot, so a long follow never narrates a position the plane has left. * @param {string} icao24 Contact identity. * @returns {object|null} Context metadata, or null when the contact is gone. */ function _contextSubjectMetadata(icao24) { const described = parts.queries._describeFlight(icao24); if (!described) return null; const info = flightState.records.data.get(icao24); const label = described.callsign || described.registration || icao24; return { id: icao24, layerId: 'military', layerName: 'Military Flights', source: flightState.feed._lastSource, label, latitude: described.latitude, longitude: described.longitude, // Flat text only: the voice payload compacts properties through a // string cleaner that drops nested objects. properties: { name: label, operator: parts.queries._toCleanText(info?.operator) || '', callsign: described.callsign || '', registration: described.registration || '', // Converted contacts report their class as TR-3B — the same override // the readout and Contacts card show. type: tr3bTypeLabel( icao24, parts.queries._toCleanText(info?.type) || '', ), altitude: described.onGround ? 'on ground' : parts.queries._formatAltitude(info?.altitudeFt), speed: Number.isFinite(described.velocityMps) ? `${Math.round(described.velocityMps * 1.944)} kt` : '', heading: Number.isFinite(described.track) ? `${roundHeadingDegrees(described.track)}°` : '', icao24, // Honesty cue: the contact is coasting on dead reckoning, so the // narrated position/velocity are last-known rather than live. // i18n-ignore: voice-context data, English for the model status: described.stale ? 'stale (missed polls)' : 'live', }, }; } function _isExplicitTrackingOrigin(origin) { return origin === 'user' || origin === 'voice' || origin === 'tool'; } /** Refresh the AIR-only Cockpit near band independently from model admission. */ function _refreshCockpitNearContacts() { const eye = flightState._cockpitContactMode ? _pose()?.position : null; if (!eye) { if (flightState._cockpitNearContacts.size) flightState._cockpitNearContacts = new Set(); return; } const previous = flightState._cockpitNearContacts; const distancesSquared = []; const icons = flightState._icons; const t = parts.icons._displayMs(); for (const icao24 of icons?.ids() ?? []) { if (icao24 === flightState._trackedIcao) continue; const position = icons.position( icao24, t, flightState._scratchCockpitPos, ); distancesSquared.push([icao24, distanceSquared(eye, position)]); } // The AIR band follows the 3D mode, not the 3D model radius. const all = flightState._models3dMode === 'all'; const next = nextCockpitNearContacts( previous, distancesSquared, all ? COCKPIT_AIR_ALL_ADD_M : COCKPIT_AIR_PROX_ADD_M, all ? COCKPIT_AIR_ALL_KEEP_M : COCKPIT_AIR_PROX_KEEP_M, ); flightState._cockpitNearContacts = next; for (const icao24 of previous) if (!next.has(icao24)) parts.icons._applyIconPresentation(icao24); for (const icao24 of next) if (!previous.has(icao24)) parts.icons._applyIconPresentation(icao24); } /** Switch all current and future ambient contacts between silhouettes and cockpit pips. */ function _setCockpitContactMode(active) { const next = active === true; if (flightState._cockpitContactMode === next) return; flightState._cockpitContactMode = next; if (next) _refreshCockpitNearContacts(); else flightState._cockpitNearContacts = new Set(); // The collection stays visible in cockpit. Near AIR contacts retain their // aircraft silhouette until a ready model takes over; far contacts are pips. // Never bulk-destroy here: that stalls the renderer exactly as cockpit begins. parts.rendering._setModelsShown(true); flightState._trail?.setVisible(!next); const icons = flightState._icons; for (const icao24 of icons?.ids() ?? []) parts.icons._applyIconPresentation(icao24); flightState._lastFleetTickMs = 0; } function _applyCockpitState(detail = {}) { const active = detail?.active === true; flightState._cockpitSubjectId = active ? String(detail?.subjectId || '') .trim() .toLowerCase() || null : null; _setCockpitContactMode(active); } /** * The tracked plane's stale cue and whether it is drawn with the stale fade. * - SIGNAL LOST ·