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
@@ -11,6 +11,7 @@
11
11
  // simulating on the first frame after init resolves.
12
12
  import * as THREE from 'three';
13
13
  import * as RAPIER from '@dimforge/rapier3d-compat';
14
+ import { remember, sameAs } from '../engine3d/unchanged.js';
14
15
 
15
16
  const FIXED_STEP = 1 / 60;
16
17
  const MAX_STEPS_PER_FRAME = 4;
@@ -28,6 +29,24 @@ function ensureRapierInit() {
28
29
  return rapierInitPromise;
29
30
  }
30
31
 
32
+ // A scene that set `"engine": { "camera": { "follow": "rigid" } }`: the
33
+ // Player behavior asks for its camera (`scene.followCamera`: the player's id,
34
+ // the scene data it was asked for, and the function that places it) and it is
35
+ // placed here, last in the frame, once this frame's positions are written back
36
+ // and after the collision callbacks, so on the position the frame is drawn
37
+ // with (behaviors run before physics, and see last frame's). It is placed on a
38
+ // frame physics could not step too (rapier still loading). A request is used
39
+ // once, and only while the scene it was made for is loaded; a load and a
40
+ // dispose drop it (`reset`). Without the key nothing is asked for, and this
41
+ // reads one property and returns.
42
+ export function placeFollowCamera(scene) {
43
+ const request = scene.followCamera;
44
+ if (!request) return;
45
+ scene.followCamera = null;
46
+ if (scene.cameraFollowsRigidly !== true || request.data !== scene.data) return;
47
+ request.place(scene, request.id, request.dt);
48
+ }
49
+
31
50
  export function installSystem(runtime) {
32
51
  const system = new Physics3DSystem();
33
52
  runtime.registerSystem(system);
@@ -52,7 +71,7 @@ export class Physics3DSystem {
52
71
  void ensureRapierInit();
53
72
  }
54
73
 
55
- reset() {
74
+ reset(runtime) {
56
75
  // Rapier objects live in wasm -- dropping the JS reference leaks them, so
57
76
  // both the world and its event queue are freed explicitly. Runs on every
58
77
  // scene load (the runtime resets registered systems) and on dispose.
@@ -66,15 +85,34 @@ export class Physics3DSystem {
66
85
  this.enter.length = 0;
67
86
  this.exit.length = 0;
68
87
  this.acc = 0;
88
+ // What a skipping pass wrote down goes with the world it was written for.
89
+ this.remembered = null;
90
+ this.rememberedIn = null;
91
+ // A follow camera asked for before the load goes with the scene it was
92
+ // asked for.
93
+ if (runtime?.followCamera) runtime.followCamera = null;
69
94
  }
70
95
 
71
- dispose() {
72
- this.reset();
96
+ dispose(runtime) {
97
+ this.reset(runtime);
73
98
  }
74
99
 
75
100
  afterBehaviors(scene, dt) {
101
+ this.simulate(scene, dt);
102
+ placeFollowCamera(scene);
103
+ }
104
+
105
+ simulate(scene, dt) {
76
106
  if (!rapierReady) return;
77
107
  this.ensureWorld(scene);
108
+ if (scene.skipsUnchangedActors === true) {
109
+ this.afterBehaviorsSkipping(scene, dt);
110
+ return;
111
+ }
112
+ if (this.remembered) {
113
+ this.remembered = null;
114
+ this.rememberedIn = null;
115
+ }
78
116
  this.reconcileBodies(scene);
79
117
  this.pushTransforms(scene);
80
118
  this.step(dt);
@@ -131,6 +169,7 @@ export class Physics3DSystem {
131
169
  if (entry.collider) this.byCollider.delete(entry.collider.handle);
132
170
  this.world.removeRigidBody(entry.body);
133
171
  this.bodies.delete(actorId);
172
+ if (this.remembered) this.remembered.delete(entry);
134
173
  for (const key of [...this.touching]) {
135
174
  if (key.startsWith(`${actorId}|`) || key.endsWith(`|${actorId}`)) this.touching.delete(key);
136
175
  }
@@ -233,6 +272,102 @@ export class Physics3DSystem {
233
272
  }
234
273
  }
235
274
 
275
+ // `afterBehaviors` for a scene that set `"engine": { "skipUnchangedActors":
276
+ // true }`: the same bodies, pushes, steps and write-back, without building a
277
+ // body's signature or a fixed body's Transform key while what they are built
278
+ // from reads the same, and with the scene's actors listed once (nothing
279
+ // between here and the write-back runs deck code, so the list is the same
280
+ // for each pass). A body's type is still read from the body every frame.
281
+ //
282
+ // By the rule world3d.js states for its passes: nothing this method relies
283
+ // on from one statement to the next is trusted across code outside the
284
+ // engine. Between listing the actors and the write-back it calls only
285
+ // rapier (wasm; no hooks are handed to `step`), `scene.getActors`, and
286
+ // `JSON.stringify` on plain components. The one place it leaves the engine
287
+ // is `dispatchCallbacks`, last, after which nothing here is used again; a
288
+ // behavior that updates the scene from a collision callback starts a pass of
289
+ // its own from the top. What is kept between frames is read against the
290
+ // live components on every use. It is kept in `this.remembered`, a WeakMap
291
+ // from a body's entry to what the body was made from and where it was last
292
+ // pushed, started afresh with each rapier world and dropped on `reset`, on
293
+ // a frame without the key and, for one body, when the body is removed;
294
+ // nothing is kept on the entries, so an entry that is removed, or held by
295
+ // someone else, holds none of it.
296
+ afterBehaviorsSkipping(scene, dt) {
297
+ if (!this.remembered || this.rememberedIn !== this.world) {
298
+ this.remembered = new WeakMap();
299
+ this.rememberedIn = this.world;
300
+ }
301
+ const actors = scene.getActors();
302
+ this.reconcileUnchanged(actors);
303
+ const dynamic = this.pushUnchanged(actors);
304
+ this.step(dt);
305
+ // `writeBack` asks its scene for the actors; it is handed the dynamic
306
+ // ones, in the scene's order, which are the only ones it writes.
307
+ this.writeBack({ getActors: () => dynamic });
308
+ this.dispatchCallbacks(scene);
309
+ }
310
+
311
+ reconcileUnchanged(actors) {
312
+ // Each body whose actor is still here is marked with this pass's count; a
313
+ // body without the mark afterwards has lost its actor.
314
+ this.pass = (this.pass ?? 0) + 1;
315
+ const pass = this.pass;
316
+ let marked = 0;
317
+ for (const actor of actors) {
318
+ if (!actor.components.Transform) continue;
319
+ const entry = this.bodyFor(actor);
320
+ if (entry.pass !== pass) {
321
+ entry.pass = pass;
322
+ marked += 1;
323
+ }
324
+ }
325
+ if (marked === this.bodies.size) return;
326
+ for (const [id, entry] of [...this.bodies]) {
327
+ if (entry.pass !== pass) this.removeBody(id);
328
+ }
329
+ }
330
+
331
+ // The actor's body, made again when its signature differs. The signature is
332
+ // built and compared, as it is every frame elsewhere, only when something
333
+ // the body is made from reads differently; what it was made from is written
334
+ // down after the body is made.
335
+ bodyFor(actor) {
336
+ let entry = this.bodies.get(actor.id);
337
+ const made = entry && this.remembered.get(entry);
338
+ if (made && madeTheSame(made.madeFrom, actor)) return entry;
339
+ const sig = bodySignature(actor);
340
+ if (!entry || entry.sig !== sig) {
341
+ if (entry) this.removeBody(actor.id);
342
+ this.createBody(actor, sig);
343
+ entry = this.bodies.get(actor.id);
344
+ }
345
+ this.remembered.set(entry, { madeFrom: rememberMaking(actor), pushedAt: null });
346
+ return entry;
347
+ }
348
+
349
+ // `pushTransforms`, returning the actors whose body is dynamic.
350
+ pushUnchanged(actors) {
351
+ const dynamic = [];
352
+ for (const actor of actors) {
353
+ const entry = this.bodies.get(actor.id);
354
+ const t = actor.components.Transform;
355
+ if (!entry || !t) continue;
356
+ const { body } = entry;
357
+ const type = body.bodyType();
358
+ if (type === RAPIER.RigidBodyType.Dynamic) {
359
+ dynamic.push(actor);
360
+ } else if (type === RAPIER.RigidBodyType.KinematicPositionBased) {
361
+ body.setNextKinematicTranslation(placeOf(t));
362
+ body.setNextKinematicRotation(quatFromEuler(t));
363
+ } else if (type === RAPIER.RigidBodyType.Fixed && !alreadyPushed(entry, this.remembered.get(entry), t)) {
364
+ body.setTranslation(placeOf(t), false);
365
+ body.setRotation(quatFromEuler(t), false);
366
+ }
367
+ }
368
+ return dynamic;
369
+ }
370
+
236
371
  dispatchCallbacks(scene) {
237
372
  for (const [a, b] of this.enter) emitPair(scene, a, b, 'onCollisionEnter');
238
373
  for (const [a, b] of this.exit) emitPair(scene, a, b, 'onCollisionExit');
@@ -263,6 +398,50 @@ function transformKey(t) {
263
398
  return `${t.x},${t.y},${t.z},${t.rotationX},${t.rotationY},${t.rotationZ}`;
264
399
  }
265
400
 
401
+ function placeOf(t) {
402
+ return { x: t.x, y: t.y, z: t.z };
403
+ }
404
+
405
+ // What a body is made from, kept so the next frame can tell without building
406
+ // the signature whether it would come out the same (`unchanged.js`).
407
+ function rememberMaking(actor) {
408
+ const components = actor.components;
409
+ return { body: remember(components.Body), shape: remember(components.Shape), solid: isSolid(actor) };
410
+ }
411
+
412
+ function madeTheSame(madeFrom, actor) {
413
+ const components = actor.components;
414
+ return (
415
+ sameAs(components.Body, madeFrom.body) &&
416
+ sameAs(components.Shape, madeFrom.shape) &&
417
+ isSolid(actor) === madeFrom.solid
418
+ );
419
+ }
420
+
421
+ // Whether a fixed body is already where its Transform says. The six numbers it
422
+ // was last compared at are kept and compared as they are; anything that is not
423
+ // plainly the same six is settled by the key `pushTransforms` compares, and
424
+ // the entry's `pushedKey` is moved on when that differs.
425
+ function alreadyPushed(entry, made, t) {
426
+ const at = made.pushedAt;
427
+ if (
428
+ at &&
429
+ at.x === t.x &&
430
+ at.y === t.y &&
431
+ at.z === t.z &&
432
+ at.rotationX === t.rotationX &&
433
+ at.rotationY === t.rotationY &&
434
+ at.rotationZ === t.rotationZ
435
+ ) {
436
+ return true;
437
+ }
438
+ const key = transformKey(t);
439
+ const same = key === entry.pushedKey;
440
+ entry.pushedKey = key;
441
+ made.pushedAt = { x: t.x, y: t.y, z: t.z, rotationX: t.rotationX, rotationY: t.rotationY, rotationZ: t.rotationZ };
442
+ return same;
443
+ }
444
+
266
445
  function bodyDescFor(actor, t) {
267
446
  const opts = actor.components.Body;
268
447
  let desc;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "castle-web-cli",
3
- "version": "0.4.230",
3
+ "version": "0.4.231",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/castle-xyz/castle-experimental-web.git"