Aetheria/Aetheria.Rts.Web/Electron/aetheria-cultmesh.ts
2026-07-04 22:46:04 +01:00

901 lines
32 KiB
TypeScript

import { encode } from "@msgpack/msgpack";
import { performance } from "node:perf_hooks";
import { CultMesh } from "cultmesh-ts";
import type {
CultMeshDocumentCatalog,
CultMeshDocumentPublicationSource,
CultMeshPublicationDocumentBinding,
CultMeshOperationContext,
CultMeshQueryContext,
CultMeshSurfaceCatalogIndexDiagnostic,
CultMeshVerse,
} from "cultmesh-ts";
import type {
CultNetDocumentPutRawMessage,
CultNetPeer,
} from "cultnet-ts";
import {
AetheriaRtsSchemas,
createAetheriaRuntimeRtsDocuments,
createAetheriaRuntimeRtsOperationHandles,
createAetheriaRuntimeRtsVerseHandles,
createAetheriaRuntimeRtsQueryHandles,
describeAetheriaRuntimeRtsLiveFeedSurface,
describeAetheriaRuntimeRtsQueryHandles,
describeAetheriaRuntimeRtsSurfaceCatalog,
type AetheriaRuntimeDaemonCommandReceipt,
type AssetManifestDocument,
type AuthorityStatusDocument,
type DaemonHealthDocument,
type GravityViewportResponse,
type InventoryDocument,
type ObjectsViewportResponse,
type AetheriaRuntimeRtsLiveFeedDiagnostic,
type AetheriaRuntimeRtsQueryDiagnostic,
type AetheriaRuntimeRtsSurfaceCatalogDiagnostic,
type AetheriaRuntimeRtsOperationHandles,
type AetheriaRuntimeRtsVerseHandles,
type AetheriaRuntimeRtsDocuments,
type AetheriaRuntimeViewportFeedRequest,
type AetheriaRuntimeViewportFeedSnapshot,
type RtsSetMoveVectorRequest,
type RtsSetTargetRequest,
type SelectedObjectDocument,
type SelectedObjectRequest,
type StarbridgeSessionDocument,
type ViewportRequest,
type ViewportResponse,
} from "./aetheria-rts-bindings.js";
import {
readAuthorityStatusDocument,
readAssetManifestDocument,
readDaemonHealthDocument,
buildGravityViewportDocumentFromFrame,
buildInventoryDocumentFromFrame,
buildObjectsViewportDocumentFromFrame,
buildSelectedObjectDocumentFromFrame,
readStarbridgeSessionSummaryDocument,
buildViewportDocumentFromFrame,
} from "./aetheria-rts-local-documents.js";
const connectionId = 0x43554c54;
type AetheriaPublicationDocumentSpec = CultMeshPublicationDocumentBinding & {
readonly localPath: string;
readonly remoteRecordKey?: string;
};
export type AetheriaCultMeshClientOptions = {
publicationMode?: "local" | "remote";
snapshotTimeoutMs?: number;
};
export type AetheriaCultMeshDaemonTarget = {
readonly uri: string;
readonly peerId?: string;
readonly verseId?: string;
readonly role?: string;
readonly endpoints?: readonly string[];
};
export type AetheriaMenuSurfaceRequest = {
surfaceId?: string;
inGame?: boolean;
canOpenRuntimeInputScreen?: boolean;
};
export type AetheriaMenuSurfaceDocument = {
providerId: string;
providerKind: string;
title: string;
version: number;
updatedAtUtc: string;
surface: AetheriaMenuSurfaceTree;
commands: AetheriaMenuSurfaceCommand[];
};
export type AetheriaMenuSurfaceTree = {
id: string;
root: AetheriaMenuSurfaceComponent;
styles: AetheriaMenuStyleToken[];
};
export type AetheriaMenuSurfaceComponent = {
id: string;
kind: string;
props: Record<string, string>;
layout?: Record<string, string>;
style?: Record<string, string>;
children: AetheriaMenuSurfaceComponent[];
};
export type AetheriaMenuStyleToken = {
name: string;
value: string;
};
export type AetheriaMenuSurfaceCommand = {
command: string;
label: string;
transport: string;
};
export type {
BodyView,
EntityStatus,
GravityViewportResponse,
GravityInfluence,
InventoryItem,
ObjectsViewportResponse,
AetheriaRuntimeViewportFeedRequest,
AetheriaRuntimeViewportFeedSnapshot,
RtsSetMoveVectorRequest,
RtsSetTargetRequest,
SelectedObjectDocument,
SelectedObjectRequest,
StarbridgeSessionDocument,
ViewObject,
ViewportRequest,
ViewportResponse,
} from "./aetheria-rts-bindings.js";
export class AetheriaCultMeshClient {
#peer: CultNetPeer | null = null;
private readonly verse: CultMeshVerse;
private readonly queryVerse: CultMeshVerse;
private readonly commandVerse: CultMeshVerse;
private readonly daemonTarget: Required<AetheriaCultMeshDaemonTarget>;
public constructor(
daemonTarget: string | AetheriaCultMeshDaemonTarget,
statePath: string,
private readonly runtimeId = "aetheria-rts-electron",
options: AetheriaCultMeshClientOptions = {},
) {
this.daemonTarget = normalizeDaemonTarget(daemonTarget);
const publicationMode = options.publicationMode ?? "local";
this.publicationDescription = publicationMode === "remote" ? this.daemonTarget.uri : statePath;
this.verse = CultMesh.verse("aetheria.local", this.runtimeId);
this.queryVerse = publicationMode === "remote"
? this.verse.withRoute("network", this.publicationDescription)
: this.verse.withRoute("shared-memory", this.publicationDescription);
this.commandVerse = this.verse
.withRoute("network", this.daemonTarget.uri)
.withClaim("commander-control", { shardId: "aetheria.local" });
this.publications = CultMesh.documentsFromPublication(
this.createPublicationSource(publicationMode),
createAetheriaPublicationDocuments(statePath, publicationMode),
{
routeHint: this.queryVerse.context.routeHint,
timeoutMs: options.snapshotTimeoutMs,
pollMs: 50,
messageIdPrefix: `${this.runtimeId}:snapshot`,
},
);
const executors = {
mapViewport: async (request: ViewportRequest) => buildViewportDocumentFromFrame(await this.fetchLatestFrameDocument(), request),
objectsViewport: async (request: ViewportRequest) => buildObjectsViewportDocumentFromFrame(await this.fetchLatestFrameDocument(), request),
gravityViewport: async (request: ViewportRequest) => buildGravityViewportDocumentFromFrame(await this.fetchLatestFrameDocument(), request),
selectedObject: async (request: SelectedObjectRequest) => buildSelectedObjectDocumentFromFrame(await this.fetchLatestFrameDocument(), request),
inventory: async (request: SelectedObjectRequest) => buildInventoryDocumentFromFrame(await this.fetchLatestFrameDocument(), request),
daemonHealth: async () => readDaemonHealthDocument(await this.fetchDaemonHealthDocument()),
authorityStatus: async () => readAuthorityStatusDocument(await this.fetchAuthorityPolicyDocument()),
starbridgeSession: async () => readStarbridgeSessionSummaryDocument(await this.fetchStarbridgeSessionSummaryDocument()),
assetManifest: async () => readAssetManifestDocument(await this.fetchAssetManifestDocument()),
};
this.queries = createAetheriaRuntimeRtsQueryHandles(
executors,
this.queryVerse.context.routeHint,
{
objectsViewport: CultMesh.pollingQueryWatcher(executors.objectsViewport, { intervalMs: 50 }),
gravityViewport: CultMesh.pollingQueryWatcher(executors.gravityViewport, { intervalMs: 50 }),
selectedObject: CultMesh.pollingQueryWatcher(executors.selectedObject, { intervalMs: 50 }),
inventory: CultMesh.pollingQueryWatcher(executors.inventory, { intervalMs: 50 }),
daemonHealth: CultMesh.pollingQueryWatcher(executors.daemonHealth, { intervalMs: 250 }),
authorityStatus: CultMesh.pollingQueryWatcher(executors.authorityStatus, { intervalMs: 250 }),
starbridgeSession: CultMesh.pollingQueryWatcher(executors.starbridgeSession, { intervalMs: 250 }),
assetManifest: CultMesh.pollingQueryWatcher(executors.assetManifest, { intervalMs: 1000 }),
},
);
this.documents = createAetheriaRuntimeRtsDocuments(
this.queryVerse.context.routeHint,
{
daemonFrame: async () => this.fetchLatestFrameDocument(),
daemonHealth: async () => this.fetchDaemonHealthDocument(),
authorityPolicy: async () => this.fetchAuthorityPolicyDocument(),
starbridgeSession: async () => this.fetchStarbridgeSessionSummaryDocument(),
assetManifest: async () => this.fetchAssetManifestDocument(),
},
);
this.operations = createAetheriaRuntimeRtsOperationHandles(
(commandId, issuedAtUtc, command, context) =>
this.sendCommandDocument(commandId, issuedAtUtc, command, context),
);
this.aetheria = createAetheriaRuntimeRtsVerseHandles(
this.queryVerse.context,
this.commandVerse.context,
this.queries,
this.operations,
this.documents,
);
}
private readonly publicationDescription: string;
private readonly publications: CultMeshDocumentCatalog;
private readonly queries: ReturnType<typeof createAetheriaRuntimeRtsQueryHandles>;
private readonly documents: AetheriaRuntimeRtsDocuments;
private readonly operations: AetheriaRuntimeRtsOperationHandles;
private readonly aetheria: AetheriaRuntimeRtsVerseHandles;
public async close(): Promise<void> {
this.#peer?.close();
this.#peer = null;
}
public async waitForFrame(timeoutMs: number): Promise<void> {
const started = Date.now();
let lastError = "";
while (Date.now() - started < timeoutMs) {
try {
await this.mapViewport(CultMesh.viewportRequest(CultMesh.rectFromBounds(-1000, -1000, 1000, 1000)));
return;
} catch (error) {
lastError = error instanceof Error ? error.message : String(error);
await delay(250);
}
}
throw new Error(`Timed out waiting for Aetheria CultMesh frame at ${this.daemonTarget.uri}. ${lastError}`);
}
public async mapViewport(request: ViewportRequest): Promise<ViewportResponse> {
return this.aetheria.zone().viewport.within(request);
}
public async objectsViewport(request: ViewportRequest): Promise<ObjectsViewportResponse> {
return this.aetheria.zone().objects.visibleWithin(request);
}
public async gravityViewport(request: ViewportRequest): Promise<GravityViewportResponse> {
return this.aetheria.zone().gravity.within(request);
}
public async selectedObject(request: SelectedObjectRequest): Promise<SelectedObjectDocument> {
return this.aetheria.selectedObject(request.entityIndex);
}
public async inventory(request: SelectedObjectRequest): Promise<InventoryDocument> {
return this.aetheria.inventory(request.entityIndex);
}
public async daemonHealth(): Promise<DaemonHealthDocument> {
return this.aetheria.daemon.health();
}
public async authorityStatus(): Promise<AuthorityStatusDocument> {
return this.aetheria.daemon.authorityStatus();
}
public async starbridgeSession(): Promise<StarbridgeSessionDocument> {
return this.aetheria.daemon.starbridgeSession();
}
public async assetManifest(): Promise<AssetManifestDocument> {
return this.aetheria.daemon.assetManifest();
}
public async mainMenuSurface(request: AetheriaMenuSurfaceRequest = {}): Promise<AetheriaMenuSurfaceDocument> {
return buildMainMenuSurface(
request.surfaceId || "aetheria.main_menu.root",
request.canOpenRuntimeInputScreen === true,
request.inGame === true);
}
public queryDiagnostics(): Readonly<Record<string, AetheriaRuntimeRtsQueryDiagnostic>> {
return describeAetheriaRuntimeRtsQueryHandles(this.queries);
}
public liveFeedDiagnostics(): Readonly<Record<string, AetheriaRuntimeRtsLiveFeedDiagnostic>> {
return {
viewportFeed: describeAetheriaRuntimeRtsLiveFeedSurface(this.createViewportFeed()),
};
}
public surfaceCatalogDiagnostics(): AetheriaRuntimeRtsSurfaceCatalogDiagnostic {
const catalog = describeAetheriaRuntimeRtsSurfaceCatalog(this.queries, this.operations, this.documents);
return CultMesh.describeSurfaceCatalog(catalog.catalogId, [
...catalog.surfaces,
CultMesh.describeSurface(this.createViewportFeed()),
]);
}
public surfaceCatalogIndexDiagnostics(): CultMeshSurfaceCatalogIndexDiagnostic {
return CultMesh.surfaceCatalogIndex(this.surfaceCatalogDiagnostics());
}
public watchViewportFeed(
request: AetheriaRuntimeViewportFeedRequest,
callback: (snapshot: AetheriaRuntimeViewportFeedSnapshot) => void,
): () => void {
return this.createViewportFeed().watch(request, this.queryContext(), callback);
}
public async setMoveVector(request: RtsSetMoveVectorRequest): Promise<AetheriaRuntimeDaemonCommandReceipt> {
return this.aetheria.entity(request.actorEntityKey).pilot.move(
CultMesh.vec2(request.directionX, request.directionY),
{
scalar: request.scalar,
observedFrameId: request.observedFrameId,
},
);
}
public async setTarget(request: RtsSetTargetRequest): Promise<AetheriaRuntimeDaemonCommandReceipt> {
return this.aetheria.entity(request.actorEntityKey).pilot.target(request.targetEntityKey, {
observedFrameId: request.observedFrameId,
});
}
private async fetchLatestFrameDocument(): Promise<unknown> {
return this.fetchPublicationDocument(AetheriaRtsSchemas.daemonFrame);
}
private async fetchDaemonHealthDocument(): Promise<unknown> {
return this.fetchPublicationDocument(AetheriaRtsSchemas.daemonHealth);
}
private async fetchAuthorityPolicyDocument(): Promise<unknown> {
return this.fetchPublicationDocument(AetheriaRtsSchemas.verseAuthorityPolicy);
}
private async fetchStarbridgeSessionSummaryDocument(): Promise<unknown> {
return this.fetchPublicationDocument(AetheriaRtsSchemas.starbridgeSessionSummary);
}
private async fetchAssetManifestDocument(): Promise<unknown> {
return this.fetchPublicationDocument(AetheriaRtsSchemas.assetManifest);
}
private fetchPublicationDocument(schemaId: string): Promise<unknown> {
return this.publications.latest({ schemaId }, this.queryContext());
}
private createPublicationSource(
mode: "local" | "remote",
): (binding: CultMeshPublicationDocumentBinding) => CultMeshDocumentPublicationSource {
if (mode === "remote") {
return () => ({
kind: "peer-snapshot",
peer: () => this.peer(),
endpoint: this.resolvedRudpEndpoint(),
});
}
return binding => ({
kind: "single-file",
path: (binding as AetheriaPublicationDocumentSpec).localPath,
});
}
private createViewportFeed() {
return CultMesh.liveFeed<AetheriaRuntimeViewportFeedRequest, AetheriaRuntimeViewportFeedSnapshot>(
"gamecult.aetheria.rts.viewport_feed.v1",
async (request, context) => {
const objects = await this.queries.objectsViewport.execute(request.viewport, context);
return this.createViewportFeedSnapshot(request, objects);
},
{
sources: [
...this.queries.objectsViewport.sources,
...this.queries.gravityViewport.sources,
...this.queries.daemonHealth.sources,
...this.queries.authorityStatus.sources,
...this.queries.starbridgeSession.sources,
...this.queries.assetManifest.sources,
],
routeHint: this.queryVerse.context.routeHint,
watchFeed: (request, context, callback) => {
let disposed = false;
let sampling = false;
const unsubscribe = this.queries.objectsViewport.watch(request.viewport, context, objects => {
if (disposed || sampling) {
return;
}
sampling = true;
void this.createViewportFeedSnapshot(request, objects)
.then(snapshot => {
if (!disposed) {
callback(snapshot);
}
})
.finally(() => {
sampling = false;
});
});
return () => {
disposed = true;
unsubscribe();
};
},
},
);
}
private async createViewportFeedSnapshot(
request: AetheriaRuntimeViewportFeedRequest,
objects: ObjectsViewportResponse,
): Promise<AetheriaRuntimeViewportFeedSnapshot> {
const startedAt = performance.now();
const selectedEntityIndex = request.selectedEntityIndex ?? -1;
const [
gravity,
daemonHealth,
authorityStatus,
starbridgeSession,
assetManifest,
selectedObject,
inventory,
] = await Promise.all([
this.aetheria.zone().gravity.within(request.viewport),
this.aetheria.daemon.health(),
this.aetheria.daemon.authorityStatus(),
this.aetheria.daemon.starbridgeSession(),
this.aetheria.daemon.assetManifest(),
selectedEntityIndex >= 0
? this.aetheria.selectedObject(selectedEntityIndex)
: Promise.resolve(null),
selectedEntityIndex >= 0
? this.aetheria.inventory(selectedEntityIndex)
: Promise.resolve(null),
]);
return {
viewport: composeViewport(objects, gravity),
selectedObject,
inventory,
daemonHealth,
authorityStatus,
starbridgeSession,
assetManifest,
receivedAtUtc: new Date().toISOString(),
sampleMs: performance.now() - startedAt,
};
}
private queryContext(): CultMeshQueryContext {
return this.queryVerse.queryContext();
}
private async sendCommandDocument(
commandId: string,
issuedAtUtc: string,
command: unknown[],
context: CultMeshOperationContext,
): Promise<void> {
const message: CultNetDocumentPutRawMessage = {
schemaVersion: "cultnet.document_put_raw.v0",
messageId: commandId,
document: {
schemaId: AetheriaRtsSchemas.daemonCommand,
recordKey: `daemon:commands:${stableToken(commandId)}:${AetheriaRtsSchemas.daemonCommand}`,
storedAt: issuedAtUtc,
payloadEncoding: "messagepack",
payload: encode(command),
sourceRuntimeId: context.runtimeId,
sourceRole: "rts-client",
tags: ["aetheria-rts"],
},
};
const peer = await this.peer();
peer.send(message);
await delay(80);
}
private async peer(): Promise<CultNetPeer> {
if (this.#peer)
return this.#peer;
const peers = CultMesh.createPeerCatalog();
const leases = CultMesh.createAuthorityLeaseCatalog();
const leaseId = `${this.daemonTarget.peerId}:authority`;
peers.upsert({
peerId: this.daemonTarget.peerId,
verseId: this.daemonTarget.verseId,
endpoints: this.daemonTarget.endpoints,
roles: [this.daemonTarget.role],
authorityLeaseId: leaseId,
});
leases.upsert({
leaseId,
verseId: this.daemonTarget.verseId,
peerId: this.daemonTarget.peerId,
roles: [this.daemonTarget.role],
validFrom: new Date(Date.now() - 60_000),
expiresAt: new Date(Date.now() + 60_000),
});
this.#peer = await CultMesh.createRudpPeerForAuthorizedPeer(
this.runtimeId,
connectionId,
peers,
leases,
this.daemonTarget.verseId,
this.daemonTarget.role,
{
connectTimeoutMs: 2000,
maxFragmentBytes: 1200,
maxPendingReliablePackets: 512,
},
);
this.#peer.on("close", () => {
this.#peer = null;
});
return this.#peer;
}
private resolvedRudpEndpoint(): string {
const endpoint = this.daemonTarget.endpoints.find(value => value.toLowerCase().startsWith("rudp://"));
if (!endpoint) {
throw new Error(`Daemon ${this.daemonTarget.uri} has no resolved RUDP transport endpoint. Resolve it through Odin/CultMesh before opening a transport peer.`);
}
return endpoint;
}
}
function normalizeDaemonTarget(target: string | AetheriaCultMeshDaemonTarget): Required<AetheriaCultMeshDaemonTarget> {
const value = typeof target === "string"
? { uri: target }
: target;
if (!value.uri.trim()) {
throw new Error("Aetheria CultMesh daemon target URI is required.");
}
if (value.uri.toLowerCase().startsWith("rudp://")) {
throw new Error("Aetheria daemon targets must be CultMesh URIs; raw RUDP endpoints belong in resolved peer-card endpoints.");
}
return {
uri: value.uri,
peerId: value.peerId?.trim() || "aetheria-rts-daemon",
verseId: value.verseId?.trim() || "aetheria.local",
role: value.role?.trim() || "aetheria-rts-daemon",
endpoints: value.endpoints ?? [],
};
}
function buildMainMenuSurface(
surfaceId: string,
canOpenRuntimeInputScreen: boolean,
inGame: boolean,
): AetheriaMenuSurfaceDocument {
const updatedAtUtc = new Date().toISOString();
if (surfaceId === "aetheria.main_menu.settings") {
return surfaceDocument(
surfaceId,
"Aetheria Settings",
updatedAtUtc,
[
command("aetheria.main_menu.settings.show_player_settings", "Player Settings"),
command("aetheria.main_menu.settings.show_verse_settings", "Verse"),
command("aetheria.main_menu.settings.show_input_settings", "Input"),
command("aetheria.main_menu.settings.back_to_main", "Back"),
],
text("aetheria.mainMenu.settings.title", "SETTINGS", "text.title"),
buttonColumn(
"aetheria.mainMenu.settings.actions",
button("aetheria.mainMenu.settings.playerSettings", "Player Settings", "aetheria.main_menu.settings.show_player_settings"),
button("aetheria.mainMenu.settings.verse", "Verse", "aetheria.main_menu.settings.show_verse_settings"),
button("aetheria.mainMenu.settings.input", "Input", "aetheria.main_menu.settings.show_input_settings"),
button("aetheria.mainMenu.settings.back", "Back", "aetheria.main_menu.settings.back_to_main"),
),
);
}
if (surfaceId === "aetheria.main_menu.input_settings") {
const children = [
text("aetheria.mainMenu.input.title", "INPUT", "text.title"),
metric("aetheria.mainMenu.input.bindingOverrides", "Binding Overrides", "0"),
metric("aetheria.mainMenu.input.actionBarInputs", "Action-Bar Inputs", "0"),
text(
"aetheria.mainMenu.input.note",
canOpenRuntimeInputScreen && inGame
? "The runtime Eve input screen owns low-level InputSystem rebinding and action-bar input edits."
: "This title shell reports typed player-settings state. Launch a run to open the runtime Eve input screen that owns low-level InputSystem rebinding."),
buttonColumn(
"aetheria.mainMenu.input.actions",
button("aetheria.mainMenu.input.back", "Back", "aetheria.main_menu.settings.back_to_settings"),
),
];
return surfaceDocument(
surfaceId,
"Aetheria Input Settings",
updatedAtUtc,
[command("aetheria.main_menu.settings.back_to_settings", "Back")],
...children);
}
const actionButtons = [
...(!inGame ? [button("aetheria.main_menu.root.continue", "Continue", "aetheria.main_menu.root.continue")] : []),
button("aetheria.main_menu.root.newGame", "New Game", "aetheria.main_menu.root.new_game"),
button("aetheria.main_menu.root.settings", "Settings", "aetheria.main_menu.root.show_settings"),
button("aetheria.main_menu.root.quit", "Quit", "aetheria.main_menu.root.quit"),
];
return surfaceDocument(
"aetheria.main_menu.root",
"Aetheria Terminus",
updatedAtUtc,
[
...(!inGame ? [command("aetheria.main_menu.root.continue", "Continue")] : []),
command("aetheria.main_menu.root.new_game", "New Game"),
command("aetheria.main_menu.root.show_settings", "Settings"),
command("aetheria.main_menu.root.quit", "Quit"),
],
gravitySurface("aetheria.main_menu.root.gravity"),
node(
"aetheria.main_menu.root.menu",
"column",
{},
{
position: "relative",
padding: "7.25rem 0 0 6.75rem",
gap: "1.1rem",
width: "44rem",
maxWidth: "calc(100vw - 3rem)",
minHeight: "100vh",
alignItems: "flex-start",
},
{ color: "#e9fbff" },
text("aetheria.main_menu.root.title", "AETHERIA", "text.title", { margin: "0 0 -1.6rem 0" }, {
font: "100 5.9rem/0.98 Montserrat, sans-serif",
color: "rgba(232, 250, 255, 0.94)",
whiteSpace: "nowrap",
}),
text("aetheria.main_menu.root.subtitle", "TERMINUS", "text.subtitle", { margin: "0 0 0.35rem 16.8rem" }, {
font: "100 2.6rem/1 Montserrat, sans-serif",
color: "rgba(232, 250, 255, 0.9)",
whiteSpace: "nowrap",
}),
buttonColumn("aetheria.main_menu.root.actions", ...actionButtons),
),
);
}
function surfaceDocument(
surfaceId: string,
title: string,
updatedAtUtc: string,
commands: AetheriaMenuSurfaceCommand[],
...children: AetheriaMenuSurfaceComponent[]
): AetheriaMenuSurfaceDocument {
return {
providerId: "aetheria",
providerKind: "game.menu",
title,
version: 1,
updatedAtUtc,
surface: {
id: surfaceId,
root: node(
`${surfaceId}.root`,
"surface",
{},
{ position: "relative", overflow: "hidden", width: "100%", height: "100vh", minHeight: "100vh" },
{ background: "#020606" },
...children),
styles: [
{ name: "font.title.family", value: "Montserrat" },
{ name: "font.title.style", value: "Thin" },
{ name: "font.title.weight", value: "100" },
{ name: "font.body.family", value: "Ubuntu" },
{ name: "font.body.style", value: "Regular" },
{ name: "font.body.weight", value: "400" },
{
name: "font.web.google",
value: "https://fonts.googleapis.com/css2?family=Montserrat:wght@100&family=Ubuntu:wght@400&display=swap",
},
],
},
commands,
};
}
function command(commandId: string, label: string): AetheriaMenuSurfaceCommand {
return { command: commandId, label, transport: "cultmesh" };
}
function node(
id: string,
kind: string,
props: Record<string, string>,
layoutOrChild?: Record<string, string> | AetheriaMenuSurfaceComponent,
styleOrChild?: Record<string, string> | AetheriaMenuSurfaceComponent,
...children: AetheriaMenuSurfaceComponent[]
): AetheriaMenuSurfaceComponent {
const layout = isSurfaceComponent(layoutOrChild) ? undefined : layoutOrChild;
const style = isSurfaceComponent(styleOrChild) ? undefined : styleOrChild;
const normalizedChildren = [
...(isSurfaceComponent(layoutOrChild) ? [layoutOrChild] : []),
...(isSurfaceComponent(styleOrChild) ? [styleOrChild] : []),
...children,
];
return { id, kind, props, layout, style, children: normalizedChildren };
}
function text(
id: string,
value: string,
kind = "text",
layout?: Record<string, string>,
style?: Record<string, string>,
): AetheriaMenuSurfaceComponent {
return node(id, kind, { value }, layout, style);
}
function metric(id: string, label: string, value: string): AetheriaMenuSurfaceComponent {
return node(id, "metric", { label, value });
}
function button(id: string, label: string, commandId: string): AetheriaMenuSurfaceComponent {
return node(
id,
"control.button",
{ label, command: commandId },
{ minWidth: "0", padding: "0.04rem 0" },
{
background: "rgba(0, 0, 0, 0)",
borderWidth: "0",
borderStyle: "solid",
boxShadow: "none",
font: "400 1.55rem/1.2 Ubuntu, sans-serif",
color: "#e8fbff",
textAlign: "left",
});
}
function buttonColumn(id: string, ...children: AetheriaMenuSurfaceComponent[]): AetheriaMenuSurfaceComponent {
return node(id, "column", {}, { gap: "0.18rem", alignItems: "flex-start" }, { color: "#e8fbff" }, ...children);
}
function gravitySurface(id: string): AetheriaMenuSurfaceComponent {
return node(
id,
"gravity.surface",
{
label: "Aetheria level gravity surface",
viewRadius: "1450",
terrainRadius: "1450",
terrainDepth: "-8",
terrainDepthExponent: "1.2",
terrainWaveFrequency: "0.6",
simulationTimeSeconds: "0",
samplesX: "196",
isolines: "22",
shader: "perlines.gravity-map.v1",
noiseAsset: "cultmesh://aetheria/assets/textures/perlines-nebula",
tintSplatAsset: "cultmesh://aetheria/assets/textures/tint_splat",
bodies: [
"terminus-sun|Sun|0|0|900|-80|3|450|10|2|cultmesh://aetheria/assets/icons/star.terminus-sun|cultmesh://aetheria/assets/textures/tint_splat",
"anchor-moon|Planet|360|-180|260|-22|2.2|180|4|1.4|cultmesh://aetheria/assets/icons/body.anchor-moon|cultmesh://aetheria/assets/textures/tint_splat",
"blueglass|Planet|-540|340|340|-34|2.6|150|5|1.1|cultmesh://aetheria/assets/icons/body.blueglass|cultmesh://aetheria/assets/textures/tint_splat",
"emberhook|Planet|-680|-420|310|-28|2.1|130|4|1.3|cultmesh://aetheria/assets/icons/body.emberhook|cultmesh://aetheria/assets/textures/tint_splat",
"vesper|Gas Giant|780|420|420|-38|2.4|210|6|0.7|cultmesh://aetheria/assets/icons/body.vesper|cultmesh://aetheria/assets/textures/tint_splat",
"latch|Moon|-160|620|210|-18|2.8|95|3|1.6|cultmesh://aetheria/assets/icons/body.latch|cultmesh://aetheria/assets/textures/tint_splat",
].join(";"),
objects: [
"anchor-station|station|Anchor Station|-220|-90|1|0|player|1|1|cultmesh://aetheria/assets/icons/station.anchor-station",
"vanguard-one|ship|Vanguard One|-60|-40|1|0.2|player|1|1|cultmesh://aetheria/assets/icons/ship.vanguard-one",
"wing-two|ship|Wing Two|180|120|-0.4|0.9|player|1|1|cultmesh://aetheria/assets/icons/ship.wing-two",
"torch-three|ship|Torch Three|-160|210|0.6|-0.8|player|1|1|cultmesh://aetheria/assets/icons/ship.torch-three",
"ash-raider|ship|Ash Raider|420|180|-0.8|-0.1|raider|0|1|cultmesh://aetheria/assets/icons/ship.ash-raider",
"derelict-relay|station|Derelict Relay|-260|260|0|1|neutral|0|1|cultmesh://aetheria/assets/icons/station.derelict-relay",
].join(";"),
},
{ position: "absolute", top: "0", right: "0", bottom: "0", left: "0", width: "100%", height: "100%" });
}
function isSurfaceComponent(value: unknown): value is AetheriaMenuSurfaceComponent {
return !!value && typeof value === "object" && "kind" in value && "props" in value && "children" in value;
}
function stableToken(value: string): string {
return value
.replace(/[^a-zA-Z0-9]+/gu, "-")
.replace(/^-+|-+$/gu, "")
.replace(/--+/gu, "-")
.toLowerCase() || "empty";
}
function composeViewport(
objects: ObjectsViewportResponse,
gravity: GravityViewportResponse,
): ViewportResponse {
return {
schema: "gamecult.aetheria.rts_viewport.v1",
frameId: objects.frameId,
publishedAtUtc: objects.publishedAtUtc,
simulationTimeSeconds: objects.simulationTimeSeconds,
runId: objects.runId,
zoneIndex: objects.zoneIndex,
zoneName: objects.zoneName,
currentEntityKey: objects.currentEntityKey,
viewport: objects.viewport,
controlledEntityIndices: objects.controlledEntityIndices,
objects: objects.objects,
gravityInfluences: gravity.gravityInfluences,
bodies: gravity.bodies,
};
}
function createAetheriaPublicationDocuments(
statePath: string,
mode: "local" | "remote",
): readonly AetheriaPublicationDocumentSpec[] {
const documents: readonly AetheriaPublicationDocumentSpec[] = [
{
...CultMesh.publicationDocument(
AetheriaRtsSchemas.daemonFrame,
"daemon:aetheria.frame.latest.v1",
{
documentId: "daemon:aetheria.frame.latest.v1",
sourceId: "daemon:aetheria.frame.latest.v1",
},
),
localPath: statePath,
},
{
...CultMesh.publicationDocument(
AetheriaRtsSchemas.daemonHealth,
"daemon:aetheria.health.latest.v1",
{
documentId: "daemon:aetheria.health.latest.v1",
sourceId: "daemon:aetheria.health.latest.v1",
},
),
localPath: statePath,
remoteRecordKey: "daemon:aetheria.health.v1",
},
{
...CultMesh.publicationDocument(
AetheriaRtsSchemas.verseAuthorityPolicy,
"daemon:aetheria.authority.policy.latest.v1",
{
documentId: "daemon:aetheria.authority.policy.latest.v1",
sourceId: "daemon:aetheria.authority.policy.latest.v1",
},
),
localPath: statePath,
remoteRecordKey: "global:aetheria.verse_authority_policy.v1",
},
{
...CultMesh.publicationDocument(
AetheriaRtsSchemas.starbridgeSessionSummary,
"daemon:aetheria.starbridge.session.latest.v1",
{
documentId: "daemon:aetheria.starbridge.session.latest.v1",
sourceId: "daemon:aetheria.starbridge.session.latest.v1",
},
),
localPath: statePath,
},
{
...CultMesh.publicationDocument(
AetheriaRtsSchemas.assetManifest,
"daemon:aetheria.asset_manifest.latest.v1",
{
documentId: "daemon:aetheria.asset_manifest.latest.v1",
sourceId: "daemon:aetheria.asset_manifest.latest.v1",
},
),
localPath: statePath,
},
];
return documents.map(document => mode === "remote" && document.remoteRecordKey
? {
...document,
recordKey: document.remoteRecordKey,
}
: document);
}
function delay(milliseconds: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, milliseconds));
}