@wildshard/dev 0.0.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/bin/wildshard.mjs +75 -0
  2. package/lint/host-callbacks.d.mts +6 -0
  3. package/lint/host-callbacks.mjs +123 -0
  4. package/lint/ratchet.json +3 -0
  5. package/package.json +95 -4
  6. package/scripts/audit-assets.d.mts +10 -0
  7. package/scripts/audit-assets.mjs +151 -0
  8. package/scripts/bake/coatFiles.d.mts +6 -0
  9. package/scripts/bake/coatFiles.mjs +37 -0
  10. package/scripts/bake/navmeshGround.ts +22 -0
  11. package/scripts/bake/near-placeholder.mjs +331 -0
  12. package/scripts/bake/viewmodelBytes.ts +26 -0
  13. package/scripts/bake/viewmodelPage.ts +11 -0
  14. package/scripts/bake-catalogue.mjs +44 -0
  15. package/scripts/bake-check.mjs +54 -0
  16. package/scripts/bake-chunk.mjs +124 -0
  17. package/scripts/bake-cloud-field.mjs +24 -0
  18. package/scripts/bake-coats.mjs +110 -0
  19. package/scripts/bake-data-maps.d.mts +24 -0
  20. package/scripts/bake-data-maps.mjs +78 -0
  21. package/scripts/bake-entry-programs.mjs +126 -0
  22. package/scripts/bake-geometry.mjs +106 -0
  23. package/scripts/bake-ktx2.mjs +477 -0
  24. package/scripts/bake-map-glb.mjs +127 -0
  25. package/scripts/bake-map-rest.d.mts +7 -0
  26. package/scripts/bake-map-rest.mjs +50 -0
  27. package/scripts/bake-maps.mjs +200 -0
  28. package/scripts/bake-navmesh.mjs +259 -0
  29. package/scripts/bake-packs.mjs +129 -0
  30. package/scripts/bake-pickups.mjs +102 -0
  31. package/scripts/bake-query-terrain.mjs +64 -0
  32. package/scripts/bake-textures.mjs +80 -0
  33. package/scripts/bake-viewmodel-sets.mjs +137 -0
  34. package/scripts/bake-voxel-ao.mjs +119 -0
  35. package/scripts/bake-water-ripples.mjs +25 -0
  36. package/scripts/browser-imports.d.mts +2 -0
  37. package/scripts/browser-imports.mjs +40 -0
  38. package/scripts/browser-machine-loader-proof.mjs +39 -0
  39. package/scripts/browser-machine-loader.d.mts +8 -0
  40. package/scripts/browser-machine-loader.mjs +87 -0
  41. package/scripts/browser-playwright.d.mts +10 -0
  42. package/scripts/browser-playwright.mjs +5 -0
  43. package/scripts/browser-test.d.mts +4 -0
  44. package/scripts/browser-test.mjs +18 -0
  45. package/scripts/build-sdk.mjs +95 -0
  46. package/scripts/build-shardfiles.d.mts +7 -0
  47. package/scripts/build-shardfiles.mjs +230 -0
  48. package/scripts/capture-hero.mjs +211 -0
  49. package/scripts/chromium-keychain.d.mts +1 -0
  50. package/scripts/chromium-keychain.mjs +7 -0
  51. package/scripts/entry-reads/crawl.ts +152 -0
  52. package/scripts/gen-host-catalogue.d.mts +14 -0
  53. package/scripts/gen-host-catalogue.mjs +108 -0
  54. package/scripts/gen-shards.d.mts +17 -0
  55. package/scripts/gen-shards.mjs +311 -0
  56. package/scripts/goldens/plot-inputs.d.mts +12 -0
  57. package/scripts/goldens/plot-inputs.mjs +46 -0
  58. package/scripts/gpu-texmem.mjs +98 -0
  59. package/scripts/ktx2-ab.mjs +111 -0
  60. package/scripts/map-bake-frames.d.mts +4 -0
  61. package/scripts/map-bake-frames.mjs +23 -0
  62. package/scripts/map-world.mjs +162 -0
  63. package/scripts/memory-product.mjs +62 -0
  64. package/scripts/music/export-midi.mjs +69 -0
  65. package/scripts/named-blob-output.d.mts +8 -0
  66. package/scripts/named-blob-output.mjs +48 -0
  67. package/scripts/practice/verify_unimate_skin.mjs +356 -0
  68. package/scripts/preparation-browser.mjs +25 -0
  69. package/scripts/preparation-seed.mjs +15 -0
  70. package/scripts/prepare-product.d.mts +14 -0
  71. package/scripts/prepare-product.mjs +133 -0
  72. package/scripts/prove-walk.mjs +83 -0
  73. package/scripts/push-tables.d.mts +1 -0
  74. package/scripts/push-tables.mjs +28 -0
  75. package/scripts/record-shard-mounts.mjs +133 -0
  76. package/scripts/section-inventory.ts +209 -0
  77. package/scripts/shard-build-pool.d.mts +4 -0
  78. package/scripts/shard-build-pool.mjs +53 -0
  79. package/scripts/shard-milestones.d.mts +15 -0
  80. package/scripts/shard-milestones.mjs +105 -0
  81. package/scripts/shard-progress.mjs +212 -0
  82. package/scripts/shardfile-cold-boot.mjs +79 -0
  83. package/scripts/shardfile-rolldown.d.mts +2 -0
  84. package/scripts/shardfile-rolldown.mjs +32 -0
  85. package/scripts/shardfile-vite.d.mts +4 -0
  86. package/scripts/shardfile-vite.mjs +20 -0
  87. package/scripts/site-facts.mjs +88 -0
  88. package/scripts/test-template-gate.mjs +44 -0
  89. package/scripts/tex-tiers.mjs +240 -0
  90. package/scripts/unused-assets.mjs +173 -0
  91. package/scripts/wildshard.d.mts +4 -0
  92. package/scripts/wildshard.mjs +146 -0
  93. package/scripts/witness-generation.d.mts +10 -0
  94. package/scripts/witness-generation.mjs +45 -0
  95. package/src/.oxlintrc.json +12 -0
  96. package/src/cli/bin.mjs +8 -0
  97. package/src/cli/content-command.ts +44 -0
  98. package/src/cli/content.ts +287 -0
  99. package/src/cli/grid/command.ts +48 -0
  100. package/src/cli/grid/grid.ts +155 -0
  101. package/src/cli/help.ts +262 -0
  102. package/src/cli/hero/adopt.ts +109 -0
  103. package/src/cli/install-home.ts +40 -0
  104. package/src/cli/main.ts +142 -0
  105. package/src/cli/version.ts +63 -0
  106. package/src/server/app.ts +19 -0
  107. package/src/server/serve.ts +49 -0
  108. package/src/sharddev/bake/json.mjs +84 -0
  109. package/src/sharddev/bake/mapRest.ts +57 -0
  110. package/src/sharddev/bake/nativeRaster.ts +106 -0
  111. package/src/sharddev/bake/source-loader.mjs +55 -0
  112. package/src/sharddev/bake/splatLayers.ts +29 -0
  113. package/src/sharddev/bootInventory.ts +132 -0
  114. package/src/sharddev/check.ts +125 -0
  115. package/src/sharddev/checkProject.ts +134 -0
  116. package/src/sharddev/cli.ts +207 -0
  117. package/src/sharddev/dev.ts +150 -0
  118. package/src/sharddev/devClient.ts +120 -0
  119. package/src/sharddev/devRoot.ts +6 -0
  120. package/src/sharddev/engineMusic.ts +23 -0
  121. package/src/sharddev/projectEntryProof.ts +32 -0
  122. package/src/sharddev/test/diskScriptScenario.ts +88 -0
  123. package/src/sharddev/test/scriptScenario.ts +254 -0
  124. package/src/sharddev/tsconfig.json +3 -0
  125. package/src/sharddev/watch/page.ts +288 -0
  126. package/src/sharddev/watch/pageEntry.ts +44 -0
  127. package/src/sharddev/watch/parity-baseline.json +22 -0
  128. package/src/sharddev/watch/parity.mjs +194 -0
  129. package/src/sharddev/watch/proof.mjs +199 -0
  130. package/src/sharddev/watch/socket.ts +57 -0
  131. package/src/sharddev/watch/watch.ts +382 -0
  132. package/src/shared/health.ts +10 -0
  133. package/src/tools/build-web.ts +59 -0
  134. package/src/web/app.tsx +53 -0
  135. package/src/web/index.html +12 -0
  136. package/src/web/main.tsx +10 -0
  137. package/tsconfig.author.json +25 -0
  138. package/tsconfig.json +47 -0
  139. package/README.md +0 -3
@@ -0,0 +1,111 @@
1
+ #!/usr/bin/env node
2
+ import { KEYCHAIN_ARGS } from './chromium-keychain.mjs';
3
+ import { debugSettings, harnessPins } from '@wildshard/engine/scripts/debug-settings.mjs';
4
+ // ktx2-ab.mjs — the E157 A/B: the same poses in each shard with the images and the KTX2 textures (the `textures` harness pin, G386),
5
+ // on the iPhone portrait the user judges on (iPhone 16 Pro UA, 390×844 @3, touch, the phone tier) — or, `--tier=desktop`
6
+ // (E173), the desktop's 1600×900 @1 landscape, the desktop tier. Every DOM layer but the
7
+ // game canvas is hidden, the clock is held (`tod` + a day of ~3 years), the weather clear; per pose the player is spawned,
8
+ // the loop runs 4 s, then stops drawing (game.frameGate) and the frozen frame is captured.
9
+ //
10
+ // Wind, animals and the weapon's idle sway still differ between two page loads, so each shard is captured TWICE with the
11
+ // images (`img`, `img2`): their difference is the run-to-run floor the KTX2 difference is read against (ktx2-ab-board.py).
12
+ //
13
+ // node scripts/ktx2-ab.mjs --url=http://localhost:4760 --out=<dir> [--shards=pine-hollow,nalati-grasslands,driftwood-isle] [--modes=img,img2,ktx2] [--tier=phone|desktop] [--viewport=phone]
14
+ //
15
+ // One headless Chromium on Metal, muted (`--mute-audio`, `mute=1`), closed at the end. Writes <out>/<shard>-<pose>-<mode>.png.
16
+ import { readShards } from '@wildshard/engine/scripts/shards.mjs';
17
+ import { mkdirSync } from 'node:fs';
18
+ import { resolve as resolvePath } from 'node:path';
19
+
20
+ const { chromium, devices } = await import("./browser-playwright.mjs");
21
+ const argv = process.argv.slice(2);
22
+ const flag = (n, d) => { const a = argv.find((x) => x.startsWith(`--${n}=`)); return a ? a.slice(n.length + 3) : d; };
23
+ const URL_BASE = flag('url', 'http://localhost:4760');
24
+ const OUT = resolvePath(flag('out', 'ktx2-ab'));
25
+ const manifests = await readShards();
26
+ const SHARDS = flag('shards', manifests.filter((m) => m.status === 'live' || m.status === 'earlyAccess').map((m) => m.slug).join(',')).split(',');
27
+ const MODES = flag('modes', 'img,img2,ktx2').split(',');
28
+ const TIER = flag('tier', 'phone');
29
+ /** --viewport=phone: the iPhone portrait frame whatever the tier (a desktop-tier board for the phone-portrait review) */
30
+ const VIEW = flag('viewport', TIER);
31
+ mkdirSync(OUT, { recursive: true });
32
+
33
+ /** per shard: the held time of day and the poses. `x, z, yaw` spawn the player (yaw faces (-sin, -cos)); `pitch` looks down */
34
+ const SHARD = {
35
+ 'pine-hollow': {
36
+ q: 'tod=0.45&weather=clear',
37
+ poses: [
38
+ { id: 'hollow', x: 6, z: 4, yaw: 2.8, pitch: -0.08 },
39
+ { id: 'ground', x: 6, z: 4, yaw: 2.8, pitch: -1.05 },
40
+ { id: 'stones', x: 140, z: -30, yaw: -1.5708, pitch: -0.2 },
41
+ { id: 'bark', x: 118, z: -118, yaw: 3.6, pitch: 0.05 },
42
+ ],
43
+ },
44
+ 'nalati-grasslands': {
45
+ q: 'tod=0.45&weather=clear',
46
+ poses: [
47
+ { id: 'spawn', pitch: -0.05 },
48
+ { id: 'ground', pitch: -1.0 },
49
+ { id: 'turn', yawAdd: 2.2, pitch: -0.25 },
50
+ ],
51
+ },
52
+ 'driftwood-isle': {
53
+ q: 'tod=0.45',
54
+ poses: [
55
+ { id: 'spawn', pitch: -0.05 },
56
+ { id: 'sand', pitch: -1.0 },
57
+ { id: 'turn', yawAdd: 2.6, pitch: -0.2 },
58
+ ],
59
+ },
60
+ };
61
+
62
+ const sleep = (ms) => new Promise((resolve) => { setTimeout(resolve, ms); });
63
+ const browser = await chromium.launch({ args: [...KEYCHAIN_ARGS, '--mute-audio', '--use-angle=metal', '--ignore-gpu-blocklist'] });
64
+ try {
65
+ const iphone = devices['iPhone 16 Pro'];
66
+ for (const shard of SHARDS) {
67
+ const spec = SHARD[shard] ?? { q: 'tod=0.45', poses: [{ id: 'spawn', pitch: -0.05 }] };
68
+ for (const mode of MODES) {
69
+ const tex = mode.startsWith('img') ? 'img' : 'ktx2';
70
+ const ctx = await browser.newContext(VIEW === 'phone'
71
+ ? { userAgent: iphone.userAgent, isMobile: true, hasTouch: true, deviceScaleFactor: 3, viewport: { width: 390, height: 844 } }
72
+ : { viewport: { width: 1600, height: 900 }, deviceScaleFactor: 1 });
73
+ // the textures are a harness pin (`__wildshardHarness.textures`, G386), never a setting or URL switch; no background prefetch
74
+ await debugSettings(ctx, { prefetch: 'off' });
75
+ await harnessPins(ctx, { textures: tex });
76
+ const page = await ctx.newPage();
77
+ const errors = [];
78
+ page.on('pageerror', (e) => errors.push(e.message.slice(0, 200)));
79
+ page.on('console', (m) => { if (m.type() === 'error') errors.push(m.text().slice(0, 200)); });
80
+ const url = `${URL_BASE}/?chunk=${shard}&mute=1&skipintro=1&nolock=1&sw=0&tier=${TIER}${TIER === 'phone' ? '&touch=1' : ''}&clock=100000000&${spec.q}`;
81
+ await page.goto(url, { waitUntil: 'domcontentloaded' });
82
+ await page.waitForFunction(() => Boolean(window.__wildshard?.world?.game && window.__wildshard.world.player), undefined, { timeout: 300000, polling: 500 });
83
+ await sleep(8000);
84
+ const start = await page.evaluate(() => {
85
+ const w = window.__wildshard?.world;
86
+ const st = document.createElement('style');
87
+ st.textContent = 'body *{visibility:hidden!important} canvas.__game{visibility:visible!important}';
88
+ w.game.renderer.domElement.classList.add('__game');
89
+ document.head.append(st);
90
+ window.__gate = w.game.frameGate;
91
+ return { x: w.player.position.x, z: w.player.position.z, yaw: w.player.yaw ?? 0 };
92
+ });
93
+ for (const p of spec.poses) {
94
+ await page.evaluate(([pp, s]) => {
95
+ const w = window.__wildshard?.world;
96
+ w.game.frameGate = window.__gate;
97
+ w.player.spawn(pp.x ?? s.x, pp.z ?? s.z, (pp.yaw ?? s.yaw) + (pp.yawAdd ?? 0));
98
+ w.player.pitch = pp.pitch ?? 0;
99
+ }, [p, start]);
100
+ await sleep(4000);
101
+ await page.evaluate(() => { window.__wildshard.world.game.frameGate = () => false; });
102
+ await sleep(250);
103
+ await page.screenshot({ path: resolvePath(OUT, `${shard}-${p.id}-${mode}.png`) });
104
+ }
105
+ console.log(`[ab] ${shard} ${mode}: ${spec.poses.length} poses${errors.length > 0 ? ` · errors: ${errors.slice(0, 3).join(' / ')}` : ''}`);
106
+ await ctx.close();
107
+ }
108
+ }
109
+ } finally {
110
+ await browser.close();
111
+ }
@@ -0,0 +1,4 @@
1
+ import type {JSHandle} from 'playwright';
2
+ import type {ByteSinkPage} from '@wildshard/engine/scripts/bake-output.mjs';
3
+ /** Save native map passes and always retire their owned handles/routes. */
4
+ export function saveMapFrames<T extends {colour:HTMLCanvasElement;height:HTMLCanvasElement|null}>(page:ByteSinkPage,handle:JSHandle<T>,colourFile:string,heightFile:string):Promise<Omit<T,'colour'|'height'>&{hasHeight:boolean}>;
@@ -0,0 +1,23 @@
1
+ import { saveCanvasPng } from '@wildshard/engine/scripts/parity/canvas-bytes.mjs';
2
+
3
+ /** Retain colour/height snapshots locally; only map metadata crosses JSON.
4
+ * @template {{colour:HTMLCanvasElement,height:HTMLCanvasElement|null}} T
5
+ * @param {import('@wildshard/engine/scripts/bake-output.mjs').ByteSinkPage} page
6
+ * @param {import('playwright').JSHandle<T>} handle
7
+ * @param {string} colourFile
8
+ * @param {string} heightFile
9
+ * @returns {Promise<Omit<T,'colour'|'height'>&{hasHeight:boolean}>} */
10
+ export async function saveMapFrames(page, handle, colourFile, heightFile) {
11
+ try {
12
+ const metadata = await handle.evaluate(({ colour: _colour, height, ...rest }) => ({ ...rest, hasHeight: height !== null }));
13
+ await saveCanvasPng(page, await handle.getProperty('colour'), colourFile);
14
+ if (metadata.hasHeight) {
15
+ const height = await handle.evaluateHandle(frame => {
16
+ if (frame.height === null) throw new Error('Missing map height canvas');
17
+ return frame.height;
18
+ });
19
+ await saveCanvasPng(page, height, heightFile);
20
+ }
21
+ return metadata;
22
+ } finally { await handle.dispose(); }
23
+ }
@@ -0,0 +1,162 @@
1
+ // Build-only rest assembly of admitted model, private-scene and frozen-weld data.
2
+ import { resolve } from 'node:path';
3
+ import { readFileSync } from 'node:fs';
4
+ import { inflateSync } from 'node:zlib';
5
+ import { readBakedGeometry } from '@wildshard/sdk/kit/bakedGeometry';
6
+ import { _applyChunkConstants } from '@wildshard/engine/core/config';
7
+ import { configureLevel } from '@wildshard/engine/level/selection';
8
+ import { placeForest } from '@wildshard/engine/world/forest/placement';
9
+
10
+ import { BufferGeometry, Euler, Float32BufferAttribute, Matrix4, Quaternion, Vector3 } from 'three';
11
+ import { MapRaster } from '@wildshard/sdk/bake/mapRaster';
12
+ import { readMapGeometry } from '@wildshard/engine/scripts/map-painter.mjs';
13
+ import { mapModel, readMapJson } from '@wildshard/sdk/scripts/map-geometry.mjs';
14
+
15
+ export const mapPose = pose => pose.matrix === undefined ? new Matrix4().compose(new Vector3(pose.x, pose.y, pose.z),
16
+ new Quaternion().setFromEuler(new Euler(pose.yxz.pitch ?? 0, pose.yxz.yawAtan2 === undefined ? pose.yxz.yaw : Math.atan2(...pose.yxz.yawAtan2), pose.yxz.roll ?? 0, 'YXZ')),
17
+ new Vector3().setScalar(pose.yxz.scale)) : new Matrix4().fromArray(pose.matrix);
18
+ const words = (values, wide = false) => wide ? new Float64Array(Uint32Array.from(values).buffer) : new Float32Array(Uint32Array.from(values).buffer);
19
+ const localMatrix = node => node.transformBits !== undefined && node.matrixAutoUpdate !== false ? (() => {
20
+ const f = words(node.transformBits, true);
21
+ return new Matrix4().compose(new Vector3().fromArray(f), new Quaternion().fromArray(f, 3), new Vector3().fromArray(f, 7));
22
+ })() : node.matrixBits === undefined ? new Matrix4() : new Matrix4().fromArray(words(node.matrixBits, true));
23
+
24
+ export class MapWorld {
25
+ constructor(assembly, base, section, catalogue, blobs) {
26
+ this.assembly = assembly; this.base = base; this.section = section; this.catalogue = catalogue;
27
+ this.files = new Map(blobs.map(row => row.file === undefined ? [row.name, resolve(base, row.source)] : [row.file.name, (row.file.kind ?? 'json') === 'json' ? JSON.parse(new TextDecoder().decode(row.bytes)) : row.bytes]));
28
+ this.geometry = new Map(); this.frames = new Map();
29
+ }
30
+ json(file) { const source = this.files.get(file); if (source === undefined || source instanceof Uint8Array) throw new Error(`Missing map world JSON ${file}`); return typeof source === 'string' ? readMapJson(source) : source; }
31
+ /** A scene part's instance matrices: an instanceMatrix row (inline or a float32 product packet), else the recipe's. */
32
+ matrices(step, id, node) {
33
+ const row = (step.instanceAttributes ?? []).find(entry => entry.part === id && entry.target === 'instanceMatrix');
34
+ if (row === undefined) return node.matrices; if (row.values !== undefined) return row.values;
35
+ const bytes = this.files.get(row.packet.file); if (!(bytes instanceof Uint8Array)) throw new Error(`Missing map world packet ${row.packet.file}`);
36
+ return new Float32Array(bytes.buffer.slice(bytes.byteOffset + row.packet.byteOffset, bytes.byteOffset + row.packet.byteOffset + row.packet.count * 64));
37
+ }
38
+ async shape(ref) {
39
+ const key = `${ref.library}/${ref.model}`;
40
+ let geometry = this.geometry.get(key);
41
+ if (geometry !== undefined) return geometry.clone();
42
+ const library = this.section.libraries.find(row => row.id === ref.library);
43
+ const model = library?.models.find(row => row.id === ref.model), file = this.files.get(typeof model?.file === 'string' ? model.file : model?.file?.desktop);
44
+ if (typeof file !== 'string' && !(file instanceof Uint8Array)) throw new Error(`Missing map world geometry ${key}`);
45
+ geometry = await mapModel(file); this.geometry.set(key, geometry); return geometry.clone();
46
+ }
47
+ ghost(id, locals = []) {
48
+ if (id === '$shadow') return true;
49
+ const material = locals.find(row => row.id === id) ?? this.section.materials.find(row => row.id === id);
50
+ return material?.properties?.depthWrite === false || material?.properties?.colorWrite === false;
51
+ }
52
+ async placement(row) {
53
+ if (row.drawnInto !== undefined) return;
54
+ const model = this.catalogue.models.find(entry => entry.key === row.model);
55
+ if (model === undefined) throw new Error(`Missing map world model ${row.model}`);
56
+ const groups = new Map();
57
+ for (const pose of row.poses) {
58
+ const variant = (model.variants ?? []).find(entry => entry.id === pose.variant);
59
+ const params = { ...model.defaults, ...variant?.params, ...pose.params };
60
+ const choice = model.choices.find(entry => Object.entries(entry.params).every(([key, value]) => JSON.stringify(params[key]) === JSON.stringify(value)));
61
+ if (choice === undefined) throw new Error(`Missing map choice ${row.id}`);
62
+ if (choice.scene !== undefined) throw new Error(`Placed frozen scene needs an explicit map rest graph: ${row.id}`);
63
+ const matrices = groups.get(choice) ?? []; matrices.push(mapPose(pose)); groups.set(choice, matrices);
64
+ }
65
+ for (const [choice, matrices] of groups) for (const draw of choice.draws) {
66
+ if (draw.depthOnly === true || this.ghost(draw.material)) continue;
67
+ this.assembly.add(`place:${row.id}:${draw.id}`, await this.shape(draw), matrices);
68
+ }
69
+ }
70
+ async batch(row) {
71
+ for (const [i, copy] of row.fixed.entries()) this.assembly.add(`batch:${row.id}:${i}`, await this.shape(row.geometry[copy.lod0]), [new Matrix4().fromArray(copy.matrix)]);
72
+ }
73
+ async weld(graph) {
74
+ const frames = new Map([[graph.root, this.frames.get(graph.root) ?? new Matrix4()]]), visible = new Map([[graph.root, true]]);
75
+ for (const node of graph.nodes) {
76
+ const parent = frames.get(node.parent) ?? this.frames.get(node.parent);
77
+ if (parent === undefined) throw new Error(`Missing map weld parent ${node.parent}`);
78
+ const matrix = new Matrix4().multiplyMatrices(parent, localMatrix(node)); frames.set(node.id, matrix); this.frames.set(node.id, matrix);
79
+ const live = (visible.get(node.parent) ?? true) && node.visible !== false; visible.set(node.id, live);
80
+ // Views partition a batch's bounds; the batch already contains all draws.
81
+ if (!live || !['mesh', 'batch', 'instances'].includes(node.kind) || this.ghost(node.material, graph.materials)) continue;
82
+ const matrices = node.kind === 'instances' ? Array.from({ length: node.count }, (_, i) => matrix.clone().multiply(new Matrix4().fromArray(words(node.matrixWords), i * 16))) : [matrix];
83
+ this.assembly.add(`weld:${graph.id}:${node.id}`, await this.shape(node.geometry), matrices);
84
+ }
85
+ }
86
+ async recipe(step, root = new Matrix4(), omitted = []) {
87
+ const recipe = this.json(step.scene), scene = recipe.scene ?? recipe;
88
+ if ((recipe.imports ?? []).length > 0) throw new Error(`Map world recipe imports need explicit rest data: ${step.id}`);
89
+ const nodes = new Map(scene.nodes.map(row => [row.id, row]));
90
+ const walk = async (id, parent, visible) => {
91
+ const node = nodes.get(id); if (node === undefined) throw new Error(`Missing map recipe node ${id}`);
92
+ const local = node.matrix === undefined ? new Matrix4().compose(new Vector3(...node.position ?? [0, 0, 0]), node.quaternion === undefined ? new Quaternion().setFromEuler(new Euler(...node.rotation ?? [0, 0, 0], node.order ?? 'XYZ')) : new Quaternion(...node.quaternion), new Vector3(...node.scale ?? [1, 1, 1])) : new Matrix4().fromArray(node.matrix);
93
+ const matrix = parent.clone().multiply(local), live = visible && node.visible !== false;
94
+ if (node.kind === 'mesh' && live && !omitted.includes(id) && !this.ghost(step.materials[node.material])) {
95
+ const ref = step.geometry[node.geometry]; if (ref === undefined) throw new Error(`Missing map recipe geometry ${node.geometry}`);
96
+ const instanced = this.matrices(step, id, node);
97
+ this.assembly.add(`scene:${step.id}:${id}`, await this.shape(ref), instanced === undefined ? [matrix] : Array.from({ length: node.instances }, (_, i) => matrix.clone().multiply(new Matrix4().fromArray(instanced, i * 16))));
98
+ } else if (!['mesh', 'group', 'anchor', 'light'].includes(node.kind)) throw new Error(`Unsupported map recipe ${node.kind}`);
99
+ for (const child of node.children ?? []) await walk(child, matrix, live);
100
+ };
101
+ await walk(scene.root, root, true);
102
+ }
103
+ dispose() { for (const geometry of this.geometry.values()) geometry.dispose(); }
104
+ }
105
+
106
+ /** Retain the renderer's original grid and float32 coordinates. Colour is a
107
+ * map palette only; the underlying triangle positions are never re-sampled. */
108
+ export function mapGround(source, resolution, size, palette, hole) {
109
+ // Same rotated PlaneGeometry lattice as Terrain. Plane's -Z row order matters.
110
+ const geometry = new BufferGeometry(), points = [], colours = [], indices = [];
111
+ for (let z = 0; z < resolution; z++) for (let x = 0; x < resolution; x++) {
112
+ const px = Math.fround(x * size / (resolution - 1) - size / 2), pz = Math.fround(z * size / (resolution - 1) - size / 2);
113
+ points.push(px, Math.fround(source.heightAt(px, pz)), pz);
114
+ const splat = source.splatAt(px, pz), rgb = [0, 0, 0];
115
+ for (let i = 0; i < 4; i++) for (let c = 0; c < 3; c++) rgb[c] += splat[i] * palette[i][c];
116
+ colours.push(...rgb);
117
+ }
118
+ for (let z = 0; z < resolution - 1; z++) for (let x = 0; x < resolution - 1; x++) {
119
+ const a = z * resolution + x;
120
+ for (const ids of [[a, a + resolution, a + 1], [a + resolution + 1, a + 1, a + resolution]]) if (hole === undefined || !hole(ids.map(i => points.slice(i * 3, i * 3 + 3)))) indices.push(...ids);
121
+ }
122
+ geometry.setAttribute('position', new Float32BufferAttribute(points, 3)); geometry.setAttribute('color', new Float32BufferAttribute(colours, 3)); geometry.setIndex(indices); return geometry;
123
+ }
124
+
125
+ /** Decode all local templates once; node transforms belong to their declared
126
+ * world placements, while raw imported scenes explicitly request world=true. */
127
+ export async function mapPool(file, world = false) {
128
+ const doc = await new MapRaster(1, {}, readMapGeometry).document(file), result = new Map();
129
+ for (const node of doc.getRoot().listNodes()) {
130
+ const mesh = node.getMesh(); if (mesh === null) continue;
131
+ if (mesh.listPrimitives().length !== 1) throw new Error(`Map pool requires one primitive ${file}`);
132
+ const primitive = mesh.listPrimitives()[0], geometry = new BufferGeometry();
133
+ for (const [semantic, name] of [['POSITION', 'position'], ['COLOR_0', 'color']]) {
134
+ const attribute = primitive.getAttribute(semantic); if (attribute === null) continue;
135
+ const data = []; for (let i = 0; i < attribute.getCount(); i++) data.push(...attribute.getElement(i, []).slice(0, 3));
136
+ geometry.setAttribute(name, new Float32BufferAttribute(data, 3));
137
+ }
138
+ const indices = primitive.getIndices(); if (indices !== null) geometry.setIndex(Array.from(indices.getArray()));
139
+ if (world) geometry.applyMatrix4(new Matrix4().fromArray(node.getWorldMatrix()));
140
+ const id = node.getName() === '' ? mesh.getName() : node.getName(); if (result.has(id)) throw new Error(`Duplicate map pool geometry ${id}`); result.set(id, geometry);
141
+ }
142
+ return result;
143
+ }
144
+
145
+ export function mapForest(constants, field, forest, variants) {
146
+ _applyChunkConstants(constants);
147
+ configureLevel({ id: constants.slug, ground: { terrain: field }, forest });
148
+ return placeForest(variants).trees;
149
+ }
150
+
151
+ export function mapBinary(file, shuffled = false) {
152
+ const bytes = new Uint8Array(inflateSync(readFileSync(file)));
153
+ if (!shuffled) return bytes;
154
+ const out = new Uint8Array(bytes.length), n = bytes.length / 4;
155
+ for (let b = 0; b < 4; b++) for (let i = 0; i < n; i++) out[i * 4 + b] = bytes[b * n + i];
156
+ return out;
157
+ }
158
+ export function mapGeometry(bytes, offset, row) {
159
+ const geometry = readBakedGeometry(bytes, offset, row);
160
+ if (!row.expand || geometry.index === null) return geometry;
161
+ const expanded = geometry.toNonIndexed(); geometry.dispose(); return expanded;
162
+ }
@@ -0,0 +1,62 @@
1
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
2
+ import { createRequire } from 'node:module';
3
+ import { dirname, resolve } from 'node:path';
4
+ import { pathToFileURL } from 'node:url';
5
+ import { clientPage } from '@wildshard/sdk/internal/clientPage';
6
+ import { preparationPreview } from '@wildshard/sdk/scripts/preparation-preview.mjs';
7
+ import { fetchPreparationClient, preparationClient } from '../src/sharddev/devClient.ts';
8
+
9
+ const ENGINE = '@wildshard/engine';
10
+ const PROBE = `${ENGINE}/scripts/boot-memory-peak.mjs`;
11
+
12
+ function installedEngine() {
13
+ const callerManifest = resolve('package.json');
14
+ const caller = createRequire(callerManifest);
15
+ const sdk = createRequire(import.meta.url);
16
+ const callerEngine = existsSync(callerManifest) ? resolveEngine(caller) : undefined;
17
+ const engine = callerEngine || sdk.resolve(`${ENGINE}/package.json`);
18
+ const manifest = JSON.parse(readFileSync(engine, 'utf8'));
19
+ const pin = callerEngine ? JSON.parse(readFileSync(callerManifest, 'utf8')).dependencies?.[ENGINE] ?? manifest.version : manifest.version;
20
+ try {
21
+ return { pin, probe: (callerEngine ? caller : sdk).resolve(PROBE) };
22
+ } catch (error) {
23
+ throw new Error(`wildshard memory: engine ${pin} does not export the boot memory probe`, { cause: error });
24
+ }
25
+ }
26
+
27
+ function resolveEngine(require) {
28
+ try { return require.resolve(`${ENGINE}/package.json`); }
29
+ catch (error) {
30
+ if (error?.code !== 'MODULE_NOT_FOUND') throw error;
31
+ return undefined;
32
+ }
33
+ }
34
+
35
+ async function memoryProduct(folder, out) {
36
+ const { pin, probe } = installedEngine();
37
+ const { measureBootMemory, bootMemoryVerdict, bootMemoryLine } = await import(pathToFileURL(probe).href);
38
+ const client = process.env.WILDSHARD_PREPARATION_CLIENT ? await preparationClient() : await fetchPreparationClient({ pin });
39
+ const preview = await preparationPreview(client, folder, { clientPage });
40
+ try {
41
+ const run = await measureBootMemory({ url: preview.url, chunk: '' });
42
+ const slug = preview.slug;
43
+ if (!run.ready) throw new Error(`${slug} did not reach its world: ${run.failure ?? 'no error card'}`);
44
+ const verdict = bootMemoryVerdict(run.samples, run.dumps);
45
+ const line = bootMemoryLine(slug, verdict);
46
+ if (out !== undefined) {
47
+ const path = resolve(out);
48
+ mkdirSync(dirname(path), { recursive: true });
49
+ writeFileSync(path, `${JSON.stringify({ ...run, chunk: slug, engine: pin, dumps: Object.fromEntries(run.dumps), verdict, line }, null, 1)}\n`);
50
+ }
51
+ console.log(line);
52
+ return verdict.pass ? 0 : 1;
53
+ } finally {
54
+ await preview.close();
55
+ }
56
+ }
57
+
58
+ const [folder, output, ...extra] = process.argv.slice(2);
59
+ if (!folder || folder.startsWith('-') || extra.length > 0 || (output !== undefined && !/^--out=.+$/u.test(output))) {
60
+ throw new Error('Usage: wildshard memory <folder> [--out=<report.json>]');
61
+ }
62
+ process.exitCode = await memoryProduct(folder, output?.slice('--out='.length));
@@ -0,0 +1,69 @@
1
+ // The score (src/client/game/audio/theme.ts) → a standard MIDI file, one track per layer, for a composer to take further.
2
+ // node scripts/music/export-midi.mjs [theme|trailer30|trailer15] [out.mid] default: theme → scripts/music/wildshard-theme.mid
3
+ // drone and pad are derived from the chord track (root D2 + D3 held; the pad's four voices per chord); pulse is a GM drum
4
+ // track on channel 10 (36 kick · 35 four-on-the-floor kick · 38 tap · 42 shaker). Tempo changes per segment; the theme's
5
+ // loop (D → B) is written once, followed by the ring-out.
6
+ import { writeFileSync } from 'node:fs';
7
+ import { resolve } from 'node:path';
8
+ import '@wildshard/engine/scripts/format-hook.mjs';
9
+ const { ARRANGEMENTS, CHORDS } = await import('@wildshard/engine/game/audio/theme');
10
+
11
+ const here = import.meta.dirname;
12
+ const name = process.argv[2] ?? 'theme';
13
+ const out = resolve(process.argv[3] ?? `${here}/wildshard-${name}.mid`);
14
+
15
+ // The defining score is installed TypeScript; the shared hook strips its type-only declarations.
16
+ const arr = Object.entries(ARRANGEMENTS).find(([key]) => key === name)?.[1];
17
+ if (!arr) throw new Error(`no arrangement ${name}`);
18
+
19
+ const PPQ = 480;
20
+ const LAYERS = ['drone', 'pad', 'pluck', 'marimba', 'bass', 'pulse', 'bell'];
21
+ const PROGRAM = { drone: 89, pad: 88, pluck: 24, marimba: 12, bass: 38, bell: 14 }; // GM: pad 2 (warm), new age, nylon guitar, marimba, synth bass 1, tubular bells
22
+ const CHANNEL = { drone: 0, pad: 1, pluck: 2, marimba: 3, bass: 4, pulse: 9, bell: 5 };
23
+
24
+ // events per track: { tick, bytes }
25
+ const tracks = Object.fromEntries(LAYERS.map((l) => [l, []]));
26
+ const tempoTrack = [];
27
+ const vlq = (n) => { let v = n; const b = [v & 0x7f]; while ((v >>= 7) > 0) b.unshift((v & 0x7f) | 0x80); return b; };
28
+ const note = (layer, tick, dur, n, v = 0.8) => {
29
+ const ch = CHANNEL[layer], vel = Math.max(1, Math.min(127, Math.round(v * 110)));
30
+ tracks[layer].push({ tick, bytes: [0x90 | ch, n, vel] }, { tick: tick + Math.max(1, Math.round(dur)), bytes: [0x80 | ch, n, 0] });
31
+ };
32
+
33
+ let tick = 0;
34
+ const segs = arr.tailFrom !== undefined ? arr.segments : arr.segments; // the loop is written once
35
+ for (const seg of segs) {
36
+ const usPerBeat = Math.round(60e6 / seg.bpm);
37
+ tempoTrack.push({ tick, bytes: [0xff, 0x51, 0x03, (usPerBeat >> 16) & 0xff, (usPerBeat >> 8) & 0xff, usPerBeat & 0xff] });
38
+ const T = (b) => tick + Math.round(b * PPQ);
39
+ const segTicks = Math.round(seg.beats * PPQ);
40
+ // chords → pad (held to the next chord) and drone (root pedal for the segment)
41
+ seg.chords.forEach((c, i) => {
42
+ const end = i + 1 < seg.chords.length ? seg.chords[i + 1].t : seg.beats;
43
+ for (const p of CHORDS[c.chord]) note('pad', T(c.t), (end - c.t) * PPQ, p, 0.6);
44
+ });
45
+ if (seg.chords.length > 0 || seg.beats > 0) { note('drone', tick, segTicks, 38, 0.5); note('drone', tick, segTicks, 50, 0.4); }
46
+ for (const layer of ['pluck', 'marimba', 'bass', 'pulse', 'bell']) for (const n of seg.notes[layer] ?? []) note(layer, T(n.t), n.d * PPQ, n.n, n.v ?? 0.8);
47
+ tick += segTicks;
48
+ }
49
+
50
+ const str = (s) => [...Buffer.from(s, 'utf8')];
51
+ const meta = (type, data) => [0xff, type, ...vlq(data.length), ...data];
52
+ const chunk = (id, body) => [...str(id), (body.length >>> 24) & 0xff, (body.length >>> 16) & 0xff, (body.length >>> 8) & 0xff, body.length & 0xff, ...body];
53
+ const trackBytes = (events, head = []) => {
54
+ events.sort((a, b) => a.tick - b.tick || (a.bytes[0] & 0xf0) - (b.bytes[0] & 0xf0)); // note-offs before note-ons at the same tick
55
+ const body = [...head]; let last = 0;
56
+ for (const e of events) { body.push(...vlq(e.tick - last), ...e.bytes); last = e.tick; }
57
+ body.push(0x00, 0xff, 0x2f, 0x00);
58
+ return chunk('MTrk', body);
59
+ };
60
+ const file = [...chunk('MThd', [0, 1, 0, LAYERS.length + 1, (PPQ >> 8) & 0xff, PPQ & 0xff])];
61
+ file.push(...trackBytes(tempoTrack, [0x00, ...meta(0x03, str(`Wildshard — ${arr.name}`)), 0x00, ...meta(0x58, [4, 2, 24, 8])]));
62
+ for (const l of LAYERS) {
63
+ const head = [0x00, ...meta(0x03, str(l))];
64
+ if (PROGRAM[l] !== undefined) head.push(0x00, 0xc0 | CHANNEL[l], PROGRAM[l]);
65
+ file.push(...trackBytes(tracks[l], head));
66
+ }
67
+ writeFileSync(out, Buffer.from(file));
68
+ const notes = Object.values(tracks).reduce((a, t) => a + t.length / 2, 0);
69
+ console.log(`${arr.name}: ${segs.length} segments, ${notes} notes, ${(tick / PPQ).toFixed(0)} beats → ${out} (${file.length} bytes)`);
@@ -0,0 +1,8 @@
1
+ import type { ScriptContract } from '@wildshard/format/scriptContract';
2
+ import type { NamedBlobKind } from '@wildshard/sdk/blobs';
3
+
4
+ /** Register exact bytes as cached author-project inputs; directory is assets or a bounded subdirectory. */
5
+ export function namedBlobOutput(project: URL, label: string, directory?: string): {
6
+ readonly add: (name: string, kind: NamedBlobKind, bytes: Uint8Array, url?: string, script?: ScriptContract, jsonSchema?: Readonly<Record<string, unknown>>) => string;
7
+ readonly finish: (metadata: URL) => void;
8
+ };
@@ -0,0 +1,48 @@
1
+ import { fileURLToPath } from 'node:url';
2
+ import { createRequire } from 'node:module';
3
+ import { nameBlobBytes } from '@wildshard/sdk/blobBytes';
4
+ import { validateNamedJson } from '@wildshard/sdk/schemaValidation';
5
+ import { byteWriter } from '@wildshard/engine/scripts/bake-output.mjs';
6
+
7
+ const requireSchema = createRequire(new URL('../src/sharddev/package.json', import.meta.url));
8
+ /** @param {Uint8Array} schemaBytes @param {Uint8Array} bytes */
9
+ function validateJson(schemaBytes, bytes) {
10
+ const Ajv = /** @type {Parameters<typeof validateNamedJson>[2]} */ (requireSchema('ajv'));
11
+ return validateNamedJson(schemaBytes, bytes, Ajv);
12
+ }
13
+
14
+ /** Build-only bridge from a producer's exact bytes to author-project blob inputs.
15
+ * Equal payloads have one canonical logical name; URL aliases never add a second index reference.
16
+ * A bounded assets/ subdirectory isolates producer ownership when payloads also exist in another job.
17
+ * @param {URL} project @param {string} label @param {string} [directory]
18
+ * @returns {ReturnType<import('./named-blob-output.d.mts').namedBlobOutput>} */
19
+ export function namedBlobOutput(project, label, directory = 'assets') {
20
+ if (!/^assets(?:\/[A-Za-z0-9_-]+)*$/u.test(directory) || directory.length > 120) throw new Error('Invalid blob source directory');
21
+ const writer = byteWriter(process.argv.includes('--check'), label);
22
+ /** @type {Map<string,NonNullable<import('@wildshard/sdk/projectOutput').ProjectOutput['blobs']>[number]>} */
23
+ const rows = new Map();
24
+ /** @type {Map<string,string>} */
25
+ const aliases = new Map();
26
+ return {
27
+ add(name, kind, bytes, url, script, jsonSchema) {
28
+ const options = { ...(script === undefined ? {} : { script }), ...(jsonSchema === undefined ? {} : { jsonSchema }) };
29
+ const blob = nameBlobBytes(name, kind, bytes, options, validateJson), old = rows.get(blob.file.hash);
30
+ if (old !== undefined && old.kind !== kind) throw new Error('Blob source changes kind');
31
+ if (old !== undefined && JSON.stringify(old.options?.script) !== JSON.stringify(blob.script)) throw new Error('Blob source changes script contract');
32
+ if (old !== undefined && JSON.stringify(old.options?.jsonSchema) !== JSON.stringify(blob.file.jsonSchema)) throw new Error('Blob source changes JSON schema');
33
+ const row = old ?? { name, kind, source: `${directory}/${blob.file.hash}`, ...(Object.keys(options).length === 0 ? {} : { options }) };
34
+ if (old === undefined && [...rows.values()].some(value => value.name === name)) throw new Error('Duplicate logical blob source name');
35
+ if (url !== undefined && aliases.has(url) && aliases.get(url) !== row.name) throw new Error('Blob URL changes content');
36
+ rows.set(blob.file.hash, row);
37
+ if (url !== undefined) aliases.set(url, row.name);
38
+ writer.put(fileURLToPath(new URL(row.source, project)), blob.bytes);
39
+ return row.name;
40
+ },
41
+ finish(metadata) {
42
+ const blobs = [...rows.values()].sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
43
+ const byUrl = Object.fromEntries([...aliases].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0));
44
+ writer.put(fileURLToPath(metadata), `${JSON.stringify({ blobs, byUrl }, null, 2)}\n`);
45
+ writer.finish();
46
+ },
47
+ };
48
+ }