castle-web-cli 0.4.230 → 0.4.232

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 (92) 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/dist/serverBundle.js +5 -3
  11. package/kits/base/castle.json +1 -1
  12. package/kits/base/sdk/README.md +148 -1
  13. package/kits/base/sdk/castle.d.ts +4 -0
  14. package/kits/base/sdk/castle.js +2 -0
  15. package/kits/base/sdk/commands.d.ts +15 -0
  16. package/kits/base/sdk/multiplayer.js +52 -4
  17. package/kits/base/sdk/save.d.ts +197 -0
  18. package/kits/base/sdk/save.js +525 -0
  19. package/kits/base/sdk/saveCoordinator.d.ts +39 -0
  20. package/kits/base/sdk/saveCoordinator.js +252 -0
  21. package/kits/base/sdk/server/storage.d.ts +3 -2
  22. package/kits/base/sdk/server/storage.js +25 -5
  23. package/kits/base/sdk/server/wrapper.d.ts +4 -0
  24. package/kits/base/sdk/server/wrapper.js +2 -0
  25. package/kits/base/sdk/store.d.ts +3 -2
  26. package/kits/base/sdk/store.js +5 -0
  27. package/kits/hub-game/CLAUDE.md +29 -4
  28. package/kits/hub-game/code/client/avatar.js +39 -0
  29. package/kits/hub-game/code/client/chat.js +12 -0
  30. package/kits/hub-game/code/client/chrome.js +237 -13
  31. package/kits/hub-game/code/client/grab.js +166 -7
  32. package/kits/hub-game/code/client/hats.js +26 -0
  33. package/kits/hub-game/code/client/hookLayer.js +36 -3
  34. package/kits/hub-game/code/client/hudClear.js +126 -0
  35. package/kits/hub-game/code/client/state.js +35 -1
  36. package/kits/hub-game/code/client/talk.js +10 -36
  37. package/kits/hub-game/code/client/tiers.js +505 -0
  38. package/kits/hub-game/code/client/together.js +144 -4
  39. package/kits/hub-game/code/client/wear.js +96 -14
  40. package/kits/hub-game/code/server/actors.js +5 -0
  41. package/kits/hub-game/code/server/grab.js +299 -13
  42. package/kits/hub-game/code/server/hub.js +162 -7
  43. package/kits/hub-game/code/server/manifest.js +80 -8
  44. package/kits/hub-game/code/server/npcs.js +21 -33
  45. package/kits/hub-game/code/server/party.js +75 -6
  46. package/kits/hub-game/code/server/say.js +8 -2
  47. package/kits/hub-game/code/server/wear.js +116 -11
  48. package/kits/hub-game/code/systems/hub.js +386 -21
  49. package/kits/hub-game/docs/contract.md +285 -19
  50. package/kits/hub-game/package.json +1 -1
  51. package/kits/hub-game/scripts/test-chat.mjs +32 -0
  52. package/kits/hub-game/scripts/test-grab-client.mjs +511 -0
  53. package/kits/hub-game/scripts/test-grab.mjs +857 -3
  54. package/kits/hub-game/scripts/test-hats.mjs +229 -0
  55. package/kits/hub-game/scripts/test-hub-trip.mjs +721 -0
  56. package/kits/hub-game/scripts/test-hud-clear.mjs +676 -0
  57. package/kits/hub-game/scripts/test-manifest.mjs +122 -1
  58. package/kits/hub-game/scripts/test-npcs.mjs +34 -24
  59. package/kits/hub-game/scripts/test-party.mjs +108 -4
  60. package/kits/hub-game/scripts/test-say.mjs +59 -2
  61. package/kits/hub-game/scripts/test-tiers.mjs +854 -0
  62. package/kits/hub-game/scripts/test-together.mjs +33 -1
  63. package/kits/hub-game/scripts/test-try-on.mjs +940 -0
  64. package/kits/physics-2d/CLAUDE.md +2 -0
  65. package/kits/physics-2d/castle.json +2 -2
  66. package/kits/physics-2d/engine/scene.js +127 -0
  67. package/kits/physics-2d/package.json +1 -1
  68. package/kits/physics-2d/scripts/test-skip-unchanged.mjs +298 -0
  69. package/kits/physics-3d/CLAUDE.md +531 -0
  70. package/kits/physics-3d/behaviors/Player.jsx +437 -3
  71. package/kits/physics-3d/castle.json +3 -3
  72. package/kits/physics-3d/docs/pxmodel-format.md +4 -0
  73. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +33 -1
  74. package/kits/physics-3d/engine3d/actornode.js +253 -0
  75. package/kits/physics-3d/engine3d/nearshadows.js +600 -0
  76. package/kits/physics-3d/engine3d/optimize.js +49 -0
  77. package/kits/physics-3d/engine3d/orbitinput.js +277 -0
  78. package/kits/physics-3d/engine3d/unchanged.js +153 -0
  79. package/kits/physics-3d/engine3d/world3d.js +525 -11
  80. package/kits/physics-3d/package.json +1 -1
  81. package/kits/physics-3d/scripts/Player.before-orbit.mjs +310 -0
  82. package/kits/physics-3d/scripts/Scene3DPlayer.before-orbit.jsx +337 -0
  83. package/kits/physics-3d/scripts/test-camera-follow.mjs +543 -0
  84. package/kits/physics-3d/scripts/test-camera-orbit.mjs +2105 -0
  85. package/kits/physics-3d/scripts/test-drop-empty-parts.mjs +787 -0
  86. package/kits/physics-3d/scripts/test-near-shadows.mjs +1073 -0
  87. package/kits/physics-3d/scripts/test-static-actors.mjs +667 -0
  88. package/kits/physics-3d/scripts/test-static-passes.mjs +459 -0
  89. package/kits/physics-3d/scripts/test-static-physics.mjs +128 -0
  90. package/kits/physics-3d/scripts/test-steady-hover.mjs +633 -0
  91. package/kits/physics-3d/systems/physics3d.js +273 -9
  92. package/package.json +1 -1
@@ -4,7 +4,7 @@
4
4
  // pointers to the friends, and to anyone the player pressed Find on, who are
5
5
  // too far away to see. A game draws none of it.
6
6
 
7
- import { hub } from './state.js';
7
+ import { deck, hub } from './state.js';
8
8
  import { NAME_NEAR_M } from './attention.js';
9
9
 
10
10
  // How often the place is asked where the tracked players are.
@@ -17,14 +17,30 @@ 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;
38
+ // Cues (v1.23): how long after a player the host has not named was first seen
39
+ // the host is asked about them again (`due`). v1.31: the first after 1 s (the
40
+ // host knows who is in the same layer within a few seconds of a join) and,
41
+ // v1.32, again 2 s later for an answer that came a moment too soon; the rest
42
+ // spread over its heartbeat for a friend in another layer.
43
+ export const EARLY_ASKS_MS = [1000, 3000, 9000, 15000];
28
44
  // The part of the screen an edge pointer keeps to: clear of the HUD above and
29
45
  // the buttons below.
30
46
  const EDGE = { side: 46, top: 84, bottom: 150 };
@@ -53,12 +69,21 @@ export function makeTogether() {
53
69
  const pinged = new Map();
54
70
  // The player stood beside, and since when (`beside`).
55
71
  let near = { playerId: null, since: 0 };
72
+ // playerId -> when (page ms) the player invited them with that button.
73
+ const asked = new Map();
74
+ // Players whose offer ran out, until they walk off.
75
+ const passed = new Set();
56
76
  // Every player seen in this session's roster (whether they came in after
57
77
  // its first moments, and whether they have been announced), and the
58
78
  // friends and party members who came in late, not yet told to the player.
59
79
  const seen = new Map();
80
+ // playerId -> { username, until }: friends and party members lately here.
81
+ const recent = new Map();
60
82
  let seenSince = null;
61
83
  const arrived = [];
84
+ // Cues (v1.23): the user ids of the friends found already here, each told
85
+ // to the player once a session.
86
+ const cued = new Set();
62
87
  // Where the last ask left off, when there are more players to ask about
63
88
  // than one ask covers: they are worked through in turn.
64
89
  let turn = 0;
@@ -91,6 +116,18 @@ export function makeTogether() {
91
116
  }
92
117
  // A friend or party member who turns up after the first moments here
93
118
  // has arrived: the player is told (those already here were the start).
119
+ // A friend or party member is remembered a little after they leave the
120
+ // roster, so word that they went through a door can still name them.
121
+ for (const [playerId, t] of tracked) {
122
+ if (t.friend || t.party) {
123
+ recent.set(playerId, { username: t.username, until: now + RECENT_MS });
124
+ }
125
+ }
126
+ for (const [playerId, r] of recent) {
127
+ if (r.until <= now) {
128
+ recent.delete(playerId);
129
+ }
130
+ }
94
131
  // When each player was first in the roster is kept apart from whether
95
132
  // they are a friend: a friends answer that comes late must not announce
96
133
  // someone who was here all along.
@@ -99,6 +136,15 @@ export function makeTogether() {
99
136
  if (!seen.has(playerId)) {
100
137
  seen.set(playerId, { late: now - seenSince > ARRIVAL_AFTER_MS, told: false });
101
138
  }
139
+ if (deck.cues) {
140
+ // When this player's user id was first known (`due`).
141
+ const first = seen.get(playerId);
142
+ const userId = net.userId?.(playerId) ?? null;
143
+ if (userId && first.userId === undefined) {
144
+ first.userId = String(userId);
145
+ first.knownAt = now;
146
+ }
147
+ }
102
148
  }
103
149
  for (const [playerId, t] of tracked) {
104
150
  const first = seen.get(playerId);
@@ -106,6 +152,11 @@ export function makeTogether() {
106
152
  first.told = true;
107
153
  if (first.late) {
108
154
  arrived.push({ playerId, username: t.username });
155
+ } else if (deck.cues && t.friend && t.userId && !cued.has(String(t.userId))) {
156
+ // Cues: a friend who was here when the player came in is told
157
+ // too, once the host has named them (`here: true` marks these).
158
+ cued.add(String(t.userId));
159
+ arrived.push({ playerId, username: t.username, here: true });
109
160
  }
110
161
  }
111
162
  }
@@ -146,14 +197,62 @@ export function makeTogether() {
146
197
  pinged.clear();
147
198
  tracked = new Map();
148
199
  near = { playerId: null, since: 0 };
200
+ asked.clear();
201
+ passed.clear();
149
202
  seen.clear();
203
+ recent.clear();
150
204
  seenSince = null;
151
205
  arrived.length = 0;
206
+ cued.clear();
207
+ },
208
+
209
+ // Cues: whether the host is worth asking for the player's friends again
210
+ // now. It names a friend only once it knows both of them are in the
211
+ // town, and it learns that on a heartbeat (about every 15 s), so an ask
212
+ // made the moment someone is seen comes back without them. Each player
213
+ // here whose user id is known and whom the host has not named is asked
214
+ // about again `EARLY_ASKS_MS` after they were first seen -- four times,
215
+ // through the heartbeat's window -- until the host names them or they
216
+ // leave. Those here when the player came in start with the player, which
217
+ // covers the player's own presence being the one the host lacks. A
218
+ // player whose user id is not known yet starts when it is.
219
+ due(friends, now) {
220
+ if (!deck.cues) {
221
+ return false;
222
+ }
223
+ const ids = new Set((friends ?? []).map((f) => String(f.userId)));
224
+ for (const first of seen.values()) {
225
+ const step = first.asks ?? 0;
226
+ if (first.userId !== undefined && !ids.has(first.userId) && step < EARLY_ASKS_MS.length && now >= first.knownAt + EARLY_ASKS_MS[step]) {
227
+ return true;
228
+ }
229
+ }
230
+ return false;
231
+ },
232
+
233
+ // Cues: the host was asked just now (early or on its cadence). Every ask
234
+ // that had come due is spent, for everyone here: one ask answers for all,
235
+ // and an ask held back is not owed later.
236
+ asked(now) {
237
+ if (!deck.cues) {
238
+ return;
239
+ }
240
+ for (const first of seen.values()) {
241
+ while (first.userId !== undefined && (first.asks ?? 0) < EARLY_ASKS_MS.length && now >= first.knownAt + EARLY_ASKS_MS[first.asks ?? 0]) {
242
+ first.asks = (first.asks ?? 0) + 1;
243
+ }
244
+ }
152
245
  },
153
246
 
247
+ // Cues: whether this player here is a friend or in the party.
248
+ close: (playerId) => Boolean(tracked.get(playerId)?.friend || tracked.get(playerId)?.party),
249
+
154
250
  // The friends and party members who arrived since this was last asked.
155
251
  arrivals: () => arrived.splice(0),
156
252
 
253
+ // The name of a friend or party member who was here a moment ago, or null.
254
+ lately: (playerId) => recent.get(playerId)?.username ?? null,
255
+
157
256
  // Whom a player who just came in should start beside: a party member
158
257
  // here first, else the first friend here in the host's order (ghost lists
159
258
  // the most recently active first); `{ playerId, username }` or null.
@@ -284,19 +383,60 @@ export function makeTogether() {
284
383
  return null;
285
384
  }
286
385
  let best = null;
386
+ let held = null;
387
+ const here = new Set();
287
388
  for (const [playerId, username] of mp?.net?.others?.() ?? []) {
288
389
  const actorId = mp.avatars?.actors?.get(playerId);
289
390
  const at = actorId ? scene.getActor(actorId)?.components.Transform : null;
290
391
  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)) {
392
+ here.add(playerId);
393
+ if (d > NEAR_LEAVE_M) {
394
+ passed.delete(playerId);
395
+ }
396
+ const sent = asked.get(playerId);
397
+ if (sent !== undefined && now - sent >= NEAR_AGAIN_MS) {
398
+ asked.delete(playerId);
399
+ }
400
+ // One whose offer ran out, or who was just invited, is not in the way
401
+ // of anyone else stood here.
402
+ const quiet = passed.has(playerId) || (asked.has(playerId) && now - sent >= NEAR_SENT_MS);
403
+ if (members?.includes(playerId) || quiet) {
404
+ continue;
405
+ }
406
+ if (d <= NEAR_INVITE_M && (!best || d < best.d)) {
292
407
  best = { d, playerId, username: username || null };
293
408
  }
409
+ if (playerId === near.playerId && d <= NEAR_LEAVE_M) {
410
+ held = { d, playerId, username: username || null };
411
+ }
294
412
  }
413
+ for (const playerId of passed) {
414
+ if (!here.has(playerId)) {
415
+ passed.delete(playerId);
416
+ }
417
+ }
418
+ // The one already stood beside is kept while they are still about.
419
+ best = held ?? best;
295
420
  if (best?.playerId !== near.playerId) {
296
421
  near = { playerId: best?.playerId ?? null, since: now };
297
422
  }
423
+ if (!best) {
424
+ return null;
425
+ }
426
+ if (asked.has(best.playerId)) {
427
+ return { ...best, sent: true };
428
+ }
298
429
  const stood = now - near.since;
299
- return best && stood >= NEAR_DWELL_MS && stood < NEAR_DWELL_MS + NEAR_OFFER_MS ? best : null;
430
+ if (!tracked.get(best.playerId)?.friend && stood >= NEAR_DWELL_MS + NEAR_OFFER_MS) {
431
+ // Not offered again until they have walked off and come back.
432
+ passed.add(best.playerId);
433
+ return null;
434
+ }
435
+ return stood >= NEAR_DWELL_MS ? best : null;
436
+ },
437
+ // The player pressed the invite button for this one.
438
+ invited(playerId, now = performance.now()) {
439
+ asked.set(playerId, now);
300
440
  },
301
441
 
302
442
  // Where each friend here is on screen, for the ones too far to read a
@@ -1,8 +1,9 @@
1
1
  // What players wear on their avatars (contract §11, v1.4): the place sends
2
2
  // playerId -> { kind, color, rarity } (`hubWear`), and this draws each one as a
3
3
  // few glowing, non-solid Shape actors that follow the avatar's Transform every
4
- // frame -- this player's own body included. Kinds this kit does not draw are
5
- // not drawn. A piece is `wear:<playerId>:<n>`; it is despawned when the
4
+ // frame -- this player's own body included. A `hat-<id>` kind is a hat
5
+ // from `hats.js`: a few matte Shapes on the avatar's head, turning with it.
6
+ // Kinds this kit does not draw are not drawn. A piece is `wear:<playerId>:<n>`; it is despawned when the
6
7
  // wearable changes or goes, or the avatar does, and retired with the
7
8
  // multiplayer kit so it is never reported to the session as gone.
8
9
 
@@ -10,6 +11,8 @@
10
11
  // over the avatar's own top and an orbit circles it (`top`, metres above its
11
12
  // model's feet), so a short avatar wears them as close as a tall one, under
12
13
  // its name tag.
14
+ import { hatId } from './hats.js';
15
+
13
16
  const FEET_Y = -0.85;
14
17
  const MODEL_FEET_Y = -0.7;
15
18
  const HALO_GAP = 0.15;
@@ -18,7 +21,14 @@ const ORBIT_RADIUS = 0.5;
18
21
  const COLOR = /^#[0-9a-f]{6}$/i;
19
22
  const MAX_RARITY = 9;
20
23
 
21
- function shape(kind, color, rarity, size) {
24
+ const DEG = Math.PI / 180;
25
+
26
+ // A piece's Shape: glowing in the wearable's colour, or `matte` (a hat), which
27
+ // has no glow at all.
28
+ function shape(kind, color, rarity, size, { matte = false } = {}) {
29
+ if (matte) {
30
+ return { kind, ...size, color, roughness: 0.9, metalness: 0, solid: false };
31
+ }
22
32
  return {
23
33
  kind,
24
34
  ...size,
@@ -31,9 +41,66 @@ function shape(kind, color, rarity, size) {
31
41
  };
32
42
  }
33
43
 
44
+ // The hats (`hats.js` has the ids). Each is at most three pieces, in metres
45
+ // from the hat's seat -- the ring of the head a 0.2 m rim rests on -- with +z
46
+ // the way the wearer faces: `disc` a cylinder and `slab` a box, centred `y`
47
+ // over the seat, leaning `tilt` degrees to the side. A piece with no colour
48
+ // takes the wearable's; the others are the hat's own. A `perch` hat is
49
+ // narrower than the rim and stands on the crown of the head instead. None is
50
+ // taller than 0.36 m before an avatar's fit scales it, which keeps every
51
+ // avatar's under its name tag.
52
+ const disc = (color, radius, height, y, { x = 0, z = 0, tilt = 0 } = {}) => ({ kind: 'cylinder', color, size: { radius, height }, x, y, z, tilt });
53
+ const slab = (color, width, height, depth, y, { x = 0, z = 0, tilt = 0 } = {}) => ({ kind: 'box', color, size: { width, height, depth }, x, y, z, tilt });
54
+ const INK = '#2b2b33';
55
+ const HAT_DESIGNS = new Map([
56
+ // stadium
57
+ ['sweatband', { pieces: [disc(null, 0.23, 0.07, 0.02), disc('#ffffff', 0.235, 0.025, 0.02)] }],
58
+ ['cap', { pieces: [disc(null, 0.21, 0.15, 0.075), slab(INK, 0.26, 0.025, 0.2, 0.02, { z: 0.27 })] }],
59
+ ['foam-crown', { pieces: [disc(null, 0.22, 0.12, 0.06), slab(null, 0.44, 0.1, 0.09, 0.16), slab(null, 0.09, 0.1, 0.44, 0.16)] }],
60
+ ['laurel', { pieces: [disc(null, 0.24, 0.05, 0.02), slab('#f2d57a', 0.08, 0.09, 0.04, 0.05, { z: 0.23 })] }],
61
+ // hollow
62
+ ['bat-clip', { pieces: [slab(INK, 0.07, 0.1, 0.06, 0.08, { x: 0.13, z: 0.04 }), slab(null, 0.32, 0.07, 0.02, 0.11, { x: 0.13, z: 0.04, tilt: -12 })] }],
63
+ ['witch', { pieces: [disc(null, 0.34, 0.025, 0.015), disc(null, 0.19, 0.17, 0.1), disc(null, 0.09, 0.15, 0.255, { x: -0.03, tilt: 14 })] }],
64
+ ['lantern', { perch: true, pieces: [disc(INK, 0.13, 0.04, 0.02), disc(null, 0.11, 0.17, 0.12), disc(INK, 0.14, 0.04, 0.22)] }],
65
+ ['crooked-top', { pieces: [disc(null, 0.3, 0.025, 0.015), disc('#d9c27a', 0.18, 0.13, 0.08), disc(null, 0.2, 0.16, 0.22, { x: -0.035, tilt: 12 })] }],
66
+ // sunny
67
+ ['straw', { pieces: [disc(null, 0.36, 0.025, 0.015), disc(null, 0.2, 0.15, 0.075)] }],
68
+ ['flower-crown', { pieces: [disc('#5fa55a', 0.24, 0.06, 0.03), slab(null, 0.6, 0.05, 0.12, 0.026), slab(null, 0.12, 0.05, 0.6, 0.026)] }],
69
+ ['beekeeper', { pieces: [disc(null, 0.19, 0.14, 0.09), disc(null, 0.34, 0.025, 0.03), disc('#e9e4cf', 0.31, 0.07, -0.02)] }],
70
+ ['sunflower', { pieces: [disc(null, 0.3, 0.03, 0.05), disc('#5a3a1e', 0.16, 0.15, 0.075)] }],
71
+ // harbour
72
+ ['sailor', { pieces: [disc('#1f3a6b', 0.2, 0.08, 0.04), disc(null, 0.25, 0.07, 0.105)] }],
73
+ ['souwester', { pieces: [disc(null, 0.2, 0.15, 0.075), disc(null, 0.33, 0.025, 0.015, { z: -0.09 })] }],
74
+ ['captain', { pieces: [disc('#1c2740', 0.2, 0.08, 0.04), disc(null, 0.26, 0.07, 0.105), slab('#15151a', 0.26, 0.02, 0.12, 0.02, { z: 0.24 })] }],
75
+ ['lighthouse', { perch: true, pieces: [disc(null, 0.13, 0.17, 0.085), disc('#ffe9a3', 0.1, 0.1, 0.215), disc('#c0392b', 0.13, 0.04, 0.28)] }],
76
+ ]);
77
+ export const HAT_IDS = [...HAT_DESIGNS.keys()];
78
+ // Where a hat sits on an avatar this kit has no fit for: just under its top.
79
+ const HAT_DROP = 0.1;
80
+ const mm = (n) => Math.round(n * 1000) / 1000;
81
+
82
+ // A hat's pieces on an avatar whose `fit` is `[seat, scale, rise]` (`avatar.js`
83
+ // `hatFit`): the seat's height over the model's feet, how big the hat is
84
+ // drawn for that head, and how far the crown of the head stands over the seat.
85
+ function hatPieces(id, color, top, fit) {
86
+ const design = HAT_DESIGNS.get(id);
87
+ if (!design) {
88
+ return null;
89
+ }
90
+ const [seat, scale, rise] = fit ?? [top - HAT_DROP, 1, 0];
91
+ const base = MODEL_FEET_Y + seat + (design.perch ? rise : 0);
92
+ return design.pieces.map((piece) => {
93
+ const size = Object.fromEntries(Object.entries(piece.size).map(([key, value]) => [key, mm(value * scale)]));
94
+ const at = { x: mm(piece.x * scale), y: mm(base + piece.y * scale), z: mm(piece.z * scale) };
95
+ return { shape: shape(piece.kind, piece.color ?? color, 0, size, { matte: true }), at: () => at, turns: true, tilt: piece.tilt };
96
+ });
97
+ }
98
+
34
99
  // The pieces of a wearable, each a Shape and where it sits relative to the
35
- // avatar at time `t` (seconds). Null for a kind this kit does not draw.
36
- export function pieces(wearable, top = DEFAULT_TOP) {
100
+ // avatar at time `t` (seconds). Null for a kind this kit does not draw. A
101
+ // hat's pieces also say `turns` (they face the way the avatar does) and their
102
+ // `tilt`; `fit` is the avatar's, for a hat alone.
103
+ export function pieces(wearable, top = DEFAULT_TOP, fit = null) {
37
104
  const color = typeof wearable?.color === 'string' && COLOR.test(wearable.color) ? wearable.color.toLowerCase() : null;
38
105
  const rarity = Number.isInteger(wearable?.rarity) ? Math.min(MAX_RARITY, Math.max(0, wearable.rarity)) : 0;
39
106
  if (!color) {
@@ -66,8 +133,10 @@ export function pieces(wearable, top = DEFAULT_TOP) {
66
133
  },
67
134
  }));
68
135
  }
69
- default:
70
- return null;
136
+ default: {
137
+ const hat = hatId(wearable.kind);
138
+ return hat ? hatPieces(hat, color, top, fit) : null;
139
+ }
71
140
  }
72
141
  }
73
142
 
@@ -88,8 +157,9 @@ export function makeWear() {
88
157
 
89
158
  return {
90
159
  // Spawn, move and remove pieces to match `wearing` (playerId -> wearable).
91
- // `topOf(playerId)` is how tall that player's avatar stands.
92
- frame(scene, local, wearing, topOf = () => DEFAULT_TOP, now = performance.now()) {
160
+ // `topOf(playerId)` is how tall that player's avatar stands, and
161
+ // `fitOf(playerId)` how a hat sits on it (asked only for a hat).
162
+ frame(scene, local, wearing, topOf = () => DEFAULT_TOP, now = performance.now(), fitOf = () => null) {
93
163
  const mp = scene.multiplayer;
94
164
  const self = mp?.net?.selfId?.() ?? null;
95
165
  const t = now / 1000;
@@ -102,7 +172,8 @@ export function makeWear() {
102
172
  const actorId = playerId === self ? local?.id : mp?.avatars?.actors?.get(playerId);
103
173
  const at = actorId ? scene.getActor(actorId)?.components.Transform : null;
104
174
  const top = topOf(playerId);
105
- const json = `${top}:${JSON.stringify(wearable)}`;
175
+ const fit = hatId(wearable?.kind) ? fitOf(playerId) : null;
176
+ const json = fit ? `${top}:${fit}:${JSON.stringify(wearable)}` : `${top}:${JSON.stringify(wearable)}`;
106
177
  if (!at || drawn.get(playerId)?.json !== json) {
107
178
  remove(scene, playerId);
108
179
  }
@@ -111,14 +182,14 @@ export function makeWear() {
111
182
  }
112
183
  let entry = drawn.get(playerId);
113
184
  if (!entry) {
114
- const parts = pieces(wearable, top) ?? [];
185
+ const parts = pieces(wearable, top, fit) ?? [];
115
186
  entry = { json, pieces: parts, ids: parts.map((_, n) => `wear:${playerId}:${n}`) };
116
187
  drawn.set(playerId, entry);
117
188
  entry.pieces.forEach((piece, n) => {
118
189
  const p = piece.at(t);
119
190
  scene.spawnActor({
120
191
  id: entry.ids[n],
121
- components: { Transform: { x: at.x + p.x, y: at.y + p.y, z: at.z + p.z }, Shape: { ...piece.shape } },
192
+ components: { Transform: { x: at.x + p.x, y: at.y + p.y, z: at.z + p.z, ...(piece.tilt ? { rotationZ: piece.tilt } : {}) }, Shape: { ...piece.shape } },
122
193
  });
123
194
  });
124
195
  }
@@ -126,9 +197,20 @@ export function makeWear() {
126
197
  const pt = scene.getActor(entry.ids[n])?.components.Transform;
127
198
  if (pt) {
128
199
  const p = piece.at(t);
129
- pt.x = at.x + p.x;
130
200
  pt.y = at.y + p.y;
131
- pt.z = at.z + p.z;
201
+ if (piece.turns) {
202
+ // A hat faces the way its wearer does: the engine turns local +x
203
+ // onto (cos, -sin) and +z onto (sin, cos).
204
+ const turn = at.rotationY ?? 0;
205
+ const cos = Math.cos(turn * DEG);
206
+ const sin = Math.sin(turn * DEG);
207
+ pt.x = at.x + cos * p.x + sin * p.z;
208
+ pt.z = at.z - sin * p.x + cos * p.z;
209
+ pt.rotationY = turn;
210
+ } else {
211
+ pt.x = at.x + p.x;
212
+ pt.z = at.z + p.z;
213
+ }
132
214
  }
133
215
  });
134
216
  }
@@ -10,6 +10,8 @@
10
10
  // a game cannot flood a client: so many movers at once, so many colour changes
11
11
  // a second, a box the world already keeps players in.
12
12
 
13
+ import { taken } from './grab.js';
14
+
13
15
  export const MAX_MOVERS = 64;
14
16
  export const MOVE_MAX_MS = 60000;
15
17
  // Colour changes a second across the place, with a burst; a change past the
@@ -595,6 +597,9 @@ export function reset(h, actorId, at, engine) {
595
597
  }
596
598
  // Not claimable for a moment, so no client is handed it before it has the
597
599
  // reset pose (`releaseResetHolds` gives it back).
600
+ // A held object pinned to its holder (`grab.keepHeld`, v1.25) is unpinned
601
+ // first, so what is saved here is the object's own `claimable`.
602
+ taken(h, actorId);
598
603
  if (!h.resetHolds.has(actorId)) {
599
604
  h.resetHolds.set(actorId, { claimable: entry.claimable, until: 0 });
600
605
  }