castle-web-cli 0.4.231 → 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 (55) hide show
  1. package/dist/serverBundle.js +5 -3
  2. package/kits/hub-game/CLAUDE.md +26 -4
  3. package/kits/hub-game/code/client/avatar.js +39 -0
  4. package/kits/hub-game/code/client/chat.js +12 -0
  5. package/kits/hub-game/code/client/chrome.js +194 -4
  6. package/kits/hub-game/code/client/grab.js +31 -7
  7. package/kits/hub-game/code/client/hats.js +26 -0
  8. package/kits/hub-game/code/client/hookLayer.js +30 -3
  9. package/kits/hub-game/code/client/hudClear.js +126 -0
  10. package/kits/hub-game/code/client/state.js +22 -0
  11. package/kits/hub-game/code/client/tiers.js +31 -6
  12. package/kits/hub-game/code/client/together.js +66 -1
  13. package/kits/hub-game/code/client/wear.js +96 -14
  14. package/kits/hub-game/code/server/actors.js +5 -0
  15. package/kits/hub-game/code/server/grab.js +296 -11
  16. package/kits/hub-game/code/server/hub.js +63 -2
  17. package/kits/hub-game/code/server/manifest.js +48 -3
  18. package/kits/hub-game/code/server/party.js +11 -0
  19. package/kits/hub-game/code/server/say.js +8 -2
  20. package/kits/hub-game/code/server/wear.js +116 -11
  21. package/kits/hub-game/code/systems/hub.js +253 -5
  22. package/kits/hub-game/docs/contract.md +204 -8
  23. package/kits/hub-game/package.json +1 -1
  24. package/kits/hub-game/scripts/test-chat.mjs +32 -0
  25. package/kits/hub-game/scripts/test-grab-client.mjs +186 -3
  26. package/kits/hub-game/scripts/test-grab.mjs +857 -3
  27. package/kits/hub-game/scripts/test-hats.mjs +229 -0
  28. package/kits/hub-game/scripts/test-hub-trip.mjs +513 -0
  29. package/kits/hub-game/scripts/test-hud-clear.mjs +676 -0
  30. package/kits/hub-game/scripts/test-manifest.mjs +75 -0
  31. package/kits/hub-game/scripts/test-party.mjs +22 -1
  32. package/kits/hub-game/scripts/test-say.mjs +59 -2
  33. package/kits/hub-game/scripts/test-tiers.mjs +190 -2
  34. package/kits/hub-game/scripts/test-try-on.mjs +940 -0
  35. package/kits/physics-2d/castle.json +1 -1
  36. package/kits/physics-2d/engine/scene.js +35 -0
  37. package/kits/physics-2d/scripts/test-skip-unchanged.mjs +44 -0
  38. package/kits/physics-3d/CLAUDE.md +252 -0
  39. package/kits/physics-3d/behaviors/Player.jsx +363 -2
  40. package/kits/physics-3d/castle.json +2 -2
  41. package/kits/physics-3d/docs/pxmodel-format.md +4 -0
  42. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +32 -0
  43. package/kits/physics-3d/engine3d/optimize.js +49 -0
  44. package/kits/physics-3d/engine3d/orbitinput.js +277 -0
  45. package/kits/physics-3d/engine3d/world3d.js +81 -25
  46. package/kits/physics-3d/scripts/Player.before-orbit.mjs +310 -0
  47. package/kits/physics-3d/scripts/Scene3DPlayer.before-orbit.jsx +337 -0
  48. package/kits/physics-3d/scripts/test-camera-follow.mjs +12 -2
  49. package/kits/physics-3d/scripts/test-camera-orbit.mjs +2105 -0
  50. package/kits/physics-3d/scripts/test-drop-empty-parts.mjs +787 -0
  51. package/kits/physics-3d/scripts/test-static-actors.mjs +6 -1
  52. package/kits/physics-3d/scripts/test-static-passes.mjs +1 -1
  53. package/kits/physics-3d/scripts/test-steady-hover.mjs +633 -0
  54. package/kits/physics-3d/systems/physics3d.js +91 -6
  55. package/package.json +1 -1
@@ -0,0 +1,126 @@
1
+ // A clear HUD (contract v1.26, a place's `hud: { clear: true }`): the sizes
2
+ // and the geometry, with no DOM in them so `scripts/test-hud-clear.mjs` can
3
+ // hold them. The chrome (`chrome.js`) calls these only in a place that asks;
4
+ // a place that does not is drawn by the code it always was.
5
+ //
6
+ // Three rules. (1) A big line with a text line under it is drawn smaller, and
7
+ // always on one line, so the text stays in view; a big line alone keeps its
8
+ // 56 px. (2) What the chrome places over the world (labels, speech, marks,
9
+ // pointers, a cabinet's card, the result ribbon) is moved to just under the
10
+ // HUD's box when it would lie over it, never hidden and never off the bottom
11
+ // of the view. (3) On a short view (a
12
+ // phone on its side) the text keeps to two lines and the box to a share of
13
+ // the height.
14
+
15
+ // The big line as it always was, and as it is alone.
16
+ export const BIG_PX = 56;
17
+ // The big line over a text line, on a tall view and on a short one.
18
+ export const BIG_WITH_TEXT_PX = 32;
19
+ export const BIG_WITH_TEXT_SHORT_PX = 28;
20
+ // A long big line shrinks to fit, never below this; past it the line is cut
21
+ // with an ellipsis rather than wrapped.
22
+ export const BIG_MIN_PX = 18;
23
+ // The HUD box's own widest (`maxWidth: 70vw`) and its width when nothing in
24
+ // it is wider (it hangs from the middle of the screen, so half the view).
25
+ const HUD_MAX_SHARE = 0.7;
26
+ const HUD_MIN_SHARE = 0.5;
27
+ // A view this short or shorter, and wider than it is tall, is a phone on its
28
+ // side. (Not height alone: the web player's portrait card on a 360 px phone is
29
+ // about 475 px tall, and is a tall view.)
30
+ export const SHORT_VIEW_H = 480;
31
+ export const SHORT_TEXT_LINES = 2;
32
+ export const SHORT_HUD_SHARE = 0.4;
33
+ // Clear air kept between the HUD's box and what is moved under it.
34
+ export const HUD_GAP = 6;
35
+
36
+ // How wide a string is at weight 800 in the system face, in ems. An estimate
37
+ // on the wide side: the chrome measures the drawn line after and shrinks it
38
+ // again if this fell short (`fitPx`).
39
+ export function bigEms(text) {
40
+ let ems = 0;
41
+ for (const ch of String(text)) {
42
+ if (ch === ' ') {
43
+ ems += 0.3;
44
+ } else if (/[.,:;!'|]/.test(ch)) {
45
+ ems += 0.34;
46
+ } else if (/[0-9]/.test(ch)) {
47
+ ems += 0.64;
48
+ } else if (/[MW@%]/.test(ch) || ch.codePointAt(0) > 0x2000) {
49
+ ems += 1;
50
+ } else if (/[a-z]/.test(ch)) {
51
+ ems += 0.6;
52
+ } else {
53
+ ems += 0.74;
54
+ }
55
+ }
56
+ return ems;
57
+ }
58
+
59
+ // The HUD's layout for what it shows. `viewW`/`viewH` are the view in CSS
60
+ // pixels; `reserve` is the width taken at each side of the big line's row (the
61
+ // Back button and its margin), 0 for none.
62
+ // bigPx the big line's font size
63
+ // bigOneLine whether it is held to one line (cut with an ellipsis)
64
+ // bigMaxWidth the widest the big line may be, or null for no limit
65
+ // textLines how many lines the text keeps to, or null for no limit
66
+ // maxHeight the tallest the box may be, or null for no limit
67
+ export function hudLayout({ big, text, viewW, viewH, reserve = 0 }) {
68
+ const short = viewH <= SHORT_VIEW_H && viewW > viewH;
69
+ const layout = {
70
+ bigPx: BIG_PX,
71
+ bigOneLine: false,
72
+ bigMaxWidth: null,
73
+ textLines: short ? SHORT_TEXT_LINES : null,
74
+ maxHeight: short ? Math.round(viewH * SHORT_HUD_SHARE) : null,
75
+ };
76
+ if (!big || !text) {
77
+ return layout;
78
+ }
79
+ const avail = Math.max(
80
+ Math.round(viewW * HUD_MIN_SHARE),
81
+ Math.min(Math.round(viewW * HUD_MAX_SHARE), Math.floor(viewW - 2 * reserve)),
82
+ );
83
+ const base = short ? BIG_WITH_TEXT_SHORT_PX : BIG_WITH_TEXT_PX;
84
+ const ems = bigEms(big);
85
+ layout.bigPx = ems * base <= avail ? base : Math.max(BIG_MIN_PX, Math.floor(avail / ems));
86
+ layout.bigOneLine = true;
87
+ layout.bigMaxWidth = avail;
88
+ return layout;
89
+ }
90
+
91
+ // A line drawn at `px` that needs `needed` pixels where it has `room`: the
92
+ // size at which it fits, never below the least.
93
+ export function fitPx(px, room, needed) {
94
+ if (!(needed > room) || !(room > 0)) {
95
+ return px;
96
+ }
97
+ return Math.max(BIG_MIN_PX, Math.min(px, Math.floor((px * room) / needed)));
98
+ }
99
+
100
+ // The box of something the chrome hangs from a point: `anchor` "bottom" is
101
+ // centred over the point with its bottom edge on it (a label, a chip over a
102
+ // friend), "center" is centred on it (a chip at the screen's edge).
103
+ export function boxAt(x, y, width, height, anchor = 'bottom') {
104
+ const top = anchor === 'center' ? y - height / 2 : y - height;
105
+ return { left: x - width / 2, right: x + width / 2, top, bottom: top + height };
106
+ }
107
+
108
+ // How far down `box` must move to lie clear under the HUD's box (`hud`, or
109
+ // null while no HUD is drawn): 0 when it is beside it, over it or already
110
+ // under it. Both are { left, right, top, bottom } in CSS pixels. Nothing is
111
+ // pushed off the bottom of the view (`viewH`, when given): a box too tall to
112
+ // lie both under the HUD and whole on screen moves only as far as keeps its
113
+ // bottom edge in view, the HUD left over its top, and one already at or past
114
+ // the bottom is not moved.
115
+ export function pushBelow(box, hud, gap = HUD_GAP, viewH = Infinity) {
116
+ if (!hud) {
117
+ return 0;
118
+ }
119
+ if (box.right <= hud.left || box.left >= hud.right) {
120
+ return 0;
121
+ }
122
+ if (box.bottom <= hud.top || box.top >= hud.bottom + gap) {
123
+ return 0;
124
+ }
125
+ return Math.max(0, Math.min(hud.bottom + gap - box.top, viewH - box.bottom));
126
+ }
@@ -9,6 +9,10 @@
9
9
  export const deck = {
10
10
  // `hub.tiers`: a cheaper picture on a slow device (`tiers.js`).
11
11
  tiers: false,
12
+ // `hub.cues` (v1.23): a town says more about the friends and party a player
13
+ // came with -- a friend already here, who reads chat, the door a friend
14
+ // went to, who joined the party. Every such branch asks this first.
15
+ cues: false,
12
16
  };
13
17
 
14
18
  export const hub = {
@@ -26,6 +30,11 @@ export const hub = {
26
30
  wearing: new Map(),
27
31
  wearAll: new Map(),
28
32
  wearMe: new Map(),
33
+ // playerId -> a wearable the place shows on that player instead of their
34
+ // own until it clears it (`hubTry`, v1.28 `hub.tryOn`), with this player's
35
+ // own (`hub-try`) over it; both empty in a place that never tries one on
36
+ tryAll: new Map(),
37
+ tryMe: new Map(),
29
38
  // the declared HUD, this player's override of it, and the page time each
30
39
  // arrived (a timer is read against the simulation clock below)
31
40
  hud: null,
@@ -81,6 +90,9 @@ export const hub = {
81
90
  npcEvents: [],
82
91
  // the place's answers about a hold (`grab.js`, v1.3): hub-grab-deny, hub-drop
83
92
  grabEvents: [],
93
+ // actor id -> the only player who may grab it (`hubKept`, v1.27); empty in
94
+ // a place that keeps nothing
95
+ keptFor: new Map(),
84
96
  // Idle play (v1.6, `idle.js`, `sound.js`): playerId -> { id, at, until } (an
85
97
  // emote playing, simulation ms), playerId -> the seat actor they sit on,
86
98
  // actorId -> { file, volume, radius, since } (a loop playing there), and the
@@ -103,6 +115,8 @@ export function clear() {
103
115
  hub.wearing.clear();
104
116
  hub.wearAll.clear();
105
117
  hub.wearMe.clear();
118
+ hub.tryAll.clear();
119
+ hub.tryMe.clear();
106
120
  hub.hud = null;
107
121
  hub.hudMe = null;
108
122
  hub.hidden.clear();
@@ -123,6 +137,7 @@ export function clear() {
123
137
  hub.saidEvents.length = 0;
124
138
  hub.wentEvents.length = 0;
125
139
  hub.grabEvents.length = 0;
140
+ hub.keptFor = new Map();
126
141
  hub.emotes = {};
127
142
  hub.seated = {};
128
143
  hub.sounds = {};
@@ -133,6 +148,13 @@ export function clear() {
133
148
  hub.commands.length = 0;
134
149
  }
135
150
 
151
+ // What each player is drawn wearing: their own wearable, or the one the place
152
+ // is showing on them (v1.28), which wins while it is up and leaves the saved
153
+ // one to be drawn again when it goes.
154
+ export function wearingNow() {
155
+ return new Map([...hub.wearAll, ...hub.wearMe, ...hub.tryAll, ...hub.tryMe]);
156
+ }
157
+
136
158
  export function avatarOf(playerId) {
137
159
  return hub.avatars.has(playerId) ? hub.avatars.get(playerId) : null;
138
160
  }
@@ -15,6 +15,10 @@
15
15
  // saves the traversal and matrix work as well as the draw. Nothing here
16
16
  // touches colliders, physics, the network, labels or anything another player
17
17
  // sees: it is this client's picture only.
18
+ // A deck may mark an actor in its scene file `"tier": "keep"` (beside `id`,
19
+ // not a component): it is drawn on every tier, whatever it would be called
20
+ // here -- a place's ground is often plain Transform/Shape/Model, and a place
21
+ // drawn without it stands on nothing.
18
22
  //
19
23
  // Where a device starts: low if it was found slow here in the last week
20
24
  // (remembered in localStorage), or its GPU or memory says it is a weak phone;
@@ -169,6 +173,11 @@ const GAMEPLAY = /^Hub|^Town|^Body$|^Ball$|^Player$/;
169
173
  const DECOR = new Set(['Transform', 'Shape', 'Model']);
170
174
  const SCENERY = new Set(['Transform', 'Shape', 'Model', 'Solid', 'Lookable']);
171
175
 
176
+ // The mark a scene file puts on an actor (`"tier": "keep"`, a field of the
177
+ // actor beside `id`) to have it drawn on every tier whatever `classify` says
178
+ // of it. Any other value is no mark.
179
+ export const KEEP = 'keep';
180
+
172
181
  export function classify(components) {
173
182
  const names = Object.keys(components ?? {});
174
183
  // physics-3d makes an actor's collider block when it has `Solid` or its
@@ -219,6 +228,9 @@ export function makeLevers({ boundsOf = nodeBounds } = {}) {
219
228
  // (never a player's avatar, what they wear, or what the place spawned).
220
229
  authored: null,
221
230
  authoredFor: null,
231
+ // Of those, the ids the scene file marks `"tier": "keep"`: never hidden.
232
+ // null when the file marks none.
233
+ kept: null,
222
234
  seen: { path: undefined, nodes: null, size: -1 },
223
235
  // The entries taken out on the last look, [actor id, entry]: the engine
224
236
  // replaces an entry in the same map when an actor's look changes
@@ -236,6 +248,7 @@ export function makeLevers({ boundsOf = nodeBounds } = {}) {
236
248
  lv.removed = new WeakMap();
237
249
  lv.authored = null;
238
250
  lv.authoredFor = null;
251
+ lv.kept = null;
239
252
  lv.seen = { path: undefined, nodes: null, size: -1 };
240
253
  lv.watch = [];
241
254
  lv.nextAt = 0;
@@ -259,12 +272,23 @@ export function makeLevers({ boundsOf = nodeBounds } = {}) {
259
272
  return lv.authored;
260
273
  }
261
274
  lv.authoredFor = scene.data;
275
+ const ids = new Set();
276
+ let kept = null;
262
277
  try {
263
278
  const file = scene.readFromFile(scene.scenePath);
264
- lv.authored = new Set((file?.actors ?? []).map((a) => a.id));
279
+ for (const a of file?.actors ?? []) {
280
+ ids.add(a.id);
281
+ if (a.tier === KEEP) {
282
+ (kept ??= new Set()).add(a.id);
283
+ }
284
+ }
265
285
  } catch {
266
- lv.authored = new Set();
286
+ // A scene that cannot be read has no authored actors: nothing is hidden.
287
+ ids.clear();
288
+ kept = null;
267
289
  }
290
+ lv.authored = ids;
291
+ lv.kept = kept;
268
292
  return lv.authored;
269
293
  };
270
294
 
@@ -310,7 +334,7 @@ export function makeLevers({ boundsOf = nodeBounds } = {}) {
310
334
  continue;
311
335
  }
312
336
  let want = false;
313
- if (authored?.has(id)) {
337
+ if (authored?.has(id) && !lv.kept?.has(id)) {
314
338
  const kind = classify(scene.getActor?.(id)?.components);
315
339
  want =
316
340
  kind === 'decor' ||
@@ -342,18 +366,19 @@ export function makeLevers({ boundsOf = nodeBounds } = {}) {
342
366
 
343
367
  // Each frame while low. The nodes are looked at every CHECK_EVERY_MS, and at
344
368
  // 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
369
+ // or put back, actors come or gone, the scene loaded again: new scene data
370
+ // may carry other `tier` marks over the same nodes). `actors` false (a game reached through
346
371
  // a door: another deck's content) puts every node back and keeps only the
347
372
  // shadow lever.
348
373
  lv.apply = (world, scene, now, { actors = true } = {}) => {
349
374
  adopt(world);
350
375
  lv.active = true;
351
376
  const seen = lv.seen;
352
- const changed = world.scenePath !== seen.path || world.nodes !== seen.nodes || world.nodes.size !== seen.size || watchBroken(world);
377
+ const changed = world.scenePath !== seen.path || world.nodes !== seen.nodes || world.nodes.size !== seen.size || scene.data !== seen.data || watchBroken(world);
353
378
  if (!changed && now < lv.nextAt && actors === seen.actors) {
354
379
  return;
355
380
  }
356
- lv.seen = { path: world.scenePath, nodes: world.nodes, size: world.nodes.size, actors };
381
+ lv.seen = { path: world.scenePath, nodes: world.nodes, size: world.nodes.size, data: scene.data, actors };
357
382
  lv.nextAt = now + CHECK_EVERY_MS;
358
383
  pass(world, scene, actors);
359
384
  };
@@ -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.
@@ -35,6 +35,12 @@ const PING_MS = 10000;
35
35
  const ARRIVAL_AFTER_MS = 8000;
36
36
  // How long a friend who left the roster can still be named.
37
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];
38
44
  // The part of the screen an edge pointer keeps to: clear of the HUD above and
39
45
  // the buttons below.
40
46
  const EDGE = { side: 46, top: 84, bottom: 150 };
@@ -75,6 +81,9 @@ export function makeTogether() {
75
81
  const recent = new Map();
76
82
  let seenSince = null;
77
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();
78
87
  // Where the last ask left off, when there are more players to ask about
79
88
  // than one ask covers: they are worked through in turn.
80
89
  let turn = 0;
@@ -127,6 +136,15 @@ export function makeTogether() {
127
136
  if (!seen.has(playerId)) {
128
137
  seen.set(playerId, { late: now - seenSince > ARRIVAL_AFTER_MS, told: false });
129
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
+ }
130
148
  }
131
149
  for (const [playerId, t] of tracked) {
132
150
  const first = seen.get(playerId);
@@ -134,6 +152,11 @@ export function makeTogether() {
134
152
  first.told = true;
135
153
  if (first.late) {
136
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 });
137
160
  }
138
161
  }
139
162
  }
@@ -180,8 +203,50 @@ export function makeTogether() {
180
203
  recent.clear();
181
204
  seenSince = null;
182
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;
183
231
  },
184
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
+ }
245
+ },
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
+
185
250
  // The friends and party members who arrived since this was last asked.
186
251
  arrivals: () => arrived.splice(0),
187
252
 
@@ -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
  }