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
@@ -10,6 +10,10 @@
10
10
  // server bounds those reports as before. A throw is the velocity it is let go
11
11
  // with. The place's server hears each grab and throw (`hub-grab`) and may
12
12
  // refuse a grab or end a hold (`hub-grab-deny`, `hub-drop`).
13
+ //
14
+ // A place may ask for a steadier offer (v1.22, `grab.reach` and
15
+ // `grab.steady`): a longer reach, and an offer that does not flicker at its
16
+ // edges. A place that asks for neither runs exactly the code it always did.
13
17
 
14
18
  import { hub } from './state.js';
15
19
 
@@ -31,12 +35,59 @@ const THROW_UP = 0.25;
31
35
  // After a throw, nothing is offered for this long: the object is still in
32
36
  // front of the player as it leaves.
33
37
  const REGRAB_MS = 400;
38
+ // v1.22, only where the place sets `grab.reach` or `grab.steady`: the centre
39
+ // of an offered object is never farther than this from the player's centre.
40
+ // The server refuses a grab past its GRAB_REACH (4 m, from the poses it has,
41
+ // which trail a moving player's: a grab can be heard before that frame's
42
+ // movement report). With `reach` or `steady`, the client keeps inside it by
43
+ // what the player may travel in that time at the speed they are moving now
44
+ // (their body's own velocity, so an authored speed, a tune, a jump and a tune
45
+ // not yet applied are all counted) or the speed they are tuned to, whichever
46
+ // is more. It is a margin, not a promise: a grab the server still refuses is
47
+ // let go, as any refused grab is (`hub-grab-deny`).
48
+ export const OFFER_CENTRE_M = 3;
49
+ const WALK_SPEED = 6; // the walk preset's speed, metres a second
50
+ const POSE_LAG_S = 0.2;
51
+ const LAG_SLACK_M = 0.25;
52
+ // `speed` is the controller's own (the kit's applyTune has already multiplied
53
+ // it by the tune); without one, the walk preset's times the tune.
54
+ export function offerCap(velocity = null, tune = hub.tune, speed = null) {
55
+ const moving = velocity ? Math.hypot(velocity.x ?? 0, velocity.y ?? 0, velocity.z ?? 0) : 0;
56
+ const tuned = Number(speed) > 0 ? Number(speed) : WALK_SPEED * Math.max(1, Number(tune?.speed) || 1);
57
+ return Math.max(0, Math.min(OFFER_CENTRE_M, 4 - LAG_SLACK_M - Math.max(moving, tuned) * POSE_LAG_S));
58
+ }
59
+ // The cap for this player now: their body's velocity and authored speed.
60
+ function capFor(scene, local) {
61
+ const body = scene.physics3d?.bodyOf?.(local.id);
62
+ return offerCap(body?.linvel?.() ?? null, hub.tune, local.components?.Player?.speed);
63
+ }
64
+ // v1.22 `grab.steady`: once an object is offered it stays the one offered
65
+ // while its near side is within the reach and this much more, and within 90
66
+ // degrees of facing. Through a moment where it is not (its owner briefly
67
+ // someone else, a turn past the cone, a step back), the button stays up for
68
+ // STEADY_GRACE_MS; a press then waits up to PRESS_WAIT_MS for the object to
69
+ // be this client's again, and is dropped if it never is.
70
+ export const KEEP_MARGIN_M = 0.4;
71
+ const KEEP_COS = 0;
72
+ export const STEADY_GRACE_MS = 600;
73
+ export const PRESS_WAIT_MS = 500;
34
74
  // Where the walk preset's capsule stands (physics-3d `Player`): its radius and
35
75
  // height, and how far it hovers.
36
76
  const PLAYER = { radius: 0.35, height: 1.4, hover: 0.15 };
37
77
 
38
78
  export function makeGrab(chrome) {
39
- const t = { chrome, held: null, net: null, pressed: false, quietUntil: 0, lastAt: 0 };
79
+ const t = {
80
+ chrome,
81
+ held: null,
82
+ net: null,
83
+ pressed: false,
84
+ quietUntil: 0,
85
+ lastAt: 0,
86
+ // v1.22 `steady` only: the object kept on offer `{ id, okAt }`, and a press
87
+ // waiting for it `{ id, until }`.
88
+ offer: null,
89
+ waiting: null,
90
+ };
40
91
  chrome.onGrab?.(() => {
41
92
  t.pressed = true;
42
93
  });
@@ -77,6 +128,8 @@ function frame(t, scene, mp, local, idle) {
77
128
  // A new session: whatever was held belonged to the last one.
78
129
  t.net = net;
79
130
  t.held = null;
131
+ t.offer = null;
132
+ t.waiting = null;
80
133
  }
81
134
  const config = hub.place?.grab ?? null;
82
135
  const phys = scene.physics3d;
@@ -108,10 +161,22 @@ function frame(t, scene, mp, local, idle) {
108
161
  }
109
162
  }
110
163
  if (t.held) {
164
+ t.offer = null;
165
+ t.waiting = null;
111
166
  t.chrome.grabPrompt?.('Throw');
112
167
  return;
113
168
  }
114
- const target = can && now >= t.quietUntil ? grabbable(scene, mp, local) : null;
169
+ if (config?.steady === true) {
170
+ steadyFrame(t, scene, mp, local, config, net, can && now >= t.quietUntil, pressed, now, dt);
171
+ return;
172
+ }
173
+ const reach = config?.reach;
174
+ const target =
175
+ can && now >= t.quietUntil
176
+ ? reach === undefined
177
+ ? grabbable(scene, mp, local)
178
+ : grabbable(scene, mp, local, reach, FRONT_COS, capFor(scene, local))
179
+ : null;
115
180
  if (target && pressed) {
116
181
  const at = phys.bodyOf(target.id).translation();
117
182
  t.held = {
@@ -128,8 +193,75 @@ function frame(t, scene, mp, local, idle) {
128
193
  t.chrome.grabPrompt?.(target ? 'Grab' : null);
129
194
  }
130
195
 
131
- // The nearest object this client owns in front of the player, or null.
132
- function grabbable(scene, mp, local) {
196
+ // v1.22 `grab.steady`: the offer with hysteresis. Only an object that passes
197
+ // the full test this frame -- this client's own, in reach, in front -- is ever
198
+ // grabbed; the grace only keeps the button up and a press waiting.
199
+ function steadyFrame(t, scene, mp, local, config, net, active, pressed, now, dt) {
200
+ if (!active) {
201
+ t.offer = null;
202
+ t.waiting = null;
203
+ t.chrome.grabPrompt?.(null);
204
+ return;
205
+ }
206
+ const reach = config.reach ?? REACH_M;
207
+ const kept = t.offer;
208
+ // The kept object holds the offer on the wider test; when it fails this
209
+ // frame (or there is none), one passing the ordinary test takes over.
210
+ const ready =
211
+ (kept &&
212
+ grabbable(scene, mp, local, reach + KEEP_MARGIN_M, KEEP_COS, capFor(scene, local), kept.id)) ||
213
+ grabbable(scene, mp, local, reach, FRONT_COS, capFor(scene, local));
214
+ if (ready) {
215
+ t.offer = { id: ready.id, okAt: now };
216
+ } else if (kept && (now - kept.okAt > STEADY_GRACE_MS || !stillThere(scene, kept.id))) {
217
+ t.offer = null;
218
+ }
219
+ if (t.waiting && (now > t.waiting.until || !stillThere(scene, t.waiting.id))) {
220
+ t.waiting = null;
221
+ }
222
+ if (pressed && (ready || t.offer)) {
223
+ const id = ready?.id ?? t.offer.id;
224
+ t.waiting = { id, until: now + PRESS_WAIT_MS };
225
+ }
226
+ if (!ready || t.waiting?.id !== ready.id) {
227
+ t.chrome.grabPrompt?.(t.offer || t.waiting ? 'Grab' : null);
228
+ return;
229
+ }
230
+ t.waiting = null;
231
+ t.offer = null;
232
+ const body = scene.physics3d.bodyOf(ready.id);
233
+ const at = body.translation();
234
+ t.held = {
235
+ id: ready.id,
236
+ extent: ready.extent,
237
+ half: ready.half,
238
+ at: { x: at.x, y: at.y, z: at.z },
239
+ };
240
+ net.send({ k: 'hub-grab', actor: ready.id, op: 'grab' });
241
+ carry(t, scene, body, local, config, dt);
242
+ t.chrome.grabPrompt?.('Throw');
243
+ }
244
+
245
+ // Whether an object kept on offer can still come back: in the scene, not
246
+ // hidden by the place. Anything else may change within a grace.
247
+ function stillThere(scene, id) {
248
+ return Boolean(scene.getActor(id)) && !hub.hidden.has(id);
249
+ }
250
+
251
+ // The nearest object this client owns in front of the player, or null. With
252
+ // no more arguments it is the test every place had before v1.22; `reach`,
253
+ // `cos` and `centre` (the farthest an object's centre may be from the
254
+ // player's) carry a place's `grab.reach` and `grab.steady`, and `only` limits
255
+ // the test to one object.
256
+ function grabbable(
257
+ scene,
258
+ mp,
259
+ local,
260
+ reach = REACH_M,
261
+ cos = FRONT_COS,
262
+ centre = Infinity,
263
+ only = null,
264
+ ) {
133
265
  const mine = mp.own?.mine;
134
266
  if (!mine?.size || typeof mp.claimable !== 'function') {
135
267
  return null;
@@ -140,7 +272,7 @@ function grabbable(scene, mp, local) {
140
272
  const fz = Math.cos(f);
141
273
  let best = null;
142
274
  for (const [id, at] of mp.claimable(scene)) {
143
- if (!mine.has(id) || hub.hidden.has(id)) {
275
+ if ((only !== null && id !== only) || !mine.has(id) || hub.hidden.has(id)) {
144
276
  continue;
145
277
  }
146
278
  const actor = scene.getActor(id);
@@ -151,10 +283,13 @@ function grabbable(scene, mp, local) {
151
283
  const dx = at.x - me.x;
152
284
  const dz = at.z - me.z;
153
285
  const d = Math.hypot(dx, dz);
154
- if (size.extent > MAX_EXTENT || d - size.extent > REACH_M || Math.abs(at.y - me.y) > RISE_M) {
286
+ if (size.extent > MAX_EXTENT || d - size.extent > reach || Math.abs(at.y - me.y) > RISE_M) {
287
+ continue;
288
+ }
289
+ if (d > 0.01 && (dx * fx + dz * fz) / d < cos) {
155
290
  continue;
156
291
  }
157
- if (d > 0.01 && (dx * fx + dz * fz) / d < FRONT_COS) {
292
+ if (Math.hypot(d, at.y - me.y) > centre) {
158
293
  continue;
159
294
  }
160
295
  if (!best || d < best.d) {
@@ -9,10 +9,11 @@ import * as Avatars from '@imports/castle.multiplayer-3d/code/client/avatars.js'
9
9
  import * as Avatar from './avatar.js';
10
10
  import * as Cabinets from './cabinets.js';
11
11
  import { firstJoinFor } from './firstJoin.js';
12
- import { hub } from './state.js';
12
+ import { deck, hub } from './state.js';
13
13
 
14
14
  // Root-anchored: the deck's own castle.json once this kit is imported.
15
15
  const castleJson = Object.values(import.meta.glob('/castle.json', { eager: true, import: 'default' }))[0] ?? {};
16
+ deck.tiers = castleJson?.hub?.tiers === true;
16
17
 
17
18
  let files = null;
18
19
  // The scene and multiplayer system of the newest snapshot, for a message that
@@ -95,6 +96,11 @@ export default {
95
96
  Object.assign(hub.whereAt, msg.at && typeof msg.at === 'object' ? msg.at : {});
96
97
  hub.whereAtAt = performance.now();
97
98
  break;
99
+ case 'hub-went':
100
+ if (hub.wentEvents.length < 32 && Array.isArray(msg.players) && typeof msg.door === 'string') {
101
+ hub.wentEvents.push(msg);
102
+ }
103
+ break;
98
104
  case 'hub-said':
99
105
  if (hub.saidEvents.length < 64) {
100
106
  hub.saidEvents.push(msg);
@@ -3,6 +3,14 @@
3
3
  // (`code/systems/hub.js`). One module instance per page; `clear` runs on every
4
4
  // session switch.
5
5
 
6
+ // What the deck's own castle.json asks of the hub client, read once by the
7
+ // hook layer (`hookLayer.js`) as the page loads. Not cleared on a session
8
+ // switch: it is the page's deck, whichever place is playing.
9
+ export const deck = {
10
+ // `hub.tiers`: a cheaper picture on a slow device (`tiers.js`).
11
+ tiers: false,
12
+ };
13
+
6
14
  export const hub = {
7
15
  // the place's public manifest (`hubPlace`), or null before the first snapshot
8
16
  place: null,
@@ -46,6 +54,9 @@ export const hub = {
46
54
  // what players said since the hub system last looked (`hub-said`, v1.11):
47
55
  // { player, r } (a reaction) or { player, p, door } (a phrase)
48
56
  saidEvents: [],
57
+ // who went through which door since the hub system last looked
58
+ // (`hub-went`, v1.15): { players, door }
59
+ wentEvents: [],
49
60
  // playerId -> { text, color }: the tag the place put over that player
50
61
  marks: {},
51
62
  // copy id -> { from, x, y, z, rotationY }: actors the place spawned, and
@@ -57,7 +68,7 @@ export const hub = {
57
68
  // player ids the place is holding still
58
69
  held: new Set(),
59
70
  // actor id -> { text, style }, and this player's own over them (an NPC's
60
- // reply to the audience it was admitted for)
71
+ // reply to this player)
61
72
  labels: {},
62
73
  labelsMe: {},
63
74
  // the facing the place gave this player on arrival, or null until it has,
@@ -110,6 +121,7 @@ export function clear() {
110
121
  hub.labelsMe = {};
111
122
  hub.npcEvents.length = 0;
112
123
  hub.saidEvents.length = 0;
124
+ hub.wentEvents.length = 0;
113
125
  hub.grabEvents.length = 0;
114
126
  hub.emotes = {};
115
127
  hub.seated = {};
@@ -5,9 +5,11 @@
5
5
  // mind or to the place's authored lines.
6
6
  //
7
7
  // With a mind, the player's words are a character turn on the world's socket
8
- // (`requestCharacterTurn`): the server admits it or refuses it, and the reply
9
- // is read back with a status read, which is also when the server shows it
10
- // over the NPC to the turn's audience. Without one, the words are a plain
8
+ // (`requestCharacterTurn`) that names no listener: the server admits it or
9
+ // refuses it, and the reply is read back with a status read, which is also
10
+ // when the server shows it over the NPC to this player alone. What a player
11
+ // says to a character and what it answers is theirs: nobody else is sent
12
+ // either. Without one, the words are a plain
11
13
  // message and the place answers. The place's notes for the character (what it
12
14
  // knows right now: a ledger, a pad) come from the server as a prefix sent
13
15
  // ahead of the player's words; the server admits only notes it issued, and the
@@ -19,7 +21,6 @@ const TALK_RANGE_M = 3.5;
19
21
  const POLL_MS = 1200;
20
22
  const POLL_LIMIT_MS = 90_000;
21
23
  const INPUT_MAX = 280;
22
- const LISTENERS_MAX = 7;
23
24
  // A turn refused for stale notes is sent again once, with the newest.
24
25
  const NOTES_RETRY_MS = 400;
25
26
  // Lines the place said because the mind was not answering, and how the panel
@@ -41,7 +42,7 @@ const newId = () => globalThis.crypto?.randomUUID?.() ?? `${Date.now().toString(
41
42
  export function makeTalk(chrome) {
42
43
  const t = {
43
44
  chrome,
44
- // { npc, name, mind, character, disclosure, hearRange } while a panel is open
45
+ // { npc, name, mind, character, prefix } while a panel is open
45
46
  open: null,
46
47
  asking: null,
47
48
  net: null,
@@ -76,8 +77,8 @@ function frame(t, scene, mp, idle) {
76
77
  for (const msg of hub.npcEvents.splice(0)) {
77
78
  if (msg.k === 'hub-npc') {
78
79
  if (msg.open) {
79
- t.open = { npc: msg.npc, name: msg.name, mind: Boolean(msg.mind), character: msg.character ?? null, disclosure: msg.disclosure, hearRange: msg.hearRange ?? 6, prefix: null };
80
- t.chrome.openTalk(msg.name, msg.disclosure === 'world' ? 'Everyone nearby hears this' : 'Players nearby can hear');
80
+ t.open = { npc: msg.npc, name: msg.name, mind: Boolean(msg.mind), character: msg.character ?? null, prefix: null };
81
+ t.chrome.openTalk(msg.name);
81
82
  if (t.open.mind) {
82
83
  registerParticipant(t, net);
83
84
  }
@@ -103,7 +104,6 @@ function frame(t, scene, mp, idle) {
103
104
  const near = idle && net && !t.open ? nearestNpc(scene) : null;
104
105
  t.near = near;
105
106
  t.chrome.talkPrompt(near ? near.name : null);
106
- t.scene = scene;
107
107
  }
108
108
 
109
109
  function nearestNpc(scene) {
@@ -183,7 +183,8 @@ async function turn(t, open, text) {
183
183
  operationId,
184
184
  characterId: open.character,
185
185
  input: `${open.prefix ?? ''}${text}`,
186
- audience: { disclosure: open.disclosure, playerIds: listeners(t, open), characterIds: [] },
186
+ // Nobody else hears a player's conversation with a character.
187
+ audience: { disclosure: 'participants', playerIds: [], characterIds: [] },
187
188
  });
188
189
  };
189
190
  try {
@@ -230,30 +231,3 @@ async function turn(t, open, text) {
230
231
  t.waiting = false;
231
232
  }
232
233
  }
233
-
234
- // Who else hears this turn: the players drawn near the NPC now, at most seven.
235
- // The server checks the same distance against its own poses.
236
- function listeners(t, open) {
237
- const scene = t.scene;
238
- const mp = scene?.multiplayer;
239
- const npc = scene?.getActor(open.npc)?.components.Transform;
240
- if (!npc || !mp?.avatars?.actors) {
241
- return [];
242
- }
243
- const near = [];
244
- for (const [playerId, actorId] of mp.avatars.actors) {
245
- const at = scene.getActor(actorId)?.components.Transform;
246
- if (at) {
247
- const d = Math.hypot(at.x - npc.x, at.z - npc.z);
248
- // A little inside the server's range, so a player at its edge is not
249
- // the reason a turn is refused.
250
- if (d <= open.hearRange - 0.5) {
251
- near.push({ playerId, d });
252
- }
253
- }
254
- }
255
- return near
256
- .sort((a, b) => a.d - b.d)
257
- .slice(0, LISTENERS_MAX)
258
- .map((p) => p.playerId);
259
- }