@wildshard/dev 0.0.0-stage → 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 +101 -3
  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,331 @@
1
+ #!/usr/bin/env node
2
+ import { shardManifestPath } from '@wildshard/engine/scripts/authored-manifest.mjs';
3
+ import { decisionLegacyLines } from '@wildshard/build/scripts/decision-legacy-lines.mjs';
4
+ // G342: bake one shard's near placeholder (public/assets/baked/<slug>/near.glb + near.json). Its drawn static world is
5
+ // built in Node from the shard's own authored world through the bake world host, as the physics bakes build it (no
6
+ // browser, no game page), harvested as prototypes with their placed copies, reduced by the SDK producer
7
+ // (`@wildshard/sdk/bake/nearPlaceholder`) to a low-LOD budget, and merged over its terrain at L1 density (17 × 17 per
8
+ // 125 m region) in its own far look: one draw, regions in TEXCOORD_0.x, no colliders.
9
+ // node --experimental-transform-types --import ./scripts/bake-loader.mjs scripts/bake/near-placeholder.mjs <slug> [--report]
10
+ //
11
+ // G348: why this runner stays beside `wildshard build` (which bakes any shardfile's near row from its own terrain
12
+ // collider and its L1 tiles' meshes, `projectPlaceholders`): the first-party hybrids (Driftwood, Nalati, Pine) have no
13
+ // world in their shardfiles yet (no terrain, no tiles; `public/shardfiles/<slug>/shard.json`). Their props and forests
14
+ // exist only when their TypeScript builders run, so the SDK build has nothing to harvest, and no shardfile input can
15
+ // reproduce these bytes (the coarsest built LOD of every placement, Pine's per-tree placements, the colour maps' mean
16
+ // texels). Nothing here is per shard (each declares its ground and tree stand-ins in data/placeholder.json); the grid
17
+ // prefers a shardfile's own `near` row and reads this runner's near.json only for a shard whose shardfile has none. It
18
+ // retires per shard as each one's world moves into its shardfile tiles.
19
+ import { spawnSync } from 'node:child_process';
20
+ import { createHash } from 'node:crypto';
21
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
22
+ import { resolve } from 'node:path';
23
+ import { pathToFileURL } from 'node:url';
24
+ import { installBakeEnvironment } from '@wildshard/sdk/scripts/bake/environment.mjs';
25
+ import { visitAuthoredWorld } from '@wildshard/sdk/scripts/bake/worldHost.mjs';
26
+ import { prepareNativeScalarBlocks } from '@wildshard/sdk/scripts/bake/nativeWorld.mjs';
27
+ import { placeholderGridBytes, placeholderOf } from '@wildshard/sdk/scripts/bake/placeholderShards.mjs';
28
+
29
+ const slug = process.argv.slice(2).find(value => !value.startsWith('--'));
30
+ if (typeof slug !== 'string') throw new Error('usage: near-placeholder.mjs <slug> [--report] [--ground-error=<m>] [--resident=<bytes>]');
31
+ const report = process.argv.includes('--report');
32
+ const root = resolve(import.meta.dirname, '../..');
33
+ const at = path => import(pathToFileURL(resolve(root, path)).href);
34
+ // a shardfile project (its shard.config.ts): what `wildshard build` reads, for its tiles, its look and its terrain; read
35
+ // before the bake environment's browser stand-ins exist (its behaviour compiler takes them for a browser)
36
+ // (its generation input roots as the author CLI declares them, scripts/wildshard.mjs: a generator listing its own files needs them)
37
+ const folder = resolve(root, 'src/shards', slug), manifestSource = { folder: slug, ...(existsSync(resolve(folder, 'manifest.json')) ? { file: resolve(folder, 'manifest.json') } : {}),
38
+ imageRoots: [{ prefix: `/src/shards/${slug}/`, directory: folder }, { prefix: '/assets/', directory: resolve(root, 'public/assets') }] };
39
+ const legacy = decisionLegacyLines(root, slug);
40
+ const project = existsSync(resolve(folder, 'shard.config.ts')) ? await (await import('@wildshard/build/project')).readProjectAssets(folder, undefined, manifestSource, legacy === undefined ? {} : { decisionLegacyLines: legacy }) : null;
41
+ const environment = installBakeEnvironment(root);
42
+ // G342: a colour map's mean colour, decoded from its encoded bytes (PNG / JPEG / WebP) by Pillow, so a textured prop keeps
43
+ // its own paint (Pine's cabins, Driftwood's huts) instead of a guessed wood tint; the bitmap is that one mean texel
44
+ const means = new Map();
45
+ const meanTexel = (bytes) => {
46
+ const key = createHash('sha256').update(bytes).digest('hex');
47
+ let rgb = means.get(key);
48
+ if (rgb === undefined) {
49
+ const out = spawnSync('python3', ['-I', '-c', 'import sys, io\nfrom PIL import Image\nim = Image.open(io.BytesIO(sys.stdin.buffer.read())).convert("RGBA").resize((1, 1), Image.Resampling.BOX)\nprint(*im.getpixel((0, 0)))'], { input: bytes });
50
+ rgb = out.status === 0 ? String(out.stdout).trim().split(/\s+/u).map(Number) : null;
51
+ if (rgb !== null && (rgb.length !== 4 || rgb.some(v => !Number.isFinite(v)))) rgb = null;
52
+ means.set(key, rgb);
53
+ }
54
+ return rgb;
55
+ };
56
+ Object.defineProperty(globalThis, 'createImageBitmap', { configurable: true, value: async (source) => {
57
+ const bytes = source instanceof Blob ? Buffer.from(await source.arrayBuffer()) : null, rgb = bytes === null ? null : meanTexel(bytes);
58
+ return rgb === null ? { width: 1, height: 1, close: () => undefined } : { width: 1, height: 1, data: Uint8ClampedArray.from(rgb), close: () => undefined };
59
+ } });
60
+ const element = () => Object.assign(environment.element(), { prepend: () => undefined, replaceChildren: () => undefined, contains: () => false, blur: () => undefined });
61
+ Object.defineProperty(document, 'createElement', { value: element, configurable: true });
62
+ Object.defineProperty(document, 'createElementNS', { value: (_ns, tag) => element(tag), configurable: true });
63
+ const noop = () => undefined;
64
+ const THREE = await import('three');
65
+ const sky = new Proxy({ setupMaterial: noop, csm: { lights: [new THREE.DirectionalLight()], update: noop },
66
+ hemi: new THREE.HemisphereLight(), sunDisc: new THREE.Mesh(new THREE.SphereGeometry(), new THREE.MeshBasicMaterial()),
67
+ planet: new THREE.Group(), clouds: null, dayNight: { dusk: 0, night: 0 }, viewCamera: new THREE.PerspectiveCamera(), sunDir: new THREE.Vector3(0, 1, 0) },
68
+ { get: (target, key) => key in target ? target[key] : typeof key === 'string' && key.endsWith('Color')
69
+ ? new THREE.Color(1, 1, 1) : typeof key === 'string' && key.endsWith('Dir') ? new THREE.Vector3(0, 1, 0) : undefined });
70
+ (await at('src/client/engine/core/tier.ts')).initializeTier('phone');
71
+ const { default: manifest } = await at(shardManifestPath(root, slug));
72
+ prepareNativeScalarBlocks(manifest, root);
73
+ (await at('src/client/game/shard/list.ts')).installShards([manifest]);
74
+ (await at('src/client/game/shard/registry.ts')).setActiveChunk(manifest.slug);
75
+ const { Terrain, terrainChunkGeometry } = await at('src/client/engine/world/Terrain.ts');
76
+ const terrainBin = resolve(root, 'public/assets/baked', slug, 'terrain.bin');
77
+ // G348: the shard's own data says where its ground comes from (its data/placeholder.json row); no slug branch here
78
+ const source = await placeholderOf(root, slug);
79
+ const groundFrom = source.ground;
80
+ // a shardfile-terrain shard (no terrain.bin, no ground row) builds its world on its own collider grid
81
+ const nativeTerrain = groundFrom === undefined ? existsSync(terrainBin) ? readFileSync(terrainBin) : undefined : (await at(`src/shards/${slug}/${groundFrom.generator}`))[groundFrom.export]();
82
+ if (nativeTerrain !== undefined && !(nativeTerrain instanceof Uint8Array)) throw new Error(`${slug}: placeholder ground ${JSON.stringify(groundFrom)} returned no bytes`);
83
+
84
+ // a manifest ground declared by key (`createBakedTerrainField`, Signal's) is its shardfile's own terrain collider: the bake
85
+ // installs it into the declared field as the session's required-ground loader would (`installProductGround`)
86
+ if (nativeTerrain === undefined && manifest.ground?.terrain !== undefined) {
87
+ const { bakedTerrainSource, installDeclaredBakedTerrain } = await at('src/shared/engine/world/bakedTerrainField.ts');
88
+ const declared = bakedTerrainSource(manifest.ground.terrain), collider = declared === null ? null : placeholderGridBytes(root, resolve(root, 'src/shards', slug), slug);
89
+ if (collider?.kind === 'collider') {
90
+ const { clientGround } = await at('src/shared/game/shardfile/clientGround.ts');
91
+ const ground = clientGround({ terrain: { collider: 'collider' }, meshCollision: null }, new Map([['collider', collider.bytes]]));
92
+ installDeclaredBakedTerrain(manifest.ground.terrain, { seed: declared.seed, heightAt: ground.heightAt, normalAt: ground.normalAt, splatAt: ground.splatAt });
93
+ }
94
+ }
95
+
96
+ /** Weathered wood (linear RGB): the stand-in for a colour map or a texture-only material Node does not decode. */
97
+ const WOOD = new THREE.Color(0.3, 0.21, 0.13);
98
+ /** A drawn thing's colour: its material colour times the mean of its colour map when the host decoded one. */
99
+ const colourOf = (material) => {
100
+ const m = Array.isArray(material) ? material[0] : material;
101
+ const c = m?.color instanceof THREE.Color ? m.color.clone() : new THREE.Color(0.6, 0.6, 0.6);
102
+ const img = m?.map?.image;
103
+ if (img?.data !== undefined && img.width > 0) {
104
+ let r = 0, g = 0, b = 0, n = 0; const d = img.data, stride = Math.max(4, Math.floor(d.length / 4 / 4096) * 4);
105
+ for (let i = 0; i + 3 < d.length; i += stride) { r += d[i]; g += d[i + 1]; b += d[i + 2]; n++; }
106
+ if (n > 0) c.multiply(new THREE.Color(r / n / 255, g / n / 255, b / n / 255).convertSRGBToLinear());
107
+ } else if (m?.map !== null && m?.map !== undefined) c.multiply(WOOD); // a colour map Node did not decode: the weathered wood most props are
108
+ return c;
109
+ };
110
+
111
+ const parts = [], rows = [];
112
+ let forestTrees = [];
113
+ /** One drawn mesh (model-space geometry, its material and its placed copies' matrices) as a producer prototype. */
114
+ const harvest = (label, geometry, materials, transforms, instanced) => {
115
+ const position = geometry?.getAttribute('position');
116
+ if (position === undefined || position.itemSize !== 3 || transforms.length === 0) return;
117
+ const material = Array.isArray(materials) ? materials[0] : materials;
118
+ if (material === undefined || material.transparent === true && material.opacity < 0.5 || material.visible === false) return;
119
+ const index = geometry.index, tri = index === null ? position.count / 3 : index.count / 3;
120
+ const colour = colourOf(materials), vc = geometry.getAttribute('color');
121
+ const base = new Float32Array(position.count * 3), colours = new Float32Array(position.count * 3);
122
+ for (let i = 0; i < position.count; i++) {
123
+ base.set([position.getX(i), position.getY(i), position.getZ(i)], i * 3);
124
+ const k = vc !== undefined && vc.itemSize >= 3 ? [vc.getX(i), vc.getY(i), vc.getZ(i)] : [1, 1, 1];
125
+ colours.set([colour.r * k[0], colour.g * k[1], colour.b * k[2]], i * 3);
126
+ }
127
+ const idx = index === null ? Uint32Array.from({ length: position.count }, (_, i) => i) : Uint32Array.from(index.array);
128
+ rows.push({ name: label, instanced, count: transforms.length, tri, total: tri * transforms.length, colour: colour.toArray().map(x => Number(x.toFixed(2))), map: material.map !== null && material.map !== undefined });
129
+ parts.push({ name: label, positions: base, colours, index: idx, transforms });
130
+ };
131
+ // Placed models (E315: huts, piers, lookouts …) are harvested where they are built, every copy, whatever their culler
132
+ // shows from one bake camera: each placement call keeps its coarsest non-empty level. Scene meshes drawing those
133
+ // models' materials are then skipped, so no copy is taken twice.
134
+ const modelMaterials = new Set(), modelBuilds = [];
135
+ const poseMatrix = (pl) => {
136
+ if (pl.matrix !== undefined) return pl.matrix.clone();
137
+ const s = pl.scale ?? 1, r = new THREE.Matrix4(), out = new THREE.Matrix4().makeScale(s, s, s);
138
+ if (pl.yaw !== undefined) out.premultiply(r.makeRotationY(pl.yaw));
139
+ if (pl.leanX !== undefined) out.premultiply(r.makeRotationX(pl.leanX));
140
+ if (pl.leanZ !== undefined) out.premultiply(r.makeRotationZ(pl.leanZ));
141
+ return out.premultiply(r.makeTranslation(pl.x, pl.y, pl.z));
142
+ };
143
+ const visitPlacement = (call) => {
144
+ if (call.drawnInto !== undefined || call.moving) return undefined;
145
+ const levels = new Map();
146
+ modelBuilds.push({ model: call.model, levels });
147
+ return (build) => {
148
+ const meshes = [];
149
+ if (Array.isArray(build.built)) for (const part of build.built) meshes.push({ geometry: part.geometry, material: part.material, local: null });
150
+ else if (build.built?.isObject3D === true) { build.built.updateMatrixWorld(true); build.built.traverse(n => { if (n.isMesh && !n.isSkinnedMesh) meshes.push({ geometry: n.geometry, material: n.material, local: n.matrixWorld.clone() }); }); }
151
+ for (const mesh of meshes) for (const mat of Array.isArray(mesh.material) ? mesh.material : [mesh.material]) modelMaterials.add(mat);
152
+ if (meshes.length === 0 || build.kind !== 'model') return;
153
+ // copy now: a builder's geometry may be transformed or merged after this callback
154
+ const copies = meshes.map(mesh => ({ geometry: mesh.geometry.clone(), material: mesh.material, local: mesh.local }));
155
+ const byGroup = levels.get(build.placements) ?? new Map(); byGroup.set(build.level, copies); levels.set(build.placements, byGroup);
156
+ };
157
+ };
158
+ /** Harvest the placed models (their coarsest non-empty level, every copy); returns each copy's world box. */
159
+ const harvestModels = () => {
160
+ const boxes = [], b = new THREE.Box3();
161
+ if (report) console.log(` models: ${JSON.stringify(Object.entries(Object.groupBy(modelBuilds, x => x.model)).map(([k, v]) => `${k}:${v.length}/${v.reduce((n, x) => n + x.levels.size, 0)}`))}`);
162
+ for (const { model, levels } of modelBuilds) for (const [placements, byLevel] of levels) {
163
+ const level = Math.max(...[...byLevel.keys()].filter(l => (byLevel.get(l) ?? []).length > 0));
164
+ const poses = placements.map(poseMatrix);
165
+ for (const mesh of byLevel.get(level) ?? []) {
166
+ const transforms = poses.map(pose => mesh.local === null ? pose.clone() : pose.clone().multiply(mesh.local));
167
+ mesh.geometry.computeBoundingBox();
168
+ for (const t of transforms) if (mesh.geometry.boundingBox !== null) boxes.push(b.copy(mesh.geometry.boundingBox).applyMatrix4(t).expandByScalar(0.5).clone());
169
+ harvest(model, mesh.geometry, mesh.material, transforms.map(t => Array.from(t.elements)), false);
170
+ }
171
+ }
172
+ return boxes;
173
+ };
174
+ // a pure shardfile project has no authored plugin to build: its near placeholder is its terrain alone; a shard whose world
175
+ // is its shardfile tiles (`near.world: 'tiles'`, a world built from its product) has no authored host to visit either
176
+ if (typeof manifest.load === 'function' && source.near?.world !== 'tiles') await visitAuthoredWorld(manifest, { root, sky, element: environment.element, tier: 'phone', nativeTerrain, collectLate: true, visitPlacement,
177
+ createTerrain: async () => {
178
+ const field = await at('src/shared/engine/world/Heightfield.ts');
179
+ const terrain = new Terrain(); terrain.mesh = new THREE.Mesh(terrainChunkGeometry(field), new THREE.MeshBasicMaterial()); terrain.mesh.name = 'near-harvest-terrain';
180
+ return terrain;
181
+ },
182
+ visit: ({ scene, camera, late, forest }) => {
183
+ // a forest its shard's own tree model draws (Pine's, E315) has no meshes in Node: its placements are the trees
184
+ if (forest.drawer !== 'self') forestTrees = forest.trees.map(t => ({ x: t.x, y: t.y, z: t.z, rot: t.rot, height: t.height, scale: t.scale,
185
+ species: t.species ?? forest.factory.variants[t.variant]?.species ?? 'tree', tint: t.tint.getHSL({ h: 0, s: 0, l: 0 }).l }));
186
+ // pose the view over the whole cell from a neighbour's distance, so view-dependent repacks keep their far copies
187
+ camera.fov = 100; camera.near = 1; camera.far = 6000; camera.position.set(0, 650, 1); camera.lookAt(0, 0, 0);
188
+ camera.updateProjectionMatrix(); camera.updateMatrixWorld(true); scene.updateMatrixWorld(true);
189
+ for (let pass = 0; pass < 3; pass++) for (const fn of late) fn(1 / 30);
190
+ scene.updateMatrixWorld(true);
191
+ const m = new THREE.Matrix4(), modelBoxes = harvestModels(), centre = new THREE.Vector3(), cb = new THREE.Box3();
192
+ scene.traverse((node) => {
193
+ if (!node.isMesh || node.isSkinnedMesh || node.name === 'near-harvest-terrain' || !node.visible) return;
194
+ let hidden = false; for (let p = node.parent; p !== null; p = p.parent) if (!p.visible) hidden = true;
195
+ if (hidden) return;
196
+ // a copy drawing a placed model's material inside one of its harvested copies is that copy (taken above)
197
+ const shared = (Array.isArray(node.material) ? node.material : [node.material]).some(mat => modelMaterials.has(mat)), transforms = [];
198
+ if (shared) node.geometry.computeBoundingBox();
199
+ for (let k = 0; k < (node.isInstancedMesh ? node.count : 1); k++) {
200
+ if (node.isInstancedMesh) { node.getMatrixAt(k, m); m.premultiply(node.matrixWorld); } else m.copy(node.matrixWorld);
201
+ if (shared && node.geometry.boundingBox !== null && modelBoxes.some(box => box.containsPoint(cb.copy(node.geometry.boundingBox).applyMatrix4(m).getCenter(centre)))) continue;
202
+ transforms.push(Array.from(m.elements));
203
+ }
204
+ let label = node.name; for (let p = node.parent; label.length === 0 && p !== null; p = p.parent) label = p.name; // the nearest named ancestor
205
+ harvest(label, node.geometry, node.material, transforms, node.isInstancedMesh === true);
206
+ });
207
+ },
208
+ });
209
+ rows.sort((a, b) => b.total - a.total);
210
+ if (report) for (const r of rows.slice(0, 40)) console.log(JSON.stringify(r));
211
+ console.log(`${slug}: harvested ${rows.length} meshes, ${rows.reduce((sum, r) => sum + r.total, 0)} triangles`);
212
+
213
+ // the terrain and its water come from the far look's lattice; harvested ground, sea and sky meshes stay out
214
+ const SKIP = /terrain|seabed|ocean|water|\bsea\b|sky|cloud|horizon|backdrop|shadow|decal|fog|haze|sun|moon|star|grass-blades|splat/iu;
215
+ const mean = (c) => { let r = 0, g = 0, b = 0; const n = c.length / 3; for (let i = 0; i < c.length; i += 3) { r += c[i]; g += c[i + 1]; b += c[i + 2]; } return [r / n, g / n, b / n]; };
216
+ const box = (p) => {
217
+ const mm = new THREE.Matrix4().fromArray(p.transforms[0]), v = new THREE.Vector3(), b = new THREE.Box3();
218
+ for (let i = 0; i < p.positions.length; i += 3) b.expandByPoint(v.set(p.positions[i], p.positions[i + 1], p.positions[i + 2]).applyMatrix4(mm));
219
+ return `[${b.min.toArray().map(x => x.toFixed(0))}]..[${b.max.toArray().map(x => x.toFixed(0))}]`;
220
+ };
221
+ const prototypes = parts.filter((p) => {
222
+ const [r, g, b] = mean(p.colours), white = r > 0.97 && g > 0.97 && b > 0.97;
223
+ // a texture-only white mesh (Pine's cabins) is painted wood; a white sheet as wide as the world is a sea or sky plane
224
+ const giant = white && p.positions.length > 0 && (() => { let lo = Infinity, hi = -Infinity; for (let i = 0; i < p.positions.length; i += 3) { lo = Math.min(lo, p.positions[i]); hi = Math.max(hi, p.positions[i]); } return hi - lo > 400; })();
225
+ if (white && !giant) for (let i = 0; i < p.colours.length; i += 3) { p.colours[i] *= WOOD.r; p.colours[i + 1] *= WOOD.g; p.colours[i + 2] *= WOOD.b; }
226
+ const skip = SKIP.test(p.name) ? 'name' : giant ? 'white' : (r > 2 || g > 2 || b > 2) ? 'glow' : null;
227
+ if (report && p.transforms.length > 0) console.log(` ${skip === null ? "take" : "skip"} ${p.name} (${skip}) ${p.index.length / 3} tris x ${p.transforms.length} ${box(p)}`);
228
+ return skip === null; // ground / sea / sky come from the lattice; an unlit white or an emissive glow is not paint
229
+ });
230
+ // the far look, the grid and the bake are the generic ones (`@wildshard/sdk/bake/placeholders`, as `wildshard build`)
231
+ const sdk = await at('src/sharddev/bake/placeholders.ts');
232
+ const exported = async (ref) => { const value = (await at(`src/shards/${slug}/${ref.module}`))[ref.export]; if (value === undefined) throw new Error(`${slug}: ${ref.module} has no export ${ref.export}`); return value; };
233
+ // G380: a shard that declares no far look takes the generic one `wildshard build` gives any shardfile (`shardfileLook`)
234
+ if (source.far === undefined && project === null) throw new Error(`${slug}: no far row in its data/placeholder.json and no shardfile project`);
235
+ const look = source.far === undefined ? sdk.shardfileLook(project.shard) : await exported(source.far.look);
236
+ // a shard whose world lives in its shardfile tiles (Signal's sand and tower, a template's props, the Blender template's
237
+ // authored mesh world) has nothing the authored host builds: its coarse (L1) tiles' meshes are its world, harvested as
238
+ // `wildshard build` harvests them (`shardfileWorld`), in their own catalogue's colours; those tiles keep refining over it, so it never owns its cell (as `wildshard build`'s
239
+ // shardfile near row never does, `loadShardfileNear`)
240
+ const fromTiles = prototypes.length === 0 && project !== null;
241
+ if (fromTiles) {
242
+ // its coarse (L1) tiles, else its L0 tiles when it ships none (Nine Dragon's 64 L0 tiles: the bake reduces them to budget)
243
+ const coarse = await sdk.shardfileWorld(project.shard, project.assets), level = coarse.length > 0 ? 1 : 0;
244
+ prototypes.push(...(level === 1 ? coarse : await sdk.shardfileWorld(project.shard, project.assets, undefined, 0)));
245
+ console.log(`${slug}: ${prototypes.length} shardfile L${level} tile meshes`);
246
+ }
247
+ /**
248
+ * A tiles world raised off its declared floor (`near.base`, Nine Dragon's city at +125 m) stands on a stack: a column
249
+ * under every `cell`-metre square its content covers, from the floor up a metre into that square's lowest content (the
250
+ * footprint's enclosed gaps filled from their neighbours), its sides only where a neighbour stands lower, coloured from
251
+ * `foot` at the floor to `lip` at the stack's top. One prototype a 125 m region, each kept whole (a floor of its own).
252
+ */
253
+ const stackBase = (content, floorY, { cell, lip, foot }) => {
254
+ const n = Math.ceil(500 / cell), low = new Float32Array(n * n).fill(Infinity), cellOf = (v) => Math.min(n - 1, Math.max(0, Math.floor((v + 250) / cell)));
255
+ for (const p of content) for (const m of p.transforms) {
256
+ const world = (k) => [0, 1, 2].map(r => (m[r] ?? 0) * p.positions[k * 3] + (m[4 + r] ?? 0) * p.positions[k * 3 + 1] + (m[8 + r] ?? 0) * p.positions[k * 3 + 2] + (m[12 + r] ?? 0));
257
+ for (let t = 0; t < p.index.length; t += 3) {
258
+ const [a, b, c] = [p.index[t], p.index[t + 1], p.index[t + 2]].map(world), y = Math.min(a[1], b[1], c[1]);
259
+ for (let i = cellOf(Math.min(a[0], b[0], c[0])); i <= cellOf(Math.max(a[0], b[0], c[0])); i++) for (let j = cellOf(Math.min(a[2], b[2], c[2])); j <= cellOf(Math.max(a[2], b[2], c[2])); j++) low[j * n + i] = Math.min(low[j * n + i], y);
260
+ }
261
+ }
262
+ // the gaps the footprint encloses (streets between towers, courts) take their neighbours' lowest content
263
+ const outside = new Uint8Array(n * n), queue = [];
264
+ for (let k = 0; k < n; k++) for (const c of [k, (n - 1) * n + k, k * n, k * n + n - 1]) if (low[c] === Infinity && outside[c] === 0) { outside[c] = 1; queue.push(c); }
265
+ while (queue.length > 0) {
266
+ const c = queue.pop(), i = c % n, j = (c - i) / n;
267
+ for (const [di, dj] of [[1, 0], [-1, 0], [0, 1], [0, -1]]) { const x = i + di, z = j + dj, d = z * n + x; if (x >= 0 && z >= 0 && x < n && z < n && low[d] === Infinity && outside[d] === 0) { outside[d] = 1; queue.push(d); } }
268
+ }
269
+ for (let pass = 0; pass < n; pass++) {
270
+ let open = 0;
271
+ for (let c = 0; c < n * n; c++) if (low[c] === Infinity && outside[c] === 0) {
272
+ const i = c % n, j = (c - i) / n, around = [[1, 0], [-1, 0], [0, 1], [0, -1]].map(([di, dj]) => (i + di >= 0 && j + dj >= 0 && i + di < n && j + dj < n ? low[(j + dj) * n + i + di] : Infinity)).filter(Number.isFinite);
273
+ if (around.length > 0) low[c] = Math.min(...around); else open++;
274
+ }
275
+ if (open === 0) break;
276
+ }
277
+ const top = (i, j) => (i < 0 || j < 0 || i >= n || j >= n || !Number.isFinite(low[j * n + i]) || low[j * n + i] < floorY + 1 ? floorY : low[j * n + i] + 1);
278
+ let peak = floorY; for (let c = 0; c < n * n; c++) peak = Math.max(peak, top(c % n, (c - (c % n)) / n));
279
+ const tint = (y) => { const k = peak > floorY ? (y - floorY) / (peak - floorY) : 1; return [0, 1, 2].map(r => foot[r] + (lip[r] - foot[r]) * k); };
280
+ const regions = new Map();
281
+ for (let j = 0; j < n; j++) for (let i = 0; i < n; i++) {
282
+ const h = top(i, j);
283
+ if (h <= floorY) continue;
284
+ const x0 = -250 + i * cell, z0 = -250 + j * cell, x1 = Math.min(250, x0 + cell), z1 = Math.min(250, z0 + cell), key = `${Math.floor((x0 + 250) / 125)}-${Math.floor((z0 + 250) / 125)}`;
285
+ const region = regions.get(key) ?? { positions: [], colours: [], index: [], cx: -250 + Math.floor((x0 + 250) / 125) * 125 + 62.5, cz: -250 + Math.floor((z0 + 250) / 125) * 125 + 62.5 };
286
+ regions.set(key, region);
287
+ // the four sides, each outward (+x, -x, +z, -z), from the neighbour's top (or the floor) up to this column's
288
+ for (const { di, dj, ax, az, bx, bz } of [{ di: 1, dj: 0, ax: x1, az: z1, bx: x1, bz: z0 }, { di: -1, dj: 0, ax: x0, az: z0, bx: x0, bz: z1 },
289
+ { di: 0, dj: 1, ax: x0, az: z1, bx: x1, bz: z1 }, { di: 0, dj: -1, ax: x1, az: z0, bx: x0, bz: z0 }]) {
290
+ const from = Math.max(floorY, top(i + di, j + dj));
291
+ if (from >= h) continue;
292
+ const v = region.positions.length / 3;
293
+ for (const [x, y, z] of [[ax, from, az], [bx, from, bz], [bx, h, bz], [ax, h, az]]) { region.positions.push(x - region.cx, y, z - region.cz); region.colours.push(...tint(y)); }
294
+ region.index.push(v, v + 1, v + 2, v, v + 2, v + 3);
295
+ }
296
+ }
297
+ return [...regions.entries()].map(([key, r]) => ({ name: `stack-base-${key}`, positions: Float32Array.from(r.positions), colours: Float32Array.from(r.colours), index: Uint32Array.from(r.index), transforms: [[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, r.cx, 0, r.cz, 1]] }));
298
+ };
299
+ const base = source.near?.base === undefined || source.near.floor === undefined ? [] : stackBase(prototypes, source.near.floor.y, source.near.base);
300
+ if (base.length > 0) {
301
+ prototypes.push(...base);
302
+ console.log(`${slug}: stack base ${base.reduce((sum, p) => sum + p.index.length / 3, 0)} triangles in ${base.length} regions`);
303
+ }
304
+ // G380: a land of models (Sky Reach's islands, its far row's `parts`) is built with the SDK's shared model tools, at
305
+ // `NEAR_BAKE.modelScale` times the far share, and reduced to the near bake's land budget
306
+ const land = source.far?.parts === undefined ? [] : await (await exported(source.far.parts))(await sdk.placeholderModelTools(root, sdk.NEAR_BAKE.modelScale));
307
+ // the ground: a declared grid, else the baked terrain.bin or the shardfile's terrain collider; a world with no height
308
+ // field (a shardfile whose terrain is null, an authored mesh world) stands on the bake's flat floor (grid null)
309
+ // (a world that is its tiles, `near.world: 'tiles'`, stands on the flat floor too: its tiles carry its ground, so a far
310
+ // grid under them would only repeat their skyline as a crude height field)
311
+ const flat = source.near?.world === 'tiles' || source.far?.grid === undefined && project?.shard.terrain === null && !existsSync(terrainBin);
312
+ const ground = source.far?.grid !== undefined || flat ? null : placeholderGridBytes(root, resolve(root, 'src/shards', slug), slug);
313
+ // G630: a declared floor (near.floor) is that flat floor at its own height, in its own colour (the bake's floor stands under
314
+ // the lowest placed point, a well shaft's bottom, in the far look's colour there)
315
+ const floor = source.near?.floor;
316
+ const grid = floor !== undefined ? { res: 2, size: 500, heights: new Float32Array(4).fill(floor.y), splat: null }
317
+ : flat ? null : ground === null ? (await exported(source.far.grid))() : ground.kind === 'baked' ? sdk.bakedTerrainGrid(ground.bytes) : sdk.colliderGrid(ground.bytes);
318
+ // round 3: the ground at the height field's own density, reduced by geometric error (--ground-error=<m> to measure)
319
+ const arg = (name) => process.argv.find(value => value.startsWith(`--${name}=`))?.slice(name.length + 3);
320
+ const { bytes, manifest: baked, reduced } = await sdk.bakeNearPlaceholder({ grid, look: floor === undefined ? look : { ...look, quads: 1, colourAt: () => [...floor.colour] }, prototypes, parts: land, trees: forestTrees, stands: source.trees,
321
+ floors: base.length === 0 ? source.near?.floors : [...source.near?.floors ?? [], ...base.map(p => ({ prototype: p.name, triangles: p.index.length / 3 }))],
322
+ groundError: Number(arg('ground-error') ?? sdk.NEAR_BAKE.groundError), residentBytes: Number(arg('resident') ?? sdk.NEAR_BAKE.residentBytes), log: line => console.log(`${slug}: ${line}`) });
323
+ // G630: a shard that declares `near.owns` (its tiles draw lit only inside its world) keeps its near view over its tiles
324
+ const row = fromTiles && prototypes.length > 0 && source.near?.owns !== true ? { ...baked, near: { ...baked.near, tiles: true } } : baked;
325
+ const { file } = row, resident = file.decoded + file.gpu;
326
+ if (report) { console.log(` error ${reduced.error === undefined ? 'none (common ratio)' : `${reduced.error.toFixed(3)} m`}`); for (const k of reduced.kept) console.log(` keep ${k.name}: ${k.copies} x ${k.triangles}/${k.source}`); for (const d of reduced.dropped) console.log(` drop ${d.name}: ${d.why}`); }
327
+ const dir = resolve(root, 'public/assets/baked', slug);
328
+ mkdirSync(dir, { recursive: true });
329
+ writeFileSync(resolve(dir, 'near.glb'), bytes); writeFileSync(resolve(dir, 'near.json'), sdk.placeholderJson(row));
330
+ console.log(`${slug.padEnd(18)} near resident ${(resident / 1e6).toFixed(3)} MB wire ${(file.compressed / 1e3).toFixed(0)} KB ${file.triangles} tris (${reduced.triangles} content) ${file.draws} draw`);
331
+ process.exit(0);
@@ -0,0 +1,26 @@
1
+ import type { Pixels } from '@wildshard/engine/player/viewmodelTextures';
2
+
3
+ /** The result carries only dimensions and the names whose raw planes were uploaded. */
4
+ export interface ViewmodelBake { readonly w: number; readonly h: number; readonly planes: readonly ('col' | 'nrm' | 'arm')[] }
5
+
6
+ /** Send exact RGBA subviews; encoding and KTX2 cache policy remain in the Node baker. */
7
+ export async function sendViewmodelPixels(pixels: Pixels, byteUrl: string, send: typeof fetch = fetch): Promise<ViewmodelBake> {
8
+ if (!Number.isSafeInteger(pixels.w) || !Number.isSafeInteger(pixels.h) || pixels.w < 1 || pixels.h < 1 || pixels.w > 4096 || pixels.h > 4096)
9
+ throw new Error('Invalid viewmodel raster dimensions');
10
+ const expected = pixels.w * pixels.h * 4, planes: ('col' | 'nrm' | 'arm')[] = [];
11
+ for (const name of ['col', 'nrm', 'arm'] as const) {
12
+ const bytes = pixels[name];
13
+ if (bytes === null) continue;
14
+ if (bytes.byteLength !== expected || !(bytes.buffer instanceof ArrayBuffer)) throw new Error('Invalid viewmodel raster plane');
15
+ }
16
+ for (const name of ['col', 'nrm', 'arm'] as const) {
17
+ const bytes = pixels[name];
18
+ if (bytes === null) continue;
19
+ const buffer = bytes.buffer;
20
+ if (!(buffer instanceof ArrayBuffer)) throw new Error('Invalid viewmodel raster storage');
21
+ const response = await send(`${byteUrl}${name}`, { method: 'POST', body: new Uint8Array(buffer, bytes.byteOffset, bytes.byteLength) });
22
+ if (!response.ok) throw new Error(`Viewmodel ${name} byte handoff refused`);
23
+ planes.push(name);
24
+ }
25
+ return { w: pixels.w, h: pixels.h, planes };
26
+ }
@@ -0,0 +1,11 @@
1
+ import { makePixels, type SetName } from '@wildshard/engine/player/viewmodelTextures';
2
+ import { sendViewmodelPixels, type ViewmodelBake } from './viewmodelBytes';
3
+
4
+ declare global { interface Window { bakeViewmodelPixels: (name: SetName, byteUrl: string) => Promise<ViewmodelBake> } }
5
+ function canvas2d(width: number, height: number): CanvasRenderingContext2D {
6
+ const canvas = document.createElement('canvas'); canvas.width = width; canvas.height = height;
7
+ const context = canvas.getContext('2d');
8
+ if (context === null) throw new Error('no 2d context');
9
+ return context;
10
+ }
11
+ window.bakeViewmodelPixels = (name, byteUrl) => sendViewmodelPixels(makePixels(name, canvas2d), byteUrl);
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env node
2
+ // C3 (D7): put a shard on an already built client, with no rebuild: what a flip does at the next deploy until E7.
3
+ // node --import ./scripts/bake-loader.mjs scripts/bake-catalogue.mjs <dist> <shard.tar> <instance> <cellX> <cellY> [<packs folder> <packs origin>]
4
+ // With a packs folder the cook goes there for the store, and pages read it from that origin (J138); only the header stays.
5
+ // The tarball is cooked into <dist> and every table a page reads names it, title, spawn and server, in files the bundle doesn't hash. A deploy that
6
+ // takes its grid from api-server names that file with WILDSHARD_CATALOGUE at build time instead.
7
+ import { createHash } from 'node:crypto';
8
+ import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
9
+ import { tmpdir } from 'node:os';
10
+ import { join, resolve } from 'node:path';
11
+ import { brotliCompressSync, constants } from 'node:zlib';
12
+
13
+ const { addCookedShards, folderVerdict, writeCatalogueDocument } = await import('@wildshard/engine/scripts/app-build/catalogue.ts');
14
+ const { cookShard, openShardfile, servedAsIs } = await import('@wildshard/engine/game/shardCook');
15
+ const { writeServiceWorker } = await import('@wildshard/engine/scripts/app-build/serviceWorker.ts');
16
+
17
+ const [dist, tar, instance, x, y, packs, origin] = process.argv.slice(2);
18
+ if (dist === undefined || tar === undefined || instance === undefined || x === undefined || y === undefined) throw new Error('usage: bake-catalogue <dist> <shard.tar> <instance> <cellX> <cellY>');
19
+ const out = resolve(dist), tarball = readFileSync(tar), shard = openShardfile(tarball), slug = shard.slug;
20
+ const cooked = mkdtempSync(join(tmpdir(), 'bake-catalogue-'));
21
+ try {
22
+ const report = await cookShard(tarball, cooked, { now: () => performance.now(), wireBytes: (url, bytes) => servedAsIs(url, bytes) ? bytes.length : brotliCompressSync(bytes, { params: { [constants.BROTLI_PARAM_QUALITY]: 5 } }).length });
23
+ // `dereference`: a preview's dist symlinks its folders.
24
+ cpSync(cooked, packs === undefined ? out : resolve(packs), { recursive: true, dereference: true, force: true });
25
+ const proved = await folderVerdict(tarball, out);
26
+ const header = join(out, 'shardfiles', slug);
27
+ mkdirSync(header, { recursive: true }); writeFileSync(join(header, 'shard.json'), shard.header);
28
+ // A shard's manifests name its document at the page's URL, loose as its header is: no part carries it.
29
+ const document = join(cooked, 'shardfiles', slug, 'discovery.json');
30
+ if (existsSync(document)) cpSync(document, join(header, 'discovery.json'));
31
+ if (packs !== undefined) { mkdirSync(join(packs, 'shardfiles', slug), { recursive: true }); writeFileSync(join(packs, 'shardfiles', slug, 'shard.json'), shard.header); }
32
+ const catalogue = JSON.parse(readFileSync(join(out, 'catalogue.json'), 'utf8'));
33
+ const hash = createHash('sha256').update(tarball).digest('hex');
34
+ catalogue.placements = [...catalogue.placements.filter(row => row.slug !== slug), { instance, slug, cell: [Number(x), Number(y)], size: [500, 500, 500], order: catalogue.placements.length + 1 }];
35
+ catalogue.shards = [...catalogue.shards.filter(row => row.slug !== slug), { slug, name: shard.index.identity.name, revision: 1, hash }];
36
+ writeCatalogueDocument(out, catalogue);
37
+ // The server worker reads its pack beside the page, wherever the page's packs live (J134: nothing in it is secret).
38
+ const { server: packed } = report;
39
+ if (packed !== undefined) cpSync(join(cooked, 'assets/spawn/server'), join(out, 'assets/spawn/server'), { recursive: true, force: true });
40
+ const server = packed === undefined ? {} : { server: JSON.parse(readFileSync(join(cooked, packed.index.slice(1)), 'utf8')) };
41
+ addCookedShards(out, [{ slug, hash, header: '', manifests: report.manifests, ...proved, ...server }], cooked, packs === undefined || origin === undefined ? undefined : { base: origin, shards: [slug] });
42
+ if (existsSync(join(out, 'sw.js'))) await writeServiceWorker(out, process.cwd());
43
+ console.log(JSON.stringify({ slug, hash, seconds: report.seconds, parts: report.parts }));
44
+ } finally { rmSync(cooked, { recursive: true, force: true }); }
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env node
2
+ // E357 F1: check committed outputs by baking them, including local GPU/encoder outputs where supported.
3
+ import { spawnSync } from 'node:child_process';
4
+ import { readFileSync } from 'node:fs';
5
+ import { join, resolve as pathResolve } from 'node:path';
6
+ import { toolVersion } from '@wildshard/engine/scripts/bake-output.mjs';
7
+
8
+ const ROOT = pathResolve(import.meta.dirname, '..');
9
+ const nodeOnly = process.argv.includes('--node-only'); // builders queue the GPU/derived batch to the lead
10
+ const skipGpu = nodeOnly ? true : process.argv.includes('--skip-gpu');
11
+ const state = { failed: false };
12
+ const run = (args, command = process.execPath) => {
13
+ const result = spawnSync(command, args, { cwd: ROOT, stdio: 'inherit' });
14
+ if (result.error) console.error(result.error.message);
15
+ if (result.status !== 0) state.failed = true;
16
+ return result.status === 0;
17
+ };
18
+ // SF74 W24 (speed audit #10): the push gate caches each node baker on its own inputs (`--list-node`, then
19
+ // `--node-only --only=<baker>` per cached step), so one touched src file re-runs only the bakers that read it.
20
+ const NODE_BAKERS = ['directors', 'chunk', 'sky', 'navmesh', 'island-cover', 'voxel-ao', 'geometry', 'cloud-field', 'water-ripples', 'nalati-bodies', 'nalati-places', 'pine-thrall-extras', 'pine-rain'];
21
+ if (process.argv.includes('--list-node')) { console.log(NODE_BAKERS.join('\n')); process.exit(0); }
22
+ const only = process.argv.find((arg) => arg.startsWith('--only='))?.slice('--only='.length);
23
+ if (only !== undefined && !NODE_BAKERS.includes(only)) { console.error(`bake-check: no node baker ${only} (${NODE_BAKERS.join(', ')})`); process.exit(64); }
24
+ if (only !== undefined && !nodeOnly) { console.error('bake-check: --only needs --node-only'); process.exit(64); }
25
+ for (const baker of NODE_BAKERS) if (only === undefined || only === baker) {
26
+ if (baker === 'directors') {
27
+ run(['--experimental-transform-types', '--import', './scripts/bake-loader.mjs', '--input-type=module', '-e', `import { runCli } from './src/sharddev/cli.ts'; await runCli(['build', 'src/shards/nalati-grasslands', '--behaviour-only', '--check']);`]);
28
+ } else {
29
+ const entry = baker.startsWith('pine-') ? `src/shards/pine-hollow/generators/bake-${baker}.mjs` : baker.startsWith('nalati-') ? `src/shards/nalati-grasslands/generators/bake-${baker === 'nalati-places' ? 'nalati-painted' : baker}.mjs` : `scripts/bake-${baker}.mjs`;
30
+ run(['--experimental-transform-types', '--import', './scripts/bake-loader.mjs', entry, '--check']);
31
+ }
32
+ }
33
+
34
+ const metal = only === undefined && process.platform === 'darwin' && spawnSync('system_profiler', ['SPDisplaysDataType'], { encoding: 'utf8' }).stdout.includes('Metal');
35
+ if (only !== undefined) { /* one node baker: no GPU bakers */ }
36
+ else if (!metal) console.log('bake-check: GPU bakers skipped (no Metal): bake-textures');
37
+ else if (skipGpu) console.log('bake-check: GPU bakers queued: bake-textures');
38
+ else if (!state.failed) {
39
+ // SF87 (G320): the sky textures render on their own bake page (no game build, no preview, no ?chunk= boot)
40
+ run(['scripts/browser-lane.sh', '--max', '4', process.execPath, 'scripts/bake-textures.mjs', '--check', '--all'], 'bash');
41
+ }
42
+ if (only !== undefined) { /* one node baker for the gate's per-baker cache */ }
43
+ else if (nodeOnly) console.log('bake-check: derived copies queued (--node-only): tex-tiers, bake-ktx2');
44
+ else {
45
+ if (toolVersion('magick', ['-version']) && toolVersion('cwebp', ['-version'])) run(['scripts/tex-tiers.mjs', '--check']);
46
+ else console.log('bake-check: phone copies skipped (no magick/cwebp): tex-tiers');
47
+ if (toolVersion('magick', ['-version']) && toolVersion('basisu', ['-version'])) run(['--import', './scripts/bake-loader.mjs', 'scripts/bake-ktx2.mjs', '--check']);
48
+ else console.log('bake-check: KTX2 copies skipped (no magick/basisu): bake-ktx2');
49
+ }
50
+ if (state.failed) {
51
+ const playwright = JSON.parse(readFileSync(join(ROOT, 'node_modules/playwright/package.json'), 'utf8'));
52
+ console.log(`bake-check: tools node ${process.version}; playwright ${playwright.version}; ${['magick', 'cwebp', 'basisu'].map((command) => `${command} ${toolVersion(command, ['-version']) ?? 'unavailable'}`).join('; ')}`);
53
+ process.exitCode = 1;
54
+ }
@@ -0,0 +1,124 @@
1
+ #!/usr/bin/env node
2
+ // bake-chunk.mjs — pre-bake every registered shard's terrain at build time (project/archive/2026-09-22-load-perf.md §P2.1).
3
+ //
4
+ // Runs the chunk's pure terrain functions (src/chunks/<slug>.ts → buildTerrain: seeded Noise2D / Rng)
5
+ // in Node and writes public/assets/baked/<slug>/terrain.bin — the height and splat weights at every
6
+ // vertex of the terrain mesh grid (TERRAIN_RES² over CHUNK_SIZE, the exact PlaneGeometry order), so
7
+ // the phone builds the mesh, plants the forest and walks the ground from a lookup instead of ~200 k
8
+ // noise evaluations at launch. src/shared/engine/world/BakedTerrain.ts reads it and swaps Heightfield's bindings.
9
+ //
10
+ // Every run bakes in memory and writes only differing output bytes. Source-only edits write nothing.
11
+ //
12
+ // node --import ./scripts/bake-loader.mjs scripts/bake-chunk.mjs [--check]
13
+ //
14
+ // Format (little-endian): 'WSTR' u32 version=1 · u32 res · f32 size · u32 seed · u32 landscapeHash (0 = unhashed legacy) ·
15
+ // f32[res²] height · u8[res²·4] splat weights (each row sums to ≈ 255) — vertex i = iz·res + ix at
16
+ // (x, z) = (−half + ix·d, −half + iz·d), d = size / (res − 1).
17
+ // Then, for a shard that grows undergrowth, the placement decision log (src/shared/engine/world/forest/placement.ts — the forest
18
+ // planted on this grid, then every undergrowth candidate's kept / skipped bit, ~17 KB): 'WSPL' u32 version=1 ·
19
+ // u32 decisions · u32 kinds · u32[kinds] counts · f64 checksum sum · u8[⌈decisions/8⌉] bits.
20
+ import { byteWriter, outputHash, jsonBytes } from '@wildshard/engine/scripts/bake-output.mjs';
21
+ import { resolve } from 'node:path';
22
+ import { pathToFileURL } from 'node:url';
23
+
24
+ const ROOT = resolve(import.meta.dirname, '..');
25
+ const OUT = resolve(ROOT, 'public/assets/baked');
26
+ const check = process.argv.includes('--check');
27
+ const VERSION = 1;
28
+ // before any game module loads: the settings (and the defs that read them) take the page's query at import
29
+
30
+
31
+ const { CHUNK_SIZE, CHUNK_HALF, TERRAIN_RES } = await import(pathToFileURL(resolve(ROOT, 'src/shared/engine/core/config.ts')).href);
32
+ const { landscapeHash } = await import(pathToFileURL(resolve(ROOT, 'src/shared/engine/world/terrainField.ts')).href);
33
+
34
+ // every chunk module that exports a ShardManifest (has slug + terrain); the registry itself needs `location`
35
+ const { SHARDS } = await import(pathToFileURL(resolve(ROOT, 'src/shards.generated.ts')).href);
36
+
37
+ const registry = await import(pathToFileURL(resolve(ROOT, 'src/client/game/shard/registry.ts')).href);
38
+ const heightfield = await import(pathToFileURL(resolve(ROOT, 'src/shared/engine/world/Heightfield.ts')).href);
39
+ const bakedTerrain = await import(pathToFileURL(resolve(ROOT, 'src/shared/engine/world/BakedTerrain.ts')).href);
40
+ const placement = await import(pathToFileURL(resolve(ROOT, 'src/shared/engine/world/forest/placement.ts')).href);
41
+ const { bakesTerrain } = await import(pathToFileURL(resolve(ROOT, 'src/shared/game/shard/terrainBake.ts')).href);
42
+
43
+ // G384: a shard whose retained WSTR alias is its shardfile's admitted query terrain (shard.config `terrain.form: 'query'`)
44
+ // ships the bare lattice. The content gate admits no placement tail, and no runtime of these shards replays one.
45
+ const QUERY_TERRAIN = new Set(['nalati-grasslands']);
46
+
47
+ /** The undergrowth decision log for `def` planted on the grid in `gridBuf` — null for a shard that grows none (main.ts: no forest carpet at sea). */
48
+ function placementSection(def, gridBuf) {
49
+ if (def.ocean || QUERY_TERRAIN.has(def.slug) || !registry.findChunk(def.slug)) return null;
50
+ registry.setActiveChunk(def.slug);
51
+ heightfield._installBakedTerrain(bakedTerrain.bakedSamplers(bakedTerrain.parseBakedTerrain(gridBuf))); // as loadBakedTerrain does at launch
52
+ const variants = placement.plantSpecs(def.trees); // the species set when the shard has one (PH-B4), else the runtime pines
53
+ const { trees, grid } = placement.placeForest(variants);
54
+ if (trees.length === 0) return null;
55
+ const log = placement.DecisionLog.record();
56
+ const run = placement.placeUndergrowth(trees, grid, log);
57
+ let r = run.next(); while (!r.done) r = run.next();
58
+ const { counts, sum } = placement.placementChecksum(r.value);
59
+ const bits = log.bits();
60
+ const out = new ArrayBuffer(16 + counts.length * 4 + 8 + bits.length);
61
+ const dv = new DataView(out);
62
+ dv.setUint8(0, 0x57); dv.setUint8(1, 0x53); dv.setUint8(2, 0x50); dv.setUint8(3, 0x4c); // 'WSPL'
63
+ dv.setUint32(4, 1, true); dv.setUint32(8, log.count, true); dv.setUint32(12, counts.length, true);
64
+ counts.forEach((c, k) => dv.setUint32(16 + k * 4, c, true));
65
+ dv.setFloat64(16 + counts.length * 4, sum, true);
66
+ new Uint8Array(out, 16 + counts.length * 4 + 8).set(bits);
67
+ return { bytes: new Uint8Array(out), decisions: log.count, counts, trees: trees.length };
68
+ }
69
+
70
+ const output = byteWriter(check, 'bake-chunk');
71
+ for (const def of SHARDS) {
72
+ // one rule with the grid's edge reader (src/shared/game/shard/terrainBake.ts): a structures world ships no bake, and the reader
73
+ // never fetches one for it (C4-R1-C13)
74
+ if (!bakesTerrain(def.ground)) continue;
75
+ // A required manifest ground is its shardfile's own terrain collider (compiled by its tiles job, loaded through the
76
+ // session's required-ground loader): it ships no WSTR bake, so never sample an unready field here.
77
+ const { bakedTerrainSource } = await import(pathToFileURL(resolve(ROOT, 'src/shared/engine/world/bakedTerrainField.ts')).href);
78
+ if (bakedTerrainSource(def.ground.terrain) !== null) continue;
79
+ // the bake holds the authored (unshifted) heights: a field shifted at runtime (its `datum`, Driftwood's G164 drop)
80
+ // installs this bake shifted by it (src/shared/engine/world/Heightfield.ts)
81
+ const terrain = def.ground.terrain, datum = terrain.datum ?? 0, authored = { heightAt: (x, z) => terrain.heightAt(x, z) - datum };
82
+ const res = TERRAIN_RES;
83
+ const dir = resolve(OUT, def.slug);
84
+ const meta = resolve(dir, 'terrain.json');
85
+ const bin = resolve(dir, 'terrain.bin');
86
+ const tag = def.slug;
87
+ const t0 = performance.now();
88
+ const n = res * res;
89
+ const header = 24;
90
+ const buf = new ArrayBuffer(header + n * 4 + n * 4);
91
+ const dv = new DataView(buf);
92
+ dv.setUint8(0, 0x57); dv.setUint8(1, 0x53); dv.setUint8(2, 0x54); dv.setUint8(3, 0x52); // 'WSTR'
93
+ const lhash = landscapeHash(authored, CHUNK_SIZE); // retained header field; staleness is checked by comparing the full bake
94
+ dv.setUint32(4, VERSION, true); dv.setUint32(8, res, true); dv.setFloat32(12, CHUNK_SIZE, true); dv.setUint32(16, def.seed >>> 0, true); dv.setUint32(20, lhash, true);
95
+ const heights = new Float32Array(buf, header, n);
96
+ const splat = new Uint8Array(buf, header + n * 4, n * 4);
97
+ const d = CHUNK_SIZE / (res - 1);
98
+ let min = Infinity, max = -Infinity;
99
+ for (let iz = 0; iz < res; iz++) {
100
+ const z = -CHUNK_HALF + iz * d;
101
+ for (let ix = 0; ix < res; ix++) {
102
+ const x = -CHUNK_HALF + ix * d;
103
+ const i = iz * res + ix;
104
+ const h = authored.heightAt(x, z);
105
+ heights[i] = h; if (h < min) min = h; if (h > max) max = h;
106
+ const s = def.ground.terrain.splatAt(x, z);
107
+ // quantise so the four bytes sum to exactly 255 (largest weight absorbs the rounding)
108
+ const q = s.map((w) => Math.round(w * 255));
109
+ const sum = q[0] + q[1] + q[2] + q[3];
110
+ let k = 0; for (let j = 1; j < 4; j++) if (q[j] > q[k]) k = j;
111
+ q[k] += 255 - sum;
112
+ splat[i * 4] = q[0]; splat[i * 4 + 1] = q[1]; splat[i * 4 + 2] = q[2]; splat[i * 4 + 3] = q[3];
113
+ }
114
+ }
115
+ const tp = performance.now();
116
+ const section = placementSection(def, buf);
117
+ const bytes = section ? Buffer.concat([Buffer.from(buf), section.bytes]) : Buffer.from(buf);
118
+ if (section) console.log(`bake: ${tag} placement: ${section.trees} trees, ${section.decisions} undergrowth candidates → ${section.counts.join(' / ')} kept, ${section.bytes.length} B in ${Math.round(performance.now() - tp)} ms`);
119
+ const digest = outputHash(bytes);
120
+ output.put(bin, bytes);
121
+ output.put(meta, jsonBytes({ hash: digest, version: VERSION, res, size: CHUNK_SIZE, seed: def.seed, landscapeHash: lhash, bytes: bytes.byteLength, placement: section ? { decisions: section.decisions, counts: section.counts } : null, heightRange: [min, max] }));
122
+ console.log(`bake: ${tag} terrain ${res}² → ${(buf.byteLength / 1024).toFixed(0)} KB in ${Math.round(performance.now() - t0)} ms (h ${min.toFixed(1)}…${max.toFixed(1)} m, ${digest})`);
123
+ }
124
+ output.finish();