@wildshard/engine 0.1.18 → 0.1.19

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 (293) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/budgets/shard-size-baseline.json +10 -4
  3. package/index.html +6 -4
  4. package/lint/engine-sides.json +14 -0
  5. package/lint/ratchet.json +0 -68
  6. package/lint/ratchet.mjs +1 -4
  7. package/package.json +17 -15
  8. package/scripts/agent-sync.mjs +27 -8
  9. package/scripts/app-build/appCook.ts +8 -2
  10. package/scripts/app-build/catalogue.ts +10 -7
  11. package/scripts/app-build/gen.ts +22 -2
  12. package/scripts/app-build/isolation.d.mts +10 -0
  13. package/scripts/app-build/isolation.mjs +47 -0
  14. package/scripts/app-build/outputProducts.mjs +4 -0
  15. package/scripts/app-build/previousDeploy.d.mts +2 -0
  16. package/scripts/app-build/previousDeploy.mjs +10 -1
  17. package/scripts/app-build/serviceWorker.ts +10 -3
  18. package/scripts/app-build/splitOutput.mjs +4 -2
  19. package/scripts/app-build/standaloneOutput.d.mts +1 -1
  20. package/scripts/app-build/standaloneOutput.mjs +5 -3
  21. package/scripts/app-build/titlePreloads.d.mts +8 -0
  22. package/scripts/app-build/titlePreloads.mjs +69 -0
  23. package/scripts/app-build/worldMapAtlas.ts +10 -6
  24. package/scripts/app-build-pipeline.mjs +31 -3
  25. package/scripts/built-product.mjs +1 -1
  26. package/scripts/cell-mem-bench.mjs +81 -23
  27. package/scripts/check-custom-sections-zero.mjs +1 -1
  28. package/scripts/check-devserver.mjs +2 -0
  29. package/scripts/check-models.mjs +2 -0
  30. package/scripts/check-paths.allow.json +0 -115
  31. package/scripts/check-paths.mjs +2 -2
  32. package/scripts/check-platform-ratchets.mjs +2 -3
  33. package/scripts/chromium-keychain.d.mts +1 -1
  34. package/scripts/ci-static.sh +1 -1
  35. package/scripts/committed-fast-checks.mjs +0 -2
  36. package/scripts/content-sw-proof.mjs +2 -1
  37. package/scripts/cook-worker.mjs +9 -3
  38. package/scripts/cook.mjs +18 -5
  39. package/scripts/docs-inputs-proof.mjs +6 -6
  40. package/scripts/e7-voice-staging-proof.mjs +1 -1
  41. package/scripts/gate-related-tests.mjs +1 -1
  42. package/scripts/git-source-view.mjs +1 -1
  43. package/scripts/goldens/capture.mjs +17 -5
  44. package/scripts/grid-crossing-nightly.mjs +59 -0
  45. package/scripts/grid-crossing-qa.mjs +44 -0
  46. package/scripts/grid-crossing-ratchet.d.mts +6 -0
  47. package/scripts/grid-crossing-ratchet.mjs +52 -0
  48. package/scripts/grid-crossing-report.mjs +122 -0
  49. package/scripts/grid-crossing-stages.mjs +47 -0
  50. package/scripts/grid-crossing-tour.mjs +277 -0
  51. package/scripts/grid-crossing-walk-proof.mjs +124 -17
  52. package/scripts/host-ruler/roots.mjs +2 -2
  53. package/scripts/host-ruler.d.mts +91 -0
  54. package/scripts/host-ruler.mjs +565 -0
  55. package/scripts/live-prepush.py +3 -5
  56. package/scripts/loading-flow/benchmax.mjs +3 -1
  57. package/scripts/look-loop/test_look_loop.py +1 -38
  58. package/scripts/memory-proof.mjs +61 -6
  59. package/scripts/mmo-bots.mjs +8 -2
  60. package/scripts/net-lag-proof.mjs +219 -0
  61. package/scripts/night-market-kit-budget.mjs +2 -2
  62. package/scripts/offline-proof-rules.d.mts +2 -2
  63. package/scripts/offline-proof-rules.mjs +15 -4
  64. package/scripts/offline-proof.mjs +74 -13
  65. package/scripts/{shard-platform-output.mjs → output-reader.mjs} +1 -1
  66. package/scripts/page-release/content.d.mts +9 -1
  67. package/scripts/page-release/content.mjs +131 -17
  68. package/scripts/page-release/flow.mjs +36 -10
  69. package/scripts/page-release/stamp-content.mjs +24 -0
  70. package/scripts/page-release.mjs +28 -6
  71. package/scripts/page-size.mjs +2 -1
  72. package/scripts/pin-packs.ts +1 -1
  73. package/scripts/pins.ts +4 -4
  74. package/scripts/precommit-checks.mjs +1 -7
  75. package/scripts/precommit-jobs.mjs +0 -1
  76. package/scripts/precommit-source.py +1 -9
  77. package/scripts/program-discovery.mjs +1 -1
  78. package/scripts/ring-cook.mjs +2 -1
  79. package/scripts/ring-step.ts +1 -1
  80. package/scripts/script-scenario-metrics.mjs +1 -1
  81. package/scripts/shard-output-content.d.mts +1 -1
  82. package/scripts/shard-output-content.mjs +2 -2
  83. package/scripts/shard-outputs.mjs +1 -1
  84. package/scripts/shard-public.mjs +1 -1
  85. package/scripts/shard-wire-meter.mjs +24 -9
  86. package/scripts/side-parity.mjs +4 -2
  87. package/scripts/standalone-smoke.mjs +8 -3
  88. package/scripts/tier-textures.mjs +22 -4
  89. package/scripts/tile-bake-report.mjs +1 -1
  90. package/scripts/tile-bakes.mjs +1 -1
  91. package/scripts/title-assets.generated.json +2 -0
  92. package/scripts/tsconfig.json +0 -2
  93. package/scripts/vendor/basisu/LICENSE +201 -0
  94. package/scripts/vendor/basisu/basis_encoder.cjs +2 -0
  95. package/scripts/vendor/basisu/basis_encoder.wasm +0 -0
  96. package/scripts/vitest-push-guards.config.ts +1 -1
  97. package/scripts/vps/harden-apply.sh +4 -1
  98. package/scripts/vps/harden-prometheus.sh +176 -0
  99. package/scripts/vps/harden.sh +16 -0
  100. package/scripts/vps/rebuild.sh +18 -0
  101. package/scripts/vps/wildshard-config +6 -2
  102. package/scripts/worker-client-play.mjs +6 -2
  103. package/scripts/worktree-reap.mjs +5 -5
  104. package/src/.oxlintrc.json +3 -182
  105. package/src/client/app/entry.ts +10 -2
  106. package/src/client/engine/boot/bootCache.ts +2 -2
  107. package/src/client/engine/boot/bootTrace.ts +12 -4
  108. package/src/client/engine/boot/browserChunkRecovery.ts +4 -0
  109. package/src/client/engine/boot/clearDownloads.ts +5 -2
  110. package/src/client/engine/boot/contentRead.ts +11 -0
  111. package/src/client/engine/boot/gpuFiles.ts +4 -2
  112. package/src/client/engine/boot/lastEnd.ts +99 -12
  113. package/src/client/engine/boot/texturePack.ts +2 -2
  114. package/src/client/engine/net/netPlayer.ts +19 -2
  115. package/src/client/engine/net/otherPlayers.ts +55 -8
  116. package/src/client/engine/net/predictor.ts +33 -9
  117. package/src/client/engine/net/redial.ts +29 -6
  118. package/src/client/engine/pwa/cacheNames.ts +58 -1
  119. package/src/client/engine/pwa/sw.js +41 -11
  120. package/src/client/engine/render/dataLook.ts +152 -40
  121. package/src/client/engine/render/families/detail.ts +89 -0
  122. package/src/client/engine/render/families/pbr.ts +7 -1
  123. package/src/client/engine/render/look.ts +6 -0
  124. package/src/client/engine/render/lookLight.ts +103 -11
  125. package/src/client/engine/render/lookRender.ts +105 -0
  126. package/src/client/engine/saves/indexed.ts +2 -2
  127. package/src/client/engine/telemetry/browserErrors.ts +24 -3
  128. package/src/client/engine/telemetry/staleBuildReport.ts +22 -1
  129. package/src/client/engine/ui/debugOptions.ts +1 -1
  130. package/src/client/engine/ui/lookLightOption.ts +10 -6
  131. package/src/client/engine/ui/namePrompt.ts +7 -0
  132. package/src/client/game/boot/audio.generated.ts +114 -114
  133. package/src/client/game/boot/bytes.generated.ts +118 -220
  134. package/src/client/game/boot/placedSpawn.ts +35 -0
  135. package/src/client/game/boot/spawnDownload.ts +25 -6
  136. package/src/client/game/boot/versions.generated.ts +7 -5
  137. package/src/client/game/devScene/hook.ts +291 -0
  138. package/src/client/game/grid/cellLoading.ts +12 -2
  139. package/src/client/game/grid/live.ts +112 -16
  140. package/src/client/game/grid/liveSession.ts +33 -2
  141. package/src/client/game/grid/mapAtlas.ts +18 -4
  142. package/src/client/game/grid/minimapBlend.ts +15 -5
  143. package/src/client/game/grid/oneWayIn.ts +5 -1
  144. package/src/client/game/grid/placedCells.ts +114 -12
  145. package/src/client/game/grid/placedContent.ts +85 -0
  146. package/src/client/game/grid/prefetch.ts +9 -1
  147. package/src/client/game/grid/productInputs.ts +3 -1
  148. package/src/client/game/grid/productLeases.ts +4 -1
  149. package/src/client/game/grid/products.ts +8 -4
  150. package/src/client/game/grid/roadLook.ts +24 -15
  151. package/src/client/game/grid/roadLookPlatform.ts +4 -2
  152. package/src/client/game/grid/roadResources.ts +24 -0
  153. package/src/client/game/grid/roadSign.worker.ts +4 -3
  154. package/src/client/game/grid/roadSignPaint.ts +16 -5
  155. package/src/client/game/grid/roadSignPreparation.ts +4 -3
  156. package/src/client/game/grid/roadSignScale.ts +8 -0
  157. package/src/client/game/grid/roadTileCapture.worker.ts +3 -1
  158. package/src/client/game/grid/roadTileCaptureWorker.ts +2 -1
  159. package/src/client/game/grid/roadTileWrite.worker.ts +4 -2
  160. package/src/client/game/grid/roadTileWriter.ts +2 -1
  161. package/src/client/game/grid/screenReports.ts +39 -0
  162. package/src/client/game/grid/session.ts +20 -4
  163. package/src/client/game/grid/softWallLook.ts +25 -8
  164. package/src/client/game/mainMenu.ts +6 -2
  165. package/src/client/game/plugin/context.ts +2 -1
  166. package/src/client/game/plugin/followedMovers.ts +7 -1
  167. package/src/client/game/quest/figures.ts +2 -0
  168. package/src/client/game/session/finish.ts +6 -0
  169. package/src/client/game/shardfile/assetCache.ts +4 -4
  170. package/src/client/game/shardfile/client.ts +4 -1
  171. package/src/client/game/shardfile/clientAssets.ts +9 -5
  172. package/src/client/game/shardfile/clientLibrary.ts +1 -1
  173. package/src/client/game/shardfile/clientWater.ts +51 -9
  174. package/src/client/game/shardfile/clientWorker.ts +19 -7
  175. package/src/client/game/shardfile/look.ts +8 -3
  176. package/src/client/game/shardfile/product.ts +34 -7
  177. package/src/client/game/shardfile/productChecks.ts +2 -2
  178. package/src/client/game/shardfile/productCost.ts +2 -2
  179. package/src/client/game/shardfile/readAsset.ts +2 -2
  180. package/src/client/game/shardfile/reflectingWater.ts +73 -0
  181. package/src/client/game/shardfile/seatGrace.ts +9 -0
  182. package/src/client/game/shardfile/skinPlayback.ts +1 -1
  183. package/src/client/game/systems/npc/figureRows.ts +9 -0
  184. package/src/client/host/online/doctor.ts +2 -0
  185. package/src/client/host/online/linkScreens.ts +30 -4
  186. package/src/client/host/standalone/shell.ts +4 -0
  187. package/src/cook/game/astc6Encode.ts +55 -0
  188. package/src/cook/game/astc6Wasm.ts +176 -0
  189. package/src/cook/game/cookJob.ts +30 -6
  190. package/src/cook/game/cookWorker.ts +38 -11
  191. package/src/cook/game/entryTotals.ts +51 -24
  192. package/src/cook/game/shardCook.ts +35 -7
  193. package/src/cook/game/spawnParts.ts +33 -8
  194. package/src/cook/game/tierTextures.ts +87 -14
  195. package/src/server/engine/ai/bossContenders.ts +27 -3
  196. package/src/server/engine/ai/phases.ts +1 -1
  197. package/src/server/engine/entities/creatureRefill.ts +9 -2
  198. package/src/server/engine/net/ledgerBook.ts +2 -0
  199. package/src/server/engine/net/room.ts +15 -0
  200. package/src/server/engine/sim/respawn.ts +8 -0
  201. package/src/server/game/boss/arenaRoomEntry.ts +5 -1
  202. package/src/server/game/boss/arenaRoomNative.ts +2 -0
  203. package/src/server/game/boss/native.ts +2 -1
  204. package/src/server/game/boss/nativeWake.ts +9 -5
  205. package/src/server/game/boss/openArenaNative.ts +2 -2
  206. package/src/server/game/boss/openArenaRoom.ts +3 -1
  207. package/src/server/game/grid/room.ts +44 -3
  208. package/src/server/game/grid/simulation.ts +23 -0
  209. package/src/server/game/population/mixedRoster.ts +11 -3
  210. package/src/server/game/population/nativeBake.ts +8 -3
  211. package/src/server/game/population/nativeReturns.ts +6 -3
  212. package/src/server/game/population/roster.ts +5 -3
  213. package/src/server/game/quest/eliteDrops.ts +15 -4
  214. package/src/server/game/quest/nativeChapter.ts +1 -1
  215. package/src/server/game/quest/nativeComposition.ts +5 -1
  216. package/src/server/game/riding/nativeTaming.ts +10 -6
  217. package/src/server/game/riding/wildRefill.ts +3 -2
  218. package/src/server/game/shardfile/bossRow.ts +4 -2
  219. package/src/server/game/shardfile/homeKeeper.ts +4 -2
  220. package/src/server/host/bake/headlessWorker.ts +1 -1
  221. package/src/server/host/dedicated/cellHost.ts +98 -16
  222. package/src/server/host/dedicated/cellProtocol.ts +16 -1
  223. package/src/server/host/dedicated/cellWorker.ts +8 -5
  224. package/src/server/host/dedicated/dedicatedRoom.ts +16 -6
  225. package/src/server/host/dedicated/gridCells.ts +15 -6
  226. package/src/server/host/dedicated/loopLag.ts +21 -0
  227. package/src/server/host/dedicated/main.ts +13 -5
  228. package/src/server/host/dedicated/packReader.ts +3 -3
  229. package/src/server/host/dedicated/placementFile.ts +3 -2
  230. package/src/server/host/dedicated/roadRoom.ts +34 -4
  231. package/src/server/host/dedicated/serverMetrics.ts +126 -0
  232. package/src/server/host/dedicated/socketPump.ts +21 -4
  233. package/src/server/host/dedicated/workerTable.ts +15 -4
  234. package/src/server/host/dedicated/world.ts +65 -7
  235. package/src/server/host/headless.ts +1 -1
  236. package/src/server/host/headlessRuntime.ts +1 -1
  237. package/src/server/host/standalone/roadWorld.ts +15 -3
  238. package/src/server/host/standalone/serverHost.ts +4 -1
  239. package/src/server/host/standalone/serverProduct.ts +41 -11
  240. package/src/shared/engine/ai/BossBrain.ts +8 -0
  241. package/src/shared/game/boss/wake.ts +6 -0
  242. package/src/shared/game/devColliders.ts +47 -0
  243. package/src/shared/game/eliteSystem.ts +8 -0
  244. package/src/shared/game/grid/allocator.ts +3 -0
  245. package/src/shared/game/grid/placementRows.ts +5 -2
  246. package/src/shared/game/plugin/bossRecord.ts +3 -2
  247. package/src/shared/game/shardfile/ktx2Targets.ts +23 -3
  248. package/src/shared/game/shardfile/outputDescriptor.ts +9 -5
  249. package/src/shared/game/shardfile/packReads.ts +28 -3
  250. package/src/shared/game/shardfile/serverPack.ts +9 -0
  251. package/src/shared/game/systems/looks/trampleField.ts +2 -0
  252. package/src/shared/host/formatJsonSchema.ts +2 -2
  253. package/src/tsconfig.json +1 -27
  254. package/lint/host-reviews.json +0 -14
  255. package/lint/listed-view-code.json +0 -13
  256. package/lint/platform-pending-users.json +0 -2520
  257. package/lint/platform-pending-users.md +0 -18
  258. package/scripts/cell-mem/metrics-preload.mjs +0 -65
  259. package/scripts/check-closed-components.d.mts +0 -7
  260. package/scripts/check-closed-components.mjs +0 -87
  261. package/scripts/g571-hosts.d.mts +0 -3
  262. package/scripts/g571-hosts.mjs +0 -54
  263. package/scripts/host-ruler/register-migrate.mjs +0 -242
  264. package/scripts/listed-view-code.d.mts +0 -7
  265. package/scripts/listed-view-code.mjs +0 -93
  266. package/scripts/look-loop/fit-lut.py +0 -142
  267. package/scripts/look-loop/palette-delta.py +0 -183
  268. package/scripts/look-loop/regions/driftwood-isle.json +0 -25
  269. package/scripts/look-loop/regions/far-reach.json +0 -225
  270. package/scripts/look-loop/regions/open-plot-night-market.json +0 -855
  271. package/scripts/look-loop/regions/pine-hollow.json +0 -22
  272. package/scripts/look-loop/regions/sunscar-dunes.json +0 -281
  273. package/scripts/platform-chargebacks.d.mts +0 -9
  274. package/scripts/platform-chargebacks.mjs +0 -135
  275. package/scripts/shard-component-spans.d.mts +0 -15
  276. package/scripts/shard-component-spans.mjs +0 -18
  277. package/scripts/shard-components.d.mts +0 -16
  278. package/scripts/shard-components.mjs +0 -363
  279. package/scripts/shard-platform-hosts.d.mts +0 -2
  280. package/scripts/shard-platform-hosts.mjs +0 -93
  281. package/scripts/shard-platform-sections.d.mts +0 -2
  282. package/scripts/shard-platform-sections.mjs +0 -9
  283. package/scripts/shard-platform.d.mts +0 -76
  284. package/scripts/shard-platform.mjs +0 -1072
  285. package/src/.oxfmtrc.json +0 -48
  286. package/src/scripts/ask-new.sh +0 -29
  287. package/src/scripts/asks.mjs +0 -142
  288. package/src/scripts/herdr-prompt.sh +0 -34
  289. package/src/scripts/herdr-question-open.sh +0 -67
  290. package/src/scripts/md-wrap.mjs +0 -229
  291. /package/scripts/{shard-platform-format.d.mts → node-format.d.mts} +0 -0
  292. /package/scripts/{shard-platform-format.mjs → node-format.mjs} +0 -0
  293. /package/scripts/{shard-platform-output.d.mts → output-reader.d.mts} +0 -0
@@ -1,9 +1,9 @@
1
1
  /// <reference types="node" />
2
2
  import * as v from 'valibot';
3
3
  // oxlint-disable-next-line import/no-nodejs-modules -- The cook is a Node process (J28).
4
- import {existsSync, readFileSync, mkdirSync, readdirSync} from 'node:fs';
4
+ import {existsSync, readFileSync, mkdirSync, readdirSync, rmSync, writeFileSync} from 'node:fs';
5
5
  // oxlint-disable-next-line import/no-nodejs-modules -- The cook is a Node process (J28).
6
- import {join, relative, resolve, sep} from 'node:path';
6
+ import {dirname, join, relative, resolve, sep} from 'node:path';
7
7
  import {PickupCatalogueSchema} from '@wildshard/format/specimenCatalogue';
8
8
  import {parseOutputIndex} from '@wildshard/format/outputIndex';
9
9
  // oxlint-disable-next-line import/no-nodejs-modules -- The cook is a Node process (J28).
@@ -11,7 +11,7 @@ import {createHash} from 'node:crypto';
11
11
  import {readSpawnManifest, type SpawnFile, type SpawnManifest, type SpawnManifestAddress} from '../../shared/game/shardfile/spawnManifest';
12
12
  import {assetIdentities, audioTableRefs, bundlerSources as manifestSources, identityFiles, joinRefs, standInRefs, packMembers, readFenceReads, resolveFenceReads, sectionRefs, type PackPart} from './fenceReads';
13
13
  import {createPartEncoder, writeSpawnManifestFiles, writeSpawnParts, type CookedMember, type SpawnPartEncoding} from './spawnParts';
14
- import {cookParts, cookShardServerPack, openShardfile, servedAsIs, shardHalf, stalePreparationLine, type PreparationState} from './shardCook';
14
+ import {cookParts, cookShardServerPack, openShardfile, preparedShardHalf, servedAsIs, stalePreparationLine, type CookTiers, type PreparationState, type ShardHalf} from './shardCook';
15
15
  import type {ServerPack} from '../../shared/game/shardfile/serverPack';
16
16
  import {contentNamed} from './contentNamed';
17
17
  // oxlint-disable-next-line import/no-nodejs-modules -- The cook is a Node process (J28).
@@ -64,6 +64,9 @@ export interface BuildHalf {
64
64
  readonly rendererRevision?: string;
65
65
  readonly cacheName: string;
66
66
  readonly gpuFiles: Parameters<typeof standInRefs>[0];
67
+ /** S12d: the cook's per-tier textures (`defaultCookTiers`), as the box's cook makes them. None: every tier reads the
68
+ * product's own table. A preparation-only build never takes them: a fence records the product's own URLs. */
69
+ readonly tiers?: () => Promise<CookTiers>;
67
70
  /** The boot tables' common and pack sources, as `assetTables.mjs` bakes them. */
68
71
  readonly tables: (root: string) => {readonly common: {readonly source: string}; readonly all: {readonly source: string}; readonly packs: Readonly<Record<string, {readonly source: string}>>};
69
72
  readonly tableAddresses: (root: string) => {readonly common: Address; readonly all: Address; readonly packs: Readonly<Record<string, Address>>};
@@ -80,17 +83,21 @@ export function desktopFence(reads: readonly string[], urls: readonly string[]):
80
83
  const desktop = new Map(urls.filter(url => !phoneOnly(url)).sort((a, b) => named(a) - named(b)).map(url => [plain(url), url]));
81
84
  return [...new Set(reads.flatMap(url => { if (!phoneOnly(url)) return [url]; const own = desktop.get(plain(url)); return own === undefined ? [] : [own]; }))];
82
85
  }
83
- /** S4: a shard's phone stand-ins laid over the engine's table (`data/astc6.generated.json`). A phone reads the overlay's
84
- * file under the same served URL, so its fence read keeps the identity it had. */
85
- function phoneOverlay(root: string, directory: string): Readonly<Record<string, string>> {
86
- const path = join(root, 'src/shards', directory, 'data/astc6.generated.json');
87
- if (existsSync(path)) return v.parse(v.record(v.string(), v.string()), JSON.parse(readFileSync(path, 'utf8')));
88
- // Any other shard's overlay is in its product: the discovery's phone table, the rows that name a 6×6 file.
89
- const discovery = join(root, 'out', directory, 'mounts/shardfiles', directory, 'discovery.json');
90
- if (!existsSync(discovery)) return {};
91
- const table = v.safeParse(v.looseObject({native: v.looseObject({ktx2: v.looseObject({GPU_FILES: v.looseObject({phone: v.record(v.string(), v.string())})})})}), JSON.parse(readFileSync(discovery, 'utf8')));
92
- return table.success ? Object.fromEntries(Object.entries(table.output.native.ktx2.GPU_FILES.phone).filter(([, url]) => url.includes('/astc6/'))) : {};
86
+ /** A phone file in place of the one its source names on a desktop: S4's hand-made 6×6 overlay (`/assets/<shard>/astc6/`)
87
+ * or the cook's own phone tier (`<name>.t<size>-<hash8>.<ext>`, S12b). */
88
+ // oxlint-disable-next-line typescript/no-inferrable-types -- isolatedDeclarations wants the type written.
89
+ export const PHONE_STAND_IN: RegExp = /\/astc6\/|\.t[0-9]+-[a-f0-9]{8}\.(?:ktx2|glb)$/u;
90
+ /** S4, S12d: a shard's phone stand-ins, read off the discovery document the phone reads (the cook's tiered table): the
91
+ * phone rows that name a stand-in. A phone reads that file under the source's URL, so its fence read keeps its identity. */
92
+ export function phoneOverlay(discovery: Uint8Array | undefined): Readonly<Record<string, string>> {
93
+ if (discovery === undefined) return {};
94
+ const table = v.safeParse(v.looseObject({native: v.looseObject({ktx2: v.looseObject({GPU_FILES: v.looseObject({phone: v.record(v.string(), v.string())})})})}), JSON.parse(new TextDecoder().decode(discovery)));
95
+ return table.success ? Object.fromEntries(Object.entries(table.output.native.ktx2.GPU_FILES.phone).filter(([, url]) => PHONE_STAND_IN.test(url))) : {};
93
96
  }
97
+ /** S12d: where a build keeps each shard's discovery document as its cook serves it, inside that build's own output, until
98
+ * `copyOutputProducts` lays it over the product's (and removes the folder); the catalogue hashes it there. */
99
+ export const SERVED_DISCOVERY = '.discovery';
100
+ export function servedDiscoveryPath(outDir: string, directory: string): string { return join(outDir, SERVED_DISCOVERY, directory, 'discovery.json'); }
94
101
  /** DL5: a shard's packs set, `src/shards/<slug>/packs-set.json` (`{"version": 1, "set": "fence"}`): 'fence' packs the
95
102
  * default set (what the boot reads before control), absent packs the whole level. Anything else fails the build. */
96
103
  export function readPacksSet(path: string): 'whole' | 'fence' {
@@ -201,22 +208,42 @@ export async function writeSpawnManifests(outDir: string, input: unknown,
201
208
  const clientAt: Record<string, {phone: number; desktop: number}> = {}, gridAt: Record<string, Record<string, {phone: number; desktop: number}>> = {};
202
209
  const packsClientAt: typeof clientAt = {}, packsGridAt: typeof gridAt = {};
203
210
  const encoder = createPartEncoder(), cooked = new Map<string, CookedMember>(), serverPacks: Record<string, ServerPack> = {};
211
+ // C1: the shard half comes from the shardfile alone, one tarball per product, and its parts from the shard's own cook.
212
+ // S12d: with the cook's tiers, the phone reads the tiered table and its own files, as from the box's cook.
213
+ const tiers = build.preparationOnly === true || build.tiers === undefined ? {} : await build.tiers();
214
+ rmSync(join(output, SERVED_DISCOVERY), {recursive: true, force: true});
215
+ // Only what the manifests' loop needs: an opened product stays open no longer than its own pass (no 27 at once).
216
+ const served = new Map<string, NonNullable<ShardHalf['discovery']>>(), open = new Map<string, () => Promise<ShardHalf>>();
217
+ for (const prefix of Object.keys(bank.products)) {
218
+ const directory = prefix.split('/')[2]; if (directory === undefined || !/^[a-z0-9_-]+$/u.test(directory)) throw new Error('Invalid spawn shard');
219
+ const product = join(root, 'out', directory), index = parseOutputIndex(JSON.parse(readFileSync(join(product, 'shard.json'), 'utf8')));
220
+ if (index.runtime !== null && (!('kind' in index.runtime) || index.runtime.kind !== 'client-plan')) continue;
221
+ const reopen = (): Promise<ShardHalf> => preparedShardHalf(openShardfile(productTarball(product), {preparationOnly: build.preparationOnly === true,
222
+ ...(build.rendererRevision === undefined ? {} : {renderer: build.rendererRevision})}), wireBytes, undefined, directory, tiers);
223
+ const half = await reopen();
224
+ open.set(directory, reopen);
225
+ // What no part answers serves at its URL: the tiered discovery document (beside its product) and the phone's own files.
226
+ if (half.discovery !== undefined) {
227
+ served.set(directory, half.discovery);
228
+ const file = servedDiscoveryPath(output, directory); mkdirSync(dirname(file), {recursive: true}); writeFileSync(file, half.discovery.bytes);
229
+ }
230
+ for (const row of half.tierFiles) { const file = join(output, row.url.slice(1)); mkdirSync(dirname(file), {recursive: true}); writeFileSync(file, half.read(row.url)); }
231
+ }
204
232
  // C3: a grid page reads every primary neighbour's discovery document before it enters.
205
233
  const discoveries = Object.keys(bank.products).flatMap(prefix => {
206
234
  const directory = prefix.split('/')[2] ?? '', url = `/shardfiles/${directory}/discovery.json`, file = join(root, 'out', directory, 'mounts', url);
207
- if (!existsSync(file)) return [];
208
- const bytes = readFileSync(file), hash = createHash('sha256').update(bytes).digest('hex'), row = {url: `${url}?v=${hash.slice(0, 8)}`, bytes: bytes.length, hash, wireBytes: wireBytes(url, bytes, hash)};
235
+ const own = served.get(directory);
236
+ if (own === undefined && !existsSync(file)) return [];
237
+ const bytes = own?.bytes ?? readFileSync(file), hash = createHash('sha256').update(bytes).digest('hex'), row = {url: `${url}?v=${hash.slice(0, 8)}`, bytes: bytes.length, hash, wireBytes: wireBytes(url, bytes, hash)};
209
238
  sources.set(row.url, () => bytes);
210
239
  return [row];
211
240
  });
212
241
  const preparations: Partial<Record<PreparationState, string[]>> = {};
213
242
  for (const prefix of Object.keys(bank.products)) {
214
- const directory = prefix.split('/')[2]; if (directory === undefined || !/^[a-z0-9_-]+$/u.test(directory)) throw new Error('Invalid spawn shard');
215
- const product = join(root, 'out', directory), index = parseOutputIndex(JSON.parse(readFileSync(join(product, 'shard.json'), 'utf8')));
216
- if (index.runtime !== null && (!('kind' in index.runtime) || index.runtime.kind !== 'client-plan')) continue;
217
- // C1: the shard half comes from the shardfile alone, one tarball per product, and its parts from the shard's own cook.
218
- const half = shardHalf(openShardfile(productTarball(product), {preparationOnly: build.preparationOnly === true,
219
- ...(build.rendererRevision === undefined ? {} : {renderer: build.rendererRevision})}), wireBytes, undefined, directory), {sections} = half.shard;
243
+ // The second opening's encodes come straight back from the first's (the encoder keeps them).
244
+ const directory = prefix.split('/')[2] ?? '', reopen = open.get(directory); if (reopen === undefined) continue;
245
+ const half = await reopen();
246
+ const index = parseOutputIndex(JSON.parse(readFileSync(join(root, 'out', directory, 'shard.json'), 'utf8'))), {sections} = half.shard, overlay = phoneOverlay(half.discovery?.bytes);
220
247
  // J262 (SP1a): a preparation from another renderer cooks as it is, and the build says which shards took that path.
221
248
  (preparations[half.shard.preparation] ??= []).push(directory);
222
249
  if (half.shard.preparation === 'stale') console.warn(stalePreparationLine(directory));
@@ -242,7 +269,7 @@ export async function writeSpawnManifests(outDir: string, input: unknown,
242
269
  const urls = publicAssets(), members = [...packParts().values()].flatMap(part => part.members);
243
270
  // Section references, then the boot tables' audio slots (Pine's colliding sfx revisions).
244
271
  const refs = joinRefs(sectionRefs(sections, new Set([...urls, ...members].map(url => url.split('?')[0] ?? url))),
245
- audioTableRefs(JSON.parse(build.tables(root).common.source) as {sfx?: unknown; music?: unknown}), standInRefs({...build.gpuFiles, phone: {...build.gpuFiles['phone'], ...phoneOverlay(root, directory)}}));
272
+ audioTableRefs(JSON.parse(build.tables(root).common.source) as {sfx?: unknown; music?: unknown}), standInRefs({...build.gpuFiles, phone: {...build.gpuFiles['phone'], ...overlay}}));
246
273
  // An identity is one file or none: one naming two distinct files (two vendor versions, sp-x9) refuses, never the first.
247
274
  const owners = identityFiles(assetIdentities(urls, packParts(), refs, {digests: assetDigests, sources: bundlerSources()}));
248
275
  const ambiguous = shell.filter(identity => (owners.get(identity)?.length ?? 0) > 1);
@@ -292,8 +319,8 @@ export async function writeSpawnManifests(outDir: string, input: unknown,
292
319
  if (inventory.length !== extra[1] || createHash('sha256').update(inventory).digest('hex') !== extra[3]) throw new Error('Spawn public inventory differs');
293
320
  // S4 on Sky: this inventory names a page's image sources on every tier. A phone with an overlay reads the 6×6 file in
294
321
  // their place, and one with no ASTC reads them loose, so they stay off the phone's list.
295
- const overlay = tier === 'phone' ? phoneOverlay(root, directory) : {};
296
- for (const [url, bytes] of v.parse(files, JSON.parse(new TextDecoder().decode(inventory)))) if (!Object.hasOwn(overlay, url.split('?')[0] ?? url)) add(asset(url, bytes));
322
+ const standIns = tier === 'phone' ? overlay : {};
323
+ for (const [url, bytes] of v.parse(files, JSON.parse(new TextDecoder().decode(inventory)))) if (!Object.hasOwn(standIns, url.split('?')[0] ?? url)) add(asset(url, bytes));
297
324
  }
298
325
  // Required code can resolve while bulk content downloads. Keep each
299
326
  // partition's original order, every row and the full download barrier.
@@ -23,7 +23,8 @@ import {readTar} from '@wildshard/format/tar';
23
23
  import type {ServerPack, ServerPackPart} from '../../shared/game/shardfile/serverPack';
24
24
  import {serverReadsFile} from '../../shared/game/shardfile/outputDescriptor';
25
25
  import {entryReadSet} from './entryReadSet';
26
- import {phoneTier} from './tierTextures';
26
+ import {phoneTier, type Astc6Encode} from './tierTextures';
27
+ import {wasmAstc6} from './astc6Wasm';
27
28
  import {createPartEncoder, packable, writeSpawnParts, type CookedMember, type PartEncoder} from './spawnParts';
28
29
 
29
30
  export type CookTier = 'phone' | 'desktop';
@@ -160,6 +161,10 @@ export interface ShardHalf {
160
161
  /** The mounts of one manifest. */
161
162
  readonly mountRows: (tier: CookTier, set: CookSet) => readonly SpawnFile[];
162
163
  readonly read: (url: string) => Uint8Array;
164
+ /** S12d: the phone tier's own files (`phoneTier`), each at its content-named URL; none with the tiers off. */
165
+ readonly tierFiles: readonly SpawnFile[];
166
+ /** The discovery document as this cook serves it (the phone tier's table when the tiers are on), and its row. */
167
+ readonly discovery: {readonly row: SpawnFile; readonly bytes: Uint8Array} | undefined;
163
168
  readonly stableKey: (url: string) => string | undefined;
164
169
  /** A mount's bytes as this cook serves it: the discovery document carries the phone tier's table (S12b). */
165
170
  readonly mountBytes: (row: CookMount) => Uint8Array;
@@ -167,8 +172,30 @@ export interface ShardHalf {
167
172
 
168
173
  /** S12b: what the cook does with a shard's textures per tier. `phone`: give each phone row of the KTX2 table its own
169
174
  * trimmed file (`phoneTier`), listed in the phone manifests in place of the file it stands in for. Off by default until
170
- * the cook's own 6×6 encode (S12c) can replace the hand-made sets. */
171
- export interface CookTiers {readonly phone?: boolean}
175
+ * the cook's own 6×6 encode (S12c) can replace the hand-made sets. `encode` (S12c): cook each phone row from its desktop
176
+ * master as ASTC 6×6 (`basisuAstc6`), what the phone reads once the hand-made sets are gone. */
177
+ export interface CookTiers {readonly phone?: boolean; readonly encode?: Astc6Encode
178
+ /** S12d: run the encodes a cook will ask for on worker threads first (`wasmAstc6`'s `record` and `flush`). */
179
+ readonly prepare?: {readonly record: Astc6Encode; readonly flush: () => Promise<void>}}
180
+
181
+ /** S12d: the phone tier is on by default, 6×6 by basisu's WebAssembly build: no child process, nothing to install, so
182
+ * the box's cook job and CI's bake cook what `cook --phone-astc6` did. `cacheDir` keeps encodes between cooks; a cook
183
+ * job passes none (its files can't be trusted by the next job). */
184
+ export async function defaultCookTiers(cacheDir?: string): Promise<CookTiers> {
185
+ const lanes = process.env['COOK_CPU_SLOTS'] === undefined ? undefined : Math.max(1, Number(process.env['COOK_CPU_SLOTS']) - 1);
186
+ const astc6 = await wasmAstc6({...(cacheDir === undefined ? {} : {cacheDir}), ...(lanes === undefined || !Number.isSafeInteger(lanes) ? {} : {lanes})});
187
+ return {phone: true, encode: astc6.encode, prepare: astc6};
188
+ }
189
+
190
+ /** `shardHalf` with its encodes run in parallel first, when the tiers can. */
191
+ export async function preparedShardHalf(shard: OpenShardfile, wireBytes: WireBytes, warn: ((line: string) => void) | undefined, folder: string, tiers: CookTiers): Promise<ShardHalf> {
192
+ if (tiers.phone === true && tiers.prepare !== undefined) {
193
+ // Only the encodes' inputs matter here: no file of this pass is sized or kept.
194
+ shardHalf(shard, () => 0, undefined, folder, {phone: true, encode: tiers.prepare.record});
195
+ await tiers.prepare.flush();
196
+ }
197
+ return shardHalf(shard, wireBytes, warn, folder, tiers);
198
+ }
172
199
 
173
200
  /** The shard half of every manifest: what the app build's `write` and the standalone cook share. `folder` is where the
174
201
  * product is served; a catalogue names it by slug, and today's `_template` folder is the one that differs (`template`). */
@@ -198,7 +225,7 @@ export function shardHalf(shard: OpenShardfile, wireBytes: WireBytes, warn: (lin
198
225
  const document = shard.cook?.mounts.find(row => row.url === `${prefix}discovery.json`), mounts = downloaded(shard);
199
226
  // S12b: the phone tier's files, made from the KTX2 table's own targets; the discovery document names them.
200
227
  const onTier = mountTiers(new Set(mounts.map(row => row.url))), mountByURL = new Map(mounts.map(row => [row.url, row])), mountOf = (url: string): Uint8Array | undefined => { const row = mountByURL.get(url); return row === undefined ? undefined : shard.read(row.path); };
201
- const phone = tiers.phone === true && document !== undefined ? phoneTier(shard.read(document.path), mountOf) : undefined;
228
+ const phone = tiers.phone === true && document !== undefined ? phoneTier(shard.read(document.path), mountOf, undefined, tiers.encode) : undefined;
202
229
  const documentBytes = phone?.discovery ?? (document === undefined ? undefined : shard.read(document.path));
203
230
  const discovery: readonly SpawnFile[] = document === undefined || documentBytes === undefined ? [] : [(() => {
204
231
  const hash = phone === undefined ? document.hash : sha256(documentBytes);
@@ -217,7 +244,8 @@ export function shardHalf(shard: OpenShardfile, wireBytes: WireBytes, warn: (lin
217
244
  })]));
218
245
  /** A phone list names each trimmed target's phone files in its place, and the target too while a kept row still reads it. */
219
246
  const tiered = (tier: CookTier, url: string): readonly string[] => tier !== 'phone' || !tierRows.has(url) ? [url] : phone?.kept.has(url) === true ? [url, `tier:${url}`] : [`tier:${url}`];
220
- return {shard, header, read: url => { const read = sources.get(url); if (read === undefined) throw new Error(`Spawn file has no source: ${url}`); return read(); },
247
+ return {shard, header, tierFiles: [...tierRows.values()].flat(), discovery: discovery[0] === undefined || documentBytes === undefined ? undefined : {row: discovery[0], bytes: documentBytes},
248
+ read: url => { const read = sources.get(url); if (read === undefined) throw new Error(`Spawn file has no source: ${url}`); return read(); },
221
249
  stableKey: url => stableKeys.get(url),
222
250
  mountBytes: row => (row.url === document?.url && documentBytes !== undefined ? documentBytes : shard.read(row.path)),
223
251
  productRows: (tier, set) => {
@@ -328,7 +356,7 @@ export interface CookShardOptions {
328
356
  readonly warn?: (line: string) => void;
329
357
  /** The renderer fingerprint to cook for; this engine's own unless a build names it. */
330
358
  readonly renderer?: string;
331
- /** S12b: per-tier texture files; off unless a cook asks. */
359
+ /** S12b: per-tier texture files; `defaultCookTiers()` (the phone tier, 6×6) unless a cook names its own. `{}` turns them off. */
332
360
  readonly tiers?: CookTiers;
333
361
  }
334
362
 
@@ -343,7 +371,7 @@ export async function cookShard(tarball: Uint8Array, out: string, options: CookS
343
371
  async function cookWith(tarball: Uint8Array, out: string, options: CookShardOptions, encoder: PartEncoder): Promise<CookReport> {
344
372
  const started = options.now(), shard = openShardfile(tarball, options.renderer === undefined ? {} : {renderer: options.renderer});
345
373
  if (shard.preparation === 'stale') options.warn?.(stalePreparationLine(shard.slug));
346
- const half = shardHalf(shard, options.wireBytes, options.warn, shard.slug, options.tiers);
374
+ const half = await preparedShardHalf(shard, options.wireBytes, options.warn, shard.slug, options.tiers ?? await defaultCookTiers());
347
375
  const before = encoder.stats();
348
376
  const table = COOK_TIERS.flatMap(tier => COOK_SETS.map(set => ({tier, set, rows: [...half.productRows(tier, set), ...half.mountRows(tier, set)]})));
349
377
  const whole = table.filter(row => row.set === 'whole');
@@ -25,6 +25,21 @@ import {SPAWN_PART_ALIGN, spawnCodeListPath, splitSpawnManifest, type SpawnCodeA
25
25
  type SpawnPart} from '../../shared/game/shardfile/spawnManifest';
26
26
 
27
27
  export const PART_BYTES = 12_000_000, PART_TARGET = 10, OVER_FETCH = 0.1;
28
+ /** A part's size rule: at most `max` raw unless one file alone is bigger, which then stands alone (never past
29
+ * PART_BYTES), and about `target` on average (DL18's key boundaries). */
30
+ export interface PartLimits {readonly max: number; readonly target: number}
31
+ /** A part's average decoded size: a key boundary falls after a member with probability size / PART_TARGET_BYTES (DL18). */
32
+ export const PART_TARGET_BYTES = 9_000_000;
33
+ /** Files no public (phone) entry reads keep the 12 MB rule: a desktop's whole level (Pine 333 MB) in 4 MiB parts would
34
+ * pass the manifest's 128 parts. */
35
+ export const WHOLE_PART_LIMITS: PartLimits = {max: PART_BYTES, target: PART_TARGET_BYTES};
36
+ /** S12e: what a phone entry reads goes in parts of at most 4 MiB, about 3 MiB on average, the boot pack's own cap
37
+ * (`boot-pack-parts.mjs` PART_MAX). Measured on Pine rev2 with the phone tier on: a one-texture edit re-downloads
38
+ * 3.28 MB of phone parts on average (9.19 MB at 12 MB, 3.77 at 5 MB), for 45 phone parts instead of 17 and the same
39
+ * wire bytes (69.24 MB against 69.20). */
40
+ export const PART_LIMITS: PartLimits = {max: 4 << 20, target: 3 << 20};
41
+ /** Under `readSpawnManifest`'s 128 parts, with room for the key boundaries' spread around their average. */
42
+ const MANIFEST_PARTS = 96;
28
43
  export interface SpawnPartInput {readonly rows: readonly SpawnFile[]; readonly public: boolean; readonly desktop?: boolean}
29
44
  /** `desktop`: a desktop list holds these files too. S3: it never shares a part with a phone's own, or it fetches them all. */
30
45
  interface Group {readonly key: string; readonly members: ReadonlySet<number>; readonly desktop: boolean; readonly urls: string[]; bytes: number}
@@ -91,8 +106,6 @@ export function spawnPartGroups(manifests: readonly SpawnPartInput[]): Group[] {
91
106
  return [...groups.values()];
92
107
  }
93
108
 
94
- /** A part's average decoded size: a key boundary falls after a member with probability size / PART_TARGET_BYTES (DL18). */
95
- export const PART_TARGET_BYTES = 9_000_000;
96
109
  /** DL18 (sp-x2's real pair 5adf2b0 -> fe210797ab: one Nine edit rewrote 41 bodies, and hashed placement scattered them
97
110
  * over 20 of 25 parts). Members are ordered by their readable stable key, so the files one generator writes (keys under
98
111
  * one section path or name prefix) sit together, and a part ends where the keys say: after a member whose key hash falls
@@ -101,7 +114,7 @@ export const PART_TARGET_BYTES = 9_000_000;
101
114
  * cut moves) instead of a scatter. Sections and schemas (`section|` keys) and build assets (`volatile|`) each form their
102
115
  * own run, so a rebuild's restamped files never share a part with content, and setup's first reads never sit behind
103
116
  * the build assets. */
104
- export function placeByKey(urls: readonly string[], size: (url: string) => number, key: (url: string) => string): string[][] {
117
+ export function placeByKey(urls: readonly string[], size: (url: string) => number, key: (url: string) => string, limits: PartLimits = WHOLE_PART_LIMITS): string[][] {
105
118
  const order = (a: string, b: string): number => (a < b ? -1 : a > b ? 1 : 0);
106
119
  const sorted = [...urls].sort((a, b) => order(key(a), key(b)) || order(a, b));
107
120
  const parts: string[][] = []; let current: string[] = [], bytes = 0, run = '';
@@ -109,9 +122,9 @@ export function placeByKey(urls: readonly string[], size: (url: string) => numbe
109
122
  for (const url of sorted) {
110
123
  const name = key(url), kind = name.slice(0, name.indexOf('|') + 1);
111
124
  if (kind !== run) { cut(); run = kind; }
112
- if (current.length > 0 && align(bytes) + size(url) > PART_BYTES) cut();
125
+ if (current.length > 0 && align(bytes) + size(url) > limits.max) cut();
113
126
  current.push(url); bytes = align(bytes) + size(url);
114
- if (Number.parseInt(createHash('sha256').update(name).digest('hex').slice(0, 8), 16) / 2 ** 32 < size(url) / PART_TARGET_BYTES) cut();
127
+ if (bytes >= limits.max || Number.parseInt(createHash('sha256').update(name).digest('hex').slice(0, 8), 16) / 2 ** 32 < size(url) / limits.target) cut();
115
128
  }
116
129
  cut();
117
130
  return parts;
@@ -136,7 +149,7 @@ export interface CookedMember {readonly url: string; readonly bytes: number; rea
136
149
  * stay what they were; a cooked file only stops being written twice. */
137
150
  export async function writeSpawnParts(outDir: string, manifests: readonly SpawnPartInput[], read: (url: string) => Uint8Array,
138
151
  stableKey: (url: string) => string | undefined = () => undefined, encoder: PartEncoder = createPartEncoder(),
139
- cooked: ReadonlyMap<string, CookedMember> = new Map()): Promise<SpawnPart[][]> {
152
+ cooked: ReadonlyMap<string, CookedMember> = new Map(), limits: PartLimits = PART_LIMITS): Promise<SpawnPart[][]> {
140
153
  const placed = new Map<string, {url: string; bytes: number; hash: string; offset: number}>(cooked), wire = new Map<string, number>();
141
154
  for (const member of cooked.values()) wire.set(member.url, member.wireBytes);
142
155
  mkdirSync(join(outDir, 'assets/spawn/parts'), {recursive: true});
@@ -149,6 +162,8 @@ export async function writeSpawnParts(outDir: string, manifests: readonly SpawnP
149
162
  return key;
150
163
  };
151
164
  const inflight = new Set<Promise<void>>(), bodies = new Map<string, readonly string[]>();
165
+ // A shard cook's part is a body too: the same bytes under another URL of one manifest must not take its name.
166
+ for (const [member, at] of cooked) bodies.set(at.hash, [...bodies.get(at.hash) ?? [], member]);
152
167
  const write = async (batch: readonly string[]): Promise<void> => {
153
168
  const length = batch.reduce((at, url) => align(at) + (sizes.get(url) ?? 0), 0);
154
169
  let body = new Uint8Array(length); const offsets = new Map<string, number>(); let cursor = 0;
@@ -175,12 +190,22 @@ export async function writeSpawnParts(outDir: string, manifests: readonly SpawnP
175
190
  inflight.add(job); void job.finally(() => { inflight.delete(job); }).catch(() => undefined);
176
191
  };
177
192
  try {
178
- for (const group of spawnPartGroups(manifests)) {
193
+ const groups = spawnPartGroups(manifests);
194
+ // A manifest names at most 128 parts. Estimate each one's count under the phone rule (its share of every group it
195
+ // touches); a shard whose desktop shares most of its files with its phone (no tier table) keeps the 12 MB rule.
196
+ const groupOf = new Map(groups.flatMap(group => group.urls.map(url => [url, group] as const)));
197
+ const estimate = (input: SpawnPartInput): number => {
198
+ const touched = new Set(input.rows.flatMap(row => { const group = groupOf.get(row.url); return group === undefined ? [] : [group]; }));
199
+ return [...touched].reduce((sum, group) => sum + Math.ceil(group.bytes / (group.key.startsWith('P:') ? limits.target : WHOLE_PART_LIMITS.target)), 0);
200
+ };
201
+ const phoneRule = manifests.every(input => estimate(input) <= MANIFEST_PARTS) ? limits : WHOLE_PART_LIMITS;
202
+ for (const group of groups) {
179
203
  for (const url of group.urls) {
180
204
  const bytes = sizes.get(url) ?? 0;
181
205
  if (bytes > PART_BYTES) throw new Error(`Spawn file larger than a part (${bytes} > ${PART_BYTES}): ${url}`);
182
206
  }
183
- for (const batch of placeByKey(group.urls.filter(url => !cooked.has(url)), url => sizes.get(url) ?? 0, keyOf)) await write(batch);
207
+ const rule = group.key.startsWith('P:') ? phoneRule : WHOLE_PART_LIMITS;
208
+ for (const batch of placeByKey(group.urls.filter(url => !cooked.has(url)), url => sizes.get(url) ?? 0, keyOf, rule)) await write(batch);
184
209
  }
185
210
  await Promise.all(inflight);
186
211
  } finally { await encoder.close(); }
@@ -95,7 +95,8 @@ export function trimKtx2(bytes: Uint8Array, maxSize: number): Uint8Array {
95
95
  }
96
96
 
97
97
  interface GltfView {buffer: number; byteOffset?: number; byteLength: number; extensions?: {EXT_meshopt_compression?: {buffer: number; byteOffset?: number; byteLength: number}}}
98
- interface Gltf {accessors?: {bufferView?: number; sparse?: {indices: {bufferView: number}; values: {bufferView: number}}}[]; bufferViews?: GltfView[]; buffers?: {byteLength: number; uri?: string}[]; images?: {bufferView?: number; mimeType?: string; uri?: string}[]}
98
+ interface GltfTexture {source?: number; extensions?: {KHR_texture_basisu?: {source?: number}}}
99
+ interface Gltf {materials?: {normalTexture?: {index: number}}[]; textures?: GltfTexture[]; accessors?: {bufferView?: number; sparse?: {indices: {bufferView: number}; values: {bufferView: number}}}[]; bufferViews?: GltfView[]; buffers?: {byteLength: number; uri?: string}[]; images?: {bufferView?: number; mimeType?: string; uri?: string}[]}
99
100
  interface Glb {readonly json: Gltf; readonly bin: Uint8Array}
100
101
 
101
102
  function readGlb(bytes: Uint8Array): Glb | undefined {
@@ -195,9 +196,46 @@ export function withImages(bytes: Uint8Array, replace: (image: GlbImage) => Uint
195
196
  return writeGlb(glb.json, bin);
196
197
  }
197
198
 
198
- /** A GLB at one tier: each embedded KTX2 image at most `maxSize` on its largest side. */
199
- export function tierGlb(bytes: Uint8Array, maxSize: number): Uint8Array {
200
- return withImages(bytes, image => (image.mimeType === 'image/ktx2' ? trimKtx2(image.bytes, maxSize) : image.bytes));
199
+ /** S12c: how a texture's texels read, so a re-encode keeps them: colour (sRGB), linear data, or a normal map. */
200
+ export type TextureRole = 'srgb' | 'linear' | 'normal';
201
+ /** S12c: a UASTC KTX2, already at its tier size, as ASTC 6×6 with the same levels; undefined when it can't. Injected, so
202
+ * this module stays bytes in, bytes out: the cook's is `basisuAstc6` (`astc6Encode.ts`), cached by input hash. */
203
+ export type Astc6Encode = (ktx2: Uint8Array, role: TextureRole) => Uint8Array | undefined;
204
+
205
+ /** The KTX2's DFD transfer function: 2 is sRGB, 1 linear. */
206
+ function transferOf(bytes: Uint8Array): number {
207
+ const view = viewOf(bytes), dfdAt = view.getUint32(48, true), dfdLength = view.getUint32(52, true);
208
+ return dfdLength >= 16 && dfdAt + 15 <= bytes.length ? (bytes[dfdAt + 14] ?? 0) : 0;
209
+ }
210
+
211
+ /** A KTX2 at one tier: trimmed to `maxSize`, then, with `encode`, a UASTC one re-encoded as ASTC 6×6 (S4's phone format,
212
+ * 16/36 of 4×4's GPU bytes). Raw ASTC is only trimmed; BasisLZ (ETC1S) keeps its bytes, as S4's data planes did. */
213
+ export function tierKtx2(bytes: Uint8Array, maxSize: number, encode?: Astc6Encode, normal = false): Uint8Array {
214
+ const trimmed = trimKtx2(bytes, maxSize);
215
+ if (encode === undefined || !isKtx2(trimmed)) return trimmed;
216
+ const info = ktx2Info(trimmed);
217
+ // Only a plain 2D texture the trim brought to its tier size: a one-level master stays as large as it was, and an array or cube
218
+ // map would lose its layers in a 2D encode.
219
+ const view = viewOf(trimmed), plain = view.getUint32(28, true) <= 1 && view.getUint32(32, true) <= 1 && view.getUint32(36, true) === 1;
220
+ if (info.vkFormat !== 0 || info.scheme === BASIS_LZ || !plain || Math.max(info.width, info.height) > maxSize) return trimmed;
221
+ return encode(trimmed, normal ? 'normal' : transferOf(trimmed) === 2 ? 'srgb' : 'linear') ?? trimmed;
222
+ }
223
+
224
+ /** The images a GLB's materials sample as normal maps. */
225
+ function normalImages(json: Gltf): ReadonlySet<number> {
226
+ const out = new Set<number>();
227
+ for (const material of json.materials ?? []) {
228
+ const texture = material.normalTexture === undefined ? undefined : json.textures?.[material.normalTexture.index];
229
+ const source = texture?.source ?? texture?.extensions?.KHR_texture_basisu?.source;
230
+ if (source !== undefined) out.add(source);
231
+ }
232
+ return out;
233
+ }
234
+
235
+ /** A GLB at one tier: each embedded KTX2 image at most `maxSize` on its largest side, and 6×6 with `encode` (`tierKtx2`). */
236
+ export function tierGlb(bytes: Uint8Array, maxSize: number, encode?: Astc6Encode): Uint8Array {
237
+ const normals = encode === undefined ? new Set<number>() : normalImages(readGlb(bytes)?.json ?? {});
238
+ return withImages(bytes, image => (image.mimeType === 'image/ktx2' ? tierKtx2(image.bytes, maxSize, encode, normals.has(image.index)) : image.bytes));
201
239
  }
202
240
 
203
241
  /** GPU memory a KTX2 takes once transcoded, mips included: ASTC 4×4 or BC7 for UASTC (1 byte a texel), its own block
@@ -217,6 +255,9 @@ export function ktx2GpuBytes(bytes: Uint8Array): number {
217
255
  * key is the author's hint (D5): a `#layer` twin (a terrain layer, tiled at a texel a metre) and a horizon strip are S4's
218
256
  * 1024 rows, kept on purpose; every other row is 512 on a phone. */
219
257
  export const PHONE_TIER_MAX = 512;
258
+ /** S12d: the cap a kept row (a `#layer` twin, a horizon) is encoded under: none it reaches, so it keeps its own size and only
259
+ * its encoding changes to 6×6, as S4's hand-made set had it. */
260
+ export const KEPT_ENCODE_MAX = 16_384;
220
261
  export function phoneTierSize(source: string): number | undefined {
221
262
  return source.endsWith('#layer') || /\/horizon\//u.test(source) ? undefined : PHONE_TIER_MAX;
222
263
  }
@@ -235,37 +276,69 @@ export interface PhoneTier {
235
276
  type Json = null | boolean | number | string | readonly Json[] | {readonly [key: string]: Json};
236
277
  const isMap = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null && !Array.isArray(value);
237
278
  const hex8 = (bytes: Uint8Array): string => createHash('sha256').update(bytes).digest('hex').slice(0, 8);
279
+ /** S4's normal-map names (`bake-ktx2.mjs` classOf): a loose KTX2 row the 6×6 encode reads as a normal map. */
280
+ const NORMAL_NAME = /(?:^|[_\-.])(?:nor|nor_gl|normal|nrm)(?:[_\-.]|$)/iu;
281
+
282
+ /** The desktop target that holds a phone row's master: the same source, or its name without the `.phone` / `-phone` mark. */
283
+ export function masterOf(desktop: Readonly<Record<string, unknown>> | undefined, source: string): string | undefined {
284
+ if (desktop === undefined) return undefined;
285
+ for (const key of [source, source.replace(/\.phone\./u, '.'), source.replace(/-phone\./u, '.')]) {
286
+ const target = desktop[key];
287
+ if (typeof target === 'string') return target;
288
+ }
289
+ return undefined;
290
+ }
238
291
 
239
292
  /** The phone tier of a shard's KTX2 table (`discovery.json` `native.ktx2`, any shape: `pageModeKtx2` args, `GPU_FILES`, a bare
240
293
  * tier map). Every `phone` map's GLB and KTX2 target that `read` can read is trimmed to its row's size; a target that gets
241
294
  * smaller moves to `<name>.t<size>-<hash8>.<ext>` beside it. Undefined when nothing gets smaller, so the cook keeps every
242
- * byte of today's product. */
243
- export function phoneTier(discovery: Uint8Array, read: (url: string) => Uint8Array | undefined, size: (source: string) => number | undefined = phoneTierSize): PhoneTier | undefined {
295
+ * byte of today's product.
296
+ *
297
+ * S12c: with `encode`, a row is cooked from its desktop master (`masterOf`, the sibling `desktop` map) when it has one,
298
+ * trimmed and re-encoded as ASTC 6×6 (`tierKtx2`, `tierGlb`): what the phone reads once the hand-made 512 sets are gone
299
+ * (S12d). That file stands in for the row's target whatever its size against today's hand-made file. */
300
+ export function phoneTier(discovery: Uint8Array, read: (url: string) => Uint8Array | undefined, size: (source: string) => number | undefined = phoneTierSize, encode?: Astc6Encode): PhoneTier | undefined {
244
301
  const document = JSON.parse(new TextDecoder().decode(discovery)) as Json;
245
302
  const native = isMap(document) && isMap(document['native']) ? document['native'] : undefined;
246
303
  if (native?.['ktx2'] === undefined) return undefined;
247
- const phoneMaps: Record<string, unknown>[] = [];
304
+ const phoneMaps: {phone: Record<string, unknown>; desktop: Record<string, unknown> | undefined}[] = [];
305
+ const stringMap = (item: unknown): item is Record<string, unknown> => isMap(item) && Object.values(item).every(target => typeof target === 'string');
248
306
  const walk = (value: unknown): void => {
249
307
  if (Array.isArray(value)) { for (const item of value) walk(item); return; }
250
308
  if (!isMap(value)) return;
251
309
  for (const [key, item] of Object.entries(value)) {
252
- if (key === 'phone' && isMap(item) && Object.values(item).every(target => typeof target === 'string')) phoneMaps.push(item);
310
+ if (key === 'phone' && stringMap(item)) phoneMaps.push({phone: item, desktop: stringMap(value['desktop']) ? value['desktop'] : undefined});
253
311
  else walk(item);
254
312
  }
255
313
  };
256
314
  walk(native['ktx2']);
257
315
  const made = new Map<string, TierFile | null>(), files = new Map<string, TierFile[]>(), kept = new Set<string>();
258
- for (const map of phoneMaps) for (const [source, target] of Object.entries(map)) {
316
+ let encodes = 0;
317
+ const counted: Astc6Encode | undefined = encode === undefined ? undefined : (bytes, role) => { const out = encode(bytes, role); if (out !== undefined) encodes++; return out; };
318
+ // A GLB's master is the same source's desktop file only: a `.phone` GLB holds a lighter mesh, not just smaller images.
319
+ const sameMaster = (desktop: Readonly<Record<string, unknown>> | undefined, source: string): string | undefined => { const target = desktop?.[source]; return typeof target === 'string' ? target : undefined; };
320
+ for (const {phone: map, desktop} of phoneMaps) for (const [source, target] of Object.entries(map)) {
259
321
  if (typeof target !== 'string') continue;
260
- const max = size(source), match = /^(.*?)(?:-[a-f0-9]{8})?\.(glb|ktx2)$/u.exec(target), bytes = max === undefined || match === null ? undefined : read(target);
322
+ // S12d: a row kept at its size is still encoded 6×6 from its own file when the cook encodes, which retires the hand sets.
323
+ const sized = size(source), own = sized === undefined && counted !== undefined, max = sized ?? (own ? KEPT_ENCODE_MAX : undefined);
324
+ const match = /^(.*?)(?:-[a-f0-9]{8})?\.(glb|ktx2)$/u.exec(target), held = max === undefined || match === null ? undefined : read(target);
325
+ // S12d: a shard that dropped its hand-made phone set (Sky Reach) names a target its product no longer carries; the row's own
326
+ // source, the file desktops load, is then its master, and the cook makes the phone's file from it (`phoneRowSources`).
327
+ const absent = held === undefined && match !== null && max !== undefined, bytes = held ?? (absent && source.endsWith(`.${match[2] ?? ''}`) ? read(source) : undefined);
261
328
  if (max === undefined || match === null || bytes === undefined) { kept.add(target); continue; }
262
- const key = `${target}@${max}`;
329
+ const ext = match[2] ?? '', master = encode === undefined || own ? undefined : (ext === 'glb' ? sameMaster(desktop, source) : masterOf(desktop, source));
330
+ const masterBytes = master?.endsWith(`.${ext}`) === true ? read(master) : undefined;
331
+ const key = `${target}<${masterBytes === undefined ? target : master ?? target}@${max}`;
263
332
  let file = made.get(key);
264
333
  if (file === undefined) {
265
334
  // A file the trim can't read keeps today's bytes: one odd texture never stops a cook.
266
- let trimmed = bytes;
267
- try { trimmed = match[2] === 'glb' ? tierGlb(bytes, max) : trimKtx2(bytes, max); } catch { trimmed = bytes; }
268
- file = trimmed.length < bytes.length ? {from: target, url: `${match[1] ?? ''}.t${max}-${hex8(trimmed)}.${match[2] ?? ''}`, bytes: trimmed} : null;
335
+ const normal = NORMAL_NAME.test(source.split('/').pop() ?? '');
336
+ const cook = (input: Uint8Array): Uint8Array => { try { return ext === 'glb' ? tierGlb(input, max, counted) : tierKtx2(input, max, counted, normal); } catch { return input; } };
337
+ // A master stands in only when the 6×6 encode ran on it; else the row's own file is cooked, as S12b does.
338
+ const encodedBefore = encodes;
339
+ const cookedMaster = masterBytes === undefined ? undefined : cook(masterBytes);
340
+ const trimmed = cookedMaster !== undefined && encodes > encodedBefore ? cookedMaster : cook(bytes), fromMaster = trimmed === cookedMaster;
341
+ file = fromMaster || absent || trimmed.length < bytes.length ? {from: target, url: `${match[1] ?? ''}.t${max}-${hex8(trimmed)}.${ext}`, bytes: trimmed} : null;
269
342
  made.set(key, file);
270
343
  if (file !== null) files.set(target, [...files.get(target) ?? [], file]);
271
344
  }
@@ -8,7 +8,15 @@ interface ContendedBoss {
8
8
  readonly engaged: boolean;
9
9
  readonly script: { readonly respawnPoint: () => { pos: Vector3; yaw: number } };
10
10
  readonly disarm: () => void; readonly arm: () => void; readonly onPlayerDeath: () => boolean;
11
+ /** SC5: the brain's state, read for a beaten boss's return, and its own way back (else a plain `disarm`). SC10: `defeated`, its record,
12
+ * for a boss whose presence puts it away after a win (a roaming boss leaves `victory` for `dormant` when everyone walks off). */
13
+ readonly state?: string; readonly defeated?: boolean; readonly returnBeaten?: () => void;
11
14
  }
15
+ /** SERVER-CELLS SC5 (J328): how a served world brings a beaten boss back, since a grid cell's world is never rebuilt. `rearm`: the
16
+ * boss stood armed from the start, so it stands armed again; a summoned one goes back to waiting for its summons. */
17
+ export interface BeatenReturn { readonly served: () => boolean; readonly rearm: boolean }
18
+ /** SC5: a served world's beaten boss comes back once nobody has stood in its arena this long, as J107's start-over waited. */
19
+ export const BOSS_RETURN_S = 300;
12
20
  type ArenaPlayer = (point: Vector3) => boolean;
13
21
  /** Marks a blow's share on a further player, which lands no further shares. */
14
22
  const ECHO = 'boss.echo';
@@ -27,10 +35,13 @@ export class BossContenders {
27
35
  private readonly players: ReadonlyMap<string, PlayerSim>;
28
36
  private readonly inArena: ArenaPlayer;
29
37
  private readonly place: (player: PlayerSim, position: Vector3, yaw: number) => void;
38
+ private readonly beaten: BeatenReturn | undefined;
39
+ /** SC5: seconds a beaten boss's arena has stood empty. */
40
+ private emptyFor = 0;
30
41
  /** The point `BossBrain` watches, live. */
31
42
  readonly player: { readonly position: Vector3 };
32
- constructor(players: ReadonlyMap<string, PlayerSim>, inArena: ArenaPlayer, place: (player: PlayerSim, position: Vector3, yaw: number) => void) {
33
- this.players = players; this.inArena = inArena; this.place = place;
43
+ constructor(players: ReadonlyMap<string, PlayerSim>, inArena: ArenaPlayer, place: (player: PlayerSim, position: Vector3, yaw: number) => void, beaten?: BeatenReturn) {
44
+ this.players = players; this.inArena = inArena; this.place = place; this.beaten = beaten;
34
45
  const contender = (): PlayerSim => this.contender();
35
46
  this.player = { get position() { return contender().position; } };
36
47
  }
@@ -74,13 +85,26 @@ export class BossContenders {
74
85
  /** The death checkpoint's answer. A room's only says the fight owns the death (`fell` returns them); a lone player's is the brain's. */
75
86
  died(boss: ContendedBoss): boolean { return this.players.size > 1 ? boss.engaged : boss.onPlayerDeath(); }
76
87
  /** Each fixed step, before the brain's: count who began the fight, and reset it once nobody is left inside. */
77
- observe(boss: ContendedBoss): void {
88
+ observe(boss: ContendedBoss, dt: number = 1 / 60): void {
89
+ if (this.beaten !== undefined && (boss.state === 'victory' || (boss.defeated === true && !boss.engaged)) && this.beaten.served()) { this.unbeaten(boss, this.beaten, dt); return; }
90
+ this.emptyFor = 0;
78
91
  if (this.players.size <= 1 && this.counted === 0) return;
79
92
  if (!boss.engaged) { this.counted = 0; return; }
80
93
  const inside = this.inside().length;
81
94
  if (this.counted === 0) this.counted = Math.max(1, inside);
82
95
  if (inside === 0) { this.counted = 0; boss.disarm(); boss.arm(); }
83
96
  }
97
+ /** SC5: a served world's beaten boss, back once its arena has stood empty `BOSS_RETURN_S`; anyone inside starts the wait over. */
98
+ private unbeaten(boss: ContendedBoss, beaten: BeatenReturn, dt: number): void {
99
+ if (this.inside().length > 0) { this.emptyFor = 0; return; }
100
+ this.emptyFor += dt;
101
+ if (this.emptyFor < BOSS_RETURN_S) return;
102
+ this.emptyFor = 0; this.counted = 0;
103
+ if (boss.returnBeaten === undefined) boss.disarm(); else boss.returnBeaten();
104
+ if (beaten.rearm) boss.arm();
105
+ }
106
+ /** SC10: the cell slept this long with nobody in it, so nobody stood in the arena: a beaten boss's wait spends it. */
107
+ slept(seconds: number): void { if (this.beaten !== undefined) this.emptyFor += seconds; }
84
108
  /** Players counted at the fight's start, for a continuation: 0 in any one-player fight. */
85
109
  get engaged(): number { return this.counted; }
86
110
  restoreEngaged(count: number): void { this.counted = count; }
@@ -56,7 +56,7 @@ export class PhaseEncounter {
56
56
  const focus = new Vector3(...spec.arena.at), saved = { ...ports.saved };
57
57
  const party = this.party = new BossContenders(sim.players, (point) => point.distanceTo(focus) < spec.arena.radius, (player, position, yaw) => {
58
58
  player.position.copy(position); player.yaw = yaw; player.health.attributes.health = player.health.attributes.maxHealth;
59
- });
59
+ }, { served: () => sim.respawn.served, rearm: true });
60
60
  const phase = (): EncounterPhase => { const value = this.spec.phases[this.boss.phase]; if (!value) throw new Error('Missing encounter phase'); return value; };
61
61
  this.boss = new BossBrain({ id: spec.id, name: spec.name, title: spec.title, retryTitle: spec.retry, intro: spec.intro, introShort: spec.introShort,
62
62
  phases: spec.phases.map((p) => ({ at: p.at, name: p.name, caption: p.caption })), reward: {} }, {
@@ -63,6 +63,8 @@ export interface RefillPorts {
63
63
  readonly random: () => number;
64
64
  /** Put the body back. Nothing else may change the population: the queue has already dropped the entry. */
65
65
  readonly spawn: (entry: RefillEntry, yaw: number) => void;
66
+ /** SC5 (J328): a served world (the MMO server's) is a room with one player in it: what falls comes back, as it does for two. */
67
+ readonly served?: () => boolean;
66
68
  }
67
69
 
68
70
  /**
@@ -76,20 +78,25 @@ export class CreatureRefill {
76
78
  private clock = 0;
77
79
  private readonly ports: RefillPorts;
78
80
  constructor(ports: RefillPorts) { this.ports = ports; }
81
+ /** A room's refill runs: two players or more, or a served world's one (SC5: a grid cell never starts over, so this is all that refills it). */
82
+ private room(players: readonly RefillPlayer[]): boolean { return players.length > 1 || this.ports.served?.() === true; }
79
83
 
80
84
  /** A body left its spawn point (it fell, or a player tamed it). */
81
85
  left(entry: Omit<RefillEntry, 'wait'>): void {
82
- if (this.ports.players().length <= 1 || this.pending.length >= REFILL_LIMIT) return;
86
+ if (!this.room(this.ports.players()) || this.pending.length >= REFILL_LIMIT) return;
83
87
  this.pending.push({ ...entry, wait: REFILL_WAIT });
84
88
  }
85
89
 
90
+ /** SC10: the cell slept this long with nobody near anything owed: each wait spends it at one player's rate. */
91
+ slept(seconds: number): void { for (const entry of this.pending) entry.wait -= seconds; }
92
+
86
93
  step(dt: number): void {
87
94
  if (this.pending.length === 0) return;
88
95
  this.clock += dt;
89
96
  if (this.clock < REFILL_CHECK) return;
90
97
  const elapsed = this.clock, players = this.ports.players();
91
98
  this.clock = 0;
92
- if (players.length <= 1) return; // the room emptied to one: what is owed waits, and that player's game is today's
99
+ if (!this.room(players)) return; // the room emptied to one: what is owed waits, and that player's game is today's
93
100
  const due: RefillEntry[] = [];
94
101
  for (const entry of this.pending) {
95
102
  entry.wait -= elapsed * refillRate(playersNear(players, entry.x, entry.z));
@@ -57,6 +57,8 @@ export class LedgerBook {
57
57
  }
58
58
  /** Seat a player's book as it was saved: their serial goes on from there, so a client that remembers a higher one still hears what comes next (E7.4). */
59
59
  seat(player: string, saved: LedgerBookSave[string]): void { this.books.set(player, { serial: saved.serial, rows: saved.rows.slice(), ids: new Set(saved.rows.map(row => row.id)) }); }
60
+ /** A player who has left: their serial and receipts went with their save (SC10: a long-lived world keeps only its live players'). */
61
+ drop(player: string): void { this.books.delete(player); }
60
62
  private of(player: string): Book {
61
63
  let book = this.books.get(player);
62
64
  if (book === undefined) { book = { serial: 0, rows: [], ids: new Set() }; this.books.set(player, book); }