castle-web-cli 0.4.230 → 0.4.231

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 (69) hide show
  1. package/dist/castle-host/host.js +81 -0
  2. package/dist/devSaveStorage.d.ts +36 -0
  3. package/dist/devSaveStorage.js +469 -0
  4. package/dist/devSessionChild.js +22 -12
  5. package/dist/devSessionServer.js +8 -2
  6. package/dist/devSessionStorage.d.ts +9 -0
  7. package/dist/devSessionStorage.js +66 -1
  8. package/dist/init.js +1 -1
  9. package/dist/serve.js +5 -1
  10. package/kits/base/castle.json +1 -1
  11. package/kits/base/sdk/README.md +148 -1
  12. package/kits/base/sdk/castle.d.ts +4 -0
  13. package/kits/base/sdk/castle.js +2 -0
  14. package/kits/base/sdk/commands.d.ts +15 -0
  15. package/kits/base/sdk/multiplayer.js +52 -4
  16. package/kits/base/sdk/save.d.ts +197 -0
  17. package/kits/base/sdk/save.js +525 -0
  18. package/kits/base/sdk/saveCoordinator.d.ts +39 -0
  19. package/kits/base/sdk/saveCoordinator.js +252 -0
  20. package/kits/base/sdk/server/storage.d.ts +3 -2
  21. package/kits/base/sdk/server/storage.js +25 -5
  22. package/kits/base/sdk/server/wrapper.d.ts +4 -0
  23. package/kits/base/sdk/server/wrapper.js +2 -0
  24. package/kits/base/sdk/store.d.ts +3 -2
  25. package/kits/base/sdk/store.js +5 -0
  26. package/kits/hub-game/CLAUDE.md +4 -1
  27. package/kits/hub-game/code/client/chrome.js +43 -9
  28. package/kits/hub-game/code/client/grab.js +142 -7
  29. package/kits/hub-game/code/client/hookLayer.js +7 -1
  30. package/kits/hub-game/code/client/state.js +13 -1
  31. package/kits/hub-game/code/client/talk.js +10 -36
  32. package/kits/hub-game/code/client/tiers.js +480 -0
  33. package/kits/hub-game/code/client/together.js +78 -3
  34. package/kits/hub-game/code/server/grab.js +3 -2
  35. package/kits/hub-game/code/server/hub.js +99 -5
  36. package/kits/hub-game/code/server/manifest.js +34 -7
  37. package/kits/hub-game/code/server/npcs.js +21 -33
  38. package/kits/hub-game/code/server/party.js +64 -6
  39. package/kits/hub-game/code/systems/hub.js +133 -16
  40. package/kits/hub-game/docs/contract.md +84 -14
  41. package/kits/hub-game/package.json +1 -1
  42. package/kits/hub-game/scripts/test-grab-client.mjs +328 -0
  43. package/kits/hub-game/scripts/test-hub-trip.mjs +208 -0
  44. package/kits/hub-game/scripts/test-manifest.mjs +47 -1
  45. package/kits/hub-game/scripts/test-npcs.mjs +34 -24
  46. package/kits/hub-game/scripts/test-party.mjs +87 -4
  47. package/kits/hub-game/scripts/test-tiers.mjs +666 -0
  48. package/kits/hub-game/scripts/test-together.mjs +33 -1
  49. package/kits/physics-2d/CLAUDE.md +2 -0
  50. package/kits/physics-2d/castle.json +2 -2
  51. package/kits/physics-2d/engine/scene.js +92 -0
  52. package/kits/physics-2d/package.json +1 -1
  53. package/kits/physics-2d/scripts/test-skip-unchanged.mjs +254 -0
  54. package/kits/physics-3d/CLAUDE.md +279 -0
  55. package/kits/physics-3d/behaviors/Player.jsx +74 -1
  56. package/kits/physics-3d/castle.json +3 -3
  57. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +1 -1
  58. package/kits/physics-3d/engine3d/actornode.js +253 -0
  59. package/kits/physics-3d/engine3d/nearshadows.js +600 -0
  60. package/kits/physics-3d/engine3d/unchanged.js +153 -0
  61. package/kits/physics-3d/engine3d/world3d.js +465 -7
  62. package/kits/physics-3d/package.json +1 -1
  63. package/kits/physics-3d/scripts/test-camera-follow.mjs +533 -0
  64. package/kits/physics-3d/scripts/test-near-shadows.mjs +1073 -0
  65. package/kits/physics-3d/scripts/test-static-actors.mjs +662 -0
  66. package/kits/physics-3d/scripts/test-static-passes.mjs +459 -0
  67. package/kits/physics-3d/scripts/test-static-physics.mjs +128 -0
  68. package/kits/physics-3d/systems/physics3d.js +182 -3
  69. package/package.json +1 -1
@@ -0,0 +1,480 @@
1
+ // Device tiers: a cheaper picture of a hub place for a phone that cannot draw
2
+ // the full one. Only in a deck whose castle.json says `"hub": { "tiers": true }`
3
+ // (the hook layer reads it into `state.js` `deck.tiers`); without it the hub
4
+ // system never calls in here, so nothing below runs, reads storage or touches
5
+ // the renderer.
6
+ //
7
+ // A device is 'high' (the picture as authored) or 'low'. The low picture was
8
+ // chosen on real phones (a Galaxy Tab A7 Lite and a Redmi 9A, CPU-bound at 12
9
+ // fps in the town): no sun shadow, decorative actors not drawn, other scenery
10
+ // not drawn past 30 m and NPC bodies past 20 m. The render resolution is left
11
+ // alone: a render scale of 1 on a 2x screen bought 2-9% on those phones and
12
+ // made Low visibly jagged, while shadows off alone took a TECNO from 32-35 to
13
+ // 48.5 fps.
14
+ // "Not drawn" takes the actor's three.js node out of the scene graph, which
15
+ // saves the traversal and matrix work as well as the draw. Nothing here
16
+ // touches colliders, physics, the network, labels or anything another player
17
+ // sees: it is this client's picture only.
18
+ //
19
+ // Where a device starts: low if it was found slow here in the last week
20
+ // (remembered in localStorage), or its GPU or memory says it is a weak phone;
21
+ // otherwise high, and the frame times decide (`makeJudge`). A player may pick
22
+ // Low or High by hand (the Graphics row in the Character card), which wins
23
+ // over all of it.
24
+
25
+ import { Box3, Sphere } from 'three';
26
+
27
+ export const TIERS = ['low', 'high'];
28
+ export const MODES = ['auto', 'low', 'high'];
29
+ // localStorage: the tier found here (`{ tier: 'low', at }`), and the mode the
30
+ // player picked.
31
+ export const TIER_KEY = 'castle.hub.tier';
32
+ export const MODE_KEY = 'castle.hub.graphics';
33
+ // A device found slow stays low this long, then is tried from high again.
34
+ export const REMEMBER_MS = 7 * 24 * 60 * 60 * 1000;
35
+ // The judge: a window's median frame time over this is a slow device. The
36
+ // first window after the player is placed is short, so a slow phone is helped
37
+ // within seconds; every later one is longer, so a passing stall is not taken
38
+ // for one.
39
+ export const SLOW_FRAME_MS = 40;
40
+ export const FIRST_WINDOW_MS = 5000;
41
+ export const WINDOW_MS = 10000;
42
+ // A gap this long between two frames is the page having been hidden or
43
+ // stalled: the window it falls in says nothing about drawing.
44
+ export const GAP_MS = 1000;
45
+ // Low: how far away scenery and NPC bodies are still drawn, and how often
46
+ // what is near is looked at again (not every frame).
47
+ export const SCENERY_NEAR_M = 30;
48
+ export const NPC_NEAR_M = 20;
49
+ export const CHECK_EVERY_MS = 500;
50
+
51
+ // GPUs of the phones the low picture is for, by the WebGL renderer string
52
+ // (`WEBGL_debug_renderer_info`): PowerVR Rogue GE8xxx, Mali-G52 MC1/MC2,
53
+ // Mali-G51, Mali-T7xx/T8xx, Adreno 3xx/4xx/50x.
54
+ const WEAK_GPUS = [
55
+ /PowerVR Rogue GE8\d{2,3}/i,
56
+ /Mali-G52 MC[12]\b/i,
57
+ /Mali-G51\b/i,
58
+ /Mali-T[78]\d\d\b/i,
59
+ /Adreno\D*(?:[34]\d\d|50\d)\b/i,
60
+ ];
61
+
62
+ export function weakGpu(renderer) {
63
+ return typeof renderer === 'string' && WEAK_GPUS.some((re) => re.test(renderer));
64
+ }
65
+
66
+ // The tier the device's own hints give, before a frame is drawn.
67
+ export function hintedTier({ gpu = null, deviceMemory = null } = {}) {
68
+ if (weakGpu(gpu)) {
69
+ return { tier: 'low', reason: 'gpu' };
70
+ }
71
+ if (typeof deviceMemory === 'number' && deviceMemory > 0 && deviceMemory <= 2) {
72
+ return { tier: 'low', reason: 'memory' };
73
+ }
74
+ return { tier: 'high', reason: 'auto' };
75
+ }
76
+
77
+ // Storage that may be missing, or throw on every access (a private window, a
78
+ // sandboxed frame, blocked site data): every read and write is guarded, and a
79
+ // failure reads as nothing stored.
80
+ export function readJson(storage, key) {
81
+ try {
82
+ const text = storage?.getItem(key);
83
+ return typeof text === 'string' ? JSON.parse(text) : null;
84
+ } catch {
85
+ return null;
86
+ }
87
+ }
88
+
89
+ export function writeJson(storage, key, value) {
90
+ try {
91
+ storage?.setItem(key, JSON.stringify(value));
92
+ return true;
93
+ } catch {
94
+ return false;
95
+ }
96
+ }
97
+
98
+ export function readMode(storage) {
99
+ const mode = readJson(storage, MODE_KEY);
100
+ return MODES.includes(mode) ? mode : 'auto';
101
+ }
102
+
103
+ // Where a device starts: found slow here within the week, then its hints.
104
+ export function initialTier({ storage, now, gpu, deviceMemory }) {
105
+ const seen = readJson(storage, TIER_KEY);
106
+ if (seen?.tier === 'low' && typeof seen.at === 'number' && now - seen.at >= 0 && now - seen.at < REMEMBER_MS) {
107
+ return { tier: 'low', reason: 'remembered' };
108
+ }
109
+ return hintedTier({ gpu, deviceMemory });
110
+ }
111
+
112
+ export function median(values) {
113
+ if (!values.length) {
114
+ return 0;
115
+ }
116
+ const sorted = [...values].sort((a, b) => a - b);
117
+ const mid = sorted.length >> 1;
118
+ return sorted.length % 2 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2;
119
+ }
120
+
121
+ // The frame-time judge. `feed(dtMs, nowMs, counts)` takes each frame; `counts`
122
+ // is false while the frame says nothing about how this device draws the place
123
+ // (the player not placed yet, a trip or scene swap under the cover, the page
124
+ // hidden), and the window then starts over. A window's median, not its mean,
125
+ // is judged, so the odd long frame (a mesh built, a collection) does not
126
+ // decide. Returns 'low' once, when a window is slow; the judge then stops: a
127
+ // visit never moves back up.
128
+ export function makeJudge() {
129
+ const judge = { done: false, first: true, start: null, frames: [] };
130
+ judge.reset = () => {
131
+ judge.start = null;
132
+ judge.frames = [];
133
+ };
134
+ judge.feed = (dt, now, counts) => {
135
+ if (judge.done) {
136
+ return null;
137
+ }
138
+ if (!counts || !(dt >= 0) || dt > GAP_MS) {
139
+ judge.reset();
140
+ return null;
141
+ }
142
+ if (judge.start === null) {
143
+ // The frame that opens a window may straddle whatever came before it.
144
+ judge.start = now;
145
+ return null;
146
+ }
147
+ judge.frames.push(dt);
148
+ if (now - judge.start < (judge.first ? FIRST_WINDOW_MS : WINDOW_MS)) {
149
+ return null;
150
+ }
151
+ const slow = median(judge.frames) > SLOW_FRAME_MS;
152
+ judge.first = false;
153
+ judge.reset();
154
+ if (slow) {
155
+ judge.done = true;
156
+ return 'low';
157
+ }
158
+ return null;
159
+ };
160
+ return judge;
161
+ }
162
+
163
+ // What an actor is to the low picture, by its components' names: 'npc' (an
164
+ // NPC body), 'decor' (nothing but Transform, Shape and Model), 'static'
165
+ // (scenery: those plus Solid or Lookable, or a Shape with `solid`) or 'game'
166
+ // (anything else, never
167
+ // touched: doors, pads, players, balls, the town's own pieces).
168
+ const GAMEPLAY = /^Hub|^Town|^Body$|^Ball$|^Player$/;
169
+ const DECOR = new Set(['Transform', 'Shape', 'Model']);
170
+ const SCENERY = new Set(['Transform', 'Shape', 'Model', 'Solid', 'Lookable']);
171
+
172
+ export function classify(components) {
173
+ const names = Object.keys(components ?? {});
174
+ // physics-3d makes an actor's collider block when it has `Solid` or its
175
+ // Shape says `solid` (physics3d.js `isSolid`); either way it is scenery that
176
+ // stops a player, never decor.
177
+ const solid = names.includes('Solid') || Boolean(components?.Shape?.solid);
178
+ if (names.includes('HubNpc')) {
179
+ return 'npc';
180
+ }
181
+ if (names.some((name) => GAMEPLAY.test(name))) {
182
+ return 'game';
183
+ }
184
+ if (!solid && names.every((name) => DECOR.has(name))) {
185
+ return 'decor';
186
+ }
187
+ return names.every((name) => SCENERY.has(name)) ? 'static' : 'game';
188
+ }
189
+
190
+ // A node's bounds once, as its middle's offset from the node's position and a
191
+ // radius: scenery does not change shape, and the offset follows a node the
192
+ // place moves (a hidden actor waits far below the ground).
193
+ function nodeBounds(node) {
194
+ const sphere = new Box3().setFromObject(node).getBoundingSphere(new Sphere());
195
+ if (!Number.isFinite(sphere.radius) || sphere.radius < 0) {
196
+ return { dx: 0, dy: 0, dz: 0, r: 0 };
197
+ }
198
+ const p = node.position;
199
+ return { dx: sphere.center.x - p.x, dy: sphere.center.y - p.y, dz: sphere.center.z - p.z, r: sphere.radius };
200
+ }
201
+
202
+ // The low picture's levers on a physics-3d world (`scene.three`: scene,
203
+ // camera, and `nodes`, actor id -> { node }; never the renderer or composer).
204
+ // `apply` puts the low picture on and keeps it on as the engine builds and
205
+ // replaces nodes (a scene load, a door trip and the way back, the town's nodes
206
+ // parked and put back: world3d.js `swapSceneNodes` adds a parked node back to
207
+ // the scene); `restore` puts back exactly what it changed.
208
+ export function makeLevers({ boundsOf = nodeBounds } = {}) {
209
+ const lv = {
210
+ world: null,
211
+ active: false,
212
+ // Lights whose shadow was turned off here.
213
+ lights: new Set(),
214
+ // node -> the parent it was taken out of.
215
+ removed: new WeakMap(),
216
+ hidden: 0,
217
+ bounds: new WeakMap(),
218
+ // The authored actors of the scene playing: only these are ever hidden
219
+ // (never a player's avatar, what they wear, or what the place spawned).
220
+ authored: null,
221
+ authoredFor: null,
222
+ seen: { path: undefined, nodes: null, size: -1 },
223
+ // The entries taken out on the last look, [actor id, entry]: the engine
224
+ // replaces an entry in the same map when an actor's look changes
225
+ // (world3d.js `ensureActorNode`), and the new node is drawn, so these are
226
+ // checked every frame -- only the hidden ones, never every actor.
227
+ watch: [],
228
+ nextAt: 0,
229
+ };
230
+
231
+ const adopt = (world) => {
232
+ if (lv.world !== world) {
233
+ // A new world: nothing changed on the old one is put back there.
234
+ lv.world = world;
235
+ lv.lights = new Set();
236
+ lv.removed = new WeakMap();
237
+ lv.authored = null;
238
+ lv.authoredFor = null;
239
+ lv.seen = { path: undefined, nodes: null, size: -1 };
240
+ lv.watch = [];
241
+ lv.nextAt = 0;
242
+ }
243
+ };
244
+
245
+ // Every light in the scene that casts a shadow stops: the sun (world3d.js)
246
+ // and any a deck added. With the sun casting none, nearshadows.js finds no
247
+ // sun and draws its frames plain, and three.js skips the shadow pass.
248
+ const lightsOff = (world) => {
249
+ for (const child of world.scene?.children ?? []) {
250
+ if (child.isLight && child.castShadow) {
251
+ child.castShadow = false;
252
+ lv.lights.add(child);
253
+ }
254
+ }
255
+ };
256
+
257
+ const authoredIds = (scene) => {
258
+ if (lv.authoredFor === scene.data && lv.authored) {
259
+ return lv.authored;
260
+ }
261
+ lv.authoredFor = scene.data;
262
+ try {
263
+ const file = scene.readFromFile(scene.scenePath);
264
+ lv.authored = new Set((file?.actors ?? []).map((a) => a.id));
265
+ } catch {
266
+ lv.authored = new Set();
267
+ }
268
+ return lv.authored;
269
+ };
270
+
271
+ const hide = (node) => {
272
+ if (node.parent) {
273
+ lv.removed.set(node, node.parent);
274
+ node.parent.remove(node);
275
+ }
276
+ };
277
+
278
+ const unhide = (node) => {
279
+ const parent = lv.removed.get(node);
280
+ if (parent && !node.parent) {
281
+ parent.add(node);
282
+ }
283
+ lv.removed.delete(node);
284
+ };
285
+
286
+ const far = (node, near, cam, sized) => {
287
+ const p = node.position;
288
+ let b = { dx: 0, dy: 0, dz: 0, r: 0 };
289
+ if (sized) {
290
+ b = lv.bounds.get(node);
291
+ if (!b) {
292
+ b = boundsOf(node);
293
+ lv.bounds.set(node, b);
294
+ }
295
+ }
296
+ const d = Math.hypot(p.x + b.dx - cam.x, p.y + b.dy - cam.y, p.z + b.dz - cam.z) - b.r;
297
+ return d > near;
298
+ };
299
+
300
+ // One look at every node: what should not be drawn is taken out, what should
301
+ // be is put back.
302
+ const pass = (world, scene, actors) => {
303
+ lightsOff(world);
304
+ const authored = actors ? authoredIds(scene) : null;
305
+ const cam = world.camera?.position ?? { x: 0, y: 0, z: 0 };
306
+ const watch = [];
307
+ for (const [id, entry] of world.nodes) {
308
+ const node = entry?.node;
309
+ if (!node || entry.instancedRig) {
310
+ continue;
311
+ }
312
+ let want = false;
313
+ if (authored?.has(id)) {
314
+ const kind = classify(scene.getActor?.(id)?.components);
315
+ want =
316
+ kind === 'decor' ||
317
+ (kind === 'static' && far(node, SCENERY_NEAR_M, cam, true)) ||
318
+ (kind === 'npc' && far(node, NPC_NEAR_M, cam, false));
319
+ }
320
+ if (want) {
321
+ hide(node);
322
+ if (!node.parent) {
323
+ watch.push([id, entry]);
324
+ }
325
+ } else if (lv.removed.has(node)) {
326
+ unhide(node);
327
+ }
328
+ }
329
+ lv.watch = watch;
330
+ lv.hidden = watch.length;
331
+ };
332
+
333
+ // Whether a node taken out was replaced (a rebuild) or put back since.
334
+ const watchBroken = (world) => {
335
+ for (const [id, entry] of lv.watch) {
336
+ if (world.nodes.get(id) !== entry || entry.node.parent) {
337
+ return true;
338
+ }
339
+ }
340
+ return false;
341
+ };
342
+
343
+ // Each frame while low. The nodes are looked at every CHECK_EVERY_MS, and at
344
+ // once when the engine swapped or rebuilt them (another scene, nodes parked
345
+ // or put back, actors come or gone). `actors` false (a game reached through
346
+ // a door: another deck's content) puts every node back and keeps only the
347
+ // shadow lever.
348
+ lv.apply = (world, scene, now, { actors = true } = {}) => {
349
+ adopt(world);
350
+ lv.active = true;
351
+ const seen = lv.seen;
352
+ const changed = world.scenePath !== seen.path || world.nodes !== seen.nodes || world.nodes.size !== seen.size || watchBroken(world);
353
+ if (!changed && now < lv.nextAt && actors === seen.actors) {
354
+ return;
355
+ }
356
+ lv.seen = { path: world.scenePath, nodes: world.nodes, size: world.nodes.size, actors };
357
+ lv.nextAt = now + CHECK_EVERY_MS;
358
+ pass(world, scene, actors);
359
+ };
360
+
361
+ // Back to the picture as authored: every node taken out of the scene put
362
+ // back where it was (a node the engine has since released or parked is
363
+ // left to the engine), every shadow on again.
364
+ lv.restore = (world) => {
365
+ adopt(world);
366
+ for (const entry of world.nodes.values()) {
367
+ if (entry?.node && lv.removed.has(entry.node)) {
368
+ unhide(entry.node);
369
+ }
370
+ }
371
+ lv.removed = new WeakMap();
372
+ for (const light of lv.lights) {
373
+ light.castShadow = true;
374
+ }
375
+ lv.lights.clear();
376
+ lv.hidden = 0;
377
+ lv.watch = [];
378
+ lv.active = false;
379
+ lv.seen = { path: undefined, nodes: null, size: -1 };
380
+ };
381
+
382
+ return lv;
383
+ }
384
+
385
+ // The WebGL renderer string, for `weakGpu`.
386
+ export function gpuOf(renderer) {
387
+ try {
388
+ const gl = renderer?.getContext?.();
389
+ if (!gl) {
390
+ return null;
391
+ }
392
+ const info = gl.getExtension('WEBGL_debug_renderer_info');
393
+ const name = gl.getParameter(info ? info.UNMASKED_RENDERER_WEBGL : gl.RENDERER);
394
+ return typeof name === 'string' ? name : null;
395
+ } catch {
396
+ return null;
397
+ }
398
+ }
399
+
400
+ function pageStorage() {
401
+ try {
402
+ return globalThis.localStorage ?? null;
403
+ } catch {
404
+ return null;
405
+ }
406
+ }
407
+
408
+ // The hub system's tiers: `frame(scene, counts, now)` each frame. `counts` is
409
+ // whether this frame says how the device draws the place (see `makeJudge`).
410
+ export function makeTiers({ storage = pageStorage(), clock = () => Date.now(), deviceMemory = globalThis.navigator?.deviceMemory ?? null, levers = makeLevers() } = {}) {
411
+ const t = {
412
+ mode: readMode(storage),
413
+ auto: null,
414
+ judge: makeJudge(),
415
+ levers,
416
+ last: null,
417
+ };
418
+
419
+ t.current = () => (t.mode === 'auto' ? (t.auto?.tier ?? 'high') : t.mode);
420
+ t.reason = () => (t.mode === 'auto' ? (t.auto?.reason ?? 'auto') : 'manual');
421
+
422
+ t.set = (mode) => {
423
+ if (!MODES.includes(mode)) {
424
+ return false;
425
+ }
426
+ t.mode = mode;
427
+ writeJson(storage, MODE_KEY, mode);
428
+ // A window half gathered under another mode says nothing about this one.
429
+ t.judge.reset();
430
+ return true;
431
+ };
432
+
433
+ t.frame = (scene, counts, now) => {
434
+ const world = scene.three;
435
+ if (!world) {
436
+ return;
437
+ }
438
+ // Hints are read once, from the renderer the place is drawn with.
439
+ t.auto ??= initialTier({ storage, now: clock(), gpu: gpuOf(world.renderer), deviceMemory });
440
+ const dt = t.last === null ? 0 : now - t.last;
441
+ t.last = now;
442
+ if (t.mode === 'auto' && t.auto.tier === 'high') {
443
+ const hidden = globalThis.document?.hidden === true;
444
+ if (t.judge.feed(dt, now, counts && !hidden) === 'low') {
445
+ t.auto = { tier: 'low', reason: 'slow' };
446
+ writeJson(storage, TIER_KEY, { tier: 'low', at: clock() });
447
+ }
448
+ } else {
449
+ // Not judging (a pick by hand, or already low): no frames are kept
450
+ // for a later window.
451
+ t.judge.reset();
452
+ }
453
+ if (t.current() === 'low') {
454
+ levers.apply(world, scene, now, { actors: !scene.content });
455
+ } else if (levers.active) {
456
+ // Low to High costs a one-off stall (about 1 s on the phones measured)
457
+ // as three.js recompiles its shader programs for the shadows coming
458
+ // back. Acceptable: auto never moves up, only a player's pick does.
459
+ levers.restore(world);
460
+ }
461
+ };
462
+
463
+ // For test drivers and phone measurements (`globalThis.__hub.tier`).
464
+ t.handle = {
465
+ get current() {
466
+ return t.current();
467
+ },
468
+ get reason() {
469
+ return t.reason();
470
+ },
471
+ get mode() {
472
+ return t.mode;
473
+ },
474
+ get hidden() {
475
+ return levers.hidden;
476
+ },
477
+ set: (mode) => t.set(mode),
478
+ };
479
+ return t;
480
+ }
@@ -17,14 +17,24 @@ const POINTERS_MAX = 3;
17
17
  // to be offered an invitation without opening the People list.
18
18
  const PARTY_MAX = 8;
19
19
  const NEAR_INVITE_M = 2.2;
20
- // The invitation is offered after standing beside someone this long, for this long.
20
+ // Once offered, the invitation stays until they are this far off: two players
21
+ // standing at the edge of the near distance would otherwise blink it.
22
+ const NEAR_LEAVE_M = 3.4;
23
+ // The invitation is offered after standing beside someone this long, for this
24
+ // long (a friend's stays as long as they are beside the player).
21
25
  const NEAR_DWELL_MS = 1500;
22
26
  const NEAR_OFFER_MS = 8000;
27
+ // After a press the button says the invitation went, this long, and then is
28
+ // not offered for that player again for a while.
29
+ const NEAR_SENT_MS = 4000;
30
+ const NEAR_AGAIN_MS = 30000;
23
31
  // How long an "Over here!" keeps the pointer on its speaker.
24
32
  const PING_MS = 10000;
25
33
  // A friend first seen this long after the session began has arrived (the
26
34
  // ones here at the start were here already).
27
35
  const ARRIVAL_AFTER_MS = 8000;
36
+ // How long a friend who left the roster can still be named.
37
+ const RECENT_MS = 20000;
28
38
  // The part of the screen an edge pointer keeps to: clear of the HUD above and
29
39
  // the buttons below.
30
40
  const EDGE = { side: 46, top: 84, bottom: 150 };
@@ -53,10 +63,16 @@ export function makeTogether() {
53
63
  const pinged = new Map();
54
64
  // The player stood beside, and since when (`beside`).
55
65
  let near = { playerId: null, since: 0 };
66
+ // playerId -> when (page ms) the player invited them with that button.
67
+ const asked = new Map();
68
+ // Players whose offer ran out, until they walk off.
69
+ const passed = new Set();
56
70
  // Every player seen in this session's roster (whether they came in after
57
71
  // its first moments, and whether they have been announced), and the
58
72
  // friends and party members who came in late, not yet told to the player.
59
73
  const seen = new Map();
74
+ // playerId -> { username, until }: friends and party members lately here.
75
+ const recent = new Map();
60
76
  let seenSince = null;
61
77
  const arrived = [];
62
78
  // Where the last ask left off, when there are more players to ask about
@@ -91,6 +107,18 @@ export function makeTogether() {
91
107
  }
92
108
  // A friend or party member who turns up after the first moments here
93
109
  // has arrived: the player is told (those already here were the start).
110
+ // A friend or party member is remembered a little after they leave the
111
+ // roster, so word that they went through a door can still name them.
112
+ for (const [playerId, t] of tracked) {
113
+ if (t.friend || t.party) {
114
+ recent.set(playerId, { username: t.username, until: now + RECENT_MS });
115
+ }
116
+ }
117
+ for (const [playerId, r] of recent) {
118
+ if (r.until <= now) {
119
+ recent.delete(playerId);
120
+ }
121
+ }
94
122
  // When each player was first in the roster is kept apart from whether
95
123
  // they are a friend: a friends answer that comes late must not announce
96
124
  // someone who was here all along.
@@ -146,7 +174,10 @@ export function makeTogether() {
146
174
  pinged.clear();
147
175
  tracked = new Map();
148
176
  near = { playerId: null, since: 0 };
177
+ asked.clear();
178
+ passed.clear();
149
179
  seen.clear();
180
+ recent.clear();
150
181
  seenSince = null;
151
182
  arrived.length = 0;
152
183
  },
@@ -154,6 +185,9 @@ export function makeTogether() {
154
185
  // The friends and party members who arrived since this was last asked.
155
186
  arrivals: () => arrived.splice(0),
156
187
 
188
+ // The name of a friend or party member who was here a moment ago, or null.
189
+ lately: (playerId) => recent.get(playerId)?.username ?? null,
190
+
157
191
  // Whom a player who just came in should start beside: a party member
158
192
  // here first, else the first friend here in the host's order (ghost lists
159
193
  // the most recently active first); `{ playerId, username }` or null.
@@ -284,19 +318,60 @@ export function makeTogether() {
284
318
  return null;
285
319
  }
286
320
  let best = null;
321
+ let held = null;
322
+ const here = new Set();
287
323
  for (const [playerId, username] of mp?.net?.others?.() ?? []) {
288
324
  const actorId = mp.avatars?.actors?.get(playerId);
289
325
  const at = actorId ? scene.getActor(actorId)?.components.Transform : null;
290
326
  const d = at ? Math.hypot(at.x - lt.x, at.z - lt.z) : Infinity;
291
- if (d <= NEAR_INVITE_M && !members?.includes(playerId) && (!best || d < best.d)) {
327
+ here.add(playerId);
328
+ if (d > NEAR_LEAVE_M) {
329
+ passed.delete(playerId);
330
+ }
331
+ const sent = asked.get(playerId);
332
+ if (sent !== undefined && now - sent >= NEAR_AGAIN_MS) {
333
+ asked.delete(playerId);
334
+ }
335
+ // One whose offer ran out, or who was just invited, is not in the way
336
+ // of anyone else stood here.
337
+ const quiet = passed.has(playerId) || (asked.has(playerId) && now - sent >= NEAR_SENT_MS);
338
+ if (members?.includes(playerId) || quiet) {
339
+ continue;
340
+ }
341
+ if (d <= NEAR_INVITE_M && (!best || d < best.d)) {
292
342
  best = { d, playerId, username: username || null };
293
343
  }
344
+ if (playerId === near.playerId && d <= NEAR_LEAVE_M) {
345
+ held = { d, playerId, username: username || null };
346
+ }
347
+ }
348
+ for (const playerId of passed) {
349
+ if (!here.has(playerId)) {
350
+ passed.delete(playerId);
351
+ }
294
352
  }
353
+ // The one already stood beside is kept while they are still about.
354
+ best = held ?? best;
295
355
  if (best?.playerId !== near.playerId) {
296
356
  near = { playerId: best?.playerId ?? null, since: now };
297
357
  }
358
+ if (!best) {
359
+ return null;
360
+ }
361
+ if (asked.has(best.playerId)) {
362
+ return { ...best, sent: true };
363
+ }
298
364
  const stood = now - near.since;
299
- return best && stood >= NEAR_DWELL_MS && stood < NEAR_DWELL_MS + NEAR_OFFER_MS ? best : null;
365
+ if (!tracked.get(best.playerId)?.friend && stood >= NEAR_DWELL_MS + NEAR_OFFER_MS) {
366
+ // Not offered again until they have walked off and come back.
367
+ passed.add(best.playerId);
368
+ return null;
369
+ }
370
+ return stood >= NEAR_DWELL_MS ? best : null;
371
+ },
372
+ // The player pressed the invite button for this one.
373
+ invited(playerId, now = performance.now()) {
374
+ asked.set(playerId, now);
300
375
  },
301
376
 
302
377
  // Where each friend here is on screen, for the ones too far to read a
@@ -16,8 +16,9 @@
16
16
  // against the simulation and bounded per player.
17
17
 
18
18
  // How far from a player's body (metres) an object they say they grabbed may
19
- // be: the client offers one within about 2.5 m, and the server's poses lag a
20
- // moving player's.
19
+ // be: the client offers one within about 2.5 m (with a place's `grab.reach`
20
+ // or `grab.steady`, v1.22, never one whose centre is past 3.5 m), and the
21
+ // server's poses lag a moving player's.
21
22
  export const GRAB_REACH = 4;
22
23
  // A held object is in front of its player; a throw is told as it leaves the
23
24
  // hand, from about multiplayer-3d's claim reach.