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
@@ -25,8 +25,16 @@ const RATE_BURST = 6;
25
25
  const RATE_EVERY_MS = 1000;
26
26
  // A member who left the layer (through a door) is kept this long.
27
27
  const AWAY_MS = 30 * 60 * 1000;
28
- // How far from a pad's middle party members are put, in a ring.
29
- const PAD_RING_M = 0.9;
28
+ // How far from a pad's middle party members are put, in a ring (at most half
29
+ // the pad's radius). Six spots round it are this far apart.
30
+ export const PAD_RING_M = 1.2;
31
+ // A spot on a pad is taken when an NPC or a player stands within this of it.
32
+ // More than two bodies' width (2 x 0.45 m): a player put down touching another
33
+ // is pushed apart by physics, and the one already there can slide off the pad
34
+ // and stop counting, so a pad that needs both never fires.
35
+ export const PAD_CLEAR_M = 1.1;
36
+ // A standing body's radius.
37
+ const BODY_RADIUS_M = 0.45;
30
38
 
31
39
  const DEG = Math.PI / 180;
32
40
 
@@ -247,7 +255,7 @@ export function toPad(d, playerId, door, n) {
247
255
  return false;
248
256
  }
249
257
  const facing = ((door.t.rotationY ?? 0) + 180) * DEG;
250
- const at = padPoint(h, door, n) ?? d.front(door);
258
+ const at = padPoint(h, door, n, playerId) ?? d.front(door);
251
259
  d.placeAt(h, playerId, { x: at.x, y: 1.2, z: at.z }, facing);
252
260
  h.session?.send(playerId, { k: 'hub-jumped', facing });
253
261
  return true;
@@ -255,15 +263,59 @@ export function toPad(d, playerId, door, n) {
255
263
 
256
264
  // Where the `n`th player sent to a door stands: on its pad, a ring about the
257
265
  // middle, or null for a door with no pad.
258
- export function padPoint(h, door, n) {
266
+ export function padPoint(h, door, n, moverId = null) {
259
267
  const pad = h.actors.find((a) => a.components.HubPad?.door === door.id && a.components.Transform && !h.hidden.has(a.id));
260
268
  if (!pad) {
261
269
  return null;
262
270
  }
263
271
  const t = pad.components.Transform;
264
272
  const radius = Math.min(PAD_RING_M, (pad.components.HubPad.radius ?? pad.components.Shape?.radius ?? 2.3) * 0.5);
265
- const angle = (n * 2 * Math.PI) / 6 + (door.t.rotationY ?? 0) * DEG;
266
- return { x: t.x + Math.sin(angle) * radius, z: t.z + Math.cos(angle) * radius };
273
+ // The `n`th spot of the ring, or the next one round that nobody stands on:
274
+ // a body put down inside an NPC or another player is pushed out by physics,
275
+ // off the pad, and the pad then never counts them.
276
+ const taken = [];
277
+ for (const npc of h.npcs?.list?.() ?? []) {
278
+ if (npc?.at) {
279
+ taken.push(npc.at);
280
+ }
281
+ }
282
+ // The mover's own body is not in its way.
283
+ for (const [playerId, body] of h.sim?.players ?? []) {
284
+ if (playerId !== moverId && body?.at) {
285
+ taken.push(body.at);
286
+ }
287
+ }
288
+ const padRadius = pad.components.HubPad.radius ?? pad.components.Shape?.radius ?? 2.3;
289
+ // The ring first (from the `n`th spot round), then an outer ring between
290
+ // its spots where the pad is wide enough, then the middle; the first with
291
+ // PAD_CLEAR_M clear. When none is, the one with the most room.
292
+ const outer = Math.max(radius, padRadius - BODY_RADIUS_M - 0.1);
293
+ const ring = (r, i, turn = 0) => {
294
+ const angle = ((i + turn) * 2 * Math.PI) / 6 + (door.t.rotationY ?? 0) * DEG;
295
+ return { x: t.x + Math.sin(angle) * r, z: t.z + Math.cos(angle) * r };
296
+ };
297
+ const spots = [];
298
+ for (let i = 0; i < 6; i += 1) {
299
+ spots.push(ring(radius, n + i));
300
+ }
301
+ if (outer > radius + 0.3) {
302
+ for (let i = 0; i < 6; i += 1) {
303
+ spots.push(ring(outer, n + i, 0.5));
304
+ }
305
+ }
306
+ spots.push({ x: t.x, z: t.z });
307
+ const room = (at) => Math.min(Infinity, ...taken.map((other) => Math.hypot(other.x - at.x, other.z - at.z)));
308
+ let best = null;
309
+ for (const at of spots) {
310
+ const r = room(at);
311
+ if (r >= PAD_CLEAR_M) {
312
+ return at;
313
+ }
314
+ if (!best || r > best.r) {
315
+ best = { at, r };
316
+ }
317
+ }
318
+ return best.at;
267
319
  }
268
320
 
269
321
  // Each tick: what has run out goes, and each player in a town is told their
@@ -353,6 +405,23 @@ export function coming(h, doorId) {
353
405
  return n;
354
406
  }
355
407
 
408
+ // Cues (v1.23): the door of a player's live party proposal -- one they made,
409
+ // or one they were asked about and have not answered -- or null.
410
+ export function proposalDoor(h, playerId) {
411
+ const partyId = h.party.of.get(keyOf(h, playerId));
412
+ const proposal = partyId ? h.party.proposals.get(partyId) : null;
413
+ if (!proposal || proposal.until <= Date.now()) {
414
+ return null;
415
+ }
416
+ return proposal.by === playerId || proposal.asked.has(playerId) ? proposal.door : null;
417
+ }
418
+
419
+ // `hub.partyOf(playerId)`: the other members of their party who are here.
420
+ export function matesHere(h, playerId) {
421
+ const partyId = h.party.of.get(keyOf(h, playerId));
422
+ return partyId ? present(h, partyId).filter((id) => id !== playerId) : [];
423
+ }
424
+
356
425
  // Whether two players here are in one party (Join goes to a party member).
357
426
  export function together(h, a, b) {
358
427
  const partyId = h.party.of.get(keyOf(h, a));
@@ -29,7 +29,10 @@ export function newSayState() {
29
29
  return { sayRate: new Map() };
30
30
  }
31
31
 
32
- // `d` is the hub layer's own: `{ h, blockedPairs, jumpDoors }`.
32
+ // `d` is the hub layer's own: `{ h, blockedPairs, jumpDoors }`, and under cues
33
+ // (v1.23, a deck whose manifest sets `cues`) `{ cues: true, goDoors(h),
34
+ // proposalDoor(h, playerId) }`: the doors the Doors list may go to, and the
35
+ // door of the player's live party proposal.
33
36
  export function message(d, player, data) {
34
37
  const h = d.h;
35
38
  const playerId = player?.playerId;
@@ -52,7 +55,10 @@ export function message(d, player, data) {
52
55
  best = { dist, id: door.id };
53
56
  }
54
57
  }
55
- out.door = best?.id ?? null;
58
+ // Cues: the door their party is going to comes first, while that
59
+ // proposal is open and the place still has the door.
60
+ const proposed = d.cues ? d.proposalDoor(h, playerId) : null;
61
+ out.door = (proposed && d.goDoors(h).some((door) => door.id === proposed) ? proposed : null) ?? best?.id ?? null;
56
62
  }
57
63
  } else {
58
64
  return;
@@ -8,6 +8,15 @@
8
8
  // The snapshot's `hubWear` is playerId -> wearable for helloed players who
9
9
  // wear one, without anyone a block hides from someone here: theirs go to each
10
10
  // player who may see them alone (`hub-wear`), as their avatars do (`hub-av`).
11
+ //
12
+ // A try-on (v1.28, `hub.tryOn(playerId, spec | null)`) is a wearable this
13
+ // place shows on a player instead of their own until it clears it: a hat on a
14
+ // shop's mat, a costume for a visit. It is kept here only (`trying`), never
15
+ // sent to ghost and never read back, so what the player owns is untouched;
16
+ // clients are told in `hubTry` and `hub-try`, the shape of `hubWear` and
17
+ // `hub-wear`, and draw it over the saved one. `trying` is empty until a place
18
+ // calls `hub.tryOn`, and every branch for it asks that first: a place that
19
+ // never does sends what it always did.
11
20
 
12
21
  import { warnOnce } from './actors.js';
13
22
 
@@ -27,6 +36,14 @@ export function newWearState() {
27
36
  wearSent: new Map(),
28
37
  // playerId -> JSON of the wearables last sent to that player alone
29
38
  sentWearMe: new Map(),
39
+ // playerId -> the wearable this place shows on them instead of their own
40
+ // (v1.28 `hub.tryOn`); empty in a place that never calls it
41
+ trying: new Map(),
42
+ // playerId -> JSON of the try-ons last sent to that player alone, or
43
+ // `RESEND` for a client that joined afresh and is to be told again
44
+ sentTryMe: new Map(),
45
+ // whether any player was ever sent try-ons alone (`hub-try`)
46
+ tryAloneUsed: false,
30
47
  };
31
48
  }
32
49
 
@@ -52,17 +69,27 @@ export function cleanWearable(spec) {
52
69
  return { kind, color: color.toLowerCase(), rarity };
53
70
  }
54
71
 
72
+ // What `hub.wear` and `hub.tryOn` (`api`) take: the wearable, `null` to clear,
73
+ // or `undefined` for one refused and logged once -- a player who is not here,
74
+ // or a spec that is not a wearable.
75
+ function asked(h, api, playerId, spec) {
76
+ if (typeof playerId !== 'string' || !(h.players.has(playerId) || h.sim?.players.has(playerId))) {
77
+ warnOnce(h, `${api}:${playerId}`, `hub.${api}: "${playerId}" is not a player here`);
78
+ return undefined;
79
+ }
80
+ const clean = cleanWearable(spec === undefined ? null : spec);
81
+ if (clean === undefined) {
82
+ warnOnce(h, `${api}:spec`, `hub.${api}: a wearable is { kind, color: "#rrggbb", rarity: 0-9 } or null`);
83
+ }
84
+ return clean;
85
+ }
86
+
55
87
  // `hub.wear(playerId, spec | null)`: true when it took. `platform` is the
56
88
  // host's hub ops or null; `userId` the player's, or null for a guest, whose
57
89
  // wearable is this session's alone; `refused` logs a refusal once.
58
90
  export function wear(h, playerId, spec, { platform, userId, refused }) {
59
- if (typeof playerId !== 'string' || !(h.players.has(playerId) || h.sim?.players.has(playerId))) {
60
- warnOnce(h, `wear:${playerId}`, `hub.wear: "${playerId}" is not a player here`);
61
- return false;
62
- }
63
- const clean = cleanWearable(spec === undefined ? null : spec);
91
+ const clean = asked(h, 'wear', playerId, spec);
64
92
  if (clean === undefined) {
65
- warnOnce(h, 'wear:spec', 'hub.wear: a wearable is { kind, color: "#rrggbb", rarity: 0-9 } or null');
66
93
  return false;
67
94
  }
68
95
  if (clean) {
@@ -84,6 +111,22 @@ export function wear(h, playerId, spec, { platform, userId, refused }) {
84
111
  return true;
85
112
  }
86
113
 
114
+ // `hub.tryOn(playerId, spec | null)` (v1.28): true when it took. Shown on the
115
+ // player in this place until `null`, their leaving or the place's end; the
116
+ // wearable they own (`wearing`, ghost's) is neither read nor written.
117
+ export function tryOn(h, playerId, spec) {
118
+ const clean = asked(h, 'tryOn', playerId, spec);
119
+ if (clean === undefined) {
120
+ return false;
121
+ }
122
+ if (clean) {
123
+ h.trying.set(playerId, clean);
124
+ } else {
125
+ h.trying.delete(playerId);
126
+ }
127
+ return true;
128
+ }
129
+
87
130
  // Ghost's word on a player's wearable, from the arrival read: kept unless the
88
131
  // game set one since the read began. A ghost older than wearables says
89
132
  // nothing, which changes nothing.
@@ -106,8 +149,17 @@ export function applyRead(h, playerId, wearRev, read) {
106
149
  // Everyone's wearables, for everyone: helloed players only, without the ones
107
150
  // a block hides from someone here.
108
151
  export function wearingOf(h, hidden) {
152
+ return shownOf(h, h.wearing, hidden);
153
+ }
154
+
155
+ // Everyone's try-ons (v1.28), for everyone, under the same two rules.
156
+ export function tryingOf(h, hidden) {
157
+ return shownOf(h, h.trying, hidden);
158
+ }
159
+
160
+ function shownOf(h, worn, hidden) {
109
161
  const out = {};
110
- for (const [playerId, wearable] of h.wearing) {
162
+ for (const [playerId, wearable] of worn) {
111
163
  if (h.players.get(playerId)?.hello && !hidden?.has(playerId)) {
112
164
  out[playerId] = wearable;
113
165
  }
@@ -118,18 +170,69 @@ export function wearingOf(h, hidden) {
118
170
  // The wearables of players in a hidden pair, each to the players who may see
119
171
  // them, when they change. A session with no blocks sends nothing here.
120
172
  export function sendOverrides(h, hidden) {
173
+ sendAlone(h, hidden, h.wearing, h.sentWearMe, (out) => ({ k: 'hub-wear', hubWear: out }));
174
+ }
175
+
176
+ // The same for try-ons (v1.28, `hub-try`). Asked only once a place has
177
+ // called `hub.tryOn` (`tryOnUsed`).
178
+ export function sendTryOverrides(h, hidden) {
179
+ sendAlone(h, hidden, h.trying, h.sentTryMe, (out) => {
180
+ h.tryAloneUsed = true;
181
+ return { k: 'hub-try', hubTry: out };
182
+ });
183
+ }
184
+
185
+ // A client joined afresh (a hello that is not an avatar change: a reconnect,
186
+ // a reloaded page that resumed its player id). What it holds of `hub-try` is
187
+ // not known -- nothing, or a map from before it dropped out -- so it is sent
188
+ // the map as it is now, empty or not, on the next tick it has a body. Only in
189
+ // a place that has ever sent one: anywhere else no client holds any.
190
+ const RESEND = 'resend';
191
+
192
+ export function joinedAfresh(h, playerId) {
193
+ if (h.tryAloneUsed) {
194
+ h.sentTryMe.set(playerId, RESEND);
195
+ }
196
+ }
197
+
198
+ // A try-on is taken for a player with a body who has not said hello (as
199
+ // `hub.wear` takes one), and such a player's going is not a `forget`: drop
200
+ // what is kept for anyone who is neither known to the place nor in the
201
+ // simulation, so both maps hold only players who are here.
202
+ export function pruneTries(h) {
203
+ for (const kept of [h.trying, h.sentTryMe]) {
204
+ for (const playerId of [...kept.keys()]) {
205
+ if (!h.players.has(playerId) && !h.sim?.players.has(playerId)) {
206
+ kept.delete(playerId);
207
+ }
208
+ }
209
+ }
210
+ }
211
+
212
+ // The place is ending: every try-on with it.
213
+ export function endTries(h) {
214
+ h.trying.clear();
215
+ h.sentTryMe.clear();
216
+ }
217
+
218
+ // Whether this place has a try-on up, or has told someone of one alone.
219
+ export function tryOnUsed(h) {
220
+ return h.trying.size > 0 || h.sentTryMe.size > 0;
221
+ }
222
+
223
+ function sendAlone(h, hidden, worn, sent, message) {
121
224
  for (const playerId of h.sim.players.keys()) {
122
225
  const out = {};
123
226
  for (const shrouded of hidden?.keys() ?? []) {
124
- const wearable = h.wearing.get(shrouded);
227
+ const wearable = worn.get(shrouded);
125
228
  if (wearable && h.players.get(shrouded)?.hello && !hidden.get(playerId)?.has(shrouded)) {
126
229
  out[shrouded] = wearable;
127
230
  }
128
231
  }
129
232
  const json = JSON.stringify(out);
130
- if ((h.sentWearMe.get(playerId) ?? '{}') !== json) {
131
- h.sentWearMe.set(playerId, json);
132
- h.session?.send(playerId, { k: 'hub-wear', hubWear: out });
233
+ if ((sent.get(playerId) ?? '{}') !== json) {
234
+ sent.set(playerId, json);
235
+ h.session?.send(playerId, message(out));
133
236
  }
134
237
  }
135
238
  }
@@ -139,4 +242,6 @@ export function forget(h, playerId) {
139
242
  h.wearRev.delete(playerId);
140
243
  h.wearSent.delete(playerId);
141
244
  h.sentWearMe.delete(playerId);
245
+ h.trying.delete(playerId);
246
+ h.sentTryMe.delete(playerId);
142
247
  }