import type { Body } from "../calculations/constants"; import { findCheapestIntercept, findCheapestLanding, findCheapestTransfer, type Orbit, type OrbitalCoordinates, type ShipParameters, type Transfer } from "../calculations/orbit-calculations"; import type { Landing, LandingParameters, LandingProgressCallbackFunction, Manoeuvre } from "../calculations/orbit-calculations"; const ctx: Worker = self as any; export interface FindBestTransferMessage { type: "FindBestTransfer", startingSituation: OrbitalCoordinates, targetOrbit: Orbit, body: Body } export interface FindBestInterceptMessage { type: "FindBestIntercept", startingSituation: OrbitalCoordinates, targetSituation: OrbitalCoordinates, additionalTrueAnomaly: number, body: Body; } export interface FindBestLandingMessage { "type": "FindBestLandingMessage", startTime: number, startingCoordinates: OrbitalCoordinates, shipParameters: ShipParameters, landingParameters: LandingParameters, body: Body } export interface ProgressMessage { type: "ProgressMessage" finished: boolean, percentDone: number, bestDeltaV: number | null, bestTransfer: Transfer | null } export interface LandingProgressMessage { type: "LandingProgressMessage", finished: boolean, percentDone: number, bestDeltaV: number | null, bestLanding: Landing | null } ctx.addEventListener("message", event => { const progressCallback = (numberChecked: number, totalNumber: number, bestDeltaV: number | null, bestTransfer: Transfer | null) => { if (numberChecked % 1 == 0) { let percentDone = numberChecked * 100 / totalNumber; let message: ProgressMessage = { type: "ProgressMessage", finished: false, percentDone: percentDone, bestDeltaV: bestDeltaV, bestTransfer: bestTransfer }; ctx.postMessage(message); } }; const landingProgressCallback: LandingProgressCallbackFunction = (pathsChecked: number, totalNumberOfPaths: number, currentBestDeltaV: number | null, currentBestLanding: Landing | null) => { if (pathsChecked % 100 == 0) { let percentDone = pathsChecked * 100 / totalNumberOfPaths; let message: LandingProgressMessage = { type: "LandingProgressMessage", finished: false, percentDone: percentDone, bestDeltaV: currentBestDeltaV, bestLanding: currentBestLanding }; ctx.postMessage(message); } } let message = event.data as FindBestTransferMessage | FindBestInterceptMessage | FindBestLandingMessage; if (message.type == "FindBestTransfer") { let bestTransfer = findCheapestTransfer(message.startingSituation, message.targetOrbit, message.body, progressCallback); let bestDeltaV = null; if (bestTransfer) { bestDeltaV = bestTransfer.firstManoeuvre.totalDeltaV + bestTransfer.secondManoeuvre.totalDeltaV; } let finishedMessage: ProgressMessage = { type: "ProgressMessage", finished: true, percentDone: 100, bestDeltaV: bestDeltaV, bestTransfer: bestTransfer }; ctx.postMessage(finishedMessage); } if (message.type == "FindBestIntercept") { let bestIntercept = findCheapestIntercept(message.startingSituation, message.targetSituation, message.body, message.additionalTrueAnomaly, progressCallback); let bestDeltaV = null; if (bestIntercept) { bestDeltaV = bestIntercept.firstManoeuvre.totalDeltaV + bestIntercept.secondManoeuvre.totalDeltaV; } let finishedMessage: ProgressMessage = { type: "ProgressMessage", finished: true, percentDone: 100, bestDeltaV: bestDeltaV, bestTransfer: bestIntercept }; ctx.postMessage(finishedMessage); } if (message.type == "FindBestLandingMessage") { let bestLanding = findCheapestLanding(message.startingCoordinates, message.startTime, message.shipParameters, message.landingParameters, message.body, landingProgressCallback); let finishedMessage: LandingProgressMessage = { type: "LandingProgressMessage", finished: true, percentDone: 100, bestDeltaV: bestLanding?.totalDeltaV ?? null, bestLanding: bestLanding }; ctx.postMessage(finishedMessage); } });